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!
X_NUCLEO_IDW01M1v2/SpwfInterface.cpp@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 | /* mbed Microcontroller Library |
PavelSavyhin | 0:4cdaf9b1e7d0 | 2 | * Copyright (c) 20015 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 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 18 | ****************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 19 | * @file SpwfInterface.cpp |
PavelSavyhin | 0:4cdaf9b1e7d0 | 20 | * @author STMicroelectronics |
PavelSavyhin | 0:4cdaf9b1e7d0 | 21 | * @brief Implementation of the NetworkStack for the SPWF Device |
PavelSavyhin | 0:4cdaf9b1e7d0 | 22 | ****************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 23 | * @copy |
PavelSavyhin | 0:4cdaf9b1e7d0 | 24 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
PavelSavyhin | 0:4cdaf9b1e7d0 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
PavelSavyhin | 0:4cdaf9b1e7d0 | 27 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
PavelSavyhin | 0:4cdaf9b1e7d0 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
PavelSavyhin | 0:4cdaf9b1e7d0 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
PavelSavyhin | 0:4cdaf9b1e7d0 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 31 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 32 | * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2> |
PavelSavyhin | 0:4cdaf9b1e7d0 | 33 | ****************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 34 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 35 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 36 | #include "SpwfInterface.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 37 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 38 | // Various timeouts for different SPWF operations |
PavelSavyhin | 0:4cdaf9b1e7d0 | 39 | #define SPWF_CONNECT_TIMEOUT 20000 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 40 | #define SPWF_SEND_TIMEOUT 500 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 41 | #define SPWF_RECV_TIMEOUT 500 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 42 | #define SPWF_MISC_TIMEOUT 15000 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 43 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 44 | /** spwf_socket class |
PavelSavyhin | 0:4cdaf9b1e7d0 | 45 | * Implementation of SPWF socket structure |
PavelSavyhin | 0:4cdaf9b1e7d0 | 46 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 47 | struct spwf_socket { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 48 | int id; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 49 | int server_port; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 50 | nsapi_protocol_t proto; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 51 | bool connected; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 52 | }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 53 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 54 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 55 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 56 | * @brief SpwfSAInterface constructor |
PavelSavyhin | 0:4cdaf9b1e7d0 | 57 | * @param tx: Pin USART TX |
PavelSavyhin | 0:4cdaf9b1e7d0 | 58 | * rx: Pin USART RX |
PavelSavyhin | 0:4cdaf9b1e7d0 | 59 | * rst: reset pin for Spwf module |
PavelSavyhin | 0:4cdaf9b1e7d0 | 60 | * wkup: reset pin for Spwf module |
PavelSavyhin | 0:4cdaf9b1e7d0 | 61 | * rts: Pin USART RTS |
PavelSavyhin | 0:4cdaf9b1e7d0 | 62 | * debug : not used |
PavelSavyhin | 0:4cdaf9b1e7d0 | 63 | * @retval none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 64 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 65 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, bool debug) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 66 | : _spwf(tx, rx, PC_12, PC_8, debug) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 67 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 68 | memset(_ids, 0, sizeof(_ids)); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 69 | isInitialized = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 70 | isListening = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 71 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 72 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 73 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName reset, PinName wakeup, bool debug) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 74 | : _spwf(tx, rx, reset, wakeup, debug) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 75 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 76 | memset(_ids, 0, sizeof(_ids)); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 77 | isInitialized = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 78 | isListening = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 79 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 80 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 81 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 82 | * @brief SpwfSAInterface destructor |
PavelSavyhin | 0:4cdaf9b1e7d0 | 83 | * @param none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 84 | * @retval none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 85 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 86 | SpwfSAInterface::~SpwfSAInterface() |
PavelSavyhin | 0:4cdaf9b1e7d0 | 87 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 88 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 89 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 90 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 91 | * @brief init function |
PavelSavyhin | 0:4cdaf9b1e7d0 | 92 | initializes SPWF FW and module |
PavelSavyhin | 0:4cdaf9b1e7d0 | 93 | * @param none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 94 | * @retval error value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 95 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 96 | int SpwfSAInterface::init(void) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 97 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 98 | if(_spwf.startup(0)) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 99 | isInitialized=true; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 100 | return true; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 101 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 102 | else return NSAPI_ERROR_DEVICE_ERROR; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 103 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 104 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 105 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 106 | * @brief network connect |
PavelSavyhin | 0:4cdaf9b1e7d0 | 107 | connects to Access Point |
PavelSavyhin | 0:4cdaf9b1e7d0 | 108 | * @param ap: Access Point (AP) Name String |
PavelSavyhin | 0:4cdaf9b1e7d0 | 109 | * pass_phrase: Password String for AP |
PavelSavyhin | 0:4cdaf9b1e7d0 | 110 | * security: type of NSAPI security supported |
PavelSavyhin | 0:4cdaf9b1e7d0 | 111 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 112 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 113 | int SpwfSAInterface::connect(const char *ap, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 114 | const char *pass_phrase, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 115 | nsapi_security_t security) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 116 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 117 | int mode; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 118 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 119 | //initialize the device before connecting |
PavelSavyhin | 0:4cdaf9b1e7d0 | 120 | if(!isInitialized) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 121 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 122 | if(!init()) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 123 | return NSAPI_ERROR_DEVICE_ERROR; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 124 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 125 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 126 | switch(security) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 127 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 128 | case NSAPI_SECURITY_NONE: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 129 | mode = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 130 | pass_phrase = NULL; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 131 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 132 | case NSAPI_SECURITY_WEP: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 133 | mode = 1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 134 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 135 | case NSAPI_SECURITY_WPA: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 136 | case NSAPI_SECURITY_WPA2: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 137 | mode = 2; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 138 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 139 | default: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 140 | mode = 2; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 141 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 142 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 143 | return (_spwf.connect((char*)ap, (char*)pass_phrase, mode)); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 144 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 145 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 146 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 147 | * @brief network disconnect |
PavelSavyhin | 0:4cdaf9b1e7d0 | 148 | disconnects from Access Point |
PavelSavyhin | 0:4cdaf9b1e7d0 | 149 | * @param none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 150 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 151 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 152 | int SpwfSAInterface::disconnect() |
PavelSavyhin | 0:4cdaf9b1e7d0 | 153 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 154 | return (_spwf.disconnect()); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 155 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 156 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 157 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 158 | * @brief Get the local IP address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 159 | * @param none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 160 | * @retval Null-terminated representation of the local IP address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 161 | * or null if not yet connected |
PavelSavyhin | 0:4cdaf9b1e7d0 | 162 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 163 | const char *SpwfSAInterface::get_ip_address() |
PavelSavyhin | 0:4cdaf9b1e7d0 | 164 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 165 | return _spwf.getIPAddress(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 166 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 167 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 168 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 169 | * @brief Get the MAC address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 170 | * @param none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 171 | * @retval Null-terminated representation of the MAC address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 172 | * or null if not yet connected |
PavelSavyhin | 0:4cdaf9b1e7d0 | 173 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 174 | const char *SpwfSAInterface::get_mac_address() |
PavelSavyhin | 0:4cdaf9b1e7d0 | 175 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 176 | return _spwf.getMACAddress(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 177 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 178 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 179 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 180 | * @brief open a socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 181 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 182 | * proto: TCP/UDP protocol |
PavelSavyhin | 0:4cdaf9b1e7d0 | 183 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 184 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 185 | int SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 186 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 187 | int id = -1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 188 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 189 | struct spwf_socket *socket = new struct spwf_socket; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 190 | if (!socket) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 191 | return NSAPI_ERROR_NO_SOCKET; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 192 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 193 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 194 | socket->id = id; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 195 | socket->server_port = id; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 196 | socket->proto = proto; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 197 | socket->connected = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 198 | *handle = socket; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 199 | return 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 200 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 201 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 202 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 203 | * @brief connect to a remote socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 204 | * @param handle: Pointer to socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 205 | * addr: Address to connect to |
PavelSavyhin | 0:4cdaf9b1e7d0 | 206 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 207 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 208 | int SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 209 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 210 | int sock_id = 99; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 211 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 212 | const char *proto; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 213 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 214 | switch (socket->proto) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 215 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 216 | case NSAPI_UDP: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 217 | proto = "u"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 218 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 219 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 220 | case NSAPI_TCP: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 221 | proto = "t"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 222 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 223 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 224 | case NSAPI_TLS: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 225 | proto = "s"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 226 | break; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 227 | default: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 228 | return NSAPI_ERROR_UNSUPPORTED; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 229 | break;// defensive programming |
PavelSavyhin | 0:4cdaf9b1e7d0 | 230 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 231 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 232 | if (!_spwf.open(proto, &sock_id, addr.get_ip_address(), addr.get_port())) {;//sock ID is allocated NOW |
PavelSavyhin | 0:4cdaf9b1e7d0 | 233 | return NSAPI_ERROR_DEVICE_ERROR; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 234 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 235 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 236 | //TODO: Maintain a socket table to map socket ID to host & port |
PavelSavyhin | 0:4cdaf9b1e7d0 | 237 | //TODO: lookup on client table to see if already socket is allocated to same host/port |
PavelSavyhin | 0:4cdaf9b1e7d0 | 238 | //multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 239 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 240 | if(sock_id <= SPWFSA_SOCKET_COUNT) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 241 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 242 | socket->id = sock_id;//the socket ID of this Socket instance |
PavelSavyhin | 0:4cdaf9b1e7d0 | 243 | _ids[socket->id] = true; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 244 | socket->connected = true; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 245 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 246 | else |
PavelSavyhin | 0:4cdaf9b1e7d0 | 247 | return NSAPI_ERROR_NO_SOCKET; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 248 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 249 | return 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 250 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 251 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 252 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 253 | * @brief bind to a port number and address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 254 | * @param handle: Pointer to socket handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 255 | * proto: address to bind to |
PavelSavyhin | 0:4cdaf9b1e7d0 | 256 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 257 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 258 | int SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 259 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 260 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 261 | socket->server_port = address.get_port(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 262 | return 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 263 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 264 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 265 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 266 | * @brief start listening on a port and address |
PavelSavyhin | 0:4cdaf9b1e7d0 | 267 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 268 | * backlog: not used (always value is 1) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 269 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 270 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 271 | int SpwfSAInterface::socket_listen(void *handle, int backlog) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 272 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 273 | return NSAPI_ERROR_UNSUPPORTED; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 274 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 275 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 276 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 277 | * @brief accept connections from remote sockets |
PavelSavyhin | 0:4cdaf9b1e7d0 | 278 | * @param handle: Pointer to handle of client socket (connecting) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 279 | * proto: handle of server socket which will accept connections |
PavelSavyhin | 0:4cdaf9b1e7d0 | 280 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 281 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 282 | int SpwfSAInterface::socket_accept(void **handle, void *server) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 283 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 284 | return NSAPI_ERROR_UNSUPPORTED; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 285 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 286 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 287 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 288 | * @brief close a socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 289 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 290 | * @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 291 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 292 | int SpwfSAInterface::socket_close(void *handle) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 293 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 294 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 295 | int err = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 296 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 297 | if(socket->id!=-1) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 298 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 299 | if (_spwf.close(socket->id)) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 300 | if(socket->id==SERVER_SOCKET_NO) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 301 | isListening = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 302 | else |
PavelSavyhin | 0:4cdaf9b1e7d0 | 303 | _ids[socket->id] = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 304 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 305 | else err = NSAPI_ERROR_DEVICE_ERROR; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 306 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 307 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 308 | delete socket; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 309 | return err; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 310 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 311 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 312 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 313 | * @brief write to a socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 314 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 315 | * data: pointer to data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 316 | * size: size of data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 317 | * @retval no of bytes sent |
PavelSavyhin | 0:4cdaf9b1e7d0 | 318 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 319 | int SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 320 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 321 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 322 | //int err; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 323 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 324 | /*if(socket->id==SERVER_SOCKET_NO) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 325 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 326 | if(socket->server_port==-1 || !isListening) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 327 | return NSAPI_ERROR_NO_SOCKET; //server socket not bound or not listening |
PavelSavyhin | 0:4cdaf9b1e7d0 | 328 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 329 | err = _spwf.socket_server_write((uint16_t)size, (char*)data); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 330 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 331 | else |
PavelSavyhin | 0:4cdaf9b1e7d0 | 332 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 333 | err = _spwf.send(socket->id, (char*)data, (uint32_t)size); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 334 | }*/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 335 | if (!_spwf.send(socket->id, (char*)data, (uint32_t)size)) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 336 | return NSAPI_ERROR_DEVICE_ERROR; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 337 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 338 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 339 | return size; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 340 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 341 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 342 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 343 | * @brief receive data on a socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 344 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 345 | * data: pointer to data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 346 | * size: size of data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 347 | * @retval no of bytes read |
PavelSavyhin | 0:4cdaf9b1e7d0 | 348 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 349 | int SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 350 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 351 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 352 | int32_t recv; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 353 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 354 | //CHECK:Receive for both Client and Server Sockets same? |
PavelSavyhin | 0:4cdaf9b1e7d0 | 355 | recv = _spwf.recv(socket->id, (char*)data, (uint32_t)size); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 356 | if (recv < 0) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 357 | //wait_ms(1);//delay of 1ms <for F4>?? |
PavelSavyhin | 0:4cdaf9b1e7d0 | 358 | //printf("."); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 359 | if (recv == -1) return NSAPI_ERROR_WOULD_BLOCK;//send this if we want to block call (else timeout will happen) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 360 | else if (recv == -2)return NSAPI_ERROR_DEVICE_ERROR; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 361 | else if (recv == -3)return NSAPI_ERROR_NO_CONNECTION; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 362 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 363 | return recv; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 364 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 365 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 366 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 367 | * @brief send data to a udp socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 368 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 369 | * addr: address of udp socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 370 | * data: pointer to data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 371 | * size: size of data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 372 | * @retval no of bytes sent |
PavelSavyhin | 0:4cdaf9b1e7d0 | 373 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 374 | int SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 375 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 376 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 377 | if (!socket->connected) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 378 | int err = socket_connect(socket, addr); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 379 | if (err < 0) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 380 | return err; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 381 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 382 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 383 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 384 | return socket_send(socket, data, size); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 385 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 386 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 387 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 388 | * @brief receive data on a udp socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 389 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 390 | * addr: address of udp socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 391 | * data: pointer to data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 392 | * size: size of data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 393 | * @retval no of bytes read |
PavelSavyhin | 0:4cdaf9b1e7d0 | 394 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 395 | int SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 396 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 397 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 398 | return socket_recv(socket, data, size); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 399 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 400 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 401 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 402 | * @brief attach function/callback to the socket |
PavelSavyhin | 0:4cdaf9b1e7d0 | 403 | * Not used |
PavelSavyhin | 0:4cdaf9b1e7d0 | 404 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 405 | * callback: callback function pointer |
PavelSavyhin | 0:4cdaf9b1e7d0 | 406 | * data: pointer to data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 407 | * @retval none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 408 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 409 | void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 410 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 411 | //No implementation yet |
PavelSavyhin | 0:4cdaf9b1e7d0 | 412 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 413 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 414 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 415 | * @brief utility debug function for printing to serial terminal |
PavelSavyhin | 0:4cdaf9b1e7d0 | 416 | * @param string: Pointer to data |
PavelSavyhin | 0:4cdaf9b1e7d0 | 417 | * @retval none |
PavelSavyhin | 0:4cdaf9b1e7d0 | 418 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 419 | void SpwfSAInterface::debug(const char * string) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 420 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 421 | //_spwf.debug_print(string); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 422 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 423 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 424 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 425 | * @brief Set the socket options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 426 | * Not used |
PavelSavyhin | 0:4cdaf9b1e7d0 | 427 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 428 | * level: SOL_SOCKET |
PavelSavyhin | 0:4cdaf9b1e7d0 | 429 | * optname: option name |
PavelSavyhin | 0:4cdaf9b1e7d0 | 430 | * optval: pointer to option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 431 | * optlen: option length |
PavelSavyhin | 0:4cdaf9b1e7d0 | 432 | @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 433 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 434 | int SpwfSAInterface::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 435 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 436 | // struct spwf_socket *socket = (struct spwf_socket *)handle; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 437 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 438 | switch (optname) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 439 | case NSAPI_REUSEADDR: /*!< Allow bind to reuse local addresses */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 440 | case NSAPI_KEEPALIVE: /*!< Enables sending of keepalive messages */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 441 | case NSAPI_LINGER: /*!< Keeps close from returning until queues empty */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 442 | case NSAPI_SNDBUF: /*!< Sets send buffer size */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 443 | case NSAPI_RCVBUF: /*!< Sets recv buffer size */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 444 | default: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 445 | printf("SpwfSAInterface::setsockopt> ERROR!!!! Unknown optname: %d \r\n", optname); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 446 | return -1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 447 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 448 | return NSAPI_ERROR_UNSUPPORTED;// defensive programming |
PavelSavyhin | 0:4cdaf9b1e7d0 | 449 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 450 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 451 | /** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 452 | * @brief Get the socket options |
PavelSavyhin | 0:4cdaf9b1e7d0 | 453 | * Not used |
PavelSavyhin | 0:4cdaf9b1e7d0 | 454 | * @param handle: Pointer to handle |
PavelSavyhin | 0:4cdaf9b1e7d0 | 455 | * level: SOL_SOCKET |
PavelSavyhin | 0:4cdaf9b1e7d0 | 456 | * optname: option name |
PavelSavyhin | 0:4cdaf9b1e7d0 | 457 | * optval: pointer to option value |
PavelSavyhin | 0:4cdaf9b1e7d0 | 458 | * optlen: pointer to option length |
PavelSavyhin | 0:4cdaf9b1e7d0 | 459 | @retval NSAPI Error Type |
PavelSavyhin | 0:4cdaf9b1e7d0 | 460 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 461 | int SpwfSAInterface::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 462 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 463 | return NSAPI_ERROR_UNSUPPORTED; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 464 | } |