NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Socket.cpp@42:49893d13c432, 2016-02-23 (annotated)
- Committer:
- Christopher Haster
- Date:
- Tue Feb 23 05:07:02 2016 -0600
- Branch:
- api-changes
- Revision:
- 42:49893d13c432
- Parent:
- 41:3ec1c97e9bbf
- Child:
- 43:09ea32f2eb54
Standardized some C++ style things
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
25:ed7b2a52e8ac | 1 | /* Socket |
Christopher Haster |
25:ed7b2a52e8ac | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
25:ed7b2a52e8ac | 3 | * |
Christopher Haster |
25:ed7b2a52e8ac | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
25:ed7b2a52e8ac | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
25:ed7b2a52e8ac | 6 | * You may obtain a copy of the License at |
Christopher Haster |
25:ed7b2a52e8ac | 7 | * |
Christopher Haster |
25:ed7b2a52e8ac | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
25:ed7b2a52e8ac | 9 | * |
Christopher Haster |
25:ed7b2a52e8ac | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
25:ed7b2a52e8ac | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
25:ed7b2a52e8ac | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
25:ed7b2a52e8ac | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
25:ed7b2a52e8ac | 14 | * limitations under the License. |
Christopher Haster |
25:ed7b2a52e8ac | 15 | */ |
Christopher Haster |
25:ed7b2a52e8ac | 16 | |
Christopher Haster |
25:ed7b2a52e8ac | 17 | #include "Socket.h" |
geky | 29:7bcec3189a93 | 18 | #include <string.h> |
Christopher Haster |
25:ed7b2a52e8ac | 19 | |
Christopher Haster |
28:163fbe3263f4 | 20 | Socket::Socket(NetworkInterface *iface, socket_protocol_t proto) |
geky | 30:3cc78f5db99d | 21 | : _iface(iface) |
geky | 30:3cc78f5db99d | 22 | , _proto(proto) |
geky | 30:3cc78f5db99d | 23 | , _socket(0) |
geky | 30:3cc78f5db99d | 24 | { |
Christopher Haster |
42:49893d13c432 | 25 | memset(_ip_address, 0, NS_IP_SIZE); |
Christopher Haster |
28:163fbe3263f4 | 26 | _port = 0; |
Christopher Haster |
41:3ec1c97e9bbf | 27 | |
Christopher Haster |
41:3ec1c97e9bbf | 28 | _timeout = iface->getTimeout(); |
Christopher Haster |
25:ed7b2a52e8ac | 29 | } |
Christopher Haster |
25:ed7b2a52e8ac | 30 | |
geky | 30:3cc78f5db99d | 31 | Socket::~Socket() |
geky | 30:3cc78f5db99d | 32 | { |
Christopher Haster |
32:2c5fc105fc50 | 33 | if (_socket) close(); |
Christopher Haster |
25:ed7b2a52e8ac | 34 | } |
Christopher Haster |
25:ed7b2a52e8ac | 35 | |
Christopher Haster |
34:c17745683385 | 36 | int32_t Socket::setURL(const char *url) |
geky | 30:3cc78f5db99d | 37 | { |
geky | 31:7f15b95f2a1d | 38 | int32_t err = _iface->getHostByName(url, _ip_address); |
Christopher Haster |
32:2c5fc105fc50 | 39 | if (err) return err; |
geky | 31:7f15b95f2a1d | 40 | |
geky | 31:7f15b95f2a1d | 41 | if (_socket) { |
geky | 31:7f15b95f2a1d | 42 | _socket->setIPAddress(_ip_address); |
geky | 31:7f15b95f2a1d | 43 | } |
Christopher Haster |
28:163fbe3263f4 | 44 | |
Christopher Haster |
28:163fbe3263f4 | 45 | return 0; |
Christopher Haster |
25:ed7b2a52e8ac | 46 | } |
Christopher Haster |
25:ed7b2a52e8ac | 47 | |
Christopher Haster |
34:c17745683385 | 48 | void Socket::setIPAddress(const char *ip) |
geky | 30:3cc78f5db99d | 49 | { |
Christopher Haster |
28:163fbe3263f4 | 50 | strcpy(_ip_address, ip); |
Christopher Haster |
28:163fbe3263f4 | 51 | |
Christopher Haster |
28:163fbe3263f4 | 52 | if (_socket) { |
geky | 31:7f15b95f2a1d | 53 | _socket->setIPAddress(_ip_address); |
Christopher Haster |
27:d7ed39727306 | 54 | } |
Christopher Haster |
25:ed7b2a52e8ac | 55 | } |
Christopher Haster |
25:ed7b2a52e8ac | 56 | |
geky | 30:3cc78f5db99d | 57 | void Socket::setPort(uint16_t port) |
geky | 30:3cc78f5db99d | 58 | { |
Christopher Haster |
28:163fbe3263f4 | 59 | _port = port; |
Christopher Haster |
28:163fbe3263f4 | 60 | |
Christopher Haster |
28:163fbe3263f4 | 61 | if (_socket) { |
geky | 31:7f15b95f2a1d | 62 | _socket->setPort(_port); |
Christopher Haster |
27:d7ed39727306 | 63 | } |
Christopher Haster |
25:ed7b2a52e8ac | 64 | } |
Christopher Haster |
25:ed7b2a52e8ac | 65 | |
geky | 30:3cc78f5db99d | 66 | const char *Socket::getIPAddress() const |
geky | 30:3cc78f5db99d | 67 | { |
Christopher Haster |
39:47138420ea42 | 68 | if (_ip_address[0]) { |
Christopher Haster |
39:47138420ea42 | 69 | return _ip_address; |
Christopher Haster |
39:47138420ea42 | 70 | } else { |
Christopher Haster |
39:47138420ea42 | 71 | return 0; |
Christopher Haster |
39:47138420ea42 | 72 | } |
Christopher Haster |
25:ed7b2a52e8ac | 73 | } |
Christopher Haster |
25:ed7b2a52e8ac | 74 | |
geky | 30:3cc78f5db99d | 75 | uint16_t Socket::getPort() const |
geky | 30:3cc78f5db99d | 76 | { |
Christopher Haster |
28:163fbe3263f4 | 77 | return _port; |
Christopher Haster |
25:ed7b2a52e8ac | 78 | } |
Christopher Haster |
25:ed7b2a52e8ac | 79 | |
Christopher Haster |
41:3ec1c97e9bbf | 80 | void Socket::setTimeout(uint32_t timeout) |
Christopher Haster |
41:3ec1c97e9bbf | 81 | { |
Christopher Haster |
41:3ec1c97e9bbf | 82 | _timeout = timeout; |
Christopher Haster |
41:3ec1c97e9bbf | 83 | |
Christopher Haster |
41:3ec1c97e9bbf | 84 | if (_socket) { |
Christopher Haster |
41:3ec1c97e9bbf | 85 | _socket->setTimeout(timeout); |
Christopher Haster |
41:3ec1c97e9bbf | 86 | } |
Christopher Haster |
41:3ec1c97e9bbf | 87 | } |
Christopher Haster |
41:3ec1c97e9bbf | 88 | |
Christopher Haster |
41:3ec1c97e9bbf | 89 | uint32_t Socket::getTimeout() const |
Christopher Haster |
41:3ec1c97e9bbf | 90 | { |
Christopher Haster |
41:3ec1c97e9bbf | 91 | return _timeout; |
Christopher Haster |
41:3ec1c97e9bbf | 92 | } |
Christopher Haster |
41:3ec1c97e9bbf | 93 | |
Christopher Haster |
38:157fb2ab965f | 94 | bool Socket::isConnected() |
Christopher Haster |
38:157fb2ab965f | 95 | { |
Christopher Haster |
38:157fb2ab965f | 96 | if (!_socket) { |
Christopher Haster |
38:157fb2ab965f | 97 | return false; |
Christopher Haster |
38:157fb2ab965f | 98 | } |
Christopher Haster |
38:157fb2ab965f | 99 | |
Christopher Haster |
38:157fb2ab965f | 100 | return _socket->isConnected(); |
Christopher Haster |
38:157fb2ab965f | 101 | } |
Christopher Haster |
38:157fb2ab965f | 102 | |
Christopher Haster |
34:c17745683385 | 103 | int32_t Socket::open(const char *url, uint16_t port) |
Christopher Haster |
32:2c5fc105fc50 | 104 | { |
Christopher Haster |
41:3ec1c97e9bbf | 105 | int32_t err; |
Christopher Haster |
41:3ec1c97e9bbf | 106 | |
Christopher Haster |
41:3ec1c97e9bbf | 107 | err = close(); |
Christopher Haster |
41:3ec1c97e9bbf | 108 | if (err) return err; |
Christopher Haster |
34:c17745683385 | 109 | |
Christopher Haster |
34:c17745683385 | 110 | if (url) { |
Christopher Haster |
41:3ec1c97e9bbf | 111 | err = setURL(url); |
Christopher Haster |
34:c17745683385 | 112 | if (err) return err; |
Christopher Haster |
34:c17745683385 | 113 | } |
Christopher Haster |
34:c17745683385 | 114 | |
Christopher Haster |
34:c17745683385 | 115 | if (port) { |
Christopher Haster |
34:c17745683385 | 116 | setPort(port); |
Christopher Haster |
34:c17745683385 | 117 | } |
Christopher Haster |
34:c17745683385 | 118 | |
Christopher Haster |
39:47138420ea42 | 119 | if (!getIPAddress() || !getPort()) { |
Christopher Haster |
35:838393fbc2ca | 120 | return -3; |
Christopher Haster |
35:838393fbc2ca | 121 | } |
Christopher Haster |
32:2c5fc105fc50 | 122 | |
Christopher Haster |
32:2c5fc105fc50 | 123 | _socket = _iface->createSocket(_proto); |
Christopher Haster |
32:2c5fc105fc50 | 124 | if (!_socket) return -2; |
Christopher Haster |
32:2c5fc105fc50 | 125 | |
Christopher Haster |
41:3ec1c97e9bbf | 126 | _socket->setTimeout(_timeout); |
Christopher Haster |
41:3ec1c97e9bbf | 127 | |
Christopher Haster |
41:3ec1c97e9bbf | 128 | err = _socket->open(_ip_address, _port); |
Christopher Haster |
34:c17745683385 | 129 | |
Christopher Haster |
32:2c5fc105fc50 | 130 | if (err) { |
Christopher Haster |
32:2c5fc105fc50 | 131 | _iface->destroySocket(_socket); |
Christopher Haster |
32:2c5fc105fc50 | 132 | } |
Christopher Haster |
32:2c5fc105fc50 | 133 | |
Christopher Haster |
32:2c5fc105fc50 | 134 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 135 | } |
Christopher Haster |
32:2c5fc105fc50 | 136 | |
Christopher Haster |
32:2c5fc105fc50 | 137 | int32_t Socket::close() |
Christopher Haster |
32:2c5fc105fc50 | 138 | { |
Christopher Haster |
32:2c5fc105fc50 | 139 | if (!_socket) return 0; |
Christopher Haster |
32:2c5fc105fc50 | 140 | |
Christopher Haster |
32:2c5fc105fc50 | 141 | int32_t err = _socket->close(); |
Christopher Haster |
32:2c5fc105fc50 | 142 | |
Christopher Haster |
32:2c5fc105fc50 | 143 | if (!err) { |
Christopher Haster |
32:2c5fc105fc50 | 144 | _iface->destroySocket(_socket); |
Christopher Haster |
32:2c5fc105fc50 | 145 | } |
Christopher Haster |
32:2c5fc105fc50 | 146 | |
Christopher Haster |
32:2c5fc105fc50 | 147 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 148 | } |
Christopher Haster |
32:2c5fc105fc50 | 149 | |
Christopher Haster |
41:3ec1c97e9bbf | 150 | int32_t Socket::send(const void *data, uint32_t len) |
geky | 30:3cc78f5db99d | 151 | { |
Christopher Haster |
32:2c5fc105fc50 | 152 | if (!_socket) return -2; |
Christopher Haster |
41:3ec1c97e9bbf | 153 | return _socket->send(data, len); |
Christopher Haster |
25:ed7b2a52e8ac | 154 | } |
Christopher Haster |
25:ed7b2a52e8ac | 155 | |
Christopher Haster |
41:3ec1c97e9bbf | 156 | int32_t Socket::recv(void *data, uint32_t len) |
geky | 30:3cc78f5db99d | 157 | { |
Christopher Haster |
32:2c5fc105fc50 | 158 | if (!_socket) return -2; |
Christopher Haster |
41:3ec1c97e9bbf | 159 | return _socket->recv(data, len); |
Christopher Haster |
25:ed7b2a52e8ac | 160 | } |
Christopher Haster |
25:ed7b2a52e8ac | 161 |