My fork

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

Committer:
JMF
Date:
Wed Sep 21 15:20:12 2016 +0000
Revision:
1:e511ea8d39d5
Child:
7:fded23f50479
Adding in all the Sockets, missed them on the initial commit.

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 1:e511ea8d39d5 51 return (WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats))? 0:-1;
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 1:e511ea8d39d5 59 int Socket::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 1:e511ea8d39d5 63 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 64 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 65
JMF 1:e511ea8d39d5 66 // lets determine if they passed in an IP or a URL
JMF 1:e511ea8d39d5 67 rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
JMF 1:e511ea8d39d5 68 (unsigned int*)&address[0], (unsigned int*)&address[1],
JMF 1:e511ea8d39d5 69 (unsigned int*)&address[2], (unsigned int*)&address[3]);
JMF 1:e511ea8d39d5 70
JMF 1:e511ea8d39d5 71 if (rslt == 4)
JMF 1:e511ea8d39d5 72 rslt = WNCInterface::_pwnc->setIpAddr(0,url);
JMF 1:e511ea8d39d5 73 else
JMF 1:e511ea8d39d5 74 rslt = WNCInterface::_pwnc->resolveUrl(0,url);
JMF 1:e511ea8d39d5 75
JMF 1:e511ea8d39d5 76 if( rslt ) {
JMF 1:e511ea8d39d5 77 _sock_type = type; //resolved the URL indicate socket 0 is open
JMF 1:e511ea8d39d5 78 rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
JMF 1:e511ea8d39d5 79 }
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 1:e511ea8d39d5 89 int Socket::disconnect() {
JMF 1:e511ea8d39d5 90 if( _sock_type<0 )
JMF 1:e511ea8d39d5 91 return 0; //nothing is connected currently
JMF 1:e511ea8d39d5 92
JMF 1:e511ea8d39d5 93 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 94 FATAL_WNC_ERROR(fail);
JMF 1:e511ea8d39d5 95
JMF 1:e511ea8d39d5 96 return !WNCInterface::_pwnc->closeSocket(0);
JMF 1:e511ea8d39d5 97 }
JMF 1:e511ea8d39d5 98
JMF 1:e511ea8d39d5 99 void Socket::set_blocking(bool blocking, unsigned int timeout) {
JMF 1:e511ea8d39d5 100 blocking = blocking;
JMF 1:e511ea8d39d5 101 timeout= timeout;
JMF 1:e511ea8d39d5 102
JMF 1:e511ea8d39d5 103 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 1:e511ea8d39d5 104 FATAL_WNC_ERROR(void);
JMF 1:e511ea8d39d5 105
JMF 1:e511ea8d39d5 106 WNCInterface::_pwnc->setReadRetryWait(0, 0);
JMF 1:e511ea8d39d5 107 WNCInterface::_pwnc->setReadRetries(0, 0);
JMF 1:e511ea8d39d5 108 }
JMF 1:e511ea8d39d5 109
JMF 1:e511ea8d39d5 110