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.
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 1:e511ea8d39d5
- Child:
- 7:fded23f50479
--- /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; +} +