messagepack implementation for embedded systems (mbed / arduino)

Dependents:   hello_message_pack

msgpack-c & msgpack-c++ https://github.com/msgpack/msgpack-c implementation for embedded systems (mbed / Arduino)

Committer:
hideakitai
Date:
Mon Feb 22 01:43:48 2016 +0000
Revision:
4:bd0c06dd6e92
Parent:
0:3f9dbf1e2cb0
fix throw error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hideakitai 0:3f9dbf1e2cb0 1 /*
hideakitai 0:3f9dbf1e2cb0 2 * MessagePack system dependencies
hideakitai 0:3f9dbf1e2cb0 3 *
hideakitai 0:3f9dbf1e2cb0 4 * Copyright (C) 2008-2010 FURUHASHI Sadayuki
hideakitai 0:3f9dbf1e2cb0 5 *
hideakitai 0:3f9dbf1e2cb0 6 * Distributed under the Boost Software License, Version 1.0.
hideakitai 0:3f9dbf1e2cb0 7 * (See accompanying file LICENSE_1_0.txt or copy at
hideakitai 0:3f9dbf1e2cb0 8 * http://www.boost.org/LICENSE_1_0.txt)
hideakitai 0:3f9dbf1e2cb0 9 */
hideakitai 0:3f9dbf1e2cb0 10 #ifndef MSGPACK_SYSDEP_H
hideakitai 0:3f9dbf1e2cb0 11 #define MSGPACK_SYSDEP_H
hideakitai 0:3f9dbf1e2cb0 12
hideakitai 0:3f9dbf1e2cb0 13 //#include <msgpack/predef.h>
hideakitai 0:3f9dbf1e2cb0 14 #define MSGPACK_ENDIAN_LITTLE_BYTE 1
hideakitai 0:3f9dbf1e2cb0 15
hideakitai 0:3f9dbf1e2cb0 16 #include <stdlib.h>
hideakitai 0:3f9dbf1e2cb0 17 #include <stddef.h>
hideakitai 0:3f9dbf1e2cb0 18 #if defined(_MSC_VER) && _MSC_VER < 1600
hideakitai 0:3f9dbf1e2cb0 19 typedef __int8 int8_t;
hideakitai 0:3f9dbf1e2cb0 20 typedef unsigned __int8 uint8_t;
hideakitai 0:3f9dbf1e2cb0 21 typedef __int16 int16_t;
hideakitai 0:3f9dbf1e2cb0 22 typedef unsigned __int16 uint16_t;
hideakitai 0:3f9dbf1e2cb0 23 typedef __int32 int32_t;
hideakitai 0:3f9dbf1e2cb0 24 typedef unsigned __int32 uint32_t;
hideakitai 0:3f9dbf1e2cb0 25 typedef __int64 int64_t;
hideakitai 0:3f9dbf1e2cb0 26 typedef unsigned __int64 uint64_t;
hideakitai 0:3f9dbf1e2cb0 27 #elif defined(_MSC_VER) // && _MSC_VER >= 1600
hideakitai 0:3f9dbf1e2cb0 28 # include <stdint.h>
hideakitai 0:3f9dbf1e2cb0 29 #else
hideakitai 0:3f9dbf1e2cb0 30 # include <stdint.h>
hideakitai 0:3f9dbf1e2cb0 31 # include <stdbool.h>
hideakitai 0:3f9dbf1e2cb0 32 #endif
hideakitai 0:3f9dbf1e2cb0 33
hideakitai 0:3f9dbf1e2cb0 34 #if !defined(MSGPACK_DLLEXPORT)
hideakitai 0:3f9dbf1e2cb0 35 #if defined(_MSC_VER)
hideakitai 0:3f9dbf1e2cb0 36 # define MSGPACK_DLLEXPORT __declspec(dllexport)
hideakitai 0:3f9dbf1e2cb0 37 #else /* _MSC_VER */
hideakitai 0:3f9dbf1e2cb0 38 # define MSGPACK_DLLEXPORT
hideakitai 0:3f9dbf1e2cb0 39 #endif /* _MSC_VER */
hideakitai 0:3f9dbf1e2cb0 40 #endif
hideakitai 0:3f9dbf1e2cb0 41
hideakitai 0:3f9dbf1e2cb0 42 #if defined(_WIN32)
hideakitai 0:3f9dbf1e2cb0 43 # define _msgpack_atomic_counter_header <windows.h>
hideakitai 0:3f9dbf1e2cb0 44 typedef long _msgpack_atomic_counter_t;
hideakitai 0:3f9dbf1e2cb0 45 # define _msgpack_sync_decr_and_fetch(ptr) InterlockedDecrement(ptr)
hideakitai 0:3f9dbf1e2cb0 46 # define _msgpack_sync_incr_and_fetch(ptr) InterlockedIncrement(ptr)
hideakitai 0:3f9dbf1e2cb0 47 #elif defined(__GNUC__) && ((__GNUC__*10 + __GNUC_MINOR__) < 41)
hideakitai 0:3f9dbf1e2cb0 48
hideakitai 0:3f9dbf1e2cb0 49 # if defined(__cplusplus)
hideakitai 0:3f9dbf1e2cb0 50 # define _msgpack_atomic_counter_header "gcc_atomic.hpp"
hideakitai 0:3f9dbf1e2cb0 51 # else
hideakitai 0:3f9dbf1e2cb0 52 # define _msgpack_atomic_counter_header "gcc_atomic.h"
hideakitai 0:3f9dbf1e2cb0 53 # endif
hideakitai 0:3f9dbf1e2cb0 54
hideakitai 0:3f9dbf1e2cb0 55 #elif defined(__MBED__)
hideakitai 0:3f9dbf1e2cb0 56
hideakitai 0:3f9dbf1e2cb0 57 # define _msgpack_atomic_counter_header "cmsis.h"
hideakitai 0:3f9dbf1e2cb0 58 typedef unsigned int _msgpack_atomic_counter_t;
hideakitai 0:3f9dbf1e2cb0 59 # define _msgpack_sync_decr_and_fetch(ptr) __sync_sub_and_fetch(ptr, 1)
hideakitai 0:3f9dbf1e2cb0 60 # define _msgpack_sync_incr_and_fetch(ptr) __sync_add_and_fetch(ptr, 1)
hideakitai 0:3f9dbf1e2cb0 61
hideakitai 0:3f9dbf1e2cb0 62 #elif defined(__AVR__)
hideakitai 0:3f9dbf1e2cb0 63
hideakitai 0:3f9dbf1e2cb0 64 # define _msgpack_atomic_counter_header <util/atomic.h>
hideakitai 0:3f9dbf1e2cb0 65 typedef unsigned int _msgpack_atomic_counter_t;
hideakitai 0:3f9dbf1e2cb0 66 # define _msgpack_sync_decr_and_fetch(ptr) __sync_sub_and_fetch(ptr, 1)
hideakitai 0:3f9dbf1e2cb0 67 # define _msgpack_sync_incr_and_fetch(ptr) __sync_add_and_fetch(ptr, 1)
hideakitai 0:3f9dbf1e2cb0 68
hideakitai 0:3f9dbf1e2cb0 69 #endif
hideakitai 0:3f9dbf1e2cb0 70
hideakitai 0:3f9dbf1e2cb0 71 #ifdef _WIN32
hideakitai 0:3f9dbf1e2cb0 72
hideakitai 0:3f9dbf1e2cb0 73 # ifdef __cplusplus
hideakitai 0:3f9dbf1e2cb0 74 /* numeric_limits<T>::min,max */
hideakitai 0:3f9dbf1e2cb0 75 # ifdef max
hideakitai 0:3f9dbf1e2cb0 76 # undef max
hideakitai 0:3f9dbf1e2cb0 77 # endif
hideakitai 0:3f9dbf1e2cb0 78 # ifdef min
hideakitai 0:3f9dbf1e2cb0 79 # undef min
hideakitai 0:3f9dbf1e2cb0 80 # endif
hideakitai 0:3f9dbf1e2cb0 81 # endif
hideakitai 0:3f9dbf1e2cb0 82
hideakitai 0:3f9dbf1e2cb0 83 //#else /* _*/
hideakitai 0:3f9dbf1e2cb0 84 #elif !defined(__MBED__) && !defined(__AVR__)
hideakitai 0:3f9dbf1e2cb0 85
hideakitai 0:3f9dbf1e2cb0 86 #include <arpa/inet.h> /* __BYTE_ORDER */
hideakitai 0:3f9dbf1e2cb0 87 # if defined(linux)
hideakitai 0:3f9dbf1e2cb0 88 # include <byteswap.h>
hideakitai 0:3f9dbf1e2cb0 89 # endif
hideakitai 0:3f9dbf1e2cb0 90
hideakitai 0:3f9dbf1e2cb0 91 #endif
hideakitai 0:3f9dbf1e2cb0 92
hideakitai 0:3f9dbf1e2cb0 93 #if MSGPACK_ENDIAN_LITTLE_BYTE
hideakitai 0:3f9dbf1e2cb0 94
hideakitai 0:3f9dbf1e2cb0 95 # if defined(_WIN32) || defined(__MBED__) || defined(__AVR__)
hideakitai 0:3f9dbf1e2cb0 96 # if defined(ntohs)
hideakitai 0:3f9dbf1e2cb0 97 # define _msgpack_be16(x) ntohs(x)
hideakitai 0:3f9dbf1e2cb0 98 # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400)
hideakitai 0:3f9dbf1e2cb0 99 # define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
hideakitai 0:3f9dbf1e2cb0 100 # else
hideakitai 0:3f9dbf1e2cb0 101 # define _msgpack_be16(x) ( \
hideakitai 0:3f9dbf1e2cb0 102 ((((uint16_t)x) << 8) ) | \
hideakitai 0:3f9dbf1e2cb0 103 ((((uint16_t)x) >> 8) ) )
hideakitai 0:3f9dbf1e2cb0 104 # endif
hideakitai 0:3f9dbf1e2cb0 105 # else
hideakitai 0:3f9dbf1e2cb0 106 # define _msgpack_be16(x) ntohs(x)
hideakitai 0:3f9dbf1e2cb0 107 # endif
hideakitai 0:3f9dbf1e2cb0 108
hideakitai 0:3f9dbf1e2cb0 109 # if defined(_WIN32) || defined(__MBED__) || defined(__AVR__)
hideakitai 0:3f9dbf1e2cb0 110 # if defined(ntohl)
hideakitai 0:3f9dbf1e2cb0 111 # define _msgpack_be32(x) ntohl(x)
hideakitai 0:3f9dbf1e2cb0 112 # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400)
hideakitai 0:3f9dbf1e2cb0 113 # define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x))
hideakitai 0:3f9dbf1e2cb0 114 # else
hideakitai 0:3f9dbf1e2cb0 115 # define _msgpack_be32(x) \
hideakitai 0:3f9dbf1e2cb0 116 ( ((((uint32_t)x) << 24) ) | \
hideakitai 0:3f9dbf1e2cb0 117 ((((uint32_t)x) << 8) & 0x00ff0000U ) | \
hideakitai 0:3f9dbf1e2cb0 118 ((((uint32_t)x) >> 8) & 0x0000ff00U ) | \
hideakitai 0:3f9dbf1e2cb0 119 ((((uint32_t)x) >> 24) ) )
hideakitai 0:3f9dbf1e2cb0 120 # endif
hideakitai 0:3f9dbf1e2cb0 121 # else
hideakitai 0:3f9dbf1e2cb0 122 # define _msgpack_be32(x) ntohl(x)
hideakitai 0:3f9dbf1e2cb0 123 # endif
hideakitai 0:3f9dbf1e2cb0 124
hideakitai 0:3f9dbf1e2cb0 125 # if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400)
hideakitai 0:3f9dbf1e2cb0 126 # define _msgpack_be64(x) (_byteswap_uint64(x))
hideakitai 0:3f9dbf1e2cb0 127 # elif defined(bswap_64)
hideakitai 0:3f9dbf1e2cb0 128 # define _msgpack_be64(x) bswap_64(x)
hideakitai 0:3f9dbf1e2cb0 129 # elif defined(__DARWIN_OSSwapInt64)
hideakitai 0:3f9dbf1e2cb0 130 # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x)
hideakitai 0:3f9dbf1e2cb0 131 # else
hideakitai 0:3f9dbf1e2cb0 132 # define _msgpack_be64(x) \
hideakitai 0:3f9dbf1e2cb0 133 ( ((((uint64_t)x) << 56) ) | \
hideakitai 0:3f9dbf1e2cb0 134 ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
hideakitai 0:3f9dbf1e2cb0 135 ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
hideakitai 0:3f9dbf1e2cb0 136 ((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
hideakitai 0:3f9dbf1e2cb0 137 ((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
hideakitai 0:3f9dbf1e2cb0 138 ((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
hideakitai 0:3f9dbf1e2cb0 139 ((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
hideakitai 0:3f9dbf1e2cb0 140 ((((uint64_t)x) >> 56) ) )
hideakitai 0:3f9dbf1e2cb0 141 # endif
hideakitai 0:3f9dbf1e2cb0 142
hideakitai 0:3f9dbf1e2cb0 143 #elif MSGPACK_ENDIAN_BIG_BYTE
hideakitai 0:3f9dbf1e2cb0 144
hideakitai 0:3f9dbf1e2cb0 145 # define _msgpack_be16(x) (x)
hideakitai 0:3f9dbf1e2cb0 146 # define _msgpack_be32(x) (x)
hideakitai 0:3f9dbf1e2cb0 147 # define _msgpack_be64(x) (x)
hideakitai 0:3f9dbf1e2cb0 148
hideakitai 0:3f9dbf1e2cb0 149 #else
hideakitai 0:3f9dbf1e2cb0 150 # error msgpack-c supports only big endian and little endian
hideakitai 0:3f9dbf1e2cb0 151 #endif /* MSGPACK_ENDIAN_LITTLE_BYTE */
hideakitai 0:3f9dbf1e2cb0 152
hideakitai 0:3f9dbf1e2cb0 153 #define _msgpack_load16(cast, from, to) do { \
hideakitai 0:3f9dbf1e2cb0 154 memcpy((cast*)(to), (from), sizeof(cast)); \
hideakitai 0:3f9dbf1e2cb0 155 *(to) = _msgpack_be16(*(to)); \
hideakitai 0:3f9dbf1e2cb0 156 } while (0);
hideakitai 0:3f9dbf1e2cb0 157
hideakitai 0:3f9dbf1e2cb0 158 #define _msgpack_load32(cast, from, to) do { \
hideakitai 0:3f9dbf1e2cb0 159 memcpy((cast*)(to), (from), sizeof(cast)); \
hideakitai 0:3f9dbf1e2cb0 160 *(to) = _msgpack_be32(*(to)); \
hideakitai 0:3f9dbf1e2cb0 161 } while (0);
hideakitai 0:3f9dbf1e2cb0 162 #define _msgpack_load64(cast, from, to) do { \
hideakitai 0:3f9dbf1e2cb0 163 memcpy((cast*)(to), (from), sizeof(cast)); \
hideakitai 0:3f9dbf1e2cb0 164 *(to) = _msgpack_be64(*(to)); \
hideakitai 0:3f9dbf1e2cb0 165 } while (0);
hideakitai 0:3f9dbf1e2cb0 166
hideakitai 0:3f9dbf1e2cb0 167 #define _msgpack_store16(to, num) \
hideakitai 0:3f9dbf1e2cb0 168 do { uint16_t val = _msgpack_be16(num); memcpy(to, &val, 2); } while(0)
hideakitai 0:3f9dbf1e2cb0 169 #define _msgpack_store32(to, num) \
hideakitai 0:3f9dbf1e2cb0 170 do { uint32_t val = _msgpack_be32(num); memcpy(to, &val, 4); } while(0)
hideakitai 0:3f9dbf1e2cb0 171 #define _msgpack_store64(to, num) \
hideakitai 0:3f9dbf1e2cb0 172 do { uint64_t val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0)
hideakitai 0:3f9dbf1e2cb0 173
hideakitai 0:3f9dbf1e2cb0 174 /*
hideakitai 0:3f9dbf1e2cb0 175 #define _msgpack_load16(cast, from) \
hideakitai 0:3f9dbf1e2cb0 176 ({ cast val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); })
hideakitai 0:3f9dbf1e2cb0 177 #define _msgpack_load32(cast, from) \
hideakitai 0:3f9dbf1e2cb0 178 ({ cast val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); })
hideakitai 0:3f9dbf1e2cb0 179 #define _msgpack_load64(cast, from) \
hideakitai 0:3f9dbf1e2cb0 180 ({ cast val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); })
hideakitai 0:3f9dbf1e2cb0 181 */
hideakitai 0:3f9dbf1e2cb0 182
hideakitai 0:3f9dbf1e2cb0 183
hideakitai 0:3f9dbf1e2cb0 184 #if !defined(__cplusplus) && defined(_MSC_VER)
hideakitai 0:3f9dbf1e2cb0 185 # if !defined(FALSE)
hideakitai 0:3f9dbf1e2cb0 186 # define FALSE (0)
hideakitai 0:3f9dbf1e2cb0 187 # endif
hideakitai 0:3f9dbf1e2cb0 188 # if !defined(TRUE)
hideakitai 0:3f9dbf1e2cb0 189 # define TRUE (!FALSE)
hideakitai 0:3f9dbf1e2cb0 190 # endif
hideakitai 0:3f9dbf1e2cb0 191 # if _MSC_VER >= 1800
hideakitai 0:3f9dbf1e2cb0 192 # include <stdbool.h>
hideakitai 0:3f9dbf1e2cb0 193 # else
hideakitai 0:3f9dbf1e2cb0 194 # define bool int
hideakitai 0:3f9dbf1e2cb0 195 # define true TRUE
hideakitai 0:3f9dbf1e2cb0 196 # define false FALSE
hideakitai 0:3f9dbf1e2cb0 197 # endif
hideakitai 0:3f9dbf1e2cb0 198 # define inline __inline
hideakitai 0:3f9dbf1e2cb0 199 #endif
hideakitai 0:3f9dbf1e2cb0 200
hideakitai 0:3f9dbf1e2cb0 201 #endif /* msgpack/sysdep.h */