OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4623|回复: 0

nrf52832 ble-lbs.c说明

[复制链接]

79

主题

395

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1766
金钱
1766
注册时间
2015-9-21
在线时间
544 小时
发表于 2021-11-24 21:02:24 | 显示全部楼层 |阅读模式
  1. /**
  2. * Copyright (c) 2013 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. *    list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. *    Semiconductor ASA integrated circuit in a product or a software update for
  14. *    such product, must reproduce the above copyright notice, this list of
  15. *    conditions and the following disclaimer in the documentation and/or other
  16. *    materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. *    contributors may be used to endorse or promote products derived from this
  20. *    software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. *    Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. *    engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(BLE_LBS)
  42. #include "ble_lbs.h"
  43. #include "ble_srv_common.h"


  44. /**@brief Function for handling the Write event.函数处理Write事件。
  45. *
  46. * @param[in] p_lbs      LED Button Service structure.LED按键业务结构。
  47. * @param[in] p_ble_evt  Event received from the BLE stack.从BLE堆栈接收到的事件
  48. */
  49. //处理Write事件的函数。该事件来自主机(GATTC)的Write写特征值
  50. //p_led -> led服务结构体
  51. //p_ble_evt -> ble事件
  52. static void on_write(ble_lbs_t * p_lbs, ble_evt_t const * p_ble_evt)
  53. {
  54.     ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  55.         //参数判断和筛选
  56.     if (   (p_evt_write->handle == p_lbs->led_char_handles.value_handle)
  57.         && (p_evt_write->len == 1)
  58.         && (p_lbs->led_write_handler != NULL))
  59.     {
  60.                         //调用mainc中service_init函数设置的处理函数。并传递从无线端接收到的数据。
  61.         p_lbs->led_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_lbs, p_evt_write->data[0]);
  62.     }
  63. }

  64. //BLE事件处理函数
  65. //p_ble_evt -> ble事件
  66. //p_context -> ble事件处理程序的参数(暂时理解应该是不同的功能,注册时所携带的结构体参数)
  67. void ble_lbs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  68. {
  69.     ble_lbs_t * p_lbs = (ble_lbs_t *)p_context;

  70.     switch (p_ble_evt->header.evt_id)
  71.     {
  72.                         // GATTClient的Write事件,本从机里对应为GATTS(Server端)
  73.         case BLE_GATTS_EVT_WRITE://产生GATT写事件
  74.             on_write(p_lbs, p_ble_evt);
  75.             break;

  76.         default:
  77.             // No implementation needed.
  78.             break;
  79.     }
  80. }


  81. /**@brief Function for adding the LED Characteristic.增加LED特性功能。
  82. *
  83. * @param[in] p_lbs      LED Button Service structure.
  84. * @param[in] p_lbs_init LED Button Service initialization structure.
  85. *
  86. * @retval NRF_SUCCESS on success, else an error value from the SoftDevice
  87. */
  88. static uint32_t led_char_add(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
  89. {
  90.     ble_gatts_char_md_t char_md;
  91.     ble_gatts_attr_t    attr_char_value;
  92.     ble_uuid_t          ble_uuid;
  93.     ble_gatts_attr_md_t attr_md;

  94.     memset(&char_md, 0, sizeof(char_md));

  95.     char_md.char_props.read  = 1;
  96.     char_md.char_props.write = 1;
  97.     char_md.p_char_user_desc = NULL;
  98.     char_md.p_char_pf        = NULL;
  99.     char_md.p_user_desc_md   = NULL;
  100.     char_md.p_cccd_md        = NULL;
  101.     char_md.p_sccd_md        = NULL;

  102.     ble_uuid.type = p_lbs->uuid_type;
  103.     ble_uuid.uuid = LBS_UUID_LED_CHAR;

  104.     memset(&attr_md, 0, sizeof(attr_md));

  105.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
  106.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
  107.     attr_md.vloc    = BLE_GATTS_VLOC_STACK;
  108.     attr_md.rd_auth = 0;
  109.     attr_md.wr_auth = 0;
  110.     attr_md.vlen    = 0;

  111.     memset(&attr_char_value, 0, sizeof(attr_char_value));

  112.     attr_char_value.p_uuid    = &ble_uuid;
  113.     attr_char_value.p_attr_md = &attr_md;
  114.     attr_char_value.init_len  = sizeof(uint8_t);
  115.     attr_char_value.init_offs = 0;
  116.     attr_char_value.max_len   = sizeof(uint8_t);
  117.     attr_char_value.p_value   = NULL;
  118. //调用特征添加方法,把我们的特征添加到服务里面去,所以这里的第一个参数为我们建立的服务的句柄,上面得到的
  119. //第2个参数为我们为特征配置的一些基本信息,第3个参数为返回值,得到的是特征的句柄,保存到我们的结构体中。
  120.     return sd_ble_gatts_characteristic_add(p_lbs->service_handle,
  121.                                            &char_md,
  122.                                            &attr_char_value,
  123.                                            &p_lbs->led_char_handles);
  124. }


  125. /**@brief Function for adding the Button Characteristic.
  126. *
  127. * @param[in] p_lbs      LED Button Service structure.
  128. * @param[in] p_lbs_init LED Button Service initialization structure.
  129. *
  130. * @retval NRF_SUCCESS on success, else an error value from the SoftDevice
  131. */
  132. static uint32_t button_char_add(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
  133. {
  134.     ble_gatts_char_md_t char_md;
  135.     ble_gatts_attr_md_t cccd_md;
  136.     ble_gatts_attr_t    attr_char_value;
  137.     ble_uuid_t          ble_uuid;
  138.     ble_gatts_attr_md_t attr_md;

  139.     memset(&cccd_md, 0, sizeof(cccd_md));

  140.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
  141.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
  142.     cccd_md.vloc = BLE_GATTS_VLOC_STACK;

  143.     memset(&char_md, 0, sizeof(char_md));

  144.     char_md.char_props.read   = 1;
  145.     char_md.char_props.notify = 1;
  146.     char_md.p_char_user_desc  = NULL;
  147.     char_md.p_char_pf         = NULL;
  148.     char_md.p_user_desc_md    = NULL;
  149.     char_md.p_cccd_md         = &cccd_md;
  150.     char_md.p_sccd_md         = NULL;

  151.     ble_uuid.type = p_lbs->uuid_type;
  152.     ble_uuid.uuid = LBS_UUID_BUTTON_CHAR;

  153.     memset(&attr_md, 0, sizeof(attr_md));

  154.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
  155.     BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
  156.     attr_md.vloc    = BLE_GATTS_VLOC_STACK;
  157.     attr_md.rd_auth = 0;
  158.     attr_md.wr_auth = 0;
  159.     attr_md.vlen    = 0;

  160.     memset(&attr_char_value, 0, sizeof(attr_char_value));

  161.     attr_char_value.p_uuid    = &ble_uuid;
  162.     attr_char_value.p_attr_md = &attr_md;
  163.     attr_char_value.init_len  = sizeof(uint8_t);
  164.     attr_char_value.init_offs = 0;
  165.     attr_char_value.max_len   = sizeof(uint8_t);
  166.     attr_char_value.p_value   = NULL;

  167.     return sd_ble_gatts_characteristic_add(p_lbs->service_handle,
  168.                                            &char_md,
  169.                                            &attr_char_value,
  170.                                            &p_lbs->button_char_handles);
  171. }

  172. //主要来看ble_lbs_init,此函数目的是增加led及button的相关Service Characteristic等
  173. uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
  174. {
  175.     uint32_t   err_code;
  176.     ble_uuid_t ble_uuid;

  177.     // Initialize service structure.初始化服务結構體
  178.     p_lbs->led_write_handler = p_lbs_init->led_write_handler;

  179.     // Add service.添加服务(128bit UUID)
  180.     ble_uuid128_t base_uuid = {LBS_UUID_BASE};
  181.                 //这个函数的功能是得到要给UUID的类型,写法基本上是固定的,第二个参数是得到UUDI_TYPE的类型
  182.     err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);
  183.     VERIFY_SUCCESS(err_code);

  184.     ble_uuid.type = p_lbs->uuid_type;
  185.     ble_uuid.uuid = LBS_UUID_SERVICE;
  186.                 //注册服务,作为主服务,传入我们刚才生成的UUID结构体,第三个参数作为返回的服务的句柄,我们保存到们的m_led结构体中
  187.     err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);
  188.     VERIFY_SUCCESS(err_code);

  189.     // Add characteristics.添加特征值
  190.     err_code = button_char_add(p_lbs, p_lbs_init);
  191.     VERIFY_SUCCESS(err_code);

  192.     err_code = led_char_add(p_lbs, p_lbs_init);
  193.     VERIFY_SUCCESS(err_code);

  194.     return NRF_SUCCESS;
  195. }


  196. uint32_t ble_lbs_on_button_change(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t button_state)
  197. {
  198.     ble_gatts_hvx_params_t params;
  199.     uint16_t len = sizeof(button_state);

  200.     memset(¶ms, 0, sizeof(params));
  201.     params.type   = BLE_GATT_HVX_NOTIFICATION;
  202.     params.handle = p_lbs->button_char_handles.value_handle;
  203.     params.p_data = &button_state;
  204.     params.p_len  = &len;

  205.     return sd_ble_gatts_hvx(conn_handle, ¶ms);
  206. }
  207. #endif // NRF_MODULE_ENABLED(BLE_LBS)
复制代码


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-25 21:04

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表