sml_message *sml_message_parse(sml_buffer *buf) { sml_message *msg = (sml_message *) malloc(sizeof(sml_message)); memset(msg, 0, sizeof(sml_message)); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 6) { buf->error = 1; goto error; } msg->transaction_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->group_id = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->abort_on_error = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->message_body = sml_message_body_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->crc = sml_u16_parse(buf); if (sml_buf_has_errors(buf)) goto error; if (sml_buf_get_current_byte(buf) == SML_MESSAGE_END) { sml_buf_update_bytes_read(buf, 1); } return msg; error: sml_message_free(msg); return 0; }
sml_message_body *sml_message_body_parse(sml_buffer *buf) { sml_message_body *msg_body = (sml_message_body *) malloc(sizeof(sml_message_body)); memset(msg_body, 0, sizeof(sml_message_body)); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 2) { buf->error = 1; goto error; } msg_body->tag = sml_u16_parse(buf); if (sml_buf_has_errors(buf)) goto error; switch (*(msg_body->tag)) { case SML_MESSAGE_OPEN_REQUEST: msg_body->data = sml_open_request_parse(buf); break; case SML_MESSAGE_OPEN_RESPONSE: msg_body->data = sml_open_response_parse(buf); break; case SML_MESSAGE_CLOSE_REQUEST: msg_body->data = sml_close_request_parse(buf); break; case SML_MESSAGE_CLOSE_RESPONSE: msg_body->data = sml_close_response_parse(buf); break; case SML_MESSAGE_GET_PROFILE_PACK_REQUEST: msg_body->data = sml_get_profile_pack_request_parse(buf); break; case SML_MESSAGE_GET_PROFILE_PACK_RESPONSE: msg_body->data = sml_get_profile_pack_response_parse(buf); break; case SML_MESSAGE_GET_PROFILE_LIST_REQUEST: msg_body->data = sml_get_profile_list_request_parse(buf); break; case SML_MESSAGE_GET_PROFILE_LIST_RESPONSE: msg_body->data = sml_get_profile_list_response_parse(buf); break; case SML_MESSAGE_GET_PROC_PARAMETER_REQUEST: msg_body->data = sml_get_proc_parameter_request_parse(buf); break; case SML_MESSAGE_GET_PROC_PARAMETER_RESPONSE: msg_body->data = sml_get_proc_parameter_response_parse(buf); break; case SML_MESSAGE_SET_PROC_PARAMETER_REQUEST: msg_body->data = sml_set_proc_parameter_request_parse(buf); break; case SML_MESSAGE_GET_LIST_REQUEST: msg_body->data = sml_get_list_request_parse(buf); break; case SML_MESSAGE_GET_LIST_RESPONSE: msg_body->data = sml_get_list_response_parse(buf); break; case SML_MESSAGE_ATTENTION_RESPONSE: msg_body->data = sml_attention_response_parse(buf); break; default: printf("error: message type %04X not yet implemented\n", *(msg_body->tag)); break; } return msg_body; error: free(msg_body); return 0; }
TEST(sml_number, parse_unsigned16) { hex2binary("630101", sml_buf_get_current_buf(buf)); u16 *n = sml_u16_parse(buf); TEST_ASSERT_EQUAL(257, *n); sml_u16_free( n ); }