Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /* Socket
switches 0:5c4d7b2438d3 2 * Copyright (c) 2015 ARM Limited
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 5 * you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 6 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 7 *
switches 0:5c4d7b2438d3 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 13 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 14 * limitations under the License.
switches 0:5c4d7b2438d3 15 */
switches 0:5c4d7b2438d3 16
switches 0:5c4d7b2438d3 17 #include "SocketAddress.h"
switches 0:5c4d7b2438d3 18 #include "NetworkInterface.h"
switches 0:5c4d7b2438d3 19 #include "NetworkStack.h"
switches 0:5c4d7b2438d3 20 #include <string.h>
switches 0:5c4d7b2438d3 21 #include "mbed.h"
switches 0:5c4d7b2438d3 22
switches 0:5c4d7b2438d3 23
switches 0:5c4d7b2438d3 24 static bool ipv4_is_valid(const char *addr)
switches 0:5c4d7b2438d3 25 {
switches 0:5c4d7b2438d3 26 int i = 0;
switches 0:5c4d7b2438d3 27
switches 0:5c4d7b2438d3 28 // Check each digit for [0-9.]
switches 0:5c4d7b2438d3 29 for (; addr[i]; i++) {
switches 0:5c4d7b2438d3 30 if (!(addr[i] >= '0' && addr[i] <= '9') && addr[i] != '.') {
switches 0:5c4d7b2438d3 31 return false;
switches 0:5c4d7b2438d3 32 }
switches 0:5c4d7b2438d3 33 }
switches 0:5c4d7b2438d3 34
switches 0:5c4d7b2438d3 35 // Ending with '.' garuntees host
switches 0:5c4d7b2438d3 36 if (i > 0 && addr[i-1] == '.') {
switches 0:5c4d7b2438d3 37 return false;
switches 0:5c4d7b2438d3 38 }
switches 0:5c4d7b2438d3 39
switches 0:5c4d7b2438d3 40 return true;
switches 0:5c4d7b2438d3 41 }
switches 0:5c4d7b2438d3 42
switches 0:5c4d7b2438d3 43 static bool ipv6_is_valid(const char *addr)
switches 0:5c4d7b2438d3 44 {
switches 0:5c4d7b2438d3 45 // Check each digit for [0-9a-fA-F:]
switches 0:5c4d7b2438d3 46 // Must also have at least 2 colons
switches 0:5c4d7b2438d3 47 int colons = 0;
switches 0:5c4d7b2438d3 48 for (int i = 0; addr[i]; i++) {
switches 0:5c4d7b2438d3 49 if (!(addr[i] >= '0' && addr[i] <= '9') &&
switches 0:5c4d7b2438d3 50 !(addr[i] >= 'a' && addr[i] <= 'f') &&
switches 0:5c4d7b2438d3 51 !(addr[i] >= 'A' && addr[i] <= 'F') &&
switches 0:5c4d7b2438d3 52 addr[i] != ':') {
switches 0:5c4d7b2438d3 53 return false;
switches 0:5c4d7b2438d3 54 }
switches 0:5c4d7b2438d3 55 if (addr[i] == ':') {
switches 0:5c4d7b2438d3 56 colons++;
switches 0:5c4d7b2438d3 57 }
switches 0:5c4d7b2438d3 58 }
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 return colons >= 2;
switches 0:5c4d7b2438d3 61 }
switches 0:5c4d7b2438d3 62
switches 0:5c4d7b2438d3 63 static void ipv4_from_address(uint8_t *bytes, const char *addr)
switches 0:5c4d7b2438d3 64 {
switches 0:5c4d7b2438d3 65 int count = 0;
switches 0:5c4d7b2438d3 66 int i = 0;
switches 0:5c4d7b2438d3 67
switches 0:5c4d7b2438d3 68 for (; count < NSAPI_IPv4_BYTES; count++) {
switches 0:5c4d7b2438d3 69 unsigned char b;
switches 0:5c4d7b2438d3 70 int scanned = sscanf(&addr[i], "%hhu", &b);
switches 0:5c4d7b2438d3 71 if (scanned < 1) {
switches 0:5c4d7b2438d3 72 return;
switches 0:5c4d7b2438d3 73 }
switches 0:5c4d7b2438d3 74
switches 0:5c4d7b2438d3 75 bytes[count] = b;
switches 0:5c4d7b2438d3 76
switches 0:5c4d7b2438d3 77 for (; addr[i] != '.'; i++) {
switches 0:5c4d7b2438d3 78 if (!addr[i]) {
switches 0:5c4d7b2438d3 79 return;
switches 0:5c4d7b2438d3 80 }
switches 0:5c4d7b2438d3 81 }
switches 0:5c4d7b2438d3 82
switches 0:5c4d7b2438d3 83 i++;
switches 0:5c4d7b2438d3 84 }
switches 0:5c4d7b2438d3 85 }
switches 0:5c4d7b2438d3 86
switches 0:5c4d7b2438d3 87 static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk) {
switches 0:5c4d7b2438d3 88 int count = 0;
switches 0:5c4d7b2438d3 89 int i = 0;
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 for (; count < NSAPI_IPv6_BYTES/2; count++) {
switches 0:5c4d7b2438d3 92 unsigned short s;
switches 0:5c4d7b2438d3 93 int scanned = sscanf(&chunk[i], "%hx", &s);
switches 0:5c4d7b2438d3 94 if (scanned < 1) {
switches 0:5c4d7b2438d3 95 return count;
switches 0:5c4d7b2438d3 96 }
switches 0:5c4d7b2438d3 97
switches 0:5c4d7b2438d3 98 shorts[count] = s;
switches 0:5c4d7b2438d3 99
switches 0:5c4d7b2438d3 100 for (; chunk[i] != ':'; i++) {
switches 0:5c4d7b2438d3 101 if (!chunk[i]) {
switches 0:5c4d7b2438d3 102 return count+1;
switches 0:5c4d7b2438d3 103 }
switches 0:5c4d7b2438d3 104 }
switches 0:5c4d7b2438d3 105
switches 0:5c4d7b2438d3 106 i++;
switches 0:5c4d7b2438d3 107 }
switches 0:5c4d7b2438d3 108
switches 0:5c4d7b2438d3 109 return count;
switches 0:5c4d7b2438d3 110 }
switches 0:5c4d7b2438d3 111
switches 0:5c4d7b2438d3 112 static void ipv6_from_address(uint8_t *bytes, const char *addr)
switches 0:5c4d7b2438d3 113 {
switches 0:5c4d7b2438d3 114 // Start with zeroed address
switches 0:5c4d7b2438d3 115 uint16_t shorts[NSAPI_IPv6_BYTES/2];
switches 0:5c4d7b2438d3 116 int suffix = 0;
switches 0:5c4d7b2438d3 117
switches 0:5c4d7b2438d3 118 // Find double colons and scan suffix
switches 0:5c4d7b2438d3 119 for (int i = 0; addr[i]; i++) {
switches 0:5c4d7b2438d3 120 if (addr[i] == ':' && addr[i+1] == ':') {
switches 0:5c4d7b2438d3 121 suffix = ipv6_scan_chunk(shorts, &addr[i+2]);
switches 0:5c4d7b2438d3 122 break;
switches 0:5c4d7b2438d3 123 }
switches 0:5c4d7b2438d3 124 }
switches 0:5c4d7b2438d3 125
switches 0:5c4d7b2438d3 126 // Move suffix to end
switches 0:5c4d7b2438d3 127 memmove(&shorts[NSAPI_IPv6_BYTES/2-suffix], &shorts[0],
switches 0:5c4d7b2438d3 128 suffix*sizeof(uint16_t));
switches 0:5c4d7b2438d3 129 memset(&shorts[0], 0,
switches 0:5c4d7b2438d3 130 (NSAPI_IPv6_BYTES/2-suffix)*sizeof(uint16_t));
switches 0:5c4d7b2438d3 131
switches 0:5c4d7b2438d3 132 // Scan prefix
switches 0:5c4d7b2438d3 133 ipv6_scan_chunk(shorts, &addr[0]);
switches 0:5c4d7b2438d3 134
switches 0:5c4d7b2438d3 135 // Flip bytes
switches 0:5c4d7b2438d3 136 for (int i = 0; i < NSAPI_IPv6_BYTES/2; i++) {
switches 0:5c4d7b2438d3 137 bytes[2*i+0] = (uint8_t)(shorts[i] >> 8);
switches 0:5c4d7b2438d3 138 bytes[2*i+1] = (uint8_t)(shorts[i] >> 0);
switches 0:5c4d7b2438d3 139 }
switches 0:5c4d7b2438d3 140 }
switches 0:5c4d7b2438d3 141
switches 0:5c4d7b2438d3 142 static void ipv4_to_address(char *addr, const uint8_t *bytes)
switches 0:5c4d7b2438d3 143 {
switches 0:5c4d7b2438d3 144 sprintf(addr, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
switches 0:5c4d7b2438d3 145 }
switches 0:5c4d7b2438d3 146
switches 0:5c4d7b2438d3 147 static void ipv6_to_address(char *addr, const uint8_t *bytes)
switches 0:5c4d7b2438d3 148 {
switches 0:5c4d7b2438d3 149 for (int i = 0; i < NSAPI_IPv6_BYTES/2; i++) {
switches 0:5c4d7b2438d3 150 sprintf(&addr[5*i], "%02x%02x", bytes[2*i], bytes[2*i+1]);
switches 0:5c4d7b2438d3 151 addr[5*i+4] = ':';
switches 0:5c4d7b2438d3 152 }
switches 0:5c4d7b2438d3 153 addr[NSAPI_IPv6_SIZE-1] = '\0';
switches 0:5c4d7b2438d3 154 }
switches 0:5c4d7b2438d3 155
switches 0:5c4d7b2438d3 156
switches 0:5c4d7b2438d3 157 SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
switches 0:5c4d7b2438d3 158 {
switches 0:5c4d7b2438d3 159 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 160 set_addr(addr);
switches 0:5c4d7b2438d3 161 set_port(port);
switches 0:5c4d7b2438d3 162 }
switches 0:5c4d7b2438d3 163
switches 0:5c4d7b2438d3 164 SocketAddress::SocketAddress(const char *addr, uint16_t port)
switches 0:5c4d7b2438d3 165 {
switches 0:5c4d7b2438d3 166 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 167 set_ip_address(addr);
switches 0:5c4d7b2438d3 168 set_port(port);
switches 0:5c4d7b2438d3 169 }
switches 0:5c4d7b2438d3 170
switches 0:5c4d7b2438d3 171 SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
switches 0:5c4d7b2438d3 172 {
switches 0:5c4d7b2438d3 173 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 174 set_ip_bytes(bytes, version);
switches 0:5c4d7b2438d3 175 set_port(port);
switches 0:5c4d7b2438d3 176 }
switches 0:5c4d7b2438d3 177
switches 0:5c4d7b2438d3 178 SocketAddress::SocketAddress(const SocketAddress &addr)
switches 0:5c4d7b2438d3 179 {
switches 0:5c4d7b2438d3 180 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 181 set_addr(addr.get_addr());
switches 0:5c4d7b2438d3 182 set_port(addr.get_port());
switches 0:5c4d7b2438d3 183 }
switches 0:5c4d7b2438d3 184
switches 0:5c4d7b2438d3 185 bool SocketAddress::set_ip_address(const char *addr)
switches 0:5c4d7b2438d3 186 {
switches 0:5c4d7b2438d3 187 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 188
switches 0:5c4d7b2438d3 189 if (addr && ipv4_is_valid(addr)) {
switches 0:5c4d7b2438d3 190 _addr.version = NSAPI_IPv4;
switches 0:5c4d7b2438d3 191 ipv4_from_address(_addr.bytes, addr);
switches 0:5c4d7b2438d3 192 return true;
switches 0:5c4d7b2438d3 193 } else if (addr && ipv6_is_valid(addr)) {
switches 0:5c4d7b2438d3 194 _addr.version = NSAPI_IPv6;
switches 0:5c4d7b2438d3 195 ipv6_from_address(_addr.bytes, addr);
switches 0:5c4d7b2438d3 196 return true;
switches 0:5c4d7b2438d3 197 } else {
switches 0:5c4d7b2438d3 198 _addr = nsapi_addr_t();
switches 0:5c4d7b2438d3 199 return false;
switches 0:5c4d7b2438d3 200 }
switches 0:5c4d7b2438d3 201 }
switches 0:5c4d7b2438d3 202
switches 0:5c4d7b2438d3 203 void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
switches 0:5c4d7b2438d3 204 {
switches 0:5c4d7b2438d3 205 nsapi_addr_t addr;
switches 0:5c4d7b2438d3 206 addr.version = version;
switches 0:5c4d7b2438d3 207 memcpy(addr.bytes, bytes, NSAPI_IP_BYTES);
switches 0:5c4d7b2438d3 208 set_addr(addr);
switches 0:5c4d7b2438d3 209 }
switches 0:5c4d7b2438d3 210
switches 0:5c4d7b2438d3 211 void SocketAddress::set_addr(nsapi_addr_t addr)
switches 0:5c4d7b2438d3 212 {
switches 0:5c4d7b2438d3 213 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 214 _addr = addr;
switches 0:5c4d7b2438d3 215 }
switches 0:5c4d7b2438d3 216
switches 0:5c4d7b2438d3 217 void SocketAddress::set_port(uint16_t port)
switches 0:5c4d7b2438d3 218 {
switches 0:5c4d7b2438d3 219 _port = port;
switches 0:5c4d7b2438d3 220 }
switches 0:5c4d7b2438d3 221
switches 0:5c4d7b2438d3 222 const char *SocketAddress::get_ip_address() const
switches 0:5c4d7b2438d3 223 {
switches 0:5c4d7b2438d3 224 if (_addr.version == NSAPI_UNSPEC) {
switches 0:5c4d7b2438d3 225 return NULL;
switches 0:5c4d7b2438d3 226 }
switches 0:5c4d7b2438d3 227
switches 0:5c4d7b2438d3 228 if (!_ip_address[0]) {
switches 0:5c4d7b2438d3 229 if (_addr.version == NSAPI_IPv4) {
switches 0:5c4d7b2438d3 230 ipv4_to_address(_ip_address, _addr.bytes);
switches 0:5c4d7b2438d3 231 } else if (_addr.version == NSAPI_IPv6) {
switches 0:5c4d7b2438d3 232 ipv6_to_address(_ip_address, _addr.bytes);
switches 0:5c4d7b2438d3 233 }
switches 0:5c4d7b2438d3 234 }
switches 0:5c4d7b2438d3 235
switches 0:5c4d7b2438d3 236 return _ip_address;
switches 0:5c4d7b2438d3 237 }
switches 0:5c4d7b2438d3 238
switches 0:5c4d7b2438d3 239 const void *SocketAddress::get_ip_bytes() const
switches 0:5c4d7b2438d3 240 {
switches 0:5c4d7b2438d3 241 return _addr.bytes;
switches 0:5c4d7b2438d3 242 }
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 nsapi_version_t SocketAddress::get_ip_version() const
switches 0:5c4d7b2438d3 245 {
switches 0:5c4d7b2438d3 246 return _addr.version;
switches 0:5c4d7b2438d3 247 }
switches 0:5c4d7b2438d3 248
switches 0:5c4d7b2438d3 249 nsapi_addr_t SocketAddress::get_addr() const
switches 0:5c4d7b2438d3 250 {
switches 0:5c4d7b2438d3 251 return _addr;
switches 0:5c4d7b2438d3 252 }
switches 0:5c4d7b2438d3 253
switches 0:5c4d7b2438d3 254 uint16_t SocketAddress::get_port() const
switches 0:5c4d7b2438d3 255 {
switches 0:5c4d7b2438d3 256 return _port;
switches 0:5c4d7b2438d3 257 }
switches 0:5c4d7b2438d3 258
switches 0:5c4d7b2438d3 259 SocketAddress::operator bool() const
switches 0:5c4d7b2438d3 260 {
switches 0:5c4d7b2438d3 261 if (_addr.version == NSAPI_IPv4) {
switches 0:5c4d7b2438d3 262 for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
switches 0:5c4d7b2438d3 263 if (_addr.bytes[i]) {
switches 0:5c4d7b2438d3 264 return true;
switches 0:5c4d7b2438d3 265 }
switches 0:5c4d7b2438d3 266 }
switches 0:5c4d7b2438d3 267
switches 0:5c4d7b2438d3 268 return false;
switches 0:5c4d7b2438d3 269 } else if (_addr.version == NSAPI_IPv6) {
switches 0:5c4d7b2438d3 270 for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
switches 0:5c4d7b2438d3 271 if (_addr.bytes[i]) {
switches 0:5c4d7b2438d3 272 return true;
switches 0:5c4d7b2438d3 273 }
switches 0:5c4d7b2438d3 274 }
switches 0:5c4d7b2438d3 275
switches 0:5c4d7b2438d3 276 return false;
switches 0:5c4d7b2438d3 277 } else {
switches 0:5c4d7b2438d3 278 return false;
switches 0:5c4d7b2438d3 279 }
switches 0:5c4d7b2438d3 280 }
switches 0:5c4d7b2438d3 281
switches 0:5c4d7b2438d3 282 bool operator==(const SocketAddress &a, const SocketAddress &b)
switches 0:5c4d7b2438d3 283 {
switches 0:5c4d7b2438d3 284 if (!a && !b) {
switches 0:5c4d7b2438d3 285 return true;
switches 0:5c4d7b2438d3 286 } else if (a._addr.version != b._addr.version) {
switches 0:5c4d7b2438d3 287 return false;
switches 0:5c4d7b2438d3 288 } else if (a._addr.version == NSAPI_IPv4) {
switches 0:5c4d7b2438d3 289 return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0;
switches 0:5c4d7b2438d3 290 } else if (a._addr.version == NSAPI_IPv6) {
switches 0:5c4d7b2438d3 291 return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0;
switches 0:5c4d7b2438d3 292 }
switches 0:5c4d7b2438d3 293
switches 0:5c4d7b2438d3 294 MBED_UNREACHABLE;
switches 0:5c4d7b2438d3 295 }
switches 0:5c4d7b2438d3 296
switches 0:5c4d7b2438d3 297 bool operator!=(const SocketAddress &a, const SocketAddress &b)
switches 0:5c4d7b2438d3 298 {
switches 0:5c4d7b2438d3 299 return !(a == b);
switches 0:5c4d7b2438d3 300 }
switches 0:5c4d7b2438d3 301
switches 0:5c4d7b2438d3 302 void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
switches 0:5c4d7b2438d3 303 {
switches 0:5c4d7b2438d3 304 _ip_address[0] = '\0';
switches 0:5c4d7b2438d3 305
switches 0:5c4d7b2438d3 306 // gethostbyname must check for literals, so can call it directly
switches 0:5c4d7b2438d3 307 int err = iface->gethostbyname(host, this);
switches 0:5c4d7b2438d3 308 _port = port;
switches 0:5c4d7b2438d3 309 if (err) {
switches 0:5c4d7b2438d3 310 _addr = nsapi_addr_t();
switches 0:5c4d7b2438d3 311 _port = 0;
switches 0:5c4d7b2438d3 312 }
switches 0:5c4d7b2438d3 313 }