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 "NetworkStack.h"
switches 0:5c4d7b2438d3 18 #include "nsapi_dns.h"
switches 0:5c4d7b2438d3 19 #include "mbed.h"
switches 0:5c4d7b2438d3 20 #include "stddef.h"
switches 0:5c4d7b2438d3 21 #include <new>
switches 0:5c4d7b2438d3 22
switches 0:5c4d7b2438d3 23
switches 0:5c4d7b2438d3 24 // Default NetworkStack operations
switches 0:5c4d7b2438d3 25 nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
switches 0:5c4d7b2438d3 26 {
switches 0:5c4d7b2438d3 27 // check for simple ip addresses
switches 0:5c4d7b2438d3 28 if (address->set_ip_address(name)) {
switches 0:5c4d7b2438d3 29 if (address->get_ip_version() != version) {
switches 0:5c4d7b2438d3 30 return NSAPI_ERROR_DNS_FAILURE;
switches 0:5c4d7b2438d3 31 }
switches 0:5c4d7b2438d3 32
switches 0:5c4d7b2438d3 33 return NSAPI_ERROR_OK;
switches 0:5c4d7b2438d3 34 }
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 // if the version is unspecified, try to guess the version from the
switches 0:5c4d7b2438d3 37 // ip address of the underlying stack
switches 0:5c4d7b2438d3 38 if (version == NSAPI_UNSPEC) {
switches 0:5c4d7b2438d3 39 SocketAddress testaddress;
switches 0:5c4d7b2438d3 40 if (testaddress.set_ip_address(this->get_ip_address())) {
switches 0:5c4d7b2438d3 41 version = testaddress.get_ip_version();
switches 0:5c4d7b2438d3 42 }
switches 0:5c4d7b2438d3 43 }
switches 0:5c4d7b2438d3 44
switches 0:5c4d7b2438d3 45 return nsapi_dns_query(this, name, address, version);
switches 0:5c4d7b2438d3 46 }
switches 0:5c4d7b2438d3 47
switches 0:5c4d7b2438d3 48 nsapi_error_t NetworkStack::add_dns_server(const SocketAddress &address)
switches 0:5c4d7b2438d3 49 {
switches 0:5c4d7b2438d3 50 return nsapi_dns_add_server(address);
switches 0:5c4d7b2438d3 51 }
switches 0:5c4d7b2438d3 52
switches 0:5c4d7b2438d3 53 nsapi_error_t NetworkStack::setstackopt(int level, int optname, const void *optval, unsigned optlen)
switches 0:5c4d7b2438d3 54 {
switches 0:5c4d7b2438d3 55 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 56 }
switches 0:5c4d7b2438d3 57
switches 0:5c4d7b2438d3 58 nsapi_error_t NetworkStack::getstackopt(int level, int optname, void *optval, unsigned *optlen)
switches 0:5c4d7b2438d3 59 {
switches 0:5c4d7b2438d3 60 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 61 }
switches 0:5c4d7b2438d3 62
switches 0:5c4d7b2438d3 63 nsapi_error_t NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen)
switches 0:5c4d7b2438d3 64 {
switches 0:5c4d7b2438d3 65 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 66 }
switches 0:5c4d7b2438d3 67
switches 0:5c4d7b2438d3 68 nsapi_error_t NetworkStack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen)
switches 0:5c4d7b2438d3 69 {
switches 0:5c4d7b2438d3 70 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 71 }
switches 0:5c4d7b2438d3 72
switches 0:5c4d7b2438d3 73
switches 0:5c4d7b2438d3 74 // NetworkStackWrapper class for encapsulating the raw nsapi_stack structure
switches 0:5c4d7b2438d3 75 class NetworkStackWrapper : public NetworkStack
switches 0:5c4d7b2438d3 76 {
switches 0:5c4d7b2438d3 77 private:
switches 0:5c4d7b2438d3 78 inline nsapi_stack_t *_stack()
switches 0:5c4d7b2438d3 79 {
switches 0:5c4d7b2438d3 80 return reinterpret_cast<nsapi_stack_t *>(
switches 0:5c4d7b2438d3 81 reinterpret_cast<uint8_t *>(this)
switches 0:5c4d7b2438d3 82 - offsetof(nsapi_stack_t, _stack_buffer));
switches 0:5c4d7b2438d3 83 }
switches 0:5c4d7b2438d3 84
switches 0:5c4d7b2438d3 85 inline const nsapi_stack_api_t *_stack_api()
switches 0:5c4d7b2438d3 86 {
switches 0:5c4d7b2438d3 87 return _stack()->stack_api;
switches 0:5c4d7b2438d3 88 }
switches 0:5c4d7b2438d3 89
switches 0:5c4d7b2438d3 90 public:
switches 0:5c4d7b2438d3 91 virtual const char *get_ip_address()
switches 0:5c4d7b2438d3 92 {
switches 0:5c4d7b2438d3 93 if (!_stack_api()->get_ip_address) {
switches 0:5c4d7b2438d3 94 return 0;
switches 0:5c4d7b2438d3 95 }
switches 0:5c4d7b2438d3 96
switches 0:5c4d7b2438d3 97 static uint8_t buffer[sizeof(SocketAddress)];
switches 0:5c4d7b2438d3 98 SocketAddress *address = new (buffer) SocketAddress(_stack_api()->get_ip_address(_stack()));
switches 0:5c4d7b2438d3 99 return address->get_ip_address();
switches 0:5c4d7b2438d3 100 }
switches 0:5c4d7b2438d3 101
switches 0:5c4d7b2438d3 102 virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
switches 0:5c4d7b2438d3 103 {
switches 0:5c4d7b2438d3 104 if (!_stack_api()->gethostbyname) {
switches 0:5c4d7b2438d3 105 return NetworkStack::gethostbyname(name, address, version);
switches 0:5c4d7b2438d3 106 }
switches 0:5c4d7b2438d3 107
switches 0:5c4d7b2438d3 108 nsapi_addr_t addr = {NSAPI_UNSPEC, 0};
switches 0:5c4d7b2438d3 109 nsapi_error_t err = _stack_api()->gethostbyname(_stack(), name, &addr, version);
switches 0:5c4d7b2438d3 110 address->set_addr(addr);
switches 0:5c4d7b2438d3 111 return err;
switches 0:5c4d7b2438d3 112 }
switches 0:5c4d7b2438d3 113
switches 0:5c4d7b2438d3 114 virtual nsapi_error_t add_dns_server(const SocketAddress &address)
switches 0:5c4d7b2438d3 115 {
switches 0:5c4d7b2438d3 116 if (!_stack_api()->add_dns_server) {
switches 0:5c4d7b2438d3 117 return NetworkStack::add_dns_server(address);
switches 0:5c4d7b2438d3 118 }
switches 0:5c4d7b2438d3 119
switches 0:5c4d7b2438d3 120 return _stack_api()->add_dns_server(_stack(), address.get_addr());
switches 0:5c4d7b2438d3 121 }
switches 0:5c4d7b2438d3 122
switches 0:5c4d7b2438d3 123 virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen)
switches 0:5c4d7b2438d3 124 {
switches 0:5c4d7b2438d3 125 if (!_stack_api()->setstackopt) {
switches 0:5c4d7b2438d3 126 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 127 }
switches 0:5c4d7b2438d3 128
switches 0:5c4d7b2438d3 129 return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen);
switches 0:5c4d7b2438d3 130 }
switches 0:5c4d7b2438d3 131
switches 0:5c4d7b2438d3 132 virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen)
switches 0:5c4d7b2438d3 133 {
switches 0:5c4d7b2438d3 134 if (!_stack_api()->getstackopt) {
switches 0:5c4d7b2438d3 135 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 136 }
switches 0:5c4d7b2438d3 137
switches 0:5c4d7b2438d3 138 return _stack_api()->getstackopt(_stack(), level, optname, optval, optlen);
switches 0:5c4d7b2438d3 139 }
switches 0:5c4d7b2438d3 140
switches 0:5c4d7b2438d3 141 protected:
switches 0:5c4d7b2438d3 142 virtual nsapi_error_t socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto)
switches 0:5c4d7b2438d3 143 {
switches 0:5c4d7b2438d3 144 if (!_stack_api()->socket_open) {
switches 0:5c4d7b2438d3 145 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 146 }
switches 0:5c4d7b2438d3 147
switches 0:5c4d7b2438d3 148 return _stack_api()->socket_open(_stack(), socket, proto);
switches 0:5c4d7b2438d3 149 }
switches 0:5c4d7b2438d3 150
switches 0:5c4d7b2438d3 151 virtual nsapi_error_t socket_close(nsapi_socket_t socket)
switches 0:5c4d7b2438d3 152 {
switches 0:5c4d7b2438d3 153 if (!_stack_api()->socket_close) {
switches 0:5c4d7b2438d3 154 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 155 }
switches 0:5c4d7b2438d3 156
switches 0:5c4d7b2438d3 157 return _stack_api()->socket_close(_stack(), socket);
switches 0:5c4d7b2438d3 158 }
switches 0:5c4d7b2438d3 159
switches 0:5c4d7b2438d3 160 virtual nsapi_error_t socket_bind(nsapi_socket_t socket, const SocketAddress &address)
switches 0:5c4d7b2438d3 161 {
switches 0:5c4d7b2438d3 162 if (!_stack_api()->socket_bind) {
switches 0:5c4d7b2438d3 163 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 164 }
switches 0:5c4d7b2438d3 165
switches 0:5c4d7b2438d3 166 return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port());
switches 0:5c4d7b2438d3 167 }
switches 0:5c4d7b2438d3 168
switches 0:5c4d7b2438d3 169 virtual nsapi_error_t socket_listen(nsapi_socket_t socket, int backlog)
switches 0:5c4d7b2438d3 170 {
switches 0:5c4d7b2438d3 171 if (!_stack_api()->socket_listen) {
switches 0:5c4d7b2438d3 172 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 173 }
switches 0:5c4d7b2438d3 174
switches 0:5c4d7b2438d3 175 return _stack_api()->socket_listen(_stack(), socket, backlog);
switches 0:5c4d7b2438d3 176 }
switches 0:5c4d7b2438d3 177
switches 0:5c4d7b2438d3 178 virtual nsapi_error_t socket_connect(nsapi_socket_t socket, const SocketAddress &address)
switches 0:5c4d7b2438d3 179 {
switches 0:5c4d7b2438d3 180 if (!_stack_api()->socket_connect) {
switches 0:5c4d7b2438d3 181 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 182 }
switches 0:5c4d7b2438d3 183
switches 0:5c4d7b2438d3 184 return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port());
switches 0:5c4d7b2438d3 185 }
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187 virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address)
switches 0:5c4d7b2438d3 188 {
switches 0:5c4d7b2438d3 189 if (!_stack_api()->socket_accept) {
switches 0:5c4d7b2438d3 190 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 191 }
switches 0:5c4d7b2438d3 192
switches 0:5c4d7b2438d3 193 nsapi_addr_t addr = {NSAPI_IPv4, 0};
switches 0:5c4d7b2438d3 194 uint16_t port = 0;
switches 0:5c4d7b2438d3 195
switches 0:5c4d7b2438d3 196 nsapi_error_t err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port);
switches 0:5c4d7b2438d3 197
switches 0:5c4d7b2438d3 198 if (address) {
switches 0:5c4d7b2438d3 199 address->set_addr(addr);
switches 0:5c4d7b2438d3 200 address->set_port(port);
switches 0:5c4d7b2438d3 201 }
switches 0:5c4d7b2438d3 202
switches 0:5c4d7b2438d3 203 return err;
switches 0:5c4d7b2438d3 204 }
switches 0:5c4d7b2438d3 205
switches 0:5c4d7b2438d3 206 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t socket, const void *data, nsapi_size_t size)
switches 0:5c4d7b2438d3 207 {
switches 0:5c4d7b2438d3 208 if (!_stack_api()->socket_send) {
switches 0:5c4d7b2438d3 209 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 210 }
switches 0:5c4d7b2438d3 211
switches 0:5c4d7b2438d3 212 return _stack_api()->socket_send(_stack(), socket, data, size);
switches 0:5c4d7b2438d3 213 }
switches 0:5c4d7b2438d3 214
switches 0:5c4d7b2438d3 215 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t socket, void *data, nsapi_size_t size)
switches 0:5c4d7b2438d3 216 {
switches 0:5c4d7b2438d3 217 if (!_stack_api()->socket_recv) {
switches 0:5c4d7b2438d3 218 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 219 }
switches 0:5c4d7b2438d3 220
switches 0:5c4d7b2438d3 221 return _stack_api()->socket_recv(_stack(), socket, data, size);
switches 0:5c4d7b2438d3 222 }
switches 0:5c4d7b2438d3 223
switches 0:5c4d7b2438d3 224 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, nsapi_size_t size)
switches 0:5c4d7b2438d3 225 {
switches 0:5c4d7b2438d3 226 if (!_stack_api()->socket_sendto) {
switches 0:5c4d7b2438d3 227 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 228 }
switches 0:5c4d7b2438d3 229
switches 0:5c4d7b2438d3 230 return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size);
switches 0:5c4d7b2438d3 231 }
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, nsapi_size_t size)
switches 0:5c4d7b2438d3 234 {
switches 0:5c4d7b2438d3 235 if (!_stack_api()->socket_recvfrom) {
switches 0:5c4d7b2438d3 236 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 237 }
switches 0:5c4d7b2438d3 238
switches 0:5c4d7b2438d3 239 nsapi_addr_t addr = {NSAPI_IPv4, 0};
switches 0:5c4d7b2438d3 240 uint16_t port = 0;
switches 0:5c4d7b2438d3 241
switches 0:5c4d7b2438d3 242 nsapi_size_or_error_t err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size);
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 if (address) {
switches 0:5c4d7b2438d3 245 address->set_addr(addr);
switches 0:5c4d7b2438d3 246 address->set_port(port);
switches 0:5c4d7b2438d3 247 }
switches 0:5c4d7b2438d3 248
switches 0:5c4d7b2438d3 249 return err;
switches 0:5c4d7b2438d3 250 }
switches 0:5c4d7b2438d3 251
switches 0:5c4d7b2438d3 252 virtual void socket_attach(nsapi_socket_t socket, void (*callback)(void *), void *data)
switches 0:5c4d7b2438d3 253 {
switches 0:5c4d7b2438d3 254 if (!_stack_api()->socket_attach) {
switches 0:5c4d7b2438d3 255 return;
switches 0:5c4d7b2438d3 256 }
switches 0:5c4d7b2438d3 257
switches 0:5c4d7b2438d3 258 return _stack_api()->socket_attach(_stack(), socket, callback, data);
switches 0:5c4d7b2438d3 259 }
switches 0:5c4d7b2438d3 260
switches 0:5c4d7b2438d3 261 virtual nsapi_error_t setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen)
switches 0:5c4d7b2438d3 262 {
switches 0:5c4d7b2438d3 263 if (!_stack_api()->setsockopt) {
switches 0:5c4d7b2438d3 264 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 265 }
switches 0:5c4d7b2438d3 266
switches 0:5c4d7b2438d3 267 return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen);
switches 0:5c4d7b2438d3 268 }
switches 0:5c4d7b2438d3 269
switches 0:5c4d7b2438d3 270 virtual nsapi_error_t getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen)
switches 0:5c4d7b2438d3 271 {
switches 0:5c4d7b2438d3 272 if (!_stack_api()->getsockopt) {
switches 0:5c4d7b2438d3 273 return NSAPI_ERROR_UNSUPPORTED;
switches 0:5c4d7b2438d3 274 }
switches 0:5c4d7b2438d3 275
switches 0:5c4d7b2438d3 276 return _stack_api()->getsockopt(_stack(), socket, level, optname, optval, optlen);
switches 0:5c4d7b2438d3 277 }
switches 0:5c4d7b2438d3 278 };
switches 0:5c4d7b2438d3 279
switches 0:5c4d7b2438d3 280
switches 0:5c4d7b2438d3 281 // Conversion function for network stacks
switches 0:5c4d7b2438d3 282 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack)
switches 0:5c4d7b2438d3 283 {
switches 0:5c4d7b2438d3 284 MBED_STATIC_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper),
switches 0:5c4d7b2438d3 285 "The nsapi_stack_t stack buffer must fit a NetworkStackWrapper");
switches 0:5c4d7b2438d3 286 return new (stack->_stack_buffer) NetworkStackWrapper;
switches 0:5c4d7b2438d3 287 }
switches 0:5c4d7b2438d3 288
switches 0:5c4d7b2438d3 289 NetworkStack *nsapi_create_stack(NetworkStack *stack)
switches 0:5c4d7b2438d3 290 {
switches 0:5c4d7b2438d3 291 return stack;
switches 0:5c4d7b2438d3 292 }
switches 0:5c4d7b2438d3 293