diff --git a/bacnet-stack/ai.c b/bacnet-stack/ai.c index 1e11205f..c351c69e 100644 --- a/bacnet-stack/ai.c +++ b/bacnet-stack/ai.c @@ -96,11 +96,12 @@ int Analog_Input_Encode_Property_APDU( apdu_len = encode_tagged_real(&apdu[0], value); break; case PROP_STATUS_FLAGS: - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); break; case PROP_EVENT_STATE: apdu_len = encode_tagged_enumerated(&apdu[0],EVENT_STATE_NORMAL); diff --git a/bacnet-stack/ao.c b/bacnet-stack/ao.c index 1e17ea24..b63f5eb6 100644 --- a/bacnet-stack/ao.c +++ b/bacnet-stack/ao.c @@ -181,11 +181,12 @@ int Analog_Output_Encode_Property_APDU( apdu_len = encode_tagged_real(&apdu[0], real_value); break; case PROP_STATUS_FLAGS: - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); break; case PROP_EVENT_STATE: apdu_len = encode_tagged_enumerated(&apdu[0],EVENT_STATE_NORMAL); diff --git a/bacnet-stack/bacapp.c b/bacnet-stack/bacapp.c index 8ce0c11d..b2ff1229 100644 --- a/bacnet-stack/bacapp.c +++ b/bacnet-stack/bacapp.c @@ -267,18 +267,11 @@ bool bacapp_compare( test_length = characterstring_length(&test_value->type.Character_String); test_str = characterstring_value(&test_value->type.Character_String); if (length != test_length) - { status = false; - printf("length=%d test_length=%d\n",length,test_length); - } for (i = 0; i < test_length; i++) { if (str[i] != test_str[i]) - { status = false; - printf("str[%d]=%c test_str[%d]=%c\n", - i,str[i],i,test_str[i]); - } } } break; diff --git a/bacnet-stack/bacdcode.c b/bacnet-stack/bacdcode.c index 7a50bcb1..821140a9 100644 --- a/bacnet-stack/bacdcode.c +++ b/bacnet-stack/bacdcode.c @@ -596,36 +596,17 @@ int decode_bitstring(uint8_t * apdu, uint32_t len_value, len = 1; for (i = 0; i < bytes_used; i++) { - bit_string->value[i] = byte_reverse_bits(apdu[len++]); + bitstring_set_octet(bit_string, i, byte_reverse_bits(apdu[len++])); } unused_bits = apdu[0] & 0x07; - bit_string->bits_used = (uint8_t)(bytes_used * 8); - bit_string->bits_used -= unused_bits; + bitstring_set_bits_used(bit_string, + (uint8_t)bytes_used, unused_bits); } } return len; } -// returns the number of bytes that a bit string is using -static int bitstring_bytes_used(BACNET_BIT_STRING *bit_string) -{ - int len = 0; // return value - uint8_t used_bytes = 0; - uint8_t last_bit = 0; - - if (bit_string->bits_used) - { - last_bit = bit_string->bits_used - 1; - used_bytes = last_bit / 8; - // add one for the first byte - used_bytes++; - len = used_bytes; - } - - return len; -} - // from clause 20.2.10 Encoding of a Bit String Value // returns the number of apdu bytes consumed int encode_bitstring(uint8_t * apdu, BACNET_BIT_STRING *bit_string) @@ -636,17 +617,18 @@ int encode_bitstring(uint8_t * apdu, BACNET_BIT_STRING *bit_string) uint8_t i = 0; // if the bit string is empty, then the first octet shall be zero - if (bit_string->bits_used == 0) + if (bitstring_bits_used(bit_string) == 0) apdu[len++] = 0; else { used_bytes = bitstring_bytes_used(bit_string); - remaining_used_bits = bit_string->bits_used - ((used_bytes - 1) * 8); + remaining_used_bits = bitstring_bits_used(bit_string) - + ((used_bytes - 1) * 8); // number of unused bits in the subsequent final octet apdu[len++] = 8 - remaining_used_bits; for (i = 0; i < used_bytes; i++) { - apdu[len++] = byte_reverse_bits(bit_string->value[i]); + apdu[len++] = byte_reverse_bits(bitstring_octet(bit_string,i)); } } diff --git a/bacnet-stack/bacstr.c b/bacnet-stack/bacstr.c index a9b5f84d..bad1eb53 100644 --- a/bacnet-stack/bacstr.c +++ b/bacnet-stack/bacstr.c @@ -88,6 +88,78 @@ uint8_t bitstring_bits_used(BACNET_BIT_STRING *bit_string) return bit_string->bits_used; } +// returns the number of bytes that a bit string is using +int bitstring_bytes_used(BACNET_BIT_STRING *bit_string) +{ + int len = 0; // return value + uint8_t used_bytes = 0; + uint8_t last_bit = 0; + + if (bit_string->bits_used) + { + last_bit = bit_string->bits_used - 1; + used_bytes = last_bit / 8; + // add one for the first byte + used_bytes++; + len = used_bytes; + } + + return len; +} + +uint8_t bitstring_octet(BACNET_BIT_STRING *bit_string, uint8_t index) +{ + uint8_t octet = 0; + + if (bit_string) + { + if (index < MAX_BITSTRING_BYTES) + { + octet = bit_string->value[index]; + } + } + + return octet; +} + +bool bitstring_set_octet( + BACNET_BIT_STRING *bit_string, + uint8_t index, + uint8_t octet) +{ + bool status = false; + + if (bit_string) + { + if (index < MAX_BITSTRING_BYTES) + { + bit_string->value[index] = octet; + status = true; + } + } + + return status; +} + +bool bitstring_set_bits_used( + BACNET_BIT_STRING *bit_string, + uint8_t bytes_used, + uint8_t unused_bits) +{ + bool status = false; + + if (bit_string) + { + /* FIXME: check that bytes_used is at least one? */ + bit_string->bits_used = bytes_used * 8; + bit_string->bits_used -= unused_bits; + status = true; + } + + return status; +} + + uint8_t bitstring_bits_capacity(BACNET_BIT_STRING *bit_string) { if (bit_string) diff --git a/bacnet-stack/bacstr.h b/bacnet-stack/bacstr.h index 9f79d773..9ae9951b 100644 --- a/bacnet-stack/bacstr.h +++ b/bacnet-stack/bacstr.h @@ -73,7 +73,19 @@ void bitstring_init(BACNET_BIT_STRING *bit_string); void bitstring_set_bit(BACNET_BIT_STRING *bit_string, uint8_t bit, bool value); bool bitstring_bit(BACNET_BIT_STRING *bit_string, uint8_t bit); uint8_t bitstring_bits_used(BACNET_BIT_STRING *bit_string); +// returns the number of bytes that a bit string is using +int bitstring_bytes_used(BACNET_BIT_STRING *bit_string); uint8_t bitstring_bits_capacity(BACNET_BIT_STRING *bit_string); +/* used for encoding and decoding from the APDU */ +uint8_t bitstring_octet(BACNET_BIT_STRING *bit_string, uint8_t octet_index); +bool bitstring_set_octet( + BACNET_BIT_STRING *bit_string, + uint8_t index, + uint8_t octet); +bool bitstring_set_bits_used( + BACNET_BIT_STRING *bit_string, + uint8_t bytes_used, + uint8_t unused_bits); /* returns false if the string exceeds capacity initialize by using length=0 */