wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/easy-connect.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* |
JMF | 0:24d3eb812fd4 | 2 | * FILE: easy-connect.cpp |
JMF | 0:24d3eb812fd4 | 3 | * |
JMF | 0:24d3eb812fd4 | 4 | * Copyright (c) 2015 - 2017 ARM Limited. All rights reserved. |
JMF | 0:24d3eb812fd4 | 5 | * SPDX-License-Identifier: Apache-2.0 |
JMF | 0:24d3eb812fd4 | 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
JMF | 0:24d3eb812fd4 | 7 | * not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 8 | * You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 9 | * |
JMF | 0:24d3eb812fd4 | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 11 | * |
JMF | 0:24d3eb812fd4 | 12 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:24d3eb812fd4 | 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
JMF | 0:24d3eb812fd4 | 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:24d3eb812fd4 | 15 | * See the License for the specific language governing permissions and |
JMF | 0:24d3eb812fd4 | 16 | * limitations under the License. |
JMF | 0:24d3eb812fd4 | 17 | */ |
JMF | 0:24d3eb812fd4 | 18 | |
JMF | 0:24d3eb812fd4 | 19 | #include "mbed.h" |
JMF | 0:24d3eb812fd4 | 20 | #include "easy-connect.h" |
JMF | 0:24d3eb812fd4 | 21 | |
JMF | 0:24d3eb812fd4 | 22 | /* |
JMF | 0:24d3eb812fd4 | 23 | * Instantiate the configured network interface |
JMF | 0:24d3eb812fd4 | 24 | */ |
JMF | 0:24d3eb812fd4 | 25 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266 |
JMF | 0:24d3eb812fd4 | 26 | #include "ESP8266Interface.h" |
JMF | 0:24d3eb812fd4 | 27 | #define EASY_CONNECT_WIFI_TYPE "ESP8266" |
JMF | 0:24d3eb812fd4 | 28 | |
JMF | 0:24d3eb812fd4 | 29 | #ifdef MBED_CONF_APP_ESP8266_DEBUG |
JMF | 0:24d3eb812fd4 | 30 | ESP8266Interface wifi(MBED_CONF_EASY_CONNECT_WIFI_ESP8266_TX, MBED_CONF_EASY_CONNECT_WIFI_ESP8266_RX, MBED_CONF_EASY_CONNECT_WIFI_ESP8266_DEBUG); |
JMF | 0:24d3eb812fd4 | 31 | #else |
JMF | 0:24d3eb812fd4 | 32 | ESP8266Interface wifi(MBED_CONF_EASY_CONNECT_WIFI_ESP8266_TX, MBED_CONF_EASY_CONNECT_WIFI_ESP8266_RX); |
JMF | 0:24d3eb812fd4 | 33 | #endif |
JMF | 0:24d3eb812fd4 | 34 | |
JMF | 0:24d3eb812fd4 | 35 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN |
JMF | 0:24d3eb812fd4 | 36 | #define EASY_CONNECT_WIFI_TYPE "Odin" |
JMF | 0:24d3eb812fd4 | 37 | #include "OdinWiFiInterface.h" |
JMF | 0:24d3eb812fd4 | 38 | OdinWiFiInterface wifi; |
JMF | 0:24d3eb812fd4 | 39 | |
JMF | 0:24d3eb812fd4 | 40 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW |
JMF | 0:24d3eb812fd4 | 41 | #define EASY_CONNECT_WIFI_TYPE "RTW" |
JMF | 0:24d3eb812fd4 | 42 | #include "RTWInterface.h" |
JMF | 0:24d3eb812fd4 | 43 | RTWInterface wifi; |
JMF | 0:24d3eb812fd4 | 44 | |
JMF | 0:24d3eb812fd4 | 45 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_IDW0XX1 |
JMF | 0:24d3eb812fd4 | 46 | #include "SpwfSAInterface.h" |
JMF | 0:24d3eb812fd4 | 47 | |
JMF | 0:24d3eb812fd4 | 48 | #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1 |
JMF | 0:24d3eb812fd4 | 49 | #define EASY_CONNECT_WIFI_TYPE "IDW01M1" |
JMF | 0:24d3eb812fd4 | 50 | SpwfSAInterface wifi(MBED_CONF_EASY_CONNECT_WIFI_IDW01M1_TX, MBED_CONF_EASY_CONNECT_WIFI_IDW01M1_RX); |
JMF | 0:24d3eb812fd4 | 51 | #endif // MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1 |
JMF | 0:24d3eb812fd4 | 52 | |
JMF | 0:24d3eb812fd4 | 53 | #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1 |
JMF | 0:24d3eb812fd4 | 54 | #define EASY_CONNECT_WIFI_TYPE "IDW04A1" |
JMF | 0:24d3eb812fd4 | 55 | SpwfSAInterface wifi(MBED_CONF_EASY_CONNECT_WIFI_IDW04A1_TX, MBED_CONF_EASY_CONNECT_WIFI_IDW04A1_RX); |
JMF | 0:24d3eb812fd4 | 56 | #endif // MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1 |
JMF | 0:24d3eb812fd4 | 57 | |
JMF | 0:24d3eb812fd4 | 58 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ISM43362 |
JMF | 0:24d3eb812fd4 | 59 | #include "ISM43362Interface.h" |
JMF | 0:24d3eb812fd4 | 60 | #define EASY_CONNECT_WIFI_TYPE "ISM43362" |
JMF | 0:24d3eb812fd4 | 61 | |
JMF | 0:24d3eb812fd4 | 62 | #ifdef MBED_CONF_APP_ISM43362_DEBUG |
JMF | 0:24d3eb812fd4 | 63 | ISM43362Interface wifi(true); |
JMF | 0:24d3eb812fd4 | 64 | #else |
JMF | 0:24d3eb812fd4 | 65 | ISM43362Interface wifi; |
JMF | 0:24d3eb812fd4 | 66 | #endif |
JMF | 0:24d3eb812fd4 | 67 | |
JMF | 0:24d3eb812fd4 | 68 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
JMF | 0:24d3eb812fd4 | 69 | #include "EthernetInterface.h" |
JMF | 0:24d3eb812fd4 | 70 | EthernetInterface eth; |
JMF | 0:24d3eb812fd4 | 71 | |
JMF | 0:24d3eb812fd4 | 72 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
JMF | 0:24d3eb812fd4 | 73 | #define EASY_CONNECT_MESH |
JMF | 0:24d3eb812fd4 | 74 | #include "NanostackInterface.h" |
JMF | 0:24d3eb812fd4 | 75 | LoWPANNDInterface mesh; |
JMF | 0:24d3eb812fd4 | 76 | |
JMF | 0:24d3eb812fd4 | 77 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
JMF | 0:24d3eb812fd4 | 78 | #define EASY_CONNECT_MESH |
JMF | 0:24d3eb812fd4 | 79 | #include "NanostackInterface.h" |
JMF | 0:24d3eb812fd4 | 80 | ThreadInterface mesh; |
JMF | 0:24d3eb812fd4 | 81 | |
JMF | 0:24d3eb812fd4 | 82 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_ONBOARD |
JMF | 0:24d3eb812fd4 | 83 | #include "OnboardCellularInterface.h" |
JMF | 0:24d3eb812fd4 | 84 | OnboardCellularInterface cellular; |
JMF | 0:24d3eb812fd4 | 85 | |
JMF | 0:24d3eb812fd4 | 86 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR |
JMF | 0:24d3eb812fd4 | 87 | #include "EasyCellularConnection.h" |
JMF | 0:24d3eb812fd4 | 88 | EasyCellularConnection cellular; |
JMF | 0:24d3eb812fd4 | 89 | |
JMF | 0:24d3eb812fd4 | 90 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_WIZFI310 |
JMF | 0:24d3eb812fd4 | 91 | #include "WizFi310Interface.h" |
JMF | 0:24d3eb812fd4 | 92 | #define EASY_CONNECT_WIFI_TYPE "WizFi310" |
JMF | 0:24d3eb812fd4 | 93 | |
JMF | 0:24d3eb812fd4 | 94 | #ifdef MBED_CONF_APP_WIZFI310_DEBUG |
JMF | 0:24d3eb812fd4 | 95 | WizFi310Interface wifi(MBED_CONF_EASY_CONNECT_WIFI_WIZFI310_TX, MBED_CONF_EASY_CONNECT_WIFI_WIZFI310_RX, MBED_CONF_EASY_CONNECT_WIFI_WIZFI310_DEBUG); |
JMF | 0:24d3eb812fd4 | 96 | #else |
JMF | 0:24d3eb812fd4 | 97 | WizFi310Interface wifi(MBED_CONF_EASY_CONNECT_WIFI_WIZFI310_TX, MBED_CONF_EASY_CONNECT_WIFI_WIZFI310_RX); |
JMF | 0:24d3eb812fd4 | 98 | #endif |
JMF | 0:24d3eb812fd4 | 99 | |
JMF | 0:24d3eb812fd4 | 100 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A |
JMF | 0:24d3eb812fd4 | 101 | #include "WNC14A2AInterface.h" |
JMF | 0:24d3eb812fd4 | 102 | |
JMF | 0:24d3eb812fd4 | 103 | #if MBED_CONF_APP_WNC_DEBUG == true |
JMF | 0:24d3eb812fd4 | 104 | #include "WNCDebug.h" |
JMF | 0:24d3eb812fd4 | 105 | WNCDebug dbgout(stderr); |
JMF | 0:24d3eb812fd4 | 106 | WNC14A2AInterface wnc(&dbgout); |
JMF | 0:24d3eb812fd4 | 107 | #else |
JMF | 0:24d3eb812fd4 | 108 | WNC14A2AInterface wnc; |
JMF | 0:24d3eb812fd4 | 109 | #endif |
JMF | 0:24d3eb812fd4 | 110 | |
JMF | 0:24d3eb812fd4 | 111 | |
JMF | 0:24d3eb812fd4 | 112 | #else |
JMF | 0:24d3eb812fd4 | 113 | #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)." |
JMF | 0:24d3eb812fd4 | 114 | #endif // MBED_CONF_APP_NETWORK_INTERFACE |
JMF | 0:24d3eb812fd4 | 115 | |
JMF | 0:24d3eb812fd4 | 116 | /* |
JMF | 0:24d3eb812fd4 | 117 | * In case of Mesh, instantiate the configured RF PHY. |
JMF | 0:24d3eb812fd4 | 118 | */ |
JMF | 0:24d3eb812fd4 | 119 | #if defined (EASY_CONNECT_MESH) |
JMF | 0:24d3eb812fd4 | 120 | #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL |
JMF | 0:24d3eb812fd4 | 121 | #include "NanostackRfPhyAtmel.h" |
JMF | 0:24d3eb812fd4 | 122 | #define EASY_CONNECT_MESH_TYPE "Atmel" |
JMF | 0:24d3eb812fd4 | 123 | NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS, |
JMF | 0:24d3eb812fd4 | 124 | ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL); |
JMF | 0:24d3eb812fd4 | 125 | |
JMF | 0:24d3eb812fd4 | 126 | #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20 |
JMF | 0:24d3eb812fd4 | 127 | #include "NanostackRfPhyMcr20a.h" |
JMF | 0:24d3eb812fd4 | 128 | #define EASY_CONNECT_MESH_TYPE "Mcr20A" |
JMF | 0:24d3eb812fd4 | 129 | NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ); |
JMF | 0:24d3eb812fd4 | 130 | |
JMF | 0:24d3eb812fd4 | 131 | #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1 |
JMF | 0:24d3eb812fd4 | 132 | #include "NanostackRfPhySpirit1.h" |
JMF | 0:24d3eb812fd4 | 133 | #define EASY_CONNECT_MESH_TYPE "Spirit1" |
JMF | 0:24d3eb812fd4 | 134 | NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK, |
JMF | 0:24d3eb812fd4 | 135 | SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED); |
JMF | 0:24d3eb812fd4 | 136 | |
JMF | 0:24d3eb812fd4 | 137 | #elif MBED_CONF_APP_MESH_RADIO_TYPE == EFR32 |
JMF | 0:24d3eb812fd4 | 138 | #include "NanostackRfPhyEfr32.h" |
JMF | 0:24d3eb812fd4 | 139 | #define EASY_CONNECT_MESH_TYPE "EFR32" |
JMF | 0:24d3eb812fd4 | 140 | NanostackRfPhyEfr32 rf_phy; |
JMF | 0:24d3eb812fd4 | 141 | |
JMF | 0:24d3eb812fd4 | 142 | #endif // MBED_CONF_APP_RADIO_TYPE |
JMF | 0:24d3eb812fd4 | 143 | #endif // EASY_CONNECT_MESH |
JMF | 0:24d3eb812fd4 | 144 | |
JMF | 0:24d3eb812fd4 | 145 | #if defined (EASY_CONNECT_WIFI) |
JMF | 0:24d3eb812fd4 | 146 | #define WIFI_SSID_MAX_LEN 32 // As per IEEE 802.11 chapter 7.3.2.1 (SSID element) |
JMF | 0:24d3eb812fd4 | 147 | #define WIFI_PASSWORD_MAX_LEN 64 // |
JMF | 0:24d3eb812fd4 | 148 | |
JMF | 0:24d3eb812fd4 | 149 | char* _ssid = NULL; |
JMF | 0:24d3eb812fd4 | 150 | char* _password = NULL; |
JMF | 0:24d3eb812fd4 | 151 | #endif // EASY_CONNECT_WIFI |
JMF | 0:24d3eb812fd4 | 152 | |
JMF | 0:24d3eb812fd4 | 153 | /* \brief print_MAC - print_MAC - helper function to print out MAC address |
JMF | 0:24d3eb812fd4 | 154 | * in: network_interface - pointer to network i/f |
JMF | 0:24d3eb812fd4 | 155 | * bool log-messages print out logs or not |
JMF | 0:24d3eb812fd4 | 156 | * MAC address is printed, if it can be acquired & log_messages is true. |
JMF | 0:24d3eb812fd4 | 157 | * |
JMF | 0:24d3eb812fd4 | 158 | */ |
JMF | 0:24d3eb812fd4 | 159 | void print_MAC(NetworkInterface* network_interface, bool log_messages) { |
JMF | 0:24d3eb812fd4 | 160 | #if MBED_CONF_APP_NETWORK_INTERFACE != CELLULAR_ONBOARD && MBED_CONF_APP_NETWORK_INTERFACE != CELLULAR |
JMF | 0:24d3eb812fd4 | 161 | const char *mac_addr = network_interface->get_mac_address(); |
JMF | 0:24d3eb812fd4 | 162 | if (mac_addr == NULL) { |
JMF | 0:24d3eb812fd4 | 163 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 164 | printf("[EasyConnect] ERROR - No MAC address\n"); |
JMF | 0:24d3eb812fd4 | 165 | } |
JMF | 0:24d3eb812fd4 | 166 | return; |
JMF | 0:24d3eb812fd4 | 167 | } |
JMF | 0:24d3eb812fd4 | 168 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 169 | printf("[EasyConnect] MAC address %s\n", mac_addr); |
JMF | 0:24d3eb812fd4 | 170 | } |
JMF | 0:24d3eb812fd4 | 171 | #endif |
JMF | 0:24d3eb812fd4 | 172 | } |
JMF | 0:24d3eb812fd4 | 173 | |
JMF | 0:24d3eb812fd4 | 174 | |
JMF | 0:24d3eb812fd4 | 175 | |
JMF | 0:24d3eb812fd4 | 176 | /* \brief easy_connect easy_connect() function to connect the pre-defined network bearer, |
JMF | 0:24d3eb812fd4 | 177 | * config done via mbed_app.json (see README.md for details). |
JMF | 0:24d3eb812fd4 | 178 | * |
JMF | 0:24d3eb812fd4 | 179 | * IN: bool log_messages print out diagnostics or not. |
JMF | 0:24d3eb812fd4 | 180 | */ |
JMF | 0:24d3eb812fd4 | 181 | NetworkInterface* easy_connect(bool log_messages) { |
JMF | 0:24d3eb812fd4 | 182 | NetworkInterface* network_interface = NULL; |
JMF | 0:24d3eb812fd4 | 183 | int connect_success = -1; |
JMF | 0:24d3eb812fd4 | 184 | |
JMF | 0:24d3eb812fd4 | 185 | #if defined (EASY_CONNECT_WIFI) |
JMF | 0:24d3eb812fd4 | 186 | // We check if the _ssid and _password have already been set (via the easy_connect() |
JMF | 0:24d3eb812fd4 | 187 | // that takes thoses parameters or not. |
JMF | 0:24d3eb812fd4 | 188 | // If they have not been set, use the ones we can gain from mbed_app.json. |
JMF | 0:24d3eb812fd4 | 189 | if (_ssid == NULL) { |
JMF | 0:24d3eb812fd4 | 190 | if(strlen(MBED_CONF_APP_WIFI_SSID) > WIFI_SSID_MAX_LEN) { |
JMF | 0:24d3eb812fd4 | 191 | printf("ERROR - MBED_CONF_APP_WIFI_SSID is too long %d vs. %d\n", |
JMF | 0:24d3eb812fd4 | 192 | strlen(MBED_CONF_APP_WIFI_SSID), |
JMF | 0:24d3eb812fd4 | 193 | WIFI_SSID_MAX_LEN); |
JMF | 0:24d3eb812fd4 | 194 | return NULL; |
JMF | 0:24d3eb812fd4 | 195 | } |
JMF | 0:24d3eb812fd4 | 196 | } |
JMF | 0:24d3eb812fd4 | 197 | |
JMF | 0:24d3eb812fd4 | 198 | if (_password == NULL) { |
JMF | 0:24d3eb812fd4 | 199 | if(strlen(MBED_CONF_APP_WIFI_PASSWORD) > WIFI_PASSWORD_MAX_LEN) { |
JMF | 0:24d3eb812fd4 | 200 | printf("ERROR - MBED_CONF_APP_WIFI_PASSWORD is too long %d vs. %d\n", |
JMF | 0:24d3eb812fd4 | 201 | strlen(MBED_CONF_APP_WIFI_PASSWORD), |
JMF | 0:24d3eb812fd4 | 202 | WIFI_PASSWORD_MAX_LEN); |
JMF | 0:24d3eb812fd4 | 203 | return NULL; |
JMF | 0:24d3eb812fd4 | 204 | } |
JMF | 0:24d3eb812fd4 | 205 | } |
JMF | 0:24d3eb812fd4 | 206 | #endif // EASY_CONNECT_WIFI |
JMF | 0:24d3eb812fd4 | 207 | |
JMF | 0:24d3eb812fd4 | 208 | /// This should be removed once mbedOS supports proper dual-stack |
JMF | 0:24d3eb812fd4 | 209 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 210 | #if defined (EASY_CONNECT_MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true) |
JMF | 0:24d3eb812fd4 | 211 | printf("[EasyConnect] IPv6 mode\n"); |
JMF | 0:24d3eb812fd4 | 212 | #else |
JMF | 0:24d3eb812fd4 | 213 | printf("[EasyConnect] IPv4 mode\n"); |
JMF | 0:24d3eb812fd4 | 214 | #endif |
JMF | 0:24d3eb812fd4 | 215 | } |
JMF | 0:24d3eb812fd4 | 216 | |
JMF | 0:24d3eb812fd4 | 217 | #if defined (EASY_CONNECT_WIFI) |
JMF | 0:24d3eb812fd4 | 218 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 219 | printf("[EasyConnect] Using WiFi (%s) \n", EASY_CONNECT_WIFI_TYPE); |
JMF | 0:24d3eb812fd4 | 220 | printf("[EasyConnect] Connecting to WiFi %s\n", |
JMF | 0:24d3eb812fd4 | 221 | ((_ssid == NULL) ? MBED_CONF_APP_WIFI_SSID : _ssid) ); |
JMF | 0:24d3eb812fd4 | 222 | } |
JMF | 0:24d3eb812fd4 | 223 | network_interface = &wifi; |
JMF | 0:24d3eb812fd4 | 224 | if (_ssid == NULL) { |
JMF | 0:24d3eb812fd4 | 225 | connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, |
JMF | 0:24d3eb812fd4 | 226 | (strlen(MBED_CONF_APP_WIFI_PASSWORD) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE); |
JMF | 0:24d3eb812fd4 | 227 | } |
JMF | 0:24d3eb812fd4 | 228 | else { |
JMF | 0:24d3eb812fd4 | 229 | connect_success = wifi.connect(_ssid, _password, (strlen(_password) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE); |
JMF | 0:24d3eb812fd4 | 230 | } |
JMF | 0:24d3eb812fd4 | 231 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_ONBOARD || MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR |
JMF | 0:24d3eb812fd4 | 232 | # ifdef MBED_CONF_APP_CELLULAR_SIM_PIN |
JMF | 0:24d3eb812fd4 | 233 | cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN); |
JMF | 0:24d3eb812fd4 | 234 | # endif |
JMF | 0:24d3eb812fd4 | 235 | # ifdef MBED_CONF_APP_CELLULAR_APN |
JMF | 0:24d3eb812fd4 | 236 | # ifndef MBED_CONF_APP_CELLULAR_USERNAME |
JMF | 0:24d3eb812fd4 | 237 | # define MBED_CONF_APP_CELLULAR_USERNAME 0 |
JMF | 0:24d3eb812fd4 | 238 | # endif |
JMF | 0:24d3eb812fd4 | 239 | # ifndef MBED_CONF_APP_CELLULAR_PASSWORD |
JMF | 0:24d3eb812fd4 | 240 | # define MBED_CONF_APP_CELLULAR_PASSWORD 0 |
JMF | 0:24d3eb812fd4 | 241 | # endif |
JMF | 0:24d3eb812fd4 | 242 | cellular.set_credentials(MBED_CONF_APP_CELLULAR_APN, MBED_CONF_APP_CELLULAR_USERNAME, MBED_CONF_APP_CELLULAR_PASSWORD); |
JMF | 0:24d3eb812fd4 | 243 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 244 | printf("[EasyConnect] Connecting using Cellular interface and APN %s\n", MBED_CONF_APP_CELLULAR_APN); |
JMF | 0:24d3eb812fd4 | 245 | } |
JMF | 0:24d3eb812fd4 | 246 | # else |
JMF | 0:24d3eb812fd4 | 247 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 248 | printf("[EasyConnect] Connecting using Cellular interface and default APN\n"); |
JMF | 0:24d3eb812fd4 | 249 | } |
JMF | 0:24d3eb812fd4 | 250 | # endif |
JMF | 0:24d3eb812fd4 | 251 | connect_success = cellular.connect(); |
JMF | 0:24d3eb812fd4 | 252 | network_interface = &cellular; |
JMF | 0:24d3eb812fd4 | 253 | |
JMF | 0:24d3eb812fd4 | 254 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A |
JMF | 0:24d3eb812fd4 | 255 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 256 | printf("[EasyConnect] Using WNC14A2A\n"); |
JMF | 0:24d3eb812fd4 | 257 | } |
JMF | 0:24d3eb812fd4 | 258 | # if MBED_CONF_APP_WNC_DEBUG == true |
JMF | 0:24d3eb812fd4 | 259 | printf("[EasyConnect] With WNC14A2A debug output set to 0x%02X\n",MBED_CONF_APP_WNC_DEBUG_SETTING); |
JMF | 0:24d3eb812fd4 | 260 | wnc.doDebug(MBED_CONF_APP_WNC_DEBUG_SETTING); |
JMF | 0:24d3eb812fd4 | 261 | # endif |
JMF | 0:24d3eb812fd4 | 262 | network_interface = &wnc; |
JMF | 0:24d3eb812fd4 | 263 | connect_success = wnc.connect(); |
JMF | 0:24d3eb812fd4 | 264 | |
JMF | 0:24d3eb812fd4 | 265 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
JMF | 0:24d3eb812fd4 | 266 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 267 | printf("[EasyConnect] Using Ethernet\n"); |
JMF | 0:24d3eb812fd4 | 268 | } |
JMF | 0:24d3eb812fd4 | 269 | network_interface = ð |
JMF | 0:24d3eb812fd4 | 270 | #if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION |
JMF | 0:24d3eb812fd4 | 271 | eth.set_blocking(false); |
JMF | 0:24d3eb812fd4 | 272 | #endif |
JMF | 0:24d3eb812fd4 | 273 | connect_success = eth.connect(); |
JMF | 0:24d3eb812fd4 | 274 | #endif |
JMF | 0:24d3eb812fd4 | 275 | |
JMF | 0:24d3eb812fd4 | 276 | #ifdef EASY_CONNECT_MESH |
JMF | 0:24d3eb812fd4 | 277 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 278 | printf("[EasyConnect] Using Mesh (%s)\n", EASY_CONNECT_MESH_TYPE); |
JMF | 0:24d3eb812fd4 | 279 | printf("[EasyConnect] Connecting to Mesh...\n"); |
JMF | 0:24d3eb812fd4 | 280 | } |
JMF | 0:24d3eb812fd4 | 281 | network_interface = &mesh; |
JMF | 0:24d3eb812fd4 | 282 | #if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION |
JMF | 0:24d3eb812fd4 | 283 | mesh.set_blocking(false); |
JMF | 0:24d3eb812fd4 | 284 | #endif |
JMF | 0:24d3eb812fd4 | 285 | mesh.initialize(&rf_phy); |
JMF | 0:24d3eb812fd4 | 286 | connect_success = mesh.connect(); |
JMF | 0:24d3eb812fd4 | 287 | #endif |
JMF | 0:24d3eb812fd4 | 288 | if(connect_success == 0 |
JMF | 0:24d3eb812fd4 | 289 | #if (MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION && (MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET || defined(EASY_CONNECT_MESH))) |
JMF | 0:24d3eb812fd4 | 290 | || connect_success == NSAPI_ERROR_IS_CONNECTED || connect_success == NSAPI_ERROR_ALREADY |
JMF | 0:24d3eb812fd4 | 291 | #endif |
JMF | 0:24d3eb812fd4 | 292 | ) { |
JMF | 0:24d3eb812fd4 | 293 | #if (MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION && (MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET || defined(EASY_CONNECT_MESH))) |
JMF | 0:24d3eb812fd4 | 294 | nsapi_connection_status_t connection_status; |
JMF | 0:24d3eb812fd4 | 295 | |
JMF | 0:24d3eb812fd4 | 296 | for (;;) { |
JMF | 0:24d3eb812fd4 | 297 | |
JMF | 0:24d3eb812fd4 | 298 | // Check current connection status. |
JMF | 0:24d3eb812fd4 | 299 | connection_status = network_interface->get_connection_status(); |
JMF | 0:24d3eb812fd4 | 300 | |
JMF | 0:24d3eb812fd4 | 301 | if (connection_status == NSAPI_STATUS_GLOBAL_UP) { |
JMF | 0:24d3eb812fd4 | 302 | |
JMF | 0:24d3eb812fd4 | 303 | // Connection ready. |
JMF | 0:24d3eb812fd4 | 304 | break; |
JMF | 0:24d3eb812fd4 | 305 | |
JMF | 0:24d3eb812fd4 | 306 | } else if (connection_status == NSAPI_STATUS_ERROR_UNSUPPORTED) { |
JMF | 0:24d3eb812fd4 | 307 | |
JMF | 0:24d3eb812fd4 | 308 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 309 | print_MAC(network_interface, log_messages); |
JMF | 0:24d3eb812fd4 | 310 | printf("[EasyConnect] Connection to Network Failed %d!\n", connection_status); |
JMF | 0:24d3eb812fd4 | 311 | } |
JMF | 0:24d3eb812fd4 | 312 | return NULL; |
JMF | 0:24d3eb812fd4 | 313 | |
JMF | 0:24d3eb812fd4 | 314 | } |
JMF | 0:24d3eb812fd4 | 315 | |
JMF | 0:24d3eb812fd4 | 316 | // Not ready yet, give some runtime to the network stack. |
JMF | 0:24d3eb812fd4 | 317 | mbed::mbed_event_queue()->dispatch(100); |
JMF | 0:24d3eb812fd4 | 318 | |
JMF | 0:24d3eb812fd4 | 319 | } |
JMF | 0:24d3eb812fd4 | 320 | #endif |
JMF | 0:24d3eb812fd4 | 321 | |
JMF | 0:24d3eb812fd4 | 322 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 323 | printf("[EasyConnect] Connected to Network successfully\n"); |
JMF | 0:24d3eb812fd4 | 324 | print_MAC(network_interface, log_messages); |
JMF | 0:24d3eb812fd4 | 325 | } |
JMF | 0:24d3eb812fd4 | 326 | } else { |
JMF | 0:24d3eb812fd4 | 327 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 328 | print_MAC(network_interface, log_messages); |
JMF | 0:24d3eb812fd4 | 329 | printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success); |
JMF | 0:24d3eb812fd4 | 330 | } |
JMF | 0:24d3eb812fd4 | 331 | return NULL; |
JMF | 0:24d3eb812fd4 | 332 | } |
JMF | 0:24d3eb812fd4 | 333 | const char *ip_addr = network_interface->get_ip_address(); |
JMF | 0:24d3eb812fd4 | 334 | if (ip_addr == NULL) { |
JMF | 0:24d3eb812fd4 | 335 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 336 | printf("[EasyConnect] ERROR - No IP address\n"); |
JMF | 0:24d3eb812fd4 | 337 | } |
JMF | 0:24d3eb812fd4 | 338 | return NULL; |
JMF | 0:24d3eb812fd4 | 339 | } |
JMF | 0:24d3eb812fd4 | 340 | |
JMF | 0:24d3eb812fd4 | 341 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 342 | printf("[EasyConnect] IP address %s\n", ip_addr); |
JMF | 0:24d3eb812fd4 | 343 | } |
JMF | 0:24d3eb812fd4 | 344 | return network_interface; |
JMF | 0:24d3eb812fd4 | 345 | } |
JMF | 0:24d3eb812fd4 | 346 | |
JMF | 0:24d3eb812fd4 | 347 | /* \brief easy_connect - easy_connect function to connect the pre-defined network bearer, |
JMF | 0:24d3eb812fd4 | 348 | * config done via mbed_app.json (see README.md for details). |
JMF | 0:24d3eb812fd4 | 349 | * This version is just a helper version and uses the easy_connect() with |
JMF | 0:24d3eb812fd4 | 350 | * one parameters to do it's job. |
JMF | 0:24d3eb812fd4 | 351 | * IN: bool log_messages print out diagnostics or not. |
JMF | 0:24d3eb812fd4 | 352 | * char* WiFiSSID WiFi SSID - pointer to WiFi SSID, but if it is NULL |
JMF | 0:24d3eb812fd4 | 353 | * then MBED_CONF_APP_WIFI_SSID will be used |
JMF | 0:24d3eb812fd4 | 354 | * char* WiFiPassword WiFi Password - pointer to WiFI password, but if it's NULL |
JMF | 0:24d3eb812fd4 | 355 | * then MBED_CONF_APP_WIFI_PASSWORD will be used |
JMF | 0:24d3eb812fd4 | 356 | */ |
JMF | 0:24d3eb812fd4 | 357 | |
JMF | 0:24d3eb812fd4 | 358 | NetworkInterface* easy_connect(bool log_messages, |
JMF | 0:24d3eb812fd4 | 359 | char* WiFiSSID, |
JMF | 0:24d3eb812fd4 | 360 | char* WiFiPassword ) { |
JMF | 0:24d3eb812fd4 | 361 | |
JMF | 0:24d3eb812fd4 | 362 | // This functionality only makes sense when using WiFi |
JMF | 0:24d3eb812fd4 | 363 | #if defined (EASY_CONNECT_WIFI) |
JMF | 0:24d3eb812fd4 | 364 | // We essentially want to populate the _ssid and _password and then call easy_connect() again. |
JMF | 0:24d3eb812fd4 | 365 | if (WiFiSSID != NULL) { |
JMF | 0:24d3eb812fd4 | 366 | if(strlen(WiFiSSID) > WIFI_SSID_MAX_LEN) { |
JMF | 0:24d3eb812fd4 | 367 | printf("ERROR - WiFi SSID is too long - %d vs %d.\n", strlen(WiFiSSID), WIFI_SSID_MAX_LEN); |
JMF | 0:24d3eb812fd4 | 368 | return NULL; |
JMF | 0:24d3eb812fd4 | 369 | } |
JMF | 0:24d3eb812fd4 | 370 | _ssid = WiFiSSID; |
JMF | 0:24d3eb812fd4 | 371 | } |
JMF | 0:24d3eb812fd4 | 372 | |
JMF | 0:24d3eb812fd4 | 373 | if (WiFiPassword != NULL) { |
JMF | 0:24d3eb812fd4 | 374 | if(strlen(WiFiPassword) > WIFI_PASSWORD_MAX_LEN) { |
JMF | 0:24d3eb812fd4 | 375 | printf("ERROR - WiFi Password is too long - %d vs %d\n", strlen(WiFiPassword), WIFI_PASSWORD_MAX_LEN); |
JMF | 0:24d3eb812fd4 | 376 | return NULL; |
JMF | 0:24d3eb812fd4 | 377 | } |
JMF | 0:24d3eb812fd4 | 378 | _password = WiFiPassword; |
JMF | 0:24d3eb812fd4 | 379 | } |
JMF | 0:24d3eb812fd4 | 380 | #endif // EASY_CONNECT_WIFI |
JMF | 0:24d3eb812fd4 | 381 | return easy_connect(log_messages); |
JMF | 0:24d3eb812fd4 | 382 | } |
JMF | 0:24d3eb812fd4 | 383 | |
JMF | 0:24d3eb812fd4 | 384 | /* \brief easy_get_netif - easy_connect function to get pointer to network interface |
JMF | 0:24d3eb812fd4 | 385 | * without connecting to it. |
JMF | 0:24d3eb812fd4 | 386 | * |
JMF | 0:24d3eb812fd4 | 387 | * IN: bool log_messages print out diagnostics or not. |
JMF | 0:24d3eb812fd4 | 388 | */ |
JMF | 0:24d3eb812fd4 | 389 | NetworkInterface* easy_get_netif(bool log_messages) { |
JMF | 0:24d3eb812fd4 | 390 | #if defined (EASY_CONNECT_WIFI) |
JMF | 0:24d3eb812fd4 | 391 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 392 | printf("[EasyConnect] WiFi: %s\n", EASY_CONNECT_WIFI_TYPE); |
JMF | 0:24d3eb812fd4 | 393 | } |
JMF | 0:24d3eb812fd4 | 394 | return &wifi; |
JMF | 0:24d3eb812fd4 | 395 | |
JMF | 0:24d3eb812fd4 | 396 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
JMF | 0:24d3eb812fd4 | 397 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 398 | printf("[EasyConnect] Ethernet\n"); |
JMF | 0:24d3eb812fd4 | 399 | } |
JMF | 0:24d3eb812fd4 | 400 | return ð |
JMF | 0:24d3eb812fd4 | 401 | |
JMF | 0:24d3eb812fd4 | 402 | #elif defined (EASY_CONNECT_MESH) |
JMF | 0:24d3eb812fd4 | 403 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 404 | printf("[EasyConnect] Mesh : %s\n", EASY_CONNECT_MESH_TYPE); |
JMF | 0:24d3eb812fd4 | 405 | } |
JMF | 0:24d3eb812fd4 | 406 | return &mesh; |
JMF | 0:24d3eb812fd4 | 407 | |
JMF | 0:24d3eb812fd4 | 408 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_ONBOARD || MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR |
JMF | 0:24d3eb812fd4 | 409 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 410 | printf("[EasyConnect] Cellular\n"); |
JMF | 0:24d3eb812fd4 | 411 | } |
JMF | 0:24d3eb812fd4 | 412 | return &cellular; |
JMF | 0:24d3eb812fd4 | 413 | |
JMF | 0:24d3eb812fd4 | 414 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A |
JMF | 0:24d3eb812fd4 | 415 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 416 | printf("[EasyConnect] WNC14A2A\n"); |
JMF | 0:24d3eb812fd4 | 417 | } |
JMF | 0:24d3eb812fd4 | 418 | return &wnc; |
JMF | 0:24d3eb812fd4 | 419 | #endif |
JMF | 0:24d3eb812fd4 | 420 | } |
JMF | 0:24d3eb812fd4 | 421 | |
JMF | 0:24d3eb812fd4 | 422 | /* \brief easy_get_wifi - easy_connect function to get pointer to Wifi interface |
JMF | 0:24d3eb812fd4 | 423 | * without connecting to it. You would want this 1st so that |
JMF | 0:24d3eb812fd4 | 424 | * you can scan the APNs, choose the right one and then connect. |
JMF | 0:24d3eb812fd4 | 425 | * |
JMF | 0:24d3eb812fd4 | 426 | * IN: bool log_messages print out diagnostics or not. |
JMF | 0:24d3eb812fd4 | 427 | */ |
JMF | 0:24d3eb812fd4 | 428 | WiFiInterface* easy_get_wifi(bool log_messages) { |
JMF | 0:24d3eb812fd4 | 429 | #if defined (EASY_CONNECT_WIFI) |
JMF | 0:24d3eb812fd4 | 430 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 431 | printf("[EasyConnect] WiFi: %s\n", EASY_CONNECT_WIFI_TYPE); |
JMF | 0:24d3eb812fd4 | 432 | } |
JMF | 0:24d3eb812fd4 | 433 | return &wifi; |
JMF | 0:24d3eb812fd4 | 434 | #else |
JMF | 0:24d3eb812fd4 | 435 | if (log_messages) { |
JMF | 0:24d3eb812fd4 | 436 | printf("[EasyConnect] ERROR - Wifi not in use, can not return WifiInterface.\n"); |
JMF | 0:24d3eb812fd4 | 437 | } |
JMF | 0:24d3eb812fd4 | 438 | return NULL; |
JMF | 0:24d3eb812fd4 | 439 | #endif |
JMF | 0:24d3eb812fd4 | 440 | } |