RTC auf true
features/netsocket/NetworkStack.cpp@0:38ceb79fef03, 2018-11-28 (annotated)
- Committer:
- kevman
- Date:
- Wed Nov 28 15:10:15 2018 +0000
- Revision:
- 0:38ceb79fef03
RTC modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* Socket |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2015 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 5 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 6 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 7 | * |
kevman | 0:38ceb79fef03 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 9 | * |
kevman | 0:38ceb79fef03 | 10 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 13 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 14 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 15 | */ |
kevman | 0:38ceb79fef03 | 16 | |
kevman | 0:38ceb79fef03 | 17 | #include "NetworkStack.h" |
kevman | 0:38ceb79fef03 | 18 | #include "nsapi_dns.h" |
kevman | 0:38ceb79fef03 | 19 | #include "mbed.h" |
kevman | 0:38ceb79fef03 | 20 | #include "stddef.h" |
kevman | 0:38ceb79fef03 | 21 | #include <new> |
kevman | 0:38ceb79fef03 | 22 | |
kevman | 0:38ceb79fef03 | 23 | // Default NetworkStack operations |
kevman | 0:38ceb79fef03 | 24 | const char *NetworkStack::get_ip_address() |
kevman | 0:38ceb79fef03 | 25 | { |
kevman | 0:38ceb79fef03 | 26 | return 0; |
kevman | 0:38ceb79fef03 | 27 | |
kevman | 0:38ceb79fef03 | 28 | } |
kevman | 0:38ceb79fef03 | 29 | nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) |
kevman | 0:38ceb79fef03 | 30 | { |
kevman | 0:38ceb79fef03 | 31 | if (name[0] == '\0') { |
kevman | 0:38ceb79fef03 | 32 | return NSAPI_ERROR_PARAMETER; |
kevman | 0:38ceb79fef03 | 33 | } |
kevman | 0:38ceb79fef03 | 34 | |
kevman | 0:38ceb79fef03 | 35 | // check for simple ip addresses |
kevman | 0:38ceb79fef03 | 36 | if (address->set_ip_address(name)) { |
kevman | 0:38ceb79fef03 | 37 | if (version != NSAPI_UNSPEC && address->get_ip_version() != version) { |
kevman | 0:38ceb79fef03 | 38 | return NSAPI_ERROR_DNS_FAILURE; |
kevman | 0:38ceb79fef03 | 39 | } |
kevman | 0:38ceb79fef03 | 40 | |
kevman | 0:38ceb79fef03 | 41 | return NSAPI_ERROR_OK; |
kevman | 0:38ceb79fef03 | 42 | } |
kevman | 0:38ceb79fef03 | 43 | |
kevman | 0:38ceb79fef03 | 44 | // if the version is unspecified, try to guess the version from the |
kevman | 0:38ceb79fef03 | 45 | // ip address of the underlying stack |
kevman | 0:38ceb79fef03 | 46 | if (version == NSAPI_UNSPEC) { |
kevman | 0:38ceb79fef03 | 47 | SocketAddress testaddress; |
kevman | 0:38ceb79fef03 | 48 | if (testaddress.set_ip_address(this->get_ip_address())) { |
kevman | 0:38ceb79fef03 | 49 | version = testaddress.get_ip_version(); |
kevman | 0:38ceb79fef03 | 50 | } |
kevman | 0:38ceb79fef03 | 51 | } |
kevman | 0:38ceb79fef03 | 52 | |
kevman | 0:38ceb79fef03 | 53 | return nsapi_dns_query(this, name, address, version); |
kevman | 0:38ceb79fef03 | 54 | } |
kevman | 0:38ceb79fef03 | 55 | |
kevman | 0:38ceb79fef03 | 56 | nsapi_value_or_error_t NetworkStack::gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version) |
kevman | 0:38ceb79fef03 | 57 | { |
kevman | 0:38ceb79fef03 | 58 | SocketAddress address; |
kevman | 0:38ceb79fef03 | 59 | |
kevman | 0:38ceb79fef03 | 60 | if (name[0] == '\0') { |
kevman | 0:38ceb79fef03 | 61 | return NSAPI_ERROR_PARAMETER; |
kevman | 0:38ceb79fef03 | 62 | } |
kevman | 0:38ceb79fef03 | 63 | |
kevman | 0:38ceb79fef03 | 64 | // check for simple ip addresses |
kevman | 0:38ceb79fef03 | 65 | if (address.set_ip_address(name)) { |
kevman | 0:38ceb79fef03 | 66 | if (version != NSAPI_UNSPEC && address.get_ip_version() != version) { |
kevman | 0:38ceb79fef03 | 67 | return NSAPI_ERROR_DNS_FAILURE; |
kevman | 0:38ceb79fef03 | 68 | } |
kevman | 0:38ceb79fef03 | 69 | |
kevman | 0:38ceb79fef03 | 70 | callback(NSAPI_ERROR_OK, &address); |
kevman | 0:38ceb79fef03 | 71 | return NSAPI_ERROR_OK; |
kevman | 0:38ceb79fef03 | 72 | } |
kevman | 0:38ceb79fef03 | 73 | |
kevman | 0:38ceb79fef03 | 74 | // if the version is unspecified, try to guess the version from the |
kevman | 0:38ceb79fef03 | 75 | // ip address of the underlying stack |
kevman | 0:38ceb79fef03 | 76 | if (version == NSAPI_UNSPEC) { |
kevman | 0:38ceb79fef03 | 77 | SocketAddress testaddress; |
kevman | 0:38ceb79fef03 | 78 | if (testaddress.set_ip_address(this->get_ip_address())) { |
kevman | 0:38ceb79fef03 | 79 | version = testaddress.get_ip_version(); |
kevman | 0:38ceb79fef03 | 80 | } |
kevman | 0:38ceb79fef03 | 81 | } |
kevman | 0:38ceb79fef03 | 82 | |
kevman | 0:38ceb79fef03 | 83 | call_in_callback_cb_t call_in_cb = get_call_in_callback(); |
kevman | 0:38ceb79fef03 | 84 | |
kevman | 0:38ceb79fef03 | 85 | return nsapi_dns_query_async(this, name, callback, call_in_cb, version); |
kevman | 0:38ceb79fef03 | 86 | } |
kevman | 0:38ceb79fef03 | 87 | |
kevman | 0:38ceb79fef03 | 88 | nsapi_error_t NetworkStack::gethostbyname_async_cancel(int id) |
kevman | 0:38ceb79fef03 | 89 | { |
kevman | 0:38ceb79fef03 | 90 | return nsapi_dns_query_async_cancel(id); |
kevman | 0:38ceb79fef03 | 91 | } |
kevman | 0:38ceb79fef03 | 92 | |
kevman | 0:38ceb79fef03 | 93 | nsapi_error_t NetworkStack::add_dns_server(const SocketAddress &address) |
kevman | 0:38ceb79fef03 | 94 | { |
kevman | 0:38ceb79fef03 | 95 | return nsapi_dns_add_server(address); |
kevman | 0:38ceb79fef03 | 96 | } |
kevman | 0:38ceb79fef03 | 97 | |
kevman | 0:38ceb79fef03 | 98 | nsapi_error_t NetworkStack::get_dns_server(int index, SocketAddress *address) |
kevman | 0:38ceb79fef03 | 99 | { |
kevman | 0:38ceb79fef03 | 100 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 101 | } |
kevman | 0:38ceb79fef03 | 102 | |
kevman | 0:38ceb79fef03 | 103 | nsapi_error_t NetworkStack::setstackopt(int level, int optname, const void *optval, unsigned optlen) |
kevman | 0:38ceb79fef03 | 104 | { |
kevman | 0:38ceb79fef03 | 105 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 106 | } |
kevman | 0:38ceb79fef03 | 107 | |
kevman | 0:38ceb79fef03 | 108 | nsapi_error_t NetworkStack::getstackopt(int level, int optname, void *optval, unsigned *optlen) |
kevman | 0:38ceb79fef03 | 109 | { |
kevman | 0:38ceb79fef03 | 110 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 111 | } |
kevman | 0:38ceb79fef03 | 112 | |
kevman | 0:38ceb79fef03 | 113 | nsapi_error_t NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) |
kevman | 0:38ceb79fef03 | 114 | { |
kevman | 0:38ceb79fef03 | 115 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 116 | } |
kevman | 0:38ceb79fef03 | 117 | |
kevman | 0:38ceb79fef03 | 118 | nsapi_error_t NetworkStack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) |
kevman | 0:38ceb79fef03 | 119 | { |
kevman | 0:38ceb79fef03 | 120 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 121 | } |
kevman | 0:38ceb79fef03 | 122 | |
kevman | 0:38ceb79fef03 | 123 | nsapi_error_t NetworkStack::call_in(int delay, mbed::Callback<void()> func) |
kevman | 0:38ceb79fef03 | 124 | { |
kevman | 0:38ceb79fef03 | 125 | static events::EventQueue *event_queue = mbed::mbed_event_queue(); |
kevman | 0:38ceb79fef03 | 126 | |
kevman | 0:38ceb79fef03 | 127 | if (!event_queue) { |
kevman | 0:38ceb79fef03 | 128 | return NSAPI_ERROR_NO_MEMORY; |
kevman | 0:38ceb79fef03 | 129 | } |
kevman | 0:38ceb79fef03 | 130 | |
kevman | 0:38ceb79fef03 | 131 | if (delay > 0) { |
kevman | 0:38ceb79fef03 | 132 | if (event_queue->call_in(delay, func) == 0) { |
kevman | 0:38ceb79fef03 | 133 | return NSAPI_ERROR_NO_MEMORY; |
kevman | 0:38ceb79fef03 | 134 | } |
kevman | 0:38ceb79fef03 | 135 | } else { |
kevman | 0:38ceb79fef03 | 136 | if (event_queue->call(func) == 0) { |
kevman | 0:38ceb79fef03 | 137 | return NSAPI_ERROR_NO_MEMORY; |
kevman | 0:38ceb79fef03 | 138 | } |
kevman | 0:38ceb79fef03 | 139 | } |
kevman | 0:38ceb79fef03 | 140 | |
kevman | 0:38ceb79fef03 | 141 | return NSAPI_ERROR_OK; |
kevman | 0:38ceb79fef03 | 142 | } |
kevman | 0:38ceb79fef03 | 143 | |
kevman | 0:38ceb79fef03 | 144 | call_in_callback_cb_t NetworkStack::get_call_in_callback() |
kevman | 0:38ceb79fef03 | 145 | { |
kevman | 0:38ceb79fef03 | 146 | call_in_callback_cb_t cb(this, &NetworkStack::call_in); |
kevman | 0:38ceb79fef03 | 147 | return cb; |
kevman | 0:38ceb79fef03 | 148 | } |
kevman | 0:38ceb79fef03 | 149 | |
kevman | 0:38ceb79fef03 | 150 | // NetworkStackWrapper class for encapsulating the raw nsapi_stack structure |
kevman | 0:38ceb79fef03 | 151 | class NetworkStackWrapper : public NetworkStack { |
kevman | 0:38ceb79fef03 | 152 | private: |
kevman | 0:38ceb79fef03 | 153 | inline nsapi_stack_t *_stack() |
kevman | 0:38ceb79fef03 | 154 | { |
kevman | 0:38ceb79fef03 | 155 | return reinterpret_cast<nsapi_stack_t *>( |
kevman | 0:38ceb79fef03 | 156 | reinterpret_cast<uint8_t *>(this) |
kevman | 0:38ceb79fef03 | 157 | - offsetof(nsapi_stack_t, _stack_buffer)); |
kevman | 0:38ceb79fef03 | 158 | } |
kevman | 0:38ceb79fef03 | 159 | |
kevman | 0:38ceb79fef03 | 160 | inline const nsapi_stack_api_t *_stack_api() |
kevman | 0:38ceb79fef03 | 161 | { |
kevman | 0:38ceb79fef03 | 162 | return _stack()->stack_api; |
kevman | 0:38ceb79fef03 | 163 | } |
kevman | 0:38ceb79fef03 | 164 | |
kevman | 0:38ceb79fef03 | 165 | public: |
kevman | 0:38ceb79fef03 | 166 | virtual const char *get_ip_address() |
kevman | 0:38ceb79fef03 | 167 | { |
kevman | 0:38ceb79fef03 | 168 | if (!_stack_api()->get_ip_address) { |
kevman | 0:38ceb79fef03 | 169 | return 0; |
kevman | 0:38ceb79fef03 | 170 | } |
kevman | 0:38ceb79fef03 | 171 | |
kevman | 0:38ceb79fef03 | 172 | static uint8_t buffer[sizeof(SocketAddress)]; |
kevman | 0:38ceb79fef03 | 173 | SocketAddress *address = new (buffer) SocketAddress(_stack_api()->get_ip_address(_stack())); |
kevman | 0:38ceb79fef03 | 174 | return address->get_ip_address(); |
kevman | 0:38ceb79fef03 | 175 | } |
kevman | 0:38ceb79fef03 | 176 | |
kevman | 0:38ceb79fef03 | 177 | virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) |
kevman | 0:38ceb79fef03 | 178 | { |
kevman | 0:38ceb79fef03 | 179 | if (!_stack_api()->gethostbyname) { |
kevman | 0:38ceb79fef03 | 180 | return NetworkStack::gethostbyname(name, address, version); |
kevman | 0:38ceb79fef03 | 181 | } |
kevman | 0:38ceb79fef03 | 182 | |
kevman | 0:38ceb79fef03 | 183 | nsapi_addr_t addr = {NSAPI_UNSPEC, 0}; |
kevman | 0:38ceb79fef03 | 184 | nsapi_error_t err = _stack_api()->gethostbyname(_stack(), name, &addr, version); |
kevman | 0:38ceb79fef03 | 185 | address->set_addr(addr); |
kevman | 0:38ceb79fef03 | 186 | return err; |
kevman | 0:38ceb79fef03 | 187 | } |
kevman | 0:38ceb79fef03 | 188 | |
kevman | 0:38ceb79fef03 | 189 | virtual nsapi_error_t add_dns_server(const SocketAddress &address) |
kevman | 0:38ceb79fef03 | 190 | { |
kevman | 0:38ceb79fef03 | 191 | if (!_stack_api()->add_dns_server) { |
kevman | 0:38ceb79fef03 | 192 | return NetworkStack::add_dns_server(address); |
kevman | 0:38ceb79fef03 | 193 | } |
kevman | 0:38ceb79fef03 | 194 | |
kevman | 0:38ceb79fef03 | 195 | return _stack_api()->add_dns_server(_stack(), address.get_addr()); |
kevman | 0:38ceb79fef03 | 196 | } |
kevman | 0:38ceb79fef03 | 197 | |
kevman | 0:38ceb79fef03 | 198 | virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen) |
kevman | 0:38ceb79fef03 | 199 | { |
kevman | 0:38ceb79fef03 | 200 | if (!_stack_api()->setstackopt) { |
kevman | 0:38ceb79fef03 | 201 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 202 | } |
kevman | 0:38ceb79fef03 | 203 | |
kevman | 0:38ceb79fef03 | 204 | return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen); |
kevman | 0:38ceb79fef03 | 205 | } |
kevman | 0:38ceb79fef03 | 206 | |
kevman | 0:38ceb79fef03 | 207 | virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen) |
kevman | 0:38ceb79fef03 | 208 | { |
kevman | 0:38ceb79fef03 | 209 | if (!_stack_api()->getstackopt) { |
kevman | 0:38ceb79fef03 | 210 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 211 | } |
kevman | 0:38ceb79fef03 | 212 | |
kevman | 0:38ceb79fef03 | 213 | return _stack_api()->getstackopt(_stack(), level, optname, optval, optlen); |
kevman | 0:38ceb79fef03 | 214 | } |
kevman | 0:38ceb79fef03 | 215 | |
kevman | 0:38ceb79fef03 | 216 | protected: |
kevman | 0:38ceb79fef03 | 217 | virtual nsapi_error_t socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto) |
kevman | 0:38ceb79fef03 | 218 | { |
kevman | 0:38ceb79fef03 | 219 | if (!_stack_api()->socket_open) { |
kevman | 0:38ceb79fef03 | 220 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 221 | } |
kevman | 0:38ceb79fef03 | 222 | |
kevman | 0:38ceb79fef03 | 223 | return _stack_api()->socket_open(_stack(), socket, proto); |
kevman | 0:38ceb79fef03 | 224 | } |
kevman | 0:38ceb79fef03 | 225 | |
kevman | 0:38ceb79fef03 | 226 | virtual nsapi_error_t socket_close(nsapi_socket_t socket) |
kevman | 0:38ceb79fef03 | 227 | { |
kevman | 0:38ceb79fef03 | 228 | if (!_stack_api()->socket_close) { |
kevman | 0:38ceb79fef03 | 229 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 230 | } |
kevman | 0:38ceb79fef03 | 231 | |
kevman | 0:38ceb79fef03 | 232 | return _stack_api()->socket_close(_stack(), socket); |
kevman | 0:38ceb79fef03 | 233 | } |
kevman | 0:38ceb79fef03 | 234 | |
kevman | 0:38ceb79fef03 | 235 | virtual nsapi_error_t socket_bind(nsapi_socket_t socket, const SocketAddress &address) |
kevman | 0:38ceb79fef03 | 236 | { |
kevman | 0:38ceb79fef03 | 237 | if (!_stack_api()->socket_bind) { |
kevman | 0:38ceb79fef03 | 238 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 239 | } |
kevman | 0:38ceb79fef03 | 240 | |
kevman | 0:38ceb79fef03 | 241 | return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port()); |
kevman | 0:38ceb79fef03 | 242 | } |
kevman | 0:38ceb79fef03 | 243 | |
kevman | 0:38ceb79fef03 | 244 | virtual nsapi_error_t socket_listen(nsapi_socket_t socket, int backlog) |
kevman | 0:38ceb79fef03 | 245 | { |
kevman | 0:38ceb79fef03 | 246 | if (!_stack_api()->socket_listen) { |
kevman | 0:38ceb79fef03 | 247 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 248 | } |
kevman | 0:38ceb79fef03 | 249 | |
kevman | 0:38ceb79fef03 | 250 | return _stack_api()->socket_listen(_stack(), socket, backlog); |
kevman | 0:38ceb79fef03 | 251 | } |
kevman | 0:38ceb79fef03 | 252 | |
kevman | 0:38ceb79fef03 | 253 | virtual nsapi_error_t socket_connect(nsapi_socket_t socket, const SocketAddress &address) |
kevman | 0:38ceb79fef03 | 254 | { |
kevman | 0:38ceb79fef03 | 255 | if (!_stack_api()->socket_connect) { |
kevman | 0:38ceb79fef03 | 256 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 257 | } |
kevman | 0:38ceb79fef03 | 258 | |
kevman | 0:38ceb79fef03 | 259 | return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port()); |
kevman | 0:38ceb79fef03 | 260 | } |
kevman | 0:38ceb79fef03 | 261 | |
kevman | 0:38ceb79fef03 | 262 | virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address) |
kevman | 0:38ceb79fef03 | 263 | { |
kevman | 0:38ceb79fef03 | 264 | if (!_stack_api()->socket_accept) { |
kevman | 0:38ceb79fef03 | 265 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 266 | } |
kevman | 0:38ceb79fef03 | 267 | |
kevman | 0:38ceb79fef03 | 268 | nsapi_addr_t addr = {NSAPI_IPv4, 0}; |
kevman | 0:38ceb79fef03 | 269 | uint16_t port = 0; |
kevman | 0:38ceb79fef03 | 270 | |
kevman | 0:38ceb79fef03 | 271 | nsapi_error_t err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port); |
kevman | 0:38ceb79fef03 | 272 | |
kevman | 0:38ceb79fef03 | 273 | if (address) { |
kevman | 0:38ceb79fef03 | 274 | address->set_addr(addr); |
kevman | 0:38ceb79fef03 | 275 | address->set_port(port); |
kevman | 0:38ceb79fef03 | 276 | } |
kevman | 0:38ceb79fef03 | 277 | |
kevman | 0:38ceb79fef03 | 278 | return err; |
kevman | 0:38ceb79fef03 | 279 | } |
kevman | 0:38ceb79fef03 | 280 | |
kevman | 0:38ceb79fef03 | 281 | virtual nsapi_size_or_error_t socket_send(nsapi_socket_t socket, const void *data, nsapi_size_t size) |
kevman | 0:38ceb79fef03 | 282 | { |
kevman | 0:38ceb79fef03 | 283 | if (!_stack_api()->socket_send) { |
kevman | 0:38ceb79fef03 | 284 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 285 | } |
kevman | 0:38ceb79fef03 | 286 | |
kevman | 0:38ceb79fef03 | 287 | return _stack_api()->socket_send(_stack(), socket, data, size); |
kevman | 0:38ceb79fef03 | 288 | } |
kevman | 0:38ceb79fef03 | 289 | |
kevman | 0:38ceb79fef03 | 290 | virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t socket, void *data, nsapi_size_t size) |
kevman | 0:38ceb79fef03 | 291 | { |
kevman | 0:38ceb79fef03 | 292 | if (!_stack_api()->socket_recv) { |
kevman | 0:38ceb79fef03 | 293 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 294 | } |
kevman | 0:38ceb79fef03 | 295 | |
kevman | 0:38ceb79fef03 | 296 | return _stack_api()->socket_recv(_stack(), socket, data, size); |
kevman | 0:38ceb79fef03 | 297 | } |
kevman | 0:38ceb79fef03 | 298 | |
kevman | 0:38ceb79fef03 | 299 | virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, nsapi_size_t size) |
kevman | 0:38ceb79fef03 | 300 | { |
kevman | 0:38ceb79fef03 | 301 | if (!_stack_api()->socket_sendto) { |
kevman | 0:38ceb79fef03 | 302 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 303 | } |
kevman | 0:38ceb79fef03 | 304 | |
kevman | 0:38ceb79fef03 | 305 | return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size); |
kevman | 0:38ceb79fef03 | 306 | } |
kevman | 0:38ceb79fef03 | 307 | |
kevman | 0:38ceb79fef03 | 308 | virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, nsapi_size_t size) |
kevman | 0:38ceb79fef03 | 309 | { |
kevman | 0:38ceb79fef03 | 310 | if (!_stack_api()->socket_recvfrom) { |
kevman | 0:38ceb79fef03 | 311 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 312 | } |
kevman | 0:38ceb79fef03 | 313 | |
kevman | 0:38ceb79fef03 | 314 | nsapi_addr_t addr = {NSAPI_IPv4, 0}; |
kevman | 0:38ceb79fef03 | 315 | uint16_t port = 0; |
kevman | 0:38ceb79fef03 | 316 | |
kevman | 0:38ceb79fef03 | 317 | nsapi_size_or_error_t err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size); |
kevman | 0:38ceb79fef03 | 318 | |
kevman | 0:38ceb79fef03 | 319 | if (address) { |
kevman | 0:38ceb79fef03 | 320 | address->set_addr(addr); |
kevman | 0:38ceb79fef03 | 321 | address->set_port(port); |
kevman | 0:38ceb79fef03 | 322 | } |
kevman | 0:38ceb79fef03 | 323 | |
kevman | 0:38ceb79fef03 | 324 | return err; |
kevman | 0:38ceb79fef03 | 325 | } |
kevman | 0:38ceb79fef03 | 326 | |
kevman | 0:38ceb79fef03 | 327 | virtual void socket_attach(nsapi_socket_t socket, void (*callback)(void *), void *data) |
kevman | 0:38ceb79fef03 | 328 | { |
kevman | 0:38ceb79fef03 | 329 | if (!_stack_api()->socket_attach) { |
kevman | 0:38ceb79fef03 | 330 | return; |
kevman | 0:38ceb79fef03 | 331 | } |
kevman | 0:38ceb79fef03 | 332 | |
kevman | 0:38ceb79fef03 | 333 | return _stack_api()->socket_attach(_stack(), socket, callback, data); |
kevman | 0:38ceb79fef03 | 334 | } |
kevman | 0:38ceb79fef03 | 335 | |
kevman | 0:38ceb79fef03 | 336 | virtual nsapi_error_t setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen) |
kevman | 0:38ceb79fef03 | 337 | { |
kevman | 0:38ceb79fef03 | 338 | if (!_stack_api()->setsockopt) { |
kevman | 0:38ceb79fef03 | 339 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 340 | } |
kevman | 0:38ceb79fef03 | 341 | |
kevman | 0:38ceb79fef03 | 342 | return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen); |
kevman | 0:38ceb79fef03 | 343 | } |
kevman | 0:38ceb79fef03 | 344 | |
kevman | 0:38ceb79fef03 | 345 | virtual nsapi_error_t getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen) |
kevman | 0:38ceb79fef03 | 346 | { |
kevman | 0:38ceb79fef03 | 347 | if (!_stack_api()->getsockopt) { |
kevman | 0:38ceb79fef03 | 348 | return NSAPI_ERROR_UNSUPPORTED; |
kevman | 0:38ceb79fef03 | 349 | } |
kevman | 0:38ceb79fef03 | 350 | |
kevman | 0:38ceb79fef03 | 351 | return _stack_api()->getsockopt(_stack(), socket, level, optname, optval, optlen); |
kevman | 0:38ceb79fef03 | 352 | } |
kevman | 0:38ceb79fef03 | 353 | }; |
kevman | 0:38ceb79fef03 | 354 | |
kevman | 0:38ceb79fef03 | 355 | |
kevman | 0:38ceb79fef03 | 356 | // Conversion function for network stacks |
kevman | 0:38ceb79fef03 | 357 | NetworkStack *nsapi_create_stack(nsapi_stack_t *stack) |
kevman | 0:38ceb79fef03 | 358 | { |
kevman | 0:38ceb79fef03 | 359 | MBED_STATIC_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper), |
kevman | 0:38ceb79fef03 | 360 | "The nsapi_stack_t stack buffer must fit a NetworkStackWrapper"); |
kevman | 0:38ceb79fef03 | 361 | return new (stack->_stack_buffer) NetworkStackWrapper; |
kevman | 0:38ceb79fef03 | 362 | } |
kevman | 0:38ceb79fef03 | 363 | |
kevman | 0:38ceb79fef03 | 364 | NetworkStack *nsapi_create_stack(NetworkStack *stack) |
kevman | 0:38ceb79fef03 | 365 | { |
kevman | 0:38ceb79fef03 | 366 | return stack; |
kevman | 0:38ceb79fef03 | 367 | } |
kevman | 0:38ceb79fef03 | 368 |