NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Socket.cpp@104:d28d8b508e7c, 2016-04-19 (annotated)
- Committer:
- Christopher Haster
- Date:
- Tue Apr 19 18:26:21 2016 -0500
- Revision:
- 104:d28d8b508e7c
- Parent:
- 99:f51358e506c1
- Child:
- 105:2fd212f8da61
Revised documentation for Interface classes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
89:b1d417383c0d | 1 | /* Socket |
Christopher Haster |
89:b1d417383c0d | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
89:b1d417383c0d | 3 | * |
Christopher Haster |
89:b1d417383c0d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
89:b1d417383c0d | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
89:b1d417383c0d | 6 | * You may obtain a copy of the License at |
Christopher Haster |
89:b1d417383c0d | 7 | * |
Christopher Haster |
89:b1d417383c0d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
89:b1d417383c0d | 9 | * |
Christopher Haster |
89:b1d417383c0d | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
89:b1d417383c0d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
89:b1d417383c0d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
89:b1d417383c0d | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
89:b1d417383c0d | 14 | * limitations under the License. |
Christopher Haster |
89:b1d417383c0d | 15 | */ |
Christopher Haster |
89:b1d417383c0d | 16 | |
Christopher Haster |
89:b1d417383c0d | 17 | #include "Socket.h" |
Christopher Haster |
89:b1d417383c0d | 18 | |
Christopher Haster |
90:0a988e4abb72 | 19 | Socket::Socket() |
Christopher Haster |
90:0a988e4abb72 | 20 | : _iface(0) |
Christopher Haster |
90:0a988e4abb72 | 21 | , _socket(0) |
Christopher Haster |
89:b1d417383c0d | 22 | , _blocking(true) |
Christopher Haster |
89:b1d417383c0d | 23 | , _timeout(0) |
Christopher Haster |
89:b1d417383c0d | 24 | { |
Christopher Haster |
89:b1d417383c0d | 25 | } |
Christopher Haster |
89:b1d417383c0d | 26 | |
Christopher Haster |
89:b1d417383c0d | 27 | Socket::~Socket() |
Christopher Haster |
89:b1d417383c0d | 28 | { |
Christopher Haster |
89:b1d417383c0d | 29 | if (_socket) { |
Christopher Haster |
91:cad29ce6a01c | 30 | close(); |
Christopher Haster |
89:b1d417383c0d | 31 | } |
Christopher Haster |
89:b1d417383c0d | 32 | } |
Christopher Haster |
89:b1d417383c0d | 33 | |
Christopher Haster |
90:0a988e4abb72 | 34 | int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto) |
Christopher Haster |
90:0a988e4abb72 | 35 | { |
Christopher Haster |
90:0a988e4abb72 | 36 | _iface = iface; |
Christopher Haster |
93:65a9f84862f0 | 37 | |
Christopher Haster |
93:65a9f84862f0 | 38 | void *socket; |
Christopher Haster |
93:65a9f84862f0 | 39 | int err = _iface->socket_open(&socket, proto); |
Christopher Haster |
93:65a9f84862f0 | 40 | if (err) { |
Christopher Haster |
93:65a9f84862f0 | 41 | return err; |
Christopher Haster |
93:65a9f84862f0 | 42 | } |
Christopher Haster |
93:65a9f84862f0 | 43 | |
Christopher Haster |
93:65a9f84862f0 | 44 | _socket = socket; |
Christopher Haster |
92:dd5f19874adf | 45 | _iface->socket_attach(_socket, &Socket::thunk, this); |
Christopher Haster |
93:65a9f84862f0 | 46 | |
Christopher Haster |
93:65a9f84862f0 | 47 | return 0; |
Christopher Haster |
90:0a988e4abb72 | 48 | } |
Christopher Haster |
90:0a988e4abb72 | 49 | |
Christopher Haster |
91:cad29ce6a01c | 50 | int Socket::close() |
Christopher Haster |
90:0a988e4abb72 | 51 | { |
Christopher Haster |
90:0a988e4abb72 | 52 | if (!_socket) { |
Christopher Haster |
90:0a988e4abb72 | 53 | return 0; |
Christopher Haster |
90:0a988e4abb72 | 54 | } |
Christopher Haster |
90:0a988e4abb72 | 55 | |
Christopher Haster |
93:65a9f84862f0 | 56 | void *socket = _socket; |
Christopher Haster |
93:65a9f84862f0 | 57 | _socket = 0; |
Christopher Haster |
93:65a9f84862f0 | 58 | return _iface->socket_close(socket); |
Christopher Haster |
90:0a988e4abb72 | 59 | } |
Christopher Haster |
90:0a988e4abb72 | 60 | |
Christopher Haster |
98:0f614f1d0398 | 61 | int Socket::bind(uint16_t port) |
Christopher Haster |
98:0f614f1d0398 | 62 | { |
Christopher Haster |
98:0f614f1d0398 | 63 | SocketAddress addr(0, port); |
Christopher Haster |
98:0f614f1d0398 | 64 | return bind(addr); |
Christopher Haster |
98:0f614f1d0398 | 65 | } |
Christopher Haster |
98:0f614f1d0398 | 66 | |
Christopher Haster |
98:0f614f1d0398 | 67 | int Socket::bind(const char *address, uint16_t port) |
Christopher Haster |
98:0f614f1d0398 | 68 | { |
Christopher Haster |
98:0f614f1d0398 | 69 | SocketAddress addr(address, port); |
Christopher Haster |
98:0f614f1d0398 | 70 | return bind(addr); |
Christopher Haster |
98:0f614f1d0398 | 71 | } |
Christopher Haster |
98:0f614f1d0398 | 72 | |
Christopher Haster |
98:0f614f1d0398 | 73 | int Socket::bind(const SocketAddress &address) |
Christopher Haster |
98:0f614f1d0398 | 74 | { |
Christopher Haster |
98:0f614f1d0398 | 75 | if (!_socket) { |
Christopher Haster |
98:0f614f1d0398 | 76 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
98:0f614f1d0398 | 77 | } |
Christopher Haster |
98:0f614f1d0398 | 78 | |
Christopher Haster |
98:0f614f1d0398 | 79 | return _iface->socket_bind(_socket, address); |
Christopher Haster |
98:0f614f1d0398 | 80 | } |
Christopher Haster |
98:0f614f1d0398 | 81 | |
Christopher Haster |
89:b1d417383c0d | 82 | void Socket::set_blocking(bool blocking) |
Christopher Haster |
89:b1d417383c0d | 83 | { |
Christopher Haster |
89:b1d417383c0d | 84 | _blocking = blocking; |
Christopher Haster |
89:b1d417383c0d | 85 | } |
Christopher Haster |
89:b1d417383c0d | 86 | |
Christopher Haster |
89:b1d417383c0d | 87 | void Socket::set_timeout(unsigned timeout) |
Christopher Haster |
89:b1d417383c0d | 88 | { |
Christopher Haster |
89:b1d417383c0d | 89 | _timeout = timeout; |
Christopher Haster |
89:b1d417383c0d | 90 | } |
Christopher Haster |
89:b1d417383c0d | 91 | |
Christopher Haster |
99:f51358e506c1 | 92 | int Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen) |
Christopher Haster |
89:b1d417383c0d | 93 | { |
Christopher Haster |
89:b1d417383c0d | 94 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 95 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
89:b1d417383c0d | 96 | } |
Christopher Haster |
89:b1d417383c0d | 97 | |
Christopher Haster |
99:f51358e506c1 | 98 | return _iface->setsockopt(_socket, level, optname, optval, optlen); |
Christopher Haster |
89:b1d417383c0d | 99 | } |
Christopher Haster |
89:b1d417383c0d | 100 | |
Christopher Haster |
99:f51358e506c1 | 101 | int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen) |
Christopher Haster |
89:b1d417383c0d | 102 | { |
Christopher Haster |
89:b1d417383c0d | 103 | if (!_socket) { |
Christopher Haster |
89:b1d417383c0d | 104 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
89:b1d417383c0d | 105 | } |
Christopher Haster |
89:b1d417383c0d | 106 | |
Christopher Haster |
99:f51358e506c1 | 107 | return _iface->getsockopt(_socket, level, optname, optval, optlen); |
Christopher Haster |
99:f51358e506c1 | 108 | |
Christopher Haster |
89:b1d417383c0d | 109 | } |
Christopher Haster |
89:b1d417383c0d | 110 | |
Christopher Haster |
92:dd5f19874adf | 111 | void Socket::thunk(void *data) |
Christopher Haster |
89:b1d417383c0d | 112 | { |
Christopher Haster |
92:dd5f19874adf | 113 | Socket *self = (Socket *)data; |
Christopher Haster |
92:dd5f19874adf | 114 | if (self->_callback) { |
Christopher Haster |
92:dd5f19874adf | 115 | self->_callback(); |
Christopher Haster |
92:dd5f19874adf | 116 | } |
Christopher Haster |
89:b1d417383c0d | 117 | } |
Christopher Haster |
92:dd5f19874adf | 118 | |
Christopher Haster |
92:dd5f19874adf | 119 | void Socket::attach(FunctionPointer callback) |
Christopher Haster |
92:dd5f19874adf | 120 | { |
Christopher Haster |
92:dd5f19874adf | 121 | _callback = callback; |
Christopher Haster |
92:dd5f19874adf | 122 | } |