Added parser for command line parsing of a BACnet BitString. This is nice if you want to write Event Enable, for example. Expects the ASCII bits as "1011001" or "1,0,1,1,0,0,1" or "1 0 1 1 0 0 1".
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user