![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Catie fork
Dependencies: SDFileSystem X_NUCLEO_IKS01A1
Fork of ATT_AWS_IoT_demo by
WNCInterface/WNCSocket/WNCTCPSocketConnection.cpp@15:6f2798e45099, 2016-12-01 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
ampembeng | 15:6f2798e45099 | 1 | /* ===================================================================== |
ampembeng | 15:6f2798e45099 | 2 | Copyright © 2016, Avnet (R) |
ampembeng | 15:6f2798e45099 | 3 | Contributors: |
ampembeng | 15:6f2798e45099 | 4 | * James M Flynn, www.em.avnet.com |
ampembeng | 15:6f2798e45099 | 5 | |
ampembeng | 15:6f2798e45099 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
ampembeng | 15:6f2798e45099 | 7 | you may not use this file except in compliance with the License. |
ampembeng | 15:6f2798e45099 | 8 | You may obtain a copy of the License at |
ampembeng | 15:6f2798e45099 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
ampembeng | 15:6f2798e45099 | 10 | Unless required by applicable law or agreed to in writing, |
ampembeng | 15:6f2798e45099 | 11 | software distributed under the License is distributed on an |
ampembeng | 15:6f2798e45099 | 12 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
ampembeng | 15:6f2798e45099 | 13 | either express or implied. See the License for the specific |
ampembeng | 15:6f2798e45099 | 14 | language governing permissions and limitations under the License. |
ampembeng | 15:6f2798e45099 | 15 | @file WNCInterface.cpp |
ampembeng | 15:6f2798e45099 | 16 | @version 1.0 |
ampembeng | 15:6f2798e45099 | 17 | @date Sept 2016 |
ampembeng | 15:6f2798e45099 | 18 | ======================================================================== */ |
ampembeng | 15:6f2798e45099 | 19 | |
ampembeng | 15:6f2798e45099 | 20 | #include "../WNCInterface.h" |
ampembeng | 15:6f2798e45099 | 21 | |
ampembeng | 15:6f2798e45099 | 22 | #include "WNCSocket.h" |
ampembeng | 15:6f2798e45099 | 23 | #include "WNCTCPSocketConnection.h" |
ampembeng | 15:6f2798e45099 | 24 | #include <cstring> |
ampembeng | 15:6f2798e45099 | 25 | |
ampembeng | 15:6f2798e45099 | 26 | #define READ_EVERYMS 500 //number of milliseconds between WNC socket reads |
ampembeng | 15:6f2798e45099 | 27 | |
ampembeng | 15:6f2798e45099 | 28 | WNCTCPSocketConnection::WNCTCPSocketConnection() : |
ampembeng | 15:6f2798e45099 | 29 | _is_blocking(0), |
ampembeng | 15:6f2798e45099 | 30 | _btimeout(0){ |
ampembeng | 15:6f2798e45099 | 31 | } |
ampembeng | 15:6f2798e45099 | 32 | |
ampembeng | 15:6f2798e45099 | 33 | // |
ampembeng | 15:6f2798e45099 | 34 | // blocking is used to make the WNC keep checking for incoming data for a |
ampembeng | 15:6f2798e45099 | 35 | // period of time. |
ampembeng | 15:6f2798e45099 | 36 | // |
ampembeng | 15:6f2798e45099 | 37 | void WNCTCPSocketConnection::set_blocking (bool blocking, unsigned int timeout) { |
ampembeng | 15:6f2798e45099 | 38 | _is_blocking = blocking; // true if we want to wait for request |
ampembeng | 15:6f2798e45099 | 39 | _btimeout = timeout; // user specs msec |
ampembeng | 15:6f2798e45099 | 40 | |
ampembeng | 15:6f2798e45099 | 41 | CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void); |
ampembeng | 15:6f2798e45099 | 42 | M_LOCK; |
ampembeng | 15:6f2798e45099 | 43 | WNCInterface::_pwnc->setReadRetryWait(0, 0); |
ampembeng | 15:6f2798e45099 | 44 | WNCInterface::_pwnc->setReadRetries(0, 0); |
ampembeng | 15:6f2798e45099 | 45 | M_ULOCK; |
ampembeng | 15:6f2798e45099 | 46 | } |
ampembeng | 15:6f2798e45099 | 47 | |
ampembeng | 15:6f2798e45099 | 48 | |
ampembeng | 15:6f2798e45099 | 49 | int WNCTCPSocketConnection::connect(const char* host, const int port) { |
ampembeng | 15:6f2798e45099 | 50 | WNCSocket::connect((char*)host, SOCK_STREAM, port); |
ampembeng | 15:6f2798e45099 | 51 | _is_blocking = false; // start out not blocking, user will set it if desired |
ampembeng | 15:6f2798e45099 | 52 | return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1; |
ampembeng | 15:6f2798e45099 | 53 | } |
ampembeng | 15:6f2798e45099 | 54 | |
ampembeng | 15:6f2798e45099 | 55 | bool WNCTCPSocketConnection::is_connected(void) { |
ampembeng | 15:6f2798e45099 | 56 | return ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 1:0; |
ampembeng | 15:6f2798e45099 | 57 | } |
ampembeng | 15:6f2798e45099 | 58 | |
ampembeng | 15:6f2798e45099 | 59 | int WNCTCPSocketConnection::send(char* data, int length) { |
ampembeng | 15:6f2798e45099 | 60 | int ret = -1; |
ampembeng | 15:6f2798e45099 | 61 | |
ampembeng | 15:6f2798e45099 | 62 | WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus(); |
ampembeng | 15:6f2798e45099 | 63 | |
ampembeng | 15:6f2798e45099 | 64 | CHK_WNCFE(( s == FATAL_FLAG ), fail); |
ampembeng | 15:6f2798e45099 | 65 | |
ampembeng | 15:6f2798e45099 | 66 | if( s == WncController_fk::WncController::WNC_ON ) { |
ampembeng | 15:6f2798e45099 | 67 | M_LOCK; |
ampembeng | 15:6f2798e45099 | 68 | if( WNCInterface::_pwnc->write(0, data, length) ) |
ampembeng | 15:6f2798e45099 | 69 | ret = length; |
ampembeng | 15:6f2798e45099 | 70 | M_ULOCK; |
ampembeng | 15:6f2798e45099 | 71 | } |
ampembeng | 15:6f2798e45099 | 72 | return ret; |
ampembeng | 15:6f2798e45099 | 73 | } |
ampembeng | 15:6f2798e45099 | 74 | |
ampembeng | 15:6f2798e45099 | 75 | int WNCTCPSocketConnection::receive(char *readBuf, int length) { |
ampembeng | 15:6f2798e45099 | 76 | Timer t; |
ampembeng | 15:6f2798e45099 | 77 | size_t done, cnt; |
ampembeng | 15:6f2798e45099 | 78 | int ret=-1; |
ampembeng | 15:6f2798e45099 | 79 | WncController_fk::WncController::WncState_e s = WNCInterface::_pwnc->getWncStatus(); |
ampembeng | 15:6f2798e45099 | 80 | |
ampembeng | 15:6f2798e45099 | 81 | CHK_WNCFE(( s == FATAL_FLAG ), fail); |
ampembeng | 15:6f2798e45099 | 82 | if( s != WncController_fk::WncController::WNC_ON ) |
ampembeng | 15:6f2798e45099 | 83 | return ret; |
ampembeng | 15:6f2798e45099 | 84 | |
ampembeng | 15:6f2798e45099 | 85 | M_LOCK; |
ampembeng | 15:6f2798e45099 | 86 | t.start(); |
ampembeng | 15:6f2798e45099 | 87 | do { |
ampembeng | 15:6f2798e45099 | 88 | if( !(t.read_ms() % READ_EVERYMS) ) |
ampembeng | 15:6f2798e45099 | 89 | cnt = WNCInterface::_pwnc->read(0, (uint8_t *)readBuf, (uint32_t) length); |
ampembeng | 15:6f2798e45099 | 90 | if( _is_blocking ) |
ampembeng | 15:6f2798e45099 | 91 | done = cnt; |
ampembeng | 15:6f2798e45099 | 92 | else |
ampembeng | 15:6f2798e45099 | 93 | done = cnt | (t.read_ms() > _btimeout); |
ampembeng | 15:6f2798e45099 | 94 | } |
ampembeng | 15:6f2798e45099 | 95 | while( !done ); |
ampembeng | 15:6f2798e45099 | 96 | t.stop(); |
ampembeng | 15:6f2798e45099 | 97 | M_ULOCK; |
ampembeng | 15:6f2798e45099 | 98 | |
ampembeng | 15:6f2798e45099 | 99 | if( WNCInterface::_pwnc->getWncStatus() == WNC_GOOD ) { |
ampembeng | 15:6f2798e45099 | 100 | //readBuf[cnt] = '\0'; |
ampembeng | 15:6f2798e45099 | 101 | ret = (int)cnt; |
ampembeng | 15:6f2798e45099 | 102 | } |
ampembeng | 15:6f2798e45099 | 103 | else |
ampembeng | 15:6f2798e45099 | 104 | ret = -1; |
ampembeng | 15:6f2798e45099 | 105 | |
ampembeng | 15:6f2798e45099 | 106 | return ret; |
ampembeng | 15:6f2798e45099 | 107 | } |
ampembeng | 15:6f2798e45099 | 108 | |
ampembeng | 15:6f2798e45099 | 109 | int WNCTCPSocketConnection::send_all(char* data, int length) { |
ampembeng | 15:6f2798e45099 | 110 | return send(data,length); |
ampembeng | 15:6f2798e45099 | 111 | } |
ampembeng | 15:6f2798e45099 | 112 | |
ampembeng | 15:6f2798e45099 | 113 | int WNCTCPSocketConnection::receive_all(char* data, int length) { |
ampembeng | 15:6f2798e45099 | 114 | return receive(data,length); |
ampembeng | 15:6f2798e45099 | 115 | } |
ampembeng | 15:6f2798e45099 | 116 | |
ampembeng | 15:6f2798e45099 | 117 | int WNCTCPSocketConnection::close(void) { |
ampembeng | 15:6f2798e45099 | 118 | WNCSocket::disconnect(); |
ampembeng | 15:6f2798e45099 | 119 | M_LOCK; |
ampembeng | 15:6f2798e45099 | 120 | int ret = ( WNCInterface::_pwnc->getWncStatus() == WncController_fk::WncController::WNC_ON )? 0:-1; |
ampembeng | 15:6f2798e45099 | 121 | M_ULOCK; |
ampembeng | 15:6f2798e45099 | 122 | return ret; |
ampembeng | 15:6f2798e45099 | 123 | } |
ampembeng | 15:6f2798e45099 | 124 | |
ampembeng | 15:6f2798e45099 | 125 |