emw3162 driver

Fork of emw3162-driver-mbed-os-5 by Maggie Mei

Committer:
Maggie17
Date:
Tue Nov 01 02:18:18 2016 +0000
Revision:
0:2d5090e1ceb4
Child:
2:fb6251306b21
Initial commit

Who changed what in which revision?

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