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
Introduction
The demo is aimed to STM32 Nucleo board with WiFi and sensors expansions. The board is a "thing" for the AWS IoT service. It updates IoT service shadow with sensors data every second and checks subscription messages.
Hardware Configuration
- NUCLEO-F401RE - Nucleo Development Board
- X-NUCLEO-IDW01M1 - Wi-Fi expansion board for STM32 Nucleo
- X-NUCLEO-IKS01A1 - Motion MEMS and environmental sensor expansion board for STM32 Nucleo
Software Configuration
- Import this Project to mbed online compiler
- Find the next part of code in main.cpp file ...
WiFi network credential
#include "mbed.h" // WiFi network credential #define SSID "" // Network must be visible otherwise it can't connect #define PASSW "" #error "Wifi SSID & password empty"
- ... And set it to your Network Name and Password. Do not forget to remove "#error" pragma line.
Information
Nucleo WiFi module is not the same as your smartphone or laptope - it is based on demo board. To avoid connection problems:
- Place Nucleo as close to WiFi hot spot as possible. Or...
- Turn on mobile hot spot in your laptop as close to the device as possible.
- Make sure that hot spot permits 2.4 GHz band communications
- Setup BackEnd and store certificates using this backend setup instruction
- Find AWS_IOT_MQTT_HOST define and change it to HTTPS point mentioned in your AWS IoT thing properties named "interact"
#define AWS_IOT_MQTT_HOST "xxxxxxxxxx.iot.us-east-1.amazonaws.com" //Use your own host.
- Find the certificate defines clientCRT and clientKey in main.cpp file and change it to ones provided by Amazon.
/********************************************************************************************** *********************************************************************************************** Device Identity Certificates: Modify for your AWS IoT Thing *********************************************************************************************** ***********************************************************************************************/ /**************************************** (somecode)-certificate.pem.crt - Amazon signed PEM sertificate. *****************************************/ //This Client cert is example. Use own instead. const uint8_t clientCRT[] = "\ -----BEGIN CERTIFICATE-----\n\ MIIDBjCCAe6gAwIBAgIUVph856omeIxW3UPioq+UrX1DbwowDQYJKoZIhvcNAQEL\ BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g\ SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTE3MDUyNTExNTEy\ OVoXDTQ5MTIzMTIzNTk1OVowgZUxCzAJBgNVBAYTAkJZMQ4wDAYDVQQIDAVNaW5z\ azEOMAwGA1UEBwwFTWluc2sxFzAVBgNVBAoMDktsaWthLVRlY2ggTExDMRcwFQYD\ VQQLDA5LbGlrYS1UZWNoIExMQzEMMAoGA1UEAwwDUm5EMSYwJAYJKoZIhvcNAQkB\ FhdtdmF0YWxldUBrbGlrYS10ZWNoLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEH\ A0IABCJgOQJmoTBJVPfli9Hm/JVixaxkY5rtlgrYO3hSl633A2hg0P/ue0wXDbF3\ aQ0X57IRFE4k4FEbr3UXjT/IczKjYDBeMB8GA1UdIwQYMBaAFK3YzTUPlYB2Li75\ i/z8rEogr1d6MB0GA1UdDgQWBBT18HXBaXFJuAR/0SwegnxJ+pyJ6TAMBgNVHRMB\ Af8EAjAAMA4GA1UdDwEB/wQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAb0Ux1aH5\ RLxjrfGqXN6rPVqh8QQRS+AyBfzmaQN8HaPZMkX5WxXLvcn0A3uWlwQxPPkcZ4zf\ 51GHtFFQWB4YZ8dx8mUQ0v/j7onHjCJgZ8iDgwOyKMGtnsDZWCakQw+a6cj+NrMZ\ tzhjwCzEEP6ePcbXwErI5OOzLuWns2L/JEr2wWNkokgRuS8ewr/SQ9OLWIWa2rFM\ ahPNTb3y/qBeWdjeJmhI+TOxdqIpsF8roWP25zwo/zkzCHCjXFBrL+0CA4MpxIl9\ x02i7aAhlJ6ys80lDxdeWeeQJXRKkGknP8mcmKn3iEqqJ5s1dQePj2b5d3ldatya\ wsxQBqqZXzIWEw==\ \n\ -----END CERTIFICATE-----\n"; /********************************************************************************************** *********************************************************************************************** Private Key: Modify for your AWS IoT Thing *********************************************************************************************** ***********************************************************************************************/ /********************************************************************8**************************************** nucleo.key.pem - client key generated according to readme. **************************************************************************************************************/ //This Client Key is example. Use own instead. const uint8_t clientKey[] ="\ -----BEGIN EC PARAMETERS-----\n\ BggqhkjOPQMBBw==\ -----END EC PARAMETERS-----\n\ -----BEGIN EC PRIVATE KEY-----\n\ MHcCAQEEIHPRfWSC8/k/BsqDWKuP15dXsI9fGwpkTIsLZe6mIrAAoAoGCCqGSM49\ AwEHoUQDQgAEImA5AmahMElU9+WL0eb8lWLFrGRjmu2WCtg7eFKXrfcDaGDQ/+57\ TBcNsXdpDRfnshEUTiTgURuvdReNP8hzMg==\ -----END EC PRIVATE KEY-----\n";
Build and Check
- Plugin your board to USB of your PC. USB Disk Drive and USB COM Port should appear in your system.
- Open any Serial Console, connect it to your USB Serial Port and setup speed equal to 115200.
- Compile this Project and save .bin file to USB Disk Drive
- After board reset you should see next log in serial console:
X-NUCLEO-IDW01M1 mbed Application connecting to AP LOG: int main() L#361 Connected to WiFI. LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#186 ===================================== LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#187 Connecting WiFi. LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#188 Nucleo IP ADDRESS: X.X.X.X LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#189 Nucleo MAC ADDRESS: 00:11:22:33:44:55 LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#190 Server Hostname: xxxxxxxx.iot.us-east-1.amazonaws.com port: 8883 LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#191 Client ID: Nucleo LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#194 ===================================== LOG: int MQTTSocket::getNTPtime(int) L#58 Success receiving time from ntp server. Tick from 1 Jan 1970 is equal to 1505399292. --->TCP Connected --->MQTT Connected --->>>MQTT subscribed to: Nucleo/test Length - 245, Publishing {"state": {"reported": {"temperature": 23.690001, "humidity": 98.190002, "pressure": 982.869141, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.420000, -2.660000, 1.750000], "magnetometer": [-3.600000, -7.100000, 53.300000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.660000, "humidity": 98.010002, "pressure": 982.770264, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.770000, -2.310000, 1.470000], "magnetometer": [-3.100000, -8.300000, 54.200000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.670000, "humidity": 98.129997, "pressure": 982.724121, "accelerometer": [-0.008000, 0.029000, 0.971000], "gyroscope": [0.630000, -2.380000, 1.400000], "magnetometer": [-3.100000, -7.900000, 53.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.690001, "humidity": 98.019997, "pressure": 982.840088, "accelerometer": [-0.009000, 0.030000, 0.972000], "gyroscope": [0.700000, -2.450000, 1.540000], "magnetometer": [-3.700000, -7.900000, 53.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.709999, "humidity": 98.040001, "pressure": 982.828613, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.630000, -2.520000, 1.470000], "magnetometer": [-2.900000, -7.400000, 52.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.719999, "humidity": 97.860001, "pressure": 982.917236, "accelerometer": [-0.026000, 0.103000, 0.891000], "gyroscope": [1.050000, -2.310000, 1.260000], "magnetometer": [-3.300000, -7.100000, 53.500000]}}}
Information
Device connection state might be checked by Green Led on the board. Green light means that device is connected and transferring data to cloud.
- Configure and start your dashboard using instruction and corresponding sources from github
- Use Blue button to set up markers to charts.
- Use AWS IoT console MQTT Client to test device subscription to "Nucleo/test". Just publish any message to this topic and serial port output.
- PROFIT!
NetworkSocketAPI/NetworkStack.h@1:042ca9148926, 2017-10-19 (annotated)
- Committer:
- PavelSavyhin
- Date:
- Thu Oct 19 11:36:41 2017 +0000
- Revision:
- 1:042ca9148926
- Parent:
- 0:4cdaf9b1e7d0
Connection times are optimized and logging is extended.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PavelSavyhin | 0:4cdaf9b1e7d0 | 1 | /* NetworkStack |
PavelSavyhin | 0:4cdaf9b1e7d0 | 2 | * Copyright (c) 2015 ARM Limited |
PavelSavyhin | 0:4cdaf9b1e7d0 | 3 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 5 | * you may not use this file except in compliance with the License. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 6 | * You may obtain a copy of the License at |
PavelSavyhin | 0:4cdaf9b1e7d0 | 7 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 9 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 10 | * Unless required by applicable law or agreed to in writing, software |
PavelSavyhin | 0:4cdaf9b1e7d0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 13 | * See the License for the specific language governing permissions and |
PavelSavyhin | 0:4cdaf9b1e7d0 | 14 | * limitations under the License. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 15 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 16 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 17 | #ifndef NETWORK_INTERFACE_H |
PavelSavyhin | 0:4cdaf9b1e7d0 | 18 | #define NETWORK_INTERFACE_H |
PavelSavyhin | 0:4cdaf9b1e7d0 | 19 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 20 | #include "mbed.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 21 | #include "SocketAddress.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 22 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 23 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 24 | /** Enum of standardized error codes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 25 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 26 | * Valid error codes have negative values and may |
PavelSavyhin | 0:4cdaf9b1e7d0 | 27 | * be returned by any network operation. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 28 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 29 | * @enum nsapi_error_t |
PavelSavyhin | 0:4cdaf9b1e7d0 | 30 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 31 | enum nsapi_error_t { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 32 | NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 33 | NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 34 | NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 35 | NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 36 | NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 37 | NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 38 | NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 39 | NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 40 | NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 41 | NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point faield */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 42 | NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network procesor */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 43 | }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 44 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 45 | /** Enum of socket protocols |
PavelSavyhin | 0:4cdaf9b1e7d0 | 46 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 47 | * The socket protocol specifies a particular protocol to |
PavelSavyhin | 0:4cdaf9b1e7d0 | 48 | * be used with a newly created socket. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 49 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 50 | * @enum nsapi_protocol_t |
PavelSavyhin | 0:4cdaf9b1e7d0 | 51 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 52 | enum nsapi_protocol_t { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 53 | NSAPI_TCP, /*!< Socket is of TCP type */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 54 | NSAPI_UDP, /*!< Socket is of UDP type */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 55 | NSAPI_TLS /*!< Socket is of TCP Secure type */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 56 | }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 57 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 58 | /* Enum of standardized stack option levels |
PavelSavyhin | 0:4cdaf9b1e7d0 | 59 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 60 | * @enum nsapi_level_t |
PavelSavyhin | 0:4cdaf9b1e7d0 | 61 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 62 | enum nsapi_level_t { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 63 | NSAPI_STACK, /*!< Stack option level */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 64 | NSAPI_SOCKET, /*!< Socket option level */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 65 | }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 66 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 67 | /* Enum of standardized stack options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 68 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 69 | * These options may not be supported on all stacks, in which |
PavelSavyhin | 0:4cdaf9b1e7d0 | 70 | * case NSAPI_ERROR_UNSUPPORTED may be returned from setsockopt. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 71 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 72 | * @enum nsapi_option_t |
PavelSavyhin | 0:4cdaf9b1e7d0 | 73 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 74 | enum nsapi_option_t { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 75 | NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 76 | NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 77 | NSAPI_LINGER, /*!< Keeps close from returning until queues empty */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 78 | NSAPI_SNDBUF, /*!< Sets send buffer size */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 79 | NSAPI_RCVBUF, /*!< Sets recv buffer size */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 80 | }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 81 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 82 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 83 | /** NetworkStack class |
PavelSavyhin | 0:4cdaf9b1e7d0 | 84 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 85 | * Common interface that is shared between hardware that |
PavelSavyhin | 0:4cdaf9b1e7d0 | 86 | * can connect to a network over IP. By implementing the |
PavelSavyhin | 0:4cdaf9b1e7d0 | 87 | * NetworkStack, a network stack can be used as a target |
PavelSavyhin | 0:4cdaf9b1e7d0 | 88 | * for instantiating network sockets. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 89 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 90 | class NetworkStack |
PavelSavyhin | 0:4cdaf9b1e7d0 | 91 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 92 | public: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 93 | virtual ~NetworkStack() {}; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 94 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 95 | /** Get the local IP address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 96 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 97 | * @return Null-terminated representation of the local IP address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 98 | * or null if not yet connected |
PavelSavyhin | 0:4cdaf9b1e7d0 | 99 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 100 | virtual const char *get_ip_address() = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 101 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 102 | /** Translates a hostname to an IP address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 103 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 104 | * The hostname may be either a domain name or an IP address. If the |
PavelSavyhin | 0:4cdaf9b1e7d0 | 105 | * hostname is an IP address, no network transactions will be performed. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 106 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 107 | * If no stack-specific DNS resolution is provided, the hostname |
PavelSavyhin | 0:4cdaf9b1e7d0 | 108 | * will be resolve using a UDP socket on the stack. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 109 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 110 | * @param address Destination for the host SocketAddress |
PavelSavyhin | 0:4cdaf9b1e7d0 | 111 | * @param host Hostname to resolve |
PavelSavyhin | 0:4cdaf9b1e7d0 | 112 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 113 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 114 | virtual int gethostbyname(SocketAddress *address, const char *host); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 115 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 116 | /* Set stack-specific stack options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 117 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 118 | * The setstackopt allow an application to pass stack-specific hints |
PavelSavyhin | 0:4cdaf9b1e7d0 | 119 | * to the underlying stack. For unsupported options, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 120 | * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 121 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 122 | * @param level Stack-specific protocol level |
PavelSavyhin | 0:4cdaf9b1e7d0 | 123 | * @param optname Stack-specific option identifier |
PavelSavyhin | 0:4cdaf9b1e7d0 | 124 | * @param optval Option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 125 | * @param optlen Length of the option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 126 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 127 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 128 | virtual int setstackopt(int level, int optname, const void *optval, unsigned optlen); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 129 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 130 | /* Get stack-specific stack options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 131 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 132 | * The getstackopt allow an application to retrieve stack-specific hints |
PavelSavyhin | 0:4cdaf9b1e7d0 | 133 | * from the underlying stack. For unsupported options, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 134 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 135 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 136 | * @param level Stack-specific protocol level |
PavelSavyhin | 0:4cdaf9b1e7d0 | 137 | * @param optname Stack-specific option identifier |
PavelSavyhin | 0:4cdaf9b1e7d0 | 138 | * @param optval Destination for option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 139 | * @param optlen Length of the option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 140 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 141 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 142 | virtual int getstackopt(int level, int optname, void *optval, unsigned *optlen); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 143 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 144 | protected: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 145 | friend class Socket; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 146 | friend class UDPSocket; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 147 | friend class TCPSocket; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 148 | friend class TCPServer; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 149 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 150 | /** Opens a socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 151 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 152 | * Creates a network socket and stores it in the specified handle. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 153 | * The handle must be passed to following calls on the socket. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 154 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 155 | * A stack may have a finite number of sockets, in this case |
PavelSavyhin | 0:4cdaf9b1e7d0 | 156 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 157 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 158 | * @param handle Destination for the handle to a newly created socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 159 | * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP |
PavelSavyhin | 0:4cdaf9b1e7d0 | 160 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 161 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 162 | virtual int socket_open(void **handle, nsapi_protocol_t proto) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 163 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 164 | /** Close the socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 165 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 166 | * Closes any open connection and deallocates any memory associated |
PavelSavyhin | 0:4cdaf9b1e7d0 | 167 | * with the socket. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 168 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 169 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 170 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 171 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 172 | virtual int socket_close(void *handle) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 173 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 174 | /** Bind a specific address to a socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 175 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 176 | * Binding a socket specifies the address and port on which to recieve |
PavelSavyhin | 0:4cdaf9b1e7d0 | 177 | * data. If the IP address is zeroed, only the port is bound. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 178 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 179 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 180 | * @param address Local address to bind |
PavelSavyhin | 0:4cdaf9b1e7d0 | 181 | * @return 0 on success, negative error code on failure. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 182 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 183 | virtual int socket_bind(void *handle, const SocketAddress &address) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 184 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 185 | /** Listen for connections on a TCP socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 186 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 187 | * Marks the socket as a passive socket that can be used to accept |
PavelSavyhin | 0:4cdaf9b1e7d0 | 188 | * incoming connections. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 189 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 190 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 191 | * @param backlog Number of pending connections that can be queued |
PavelSavyhin | 0:4cdaf9b1e7d0 | 192 | * simultaneously |
PavelSavyhin | 0:4cdaf9b1e7d0 | 193 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 194 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 195 | virtual int socket_listen(void *handle, int backlog) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 196 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 197 | /** Connects TCP socket to a remote host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 198 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 199 | * Initiates a connection to a remote server specified by the |
PavelSavyhin | 0:4cdaf9b1e7d0 | 200 | * indicated address. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 201 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 202 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 203 | * @param address The SocketAddress of the remote host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 204 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 205 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 206 | virtual int socket_connect(void *handle, const SocketAddress &address) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 207 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 208 | /** Accepts a connection on a TCP socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 209 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 210 | * The server socket must be bound and set to listen for connections. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 211 | * On a new connection, creates a network socket and stores it in the |
PavelSavyhin | 0:4cdaf9b1e7d0 | 212 | * specified handle. The handle must be passed to following calls on |
PavelSavyhin | 0:4cdaf9b1e7d0 | 213 | * the socket. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 214 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 215 | * A stack may have a finite number of sockets, in this case |
PavelSavyhin | 0:4cdaf9b1e7d0 | 216 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 217 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 218 | * This call is non-blocking. If accept would block, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 219 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 220 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 221 | * @param handle Destination for a handle to the newly created sockey |
PavelSavyhin | 0:4cdaf9b1e7d0 | 222 | * @param server Socket handle to server to accept from |
PavelSavyhin | 0:4cdaf9b1e7d0 | 223 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 224 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 225 | virtual int socket_accept(void **handle, void *server) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 226 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 227 | /** Send data over a TCP socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 228 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 229 | * The socket must be connected to a remote host. Returns the number of |
PavelSavyhin | 0:4cdaf9b1e7d0 | 230 | * bytes sent from the buffer. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 231 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 232 | * This call is non-blocking. If send would block, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 233 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 234 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 235 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 236 | * @param data Buffer of data to send to the host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 237 | * @param size Size of the buffer in bytes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 238 | * @return Number of sent bytes on success, negative error |
PavelSavyhin | 0:4cdaf9b1e7d0 | 239 | * code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 240 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 241 | virtual int socket_send(void *handle, const void *data, unsigned size) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 242 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 243 | /** Receive data over a TCP socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 244 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 245 | * The socket must be connected to a remote host. Returns the number of |
PavelSavyhin | 0:4cdaf9b1e7d0 | 246 | * bytes received into the buffer. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 247 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 248 | * This call is non-blocking. If recv would block, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 249 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 250 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 251 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 252 | * @param data Destination buffer for data received from the host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 253 | * @param size Size of the buffer in bytes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 254 | * @return Number of received bytes on success, negative error |
PavelSavyhin | 0:4cdaf9b1e7d0 | 255 | * code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 256 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 257 | virtual int socket_recv(void *handle, void *data, unsigned size) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 258 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 259 | /** Send a packet over a UDP socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 260 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 261 | * Sends data to the specified address. Returns the number of bytes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 262 | * sent from the buffer. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 263 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 264 | * This call is non-blocking. If sendto would block, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 265 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 266 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 267 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 268 | * @param address The SocketAddress of the remote host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 269 | * @param data Buffer of data to send to the host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 270 | * @param size Size of the buffer in bytes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 271 | * @return Number of sent bytes on success, negative error |
PavelSavyhin | 0:4cdaf9b1e7d0 | 272 | * code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 273 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 274 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 275 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 276 | /** Receive a packet over a UDP socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 277 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 278 | * Receives data and stores the source address in address if address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 279 | * is not NULL. Returns the number of bytes received into the buffer. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 280 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 281 | * This call is non-blocking. If recvfrom would block, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 282 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 283 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 284 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 285 | * @param address Destination for the source address or NULL |
PavelSavyhin | 0:4cdaf9b1e7d0 | 286 | * @param data Destination buffer for data received from the host |
PavelSavyhin | 0:4cdaf9b1e7d0 | 287 | * @param size Size of the buffer in bytes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 288 | * @return Number of received bytes on success, negative error |
PavelSavyhin | 0:4cdaf9b1e7d0 | 289 | * code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 290 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 291 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 292 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 293 | /** Register a callback on state change of the socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 294 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 295 | * The specified callback will be called on state changes such as when |
PavelSavyhin | 0:4cdaf9b1e7d0 | 296 | * the socket can recv/send/accept successfully and on when an error |
PavelSavyhin | 0:4cdaf9b1e7d0 | 297 | * occurs. The callback may also be called spuriously without reason. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 298 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 299 | * The callback may be called in an interrupt context and should not |
PavelSavyhin | 0:4cdaf9b1e7d0 | 300 | * perform expensive operations such as recv/send calls. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 301 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 302 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 303 | * @param callback Function to call on state change |
PavelSavyhin | 0:4cdaf9b1e7d0 | 304 | * @param data Argument to pass to callback |
PavelSavyhin | 0:4cdaf9b1e7d0 | 305 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 306 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data) = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 307 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 308 | /* Set stack-specific socket options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 309 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 310 | * The setsockopt allow an application to pass stack-specific hints |
PavelSavyhin | 0:4cdaf9b1e7d0 | 311 | * to the underlying stack. For unsupported options, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 312 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 313 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 314 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 315 | * @param level Stack-specific protocol level |
PavelSavyhin | 0:4cdaf9b1e7d0 | 316 | * @param optname Stack-specific option identifier |
PavelSavyhin | 0:4cdaf9b1e7d0 | 317 | * @param optval Option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 318 | * @param optlen Length of the option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 319 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 320 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 321 | virtual int setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 322 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 323 | /* Get stack-specific socket options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 324 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 325 | * The getstackopt allow an application to retrieve stack-specific hints |
PavelSavyhin | 0:4cdaf9b1e7d0 | 326 | * from the underlying stack. For unsupported options, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 327 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 328 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 329 | * @param handle Socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 330 | * @param level Stack-specific protocol level |
PavelSavyhin | 0:4cdaf9b1e7d0 | 331 | * @param optname Stack-specific option identifier |
PavelSavyhin | 0:4cdaf9b1e7d0 | 332 | * @param optval Destination for option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 333 | * @param optlen Length of the option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 334 | * @return 0 on success, negative error code on failure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 335 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 336 | virtual int getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 337 | }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 338 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 339 | #endif |