Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WncControllerK64F
Fork of WNCInterface by
Revision 1:e511ea8d39d5, committed 2016-09-21
- Comitter:
- JMF
- Date:
- Wed Sep 21 15:20:12 2016 +0000
- Parent:
- 0:55ec71dc0347
- Child:
- 2:dab56b75d4dd
- Commit message:
- Adding in all the Sockets, missed them on the initial commit.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.cpp Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,78 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+
+#include "../WNCInterface.h"
+#include "Socket.h"
+#include "Endpoint.h"
+
+Endpoint::Endpoint() {
+ reset_address();
+}
+
+Endpoint::~Endpoint() {}
+
+void Endpoint::reset_address(void) {
+ std::memset(&_epAddr, 0, sizeof(struct EndPointAddr));
+}
+
+//
+// It is possible to call set_address with either a URL or
+// an IP address. So try each in-turn and set the end point
+// address.
+//
+
+int Endpoint::set_address(const char* host, const int port) {
+ // IP Address
+ char address[5];
+ int rslt;
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+ reset_address();
+ _epAddr.port = port; //go ahead and save the port
+
+ // Dot-decimal notation?
+ rslt = std::sscanf(host, "%3u.%3u.%3u.%3u",
+ (unsigned int*)&address[0], (unsigned int*)&address[1],
+ (unsigned int*)&address[2], (unsigned int*)&address[3]);
+
+ if (rslt != 4) // No, need to resolve address with DNS
+ WNCInterface::_pwnc->resolveUrl(0,host);
+ else
+ WNCInterface::_pwnc->setIpAddr(0,host);
+
+ rslt = WNCInterface::_pwnc->getIpAddr(0,_epAddr.IP);
+
+ return rslt;
+}
+
+char* Endpoint::get_address() {
+ return _epAddr.IP;
+}
+
+int Endpoint::get_port() {
+ return _epAddr.port;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.h Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,71 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+
+#include "Socket.h"
+
+#ifndef ENDPOINT_H
+#define ENDPOINT_H
+
+struct EndPointAddr {
+ char IP[16];
+ unsigned int port;
+ };
+
+class UDPSocket;
+
+class Endpoint {
+ friend class UDPSocket;
+
+public:
+ Endpoint(void);
+ ~Endpoint(void);
+
+ /** Reset the address of the endpoint by clearning the internal endpoint IP address
+ \param none
+ \return none.
+ */
+ void reset_address(void);
+
+ /** Set the address of the 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 the endpoint
+ \return The IP address of the endpoint.
+ */
+ char* get_address(void);
+
+ /** Get the port of the endpoint
+ \return The port of the endpoint
+ */
+ int get_port(void);
+
+private:
+ EndPointAddr _epAddr;
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.cpp Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,110 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+#include "../WNCInterface.h"
+#include "Socket.h"
+#include <cstring>
+
+class WNCInterface;
+
+//
+// Set up the defaults in the constructor. If the caller doesn't change anything
+// the APN will be set for AT&T, port #40 and timeout 1.5 seconds
+//
+Socket::Socket() :
+ _sock_type(-1),
+ _timeout(1500) {
+}
+
+Socket::~Socket() {
+}
+
+
+//
+// ensure we have a WNC Controller attached and initialized by calling to get the
+// network status, This will provide us with all the network information. if we
+// are not connected, will return -1.
+//
+int Socket::init(int timeout) {
+
+ _timeout = timeout;
+ return (WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats))? 0:-1;
+}
+
+//
+// Connect this socket to a user specified URL or IP address. It could be
+// either a TCP or UDP socket. The user is also expected to provide a port #.
+// If the connection failed for any reason return 0, otherwise, return 1;
+//
+int Socket::connect(char *url, int type, int port) {
+ int rslt;
+ char address[5];
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+ // lets determine if they passed in an IP or a URL
+ rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
+ (unsigned int*)&address[0], (unsigned int*)&address[1],
+ (unsigned int*)&address[2], (unsigned int*)&address[3]);
+
+ if (rslt == 4)
+ rslt = WNCInterface::_pwnc->setIpAddr(0,url);
+ else
+ rslt = WNCInterface::_pwnc->resolveUrl(0,url);
+
+ if( rslt ) {
+ _sock_type = type; //resolved the URL indicate socket 0 is open
+ rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
+ }
+ return rslt;
+}
+
+
+//
+// disconnect the currently open socket.
+// -1 if there was an error
+// 0 if we disconnected
+//
+int Socket::disconnect() {
+ if( _sock_type<0 )
+ return 0; //nothing is connected currently
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+ return !WNCInterface::_pwnc->closeSocket(0);
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout) {
+ blocking = blocking;
+ timeout= timeout;
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(void);
+
+ WNCInterface::_pwnc->setReadRetryWait(0, 0);
+ WNCInterface::_pwnc->setReadRetries(0, 0);
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.h Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,54 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+
+
+#include <stddef.h>
+#include "WNCInterface.h"
+
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+#define SOCK_STREAM 1 //A TCP Socket type
+#define SOCK_DGRAM 2 //a UDP Socket type
+
+/** Socket file descriptor and select wrapper */
+class Socket {
+
+public:
+ Socket();
+ ~Socket();
+
+ int init(int timeout=1500);
+
+ int connect(char *url, int type, int port);
+ int disconnect();
+ void set_blocking(bool blocking, unsigned int timeout); //not used
+
+private:
+ int _sock_type; //contains the type of socket this is
+ unsigned int _timeout; //default timeout for all socket transactions
+};
+
+
+#endif /* SOCKET_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.cpp Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,118 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+======================================================================== */
+
+#include "../WNCInterface.h"
+
+#include "Socket.h"
+#include "TCPSocketConnection.h"
+#include <cstring>
+
+TCPSocketConnection::TCPSocketConnection() :
+ _is_blocking(0),
+ _btimeout(0){
+}
+
+//
+// blocking is used to make the WNC keep checking for incoming data for a
+// period of time.
+//
+void TCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) {
+ _is_blocking = blocking; // true if we want to wait for request
+ _btimeout = timeout; // user specs msec
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(void);
+
+ WNCInterface::_pwnc->setReadRetryWait(0, 0);
+ WNCInterface::_pwnc->setReadRetries(0, 0);
+}
+
+
+int TCPSocketConnection::connect(const char* host, const int port) {
+ Socket::connect((char*)host, SOCK_STREAM, port);
+ _is_blocking = false; // start out not blocking, user will set it if desired
+ return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
+}
+
+bool TCPSocketConnection::is_connected(void) {
+ return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0;
+}
+
+int TCPSocketConnection::send(char* data, int length) {
+ WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
+
+ if( s == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+ if( s != WncController_fk::WncController::WNC_ON )
+ return -1;
+
+ if( WNCInterface::_pwnc->write(0, data, length) )
+ return length;
+ else
+ return -1;
+}
+
+int TCPSocketConnection::receive(char *readBuf, int length) {
+ Timer t;
+ size_t done, cnt;
+ int ret=-1;
+ WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
+
+ if( s == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+
+ if( s != WncController_fk::WncController::WNC_ON )
+ return ret;
+
+ t.start();
+ do {
+ cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
+ if( _is_blocking )
+ done = cnt;
+ else
+ done = cnt | (t.read_ms() > _btimeout);
+ }
+ while( !done );
+ t.stop();
+
+ if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
+ readBuf[cnt] = '\0';
+ ret = (int)cnt;
+ }
+ else
+ ret = -1;
+
+ return ret;
+}
+
+int TCPSocketConnection::send_all(char* data, int length) {
+ return send(data,length);
+}
+
+int TCPSocketConnection::receive_all(char* data, int length) {
+ return receive(data,length);
+}
+
+int TCPSocketConnection::close(void) {
+ Socket::disconnect();
+ return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.h Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,97 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+
+#ifndef TCPSOCKET_H
+#define TCPSOCKET_H
+
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+
+/**
+TCP socket connection
+*/
+class TCPSocketConnection : public Socket, public Endpoint {
+
+public:
+ 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);
+
+ /** Set blocking or non-blocking mode of the socket and a timeout
+ \param blocking true for blocking mode, false for non-blocking mode.
+ \return none
+ */
+ void set_blocking (bool blocking, unsigned int timeout=1500);
+
+ /** Close the socket
+ \param none
+ \return 0 if closed successfully, -1 on failure
+ */
+ int close(void);
+
+private:
+ bool _is_blocking;
+ unsigned int _btimeout;
+
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/UDPSocket.cpp Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,111 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+
+#include "../WNCInterface.h"
+
+#include "UDPSocket.h"
+#include <cstring>
+
+UDPSocket::UDPSocket() :
+ _is_blocking(0),
+ _btimeout(0){
+}
+
+UDPSocket::~UDPSocket() {
+}
+
+int UDPSocket::init(void) {
+ _is_blocking = false; // start out not blocking, user will set it if desired
+ return ( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD )? 0:-1;
+}
+
+
+int UDPSocket::close(void) {
+ Socket::disconnect();
+ return ( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD )? 0:-1;
+}
+
+// -1 if unsuccessful, else number of bytes written
+int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
+ int ret = -1;
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+ if( remote._epAddr.port ) { //make sure the Endpoint has an port assoicated with it
+ if( Socket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port) ) {
+ if( WNCInterface::_pwnc->write(0,packet,length) )
+ ret = length;
+ close();
+ }
+ }
+ return ret;
+}
+
+//
+// blocking is used to make the WNC keep checking for incoming data for a
+// period of time.
+
+void UDPSocket::set_blocking (bool blocking, unsigned int timeout) {
+ _is_blocking = blocking; // true or false
+ _btimeout = timeout; // user specifies in msec
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(void);
+
+ WNCInterface::_pwnc->setReadRetryWait(0, 0);
+ WNCInterface::_pwnc->setReadRetries(0, 0);
+}
+
+// -1 if unsuccessful, else number of bytes received
+int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
+ const uint8_t *ptr;
+ Timer t;
+ int done, ret = -1;
+
+ //make sure the Endpoint has an port assoicated with it
+ if( !remote._epAddr.port )
+ return -1;
+
+ if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
+ FATAL_WNC_ERROR(fail);
+
+ ret = Socket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port);
+
+ t.start();
+ do {
+ ret = WNCInterface::_pwnc->read(0, &ptr);
+ done = ret | (t.read_ms() > _btimeout);
+ }
+ while( _is_blocking && !done );
+ t.stop();
+
+ if( ret > length )
+ ret = length;
+ memcpy( buffer, ptr, ret );
+
+ return ret;
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/UDPSocket.h Wed Sep 21 15:20:12 2016 +0000
@@ -0,0 +1,77 @@
+/* =====================================================================
+ Copyright © 2016, Avnet (R)
+
+ Contributors:
+ * James M Flynn, www.em.avnet.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ @file WNCInterface.cpp
+ @version 1.0
+ @date Sept 2016
+
+======================================================================== */
+
+
+#ifndef UDPSOCKET_H
+#define UDPSOCKET_H
+
+#include "Socket.h"
+#include "Endpoint.h"
+
+/**
+UDP Socket
+*/
+class UDPSocket : public Socket, public WNCInterface {
+
+public:
+ UDPSocket();
+ ~UDPSocket();
+
+ int init(void);
+
+ /** sendTo - send data to the remote host.
+ \param remote, a pointer to the endpoint (class)
+ \param packet, pointer to 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 sendTo(Endpoint &remote, char *packet, int length);
+
+ /** receiveFrom - receive data from the remote host.
+ \param remote, a pointer to the endpoint (class)
+ \param packet, 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 receiveFrom(Endpoint &remote, char *buffer, int length);
+
+ /** Set blocking or non-blocking mode of the socket and a timeout
+ \param blocking true for blocking mode, false for non-blocking mode.
+ \return none.
+ */
+ void set_blocking (bool blocking, unsigned int timeout=1500);
+
+ /** Close the socket
+ \param none
+ \return 0 if closed successfully, -1 on failure
+ */
+ int close(void);
+
+private:
+ bool _is_blocking;
+ unsigned int _btimeout;
+};
+
+#endif
+
