当前文档版本为 v0.7.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

如何添加 ANT+ Profile

1 概述

ANT+是基于各个profile协议实现的。本文档旨在介绍如何在SDK中自行添加一个profile。从而配合上层应用实现一个ANT+的设备。

2 Profile的代码结构

profile_layer

图 2-1 Profile code structure

3 创建一个新的profile

3.1 需要创建那些目录和文件

根据上图的结构层次可以看出我们实现一个profile其实就是实现中间层的三个部分。

  • Page:page是ANT+发送数据的基本单元。每个profile都规定了需要发送那些page,所以我们需要创建page目录和文件。

  • Simulator:simulator是一个传感器模拟器,这是一个可选单元,实际应用中可以不实现本单元,完全由实际传感器逻辑代替。

  • Profile:Profile单元是整个profile实现的关键逻辑实现。

小技巧

Profile的目录和文件创建可以参考现有的profile。

代码目录:modules/hal/panchip/panplat/pan1080/ant/ant_profile

注:因系统限制原因,新加文件时文件名不要过长,否则会引起编译错误。

3.2 代码文件加入cmake

新添加的代码文件需要加入ant/CMakeLists.txt才会参与编译,zephyr_include_directories添加头文件所在目录,zephyr_library_sources添加新增的代码文件

zephyr_include_directories(
  ant_profile/ant_hrm/pages
  ant_profile/ant_hrm/simulator
)

zephyr_library_sources(
  ant_profile/ant_hrm/ant_hrm.c
  ant_profile/ant_hrm/simulator/ant_hrm_simulator.c
  ant_profile/ant_hrm/pages/ant_hrm_page_0.c
  ant_profile/ant_hrm/pages/ant_hrm_page_1.c
  ant_profile/ant_hrm/pages/ant_hrm_page_2.c
  ant_profile/ant_hrm/pages/ant_hrm_page_3.c
  ant_profile/ant_hrm/pages/ant_hrm_page_4.c
)

3.3 各部分分别需要实现什么

  1. Page单元

    Page单元将实现协议规定的所有必须page的编码和解码,例如每个page都应该实现如下两个接口。

    /**
     * @brief Function for encoding page 1
     *
     * @param[in] p_page_data        Pointer to the page data
     * @param[out] P_page_buffer     Pointer to the page buffer
     */
    void ant_bsc_page_1_encode(uint8_t *p_page_buffer, ant_bsc_page1_data_t *p_page_data);
    
    /**
     * @brief Function for decoding page 1
     *
     * @param[in] p_page_buffer      Pointer to the page buffer
     * @param[out] p_page_data       Pointer to the page data
     */
    void ant_bsc_page_1_decode(uint8_t const *p_page_buffer, ant_bsc_page1_data_t *p_page_data);
    
  2. Simulator单元

    Simulator单元将实现传感器采集数据的自动更新。例如每个simulator都应该实现如下数据更新接口。

    /**
     * @brief Function for updateing data of simulator.
     *
     * @param bsc_profile            Pointer to the bsc profile instance.
     * @param update_mode            update mode, 0 - manual, 1 - auto
     */
    void ant_bsc_update_simulator(ant_bsc_profile_t *bsc_profile, uint8_t update_mode);
    
  3. Profile单元

    Profile单元将实现整个profile的核心逻辑。需要实现的接口函数如下:

    • 数据发送事件的接口函数,此函数将由应用层在需要数据发送时调用。同时需要实现相关联的一系列page选择,发送编码等函数。

      /**
       * @brief Function for handling the sensor ANT events.
       *
       * @param[in] p_ant_evt    Event received from the ANT stack.
       * @param[in] p_context    Pointer to the profile instance.
       */
      void ant_bsc_sensor_evt_handler(ant_evt_t *p_ant_evt, void *p_context);
      
    • profile的初始化接口函数,此函数实现了相关初始值的赋值以及指定通道的初始化。

      /**
       * @brief Function for bsc sensor initialize.
       *
       * @param[in] profile            Pointer to the profile instance.
       * @param[in] channel_config     Pointer to the channel configuration structure.
       * @return uint8_t  0 SUCCESS, other FAIL
       */
      uint8_t ant_bsc_sensor_init(ant_bsc_profile_t *profile, ant_channel_config_t *channel_config);
      
    • profile的开启接口函数,此函数实现了指定通道的开启。

      /**
       * @brief Function for openqing the profile instance channel for ANT BSC sersor.
       *
       * @param[in] profile       Pointer to the profile instance.
       * @return uint8_t   0 SUCCESS, other FAIL
       */
      uint8_t ant_bsc_sensor_open(ant_bsc_profile_t *profile);
      

    小技巧

    详细的代码实现请参考目前已实现的profile源码。