publish final code

Dependencies:   WncControllerK64F

Fork of WNCInterface by Avnet

Committer:
JMF
Date:
Wed Sep 21 15:20:12 2016 +0000
Revision:
1:e511ea8d39d5
Child:
7:fded23f50479
Adding in all the Sockets, missed them on the initial commit.

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 1:e511ea8d39d5 41
JMF 1:e511ea8d39d5 42 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 43 WNCInterface::_pwnc->setReadRetries(0, 0);
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 1:e511ea8d39d5 58 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 59
JMF 1:e511ea8d39d5 60 if( s == FATAL_FLAG )
JMF 1:e511ea8d39d5 61 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 62
JMF 1:e511ea8d39d5 63 if( s != WncController_fk::WncController::WNC_ON )
JMF 1:e511ea8d39d5 64 return -1;
JMF 1:e511ea8d39d5 65
JMF 1:e511ea8d39d5 66 if( WNCInterface::_pwnc->write(0, data, length) )
JMF 1:e511ea8d39d5 67 return length;
JMF 1:e511ea8d39d5 68 else
JMF 1:e511ea8d39d5 69 return -1;
JMF 1:e511ea8d39d5 70 }
JMF 1:e511ea8d39d5 71
JMF 1:e511ea8d39d5 72 int TCPSocketConnection::receive(char *readBuf, int length) {
JMF 1:e511ea8d39d5 73 Timer t;
JMF 1:e511ea8d39d5 74 size_t done, cnt;
JMF 1:e511ea8d39d5 75 int ret=-1;
JMF 1:e511ea8d39d5 76 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 77
JMF 1:e511ea8d39d5 78 if( s == FATAL_FLAG )
JMF 1:e511ea8d39d5 79 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 80
JMF 1:e511ea8d39d5 81
JMF 1:e511ea8d39d5 82 if( s != WncController_fk::WncController::WNC_ON )
JMF 1:e511ea8d39d5 83 return ret;
JMF 1:e511ea8d39d5 84
JMF 1:e511ea8d39d5 85 t.start();
JMF 1:e511ea8d39d5 86 do {
JMF 1:e511ea8d39d5 87 cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
JMF 1:e511ea8d39d5 88 if( _is_blocking )
JMF 1:e511ea8d39d5 89 done = cnt;
JMF 1:e511ea8d39d5 90 else
JMF 1:e511ea8d39d5 91 done = cnt | (t.read_ms() > _btimeout);
JMF 1:e511ea8d39d5 92 }
JMF 1:e511ea8d39d5 93 while( !done );
JMF 1:e511ea8d39d5 94 t.stop();
JMF 1:e511ea8d39d5 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 1:e511ea8d39d5 116 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 1:e511ea8d39d5 117 }
JMF 1:e511ea8d39d5 118