Avnet / WNCInterface

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WncSocket.cpp Source File

WncSocket.cpp

00001 /* =====================================================================
00002    Copyright © 2016, Avnet (R)
00003 
00004    Contributors:
00005      * James M Flynn, www.em.avnet.com 
00006  
00007    Licensed under the Apache License, Version 2.0 (the "License"); 
00008    you may not use this file except in compliance with the License.
00009    You may obtain a copy of the License at
00010 
00011     http://www.apache.org/licenses/LICENSE-2.0
00012 
00013    Unless required by applicable law or agreed to in writing, 
00014    software distributed under the License is distributed on an 
00015    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
00016    either express or implied. See the License for the specific 
00017    language governing permissions and limitations under the License.
00018 
00019     @file          WNCInterface.cpp
00020     @version       1.0
00021     @date          Sept 2016
00022 
00023 ======================================================================== */
00024 #include "../WNCInterface.h"
00025 #include "WncSocket.h"
00026 #include <cstring>
00027 
00028 class WNCInterface;
00029 
00030 //
00031 // Set up the defaults in the constructor.  If the caller doesn't change anything
00032 // the APN will be set for AT&T, port #40 and timeout 1.5 seconds
00033 //
00034 WncSocket::WncSocket() : 
00035   _sock_type(-1), 
00036   _timeout(1500) {}
00037 
00038 WncSocket::~WncSocket() {}
00039 
00040 //
00041 // ensure we have a WNC Controller attached and initialized by calling to get the 
00042 // network status,  This will provide us with all the network information.  if we
00043 // are not connected, will return -1.
00044 // 
00045 int WncSocket::init(int timeout) {
00046 
00047     _timeout = timeout;
00048     M_LOCK;
00049     int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
00050     M_ULOCK;
00051     return ret;
00052 }
00053 
00054 //
00055 // Connect this socket to a user specified URL or IP address.  It could be 
00056 // either a TCP or UDP socket. The user is also expected to provide a port #.  
00057 // If the connection failed for any reason return 0, otherwise, return 1;
00058 //
00059 int WncSocket::connect(char *url, int type, int port) {
00060   int rslt;
00061   char address[5];
00062   
00063   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
00064  
00065   // lets determine if they passed in an IP or a URL
00066   rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
00067             (unsigned int*)&address[0], (unsigned int*)&address[1],
00068             (unsigned int*)&address[2], (unsigned int*)&address[3]);
00069   M_LOCK;  
00070   if (rslt == 4) 
00071     rslt = WNCInterface::_pwnc->setIpAddr(0,url);
00072   else 
00073     rslt = WNCInterface::_pwnc->resolveUrl(0,url);
00074 
00075   if( rslt ) {
00076     _sock_type = type;             //resolved the URL indicate socket 0 is open
00077     rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
00078     } 
00079   M_ULOCK;
00080   return rslt;
00081 } 
00082 
00083 
00084 //
00085 // disconnect the currently open socket.
00086 // -1 if there was an error
00087 //  0 if we disconnected
00088 //
00089 int WncSocket::disconnect() {
00090   if( _sock_type<0 )
00091     return 0;  //nothing is connected currently
00092 
00093   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
00094   M_LOCK;
00095   int ret = !WNCInterface::_pwnc->closeSocket(0);
00096   M_ULOCK;
00097   return ret;
00098 }
00099 
00100 void WncSocket::set_blocking(bool blocking, unsigned int timeout) {
00101     blocking = blocking;
00102     timeout= timeout;
00103 
00104     CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
00105     M_LOCK;
00106     WNCInterface::_pwnc->setReadRetryWait(0, 0);
00107     WNCInterface::_pwnc->setReadRetries(0, 0);
00108     M_ULOCK;
00109 }
00110 
00111