Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NetworkStack.cpp
00001 /* Socket 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "NetworkStack.h" 00018 #include "nsapi_dns.h" 00019 #include "mbed.h" 00020 #include "stddef.h" 00021 #include <new> 00022 00023 // Default NetworkStack operations 00024 const char *NetworkStack::get_ip_address() 00025 { 00026 return 0; 00027 00028 } 00029 nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) 00030 { 00031 if (name[0] == '\0') { 00032 return NSAPI_ERROR_PARAMETER ; 00033 } 00034 00035 // check for simple ip addresses 00036 if (address->set_ip_address(name)) { 00037 if (version != NSAPI_UNSPEC && address->get_ip_version() != version) { 00038 return NSAPI_ERROR_DNS_FAILURE ; 00039 } 00040 00041 return NSAPI_ERROR_OK ; 00042 } 00043 00044 // if the version is unspecified, try to guess the version from the 00045 // ip address of the underlying stack 00046 if (version == NSAPI_UNSPEC ) { 00047 SocketAddress testaddress; 00048 if (testaddress.set_ip_address(this->get_ip_address())) { 00049 version = testaddress.get_ip_version(); 00050 } 00051 } 00052 00053 return nsapi_dns_query(this, name, address, version); 00054 } 00055 00056 nsapi_value_or_error_t NetworkStack::gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version) 00057 { 00058 SocketAddress address; 00059 00060 if (name[0] == '\0') { 00061 return NSAPI_ERROR_PARAMETER ; 00062 } 00063 00064 // check for simple ip addresses 00065 if (address.set_ip_address(name)) { 00066 if (version != NSAPI_UNSPEC && address.get_ip_version() != version) { 00067 return NSAPI_ERROR_DNS_FAILURE ; 00068 } 00069 00070 callback(NSAPI_ERROR_OK , &address); 00071 return NSAPI_ERROR_OK ; 00072 } 00073 00074 // if the version is unspecified, try to guess the version from the 00075 // ip address of the underlying stack 00076 if (version == NSAPI_UNSPEC ) { 00077 SocketAddress testaddress; 00078 if (testaddress.set_ip_address(this->get_ip_address())) { 00079 version = testaddress.get_ip_version(); 00080 } 00081 } 00082 00083 call_in_callback_cb_t call_in_cb = get_call_in_callback(); 00084 00085 return nsapi_dns_query_async(this, name, callback, call_in_cb, version); 00086 } 00087 00088 nsapi_error_t NetworkStack::gethostbyname_async_cancel(int id) 00089 { 00090 return nsapi_dns_query_async_cancel(id); 00091 } 00092 00093 nsapi_error_t NetworkStack::add_dns_server(const SocketAddress &address) 00094 { 00095 return nsapi_dns_add_server(address); 00096 } 00097 00098 nsapi_error_t NetworkStack::get_dns_server(int index, SocketAddress *address) 00099 { 00100 return NSAPI_ERROR_UNSUPPORTED ; 00101 } 00102 00103 nsapi_error_t NetworkStack::setstackopt(int level, int optname, const void *optval, unsigned optlen) 00104 { 00105 return NSAPI_ERROR_UNSUPPORTED ; 00106 } 00107 00108 nsapi_error_t NetworkStack::getstackopt(int level, int optname, void *optval, unsigned *optlen) 00109 { 00110 return NSAPI_ERROR_UNSUPPORTED ; 00111 } 00112 00113 nsapi_error_t NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) 00114 { 00115 return NSAPI_ERROR_UNSUPPORTED ; 00116 } 00117 00118 nsapi_error_t NetworkStack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) 00119 { 00120 return NSAPI_ERROR_UNSUPPORTED ; 00121 } 00122 00123 nsapi_error_t NetworkStack::call_in(int delay, mbed::Callback<void()> func) 00124 { 00125 static events::EventQueue *event_queue = mbed::mbed_event_queue(); 00126 00127 if (!event_queue) { 00128 return NSAPI_ERROR_NO_MEMORY ; 00129 } 00130 00131 if (delay > 0) { 00132 if (event_queue->call_in(delay, func) == 0) { 00133 return NSAPI_ERROR_NO_MEMORY ; 00134 } 00135 } else { 00136 if (event_queue->call(func) == 0) { 00137 return NSAPI_ERROR_NO_MEMORY ; 00138 } 00139 } 00140 00141 return NSAPI_ERROR_OK ; 00142 } 00143 00144 call_in_callback_cb_t NetworkStack::get_call_in_callback() 00145 { 00146 call_in_callback_cb_t cb(this, &NetworkStack::call_in); 00147 return cb; 00148 } 00149 00150 // NetworkStackWrapper class for encapsulating the raw nsapi_stack structure 00151 class NetworkStackWrapper : public NetworkStack 00152 { 00153 private: 00154 inline nsapi_stack_t *_stack() 00155 { 00156 return reinterpret_cast<nsapi_stack_t *>( 00157 reinterpret_cast<uint8_t *>(this) 00158 - offsetof(nsapi_stack_t, _stack_buffer)); 00159 } 00160 00161 inline const nsapi_stack_api_t *_stack_api() 00162 { 00163 return _stack()->stack_api; 00164 } 00165 00166 public: 00167 virtual const char *get_ip_address() 00168 { 00169 if (!_stack_api()->get_ip_address) { 00170 return 0; 00171 } 00172 00173 static uint8_t buffer[sizeof(SocketAddress)]; 00174 SocketAddress *address = new (buffer) SocketAddress(_stack_api()->get_ip_address(_stack())); 00175 return address->get_ip_address(); 00176 } 00177 00178 virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) 00179 { 00180 if (!_stack_api()->gethostbyname) { 00181 return NetworkStack::gethostbyname(name, address, version); 00182 } 00183 00184 nsapi_addr_t addr = {NSAPI_UNSPEC , 0}; 00185 nsapi_error_t err = _stack_api()->gethostbyname(_stack(), name, &addr, version); 00186 address->set_addr(addr); 00187 return err; 00188 } 00189 00190 virtual nsapi_error_t add_dns_server(const SocketAddress &address) 00191 { 00192 if (!_stack_api()->add_dns_server) { 00193 return NetworkStack::add_dns_server(address); 00194 } 00195 00196 return _stack_api()->add_dns_server(_stack(), address.get_addr()); 00197 } 00198 00199 virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen) 00200 { 00201 if (!_stack_api()->setstackopt) { 00202 return NSAPI_ERROR_UNSUPPORTED ; 00203 } 00204 00205 return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen); 00206 } 00207 00208 virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen) 00209 { 00210 if (!_stack_api()->getstackopt) { 00211 return NSAPI_ERROR_UNSUPPORTED ; 00212 } 00213 00214 return _stack_api()->getstackopt(_stack(), level, optname, optval, optlen); 00215 } 00216 00217 protected: 00218 virtual nsapi_error_t socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto) 00219 { 00220 if (!_stack_api()->socket_open) { 00221 return NSAPI_ERROR_UNSUPPORTED ; 00222 } 00223 00224 return _stack_api()->socket_open(_stack(), socket, proto); 00225 } 00226 00227 virtual nsapi_error_t socket_close(nsapi_socket_t socket) 00228 { 00229 if (!_stack_api()->socket_close) { 00230 return NSAPI_ERROR_UNSUPPORTED ; 00231 } 00232 00233 return _stack_api()->socket_close(_stack(), socket); 00234 } 00235 00236 virtual nsapi_error_t socket_bind(nsapi_socket_t socket, const SocketAddress &address) 00237 { 00238 if (!_stack_api()->socket_bind) { 00239 return NSAPI_ERROR_UNSUPPORTED ; 00240 } 00241 00242 return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port()); 00243 } 00244 00245 virtual nsapi_error_t socket_listen(nsapi_socket_t socket, int backlog) 00246 { 00247 if (!_stack_api()->socket_listen) { 00248 return NSAPI_ERROR_UNSUPPORTED ; 00249 } 00250 00251 return _stack_api()->socket_listen(_stack(), socket, backlog); 00252 } 00253 00254 virtual nsapi_error_t socket_connect(nsapi_socket_t socket, const SocketAddress &address) 00255 { 00256 if (!_stack_api()->socket_connect) { 00257 return NSAPI_ERROR_UNSUPPORTED ; 00258 } 00259 00260 return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port()); 00261 } 00262 00263 virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address) 00264 { 00265 if (!_stack_api()->socket_accept) { 00266 return NSAPI_ERROR_UNSUPPORTED ; 00267 } 00268 00269 nsapi_addr_t addr = {NSAPI_IPv4 , 0}; 00270 uint16_t port = 0; 00271 00272 nsapi_error_t err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port); 00273 00274 if (address) { 00275 address->set_addr(addr); 00276 address->set_port(port); 00277 } 00278 00279 return err; 00280 } 00281 00282 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t socket, const void *data, nsapi_size_t size) 00283 { 00284 if (!_stack_api()->socket_send) { 00285 return NSAPI_ERROR_UNSUPPORTED ; 00286 } 00287 00288 return _stack_api()->socket_send(_stack(), socket, data, size); 00289 } 00290 00291 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t socket, void *data, nsapi_size_t size) 00292 { 00293 if (!_stack_api()->socket_recv) { 00294 return NSAPI_ERROR_UNSUPPORTED ; 00295 } 00296 00297 return _stack_api()->socket_recv(_stack(), socket, data, size); 00298 } 00299 00300 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, nsapi_size_t size) 00301 { 00302 if (!_stack_api()->socket_sendto) { 00303 return NSAPI_ERROR_UNSUPPORTED ; 00304 } 00305 00306 return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size); 00307 } 00308 00309 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, nsapi_size_t size) 00310 { 00311 if (!_stack_api()->socket_recvfrom) { 00312 return NSAPI_ERROR_UNSUPPORTED ; 00313 } 00314 00315 nsapi_addr_t addr = {NSAPI_IPv4 , 0}; 00316 uint16_t port = 0; 00317 00318 nsapi_size_or_error_t err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size); 00319 00320 if (address) { 00321 address->set_addr(addr); 00322 address->set_port(port); 00323 } 00324 00325 return err; 00326 } 00327 00328 virtual void socket_attach(nsapi_socket_t socket, void (*callback)(void *), void *data) 00329 { 00330 if (!_stack_api()->socket_attach) { 00331 return; 00332 } 00333 00334 return _stack_api()->socket_attach(_stack(), socket, callback, data); 00335 } 00336 00337 virtual nsapi_error_t setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen) 00338 { 00339 if (!_stack_api()->setsockopt) { 00340 return NSAPI_ERROR_UNSUPPORTED ; 00341 } 00342 00343 return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen); 00344 } 00345 00346 virtual nsapi_error_t getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen) 00347 { 00348 if (!_stack_api()->getsockopt) { 00349 return NSAPI_ERROR_UNSUPPORTED ; 00350 } 00351 00352 return _stack_api()->getsockopt(_stack(), socket, level, optname, optval, optlen); 00353 } 00354 }; 00355 00356 00357 // Conversion function for network stacks 00358 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack) 00359 { 00360 MBED_STATIC_ASSERT(sizeof stack->_stack_buffer >= sizeof(NetworkStackWrapper), 00361 "The nsapi_stack_t stack buffer must fit a NetworkStackWrapper"); 00362 return new (stack->_stack_buffer) NetworkStackWrapper; 00363 } 00364 00365 NetworkStack *nsapi_create_stack(NetworkStack *stack) 00366 { 00367 return stack; 00368 } 00369
Generated on Tue Jul 12 2022 12:45:36 by
