123r

Dependencies:   WNC14A2AInterface

Committer:
JMF
Date:
Wed Apr 19 20:58:54 2017 +0000
Revision:
4:daf182af022b
Parent:
3:48292c226682
json file changes;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:2563b0415d1f 1 #ifndef __MAGIC_CONNECT_H__
JMF 0:2563b0415d1f 2 #define __MAGIC_CONNECT_H__
JMF 0:2563b0415d1f 3
JMF 0:2563b0415d1f 4 #include "mbed.h"
JMF 0:2563b0415d1f 5
JMF 0:2563b0415d1f 6 #define ETHERNET 1
JMF 0:2563b0415d1f 7 #define WIFI_ESP8266 2
JMF 0:2563b0415d1f 8 #define MESH_LOWPAN_ND 3
JMF 0:2563b0415d1f 9 #define MESH_THREAD 4
JMF 0:2563b0415d1f 10 #define WIFI_ODIN 5
JMF 0:2563b0415d1f 11 #define WNC14A2A 6
JMF 0:2563b0415d1f 12
JMF 0:2563b0415d1f 13 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
JMF 0:2563b0415d1f 14 #include "ESP8266Interface.h"
JMF 0:2563b0415d1f 15
JMF 0:2563b0415d1f 16 #ifdef MBED_CONF_APP_ESP8266_DEBUG
JMF 0:2563b0415d1f 17 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
JMF 0:2563b0415d1f 18 #else
JMF 0:2563b0415d1f 19 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
JMF 0:2563b0415d1f 20 #endif
JMF 0:2563b0415d1f 21
JMF 0:2563b0415d1f 22 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
JMF 0:2563b0415d1f 23 #include "OdinWiFiInterface.h"
JMF 0:2563b0415d1f 24 OdinWiFiInterface wifi;
JMF 0:2563b0415d1f 25
JMF 0:2563b0415d1f 26 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
JMF 0:2563b0415d1f 27 #include "EthernetInterface.h"
JMF 0:2563b0415d1f 28 EthernetInterface eth;
JMF 0:2563b0415d1f 29
JMF 0:2563b0415d1f 30 #elif MBED_CONF_APP_NETWORK_INTERFACE == WNC14A2A
JMF 0:2563b0415d1f 31 #include "WNC14A2AInterface.h"
JMF 0:2563b0415d1f 32 WNC14A2AInterface *wnc;
JMF 0:2563b0415d1f 33
JMF 0:2563b0415d1f 34 #if MBED_CONF_APP_WNC_DEBUG == true
JMF 3:48292c226682 35 #include "WNCDebug.h"
JMF 3:48292c226682 36 WNCDebug dbgout(stderr);
JMF 0:2563b0415d1f 37 #endif
JMF 0:2563b0415d1f 38
JMF 0:2563b0415d1f 39 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
JMF 0:2563b0415d1f 40 #define MESH
JMF 0:2563b0415d1f 41 #include "NanostackInterface.h"
JMF 0:2563b0415d1f 42 LoWPANNDInterface mesh;
JMF 0:2563b0415d1f 43 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
JMF 0:2563b0415d1f 44 #define MESH
JMF 0:2563b0415d1f 45 #include "NanostackInterface.h"
JMF 0:2563b0415d1f 46 ThreadInterface mesh;
JMF 0:2563b0415d1f 47 #else
JMF 0:2563b0415d1f 48 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
JMF 0:2563b0415d1f 49 #endif
JMF 0:2563b0415d1f 50
JMF 0:2563b0415d1f 51 #if defined(MESH)
JMF 0:2563b0415d1f 52 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
JMF 0:2563b0415d1f 53 #include "NanostackRfPhyAtmel.h"
JMF 0:2563b0415d1f 54 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
JMF 0:2563b0415d1f 55 ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
JMF 0:2563b0415d1f 56 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
JMF 0:2563b0415d1f 57 #include "NanostackRfPhyMcr20a.h"
JMF 0:2563b0415d1f 58 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
JMF 0:2563b0415d1f 59 #endif //MBED_CONF_APP_RADIO_TYPE
JMF 0:2563b0415d1f 60 #endif //MESH
JMF 0:2563b0415d1f 61
JMF 0:2563b0415d1f 62 #ifndef MESH
JMF 0:2563b0415d1f 63 // This is address to mbed Device Connector
JMF 0:2563b0415d1f 64 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
JMF 0:2563b0415d1f 65 #else
JMF 0:2563b0415d1f 66 // This is address to mbed Device Connector
JMF 0:2563b0415d1f 67 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
JMF 0:2563b0415d1f 68 #endif
JMF 0:2563b0415d1f 69
JMF 0:2563b0415d1f 70 #ifdef MBED_CONF_APP_ESP8266_SSID
JMF 0:2563b0415d1f 71 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
JMF 0:2563b0415d1f 72 #endif
JMF 0:2563b0415d1f 73
JMF 0:2563b0415d1f 74 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
JMF 0:2563b0415d1f 75 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
JMF 0:2563b0415d1f 76 #endif
JMF 0:2563b0415d1f 77
JMF 0:2563b0415d1f 78
JMF 0:2563b0415d1f 79 NetworkInterface* easy_connect(bool log_messages = false) {
JMF 0:2563b0415d1f 80 NetworkInterface* network_interface = 0;
JMF 0:2563b0415d1f 81 int connect_success = -1;
JMF 0:2563b0415d1f 82 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
JMF 0:2563b0415d1f 83 if (log_messages) {
JMF 0:2563b0415d1f 84 printf("[EasyConnect] Using WiFi (ESP8266) \n");
JMF 0:2563b0415d1f 85 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
JMF 0:2563b0415d1f 86 }
JMF 0:2563b0415d1f 87 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
JMF 0:2563b0415d1f 88 network_interface = &wifi;
JMF 0:2563b0415d1f 89 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
JMF 0:2563b0415d1f 90 if (log_messages) {
JMF 0:2563b0415d1f 91 printf("[EasyConnect] Using WiFi (ODIN) \n");
JMF 0:2563b0415d1f 92 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
JMF 0:2563b0415d1f 93 }
JMF 0:2563b0415d1f 94 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
JMF 0:2563b0415d1f 95 network_interface = &wifi;
JMF 0:2563b0415d1f 96
JMF 0:2563b0415d1f 97 #elif MBED_CONF_APP_NETWORK_INTERFACE == WNC14A2A
JMF 0:2563b0415d1f 98 if (log_messages) {
JMF 0:2563b0415d1f 99 printf("[EasyConnect] Using WNC14A2A\n");
JMF 0:2563b0415d1f 100 #if MBED_CONF_APP_WNC_DEBUG == true
JMF 0:2563b0415d1f 101 printf("[EasyConnect] with debug output\n");
JMF 0:2563b0415d1f 102 printf("[WNC Driver ] debug = %d\n",MBED_CONF_APP_WNC_DEBUG_SETTING);
JMF 3:48292c226682 103 wnc = new WNC14A2AInterface(&dbgout);
JMF 0:2563b0415d1f 104 wnc->doDebug(MBED_CONF_APP_WNC_DEBUG_SETTING);
JMF 0:2563b0415d1f 105 #else
JMF 0:2563b0415d1f 106 wnc = new WNC14A2AInterface;
JMF 0:2563b0415d1f 107 #endif
JMF 0:2563b0415d1f 108 }
JMF 0:2563b0415d1f 109 connect_success = wnc->connect();
JMF 0:2563b0415d1f 110 network_interface = wnc;
JMF 0:2563b0415d1f 111 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
JMF 0:2563b0415d1f 112 if (log_messages) {
JMF 0:2563b0415d1f 113 printf("[EasyConnect] Using Ethernet\n");
JMF 0:2563b0415d1f 114 }
JMF 0:2563b0415d1f 115 connect_success = eth.connect();
JMF 0:2563b0415d1f 116 network_interface = ð
JMF 0:2563b0415d1f 117 #endif
JMF 0:2563b0415d1f 118 #ifdef MESH
JMF 0:2563b0415d1f 119 if (log_messages) {
JMF 0:2563b0415d1f 120 printf("[EasyConnect] Using Mesh\n");
JMF 0:2563b0415d1f 121 printf("[EasyConnect] Connecting to Mesh..\n");
JMF 0:2563b0415d1f 122 }
JMF 0:2563b0415d1f 123 mesh.initialize(&rf_phy);
JMF 0:2563b0415d1f 124 connect_success = mesh.connect();
JMF 0:2563b0415d1f 125 network_interface = &mesh;
JMF 0:2563b0415d1f 126 #endif
JMF 0:2563b0415d1f 127 if(connect_success == 0) {
JMF 0:2563b0415d1f 128 if (log_messages) {
JMF 0:2563b0415d1f 129 printf("[EasyConnect] Connected to Network successfully\n");
JMF 0:2563b0415d1f 130 }
JMF 0:2563b0415d1f 131 } else {
JMF 0:2563b0415d1f 132 if (log_messages) {
JMF 0:2563b0415d1f 133 printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
JMF 0:2563b0415d1f 134 }
JMF 0:2563b0415d1f 135 return NULL;
JMF 0:2563b0415d1f 136 }
JMF 0:2563b0415d1f 137 const char *ip_addr = network_interface->get_ip_address();
JMF 0:2563b0415d1f 138 const char *mac_addr = network_interface->get_mac_address();
JMF 0:2563b0415d1f 139 if (ip_addr == NULL) {
JMF 0:2563b0415d1f 140 if (log_messages) {
JMF 0:2563b0415d1f 141 printf("[EasyConnect] ERROR - No IP address\n");
JMF 0:2563b0415d1f 142 }
JMF 0:2563b0415d1f 143 return NULL;
JMF 0:2563b0415d1f 144 }
JMF 0:2563b0415d1f 145 if (mac_addr == NULL) {
JMF 0:2563b0415d1f 146 if (log_messages) {
JMF 0:2563b0415d1f 147 printf("[EasyConnect] ERROR - No MAC address\n");
JMF 0:2563b0415d1f 148 }
JMF 0:2563b0415d1f 149 return NULL;
JMF 0:2563b0415d1f 150 }
JMF 0:2563b0415d1f 151 if (log_messages) {
JMF 0:2563b0415d1f 152 printf("[EasyConnect] IP address %s\n", ip_addr);
JMF 0:2563b0415d1f 153 printf("[EasyConnect] MAC address %s\n", mac_addr);
JMF 0:2563b0415d1f 154 }
JMF 0:2563b0415d1f 155 return network_interface;
JMF 0:2563b0415d1f 156 }
JMF 0:2563b0415d1f 157
JMF 0:2563b0415d1f 158 #endif // __MAGIC_CONNECT_H__