WNCInterface

Dependencies:   WncControllerK64F

Fork of WNCInterface by Avnet

Committer:
JMF
Date:
Thu Oct 06 21:17:18 2016 +0000
Revision:
7:fded23f50479
Parent:
1:e511ea8d39d5
Child:
9:9f0578ff157a
This version adds the SMS class and adds support for multi-threading to eliminate  re-entrant with the WncController class

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 1:e511ea8d39d5 39 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 40 FATAL_WNC_ERROR(void);
JMF 7:fded23f50479 41 M_LOCK;
JMF 1:e511ea8d39d5 42 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 43 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 7:fded23f50479 44 M_ULOCK;
JMF 1:e511ea8d39d5 45 }
JMF 1:e511ea8d39d5 46
JMF 1:e511ea8d39d5 47
JMF 1:e511ea8d39d5 48 int TCPSocketConnection::connect(const char* host, const int port) {
JMF 1:e511ea8d39d5 49 Socket::connect((char*)host, SOCK_STREAM, port);
JMF 1:e511ea8d39d5 50 _is_blocking = false; // start out not blocking, user will set it if desired
JMF 1:e511ea8d39d5 51 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 1:e511ea8d39d5 52 }
JMF 1:e511ea8d39d5 53
JMF 1:e511ea8d39d5 54 bool TCPSocketConnection::is_connected(void) {
JMF 1:e511ea8d39d5 55 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0;
JMF 1:e511ea8d39d5 56 }
JMF 1:e511ea8d39d5 57
JMF 1:e511ea8d39d5 58 int TCPSocketConnection::send(char* data, int length) {
JMF 7:fded23f50479 59 int ret = -1;
JMF 7:fded23f50479 60
JMF 1:e511ea8d39d5 61 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 62
JMF 1:e511ea8d39d5 63 if( s == FATAL_FLAG )
JMF 1:e511ea8d39d5 64 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 65
JMF 7:fded23f50479 66 if( s == WncController_fk::WncController::WNC_ON ) {
JMF 7:fded23f50479 67 M_LOCK;
JMF 7:fded23f50479 68 if( WNCInterface::_pwnc->write(0, data, length) )
JMF 7:fded23f50479 69 ret = length;
JMF 7:fded23f50479 70 M_ULOCK;
JMF 7:fded23f50479 71 }
JMF 7:fded23f50479 72 return ret;
JMF 1:e511ea8d39d5 73 }
JMF 1:e511ea8d39d5 74
JMF 1:e511ea8d39d5 75 int TCPSocketConnection::receive(char *readBuf, int length) {
JMF 1:e511ea8d39d5 76 Timer t;
JMF 1:e511ea8d39d5 77 size_t done, cnt;
JMF 1:e511ea8d39d5 78 int ret=-1;
JMF 1:e511ea8d39d5 79 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 80
JMF 1:e511ea8d39d5 81 if( s == FATAL_FLAG )
JMF 1:e511ea8d39d5 82 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 83
JMF 1:e511ea8d39d5 84
JMF 1:e511ea8d39d5 85 if( s != WncController_fk::WncController::WNC_ON )
JMF 1:e511ea8d39d5 86 return ret;
JMF 1:e511ea8d39d5 87
JMF 1:e511ea8d39d5 88 t.start();
JMF 7:fded23f50479 89 M_LOCK;
JMF 1:e511ea8d39d5 90 do {
JMF 1:e511ea8d39d5 91 cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
JMF 1:e511ea8d39d5 92 if( _is_blocking )
JMF 1:e511ea8d39d5 93 done = cnt;
JMF 1:e511ea8d39d5 94 else
JMF 1:e511ea8d39d5 95 done = cnt | (t.read_ms() > _btimeout);
JMF 1:e511ea8d39d5 96 }
JMF 1:e511ea8d39d5 97 while( !done );
JMF 1:e511ea8d39d5 98 t.stop();
JMF 7:fded23f50479 99 M_ULOCK;
JMF 7:fded23f50479 100
JMF 1:e511ea8d39d5 101 if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
JMF 1:e511ea8d39d5 102 readBuf[cnt] = '\0';
JMF 1:e511ea8d39d5 103 ret = (int)cnt;
JMF 1:e511ea8d39d5 104 }
JMF 1:e511ea8d39d5 105 else
JMF 1:e511ea8d39d5 106 ret = -1;
JMF 1:e511ea8d39d5 107
JMF 1:e511ea8d39d5 108 return ret;
JMF 1:e511ea8d39d5 109 }
JMF 1:e511ea8d39d5 110
JMF 1:e511ea8d39d5 111 int TCPSocketConnection::send_all(char* data, int length) {
JMF 1:e511ea8d39d5 112 return send(data,length);
JMF 1:e511ea8d39d5 113 }
JMF 1:e511ea8d39d5 114
JMF 1:e511ea8d39d5 115 int TCPSocketConnection::receive_all(char* data, int length) {
JMF 1:e511ea8d39d5 116 return receive(data,length);
JMF 1:e511ea8d39d5 117 }
JMF 1:e511ea8d39d5 118
JMF 1:e511ea8d39d5 119 int TCPSocketConnection::close(void) {
JMF 1:e511ea8d39d5 120 Socket::disconnect();
JMF 7:fded23f50479 121 M_LOCK;
JMF 7:fded23f50479 122 int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 7:fded23f50479 123 M_ULOCK;
JMF 7:fded23f50479 124 return ret;
JMF 1:e511ea8d39d5 125 }
JMF 1:e511ea8d39d5 126