5.9.1 Execution
main.c - Initialize the device and connect to a server using SSL.
- Code summary.
- Configure the network and
server parameters in
main.h.
/** Wi-Fi Settings */ #define MAIN_WLAN_SSID "DEMO_AP" /**< Destination SSID */ #define MAIN_WLAN_AUTH M2M_WIFI_SEC_WPA_PSK /**< Security type */ #define MAIN_WLAN_PSK "12345678" /**< Password for Destination SSID */ /** All SSL defines */ #define MAIN_HOST_NAME "www.google.com" #define MAIN_HOST_PORT 443 - Initialize the socket
module and create the UDP server socket.
socketInit(); registerSocketCallback(socket_cb, resolve_cb); - Connect to the AP.
/* Connect to router. */ m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); - After the device is
connected to the AP, the
gethostbyname()function will be executed.static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { case M2M_WIFI_REQ_DHCP_CONF: { /* Obtain the IP Address by network name */ gethostbyname((uint8_t *)MAIN_HOST_NAME); - In the main loop, try to
connect to the SSL server after resolving the domain host
name.
if (sslConnect() != SOCK_ERR_NO_ERROR) { gu8SocketStatus = SocketInit; } - If successfully
connected, the
socket_cb()function will be called with the SOCKET_MSG_CONNECT message.static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg) { switch (u8Msg) { case SOCKET_MSG_CONNECT: if (pstrConnect && pstrConnect->s8Error >= SOCK_ERR_NO_ERROR) printf("Successfully connected.\r\n"); break; - You can receive data in
the
socket_cb()function with theSOCKET_MSG_RECVFROMmessage when a client device sends data. (Use the “UDP Client” example.)static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg) { ... } else if (u8Msg == SOCKET_MSG_RECVFROM) { tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg *)pvMsg; if (pstrRx->pu8Buffer && pstrRx->s16BufferSize) { printf("socket_cb: received app message.(%u)\r\n", packetCnt); /* Prepare next buffer reception. */ recvfrom(sock, gau8SocketTestBuffer, MAIN_WIFI_M2M_BUFFER_SIZE, 0);
- Configure the network and
server parameters in
main.h.
- Build the program and download it into the board.
- Start the application.
Note: To set up an SSL connection, a root
certificate must be installed.
