CH592/CH582/CH573/CH579服务修改

发布时间 2023-09-26 15:39:15作者: BLE_Baby

原文链接:

https://www.cnblogs.com/frontier/p/16845596.html

 

在对ble系列应用时,很多时候拿手机充当主机。在使用ble 调试助手时常会用到write、read、notify等功能。有时可能会根据自己的需求对这些服务进行修改。下图是官方例程体现出的services。

使用的是沁恒的CH582M,例程是peripheral。

想要对服务进行修改,要从从机抓起。毕竟执行服务的是从机。需要操作的gattprofile.c的文件。

以添加服务为例新增一个notify服务,在UUID为0XFFF2的服务进行修改。要模拟一个服务就要做到神形兼备 ,既有相应的功能选项又要能够实现相应的功能。

#define SIMPLEPROFILE_CHAR2_VALUE_POS 5 
#define SIMPLEPROFILE_CHAR4_VALUE_POS 13 

首先让他先有形,这一步是比较容易的在图中的部分进行修改(根据已经有的notify服务进行修改),修改后的代码为

 

 

static uint8_t simpleProfileChar2Props = GATT_PROP_READ | GATT_PROP_NOTIFY;下载程序后的效果为,但是是无法实现notify的功能的。

 

 

 接下来就要为notify实现功能而努力。继续往下看程序我们会发现关于UUID为0XFFF4的notify服务的配置。经过对比我们会发现具体实现notify的功能的程序在红框中

 

 

 

,

那我们就要对我们新增的notify进行修改

复制代码
// Characteristic 2 Declaration
{
{ATT_BT_UUID_SIZE, characterUUID},
GATT_PERMIT_READ,
0,
&simpleProfileChar2Props},

// Characteristic Value 2
{
{ATT_BT_UUID_SIZE, simpleProfilechar2UUID},
GATT_PERMIT_READ,
0,
simpleProfileChar2},
// rzz
{
{ATT_BT_UUID_SIZE, simpleProfilechar2UUID},
0,
0,
simpleProfileChar2},

// rzz
{
{ATT_BT_UUID_SIZE, clientCharCfgUUID},
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8_t *)simpleProfileChar2Config},

// Characteristic 2 User Description
{
{ATT_BT_UUID_SIZE, charUserDescUUID},
GATT_PERMIT_READ,
0,
simpleProfileChar2UserDesp},
复制代码

 

 到这里就能使用了吗?不,仍需努力。接下来我们要继续浏览gattprofile.c中的程序。

请看好接下来的操作:这里可以发现红框中出现的simpleProfileChar4Config与前面的的相对应,在这里要考虑新增一个

 GATTServApp_InitCharCfg(INVALID_CONNHANDLE, simpleProfileChar2Config)语句,再回头看一下有对simpleProfileChar4Config的定义的全局变量。

 static gattCharCfg_t simpleProfileChar4Config[PERIPHERAL_MAX_CONNECTION];

 static gattCharCfg_t simpleProfileChar2Config[PERIPHERAL_MAX_CONNECTION];

接下来对NOTIFY进行操作,

复制代码

bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti)
{
uint16_t value = GATTServApp_ReadCharCfg(connHandle, simpleProfileChar4Config);
// uint16_t value1 = GATTServApp_ReadCharCfg(connHandle, simpleProfileChar2Config);
// If notifications enabled
if(value & GATT_CLIENT_CFG_NOTIFY)
{
// Set the handle
pNoti->handle = simpleProfileAttrTbl[SIMPLEPROFILE_CHAR4_VALUE_POS].handle;
// Send the notification
return GATT_Notification(connHandle, pNoti, FALSE);
}
// if(value1 & GATT_CLIENT_CFG_NOTIFY)
// {
// // Set the handle
// pNoti->handle = simpleProfileAttrTbl[SIMPLEPROFILE_CHAR2_VALUE_POS].handle; //rzz
// // Send the notification
// return GATT_Notification(connHandle, pNoti, FALSE);
// }
return bleIncorrectMode;
}

bStatus_t newsimpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti)//rzz
{
uint16_t value1 = GATTServApp_ReadCharCfg(connHandle, simpleProfileChar2Config);
if(value1 & GATT_CLIENT_CFG_NOTIFY)
{
// Set the handle
pNoti->handle = simpleProfileAttrTbl[SIMPLEPROFILE_CHAR2_VALUE_POS].handle; //rzz
// Send the notification
return GATT_Notification(connHandle, pNoti, FALSE);
}

return bleIncorrectMode;
}

复制代码

 

复制代码
 

static void simpleProfile_HandleConnStatusCB(uint16_t connHandle, uint8_t changeType)
{
// Make sure this is not loopback connection
if(connHandle != LOOPBACK_CONNHANDLE)
{
// Reset Client Char Config if connection has dropped
if((changeType == LINKDB_STATUS_UPDATE_REMOVED) ||
((changeType == LINKDB_STATUS_UPDATE_STATEFLAGS) &&
(!linkDB_Up(connHandle))))
{
GATTServApp_InitCharCfg(connHandle, simpleProfileChar4Config);
GATTServApp_InitCharCfg(connHandle, simpleProfileChar2Config);//rzz
}
}
}

 
复制代码

 

更改到此处notify的功能已经可以用了,根据自己的需求在peripheral.c中进行修改,在notify中接收相应的数据。

 

 

 

补充:这里是在原有的其他服务的位置新增加的noti如果自己新增服务的话,关键的一点就是要开启CCCD:

gattServiceCBs_t simpleProfileCBs = {
    simpleProfile_ReadAttrCB,  // Read callback function pointer
    simpleProfile_WriteAttrCB, // Write callback function pointer
    NULL                       // Authorization callback function pointer
};
           case GATT_CLIENT_CHAR_CFG_UUID:
                status = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue, len,
                                                        offset, GATT_CLIENT_CFG_NOTIFY);
                break;

 

 这只是最基础的修改,如有问题请指正!

 如转载请标明出处!之前的文章可能被无良网站搬运,之前是初稿有些问题。某些网站拿着别人的文章写着“我的编程学习分享”。

禁止写着我的编程学习分享的网站转载。