From 9d4153b78d16673425997d72e37f28c69933146e Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 16 Dec 2023 22:13:21 -0500 Subject: [PATCH] SDSM Common Properties encoding --- .../src/JsonToJ3224SDSMConverter.cpp | 91 +++++++++++++------ .../src/JsonToJ3224SDSMConverter.h | 12 ++- .../test/test_JsonToJ3224SDSMConverter.cpp | 81 +++++++++++++++-- 3 files changed, 147 insertions(+), 37 deletions(-) diff --git a/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.cpp b/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.cpp index e0a7f0f2f..2aa218442 100644 --- a/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.cpp +++ b/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.cpp @@ -35,39 +35,65 @@ namespace CARMAStreetsPlugin } return parseResult; } - void JsonToJ3224SDSMConverter::convertJson2SDSM(const Json::Value &sdsm_json, std::shared_ptr sdsm) const { - ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_SignalStatusMessage, sdsm.get()); + void JsonToJ3224SDSMConverter::convertJson2SDSM(const Json::Value &sdsm_json, const std::shared_ptr &sdsm) const { + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_SensorDataSharingMessage, sdsm.get()); // Message Count sdsm->msgCnt = sdsm_json["msg_cnt"].asInt64(); - // Source ID - std::string id_data = sdsm_json["source_id"].asString(); - TemporaryID_t tempID; - std::vector id_vector(id_data.begin(), id_data.end()); - uint8_t *id_ptr = &id_vector[0]; - tempID.buf = id_ptr; - tempID.size = sizeof(4); - sdsm->sourceID = tempID; + // Source ID (Expecting format "rsu_<4-digit-number>") + std::string id_data = sdsm_json["source_id"].asString().substr(4); + TemporaryID_t *tempID = (TemporaryID_t *)calloc(1, sizeof(TemporaryID_t)); + OCTET_STRING_fromString(tempID, id_data.c_str()); + sdsm->sourceID = *tempID; + free(tempID); + // Equipment Type sdsm->equipmentType = sdsm_json["equipment_type"].asInt64(); // SDSM DateTime timestamp - auto sDSMTimeStamp = (DDateTime_t*) calloc(1, sizeof(DDateTime_t)); - auto year = (DYear_t*) calloc(1, sizeof(DYear_t)); - *year = sdsm_json["sdsm_time_stamp"]["year"].asInt64(); - sDSMTimeStamp->year = year; - auto month = (DMonth_t*) calloc(1, sizeof(DMonth_t)); - *month = sdsm_json["sdsm_time_stamp"]["month"].asInt64(); - sDSMTimeStamp->month = month; - auto day = (DDay_t*) calloc(1, sizeof(DDay_t)); - *day = sdsm_json["sdsm_time_stamp"]["day"].asInt64(); - sDSMTimeStamp->day = day; - auto minute = (DMinute_t*) calloc(1, sizeof(DMinute_t)); - *minute = sdsm_json["sdsm_time_stamp"]["minute"].asInt64(); - sDSMTimeStamp->minute = minute; - auto second = (DSecond_t*) calloc(1, sizeof(DSecond_t)); - *second = sdsm_json["sdsm_time_stamp"]["second"].asInt64(); - sDSMTimeStamp->second = second; - sdsm->sDSMTimeStamp = *sDSMTimeStamp; + DDateTime_t sDSMTimeStamp; + // Optional Year + if ( sdsm_json["sdsm_time_stamp"].isMember("year") ) { + auto year = (DYear_t*) calloc(1, sizeof(DYear_t)); + *year = sdsm_json["sdsm_time_stamp"]["year"].asInt64(); + sDSMTimeStamp.year = year; + } + // Optional Month + if ( sdsm_json["sdsm_time_stamp"].isMember("month") ) { + auto month = (DMonth_t*) calloc(1, sizeof(DMonth_t)); + *month = sdsm_json["sdsm_time_stamp"]["month"].asInt64(); + sDSMTimeStamp.month = month; + } + // Optional Day + if ( sdsm_json["sdsm_time_stamp"].isMember("day") ) { + auto day = (DDay_t*) calloc(1, sizeof(DDay_t)); + *day = sdsm_json["sdsm_time_stamp"]["day"].asInt64(); + sDSMTimeStamp.day = day; + } + // Optional Hour + if ( sdsm_json["sdsm_time_stamp"].isMember("hour") ) { + auto hour = (DHour_t*) calloc(1, sizeof(DHour_t)); + *hour = sdsm_json["sdsm_time_stamp"]["hour"].asInt64(); + sDSMTimeStamp.hour = hour; + } + // Optional Minute + if ( sdsm_json["sdsm_time_stamp"].isMember("minute") ) { + auto minute = (DMinute_t*) calloc(1, sizeof(DMinute_t)); + *minute = sdsm_json["sdsm_time_stamp"]["minute"].asInt64(); + sDSMTimeStamp.minute = minute; + } + // Optional Second + if ( sdsm_json["sdsm_time_stamp"].isMember("second") ) { + auto second = (DSecond_t*) calloc(1, sizeof(DSecond_t)); + *second = sdsm_json["sdsm_time_stamp"]["second"].asInt64(); + sDSMTimeStamp.second = second; + } + // Optional Offset + if ( sdsm_json["sdsm_time_stamp"].isMember("offset") ) { + auto offset = (DOffset_t*) calloc( 1, sizeof(DOffset_t)); + *offset = sdsm_json["sdsm_time_stamp"]["offset"].asInt64(); + sDSMTimeStamp.offset = offset; + } + sdsm->sDSMTimeStamp = sDSMTimeStamp; // Reference Position sdsm->refPos.lat = sdsm_json["ref_pos"]["lat"].asInt64(); sdsm->refPos.Long = sdsm_json["ref_pos"]["long"].asInt64(); @@ -170,7 +196,7 @@ namespace CARMAStreetsPlugin std::vector id_vector(id_data.begin(), id_data.end()); uint8_t *id_ptr = &id_vector[0]; tempID.buf = id_ptr; - tempID.size = sizeof(id_ptr); + tempID.size = sizeof(id_vector); sdsm->sourceID = tempID; sdsm->equipmentType = sdsm_json["equipment_type"].asInt64(); @@ -485,5 +511,14 @@ namespace CARMAStreetsPlugin free(frame.get_j2735_data().get()); } + void JsonToJ3224SDSMConverter::encodeSDSM(SensorDataSharingMessage_t *sdsmPtr, tmx::messages::SdsmEncodedMessage &encodedSDSM) const + { + auto _sdsmMessage = new tmx::messages::SdsmMessage(sdsmPtr); + tmx::messages::MessageFrameMessage frame(_sdsmMessage->get_j2735_data()); + encodedSDSM.set_data(tmx::messages::TmxJ2735EncodedMessage::encode_j2735_message>(frame)); + asn_fprint(stdout, &asn_DEF_MessageFrame, frame.get_j2735_data().get()); + free(frame.get_j2735_data().get()); + } + } \ No newline at end of file diff --git a/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.h b/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.h index 1253b3435..849a889ff 100644 --- a/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.h +++ b/src/v2i-hub/CARMAStreetsPlugin/src/JsonToJ3224SDSMConverter.h @@ -31,10 +31,16 @@ namespace CARMAStreetsPlugin * @param json Incoming Json value with sdsm information that is consumed from a Kafka topic. * @param sdsm Outgoing J3224 sdsm object that is populated by the json value. */ - void convertJsonToSDSM(const Json::Value &sdsm_json, std::shared_ptr sdsm) const; - void convertJson2SDSM(const Json::Value &sdsm_json, std::shared_ptr sdsm) const; + void convertJson2SDSM(const Json::Value &sdsm_json, const std::shared_ptr &sdsm) const; + /** + * @brief Convert the Json value with sdsm information info tmx SDSM object. + * @param json Incoming Json value with sdsm information that is consumed from a Kafka topic. + * @param sdsm Outgoing J3224 sdsm object that is populated by the json value. + */ + + void convertJsonToSDSM(const Json::Value &sdsm_json, std::shared_ptr sdsm) const; /*** * @brief Encode J3224 SDSM * @param Pointer to J3224 SDSM object @@ -42,6 +48,8 @@ namespace CARMAStreetsPlugin */ void encodeSDSM(const std::shared_ptr &sdsmPtr, tmx::messages::SdsmEncodedMessage &encodedSDSM) const; + void encodeSDSM(SensorDataSharingMessage_t *sdsmPtr, tmx::messages::SdsmEncodedMessage &encodedSDSM) const; + ~JsonToJ3224SDSMConverter() = default; }; diff --git a/src/v2i-hub/CARMAStreetsPlugin/test/test_JsonToJ3224SDSMConverter.cpp b/src/v2i-hub/CARMAStreetsPlugin/test/test_JsonToJ3224SDSMConverter.cpp index 7d238cb53..823e84757 100644 --- a/src/v2i-hub/CARMAStreetsPlugin/test/test_JsonToJ3224SDSMConverter.cpp +++ b/src/v2i-hub/CARMAStreetsPlugin/test/test_JsonToJ3224SDSMConverter.cpp @@ -67,14 +67,79 @@ namespace CARMAStreetsPlugin TEST_F(test_JsonToJ3224SDSMConverter, convert2Sdsm) { JsonToJ3224SDSMConverter converter; - std::string valid_json_str = "{\"equipment_type\":1,\"msg_cnt\":1,\"ref_pos\":{\"long\":600000000,\"elevation\":30,\"lat\":400000000},\"ref_pos_el_conf\":10,\"ref_pos_xy_conf\":{\"orientation\":25000,\"semi_major\":235,\"semi_minor\":200},\"sdsm_time_stamp\":{\"day\":4,\"hour\":19,\"minute\":15,\"month\":7,\"offset\":400,\"second\":5000,\"year\":2007},\"source_id\":\"01020304\",\"objects\":[{\"detected_object_data\":{\"detected_object_common_data\":{\"acc_cfd_x\":4,\"acc_cfd_y\":5,\"acc_cfd_yaw\":3,\"acc_cfd_z\":6,\"accel_4_way\":{\"lat\":-500,\"long\":200,\"vert\":1,\"yaw\":400},\"heading\":16000,\"heading_conf\":4,\"measurement_time\":-1100,\"object_id\":12200,\"obj_type\":1,\"obj_type_cfd\":65,\"pos\":{\"offset_x\":4000,\"offset_y\":-720,\"offset_z\":20},\"pos_confidence\":{\"elevation\":5,\"pos\":2},\"speed\":2100,\"speed_confidence\":3,\"speed_confidence_z\":4,\"speed_z\":1000,\"time_confidence\":2}}}]}"; + std::string valid_json_str = R"( + { + "equipment_type":1, + "msg_cnt":1, + "ref_pos":{ + "long":600000000, + "elevation":30, + "lat":400000000 + }, + "ref_pos_el_conf":10, + "ref_pos_xy_conf":{ + "orientation":25000, + "semi_major":235, + "semi_minor":200 + }, + "sdsm_time_stamp":{ + "day":4, + "hour":19, + "minute":15, + "month":7, + "offset":400, + "second":5000, + "year":2007 + }, + "source_id":"rsu_1234", + "objects":[ + { + "detected_object_data":{ + "detected_object_common_data":{ + "acc_cfd_x":4, + "acc_cfd_y":5, + "acc_cfd_yaw":3, + "acc_cfd_z":6, + "accel_4_way":{ + "lat":-500, + "long":200, + "vert":1, + "yaw":400 + }, + "heading":16000, + "heading_conf":4, + "measurement_time":-1100, + "object_id":12200, + "obj_type":1, + "obj_type_cfd":65, + "pos":{ + "offset_x":4000, + "offset_y":-720, + "offset_z":20 + }, + "pos_confidence":{ + "elevation":5, + "pos":2 + }, + "speed":2100, + "speed_confidence":3, + "speed_confidence_z":4, + "speed_z":1000, + "time_confidence":2 + } + } + } + ] + } + )"; Json::Value root; bool result = converter.parseJsonString(valid_json_str, root); ASSERT_TRUE(result); - auto sdsmPtr = std::make_shared(); + auto sdsmPtr = std::make_shared(); converter.convertJson2SDSM(root, sdsmPtr); + asn_fprint(stdout, &asn_DEF_SensorDataSharingMessage, sdsmPtr.get()); + tmx::messages::SdsmEncodedMessage encodedSdsm; - converter.encodeSDSM(sdsmPtr, encodedSdsm); ASSERT_EQ(1, sdsmPtr->objects.list.array[0]->detObjCommon.objType); ASSERT_EQ(65, sdsmPtr->objects.list.array[0]->detObjCommon.objTypeCfd); ASSERT_EQ(12200, sdsmPtr->objects.list.array[0]->detObjCommon.objectID); @@ -100,10 +165,12 @@ namespace CARMAStreetsPlugin ASSERT_EQ(-500, sdsmPtr->objects.list.array[0]->detObjCommon.accel4way->lat); ASSERT_EQ(1, sdsmPtr->objects.list.array[0]->detObjCommon.accel4way->vert); ASSERT_EQ(400, sdsmPtr->objects.list.array[0]->detObjCommon.accel4way->yaw); - - ASSERT_EQ(41, encodedSdsm.get_msgId()); - // std::string expectedSDSMEncHex = "00293681c0de24f33d9f5dc8789c429af8da011e1a2ffe203dd790c35140070304bea06402c7cfbe97c00992a0d18fa23e809130bb901031e0"; - // ASSERT_EQ(expectedSDSMEncHex, encodedSdsm.get_payload_str()); + converter.encodeSDSM(sdsmPtr, encodedSdsm); + + ASSERT_EQ(41, encodedSdsm.get_msgId()); + + std::string expectedSDSMEncHex = "00293881313233343fdf5dc933c4e226c29af8da011e1a2ffe203dd790c35140070304bea06402c7cfbe97c00992a0d18fa23e809130bb901031e0"; + ASSERT_EQ(expectedSDSMEncHex, encodedSdsm.get_payload_str()); } // // Test for SDSM common data