NTP Development Guide_Rev1.2

中文

Revision History

Version

Date

Author

Reviewer

Revision Content

Rev1.0

2023-09-19

LJZ

zlc

Initial document creation

Rev1.1

2024-03-25

sxx

Changed document name

Rev1.2

2025-05-14

LJZ

Optimized document format

1 Introduction

This document introduces the LTE-EC71X NTP interface APIs. The API interfaces are declared in the file components/kernel/lierda_api/liot_ntp/liot_ntp.h.

2 API Function Overview

Function

Description

liot_ntp_sync()

Enable NTP time synchronization function

3 Type Descriptions

3.1 liot_ntp_sync_option

NTP configuration information.

  1. Declaration

typedef struct
{
    int pdp_cid;
    int sim_id;
    int retry_cnt;
    int retry_interval_tm;
} liot_ntp_sync_option;
  1. Parameters

Type

Parameter

Description

int

pdp_cid

PDP context index

int

sim_id

SIM card index, value: 0-1

int

retry_cnt

Retry count

int

retry_interval_tm

Retry interval time

3.2 liot_ntp_error_code_e

NTP API execution result error codes.

  1. Declaration

typedef enum
{
    LIOT_NTP_SUCCESS                 = 0,
    LIOT_NTP_ERROR_UNKNOWN           = -1,
    LIOT_NTP_ERROR_WODBLOCK          = -2,
    LIOT_NTP_ERROR_INVALID_PARAM     = -3,
    LIOT_NTP_ERROR_OUT_OF_MEM        = -4,
    LIOT_NTP_ERROR_TIMEOUT           = -5,
    LIOT_NTP_ERROR_DNS_FAIL          = -6,
    LIOT_NTP_ERROR_SOCKET_ALLOC_FAIL = -7,
    LIOT_NTP_ERROR_SOCKET_SEND_FAIL  = -8,
    LIOT_NTP_ERROR_SOCKET_RECV_FAIL  = -9,
    LIOT_NTP_ERROR_INVALID_REPLY     = -10
} liot_ntp_error_code_e;
  1. Parameters

  • LIOT_NTP_SUCCESS: NTP synchronization successful.

  • LIOT_NTP_ERROR_UNKNOWN: Unknown error.

  • LIOT_NTP_ERROR_WODBLOCK:

  • LIOT_NTP_ERROR_INVALID_PARAM: Parameter error.

  • LIOT_NTP_ERROR_OUT_OF_MEM: Insufficient memory.

  • LIOT_NTP_ERROR_TIMEOUT: Operation timeout.

  • LIOT_NTP_ERROR_DNS_FAIL: DNS resolution failed.

  • LIOT_NTP_ERROR_SOCKET_ALLOC_FAIL: Socket memory allocation failed.

  • LIOT_NTP_ERROR_SOCKET_SEND_FAIL: Socket send failed.

  • LIOT_NTP_ERROR_SOCKET_RECV_FAIL: Socket receive failed.

  • LIOT_NTP_ERROR_INVALID_REPLY: Reply failed.

4 API Function Details

4.1 liot_ntp_sync_result_cb

NTP synchronization result callback function.

  1. Declaration

typedef void (*liot_ntp_sync_result_cb)(ntp_client_id cli_id, int result, struct tm *sync_time, void *arg);
  1. Parameters

  • p_imei: [In] Context ID number for NTP service information.

  • result: [In] NTP synchronization result.

  • sync_time: [In] Synchronized time.

  • arg: [In] Custom parameter.

4.2 liot_ntp_sync

This function is used to synchronize device time via NTP.

  1. Declaration

ntp_client_id liot_ntp_sync(
    const char *host, liot_ntp_sync_option *user_option, liot_ntp_sync_result_cb cb, void *arg, int *error_code);
  1. Parameters

  • host: [In] NTP server IP address or domain name address, and port number.

  • user_option: [In] Record PDP related information and configuration information for NTP time synchronization, see liot_ntp_sync_option.

  • cb: [In] NTP event callback function, set event type as NTP and judge callback results, see liot_ntp_sync_result_cb.

  • arg: [In] Custom parameter.

  • error_code: [Out] Return error code during NTP service process.

  1. Return Value

  • ntp_client_id: Context ID number recording NTP service information.

5 Code Example

  1. Sample code reference: examples/demo/src/demo_ntp.c file.

  • Running Result:

_images/ntp-guide/image_1.png

As shown in the figure above, it indicates that all information has been obtained normally.