diff --git a/bacnet-stack/include/bacstr.h b/bacnet-stack/include/bacstr.h index 77732fe7..a9b973bb 100644 --- a/bacnet-stack/include/bacstr.h +++ b/bacnet-stack/include/bacstr.h @@ -87,10 +87,12 @@ extern "C" { bool bitstring_copy( BACNET_BIT_STRING * dest, BACNET_BIT_STRING * src); - bool bitstring_same( BACNET_BIT_STRING * bitstring1, BACNET_BIT_STRING * bitstring2); + bool bitstring_init_ascii( + BACNET_BIT_STRING * bit_string, + const char *ascii); /* returns false if the string exceeds capacity initialize by using length=0 */ diff --git a/bacnet-stack/src/bacapp.c b/bacnet-stack/src/bacapp.c index 7405f64b..7a3ac2cb 100644 --- a/bacnet-stack/src/bacapp.c +++ b/bacnet-stack/src/bacapp.c @@ -1467,9 +1467,9 @@ bool bacapp_parse_application_data( (char *) argv); break; case BACNET_APPLICATION_TAG_BIT_STRING: - /* FIXME: how to parse a bit string? */ - status = false; - bitstring_init(&value->type.Bit_String); +#if PRINT_ENABLED + status = bitstring_init_ascii(&value->type.Bit_String, argv); +#endif break; case BACNET_APPLICATION_TAG_ENUMERATED: unsigned_long_value = strtoul(argv, NULL, 0); diff --git a/bacnet-stack/src/bacstr.c b/bacnet-stack/src/bacstr.c index 28a9a62f..bd965c04 100644 --- a/bacnet-stack/src/bacstr.c +++ b/bacnet-stack/src/bacstr.c @@ -230,6 +230,53 @@ bool bitstring_same( return false; } +#if PRINT_ENABLED +/* converts an null terminated ASCII string to an bitstring. + Expects "1,0,1,0,1,1" or "101011" as the bits + returns true if successfully converted and fits; false if too long */ +bool bitstring_init_ascii( + BACNET_BIT_STRING * bit_string, + const char *ascii) +{ + bool status = false; /* return value */ + unsigned index = 0; /* offset into buffer */ + uint8_t bit_number = 0; + + if (bit_string) { + bitstring_init(bit_string); + if (ascii[0] == 0) { + /* nothing to decode, so success! */ + status = true; + } else { + while (ascii[index] != 0) { + if (bit_number > bitstring_bits_capacity(bit_string)) { + /* too long of a string */ + status = false; + break; + } + if (ascii[index] == '1') { + bitstring_set_bit(bit_string, bit_number, true); + bit_number++; + status = true; + } else if (ascii[index] == '0') { + bitstring_set_bit(bit_string, bit_number, false); + bit_number++; + status = true; + } else { + /* skip non-numeric or alpha */ + index++; + continue; + } + /* next character */ + index++; + } + } + } + + return status; +} +#endif + #define CHARACTER_STRING_CAPACITY (MAX_CHARACTER_STRING_BYTES - 1) /* returns false if the string exceeds capacity initialize by using value=NULL */