Usage example
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
- Add the header file.
…… …… #include "aiot_ntp_api.h" Configure underlying dependencies and log output.
aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile); aiot_state_set_logcb(demo_state_logcb);- 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.
- Associate the MQTT connection handle.
- Set the time zone.
- Configure a message callback.
- Configure a status monitoring callback.
- 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 item Example Description AIOT_NTPOPT_MQTT_HANDLE mqtt_handle The NTP service sends requests over an MQTT connection. This configuration item associates the MQTT connection handle.
- Set the time zone.
int8_t time_zone = 8; …… …… aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_TIME_ZONE, (int8_t *)&time_zone);Configuration item Example Description AIOT_NTPOPT_TIME_ZONE time_zone The 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,-3specifies UTC-3.
- Configure a message callback.
aiot_ntp_setopt(ntp_handle, AIOT_NTPOPT_RECV_HANDLER, (void *)demo_ntp_recv_handler);Configuration item Example value Description AIOT_NTPOPT_RECV_HANDLER demo_ntp_recv_handler This function is called when an NTP service message is received.
- .
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 item Example value Description AIOT_NTPOPT_EVENT_HANDLER demo_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.
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
./demos/ntp-posix-demo.
For more information, see Compile and run.