Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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