changed encode API to make it clearer.

This commit is contained in:
skarg
2004-11-19 15:38:57 +00:00
parent b26f693bea
commit 1e9ad1d231
2 changed files with 70 additions and 45 deletions
+57 -37
View File
@@ -200,6 +200,25 @@ bool decode_is_closing_tag(uint8_t * apdu)
return ((apdu[0] & 0x07) == 7); return ((apdu[0] & 0x07) == 7);
} }
int decode_tag_number(uint8_t * apdu,
uint8_t * tag_number)
{
int len = 1; // return value
// decode the tag number first
if (decode_is_extended_tag_number(&apdu[0])) {
// extended tag
if (tag_number)
*tag_number = apdu[1];
len++;
} else {
if (tag_number)
*tag_number = (apdu[0] >> 4);
}
return len;
}
// from clause 20.2.1.3.2 Constructed Data // from clause 20.2.1.3.2 Constructed Data
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
// from clause 20.2.1.3.2 Constructed Data // from clause 20.2.1.3.2 Constructed Data
@@ -219,16 +238,8 @@ int decode_tag_number_and_value(uint8_t * apdu,
} long_data = { { } long_data = { {
0}}; 0}};
// decode the tag number first len = decode_tag_number(&apdu[0],tag_number);
if (decode_is_extended_tag_number(&apdu[0])) { // decode the value
// extended tag
if (tag_number)
*tag_number = apdu[1];
len++;
} else {
if (tag_number)
*tag_number = (apdu[0] >> 4);
}
if (decode_is_extended_value(&apdu[0])) { if (decode_is_extended_value(&apdu[0])) {
// tagged as uint32_t // tagged as uint32_t
if (apdu[len] == 255) { if (apdu[len] == 255) {
@@ -282,6 +293,19 @@ int decode_tag_number_and_value(uint8_t * apdu,
return len; return len;
} }
// from clause 20.2.1.3.2 Constructed Data
// returns true if the tag is context specific and matches
bool decode_is_context_tag(uint8_t * apdu, uint8_t tag_number)
{
uint8_t my_tag_number = 0;
bool context_specific = false;
context_specific = decode_is_context_specific(apdu);
decode_tag_number(apdu,&my_tag_number);
return (context_specific && (my_tag_number == tag_number));
}
// from clause 20.2.6 Encoding of a Real Number Value // from clause 20.2.6 Encoding of a Real Number Value
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int decode_real(uint8_t * apdu, float *real_value) int decode_real(uint8_t * apdu, float *real_value)
@@ -337,7 +361,7 @@ int encode_bacnet_real(float value, uint8_t * apdu)
// from clause 20.2.6 Encoding of a Real Number Value // from clause 20.2.6 Encoding of a Real Number Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_real(uint8_t * apdu, float value) int encode_tagged_real(uint8_t * apdu, float value)
{ {
int len = 0; int len = 0;
@@ -419,7 +443,7 @@ int encode_context_object_id(uint8_t * apdu, int tag_number,
// from clause 20.2.14 Encoding of an Object Identifier Value // from clause 20.2.14 Encoding of an Object Identifier Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_object_id(uint8_t * apdu, int object_type, int instance) int encode_tagged_object_id(uint8_t * apdu, int object_type, int instance)
{ {
int len = 0; int len = 0;
@@ -464,7 +488,7 @@ int encode_bacnet_character_string(uint8_t * apdu, const char *char_string)
// from clause 20.2.9 Encoding of a Character String Value // from clause 20.2.9 Encoding of a Character String Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_character_string(uint8_t * apdu, const char *char_string) int encode_tagged_character_string(uint8_t * apdu, const char *char_string)
{ {
int len = 0; int len = 0;
int string_len = 0; int string_len = 0;
@@ -556,7 +580,7 @@ int encode_context_unsigned(uint8_t * apdu, int tag_number, int value)
// from clause 20.2.4 Encoding of an Unsigned Integer Value // from clause 20.2.4 Encoding of an Unsigned Integer Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_unsigned(uint8_t * apdu, unsigned int value) int encode_tagged_unsigned(uint8_t * apdu, unsigned int value)
{ {
int len = 0; int len = 0;
@@ -622,7 +646,7 @@ int encode_bacnet_enumerated(uint8_t * apdu, int value)
// from clause 20.2.11 Encoding of an Enumerated Value // from clause 20.2.11 Encoding of an Enumerated Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_enumerated(uint8_t * apdu, int value) int encode_tagged_enumerated(uint8_t * apdu, int value)
{ {
int len = 0; // return value int len = 0; // return value
@@ -667,7 +691,7 @@ int encode_bacnet_signed(uint8_t * apdu, int value)
// from clause 20.2.5 Encoding of a Signed Integer Value // from clause 20.2.5 Encoding of a Signed Integer Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_signed(uint8_t * apdu, int value) int encode_tagged_signed(uint8_t * apdu, int value)
{ {
int len = 0; // return value int len = 0; // return value
@@ -710,7 +734,7 @@ int encode_bacnet_time(uint8_t * apdu, int hour, int min, int sec,
// from clause 20.2.13 Encoding of a Time Value // from clause 20.2.13 Encoding of a Time Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_time(uint8_t * apdu, int hour, int min, int sec, int hundredths) int encode_tagged_time(uint8_t * apdu, int hour, int min, int sec, int hundredths)
{ {
int len = 0; int len = 0;
@@ -759,7 +783,7 @@ int encode_bacnet_date(uint8_t * apdu, int year, int month, int day,
// from clause 20.2.12 Encoding of a Date Value // from clause 20.2.12 Encoding of a Date Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_date(uint8_t * apdu, int year, int month, int day, int wday) int encode_tagged_date(uint8_t * apdu, int year, int month, int day, int wday)
{ {
int len = 0; int len = 0;
@@ -788,7 +812,6 @@ int decode_date(uint8_t * apdu, int *year, int *month, int *day, int *wday)
#ifdef TEST #ifdef TEST
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include "bacenum.h"
#include "ctest.h" #include "ctest.h"
static int get_apdu_len(bool extended_tag, uint32_t value) static int get_apdu_len(bool extended_tag, uint32_t value)
@@ -849,9 +872,6 @@ void testBACDCodeTags(Test * pTest)
test_len = test_len =
get_apdu_len(decode_is_extended_tag_number(&apdu[0]), get_apdu_len(decode_is_extended_tag_number(&apdu[0]),
value); value);
if (len != test_len)
printf("len=%d test_len=%d value=%lu tag#%d\n",
len, test_len, value, (int) tag_number);
ct_test(pTest, len == test_len); ct_test(pTest, len == test_len);
// stop at the the last value // stop at the the last value
if (value & BIT31) if (value & BIT31)
@@ -884,7 +904,7 @@ void testBACDCodeReal(Test * pTest)
sizeof(real_array)) == 0); sizeof(real_array)) == 0);
// a real will take up 4 octects plus a one octet tag // a real will take up 4 octects plus a one octet tag
apdu_len = encode_real(&apdu[0], value); apdu_len = encode_tagged_real(&apdu[0], value);
ct_test(pTest, apdu_len == 5); ct_test(pTest, apdu_len == 5);
// len tells us how many octets were used for encoding the value // len tells us how many octets were used for encoding the value
len = decode_tag_number_and_value(&apdu[0], &tag_number, &long_value); len = decode_tag_number_and_value(&apdu[0], &tag_number, &long_value);
@@ -910,15 +930,15 @@ void testBACDCodeEnumerated(Test * pTest)
uint8_t tag_number = 0; uint8_t tag_number = 0;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
encode_enumerated(&array[0], value); encode_tagged_enumerated(&array[0], value);
decode_enumerated(&array[0], &decoded_value); decode_enumerated(&array[0], &decoded_value);
ct_test(pTest, decoded_value == value); ct_test(pTest, decoded_value == value);
encode_enumerated(&encoded_array[0], decoded_value); encode_tagged_enumerated(&encoded_array[0], decoded_value);
ct_test(pTest, memcmp(&array[0], &encoded_array[0], ct_test(pTest, memcmp(&array[0], &encoded_array[0],
sizeof(array)) == 0); sizeof(array)) == 0);
// an enumerated will take up to 4 octects // an enumerated will take up to 4 octects
// plus a one octet for the tag // plus a one octet for the tag
apdu_len = encode_enumerated(&apdu[0], value); apdu_len = encode_tagged_enumerated(&apdu[0], value);
len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL); len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL);
ct_test(pTest, len == 1); ct_test(pTest, len == 1);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_ENUMERATED); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_ENUMERATED);
@@ -947,15 +967,15 @@ void testBACDCodeUnsigned(Test * pTest)
uint8_t tag_number = 0; uint8_t tag_number = 0;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
encode_unsigned(&array[0], value); encode_tagged_unsigned(&array[0], value);
decode_unsigned(&array[0], &decoded_value); decode_unsigned(&array[0], &decoded_value);
ct_test(pTest, decoded_value == value); ct_test(pTest, decoded_value == value);
encode_unsigned(&encoded_array[0], decoded_value); encode_tagged_unsigned(&encoded_array[0], decoded_value);
ct_test(pTest, memcmp(&array[0], &encoded_array[0], ct_test(pTest, memcmp(&array[0], &encoded_array[0],
sizeof(array)) == 0); sizeof(array)) == 0);
// an unsigned will take up to 4 octects // an unsigned will take up to 4 octects
// plus a one octet for the tag // plus a one octet for the tag
apdu_len = encode_unsigned(&apdu[0], value); apdu_len = encode_tagged_unsigned(&apdu[0], value);
// apdu_len varies... // apdu_len varies...
//ct_test(pTest, apdu_len == 5); //ct_test(pTest, apdu_len == 5);
len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL); len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL);
@@ -979,15 +999,15 @@ void testBACDCodeSigned(Test * pTest)
uint8_t tag_number = 0; uint8_t tag_number = 0;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
encode_signed(&array[0], value); encode_tagged_signed(&array[0], value);
decode_signed(&array[0], &decoded_value); decode_signed(&array[0], &decoded_value);
ct_test(pTest, decoded_value == value); ct_test(pTest, decoded_value == value);
encode_signed(&encoded_array[0], decoded_value); encode_tagged_signed(&encoded_array[0], decoded_value);
ct_test(pTest, memcmp(&array[0], &encoded_array[0], ct_test(pTest, memcmp(&array[0], &encoded_array[0],
sizeof(array)) == 0); sizeof(array)) == 0);
// a signed int will take up to 4 octects // a signed int will take up to 4 octects
// plus a one octet for the tag // plus a one octet for the tag
apdu_len = encode_signed(&apdu[0], value); apdu_len = encode_tagged_signed(&apdu[0], value);
len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL); len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_SIGNED_INT); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_SIGNED_INT);
ct_test(pTest, decode_is_context_specific(&apdu[0]) == false); ct_test(pTest, decode_is_context_specific(&apdu[0]) == false);
@@ -995,15 +1015,15 @@ void testBACDCodeSigned(Test * pTest)
} }
value = -1; value = -1;
encode_signed(&array[0], value); encode_tagged_signed(&array[0], value);
decode_signed(&array[0], &decoded_value); decode_signed(&array[0], &decoded_value);
ct_test(pTest, decoded_value == value); ct_test(pTest, decoded_value == value);
encode_signed(&encoded_array[0], decoded_value); encode_tagged_signed(&encoded_array[0], decoded_value);
ct_test(pTest, memcmp(&array[0], &encoded_array[0], ct_test(pTest, memcmp(&array[0], &encoded_array[0],
sizeof(array)) == 0); sizeof(array)) == 0);
// a signed int will take up to 4 octects // a signed int will take up to 4 octects
// plus a one octet for the tag // plus a one octet for the tag
apdu_len = encode_signed(&apdu[0], value); apdu_len = encode_tagged_signed(&apdu[0], value);
len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL); len = decode_tag_number_and_value(&apdu[0], &tag_number, NULL);
ct_test(pTest, len == 1); ct_test(pTest, len == 1);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_SIGNED_INT); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_SIGNED_INT);
@@ -1024,7 +1044,7 @@ void testBACDCodeString(Test * pTest)
size_t len; size_t len;
char *test_string0 = ""; char *test_string0 = "";
apdu_len = encode_character_string(&array[0], &test_string0[0]); apdu_len = encode_tagged_character_string(&array[0], &test_string0[0]);
decode_character_string(&array[0], &decoded_string[0]); decode_character_string(&array[0], &decoded_string[0]);
ct_test(pTest, apdu_len == 2); ct_test(pTest, apdu_len == 2);
ct_test(pTest, strcmp(&test_string0[0], &decoded_string[0]) == 0); ct_test(pTest, strcmp(&test_string0[0], &decoded_string[0]) == 0);
@@ -1032,7 +1052,7 @@ void testBACDCodeString(Test * pTest)
test_string[i] = 'S'; test_string[i] = 'S';
test_string[i + 1] = '\0'; test_string[i + 1] = '\0';
apdu_len = apdu_len =
encode_character_string(&encoded_array[0], &test_string[0]); encode_tagged_character_string(&encoded_array[0], &test_string[0]);
test_apdu_len = test_apdu_len =
decode_character_string(&encoded_array[0], &decoded_string[0]); decode_character_string(&encoded_array[0], &decoded_string[0]);
len = strlen(test_string); len = strlen(test_string);
+13 -8
View File
@@ -48,16 +48,21 @@ int encode_opening_tag(uint8_t * apdu, uint8_t tag_number);
int encode_closing_tag(uint8_t * apdu, uint8_t tag_number); int encode_closing_tag(uint8_t * apdu, uint8_t tag_number);
int decode_tag_number_and_value(uint8_t * apdu, uint8_t * tag_number, int decode_tag_number_and_value(uint8_t * apdu, uint8_t * tag_number,
uint32_t * value); uint32_t * value);
// returns true if the tag is context specific
bool decode_is_context_specific(uint8_t * apdu); bool decode_is_context_specific(uint8_t * apdu);
// returns true if the tag is an opening tag
bool decode_is_opening_tag(uint8_t * apdu); bool decode_is_opening_tag(uint8_t * apdu);
// returns true if the tag is an closing tag
bool decode_is_closing_tag(uint8_t * apdu); bool decode_is_closing_tag(uint8_t * apdu);
// returns true if the tag is context specific and matches
bool decode_is_context_tag(uint8_t * apdu, uint8_t tag_number);
// from clause 20.2.6 Encoding of a Real Number Value // from clause 20.2.6 Encoding of a Real Number Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int decode_real(uint8_t * apdu, float *real_value); int decode_real(uint8_t * apdu, float *real_value);
int encode_bacnet_real(float value, uint8_t * apdu); int encode_bacnet_real(float value, uint8_t * apdu);
int encode_real(uint8_t * apdu, float value); int encode_tagged_real(uint8_t * apdu, float value);
// from clause 20.2.14 Encoding of an Object Identifier Value // from clause 20.2.14 Encoding of an Object Identifier Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
@@ -66,7 +71,7 @@ int decode_object_id(uint8_t * apdu, int *object_type, int *instance);
int encode_bacnet_object_id(uint8_t * apdu, int object_type, int instance); int encode_bacnet_object_id(uint8_t * apdu, int object_type, int instance);
int encode_context_object_id(uint8_t * apdu, int tag_number, int encode_context_object_id(uint8_t * apdu, int tag_number,
int object_type, int instance); int object_type, int instance);
int encode_object_id(uint8_t * apdu, int object_type, int instance); int encode_tagged_object_id(uint8_t * apdu, int object_type, int instance);
// from clause 20.2.9 Encoding of a Character String Value // from clause 20.2.9 Encoding of a Character String Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
@@ -75,7 +80,7 @@ int encode_bacnet_string(uint8_t * apdu,
const char *char_string, int string_len); const char *char_string, int string_len);
int encode_bacnet_character_string(uint8_t * apdu, int encode_bacnet_character_string(uint8_t * apdu,
const char *char_string); const char *char_string);
int encode_character_string(uint8_t * apdu, const char *char_string); int encode_tagged_character_string(uint8_t * apdu, const char *char_string);
int decode_character_string(uint8_t * apdu, char *char_string); int decode_character_string(uint8_t * apdu, char *char_string);
// from clause 20.2.4 Encoding of an Unsigned Integer Value // from clause 20.2.4 Encoding of an Unsigned Integer Value
@@ -83,14 +88,14 @@ int decode_character_string(uint8_t * apdu, char *char_string);
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_bacnet_unsigned(uint8_t * apdu, unsigned int value); int encode_bacnet_unsigned(uint8_t * apdu, unsigned int value);
int encode_context_unsigned(uint8_t * apdu, int tag_number, int value); int encode_context_unsigned(uint8_t * apdu, int tag_number, int value);
int encode_unsigned(uint8_t * apdu, unsigned int value); int encode_tagged_unsigned(uint8_t * apdu, unsigned int value);
int decode_unsigned(uint8_t * apdu, int *value); int decode_unsigned(uint8_t * apdu, int *value);
// from clause 20.2.5 Encoding of a Signed Integer Value // from clause 20.2.5 Encoding of a Signed Integer Value
// and 20.2.1 General Rules for Encoding BACnet Tags // and 20.2.1 General Rules for Encoding BACnet Tags
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_bacnet_signed(uint8_t * apdu, int value); int encode_bacnet_signed(uint8_t * apdu, int value);
int encode_signed(uint8_t * apdu, int value); int encode_tagged_signed(uint8_t * apdu, int value);
int encode_context_signed(uint8_t * apdu, int tag_number, int value); int encode_context_signed(uint8_t * apdu, int tag_number, int value);
int decode_signed(uint8_t * apdu, int *value); int decode_signed(uint8_t * apdu, int *value);
@@ -99,7 +104,7 @@ int decode_signed(uint8_t * apdu, int *value);
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int decode_enumerated(uint8_t * apdu, int *value); int decode_enumerated(uint8_t * apdu, int *value);
int encode_bacnet_enumerated(uint8_t * apdu, int value); int encode_bacnet_enumerated(uint8_t * apdu, int value);
int encode_enumerated(uint8_t * apdu, int value); int encode_tagged_enumerated(uint8_t * apdu, int value);
int encode_context_enumerated(uint8_t * apdu, int tag_number, int value); int encode_context_enumerated(uint8_t * apdu, int tag_number, int value);
// from clause 20.2.13 Encoding of a Time Value // from clause 20.2.13 Encoding of a Time Value
@@ -107,7 +112,7 @@ int encode_context_enumerated(uint8_t * apdu, int tag_number, int value);
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_bacnet_time(uint8_t * apdu, int hour, int min, int sec, int encode_bacnet_time(uint8_t * apdu, int hour, int min, int sec,
int hundredths); int hundredths);
int encode_time(uint8_t * apdu, int hour, int min, int sec, int encode_tagged_time(uint8_t * apdu, int hour, int min, int sec,
int hundredths); int hundredths);
int decode_bacnet_time(uint8_t * apdu, int *hour, int *min, int *sec, int decode_bacnet_time(uint8_t * apdu, int *hour, int *min, int *sec,
int *hundredths); int *hundredths);
@@ -123,7 +128,7 @@ int decode_bacnet_time(uint8_t * apdu, int *hour, int *min, int *sec,
// returns the number of apdu bytes consumed // returns the number of apdu bytes consumed
int encode_bacnet_date(uint8_t * apdu, int year, int month, int day, int encode_bacnet_date(uint8_t * apdu, int year, int month, int day,
int wday); int wday);
int encode_date(uint8_t * apdu, int year, int month, int day, int wday); int encode_tagged_date(uint8_t * apdu, int year, int month, int day, int wday);
int decode_date(uint8_t * apdu, int *year, int *month, int *day, int decode_date(uint8_t * apdu, int *year, int *month, int *day,
int *wday); int *wday);