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