Added support for WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Dependents:   http-example-wnc http-example-wnc-modified

Committer:
JMF
Date:
Wed Apr 19 01:13:10 2017 +0000
Revision:
0:2563b0415d1f
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:2563b0415d1f 1 /* ESP8266Interface Example
JMF 0:2563b0415d1f 2 * Copyright (c) 2015 ARM Limited
JMF 0:2563b0415d1f 3 *
JMF 0:2563b0415d1f 4 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:2563b0415d1f 5 * you may not use this file except in compliance with the License.
JMF 0:2563b0415d1f 6 * You may obtain a copy of the License at
JMF 0:2563b0415d1f 7 *
JMF 0:2563b0415d1f 8 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:2563b0415d1f 9 *
JMF 0:2563b0415d1f 10 * Unless required by applicable law or agreed to in writing, software
JMF 0:2563b0415d1f 11 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:2563b0415d1f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:2563b0415d1f 13 * See the License for the specific language governing permissions and
JMF 0:2563b0415d1f 14 * limitations under the License.
JMF 0:2563b0415d1f 15 */
JMF 0:2563b0415d1f 16
JMF 0:2563b0415d1f 17 #ifndef ESP8266_H
JMF 0:2563b0415d1f 18 #define ESP8266_H
JMF 0:2563b0415d1f 19
JMF 0:2563b0415d1f 20 #include "ATParser.h"
JMF 0:2563b0415d1f 21
JMF 0:2563b0415d1f 22 /** ESP8266Interface class.
JMF 0:2563b0415d1f 23 This is an interface to a ESP8266 radio.
JMF 0:2563b0415d1f 24 */
JMF 0:2563b0415d1f 25 class ESP8266
JMF 0:2563b0415d1f 26 {
JMF 0:2563b0415d1f 27 public:
JMF 0:2563b0415d1f 28 ESP8266(PinName tx, PinName rx, bool debug=false);
JMF 0:2563b0415d1f 29
JMF 0:2563b0415d1f 30 /**
JMF 0:2563b0415d1f 31 * Startup the ESP8266
JMF 0:2563b0415d1f 32 *
JMF 0:2563b0415d1f 33 * @param mode mode of WIFI 1-client, 2-host, 3-both
JMF 0:2563b0415d1f 34 * @return true only if ESP8266 was setup correctly
JMF 0:2563b0415d1f 35 */
JMF 0:2563b0415d1f 36 bool startup(int mode);
JMF 0:2563b0415d1f 37
JMF 0:2563b0415d1f 38 /**
JMF 0:2563b0415d1f 39 * Reset ESP8266
JMF 0:2563b0415d1f 40 *
JMF 0:2563b0415d1f 41 * @return true only if ESP8266 resets successfully
JMF 0:2563b0415d1f 42 */
JMF 0:2563b0415d1f 43 bool reset(void);
JMF 0:2563b0415d1f 44
JMF 0:2563b0415d1f 45 /**
JMF 0:2563b0415d1f 46 * Enable/Disable DHCP
JMF 0:2563b0415d1f 47 *
JMF 0:2563b0415d1f 48 * @param enabled DHCP enabled when true
JMF 0:2563b0415d1f 49 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
JMF 0:2563b0415d1f 50 * @return true only if ESP8266 enables/disables DHCP successfully
JMF 0:2563b0415d1f 51 */
JMF 0:2563b0415d1f 52 bool dhcp(bool enabled, int mode);
JMF 0:2563b0415d1f 53
JMF 0:2563b0415d1f 54 /**
JMF 0:2563b0415d1f 55 * Connect ESP8266 to AP
JMF 0:2563b0415d1f 56 *
JMF 0:2563b0415d1f 57 * @param ap the name of the AP
JMF 0:2563b0415d1f 58 * @param passPhrase the password of AP
JMF 0:2563b0415d1f 59 * @return true only if ESP8266 is connected successfully
JMF 0:2563b0415d1f 60 */
JMF 0:2563b0415d1f 61 bool connect(const char *ap, const char *passPhrase);
JMF 0:2563b0415d1f 62
JMF 0:2563b0415d1f 63 /**
JMF 0:2563b0415d1f 64 * Disconnect ESP8266 from AP
JMF 0:2563b0415d1f 65 *
JMF 0:2563b0415d1f 66 * @return true only if ESP8266 is disconnected successfully
JMF 0:2563b0415d1f 67 */
JMF 0:2563b0415d1f 68 bool disconnect(void);
JMF 0:2563b0415d1f 69
JMF 0:2563b0415d1f 70 /**
JMF 0:2563b0415d1f 71 * Get the IP address of ESP8266
JMF 0:2563b0415d1f 72 *
JMF 0:2563b0415d1f 73 * @return null-teriminated IP address or null if no IP address is assigned
JMF 0:2563b0415d1f 74 */
JMF 0:2563b0415d1f 75 const char *getIPAddress(void);
JMF 0:2563b0415d1f 76
JMF 0:2563b0415d1f 77 /**
JMF 0:2563b0415d1f 78 * Get the MAC address of ESP8266
JMF 0:2563b0415d1f 79 *
JMF 0:2563b0415d1f 80 * @return null-terminated MAC address or null if no MAC address is assigned
JMF 0:2563b0415d1f 81 */
JMF 0:2563b0415d1f 82 const char *getMACAddress(void);
JMF 0:2563b0415d1f 83
JMF 0:2563b0415d1f 84 /** Get the local gateway
JMF 0:2563b0415d1f 85 *
JMF 0:2563b0415d1f 86 * @return Null-terminated representation of the local gateway
JMF 0:2563b0415d1f 87 * or null if no network mask has been recieved
JMF 0:2563b0415d1f 88 */
JMF 0:2563b0415d1f 89 const char *getGateway();
JMF 0:2563b0415d1f 90
JMF 0:2563b0415d1f 91 /** Get the local network mask
JMF 0:2563b0415d1f 92 *
JMF 0:2563b0415d1f 93 * @return Null-terminated representation of the local network mask
JMF 0:2563b0415d1f 94 * or null if no network mask has been recieved
JMF 0:2563b0415d1f 95 */
JMF 0:2563b0415d1f 96 const char *getNetmask();
JMF 0:2563b0415d1f 97
JMF 0:2563b0415d1f 98 /* Return RSSI for active connection
JMF 0:2563b0415d1f 99 *
JMF 0:2563b0415d1f 100 * @return Measured RSSI
JMF 0:2563b0415d1f 101 */
JMF 0:2563b0415d1f 102 int8_t getRSSI();
JMF 0:2563b0415d1f 103
JMF 0:2563b0415d1f 104 /**
JMF 0:2563b0415d1f 105 * Check if ESP8266 is conenected
JMF 0:2563b0415d1f 106 *
JMF 0:2563b0415d1f 107 * @return true only if the chip has an IP address
JMF 0:2563b0415d1f 108 */
JMF 0:2563b0415d1f 109 bool isConnected(void);
JMF 0:2563b0415d1f 110
JMF 0:2563b0415d1f 111 /** Scan for available networks
JMF 0:2563b0415d1f 112 *
JMF 0:2563b0415d1f 113 * @param ap Pointer to allocated array to store discovered AP
JMF 0:2563b0415d1f 114 * @param limit Size of allocated @a res array, or 0 to only count available AP
JMF 0:2563b0415d1f 115 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
JMF 0:2563b0415d1f 116 * see @a nsapi_error
JMF 0:2563b0415d1f 117 */
JMF 0:2563b0415d1f 118 int scan(WiFiAccessPoint *res, unsigned limit);
JMF 0:2563b0415d1f 119
JMF 0:2563b0415d1f 120 /**
JMF 0:2563b0415d1f 121 * Open a socketed connection
JMF 0:2563b0415d1f 122 *
JMF 0:2563b0415d1f 123 * @param type the type of socket to open "UDP" or "TCP"
JMF 0:2563b0415d1f 124 * @param id id to give the new socket, valid 0-4
JMF 0:2563b0415d1f 125 * @param port port to open connection with
JMF 0:2563b0415d1f 126 * @param addr the IP address of the destination
JMF 0:2563b0415d1f 127 * @return true only if socket opened successfully
JMF 0:2563b0415d1f 128 */
JMF 0:2563b0415d1f 129 bool open(const char *type, int id, const char* addr, int port);
JMF 0:2563b0415d1f 130
JMF 0:2563b0415d1f 131 /**
JMF 0:2563b0415d1f 132 * Sends data to an open socket
JMF 0:2563b0415d1f 133 *
JMF 0:2563b0415d1f 134 * @param id id of socket to send to
JMF 0:2563b0415d1f 135 * @param data data to be sent
JMF 0:2563b0415d1f 136 * @param amount amount of data to be sent - max 1024
JMF 0:2563b0415d1f 137 * @return true only if data sent successfully
JMF 0:2563b0415d1f 138 */
JMF 0:2563b0415d1f 139 bool send(int id, const void *data, uint32_t amount);
JMF 0:2563b0415d1f 140
JMF 0:2563b0415d1f 141 /**
JMF 0:2563b0415d1f 142 * Receives data from an open socket
JMF 0:2563b0415d1f 143 *
JMF 0:2563b0415d1f 144 * @param id id to receive from
JMF 0:2563b0415d1f 145 * @param data placeholder for returned information
JMF 0:2563b0415d1f 146 * @param amount number of bytes to be received
JMF 0:2563b0415d1f 147 * @return the number of bytes received
JMF 0:2563b0415d1f 148 */
JMF 0:2563b0415d1f 149 int32_t recv(int id, void *data, uint32_t amount);
JMF 0:2563b0415d1f 150
JMF 0:2563b0415d1f 151 /**
JMF 0:2563b0415d1f 152 * Closes a socket
JMF 0:2563b0415d1f 153 *
JMF 0:2563b0415d1f 154 * @param id id of socket to close, valid only 0-4
JMF 0:2563b0415d1f 155 * @return true only if socket is closed successfully
JMF 0:2563b0415d1f 156 */
JMF 0:2563b0415d1f 157 bool close(int id);
JMF 0:2563b0415d1f 158
JMF 0:2563b0415d1f 159 /**
JMF 0:2563b0415d1f 160 * Allows timeout to be changed between commands
JMF 0:2563b0415d1f 161 *
JMF 0:2563b0415d1f 162 * @param timeout_ms timeout of the connection
JMF 0:2563b0415d1f 163 */
JMF 0:2563b0415d1f 164 void setTimeout(uint32_t timeout_ms);
JMF 0:2563b0415d1f 165
JMF 0:2563b0415d1f 166 /**
JMF 0:2563b0415d1f 167 * Checks if data is available
JMF 0:2563b0415d1f 168 */
JMF 0:2563b0415d1f 169 bool readable();
JMF 0:2563b0415d1f 170
JMF 0:2563b0415d1f 171 /**
JMF 0:2563b0415d1f 172 * Checks if data can be written
JMF 0:2563b0415d1f 173 */
JMF 0:2563b0415d1f 174 bool writeable();
JMF 0:2563b0415d1f 175
JMF 0:2563b0415d1f 176 /**
JMF 0:2563b0415d1f 177 * Attach a function to call whenever network state has changed
JMF 0:2563b0415d1f 178 *
JMF 0:2563b0415d1f 179 * @param func A pointer to a void function, or 0 to set as none
JMF 0:2563b0415d1f 180 */
JMF 0:2563b0415d1f 181 void attach(Callback<void()> func);
JMF 0:2563b0415d1f 182
JMF 0:2563b0415d1f 183 /**
JMF 0:2563b0415d1f 184 * Attach a function to call whenever network state has changed
JMF 0:2563b0415d1f 185 *
JMF 0:2563b0415d1f 186 * @param obj pointer to the object to call the member function on
JMF 0:2563b0415d1f 187 * @param method pointer to the member function to call
JMF 0:2563b0415d1f 188 */
JMF 0:2563b0415d1f 189 template <typename T, typename M>
JMF 0:2563b0415d1f 190 void attach(T *obj, M method) {
JMF 0:2563b0415d1f 191 attach(Callback<void()>(obj, method));
JMF 0:2563b0415d1f 192 }
JMF 0:2563b0415d1f 193
JMF 0:2563b0415d1f 194 private:
JMF 0:2563b0415d1f 195 BufferedSerial _serial;
JMF 0:2563b0415d1f 196 ATParser _parser;
JMF 0:2563b0415d1f 197
JMF 0:2563b0415d1f 198 struct packet {
JMF 0:2563b0415d1f 199 struct packet *next;
JMF 0:2563b0415d1f 200 int id;
JMF 0:2563b0415d1f 201 uint32_t len;
JMF 0:2563b0415d1f 202 // data follows
JMF 0:2563b0415d1f 203 } *_packets, **_packets_end;
JMF 0:2563b0415d1f 204 void _packet_handler();
JMF 0:2563b0415d1f 205 bool recv_ap(nsapi_wifi_ap_t *ap);
JMF 0:2563b0415d1f 206
JMF 0:2563b0415d1f 207 char _ip_buffer[16];
JMF 0:2563b0415d1f 208 char _gateway_buffer[16];
JMF 0:2563b0415d1f 209 char _netmask_buffer[16];
JMF 0:2563b0415d1f 210 char _mac_buffer[18];
JMF 0:2563b0415d1f 211 };
JMF 0:2563b0415d1f 212
JMF 0:2563b0415d1f 213 #endif