+ uint8_t measRecIdx = 0;
+ CmLList *measValNode = NULLP;
+ double measVal = 0;
+
+ measRecord->list.count = measInfoDb->measuredValue.count;
+ measRecord->list.size = measRecord->list.count * sizeof(MeasurementRecordItem_t *);
+
+ DU_ALLOC(measRecord->list.array, measRecord->list.size);
+ if(!measRecord->list.array)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ for(measRecIdx = 0; measRecIdx < measRecord->list.count; measRecIdx++)
+ {
+ DU_ALLOC(measRecord->list.array[measRecIdx], sizeof(MeasurementRecordItem_t));
+ if(!measRecord->list.array[measRecIdx])
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+ }
+
+ measRecIdx = 0;
+ CM_LLIST_FIRST_NODE(&measInfoDb->measuredValue, measValNode);
+ while(measValNode)
+ {
+ measVal = *(double *)measValNode->node;
+ if(measVal == (int)measVal)
+ {
+ measRecord->list.array[measRecIdx]->present = MeasurementRecordItem_PR_integer;
+ measRecord->list.array[measRecIdx]->choice.integer = (int)measVal;
+ }
+ else
+ {
+ measRecord->list.array[measRecIdx]->present = MeasurementRecordItem_PR_real;
+ measRecord->list.array[measRecIdx]->choice.real = measVal;
+ }
+ measRecIdx++;
+
+ /* Once the measurement record is added to the message, delete it from DB */
+ cmLListDelFrm(&measInfoDb->measuredValue, measValNode);
+ DU_FREE(measValNode->node, sizeof(double));
+ DU_FREE(measValNode, sizeof(CmLList));
+
+ CM_LLIST_FIRST_NODE(&measInfoDb->measuredValue, measValNode);
+ measVal = 0;
+ }
+
+ return ROK;
+}
+
+/*******************************************************************
+ *
+ * @brief Fills measuerement data
+ *
+ * @details
+ *
+ * Function : fillMeasData
+ *
+ * Functionality: Fill all measurement recorded for all measurements
+ * in an action in a RIC subscription
+ *
+ * @param Measurement data to be filled
+ * Measurement info list from an action DB
+ * @return ROK - success
+ * RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillMeasData(MeasurementData_t *measData, CmLListCp *measInfoListDb)
+{
+ uint8_t measIdx = 0;
+ CmLList *measInfoNode = NULLP;
+ MeasurementInfo *measInfoDb = NULLP;
+ MeasurementRecord_t *measRecord = NULLP;
+
+ measData->list.count = measInfoListDb->count;
+ measData->list.size = measData->list.count * sizeof(MeasurementDataItem_t *);
+
+ DU_ALLOC(measData->list.array, measData->list.size);
+ if(!measData->list.array)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ measIdx = 0;
+ CM_LLIST_FIRST_NODE(measInfoListDb, measInfoNode);
+ while(measInfoNode)
+ {
+ measInfoDb = (MeasurementInfo *)measInfoNode->node;
+ if(measInfoDb)
+ {
+ DU_ALLOC(measData->list.array[measIdx], sizeof(MeasurementDataItem_t));
+ if(!measData->list.array[measIdx])
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ measRecord = &measData->list.array[measIdx]->measRecord;
+ if(fillMeasRecord(measRecord, measInfoDb) != ROK)
+ {
+ DU_LOG("\nERROR --> E2AP : Failed to fill measurement record");
+ return RFAILED;
+ }
+ measIdx++;
+ }
+ measInfoNode = measInfoNode->next;
+ }
+
+ return ROK;
+}
+
+/*******************************************************************
+ *
+ * @brief Fill all measurement info
+ *
+ * @details
+ *
+ * Function : fillMeasInfoList
+ *
+ * Functionality: Fills all measurement info belonging to an action
+ * in a RIC subscription
+ *
+ * @param Measurement Info list to be filled
+ * Measurement Info list from E2AP DB
+ * @return ROK - success
+ * RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillMeasInfoList(MeasurementInfoList_t *measInfoList, CmLListCp *measInfoListDb)
+{
+ uint8_t measInfoIdx = 0;
+ CmLList *measInfoNode = NULLP;
+ MeasurementInfo *measInfoDb = NULLP;
+ MeasurementInfoItem_t *measInfoItem = NULLP;
+
+ measInfoList->list.count = measInfoListDb->count;
+ measInfoList->list.size = measInfoList->list.count * sizeof(MeasurementInfoItem_t *);
+
+ DU_ALLOC(measInfoList->list.array, measInfoList->list.size);
+ if(!measInfoList->list.array)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ measInfoIdx = 0;
+ CM_LLIST_FIRST_NODE(measInfoListDb, measInfoNode);
+ while(measInfoNode)
+ {
+ DU_ALLOC(measInfoList->list.array[measInfoIdx], sizeof(MeasurementInfoItem_t));
+ if(!measInfoList->list.array[measInfoIdx])
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ measInfoItem = measInfoList->list.array[measInfoIdx];
+ measInfoDb = (MeasurementInfo *)measInfoNode->node;
+ if(measInfoDb)
+ {
+ /* Measurement Type */
+ measInfoItem->measType.present = MeasurementType_PR_measName;
+ measInfoItem->measType.choice.measName.size = strlen(measInfoDb->measurementTypeName);
+
+ DU_ALLOC(measInfoItem->measType.choice.measName.buf, measInfoItem->measType.choice.measName.size);
+ if(!measInfoItem->measType.choice.measName.buf)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ memcpy(measInfoItem->measType.choice.measName.buf, measInfoDb->measurementTypeName,\
+ measInfoItem->measType.choice.measName.size);
+
+ measInfoIdx++;
+ }
+ measInfoNode = measInfoNode->next;
+ measInfoDb = NULLP;
+ }
+
+ return ROK;
+}
+
+ /*******************************************************************
+ *
+ * @brief Fill E2SM-KPM Indication Message Format 1
+ *
+ * @details
+ *
+ * Function : fillE2smKpmIndMsgFormat1
+ *
+ * Functionality: Fill E2SM-KPM Indication Message Format 1
+ *
+ * @param Format 1 Message to be filled
+ * Action Definition format 1 from E2AP DB
+ * @return ROK - success
+ * RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillE2smKpmIndMsgFormat1(E2SM_KPM_IndicationMessage_Format1_t *format1Msg, ActionDefFormat1 *format1)
+{
+ /* Measurement Data */
+ if(fillMeasData(&format1Msg->measData, &format1->measurementInfoList) != ROK)
+ {
+ DU_LOG("\nERROR --> E2AP : Failed to fill measurement data");
+ return RFAILED;
+ }
+
+ /* Measurement Information */
+ DU_ALLOC(format1Msg->measInfoList, sizeof(MeasurementInfoList_t));
+ if(!format1Msg->measInfoList)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+
+ if(fillMeasInfoList(format1Msg->measInfoList, &format1->measurementInfoList) != ROK)
+ {
+ DU_LOG("\nERROR --> E2AP : Failed to fill measurement information list");
+ return RFAILED;
+ }
+
+ /* Granularity Period */
+ DU_ALLOC(format1Msg->granulPeriod, sizeof(GranularityPeriod_t));
+ if(!format1Msg->granulPeriod)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ return RFAILED;
+ }
+ *(format1Msg->granulPeriod) = format1->granularityPeriod;
+
+ return ROK;
+}
+
+/*******************************************************************
+ *
+ * @brief Fill RIC Indication Message buffer
+ *
+ * @details
+ *
+ * Function : fillRicIndMsgBuf
+ *
+ * Functionality: Fill E2SM-KPM Indication Message
+ * Encode this message and copy to RIC Indication Message buffer
+ *
+ * @param RIC Indication Message buffer to be filled
+ * Source action info from E2AP DB
+ * @return ROK - success
+ * RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillRicIndMsgBuf(RICindicationMessage_t *ricIndMsgBuf, ActionInfo *actionInfo)
+{
+ uint8_t ret = RFAILED;
+ bool failedInFormat = false;
+ E2SM_KPM_IndicationMessage_t e2smKpmIndMsg;
+ asn_enc_rval_t encRetVal; /* Encoder return value */
+
+ memset(&e2smKpmIndMsg, 0, sizeof(E2SM_KPM_IndicationMessage_t));
+
+ while(true)
+ {
+ /* E2SM-KPM Indication message format type */
+ e2smKpmIndMsg.indicationMessage_formats.present = actionInfo->definition.formatType;
+ switch(e2smKpmIndMsg.indicationMessage_formats.present)
+ {
+ case E2SM_KPM_IndicationMessage__indicationMessage_formats_PR_indicationMessage_Format1:
+ {
+ /* E2SM-KPM Indication message format 1 */
+ DU_ALLOC(e2smKpmIndMsg.indicationMessage_formats.choice.indicationMessage_Format1, \
+ sizeof(E2SM_KPM_IndicationMessage_Format1_t));
+ if(!e2smKpmIndMsg.indicationMessage_formats.choice.indicationMessage_Format1)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ failedInFormat = true;
+ break;
+ }
+
+ if(fillE2smKpmIndMsgFormat1(e2smKpmIndMsg.indicationMessage_formats.choice.indicationMessage_Format1, \
+ &actionInfo->definition.choice.format1) != ROK)
+ {
+ DU_LOG("\nERROR --> E2AP : Failed to fill E2SM-KPM Indication message format 1");
+ failedInFormat = true;
+ break;
+ }
+ break;
+ }
+
+ case E2SM_KPM_IndicationMessage__indicationMessage_formats_PR_NOTHING:
+ case E2SM_KPM_IndicationMessage__indicationMessage_formats_PR_indicationMessage_Format2:
+ default:
+ {
+ DU_LOG("\nERROR --> E2AP : fillRicIndMsgBuf: Only Format 1 supported");
+ failedInFormat = true;
+ break;
+ }
+ }
+
+ if(failedInFormat)
+ break;
+
+ /* Encode E2SM-KPM Indication Message */
+ xer_fprint(stdout, &asn_DEF_E2SM_KPM_IndicationMessage, &e2smKpmIndMsg);
+ memset(encBuf, 0, ENC_BUF_MAX_LEN);
+ encBufSize = 0;
+ encRetVal = aper_encode(&asn_DEF_E2SM_KPM_IndicationMessage, 0, &e2smKpmIndMsg, PrepFinalEncBuf, encBuf);
+ if(encRetVal.encoded == ENCODE_FAIL)
+ {
+ DU_LOG("\nERROR --> E2AP : Could not encode E2SM-KPM Indication Message (at %s)\n",\
+ encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
+ break;
+ }
+ else
+ {
+ DU_LOG("\nDEBUG --> E2AP : Created APER encoded buffer for E2SM-KPM Indication Message \n");
+#ifdef DEBUG_ASN_PRINT
+ for(int i=0; i< encBufSize; i++)
+ {
+ printf("%x",encBuf[i]);
+ }
+#endif
+ }
+
+ /* Copy encoded string to RIC Indication Message buffer */
+ ricIndMsgBuf->size = encBufSize;
+ DU_ALLOC(ricIndMsgBuf->buf, ricIndMsgBuf->size);
+ if(!ricIndMsgBuf->buf)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ break;
+ }
+ memset(ricIndMsgBuf->buf, 0, ricIndMsgBuf->size);
+ memcpy(ricIndMsgBuf->buf, encBuf, encBufSize);
+
+ ret = ROK;
+ break;
+ }
+
+ /* Free E2SM-KPM Indication Message */
+ FreeE2smKpmIndicationMessage(&e2smKpmIndMsg);
+
+ return ret;
+}
+
+/*******************************************************************
+ *
+ * @brief Free E2SM-KPM Indication Header
+ *
+ * @details
+ *
+ * Function : FreeE2smKpmIndicationHeader
+ *
+ * Functionality: Free E2SM-KPM Indication Header
+ *
+ * @param E2SM-KPM Indication Header to be free
+ * @return void
+ *
+ ******************************************************************/
+void FreeE2smKpmIndicationHeader(E2SM_KPM_IndicationHeader_t *e2smKpmIndHdr)
+{
+ E2SM_KPM_IndicationHeader_Format1_t *format1 = NULLP;
+
+ if(e2smKpmIndHdr)
+ {
+ switch(e2smKpmIndHdr->indicationHeader_formats.present)
+ {
+ case E2SM_KPM_IndicationHeader__indicationHeader_formats_PR_indicationHeader_Format1:
+ {
+ if(e2smKpmIndHdr->indicationHeader_formats.choice.indicationHeader_Format1)
+ {
+ format1 = e2smKpmIndHdr->indicationHeader_formats.choice.indicationHeader_Format1;
+
+ DU_FREE(format1->colletStartTime.buf, format1->colletStartTime.size);
+ DU_FREE(format1, sizeof(E2SM_KPM_IndicationHeader_Format1_t));
+ }
+ break;
+ }
+ case E2SM_KPM_IndicationHeader__indicationHeader_formats_PR_NOTHING:
+ default:
+ break;
+ }
+ }
+}
+
+/*******************************************************************
+ *
+ * @brief Fill RIC Indication Header buffer
+ *
+ * @details
+ *
+ * Function : fillRicIndHeader
+ *
+ * Functionality: Fill E2SM-KPM Indication Header
+ * Encode this message and copy to RIC Indication Header buffer
+ *
+ * @param RIC Indication Header buffer to be filled
+ * Source RIC subscription info from E2AP DB
+ * @return ROK - success
+ * RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillRicIndHeader(RICindicationHeader_t *ricIndHdr, RicSubscription *ricSubsInfo)
+{
+ uint8_t ret = RFAILED;
+ uint8_t secBufIdx = 0, milliSecBufIdx = 0;
+ int8_t byteIdx = 0;
+ bool formatFailure = false;
+ RanFunction *ranFunc = NULLP;
+ ReportStartTime *startTime = NULLP;
+ E2SM_KPM_IndicationHeader_t e2smKpmIndHdr;
+ E2SM_KPM_IndicationHeader_Format1_t *format1 = NULLP;
+ asn_enc_rval_t encRetVal; /* Encoder return value */
+
+ while(true)
+ {
+ ranFunc = fetchRanFuncFromRanFuncId(ricSubsInfo->ranFuncId);
+ if(ranFunc == NULLP)
+ {
+ DU_LOG("\nERROR --> E2AP : RAN Function ID [%d] not found", ricSubsInfo->ranFuncId);
+ break;
+ }
+
+ memset(&e2smKpmIndHdr, 0, sizeof(E2SM_KPM_IndicationHeader_t));
+
+ e2smKpmIndHdr.indicationHeader_formats.present = ranFunc->ricIndicationHeaderFormat;
+ switch(e2smKpmIndHdr.indicationHeader_formats.present)
+ {
+ case E2SM_KPM_IndicationHeader__indicationHeader_formats_PR_indicationHeader_Format1:
+ {
+ DU_ALLOC(e2smKpmIndHdr.indicationHeader_formats.choice.indicationHeader_Format1, \
+ sizeof(E2SM_KPM_IndicationHeader_Format1_t));
+ if(!e2smKpmIndHdr.indicationHeader_formats.choice.indicationHeader_Format1)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ formatFailure = true;
+ break;
+ }
+ format1 = e2smKpmIndHdr.indicationHeader_formats.choice.indicationHeader_Format1;
+
+ /* Fetch reporting period start time from DB */
+ switch(ricSubsInfo->eventTriggerDefinition.formatType)
+ {
+ case 1:
+ {
+ startTime = &ricSubsInfo->eventTriggerDefinition.choice.format1.startTime;
+ }
+ }
+
+ format1->colletStartTime.size = 8 * sizeof(uint8_t);
+ DU_ALLOC(format1->colletStartTime.buf, format1->colletStartTime.size);
+ if(!format1->colletStartTime.buf)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ formatFailure = true;
+ break;
+ }
+
+ /* As per O-RAN.WG3.E2SM-KPM-R003-v03.00, section 8.3.12 and
+ * RFC 5905, section 6 :
+ * Time stamp has a 64-bit format where first 32-bit is seconds
+ * and next 32-bit is fraction in picosecond-level.
+ * This fraction has been rounded in microseconds.
+ *
+ * Hence,
+ * Storing 32-bit seconds at MSB 0-3 and
+ * 32-bit milliseconds at next 4 bytes i.e. bytes 4-7
+ */
+ secBufIdx = 0;
+ milliSecBufIdx = 4;
+ for(byteIdx = 3; byteIdx >= 0; byteIdx--)
+ {
+ format1->colletStartTime.buf[secBufIdx++] = startTime->timeInSec >> (8*byteIdx);
+ format1->colletStartTime.buf[milliSecBufIdx++] = startTime->timeInMilliSec >> (8*byteIdx);
+ }
+ break;
+ }
+
+ case E2SM_KPM_IndicationHeader__indicationHeader_formats_PR_NOTHING:
+ default:
+ {
+ DU_LOG("\nERROR --> E2AP : Only E2SM-KPM Indication Header Format 1 supported");
+ formatFailure = true;
+ break;
+ }
+ }
+
+ if(formatFailure)
+ break;
+
+ /* Encode E2SM-KPM Indication Header */
+ xer_fprint(stdout, &asn_DEF_E2SM_KPM_IndicationHeader, &e2smKpmIndHdr);
+ memset(encBuf, 0, ENC_BUF_MAX_LEN);
+ encBufSize = 0;
+ encRetVal = aper_encode(&asn_DEF_E2SM_KPM_IndicationHeader, 0, &e2smKpmIndHdr, PrepFinalEncBuf, encBuf);
+ if(encRetVal.encoded == ENCODE_FAIL)
+ {
+ DU_LOG("\nERROR --> E2AP : Could not encode E2SM-KPM Indication Header (at %s)\n",\
+ encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
+ break;
+ }
+ else
+ {
+ DU_LOG("\nDEBUG --> E2AP : Created APER encoded buffer for E2SM-KPM Indication Header \n");
+#ifdef DEBUG_ASN_PRINT
+ for(int i=0; i< encBufSize; i++)
+ {
+ printf("%x",encBuf[i]);
+ }
+#endif
+ }
+
+ /* Copy encoded string to RIC Indication Header buffer */
+ ricIndHdr->size = encBufSize;
+ DU_ALLOC(ricIndHdr->buf, ricIndHdr->size);
+ if(!ricIndHdr->buf)
+ {
+ DU_LOG("\nERROR --> E2AP : Memory allocation in [%s] at line [%d]", __func__, __LINE__);
+ break;
+ }
+ memset(ricIndHdr->buf, 0, ricIndHdr->size);
+ memcpy(ricIndHdr->buf, encBuf, encBufSize);
+ ret = ROK;
+ break;
+ }
+
+ /* Free E2SM-KPM Indication Header */
+ FreeE2smKpmIndicationHeader(&e2smKpmIndHdr);
+
+ return ret;
+}
+
+/*******************************************************************
+ *
+ * brief Fill the RIC Indication Message
+ *
+ * @details
+ *
+ * Function : fillRicIndication
+ *
+ * Functionality: Fills the RIC Indication Message
+ *
+ * @param RIC Indication Message to be filled
+ * RIC Subscription DB
+ * Action DB
+ * @return ROK - success
+ * RFAILED - failure
+ *
+ ******************************************************************/
+uint8_t fillRicIndication(RICindication_t *ricIndicationMsg, RicSubscription *ricSubscriptionInfo, ActionInfo *actionInfo)
+{
+ uint8_t elementCnt = 0, idx = 0;