static void * sml_value_entry_parse_(sml_buffer *buf) { sml_value_entry *entry = sml_value_entry_init(); 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; } entry->value = sml_value_parse(buf); if (sml_buf_has_errors(buf)) goto error; entry->value_signature = sml_signature_parse(buf); if (sml_buf_has_errors(buf)) goto error; return entry; error: buf->error = 1; sml_value_entry_free(entry); return 0; }
sml_set_proc_parameter_request *sml_set_proc_parameter_request_parse(sml_buffer *buf) { sml_set_proc_parameter_request *msg = sml_set_proc_parameter_request_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 5) { buf->error = 1; goto error; } msg->server_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->username = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->password = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->parameter_tree_path = sml_tree_path_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->parameter_tree = sml_tree_parse(buf); if (sml_buf_has_errors(buf)) goto error; return msg; error: sml_set_proc_parameter_request_free(msg); return 0; }
static void * sml_prof_obj_header_entry_parse_(sml_buffer *buf) { sml_prof_obj_header_entry *entry = sml_prof_obj_header_entry_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 3) { buf->error = 1; goto error; } entry->obj_name = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; entry->unit = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; entry->scaler = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; return entry; error: buf->error = 1; sml_prof_obj_header_entry_free(entry); return 0; }
static void * sml_prof_obj_period_entry_parse_(sml_buffer *buf) { sml_prof_obj_period_entry *entry = sml_prof_obj_period_entry_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 4) { buf->error = 1; goto error; } entry->val_time = sml_time_parse(buf); if (sml_buf_has_errors(buf)) goto error; entry->status = sml_u64_parse(buf); if (sml_buf_has_errors(buf)) goto error; entry->value_list = sml_sequence_parse(buf, sml_value_entry_parse_, sml_value_entry_free_); if (sml_buf_has_errors(buf)) goto error; entry->period_signature = sml_signature_parse(buf); if (sml_buf_has_errors(buf)) goto error; return entry; error: buf->error = 1; sml_prof_obj_period_entry_free(entry); return 0; }
sml_time *sml_time_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_time *tme = sml_time_init(); 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; } tme->tag = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tme->data.timestamp = sml_u32_parse(buf); if (sml_buf_has_errors(buf)) goto error; return tme; error: sml_time_free(tme); return 0; }
sml_attention_response *sml_attention_response_parse(sml_buffer *buf){ sml_attention_response *msg = sml_attention_response_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 4) { buf->error = 1; goto error; } msg->server_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->attention_number = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->attention_message = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->attention_details = sml_tree_parse(buf); if (sml_buf_has_errors(buf)) goto error; return msg; error: sml_attention_response_free(msg); return 0; }
sml_time *sml_time_init() { sml_time *t = (sml_time *) malloc(sizeof(sml_time)); *t = ( sml_time ) { .tag = NULL, .data.sec_index = NULL }; return t; } sml_time *sml_time_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_time *tme = sml_time_init(); 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; } tme->tag = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tme->data.timestamp = sml_u32_parse(buf); if (sml_buf_has_errors(buf)) goto error; return tme; error: sml_time_free(tme); return 0; } void sml_time_write(sml_time *t, sml_buffer *buf) { if (t == 0) { sml_buf_optional_write(buf); return; } sml_buf_set_type_and_length(buf, SML_TYPE_LIST, 2); sml_u8_write(t->tag, buf); sml_u32_write(t->data.timestamp, buf); } void sml_time_free(sml_time *tme) { if (tme) { sml_number_free(tme->tag); sml_number_free(tme->data.timestamp); free(tme); } }
sml_tree_path *sml_tree_path_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_tree_path *tree_path = sml_tree_path_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; return 0; } octet_string *s; int elems; for (elems = sml_buf_get_next_length(buf); elems > 0; elems--) { s = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; if (s) { sml_tree_path_add_path_entry(tree_path, s); } } return tree_path; error: buf->error = 1; sml_tree_path_free(tree_path); return 0; }
TEST(sml_status, parse_not_unsigned) { hex2binary("5001", sml_buf_get_current_buf(buf)); sml_status *s = sml_status_parse(buf); TEST_ASSERT_NULL(s); TEST_ASSERT_TRUE(sml_buf_has_errors(buf)); }
TEST(sml_status, parse_optional) { hex2binary("01", sml_buf_get_current_buf(buf)); sml_status *s = sml_status_parse(buf); TEST_ASSERT_NULL(s); TEST_ASSERT_FALSE(sml_buf_has_errors(buf)); TEST_ASSERT_EQUAL(1, buf->cursor); }
sml_tree *sml_tree_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_tree *tree = sml_tree_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 3) { buf->error = 1; goto error; } tree->parameter_name = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; tree->parameter_value = sml_proc_par_value_parse(buf); if (sml_buf_has_errors(buf)) goto error; if (!sml_buf_optional_is_skipped(buf)) { if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } sml_tree *c; int elems; for (elems = sml_buf_get_next_length(buf); elems > 0; elems--) { c = sml_tree_parse(buf); if (sml_buf_has_errors(buf)) goto error; if (c) { sml_tree_add_tree(tree, c); } } } return tree; error: sml_tree_free(tree); return 0; }
TEST(sml_list, parse_two_entries) { hex2binary("727702610101010142000177026101010101420001", sml_buf_get_current_buf(buf)); sml_list *l = sml_list_parse(buf); TEST_ASSERT_FALSE(sml_buf_has_errors(buf)); TEST_ASSERT_NOT_NULL(l); TEST_ASSERT_NOT_NULL(l->next); TEST_ASSERT_EQUAL(0, sml_octet_string_cmp_with_hex(l->obj_name, "61")); }
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_period_entry *sml_period_entry_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_period_entry *period = sml_period_entry_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 5) { buf->error = 1; goto error; } period->obj_name = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; period->unit = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; period->scaler = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; period->value = sml_value_parse(buf); if (sml_buf_has_errors(buf)) goto error; period->value_signature = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; return period; error: sml_period_entry_free(period); return 0; }
sml_get_profile_pack_response *sml_get_profile_pack_response_parse(sml_buffer *buf){ sml_get_profile_pack_response *msg = sml_get_profile_pack_response_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 8) { buf->error = 1; goto error; } msg->server_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->act_time = sml_time_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->reg_period = sml_u32_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->parameter_tree_path = sml_tree_path_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->header_list = sml_sequence_parse(buf, sml_prof_obj_header_entry_parse_, sml_prof_obj_header_entry_free_); if (sml_buf_has_errors(buf)) goto error; msg->period_list = sml_sequence_parse(buf, sml_prof_obj_period_entry_parse_, sml_prof_obj_period_entry_free_); if (sml_buf_has_errors(buf)) goto error; msg->rawdata = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->profile_signature = sml_signature_parse(buf); if (sml_buf_has_errors(buf)) goto error; return msg; error: buf->error = 1; sml_get_profile_pack_response_free(msg); return 0; }
sml_proc_par_value *sml_proc_par_value_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_proc_par_value *ppv = sml_proc_par_value_init(); 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; } ppv->tag = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; switch (*(ppv->tag)) { case SML_PROC_PAR_VALUE_TAG_VALUE: ppv->data.value = sml_value_parse(buf); break; case SML_PROC_PAR_VALUE_TAG_PERIOD_ENTRY: ppv->data.period_entry = sml_period_entry_parse(buf); break; case SML_PROC_PAR_VALUE_TAG_TUPEL_ENTRY: ppv->data.tupel_entry = sml_tupel_entry_parse(buf); break; case SML_PROC_PAR_VALUE_TAG_TIME: ppv->data.time = sml_time_parse(buf); break; default: buf->error = 1; goto error; } return ppv; error: sml_proc_par_value_free(ppv); return 0; }
sml_value *sml_value_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } int max = 1; int type = sml_buf_get_next_type(buf); unsigned char byte = sml_buf_get_current_byte(buf); sml_value *value = sml_value_init(); value->type = type; switch (type) { case SML_TYPE_OCTET_STRING: value->data.bytes = sml_octet_string_parse(buf); break; case SML_TYPE_BOOLEAN: value->data.boolean = sml_boolean_parse(buf); break; case SML_TYPE_UNSIGNED: case SML_TYPE_INTEGER: // get maximal size, if not all bytes are used (example: only 6 bytes for a u64) while (max < ((byte & SML_LENGTH_FIELD) - 1)) { max <<= 1; } value->data.uint8 = sml_number_parse(buf, type, max); value->type |= max; break; default: buf->error = 1; break; } if (sml_buf_has_errors(buf)) { sml_value_free(value); return 0; } return value; }
sml_open_request *sml_open_request_parse(sml_buffer *buf) { sml_open_request *msg = sml_open_request_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 7) { buf->error = 1; goto error; } msg->codepage = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->client_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->req_file_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->server_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->username = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->password = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; msg->sml_version = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; return msg; error: sml_open_request_free(msg); return 0; }
sml_tupel_entry *sml_tupel_entry_parse(sml_buffer *buf) { if (sml_buf_optional_is_skipped(buf)) { return 0; } sml_tupel_entry *tupel = sml_tupel_entry_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; goto error; } if (sml_buf_get_next_length(buf) != 23) { buf->error = 1; goto error; } tupel->server_id = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->sec_index = sml_time_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->status = sml_u64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->unit_pA = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->scaler_pA = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->value_pA = sml_i64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->unit_R1 = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->scaler_R1 = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->value_R1 = sml_i64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->unit_R4 = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->scaler_R4 = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->value_R4 = sml_i64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->signature_pA_R1_R4 = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->unit_mA = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->scaler_mA = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->value_mA = sml_i64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->unit_R2 = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->scaler_R2 = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->value_R2 = sml_i64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->unit_R3 = sml_unit_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->scaler_R3 = sml_i8_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->value_R3 = sml_i64_parse(buf); if (sml_buf_has_errors(buf)) goto error; tupel->signature_mA_R2_R3 = sml_octet_string_parse(buf); if (sml_buf_has_errors(buf)) goto error; return tupel; error: sml_tupel_entry_free(tupel); 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_list, parse_optional) { hex2binary("01", sml_buf_get_current_buf(buf)); sml_list *l = sml_list_parse(buf); TEST_ASSERT_NULL(l); TEST_ASSERT_FALSE(sml_buf_has_errors(buf)); }