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
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
JMF 1:e511ea8d39d5 4 Contributors:
JMF 1:e511ea8d39d5 5 * James M Flynn, www.em.avnet.com
JMF 1:e511ea8d39d5 6
JMF 1:e511ea8d39d5 7 Licensed under the Apache License, Version 2.0 (the "License");
JMF 1:e511ea8d39d5 8 you may not use this file except in compliance with the License.
JMF 1:e511ea8d39d5 9 You may obtain a copy of the License at
JMF 1:e511ea8d39d5 10
JMF 1:e511ea8d39d5 11 http://www.apache.org/licenses/LICENSE-2.0
JMF 1:e511ea8d39d5 12
JMF 1:e511ea8d39d5 13 Unless required by applicable law or agreed to in writing,
JMF 1:e511ea8d39d5 14 software distributed under the License is distributed on an
JMF 1:e511ea8d39d5 15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
JMF 1:e511ea8d39d5 16 either express or implied. See the License for the specific
JMF 1:e511ea8d39d5 17 language governing permissions and limitations under the License.
JMF 1:e511ea8d39d5 18
JMF 1:e511ea8d39d5 19 @file WNCInterface.cpp
JMF 1:e511ea8d39d5 20 @version 1.0
JMF 1:e511ea8d39d5 21 @date Sept 2016
JMF 1:e511ea8d39d5 22
JMF 1:e511ea8d39d5 23 ======================================================================== */
JMF 1:e511ea8d39d5 24 #include "../WNCInterface.h"
JMF 26:81e520908460 25 #include "WncSocket.h"
JMF 1:e511ea8d39d5 26 #include <cstring>
JMF 1:e511ea8d39d5 27
JMF 1:e511ea8d39d5 28 class WNCInterface;
JMF 1:e511ea8d39d5 29
JMF 1:e511ea8d39d5 30 //
JMF 1:e511ea8d39d5 31 // Set up the defaults in the constructor. If the caller doesn't change anything
JMF 1:e511ea8d39d5 32 // the APN will be set for AT&T, port #40 and timeout 1.5 seconds
JMF 1:e511ea8d39d5 33 //
JMF 28:dceb8da78e6d 34 WncSocket::WncSocket() :
JMF 1:e511ea8d39d5 35 _sock_type(-1),
JMF 28:dceb8da78e6d 36 _timeout(1500) {}
JMF 1:e511ea8d39d5 37
JMF 28:dceb8da78e6d 38 WncSocket::~WncSocket() {}
JMF 1:e511ea8d39d5 39
JMF 1:e511ea8d39d5 40 //
JMF 1:e511ea8d39d5 41 // ensure we have a WNC Controller attached and initialized by calling to get the
JMF 1:e511ea8d39d5 42 // network status, This will provide us with all the network information. if we
JMF 1:e511ea8d39d5 43 // are not connected, will return -1.
JMF 1:e511ea8d39d5 44 //
JMF 28:dceb8da78e6d 45 int WncSocket::init(int timeout) {
JMF 1:e511ea8d39d5 46
JMF 1:e511ea8d39d5 47 _timeout = timeout;
JMF 7:fded23f50479 48 M_LOCK;
JMF 7:fded23f50479 49 int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
JMF 7:fded23f50479 50 M_ULOCK;
JMF 7:fded23f50479 51 return ret;
JMF 1:e511ea8d39d5 52 }
JMF 1:e511ea8d39d5 53
JMF 1:e511ea8d39d5 54 //
JMF 1:e511ea8d39d5 55 // Connect this socket to a user specified URL or IP address. It could be
JMF 1:e511ea8d39d5 56 // either a TCP or UDP socket. The user is also expected to provide a port #.
JMF 1:e511ea8d39d5 57 // If the connection failed for any reason return 0, otherwise, return 1;
JMF 1:e511ea8d39d5 58 //
JMF 28:dceb8da78e6d 59 int WncSocket::connect(char *url, int type, int port) {
JMF 1:e511ea8d39d5 60 int rslt;
JMF 1:e511ea8d39d5 61 char address[5];
JMF 1:e511ea8d39d5 62
JMF 9:9f0578ff157a 63 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
JMF 1:e511ea8d39d5 64
JMF 1:e511ea8d39d5 65 // lets determine if they passed in an IP or a URL
JMF 1:e511ea8d39d5 66 rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
JMF 1:e511ea8d39d5 67 (unsigned int*)&address[0], (unsigned int*)&address[1],
JMF 1:e511ea8d39d5 68 (unsigned int*)&address[2], (unsigned int*)&address[3]);
JMF 7:fded23f50479 69 M_LOCK;
JMF 1:e511ea8d39d5 70 if (rslt == 4)
JMF 1:e511ea8d39d5 71 rslt = WNCInterface::_pwnc->setIpAddr(0,url);
JMF 1:e511ea8d39d5 72 else
JMF 1:e511ea8d39d5 73 rslt = WNCInterface::_pwnc->resolveUrl(0,url);
JMF 1:e511ea8d39d5 74
JMF 1:e511ea8d39d5 75 if( rslt ) {
JMF 1:e511ea8d39d5 76 _sock_type = type; //resolved the URL indicate socket 0 is open
JMF 1:e511ea8d39d5 77 rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
JMF 1:e511ea8d39d5 78 }
JMF 7:fded23f50479 79 M_ULOCK;
JMF 1:e511ea8d39d5 80 return rslt;
JMF 1:e511ea8d39d5 81 }
JMF 1:e511ea8d39d5 82
JMF 1:e511ea8d39d5 83
JMF 1:e511ea8d39d5 84 //
JMF 1:e511ea8d39d5 85 // disconnect the currently open socket.
JMF 1:e511ea8d39d5 86 // -1 if there was an error
JMF 1:e511ea8d39d5 87 // 0 if we disconnected
JMF 1:e511ea8d39d5 88 //
JMF 28:dceb8da78e6d 89 int WncSocket::disconnect() {
JMF 1:e511ea8d39d5 90 if( _sock_type<0 )
JMF 1:e511ea8d39d5 91 return 0; //nothing is connected currently
JMF 1:e511ea8d39d5 92
JMF 9:9f0578ff157a 93 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
JMF 7:fded23f50479 94 M_LOCK;
JMF 7:fded23f50479 95 int ret = !WNCInterface::_pwnc->closeSocket(0);
JMF 7:fded23f50479 96 M_ULOCK;
JMF 7:fded23f50479 97 return ret;
JMF 1:e511ea8d39d5 98 }
JMF 1:e511ea8d39d5 99
JMF 28:dceb8da78e6d 100 void WncSocket::set_blocking(bool blocking, unsigned int timeout) {
JMF 1:e511ea8d39d5 101 blocking = blocking;
JMF 1:e511ea8d39d5 102 timeout= timeout;
JMF 1:e511ea8d39d5 103
JMF 9:9f0578ff157a 104 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
JMF 7:fded23f50479 105 M_LOCK;
JMF 1:e511ea8d39d5 106 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 107 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 7:fded23f50479 108 M_ULOCK;
JMF 1:e511ea8d39d5 109 }
JMF 1:e511ea8d39d5 110
JMF 1:e511ea8d39d5 111