当前位置:网站首页>12. Tencent cloud IOT device side learning -- NTP function and Implementation

12. Tencent cloud IOT device side learning -- NTP function and Implementation

2022-06-24 05:58:00 fancyxu

The main purpose of this series is to record the learning notes of Tencent cloud Internet of things device , And on the device side SDK Make a supplementary explanation .

brief introduction

ntp function

ntp The function is simple uplink and downlink topic Matching functions , The device side subscribes to the downlink topic , Send a message to the uplink topic , Trigger the server to send downlink messages , And process according to the downlink message .

As follows: NTP Function as an example , Introduce how to base on iot-hub-device-c-sdk Realize this function .

Module add

stay services/hub/system/ New modules , Need to achieve

  • src: Module source code , Provide sdk External interface
  • inc: The module uses header files internally , None in this function
  • sample: Sample code , Examples of calling interfaces
  • test: Unit test case writing
  • CMakeLists.txt:cmake Script , Simply set the source code and header file path , Add examples and unit tests ( Later, it can be simplified without modifying the service )

NTP The function mainly implements two interfaces :

  • IOT_Sys_GetTime: This interface is used to get the time from the server , Mainly do the following three things
    • _system_mqtt_result_topic_check_and_sub: Detect downlink topic Subscription status , If you don't subscribe, subscribe
    • _system_mqtt_get_resource_time_publish: Up topic Release the news , To get time
    • _system_mqtt_result_wait: Timeout waiting for server reply
  • IOT_Sys_SyncNTPTime: and IOT_Sys_GetTime similar , The difference is that it passes according to the time before and after receiving the message HAL_Timer_SetSystimeSec and HAL_Timer_SetSystimeMs Set system time .

The source code to achieve ( see system_mqtt.c), The following is a brief description of the development ideas of such services :

  1. Sync subscription : Most services need to ensure the success of the subscription , To ensure the success of the subscription , You can call IOT_MQTT_SubscribeSync
  2. Message processing : The old version of the service will be in mqtt client Add a new member to the structure , Obviously, this is not conducive to code maintenance and subsequent function development , So you can subscribe to the usr_data To pass in the context that needs to be processed by the service , At the same time, we need to usr_data Register the release function of , Because this part follows mqtt client Life cycle exists , When mqtt client Be destroyed when the , It needs to be released usr_data.
  3. Interface encapsulation : Different services require different logic perceived by users , Services identified for such agreements , Generally by SDK encapsulate , Only provide data that users need to be aware of , Such as time .

Add... To the external header file

stay include/services/common Add external header file in qcloud_iot_system.h, It mainly provides external interfaces

Compile add

stay config/settings/iot_hub_setting.cmake Add module compilation in , Simply add the corresponding directory to the compilation , It will automatically detect the CMakeLists.txt

add_subdirectory(${PROJECT_SOURCE_DIR}/services/common/system)
原网站

版权声明
本文为[fancyxu]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/07/20210729204831450w.html