V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

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 
00039 WNCSocket::~WNCSocket() {
00040 }
00041 
00042 
00043 //
00044 // ensure we have a WNC Controller attached and initialized by calling to get the 
00045 // network status,  This will provide us with all the network information.  if we
00046 // are not connected, will return -1.
00047 // 
00048 int WNCSocket::init(int timeout) {
00049 
00050     _timeout = timeout;
00051     M_LOCK;
00052     int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
00053     M_ULOCK;
00054     return ret;
00055 }
00056 
00057 //
00058 // Connect this socket to a user specified URL or IP address.  It could be 
00059 // either a TCP or UDP socket. The user is also expected to provide a port #.  
00060 // If the connection failed for any reason return 0, otherwise, return 1;
00061 //
00062 int WNCSocket::connect(char *url, int type, int port) {
00063   int rslt;
00064   char address[5];
00065   
00066   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
00067  
00068   // lets determine if they passed in an IP or a URL
00069   rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
00070             (unsigned int*)&address[0], (unsigned int*)&address[1],
00071             (unsigned int*)&address[2], (unsigned int*)&address[3]);
00072   M_LOCK;  
00073   if (rslt == 4) 
00074     rslt = WNCInterface::_pwnc->setIpAddr(0,url);
00075   else 
00076     rslt = WNCInterface::_pwnc->resolveUrl(0,url);
00077 
00078   if( rslt ) {
00079     _sock_type = type;             //resolved the URL indicate socket 0 is open
00080     rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
00081     } 
00082   M_ULOCK;
00083   return rslt;
00084 } 
00085 
00086 
00087 //
00088 // disconnect the currently open socket.
00089 // -1 if there was an error
00090 //  0 if we disconnected
00091 //
00092 int WNCSocket::disconnect() {
00093   if( _sock_type<0 )
00094     return 0;  //nothing is connected currently
00095 
00096   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
00097   M_LOCK;
00098   int ret = !WNCInterface::_pwnc->closeSocket(0);
00099   M_ULOCK;
00100   return ret;
00101 }
00102 
00103 void WNCSocket::set_blocking(bool blocking, unsigned int timeout) {
00104     blocking = blocking;
00105     timeout= timeout;
00106 
00107     CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
00108     M_LOCK;
00109     WNCInterface::_pwnc->setReadRetryWait(0, 0);
00110     WNCInterface::_pwnc->setReadRetries(0, 0);
00111     M_ULOCK;
00112 }
00113 
00114 
00115