emw3162 driver for mbed os 5
Fork of emw3162-driver by
Revision 2:fb6251306b21, committed 2016-11-14
- Comitter:
- Maggie17
- Date:
- Mon Nov 14 02:30:49 2016 +0000
- Parent:
- 1:3c8bed04849d
- Child:
- 3:a6f80a95b039
- Commit message:
- Change the API name from ESP8266 to EMW3162
Changed in this revision
--- a/EMW3162Interface.cpp Fri Nov 04 02:14:19 2016 +0000 +++ b/EMW3162Interface.cpp Mon Nov 14 02:30:49 2016 +0000 @@ -1,4 +1,4 @@ -/* ESP8266 implementation of NetworkInterfaceAPI +/* EMW3162 implementation of NetworkInterfaceAPI * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,29 +17,29 @@ #include "EMW3162Interface.h" -// Various timeouts for different ESP8266 operations -#define ESP8266_CONNECT_TIMEOUT 15000 -#define ESP8266_SEND_TIMEOUT 500 -#define ESP8266_RECV_TIMEOUT 0 -#define ESP8266_MISC_TIMEOUT 500 +// Various timeouts for different EMW3162 operations +#define EMW3162_CONNECT_TIMEOUT 15000 +#define EMW3162_SEND_TIMEOUT 500 +#define EMW3162_RECV_TIMEOUT 0 +#define EMW3162_MISC_TIMEOUT 500 -// ESP8266Interface implementation -ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) +// EMW3162Interface implementation +EMW3162Interface::EMW3162Interface(PinName tx, PinName rx, bool debug) : _esp(tx, rx, debug) { memset(_ids, 0, sizeof(_ids)); memset(_cbs, 0, sizeof(_cbs)); - _esp.attach(this, &ESP8266Interface::event); + _esp.attach(this, &EMW3162Interface::event); } -int ESP8266Interface::connect( +int EMW3162Interface::connect( const char *ssid, const char *pass, nsapi_security_t security) { - _esp.setTimeout(ESP8266_CONNECT_TIMEOUT); + _esp.setTimeout(EMW3162_CONNECT_TIMEOUT); if (!_esp.startup()) { return NSAPI_ERROR_DEVICE_ERROR; @@ -60,9 +60,9 @@ return 0; } -int ESP8266Interface::disconnect() +int EMW3162Interface::disconnect() { - _esp.setTimeout(ESP8266_MISC_TIMEOUT); + _esp.setTimeout(EMW3162_MISC_TIMEOUT); if (!_esp.disconnect()) { return NSAPI_ERROR_DEVICE_ERROR; @@ -71,29 +71,29 @@ return 0; } -const char* ESP8266Interface::get_ip_address() +const char* EMW3162Interface::get_ip_address() { return _esp.getIPAddress(); } -const char* ESP8266Interface::get_mac_address() +const char* EMW3162Interface::get_mac_address() { return _esp.getMACAddress(); } -struct esp8266_socket { +struct EMW3162_socket { int id; int socketId; nsapi_protocol_t proto; bool connected; }; -int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto) +int EMW3162Interface::socket_open(void **handle, nsapi_protocol_t proto) { // Look for an unused socket int id = -1; - for (int i = 1; i < ESP8266_SOCKET_COUNT; i++) { + for (int i = 1; i < EMW3162_SOCKET_COUNT; i++) { if (!_ids[i]) { id = i; _ids[i] = true; @@ -105,7 +105,7 @@ return NSAPI_ERROR_NO_SOCKET; } - struct esp8266_socket *socket = new struct esp8266_socket; + struct EMW3162_socket *socket = new struct EMW3162_socket; if (!socket) { return NSAPI_ERROR_NO_SOCKET; } @@ -118,11 +118,11 @@ return 0; } -int ESP8266Interface::socket_close(void *handle) +int EMW3162Interface::socket_close(void *handle) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; int err = 0; - _esp.setTimeout(ESP8266_MISC_TIMEOUT); + _esp.setTimeout(EMW3162_MISC_TIMEOUT); if (!_esp.close(socket->socketId)) { err = NSAPI_ERROR_DEVICE_ERROR; @@ -133,20 +133,20 @@ return err; } -int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address) +int EMW3162Interface::socket_bind(void *handle, const SocketAddress &address) { return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_listen(void *handle, int backlog) +int EMW3162Interface::socket_listen(void *handle, int backlog) { return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr) +int EMW3162Interface::socket_connect(void *handle, const SocketAddress &addr) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; - _esp.setTimeout(ESP8266_MISC_TIMEOUT); + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; + _esp.setTimeout(EMW3162_MISC_TIMEOUT); const char *proto = (socket->proto == NSAPI_UDP) ? "UNICAST" : "CLIENT"; socket -> socketId = _esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port()); @@ -158,15 +158,15 @@ return 0; } -int ESP8266Interface::socket_accept(void **handle, void *server) +int EMW3162Interface::socket_accept(void **handle, void *server) { return NSAPI_ERROR_UNSUPPORTED; } -int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) +int EMW3162Interface::socket_send(void *handle, const void *data, unsigned size) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; - _esp.setTimeout(ESP8266_SEND_TIMEOUT); + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; + _esp.setTimeout(EMW3162_SEND_TIMEOUT); if (!_esp.send(socket->socketId, data, size)) { return NSAPI_ERROR_DEVICE_ERROR; @@ -175,10 +175,10 @@ return size; } -int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size) +int EMW3162Interface::socket_recv(void *handle, void *data, unsigned size) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; - _esp.setTimeout(ESP8266_RECV_TIMEOUT); + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; + _esp.setTimeout(EMW3162_RECV_TIMEOUT); int32_t recv = _esp.recv(socket->socketId, data, size); if (recv < 0) { @@ -188,9 +188,9 @@ return recv; } -int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) +int EMW3162Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; if (!socket->connected) { int err = socket_connect(socket, addr); if (err < 0) { @@ -201,21 +201,21 @@ return socket_send(socket, data, size); } -int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) +int EMW3162Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; return socket_recv(socket, data, size); } -void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data) +void EMW3162Interface::socket_attach(void *handle, void (*callback)(void *), void *data) { - struct esp8266_socket *socket = (struct esp8266_socket *)handle; + struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; _cbs[socket->id].callback = callback; _cbs[socket->id].data = data; } -void ESP8266Interface::event() { - for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { +void EMW3162Interface::event() { + for (int i = 0; i < EMW3162_SOCKET_COUNT; i++) { if (_cbs[i].callback) { _cbs[i].callback(_cbs[i].data); }
--- a/EMW3162Interface.h Fri Nov 04 02:14:19 2016 +0000 +++ b/EMW3162Interface.h Mon Nov 14 02:30:49 2016 +0000 @@ -1,4 +1,4 @@ -/* ESP8266 implementation of NetworkInterfaceAPI +/* EMW3162 implementation of NetworkInterfaceAPI * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,28 +14,28 @@ * limitations under the License. */ -#ifndef ESP8266_INTERFACE_H -#define ESP8266_INTERFACE_H +#ifndef EMW3162_INTERFACE_H +#define EMW3162_INTERFACE_H #include "NetworkSocketAPI/NetworkStack.h" #include "NetworkSocketAPI/WiFiInterface.h" #include "EMW3162.h" -#define ESP8266_SOCKET_COUNT 5 +#define EMW3162_SOCKET_COUNT 5 -/** ESP8266Interface class - * Implementation of the NetworkStack for the ESP8266 +/** EMW3162Interface class + * Implementation of the NetworkStack for the EMW3162 */ -class ESP8266Interface : public NetworkStack, public WiFiInterface +class EMW3162Interface : public NetworkStack, public WiFiInterface { public: - /** ESP8266Interface lifetime + /** EMW3162Interface lifetime * @param tx TX pin * @param rx RX pin * @param debug Enable debugging */ - ESP8266Interface(PinName tx, PinName rx, bool debug = false); + EMW3162Interface(PinName tx, PinName rx, bool debug = false); /** Start the interface * @@ -176,14 +176,14 @@ } private: - ESP8266 _esp; - bool _ids[ESP8266_SOCKET_COUNT]; + EMW3162 _esp; + bool _ids[EMW3162_SOCKET_COUNT]; void event(); struct { void (*callback)(void *); void *data; - } _cbs[ESP8266_SOCKET_COUNT]; + } _cbs[EMW3162_SOCKET_COUNT]; };
--- a/emw3162/EMW3162.cpp Fri Nov 04 02:14:19 2016 +0000 +++ b/emw3162/EMW3162.cpp Mon Nov 14 02:30:49 2016 +0000 @@ -1,4 +1,4 @@ -/* ESP8266 Example +/* EMW3162 Example * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +16,7 @@ #include "EMW3162.h" -ESP8266::ESP8266(PinName tx, PinName rx, bool debug) +EMW3162::EMW3162(PinName tx, PinName rx, bool debug) : _serial(tx, rx, 1024), _parser(_serial) , _packets(0), _packets_end(&_packets) { @@ -24,16 +24,16 @@ _parser.debugOn(debug); } -bool ESP8266::startup(void) +bool EMW3162::startup(void) { bool success = reset("FACTORY"); - _parser.oob("+EVENT=SOCKET", this, &ESP8266::_packet_handler); + _parser.oob("+EVENT=SOCKET", this, &EMW3162::_packet_handler); return success; } -bool ESP8266::reset(const char *reset) +bool EMW3162::reset(const char *reset) { for (int i = 0; i < 2; i++) { if (_parser.send("AT+%s", reset) @@ -45,13 +45,13 @@ return false; } -bool ESP8266::dhcp(bool enabled) +bool EMW3162::dhcp(bool enabled) { return _parser.send("AT+DHCP=%s", enabled ? "ON":"OFF") && _parser.recv("+OK"); } -bool ESP8266::connect(const char *ap, const char *passPhrase) +bool EMW3162::connect(const char *ap, const char *passPhrase) { return _parser.send("AT+WSTA=%s,%s", ap, passPhrase) && _parser.recv("+OK") @@ -60,7 +60,7 @@ && _parser.recv("+EVENT=WIFI_LINK,STATION_UP"); } -bool ESP8266::disconnect(void) +bool EMW3162::disconnect(void) { return _parser.send("AT+WLANF=STA,OFF") @@ -70,7 +70,7 @@ && _parser.recv("+OK"); } -const char *ESP8266::getIPAddress(void) +const char *EMW3162::getIPAddress(void) { if (!(_parser.send("AT+IPCONFIG") && _parser.recv("%*[^,],%*[^,],%*[^,],%[^,]%*[^#]#", _ip_buffer))) { @@ -80,7 +80,7 @@ return _ip_buffer; } -const char *ESP8266::getMACAddress(void) +const char *EMW3162::getMACAddress(void) { if (!(_parser.send("AT+WMAC") && _parser.recv("%*[^=]=%[^#]#", _mac_buffer))) { @@ -90,12 +90,12 @@ return _mac_buffer; } -bool ESP8266::isConnected(void) +bool EMW3162::isConnected(void) { return getIPAddress() != 0; } -int ESP8266::open(const char *type, int id, const char* addr, int port) +int EMW3162::open(const char *type, int id, const char* addr, int port) { int state1 = 0, state2 = 0; state1 = _parser.send("AT+CON1=%s,%d,%d,%s", type, id, port, addr) @@ -119,7 +119,7 @@ return -1; } -bool ESP8266::send(int id, const void *data, uint32_t amount) +bool EMW3162::send(int id, const void *data, uint32_t amount) { //May take a second try if device is busy for (unsigned i = 0; i < 2; i++) { @@ -135,7 +135,7 @@ return false; } -void ESP8266::_packet_handler() +void EMW3162::_packet_handler() { int id; uint32_t amount; @@ -165,7 +165,7 @@ _packets_end = &packet->next; } -int32_t ESP8266::recv(int id, void *data, uint32_t amount) +int32_t EMW3162::recv(int id, void *data, uint32_t amount) { while (true) { // check if any packets are ready for us @@ -202,7 +202,7 @@ } } -bool ESP8266::close(int id) +bool EMW3162::close(int id) { //May take a second try if device is busy for (unsigned i = 0; i < 2; i++) { @@ -219,22 +219,22 @@ return false; } -void ESP8266::setTimeout(uint32_t timeout_ms) +void EMW3162::setTimeout(uint32_t timeout_ms) { _parser.setTimeout(timeout_ms); } -bool ESP8266::readable() +bool EMW3162::readable() { return _serial.readable(); } -bool ESP8266::writeable() +bool EMW3162::writeable() { return _serial.writeable(); } -void ESP8266::attach(Callback<void()> func) +void EMW3162::attach(Callback<void()> func) { _serial.attach(func); }
--- a/emw3162/EMW3162.h Fri Nov 04 02:14:19 2016 +0000 +++ b/emw3162/EMW3162.h Mon Nov 14 02:30:49 2016 +0000 @@ -1,4 +1,4 @@ -/* ESP8266Interface Example +/* EMW3162Interface Example * Copyright (c) 2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,31 +14,31 @@ * limitations under the License. */ -#ifndef ESP8266_H -#define ESP8266_H +#ifndef EMW3162_H +#define EMW3162_H #include "ATParser.h" -/** ESP8266Interface class. - This is an interface to a ESP8266 radio. +/** EMW3162Interface class. + This is an interface to a EMW3162 radio. */ -class ESP8266 +class EMW3162 { public: - ESP8266(PinName tx, PinName rx, bool debug=false); + EMW3162(PinName tx, PinName rx, bool debug=false); /** - * Startup the ESP8266 + * Startup the EMW3162 * * @param mode mode of WIFI 1-client, 2-host, 3-both - * @return true only if ESP8266 was setup correctly + * @return true only if EMW3162 was setup correctly */ bool startup(); /** - * Reset ESP8266 + * Reset EMW3162 * - * @return true only if ESP8266 resets successfully + * @return true only if EMW3162 resets successfully */ bool reset(const char *reset); @@ -47,42 +47,42 @@ * * @param enabled DHCP enabled when true * @param mode mode of DHCP 0-softAP, 1-station, 2-both - * @return true only if ESP8266 enables/disables DHCP successfully + * @return true only if EMW3162 enables/disables DHCP successfully */ bool dhcp(bool enabled); /** - * Connect ESP8266 to AP + * Connect EMW3162 to AP * * @param ap the name of the AP * @param passPhrase the password of AP - * @return true only if ESP8266 is connected successfully + * @return true only if EMW3162 is connected successfully */ bool connect(const char *ap, const char *passPhrase); /** - * Disconnect ESP8266 from AP + * Disconnect EMW3162 from AP * - * @return true only if ESP8266 is disconnected successfully + * @return true only if EMW3162 is disconnected successfully */ bool disconnect(void); /** - * Get the IP address of ESP8266 + * Get the IP address of EMW3162 * * @return null-teriminated IP address or null if no IP address is assigned */ const char *getIPAddress(void); /** - * Get the MAC address of ESP8266 + * Get the MAC address of EMW3162 * * @return null-terminated MAC address or null if no MAC address is assigned */ const char *getMACAddress(void); /** - * Check if ESP8266 is conenected + * Check if EMW3162 is conenected * * @return true only if the chip has an IP address */