Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_B-L475E-IOT01 mbed es_wifi jsmn
TCPConnector.h
00001 #pragma once 00002 00003 #include <mbed.h> 00004 #include "es_wifi.h" 00005 00006 00007 #define WIFI_WRITE_TIMEOUT 3000 00008 #define WIFI_READ_TIMEOUT 25000 00009 00010 class TCPConnector 00011 { 00012 private: 00013 ES_WIFIObject_t m_es_wifi_ctx; 00014 00015 public: 00016 00017 TCPConnector() {} 00018 ~TCPConnector(){} 00019 00020 bool wifi_connect(char *wifi_ssid, char* wifi_wpa_password, uint32_t max_attempts) 00021 { 00022 00023 /* HW setup */ 00024 if (ES_WIFI_RegisterBusIO(&m_es_wifi_ctx, 00025 SPI_WIFI_Init, 00026 SPI_WIFI_DeInit, 00027 SPI_WIFI_Delay, 00028 SPI_WIFI_SendData, 00029 SPI_WIFI_ReceiveData) != ES_WIFI_STATUS_OK) 00030 return false; 00031 00032 if (ES_WIFI_Init(&m_es_wifi_ctx) != ES_WIFI_STATUS_OK) 00033 return false; 00034 ES_WIFI_ResetToFactoryDefault(&m_es_wifi_ctx); 00035 00036 /* JOIN AP */ 00037 for (int i = 0; i < max_attempts; i++) 00038 { 00039 if (ES_WIFI_Connect(&m_es_wifi_ctx, wifi_ssid, wifi_wpa_password, ES_WIFI_SEC_WPA_WPA2) == ES_WIFI_STATUS_OK) 00040 { 00041 return (ES_WIFI_GetNetworkSettings(&m_es_wifi_ctx) == ES_WIFI_STATUS_OK); 00042 } 00043 wait_ms(1000); 00044 } 00045 00046 return false; 00047 } 00048 00049 00050 bool tcp_connect(uint8_t socket, char *domain_name, uint16_t remote_port, bool secure,uint32_t max_attempts) 00051 { 00052 ES_WIFI_Conn_t conn; 00053 ES_WIFI_Status_t res; 00054 00055 conn.Number = socket; 00056 conn.RemotePort = remote_port; 00057 conn.LocalPort = 0; 00058 conn.Type = (secure)?ES_WIFI_TCP_SSL_CONNECTION:ES_WIFI_TCP_CONNECTION; 00059 strncpy((char *)conn.RemoteHost, domain_name, sizeof(conn.RemoteHost)); 00060 for(int i=0;i<max_attempts;i++) 00061 { 00062 res = ES_WIFI_ConnectToRemoteHost(&m_es_wifi_ctx, &conn); 00063 if(res == ES_WIFI_STATUS_OK) return true; 00064 } 00065 return false; 00066 } 00067 00068 00069 bool tcp_close(uint32_t socket) 00070 { 00071 ES_WIFI_Conn_t conn; 00072 conn.Number = socket; 00073 00074 return (ES_WIFI_StopClientConnection(&m_es_wifi_ctx, &conn)== ES_WIFI_STATUS_OK); 00075 } 00076 00077 00078 bool tcp_write(uint8_t socket, char *data, uint32_t data_len) 00079 { 00080 uint16_t ack; 00081 uint16_t chunk_size; 00082 ES_WIFI_Status_t res; 00083 00084 for(int i=0;i<data_len;i+=ES_WIFI_PAYLOAD_SIZE) 00085 { 00086 chunk_size = ((data_len-i)<ES_WIFI_PAYLOAD_SIZE)?(data_len-i):ES_WIFI_PAYLOAD_SIZE; 00087 res = ES_WIFI_SendData(&m_es_wifi_ctx, socket, (uint8_t*)data+i, chunk_size, &ack, WIFI_WRITE_TIMEOUT); 00088 if( res != ES_WIFI_STATUS_OK || chunk_size != ack ) return false; 00089 } 00090 return true; 00091 } 00092 00093 00094 bool tcp_read(uint8_t socket, char *rx_buffer, uint16_t rx_buffer_len, uint16_t *received_bytes,bool continued) 00095 { 00096 bool rx = false; 00097 *received_bytes = 0; 00098 00099 rx = (continued)?( ES_WIFI_ReceiveData2(&m_es_wifi_ctx, (uint8_t*)rx_buffer, rx_buffer_len, received_bytes) == ES_WIFI_STATUS_OK ): 00100 ( ES_WIFI_ReceiveData(&m_es_wifi_ctx, socket, (uint8_t*)rx_buffer, rx_buffer_len, received_bytes, WIFI_READ_TIMEOUT) == ES_WIFI_STATUS_OK ); 00101 00102 return rx; 00103 } 00104 00105 00106 00107 00108 00109 };
Generated on Fri Jul 15 2022 03:03:13 by
1.7.2