4.4.1 Execution

main.c – Initialize the Wi-Fi module and test the TCP server.

  1. Code summary.
    • Configure the network 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 manner */
      #define MAIN_WLAN_PSK                     "12345678" /**< Password for Destination SSID */
      #define MAIN_WIFI_M2M_PRODUCT_NAME        "NMCTemp"
      #define MAIN_WIFI_M2M_SERVER_IP           0xFFFFFFFF /* 255.255.255.255 */
      #define MAIN_WIFI_M2M_SERVER_PORT         (6666)
      #define MAIN_WIFI_M2M_REPORT_INTERVAL     (1000)
    • Initialize the socket module and register the socket callback function.
      /* Initialize socket address structure. */
      addr.sin_family = AF_INET;
      addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT);
      addr.sin_addr.s_addr = _htonl(MAIN_WIFI_M2M_SERVER_IP);
      /* Initialize socket module */
      socketInit();
      registerSocketCallback(socket_cb, NULL);
    • 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, create a TCP server socket and bind it in the main loop.
      if (tcp_server_socket < 0) {
      /* Open TCP server socket */
          if ((tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
              printf("main: failed to create TCP server socket error!\r\n");
              continue;
          }
      /* Bind service*/
          bind(tcp_server_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
      }
    • Five operations (bind/listen/accept/recv/send) must be executed sequentially in the socket_cb() function.
      static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
      {
          ...
          case SOCKET_MSG_BIND:
          {
              tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg *)pvMsg;
              if (pstrBind && pstrBind->status == 0)
                  listen(tcp_server_socket, 0);
          }
          case SOCKET_MSG_LISTEN:
          {
          tstrSocketListenMsg *pstrListen = (tstrSocketListenMsg *)pvMsg;
          if (pstrListen && pstrListen->status == 0)
              accept(tcp_server_socket, NULL, NULL);
          }
          case SOCKET_MSG_ACCEPT:
          {
              tstrSocketAcceptMsg *pstrAccept = (tstrSocketAcceptMsg *)pvMsg;
              if (pstrAccept) {
                  tcp_client_socket = pstrAccept->sock;
                  recv(tcp_client_socket, gau8SocketTestBuffer, ..., 0);
              }
          }
          case SOCKET_MSG_RECV:
          {
              tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg;
              if (pstrRecv && pstrRecv->s16BufferSize > 0)
                  send(tcp_client_socket, &msg_wifi_product, ..., 0);
          }
          case SOCKET_MSG_SEND:
          {
              printf("socket_cb: send success!\r\n");
              printf("TCP Server Test Complete!\r\n");
              printf("close socket\n");
          }
  2. Build the program and download it into the board.
  3. Start the application.
    Note: In case the Peer (TCP client) closes the socket, the only way for the application running on the MCU to find out about the closure of the socket is in the socket_cb() function in the recv operation, which returns an error -12 (SOCK_ERR_CONN_ABORTED) indicating the socket is closed by the peer.
    static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
    {
    ...
    SOCKET_MSG_RECV:
    {
    tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg;
    if (pstrRecv && pstrRecv->s16BufferSize > 0) {
    
    printf("r %s\r\n",gau8SocketTestBuffer); 
    recv(tcp_client_socket, gau8SocketTestBuffer, sizeof(gau8SocketTestBuffer), 0);
    } else {
    
    printf("socket_cb: recv error! %d\r\n",pstrRecv->s16BufferSize); 
    // Prints the error value in the receive callback.
    close(tcp_server_socket);
    tcp_server_socket = -1;
    }
    ...
    }