WizFi310Interface
Fork of WizFi310Interface_Legacynew by
Revision 0:774ff1e8b26b, committed 2017-04-19
- Comitter:
- kaizen
- Date:
- Wed Apr 19 00:46:44 2017 +0000
- Child:
- 1:e08ea0ee2788
- Commit message:
- First release WizFi310Interface for mbed OS2 ( Legacy version )
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Helper/def.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,28 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef DEF_H +#define DEF_H + +#include "cmsis.h" +#define htons(x) __REV16(x) +#define ntohs(x) __REV16(x) +#define htonl(x) __REV(x) +#define ntohl(x) __REV(x) + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "Socket/Socket.h" +#include "Socket/Endpoint.h" +#include <cstring> + +using std::memset; + +Endpoint::Endpoint() { + _ewizfi310 = WizFi310::getInstance(); + if (_ewizfi310 == NULL) + error("Endpoint constructor error: no WizFi250 instance available!\r\n"); + reset_address(); +} +Endpoint::~Endpoint() {} + +void Endpoint::reset_address(void) { + _ipAddress[0] = '\0'; + _port = 0; +} + +int Endpoint::set_address(const char* host, const int port) { + //Resolve DNS address or populate hard-coded IP address + _port = port; + return _ewizfi310->getHostByName(host, _ipAddress); +} + +char* Endpoint::get_address() { + return _ipAddress; +} + +int Endpoint::get_port() { + return _port; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef ENDPOINT_H +#define ENDPOINT_H + +#include "WizFi310.h" + +class UDPSocket; + +/** +IP Endpoint (address, port) +*/ +class Endpoint { + friend class UDPSocket; + +public: + /** IP Endpoint (address, port) + */ + Endpoint(void); + + ~Endpoint(void); + + /** Reset the address of this endpoint + */ + void reset_address(void); + + /** Set the address of this endpoint + \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). + \param port The endpoint port + \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + */ + int set_address(const char* host, const int port); + + /** Get the IP address of this endpoint + \return The IP address of this endpoint. + */ + char* get_address(void); + + /** Get the port of this endpoint + \return The port of this endpoint + */ + int get_port(void); + +protected: + char _ipAddress[16]; + int _port; + WizFi310 * _ewizfi310; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "Socket.h" +#include <cstring> + +Socket::Socket() { + _wizfi310 = NULL; + _wizfi310 = WizFi310::getInstance(); + if (_wizfi310 == NULL) + error("Socket constructor error: no WizFi250 instance available!\r\n"); + _blocking = true; + _timeout = 1500; + + _server = false; + _cid = -1; + _port = 0; +} + +void Socket::set_blocking(bool blocking, unsigned int timeout) { + _blocking = blocking; + _timeout = timeout; +} + +int Socket::close() { + if (_cid < 0) return 0; + + _wizfi310->close(_cid); + _cid = -1; + return 0; +} + +Socket::~Socket() { + close(); //Don't want to leak +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef SOCKET_H_ +#define SOCKET_H_ + +#include "WizFi310.h" + +/** Socket file descriptor and select wrapper + */ +class Socket { +public: + /** Socket + */ + Socket(); + + /** Set blocking or non-blocking mode of the socket and a timeout on + blocking socket operations + \param blocking true for blocking mode, false for non-blocking mode. + \param timeout timeout in ms [Default: (1500)ms]. + */ + void set_blocking(bool blocking, unsigned int timeout=1500); + + /** Close the socket file descriptor + */ + int close(); + + ~Socket(); + +protected: + bool _blocking; + int _timeout; + WizFi310 * _wizfi310; + + bool _server; + int _cid; + int _port; +}; + + +#endif /* SOCKET_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "TCPSocketConnection.h" +#include <algorithm> + +TCPSocketConnection::TCPSocketConnection() {} + +int TCPSocketConnection::connect(const char* host, const int port) +{ + if (set_address(host, port) != 0) return -1; + + _server = false; + _cid = _wizfi310->open(WizFi310::PROTO_TCP, get_address(), get_port()); + if (_cid < 0 ) return -1; + + return 0; +} + +bool TCPSocketConnection::is_connected(void) +{ + bool _is_connected = _wizfi310->isConnected(_cid); + if(!_is_connected) _cid = -1; + + return _is_connected; +} + +int TCPSocketConnection::send(char *data, int length) +{ + if (_cid < 0 || !is_connected()) return -1; + + return _wizfi310->send(_cid, data, length); +} + +int TCPSocketConnection::send_all(char *data, int length) +{ + Timer tmr; + int idx = 0; + + if(_cid <0 || !is_connected()) return -1; + + tmr.start(); + while((tmr.read_ms() < _timeout) || _blocking) + { + idx += _wizfi310->send(_cid, &data[idx], length - idx); + if(idx < 0) return -1; + + if(idx == length) + return idx; + } + return (idx == 0) ? -1 : idx; +} + +int TCPSocketConnection::receive(char* data, int length) +{ + Timer tmr; + int time = -1; + int len = 0; + + if(_cid < 0 || !is_connected()) return -1; + + if(_blocking) + { + tmr.start(); + while(time < _timeout) + { + len = _wizfi310->readable(_cid); + if(len == -1) + return len; + + if(len > 0) + { + WIZ_DBG("receive readable : %d\r\n",len); + break; + } + time = tmr.read_ms(); + } + if(time >= _timeout) + { + //WIZ_DBG("receive timeout"); + return 0; + } + } + + return _wizfi310->recv(_cid, data, length); +} + +int TCPSocketConnection::receive_all(char* data, int length) +{ + Timer tmr; + int idx = 0; + int time = -1; + + if(_cid < 0 || !is_connected()) return -1; + + tmr.start(); + + while(time < _timeout || _blocking) + { + idx += _wizfi310->recv(_cid, &data[idx], length - idx); + if (idx < 0) return -1; + + if(idx == length) + break; + + time = tmr.read_ms(); + } + + return (idx == 0) ? -1 : idx; +} + +void TCPSocketConnection::acceptCID (int cid) +{ + char *ip; + int port; + _server = true; + _cid = cid; + + + if( _wizfi310->cmdSMGMT(cid) ) return; + if( !_wizfi310->getRemote(_cid, &ip, &port)) + { + set_address(ip, port); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef TCPSOCKET_H +#define TCPSOCKET_H + +#include "Socket.h" +#include "Endpoint.h" + +/** +TCP socket connection +*/ +class TCPSocketConnection: public Socket, public Endpoint { + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + + /** Connects this TCP socket to the server + \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. + \param port The host's port to connect to. + \return 0 on success, -1 on failure. + */ + int connect(const char* host, const int port); + + /** Check if the socket is connected + \return true if connected, false otherwise. + */ + bool is_connected(void); + + /** Send data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send(char* data, int length); + + /** Send all the data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send_all(char* data, int length); + + /** Receive data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(char* data, int length); + + /** Receive all the data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive_all(char* data, int length); + + void acceptCID (int cid); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "TCPSocketServer.h" +#include <string> + +TCPSocketServer::TCPSocketServer() {} + +// Server initialization +int TCPSocketServer::bind(int port) { + + _port = port; + return 0; +} + +int TCPSocketServer::listen(int backlog) { + _server = true; + if (_cid < 0) { + // Socket open + _server = false; + _cid = _wizfi310->listen(WizFi310::PROTO_TCP, _port); + + //WIZ_DBG("TEST CID : %d",_cid); + if (_cid < 0) return -1; + } + + if (backlog != 1) + return -1; + return 0; +} + + +int TCPSocketServer::accept(TCPSocketConnection& connection) { + int acid = -1; + + while (1) { + while (acid < 0) { + acid = _wizfi310->accept(_cid); + } + + if (acid >= 0) { + connection.acceptCID(acid); + return 0; + } + } + return -1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef TCPSOCKETSERVER_H +#define TCPSOCKETSERVER_H + +#include "Socket/Socket.h" +#include "TCPSocketConnection.h" + +/** TCP Server. + */ +class TCPSocketServer : public Socket { + public: + /** Instantiate a TCP Server. + */ + TCPSocketServer(); + + /** Bind a socket to a specific port. + \param port The port to listen for incoming connections on. + \return 0 on success, -1 on failure. + */ + int bind(int port); + + /** Start listening for incoming connections. + \param backlog number of pending connections that can be queued up at any + one time [Default: 1]. + \return 0 on success, -1 on failure. + */ + int listen(int backlog=1); + + /** Accept a new connection. + \param connection A TCPSocketConnection instance that will handle the incoming connection. + \return 0 on success, -1 on failure. + */ + int accept(TCPSocketConnection& connection); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "UDPSocket.h" + +#include <string> +#include <algorithm> + +UDPSocket::UDPSocket() +{ + endpoint_connected = false; +} + +int UDPSocket::init(void) +{ + _server = false; + return 0; +} + +// Server initialization +int UDPSocket::bind(int port) +{ + if(port == 0) + { + _port = 5000; + _server = false; + } + else + { + _port = port; + _server = true; + } + + return 0; +} + +int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) +{ + Timer tmr; + int idx = 0; + + if (_cid < 0 && _wizfi310->isAssociated()) + { + if (_server) + { + _cid = _wizfi310->listen(WizFi310::PROTO_UDP, _port); + } + else + { + _cid = _wizfi310->open(WizFi310::PROTO_UDP, remote.get_address(), remote.get_port(), _port); + } + if (_cid < 0) return -1; + } + + tmr.start(); + + while ((tmr.read_ms() < _timeout) || _blocking) + { + if(_server) + { + idx += _wizfi310->sendto(_cid, packet, length, remote.get_address(), remote.get_port()); + } + else + { + idx += _wizfi310->send(_cid, packet, length); + } + if (idx < 0) + { + if (!_wizfi310->isConnected(_cid)) _cid = -1; + } + + if (idx == length) + return idx; + } + return (idx == 0) ? -1 : idx; +} + + +int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) +{ + Timer tmr; + int idx = 0; + int time = -1; + char ip[16]; + int port; + + if(_cid < 0 && _wizfi310->isAssociated()) + { + // Socket open + if (_server) { + _cid = _wizfi310->listen(WizFi310::PROTO_UDP, _port); + //WIZ_DBG("TEST CID : %d",_cid); + } + else + { + _cid = _wizfi310->open(WizFi310::PROTO_UDP, remote.get_address(), remote.get_port(), _port); + } + if (_cid < 0) return -1; + } + + if (_blocking) + { + tmr.start(); + while (time < _timeout + 20) + { + if(_wizfi310->readable(_cid)) + { + break; + } + time = tmr.read_ms(); + } + if (time >= _timeout + 20) + { + return -1; + } + } + else + { + if(!_wizfi310->readable(_cid)) + { + return 0; + } + } + + + tmr.reset(); + time = -1; + tmr.start(); + + while (time < _timeout) + { + if(_server) + { + idx += _wizfi310->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); + } + else + { + //idx += _wizfi250->recv(_cid, &buffer[idx], length - idx); + idx += _wizfi310->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); + } + + if (idx == length) + break; + + + time = tmr.read_ms(); + } + + remote.set_address(ip, port); + return (idx==0) ? -1 : idx; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef UDPSOCKET_H +#define UDPSOCKET_H + +#include "Endpoint.h" +#include "Socket.h" + +//#include <cstdint> + +/** +UDP Socket +*/ +class UDPSocket: public Socket { + +public: + /** Instantiate an UDP Socket. + */ + UDPSocket(); + + /** Init the UDP Client Socket without binding it to any specific port + \return 0 on success, -1 on failure. + */ + int init(void); + + /** Bind a UDP Server Socket to a specific port + \param port The port to listen for incoming connections on + \return 0 on success, -1 on failure. + */ + int bind(int port = -1); + + /** Send a packet to a remote endpoint + \param remote The remote endpoint + \param packet The packet to be sent + \param length The length of the packet to be sent + \return the number of written bytes on success (>=0) or -1 on failure + */ + int sendTo(Endpoint &remote, char *packet, int length); + + /** Receive a packet from a remote endpoint + \param remote The remote endpoint + \param buffer The buffer for storing the incoming packet data. If a packet + is too long to fit in the supplied buffer, excess bytes are discarded + \param length The length of the buffer + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receiveFrom(Endpoint &remote, char *buffer, int length); + +private: + bool endpoint_connected; + +}; + +#include "def.h" + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/CBuffer.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,78 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef CIRCBUFFER_H_ +#define CIRCBUFFER_H_ + +template <class T> +class CircBuffer { +public: + CircBuffer(int length) { + write = 0; + read = 0; + size = length + 1; + buf = (T *)malloc(size * sizeof(T)); + if (buf == NULL) + error("Can't allocate memory"); + }; + + bool isFull() { + return (((write + 1) % size) == read); + }; + + bool isEmpty() { + return (read == write); + }; + + void queue(T k) { + if (isFull()) { +// read++; +// read %= size; + return; + } + buf[write++] = k; + write %= size; + } + + void flush() { + read = 0; + write = 0; + } + + + uint32_t available() { + return (write >= read) ? write - read : size - read + write; + }; + + bool dequeue(T * c) { + bool empty = isEmpty(); + if (!empty) { + *c = buf[read++]; + read %= size; + } + return(!empty); + }; + +private: + volatile uint32_t write; + volatile uint32_t read; + uint32_t size; + T * buf; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "mbed.h" +#include "WizFi310.h" + +WizFi310 * WizFi310::_inst; + + +WizFi310::WizFi310(PinName tx,PinName rx,PinName cts, PinName rts,PinName reset, PinName alarm, int baud): + _wizfi(tx,rx), _reset(reset) +{ + _inst = this; + memset(&_state, 0, sizeof(_state)); + memset(&_con, 0, sizeof(_con)); + _state.initialized = false; + _state.status = STAT_READY; + _state.cid = -1; + _state.buf = new CircBuffer<char>(CFG_DATA_SIZE); + + initUart(cts, rts, alarm, baud); + _reset.output(); + + setRts(true); // release + + for(int i=0;i<10;i++) + { + if( cmdAT() == 0 ) break; + } + cmdMECHO(false); + /* + wait_ms(500); + cmdAT(); + cmdMECHO(false); + if(cts != NC && rts != NC) + cmdUSET(baud,"HW"); + else + cmdUSET(baud,"N"); + + // WizFi310 will restart by cmdUSET command. + wait_ms(1000); + cmdAT(); + */ +} + +int WizFi310::join(WiFiMode mode) +{ + char sec[10]; + + if( cmdMMAC() ) return -1; + + if(mode == WM_AP) + _state.wm = WM_AP; + else + _state.wm = WM_STATION; + + if ( cmdWNET(_state.dhcp) ) return -1; + if ( cmdWSET(_state.wm, _state.ssid) ) return -1; + + + switch (_state.sec) + { + case SEC_OPEN: + strcpy(sec,"OPEN"); + break; + case SEC_WEP: + strcpy(sec,"WEP"); + break; + case SEC_WPA_TKIP: + strcpy(sec,"WPA"); + break; + case SEC_WPA2_MIXED: + strcpy(sec,"WPA2"); + break; + default: + strcpy(sec,""); + break; + } + + if ( cmdWSEC(_state.wm, _state.pass, sec) ) return -1; + if ( cmdWJOIN() ) return -1;; + _state.associated = true; + + return 0; +} + +bool WizFi310::isAssociated() +{ + return _state.associated; +} + +int WizFi310::setMacAddress (const char *mac) +{ + if (cmdMMAC(mac)) return -1; + strncpy(_state.mac, mac, sizeof(_state.mac)); + return 0; +} + +int WizFi310::getMacAddress (char *mac) +{ + if (cmdMMAC()) return -1; + strcpy(mac, _state.mac); + return 0; +} + +int WizFi310::setAddress (const char *name) +{ + _state.dhcp = true; + strncpy(_state.name, name, sizeof(_state.name)); + return 0; +} + +int WizFi310::setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns, const char *name) +{ + _state.dhcp = false; + strncpy(_state.ip, ip, sizeof(_state.ip)); + strncpy(_state.netmask, netmask, sizeof(_state.netmask)); + strncpy(_state.gateway, gateway, sizeof(_state.gateway)); + strncpy(_state.nameserver, dns, sizeof(_state.nameserver)); + strncpy(_state.name, name, sizeof(_state.name)); + return 0; +} + +int WizFi310::getAddress (char *ip, char *netmask, char *gateway) +{ + strcpy(ip, _state.ip); + strcpy(netmask, _state.netmask); + strcpy(gateway, _state.gateway); + return 0; +} + +int WizFi310::setSsid (const char *ssid) +{ + strncpy(_state.ssid, ssid, sizeof(_state.ssid)); + return 0; +} + +int WizFi310::setSec ( Security sec, const char *phrase ) +{ + _state.sec = sec; + strncpy(_state.pass, phrase, strlen(phrase)); + return 0; +} + +const char *WizFi310::getIPAddress(void) +{ + return _state.ip; +} + +const char *WizFi310::getMACAddress(void) +{ + return _state.mac; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,333 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef WIZFI310_H_ +#define WIZFI310_H_ + +#include "WizFi310_conf.h" + +#include "mbed.h" +#include "RawSerial.h" +#include "Serial.h" +#include "CBuffer.h" +#include <ctype.h> +#include <stdlib.h> +#include <string> + +using namespace std; + +#define BUF_SIZE 1600 + +//Debug is disabled by default +#if 1 +#define WIZ_DBG(x, ...) std::printf("[WizFi310: DBG]" x "\r\n", ##__VA_ARGS__); +#define WIZ_WARN(x, ...) std::printf("[WizFi310: WARN]" x "\r\n", ##__VA_ARGS__); +#define WIZ_ERR(x, ...) std::printf("[WizFi310: ERR]" x "\r\n", ##__VA_ARGS__); +#define WIZ_INFO(x, ...) std::printf("[WizFi310: INFO]" x "\r\n", ##__VA_ARGS__); +#else +#define WIZ_DBG(x, ...) +#define WIZ_WARN(x, ...) +#define WIZ_ERR(x, ...) +#define WIZ_INFO(x, ...) +#endif + + +class WizFi310 +{ +public: + + enum AntennaMode{ + PCB = 0, + UFL = 1, + AUTO = 3, + }; + + enum WiFiMode { + WM_STATION = 0, + WM_AP = 1, + }; + + /** Wi-Fi security + */ + enum Security { + // kaizen need to change + SEC_AUTO = 0, + SEC_OPEN = 1, + SEC_WEP = 2, + SEC_WPA_TKIP = 3, + SEC_WPA_AES = 4, + SEC_WPA2_AES = 5, + SEC_WPA2_TKIP = 6, + SEC_WPA2_MIXED = 7, + }; + + /** TCP/IP protocol + */ + enum Protocol { + PROTO_UDP = 0, + PROTO_TCP = 1, + }; + + /** Client/Server + */ + enum Type { + TYPE_CLIENT = 0, + TYPE_SERVER = 1, + }; + + enum Response { + RES_NULL, + RES_MACADDRESS, + RES_WJOIN, + RES_CONNECT, + RES_SSEND, + RES_FDNS, + RES_SMGMT, + RES_WSTATUS, + + }; + + enum Mode { + MODE_COMMAND, + MODE_CMDRESP, + MODE_DATA_RX, + MODE_DATA_RXUDP, + MODE_DATA_RXUDP_BULK, + }; + + enum Status { + STAT_NONE, + STAT_READY, + STAT_STANDBY, + STAT_WAKEUP, + STAT_DEEPSLEEP, + }; + + + WizFi310 (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200); + + // --------- WizFi250_at.cpp --------- + void clearFlags (); + int sendCommand (const char * cmd, Response res = RES_NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT, int opt = 1); + + int cmdAT (); + int cmdMECHO (bool flg); + int cmdUSET (int baud, char *flow); + int cmdMMAC (const char *mac = NULL); + int cmdWSET (WiFiMode mode, const char *ssid, const char *bssid = NULL, int channel = 1); + int cmdWANT (AntennaMode mode); + int cmdWNET (bool is_dhcp); + int cmdWSEC (WiFiMode mode, const char *key, const char *sec = NULL); + int cmdWJOIN (); + int cmdWLEAVE (); + int cmdWSTATUS (); + int cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode = "0"); + int cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort = 0, const char *dataMode = "0"); + int cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp = NULL, int remotePort = 0, int Timeout = 2000 ); + int cmdCLOSE ( int cid ); + int cmdFDNS (const char *host); + int cmdSMGMT ( int cid ); + + + static WizFi310 * getInstance() { + return _inst; + }; + + + // --------- WizFi2550_sock.cpp --------- + int getHostByName (const char * host, char *ip); + int open (Protocol proto, const char *ip, int remotePort, int localPort = 0, void(*func)(int) = NULL); + int listen (Protocol proto, int port, void(*func)(int)=NULL); + int close (int cid); + void initCon (int cid, bool connected); + int send (int cid, const char *buf, int len); + int sendto (int cid, const char *buf, int len, const char *ip, int port); + int recv (int cid, char *buf, int len); + int recvfrom (int cid, char *buf, int len, char *ip, int *port); + int readable (int cid); + bool isConnected (int cid); + int accept (int cid); + int getRemote (int cid, char **ip, int *port); + + +protected: + static WizFi310 * _inst; + + + // Serial _wizfi + RawSerial _wizfi; + int _baud; + DigitalIn *_cts; + DigitalOut *_rts; + int _flow; + + DigitalInOut _reset; + + struct STATE { + WiFiMode wm; + + Security sec; + char ssid[35]; + char pass[66]; + char ip[16]; + char netmask[16]; + char gateway[16]; + char nameserver[16]; + char mac[18]; + char resolv[16]; + char name[32]; + int rssi; + bool dhcp; + time_t time; + + bool initialized; + bool associated; + volatile Mode mode; + volatile Status status; + bool escape; + volatile bool ok, failure; + volatile Response res; + int cid; + int n; + CircBuffer<char> *buf; + char dbgRespBuf[BUF_SIZE]; + //char dummyBuf[BUF_SIZE]; + } _state; + + +public: + struct CONNECTION { + Protocol protocol; + Type type; + bool connected; + char ip[16]; + int port; + int send_length; + int recv_length; + CircBuffer<char> *buf; + volatile bool received; + volatile int parent; + volatile bool accept; + void(*func)(int); + } _con[8]; + + // --------- WizFi310.cpp --------- + + int join(WiFiMode mode); + bool isAssociated(); + + //int limitedap (); + //int dissociate (); + /* + int disconnect () { + return dissociate(); + } + */ + + Status getStatus (); + + int setMacAddress (const char *mac); + int getMacAddress (char *mac); + int setAddress (const char *name = NULL); + int setAddress (const char *ip, const char *netmask, const char *gateway, const char *dns = NULL, const char *name = NULL); + int getAddress (char *ip, char *netmask, char *gateway); + int setSsid (const char *ssid); + int setSec (Security sec, const char *phrase); + + const char* getIPAddress (void); + const char* getMACAddress (void); + + // --------- WizFi250_msg.cpp --------- + void recvData ( char c ); + int parseMessage (); + void msgOk (const char *buf); + void msgError (const char *buf); + void msgConnect (const char *buf); + void msgDisconnect (const char *buf); + void msgListen (const char *buf); + //daniel + void msgMQTTConnect (const char *buf); + void msgMQTTDisconnect (const char *buf); + + void resMacAddress (const char *buf); + void resWJOIN (const char *buf); + void resConnect (const char *buf); + void resSSEND (const char *buf); + void resFDNS (const char *buf); + void resSMGMT (const char *buf); + void resWSTATUS (const char *buf); + + + // --------- WizFi250_hal.cpp --------- + void setReset (bool flg); + void isrUart (); + int getUart (); + void putUart (char c); + void setRts (bool flg); + int lockUart (int ms); + void unlockUart (); + void initUart (PinName cts, PinName rts, PinName alarm, int baud); + + + // --------- WizFi2550_util.cpp --------- + int x2i (char c); + int i2x (int i); + + // --------- WizFi250_ifc.cpp (For NetworkSocketAPI) --------- + /** + * Startup the WizFi310 + * + * @return true only if WizFi310 was setup correctly + */ + bool startup(void); + + /** + * Reset WizFi310 + * + * @return true only if WizFi310 resets successfully + */ + bool reset(void); + + /** + * Disconnect WizFi310 from AP + * + * @return true only if WizFi310 is disconnected successfully + */ + bool disconnect(void); + + /** + * Check if WizFi310 is conenected + * + * @return true only if the chip has an IP address + */ + bool isConnected(void); + + + //daniel for mqtt + char rcvd_mqtt_topic[128]; + +}; + +#endif /* WIZFI250_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_at.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,349 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "WizFi310.h" + + + +void WizFi310::clearFlags() +{ + _state.ok = false; + _state.failure = false; + _state.res = RES_NULL; + _state.n = 0; +} + + +int WizFi310::sendCommand(const char * cmd, Response res, int timeout, int opt) +{ + unsigned int i; + Timer t; + + if (lockUart(timeout)) return -1; + + clearFlags(); + _state.res = res; + + WIZ_INFO("%s", cmd) + + for (i=0; i< strlen(cmd); i++) + { + putUart(cmd[i]); + } + + if(opt == 1) + { + putUart('\r'); + } + else if(opt == 2) + { + putUart('\r'); + putUart('\n'); + } + unlockUart(); + + if(timeout) + { + t.start(); + for(;;) + { + if (_state.ok && _state.res == RES_NULL){ + break; + } + + if (_state.failure || t.read_ms() > timeout) + { + WIZ_WARN("failure of timeout[%d]ms\r\n",t.read_ms()); + _state.res = RES_NULL; + t.stop(); + return -1; + } + } + + t.stop(); + } + + WIZ_INFO("[OK]\r\n"); + _state.res = RES_NULL; + + return 0; +} + +int WizFi310::cmdAT() +{ + int resp; + + resp = sendCommand("AT"); + + return resp; +} + +int WizFi310::cmdMECHO(bool flg) +{ + int status; + char cmd[CFG_CMD_SIZE]; + + sprintf(cmd,"AT+MECHO=%d",flg); + status = sendCommand(cmd); + + return status; +} + +int WizFi310::cmdUSET(int baud, char *flow) +{ + int status; + char cmd[CFG_CMD_SIZE]; + + sprintf(cmd,"AT+USET=%d,N,8,1,%s",baud, flow); + status = sendCommand(cmd); + + if(status == 0) + { + wait(1); + _state.buf->flush(); + } + + return status; +} + +int WizFi310::cmdMMAC(const char *mac) +{ + int resp; + char cmd[CFG_CMD_SIZE]; + + if (mac) + { + sprintf(cmd, "AT+MMAC=%s",mac); + resp = sendCommand(cmd); + } + else + { + sprintf(cmd, "AT+MMAC=?"); + resp = sendCommand(cmd, RES_MACADDRESS); + } + + return resp; +} + +int WizFi310::cmdWSET(WiFiMode mode, const char *ssid, const char *bssid, int channel) +{ + char cmd[CFG_CMD_SIZE]; + + if(*bssid == NULL) + { + sprintf(cmd, "AT+WSET=%d,%s",mode, ssid); + } + else + { + sprintf(cmd, "AT+WSET=%d,%s,%s,%d",mode, ssid, bssid, channel); + } + + return sendCommand(cmd); +} + +int WizFi310::cmdWANT(AntennaMode mode) +{ + char cmd[CFG_CMD_SIZE]; + sprintf(cmd, "AT+WANT=%d",mode); + + return sendCommand(cmd); +} + +int WizFi310::cmdWNET(bool is_dhcp) +{ + char cmd[CFG_CMD_SIZE]; + + if(is_dhcp == true) + { + sprintf(cmd, "AT+WNET=1"); + } + else + { + sprintf(cmd, "AT+WNET=0,%s,%s,%s",_state.ip,_state.netmask,_state.gateway); + } + + return sendCommand(cmd); +} + +int WizFi310::cmdWSEC(WiFiMode mode, const char *key, const char *sec) +{ + char cmd[CFG_CMD_SIZE]; + + if(*sec == NULL) + { + sprintf(cmd, "AT+WSEC=%d,,%s",mode, key); + } + else + { + sprintf(cmd, "AT+WSEC=%d,%s,%s",mode, sec, key); + } + + return sendCommand(cmd); +} + +int WizFi310::cmdWJOIN() +{ + //if( sendCommand("AT+WJOIN", RES_WJOIN, CFG_JOIN_TIMEOUT) ) + if( sendCommand("AT+WJOIN", RES_NULL, CFG_JOIN_TIMEOUT) ) + { + WIZ_ERR("cmdWJOIN"); + return -1; + } + + if( cmdWSTATUS() ) + return -1; + + WIZ_INFO("WizFi310 is successfully join to AP"); + + return 0; +} + +int WizFi310::cmdWLEAVE() +{ + return sendCommand("AT+WLEAVE"); +} + + +int WizFi310::cmdWSTATUS() +{ + if( sendCommand("AT+WSTATUS", RES_WSTATUS, DEFAULT_WAIT_RESP_TIMEOUT) ) + { + WIZ_ERR("cmdWSTATUS"); + return -1; + } + + WIZ_INFO("IP : %s", _state.ip); + WIZ_INFO("Gateway : %s", _state.gateway); + + return 0; +} + +int WizFi310::cmdSCON ( const char *openType, const char *socketType, int localPort, const char *dataMode) +{ + char cmd[CFG_CMD_SIZE]; + + sprintf(cmd,"AT+SCON=%s,%s,,,%d,%s",openType, socketType, localPort, dataMode); + return sendCommand(cmd); +} + +int WizFi310::cmdSCON ( const char *openType, const char *socketType, const char *remoteIp, int remotePort, int localPort, const char *dataMode) +{ + int resp; + char cmd[CFG_CMD_SIZE]; + + if(localPort == 0) + sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%s,%s",openType, socketType, remoteIp, remotePort, "", dataMode); + else + sprintf(cmd,"AT+SCON=%s,%s,%s,%d,%d,%s",openType, socketType, remoteIp, remotePort, localPort, dataMode); + + resp = sendCommand(cmd, RES_CONNECT, 30000 ); + + return resp; +} + +int WizFi310::cmdSSEND ( const char *data, int cid, int sendSize, const char *remoteIp, int remotePort, int Timeout ) +{ + int i, resp; + Timer t; + char cmd[CFG_CMD_SIZE]; + + if (lockUart(Timeout)) return -1; + + clearFlags(); + if(remoteIp == NULL) + { + sprintf(cmd,"AT+SSEND=%d,,,%d",cid, sendSize); + } + else + { + sprintf(cmd,"AT+SSEND=%d,%s,%d,%d",cid, remoteIp, remotePort, sendSize); + } + + _con[cid].send_length = sendSize; + + resp = sendCommand(cmd, RES_SSEND, 2000, 1); + + unlockUart(); + if(resp){ + WIZ_DBG("Fail cmdSSEND") + return -1; + } + + for(i=0; i<sendSize; i++) + { + putUart(data[i]); + } + unlockUart(); + + if(Timeout) + { + t.start(); + for(;;) + { + if (_state.ok) break; + if (_state.failure || t.read_ms() > Timeout) + { + WIZ_WARN("failure or timeout\r\n"); + return -1; + } + } + t.stop(); + } + + wait(0.05); + WIZ_INFO("%s\r\n",data); + + return i; +} + + +int WizFi310::cmdCLOSE ( int cid ) +{ + char cmd[CFG_CMD_SIZE]; + + sprintf(cmd,"AT+SMGMT=%d",cid); + return sendCommand(cmd); +} + + +int WizFi310::cmdFDNS (const char *host) +{ + char cmd[CFG_CMD_SIZE]; + int resp; + + sprintf(cmd,"AT+FDNS=%s,3000",host); + resp = sendCommand(cmd, RES_FDNS); + + WIZ_DBG("%s",_state.resolv); + return resp; +} + +int WizFi310::cmdSMGMT ( int cid ) +{ + int resp; + + resp = sendCommand("AT+SMGMT=?", RES_SMGMT); + return resp; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_conf.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef WIZFI250_CONF_H_ +#define WIZFI250_CONF_H_ + + +#define CFG_TRYJOIN 3 + + +#define DEFAULT_WAIT_RESP_TIMEOUT 2000 // ms +#define CFG_JOIN_TIMEOUT 60000 // ms +#define CFG_CMD_SIZE 128 + +#define CFG_DATA_SIZE BUF_SIZE + +#define CFG_DEFAULT_MAC "00:08:DC:00:00:00" + + +#endif /* WIZFI250_CONF_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_hal.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "WizFi310.h" + +void WizFi310::setReset(bool flg) +{ + if( flg ) + { + // low + _reset.output(); + _reset = 0; + } + else + { + // high z + _reset.input(); + _reset.mode(PullNone); + } +} + +void WizFi310::isrUart() +{ + char c; + + c = getUart(); + + recvData(c); + //S_UartPutc(c); +} + +int WizFi310::getUart() +{ + return _wizfi.getc(); +} + +void WizFi310::putUart (char c) +{ + _wizfi.putc(c); +} + +void WizFi310::setRts (bool flg) +{ + if (flg) + { + if(_flow == 2) + { + if(_rts) + { + _rts->write(0); // low + } + } + } + else + { + if(_flow == 2) + { + if(_rts) + { + _rts->write(1); // high + } + } + } +} + +int WizFi310::lockUart (int ms) +{ + Timer t; + + if(_state.mode != MODE_COMMAND) + { + t.start(); + while(_state.mode != MODE_COMMAND) + { + if(t.read_ms() >= ms) + { + WIZ_WARN("lock timeout (%d)\r\n", _state.mode); + return -1; + } + } + } + +#ifdef CFG_ENABLE_RTOS + if (_mutexUart.lock(ms) != osOK) return -1; +#endif + + if(_flow == 2) + { + if(_cts && _cts->read()) + { + // CTS check + t.start(); + while (_cts->read()) + { + if(t.read_ms() >= ms) + { + WIZ_DBG("cts timeout\r\n"); + return -1; + } + } + } + } + + setRts(false); // blcok + return 0; +} + +void WizFi310::unlockUart() +{ + setRts(true); // release +#ifdef CFG_ENABLE_RTOS + _mutexUart.unlock(); +#endif +} + +void WizFi310::initUart (PinName cts, PinName rts, PinName alarm, int baud) +{ + _baud = baud; + if (_baud) _wizfi.baud(_baud); + + _wizfi.attach(this, &WizFi310::isrUart, Serial::RxIrq); + + _cts = NULL; + _rts = NULL; + _flow = 0; + + if(cts != NC) + { + _cts = new DigitalIn(cts); + } + if(rts != NC) + { + _rts = new DigitalOut(rts); + _flow = 2; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_ifc.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "WizFi310.h" + +bool WizFi310::startup(void) +{ + reset(); + + cmdAT(); + if( cmdMECHO(false) == -1) return false; + //if( cmdMECHO(true) == -1) return false; + if(_cts != NULL && _rts != NULL) + { + if( cmdUSET(_baud,"HW") == -1) return false; + } + else + { + if( cmdUSET(_baud,"N") == -1) return false; + } + wait_ms(1000); + if( cmdAT() == -1) return false; + + return true; +} + +bool WizFi310::reset(void) +{ + _reset = 0; + wait_ms(500); + _reset = 1; + + return true; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_msg.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,492 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "WizFi310.h" + +#ifdef CFG_ENABLE_RTOS +#undef WIZ_DBG +#define WIZ_DBG(x, ...) +#endif + +//daniel +char g_asyncbuf[256]; + +// This function is operating in ISR. So you can't use debug message. +void WizFi310::recvData ( char c ) +{ + static int cid, sub, len, count; + static int is_mqtt_data = 0; + char tbf[10]; + + switch(_state.mode) + { + case MODE_COMMAND: + switch(c) + { + case 0: + case 0x0a: // LF + case 0x0d: // CR + break; + + case '{': + _state.buf->flush(); + _state.mode = MODE_DATA_RX; + sub = 0; + break; + + default: + _state.buf->flush(); + _state.buf->queue(c); + _state.mode = MODE_CMDRESP; + break; + } + break; + + case MODE_CMDRESP: + switch(c) + { + case 0: + break; + case 0x0a: // LF + break; + case 0x0d: // CR + if (_flow == 2) setRts(false); // block + _state.mode = MODE_COMMAND; + parseMessage(); + if (_flow == 2) setRts(true); // release + break; + default: + _state.buf->queue(c); + break; + } + break; + + case MODE_DATA_RX: + + switch(sub) + { + case 0: + // cid + if( (c >= '0') && (c <= '9') ) + { + cid = x2i(c); + } + else if ( c == ',' ) + { + sub++; + count = 0; + len = 0; + } + //daniel add for mqtt + else if ( c == 'Q' ) + { + cid = 0; + is_mqtt_data = 1; + } + // + else + { + _state.mode = MODE_COMMAND; + } + break; + + case 1: + // ip +// if ((c >= '0' && c <= '9') || c == '.') + if (((c >= '0' && c <= '9') || c == '.') && is_mqtt_data == 0 ) + { + _con[cid].ip[count] = c; + count++; + } + else if( c == ',' ) + { + _con[cid].ip[count] = '\0'; + _con[cid].port = 0; + sub++; + } + //daniel for mqtt + else if( is_mqtt_data == 1) + { + rcvd_mqtt_topic[count] = c; + count++; + } +// else + else if( is_mqtt_data == 0 ) + { + _state.mode = MODE_COMMAND; + } + break; + + case 2: + // port + if ( c >= '0' && c <= '9' ) + { + _con[cid].port = (_con[cid].port * 10) + ( c - '0' ); + } + else if( c == ',') + { + sub++; + count = 0; + } + else + { + _state.mode = MODE_COMMAND; + } + break; + + case 3: + // data length + if ( c >= '0' && c <= '9' ) + { + //_con[cid].recv_length = (_con[cid].recv_length * 10) + (c - '0'); + len = (len * 10) + (c - '0'); + } + else if( c == '}' ) + { + sub++; + count = 0; + _con[cid].recv_length = len; + } + else + { + _state.mode = MODE_COMMAND; + } + break; + + default: + + if(_con[cid].buf != NULL) + { + _con[cid].buf->queue(c); + if(_con[cid].buf->available() > CFG_DATA_SIZE - 16 ) + { + setRts(false); // blcok + _con[cid].received = true; + WIZ_WARN("buf full"); + } + } + _con[cid].recv_length--; + if(_con[cid].recv_length == 0) + { + //WIZ_DBG("recv cid: %d, count : %d, len : %d",cid, count, len); + //sprintf(tbf, "recv cid: %d, count : %d, len : %d",cid, count, len); + //strcat(g_asyncbuf, tbf); + _con[cid].received = true; + _state.mode = MODE_COMMAND; + } + break; + } + break; + } +} + + +//#define MSG_TABLE_NUM 6 +//daniel +#define MSG_TABLE_NUM 8 +#define RES_TABLE_NUM 7 +int WizFi310::parseMessage () { + int i; + char buf[128]; + + static const struct MSG_TABLE { + const char msg[24]; + void (WizFi310::*func)(const char *); + } msg_table[MSG_TABLE_NUM] = { + {"[OK]", &WizFi310::msgOk}, + {"[ERROR]", &WizFi310::msgError}, + {"[ERROR:INVALIDINPUT]", &WizFi310::msgError}, + {"[CONNECT ", &WizFi310::msgConnect}, + {"[DISCONNECT ", &WizFi310::msgDisconnect}, + {"[LISTEN ", &WizFi310::msgListen}, + //daniel + {"[MQTT CONNECT]", &WizFi310::msgMQTTConnect}, + {"[MQTT DISCONNECT]", &WizFi310::msgMQTTDisconnect}, + }; + static const struct RES_TABLE{ + const Response res; + void (WizFi310::*func)(const char *); + }res_table[RES_TABLE_NUM]={ + {RES_NULL, NULL}, + {RES_MACADDRESS, &WizFi310::resMacAddress}, +// {RES_WJOIN, &WizFi310::resWJOIN}, + {RES_CONNECT, &WizFi310::resConnect}, + {RES_SSEND, &WizFi310::resSSEND}, + {RES_FDNS, &WizFi310::resFDNS}, + {RES_SMGMT, &WizFi310::resSMGMT}, + {RES_WSTATUS, &WizFi310::resWSTATUS}, + }; + + + for( i=0; i<sizeof(buf); i++ ) + { + if( _state.buf->dequeue(&buf[i]) == false ) break; + } + + buf[i] = '\0'; + //strncpy(_state.dbgRespBuf, buf, sizeof(buf) ); + //WIZ_DBG("%s\r\n",_state.dbgRespBuf); + + if(_state.res != RES_NULL) + { + for( i=0; i<RES_TABLE_NUM; i++) + { + if(res_table[i].res == _state.res) + { + //WIZ_DBG("parse res %d '%s'\r\n", i, buf); + if(res_table[i].func != NULL) + { + (this->*(res_table[i].func))(buf); + } + + if(res_table[i].res == RES_CONNECT && _state.n < 2) + return -1; + } + } + } + + for( i=0; i<MSG_TABLE_NUM; i++) + { + if( strncmp(buf, msg_table[i].msg, strlen(msg_table[i].msg)) == 0 ) + { + //WIZ_DBG("parse msg '%s'\r\n", buf); + if(msg_table[i].func != NULL) + { + (this->*(msg_table[i].func))(buf); + } + return 0; + } + } + + return -1; +} + + +void WizFi310::msgOk (const char *buf) +{ + _state.ok = true; +} + +void WizFi310::msgError (const char *buf) +{ + _state.failure = true; +} + +void WizFi310::msgConnect (const char *buf) +{ + int cid; + + if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; + + cid = x2i(buf[9]); + + initCon(cid, true); + _state.cid = cid; + _con[cid].accept = true; + _con[cid].parent = cid; +} + +void WizFi310::msgDisconnect (const char *buf) +{ + int cid; + + if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; + + cid = x2i(buf[12]); + _con[cid].connected = false; +} + + +void WizFi310::msgMQTTConnect (const char *buf) +{ + int cid = 0; + + //if (buf[9] < '0' || buf[9] > '8' || buf[10] != ']') return; + + //cid = x2i(buf[9]); + initCon(cid, true); + _state.cid = cid; + _con[cid].accept = true; + _con[cid].parent = cid; + + _con[cid].connected = true; + _state.res = RES_NULL; + _state.ok = true; +} + +void WizFi310::msgMQTTDisconnect (const char *buf) +{ + int cid = 0; + + //if(buf[12] < '0' || buf[12] > '8' || buf[13] != ']') return; + + //cid = x2i(buf[12]); + _con[cid].connected = false; +} + + +void WizFi310::msgListen (const char *buf) +{ + int cid; + + if(buf[8] < '0' || buf[8] > '8' || buf[9] != ']') return; + + cid = x2i(buf[8]); + _state.cid = cid; +} + +void WizFi310::resMacAddress (const char *buf) +{ + if( buf[2] == ':' && buf[5] == ':') + { + strncpy(_state.mac, buf, sizeof(_state.mac)); + _state.mac[17] = 0; + _state.res = RES_NULL; + + if(strncmp(_state.mac,CFG_DEFAULT_MAC,sizeof(CFG_DEFAULT_MAC)) == 0) + _state.ok = false; + _state.ok = true; + } +} + +void WizFi310::resConnect (const char *buf) +{ + int cid; + + if (buf[0] == '[' && buf[1] == 'O' && buf[2] == 'K' && buf[3] == ']') + { + _state.n++; + } + else if( buf[0] == '[' && buf[1] == 'C' && buf[2] == 'O' && buf[3] == 'N' && + buf[4] == 'N' && buf[5] == 'E' && buf[6] == 'C' && buf[7] == 'T') + { + cid = x2i(buf[9]); + _state.cid = cid; + _state.n++; + } + + if(_state.n >= 2) + { + _state.res = RES_NULL; + _state.ok = true; + } +} + +void WizFi310::resSSEND (const char *buf) +{ + if(_state.cid != -1) + { + _state.res = RES_NULL; + _state.ok = true; + } +} + +void WizFi310::resFDNS (const char *buf) +{ + int i; + + for(i=0; i<strlen(buf); i++) + { + if( (buf[i] < '0' || buf[i] > '9') && buf[i] != '.' ) + { + return; + } + } + + strncpy(_state.resolv, buf, sizeof(_state.resolv)); + _state.res = RES_NULL; +} + +void WizFi310::resSMGMT (const char *buf) +{ + int cid, i; + char *c; + + if( (buf[0] < '0' || buf[0] > '8') ) return; + + cid = x2i(buf[0]); + if( cid != _state.cid ) return; + + // IP + c = (char*)(buf+6); + for( i=0; i<16; i++ ) + { + if( *(c+i) == ':') + { + _con[cid].ip[i] = '\0'; + i++; + break; + } + if( ( *(c+i) < '0' || *(c+i) > '9') && *(c+i) != '.' ) return; + _con[cid].ip[i] = *(c+i); + } + + // Port + c = (c+i); + _con[cid].port = 0; + for( i=0; i<5; i++ ) + { + if( *(c+i) == '/') break; + if( *(c+i) < '0' || *(c+i) > '9' ) return; + + _con[cid].port = (_con[cid].port * 10) + ( *(c+i) - '0' ); + } + + _state.res = RES_NULL; +} + +void WizFi310::resWSTATUS (const char *buf) +{ + int idx=0,sep_cnt=0; + int ip_idx=0,gw_idx=0; + + if(_state.n == 0) + { + _state.n++; + } + else if(_state.n == 1) + { + for(idx=0;buf[idx]!='\r';idx++) + { + if(buf[idx] =='/') + { + sep_cnt++; + continue; + } + + if( sep_cnt == 2) // IP Address + { + _state.ip[ip_idx++] = buf[idx]; + } + else if(sep_cnt == 3) + { + _state.gateway[gw_idx++] = buf[idx]; + } + } + _state.ip[ip_idx] = '\0'; + _state.gateway[gw_idx] = '\0'; + _state.res = RES_NULL; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_sock.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "WizFi310.h" + +int WizFi310::getHostByName(const char * host, char *ip) +{ + int i, flg = 0; + + if(!isAssociated() || _state.status != STAT_READY) return -1; + + for(i=0; i<strlen(host); i++) + { + if( (host[i] < '0' || host[i] > '9') && host[i] != '.') + { + flg = 1; + break; + } + } + if (!flg) + { + strncpy(ip, host, 16); + return 0; + } + + if ( cmdFDNS(host) ) + { + wait_ms(1000); + if( cmdFDNS(host) ) return -1; + } + strncpy(ip, _state.resolv, 16); + return 0; +} + +int WizFi310::open(Protocol proto, const char *ip, int remotePort, int localPort, void(*func)(int)) +{ + int cid; + + if (!isAssociated() || _state.status != STAT_READY) return -1; + + _state.cid = -1; + + if (proto == PROTO_TCP) + { + if( cmdSCON( "O","TCN",ip, remotePort, localPort, "0" ) ) return -1; + } + else if(proto == PROTO_UDP) + { + if( cmdSCON( "O","UCN",ip, remotePort, localPort, "0" ) ) return -1; + } + if(_state.cid < 0) return -1; + + initCon(_state.cid, true); + cid = _state.cid; + _con[cid].protocol = proto; + _con[cid].type = TYPE_CLIENT; + _con[cid].func = func; + return cid; +} + +int WizFi310::listen (Protocol proto, int port, void(*func)(int)) +{ + int cid; + + if(!isAssociated() || _state.status != STAT_READY) return -1; + + _state.cid = -1; + + if(proto == PROTO_TCP) + { + if( sendCommand("AT+MEVTMSG=1") ) return -1; + if( cmdSCON("O","TSN",port) ) return -1; + } + else + { + if( cmdSCON("O","USN",port) ) return -1; + } + + if (_state.cid < 0) return -1; + cid = _state.cid; + _con[cid].protocol = proto; + _con[cid].type = TYPE_SERVER; + _con[cid].func = func; + + return cid; +} + +int WizFi310::close (int cid) +{ +// if(!isConnected(cid)) return -1; + + _con[cid].connected = false; + return cmdCLOSE(cid); +} + + +void WizFi310::initCon ( int cid, bool connected ) +{ + _con[cid].parent = -1; // It will be delete because It is not need + _con[cid].func = NULL; + _con[cid].accept = false; + +//#ifndef CFG_ENABLE_RTOS + if ( _con[cid].buf == NULL ) + { + _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE); + if ( _con[cid].buf == NULL ) error("Can't allocate memory"); + } +//#endif + if ( _con[cid].buf != NULL ) + { + _con[cid].buf->flush(); + } + _con[cid].connected = connected; +} + +int WizFi310::send(int cid, const char *buf, int len) +{ + if(!isConnected(cid)) return -1; + + if((_con[cid].protocol == PROTO_TCP) || + (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) ) + { +// if ( len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; + return cmdSSEND(buf,cid,len); + } + else + { + return -1; + } +} + +int WizFi310::sendto (int cid, const char *buf, int len, const char *ip, int port) +{ + if(!isConnected(cid)) return -1; + + if((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) + { + if ( len > CFG_DATA_SIZE ) len = CFG_DATA_SIZE; + return cmdSSEND(buf,cid,len,ip,port); + } + else + { + return -1; + } +} + +int WizFi310::recv (int cid, char *buf, int len) +{ + int i; + + if (!isConnected(cid)) return -1; + + if (_con[cid].buf == NULL ) return 0; + + while (!_con[cid].received && _state.mode != MODE_COMMAND); + _con[cid].received = false; + + for(i=0; i<len; i++) + { + if(_con[cid].buf->dequeue(&buf[i]) == false) break; + } + + setRts(true); // release + return i; +} + +int WizFi310::recvfrom (int cid, char *buf, int len, char *ip, int *port) +{ + int i; + + if (!isConnected(cid)) return -1; + + if (_con[cid].buf == NULL) return 0; + + while (!_con[cid].received && _state.mode != MODE_COMMAND); + + _con[cid].received = false; + for(i=0; i<len; i++) + { + if( _con[cid].buf->dequeue(&buf[i]) == false ) break; + } + //buf[i] = '\0'; + strncpy(ip, _con[cid].ip, 16); + *port = _con[cid].port; + setRts(true); // release + + return i; +} + +int WizFi310::readable (int cid) +{ + if (!isConnected(cid)) return -1; + + if(_con[cid].buf == NULL) return -1; + return _con[cid].buf->available(); +} + +bool WizFi310::isConnected (int cid) +{ + if ( cid < 0 || cid >=8 ) return false; + //printf("%d %d\r\n", cid, _con[cid].connected); + return _con[cid].connected; +} + +int WizFi310::accept (int cid) +{ + if(!isConnected(cid)) return -1; + + if(_con[cid].connected && _con[cid].accept) + { + _con[cid].accept = false; + return cid; + } + + return -1; +} + +int WizFi310::getRemote(int cid, char **ip, int *port) +{ + if (!isConnected(cid)) return -1; + + *ip = _con[cid].ip; + *port = _con[cid].port; + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310/WizFi310_util.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#include "WizFi310.h" + +int WizFi310::x2i(char c) +{ + if ( c >= '0' && c <= '9') + { + return c - '0'; + } + else if ( c >= 'A' && c <= 'F') + { + return c - 'A' + 10; + } + else if ( c >= 'a' && c <= 'f') + { + return c - 'a' + 10; + } + + return 0; +} + +int WizFi310::i2x(int i) +{ + if ( i >= 0 && i <= 9 ) + { + return i + '0'; + } + else if ( i >= 10 && i <= 15 ) + { + return i - 10 + 'A'; + } + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310Interface.cpp Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + + +#include "WizFi310Interface.h" + +WizFi310Interface::WizFi310Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud): + WizFi310(tx, rx, cts, rts, reset, alarm, baud) +{ + +} + +int WizFi310Interface::init(const char *name) +{ + return setAddress(name); +} + +int WizFi310Interface::init(const char* ip, const char* netmask, const char* gateway, const char* name) +{ + return setAddress(ip, netmask, gateway, name); +} + +int WizFi310Interface::connect(Security sec, const char* ssid, const char* phrase, WiFiMode mode) +{ + setSsid(ssid); + setSec(sec, phrase); + + switch (mode) + { + case WM_STATION: + return join(WizFi310::WM_STATION); + case WM_AP: + return join(WizFi310::WM_AP); + } + + return 0; +} + +int WizFi310Interface::disconnect() +{ + return cmdWLEAVE(); +} + +char* WizFi310Interface::getMACAddress() +{ + return _state.mac; +} + +char* WizFi310Interface::getIPAddress() +{ + return _state.ip; +} + +char* WizFi310Interface::getGateway() +{ + return _state.gateway; +} + +int WizFi310Interface::setAntMode(AntennaMode mode) +{ + return cmdWANT(mode); +} + +//char* getNetworkMask();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizFi310Interface.h Wed Apr 19 00:46:44 2017 +0000 @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013 gsfan, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +/* Copyright (C) 2014 Wiznet, MIT License + * port to the Wiznet Module WizFi250 + */ +/* Copyright (C) 2017 Wiznet, MIT License + * port to the Wiznet Module WizFi310 + */ + +#ifndef WIZFI310INTERFACE_H_ +#define WIZFI310INTERFACE_H_ + +#include "WizFi310.h" + +class WizFi310Interface : public WizFi310{ +public: + + WizFi310Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200); + + int init(const char *name = NULL); + int init(const char* ip, const char* mask, const char* gateway, const char* name = NULL); + int connect(Security sec, const char* ssid, const char* phrase, WiFiMode mode = WM_STATION); + int disconnect(); + char* getMACAddress(); + char* getIPAddress(); + char* getGateway(); + char* getNetworkMask(); + + int setAntMode(AntennaMode mode); + + +}; + + +#include "TCPSocketConnection.h" +#include "TCPSocketServer.h" + +#include "Endpoint.h" +#include "UDPSocket.h" + + +#endif /* WIZFI310INTERFACE_H_ */