From 437112cc52c12a99b671b9f9c16153d0fd14ce43 Mon Sep 17 00:00:00 2001 From: skarg Date: Sun, 31 Oct 2010 19:13:10 +0000 Subject: [PATCH] Reverted the BIG_ENDIAN additions as they are not needed since bacint is converting integers in an endian neutral manner. --- bacnet-stack/src/bacint.c | 150 ++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 87 deletions(-) diff --git a/bacnet-stack/src/bacint.c b/bacnet-stack/src/bacint.c index b242dfbd..bfb4ec5a 100644 --- a/bacnet-stack/src/bacint.c +++ b/bacnet-stack/src/bacint.c @@ -41,23 +41,12 @@ /** @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) { - 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 + apdu[0] = (uint8_t) ((value & 0xff00) >> 8); + apdu[1] = (uint8_t) (value & 0x00ff); return 2; } @@ -66,17 +55,11 @@ int decode_unsigned16( uint8_t * apdu, uint16_t * value) { - 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 + if (value) { + *value = (uint16_t) ((((uint16_t) apdu[0]) << 8) & 0xff00); + *value |= ((uint16_t) (((uint16_t) apdu[1]) & 0x00ff)); } + return 2; } @@ -84,17 +67,9 @@ int encode_unsigned24( uint8_t * apdu, uint32_t value) { - 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 + apdu[0] = (uint8_t) ((value & 0xff0000) >> 16); + apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8); + apdu[2] = (uint8_t) (value & 0x0000ff); return 3; } @@ -103,20 +78,10 @@ int decode_unsigned24( uint8_t * apdu, uint32_t * value) { - 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 + 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)); } return 3; @@ -126,19 +91,10 @@ int encode_unsigned32( uint8_t * apdu, uint32_t value) { - 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 + 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; } @@ -147,21 +103,13 @@ int decode_unsigned32( uint8_t * apdu, uint32_t * value) { - 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 + 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)); } + return 4; } @@ -194,17 +142,25 @@ int encode_signed16( uint8_t * apdu, int16_t value) { - return encode_unsigned16 (apdu, value); + apdu[0] = (uint8_t) ((value & 0xff00) >> 8); + apdu[1] = (uint8_t) (value & 0x00ff); + + return 2; } int decode_signed16( uint8_t * apdu, int32_t * value) { - int16_t val; - - decode_unsigned16 (apdu, (uint16_t *)&val); - *value = (int32_t)val; + 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)); + } return 2; } @@ -213,7 +169,11 @@ int encode_signed24( uint8_t * apdu, int32_t value) { - return encode_unsigned24 (apdu, value); + apdu[0] = (uint8_t) ((value & 0xff0000) >> 16); + apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8); + apdu[2] = (uint8_t) (value & 0x0000ff); + + return 3; } int decode_signed24( @@ -221,10 +181,14 @@ int decode_signed24( int32_t * value) { if (value) { - decode_unsigned24 (apdu, (uint32_t *)value); - if (*value & 0x00800000) { - *value |= 0xFF000000; - } + /* 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)); } return 3; @@ -234,14 +198,26 @@ int encode_signed32( uint8_t * apdu, int32_t value) { - return encode_unsigned32 (apdu, 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; } int decode_signed32( uint8_t * apdu, int32_t * value) { - return decode_unsigned32 (apdu, (uint32_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; } /* end of decoding_encoding.c */