NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Socket.cpp@34:c17745683385, 2016-02-22 (annotated)
- Committer:
- Christopher Haster
- Date:
- Mon Feb 22 19:56:44 2016 -0600
- Branch:
- api-changes
- Revision:
- 34:c17745683385
- Parent:
- 32:2c5fc105fc50
- Child:
- 35:838393fbc2ca
Added url/ip/port as optional arguments to open
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 |
28:163fbe3263f4 | 25 | memset(_ip_address, 0, SOCK_IP_SIZE); |
Christopher Haster |
28:163fbe3263f4 | 26 | _port = 0; |
Christopher Haster |
25:ed7b2a52e8ac | 27 | } |
Christopher Haster |
25:ed7b2a52e8ac | 28 | |
geky | 30:3cc78f5db99d | 29 | Socket::~Socket() |
geky | 30:3cc78f5db99d | 30 | { |
Christopher Haster |
32:2c5fc105fc50 | 31 | if (_socket) close(); |
Christopher Haster |
25:ed7b2a52e8ac | 32 | } |
Christopher Haster |
25:ed7b2a52e8ac | 33 | |
Christopher Haster |
34:c17745683385 | 34 | int32_t Socket::setURL(const char *url) |
geky | 30:3cc78f5db99d | 35 | { |
geky | 31:7f15b95f2a1d | 36 | int32_t err = _iface->getHostByName(url, _ip_address); |
Christopher Haster |
32:2c5fc105fc50 | 37 | if (err) return err; |
geky | 31:7f15b95f2a1d | 38 | |
geky | 31:7f15b95f2a1d | 39 | if (_socket) { |
geky | 31:7f15b95f2a1d | 40 | _socket->setIPAddress(_ip_address); |
geky | 31:7f15b95f2a1d | 41 | } |
Christopher Haster |
28:163fbe3263f4 | 42 | |
Christopher Haster |
28:163fbe3263f4 | 43 | return 0; |
Christopher Haster |
25:ed7b2a52e8ac | 44 | } |
Christopher Haster |
25:ed7b2a52e8ac | 45 | |
Christopher Haster |
34:c17745683385 | 46 | void Socket::setIPAddress(const char *ip) |
geky | 30:3cc78f5db99d | 47 | { |
Christopher Haster |
28:163fbe3263f4 | 48 | strcpy(_ip_address, ip); |
Christopher Haster |
28:163fbe3263f4 | 49 | |
Christopher Haster |
28:163fbe3263f4 | 50 | if (_socket) { |
geky | 31:7f15b95f2a1d | 51 | _socket->setIPAddress(_ip_address); |
Christopher Haster |
27:d7ed39727306 | 52 | } |
Christopher Haster |
25:ed7b2a52e8ac | 53 | } |
Christopher Haster |
25:ed7b2a52e8ac | 54 | |
geky | 30:3cc78f5db99d | 55 | void Socket::setPort(uint16_t port) |
geky | 30:3cc78f5db99d | 56 | { |
Christopher Haster |
28:163fbe3263f4 | 57 | _port = port; |
Christopher Haster |
28:163fbe3263f4 | 58 | |
Christopher Haster |
28:163fbe3263f4 | 59 | if (_socket) { |
geky | 31:7f15b95f2a1d | 60 | _socket->setPort(_port); |
Christopher Haster |
27:d7ed39727306 | 61 | } |
Christopher Haster |
25:ed7b2a52e8ac | 62 | } |
Christopher Haster |
25:ed7b2a52e8ac | 63 | |
geky | 30:3cc78f5db99d | 64 | const char *Socket::getIPAddress() const |
geky | 30:3cc78f5db99d | 65 | { |
Christopher Haster |
28:163fbe3263f4 | 66 | return _ip_address; |
Christopher Haster |
25:ed7b2a52e8ac | 67 | } |
Christopher Haster |
25:ed7b2a52e8ac | 68 | |
geky | 30:3cc78f5db99d | 69 | uint16_t Socket::getPort() const |
geky | 30:3cc78f5db99d | 70 | { |
Christopher Haster |
28:163fbe3263f4 | 71 | return _port; |
Christopher Haster |
25:ed7b2a52e8ac | 72 | } |
Christopher Haster |
25:ed7b2a52e8ac | 73 | |
Christopher Haster |
34:c17745683385 | 74 | int32_t Socket::open(const char *url, uint16_t port) |
Christopher Haster |
32:2c5fc105fc50 | 75 | { |
Christopher Haster |
34:c17745683385 | 76 | if (_socket) { |
Christopher Haster |
34:c17745683385 | 77 | int32_t err = close(); |
Christopher Haster |
34:c17745683385 | 78 | if (err) return err; |
Christopher Haster |
34:c17745683385 | 79 | } |
Christopher Haster |
34:c17745683385 | 80 | |
Christopher Haster |
34:c17745683385 | 81 | if (url) { |
Christopher Haster |
34:c17745683385 | 82 | int32_t err = setURL(url); |
Christopher Haster |
34:c17745683385 | 83 | if (err) return err; |
Christopher Haster |
34:c17745683385 | 84 | } |
Christopher Haster |
34:c17745683385 | 85 | |
Christopher Haster |
34:c17745683385 | 86 | if (port) { |
Christopher Haster |
34:c17745683385 | 87 | setPort(port); |
Christopher Haster |
34:c17745683385 | 88 | } |
Christopher Haster |
34:c17745683385 | 89 | |
Christopher Haster |
32:2c5fc105fc50 | 90 | |
Christopher Haster |
32:2c5fc105fc50 | 91 | _socket = _iface->createSocket(_proto); |
Christopher Haster |
32:2c5fc105fc50 | 92 | if (!_socket) return -2; |
Christopher Haster |
32:2c5fc105fc50 | 93 | |
Christopher Haster |
32:2c5fc105fc50 | 94 | if (_ip_address[0]) { |
Christopher Haster |
32:2c5fc105fc50 | 95 | _socket->setIPAddress(_ip_address); |
Christopher Haster |
32:2c5fc105fc50 | 96 | } |
Christopher Haster |
32:2c5fc105fc50 | 97 | |
Christopher Haster |
32:2c5fc105fc50 | 98 | if (_port) { |
Christopher Haster |
32:2c5fc105fc50 | 99 | _socket->setPort(_port); |
Christopher Haster |
32:2c5fc105fc50 | 100 | } |
Christopher Haster |
32:2c5fc105fc50 | 101 | |
Christopher Haster |
34:c17745683385 | 102 | |
Christopher Haster |
32:2c5fc105fc50 | 103 | int32_t err = _socket->open(); |
Christopher Haster |
32:2c5fc105fc50 | 104 | if (err) { |
Christopher Haster |
32:2c5fc105fc50 | 105 | _iface->destroySocket(_socket); |
Christopher Haster |
32:2c5fc105fc50 | 106 | } |
Christopher Haster |
32:2c5fc105fc50 | 107 | |
Christopher Haster |
32:2c5fc105fc50 | 108 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 109 | } |
Christopher Haster |
32:2c5fc105fc50 | 110 | |
Christopher Haster |
32:2c5fc105fc50 | 111 | int32_t Socket::close() |
Christopher Haster |
32:2c5fc105fc50 | 112 | { |
Christopher Haster |
32:2c5fc105fc50 | 113 | if (!_socket) return 0; |
Christopher Haster |
32:2c5fc105fc50 | 114 | |
Christopher Haster |
32:2c5fc105fc50 | 115 | int32_t err = _socket->close(); |
Christopher Haster |
32:2c5fc105fc50 | 116 | |
Christopher Haster |
32:2c5fc105fc50 | 117 | if (!err) { |
Christopher Haster |
32:2c5fc105fc50 | 118 | _iface->destroySocket(_socket); |
Christopher Haster |
32:2c5fc105fc50 | 119 | } |
Christopher Haster |
32:2c5fc105fc50 | 120 | |
Christopher Haster |
32:2c5fc105fc50 | 121 | return err; |
Christopher Haster |
32:2c5fc105fc50 | 122 | } |
Christopher Haster |
32:2c5fc105fc50 | 123 | |
geky | 30:3cc78f5db99d | 124 | int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms) |
geky | 30:3cc78f5db99d | 125 | { |
Christopher Haster |
32:2c5fc105fc50 | 126 | if (!_socket) return -2; |
Christopher Haster |
32:2c5fc105fc50 | 127 | return _socket->send(data, len, timeout_ms); |
Christopher Haster |
25:ed7b2a52e8ac | 128 | } |
Christopher Haster |
25:ed7b2a52e8ac | 129 | |
geky | 30:3cc78f5db99d | 130 | int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms) |
geky | 30:3cc78f5db99d | 131 | { |
Christopher Haster |
32:2c5fc105fc50 | 132 | if (!_socket) return -2; |
Christopher Haster |
32:2c5fc105fc50 | 133 | return _socket->recv(data, len, timeout_ms); |
Christopher Haster |
25:ed7b2a52e8ac | 134 | } |
Christopher Haster |
25:ed7b2a52e8ac | 135 |