My fork
Dependencies: WncControllerK64F
Fork of WNCInterface by
Socket/WncTCPSocketConnection.cpp@29:b278b745fb4f, 2017-03-24 (annotated)
- Committer:
- JMF
- Date:
- Fri Mar 24 22:26:23 2017 +0000
- Revision:
- 29:b278b745fb4f
- Parent:
- 28:dceb8da78e6d
updated Class name of TCPSocketConnection to WncTCPSocketConnection;
Who changed what in which revision?
User | Revision | Line number | New 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 | 26:81e520908460 | 22 | #include "WncSocket.h" |
JMF | 26:81e520908460 | 23 | #include "WncTCPSocketConnection.h" |
JMF | 1:e511ea8d39d5 | 24 | #include <cstring> |
JMF | 1:e511ea8d39d5 | 25 | |
JMF | 11:75cf1e1c921c | 26 | #define READ_EVERYMS 500 //number of milliseconds between WNC socket reads |
JMF | 11:75cf1e1c921c | 27 | |
JMF | 1:e511ea8d39d5 | 28 | |
JMF | 1:e511ea8d39d5 | 29 | // |
JMF | 1:e511ea8d39d5 | 30 | // blocking is used to make the WNC keep checking for incoming data for a |
JMF | 1:e511ea8d39d5 | 31 | // period of time. |
JMF | 1:e511ea8d39d5 | 32 | // |
JMF | 29:b278b745fb4f | 33 | void WncTCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) { |
JMF | 1:e511ea8d39d5 | 34 | _is_blocking = blocking; // true if we want to wait for request |
JMF | 1:e511ea8d39d5 | 35 | _btimeout = timeout; // user specs msec |
JMF | 1:e511ea8d39d5 | 36 | |
JMF | 9:9f0578ff157a | 37 | CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void); |
JMF | 7:fded23f50479 | 38 | M_LOCK; |
JMF | 1:e511ea8d39d5 | 39 | WNCInterface::_pwnc->setReadRetryWait(0, 0); |
JMF | 1:e511ea8d39d5 | 40 | WNCInterface::_pwnc->setReadRetries(0, 0); |
JMF | 7:fded23f50479 | 41 | M_ULOCK; |
JMF | 1:e511ea8d39d5 | 42 | } |
JMF | 1:e511ea8d39d5 | 43 | |
JMF | 1:e511ea8d39d5 | 44 | |
JMF | 29:b278b745fb4f | 45 | int WncTCPSocketConnection::connect(const char* host, const int port) { |
JMF | 28:dceb8da78e6d | 46 | WncSocket::connect((char*)host, SOCK_STREAM, port); |
JMF | 1:e511ea8d39d5 | 47 | _is_blocking = false; // start out not blocking, user will set it if desired |
JMF | 1:e511ea8d39d5 | 48 | return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1; |
JMF | 1:e511ea8d39d5 | 49 | } |
JMF | 1:e511ea8d39d5 | 50 | |
JMF | 29:b278b745fb4f | 51 | bool WncTCPSocketConnection::is_connected(void) { |
JMF | 1:e511ea8d39d5 | 52 | return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0; |
JMF | 1:e511ea8d39d5 | 53 | } |
JMF | 1:e511ea8d39d5 | 54 | |
JMF | 29:b278b745fb4f | 55 | int WncTCPSocketConnection::send(char* data, int length) { |
JMF | 7:fded23f50479 | 56 | int ret = -1; |
JMF | 7:fded23f50479 | 57 | |
JMF | 1:e511ea8d39d5 | 58 | WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus(); |
JMF | 1:e511ea8d39d5 | 59 | |
JMF | 9:9f0578ff157a | 60 | CHK_WNCFE(( s == FATAL_FLAG ), fail); |
JMF | 1:e511ea8d39d5 | 61 | |
JMF | 7:fded23f50479 | 62 | if( s == WncController_fk::WncController::WNC_ON ) { |
JMF | 7:fded23f50479 | 63 | M_LOCK; |
JMF | 25:52bad4105cac | 64 | if( WNCInterface::_pwnc->write(0, (const uint8_t*)data, length) ) |
JMF | 7:fded23f50479 | 65 | ret = length; |
JMF | 7:fded23f50479 | 66 | M_ULOCK; |
JMF | 7:fded23f50479 | 67 | } |
JMF | 7:fded23f50479 | 68 | return ret; |
JMF | 1:e511ea8d39d5 | 69 | } |
JMF | 1:e511ea8d39d5 | 70 | |
JMF | 29:b278b745fb4f | 71 | int WncTCPSocketConnection::receive(char *readBuf, int length) { |
JMF | 1:e511ea8d39d5 | 72 | Timer t; |
JMF | 1:e511ea8d39d5 | 73 | size_t done, cnt; |
JMF | 1:e511ea8d39d5 | 74 | int ret=-1; |
JMF | 1:e511ea8d39d5 | 75 | WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus(); |
JMF | 1:e511ea8d39d5 | 76 | |
JMF | 9:9f0578ff157a | 77 | CHK_WNCFE(( s == FATAL_FLAG ), fail); |
JMF | 1:e511ea8d39d5 | 78 | if( s != WncController_fk::WncController::WNC_ON ) |
JMF | 1:e511ea8d39d5 | 79 | return ret; |
JMF | 1:e511ea8d39d5 | 80 | |
JMF | 11:75cf1e1c921c | 81 | M_LOCK; |
JMF | 1:e511ea8d39d5 | 82 | t.start(); |
JMF | 1:e511ea8d39d5 | 83 | do { |
JMF | 11:75cf1e1c921c | 84 | if( !(t.read_ms() % READ_EVERYMS) ) |
JMF | 11:75cf1e1c921c | 85 | cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length); |
JMF | 1:e511ea8d39d5 | 86 | if( _is_blocking ) |
JMF | 1:e511ea8d39d5 | 87 | done = cnt; |
JMF | 1:e511ea8d39d5 | 88 | else |
JMF | 1:e511ea8d39d5 | 89 | done = cnt | (t.read_ms() > _btimeout); |
JMF | 1:e511ea8d39d5 | 90 | } |
JMF | 1:e511ea8d39d5 | 91 | while( !done ); |
JMF | 1:e511ea8d39d5 | 92 | t.stop(); |
JMF | 7:fded23f50479 | 93 | M_ULOCK; |
JMF | 7:fded23f50479 | 94 | |
JMF | 1:e511ea8d39d5 | 95 | if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) { |
JMF | 11:75cf1e1c921c | 96 | //readBuf[cnt] = '\0'; |
JMF | 1:e511ea8d39d5 | 97 | ret = (int)cnt; |
JMF | 1:e511ea8d39d5 | 98 | } |
JMF | 1:e511ea8d39d5 | 99 | else |
JMF | 1:e511ea8d39d5 | 100 | ret = -1; |
JMF | 1:e511ea8d39d5 | 101 | |
JMF | 1:e511ea8d39d5 | 102 | return ret; |
JMF | 1:e511ea8d39d5 | 103 | } |
JMF | 1:e511ea8d39d5 | 104 | |
JMF | 29:b278b745fb4f | 105 | int WncTCPSocketConnection::send_all(char* data, int length) { |
JMF | 1:e511ea8d39d5 | 106 | return send(data,length); |
JMF | 1:e511ea8d39d5 | 107 | } |
JMF | 1:e511ea8d39d5 | 108 | |
JMF | 29:b278b745fb4f | 109 | int WncTCPSocketConnection::receive_all(char* data, int length) { |
JMF | 1:e511ea8d39d5 | 110 | return receive(data,length); |
JMF | 1:e511ea8d39d5 | 111 | } |
JMF | 1:e511ea8d39d5 | 112 | |
JMF | 29:b278b745fb4f | 113 | int WncTCPSocketConnection::close(void) { |
JMF | 28:dceb8da78e6d | 114 | WncSocket::disconnect(); |
JMF | 7:fded23f50479 | 115 | M_LOCK; |
JMF | 7:fded23f50479 | 116 | int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1; |
JMF | 7:fded23f50479 | 117 | M_ULOCK; |
JMF | 7:fded23f50479 | 118 | return ret; |
JMF | 1:e511ea8d39d5 | 119 | } |
JMF | 1:e511ea8d39d5 | 120 |