My fork

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

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
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 1:e511ea8d39d5 25 #include "Socket.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 1:e511ea8d39d5 34 Socket::Socket() :
JMF 1:e511ea8d39d5 35 _sock_type(-1),
JMF 1:e511ea8d39d5 36 _timeout(1500) {
JMF 1:e511ea8d39d5 37 }
JMF 1:e511ea8d39d5 38
JMF 1:e511ea8d39d5 39 Socket::~Socket() {
JMF 1:e511ea8d39d5 40 }
JMF 1:e511ea8d39d5 41
JMF 1:e511ea8d39d5 42
JMF 1:e511ea8d39d5 43 //
JMF 1:e511ea8d39d5 44 // ensure we have a WNC Controller attached and initialized by calling to get the
JMF 1:e511ea8d39d5 45 // network status, This will provide us with all the network information. if we
JMF 1:e511ea8d39d5 46 // are not connected, will return -1.
JMF 1:e511ea8d39d5 47 //
JMF 1:e511ea8d39d5 48 int Socket::init(int timeout) {
JMF 1:e511ea8d39d5 49
JMF 1:e511ea8d39d5 50 _timeout = timeout;
JMF 7:fded23f50479 51 M_LOCK;
JMF 7:fded23f50479 52 int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
JMF 7:fded23f50479 53 M_ULOCK;
JMF 7:fded23f50479 54 return ret;
JMF 1:e511ea8d39d5 55 }
JMF 1:e511ea8d39d5 56
JMF 1:e511ea8d39d5 57 //
JMF 1:e511ea8d39d5 58 // Connect this socket to a user specified URL or IP address. It could be
JMF 1:e511ea8d39d5 59 // either a TCP or UDP socket. The user is also expected to provide a port #.
JMF 1:e511ea8d39d5 60 // If the connection failed for any reason return 0, otherwise, return 1;
JMF 1:e511ea8d39d5 61 //
JMF 1:e511ea8d39d5 62 int Socket::connect(char *url, int type, int port) {
JMF 1:e511ea8d39d5 63 int rslt;
JMF 1:e511ea8d39d5 64 char address[5];
JMF 1:e511ea8d39d5 65
JMF 1:e511ea8d39d5 66 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 67 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 68
JMF 1:e511ea8d39d5 69 // lets determine if they passed in an IP or a URL
JMF 1:e511ea8d39d5 70 rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
JMF 1:e511ea8d39d5 71 (unsigned int*)&address[0], (unsigned int*)&address[1],
JMF 1:e511ea8d39d5 72 (unsigned int*)&address[2], (unsigned int*)&address[3]);
JMF 7:fded23f50479 73 M_LOCK;
JMF 1:e511ea8d39d5 74 if (rslt == 4)
JMF 1:e511ea8d39d5 75 rslt = WNCInterface::_pwnc->setIpAddr(0,url);
JMF 1:e511ea8d39d5 76 else
JMF 1:e511ea8d39d5 77 rslt = WNCInterface::_pwnc->resolveUrl(0,url);
JMF 1:e511ea8d39d5 78
JMF 1:e511ea8d39d5 79 if( rslt ) {
JMF 1:e511ea8d39d5 80 _sock_type = type; //resolved the URL indicate socket 0 is open
JMF 1:e511ea8d39d5 81 rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
JMF 1:e511ea8d39d5 82 }
JMF 7:fded23f50479 83 M_ULOCK;
JMF 1:e511ea8d39d5 84 return rslt;
JMF 1:e511ea8d39d5 85 }
JMF 1:e511ea8d39d5 86
JMF 1:e511ea8d39d5 87
JMF 1:e511ea8d39d5 88 //
JMF 1:e511ea8d39d5 89 // disconnect the currently open socket.
JMF 1:e511ea8d39d5 90 // -1 if there was an error
JMF 1:e511ea8d39d5 91 // 0 if we disconnected
JMF 1:e511ea8d39d5 92 //
JMF 1:e511ea8d39d5 93 int Socket::disconnect() {
JMF 1:e511ea8d39d5 94 if( _sock_type<0 )
JMF 1:e511ea8d39d5 95 return 0; //nothing is connected currently
JMF 1:e511ea8d39d5 96
JMF 1:e511ea8d39d5 97 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 98 FATAL_WNC_ERROR(fail);
JMF 7:fded23f50479 99 M_LOCK;
JMF 7:fded23f50479 100 int ret = !WNCInterface::_pwnc->closeSocket(0);
JMF 7:fded23f50479 101 M_ULOCK;
JMF 7:fded23f50479 102 return ret;
JMF 1:e511ea8d39d5 103 }
JMF 1:e511ea8d39d5 104
JMF 1:e511ea8d39d5 105 void Socket::set_blocking(bool blocking, unsigned int timeout) {
JMF 1:e511ea8d39d5 106 blocking = blocking;
JMF 1:e511ea8d39d5 107 timeout= timeout;
JMF 1:e511ea8d39d5 108
JMF 1:e511ea8d39d5 109 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 110 FATAL_WNC_ERROR(void);
JMF 7:fded23f50479 111 M_LOCK;
JMF 1:e511ea8d39d5 112 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 113 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 7:fded23f50479 114 M_ULOCK;
JMF 1:e511ea8d39d5 115 }
JMF 1:e511ea8d39d5 116
JMF 1:e511ea8d39d5 117