Commit d15f6469 authored by Rúben Soares da Silva's avatar Rúben Soares da Silva Committed by Rúben Soares Silva

Fix packing/unpacking procedures for DCI payload with inversion

Add unitary test for DCI payload pack/unpack with random length/data
parent b564e57f
......@@ -587,39 +587,59 @@ uint8_t unpackarray(uint8_t **ppReadPackedMsg, void *array, uint16_t array_eleme
uint32_t pack_dci_payload(uint8_t payload[], uint16_t payloadSizeBits, uint8_t **out, uint8_t *end)
{
// Helper vars for DCI Payload
uint8_t dci_bytes_inverted[DCI_PAYLOAD_BYTE_LEN];
uint8_t dci_byte_len = (payloadSizeBits + 7) / 8;
#ifdef FAPI_BYTE_ORDERING_BIG_ENDIAN
// Helper vars for DCI Payload
uint8_t dci_bytes_inverted[DCI_PAYLOAD_BYTE_LEN] = {0};
uint8_t payload_internal[DCI_PAYLOAD_BYTE_LEN] = {0}; // Used to not edit the "outside" pointer
uint8_t rotation_bits = 0;
// Align the dci payload bits to the left on the payload buffer
uint64_t *dci_pdu = (uint64_t *)payload;
if (payloadSizeBits % 8 != 0) {
uint8_t rotation_bits = 8 - (payloadSizeBits % 8);
*dci_pdu = (*dci_pdu << rotation_bits);
rotation_bits = 8 - (payloadSizeBits % 8);
// Bit shifting value ( << )
uint64_t t = 0;
memcpy(&t, payload, dci_byte_len);
t = t << rotation_bits;
memcpy(payload_internal, &t, dci_byte_len);
} else {
// No rotation needed
memcpy(payload_internal, payload, dci_byte_len);
}
// Invert the byte order of the DCI Payload
for (int j = 0; j < dci_byte_len; j++) {
dci_bytes_inverted[j] = payload[(dci_byte_len - 1) - j];
dci_bytes_inverted[j] = payload_internal[(dci_byte_len - 1) - j];
}
return pusharray8(dci_bytes_inverted, DCI_PAYLOAD_BYTE_LEN, dci_byte_len, out, end);
#else
return pusharray8(payload, DCI_PAYLOAD_BYTE_LEN, dci_byte_len, out, end);
#endif
}
uint32_t unpack_dci_payload(uint8_t payload[], uint16_t payloadSizeBits, uint8_t **in, uint8_t *end)
{
// Pull the inverted DCI and invert it back
// Helper vars for DCI Payload
uint8_t dci_bytes_inverted[DCI_PAYLOAD_BYTE_LEN];
uint8_t dci_byte_len = (payloadSizeBits + 7) / 8;
#ifdef FAPI_BYTE_ORDERING_BIG_ENDIAN
uint8_t dci_bytes_inverted[DCI_PAYLOAD_BYTE_LEN] = {0};
// Get DCI array inverted
uint32_t pullresult = pullarray8(in, dci_bytes_inverted, DCI_PAYLOAD_BYTE_LEN, dci_byte_len, end);
uint64_t *dci_pdu = (uint64_t *)payload;
// Reversing the byte order of the inverted DCI payload
for (uint16_t j = 0; j < dci_byte_len; j++) {
payload[j] = dci_bytes_inverted[(dci_byte_len - 1) - j];
}
uint64_t t = 0;
memcpy(&t, payload, dci_byte_len);
if (payloadSizeBits % 8 != 0) {
uint8_t rotation_bits = 8 - (payloadSizeBits % 8);
*dci_pdu = (*dci_pdu >> rotation_bits);
t = (t >> (uint64_t)rotation_bits);
}
memcpy(payload, &t, dci_byte_len);
#else
uint32_t pullresult = pullarray8(in, payload, DCI_PAYLOAD_BYTE_LEN, dci_byte_len, end);
#endif
return pullresult;
}
......
if(ENABLE_TESTS)
add_subdirectory(p5)
add_subdirectory(p7)
endif()
set(Test_Labels fapi p7)
#Pack DCI test
add_executable(nr_fapi_dci_inversion_test nr_fapi_dci_inversion_test.c)
target_link_libraries(nr_fapi_dci_inversion_test PUBLIC nr_fapi_p5)
target_link_libraries(nr_fapi_dci_inversion_test PRIVATE pthread UTIL minimal_lib)
target_compile_definitions(nr_fapi_dci_inversion_test PRIVATE TEST="nr_fapi_test.h")
add_dependencies(tests nr_fapi_dci_inversion_test)
#set compile definitions to test payload inversion
target_compile_definitions(nfapi_common PRIVATE FAPI_BYTE_ORDERING_BIG_ENDIAN)
set(dci_labels dci ${Test_Labels})
add_test(NAME nr_fapi_dci_inversion_test
COMMAND nr_fapi_dci_inversion_test ${CMAKE_CURRENT_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set_tests_properties(nr_fapi_dci_inversion_test PROPERTIES LABELS "${dci_labels}")
/*
* 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
*/
/*! \file nfapi/tests/p5/nr_fapi_param_request_test.c
* \brief
* \author Ruben S. Silva
* \date 2024
* \version 0.1
* \company OpenAirInterface Software Alliance
* \email: contact@openairinterface.org, rsilva@allbesmart.pt
* \note
* \warning
*/
#include "nfapi/tests/nr_fapi_test.h"
#include "nr_fapi_p5.h"
#include "nr_fapi.h"
void printbits(uint64_t n, uint8_t numBytesToPrint)
{
uint64_t i;
uint8_t counter = 0;
if (numBytesToPrint == 0) {
i = 1UL << (sizeof(n) * 8 - 1);
} else {
i = 1UL << (numBytesToPrint * 8 - 1);
}
while (i > 0) {
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
counter++;
if (counter % 8 == 0) {
printf(" ");
}
}
}
void truncate_unwanted_bits(uint8_t payloadSizeBits, uint8_t payload[])
{
uint8_t payloadSizeBytes = (payloadSizeBits + 7) / 8;
printf("Original Value:\t");
uint64_t t = 0;
memcpy(&t, payload, payloadSizeBytes);
printbits(t, payloadSizeBytes);
printf("\n");
uint64_t bitmask = 1;
for (int i = 0; i < payloadSizeBits - 1; i++) {
bitmask = bitmask << 1 | 1;
}
printf("Calculated Bitmask:\t");
printbits(bitmask, payloadSizeBytes);
printf("\n");
t = t & bitmask;
printf("Truncated Value:\t");
printbits(t, payloadSizeBytes);
printf("\n");
memcpy(payload, &t, payloadSizeBytes);
}
int main(int n, char *v[])
{
fapi_test_init();
uint8_t msg_buf[8192] = {0};
uint8_t *pWritePackedMessage = msg_buf;
uint8_t *pPackMessageEnd = msg_buf + sizeof(msg_buf);
uint8_t upper = 8 * DCI_PAYLOAD_BYTE_LEN;
uint8_t lower = 1;
uint16_t payloadSizeBits = (rand() % (upper - lower + 1)) + lower; // from 1 bit to DCI_PAYLOAD_BYTE_LEN, in bits
printf("payloadSizeBits:%d\n", payloadSizeBits);
uint8_t payloadSizeBytes = (payloadSizeBits + 7) / 8;
uint8_t payload[payloadSizeBytes];
for (int i = 0; i < payloadSizeBytes; ++i) {
payload[i] = rand8();
}
truncate_unwanted_bits(payloadSizeBits, payload);
pack_dci_payload(payload, payloadSizeBits, &pWritePackedMessage, pPackMessageEnd);
uint8_t *unpack_buf = calloc_or_fail(payloadSizeBytes, sizeof(uint8_t));
pWritePackedMessage = msg_buf;
unpack_dci_payload(unpack_buf, payloadSizeBits, &pWritePackedMessage, pPackMessageEnd);
printf("\nOriginal:\t");
for (int j = payloadSizeBytes - 1; j >= 0; j--) {
printbits(payload[j], 1);
printf(" ");
}
printf("\n");
printf("Unpacked:\t");
for (int j = payloadSizeBytes - 1; j >= 0; j--) {
printbits(unpack_buf[j],1);
printf(" ");
}
printf("\n");
DevAssert(memcmp(payload, unpack_buf, payloadSizeBytes) == 0);
// All tests successful!
free(unpack_buf);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment