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:
Mon Dec 19 20:52:28 2016 +0000
Revision:
25:91d771247ac8
Parent:
15:6f2798e45099
Updated the look/feel of the Python GUI to match the AT&T locker demo S3 page.  Added "assets" folder to support this.  Updated the README.

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
ampembeng 15:6f2798e45099 25 #include "../WNCInterface.h"
ampembeng 15:6f2798e45099 26
ampembeng 15:6f2798e45099 27 #include "WNCUDPSocket.h"
ampembeng 15:6f2798e45099 28 #include <cstring>
ampembeng 15:6f2798e45099 29
ampembeng 15:6f2798e45099 30 WNCUDPSocket::WNCUDPSocket() :
ampembeng 15:6f2798e45099 31 _is_blocking(0),
ampembeng 15:6f2798e45099 32 _btimeout(0){
ampembeng 15:6f2798e45099 33 }
ampembeng 15:6f2798e45099 34
ampembeng 15:6f2798e45099 35 WNCUDPSocket::~WNCUDPSocket() {
ampembeng 15:6f2798e45099 36 }
ampembeng 15:6f2798e45099 37
ampembeng 15:6f2798e45099 38 int WNCUDPSocket::init(void) {
ampembeng 15:6f2798e45099 39 _is_blocking = false; // start out not blocking, user will set it if desired
ampembeng 15:6f2798e45099 40 return ( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD )? 0:-1;
ampembeng 15:6f2798e45099 41 }
ampembeng 15:6f2798e45099 42
ampembeng 15:6f2798e45099 43
ampembeng 15:6f2798e45099 44 int WNCUDPSocket::close(void) {
ampembeng 15:6f2798e45099 45 WNCSocket::disconnect();
ampembeng 15:6f2798e45099 46 return ( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD )? 0:-1;
ampembeng 15:6f2798e45099 47 }
ampembeng 15:6f2798e45099 48
ampembeng 15:6f2798e45099 49 // -1 if unsuccessful, else number of bytes written
ampembeng 15:6f2798e45099 50 int WNCUDPSocket::sendTo(WNCEndpoint &remote, char *packet, int length) {
ampembeng 15:6f2798e45099 51 int ret = -1;
ampembeng 15:6f2798e45099 52
ampembeng 15:6f2798e45099 53 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
ampembeng 15:6f2798e45099 54 if( remote._epAddr.port ) { //make sure the WNCEndpoint has an port assoicated with it
ampembeng 15:6f2798e45099 55 if( WNCSocket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port) ) {
ampembeng 15:6f2798e45099 56 if( WNCInterface::_pwnc->write(0,packet,length) )
ampembeng 15:6f2798e45099 57 ret = length;
ampembeng 15:6f2798e45099 58 close();
ampembeng 15:6f2798e45099 59 }
ampembeng 15:6f2798e45099 60 }
ampembeng 15:6f2798e45099 61 return ret;
ampembeng 15:6f2798e45099 62 }
ampembeng 15:6f2798e45099 63
ampembeng 15:6f2798e45099 64 //
ampembeng 15:6f2798e45099 65 // blocking is used to make the WNC keep checking for incoming data for a
ampembeng 15:6f2798e45099 66 // period of time.
ampembeng 15:6f2798e45099 67
ampembeng 15:6f2798e45099 68 void WNCUDPSocket::set_blocking (bool blocking, unsigned int timeout) {
ampembeng 15:6f2798e45099 69 _is_blocking = blocking; // true or false
ampembeng 15:6f2798e45099 70 _btimeout = timeout; // user specifies in msec
ampembeng 15:6f2798e45099 71
ampembeng 15:6f2798e45099 72 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
ampembeng 15:6f2798e45099 73 WNCInterface::_pwnc->setReadRetryWait(0, 0);
ampembeng 15:6f2798e45099 74 WNCInterface::_pwnc->setReadRetries(0, 0);
ampembeng 15:6f2798e45099 75 }
ampembeng 15:6f2798e45099 76
ampembeng 15:6f2798e45099 77 // -1 if unsuccessful, else number of bytes received
ampembeng 15:6f2798e45099 78 int WNCUDPSocket::receiveFrom(WNCEndpoint &remote, char *buffer, int length) {
ampembeng 15:6f2798e45099 79 const uint8_t *ptr;
ampembeng 15:6f2798e45099 80 Timer t;
ampembeng 15:6f2798e45099 81 int done, ret = -1;
ampembeng 15:6f2798e45099 82
ampembeng 15:6f2798e45099 83 //make sure the WNCEndpoint has an port assoicated with it
ampembeng 15:6f2798e45099 84 if( !remote._epAddr.port )
ampembeng 15:6f2798e45099 85 return -1;
ampembeng 15:6f2798e45099 86
ampembeng 15:6f2798e45099 87 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
ampembeng 15:6f2798e45099 88 ret = WNCSocket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port);
ampembeng 15:6f2798e45099 89
ampembeng 15:6f2798e45099 90 t.start();
ampembeng 15:6f2798e45099 91 do {
ampembeng 15:6f2798e45099 92 ret = WNCInterface::_pwnc->read(0, &ptr);
ampembeng 15:6f2798e45099 93 done = ret | (t.read_ms() > _btimeout);
ampembeng 15:6f2798e45099 94 }
ampembeng 15:6f2798e45099 95 while( _is_blocking && !done );
ampembeng 15:6f2798e45099 96 t.stop();
ampembeng 15:6f2798e45099 97
ampembeng 15:6f2798e45099 98 if( ret > length )
ampembeng 15:6f2798e45099 99 ret = length;
ampembeng 15:6f2798e45099 100 memcpy( buffer, ptr, ret );
ampembeng 15:6f2798e45099 101
ampembeng 15:6f2798e45099 102 return ret;
ampembeng 15:6f2798e45099 103 }
ampembeng 15:6f2798e45099 104
ampembeng 15:6f2798e45099 105
ampembeng 15:6f2798e45099 106