Corrected for ENDIANNESS.

This commit is contained in:
bacpack
2010-10-27 19:15:10 +00:00
parent 76260a994d
commit 79b86d3a8c
+87 -63
View File
@@ -41,12 +41,23 @@
/** @file bacint.c Encode/Decode Integer Types */
#ifndef BIG_ENDIAN
#error Define BIG_ENDIAN=0 or BIG_ENDIAN=1 for BACnet Stack in compiler settings
#endif
int encode_unsigned16(
uint8_t * apdu,
uint16_t value)
{
apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
apdu[1] = (uint8_t) (value & 0x00ff);
uint8_t *in = (uint8_t *)&value;
#if BIG_ENDIAN
apdu[0] = in[0];
apdu[1] = in[1];
#else
apdu[0] = in[1];
apdu[1] = in[0];
#endif
return 2;
}
@@ -55,11 +66,17 @@ int decode_unsigned16(
uint8_t * apdu,
uint16_t * value)
{
if (value) {
*value = (uint16_t) ((((uint16_t) apdu[0]) << 8) & 0xff00);
*value |= ((uint16_t) (((uint16_t) apdu[1]) & 0x00ff));
}
uint8_t *out = (uint8_t *)value;
if (out) {
#if BIG_ENDIAN
out[0] = apdu[0];
out[1] = apdu[1];
#else
out[0] = apdu[1];
out[1] = apdu[0];
#endif
}
return 2;
}
@@ -67,9 +84,17 @@ int encode_unsigned24(
uint8_t * apdu,
uint32_t value)
{
apdu[0] = (uint8_t) ((value & 0xff0000) >> 16);
apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8);
apdu[2] = (uint8_t) (value & 0x0000ff);
uint8_t *in = (uint8_t *)&value;
#if BIG_ENDIAN
apdu[0] = in[1];
apdu[1] = in[2];
apdu[2] = in[3];
#else
apdu[0] = in[2];
apdu[1] = in[1];
apdu[2] = in[0];
#endif
return 3;
}
@@ -78,10 +103,20 @@ int decode_unsigned24(
uint8_t * apdu,
uint32_t * value)
{
if (value) {
*value = ((uint32_t) ((((uint32_t) apdu[0]) << 16) & 0x00ff0000));
*value |= (uint32_t) ((((uint32_t) apdu[1]) << 8) & 0x0000ff00);
*value |= ((uint32_t) (((uint32_t) apdu[2]) & 0x000000ff));
uint8_t *out = (uint8_t *)value;
if (out) {
#if BIG_ENDIAN
out[0] = 0;
out[1] = apdu[0];
out[2] = apdu[1];
out[3] = apdu[2];
#else
out[0] = apdu[2];
out[1] = apdu[1];
out[2] = apdu[0];
out[3] = 0;
#endif
}
return 3;
@@ -91,10 +126,19 @@ int encode_unsigned32(
uint8_t * apdu,
uint32_t value)
{
apdu[0] = (uint8_t) ((value & 0xff000000) >> 24);
apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16);
apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8);
apdu[3] = (uint8_t) (value & 0x000000ff);
uint8_t *in = (uint8_t *)&value;
#if BIG_ENDIAN
apdu[0] = in[0];
apdu[1] = in[1];
apdu[2] = in[2];
apdu[3] = in[3];
#else
apdu[0] = in[3];
apdu[1] = in[2];
apdu[2] = in[1];
apdu[3] = in[0];
#endif
return 4;
}
@@ -103,13 +147,21 @@ int decode_unsigned32(
uint8_t * apdu,
uint32_t * value)
{
if (value) {
*value = ((uint32_t) ((((uint32_t) apdu[0]) << 24) & 0xff000000));
*value |= ((uint32_t) ((((uint32_t) apdu[1]) << 16) & 0x00ff0000));
*value |= ((uint32_t) ((((uint32_t) apdu[2]) << 8) & 0x0000ff00));
*value |= ((uint32_t) (((uint32_t) apdu[3]) & 0x000000ff));
}
uint8_t *out = (uint8_t *)value;
if (out) {
#if BIG_ENDIAN
out[0] = apdu[0];
out[1] = apdu[1];
out[2] = apdu[2];
out[3] = apdu[3];
#else
out[0] = apdu[3];
out[1] = apdu[2];
out[2] = apdu[1];
out[3] = apdu[0];
#endif
}
return 4;
}
@@ -142,25 +194,17 @@ int encode_signed16(
uint8_t * apdu,
int16_t value)
{
apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
apdu[1] = (uint8_t) (value & 0x00ff);
return 2;
return encode_unsigned16 (apdu, value);
}
int decode_signed16(
uint8_t * apdu,
int32_t * value)
{
if (value) {
/* negative - bit 7 is set */
if (apdu[0] & 0x80)
*value = 0xFFFF0000;
else
*value = 0;
*value |= ((int32_t) ((((int32_t) apdu[0]) << 8) & 0x0000ff00));
*value |= ((int32_t) (((int32_t) apdu[1]) & 0x000000ff));
}
int16_t val;
decode_unsigned16 (apdu, (uint16_t *)&val);
*value = (int32_t)val;
return 2;
}
@@ -169,11 +213,7 @@ int encode_signed24(
uint8_t * apdu,
int32_t value)
{
apdu[0] = (uint8_t) ((value & 0xff0000) >> 16);
apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8);
apdu[2] = (uint8_t) (value & 0x0000ff);
return 3;
return encode_unsigned24 (apdu, value);
}
int decode_signed24(
@@ -181,14 +221,10 @@ int decode_signed24(
int32_t * value)
{
if (value) {
/* negative - bit 7 is set */
if (apdu[0] & 0x80)
*value = 0xFF000000;
else
*value = 0;
*value |= ((int32_t) ((((int32_t) apdu[0]) << 16) & 0x00ff0000));
*value |= ((int32_t) ((((int32_t) apdu[1]) << 8) & 0x0000ff00));
*value |= ((int32_t) (((int32_t) apdu[2]) & 0x000000ff));
decode_unsigned24 (apdu, (uint32_t *)value);
if (*value & 0x00800000) {
*value |= 0xFF000000;
}
}
return 3;
@@ -198,26 +234,14 @@ int encode_signed32(
uint8_t * apdu,
int32_t value)
{
apdu[0] = (uint8_t) ((value & 0xff000000) >> 24);
apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16);
apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8);
apdu[3] = (uint8_t) (value & 0x000000ff);
return 4;
return encode_unsigned32 (apdu, value);
}
int decode_signed32(
uint8_t * apdu,
int32_t * value)
{
if (value) {
*value = ((int32_t) ((((int32_t) apdu[0]) << 24) & 0xff000000));
*value |= ((int32_t) ((((int32_t) apdu[1]) << 16) & 0x00ff0000));
*value |= ((int32_t) ((((int32_t) apdu[2]) << 8) & 0x0000ff00));
*value |= ((int32_t) (((int32_t) apdu[3]) & 0x000000ff));
}
return 4;
return decode_unsigned32 (apdu, (uint32_t *)value);
}
/* end of decoding_encoding.c */