WNCInterface

Dependencies:   WncControllerK64F

Fork of WNCInterface by Avnet

Committer:
jk431j
Date:
Mon Dec 11 20:01:37 2017 +0000
Revision:
31:0191ddb31b46
Parent:
28:dceb8da78e6d
Child:
29:b278b745fb4f
Added wait for cellular link in WncController::softwareInitMdm

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 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 TCPSocketConnection::TCPSocketConnection() :
JMF 1:e511ea8d39d5 29 _is_blocking(0),
JMF 1:e511ea8d39d5 30 _btimeout(0){
JMF 1:e511ea8d39d5 31 }
JMF 1:e511ea8d39d5 32
JMF 1:e511ea8d39d5 33 //
JMF 1:e511ea8d39d5 34 // blocking is used to make the WNC keep checking for incoming data for a
JMF 1:e511ea8d39d5 35 // period of time.
JMF 1:e511ea8d39d5 36 //
JMF 1:e511ea8d39d5 37 void TCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) {
JMF 1:e511ea8d39d5 38 _is_blocking = blocking; // true if we want to wait for request
JMF 1:e511ea8d39d5 39 _btimeout = timeout; // user specs msec
JMF 1:e511ea8d39d5 40
JMF 9:9f0578ff157a 41 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
JMF 7:fded23f50479 42 M_LOCK;
JMF 1:e511ea8d39d5 43 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 44 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 7:fded23f50479 45 M_ULOCK;
JMF 1:e511ea8d39d5 46 }
JMF 1:e511ea8d39d5 47
JMF 1:e511ea8d39d5 48
JMF 1:e511ea8d39d5 49 int TCPSocketConnection::connect(const char* host, const int port) {
JMF 28:dceb8da78e6d 50 WncSocket::connect((char*)host, SOCK_STREAM, port);
JMF 1:e511ea8d39d5 51 _is_blocking = false; // start out not blocking, user will set it if desired
JMF 1:e511ea8d39d5 52 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 1:e511ea8d39d5 53 }
JMF 1:e511ea8d39d5 54
JMF 1:e511ea8d39d5 55 bool TCPSocketConnection::is_connected(void) {
JMF 1:e511ea8d39d5 56 return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0;
JMF 1:e511ea8d39d5 57 }
JMF 1:e511ea8d39d5 58
JMF 1:e511ea8d39d5 59 int TCPSocketConnection::send(char* data, int length) {
JMF 7:fded23f50479 60 int ret = -1;
JMF 7:fded23f50479 61
JMF 1:e511ea8d39d5 62 WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus();
JMF 1:e511ea8d39d5 63
JMF 9:9f0578ff157a 64 CHK_WNCFE(( s == FATAL_FLAG ), fail);
JMF 1:e511ea8d39d5 65
JMF 7:fded23f50479 66 if( s == WncController_fk::WncController::WNC_ON ) {
JMF 7:fded23f50479 67 M_LOCK;
JMF 25:52bad4105cac 68 if( WNCInterface::_pwnc->write(0, (const uint8_t*)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 9:9f0578ff157a 81 CHK_WNCFE(( s == FATAL_FLAG ), fail);
JMF 1:e511ea8d39d5 82 if( s != WncController_fk::WncController::WNC_ON )
JMF 1:e511ea8d39d5 83 return ret;
JMF 1:e511ea8d39d5 84
JMF 11:75cf1e1c921c 85 M_LOCK;
JMF 1:e511ea8d39d5 86 t.start();
JMF 1:e511ea8d39d5 87 do {
JMF 11:75cf1e1c921c 88 if( !(t.read_ms() % READ_EVERYMS) )
JMF 11:75cf1e1c921c 89 cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length);
JMF 1:e511ea8d39d5 90 if( _is_blocking )
JMF 1:e511ea8d39d5 91 done = cnt;
JMF 1:e511ea8d39d5 92 else
JMF 1:e511ea8d39d5 93 done = cnt | (t.read_ms() > _btimeout);
JMF 1:e511ea8d39d5 94 }
JMF 1:e511ea8d39d5 95 while( !done );
JMF 1:e511ea8d39d5 96 t.stop();
JMF 7:fded23f50479 97 M_ULOCK;
JMF 7:fded23f50479 98
JMF 1:e511ea8d39d5 99 if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) {
JMF 11:75cf1e1c921c 100 //readBuf[cnt] = '\0';
JMF 1:e511ea8d39d5 101 ret = (int)cnt;
JMF 1:e511ea8d39d5 102 }
JMF 1:e511ea8d39d5 103 else
JMF 1:e511ea8d39d5 104 ret = -1;
JMF 1:e511ea8d39d5 105
JMF 1:e511ea8d39d5 106 return ret;
JMF 1:e511ea8d39d5 107 }
JMF 1:e511ea8d39d5 108
JMF 1:e511ea8d39d5 109 int TCPSocketConnection::send_all(char* data, int length) {
JMF 1:e511ea8d39d5 110 return send(data,length);
JMF 1:e511ea8d39d5 111 }
JMF 1:e511ea8d39d5 112
JMF 1:e511ea8d39d5 113 int TCPSocketConnection::receive_all(char* data, int length) {
JMF 1:e511ea8d39d5 114 return receive(data,length);
JMF 1:e511ea8d39d5 115 }
JMF 1:e511ea8d39d5 116
JMF 1:e511ea8d39d5 117 int TCPSocketConnection::close(void) {
JMF 28:dceb8da78e6d 118 WncSocket::disconnect();
JMF 7:fded23f50479 119 M_LOCK;
JMF 7:fded23f50479 120 int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1;
JMF 7:fded23f50479 121 M_ULOCK;
JMF 7:fded23f50479 122 return ret;
JMF 1:e511ea8d39d5 123 }
JMF 1:e511ea8d39d5 124