PIR sensor
Fork of WizFi310Interface_Legacy_newv2 by
Socket/TCPSocketConnection.cpp@2:3194ebe06182, 2017-09-18 (annotated)
- Committer:
- ajeet3004
- Date:
- Mon Sep 18 08:11:28 2017 +0000
- Revision:
- 2:3194ebe06182
- Parent:
- 0:774ff1e8b26b
bgh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:774ff1e8b26b | 1 | /* |
kaizen | 0:774ff1e8b26b | 2 | * Copyright (C) 2013 gsfan, MIT License |
kaizen | 0:774ff1e8b26b | 3 | * |
kaizen | 0:774ff1e8b26b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kaizen | 0:774ff1e8b26b | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kaizen | 0:774ff1e8b26b | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kaizen | 0:774ff1e8b26b | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kaizen | 0:774ff1e8b26b | 8 | * furnished to do so, subject to the following conditions: |
kaizen | 0:774ff1e8b26b | 9 | * |
kaizen | 0:774ff1e8b26b | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kaizen | 0:774ff1e8b26b | 11 | * substantial portions of the Software. |
kaizen | 0:774ff1e8b26b | 12 | * |
kaizen | 0:774ff1e8b26b | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kaizen | 0:774ff1e8b26b | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kaizen | 0:774ff1e8b26b | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kaizen | 0:774ff1e8b26b | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kaizen | 0:774ff1e8b26b | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kaizen | 0:774ff1e8b26b | 18 | */ |
kaizen | 0:774ff1e8b26b | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 20 | * port to the Wiznet Module WizFi250 |
kaizen | 0:774ff1e8b26b | 21 | */ |
kaizen | 0:774ff1e8b26b | 22 | /* Copyright (C) 2017 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 23 | * port to the Wiznet Module WizFi310 |
kaizen | 0:774ff1e8b26b | 24 | */ |
kaizen | 0:774ff1e8b26b | 25 | |
kaizen | 0:774ff1e8b26b | 26 | #include "TCPSocketConnection.h" |
kaizen | 0:774ff1e8b26b | 27 | #include <algorithm> |
kaizen | 0:774ff1e8b26b | 28 | |
kaizen | 0:774ff1e8b26b | 29 | TCPSocketConnection::TCPSocketConnection() {} |
kaizen | 0:774ff1e8b26b | 30 | |
kaizen | 0:774ff1e8b26b | 31 | int TCPSocketConnection::connect(const char* host, const int port) |
kaizen | 0:774ff1e8b26b | 32 | { |
kaizen | 0:774ff1e8b26b | 33 | if (set_address(host, port) != 0) return -1; |
kaizen | 0:774ff1e8b26b | 34 | |
kaizen | 0:774ff1e8b26b | 35 | _server = false; |
kaizen | 0:774ff1e8b26b | 36 | _cid = _wizfi310->open(WizFi310::PROTO_TCP, get_address(), get_port()); |
ajeet3004 | 2:3194ebe06182 | 37 | printf("_cid : %d\n",_cid); |
kaizen | 0:774ff1e8b26b | 38 | if (_cid < 0 ) return -1; |
kaizen | 0:774ff1e8b26b | 39 | |
kaizen | 0:774ff1e8b26b | 40 | return 0; |
kaizen | 0:774ff1e8b26b | 41 | } |
kaizen | 0:774ff1e8b26b | 42 | |
kaizen | 0:774ff1e8b26b | 43 | bool TCPSocketConnection::is_connected(void) |
ajeet3004 | 2:3194ebe06182 | 44 | { printf("_cid: %d\n", _cid); |
kaizen | 0:774ff1e8b26b | 45 | bool _is_connected = _wizfi310->isConnected(_cid); |
kaizen | 0:774ff1e8b26b | 46 | if(!_is_connected) _cid = -1; |
ajeet3004 | 2:3194ebe06182 | 47 | printf(" is_connected: %d\n",_is_connected); |
kaizen | 0:774ff1e8b26b | 48 | return _is_connected; |
kaizen | 0:774ff1e8b26b | 49 | } |
kaizen | 0:774ff1e8b26b | 50 | |
kaizen | 0:774ff1e8b26b | 51 | int TCPSocketConnection::send(char *data, int length) |
kaizen | 0:774ff1e8b26b | 52 | { |
kaizen | 0:774ff1e8b26b | 53 | if (_cid < 0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 54 | |
kaizen | 0:774ff1e8b26b | 55 | return _wizfi310->send(_cid, data, length); |
kaizen | 0:774ff1e8b26b | 56 | } |
kaizen | 0:774ff1e8b26b | 57 | |
kaizen | 0:774ff1e8b26b | 58 | int TCPSocketConnection::send_all(char *data, int length) |
kaizen | 0:774ff1e8b26b | 59 | { |
kaizen | 0:774ff1e8b26b | 60 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 61 | int idx = 0; |
kaizen | 0:774ff1e8b26b | 62 | |
kaizen | 0:774ff1e8b26b | 63 | if(_cid <0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 64 | |
kaizen | 0:774ff1e8b26b | 65 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 66 | while((tmr.read_ms() < _timeout) || _blocking) |
kaizen | 0:774ff1e8b26b | 67 | { |
kaizen | 0:774ff1e8b26b | 68 | idx += _wizfi310->send(_cid, &data[idx], length - idx); |
kaizen | 0:774ff1e8b26b | 69 | if(idx < 0) return -1; |
kaizen | 0:774ff1e8b26b | 70 | |
kaizen | 0:774ff1e8b26b | 71 | if(idx == length) |
kaizen | 0:774ff1e8b26b | 72 | return idx; |
kaizen | 0:774ff1e8b26b | 73 | } |
kaizen | 0:774ff1e8b26b | 74 | return (idx == 0) ? -1 : idx; |
kaizen | 0:774ff1e8b26b | 75 | } |
kaizen | 0:774ff1e8b26b | 76 | |
kaizen | 0:774ff1e8b26b | 77 | int TCPSocketConnection::receive(char* data, int length) |
kaizen | 0:774ff1e8b26b | 78 | { |
kaizen | 0:774ff1e8b26b | 79 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 80 | int time = -1; |
kaizen | 0:774ff1e8b26b | 81 | int len = 0; |
kaizen | 0:774ff1e8b26b | 82 | |
kaizen | 0:774ff1e8b26b | 83 | if(_cid < 0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 84 | |
kaizen | 0:774ff1e8b26b | 85 | if(_blocking) |
kaizen | 0:774ff1e8b26b | 86 | { |
kaizen | 0:774ff1e8b26b | 87 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 88 | while(time < _timeout) |
kaizen | 0:774ff1e8b26b | 89 | { |
kaizen | 0:774ff1e8b26b | 90 | len = _wizfi310->readable(_cid); |
kaizen | 0:774ff1e8b26b | 91 | if(len == -1) |
kaizen | 0:774ff1e8b26b | 92 | return len; |
kaizen | 0:774ff1e8b26b | 93 | |
kaizen | 0:774ff1e8b26b | 94 | if(len > 0) |
kaizen | 0:774ff1e8b26b | 95 | { |
kaizen | 0:774ff1e8b26b | 96 | WIZ_DBG("receive readable : %d\r\n",len); |
kaizen | 0:774ff1e8b26b | 97 | break; |
kaizen | 0:774ff1e8b26b | 98 | } |
kaizen | 0:774ff1e8b26b | 99 | time = tmr.read_ms(); |
kaizen | 0:774ff1e8b26b | 100 | } |
kaizen | 0:774ff1e8b26b | 101 | if(time >= _timeout) |
kaizen | 0:774ff1e8b26b | 102 | { |
kaizen | 0:774ff1e8b26b | 103 | //WIZ_DBG("receive timeout"); |
kaizen | 0:774ff1e8b26b | 104 | return 0; |
kaizen | 0:774ff1e8b26b | 105 | } |
kaizen | 0:774ff1e8b26b | 106 | } |
kaizen | 0:774ff1e8b26b | 107 | |
kaizen | 0:774ff1e8b26b | 108 | return _wizfi310->recv(_cid, data, length); |
kaizen | 0:774ff1e8b26b | 109 | } |
kaizen | 0:774ff1e8b26b | 110 | |
kaizen | 0:774ff1e8b26b | 111 | int TCPSocketConnection::receive_all(char* data, int length) |
kaizen | 0:774ff1e8b26b | 112 | { |
kaizen | 0:774ff1e8b26b | 113 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 114 | int idx = 0; |
kaizen | 0:774ff1e8b26b | 115 | int time = -1; |
kaizen | 0:774ff1e8b26b | 116 | |
kaizen | 0:774ff1e8b26b | 117 | if(_cid < 0 || !is_connected()) return -1; |
kaizen | 0:774ff1e8b26b | 118 | |
kaizen | 0:774ff1e8b26b | 119 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 120 | |
kaizen | 0:774ff1e8b26b | 121 | while(time < _timeout || _blocking) |
kaizen | 0:774ff1e8b26b | 122 | { |
kaizen | 0:774ff1e8b26b | 123 | idx += _wizfi310->recv(_cid, &data[idx], length - idx); |
kaizen | 0:774ff1e8b26b | 124 | if (idx < 0) return -1; |
kaizen | 0:774ff1e8b26b | 125 | |
kaizen | 0:774ff1e8b26b | 126 | if(idx == length) |
kaizen | 0:774ff1e8b26b | 127 | break; |
kaizen | 0:774ff1e8b26b | 128 | |
kaizen | 0:774ff1e8b26b | 129 | time = tmr.read_ms(); |
kaizen | 0:774ff1e8b26b | 130 | } |
kaizen | 0:774ff1e8b26b | 131 | |
kaizen | 0:774ff1e8b26b | 132 | return (idx == 0) ? -1 : idx; |
kaizen | 0:774ff1e8b26b | 133 | } |
kaizen | 0:774ff1e8b26b | 134 | |
kaizen | 0:774ff1e8b26b | 135 | void TCPSocketConnection::acceptCID (int cid) |
kaizen | 0:774ff1e8b26b | 136 | { |
kaizen | 0:774ff1e8b26b | 137 | char *ip; |
kaizen | 0:774ff1e8b26b | 138 | int port; |
kaizen | 0:774ff1e8b26b | 139 | _server = true; |
kaizen | 0:774ff1e8b26b | 140 | _cid = cid; |
kaizen | 0:774ff1e8b26b | 141 | |
kaizen | 0:774ff1e8b26b | 142 | |
kaizen | 0:774ff1e8b26b | 143 | if( _wizfi310->cmdSMGMT(cid) ) return; |
kaizen | 0:774ff1e8b26b | 144 | if( !_wizfi310->getRemote(_cid, &ip, &port)) |
ajeet3004 | 2:3194ebe06182 | 145 | { |
kaizen | 0:774ff1e8b26b | 146 | set_address(ip, port); |
kaizen | 0:774ff1e8b26b | 147 | } |
kaizen | 0:774ff1e8b26b | 148 | } |