尝试创建一个简单的BLE服务来用日志消息通知

⚠️
嗨,...感谢您来论坛。令人兴奋的消息!我们现在正在迁至我们的新论坛平台,将提供更好的功能,并包含在主对话框网站中。所有帖子和帐户都已迁移。我们现在只接受新论坛上的流量 - 请发布任何新线程https://www.dialog-seminile.com/support.。我们将在未来几天修复错误/优化搜索和标记。
3个帖子/ 0新
最后一篇
alarner.
离线
最后一次露面:2个月2周前
加入:2017-05-27 02:11
尝试创建一个简单的BLE服务来用日志消息通知

Hello, I'm trying to create a simple BLE service that I can use to log messages to a mobile client. I'm working from the cts service but trying to simplify it. I've included the code below. The issue that I'm running into is that when I connect to the peripheral via my phone I do not see the service defined below. I do however see all of the other services that I've set up (device information, immediate alert, link loss, tx power, battery service, etc). What am I missing?

log_service.h.

#include“osal.h”
#include“ble_service.h”
#包括"ble_uuid.h"

#define uuid_service_log“6c8f78d9-292e-4b0d-b583-1616a777edbb”
#define uuid_current_log“b8ce3c4f-3ea0-47f3-8fb-f48732f99ba9”

typedef struct {
ble_service_t svc;

//处理
uint16_t log_val_h;//当前日志值
log_service_t;

ble_service_t * ls_init()
{
log_service_t * logservice;
返回&logservice-> svc;
uint16_t num_attr;
ATT_UUID_T UUID;

logservice = os_malloc(sizeof(* logservice));
memset(logservice,0,sizeof(* logservice));

/ *
*日志值的一个特征
* /
num_attr = ble_gatts_get_num_attr(0,1,0);

BLE_UUID_CREATE16(UUID_SERVICE_LOG,&UUID);
ble_gatts_add_service(&uuid,gatt_service_primary,num_attr);

// write是可选的,如果set_time_cb不为null使特性读/写
BLE_UUID_CREATE16(UUID_CURRENT_LOG,&UUID);
BLE_GATTS_ADD_CHAROURATIC(&UUID,GATT_PROP_READ | GATT_PROP_NOTIFY,
ATT_PERM_READ,512,
gatts_flag_char_read_req,null,&logservice-> log_val_h);

ble_gatts_register_service(&logservice-> svc.start_h,&logservice-> log_val_h,0);
logservice-> svc.end_h = logservice-> svc.start_h + num_attr;

ble_service_add(&logservice-> svc);

返回&logservice-> svc;
}

bluetooth_task.c.

......
ble_service_t * logservice = ls_init();
......

设备:
alarner.
离线
最后一次露面:2个月2周前
加入:2017-05-27 02:11
划伤。我想了

划伤。我想到了。对于尝试做类似的人来说,这是我可以提出的最赤裸的骨骼代码:


#包括
#包括
#include“osal.h”
#include“ble_att.h”
#include“ble_bufops.h”
#include“ble_common.h”
#include“ble_gatts.h”
#包括"ble_uuid.h"
#include“ble_storage.h”
#include“ble_service.h”

#define uuid_service_log“您的服务UUID”
#define uuid_charactery_log“你的特征uuid”

static const char log_desc [] =“日志数据”;

typedef struct {
ble_service_t svc;
uint16_t log_val_h;// 当前时间
uint16_t log_ccc_h;
log_service_t;

静态void清理(ble_service_t * svc)
{
log_service_t * ls =(log_service_t *)svc;

ble_storage_remove_all(ls-> log_val_h);
ble_storage_remove_all(ls-> log_ccc_h);

OS_FREE(SVC);
}

staticat att_error_t handle_log_ccc_write(log_service_t * ls,uint16_t conn_idx,
UINT16_T偏移,UINT16_T长度,CONSS UINT8_T *值)
{
UINT16_T CCC;

if(offset){
returnat att_error_attribute_not_long;
}

if(length!= sizeof(ccc)){
return ATT_ERROR_APPLICATION_ERROR;
}

ccc = get_u16(价值);

ble_storage_put_u32(conn_idx,ls-> log_cc_h,ccc,true);

返回att_error_ok;
}

静态void handle_write_req(ble_service_t * svc,const ble_evt_gatts_write_req_t * evt)
{
log_service_t * ls =(log_service_t *)svc;
ATT_ERROR_T Status = ATT_ERROR_ATTRIBUTE_NOT_FOUND;
uint16_t handle = evt->手柄;

if(handle == ls-> log_ccc_h){
status = handle_log_ccc_write(ls,evt-> conn_idx,EVT->偏移,EVT->长度,EVT-> Value);
}

ble_gatts_write_cfm(evt->conn_idx, evt->handle, status);
}

ble_service_t * log_init()
{
log_service_t * ls;
uint16_t num_attr,log_desc_h;
ATT_UUID_T UUID;

ls = os_malloc(sizeof(* ls));
memset(ls,0,sizeof(* ls));

num_attr = ble_gatts_get_num_attr(0, 1, 2);

BLE_UUID_FROM_STRING(UUID_SERVICE_LOG,&UUID);
ble_gatts_add_service(&uuid,gatt_service_primary,num_attr);

BLE_UUID_FROM_STRING(UUID_CHAROURATIC_LOG,&UUID);
ble_gatts_add_Charactery(
&Uuid,
gatt_prop_read |gatt_prop_notify,
att_perm_read,
512,
0,
NULL,
&ls-> log_val_h
);

ble_uuid_create16(UUID_GATT_CLIENT_CHAR_CONFIGURATION, &uuid);
ble_gatts_add_descriptor(&uuid,att_perm_rw,2,0,&ls-> log_ccc_h);

BLE_UUID_CREATE16(UUID_GATT_CHAR_USER_DESCRIPTION,&UUID);
ble_gatts_add_descriptor(&uuid,att_perm_read,sizeof(log_desc),0,&log_desc_h);

ble_gatts_register_service(&ls-> svc.start_h,&ls-> log_val_h,&ls-> log_ccc_h,&log_desc_h,0);

ble_gatts_set_value(ls-> log_val_h,4,“test”);
ble_gatts_set_value(log_desc_h,sizeof(log_desc),log_desc);

ls-> svc.cleanup =清理;
ls-> svc.end_h = ls-> svc.start_h + num_attr;
ls-> svc.write_req = handle_write_req;

ble_service_add(&ls->svc);

return &ls->svc;
}

void log_notify(ble_service_t * svc,uint16_t conn_idx,uint8_t * data,uint16_t len){
log_service_t * logservice =(log_service_t *)svc;

ble_gatts_send_event(conn_idx,logservice-> log_val_h,gatt_event_notification,len,data);
}

PM_DIALOG.
离线
最后一次露面:6小时1分钟前
职员
加入:2018-02-08 11:03
嗨alarner,

嗨alarner,

很高兴你识上你的问题。感谢您的指示,谢谢您接受对其他客户的非常有用的答案。

谢谢,PM_DIALOG.