Moved bitstring handling to its own module - bacstr.c
This commit is contained in:
+1
-50
@@ -39,6 +39,7 @@
|
||||
#include "bacenum.h"
|
||||
#include "bits.h"
|
||||
#include "bigend.h"
|
||||
#include "bacstr.h"
|
||||
|
||||
// NOTE: byte order plays a role in decoding multibyte values
|
||||
// http://www.unixpapa.com/incnote/byteorder.html
|
||||
@@ -273,56 +274,6 @@ int decode_unsigned32(uint8_t * apdu, uint32_t *value)
|
||||
return 4;
|
||||
}
|
||||
|
||||
void bitstring_init(BACNET_BIT_STRING *bit_string)
|
||||
{
|
||||
int i;
|
||||
|
||||
bit_string->bits_used = 0;
|
||||
for (i = 0; i < MAX_BITSTRING_BYTES; i++)
|
||||
{
|
||||
bit_string->value[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void bitstring_set_bit(BACNET_BIT_STRING *bit_string, uint8_t bit, bool value)
|
||||
{
|
||||
uint8_t byte_number = bit/8;
|
||||
uint8_t bit_mask = 1;
|
||||
|
||||
if (byte_number < MAX_BITSTRING_BYTES)
|
||||
{
|
||||
// set max bits used
|
||||
if (bit_string->bits_used < (bit + 1))
|
||||
bit_string->bits_used = bit + 1;
|
||||
bit_mask = bit_mask << (bit - (byte_number * 8));
|
||||
if (value)
|
||||
bit_string->value[byte_number] |= bit_mask;
|
||||
else
|
||||
bit_string->value[byte_number] &= (~(bit_mask));
|
||||
}
|
||||
}
|
||||
|
||||
bool bitstring_bit(BACNET_BIT_STRING *bit_string, uint8_t bit)
|
||||
{
|
||||
bool value = false;
|
||||
uint8_t byte_number = bit/8;
|
||||
uint8_t bit_mask = 1;
|
||||
|
||||
if (bit < (MAX_BITSTRING_BYTES * 8))
|
||||
{
|
||||
bit_mask = bit_mask << (bit - (byte_number * 8));
|
||||
if (bit_string->value[byte_number] & bit_mask)
|
||||
value = true;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint8_t bitstring_bits_used(BACNET_BIT_STRING *bit_string)
|
||||
{
|
||||
return bit_string->bits_used;
|
||||
}
|
||||
|
||||
// from clause 20.2.1 General Rules for Encoding BACnet Tags
|
||||
// returns the number of apdu bytes consumed
|
||||
int encode_tag(uint8_t * apdu, uint8_t tag_number, bool context_specific,
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "bacdef.h"
|
||||
#include "bacstr.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -73,10 +74,6 @@ int encode_context_boolean(uint8_t * apdu, bool boolean_value);
|
||||
bool decode_context_boolean(uint8_t * apdu);
|
||||
|
||||
// from clause 20.2.10 Encoding of a Bit String Value
|
||||
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 apdu bytes consumed
|
||||
int decode_bitstring(uint8_t * apdu, uint32_t len_value,
|
||||
BACNET_BIT_STRING *bit_string);
|
||||
|
||||
@@ -81,32 +81,6 @@ struct BACnet_Device_Address {
|
||||
};
|
||||
typedef struct BACnet_Device_Address BACNET_ADDRESS;
|
||||
|
||||
/* FIXME: move the string types into their own modules with unit testings */
|
||||
/* bit strings
|
||||
They could be as large as 256/8=32 octets */
|
||||
#define MAX_BITSTRING_BYTES 15
|
||||
typedef struct BACnet_Bit_String
|
||||
{
|
||||
uint8_t bits_used;
|
||||
uint8_t value[MAX_BITSTRING_BYTES];
|
||||
} BACNET_BIT_STRING;
|
||||
|
||||
/* FIXME: create some init/add/remove helper functions in a library */
|
||||
typedef struct BACnet_Character_String
|
||||
{
|
||||
size_t length;
|
||||
char value[MAX_APDU];
|
||||
} BACNET_CHARACTER_STRING;
|
||||
|
||||
/* FIXME: convert the bacdcode library to use this for APDU buffer
|
||||
to prevent buffer overflows */
|
||||
/* FIXME: create some init/add/remove helper functions in a library */
|
||||
typedef struct BACnet_Octet_String
|
||||
{
|
||||
size_t length;
|
||||
uint8_t value[MAX_APDU];
|
||||
} BACNET_OCTET_STRING;
|
||||
|
||||
/* date */
|
||||
typedef struct BACnet_Date
|
||||
{
|
||||
|
||||
@@ -34,10 +34,11 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "bacdef.h"
|
||||
#include "bacenum.h"
|
||||
#include "bacstr.h"
|
||||
//#include "bacdef.h"
|
||||
//#include "bacenum.h"
|
||||
#include "bits.h"
|
||||
#include "bigend.h"
|
||||
//#include "bigend.h"
|
||||
|
||||
void bitstring_init(BACNET_BIT_STRING *bit_string)
|
||||
{
|
||||
@@ -199,7 +200,6 @@ size_t characterstring_value(BACNET_CHARACTER_STRING *char_string, char *value)
|
||||
size_t characterstring_length(BACNET_CHARACTER_STRING *char_string)
|
||||
{
|
||||
size_t length = 0;
|
||||
size_t i; /* counter */
|
||||
|
||||
if (char_string)
|
||||
{
|
||||
@@ -219,7 +219,6 @@ void testBitString(Test * pTest)
|
||||
{
|
||||
uint8_t bit = 0;
|
||||
BACNET_BIT_STRING bit_string;
|
||||
BACNET_BIT_STRING decoded_bit_string;
|
||||
|
||||
bitstring_init(&bit_string);
|
||||
// verify initialization
|
||||
@@ -249,7 +248,6 @@ void testBitString(Test * pTest)
|
||||
void testCharacterString(Test * pTest)
|
||||
{
|
||||
BACNET_CHARACTER_STRING bacnet_string;
|
||||
BACNET_CHARACTER_STRING test_bacnet_string;
|
||||
char value[MAX_APDU] = "Joshua,Mary,Anna,Christopher";
|
||||
char test_value[MAX_APDU] = "Patricia";
|
||||
char test_append_value[MAX_APDU] = " and the Kids";
|
||||
|
||||
Reference in New Issue
Block a user