Skip to content

Commit

Permalink
Merge pull request #25 from MK16kawai/dev
Browse files Browse the repository at this point in the history
add time::ntp_xxx support, add sync_time example
  • Loading branch information
Neutree authored Sep 11, 2024
2 parents 67cfb3e + ae94a11 commit 00117d8
Show file tree
Hide file tree
Showing 24 changed files with 3,879 additions and 411 deletions.
54 changes: 54 additions & 0 deletions components/3rd_party/ntp_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
################# Add include #################
list(APPEND ADD_INCLUDE "ntp_client")
# list(APPEND ADD_PRIVATE_INCLUDE "include_private")
###############################################

############## Add source files ###############
# list(APPEND ADD_SRCS "src/lib1.c"
# )
aux_source_directory("ntp_client" ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
###############################################

###### Add required/dependent components ######
# list(APPEND ADD_REQUIREMENTS component1)
###############################################

###### Add link search path for requirements/libs ######
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
# list(APPEND ADD_REQUIREMENTS m) # add system libs, pthread or m(math) lib for example
###############################################

############ Add static libs ##################
# if(CONFIG_COMPONENT1_INCLUDE_STATIC_LIB)
# list(APPEND ADD_STATIC_LIB "lib/libtest.a")
# endif()
###############################################

############ Add dynamic libs ##################
# list(APPEND ADD_DYNAMIC_LIB "lib/arch/v831/libmaix_nn.so"
# "lib/arch/v831/libmaix_cam.so"
# )
###############################################

#### Add compile option for this component ####
#### Just for this component, won't affect other
#### modules, including component that depend
#### on this component
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAAA=1)

#### Add compile option for this component
#### and components denpend on this component
# list(APPEND ADD_DEFINITIONS -DAAAAA222=1
# -DAAAAA333=1)
###############################################

############ Add static libs ##################
#### Update parent's variables like CMAKE_C_LINK_FLAGS
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
###############################################

# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
register_component()

Empty file.
File renamed without changes.
2 changes: 1 addition & 1 deletion components/basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ endif()


###### Add required/dependent components ######
list(APPEND ADD_REQUIREMENTS pthread stdc++fs ini yaml)
list(APPEND ADD_REQUIREMENTS pthread stdc++fs ini yaml ntp_client)
###############################################

###### Add link search path for requirements/libs ######
Expand Down
98 changes: 98 additions & 0 deletions components/basic/include/maix_time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,5 +420,103 @@ namespace maix::time
*/
std::map<std::string, std::vector<std::string>> list_timezones();


/**
* @brief Retrieves time from an NTP server
*
* This function fetches the current time from the specified NTP server and port,
* returning a tuple containing the time details.
*
* @param host The hostname or IP address of the NTP server.
* @param port The port number of the NTP server. Use -1 for the default port 123.
* @param retry The number of retry attempts. Must be at least 1.
* @param timeout_ms The timeout duration in milliseconds. Must be non-negative.
* @return A list of 6 elements: [year, month, day, hour, minute, second]
*
* @maixpy maix.time.ntp_timetuple
*/
std::vector<int> ntp_timetuple(std::string host, int port=-1, uint8_t retry=3, int timeout_ms=0);

/**
* @brief Retrieves time from an NTP server using a configuration file
*
* This function reads the configuration from a YAML file to fetch the current time
* from a list of specified NTP servers, returning a tuple containing the time details.
*
* @param path The path to the YAML configuration file, which should include:
* - Config:
* - retry: Number of retry attempts (must be at least 1)
* - total_timeout_ms: Total timeout duration in milliseconds (must be non-negative)
* - NtpServers:
* - host: Hostname or IP address of the NTP server
* - port: Port number of the NTP server (use 123 for default)
*
* Example YAML configuration:
* Config:
* - retry: 3
* - total_timeout_ms: 10000
*
* NtpServers:
* - host: "pool.ntp.org"
* port: 123
* - host: "time.nist.gov"
* port: 123
* - host: "time.windows.com"
* port: 123
*
* @return A list of 6 elements: [year, month, day, hour, minute, second]
*
* @maixpy maix.time.ntp_timetuple_with_config
*/
std::vector<int> ntp_timetuple_with_config(std::string path);

/**
* @brief Retrieves time from an NTP server and synchronizes the system time
*
* This function fetches the current time from the specified NTP server and port,
* then synchronizes the system time with the retrieved time.
*
* @param host The hostname or IP address of the NTP server.
* @param port The port number of the NTP server. Use 123 for the default port.
* @param retry The number of retry attempts. Must be at least 1.
* @param timeout_ms The timeout duration in milliseconds. Must be non-negative.
* @return A list of 6 elements: [year, month, day, hour, minute, second]
*
* @maixpy maix.time.ntp_sync_sys_time
*/
std::vector<int> ntp_sync_sys_time(std::string host, int port=-1, uint8_t retry=3, int timeout_ms=0);

/**
* @brief Retrieves time from an NTP server using a configuration file and synchronizes the system time
*
* This function reads the configuration from a YAML file to fetch the current time
* from a list of specified NTP servers, then synchronizes the system time with the retrieved time.
*
* @param path The path to the YAML configuration file, which should include:
* - Config:
* - retry: Number of retry attempts (must be at least 1)
* - total_timeout_ms: Total timeout duration in milliseconds (must be non-negative)
* - NtpServers:
* - host: Hostname or IP address of the NTP server
* - port: Port number of the NTP server (use 123 for default)
*
* Example YAML configuration:
* Config:
* - retry: 3
* - total_timeout_ms: 10000
*
* NtpServers:
* - host: "pool.ntp.org"
* port: 123
* - host: "time.nist.gov"
* port: 123
* - host: "time.windows.com"
* port: 123
*
* @return A vector of integers containing the time details: [year, month, day, hour, minute, second]
*
* @maixpy maix.time.ntp_sync_sys_time_with_config
*/
std::vector<int> ntp_sync_sys_time_with_config(std::string path);
}

Loading

0 comments on commit 00117d8

Please sign in to comment.