//------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame) { char buff[PACKET_BUFF_SIZE]; int len, remaining, nread; bzero((void *)buff, sizeof(buff)); // // read data until a packet is received // remaining = 1; // start by reading 1 byte len = 0; do { //printf("%s: Attempt to read %d bytes [len = %d]", __PRETTY_FUNCTION__, remaining, len); if ((nread = read(handle->sock, &buff[len], remaining)) == -1) { mbus_error_str_set("M-Bus tcp transport layer failed to read data."); //fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)", // __PRETTY_FUNCTION__, remaining, len, nread); return -1; } //printf("%s: Got %d byte [remaining %d, len %d]", __PRETTY_FUNCTION__, nread, remaining, len); len += nread; } while ((remaining = mbus_parse(frame, buff, len)) > 0); if (remaining < 0) { mbus_error_str_set("M-Bus layer failed to parse data."); return -2; } return 0; }
//------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ int mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame) { char buff[PACKET_BUFF_SIZE]; int remaining, timeouts; ssize_t len, nread; if (handle == NULL || frame == NULL) { fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__); return MBUS_RECV_RESULT_ERROR; } // Make sure serial connection is open if (isatty(handle->fd) == 0) { fprintf(stderr, "%s: Serial connection is not available.\n", __PRETTY_FUNCTION__); return MBUS_RECV_RESULT_ERROR; } memset((void *)buff, 0, sizeof(buff)); // // read data until a packet is received // remaining = 1; // start by reading 1 byte len = 0; timeouts = 0; do { if (len + remaining > PACKET_BUFF_SIZE) { // avoid out of bounds access return MBUS_RECV_RESULT_ERROR; } //printf("%s: Attempt to read %d bytes [len = %d]\n", __PRETTY_FUNCTION__, remaining, len); if ((nread = read(handle->fd, &buff[len], remaining)) == -1) { // fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n", // __PRETTY_FUNCTION__, remaining, len, nread); return MBUS_RECV_RESULT_ERROR; } // printf("%s: Got %d byte [remaining %d, len %d]\n", __PRETTY_FUNCTION__, nread, remaining, len); if (nread == 0) { timeouts++; if (timeouts >= 3) { // abort to avoid endless loop fprintf(stderr, "%s: Timeout\n", __PRETTY_FUNCTION__); break; } } if (len > (SSIZE_MAX-nread)) { // avoid overflow return MBUS_RECV_RESULT_ERROR; } len += nread; } while ((remaining = mbus_parse(frame, buff, len)) > 0); if (len == 0) { // No data received return MBUS_RECV_RESULT_TIMEOUT; } // // call the receive event function, if the callback function is registered // if (_mbus_recv_event) _mbus_recv_event(MBUS_HANDLE_TYPE_SERIAL, buff, len); if (remaining != 0) { // Would be OK when e.g. scanning the bus, otherwise it is a failure. // printf("%s: M-Bus layer failed to receive complete data.\n", __PRETTY_FUNCTION__); return MBUS_RECV_RESULT_INVALID; } if (len == -1) { fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__); return MBUS_RECV_RESULT_ERROR; } return MBUS_RECV_RESULT_OK; }
//------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ int mbus_serial_recv_frame(SoftwareSerial *handle, mbus_frame *frame) { uint8_t buff[PACKET_BUFF_SIZE]; int len, remaining, nread, timeouts; printf_P(PSTR("%s: Entered \n"), __PRETTY_FUNCTION__); if (handle == NULL || frame == NULL) { printf_P(PSTR("%s: Invalid parameter.\n"), __PRETTY_FUNCTION__); return -1; } memset(buff, 0, sizeof(buff)); // // read data until a packet is received // remaining = 1; // start by reading 1 byte len = 0; timeouts = 0; printf_P(PSTR("%s: Entered3 \n"), __PRETTY_FUNCTION__); do { printf_P(PSTR("%s: Attempt to read %d bytes [len = %d]\n"), __PRETTY_FUNCTION__, remaining, len); while(handle->available()>0 && len<PACKET_BUFF_SIZE) { buff[len] = handle->read(); len++; } } while ((remaining = mbus_parse(frame, buff, len)) > 0); if (len == 0) { // No data received return -1; } // // call the receive event function, if the callback function is registered // if (_mbus_recv_event) _mbus_recv_event(MBUS_HANDLE_TYPE_SERIAL, buff, len); if (remaining != 0) { // Would be OK when e.g. scanning the bus, otherwise it is a failure. printf_P(PSTR("%s: M-Bus layer failed to receive complete data.\n"), __PRETTY_FUNCTION__); return -2; } if (len == -1) { printf_P(PSTR("%s: M-Bus layer failed to parse data.\n"), __PRETTY_FUNCTION__); return -1; } return 0; }
int main(int argc, char *argv[]) { FILE *fp = NULL; size_t buff_len, len; int normalized = 0; unsigned char buf[1024]; mbus_frame reply; mbus_frame_data frame_data; char *xml_result = NULL, *file = NULL; if (argc == 3 && strcmp(argv[1], "-n") == 0) { file = argv[2]; normalized = 1; } else if (argc == 2) { file = argv[1]; } else { fprintf(stderr, "usage: %s [-n] binary-file\n", argv[0]); fprintf(stderr, " optional flag -n for normalized values\n"); return 1; } if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "%s: failed to open '%s'\n", argv[0], file); return 1; } memset(buf, 0, sizeof(buf)); len = fread(buf, 1, sizeof(buf), fp); if (ferror(fp) != 0) { fprintf(stderr, "%s: failed to read '%s'\n", argv[0], file); return 1; } fclose(fp); memset(&reply, 0, sizeof(reply)); memset(&frame_data, 0, sizeof(frame_data)); mbus_parse(&reply, buf, len); mbus_frame_data_parse(&reply, &frame_data); mbus_frame_print(&reply); xml_result = normalized ? mbus_frame_data_xml_normalized(&frame_data) : mbus_frame_data_xml(&frame_data); if (xml_result == NULL) { fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str()); return 1; } printf("%s", xml_result); free(xml_result); return 0; }
int main(int argc, char *argv[]) { FILE *fp = NULL; size_t buff_len, len; int result; u_char raw_buff[4096], buff[4096]; mbus_frame reply; mbus_frame_data frame_data; char *xml_result = NULL; if (argc != 2) { fprintf(stderr, "usage: %s hex-file\n", argv[0]); return 1; } if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: failed to open '%s'\n", argv[0], argv[1]); return 1; } memset(raw_buff, 0, sizeof(raw_buff)); len = fread(raw_buff, 1, sizeof(raw_buff), fp); fclose(fp); buff_len = mbus_hex2bin(buff,sizeof(buff),raw_buff,sizeof(raw_buff)); memset(&reply, 0, sizeof(reply)); memset(&frame_data, 0, sizeof(frame_data)); //mbus_parse_set_debug(1); result = mbus_parse(&reply, buff, buff_len); if (result < 0) { fprintf(stderr, "mbus_parse: %s\n", mbus_error_str()); return 1; } else if (result > 0) { fprintf(stderr, "mbus_parse: need %d more bytes\n", result); return 1; } result = mbus_frame_data_parse(&reply, &frame_data); if (result != 0) { mbus_frame_print(&reply); fprintf(stderr, "mbus_frame_data_parse: %s\n", mbus_error_str()); return 1; } //mbus_frame_print(&reply); //mbus_frame_data_print(&frame_data); if ((xml_result = mbus_frame_data_xml(&frame_data)) == NULL) { fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str()); return 1; } printf("%s", xml_result); free(xml_result); return 0; }
int main(int argc, char *argv[]) { FILE *fp = NULL; size_t buff_len, len; int result, normalized = 0; unsigned char raw_buff[4096], buff[4096]; mbus_frame reply; mbus_frame_data frame_data; char *xml_result = NULL, *file = NULL; if (argc == 3 && strcmp(argv[1], "-n") == 0) { file = argv[2]; normalized = 1; } else if (argc == 2) { file = argv[1]; } else { fprintf(stderr, "usage: %s [-n] hex-file\n", argv[0]); fprintf(stderr, " optional flag -n for normalized values\n"); return 1; } if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "%s: failed to open '%s'\n", argv[0], file); return 1; } memset(raw_buff, 0, sizeof(raw_buff)); len = fread(raw_buff, 1, sizeof(raw_buff), fp); if (ferror(fp) != 0) { fprintf(stderr, "%s: failed to read '%s'\n", argv[0], file); return 1; } fclose(fp); buff_len = mbus_hex2bin(buff,sizeof(buff),raw_buff,sizeof(raw_buff)); memset(&reply, 0, sizeof(reply)); memset(&frame_data, 0, sizeof(frame_data)); //mbus_parse_set_debug(1); result = mbus_parse(&reply, buff, buff_len); if (result < 0) { fprintf(stderr, "mbus_parse: %s\n", mbus_error_str()); return 1; } else if (result > 0) { fprintf(stderr, "mbus_parse: need %d more bytes\n", result); return 1; } result = mbus_frame_data_parse(&reply, &frame_data); if (result != 0) { mbus_frame_print(&reply); fprintf(stderr, "mbus_frame_data_parse: %s\n", mbus_error_str()); return 1; } //mbus_frame_print(&reply); //mbus_frame_data_print(&frame_data); xml_result = normalized ? mbus_frame_data_xml_normalized(&frame_data) : mbus_frame_data_xml(&frame_data); if (xml_result == NULL) { fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str()); return 1; } printf("%s", xml_result); free(xml_result); mbus_data_record_free(frame_data.data_var.record); return 0; }
//------------------------------------------------------------------------------ // client_io: // //------------------------------------------------------------------------------ static gboolean server_io(GIOChannel *gio, GIOCondition condition, gpointer data) { static gchar *func_name = "server_io"; gchar buff[PACKET_BUFF_SIZE]; gint len, len_tot = 0; int fd; FsNetBuffer *nbdata; bzero((void *)buff, sizeof(buff)); mbus_frame *frame; mbus_frame_data frame_data; // // init // if ((fd = g_io_channel_unix_get_fd(gio)) < 0) { g_warning("%s: Failed to aquire socket fd.", func_name); return FALSE; } if (condition & (G_IO_HUP|G_IO_NVAL|G_IO_ERR)) { exit(0); } if ((nbdata = fs_net_buffer_get(gio)) == NULL) { g_warning("%s: Serious error, couldn't retrieve data buffer for GIO Channel.", func_name); return FALSE; } // // start reading data // frame = mbus_frame_new(MBUS_FRAME_TYPE_ANY); len_tot = 1; while (len > 0) { if ((len = fs_net_read_data_nonblocking(gio, nbdata, len_tot)) < 0) { if (len == -2 && nbdata->offset == 0) { // Return value -2 is a special return value that we use to indicate // that the read operation was interrupted due to unavailability of // data (so we got an try-again error, preventing a blocking read). // // Restart the read operation later. mbus_frame_free(frame); return TRUE; } if (len == -1) { fs_net_buffer_free(gio); fs_net_socket_close(fd); exit(0); } } nbdata->offset = len_tot; // start next read at the correct offset len = mbus_parse(frame, nbdata->buff, len_tot); len_tot += len; } if (len == -1) { mbus_frame_free(frame); return TRUE; } // // parse data and print in XML format // mbus_frame_data_parse(frame, &frame_data); printf("%s", mbus_frame_data_xml(&frame_data)); // // Free and exit // mbus_frame_free(frame); exit(0); return TRUE; }