Etherios Cloud Connector very first porting for mbed. Tested in an LPC1768
Etherios Cloud Connector for Embedded v2.1.0.3 library for mbed. Early porting.
This port is centered mainly in the platform code. So it should work properly with the provided examples of send_data, device_request, data_points, RCI and firmware_update (stub implementation, not a real one... yet ;-)). Filesystem is not implemented yet, and some examples might need changes.
To run, it needs the following libraries: - mbed - mbed-rtos - EthernetInterface
Find more information (and the source code!) about Etherios Cloud Connector for Embedded here: http://www.etherios.com/products/devicecloud/support/connector and in: http://www.etherios.com
private/connector_sm_utils.h@0:1c358ea10753, 2013-12-03 (annotated)
- Committer:
- spastor
- Date:
- Tue Dec 03 13:34:02 2013 +0000
- Revision:
- 0:1c358ea10753
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spastor | 0:1c358ea10753 | 1 | /* |
spastor | 0:1c358ea10753 | 2 | * Copyright (c) 2013 Digi International Inc., |
spastor | 0:1c358ea10753 | 3 | * All rights not expressly granted are reserved. |
spastor | 0:1c358ea10753 | 4 | * |
spastor | 0:1c358ea10753 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
spastor | 0:1c358ea10753 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
spastor | 0:1c358ea10753 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
spastor | 0:1c358ea10753 | 8 | * |
spastor | 0:1c358ea10753 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
spastor | 0:1c358ea10753 | 10 | * ======================================================================= |
spastor | 0:1c358ea10753 | 11 | */ |
spastor | 0:1c358ea10753 | 12 | |
spastor | 0:1c358ea10753 | 13 | static uint16_t const crc_table[] = |
spastor | 0:1c358ea10753 | 14 | { |
spastor | 0:1c358ea10753 | 15 | 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, |
spastor | 0:1c358ea10753 | 16 | 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, |
spastor | 0:1c358ea10753 | 17 | 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, |
spastor | 0:1c358ea10753 | 18 | 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, |
spastor | 0:1c358ea10753 | 19 | 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, |
spastor | 0:1c358ea10753 | 20 | 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, |
spastor | 0:1c358ea10753 | 21 | 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, |
spastor | 0:1c358ea10753 | 22 | 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, |
spastor | 0:1c358ea10753 | 23 | 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, |
spastor | 0:1c358ea10753 | 24 | 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, |
spastor | 0:1c358ea10753 | 25 | 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, |
spastor | 0:1c358ea10753 | 26 | 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, |
spastor | 0:1c358ea10753 | 27 | 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, |
spastor | 0:1c358ea10753 | 28 | 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, |
spastor | 0:1c358ea10753 | 29 | 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, |
spastor | 0:1c358ea10753 | 30 | 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, |
spastor | 0:1c358ea10753 | 31 | 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, |
spastor | 0:1c358ea10753 | 32 | 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, |
spastor | 0:1c358ea10753 | 33 | 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, |
spastor | 0:1c358ea10753 | 34 | 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, |
spastor | 0:1c358ea10753 | 35 | 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, |
spastor | 0:1c358ea10753 | 36 | 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, |
spastor | 0:1c358ea10753 | 37 | 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, |
spastor | 0:1c358ea10753 | 38 | 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, |
spastor | 0:1c358ea10753 | 39 | 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, |
spastor | 0:1c358ea10753 | 40 | 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, |
spastor | 0:1c358ea10753 | 41 | 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, |
spastor | 0:1c358ea10753 | 42 | 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, |
spastor | 0:1c358ea10753 | 43 | 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, |
spastor | 0:1c358ea10753 | 44 | 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, |
spastor | 0:1c358ea10753 | 45 | 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, |
spastor | 0:1c358ea10753 | 46 | 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 |
spastor | 0:1c358ea10753 | 47 | }; |
spastor | 0:1c358ea10753 | 48 | |
spastor | 0:1c358ea10753 | 49 | #define SmCRC16(crcval, newchar) crcval = ((crcval) >> 8) ^ crc_table[((crcval) ^ (newchar)) & 0x00ff] |
spastor | 0:1c358ea10753 | 50 | |
spastor | 0:1c358ea10753 | 51 | static uint16_t sm_calculate_crc16(uint16_t crc, uint8_t const * const data, size_t const bytes) |
spastor | 0:1c358ea10753 | 52 | { |
spastor | 0:1c358ea10753 | 53 | size_t i; |
spastor | 0:1c358ea10753 | 54 | |
spastor | 0:1c358ea10753 | 55 | for (i = 0; i < bytes; i++) |
spastor | 0:1c358ea10753 | 56 | SmCRC16(crc, data[i]); |
spastor | 0:1c358ea10753 | 57 | |
spastor | 0:1c358ea10753 | 58 | return crc; |
spastor | 0:1c358ea10753 | 59 | } |
spastor | 0:1c358ea10753 | 60 | |
spastor | 0:1c358ea10753 | 61 | #if (defined CONNECTOR_TRANSPORT_SMS) |
spastor | 0:1c358ea10753 | 62 | /* Base85 encoding lookup table. */ |
spastor | 0:1c358ea10753 | 63 | static uint8_t const encode85_table[] = |
spastor | 0:1c358ea10753 | 64 | { |
spastor | 0:1c358ea10753 | 65 | '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', |
spastor | 0:1c358ea10753 | 66 | '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', |
spastor | 0:1c358ea10753 | 67 | '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', |
spastor | 0:1c358ea10753 | 68 | '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
spastor | 0:1c358ea10753 | 69 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
spastor | 0:1c358ea10753 | 70 | 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', 'a', |
spastor | 0:1c358ea10753 | 71 | 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', |
spastor | 0:1c358ea10753 | 72 | 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', |
spastor | 0:1c358ea10753 | 73 | 'v', 'w', 'x', 'y', 'z' |
spastor | 0:1c358ea10753 | 74 | }; |
spastor | 0:1c358ea10753 | 75 | |
spastor | 0:1c358ea10753 | 76 | /* Base85 decoding lookup table. */ |
spastor | 0:1c358ea10753 | 77 | static uint8_t const decode85_table[] = |
spastor | 0:1c358ea10753 | 78 | { |
spastor | 0:1c358ea10753 | 79 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 80 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, |
spastor | 0:1c358ea10753 | 81 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
spastor | 0:1c358ea10753 | 82 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, |
spastor | 0:1c358ea10753 | 83 | 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 0, 0, 0, 58, 0, 59, 60, 61, |
spastor | 0:1c358ea10753 | 84 | 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, |
spastor | 0:1c358ea10753 | 85 | 82, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 86 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 87 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 88 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 89 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 90 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
spastor | 0:1c358ea10753 | 91 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
spastor | 0:1c358ea10753 | 92 | }; |
spastor | 0:1c358ea10753 | 93 | |
spastor | 0:1c358ea10753 | 94 | |
spastor | 0:1c358ea10753 | 95 | static unsigned long const pow85[] = |
spastor | 0:1c358ea10753 | 96 | { |
spastor | 0:1c358ea10753 | 97 | 85*85*85*85, 85*85*85, 85*85, 85, 1 |
spastor | 0:1c358ea10753 | 98 | }; |
spastor | 0:1c358ea10753 | 99 | |
spastor | 0:1c358ea10753 | 100 | static int sm_encode85(uint8_t * dest, size_t dest_len, uint8_t const * const src, size_t const src_len) |
spastor | 0:1c358ea10753 | 101 | { |
spastor | 0:1c358ea10753 | 102 | uint8_t buf[5]; |
spastor | 0:1c358ea10753 | 103 | uint32_t tuple = 0; |
spastor | 0:1c358ea10753 | 104 | size_t src_count = 0; |
spastor | 0:1c358ea10753 | 105 | size_t dest_count = 0; |
spastor | 0:1c358ea10753 | 106 | size_t count = 0; |
spastor | 0:1c358ea10753 | 107 | size_t i; |
spastor | 0:1c358ea10753 | 108 | unsigned char *s; |
spastor | 0:1c358ea10753 | 109 | |
spastor | 0:1c358ea10753 | 110 | while (src_count < src_len) |
spastor | 0:1c358ea10753 | 111 | { |
spastor | 0:1c358ea10753 | 112 | unsigned const c = (*(src + src_count)) & 0xFF; |
spastor | 0:1c358ea10753 | 113 | |
spastor | 0:1c358ea10753 | 114 | switch (count++) { |
spastor | 0:1c358ea10753 | 115 | case 0: |
spastor | 0:1c358ea10753 | 116 | tuple |= (c << 24); |
spastor | 0:1c358ea10753 | 117 | break; |
spastor | 0:1c358ea10753 | 118 | case 1: |
spastor | 0:1c358ea10753 | 119 | tuple |= (c << 16); |
spastor | 0:1c358ea10753 | 120 | break; |
spastor | 0:1c358ea10753 | 121 | case 2: |
spastor | 0:1c358ea10753 | 122 | tuple |= (c << 8); |
spastor | 0:1c358ea10753 | 123 | break; |
spastor | 0:1c358ea10753 | 124 | case 3: |
spastor | 0:1c358ea10753 | 125 | tuple |= c; |
spastor | 0:1c358ea10753 | 126 | i = 5; |
spastor | 0:1c358ea10753 | 127 | s = buf; |
spastor | 0:1c358ea10753 | 128 | do { |
spastor | 0:1c358ea10753 | 129 | *s++ = tuple % 85; |
spastor | 0:1c358ea10753 | 130 | tuple /= 85; |
spastor | 0:1c358ea10753 | 131 | } while (--i > 0); |
spastor | 0:1c358ea10753 | 132 | i = count; |
spastor | 0:1c358ea10753 | 133 | do { |
spastor | 0:1c358ea10753 | 134 | *dest++ = encode85_table[*--s]; |
spastor | 0:1c358ea10753 | 135 | dest_count++; |
spastor | 0:1c358ea10753 | 136 | if (dest_count >= dest_len) { |
spastor | 0:1c358ea10753 | 137 | return dest_count; |
spastor | 0:1c358ea10753 | 138 | } |
spastor | 0:1c358ea10753 | 139 | } while (i-- > 0); |
spastor | 0:1c358ea10753 | 140 | |
spastor | 0:1c358ea10753 | 141 | tuple = 0; |
spastor | 0:1c358ea10753 | 142 | count = 0; |
spastor | 0:1c358ea10753 | 143 | break; |
spastor | 0:1c358ea10753 | 144 | } |
spastor | 0:1c358ea10753 | 145 | src_count++; |
spastor | 0:1c358ea10753 | 146 | } |
spastor | 0:1c358ea10753 | 147 | |
spastor | 0:1c358ea10753 | 148 | /* Cleanup any remaining bytes... */ |
spastor | 0:1c358ea10753 | 149 | if (count > 0) { |
spastor | 0:1c358ea10753 | 150 | i = 5; |
spastor | 0:1c358ea10753 | 151 | s = buf; |
spastor | 0:1c358ea10753 | 152 | do { |
spastor | 0:1c358ea10753 | 153 | *s++ = tuple % 85; |
spastor | 0:1c358ea10753 | 154 | tuple /= 85; |
spastor | 0:1c358ea10753 | 155 | } while (--i > 0); |
spastor | 0:1c358ea10753 | 156 | i = count; |
spastor | 0:1c358ea10753 | 157 | do { |
spastor | 0:1c358ea10753 | 158 | *dest++ = encode85_table[*--s]; |
spastor | 0:1c358ea10753 | 159 | dest_count++; |
spastor | 0:1c358ea10753 | 160 | if (dest_count >= dest_len) { |
spastor | 0:1c358ea10753 | 161 | return dest_count; |
spastor | 0:1c358ea10753 | 162 | } |
spastor | 0:1c358ea10753 | 163 | } while (i-- > 0); |
spastor | 0:1c358ea10753 | 164 | } |
spastor | 0:1c358ea10753 | 165 | |
spastor | 0:1c358ea10753 | 166 | return dest_count; |
spastor | 0:1c358ea10753 | 167 | } |
spastor | 0:1c358ea10753 | 168 | |
spastor | 0:1c358ea10753 | 169 | static int sm_decode85(uint8_t * dest, size_t dest_len, uint8_t const * const src, size_t const src_len) |
spastor | 0:1c358ea10753 | 170 | { |
spastor | 0:1c358ea10753 | 171 | unsigned long tuple = 0; |
spastor | 0:1c358ea10753 | 172 | int c; |
spastor | 0:1c358ea10753 | 173 | size_t count = 0; |
spastor | 0:1c358ea10753 | 174 | size_t src_count = 0; |
spastor | 0:1c358ea10753 | 175 | size_t dest_count = 0; |
spastor | 0:1c358ea10753 | 176 | |
spastor | 0:1c358ea10753 | 177 | ASSERT_GOTO(dest_len >= (src_len * 4)/5, error); |
spastor | 0:1c358ea10753 | 178 | while (src_count < src_len) { |
spastor | 0:1c358ea10753 | 179 | c = (*(src + src_count)) & 0xFF; |
spastor | 0:1c358ea10753 | 180 | tuple += decode85_table[c] * pow85[count++]; |
spastor | 0:1c358ea10753 | 181 | if (count == 5) { |
spastor | 0:1c358ea10753 | 182 | *dest++ = tuple >> 24; |
spastor | 0:1c358ea10753 | 183 | *dest++ = tuple >> 16; |
spastor | 0:1c358ea10753 | 184 | *dest++ = tuple >> 8; |
spastor | 0:1c358ea10753 | 185 | *dest++ = tuple; |
spastor | 0:1c358ea10753 | 186 | dest_count += 4; |
spastor | 0:1c358ea10753 | 187 | count = 0; |
spastor | 0:1c358ea10753 | 188 | tuple = 0; |
spastor | 0:1c358ea10753 | 189 | } |
spastor | 0:1c358ea10753 | 190 | src_count++; |
spastor | 0:1c358ea10753 | 191 | } |
spastor | 0:1c358ea10753 | 192 | |
spastor | 0:1c358ea10753 | 193 | /* Cleanup any remaining bytes... */ |
spastor | 0:1c358ea10753 | 194 | if (count > 0) { |
spastor | 0:1c358ea10753 | 195 | count--; |
spastor | 0:1c358ea10753 | 196 | tuple += pow85[count]; |
spastor | 0:1c358ea10753 | 197 | |
spastor | 0:1c358ea10753 | 198 | switch (count) { |
spastor | 0:1c358ea10753 | 199 | case 4: |
spastor | 0:1c358ea10753 | 200 | *dest++ = tuple >> 24; |
spastor | 0:1c358ea10753 | 201 | *dest++ = tuple >> 16; |
spastor | 0:1c358ea10753 | 202 | *dest++ = tuple >> 8; |
spastor | 0:1c358ea10753 | 203 | *dest++ = tuple; |
spastor | 0:1c358ea10753 | 204 | dest_count += 4; |
spastor | 0:1c358ea10753 | 205 | break; |
spastor | 0:1c358ea10753 | 206 | case 3: |
spastor | 0:1c358ea10753 | 207 | *dest++ = tuple >> 24; |
spastor | 0:1c358ea10753 | 208 | *dest++ = tuple >> 16; |
spastor | 0:1c358ea10753 | 209 | *dest++ = tuple >> 8; |
spastor | 0:1c358ea10753 | 210 | dest_count += 3; |
spastor | 0:1c358ea10753 | 211 | break; |
spastor | 0:1c358ea10753 | 212 | case 2: |
spastor | 0:1c358ea10753 | 213 | *dest++ = tuple >> 24; |
spastor | 0:1c358ea10753 | 214 | *dest++ = tuple >> 16; |
spastor | 0:1c358ea10753 | 215 | dest_count += 2; |
spastor | 0:1c358ea10753 | 216 | break; |
spastor | 0:1c358ea10753 | 217 | case 1: |
spastor | 0:1c358ea10753 | 218 | *dest++ = tuple >> 24; |
spastor | 0:1c358ea10753 | 219 | dest_count++;; |
spastor | 0:1c358ea10753 | 220 | break; |
spastor | 0:1c358ea10753 | 221 | } |
spastor | 0:1c358ea10753 | 222 | } |
spastor | 0:1c358ea10753 | 223 | |
spastor | 0:1c358ea10753 | 224 | error: |
spastor | 0:1c358ea10753 | 225 | return dest_count; |
spastor | 0:1c358ea10753 | 226 | } |
spastor | 0:1c358ea10753 | 227 | #endif |