NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Socket.cpp@120:6eb542426f15, 2016-05-09 (annotated)
- Committer:
- Christopher Haster
- Date:
- Mon May 09 22:40:00 2016 -0500
- Revision:
- 120:6eb542426f15
- Parent:
- 118:96627c4b83d5
Fix port issue in SocketAddress constructor
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" |
Christopher Haster |
25:ed7b2a52e8ac | 18 | |
Christopher Haster |
90:0a988e4abb72 | 19 | Socket::Socket() |
Christopher Haster |
90:0a988e4abb72 | 20 | : _iface(0) |
geky | 30:3cc78f5db99d | 21 | , _socket(0) |
Christopher Haster |
115:950b19eb0f02 | 22 | , _timeout(-1) |
geky | 30:3cc78f5db99d | 23 | { |
Christopher Haster |
25:ed7b2a52e8ac | 24 | } |
Christopher Haster |
25:ed7b2a52e8ac | 25 | |
geky | 30:3cc78f5db99d | 26 | Socket::~Socket() |
geky | 30:3cc78f5db99d | 27 | { |
Christopher Haster |
43:09ea32f2eb54 | 28 | if (_socket) { |
Christopher Haster |
43:09ea32f2eb54 | 29 | close(); |
Christopher Haster |
43:09ea32f2eb54 | 30 | } |
Christopher Haster |
25:ed7b2a52e8ac | 31 | } |
Christopher Haster |
25:ed7b2a52e8ac | 32 | |
Christopher Haster |
105:2fd212f8da61 | 33 | int Socket::open(NetworkStack *iface, nsapi_protocol_t proto) |
Christopher Haster |
32:2c5fc105fc50 | 34 | { |
Christopher Haster |
90:0a988e4abb72 | 35 | _iface = iface; |
Christopher Haster |
41:3ec1c97e9bbf | 36 | |
Christopher Haster |
93:65a9f84862f0 | 37 | void *socket; |
Christopher Haster |
93:65a9f84862f0 | 38 | int err = _iface->socket_open(&socket, proto); |
Christopher Haster |
58:1caa187fa5af | 39 | if (err) { |
Christopher Haster |
58:1caa187fa5af | 40 | return err; |
Christopher Haster |
35:838393fbc2ca | 41 | } |
Christopher Haster |
32:2c5fc105fc50 | 42 | |
Christopher Haster |
93:65a9f84862f0 | 43 | _socket = socket; |
Christopher Haster |
92:dd5f19874adf | 44 | _iface->socket_attach(_socket, &Socket::thunk, this); |
Christopher Haster |
34:c17745683385 | 45 | |
Christopher Haster |
93:65a9f84862f0 | 46 | return 0; |
Christopher Haster |
32:2c5fc105fc50 | 47 | } |
Christopher Haster |
32:2c5fc105fc50 | 48 | |
Christopher Haster |
91:cad29ce6a01c | 49 | int Socket::close() |
Christopher Haster |
32:2c5fc105fc50 | 50 | { |
Christopher Haster |
43:09ea32f2eb54 | 51 | if (!_socket) { |
Christopher Haster |
43:09ea32f2eb54 | 52 | return 0; |
Christopher Haster |
43:09ea32f2eb54 | 53 | } |
geky | 65:ca337f9ebdab | 54 | |
geky | 108:2e5eccf30a84 | 55 | _iface->socket_attach(_socket, 0, 0); |
geky | 108:2e5eccf30a84 | 56 | |
geky | 108:2e5eccf30a84 | 57 | void *volatile socket = _socket; |
geky | 65:ca337f9ebdab | 58 | _socket = 0; |
Christopher Haster |
93:65a9f84862f0 | 59 | return _iface->socket_close(socket); |
Christopher Haster |
32:2c5fc105fc50 | 60 | } |
Christopher Haster |
32:2c5fc105fc50 | 61 | |
Christopher Haster |
98:0f614f1d0398 | 62 | int Socket::bind(uint16_t port) |
Christopher Haster |
98:0f614f1d0398 | 63 | { |
Christopher Haster |
98:0f614f1d0398 | 64 | SocketAddress addr(0, port); |
Christopher Haster |
98:0f614f1d0398 | 65 | return bind(addr); |
Christopher Haster |
98:0f614f1d0398 | 66 | } |
Christopher Haster |
98:0f614f1d0398 | 67 | |
Christopher Haster |
98:0f614f1d0398 | 68 | int Socket::bind(const char *address, uint16_t port) |
Christopher Haster |
98:0f614f1d0398 | 69 | { |
Christopher Haster |
98:0f614f1d0398 | 70 | SocketAddress addr(address, port); |
Christopher Haster |
98:0f614f1d0398 | 71 | return bind(addr); |
Christopher Haster |
98:0f614f1d0398 | 72 | } |
Christopher Haster |
98:0f614f1d0398 | 73 | |
Christopher Haster |
98:0f614f1d0398 | 74 | int Socket::bind(const SocketAddress &address) |
geky | 30:3cc78f5db99d | 75 | { |
Christopher Haster |
43:09ea32f2eb54 | 76 | if (!_socket) { |
Christopher Haster |
98:0f614f1d0398 | 77 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
43:09ea32f2eb54 | 78 | } |
Christopher Haster |
98:0f614f1d0398 | 79 | |
Christopher Haster |
98:0f614f1d0398 | 80 | return _iface->socket_bind(_socket, address); |
Christopher Haster |
98:0f614f1d0398 | 81 | } |
Christopher Haster |
98:0f614f1d0398 | 82 | |
Christopher Haster |
89:b1d417383c0d | 83 | void Socket::set_blocking(bool blocking) |
Christopher Haster |
89:b1d417383c0d | 84 | { |
Christopher Haster |
115:950b19eb0f02 | 85 | set_timeout(blocking ? -1 : 0); |
Christopher Haster |
89:b1d417383c0d | 86 | } |
Christopher Haster |
89:b1d417383c0d | 87 | |
Christopher Haster |
115:950b19eb0f02 | 88 | void Socket::set_timeout(int timeout) |
Christopher Haster |
89:b1d417383c0d | 89 | { |
Christopher Haster |
89:b1d417383c0d | 90 | _timeout = timeout; |
Christopher Haster |
25:ed7b2a52e8ac | 91 | } |
Christopher Haster |
25:ed7b2a52e8ac | 92 | |
Christopher Haster |
99:f51358e506c1 | 93 | int Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen) |
Christopher Haster |
89:b1d417383c0d | 94 | { |
Christopher Haster |
89:b1d417383c0d | 95 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 96 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
89:b1d417383c0d | 97 | } |
Christopher Haster |
89:b1d417383c0d | 98 | |
Christopher Haster |
99:f51358e506c1 | 99 | return _iface->setsockopt(_socket, level, optname, optval, optlen); |
Christopher Haster |
89:b1d417383c0d | 100 | } |
Christopher Haster |
89:b1d417383c0d | 101 | |
Christopher Haster |
99:f51358e506c1 | 102 | int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen) |
geky | 30:3cc78f5db99d | 103 | { |
Christopher Haster |
89:b1d417383c0d | 104 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 105 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
89:b1d417383c0d | 106 | } |
Christopher Haster |
89:b1d417383c0d | 107 | |
Christopher Haster |
99:f51358e506c1 | 108 | return _iface->getsockopt(_socket, level, optname, optval, optlen); |
Christopher Haster |
48:b3bbe28a7963 | 109 | |
Christopher Haster |
89:b1d417383c0d | 110 | } |
Christopher Haster |
89:b1d417383c0d | 111 | |
Christopher Haster |
118:96627c4b83d5 | 112 | void Socket::wakeup() |
Christopher Haster |
118:96627c4b83d5 | 113 | { |
Christopher Haster |
118:96627c4b83d5 | 114 | } |
Christopher Haster |
118:96627c4b83d5 | 115 | |
Christopher Haster |
92:dd5f19874adf | 116 | void Socket::thunk(void *data) |
Christopher Haster |
89:b1d417383c0d | 117 | { |
Christopher Haster |
92:dd5f19874adf | 118 | Socket *self = (Socket *)data; |
Christopher Haster |
92:dd5f19874adf | 119 | if (self->_callback) { |
Christopher Haster |
92:dd5f19874adf | 120 | self->_callback(); |
Christopher Haster |
48:b3bbe28a7963 | 121 | } |
Christopher Haster |
25:ed7b2a52e8ac | 122 | } |
Christopher Haster |
25:ed7b2a52e8ac | 123 | |
Christopher Haster |
92:dd5f19874adf | 124 | void Socket::attach(FunctionPointer callback) |
Christopher Haster |
58:1caa187fa5af | 125 | { |
Christopher Haster |
92:dd5f19874adf | 126 | _callback = callback; |
Christopher Haster |
58:1caa187fa5af | 127 | } |