AWS IoT demonstration using the Avnet Shield (AT&T LTE) and the FRDM-K64F target board.

Dependencies:   K64F_FATFileSystem

Fork of mbed-os-example-tls-tls-client by mbed-os-examples

Committer:
ampembeng
Date:
Thu Dec 01 18:05:38 2016 +0000
Revision:
15:6f2798e45099
Initial commit.  Demo works with both the FRDM wired Ethernet and the Avnet Shield wireless modem.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ampembeng 15:6f2798e45099 1 /* =====================================================================
ampembeng 15:6f2798e45099 2 Copyright © 2016, Avnet (R)
ampembeng 15:6f2798e45099 3
ampembeng 15:6f2798e45099 4 Contributors:
ampembeng 15:6f2798e45099 5 * James M Flynn, www.em.avnet.com
ampembeng 15:6f2798e45099 6
ampembeng 15:6f2798e45099 7 Licensed under the Apache License, Version 2.0 (the "License");
ampembeng 15:6f2798e45099 8 you may not use this file except in compliance with the License.
ampembeng 15:6f2798e45099 9 You may obtain a copy of the License at
ampembeng 15:6f2798e45099 10
ampembeng 15:6f2798e45099 11 http://www.apache.org/licenses/LICENSE-2.0
ampembeng 15:6f2798e45099 12
ampembeng 15:6f2798e45099 13 Unless required by applicable law or agreed to in writing,
ampembeng 15:6f2798e45099 14 software distributed under the License is distributed on an
ampembeng 15:6f2798e45099 15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
ampembeng 15:6f2798e45099 16 either express or implied. See the License for the specific
ampembeng 15:6f2798e45099 17 language governing permissions and limitations under the License.
ampembeng 15:6f2798e45099 18
ampembeng 15:6f2798e45099 19 @file WNCInterface.cpp
ampembeng 15:6f2798e45099 20 @version 1.0
ampembeng 15:6f2798e45099 21 @date Sept 2016
ampembeng 15:6f2798e45099 22
ampembeng 15:6f2798e45099 23 ======================================================================== */
ampembeng 15:6f2798e45099 24 #include "../WNCInterface.h"
ampembeng 15:6f2798e45099 25 #include "WNCSocket.h"
ampembeng 15:6f2798e45099 26 #include <cstring>
ampembeng 15:6f2798e45099 27
ampembeng 15:6f2798e45099 28 class WNCInterface;
ampembeng 15:6f2798e45099 29
ampembeng 15:6f2798e45099 30 //
ampembeng 15:6f2798e45099 31 // Set up the defaults in the constructor. If the caller doesn't change anything
ampembeng 15:6f2798e45099 32 // the APN will be set for AT&T, port #40 and timeout 1.5 seconds
ampembeng 15:6f2798e45099 33 //
ampembeng 15:6f2798e45099 34 WNCSocket::WNCSocket() :
ampembeng 15:6f2798e45099 35 _sock_type(-1),
ampembeng 15:6f2798e45099 36 _timeout(1500) {
ampembeng 15:6f2798e45099 37 }
ampembeng 15:6f2798e45099 38
ampembeng 15:6f2798e45099 39 WNCSocket::~WNCSocket() {
ampembeng 15:6f2798e45099 40 }
ampembeng 15:6f2798e45099 41
ampembeng 15:6f2798e45099 42
ampembeng 15:6f2798e45099 43 //
ampembeng 15:6f2798e45099 44 // ensure we have a WNC Controller attached and initialized by calling to get the
ampembeng 15:6f2798e45099 45 // network status, This will provide us with all the network information. if we
ampembeng 15:6f2798e45099 46 // are not connected, will return -1.
ampembeng 15:6f2798e45099 47 //
ampembeng 15:6f2798e45099 48 int WNCSocket::init(int timeout) {
ampembeng 15:6f2798e45099 49
ampembeng 15:6f2798e45099 50 _timeout = timeout;
ampembeng 15:6f2798e45099 51 M_LOCK;
ampembeng 15:6f2798e45099 52 int ret = WNCInterface::_pwnc->getWncNetworkingStats(&WNCInterface::myNetStats)? 0:-1;
ampembeng 15:6f2798e45099 53 M_ULOCK;
ampembeng 15:6f2798e45099 54 return ret;
ampembeng 15:6f2798e45099 55 }
ampembeng 15:6f2798e45099 56
ampembeng 15:6f2798e45099 57 //
ampembeng 15:6f2798e45099 58 // Connect this socket to a user specified URL or IP address. It could be
ampembeng 15:6f2798e45099 59 // either a TCP or UDP socket. The user is also expected to provide a port #.
ampembeng 15:6f2798e45099 60 // If the connection failed for any reason return 0, otherwise, return 1;
ampembeng 15:6f2798e45099 61 //
ampembeng 15:6f2798e45099 62 int WNCSocket::connect(char *url, int type, int port) {
ampembeng 15:6f2798e45099 63 int rslt;
ampembeng 15:6f2798e45099 64 char address[5];
ampembeng 15:6f2798e45099 65
ampembeng 15:6f2798e45099 66 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
ampembeng 15:6f2798e45099 67
ampembeng 15:6f2798e45099 68 // lets determine if they passed in an IP or a URL
ampembeng 15:6f2798e45099 69 rslt = std::sscanf(url, "%3u.%3u.%3u.%3u",
ampembeng 15:6f2798e45099 70 (unsigned int*)&address[0], (unsigned int*)&address[1],
ampembeng 15:6f2798e45099 71 (unsigned int*)&address[2], (unsigned int*)&address[3]);
ampembeng 15:6f2798e45099 72 M_LOCK;
ampembeng 15:6f2798e45099 73 if (rslt == 4)
ampembeng 15:6f2798e45099 74 rslt = WNCInterface::_pwnc->setIpAddr(0,url);
ampembeng 15:6f2798e45099 75 else
ampembeng 15:6f2798e45099 76 rslt = WNCInterface::_pwnc->resolveUrl(0,url);
ampembeng 15:6f2798e45099 77
ampembeng 15:6f2798e45099 78 if( rslt ) {
ampembeng 15:6f2798e45099 79 _sock_type = type; //resolved the URL indicate socket 0 is open
ampembeng 15:6f2798e45099 80 rslt = WNCInterface::_pwnc->openSocket(0, port, (_sock_type==SOCK_STREAM)? 1:0, _timeout);
ampembeng 15:6f2798e45099 81 }
ampembeng 15:6f2798e45099 82 M_ULOCK;
ampembeng 15:6f2798e45099 83 return rslt;
ampembeng 15:6f2798e45099 84 }
ampembeng 15:6f2798e45099 85
ampembeng 15:6f2798e45099 86
ampembeng 15:6f2798e45099 87 //
ampembeng 15:6f2798e45099 88 // disconnect the currently open socket.
ampembeng 15:6f2798e45099 89 // -1 if there was an error
ampembeng 15:6f2798e45099 90 // 0 if we disconnected
ampembeng 15:6f2798e45099 91 //
ampembeng 15:6f2798e45099 92 int WNCSocket::disconnect() {
ampembeng 15:6f2798e45099 93 if( _sock_type<0 )
ampembeng 15:6f2798e45099 94 return 0; //nothing is connected currently
ampembeng 15:6f2798e45099 95
ampembeng 15:6f2798e45099 96 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
ampembeng 15:6f2798e45099 97 M_LOCK;
ampembeng 15:6f2798e45099 98 int ret = !WNCInterface::_pwnc->closeSocket(0);
ampembeng 15:6f2798e45099 99 M_ULOCK;
ampembeng 15:6f2798e45099 100 return ret;
ampembeng 15:6f2798e45099 101 }
ampembeng 15:6f2798e45099 102
ampembeng 15:6f2798e45099 103 void WNCSocket::set_blocking(bool blocking, unsigned int timeout) {
ampembeng 15:6f2798e45099 104 blocking = blocking;
ampembeng 15:6f2798e45099 105 timeout= timeout;
ampembeng 15:6f2798e45099 106
ampembeng 15:6f2798e45099 107 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
ampembeng 15:6f2798e45099 108 M_LOCK;
ampembeng 15:6f2798e45099 109 WNCInterface::_pwnc->setReadRetryWait(0, 0);
ampembeng 15:6f2798e45099 110 WNCInterface::_pwnc->setReadRetries(0, 0);
ampembeng 15:6f2798e45099 111 M_ULOCK;
ampembeng 15:6f2798e45099 112 }
ampembeng 15:6f2798e45099 113
ampembeng 15:6f2798e45099 114
ampembeng 15:6f2798e45099 115