Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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