This article describes how to call the API operations of Link SDK for C to to obtain the UTC time from IoT Platform. In this example, the ./demos/ntp_posix_demo.c sample code file is used.

Background information

  • For more information about the NTP service, see Overview.

  • You must establish an MQTT connection. For more information, see Overview.

Step 1: Initialize a client

  1. Add header files.
    ...
    ...
    
    #include "aiot_ntp_api.h"
  2. Add the underlying dependency and configure the log output feature.
        aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
        aiot_state_set_logcb(demo_state_logcb);
  3. Call the aiot_ntp_init operation to create an NTP client instance and initialize the default parameters.
        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 required features

Call the aiot_ntp_setopt operation to configure the following items:

  1. Associate with an MQTT connection handle.
    Notice Before you set NTP service-specific parameters, make sure that device authentication information is specified. For more information, see Example.
    • Sample code
          aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_MQTT_HANDLE, mqtt_handle);
    • Parameters:
      Parameter Example Description
      AIOT_NTPOPT_MQTT_HANDLE mqtt_handle You must establish an MQTT connection. This parameter is used to associate with the MQTT connection handle.
  2. Set a time zone.
    • Sample code
          int8_t      time_zone = 8;
          ...
          ...
          aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_TIME_ZONE, (int8_t *)&time_zone);
    • Parameters:
      Parameter Example Description
      AIOT_NTPOPT_TIME_ZONE time_zone The time zone in which the device resides.

      In this example, time_zone=8 indicates UTC+8.

      Set a time zone by specifying time_zone. Valid values: -12 to 12. For example, the value -3 indicates UTC-3.

  3. Configure a message callback.
    • Sample code
          aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_RECV_HANDLER, (void *)demo_ntp_recv_handler);
    • Parameters:
      Parameter Example Description
      AIOT_NTPOPT_RECV_HANDLER demo_ntp_recv_handler This function is called when an NTP service message is received.
  4. Configure status monitoring.
    1. Define the callback to monitor status.
      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: {
      
              }
          }
      }
    2. Specify the callbacks to monitor status.
      • Sample code
            aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_EVENT_HANDLER, (void *)demo_ntp_event_handler);
      • Parameters:
        Parameter Example Description
        AIOT_NTPOPT_EVENT_HANDLER demo_ntp_event_handler When the status of device connection changes, the callback is called to perform the required operations.

Step 3: Send a request

Call the aiot_ntp_send_request operation to send the current time of the device to the server by using the /ext/ntp/${YourProductKey}/${YourDeviceName}/request topic. For more information about the parameters, see the previous step.

Link SDK can automatically submit the current time of the device by calling the API operation.

    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 a response

After the device receives the NTP service message, the demo_ntp_recv_handler callback is called to perform the required operations.

When you specify the processing logic of the callback, take note of the following items:
  • IoT Platform sends the server time to the device by using the /ext/ntp/${YourProductKey}/${YourDeviceName}/response topic.

  • aiot_ntp_recv_t indicates the data format. For more information, see the sample code.
  • In this example, after the response message is parsed, the packet pointer is used to print the parsed message.

void demo_ntp_recv_handler(void *handle, const aiot_ntp_recv_t *packet, void *userdata)
{
    switch (packet->type) {
        /* TODO: Parse and store the time information in the current time zone. The time information includes the year, month, day, hour, minute, and second. */
        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 the aiot_ntp_deinit operation to destroy the NTP client instance and release resources.

    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

  • After you configure the sample code file, compile the file to generate an executable file In this example, the ./demos/ntp-posix-demo executable file is generated.

    For more information, see Compilation and running.

  • For more information about running results, see View logs.