蓝牙芯片----BK3431开发笔记------RW stack中添加自定义服务教程(4)

简介: 蓝牙芯片----BK3431开发笔记------RW stack中添加自定义服务教程(4)

一、实现自定义服务


在 RW 协议栈协议栈中添加一个自定义服务需要实现 6 个文件,分别为:

custom.c

custom.h

custom_task.c

custom_task.h

app_custom.c

app_custom.h


在 sdk\ble_stack\common\profiles\custom。将custom.h、custom_task.h文件放在api目录下,custom.c、custom_task.c两个文件放在src目录下。将app_custom.c与app_custom.h存放在projects\ble\ble_app_gatt\app\下。

本文档以 custom 服务为例介绍了如何添加一个服务到 RW 的详细步骤。

1. sdk\ble_stack\common\profiles\custom 目录如下:

image.png

2. projects\ble_app_gatt\app 目录如下:

image.png


二、添加 service 到工程中

1. 使用 keil 5.12 打开对应工程,在工程目录 profile 目录下添加指定 profile 相关源文

件,本例中是 custom.c 和 custom_task.c,如下图:

image.png


2. 在 keil C/C++选项卡下面添加对应 profile 的路径,如下:

image.png

3. 在工程目录 app 目录下添加指定自定义服务在应用层文件:

image.png

4. 在 app_task.c 文件中找到 appm_msg_handler 函数,在其中自定义服务获取 handler 的

函数,如下:

case (TASK_ID_CUSTOM):
        {
            // Call the custom Modules
             msg_pol = appm_get_handler(&app_custom_table_handler, msgid, param, src_id);
        } break;

5. 在 app.c 文件中的服务列表中添加自定义服务,如下:

/// List of service to add in the database
enum appm_svc_list
{
    APPM_SVC_CUSTOM,
    APPM_SVC_DIS,
    APPM_SVC_BATT,
  APPM_SVC_OADS,
    APPM_SVC_LIST_STOP ,
};


6. 在 app.c 文件中的函数列表中增加往 db 添加自定义服务的函数,如下:

/// List of functions used to create the database
static const appm_add_svc_func_t appm_add_svc_func_list[APPM_SVC_LIST_STOP] =
{
    (appm_add_svc_func_t)app_custom_add_custom,
    (appm_add_svc_func_t)app_dis_add_dis,
    (appm_add_svc_func_t)app_batt_add_bas,
    (appm_add_svc_func_t)app_oad_add_oads,
};


7. 在 app.c 文件中找到 appm_init 函数,在其中添加 app_custom_init()函数,如下

image.png

8. 在 rwip_task.h 文件中增加自定义服务的 task_id,如下:

image.png

9. 在 prf.c 文件中添加 custom_prf_itf_get()的调用:

image.png

10. 在 prf.c 文件中添加 custom_prf_itf_get()的声明:

image.png

11. 在 rwprf_config.h 文件中增加如下定义:

image.png

BLE_CUSTOM_SERVER 这 个 宏 定 义 在 custom.c 、 custom.h 、 custom_task.c 、 custom_task.h 中都有用到,只有当这个宏被打开后,编译器在编译的时候才能把 自定义的服务编译进去。部分截图如下

image.png

以上步骤全部完成之后,整体编译 project,使用 lightblue 连接进行验证是否添 加成功。

三、app_custom.c 接口说明:

度 备注


1. custom 服务实现了两个特征,其 UUID 及相关属性如下表所示


image.png


2. 数据发送 API

image.png

例如:

//send data api
void app_fff1_send_lvl(uint8_t* buf, uint8_t len)
{
    if(app_fff0_env.send_status==0)
    {
        app_fff0_env.send_status=1;
        // Allocate the message
        struct fff0s_fff1_level_upd_req * req = KE_MSG_ALLOC(FFF0S_FFF1_LEVEL_UPD_REQ,
                                                            prf_get_task_from_id(TASK_ID_FFF0S),
                                                            TASK_APP,
                                                            fff0s_fff1_level_upd_req);
        // Fill in the parameter structure  
        req->length = len;
      memcpy(req->fff1_level, buf, len);
        // Send the message
        ke_msg_send(req);
    }
}



3. 数据接收回调

image.png

例如:

//数据接收回调函数
static int fff2_writer_req_handler(ke_msg_id_t const msgid,
                                     struct fff0s_fff2_writer_ind *param,
                                     ke_task_id_t const dest_id,
                                     ke_task_id_t const src_id)
{
    // Drop the message
  UART_PRINTF("FFF2 param->value = 0x ");
  for(uint8_t i = 0;i < param->length;i++)
  {
  UART_PRINTF("%02x ",param->fff2_value[i]);
  }
  UART_PRINTF("\r\n");
    return (KE_MSG_CONSUMED);
}


4. 数据发送状态回复

image.png

static int fff1_level_upd_handler(ke_msg_id_t const msgid,
                                      struct fff0s_fff1_level_upd_rsp const *param,
                                      ke_task_id_t const dest_id,
                                      ke_task_id_t const src_id)
{
  if(param->status == GAP_ERR_NO_ERROR)
  {
  //uint8_t buf[128];
  //memset(buf, 0xcc, 128);
  //app_fff1_send_lvl(buf, 128);
  app_fff0_env.send_status=0;
  }
    return (KE_MSG_CONSUMED);
}


5. Notify 成功使能回调


static int fff0s_fff1_level_ntf_cfg_ind_handler(ke_msg_id_t const msgid,
                                               struct fff0s_fff1_level_ntf_cfg_ind const *param,
                                               ke_task_id_t const dest_id,
                                               ke_task_id_t const src_id)
{
  UART_PRINTF("param->ntf_cfg = %x\r\n",param->ntf_cfg);
  if(param->ntf_cfg == PRF_CLI_STOP_NTFIND)
  {
  //ke_timer_clear(FFF0S_FFF1_LEVEL_PERIOD_NTF,dest_id);
  }else
  {
  //ke_timer_set(FFF0S_FFF1_LEVEL_PERIOD_NTF,dest_id , 1);
  }
    return (KE_MSG_CONSUMED);
}


为了系统的稳定,应用层发送 notify 到手机端时需要在发送完成一包数据之后再 触发下一次的发送。

 

目录
相关文章
|
Linux
Linux驱动学习-----最简单的Hello程序
Linux驱动学习-----最简单的Hello程序
158 0