00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "C_ProtocolBinarySeparator.hpp"
00021
00022 #include "GeneratorTrace.hpp"
00023 #include "BufferUtils.hpp"
00024 #include "GeneratorError.h"
00025 #include "string_t.hpp"
00026 #include "C_MessageBinarySeparator.hpp"
00027
00028
00029 C_ProtocolBinarySeparator::C_ProtocolBinarySeparator() : C_ProtocolBinary() {
00030 GEN_DEBUG(1, "C_ProtocolBinarySeparator::C_ProtocolBinarySeparator() start");
00031 m_header_body_field_separator_size = 0;
00032 m_session_id = -1 ;
00033 GEN_DEBUG(1, "C_ProtocolBinarySeparator::C_ProtocolBinarySeparator() end" );
00034 }
00035
00036 C_ProtocolBinarySeparator::~C_ProtocolBinarySeparator() {
00037 GEN_DEBUG(1, "C_ProtocolBinarySeparator::~C_ProtocolBinarySeparator() start" );
00038 m_header_body_field_separator_size = 0;
00039 GEN_DEBUG(1, "C_ProtocolBinarySeparator::~C_ProtocolBinarySeparator() end" );
00040 }
00041
00042 void C_ProtocolBinarySeparator::construction_data(C_XmlData *P_def,
00043 char **P_name,
00044 T_pContructorResult P_res) {
00045
00046 C_ProtocolBinary::construction_data(P_def, P_name, P_res);
00047
00048 GEN_DEBUG(1, "C_ProtocolBinarySeparator::construction_data() start" );
00049
00050 if (*P_res == E_CONSTRUCTOR_OK) {
00051 if (m_header_body_field_separator != NULL) {
00052 m_header_body_field_separator_size
00053 = strlen(m_header_body_field_separator) ;
00054 }
00055
00056 m_session_id = m_msg_id_id ;
00057 if (m_session_id_position == -1) {
00058 GEN_ERROR(E_GEN_FATAL_ERROR,
00059 "session-id-position not found in dictionnary");
00060 *P_res = E_CONSTRUCTOR_KO ;
00061 }
00062 }
00063 GEN_DEBUG(1, "C_ProtocolBinarySeparator::construction_data() end" );
00064 }
00065
00066 C_MessageFrame* C_ProtocolBinarySeparator::create_new_message(C_MessageFrame *P_msg) {
00067
00068 C_MessageBinarySeparator *L_msg ;
00069
00070 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() start: msg pt");
00071 NEW_VAR(L_msg, C_MessageBinarySeparator(this));
00072 if (P_msg != NULL) {
00073 (*L_msg) = *((C_MessageBinarySeparator*)P_msg) ;
00074 }
00075 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() end: msg pt");
00076 return (L_msg);
00077 }
00078
00079 C_MessageFrame* C_ProtocolBinarySeparator::create_new_message(void *P_xml,
00080 T_pInstanceDataList P_list,
00081 int *P_nb_value) {
00082
00083
00084 C_XmlData *L_Xml = (C_XmlData*) P_xml ;
00085
00086 bool L_msgOk = true ;
00087 C_MessageBinarySeparator *L_msg ;
00088 char *L_currentName ;
00089
00090 C_ProtocolFrame::T_MsgError L_decode_result ;
00091
00092 unsigned char *L_buf ;
00093 size_t L_size ;
00094
00095 int L_header_val_id ;
00096 int L_body_val_id ;
00097
00098 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() start: xml");
00099
00100
00101
00102 NEW_VAR(L_msg, C_MessageBinarySeparator(this));
00103
00104
00105 L_currentName = L_Xml -> get_name () ;
00106
00107 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() msg xml name is "
00108 << L_currentName );
00109
00110 if (strcmp(L_currentName, (char*)"CDATA") == 0) {
00111
00112
00113 L_currentName = L_Xml->find_value((char*)"value");
00114
00115 L_buf = convert_hexa_char_to_bin(L_currentName, &L_size);
00116 (void) L_msg -> decode(L_buf, L_size, &L_decode_result);
00117
00118 if (L_decode_result != C_ProtocolFrame::E_MSG_OK) {
00119 GEN_ERROR(E_GEN_FATAL_ERROR,
00120 "Error while decoding direct data buffer ["
00121 << L_currentName << "]");
00122 L_msgOk = false ;
00123 }
00124
00125 } else if (strcmp(L_currentName, get_header_name()) == 0) {
00126
00127
00128 L_currentName = L_Xml -> find_value ((char*) "name") ;
00129 if (L_currentName == NULL) {
00130 GEN_ERROR(E_GEN_FATAL_ERROR,
00131 "name value is mandatory for ["
00132 << get_header_name()
00133 << "]");
00134 L_msgOk = false ;
00135 } else {
00136 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() "
00137 << "the name of this message is " << L_currentName );
00138
00139 L_header_val_id = get_header_value_id(L_currentName) ;
00140
00141 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() "
00142 << "L_header_val_id = " << L_header_val_id );
00143
00144 if (L_header_val_id == -1) {
00145 GEN_ERROR(E_GEN_FATAL_ERROR,
00146 "unknown name ["
00147 << L_currentName << "] for [" << get_header_name() << "]");
00148 L_msgOk = false ;
00149
00150 } else {
00151
00152
00153 T_pXmlData_List L_listBodyVal ;
00154 T_XmlData_List::iterator L_bodyValIt ;
00155 C_XmlData *L_bodyData ;
00156 char *L_bodyName, *L_bodyValue ;
00157 C_ProtocolBinary::T_BodyValue L_bodyVal ;
00158 T_TypeType L_type ;
00159
00160 L_msg->set_header_id_value(L_header_val_id);
00161
00162 L_listBodyVal = L_Xml -> get_sub_data() ;
00163 if (L_listBodyVal != NULL) {
00164 for (L_bodyValIt = L_listBodyVal->begin() ;
00165 L_bodyValIt != L_listBodyVal->end() ;
00166 L_bodyValIt++) {
00167 L_bodyData = *L_bodyValIt ;
00168
00169 if (strcmp(L_bodyData->get_name(),
00170 get_header_body_name())==0) {
00171
00172 L_bodyName = L_bodyData->find_value((char*)"name");
00173 if (L_bodyName == NULL) {
00174 GEN_ERROR(E_GEN_FATAL_ERROR,
00175 "name value is mandatory for ["
00176 << get_header_body_name()
00177 << "] definition");
00178 L_msgOk = false ;
00179 }
00180
00181 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() "
00182 << "L_bodyName is [" << L_bodyName << "]");
00183
00184 L_body_val_id = get_header_body_value_id(L_bodyName) ;
00185 if (L_body_val_id == -1) {
00186 GEN_ERROR(E_GEN_FATAL_ERROR,
00187 "No definition found for ["
00188 << L_bodyName << "]");
00189 L_msgOk = false ;
00190 break ;
00191 }
00192
00193 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() "
00194 << "L_body_val_id is "
00195 << L_body_val_id );
00196
00197 L_type = get_body_value_type (L_body_val_id) ;
00198 if (L_type
00199 == E_TYPE_GROUPED) {
00200
00201 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() "
00202 << L_bodyName << " is grouped element" );
00203
00204
00205 if (process_grouped_type(L_bodyData, L_body_val_id,
00206 &L_bodyVal) != 0) {
00207 GEN_ERROR(E_GEN_FATAL_ERROR,
00208 "Grouped Type processing Error for "
00209 << L_bodyName);
00210 L_msgOk = false ;
00211 break ;
00212 }
00213
00214
00215
00216 L_msg->set_body_value(&L_bodyVal);
00217
00218
00219 reset_grouped_body_value(&L_bodyVal);
00220 FREE_TABLE(L_bodyVal.m_sub_val);
00221 } else {
00222 bool L_toBeDelete = false;
00223
00224
00225 L_bodyValue = L_bodyData->find_value((char*)"value");
00226 if (L_bodyValue == NULL) {
00227 GEN_ERROR(E_GEN_FATAL_ERROR,
00228 "value is mandatory for ["
00229 << get_header_body_name()
00230 << "] definition");
00231 L_msgOk = false ;
00232 break ;
00233 }
00234
00235 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() "
00236 << "L_bodyValue is \"" << L_bodyValue << "\"" );
00237
00238 if (set_body_value(L_body_val_id,
00239 L_bodyValue,
00240 1,
00241 &L_bodyVal,
00242 &L_toBeDelete) == 0) {
00243
00244 if (m_header_body_field_separator == NULL) {
00245 L_msg->set_body_value(&L_bodyVal);
00246 } else {
00247 T_ValueData L_tmp_value ;
00248 bool L_exist ;
00249 L_tmp_value.m_value = L_bodyVal.m_value ;
00250 L_tmp_value.m_id = L_bodyVal.m_id ;
00251 L_tmp_value.m_type = L_type;
00252 L_exist = L_msg->set_body_value(L_body_val_id,&L_tmp_value);
00253 if (L_exist == false) {
00254 L_msg->set_body_value(&L_bodyVal);
00255 }
00256 }
00257
00258 if (L_toBeDelete) {
00259
00260 delete_body_value(&L_bodyVal);
00261 }
00262
00263 } else {
00264 GEN_ERROR(E_GEN_FATAL_ERROR,
00265 "Bad format for ["
00266 << L_bodyValue << "]");
00267 L_msgOk = false ;
00268 break ;
00269 }
00270
00271 }
00272 } else if (strcmp(L_bodyData->get_name(),
00273 (char*)"setfield")==0) {
00274 unsigned long L_val_setfield = 0 ;
00275 int L_id_setfield = 0 ;
00276 L_msgOk = (analyze_setfield(L_bodyData, &L_id_setfield ,&L_val_setfield) == -1)
00277 ? false : true ;
00278 if (L_msgOk) {
00279 L_msg->C_MessageBinary::set_header_value(L_id_setfield, L_val_setfield);
00280 }
00281
00282 } else {
00283 GEN_ERROR(E_GEN_FATAL_ERROR,
00284 "Unkown section ["
00285 << L_bodyData->get_name()
00286 << "]");
00287 L_msgOk = false;
00288 break ;
00289 }
00290 if (L_msgOk == false) break ;
00291 }
00292 }
00293 }
00294 }
00295 }
00296
00297
00298 if (L_msgOk == false) {
00299 DELETE_VAR(L_msg);
00300 } else {
00301
00302 m_messageList->push_back(L_msg);
00303 }
00304
00305 GEN_DEBUG(1, "C_ProtocolBinarySeparator::create_new_message() end: xml");
00306
00307 return (L_msg);
00308 }
00309
00310
00311 C_ProtocolFrame::T_MsgError C_ProtocolBinarySeparator::encode_body (int P_nbVal,
00312 T_pBodyValue P_val,
00313 unsigned char *P_buf,
00314 size_t *P_size) {
00315
00316 unsigned char *L_ptr = P_buf ;
00317 int L_i, L_body_id ;
00318 size_t L_total_size = 0 ;
00319 size_t L_current_size = 0 ;
00320 T_pHeaderBodyValue L_body_fieldValues ;
00321 T_pBodyValue L_body_val ;
00322 unsigned long L_valueSize ;
00323 int L_type_id ;
00324 T_TypeType L_type ;
00325
00326 unsigned char *L_save_length_ptr = NULL;
00327 unsigned long L_save_length = 0;
00328 size_t L_length_size = 0;
00329
00330 size_t L_sub_size ;
00331 C_ProtocolFrame::T_MsgError L_error = C_ProtocolFrame::E_MSG_OK;
00332
00333
00334 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() start");
00335
00336 L_total_size = 0 ;
00337 L_current_size = 0 ;
00338
00339 for (L_i = 0; L_i < P_nbVal ; L_i ++) {
00340
00341 L_body_val = &P_val[L_i] ;
00342 L_body_id = L_body_val->m_id ;
00343
00344
00345
00346 if (m_stats) {
00347 if (L_body_val -> m_value.m_val_binary.m_size != 0) {
00348 m_stats->updateStats (E_MESSAGE_COMPONENT,
00349 E_SEND,
00350 L_body_id);
00351 }
00352 }
00353
00354 L_body_fieldValues = &m_header_body_value_table[L_body_id] ;
00355
00356 L_type_id = L_body_fieldValues->m_type_id ;
00357 L_type = m_type_def_table[L_type_id].m_type ;
00358
00359 if (L_type == E_TYPE_STRING) {
00360 L_valueSize =
00361 L_body_val -> m_value.m_val_binary.m_size ;
00362 } else {
00363 L_valueSize =
00364 m_type_def_table[L_type_id].m_size ;
00365 }
00366
00367
00368 switch (L_type) {
00369
00370 case E_TYPE_NUMBER:
00371 convert_ul_to_bin_network(L_ptr,
00372 L_valueSize,
00373 L_body_val -> m_value.m_val_number);
00374
00375 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with number value = "
00376 << L_body_val->m_value.m_val_number);
00377 break ;
00378
00379 case E_TYPE_SIGNED:
00380 convert_l_to_bin_network(L_ptr,
00381 L_valueSize,
00382 L_body_val -> m_value.m_val_signed);
00383
00384 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with signed value = "
00385 << L_body_val->m_value.m_val_signed);
00386 break ;
00387
00388 case E_TYPE_STRING: {
00389 size_t L_padding ;
00390 memcpy(L_ptr, L_body_val->m_value.m_val_binary.m_value, L_valueSize);
00391 if (m_padding_value) {
00392 L_padding = L_valueSize % m_padding_value ;
00393 if (L_padding) {
00394 L_padding = m_padding_value - L_padding ;
00395
00396 if ((L_total_size+L_valueSize+L_padding) > *P_size) {
00397 GEN_LOG_EVENT(LOG_LEVEL_TRAFFIC_ERR,
00398 "Buffer max size reached [" << *P_size << "]");
00399 L_error = C_ProtocolFrame::E_MSG_ERROR_ENCODING ;
00400 break ;
00401 }
00402
00403 }
00404 while (L_padding) {
00405 *(L_ptr+L_valueSize) = '\0' ;
00406 L_valueSize++ ;
00407 L_padding-- ;
00408 }
00409 } else {
00410 L_padding = 0 ;
00411 }
00412
00413 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with string value (size: "
00414 << L_valueSize << " and padding: " << L_padding);
00415 }
00416 break ;
00417
00418 case E_TYPE_STRUCT: {
00419 size_t L_sub_value_size = L_valueSize/2 ;
00420
00421 convert_ul_to_bin_network(L_ptr,
00422 L_sub_value_size,
00423 L_body_val -> m_value.m_val_struct.m_id_1);
00424
00425 convert_ul_to_bin_network(L_ptr + L_sub_value_size,
00426 L_sub_value_size,
00427 L_body_val -> m_value.m_val_struct.m_id_2);
00428
00429 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with struct value = ["
00430 << L_body_val -> m_value.m_val_struct.m_id_1 << ";"
00431 << L_body_val -> m_value.m_val_struct.m_id_2);
00432 }
00433 break ;
00434
00435 case E_TYPE_GROUPED:
00436 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with grouped value " );
00437
00438 L_sub_size = *P_size - L_total_size ;
00439
00440 L_sub_size = *P_size - L_total_size ;
00441
00442 L_error = encode_body(L_body_val->m_value.m_val_number,
00443 L_body_val->m_sub_val,
00444 L_ptr,
00445 &L_sub_size);
00446
00447 if (L_error == C_ProtocolFrame::E_MSG_OK) {
00448 L_total_size += L_sub_size ;
00449
00450 if (L_total_size > *P_size) {
00451 GEN_LOG_EVENT(LOG_LEVEL_TRAFFIC_ERR,
00452 "Buffer max size reached [" << *P_size << "]");
00453 L_error = C_ProtocolFrame::E_MSG_ERROR_ENCODING ;
00454 break ;
00455 }
00456
00457 L_save_length += L_sub_size ;
00458 convert_ul_to_bin_network(L_save_length_ptr,
00459 L_length_size,
00460 L_save_length) ;
00461 L_ptr += L_sub_size ;
00462 }
00463
00464 break ;
00465
00466 case E_TYPE_NUMBER_64:
00467 convert_ull_to_bin_network(L_ptr,
00468 L_valueSize,
00469 L_body_val -> m_value.m_val_number_64);
00470
00471 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with number64 value = "
00472 << L_body_val->m_value.m_val_number_64);
00473 break ;
00474
00475 case E_TYPE_SIGNED_64:
00476 convert_ll_to_bin_network(L_ptr,
00477 L_valueSize,
00478 L_body_val -> m_value.m_val_signed_64);
00479
00480 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() with signed64 value = "
00481 << L_body_val->m_value.m_val_signed_64);
00482 break ;
00483
00484 default:
00485 GEN_FATAL(E_GEN_FATAL_ERROR,
00486 "Encoding method not implemented for this value");
00487 break ;
00488 }
00489
00490
00491 if (L_error != C_ProtocolFrame::E_MSG_OK) {
00492 break ;
00493 }
00494
00495
00496 L_total_size += L_valueSize ;
00497 L_ptr += L_valueSize ;
00498
00499
00500 if (m_header_body_field_separator_size > 0) {
00501 memcpy(L_ptr,
00502 m_header_body_field_separator,
00503 m_header_body_field_separator_size);
00504
00505 L_total_size += m_header_body_field_separator_size ;
00506 L_ptr += m_header_body_field_separator_size ;
00507 }
00508 }
00509
00510 if (L_error == C_ProtocolFrame::E_MSG_OK) {
00511 *P_size = L_total_size ;
00512 }
00513
00514 GEN_DEBUG(1, "C_ProtocolBinarySeparator::encode_body() end");
00515
00516 return (L_error);
00517 }
00518
00519
00520
00521 int C_ProtocolBinarySeparator::decode_body(unsigned char *P_buf,
00522 size_t P_size,
00523 T_pBodyValue P_valDec,
00524 int *P_nbValDec,
00525 int *P_headerId) {
00526
00527 int L_max_values = *P_nbValDec ;
00528 int L_ret = 0 ;
00529 unsigned long L_total_size, L_data_size, L_data_type ;
00530 int L_i ;
00531
00532 unsigned char *L_buf = NULL ;
00533 char L_char ;
00534
00535 unsigned char *L_ptr = NULL ;
00536 char *L_pos ;
00537
00538
00539 GEN_DEBUG(1, "C_ProtocolBinarySeparator::decode_body() start");
00540
00541 if (memcmp((P_buf + (P_size - m_header_body_field_separator_size)),
00542 m_header_body_field_separator,
00543 m_header_body_field_separator_size) == 0) {
00544 ALLOC_TABLE(L_buf, unsigned char*, sizeof(unsigned char), P_size +1);
00545 memcpy(L_buf, P_buf, P_size);
00546 L_buf[P_size] = 0 ;
00547 } else {
00548 L_char = P_buf[(P_size-1)] ;
00549 P_buf[(P_size-1)] = 0 ;
00550 }
00551
00552 L_ptr = (L_buf == NULL) ? P_buf : L_buf ;
00553
00554 L_total_size = 0 ;
00555 *P_nbValDec = 0 ;
00556
00557 L_i = 0 ;
00558
00559 while (L_total_size < P_size) {
00560
00561 L_data_size = 0 ;
00562 L_data_type = 0 ;
00563
00564 L_pos = strstr((char*)L_ptr, m_header_body_field_separator);
00565 if (L_pos != NULL) {
00566 L_data_size = (L_pos - (char*)L_ptr) ;
00567 }
00568
00569 if ((L_total_size + L_data_size) <= P_size) {
00570
00571 if (L_i == m_session_id_position) {
00572 P_valDec[L_i].m_id = m_session_id ;
00573 } else {
00574 P_valDec[L_i].m_id = -1 ;
00575 }
00576
00577 P_valDec[L_i].m_value.m_val_binary.m_size = L_data_size ;
00578
00579 ALLOC_TABLE(P_valDec[L_i].m_value.m_val_binary.m_value,
00580 unsigned char*,
00581 sizeof(unsigned char),
00582 L_data_size);
00583
00584 memcpy(P_valDec[L_i].m_value.m_val_binary.m_value,
00585 L_ptr,
00586 L_data_size);
00587
00588 L_ptr = (unsigned char*)L_pos + 1 ;
00589 L_total_size += L_data_size + m_header_body_field_separator_size ;
00590 L_i++ ;
00591 } else {
00592 GEN_ERROR (E_GEN_FATAL_ERROR, "message size error (body size)");
00593 L_ret = -1 ;
00594 break ;
00595 }
00596
00597 if (L_ret == -1) break ;
00598
00599 if (L_i == L_max_values) {
00600 GEN_FATAL(E_GEN_FATAL_ERROR, "Maximum number of values reached ["
00601 << L_max_values << "]");
00602 L_ret = -1 ;
00603 break ;
00604 }
00605 }
00606
00607 if (L_ret == -1 ) {
00608 if (!L_buf) { P_buf[(P_size-1)] = L_char ; }
00609 }
00610
00611 *P_nbValDec = L_i ;
00612
00613
00614 if (L_total_size != P_size) {
00615 if (!L_buf) { P_buf[(P_size-1)] = L_char ; }
00616 L_ret = -1 ;
00617 }
00618
00619 if (L_buf) {
00620 FREE_TABLE(L_buf);
00621 }
00622
00623 GEN_DEBUG(1, "C_ProtocolBinarySeparator::decode_body() end");
00624
00625 return (L_ret) ;
00626
00627 }