4.5.1 Execution

main.c - Initialize the socket and get the time from the NTP 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 */
    • Initialize the socket module and register the socket callback function.
      /* Initialize Socket module */
      socketInit();
      /* Register socket handler, resolve handler */
      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, create a UDP socket and bind it in the main loop.
      if (udp_socket < 0) {
          udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
          if (udp_socket < 0) {
      	printf("main: UDP Client Socket Creation Failed.\r\n");
      	continue;
          }
      /* Initialize default socket address structure. */
      addr_in.sin_family = AF_INET;
      addr_in.sin_addr.s_addr = _htonl(MAIN_DEFAULT_ADDRESS);
      addr_in.sin_port = _htons(MAIN_DEFAULT_PORT);
      bind(udp_socket, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in));
    • Initialize the socket module and send an NTP time query to the NTP server in the resolve_cb() function
      static void resolve_cb(uint8_t *pu8DomainName, uint32_t u32ServerIP)
      {
      	...
      if (udp_socket >= 0) {
      		addr.sin_family = AF_INET;
      		addr.sin_port = _htons(MAIN_SERVER_PORT_FOR_UDP);
      		addr.sin_addr.s_addr = u32ServerIP;
      
      		ret = sendto(udp_socket, (int8_t *)&cDataBuf, ...);
      	}
    • Receive the NTP time from the server and convert it in the socket_cb() function.
      static void resolve_cb(uint8_t *pu8DomainName, uint32_t u32ServerIP)
      {
      	...
      if (udp_socket >= 0) {
      		addr.sin_family = AF_INET;
      		addr.sin_port = _htons(MAIN_SERVER_PORT_FOR_UDP);
      		addr.sin_addr.s_addr = u32ServerIP;
      
      		ret = sendto(udp_socket, (int8_t *)&cDataBuf, ...);
      	}
    • Parse the time from the received server response.
      static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
      {
          ...
          case SOCKET_MSG_RECVFROM:
          {
              /* printf("socket_cb: socket_msg_recvfrom!\r\n"); */
              tstrSocketRecvMsg *pstrRx = (tstrSocketRecvMsg *)pvMsg;
              if (pstrRx->pu8Buffer && pstrRx->s16BufferSize) {
                  uint8_t packetBuffer[48];
                  memcpy(&packetBuffer, pstrRx->pu8Buffer, sizeof(packetBuffer));
                  ...
                  uint32_t secsSince1900 = packetBuffer[40] << 24 |
                                          packetBuffer[41] << 16 |
                                          packetBuffer[42] << 8 |
                                          packetBuffer[43];
      
                  /* Now convert NTP time into everyday time.
                   * Unix time starts on Jan 1 1970. In seconds, that's 2208988800.
                  * Subtract seventy years. */
                  const uint32_t seventyYears = 2208988800UL;
                  uint32_t epoch = secsSince1900 - seventyYears;
          	/* Print the hour, minute and second. GMT is the time at Greenwich Meridian. */
                  printf("socket_cb: The GMT time is %lu:%02lu:%02lu\r\n",
      						(epoch  % 86400L) / 3600,           /* hour (86400 equals secs per day) */
      						(epoch  % 3600) / 60,               /* minute (3600 equals secs per minute) */
      						epoch % 60);                        /* second */
  2. Build the program and download it into the board.
  3. Start the application.