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