Skip to content

Commit

Permalink
inital work not tested to add a binary mode. still needs binary switc…
Browse files Browse the repository at this point in the history
…h work to potentially remove \!FOOBAR\r\n status messages
  • Loading branch information
f34rdotcom committed Mar 29, 2014
1 parent 47d0696 commit cf3f99d
Showing 1 changed file with 71 additions and 39 deletions.
110 changes: 71 additions & 39 deletions ser2sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ speed_spec speeds[] =


/* </Types and Constants> */
#if defined __FreeBSD__
typedef unsigned char byte_t;
#else
typedef char byte_t;
#endif

/* <Structures> */

Expand All @@ -138,6 +143,12 @@ typedef struct
void **table;
} fifo;

typedef struct
{
int size;
byte_t buffer[];
} fifo_buffer;

typedef struct
{
/* flags */
Expand All @@ -159,20 +170,15 @@ typedef struct
} FDs;


#if defined __FreeBSD__
typedef unsigned char byte_t;
#else
typedef char byte_t;
#endif

/* </Structures> */

/* <Prototypes> */
int init_system();
int init_listen_socket_fd();
int init_serial_fd(char *path);
void add_to_all_socket_fds(char *message);
void add_to_serial_fd(char * message);
void add_to_all_socket_fds(char *message, unsigned int len);
void add_to_serial_fd(char * message, unsigned int len);
int cleanup_fd(int n);
void set_non_blocking(int fd);
void print_serial_fd_status(int fd);
Expand All @@ -193,6 +199,7 @@ BOOL read_config(char* filename);
void fifo_init(fifo *f, int size);
void fifo_destroy(fifo *f);
int fifo_empty(fifo *f);
void* fifo_make_buffer(void *in_buffer, unsigned int len);
int fifo_add(fifo *f, void *next);
void* fifo_get(fifo *f);
void fifo_clear(fifo *f);
Expand Down Expand Up @@ -232,6 +239,7 @@ fifo data_buffer;
char * option_bind_ip = 0;
char * option_baud_rate = 0;
BOOL option_daemonize = FALSE;
BOOL option_binary_mode = FALSE;
BOOL option_send_terminal_init = FALSE;
int option_debug_level = 0;
BOOL option_keep_connection = FALSE;
Expand Down Expand Up @@ -403,6 +411,8 @@ void show_help(const char *appName)
" -i IP bind to a specific ip address; default is ALL\n"
" -b baudrate set baud rate; defaults to 9600\n"
" -d daemonize\n"
" -0 binary mode will not send any status messages and\n"
" will transmit 0x00 bytes\n"
" -t send terminal init string\n"
" -g debug level 0-3\n"
" -c keep incoming connections when a serial device is disconnected\n"
Expand Down Expand Up @@ -822,8 +832,8 @@ long get_time_difference(struct timeval *startTime)
tv_serial_start.tv_usec = 0; \
serial_connected = 0; \
cleanup_fd(n); \
add_to_all_socket_fds("\r\n"); \
add_to_all_socket_fds(SERIAL_DISCONNECTED_MSG); \
add_to_all_socket_fds("\r\n", 0); \
add_to_all_socket_fds(SERIAL_DISCONNECTED_MSG, 0); \
msleep(100); \

/*
Expand Down Expand Up @@ -863,7 +873,7 @@ void poll_serial_port()
serial_connected = 1;
tv_last_serial_check.tv_sec = 0;
tv_last_serial_check.tv_usec = 0;
add_to_all_socket_fds(SERIAL_CONNECTED_MSG);
add_to_all_socket_fds(SERIAL_CONNECTED_MSG, 0);
}
else
msleep(10);
Expand Down Expand Up @@ -1046,30 +1056,30 @@ BOOL poll_read_fdset(fd_set *read_fdset)
/* adding anything to the fifo must be pre allocated */
if (option_send_terminal_init)
{
tempbuffer = strdup("!");
tempbuffer = fifo_make_buffer("!", 0);
fifo_add(
&my_fds[added_slot].send_buffer,
tempbuffer);
tempbuffer = strdup(
terminal_init_string);
tempbuffer = fifo_make_buffer(
(void*)terminal_init_string, 0);
fifo_add(
&my_fds[added_slot].send_buffer,
tempbuffer);
tempbuffer = strdup("\r\n");
tempbuffer = fifo_make_buffer("\r\n", 0);
fifo_add(
&my_fds[added_slot].send_buffer,
tempbuffer);
}

tempbuffer = strdup(SOCKET_CONNECTED_MSG);
tempbuffer = fifo_make_buffer(SOCKET_CONNECTED_MSG, 0);
fifo_add(&my_fds[added_slot].send_buffer,
tempbuffer);
if (serial_connected)
tempbuffer = strdup(
SERIAL_CONNECTED_MSG);
tempbuffer = fifo_make_buffer(
SERIAL_CONNECTED_MSG, 0);
else
tempbuffer = strdup(
SERIAL_DISCONNECTED_MSG);
tempbuffer = fifo_make_buffer(
SERIAL_DISCONNECTED_MSG, 0);
fifo_add(&my_fds[added_slot].send_buffer,
tempbuffer);
did_work = TRUE;
Expand Down Expand Up @@ -1108,8 +1118,7 @@ BOOL poll_read_fdset(fd_set *read_fdset)
if (received > 0)
{
did_work = TRUE;
buffer[received] = 0;
add_to_all_socket_fds(buffer);
add_to_all_socket_fds(buffer, received);
if (option_debug_level > 1)
{
if (option_debug_level > 2)
Expand Down Expand Up @@ -1177,11 +1186,11 @@ BOOL poll_read_fdset(fd_set *read_fdset)
{
did_work = TRUE;
buffer[received] = 0;
add_to_serial_fd(buffer);
add_to_serial_fd(buffer, received);
if (option_debug_level > 2)
{
log_message(STREAM_MAIN, MSG_WARN, "SOCKET[%i]>", n);
for (x = 0; x < strlen(buffer); x++)
for (x = 0; x < received; x++)
{
log_message(STREAM_MAIN, MSG_WARN, "[%02x]", buffer[x]);
}
Expand All @@ -1204,7 +1213,7 @@ BOOL poll_read_fdset(fd_set *read_fdset)
BOOL poll_write_fdset(fd_set *write_fdset)
{
int x, n, written;
byte_t *tempbuffer;
fifo_buffer* tempbuffer;
BOOL did_work = FALSE;

/* check every socket to find the one that needs write */
Expand All @@ -1230,7 +1239,7 @@ BOOL poll_write_fdset(fd_set *write_fdset)
/* load our buffer with data to send */
tempbuffer = (char *) fifo_get(
&my_fds[n].send_buffer);
written = BIO_write(my_fds[n].ssl, tempbuffer, strlen(tempbuffer));
written = BIO_write(my_fds[n].ssl, tempbuffer->buffer, tempbuffer->size));
if (written <= 0 && BIO_should_retry(my_fds[n].ssl))
continue;
} else
Expand All @@ -1253,9 +1262,9 @@ BOOL poll_write_fdset(fd_set *write_fdset)
#endif
/* load our buffer with data to send */
{
tempbuffer = (char *) fifo_get(
tempbuffer = (fifo_buffer *) fifo_get(
&my_fds[n].send_buffer);
send(my_fds[n].fd, tempbuffer, strlen(tempbuffer), 0);
send(my_fds[n].fd, tempbuffer->buffer, tempbuffer->size, 0);
}

/* did we do any work? */
Expand All @@ -1271,14 +1280,13 @@ BOOL poll_write_fdset(fd_set *write_fdset)
if (my_fds[n].fd_type == SERIAL)
{
/* load our buffer with data to send */
tempbuffer = (char *) fifo_get(
tempbuffer = (fifo_buffer *) fifo_get(
&my_fds[n].send_buffer);
errno = 0;
did_work = TRUE;
if (option_debug_level > 2)
log_message(STREAM_MAIN, MSG_WARN, "SERIAL[%i]<", n);
if (write(my_fds[n].fd, tempbuffer, strlen(
tempbuffer)) < 0)
if (write(my_fds[n].fd, tempbuffer->buffer, tempbuffer->size) < 0)
{
if (errno != EAGAIN)
{
Expand All @@ -1293,9 +1301,9 @@ BOOL poll_write_fdset(fd_set *write_fdset)
/* logging if needed */
if (option_debug_level > 2 && tempbuffer)
{
for (x = 0; x < strlen(tempbuffer); x++)
for (x = 0; x < tempbuffer->size; x++)
{
log_message(STREAM_MAIN, MSG_WARN, "[%02x]", tempbuffer[x]);
log_message(STREAM_MAIN, MSG_WARN, "[%02x]", tempbuffer->buffer[x]);
}
log_message(STREAM_MAIN, MSG_WARN, "\n");
}
Expand Down Expand Up @@ -1406,11 +1414,11 @@ void listen_loop()

/*
add_to_all_socket_fds
adds a buffer to ever connected socket fd ie multiplexes
adds a buffer to every connected socket fd ie multiplexes
*/
void add_to_all_socket_fds(char * message)
void add_to_all_socket_fds(char * message, unsigned int len)
{
char * tempbuffer;
void * tempbuffer;
int n;

/*
Expand All @@ -1425,7 +1433,7 @@ void add_to_all_socket_fds(char * message)
if (my_fds[n].fd_type == CLIENT_SOCKET)
{
/* caller of fifo_get must free this */
tempbuffer = strdup(message);
tempbuffer = fifo_make_buffer(message,len);
fifo_add(&my_fds[n].send_buffer, tempbuffer);
}
}
Expand All @@ -1435,17 +1443,17 @@ void add_to_all_socket_fds(char * message)
/*
adds data to the serial fifo buffer should be at 0 ever time
*/
void add_to_serial_fd(char *message)
void add_to_serial_fd(char *message, unsigned int len)
{
int n;
char * tempbuffer;
void * tempbuffer;
for (n = 0; n < MAXCONNECTIONS; n++)
{
if (my_fds[n].inuse == TRUE)
{
if (my_fds[n].fd_type == SERIAL)
{
tempbuffer = strdup(message);
tempbuffer = fifo_make_buffer(message, len);
fifo_add(&my_fds[n].send_buffer, tempbuffer);
break;
}
Expand Down Expand Up @@ -1899,6 +1907,30 @@ void fifo_clear(fifo *f)
log_message(STREAM_MAIN, MSG_GOOD, " done.\n");
}

/*
allocate a fifo_buffer and fill it with the supplied in_buffer and len.
if len is 0 it will be calculated using strlen() so nulls will
be excluded and thus not true binary. For binary and to include
nulls len must be > 0.
note:
this has a specific type where all the other fifo low level routines
are all void *.
*/
void* fifo_make_buffer(void *in_buffer, unsigned int len)
{
fifo_buffer* out_buffer;

// Calcualte the buffer size as a string not including the null terminator
if (!len) {
len = strlen(in_buffer);
}
out_buffer = (fifo_buffer *)malloc(sizeof(out_buffer->size)+len);
memcpy(out_buffer->buffer, in_buffer, len);
out_buffer->size = len;
return (void*) out_buffer;
}

/*
insert an element
this must be already allocated with malloc or strdup
Expand Down

0 comments on commit cf3f99d

Please sign in to comment.