The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).
Dependencies: WncControllerK64F
Dependents: WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more
See the WNCInterface README in the Wiki tab for detailed information on this library.
Revision 26:81e520908460, committed 2017-03-24
- Comitter:
- JMF
- Date:
- Fri Mar 24 21:43:57 2017 +0000
- Parent:
- 25:52bad4105cac
- Child:
- 27:2dc9461c04dc
- Commit message:
- Changing WNC Networking Class names to be prefixed with "Wnc" so they don't collide with the standard networking classes.
Changed in this revision
--- a/Socket/Endpoint.cpp Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,78 +0,0 @@
-/* =====================================================================
- 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;
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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]);
-
- M_LOCK;
- 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);
- M_ULOCK;
- return rslt;
-}
-
-char* Endpoint::get_address() {
- return _epAddr.IP;
-}
-
-int Endpoint::get_port() {
- return _epAddr.port;
-}
-
--- a/Socket/Endpoint.h Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,71 +0,0 @@
-/* =====================================================================
- 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
--- a/Socket/Socket.cpp Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-/* =====================================================================
- 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;
- M_LOCK;
- int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
- M_ULOCK;
- return ret;
-}
-
-//
-// 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];
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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]);
- M_LOCK;
- 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);
- }
- M_ULOCK;
- 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
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
- M_LOCK;
- int ret = !WNCInterface::_pwnc->closeSocket(0);
- M_ULOCK;
- return ret;
-}
-
-void Socket::set_blocking(bool blocking, unsigned int timeout) {
- blocking = blocking;
- timeout= timeout;
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
- M_LOCK;
- WNCInterface::_pwnc->setReadRetryWait(0, 0);
- WNCInterface::_pwnc->setReadRetries(0, 0);
- M_ULOCK;
-}
-
-
--- a/Socket/Socket.h Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-/* =====================================================================
- 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_ */
--- a/Socket/TCPSocketConnection.cpp Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,124 +0,0 @@
-/* =====================================================================
- 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>
-
-#define READ_EVERYMS 500 //number of milliseconds between WNC socket reads
-
-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
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
- M_LOCK;
- WNCInterface::_pwnc->setReadRetryWait(0, 0);
- WNCInterface::_pwnc->setReadRetries(0, 0);
- M_ULOCK;
-}
-
-
-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) {
- int ret = -1;
-
- WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
-
- CHK_WNCFE(( s == FATAL_FLAG ), fail);
-
- if( s == WncController_fk::WncController::WNC_ON ) {
- M_LOCK;
- if( WNCInterface::_pwnc->write(0, (const uint8_t*)data, length) )
- ret = length;
- M_ULOCK;
- }
- return ret;
-}
-
-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();
-
- CHK_WNCFE(( s == FATAL_FLAG ), fail);
- if( s != WncController_fk::WncController::WNC_ON )
- return ret;
-
- M_LOCK;
- t.start();
- do {
- if( !(t.read_ms() % READ_EVERYMS) )
- 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();
- M_ULOCK;
-
- 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();
- M_LOCK;
- int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
- M_ULOCK;
- return ret;
-}
-
--- a/Socket/TCPSocketConnection.h Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-/* =====================================================================
- 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
--- a/Socket/UDPSocket.cpp Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,105 +0,0 @@
-/* =====================================================================
- 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;
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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,(const uint8_t*)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
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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;
-
- CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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;
-}
-
-
--- a/Socket/UDPSocket.h Thu Mar 09 01:00:12 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,77 +0,0 @@
-/* =====================================================================
- 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
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/WncEndpoint.cpp Fri Mar 24 21:43:57 2017 +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 "WncSocket.h"
+#include "WncEndpoint.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;
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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]);
+
+ M_LOCK;
+ 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);
+ M_ULOCK;
+ 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/WncEndpoint.h Fri Mar 24 21:43:57 2017 +0000
@@ -0,0 +1,72 @@
+/* =====================================================================
+ 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 ENDPOINT_H
+#define ENDPOINT_H
+
+#include "WncSocket.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/WncSocket.cpp Fri Mar 24 21:43:57 2017 +0000
@@ -0,0 +1,114 @@
+/* =====================================================================
+ 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 "WncSocket.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;
+ M_LOCK;
+ int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
+ M_ULOCK;
+ return ret;
+}
+
+//
+// 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];
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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]);
+ M_LOCK;
+ 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);
+ }
+ M_ULOCK;
+ 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
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
+ M_LOCK;
+ int ret = !WNCInterface::_pwnc->closeSocket(0);
+ M_ULOCK;
+ return ret;
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout) {
+ blocking = blocking;
+ timeout= timeout;
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
+ M_LOCK;
+ WNCInterface::_pwnc->setReadRetryWait(0, 0);
+ WNCInterface::_pwnc->setReadRetries(0, 0);
+ M_ULOCK;
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/WncSocket.h Fri Mar 24 21:43:57 2017 +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/WncTCPSocketConnection.cpp Fri Mar 24 21:43:57 2017 +0000
@@ -0,0 +1,124 @@
+/* =====================================================================
+ 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 "WncSocket.h"
+#include "WncTCPSocketConnection.h"
+#include <cstring>
+
+#define READ_EVERYMS 500 //number of milliseconds between WNC socket reads
+
+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
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
+ M_LOCK;
+ WNCInterface::_pwnc->setReadRetryWait(0, 0);
+ WNCInterface::_pwnc->setReadRetries(0, 0);
+ M_ULOCK;
+}
+
+
+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) {
+ int ret = -1;
+
+ WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
+
+ CHK_WNCFE(( s == FATAL_FLAG ), fail);
+
+ if( s == WncController_fk::WncController::WNC_ON ) {
+ M_LOCK;
+ if( WNCInterface::_pwnc->write(0, (const uint8_t*)data, length) )
+ ret = length;
+ M_ULOCK;
+ }
+ return ret;
+}
+
+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();
+
+ CHK_WNCFE(( s == FATAL_FLAG ), fail);
+ if( s != WncController_fk::WncController::WNC_ON )
+ return ret;
+
+ M_LOCK;
+ t.start();
+ do {
+ if( !(t.read_ms() % READ_EVERYMS) )
+ 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();
+ M_ULOCK;
+
+ 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();
+ M_LOCK;
+ int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
+ M_ULOCK;
+ return ret;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/WncTCPSocketConnection.h Fri Mar 24 21:43:57 2017 +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 "WncSocket.h"
+#include "WncEndpoint.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/WncUDPSocket.cpp Fri Mar 24 21:43:57 2017 +0000
@@ -0,0 +1,105 @@
+/* =====================================================================
+ 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 "WncUDPSocket.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;
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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,(const uint8_t*)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
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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;
+
+ CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), 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/WncUDPSocket.h Fri Mar 24 21:43:57 2017 +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 "WncSocket.h"
+#include "WncEndpoint.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
+

