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
TCPSocket.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 "TCPSocket.h" 00018 #include "Timer.h" 00019 00020 TCPSocket::TCPSocket(bool isTLS) : _isTLS(isTLS) 00021 { 00022 } 00023 00024 TCPSocket::TCPSocket(NetworkStack *iface) 00025 { 00026 open(iface); 00027 } 00028 00029 int TCPSocket::open(NetworkStack *iface) 00030 { 00031 return Socket::open(iface, _isTLS ? NSAPI_TLS : NSAPI_TCP); 00032 } 00033 00034 int TCPSocket::connect(const SocketAddress &addr) 00035 { 00036 if (!_socket) { 00037 return NSAPI_ERROR_NO_SOCKET; 00038 } 00039 00040 return _iface->socket_connect(_socket, addr); 00041 } 00042 00043 00044 int TCPSocket::connect(const char *host, uint16_t port) 00045 { 00046 SocketAddress addr(_iface, host, port); 00047 if (!addr) { 00048 return NSAPI_ERROR_DNS_FAILURE; 00049 } 00050 00051 return connect(addr); 00052 } 00053 00054 int TCPSocket::send(const void *data, unsigned size) 00055 { 00056 mbed::Timer timer; 00057 timer.start(); 00058 mbed::Timeout timeout; 00059 if (_timeout >= 0) { 00060 timeout.attach_us(&Socket::wakeup, _timeout * 1000); 00061 } 00062 00063 while (true) { 00064 if (!_socket) { 00065 return NSAPI_ERROR_NO_SOCKET; 00066 } 00067 00068 int sent = _iface->socket_send(_socket, data, size); 00069 if (sent != NSAPI_ERROR_WOULD_BLOCK 00070 || (_timeout >= 0 && timer.read_ms() >= _timeout)) { 00071 return sent; 00072 } 00073 00074 __WFI(); 00075 } 00076 } 00077 00078 int TCPSocket::recv(void *data, unsigned size) 00079 { 00080 mbed::Timer timer; 00081 timer.start(); 00082 mbed::Timeout timeout; 00083 if (_timeout >= 0) { 00084 timeout.attach_us(&Socket::wakeup, _timeout * 1000); 00085 } 00086 00087 while (true) { 00088 if (!_socket) { 00089 return NSAPI_ERROR_NO_SOCKET; 00090 } 00091 00092 int recv = _iface->socket_recv(_socket, data, size); 00093 if (recv != NSAPI_ERROR_WOULD_BLOCK 00094 || (_timeout >= 0 && timer.read_ms() >= _timeout)) { 00095 return recv; 00096 } 00097 00098 __WFI(); 00099 } 00100 }
Generated on Wed Jul 13 2022 20:28:49 by 1.7.2