The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).
Dependencies: WncControllerK64F
Dependents: WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more
See the WNCInterface README in the Wiki tab for detailed information on this library.
Socket/WncUDPSocket.cpp@26:81e520908460, 2017-03-24 (annotated)
- Committer:
- JMF
- Date:
- Fri Mar 24 21:43:57 2017 +0000
- Revision:
- 26:81e520908460
- Parent:
- Socket/UDPSocket.cpp@25:52bad4105cac
- Child:
- 27:2dc9461c04dc
Changing WNC Networking Class names to be prefixed with "Wnc" so they don't collide with the standard networking classes.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
JMF | 1:e511ea8d39d5 | 25 | #include "../WNCInterface.h" |
JMF | 1:e511ea8d39d5 | 26 | |
JMF | 26:81e520908460 | 27 | #include "WncUDPSocket.h" |
JMF | 1:e511ea8d39d5 | 28 | #include <cstring> |
JMF | 1:e511ea8d39d5 | 29 | |
JMF | 1:e511ea8d39d5 | 30 | UDPSocket::UDPSocket() : |
JMF | 1:e511ea8d39d5 | 31 | _is_blocking(0), |
JMF | 1:e511ea8d39d5 | 32 | _btimeout(0){ |
JMF | 1:e511ea8d39d5 | 33 | } |
JMF | 1:e511ea8d39d5 | 34 | |
JMF | 1:e511ea8d39d5 | 35 | UDPSocket::~UDPSocket() { |
JMF | 1:e511ea8d39d5 | 36 | } |
JMF | 1:e511ea8d39d5 | 37 | |
JMF | 1:e511ea8d39d5 | 38 | int UDPSocket::init(void) { |
JMF | 1:e511ea8d39d5 | 39 | _is_blocking = false; // start out not blocking, user will set it if desired |
JMF | 1:e511ea8d39d5 | 40 | return ( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD )? 0:-1; |
JMF | 1:e511ea8d39d5 | 41 | } |
JMF | 1:e511ea8d39d5 | 42 | |
JMF | 1:e511ea8d39d5 | 43 | |
JMF | 1:e511ea8d39d5 | 44 | int UDPSocket::close(void) { |
JMF | 1:e511ea8d39d5 | 45 | Socket::disconnect(); |
JMF | 1:e511ea8d39d5 | 46 | return ( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD )? 0:-1; |
JMF | 1:e511ea8d39d5 | 47 | } |
JMF | 1:e511ea8d39d5 | 48 | |
JMF | 1:e511ea8d39d5 | 49 | // -1 if unsuccessful, else number of bytes written |
JMF | 1:e511ea8d39d5 | 50 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) { |
JMF | 1:e511ea8d39d5 | 51 | int ret = -1; |
JMF | 1:e511ea8d39d5 | 52 | |
JMF | 9:9f0578ff157a | 53 | CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail); |
JMF | 1:e511ea8d39d5 | 54 | if( remote._epAddr.port ) { //make sure the Endpoint has an port assoicated with it |
JMF | 1:e511ea8d39d5 | 55 | if( Socket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port) ) { |
JMF | 25:52bad4105cac | 56 | if( WNCInterface::_pwnc->write(0,(const uint8_t*)packet,length) ) |
JMF | 1:e511ea8d39d5 | 57 | ret = length; |
JMF | 1:e511ea8d39d5 | 58 | close(); |
JMF | 1:e511ea8d39d5 | 59 | } |
JMF | 1:e511ea8d39d5 | 60 | } |
JMF | 1:e511ea8d39d5 | 61 | return ret; |
JMF | 1:e511ea8d39d5 | 62 | } |
JMF | 1:e511ea8d39d5 | 63 | |
JMF | 1:e511ea8d39d5 | 64 | // |
JMF | 1:e511ea8d39d5 | 65 | // blocking is used to make the WNC keep checking for incoming data for a |
JMF | 1:e511ea8d39d5 | 66 | // period of time. |
JMF | 1:e511ea8d39d5 | 67 | |
JMF | 1:e511ea8d39d5 | 68 | void UDPSocket::set_blocking (bool blocking, unsigned int timeout) { |
JMF | 1:e511ea8d39d5 | 69 | _is_blocking = blocking; // true or false |
JMF | 1:e511ea8d39d5 | 70 | _btimeout = timeout; // user specifies in msec |
JMF | 1:e511ea8d39d5 | 71 | |
JMF | 9:9f0578ff157a | 72 | CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void); |
JMF | 1:e511ea8d39d5 | 73 | WNCInterface::_pwnc->setReadRetryWait(0, 0); |
JMF | 1:e511ea8d39d5 | 74 | WNCInterface::_pwnc->setReadRetries(0, 0); |
JMF | 1:e511ea8d39d5 | 75 | } |
JMF | 1:e511ea8d39d5 | 76 | |
JMF | 1:e511ea8d39d5 | 77 | // -1 if unsuccessful, else number of bytes received |
JMF | 1:e511ea8d39d5 | 78 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) { |
JMF | 1:e511ea8d39d5 | 79 | const uint8_t *ptr; |
JMF | 1:e511ea8d39d5 | 80 | Timer t; |
JMF | 1:e511ea8d39d5 | 81 | int done, ret = -1; |
JMF | 1:e511ea8d39d5 | 82 | |
JMF | 1:e511ea8d39d5 | 83 | //make sure the Endpoint has an port assoicated with it |
JMF | 1:e511ea8d39d5 | 84 | if( !remote._epAddr.port ) |
JMF | 1:e511ea8d39d5 | 85 | return -1; |
JMF | 1:e511ea8d39d5 | 86 | |
JMF | 9:9f0578ff157a | 87 | CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail); |
JMF | 1:e511ea8d39d5 | 88 | ret = Socket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port); |
JMF | 1:e511ea8d39d5 | 89 | |
JMF | 1:e511ea8d39d5 | 90 | t.start(); |
JMF | 1:e511ea8d39d5 | 91 | do { |
JMF | 1:e511ea8d39d5 | 92 | ret = WNCInterface::_pwnc->read(0, &ptr); |
JMF | 1:e511ea8d39d5 | 93 | done = ret | (t.read_ms() > _btimeout); |
JMF | 1:e511ea8d39d5 | 94 | } |
JMF | 1:e511ea8d39d5 | 95 | while( _is_blocking && !done ); |
JMF | 1:e511ea8d39d5 | 96 | t.stop(); |
JMF | 1:e511ea8d39d5 | 97 | |
JMF | 1:e511ea8d39d5 | 98 | if( ret > length ) |
JMF | 1:e511ea8d39d5 | 99 | ret = length; |
JMF | 1:e511ea8d39d5 | 100 | memcpy( buffer, ptr, ret ); |
JMF | 1:e511ea8d39d5 | 101 | |
JMF | 1:e511ea8d39d5 | 102 | return ret; |
JMF | 1:e511ea8d39d5 | 103 | } |
JMF | 1:e511ea8d39d5 | 104 | |
JMF | 1:e511ea8d39d5 | 105 |