Example of AWS IoT connection and Web Dashboard thru STM32 Nucleo evaluation board and mbed OS.

Dependencies:   X_NUCLEO_IKS01A1 mbed FP MQTTPacket DnsQuery ATParser

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.cpp Source File

Socket.cpp

00001 /* Socket
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "Socket.h"
00018 
00019 Socket::Socket()
00020     : _iface(0)
00021     , _socket(0)
00022     , _timeout(-1)
00023 {
00024 }
00025 
00026 Socket::~Socket()
00027 {
00028     if (_socket) {
00029         close();
00030     }
00031 }
00032 
00033 int Socket::open(NetworkStack *iface, nsapi_protocol_t proto)
00034 {
00035     _iface = iface;
00036 
00037     void *socket;
00038     int err = _iface->socket_open(&socket, proto);
00039     if (err) {
00040         return err;
00041     }
00042 
00043     _socket = socket;
00044     _iface->socket_attach(_socket, &Socket::thunk, this);
00045 
00046     return 0;
00047 }
00048 
00049 int Socket::close()
00050 {
00051     if (!_socket) {
00052         return 0;
00053     }
00054     
00055     _iface->socket_attach(_socket, 0, 0);
00056     
00057     void *volatile socket = _socket;
00058     _socket = 0;
00059     return _iface->socket_close(socket);
00060 }
00061 
00062 int Socket::bind(uint16_t port)
00063 {
00064     SocketAddress addr(0, port);
00065     return bind(addr);
00066 }
00067 
00068 int Socket::bind(const char *address, uint16_t port)
00069 {
00070     SocketAddress addr(address, port);
00071     return bind(addr);
00072 }
00073 
00074 int Socket::bind(const SocketAddress &address)
00075 {
00076     if (!_socket) {
00077         return NSAPI_ERROR_NO_SOCKET;
00078     }
00079 
00080     return _iface->socket_bind(_socket, address);
00081 }
00082 
00083 void Socket::set_blocking(bool blocking)
00084 {
00085     set_timeout(blocking ? -1 : 0);
00086 }
00087 
00088 void Socket::set_timeout(int timeout)
00089 {
00090     _timeout = timeout;
00091 }
00092 
00093 int Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
00094 {
00095     if (!_socket) {
00096         return NSAPI_ERROR_NO_SOCKET;
00097     }
00098 
00099     return _iface->setsockopt(_socket, level, optname, optval, optlen);
00100 }
00101 
00102 int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
00103 {
00104     if (!_socket) {
00105         return NSAPI_ERROR_NO_SOCKET;
00106     }
00107 
00108     return _iface->getsockopt(_socket, level, optname, optval, optlen);
00109 
00110 }
00111 
00112 void Socket::wakeup()
00113 {
00114 }
00115 
00116 void Socket::thunk(void *data)
00117 {
00118     Socket *self = (Socket *)data;
00119     if (self->_callback) {
00120         self->_callback();
00121     }
00122 }
00123 
00124 void Socket::attach(FunctionPointer callback)
00125 {
00126     _callback = callback;
00127 }