Knight KE / Mbed OS Game_Master
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 "CellularBase.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 CellularBase *CellularBase::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 /* Finally the dispatch from the JSON default interface type to the specific
00066  * subclasses. It's our job to configure - the default NetworkInterface is
00067  * preconfigured - the specific subtypes' defaults are not (necessarily).
00068  */
00069 #define ETHERNET 1
00070 #define WIFI 2
00071 #define MESH 3
00072 #define CELLULAR 4
00073 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == ETHERNET
00074 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00075 {
00076     return EthInterface::get_default_instance();
00077 }
00078 #elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI
00079 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00080 {
00081     /* WiFi is not expected to work unless we have configuration parameters.
00082      * We do not hook up to WifiInterface::get_default_instance() unless
00083      * we have at least an access point name.
00084      */
00085 #ifdef MBED_CONF_NSAPI_DEFAULT_WIFI_SSID
00086     WiFiInterface *wifi = WiFiInterface::get_default_instance();
00087     if (!wifi) {
00088         return NULL;
00089     }
00090 #ifndef MBED_CONF_NSAPI_DEFAULT_WIFI_PASSWORD
00091 #define MBED_CONF_NSAPI_DEFAULT_WIFI_PASSWORD NULL
00092 #endif
00093 #ifndef MBED_CONF_NSAPI_DEFAULT_WIFI_SECURITY
00094 #define MBED_CONF_NSAPI_DEFAULT_WIFI_SECURITY NONE
00095 #endif
00096 #define concat_(x,y) x##y
00097 #define concat(x,y) concat_(x,y)
00098 #define SECURITY concat(NSAPI_SECURITY_,MBED_CONF_NSAPI_DEFAULT_WIFI_SECURITY)
00099     wifi->set_credentials(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID, MBED_CONF_NSAPI_DEFAULT_WIFI_PASSWORD, SECURITY);
00100     return wifi;
00101 #else
00102     return NULL;
00103 #endif
00104 }
00105 #elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == MESH
00106 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00107 {
00108     return MeshInterface::get_default_instance();
00109 }
00110 #elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == CELLULAR
00111 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00112 {
00113     CellularBase *cellular = CellularBase::get_default_instance();
00114     if (!cellular) {
00115         return NULL;
00116     }
00117     /* CellularBase is expected to attempt to work without any parameters - we
00118      * will try, at least.
00119      */
00120 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN
00121 #ifndef MBED_CONF_NSAPI_DEFAULT_CELLULAR_USERNAME
00122 #define MBED_CONF_NSAPI_DEFAULT_CELLULAR_USERNAME NULL
00123 #endif
00124 #ifndef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PASSWORD
00125 #define MBED_CONF_NSAPI_DEFAULT_CELLULAR_PASSWORD NULL
00126 #endif
00127     cellular->set_credentials(MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN, MBED_CONF_NSAPI_DEFAULT_CELLULAR_USERNAME, MBED_CONF_NSAPI_DEFAULT_CELLULAR_PASSWORD);
00128 #endif
00129 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN
00130     cellular->set_sim_pin(MBED_CONF_NSAPI_DEFAULT_CELLULAR_SIM_PIN);
00131 #endif
00132 
00133     return cellular;
00134 }
00135 #elif defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE)
00136 /* If anyone invents a new JSON value, they must have their own default weak
00137  * implementation.
00138  */
00139 #else
00140 /* When the default type is null */
00141 MBED_WEAK NetworkInterface *NetworkInterface::get_target_default_instance()
00142 {
00143     return NULL;
00144 }
00145 #endif