From 18a589ec46c5c31ee406bd65dc45dcf2628f6284 Mon Sep 17 00:00:00 2001 From: Juri Glass Date: Sat, 25 Jun 2011 00:44:03 +0200 Subject: [PATCH] added sml_transport_write, comments for sml_transport functions --- sml/include/sml/sml_transport.h | 19 ++++++++++--- sml/src/sml.c | 10 ------- sml/src/sml_transport.c | 48 +++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 18 deletions(-) diff --git a/sml/include/sml/sml_transport.h b/sml/include/sml/sml_transport.h index 92b1bb2..1d331f4 100644 --- a/sml/include/sml/sml_transport.h +++ b/sml/include/sml/sml_transport.h @@ -20,15 +20,28 @@ #define _SML_TRANSPORT_H #include +#include #ifdef __cplusplus extern "C" { #endif - -//void (*sml_transport_receiver_fun)(unsigned char *buffer, size_t buffer_len) = 0; - +// sml_transport_listen reads continously bytes from fd and scans +// for the SML transport protocol escape sequences. If a SML file +// is detected, the sml_transporter_receiver is called with the +// complete bytes, including the escape sequences. +// The buffer is free'd after the sml_transport_receiver comes +// back. void sml_transport_listen(int fd, void (*sml_transport_receiver)(unsigned char *buffer, size_t buffer_len)); + +// sml_transport_writes adds the SML transport protocol escape +// sequences and writes the given file to fd. The file must be +// in the parsed format. +// The number of bytes written is returned, 0 if there was an +// error. +// The sml_file must be free'd elsewhere. +int sml_transport_write(int fd, sml_file *file); + #ifdef __cplusplus } #endif diff --git a/sml/src/sml.c b/sml/src/sml.c index 7221b15..0cf510c 100644 --- a/sml/src/sml.c +++ b/sml/src/sml.c @@ -78,7 +78,6 @@ void sml_file_add_message(sml_file *file, sml_message *message) { } void sml_file_write(sml_file *file) { - int i; if (file->messages && file->messages_len > 0) { for (i = 0; i < file->messages_len; i++) { @@ -119,12 +118,3 @@ void sml_file_print(sml_file *file) { - - - - - - - - - diff --git a/sml/src/sml_transport.c b/sml/src/sml_transport.c index fced91f..86f0cbf 100644 --- a/sml/src/sml_transport.c +++ b/sml/src/sml_transport.c @@ -24,10 +24,11 @@ #include #include #include +#include +#include #define MC_SML_BUFFER_LEN 8096 -// Invokes on every incoming SML message the given method. The buffer is freed afterwards. void sml_transport_listen(int fd, void (*sml_transport_receiver)(unsigned char *buffer, size_t buffer_len)) { fd_set readfds; @@ -80,8 +81,7 @@ void sml_transport_listen(int fd, void (*sml_transport_receiver)(unsigned char * if (end == 5) { char *sml_file = (char *) malloc(i); memcpy(sml_file, &(buf[0]), i); - // without the SML transport stuff - sml_transport_receiver((unsigned char *)(sml_file ), i); + sml_transport_receiver((unsigned char *)(sml_file), i); free(sml_file); i = -1; esc = 0; @@ -114,6 +114,44 @@ void sml_transport_listen(int fd, void (*sml_transport_receiver)(unsigned char * printf("error: no end sequence found, buffer full."); } - - +int sml_transport_write(int fd, sml_file *file) { + + unsigned char start_seq[] = {0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01}; + unsigned char end_seq[] = {0x1b, 0x1b, 0x1b, 0x1b, 0x1a}; + sml_buffer *buf = file->buf; + buf->cursor = 0; + + // add start sequence + memcpy(sml_buf_get_current_buf(buf), start_seq, 8); + buf->cursor += 8; + + // add file + sml_file_write(file); + + // add padding bytes + int padding = (buf->cursor % 4) ? (4 - buf->cursor % 4) : 0; + if (padding) { + // write zeroed bytes + memset(sml_buf_get_current_buf(buf), 0, padding); + buf->cursor += padding; + } + + // begin end sequence + memcpy(sml_buf_get_current_buf(buf), end_seq, 5); + buf->cursor += 5; + + // add padding info + buf->buffer[buf->cursor++] = (unsigned char) padding; + + // add crc checksum + u16 crc = sml_crc16_calculate(buf->buffer, buf->cursor); + buf->buffer[buf->cursor++] = (unsigned char) ((crc & 0xFF00) >> 8); + buf->buffer[buf->cursor++] = (unsigned char) (crc & 0x00FF); + + size_t wr = write(fd, buf->buffer, buf->cursor); + if (wr == buf->cursor) { + return wr; + } + return 0; +}