From 3fc784cda5cb64dd7e5d1db722a5ef35b2e96862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BAben=20Soares=20Silva?= <rsilva@allbesmart.pt> Date: Fri, 27 Sep 2024 16:46:15 +0100 Subject: [PATCH] Move functions for packing/unpacking SRS.indication Changed parameter in unpack_nr_srs_indication to void * to be inline with other unpack procedures. Added memory allocation for pdu_list inside unpack function. Add unitary test for SRS.indication( test pack/unpack, free, copy and compare ) Added rands16 to provide random int16_t values, needed for SRS.indication unitary test Added constant NFAPI_NR_SRS_IND_MAX_PDU to follow constants related to other P7 messages limiting the maximum number of PDUs to use. --- nfapi/open-nFAPI/fapi/inc/nr_fapi_p7.h | 3 + nfapi/open-nFAPI/fapi/src/nr_fapi_p7.c | 111 ++++++++++++++++ .../nfapi/public_inc/nfapi_nr_interface_scf.h | 2 +- .../open-nFAPI/nfapi/public_inc/nr_nfapi_p7.h | 7 -- nfapi/open-nFAPI/nfapi/src/nfapi_p7.c | 105 ---------------- nfapi/tests/nr_fapi_test.h | 5 + nfapi/tests/p7/CMakeLists.txt | 2 +- nfapi/tests/p7/nr_fapi_srs_indication_test.c | 119 ++++++++++++++++++ 8 files changed, 240 insertions(+), 114 deletions(-) create mode 100644 nfapi/tests/p7/nr_fapi_srs_indication_test.c diff --git a/nfapi/open-nFAPI/fapi/inc/nr_fapi_p7.h b/nfapi/open-nFAPI/fapi/inc/nr_fapi_p7.h index 4b6ca848ebf..e1a4321a1bf 100644 --- a/nfapi/open-nFAPI/fapi/inc/nr_fapi_p7.h +++ b/nfapi/open-nFAPI/fapi/inc/nr_fapi_p7.h @@ -48,4 +48,7 @@ uint8_t pack_nr_crc_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *e uint8_t unpack_nr_crc_indication(uint8_t **ppReadPackedMsg, uint8_t *end, void *msg, nfapi_p7_codec_config_t *config); uint8_t pack_nr_uci_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); uint8_t unpack_nr_uci_indication(uint8_t **ppReadPackedMsg, uint8_t *end, void *msg, nfapi_p7_codec_config_t *config); +uint8_t pack_nr_srs_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); +uint8_t unpack_nr_srs_indication(uint8_t **ppReadPackedMsg, uint8_t *end, void *msg, nfapi_p7_codec_config_t *config); + #endif // OPENAIRINTERFACE_NR_FAPI_P7_H diff --git a/nfapi/open-nFAPI/fapi/src/nr_fapi_p7.c b/nfapi/open-nFAPI/fapi/src/nr_fapi_p7.c index e6cab2d09f9..9958a463a82 100644 --- a/nfapi/open-nFAPI/fapi/src/nr_fapi_p7.c +++ b/nfapi/open-nFAPI/fapi/src/nr_fapi_p7.c @@ -55,6 +55,9 @@ uint8_t fapi_nr_p7_message_body_pack(nfapi_nr_p7_message_header_t *header, case NFAPI_NR_PHY_MSG_TYPE_UCI_INDICATION: result = pack_nr_uci_indication(header, ppWritePackedMsg, end, config); break; + case NFAPI_NR_PHY_MSG_TYPE_SRS_INDICATION: + result = pack_nr_srs_indication(header, ppWritePackedMsg, end, config); + break; default: { if (header->message_id >= NFAPI_VENDOR_EXT_MSG_MIN && header->message_id <= NFAPI_VENDOR_EXT_MSG_MAX) { if (config && config->pack_p7_vendor_extension) { @@ -192,6 +195,11 @@ int fapi_nr_p7_message_unpack(void *pMessageBuf, result = unpack_nr_uci_indication(&pReadPackedMessage, end, pMessageHeader, config); } break; + case NFAPI_NR_PHY_MSG_TYPE_SRS_INDICATION: + if (check_nr_fapi_unpack_length(NFAPI_NR_PHY_MSG_TYPE_SRS_INDICATION, unpackedBufLen)) { + result = unpack_nr_srs_indication(&pReadPackedMessage, end, pMessageHeader, config); + } + break; default: if (pMessageHeader->message_id >= NFAPI_VENDOR_EXT_MSG_MIN && pMessageHeader->message_id <= NFAPI_VENDOR_EXT_MSG_MAX) { @@ -2210,3 +2218,106 @@ uint8_t unpack_nr_uci_indication(uint8_t **ppReadPackedMsg, uint8_t *end, void * return 1; } + +static uint8_t pack_nr_srs_report_tlv(const nfapi_srs_report_tlv_t *report_tlv, uint8_t **ppWritePackedMsg, uint8_t *end) { + + if(!(push16(report_tlv->tag, ppWritePackedMsg, end) && + push32(report_tlv->length, ppWritePackedMsg, end))) { + return 0; + } + + for (int i = 0; i < (report_tlv->length + 3) / 4; i++) { + if (!push32(report_tlv->value[i], ppWritePackedMsg, end)) { + return 0; + } + } + + return 1; +} + +static uint8_t pack_nr_srs_indication_body(const nfapi_nr_srs_indication_pdu_t *value, uint8_t **ppWritePackedMsg, uint8_t *end) { + + if(!(push32(value->handle, ppWritePackedMsg, end) && + push16(value->rnti, ppWritePackedMsg, end) && + push16(value->timing_advance_offset, ppWritePackedMsg, end) && + pushs16(value->timing_advance_offset_nsec, ppWritePackedMsg, end) && + push8(value->srs_usage, ppWritePackedMsg, end) && + push8(value->report_type, ppWritePackedMsg, end))) { + return 0; + } + + if (!pack_nr_srs_report_tlv(&value->report_tlv, ppWritePackedMsg, end)) { + return 0; + } + + return 1; +} + +uint8_t pack_nr_srs_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config) +{ + nfapi_nr_srs_indication_t *pNfapiMsg = (nfapi_nr_srs_indication_t *)msg; + + if (!(push16(pNfapiMsg->sfn, ppWritePackedMsg, end) && push16(pNfapiMsg->slot, ppWritePackedMsg, end) + && push16(pNfapiMsg->control_length, ppWritePackedMsg, end) && push8(pNfapiMsg->number_of_pdus, ppWritePackedMsg, end))) { + return 0; + } + + for (int i = 0; i < pNfapiMsg->number_of_pdus; i++) { + if (!pack_nr_srs_indication_body(&(pNfapiMsg->pdu_list[i]), ppWritePackedMsg, end)) { + return 0; + } + } + + return 1; +} + +static uint8_t unpack_nr_srs_report_tlv(nfapi_srs_report_tlv_t *report_tlv, uint8_t **ppReadPackedMsg, uint8_t *end) { + + if(!(pull16(ppReadPackedMsg, &report_tlv->tag, end) && + pull32(ppReadPackedMsg, &report_tlv->length, end))) { + return 0; + } +#ifndef ENABLE_AERIAL + for (int i = 0; i < (report_tlv->length + 3) / 4; i++) { + if (!pull32(ppReadPackedMsg, &report_tlv->value[i], end)) { + return 0; + } + } +#endif + return 1; +} + +static uint8_t unpack_nr_srs_indication_body(nfapi_nr_srs_indication_pdu_t *value, uint8_t **ppReadPackedMsg, uint8_t *end) { + + if(!(pull32(ppReadPackedMsg, &value->handle, end) && + pull16(ppReadPackedMsg, &value->rnti, end) && + pull16(ppReadPackedMsg, &value->timing_advance_offset, end) && + pulls16(ppReadPackedMsg, &value->timing_advance_offset_nsec, end) && + pull8(ppReadPackedMsg, &value->srs_usage, end) && + pull8(ppReadPackedMsg, &value->report_type, end))) { + return 0; + } + + if (!unpack_nr_srs_report_tlv(&value->report_tlv, ppReadPackedMsg, end)) { + return 0; + } + + return 1; +} + +uint8_t unpack_nr_srs_indication(uint8_t **ppReadPackedMsg, uint8_t *end, void *msg, nfapi_p7_codec_config_t *config) +{ + nfapi_nr_srs_indication_t *pNfapiMsg = (nfapi_nr_srs_indication_t *)msg; + if (!(pull16(ppReadPackedMsg, &pNfapiMsg->sfn, end) && pull16(ppReadPackedMsg, &pNfapiMsg->slot, end) + && pull16(ppReadPackedMsg, &pNfapiMsg->control_length, end) && pull8(ppReadPackedMsg, &pNfapiMsg->number_of_pdus, end))) { + return 0; + } + pNfapiMsg->pdu_list = calloc(pNfapiMsg->number_of_pdus, sizeof(*pNfapiMsg->pdu_list)); + for (int i = 0; i < pNfapiMsg->number_of_pdus; i++) { + if (!unpack_nr_srs_indication_body(&pNfapiMsg->pdu_list[i], ppReadPackedMsg, end)) { + return 0; + } + } + + return 1; +} diff --git a/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h b/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h index afd9e3d67ca..6a5a6ec1f09 100644 --- a/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h +++ b/nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface_scf.h @@ -1829,7 +1829,7 @@ typedef struct { } nfapi_nr_srs_beamforming_report_t; // SRS indication - +#define NFAPI_NR_SRS_IND_MAX_PDU 100 typedef struct { uint16_t tag; // 0: Report is carried directly in the value field; 3: The offset from the end of the control portion of the message to the beginning of the report. Other values are reserved. uint32_t length; // Length of the actual report in bytes, without the padding bytes. diff --git a/nfapi/open-nFAPI/nfapi/public_inc/nr_nfapi_p7.h b/nfapi/open-nFAPI/nfapi/public_inc/nr_nfapi_p7.h index 67fe0157295..1ab1a9f9691 100644 --- a/nfapi/open-nFAPI/nfapi/public_inc/nr_nfapi_p7.h +++ b/nfapi/open-nFAPI/nfapi/public_inc/nr_nfapi_p7.h @@ -36,17 +36,10 @@ void *nfapi_p7_allocate(size_t size, nfapi_p7_codec_config_t *config); -uint8_t unpack_nr_srs_indication(uint8_t **ppReadPackedMsg, - uint8_t *end, - nfapi_nr_srs_indication_t *pNfapiMsg, - nfapi_p7_codec_config_t *config); - uint8_t pack_ue_release_request(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); uint8_t pack_ue_release_response(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); -uint8_t pack_nr_srs_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); - uint8_t pack_nr_rach_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); uint8_t pack_nr_dl_node_sync(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config); diff --git a/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c b/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c index c2cf3b2439d..bff9186c5b0 100644 --- a/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c +++ b/nfapi/open-nFAPI/nfapi/src/nfapi_p7.c @@ -2597,58 +2597,6 @@ int pack_nr_srs_beamforming_report(void *pMessageBuf, void *pPackedBuf, uint32_t return (msgEnd - msgHead); } -static uint8_t pack_nr_srs_report_tlv(nfapi_srs_report_tlv_t *report_tlv, uint8_t **ppWritePackedMsg, uint8_t *end) { - - if(!(push16(report_tlv->tag, ppWritePackedMsg, end) && - push32(report_tlv->length, ppWritePackedMsg, end))) { - return 0; - } -#ifndef ENABLE_AERIAL - for (int i = 0; i < (report_tlv->length + 3) / 4; i++) { - if (!push32(report_tlv->value[i], ppWritePackedMsg, end)) { - return 0; - } - } -#endif - return 1; -} - -static uint8_t pack_nr_srs_indication_body(nfapi_nr_srs_indication_pdu_t *value, uint8_t **ppWritePackedMsg, uint8_t *end) { - - if(!(push32(value->handle, ppWritePackedMsg, end) && - push16(value->rnti, ppWritePackedMsg, end) && - push16(value->timing_advance_offset, ppWritePackedMsg, end) && - pushs16(value->timing_advance_offset_nsec, ppWritePackedMsg, end) && - push8(value->srs_usage, ppWritePackedMsg, end) && - push8(value->report_type, ppWritePackedMsg, end))) { - return 0; - } - - if (!pack_nr_srs_report_tlv(&value->report_tlv, ppWritePackedMsg, end)) { - return 0; - } - - return 1; -} - -uint8_t pack_nr_srs_indication(void *msg, uint8_t **ppWritePackedMsg, uint8_t *end, nfapi_p7_codec_config_t *config) -{ - nfapi_nr_srs_indication_t *pNfapiMsg = (nfapi_nr_srs_indication_t *)msg; - - if (!(push16(pNfapiMsg->sfn, ppWritePackedMsg, end) && push16(pNfapiMsg->slot, ppWritePackedMsg, end) - && push16(pNfapiMsg->control_length, ppWritePackedMsg, end) && push8(pNfapiMsg->number_of_pdus, ppWritePackedMsg, end))) { - return 0; - } - - for (int i = 0; i < pNfapiMsg->number_of_pdus; i++) { - if (!pack_nr_srs_indication_body(&(pNfapiMsg->pdu_list[i]), ppWritePackedMsg, end)) { - return 0; - } - } - - return 1; -} - //RACH INDICATION static uint8_t pack_nr_rach_indication_body(void* tlv, uint8_t **ppWritePackedMsg, uint8_t *end) @@ -4562,59 +4510,6 @@ int unpack_nr_srs_beamforming_report(void *pMessageBuf, uint32_t messageBufLen, return 0; } -static uint8_t unpack_nr_srs_report_tlv(nfapi_srs_report_tlv_t *report_tlv, uint8_t **ppReadPackedMsg, uint8_t *end) { - - if(!(pull16(ppReadPackedMsg, &report_tlv->tag, end) && - pull32(ppReadPackedMsg, &report_tlv->length, end))) { - return 0; - } - - for (int i = 0; i < (report_tlv->length + 3) / 4; i++) { - if (!pull32(ppReadPackedMsg, &report_tlv->value[i], end)) { - return 0; - } - } - - return 1; -} - -static uint8_t unpack_nr_srs_indication_body(nfapi_nr_srs_indication_pdu_t *value, uint8_t **ppReadPackedMsg, uint8_t *end) { - - if(!(pull32(ppReadPackedMsg, &value->handle, end) && - pull16(ppReadPackedMsg, &value->rnti, end) && - pull16(ppReadPackedMsg, &value->timing_advance_offset, end) && - pulls16(ppReadPackedMsg, &value->timing_advance_offset_nsec, end) && - pull8(ppReadPackedMsg, &value->srs_usage, end) && - pull8(ppReadPackedMsg, &value->report_type, end))) { - return 0; - } - - if (!unpack_nr_srs_report_tlv(&value->report_tlv, ppReadPackedMsg, end)) { - return 0; - } - - return 1; -} - -uint8_t unpack_nr_srs_indication(uint8_t **ppReadPackedMsg, - uint8_t *end, - nfapi_nr_srs_indication_t *pNfapiMsg, - nfapi_p7_codec_config_t *config) -{ - if (!(pull16(ppReadPackedMsg, &pNfapiMsg->sfn, end) && pull16(ppReadPackedMsg, &pNfapiMsg->slot, end) - && pull16(ppReadPackedMsg, &pNfapiMsg->control_length, end) && pull8(ppReadPackedMsg, &pNfapiMsg->number_of_pdus, end))) { - return 0; - } - - for (int i = 0; i < pNfapiMsg->number_of_pdus; i++) { - if (!unpack_nr_srs_indication_body(&pNfapiMsg->pdu_list[i], ppReadPackedMsg, end)) { - return 0; - } - } - - return 1; -} - //NR RACH static uint8_t unpack_nr_rach_indication_body(nfapi_nr_prach_indication_pdu_t *value, diff --git a/nfapi/tests/nr_fapi_test.h b/nfapi/tests/nr_fapi_test.h index 6681a74f241..647cdf94f29 100644 --- a/nfapi/tests/nr_fapi_test.h +++ b/nfapi/tests/nr_fapi_test.h @@ -81,6 +81,11 @@ uint16_t rand16_range(uint16_t lower, uint16_t upper) return (rand() % (upper - lower + 1)) + lower; } +int16_t rands16_range(int16_t lower, int16_t upper) +{ + return (rand() % (upper - lower + 1)) + lower; +} + uint32_t rand32_range(uint32_t lower, uint32_t upper) { return (rand() % (upper - lower + 1)) + lower; diff --git a/nfapi/tests/p7/CMakeLists.txt b/nfapi/tests/p7/CMakeLists.txt index c516634345f..d697207c432 100644 --- a/nfapi/tests/p7/CMakeLists.txt +++ b/nfapi/tests/p7/CMakeLists.txt @@ -1,5 +1,5 @@ set(Test_Labels fapi p7) -set(_fapi_p7_messages "dci_inversion;dl_tti_request;ul_tti_request;slot_indication;ul_dci_request;tx_data_request;rx_data_indication;crc_indication;uci_indication") +set(_fapi_p7_messages "dci_inversion;dl_tti_request;ul_tti_request;slot_indication;ul_dci_request;tx_data_request;rx_data_indication;crc_indication;uci_indication;srs_indication") foreach (fapi_p7_message IN LISTS _fapi_p7_messages) add_executable(nr_fapi_${fapi_p7_message}_test nr_fapi_${fapi_p7_message}_test.c) target_link_libraries(nr_fapi_${fapi_p7_message}_test PUBLIC nr_fapi_p7) diff --git a/nfapi/tests/p7/nr_fapi_srs_indication_test.c b/nfapi/tests/p7/nr_fapi_srs_indication_test.c new file mode 100644 index 00000000000..49b73fd8f5c --- /dev/null +++ b/nfapi/tests/p7/nr_fapi_srs_indication_test.c @@ -0,0 +1,119 @@ +/* + * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The OpenAirInterface Software Alliance licenses this file to You under + * the OAI Public License, Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.openairinterface.org/?page_id=698 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------------------- + * For more information about the OpenAirInterface (OAI) Software Alliance: + * contact@openairinterface.org + */ +#include "dci_payload_utils.h" +#include "nr_fapi_p7.h" +#include "nr_fapi_p7_utils.h" + +static void fil_srs_indication_report_tlv(nfapi_srs_report_tlv_t *tlv) +{ + tlv->tag = rand16_range(0,3); + tlv->length = rand32_range(0,sizeof(tlv->value)); + for (int i = 0; i < (tlv->length + 3) / 4; ++i) { + tlv->value[i] = rand32(); + } +} + +static void fill_srs_indication_PDU(nfapi_nr_srs_indication_pdu_t *pdu) +{ + pdu->handle = rand32(); + pdu->rnti = rand16_range(1, 65535); + pdu->timing_advance_offset = rand16_range(0, 63); + pdu->timing_advance_offset_nsec = rands16_range(-16800, 16800); + pdu->srs_usage = rand8_range(0,3); + pdu->report_type = rand8_range(0,1); + fil_srs_indication_report_tlv(&pdu->report_tlv); +} + +static void fill_srs_indication(nfapi_nr_srs_indication_t *msg) +{ + msg->sfn = rand16_range(0, 1023); + msg->slot = rand16_range(0, 159); + msg->control_length = rand16(); // being ignored number_of_pdusat the moment, report is always sent inline + msg->number_of_pdus = rand8_range(1, NFAPI_NR_SRS_IND_MAX_PDU); // Minimum 1 PDUs in order to test at least one + msg->pdu_list = calloc_or_fail(msg->number_of_pdus, sizeof(*msg->pdu_list)); + for (int pdu_idx = 0; pdu_idx < msg->number_of_pdus; ++pdu_idx) { + fill_srs_indication_PDU(&msg->pdu_list[pdu_idx]); + } +} + +static void test_pack_unpack(nfapi_nr_srs_indication_t *req) +{ + size_t message_size = get_srs_indication_size(req); + uint8_t *msg_buf = calloc_or_fail(message_size, sizeof(uint8_t)); + /*uint8_t msg_buf[1024*1024*3]; + size_t message_size = sizeof(msg_buf);*/ + // first test the packing procedure + int pack_result = fapi_nr_p7_message_pack(req, msg_buf, message_size, NULL); + + DevAssert(pack_result >= 0 + NFAPI_HEADER_LENGTH); + // update req message_length value with value calculated in message_pack procedure + req->header.message_length = pack_result; //- NFAPI_HEADER_LENGTH; + // test the unpacking of the header + // copy first NFAPI_HEADER_LENGTH bytes into a new buffer, to simulate SCTP PEEK + fapi_message_header_t header; + uint32_t header_buffer_size = NFAPI_HEADER_LENGTH; + uint8_t header_buffer[header_buffer_size]; + for (int idx = 0; idx < header_buffer_size; idx++) { + header_buffer[idx] = msg_buf[idx]; + } + uint8_t *pReadPackedMessage = header_buffer; + int unpack_header_result = fapi_nr_message_header_unpack(&pReadPackedMessage, NFAPI_HEADER_LENGTH, &header, sizeof(header), 0); + DevAssert(unpack_header_result >= 0); + DevAssert(header.message_id == req->header.message_id); + DevAssert(header.message_length == req->header.message_length); + // test the unpacking and compare with initial message + nfapi_nr_srs_indication_t unpacked_req = {0}; + int unpack_result = + fapi_nr_p7_message_unpack(msg_buf, header.message_length + NFAPI_HEADER_LENGTH, &unpacked_req, sizeof(unpacked_req), 0); + DevAssert(unpack_result >= 0); + DevAssert(eq_srs_indication(&unpacked_req, req)); + free_srs_indication(&unpacked_req); + free(msg_buf); +} + +static void test_copy(const nfapi_nr_srs_indication_t *msg) +{ + // Test copy function + nfapi_nr_srs_indication_t copy = {0}; + copy_srs_indication(msg, ©); + DevAssert(eq_srs_indication(msg, ©)); + free_srs_indication(©); +} + +int main(int n, char *v[]) +{ + fapi_test_init(); + + nfapi_nr_srs_indication_t *req = calloc_or_fail(1, sizeof(nfapi_nr_srs_indication_t)); + req->header.message_id = NFAPI_NR_PHY_MSG_TYPE_SRS_INDICATION; + // Get the actual allocated size + printf("Allocated size before filling: %zu bytes\n", get_srs_indication_size(req)); + // Fill TX_DATA request + fill_srs_indication(req); + printf("Allocated size after filling: %zu bytes\n", get_srs_indication_size(req)); + // Perform tests + test_pack_unpack(req); + test_copy(req); + // All tests successful! + free_srs_indication(req); + free(req); + return 0; +} -- GitLab