My fork

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

Committer:
JMF
Date:
Fri Mar 24 21:43:57 2017 +0000
Revision:
26:81e520908460
Parent:
Socket/Socket.cpp@9:9f0578ff157a
Child:
28:dceb8da78e6d
Changing WNC Networking Class names to be prefixed with "Wnc" so they don't collide with the standard networking classes.

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 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 9:9f0578ff157a 66 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
JMF 1:e511ea8d39d5 67
JMF 1:e511ea8d39d5 68 // lets determine if they passed in an IP or a URL
JMF 1:e511ea8d39d5 69 rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
JMF 1:e511ea8d39d5 70 (unsigned int*)&address[0], (unsigned int*)&address[1],
JMF 1:e511ea8d39d5 71 (unsigned int*)&address[2], (unsigned int*)&address[3]);
JMF 7:fded23f50479 72 M_LOCK;
JMF 1:e511ea8d39d5 73 if (rslt == 4)
JMF 1:e511ea8d39d5 74 rslt = WNCInterface::_pwnc->setIpAddr(0,url);
JMF 1:e511ea8d39d5 75 else
JMF 1:e511ea8d39d5 76 rslt = WNCInterface::_pwnc->resolveUrl(0,url);
JMF 1:e511ea8d39d5 77
JMF 1:e511ea8d39d5 78 if( rslt ) {
JMF 1:e511ea8d39d5 79 _sock_type = type; //resolved the URL indicate socket 0 is open
JMF 1:e511ea8d39d5 80 rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
JMF 1:e511ea8d39d5 81 }
JMF 7:fded23f50479 82 M_ULOCK;
JMF 1:e511ea8d39d5 83 return rslt;
JMF 1:e511ea8d39d5 84 }
JMF 1:e511ea8d39d5 85
JMF 1:e511ea8d39d5 86
JMF 1:e511ea8d39d5 87 //
JMF 1:e511ea8d39d5 88 // disconnect the currently open socket.
JMF 1:e511ea8d39d5 89 // -1 if there was an error
JMF 1:e511ea8d39d5 90 // 0 if we disconnected
JMF 1:e511ea8d39d5 91 //
JMF 1:e511ea8d39d5 92 int Socket::disconnect() {
JMF 1:e511ea8d39d5 93 if( _sock_type<0 )
JMF 1:e511ea8d39d5 94 return 0; //nothing is connected currently
JMF 1:e511ea8d39d5 95
JMF 9:9f0578ff157a 96 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
JMF 7:fded23f50479 97 M_LOCK;
JMF 7:fded23f50479 98 int ret = !WNCInterface::_pwnc->closeSocket(0);
JMF 7:fded23f50479 99 M_ULOCK;
JMF 7:fded23f50479 100 return ret;
JMF 1:e511ea8d39d5 101 }
JMF 1:e511ea8d39d5 102
JMF 1:e511ea8d39d5 103 void Socket::set_blocking(bool blocking, unsigned int timeout) {
JMF 1:e511ea8d39d5 104 blocking = blocking;
JMF 1:e511ea8d39d5 105 timeout= timeout;
JMF 1:e511ea8d39d5 106
JMF 9:9f0578ff157a 107 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
JMF 7:fded23f50479 108 M_LOCK;
JMF 1:e511ea8d39d5 109 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 110 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 7:fded23f50479 111 M_ULOCK;
JMF 1:e511ea8d39d5 112 }
JMF 1:e511ea8d39d5 113
JMF 1:e511ea8d39d5 114