WNCInterface
Dependencies: WncControllerK64F
Fork of WNCInterface by
Socket/UDPSocket.cpp@1:e511ea8d39d5, 2016-09-21 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 21 15:20:12 2016 +0000
- Revision:
- 1:e511ea8d39d5
- Child:
- 9:9f0578ff157a
Adding in all the Sockets, missed them on the initial commit.
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 | 1:e511ea8d39d5 | 27 | #include "UDPSocket.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 | 1:e511ea8d39d5 | 53 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 1:e511ea8d39d5 | 54 | FATAL_WNC_ERROR(fail); |
JMF | 1:e511ea8d39d5 | 55 | |
JMF | 1:e511ea8d39d5 | 56 | if( remote._epAddr.port ) { //make sure the Endpoint has an port assoicated with it |
JMF | 1:e511ea8d39d5 | 57 | if( Socket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port) ) { |
JMF | 1:e511ea8d39d5 | 58 | if( WNCInterface::_pwnc->write(0,packet,length) ) |
JMF | 1:e511ea8d39d5 | 59 | ret = length; |
JMF | 1:e511ea8d39d5 | 60 | close(); |
JMF | 1:e511ea8d39d5 | 61 | } |
JMF | 1:e511ea8d39d5 | 62 | } |
JMF | 1:e511ea8d39d5 | 63 | return ret; |
JMF | 1:e511ea8d39d5 | 64 | } |
JMF | 1:e511ea8d39d5 | 65 | |
JMF | 1:e511ea8d39d5 | 66 | // |
JMF | 1:e511ea8d39d5 | 67 | // blocking is used to make the WNC keep checking for incoming data for a |
JMF | 1:e511ea8d39d5 | 68 | // period of time. |
JMF | 1:e511ea8d39d5 | 69 | |
JMF | 1:e511ea8d39d5 | 70 | void UDPSocket::set_blocking (bool blocking, unsigned int timeout) { |
JMF | 1:e511ea8d39d5 | 71 | _is_blocking = blocking; // true or false |
JMF | 1:e511ea8d39d5 | 72 | _btimeout = timeout; // user specifies in msec |
JMF | 1:e511ea8d39d5 | 73 | |
JMF | 1:e511ea8d39d5 | 74 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 1:e511ea8d39d5 | 75 | FATAL_WNC_ERROR(void); |
JMF | 1:e511ea8d39d5 | 76 | |
JMF | 1:e511ea8d39d5 | 77 | WNCInterface::_pwnc->setReadRetryWait(0, 0); |
JMF | 1:e511ea8d39d5 | 78 | WNCInterface::_pwnc->setReadRetries(0, 0); |
JMF | 1:e511ea8d39d5 | 79 | } |
JMF | 1:e511ea8d39d5 | 80 | |
JMF | 1:e511ea8d39d5 | 81 | // -1 if unsuccessful, else number of bytes received |
JMF | 1:e511ea8d39d5 | 82 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) { |
JMF | 1:e511ea8d39d5 | 83 | const uint8_t *ptr; |
JMF | 1:e511ea8d39d5 | 84 | Timer t; |
JMF | 1:e511ea8d39d5 | 85 | int done, ret = -1; |
JMF | 1:e511ea8d39d5 | 86 | |
JMF | 1:e511ea8d39d5 | 87 | //make sure the Endpoint has an port assoicated with it |
JMF | 1:e511ea8d39d5 | 88 | if( !remote._epAddr.port ) |
JMF | 1:e511ea8d39d5 | 89 | return -1; |
JMF | 1:e511ea8d39d5 | 90 | |
JMF | 1:e511ea8d39d5 | 91 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 1:e511ea8d39d5 | 92 | FATAL_WNC_ERROR(fail); |
JMF | 1:e511ea8d39d5 | 93 | |
JMF | 1:e511ea8d39d5 | 94 | ret = Socket::connect(remote._epAddr.IP,SOCK_DGRAM,remote._epAddr.port); |
JMF | 1:e511ea8d39d5 | 95 | |
JMF | 1:e511ea8d39d5 | 96 | t.start(); |
JMF | 1:e511ea8d39d5 | 97 | do { |
JMF | 1:e511ea8d39d5 | 98 | ret = WNCInterface::_pwnc->read(0, &ptr); |
JMF | 1:e511ea8d39d5 | 99 | done = ret | (t.read_ms() > _btimeout); |
JMF | 1:e511ea8d39d5 | 100 | } |
JMF | 1:e511ea8d39d5 | 101 | while( _is_blocking && !done ); |
JMF | 1:e511ea8d39d5 | 102 | t.stop(); |
JMF | 1:e511ea8d39d5 | 103 | |
JMF | 1:e511ea8d39d5 | 104 | if( ret > length ) |
JMF | 1:e511ea8d39d5 | 105 | ret = length; |
JMF | 1:e511ea8d39d5 | 106 | memcpy( buffer, ptr, ret ); |
JMF | 1:e511ea8d39d5 | 107 | |
JMF | 1:e511ea8d39d5 | 108 | return ret; |
JMF | 1:e511ea8d39d5 | 109 | } |
JMF | 1:e511ea8d39d5 | 110 | |
JMF | 1:e511ea8d39d5 | 111 |