5.12.1 Execution

main.c - Initialize the device and connect to a server using SSL.

  1. 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 AP. */ 
      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);
      
    • Ensure the following configuration in sslConnect():

      Call socket() with the SOCKET_FLAGS_SSL bit set in u8Flags parameter.

      /* Create secure socket */
          if (tcp_client_socket < 0) {
              tcp_client_socket = socket(AF_INET, SOCK_STREAM, SOCKET_FLAGS_SSL);  
              }
      
    • Configure the SNI for the socket via setsockopt() and option SO_SSL_SNI.
      /*Configure the SNI for the socket */
      setsockopt(tcp_client_socket, SOL_SSL_SOCKET, SO_SSL_SNI, MAIN_HOST_NAME, sizeof(MAIN_HOST_NAME));
      
    • Call set_alpn_list() with parameter pointing to "h2 http/1.1" (this means that HTTP/2 and HTTP/1.1 are both supported).
      /* Sets the protocol list to be used for ALPN */
      set_alpn_list(tcp_client_socket,"h2 http/1.1");
      
    • After the configuration is applied, try to connect to the SSL server by calling sslConnect().
      if (sslConnect() != SOCK_ERR_NO_ERROR) {    
          gu8SocketStatus = SocketInit; 
      }
    • If successfully connected, the socket_cb() function will be called with the SOCKET_MSG_CONNECT message. The user can check for the ALPN negotiation type in the socket_cb() function with the get_alpn_index() function.
      /* Check for ALPN negotiation type. */
      	      switch (alpn_index)
      	      {
      		case 1:
      		printf("Negotiated HTTP/2.\r\n");
      		break;
      		case 2:
      		printf("Negotiated HTTP/1.1.\r\n");
      		break;
      		case 0:
      		printf("Protocol negotiation did not occur.\r\n");
      		break;
      	      }
      
  2. Build the program and download it into the board.
  3. Start the application.
    Note: To set up an SSL connection, a root certificate must be installed.