Secured I-Am request encoding and decoding, and updated the example apps and handlers to use secure version of I-Am decoder. (#1080)

This commit is contained in:
Steve Karg
2025-08-19 21:31:50 -05:00
committed by GitHub
parent 743c5845b3
commit 25c2aaa20d
9 changed files with 285 additions and 106 deletions
+76 -8
View File
@@ -26,7 +26,7 @@ static int iam_decode_apdu(
int *pSegmentation,
uint16_t *pVendor_id)
{
int apdu_len = 0; /* total length of the apdu, return value */
int apdu_len;
/* valid data? */
if (!apdu) {
@@ -39,7 +39,8 @@ static int iam_decode_apdu(
if (apdu[1] != SERVICE_UNCONFIRMED_I_AM) {
return -1;
}
apdu_len = iam_decode_service_request(
apdu_len = 2;
apdu_len += iam_decode_service_request(
&apdu[2], pDevice_id, pMax_apdu, pSegmentation, pVendor_id);
return apdu_len;
@@ -52,7 +53,7 @@ static void testIAm(void)
#endif
{
uint8_t apdu[480] = { 0 };
int len = 0;
int test_len = 0, null_len = 0, apdu_len;
uint32_t device_id = 42;
unsigned max_apdu = 480;
int segmentation = SEGMENTATION_NONE;
@@ -62,19 +63,86 @@ static void testIAm(void)
int test_segmentation = 0;
uint16_t test_vendor_id = 0;
len =
null_len =
iam_encode_apdu(NULL, device_id, max_apdu, segmentation, vendor_id);
apdu_len =
iam_encode_apdu(&apdu[0], device_id, max_apdu, segmentation, vendor_id);
zassert_not_equal(len, 0, NULL);
zassert_not_equal(apdu_len, 0, NULL);
zassert_equal(apdu_len, null_len, NULL);
len = iam_decode_apdu(
test_len = iam_decode_apdu(
&apdu[0], &test_device_id, &test_max_apdu, &test_segmentation,
&test_vendor_id);
zassert_not_equal(len, -1, NULL);
zassert_equal(
test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len);
zassert_equal(test_device_id, device_id, NULL);
zassert_equal(test_vendor_id, vendor_id, NULL);
zassert_equal(test_max_apdu, max_apdu, NULL);
zassert_equal(test_segmentation, segmentation, NULL);
null_len = bacnet_iam_request_encode(
NULL, device_id, max_apdu, segmentation, vendor_id);
apdu_len = bacnet_iam_request_encode(
&apdu[0], device_id, max_apdu, segmentation, vendor_id);
zassert_not_equal(apdu_len, 0, NULL);
zassert_equal(apdu_len, null_len, NULL);
test_len = bacnet_iam_request_decode(
&apdu[0], sizeof(apdu), &test_device_id, &test_max_apdu,
&test_segmentation, &test_vendor_id);
zassert_equal(test_len, apdu_len, NULL);
zassert_equal(test_device_id, device_id, NULL);
zassert_equal(test_vendor_id, vendor_id, NULL);
zassert_equal(test_max_apdu, max_apdu, NULL);
zassert_equal(test_segmentation, segmentation, NULL);
test_len = bacnet_iam_request_decode(
NULL, apdu_len, &test_device_id, &test_max_apdu, &test_segmentation,
&test_vendor_id);
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
test_len = bacnet_iam_request_decode(
&apdu[0], apdu_len, NULL, &test_max_apdu, &test_segmentation,
&test_vendor_id);
zassert_equal(test_len, apdu_len, NULL);
test_len = bacnet_iam_request_decode(
&apdu[0], apdu_len, &test_device_id, NULL, &test_segmentation,
&test_vendor_id);
zassert_equal(test_len, apdu_len, NULL);
test_len = bacnet_iam_request_decode(
&apdu[0], apdu_len, &test_device_id, &test_max_apdu, NULL,
&test_vendor_id);
zassert_equal(test_len, apdu_len, NULL);
test_len = bacnet_iam_request_decode(
&apdu[0], apdu_len, &test_device_id, &test_max_apdu, &test_segmentation,
NULL);
zassert_equal(test_len, apdu_len, NULL);
while (apdu_len) {
apdu_len--;
test_len = bacnet_iam_request_decode(
&apdu[0], apdu_len, &test_device_id, &test_max_apdu,
&test_segmentation, &test_vendor_id);
zassert_equal(
test_len, BACNET_STATUS_ERROR, "apdu_len=%d test_len=%d", apdu_len,
test_len);
}
apdu_len = bacnet_iam_service_request_encode(
apdu, sizeof(apdu), device_id, max_apdu, segmentation, vendor_id);
zassert_not_equal(apdu_len, 0, NULL);
zassert_equal(apdu_len, null_len, NULL);
while (apdu_len) {
apdu_len--;
test_len = bacnet_iam_service_request_encode(
apdu, apdu_len, device_id, max_apdu, segmentation, vendor_id);
zassert_equal(test_len, 0, NULL);
}
/* internal bounds checking - segmentation enumeration */
apdu_len = bacnet_iam_request_encode(
&apdu[0], device_id, max_apdu, MAX_BACNET_SEGMENTATION, vendor_id);
zassert_not_equal(apdu_len, 0, NULL);
test_len = bacnet_iam_request_decode(
&apdu[0], sizeof(apdu), &test_device_id, &test_max_apdu,
&test_segmentation, &test_vendor_id);
zassert_equal(
test_len, BACNET_STATUS_ERROR, "apdu_len=%d test_len=%d", apdu_len,
test_len);
}
/**
* @}