Avnet / WNCInterface

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

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 
00029 //
00030 // blocking is used to make the WNC keep checking for incoming data for a
00031 // period of time.
00032 //
00033 void WncTCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) {
00034     _is_blocking = blocking;   // true if we want to wait for request
00035     _btimeout = timeout;       // user specs msec
00036 
00037     CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
00038     M_LOCK;
00039     WNCInterface::_pwnc->setReadRetryWait(0, 0);
00040     WNCInterface::_pwnc->setReadRetries(0, 0);
00041     M_ULOCK;
00042 }
00043 
00044 
00045 int WncTCPSocketConnection::connect(const char* host, const int port) {
00046     WncSocket::connect((char*)host, SOCK_STREAM, port);
00047     _is_blocking = false;   // start out not blocking, user will set it if desired
00048     return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
00049 }
00050 
00051 bool WncTCPSocketConnection::is_connected(void) {
00052     return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0;
00053 }
00054 
00055 int WncTCPSocketConnection::send(char* data, int length) {
00056     int ret = -1;
00057     
00058     WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
00059 
00060     CHK_WNCFE(( s == FATAL_FLAG ), fail);
00061  
00062     if( s == WncController_fk::WncController::WNC_ON ) {
00063       M_LOCK;
00064       if( WNCInterface::_pwnc->write(0, (const uint8_t*)data, length) )
00065         ret = length;
00066       M_ULOCK;
00067       }
00068     return ret;
00069 }
00070 
00071 int WncTCPSocketConnection::receive(char *readBuf, int length) {
00072     Timer t;
00073     size_t done, cnt;
00074     int ret=-1;
00075     WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
00076 
00077     CHK_WNCFE(( s  == FATAL_FLAG ), fail);
00078     if( s != WncController_fk::WncController::WNC_ON )
00079         return ret;
00080 
00081     M_LOCK;
00082     t.start();
00083     do {
00084         if( !(t.read_ms() % READ_EVERYMS) )
00085           cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
00086         if( _is_blocking )
00087             done = cnt;
00088         else
00089             done = cnt | (t.read_ms() > _btimeout);
00090         }
00091     while( !done );
00092     t.stop();
00093     M_ULOCK;
00094     
00095     if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
00096         //readBuf[cnt] = '\0';
00097         ret = (int)cnt;
00098         }
00099     else
00100         ret = -1;
00101     
00102     return ret;
00103 }
00104 
00105 int WncTCPSocketConnection::send_all(char* data, int length) {
00106   return send(data,length);
00107 }
00108 
00109 int WncTCPSocketConnection::receive_all(char* data, int length) {
00110   return receive(data,length);
00111 }
00112 
00113 int WncTCPSocketConnection::close(void) {
00114   WncSocket::disconnect();
00115   M_LOCK;
00116   int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
00117   M_ULOCK;
00118   return ret;
00119 }
00120