Sebastián Pastor / EtheriosCloudConnector
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bele.h Source File

bele.h

00001 /*
00002  * Copyright (c) 2013 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #ifndef BELE_H_
00014 #define BELE_H_
00015 
00016 #include "connector_types.h"
00017 
00018 /*
00019  *  Endian-independent byte-extraction macros
00020  */
00021 #define LOW8(x16)      ((uint8_t) (x16))
00022 #define HIGH8(x16)      ((uint8_t) (((uint16_t)(x16)) >> 8))
00023 
00024 #define LOW16(x32)      ((uint16_t) (x32))
00025 #define HIGH16(x32)     ((uint16_t) (((uint32_t)(x32)) >> 16))
00026 
00027 #define BYTE32_3(x32)   ((uint8_t) (((uint32_t)(x32)) >> 24))
00028 #define BYTE32_2(x32)   ((uint8_t) (((uint32_t)(x32)) >> 16))
00029 #define BYTE32_1(x32)   ((uint8_t) (((uint32_t)(x32)) >>  8))
00030 #define BYTE32_0(x32)   ((uint8_t)  ((uint32_t)(x32)))
00031 
00032 #if (defined CONNECTOR_HAS_64_BIT_INTEGERS)
00033 #define LOW32(x64)      ((uint32_t) (x64))
00034 #define HIGH32(x64)     ((uint32_t) (((uint64_t)(x64)) >> 32))
00035 
00036 #define BYTE64_7(x64)   ((uint8_t) (((uint64_t)(x64)) >> 56))
00037 #define BYTE64_6(x64)   ((uint8_t) (((uint64_t)(x64)) >> 48))
00038 #define BYTE64_5(x64)   ((uint8_t) (((uint64_t)(x64)) >> 40))
00039 #define BYTE64_4(x64)   ((uint8_t) (((uint64_t)(x64)) >> 32))
00040 #define BYTE64_3(x64)   ((uint8_t) (((uint64_t)(x64)) >> 24))
00041 #define BYTE64_2(x64)   ((uint8_t) (((uint64_t)(x64)) >> 16))
00042 #define BYTE64_1(x64)   ((uint8_t) (((uint64_t)(x64)) >>  8))
00043 #define BYTE64_0(x64)   ((uint8_t)  ((uint64_t)(x64)))
00044 
00045 static  void StoreBE64(void * const array, uint64_t const val)
00046 {
00047     ((uint8_t *)(array))[0] = BYTE64_7(val);
00048     ((uint8_t *)(array))[1] = BYTE64_6(val);
00049     ((uint8_t *)(array))[2] = BYTE64_5(val);
00050     ((uint8_t *)(array))[3] = BYTE64_4(val);
00051     ((uint8_t *)(array))[4] = BYTE64_3(val);
00052     ((uint8_t *)(array))[5] = BYTE64_2(val);
00053     ((uint8_t *)(array))[6] = BYTE64_1(val);
00054     ((uint8_t *)(array))[7] = BYTE64_0(val);
00055 }
00056 #endif
00057 
00058 /*
00059  *  Endian-independent multi-byte-creation macros
00060  */
00061 #define MAKE16(hi8,lo8)     ((uint16_t) (((uint16_t)(((uint16_t) (hi8) ) << 8 )) | ((uint16_t) (lo8))))
00062 #define MAKE32(hi16,lo16)   ((uint32_t)  (((uint32_t) (((uint32_t)(hi16)) << 16)) | ((uint32_t)(lo16))))
00063 #define MAKE64(hi32,lo32)   ((uint64_t)  (((uint64_t) (((uint32_t)(hi32)) << 32)) | ((uint32_t)(lo32))))
00064 #define MAKE32_4(b3, b2, b1, b0)    MAKE32( MAKE16( b3, b2 ), MAKE16( b1, b0 ))
00065 
00066 static  uint16_t LoadBE16(void const * const array)
00067 {
00068     return MAKE16(((uint8_t *)(array))[0], ((uint8_t *)(array))[1]);
00069 }
00070 
00071 static  uint32_t LoadBE32(void const * const array)
00072 {
00073     return MAKE32_4(((uint8_t *)(array))[0], ((uint8_t *)(array))[1], ((uint8_t *)(array))[2], ((uint8_t *)(array))[3]);
00074 }
00075 
00076 static  void StoreBE16(void * const array, uint16_t const val)
00077 {
00078     ((uint8_t *)(array))[0] = HIGH8(val);
00079     ((uint8_t *)(array))[1] = LOW8(val);
00080 }
00081 
00082 static  void StoreBE32(void * const array, uint32_t const val)
00083 {
00084     ((uint8_t *)(array))[0] = BYTE32_3(val);
00085     ((uint8_t *)(array))[1] = BYTE32_2(val);
00086     ((uint8_t *)(array))[2] = BYTE32_1(val);
00087     ((uint8_t *)(array))[3] = BYTE32_0(val);
00088 }
00089 
00090 /*
00091  * static  uint32_t bele_SWAP32(uint32_t const val)
00092  * {
00093  *     return MAKE32(bele_SWAP16(LOW16(val)), bele_SWAP16(HIGH16(val)));
00094  * }
00095  */
00096 
00097 #if (defined CONNECTOR_LITTLE_ENDIAN)
00098 
00099 static  uint16_t bele_SWAP16(uint16_t const val)
00100 {
00101     return MAKE16(LOW8(val), HIGH8(val));
00102 }
00103 
00104 #define FROM_LE32(x)    (x)
00105 #define FROM_LE16(x)    (x)
00106 
00107 #define TO_BE32(x)      (bele_SWAP32(x))
00108 #define TO_BE16(x)      (bele_SWAP16(x))
00109 #define FROM_BE32(x)    (bele_SWAP32(x))
00110 #define FROM_BE16(x)    (bele_SWAP16(x))
00111 
00112 #else
00113 
00114 #define FROM_LE32(x)    (bele_SWAP32(x))
00115 #define FROM_LE16(x)    (bele_SWAP16(x))
00116 
00117 #define TO_BE32(x)      (x)
00118 #define TO_BE16(x)      (x)
00119 #define FROM_BE32(x)    (x)
00120 #define FROM_BE16(x)    (x)
00121 
00122 #endif
00123 
00124 #endif
00125