http

Committer:
DuaaAbusharkh
Date:
Mon Apr 15 08:30:22 2019 +0000
Revision:
0:a49e37a83a7a
blink

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuaaAbusharkh 0:a49e37a83a7a 1 #ifndef __EASY_CONNECT_H__
DuaaAbusharkh 0:a49e37a83a7a 2 #define __EASY_CONNECT_H__
DuaaAbusharkh 0:a49e37a83a7a 3
DuaaAbusharkh 0:a49e37a83a7a 4 #include "mbed.h"
DuaaAbusharkh 0:a49e37a83a7a 5
DuaaAbusharkh 0:a49e37a83a7a 6 #define ETHERNET 1
DuaaAbusharkh 0:a49e37a83a7a 7 #define WIFI_ESP8266 2
DuaaAbusharkh 0:a49e37a83a7a 8 #define MESH_LOWPAN_ND 3
DuaaAbusharkh 0:a49e37a83a7a 9 #define MESH_THREAD 4
DuaaAbusharkh 0:a49e37a83a7a 10 #define WIFI_ODIN 5
DuaaAbusharkh 0:a49e37a83a7a 11 #define WIFI_REALTEK 6
DuaaAbusharkh 0:a49e37a83a7a 12
DuaaAbusharkh 0:a49e37a83a7a 13 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
DuaaAbusharkh 0:a49e37a83a7a 14 #include "ESP8266Interface.h"
DuaaAbusharkh 0:a49e37a83a7a 15
DuaaAbusharkh 0:a49e37a83a7a 16 #ifdef MBED_CONF_APP_ESP8266_DEBUG
DuaaAbusharkh 0:a49e37a83a7a 17 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
DuaaAbusharkh 0:a49e37a83a7a 18 #else
DuaaAbusharkh 0:a49e37a83a7a 19 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
DuaaAbusharkh 0:a49e37a83a7a 20 #endif
DuaaAbusharkh 0:a49e37a83a7a 21
DuaaAbusharkh 0:a49e37a83a7a 22 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
DuaaAbusharkh 0:a49e37a83a7a 23 #include "OdinWiFiInterface.h"
DuaaAbusharkh 0:a49e37a83a7a 24
DuaaAbusharkh 0:a49e37a83a7a 25 OdinWiFiInterface wifi;
DuaaAbusharkh 0:a49e37a83a7a 26 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
DuaaAbusharkh 0:a49e37a83a7a 27 #include "RTWInterface.h"
DuaaAbusharkh 0:a49e37a83a7a 28 RTWInterface wifi;
DuaaAbusharkh 0:a49e37a83a7a 29 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
DuaaAbusharkh 0:a49e37a83a7a 30 #include "EthernetInterface.h"
DuaaAbusharkh 0:a49e37a83a7a 31 EthernetInterface eth;
DuaaAbusharkh 0:a49e37a83a7a 32 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
DuaaAbusharkh 0:a49e37a83a7a 33 #define MESH
DuaaAbusharkh 0:a49e37a83a7a 34 #include "NanostackInterface.h"
DuaaAbusharkh 0:a49e37a83a7a 35 LoWPANNDInterface mesh;
DuaaAbusharkh 0:a49e37a83a7a 36 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
DuaaAbusharkh 0:a49e37a83a7a 37 #define MESH
DuaaAbusharkh 0:a49e37a83a7a 38 #include "NanostackInterface.h"
DuaaAbusharkh 0:a49e37a83a7a 39 ThreadInterface mesh;
DuaaAbusharkh 0:a49e37a83a7a 40 #else
DuaaAbusharkh 0:a49e37a83a7a 41 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
DuaaAbusharkh 0:a49e37a83a7a 42 #endif
DuaaAbusharkh 0:a49e37a83a7a 43
DuaaAbusharkh 0:a49e37a83a7a 44 #if defined(MESH)
DuaaAbusharkh 0:a49e37a83a7a 45 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
DuaaAbusharkh 0:a49e37a83a7a 46 #include "NanostackRfPhyAtmel.h"
DuaaAbusharkh 0:a49e37a83a7a 47 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
DuaaAbusharkh 0:a49e37a83a7a 48 ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
DuaaAbusharkh 0:a49e37a83a7a 49 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
DuaaAbusharkh 0:a49e37a83a7a 50 #include "NanostackRfPhyMcr20a.h"
DuaaAbusharkh 0:a49e37a83a7a 51 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
DuaaAbusharkh 0:a49e37a83a7a 52 #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1
DuaaAbusharkh 0:a49e37a83a7a 53 #include "NanostackRfPhySpirit1.h"
DuaaAbusharkh 0:a49e37a83a7a 54 NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK,
DuaaAbusharkh 0:a49e37a83a7a 55 SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED);
DuaaAbusharkh 0:a49e37a83a7a 56 #elif MBED_CONF_APP_MESH_RADIO_TYPE == EFR32
DuaaAbusharkh 0:a49e37a83a7a 57 #include "NanostackRfPhyEfr32.h"
DuaaAbusharkh 0:a49e37a83a7a 58 NanostackRfPhyEfr32 rf_phy;
DuaaAbusharkh 0:a49e37a83a7a 59 #endif //MBED_CONF_APP_RADIO_TYPE
DuaaAbusharkh 0:a49e37a83a7a 60 #endif //MESH
DuaaAbusharkh 0:a49e37a83a7a 61
DuaaAbusharkh 0:a49e37a83a7a 62 #ifndef MESH
DuaaAbusharkh 0:a49e37a83a7a 63 // This is address to mbed Device Connector
DuaaAbusharkh 0:a49e37a83a7a 64 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
DuaaAbusharkh 0:a49e37a83a7a 65 #else
DuaaAbusharkh 0:a49e37a83a7a 66 // This is address to mbed Device Connector
DuaaAbusharkh 0:a49e37a83a7a 67 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
DuaaAbusharkh 0:a49e37a83a7a 68 #endif
DuaaAbusharkh 0:a49e37a83a7a 69
DuaaAbusharkh 0:a49e37a83a7a 70 #ifdef MBED_CONF_APP_ESP8266_SSID
DuaaAbusharkh 0:a49e37a83a7a 71 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
DuaaAbusharkh 0:a49e37a83a7a 72 #endif
DuaaAbusharkh 0:a49e37a83a7a 73
DuaaAbusharkh 0:a49e37a83a7a 74 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
DuaaAbusharkh 0:a49e37a83a7a 75 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
DuaaAbusharkh 0:a49e37a83a7a 76 #endif
DuaaAbusharkh 0:a49e37a83a7a 77
DuaaAbusharkh 0:a49e37a83a7a 78 /* \brief print_MAC - print_MAC - helper function to print out MAC address
DuaaAbusharkh 0:a49e37a83a7a 79 * in: network_interface - pointer to network i/f
DuaaAbusharkh 0:a49e37a83a7a 80 * bool log-messages print out logs or not
DuaaAbusharkh 0:a49e37a83a7a 81 * MAC address is print, if it can be acquired & log_messages is true.
DuaaAbusharkh 0:a49e37a83a7a 82 *
DuaaAbusharkh 0:a49e37a83a7a 83 */
DuaaAbusharkh 0:a49e37a83a7a 84 void print_MAC(NetworkInterface* network_interface, bool log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 85 const char *mac_addr = network_interface->get_mac_address();
DuaaAbusharkh 0:a49e37a83a7a 86 if (mac_addr == NULL) {
DuaaAbusharkh 0:a49e37a83a7a 87 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 88 printf("[EasyConnect] ERROR - No MAC address\n");
DuaaAbusharkh 0:a49e37a83a7a 89 }
DuaaAbusharkh 0:a49e37a83a7a 90 return;
DuaaAbusharkh 0:a49e37a83a7a 91 }
DuaaAbusharkh 0:a49e37a83a7a 92 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 93 printf("[EasyConnect] MAC address %s\n", mac_addr);
DuaaAbusharkh 0:a49e37a83a7a 94 }
DuaaAbusharkh 0:a49e37a83a7a 95 }
DuaaAbusharkh 0:a49e37a83a7a 96
DuaaAbusharkh 0:a49e37a83a7a 97
DuaaAbusharkh 0:a49e37a83a7a 98 /* \brief easy_connect - easy_connect function to connect the pre-defined network bearer,
DuaaAbusharkh 0:a49e37a83a7a 99 * config done via mbed_app.json (see README.md for details).
DuaaAbusharkh 0:a49e37a83a7a 100 * IN: bool log_messages print out diagnostics or not.
DuaaAbusharkh 0:a49e37a83a7a 101 *
DuaaAbusharkh 0:a49e37a83a7a 102 */
DuaaAbusharkh 0:a49e37a83a7a 103 NetworkInterface* easy_connect(bool log_messages = false) {
DuaaAbusharkh 0:a49e37a83a7a 104 NetworkInterface* network_interface = 0;
DuaaAbusharkh 0:a49e37a83a7a 105 int connect_success = -1;
DuaaAbusharkh 0:a49e37a83a7a 106 /// This should be removed once mbedOS supports proper dual-stack
DuaaAbusharkh 0:a49e37a83a7a 107 #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
DuaaAbusharkh 0:a49e37a83a7a 108 printf("[EasyConnect] IPv6 mode\n");
DuaaAbusharkh 0:a49e37a83a7a 109 #else
DuaaAbusharkh 0:a49e37a83a7a 110 printf("[EasyConnect] IPv4 mode\n");
DuaaAbusharkh 0:a49e37a83a7a 111 #endif
DuaaAbusharkh 0:a49e37a83a7a 112
DuaaAbusharkh 0:a49e37a83a7a 113 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
DuaaAbusharkh 0:a49e37a83a7a 114 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 115 printf("[EasyConnect] Using WiFi (ESP8266) \n");
DuaaAbusharkh 0:a49e37a83a7a 116 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
DuaaAbusharkh 0:a49e37a83a7a 117 }
DuaaAbusharkh 0:a49e37a83a7a 118 network_interface = &wifi;
DuaaAbusharkh 0:a49e37a83a7a 119 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
DuaaAbusharkh 0:a49e37a83a7a 120 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
DuaaAbusharkh 0:a49e37a83a7a 121 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 122 printf("[EasyConnect] Using WiFi (ODIN) \n");
DuaaAbusharkh 0:a49e37a83a7a 123 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
DuaaAbusharkh 0:a49e37a83a7a 124 }
DuaaAbusharkh 0:a49e37a83a7a 125 network_interface = &wifi;
DuaaAbusharkh 0:a49e37a83a7a 126 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
DuaaAbusharkh 0:a49e37a83a7a 127 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
DuaaAbusharkh 0:a49e37a83a7a 128 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 129 printf("[EasyConnect] Using WiFi (RTW)\n");
DuaaAbusharkh 0:a49e37a83a7a 130 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
DuaaAbusharkh 0:a49e37a83a7a 131 }
DuaaAbusharkh 0:a49e37a83a7a 132 network_interface = &wifi;
DuaaAbusharkh 0:a49e37a83a7a 133 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
DuaaAbusharkh 0:a49e37a83a7a 134 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
DuaaAbusharkh 0:a49e37a83a7a 135 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 136 printf("[EasyConnect] Using Ethernet\n");
DuaaAbusharkh 0:a49e37a83a7a 137 }
DuaaAbusharkh 0:a49e37a83a7a 138 network_interface = ð
DuaaAbusharkh 0:a49e37a83a7a 139 connect_success = eth.connect();
DuaaAbusharkh 0:a49e37a83a7a 140 #endif
DuaaAbusharkh 0:a49e37a83a7a 141
DuaaAbusharkh 0:a49e37a83a7a 142 #ifdef MESH
DuaaAbusharkh 0:a49e37a83a7a 143 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 144 printf("[EasyConnect] Using Mesh\n");
DuaaAbusharkh 0:a49e37a83a7a 145 printf("[EasyConnect] Connecting to Mesh..\n");
DuaaAbusharkh 0:a49e37a83a7a 146 }
DuaaAbusharkh 0:a49e37a83a7a 147 network_interface = &mesh;
DuaaAbusharkh 0:a49e37a83a7a 148 mesh.initialize(&rf_phy);
DuaaAbusharkh 0:a49e37a83a7a 149 connect_success = mesh.connect();
DuaaAbusharkh 0:a49e37a83a7a 150 #endif
DuaaAbusharkh 0:a49e37a83a7a 151 if(connect_success == 0) {
DuaaAbusharkh 0:a49e37a83a7a 152 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 153 printf("[EasyConnect] Connected to Network successfully\n");
DuaaAbusharkh 0:a49e37a83a7a 154 print_MAC(network_interface, log_messages);
DuaaAbusharkh 0:a49e37a83a7a 155 }
DuaaAbusharkh 0:a49e37a83a7a 156 } else {
DuaaAbusharkh 0:a49e37a83a7a 157 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 158 print_MAC(network_interface, log_messages);
DuaaAbusharkh 0:a49e37a83a7a 159 printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
DuaaAbusharkh 0:a49e37a83a7a 160 }
DuaaAbusharkh 0:a49e37a83a7a 161 return NULL;
DuaaAbusharkh 0:a49e37a83a7a 162 }
DuaaAbusharkh 0:a49e37a83a7a 163 const char *ip_addr = network_interface->get_ip_address();
DuaaAbusharkh 0:a49e37a83a7a 164 if (ip_addr == NULL) {
DuaaAbusharkh 0:a49e37a83a7a 165 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 166 printf("[EasyConnect] ERROR - No IP address\n");
DuaaAbusharkh 0:a49e37a83a7a 167 }
DuaaAbusharkh 0:a49e37a83a7a 168 return NULL;
DuaaAbusharkh 0:a49e37a83a7a 169 }
DuaaAbusharkh 0:a49e37a83a7a 170 if (log_messages) {
DuaaAbusharkh 0:a49e37a83a7a 171 printf("[EasyConnect] IP address %s\n", ip_addr);
DuaaAbusharkh 0:a49e37a83a7a 172 }
DuaaAbusharkh 0:a49e37a83a7a 173 return network_interface;
DuaaAbusharkh 0:a49e37a83a7a 174 }
DuaaAbusharkh 0:a49e37a83a7a 175
DuaaAbusharkh 0:a49e37a83a7a 176 #endif // __EASY_CONNECT_H__