| | |
| | | int comport_send(comport_t *comport, char *buf, int send_bytes) |
| | | { |
| | | int rv = 0; |
| | | char *ptr, *end; |
| | | char *ptr, left_bytes; |
| | | int send = 0; |
| | | |
| | | if ( !comport || !buf || send_bytes<=0 ) |
| | |
| | | |
| | | //printf("Send %s with %d bytes.\n", buf, send_bytes); |
| | | |
| | | // Large data, then slice them and send |
| | | if (comport->frag_size < send_bytes) |
| | | { |
| | | ptr = buf; |
| | | end = buf + send_bytes; |
| | | |
| | | do |
| | | { |
| | | // Large than frag_size |
| | | if (comport->frag_size < (end - ptr)) |
| | | { |
| | | send = write(comport->fd, ptr, comport->frag_size); |
| | | if (0 >= send || comport->frag_size != send) |
| | | { |
| | | rv = -4; |
| | | goto CleanUp; |
| | | } |
| | | ptr += comport->frag_size; |
| | | } |
| | | else // Less than frag_size, maybe last fragmention. |
| | | { |
| | | send = write(comport->fd, ptr, (end - ptr)); |
| | | if (0 >= send || (end - ptr) != send) |
| | | { |
| | | rv = -4; |
| | | goto CleanUp; |
| | | } |
| | | ptr += (end - ptr); |
| | | } |
| | | } |
| | | while (ptr < end); |
| | | } |
| | | else // The send data is not large than a fragmention. |
| | | left_bytes = send_bytes; |
| | | ptr = buf; |
| | | |
| | | while( left_bytes > 0 ) |
| | | { |
| | | send = write(comport->fd, buf, send_bytes); |
| | | if (0 >= send || send_bytes != send) |
| | | { |
| | | rv = -5; |
| | | goto CleanUp; |
| | | } |
| | | /* Large data, then slice them to frag and send */ |
| | | send = left_bytes>comport->frag_size ? comport->frag_size : left_bytes; |
| | | |
| | | rv = write(comport->fd, ptr, send); |
| | | if( rv<0 ) |
| | | { |
| | | rv = -4; |
| | | goto CleanUp; |
| | | } |
| | | |
| | | left_bytes -= rv; |
| | | ptr += rv; |
| | | } |
| | | |
| | | rv = 0; |
| | | |
| | | CleanUp: |
| | | return rv; |
| | | } |