V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WNCTCPSocketConnection.cpp Source File

WNCTCPSocketConnection.cpp

00001 /* =====================================================================
00002    Copyright © 2016, Avnet (R)
00003    Contributors:
00004      * James M Flynn, www.em.avnet.com 
00005  
00006    Licensed under the Apache License, Version 2.0 (the "License"); 
00007    you may not use this file except in compliance with the License.
00008    You may obtain a copy of the License at
00009     http://www.apache.org/licenses/LICENSE-2.0
00010    Unless required by applicable law or agreed to in writing, 
00011    software distributed under the License is distributed on an 
00012    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
00013    either express or implied. See the License for the specific 
00014    language governing permissions and limitations under the License.
00015     @file          WNCInterface.cpp
00016     @version       1.0
00017     @date          Sept 2016
00018 ======================================================================== */
00019 
00020 #include "../WNCInterface.h"
00021 
00022 #include "WNCSocket.h"
00023 #include "WNCTCPSocketConnection.h"
00024 #include <cstring>
00025 
00026 #define READ_EVERYMS   500     //number of milliseconds between WNC socket reads
00027 
00028 WNCTCPSocketConnection::WNCTCPSocketConnection() :
00029         _is_blocking(0),
00030         _btimeout(0){
00031 }
00032 
00033 //
00034 // blocking is used to make the WNC keep checking for incoming data for a
00035 // period of time.
00036 //
00037 void WNCTCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) {
00038     _is_blocking = blocking;   // true if we want to wait for request
00039     _btimeout = timeout;       // user specs msec
00040 
00041     CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
00042     M_LOCK;
00043     WNCInterface::_pwnc->setReadRetryWait(0, 0);
00044     WNCInterface::_pwnc->setReadRetries(0, 0);
00045     M_ULOCK;
00046 }
00047 
00048 
00049 int WNCTCPSocketConnection::connect(const char* host, const int port) {
00050     WNCSocket::connect((char*)host, SOCK_STREAM, port);
00051     _is_blocking = false;   // start out not blocking, user will set it if desired
00052     return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
00053 }
00054 
00055 bool WNCTCPSocketConnection::is_connected(void) {
00056     return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0;
00057 }
00058 
00059 int WNCTCPSocketConnection::send(char* data, int length) {
00060     int ret = -1;
00061     
00062     WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
00063 
00064     CHK_WNCFE(( s == FATAL_FLAG ), fail);
00065  
00066     if( s == WncController_fk::WncController::WNC_ON ) {
00067       M_LOCK;
00068       if( WNCInterface::_pwnc->write(0, data, length) )
00069         ret = length;
00070       M_ULOCK;
00071       }
00072     return ret;
00073 }
00074 
00075 int WNCTCPSocketConnection::receive(char *readBuf, int length) {
00076     Timer t;
00077     size_t done, cnt;
00078     int ret=-1;
00079     WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
00080 
00081     CHK_WNCFE(( s  == FATAL_FLAG ), fail);
00082     if( s != WncController_fk::WncController::WNC_ON )
00083         return ret;
00084 
00085     M_LOCK;
00086     t.start();
00087     do {
00088         if( !(t.read_ms() % READ_EVERYMS) )
00089           cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
00090         if( _is_blocking )
00091             done = cnt;
00092         else
00093             done = cnt | (t.read_ms() > _btimeout);
00094         }
00095     while( !done );
00096     t.stop();
00097     M_ULOCK;
00098     
00099     if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
00100         //readBuf[cnt] = '\0';
00101         ret = (int)cnt;
00102         }
00103     else
00104         ret = -1;
00105     
00106     return ret;
00107 }
00108 
00109 int WNCTCPSocketConnection::send_all(char* data, int length) {
00110   return send(data,length);
00111 }
00112 
00113 int WNCTCPSocketConnection::receive_all(char* data, int length) {
00114   return receive(data,length);
00115 }
00116 
00117 int WNCTCPSocketConnection::close(void) {
00118   WNCSocket::disconnect();
00119   M_LOCK;
00120   int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
00121   M_ULOCK;
00122   return ret;
00123 }
00124 
00125