From 120c2734ee49855c33f102cd3faff9064951f9c1 Mon Sep 17 00:00:00 2001 From: skarg Date: Sat, 17 Sep 2011 04:21:32 +0000 Subject: [PATCH] Added ascii-hex conversion in the octet-string library to allow ascii-hex to be parsed correctly from demo applications. --- bacnet-stack/include/bacstr.h | 9 ++++++- bacnet-stack/src/bacapp.c | 5 ++-- bacnet-stack/src/bacstr.c | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/bacnet-stack/include/bacstr.h b/bacnet-stack/include/bacstr.h index 45996956..f13e45d4 100644 --- a/bacnet-stack/include/bacstr.h +++ b/bacnet-stack/include/bacstr.h @@ -162,7 +162,14 @@ extern "C" { BACNET_OCTET_STRING * octet_string, uint8_t * value, size_t length); - bool octetstring_copy( +#ifdef PRINT_ENABLED + /* converts an null terminated ASCII Hex string to an octet string. + returns true if successfully converted and fits; false if too long */ + bool octetstring_init_ascii_hex( + BACNET_OCTET_STRING * octet_string, + const char * ascii_hex); +#endif +bool octetstring_copy( BACNET_OCTET_STRING * dest, BACNET_OCTET_STRING * src); /* returns false if the string exceeds capacity */ diff --git a/bacnet-stack/src/bacapp.c b/bacnet-stack/src/bacapp.c index 67e74282..3611c29b 100644 --- a/bacnet-stack/src/bacapp.c +++ b/bacnet-stack/src/bacapp.c @@ -45,6 +45,7 @@ #include "bacapp.h" #include "bactext.h" #include "datetime.h" +#include "bacstr.h" /** @file bacapp.c Utilities for the BACnet_Application_Data_Value */ @@ -1188,8 +1189,8 @@ bool bacapp_parse_application_data( #endif case BACNET_APPLICATION_TAG_OCTET_STRING: status = - octetstring_init(&value->type.Octet_String, - (uint8_t *) argv, strlen(argv)); + octetstring_init_ascii_hex(&value->type.Octet_String, + argv); break; case BACNET_APPLICATION_TAG_CHARACTER_STRING: status = diff --git a/bacnet-stack/src/bacstr.c b/bacnet-stack/src/bacstr.c index 3d02b79f..ab11bda3 100644 --- a/bacnet-stack/src/bacstr.c +++ b/bacnet-stack/src/bacstr.c @@ -35,6 +35,10 @@ #include #include #include /* for strlen */ +#ifdef PRINT_ENABLED +#include /* for strtol */ +#include /* for isalnum */ +#endif #include "config.h" #include "bacstr.h" #include "bits.h" @@ -641,6 +645,49 @@ bool octetstring_init( return status; } +#ifdef PRINT_ENABLED +/* converts an null terminated ASCII Hex string to an octet string. + returns true if successfully converted and fits; false if too long */ +bool octetstring_init_ascii_hex( + BACNET_OCTET_STRING * octet_string, + const char * ascii_hex) +{ + bool status = false; /* return value */ + unsigned index = 0; /* offset into buffer */ + uint8_t value = 0; + char hex_pair_string[3] = ""; + + if (octet_string) { + octet_string->length = 0; + while (ascii_hex[index] != 0) { + if (!isalnum(ascii_hex[index])) { + /* skip non-numeric or alpha */ + index++; + continue; + } + if (ascii_hex[index+1] == 0) { + break; + } + hex_pair_string[0] = ascii_hex[index]; + hex_pair_string[1] = ascii_hex[index+1]; + value = (uint8_t)strtol(hex_pair_string, NULL, 16); + if (octet_string->length <= MAX_OCTET_STRING_BYTES) { + octet_string->value[octet_string->length] = value; + octet_string->length++; + status = true; + } else { + break; + status = false; + } + /* set up for next pair */ + index += 2; + } + } + + return status; +} +#endif + bool octetstring_copy( BACNET_OCTET_STRING * dest, BACNET_OCTET_STRING * src)