cc2640r2l do master/ host
please give the examples. we need to open Notify , receive notify data , and if connected, the data transfer will be start auto,
// Initialize application
multi_role_init();
// Application main loop
for (;;)
{
uint32_t events;
// Waits for an event to be posted associated with the calling thread.
// Note that an event associated with a thread is posted when a
// message is queued to the message receive queue of the thread
events = Event_pend(syncEvent, Event_Id_NONE, MR_ALL_EVENTS,
ICALL_TIMEOUT_FOREVER);
if (events)
{
ICall_EntityID dest;
ICall_ServiceEnum src;
ICall_HciExtEvt *pMsg = NULL;
if (ICall_fetchServiceMsg(&src, &dest,
(void **)&pMsg) == ICALL_ERRNO_SUCCESS)
{
uint8_t safeToDealloc = TRUE;
if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
{
ICall_Stack_Event *pEvt = (ICall_Stack_Event *)pMsg;
if (pEvt->signature != 0xffff)
{
// Process inter-task message
safeToDealloc = multi_role_processStackMsg((ICall_Hdr *)pMsg);
}
}
if (pMsg && safeToDealloc)
{
ICall_freeMsg(pMsg);
}
}
// If RTOS queue is not empty, process app message.
if (events & MR_QUEUE_EVT)
{
while (!Queue_empty(appMsgQueue))
{
mrEvt_t *pMsg = (mrEvt_t *)Util_dequeueMsg(appMsgQueue);
if (pMsg)
{
// Process message.
multi_role_processAppMsg(pMsg);
// Free the space from the message.
ICall_free(pMsg);
}
}
}
if (events & MR_PERIODIC_EVT)
{
//Util_startClock(&periodicClock);
// drct_doConnect(0);
// Perform periodic application task
//multi_role_performPeriodicTask();
}
if (events & MR_SWITCH_PERIODIC_EVT)
{
//mr_doScan(0);
//Util_startClock(&switchPeriodicClock);
// drct_doGattRw(connIndex);
//delayMs(500);
//drct_rdNotify(connIndex);
}
if(events & MR_DISC_PERIODIC_EVT)
{
// Start service discovery
// multi_role_startDiscovery(connhdl);
}
}
}
static void multi_role_processRoleEvent(gapMultiRoleEvent_t *pEvent)
{
unsigned char conn_times = 3;
unsigned char res = 0xFF;
switch (pEvent->gap.opcode)
{
// GAPRole started
case GAP_DEVICE_INIT_DONE_EVENT:
{
// Store max pdu size
maxPduSize = pEvent->initDone.dataPktLen;
Display_print0(dispHandle, MR_ROW_DEV_ADDR, 0, Util_convertBdAddr2Str(pEvent->initDone.devAddr));
Display_print0(dispHandle, MR_ROW_CONN_STATUS, 0, "Connected to 0");
Display_print0(dispHandle, MR_ROW_STATUS1, 0, "Initialized");
// Set device info characteristic
DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, pEvent->initDone.devAddr);
ICall_freeMsg(pEvent);
//mr_doScan(0);
// 初始化完成后直接去指定mac建立连接
res = drct_doConnect(0);
//Util_startClock(&periodicClock);
}
....
break;
// Connection has been established
case GAP_LINK_ESTABLISHED_EVENT:
{
// If succesfully established
if (pEvent->gap.hdr.status == SUCCESS)
{
Display_print0(dispHandle, MR_ROW_STATUS1, 0, "Connected!");
Display_print1(dispHandle, MR_ROW_CONN_STATUS, 0, "Connected to %d", linkDB_NumActive());
// Clear connecting flag
connecting = FALSE;
// Add index-to-connHandle mapping entry and update menus
uint8_t index = multi_role_addMappingEntry(pEvent->linkCmpl.connectionHandle, pEvent->linkCmpl.devAddr);
//turn off advertising if no available links
if (linkDB_NumActive() >= maxNumBleConns)
{
uint8_t advertEnabled = FALSE;
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertEnabled, NULL);
Display_print0(dispHandle, MR_ROW_ADV, 0, "Can't adv: no links");
}
// Print last connected device
Display_print0(dispHandle, MR_ROW_STATUS2, 0, (char*)connHandleMap[index].strAddr);
// Return to main menu
//tbm_goTo(&mrMenuMain);
connhdl = pEvent->linkCmpl.connectionHandle;
// Util_startClock(&discPeriodicClock);
ICall_freeMsg(pEvent);
// 连接建立之后立即Start service discovery
multi_role_startDiscovery(pEvent->linkCmpl.connectionHandle);
// Start periodic clock if this is the first connection
if (linkDB_NumActive() == 1)
{
//Util_startClock(&periodicClock);
}
}
// If the connection was not successfully established
else
{
Display_print0(dispHandle, MR_ROW_STATUS1, 0, "Connect Failed");
Display_print1(dispHandle, MR_ROW_STATUS2, 0, "Reason: %d", pEvent->gap.hdr.status);
}
}
break;
}
}
static void multi_role_processGATTDiscEvent(gattMsgEvent_t *pMsg)
{
// Map connection handle to index
connIndex = multi_role_mapConnHandleToIndex(pMsg->connHandle);
// Check to prevent buffer overrun
if (connIndex < maxNumBleConns)
{
//MTU update
if (pMsg->method == ATT_MTU_UPDATED_EVENT)
{
// MTU size updated
Display_print1(dispHandle, MR_ROW_STATUS1+1, 0, "MTU Size: %d", pMsg->msg.mtuEvt.MTU);
}
// If we've updated the MTU size
else if (discInfo[connIndex].discState == BLE_DISC_STATE_MTU)
{
// MTU size response received, discover simple service
if (pMsg->method == ATT_EXCHANGE_MTU_RSP)
{
uint8_t uuid[ATT_BT_UUID_SIZE] = { LO_UINT16(SIMPLEPROFILE_SERV_UUID),
HI_UINT16(SIMPLEPROFILE_SERV_UUID) };
// Advance state
discInfo[connIndex].discState= BLE_DISC_STATE_SVC;
// Discovery of simple service
VOID GATT_DiscPrimaryServiceByUUID(pMsg->connHandle, uuid, ATT_BT_UUID_SIZE,
selfEntity);
}
}
// If we're performing service discovery
else if (discInfo[connIndex].discState == BLE_DISC_STATE_SVC)
{
// Service found, store handles
if (pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
pMsg->msg.findByTypeValueRsp.numInfo > 0)
{
discInfo[connIndex].svcStartHdl = ATT_ATTR_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0);
discInfo[connIndex].svcEndHdl = ATT_GRP_END_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0);
}
// If procedure is complete
if (((pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP) &&
(pMsg->hdr.status == bleProcedureComplete)) ||
(pMsg->method == ATT_ERROR_RSP))
{
// If we've discovered the service
if (discInfo[connIndex].svcStartHdl != 0)
{
if(0)
{
attReadByTypeReq_t req;
// Discover characteristic
discInfo[connIndex].discState = BLE_DISC_STATE_CHAR;
req.startHandle = discInfo[connIndex].svcStartHdl;
req.endHandle = discInfo[connIndex].svcEndHdl;
req.type.len = ATT_BT_UUID_SIZE;
req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID);
req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);
// Send characteristic discovery request
VOID GATT_DiscCharsByUUID(pMsg->connHandle, &req, selfEntity);
}
else
{
attReadByTypeReq_t req;
// Discover characteristic
discInfo[connIndex].discState = BLE_DISC_STATE_CHAR;
req.startHandle = discInfo[connIndex].svcStartHdl;
req.endHandle = discInfo[connIndex].svcEndHdl;
req.type.len = ATT_BT_UUID_SIZE;
req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR4_UUID);
req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR4_UUID);
// Send characteristic discovery request
VOID GATT_DiscCharsByUUID(pMsg->connHandle, &req, selfEntity);
}
}
}
}
// If we're discovering characteristics
else if (discInfo[connIndex].discState == BLE_DISC_STATE_CHAR)
{
// Characteristic found
if ((pMsg->method == ATT_READ_BY_TYPE_RSP) &&
(pMsg->msg.readByTypeRsp.numPairs > 0))
{
// Store handle
discInfo[connIndex].charHdl = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[3],
pMsg->msg.readByTypeRsp.pDataList[4]);
Display_print1(dispHandle, MR_ROW_STATUS1, 0, "Simple Svc Found %d\n",discInfo[connIndex].charHdl);
// write data
ICall_freeMsg(pMsg);
//查找到 char 之后立即开始写 特征值
drct_doGattRw(connIndex);
// Util_startClock(&switchPeriodicClock);
}
}
}
}