Usage example

Updated at:

The ./demos/ntp_posix_demo.c demo file demonstrates how a device obtains the UTC time from IoT Platform.

Background information

  • For more information about the Network Time Protocol (NTP) service, see Overview.

  • The NTP service uses an MQTT connection. For more information about the code for MQTT connections, see MQTT connection.

Step 1: Initialization

  1. Add the header file.
    ……
    ……
    
    #include "aiot_ntp_api.h"

  2. Configure underlying dependencies and log output.

        aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
        aiot_state_set_logcb(demo_state_logcb);
  3. Call aiot_ntp_init to create an NTP handle.
        ntp_handle = aiot_ntp_init();
        if (ntp_handle == NULL) {
            demo_mqtt_stop(&mqtt_handle);
            printf("aiot_ntp_init failed\n");
            return -1;
        }

Step 2: Configure features

Call aiot_ntp_setopt to configure the following features.

  1. Associate the MQTT connection handle.
    Important Before you configure NTP service parameters, ensure that device authentication information and other related parameters are configured. For more information, see Configure MQTT connection parameters.
    •     aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_MQTT_HANDLE, mqtt_handle);
    • Configuration itemExampleDescription
      AIOT_NTPOPT_MQTT_HANDLEmqtt_handleThe NTP service sends requests over an MQTT connection. This configuration item associates the MQTT connection handle.

  2. Set the time zone.
    •     int8_t      time_zone = 8;
          ……
          ……
          aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_TIME_ZONE, (int8_t *)&time_zone);
    • Configuration itemExampleDescription
      AIOT_NTPOPT_TIME_ZONEtime_zoneThe time zone of the device.

      The time zone defined in the sample code is time_zone=8, which sets the device time zone to UTC+8.

      Set the device time zone by setting the value of time_zone. Valid values: -12 to 12. For example, -3 specifies UTC-3.

  3. Configure a message callback.
    •     aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_RECV_HANDLER, (void *)demo_ntp_recv_handler);
    • Configuration itemExample valueDescription
      AIOT_NTPOPT_RECV_HANDLERdemo_ntp_recv_handlerThis function is called when an NTP service message is received.
  4. .

    1. void demo_ntp_event_handler(void *handle, const aiot_ntp_event_t *event, void *userdata)
      {
          switch (event->type) {
              case AIOT_NTPEVT_INVALID_RESPONSE: {
                  printf("AIOT_NTPEVT_INVALID_RESPONSE\n");
              }
              break;
              case AIOT_NTPEVT_INVALID_TIME_FORMAT: {
                  printf("AIOT_NTPEVT_INVALID_TIME_FORMAT\n");
              }
              break;
              default: {
      
              }
          }
      }


      •     aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_EVENT_HANDLER, (void *)demo_ntp_event_handler);
      • Configuration itemExample valueDescription
        AIOT_NTPOPT_EVENT_HANDLERdemo_ntp_event_handler

        When the device connection status changes, the corresponding processing logic defined in this callback function is executed.


Step 3: Send a request

Call aiot_ntp_send_request to send a request message that contains the device's current time to the server over the topic /ext/ntp/${YourProductKey}/${YourDeviceName}/request. This call uses the parameters that you configured in the previous step.

Link SDK automatically reports the current device time using system interfaces.

    res = aiot_ntp_send_request(ntp_handle);
    if (res < STATE_SUCCESS) {
        aiot_ntp_deinit(&ntp_handle);
        demo_mqtt_stop(&mqtt_handle);
        return -1;
    }

Step 4: Receive the acknowledgement

After the device receives the NTP service message, the demo_ntp_recv_handler callback function is triggered to process the message.

Use the following information to write the logic for the callback function:
  • IoT Platform sends a message that contains the time to the device over the topic /ext/ntp/${YourProductKey}/${YourDeviceName}/response.

  • The data structure of the time message is aiot_ntp_recv_t. See the sample code for an example of how to retrieve the time.
  • The sample code in this topic parses the returned message, which is available in the packet parameter, and then prints the time message.

void demo_ntp_recv_handler(void *handle, const aiot_ntp_recv_t *packet, void *userdata)
{
    switch (packet->type) {
        /* TODO: Parse and store the values of year, month, day, hour, minute, and second in the current time zone. */
        case AIOT_NTPRECV_LOCAL_TIME: {
            printf("local time: %llu, %02d/%02d/%02d-%02d:%02d:%02d:%d\n",
                   (long long unsigned int)packet->data.local_time.timestamp,
                   packet->data.local_time.year,
                   packet->data.local_time.mon, packet->data.local_time.day, packet->data.local_time.hour, packet->data.local_time.min,
                   packet->data.local_time.sec, packet->data.local_time.msec);
        }
        break;

        default: {

        }
    }
}

Step 5: Exit the program

Call aiot_ntp_deinit to destroy the NTP handle.

    res = aiot_ntp_deinit(&ntp_handle);
    if (res < STATE_SUCCESS) {
        demo_mqtt_stop(&mqtt_handle);
        printf("aiot_ntp_deinit failed: -0x%04X\n", -res);
        return -1;
    }

What to do next