My fork

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

Committer:
JMF
Date:
Fri Oct 07 13:26:00 2016 +0000
Revision:
9:9f0578ff157a
Parent:
7:fded23f50479
Child:
11:75cf1e1c921c
Cleaned up WNC Error checking to ensure mutex was released when an error occurs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 1:e511ea8d39d5 1 /* =====================================================================
JMF 1:e511ea8d39d5 2 Copyright © 2016, Avnet (R)
JMF 1:e511ea8d39d5 3 Contributors:
JMF 1:e511ea8d39d5 4 * James M Flynn, www.em.avnet.com
JMF 1:e511ea8d39d5 5
JMF 1:e511ea8d39d5 6 Licensed under the Apache License, Version 2.0 (the "License");
JMF 1:e511ea8d39d5 7 you may not use this file except in compliance with the License.
JMF 1:e511ea8d39d5 8 You may obtain a copy of the License at
JMF 1:e511ea8d39d5 9 http://www.apache.org/licenses/LICENSE-2.0
JMF 1:e511ea8d39d5 10 Unless required by applicable law or agreed to in writing,
JMF 1:e511ea8d39d5 11 software distributed under the License is distributed on an
JMF 1:e511ea8d39d5 12 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
JMF 1:e511ea8d39d5 13 either express or implied. See the License for the specific
JMF 1:e511ea8d39d5 14 language governing permissions and limitations under the License.
JMF 1:e511ea8d39d5 15 @file WNCInterface.cpp
JMF 1:e511ea8d39d5 16 @version 1.0
JMF 1:e511ea8d39d5 17 @date Sept 2016
JMF 1:e511ea8d39d5 18 ======================================================================== */
JMF 1:e511ea8d39d5 19
JMF 1:e511ea8d39d5 20 #include "../WNCInterface.h"
JMF 1:e511ea8d39d5 21
JMF 1:e511ea8d39d5 22 #include "Socket.h"
JMF 1:e511ea8d39d5 23 #include "TCPSocketConnection.h"
JMF 1:e511ea8d39d5 24 #include <cstring>
JMF 1:e511ea8d39d5 25
JMF 1:e511ea8d39d5 26 TCPSocketConnection::TCPSocketConnection() :
JMF 1:e511ea8d39d5 27 _is_blocking(0),
JMF 1:e511ea8d39d5 28 _btimeout(0){
JMF 1:e511ea8d39d5 29 }
JMF 1:e511ea8d39d5 30
JMF 1:e511ea8d39d5 31 //
JMF 1:e511ea8d39d5 32 // blocking is used to make the WNC keep checking for incoming data for a
JMF 1:e511ea8d39d5 33 // period of time.
JMF 1:e511ea8d39d5 34 //
JMF 1:e511ea8d39d5 35 void TCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) {
JMF 1:e511ea8d39d5 36 _is_blocking = blocking; // true if we want to wait for request
JMF 1:e511ea8d39d5 37 _btimeout = timeout; // user specs msec
JMF 1:e511ea8d39d5 38
JMF 9:9f0578ff157a 39 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
JMF 7:fded23f50479 40 M_LOCK;
JMF 1:e511ea8d39d5 41 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 42 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 7:fded23f50479 43 M_ULOCK;
JMF 1:e511ea8d39d5 44 }
JMF 1:e511ea8d39d5 45
JMF 1:e511ea8d39d5 46
JMF 1:e511ea8d39d5 47 int TCPSocketConnection::connect(const char* host, const int port) {
JMF 1:e511ea8d39d5 48 Socket::connect((char*)host, SOCK_STREAM, port);
JMF 1:e511ea8d39d5 49 _is_blocking = false; // start out not blocking, user will set it if desired
JMF 1:e511ea8d39d5 50 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 1:e511ea8d39d5 51 }
JMF 1:e511ea8d39d5 52
JMF 1:e511ea8d39d5 53 bool TCPSocketConnection::is_connected(void) {
JMF 1:e511ea8d39d5 54 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0;
JMF 1:e511ea8d39d5 55 }
JMF 1:e511ea8d39d5 56
JMF 1:e511ea8d39d5 57 int TCPSocketConnection::send(char* data, int length) {
JMF 7:fded23f50479 58 int ret = -1;
JMF 7:fded23f50479 59
JMF 1:e511ea8d39d5 60 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 61
JMF 9:9f0578ff157a 62 CHK_WNCFE(( s == FATAL_FLAG ), fail);
JMF 1:e511ea8d39d5 63
JMF 7:fded23f50479 64 if( s == WncController_fk::WncController::WNC_ON ) {
JMF 7:fded23f50479 65 M_LOCK;
JMF 7:fded23f50479 66 if( WNCInterface::_pwnc->write(0, data, length) )
JMF 7:fded23f50479 67 ret = length;
JMF 7:fded23f50479 68 M_ULOCK;
JMF 7:fded23f50479 69 }
JMF 7:fded23f50479 70 return ret;
JMF 1:e511ea8d39d5 71 }
JMF 1:e511ea8d39d5 72
JMF 1:e511ea8d39d5 73 int TCPSocketConnection::receive(char *readBuf, int length) {
JMF 1:e511ea8d39d5 74 Timer t;
JMF 1:e511ea8d39d5 75 size_t done, cnt;
JMF 1:e511ea8d39d5 76 int ret=-1;
JMF 1:e511ea8d39d5 77 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 78
JMF 9:9f0578ff157a 79 CHK_WNCFE(( s == FATAL_FLAG ), fail);
JMF 1:e511ea8d39d5 80 if( s != WncController_fk::WncController::WNC_ON )
JMF 1:e511ea8d39d5 81 return ret;
JMF 1:e511ea8d39d5 82
JMF 1:e511ea8d39d5 83 t.start();
JMF 7:fded23f50479 84 M_LOCK;
JMF 1:e511ea8d39d5 85 do {
JMF 1:e511ea8d39d5 86 cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
JMF 1:e511ea8d39d5 87 if( _is_blocking )
JMF 1:e511ea8d39d5 88 done = cnt;
JMF 1:e511ea8d39d5 89 else
JMF 1:e511ea8d39d5 90 done = cnt | (t.read_ms() > _btimeout);
JMF 1:e511ea8d39d5 91 }
JMF 1:e511ea8d39d5 92 while( !done );
JMF 1:e511ea8d39d5 93 t.stop();
JMF 7:fded23f50479 94 M_ULOCK;
JMF 7:fded23f50479 95
JMF 1:e511ea8d39d5 96 if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
JMF 1:e511ea8d39d5 97 readBuf[cnt] = '\0';
JMF 1:e511ea8d39d5 98 ret = (int)cnt;
JMF 1:e511ea8d39d5 99 }
JMF 1:e511ea8d39d5 100 else
JMF 1:e511ea8d39d5 101 ret = -1;
JMF 1:e511ea8d39d5 102
JMF 1:e511ea8d39d5 103 return ret;
JMF 1:e511ea8d39d5 104 }
JMF 1:e511ea8d39d5 105
JMF 1:e511ea8d39d5 106 int TCPSocketConnection::send_all(char* data, int length) {
JMF 1:e511ea8d39d5 107 return send(data,length);
JMF 1:e511ea8d39d5 108 }
JMF 1:e511ea8d39d5 109
JMF 1:e511ea8d39d5 110 int TCPSocketConnection::receive_all(char* data, int length) {
JMF 1:e511ea8d39d5 111 return receive(data,length);
JMF 1:e511ea8d39d5 112 }
JMF 1:e511ea8d39d5 113
JMF 1:e511ea8d39d5 114 int TCPSocketConnection::close(void) {
JMF 1:e511ea8d39d5 115 Socket::disconnect();
JMF 7:fded23f50479 116 M_LOCK;
JMF 7:fded23f50479 117 int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 7:fded23f50479 118 M_ULOCK;
JMF 7:fded23f50479 119 return ret;
JMF 1:e511ea8d39d5 120 }
JMF 1:e511ea8d39d5 121