Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NetworkInterface.cpp Source File

NetworkInterface.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 "ns_list.h"
00018 #include "netsocket/NetworkInterface.h"
00019 #include "netsocket/NetworkStack.h"
00020 #include "platform/Callback.h"
00021 #include "platform/mbed_error.h"
00022 #include <string.h>
00023 
00024 // Default network-interface state
00025 void NetworkInterface::set_as_default()
00026 {
00027 
00028 }
00029 
00030 const char *NetworkInterface::get_mac_address()
00031 {
00032     return 0;
00033 }
00034 
00035 nsapi_error_t NetworkInterface::get_ip_address(SocketAddress *)
00036 {
00037     return NSAPI_ERROR_UNSUPPORTED ;
00038 }
00039 
00040 const char *NetworkInterface::get_ip_address()
00041 {
00042     return nullptr;
00043 }
00044 
00045 nsapi_error_t NetworkInterface::get_ipv6_link_local_address(SocketAddress *address)
00046 {
00047     return NSAPI_ERROR_UNSUPPORTED ;
00048 }
00049 
00050 nsapi_error_t NetworkInterface::get_netmask(SocketAddress *)
00051 {
00052     return NSAPI_ERROR_UNSUPPORTED ;
00053 }
00054 
00055 const char *NetworkInterface::get_netmask()
00056 {
00057     return nullptr;
00058 }
00059 
00060 nsapi_error_t NetworkInterface::get_gateway(SocketAddress *)
00061 {
00062     return NSAPI_ERROR_UNSUPPORTED ;
00063 }
00064 
00065 const char *NetworkInterface::get_gateway()
00066 {
00067     return nullptr;
00068 }
00069 
00070 char *NetworkInterface::get_interface_name(char *interface_name)
00071 {
00072     return 0;
00073 }
00074 
00075 nsapi_error_t NetworkInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00076 {
00077     return NSAPI_ERROR_UNSUPPORTED ;
00078 }
00079 
00080 nsapi_error_t NetworkInterface::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
00081 {
00082     return NSAPI_ERROR_UNSUPPORTED ;
00083 }
00084 
00085 nsapi_error_t NetworkInterface::set_dhcp(bool dhcp)
00086 {
00087     if (!dhcp) {
00088         return NSAPI_ERROR_UNSUPPORTED ;
00089     } else {
00090         return NSAPI_ERROR_OK ;
00091     }
00092 }
00093 
00094 nsapi_error_t NetworkInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name)
00095 {
00096     return get_stack()->gethostbyname(name, address, version, interface_name);
00097 }
00098 
00099 nsapi_value_or_error_t NetworkInterface::gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
00100 {
00101     return get_stack()->gethostbyname_async(host, callback, version, interface_name);
00102 }
00103 
00104 nsapi_error_t NetworkInterface::gethostbyname_async_cancel(int id)
00105 {
00106     return get_stack()->gethostbyname_async_cancel(id);
00107 }
00108 
00109 nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address, const char *interface_name)
00110 {
00111     return get_stack()->add_dns_server(address, interface_name);
00112 }
00113 
00114 void NetworkInterface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00115 {
00116     // Dummy, that needs to be overwritten when inherited, but cannot be removed
00117     // because suplied previously and can be referred from binaries.
00118 }
00119 
00120 typedef struct iface_eventlist_entry {
00121     NetworkInterface *iface;
00122     mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb;
00123     ns_list_link_t link;
00124 } iface_eventlist_entry_t;
00125 
00126 typedef NS_LIST_HEAD (iface_eventlist_entry_t, link) iface_eventlist_t;
00127 
00128 static iface_eventlist_t *get_interface_event_list_head()
00129 {
00130     static iface_eventlist_t NS_LIST_NAME_INIT(event_list);
00131     return &event_list;
00132 }
00133 
00134 static void call_all_event_listeners(NetworkInterface *iface, nsapi_event_t event, intptr_t val)
00135 {
00136     iface_eventlist_t *event_list = get_interface_event_list_head();
00137     ns_list_foreach(iface_eventlist_entry_t, entry, event_list) {
00138         if (entry->iface == iface) {
00139             entry->status_cb(event, val);
00140         }
00141     }
00142 }
00143 
00144 void NetworkInterface::add_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00145 {
00146     iface_eventlist_t *event_list = get_interface_event_list_head();
00147     iface_eventlist_entry_t *entry = new iface_eventlist_entry_t;
00148     entry->iface = this;
00149     entry->status_cb = status_cb;
00150     ns_list_add_to_end(event_list, entry);
00151     attach(mbed::callback(&call_all_event_listeners, this));
00152 }
00153 
00154 void NetworkInterface::remove_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00155 {
00156     iface_eventlist_t *event_list = get_interface_event_list_head();
00157     ns_list_foreach_safe(iface_eventlist_entry_t, entry, event_list) {
00158         if (entry->status_cb == status_cb && entry->iface == this) {
00159             ns_list_remove(event_list, entry);
00160             delete entry;
00161             return;
00162         }
00163     }
00164 }
00165 
00166 NetworkInterface::~NetworkInterface()
00167 {
00168     iface_eventlist_t *event_list = get_interface_event_list_head();
00169     ns_list_foreach_safe(iface_eventlist_entry_t, entry, event_list) {
00170         if (entry->iface == this) {
00171             ns_list_remove(event_list, entry);
00172             delete entry;
00173         }
00174     }
00175 }
00176 
00177 nsapi_connection_status_t NetworkInterface::get_connection_status() const
00178 {
00179     return NSAPI_STATUS_ERROR_UNSUPPORTED;
00180 }
00181 
00182 nsapi_error_t NetworkInterface::set_blocking(bool blocking)
00183 {
00184     return NSAPI_ERROR_UNSUPPORTED ;
00185 }
00186