Digi XBee(R) ANSI C Host Library
byteorder.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012 Digi International Inc.,
3  * All rights not expressly granted are reserved.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
7  * You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
10  * =======================================================================
11  */
12 
73 #ifndef __XBEE_ENDIAN_H
74 #define __XBEE_ENDIAN_H
75  #include <string.h> // for memcpy
76 
77  // xbee/platform will load the platform's endian.h or at least define
78  // the macros LITTLE_ENDIAN, BIG_ENDIAN and BYTE_ORDER.
79  #include "xbee/platform.h"
80 
82 
83  // On DOS, swap16() and swap32() are already defined as macros, so don't
84  // define them here.
85  #ifndef swap16
86  uint16_t swap16( uint16_t value);
87  #endif
88  #ifndef swap32
89  uint32_t swap32( uint32_t value);
90  #endif
91 
103  void _swapcpy( void FAR *dst, const void FAR *src, uint_fast8_t count);
104 
105  #if BYTE_ORDER == LITTLE_ENDIAN
106  #define memcpy_letoh( dst, src_le, count) _f_memcpy( dst, src_le, count)
107  #define memcpy_htole( dst_le, src, count) _f_memcpy( dst_le, src, count)
108 
109  #define memcpy_betoh( dst, src_be, count) _swapcpy( dst, src_be, count)
110  #define memcpy_htobe( dst_be, src, count) _swapcpy( dst_be, src, count)
111  #else
112  #define memcpy_letoh( dst, src_le, count) _swapcpy( dst, src_le, count)
113  #define memcpy_htole( dst_le, src, count) _swapcpy( dst_le, src, count)
114 
115  #define memcpy_betoh( dst, src_be, count) _f_memcpy( dst, src_be, count)
116  #define memcpy_htobe( dst_be, src, count) _f_memcpy( dst_be, src, count)
117  #endif
118 
119  #define memcpy_betole( dst_le, src_be, count) \
120  _swapcpy( dst_le, src_be, count)
121  #define memcpy_letobe( dst_be, src_le, count) \
122  _swapcpy( dst_be, src_le, count)
123 
124  // define byte-swapping macros if the platform hasn't already done so
125  #ifndef htobe16
126  #if BYTE_ORDER == LITTLE_ENDIAN
127  // host to big-endian
128  #define htobe16(x) swap16(x)
129  #define htobe32(x) swap32(x)
130 
131  // big-endian to host
132  #define be16toh(x) swap16(x)
133  #define be32toh(x) swap32(x)
134 
135  // host to little-endian
136  #define htole16(x) (x)
137  #define htole32(x) (x)
138 
139  // little-endian to host
140  #define le16toh(x) (x)
141  #define le32toh(x) (x)
142  #else
143  // host to little-endian
144  #define htole16(x) swap16(x)
145  #define htole32(x) swap32(x)
146 
147  // little-endian to host
148  #define le16toh(x) swap16(x)
149  #define le32toh(x) swap32(x)
150 
151  // host to big-endian
152  #define htobe16(x) (x)
153  #define htobe32(x) (x)
154 
155  // big-endian to host
156  #define be16toh(x) (x)
157  #define be32toh(x) (x)
158  #endif
159  #endif
160 
162 
163 #endif
164 
unsigned long uint32_t
32-bit unsigned integer
Definition: platform_config.h:43
#define FAR
On platforms with "far" pointers, define to the proper keyword; empty definition if not required...
Definition: platform.h:396
#define XBEE_BEGIN_DECLS
Macro defined to.
Definition: platform.h:41
Common header for Hardware Abstraction Layer.
uint16_t swap16(uint16_t value)
Swap the byte order of a 16-bit value.
Definition: swapbytes.c:47
unsigned short uint16_t
16-bit unsigned integer
Definition: platform_config.h:41
void _swapcpy(void FAR *dst, const void FAR *src, uint_fast8_t count)
Function similar to memcpy() but reverses byte order during copy. Copy count from src to dst while re...
Definition: swapcpy.c:24
#define XBEE_END_DECLS
Macro defined to.
Definition: platform.h:42
uint32_t swap32(uint32_t value)
Swap the byte order of a 32-bit value.
Definition: swapbytes.c:33