主要思路类似原子的SMUSART(我还真没有看原子是怎么做的)
第一步:节点
typedef struct _AppHandleArry
{
uint8_t Id;
uint8_t (*EventHandlerFn)(BleAppMsgType *pMsg);
}AppHandleArryType;
一个ID号 对应一个函数
一一对应的 就像命令行一样。
函数的名字是EventHandlerFn 需要的参数是一个如下的结构体指针
typedef struct _BleAppMsgType
{
uint8_t FormAddr[6];
uint16_t Handle;
uint8_t WriteType;
uint8_t Data[350];
uint16_t DataLength;
uint8_t Flag;
}BleAppMsgType;
第二步:所有服务列表
AppHandleArryType gAppTaskArry[]=
{
{0x09 , BleAppPairUserNotModify},
{0x08 , BleAppPairUserModify},
{0x05 , BleGetOpenDoorCmd},
{0x22 , BleModifyUserPwd},
{0x01 , BleModifyPairPwd},
{0x89 , BleRestoreFactory},
{0x03 , BleGetOpenRecord},
{0x44 , BleSetDeviceInitInfo},
{0x80 , BleSetFuKaiDeviceInitInfo},
{0x45 , BleSetLadderInitInfo},
{0x43 , BleGetDeviceInitInfo},
{0x49 , BleSetNfcMsg},
{0x88 , BleGetWecatMac},
{0x41 , BleSetIbeaconRssi},
//{0x40 , BleGetNetStatus},
{0x85 , BleGetLadderStatus},
{0x86 , BleSetLadderStatus},
{0x48 , BleSetWifiInfo},
{0x40 , BleGetDeviceStatusInfo},
{0x46 , BleSetDeviceIp},
{0x61 , BleSetDeviceNetIp},
{0x62 , BleSetDevicePos},
{0x63 , BleSetDeviceMqttKey},
{0x64 , BleSetDeviceMqttRsa},
{0x65 , BleSetNfcMsg},
};
const uint8_t gAppTasksCnt = sizeof (gAppTaskArry) / sizeof (gAppTaskArry[0]);
好了 现在就一一对应好了 是一个数组
第三部:用起来
一直在轮训,过来的参数,拿到他的pMsg->Data[0] 在数组中轮训 如果和数组某一个节点的ID一样的话
那就成功了,启动服务。
static uint8_t BleApplicationHandle(const BleAppMsgType *pMsg)
{
uint8_t idx =0 ;
for(idx = 0; idx < gAppTasksCnt; idx++)
{
if( pMsg->Data[0] == gAppTaskArry[idx].Id)
{
log(DEBUG,"Bt receive command id = %x.\r\n" , gAppTaskArry[idx].Id);
if(gAppTaskArry[idx].EventHandlerFn != NULL)
{
return (gAppTaskArry[idx].EventHandlerFn((BleAppMsgType*)pMsg));
}
else
{
log(DEBUG,"This command has no callback , CMD = %x.\r\n" , pMsg->Data[0]);
return APP_NO_COMMAND_ERR;
}
}
}
log(DEBUG,"This command has no register. cmd = %x.\r\n" ,pMsg->Data[0]);
return APP_NO_COMMAND_ERR;
}
后面的任务就是写好每一个服务!