Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NetworkInterfaceDefaults.cpp Source File

NetworkInterfaceDefaults.cpp

00001 /* Network interface defaults
00002  * Copyright (c) 2018 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 "netsocket/NetworkInterface.h"
00018 
00019 #include "EthInterface.h"
00020 #include "WiFiInterface.h"
00021 #include "CellularInterface.h"
00022 #include "MeshInterface.h"
00023 
00024 /* Weak default instance static classes for the various abstract classes.
00025  * Applications can override these.
00026  */
00027 
00028 MBED_WEAK EthInterface *EthInterface::get_default_instance()
00029 {
00030     return get_target_default_instance();
00031 }
00032 
00033 MBED_WEAK WiFiInterface *WiFiInterface::get_default_instance()
00034 {
00035     return get_target_default_instance();
00036 }
00037 
00038 MBED_WEAK MeshInterface *MeshInterface::get_default_instance()
00039 {
00040     return get_target_default_instance();
00041 }
00042 
00043 MBED_WEAK CellularInterface *CellularInterface::get_default_instance()
00044 {
00045     return get_target_default_instance();
00046 }
00047 
00048 /* For other types, we can provide a reasonable get_target_default_instance
00049  * in some cases. This is done in EthernetInterface.cpp, mbed-mesh-api and
00050  * OnboardCellularInterface.cpp. We have no implementation for WiFi, so a
00051  * default empty one lives here.
00052  */
00053 
00054 MBED_WEAK WiFiInterface *WiFiInterface::get_target_default_instance()
00055 {
00056     return NULL;
00057 }
00058 
00059 /* The top-level get_default_instance() call. Weak for application override. */
00060 MBED_WEAK NetworkInterface *NetworkInterface::get_default_instance()
00061 {
00062     return get_target_default_instance();
00063 }
00064 
00065 
00066 /* Helpers to set default parameters - used by NetworkInterface::get_default_instance,
00067  * but exposed for apps which want to get these defaults after requesting a specific type.
00068  */
00069 void NetworkInterface::set_default_parameters()
00070 {
00071 
00072 }
00073 
00074 void WiFiInterface::set_default_parameters()
00075 {
00076 #ifdef MBED_CONF_NSAPI_DEFAULT_WIFI_SSID
00077 #ifndef MBED_CONF_NSAPI_DEFAULT_WIFI_PASSWORD
00078 #define MBED_CONF_NSAPI_DEFAULT_WIFI_PASSWORD NULL
00079 #endif
00080 #ifndef MBED_CONF_NSAPI_DEFAULT_WIFI_SECURITY
00081 #define MBED_CONF_NSAPI_DEFAULT_WIFI_SECURITY NONE
00082 #endif
00083 #define concat_(x,y) x##y
00084 #define concat(x,y) concat_(x,y)
00085 #define SECURITY concat(NSAPI_SECURITY_,MBED_CONF_NSAPI_DEFAULT_WIFI_SECURITY)
00086     set_credentials(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID, MBED_CONF_NSAPI_DEFAULT_WIFI_PASSWORD, SECURITY);
00087 #endif
00088 }
00089 
00090 void CellularInterface::set_default_parameters()
00091 {
00092     /* CellularInterface is expected to attempt to work without any parameters - we
00093      * will try, at least.
00094      */
00095 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN
00096 #ifndef MBED_CONF_NSAPI_DEFAULT_CELLULAR_USERNAME
00097 #define MBED_CONF_NSAPI_DEFAULT_CELLULAR_USERNAME NULL
00098 #endif
00099 #ifndef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PASSWORD
00100 #define MBED_CONF_NSAPI_DEFAULT_CELLULAR_PASSWORD NULL
00101 #endif
00102     set_credentials(MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN, MBED_CONF_NSAPI_DEFAULT_CELLULAR_USERNAME, MBED_CONF_NSAPI_DEFAULT_CELLULAR_PASSWORD);
00103 #endif
00104 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN
00105     set_sim_pin(MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN);
00106 #endif
00107 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN
00108     set_plmn(MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN);
00109 #endif
00110 }
00111 
00112 /* Finally the dispatch from the JSON default interface type to the specific
00113  * subclasses. It's our job to configure - the default NetworkInterface is
00114  * preconfigured - the specific subtypes' defaults are not (necessarily).
00115  */
00116 #define ETHERNET 1
00117 #define WIFI 2
00118 #define MESH 3
00119 #define CELLULAR 4
00120 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == ETHERNET
00121 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00122 {
00123     return EthInterface::get_default_instance();
00124 }
00125 #elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI
00126 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00127 {
00128     /* WiFi is not expected to work unless we have configuration parameters.
00129      * We do not hook up to WifiInterface::get_default_instance() unless
00130      * we have at least an access point name.
00131      */
00132 #ifdef MBED_CONF_NSAPI_DEFAULT_WIFI_SSID
00133     WiFiInterface *wifi = WiFiInterface::get_default_instance();
00134     if (!wifi) {
00135         return NULL;
00136     }
00137     wifi->set_default_parameters();
00138     return wifi;
00139 #else
00140     return NULL;
00141 #endif
00142 }
00143 #elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == MESH
00144 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00145 {
00146     return MeshInterface::get_default_instance();
00147 }
00148 #elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == CELLULAR
00149 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00150 {
00151     CellularInterface *cellular = CellularInterface::get_default_instance();
00152     if (!cellular) {
00153         return NULL;
00154     }
00155     cellular->set_default_parameters();
00156     return cellular;
00157 }
00158 #elif defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE)
00159 /* If anyone invents a new JSON value, they must have their own default weak
00160  * implementation.
00161  */
00162 #else
00163 /* When the default type is null */
00164 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00165 {
00166     return NULL;
00167 }
00168 #endif