hiddev.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*********************************************************************
  29. * INCLUDES
  30. */
  31. #include "OSAL.h"
  32. #include "gatt.h"
  33. #include "hci.h"
  34. #include "gapgattserver.h"
  35. #include "gattservapp.h"
  36. #include "gatt_uuid.h"
  37. #include "gatt_profile_uuid.h"
  38. #include "linkdb.h"
  39. #include "peripheral.h"
  40. #include "gapbondmgr.h"
  41. #include "devinfoservice.h"
  42. #include "battservice.h"
  43. #include "scanparamservice.h"
  44. #include "hiddev.h"
  45. #include "hidkbd.h"
  46. #include "hidkbdservice.h"
  47. //#include "touch_key.h"
  48. #include "log.h"
  49. /*********************************************************************
  50. * MACROS
  51. */
  52. // Battery measurement period in ms
  53. #define DEFAULT_BATT_PERIOD 15000
  54. // TRUE to run scan parameters refresh notify test
  55. #define DEFAULT_SCAN_PARAM_NOTIFY_TEST TRUE
  56. // Advertising intervals (units of 625us, 160=100ms)
  57. #define HID_INITIAL_ADV_INT_MIN 48
  58. #define HID_INITIAL_ADV_INT_MAX 80
  59. #define HID_HIGH_ADV_INT_MIN 32
  60. #define HID_HIGH_ADV_INT_MAX 48
  61. #define HID_LOW_ADV_INT_MIN 1600
  62. #define HID_LOW_ADV_INT_MAX 1600
  63. // Advertising timeouts in sec
  64. #define HID_INITIAL_ADV_TIMEOUT 60
  65. #define HID_HIGH_ADV_TIMEOUT 5
  66. #define HID_LOW_ADV_TIMEOUT 0
  67. // Heart Rate Task Events
  68. #define START_DEVICE_EVT 0x0001
  69. #define BATT_PERIODIC_EVT 0x0002
  70. #define HID_IDLE_EVT 0x0004
  71. #define HID_SEND_REPORT_EVT 0x0008
  72. #define reportQEmpty() ( firstQIdx == lastQIdx )
  73. /*********************************************************************
  74. * CONSTANTS
  75. */
  76. #define HID_DEV_DATA_LEN 8
  77. #ifdef HID_DEV_RPT_QUEUE_LEN
  78. #define HID_DEV_REPORT_Q_SIZE (HID_DEV_RPT_QUEUE_LEN+1)
  79. #else
  80. #define HID_DEV_REPORT_Q_SIZE (10+1)
  81. #endif
  82. /*********************************************************************
  83. * TYPEDEFS
  84. */
  85. typedef struct
  86. {
  87. uint8 id;
  88. uint8 type;
  89. uint8 len;
  90. uint8 data[HID_DEV_DATA_LEN];
  91. } hidDevReport_t;
  92. /*********************************************************************
  93. * GLOBAL VARIABLES
  94. */
  95. // Task ID
  96. uint8 hidDevTaskId;
  97. /*********************************************************************
  98. * EXTERNAL VARIABLES
  99. */
  100. /*********************************************************************
  101. * EXTERNAL FUNCTIONS
  102. */
  103. // HID report mapping table
  104. extern hidRptMap_t hidRptMap[];
  105. /*********************************************************************
  106. * LOCAL VARIABLES
  107. */
  108. // GAP State
  109. gaprole_States_t hidDevGapState = GAPROLE_INIT;
  110. // TRUE if connection is secure
  111. static uint8 hidDevConnSecure = FALSE;
  112. // GAP connection handle
  113. uint16 gapConnHandle;
  114. // TRUE if pairing in progress
  115. static uint8 hidDevPairingStarted = FALSE;
  116. // Status of last pairing
  117. static uint8 pairingStatus = SUCCESS;
  118. static hidRptMap_t *pHidDevRptTbl;
  119. static uint8 hidDevRptTblLen;
  120. static hidDevCB_t *pHidDevCB;
  121. static hidDevCfg_t *pHidDevCfg;
  122. // Whether to change to the preferred connection parameters
  123. static uint8 updateConnParams = TRUE;
  124. // Pending reports
  125. static uint8 firstQIdx = 0;
  126. static uint8 lastQIdx = 0;
  127. static hidDevReport_t hidDevReportQ[HID_DEV_REPORT_Q_SIZE];
  128. // Last report sent out
  129. static attHandleValueNoti_t lastNoti = { 0 };
  130. /*********************************************************************
  131. * LOCAL FUNCTIONS
  132. */
  133. static void hidDev_ProcessOSALMsg( osal_event_hdr_t *pMsg );
  134. static void hidDevProcessGattMsg( gattMsgEvent_t *pMsg );
  135. static void hidDevDisconnected( void );
  136. static void hidDevGapStateCB( gaprole_States_t newState );
  137. static void hidDevPairStateCB( uint16 connHandle, uint8 state, uint8 status );
  138. static void hidDevPasscodeCB( uint8 *deviceAddr, uint16 connectionHandle,
  139. uint8 uiInputs, uint8 uiOutputs );
  140. void hidDevBattCB( uint8 event );
  141. void hidDevScanParamCB( uint8 event );
  142. static void hidDevBattPeriodicTask( void );
  143. static hidRptMap_t *hidDevRptByHandle( uint16 handle );
  144. static hidRptMap_t *hidDevRptById( uint8 id, uint8 type );
  145. static hidRptMap_t *hidDevRptByCccdHandle( uint16 handle );
  146. static void hidDevEnqueueReport( uint8 id, uint8 type, uint8 len, uint8 *pData );
  147. static hidDevReport_t *hidDevDequeueReport( void );
  148. static void hidDevSendReport( uint8 id, uint8 type, uint8 len, uint8 *pData );
  149. static void hidDevHighAdvertising( void );
  150. static void hidDevLowAdvertising( void );
  151. static void hidDevInitialAdvertising( void );
  152. static uint8 hidDevBondCount( void );
  153. static void hidDevStartIdleTimer( void );
  154. static void hidDevStopIdleTimer( void );
  155. static void HidDev_scanParamCB(uint8_t event);
  156. /*********************************************************************
  157. * PROFILE CALLBACKS
  158. */
  159. // GAP Role Callbacks
  160. static gapRolesCBs_t hidDev_PeripheralCBs =
  161. {
  162. hidDevGapStateCB, // Profile State Change Callbacks
  163. NULL // When a valid RSSI is read from controller
  164. };
  165. // Bond Manager Callbacks
  166. static const gapBondCBs_t hidDevBondCB =
  167. {
  168. hidDevPasscodeCB,
  169. hidDevPairStateCB
  170. };
  171. /*********************************************************************
  172. * PUBLIC FUNCTIONS
  173. */
  174. /*********************************************************************
  175. * @fn HidDev_Init
  176. *
  177. * @brief Initialization function for the Hid Dev Task.
  178. * This is called during initialization and should contain
  179. * any application specific initialization (ie. hardware
  180. * initialization/setup, table initialization, power up
  181. * notificaiton ... ).
  182. *
  183. * @param task_id - the ID assigned by OSAL. This ID should be
  184. * used to send messages and set timers.
  185. *
  186. * @return none
  187. */
  188. void HidDev_Init( uint8 task_id )
  189. {
  190. hidDevTaskId = task_id;
  191. // Setup the GAP Bond Manager
  192. {
  193. uint8 syncWL = FALSE;//TRUE;
  194. // If a bond is created, the HID Device should write the address of the
  195. // HID Host in the HID Device controller's white list and set the HID
  196. // Device controller's advertising filter policy to 'process scan and
  197. // connection requests only from devices in the White List'.
  198. VOID GAPBondMgr_SetParameter( GAPBOND_AUTO_SYNC_WL, sizeof( uint8 ), &syncWL );
  199. }
  200. // Set up services
  201. GGS_AddService( GATT_ALL_SERVICES ); // GAP
  202. GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes
  203. DevInfo_AddService( );
  204. Batt_AddService();
  205. ScanParam_AddService();
  206. Batt_Register(NULL);
  207. // Register for Scan Parameters service callback.
  208. ScanParam_Register(HidDev_scanParamCB);
  209. //touch_init(on_key);
  210. // Setup a delayed profile startup
  211. osal_set_event( hidDevTaskId, START_DEVICE_EVT );
  212. }
  213. /*********************************************************************
  214. * @fn HidDev_ProcessEvent
  215. *
  216. * @brief Hid Dev Task event processor. This function
  217. * is called to process all events for the task. Events
  218. * include timers, messages and any other user defined events.
  219. *
  220. * @param task_id - The OSAL assigned task ID.
  221. * @param events - events to process. This is a bit map and can
  222. * contain more than one event.
  223. *
  224. * @return events not processed
  225. */
  226. uint16 HidDev_ProcessEvent( uint8 task_id, uint16 events )
  227. {
  228. VOID task_id; // OSAL required parameter that isn't used in this function
  229. LOG("%s\n",__FUNCTION__);
  230. if ( events & SYS_EVENT_MSG )
  231. {
  232. uint8 *pMsg;
  233. if ( (pMsg = osal_msg_receive( hidDevTaskId )) != NULL )
  234. {
  235. hidDev_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
  236. // Release the OSAL message
  237. VOID osal_msg_deallocate( pMsg );
  238. }
  239. // return unprocessed events
  240. return (events ^ SYS_EVENT_MSG);
  241. }
  242. if ( events & START_DEVICE_EVT )
  243. {
  244. // Start the Device
  245. VOID GAPRole_StartDevice( &hidDev_PeripheralCBs );
  246. // Register with bond manager after starting device
  247. GAPBondMgr_Register( (gapBondCBs_t *) &hidDevBondCB );
  248. GAPBondMgr_SetParameter(GAPBOND_ERASE_ALLBONDS,0,NULL);
  249. LOG("start Device EVT\n\r");
  250. return ( events ^ START_DEVICE_EVT );
  251. }
  252. if ( events & HID_IDLE_EVT )
  253. {
  254. if ( hidDevGapState == GAPROLE_CONNECTED )
  255. {
  256. // if pairing in progress then restart timer
  257. if ( hidDevPairingStarted )
  258. {
  259. hidDevStartIdleTimer();
  260. LOG("hidDevStartIdleTimer \n\r");
  261. }
  262. // else disconnect
  263. else
  264. {
  265. GAPRole_TerminateConnection();
  266. }
  267. }
  268. return ( events ^ HID_IDLE_EVT );
  269. }
  270. if ( events & BATT_PERIODIC_EVT )
  271. {
  272. // Perform periodic battery task
  273. hidDevBattPeriodicTask();
  274. return ( events ^ BATT_PERIODIC_EVT );
  275. }
  276. if ( events & HID_SEND_REPORT_EVT )
  277. {
  278. // if connection is secure
  279. if ( hidDevConnSecure )
  280. {
  281. LOG("Send Hid Report\n\r");
  282. hidDevReport_t *pReport = hidDevDequeueReport();
  283. if ( pReport != NULL )
  284. {
  285. // Send report
  286. hidDevSendReport( pReport->id, pReport->type, pReport->len, pReport->data );
  287. }
  288. return ( reportQEmpty() ? events ^ HID_SEND_REPORT_EVT : events );
  289. }
  290. return ( events ^ HID_SEND_REPORT_EVT );
  291. }
  292. return 0;
  293. }
  294. /*********************************************************************
  295. * @fn HidDev_Register
  296. *
  297. * @brief Register a callback function with HID Dev.
  298. *
  299. * @param pCfg - Parameter configuration.
  300. * @param pfnServiceCB - Callback function.
  301. *
  302. * @return None.
  303. */
  304. void HidDev_Register( hidDevCfg_t *pCfg, hidDevCB_t *pCBs )
  305. {
  306. pHidDevCB = pCBs;
  307. pHidDevCfg = pCfg;
  308. }
  309. /*********************************************************************
  310. * @fn HidDev_RegisterReports
  311. *
  312. * @brief Register the report table with HID Dev.
  313. *
  314. * @param numReports - Length of report table.
  315. * @param pRpt - Report table.
  316. *
  317. * @return None.
  318. */
  319. void HidDev_RegisterReports( uint8 numReports, hidRptMap_t *pRpt )
  320. {
  321. pHidDevRptTbl = pRpt;
  322. hidDevRptTblLen = numReports;
  323. }
  324. /*********************************************************************
  325. * @fn HidDev_Report
  326. *
  327. * @brief Send a HID report.
  328. *
  329. * @param id - HID report ID.
  330. * @param type - HID report type.
  331. * @param len - Length of report.
  332. * @param pData - Report data.
  333. *
  334. * @return None.
  335. */
  336. void HidDev_Report( uint8 id, uint8 type, uint8 len, uint8*pData )
  337. {
  338. // if connected
  339. if ( hidDevGapState == GAPROLE_CONNECTED )
  340. {
  341. // if connection is secure
  342. if ( hidDevConnSecure )
  343. {
  344. // Make sure there're no pending reports
  345. if ( reportQEmpty() )
  346. {
  347. // send report
  348. hidDevSendReport( id, type, len, pData );
  349. LOG("send key action\n\r");
  350. return; // we're done
  351. }
  352. }
  353. }
  354. // else if not already advertising
  355. else if ( hidDevGapState != GAPROLE_ADVERTISING )
  356. {
  357. // if bonded
  358. if ( hidDevBondCount() > 0 )
  359. {
  360. // start high duty cycle advertising
  361. hidDevHighAdvertising();
  362. }
  363. // else not bonded
  364. else
  365. {
  366. // start initial advertising
  367. hidDevInitialAdvertising();
  368. }
  369. }
  370. // hidDev task will send report when secure connection is established
  371. hidDevEnqueueReport( id, type, len, pData );
  372. }
  373. /*********************************************************************
  374. * @fn HidDev_Close
  375. *
  376. * @brief Close the connection or stop advertising.
  377. *
  378. * @return None.
  379. */
  380. void HidDev_Close( void )
  381. {
  382. uint8 param;
  383. // if connected then disconnect
  384. if ( hidDevGapState == GAPROLE_CONNECTED )
  385. {
  386. GAPRole_TerminateConnection();
  387. }
  388. // else stop advertising
  389. else
  390. {
  391. param = FALSE;
  392. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &param );
  393. }
  394. }
  395. /*********************************************************************
  396. * @fn HidDev_SetParameter
  397. *
  398. * @brief Set a HID Dev parameter.
  399. *
  400. * @param param - Profile parameter ID
  401. * @param len - length of data to right
  402. * @param pValue - pointer to data to write. This is dependent on
  403. * the parameter ID and WILL be cast to the appropriate
  404. * data type (example: data type of uint16 will be cast to
  405. * uint16 pointer).
  406. *
  407. * @return bStatus_t
  408. */
  409. bStatus_t HidDev_SetParameter( uint8 param, uint8 len, void *pValue )
  410. {
  411. bStatus_t ret = SUCCESS;
  412. switch ( param )
  413. {
  414. case HIDDEV_ERASE_ALLBONDS:
  415. if ( len == 0 )
  416. {
  417. // See if the last report sent out wasn't a release key
  418. if ( osal_isbufset( lastNoti.value, 0x00, lastNoti.len ) == FALSE )
  419. {
  420. // Send a release report before disconnecting, otherwise
  421. // the last pressed key would get 'stuck' on the HID Host.
  422. osal_memset( lastNoti.value, 0x00, lastNoti.len );
  423. GATT_Notification( gapConnHandle, &lastNoti, FALSE );
  424. }
  425. // Drop connection
  426. if ( hidDevGapState == GAPROLE_CONNECTED )
  427. {
  428. GAPRole_TerminateConnection();
  429. }
  430. // Flush report queue
  431. firstQIdx = lastQIdx = 0;
  432. // Erase bonding info
  433. GAPBondMgr_SetParameter( GAPBOND_ERASE_ALLBONDS, 0, NULL );
  434. }
  435. else
  436. {
  437. ret = bleInvalidRange;
  438. }
  439. break;
  440. default:
  441. ret = INVALIDPARAMETER;
  442. break;
  443. }
  444. return ( ret );
  445. }
  446. /*********************************************************************
  447. * @fn HidDev_GetParameter
  448. *
  449. * @brief Get a HID Dev parameter.
  450. *
  451. * @param param - Profile parameter ID
  452. * @param pValue - pointer to data to get. This is dependent on
  453. * the parameter ID and WILL be cast to the appropriate
  454. * data type (example: data type of uint16 will be cast to
  455. * uint16 pointer).
  456. *
  457. * @return bStatus_t
  458. */
  459. bStatus_t HidDev_GetParameter( uint8 param, void *pValue )
  460. {
  461. bStatus_t ret = SUCCESS;
  462. switch ( param )
  463. {
  464. default:
  465. ret = INVALIDPARAMETER;
  466. break;
  467. }
  468. return ( ret );
  469. }
  470. /*********************************************************************
  471. * @fn HidDev_PasscodeRsp
  472. *
  473. * @brief Respond to a passcode request.
  474. *
  475. * @param status - SUCCESS if passcode is available, otherwise
  476. * see @ref SMP_PAIRING_FAILED_DEFINES.
  477. * @param passcode - integer value containing the passcode.
  478. *
  479. * @return none
  480. */
  481. void HidDev_PasscodeRsp( uint8 status, uint32 passcode )
  482. {
  483. // Send passcode response
  484. GAPBondMgr_PasscodeRsp( gapConnHandle, status, passcode );
  485. }
  486. /*********************************************************************
  487. * @fn HidDev_ReadAttrCB
  488. *
  489. * @brief HID Dev attribute read callback.
  490. *
  491. * @param connHandle - connection message was received on
  492. * @param pAttr - pointer to attribute
  493. * @param pValue - pointer to data to be read
  494. * @param pLen - length of data to be read
  495. * @param offset - offset of the first octet to be read
  496. * @param maxLen - maximum length of data to be read
  497. * @param method - type of read message
  498. *
  499. * @return SUCCESS, blePending or Failure
  500. */
  501. uint8 HidDev_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  502. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  503. {
  504. bStatus_t status = SUCCESS;
  505. hidRptMap_t *pRpt;
  506. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  507. // Only report map is long
  508. if ( offset > 0 && uuid != REPORT_MAP_UUID )
  509. {
  510. return ( ATT_ERR_ATTR_NOT_LONG );
  511. }
  512. if ( uuid == REPORT_UUID ||
  513. uuid == BOOT_KEY_INPUT_UUID ||
  514. uuid == BOOT_KEY_OUTPUT_UUID ||
  515. uuid == BOOT_MOUSE_INPUT_UUID )
  516. {
  517. // find report ID in table
  518. if ( (pRpt = hidDevRptByHandle(pAttr->handle)) != NULL )
  519. {
  520. // execute report callback
  521. status = (*pHidDevCB->reportCB)( pRpt->id, pRpt->type, uuid,
  522. HID_DEV_OPER_READ, pLen, pValue );
  523. }
  524. else
  525. {
  526. *pLen = 0;
  527. }
  528. }
  529. else if ( uuid == REPORT_MAP_UUID )
  530. {
  531. // verify offset
  532. if ( offset >= hidReportMapLen )
  533. {
  534. status = ATT_ERR_INVALID_OFFSET;
  535. }
  536. else
  537. {
  538. // determine read length
  539. *pLen = MIN( maxLen, (hidReportMapLen - offset) );
  540. // copy data
  541. osal_memcpy( pValue, pAttr->pValue + offset, *pLen );
  542. }
  543. }
  544. else if ( uuid == HID_INFORMATION_UUID )
  545. {
  546. *pLen = HID_INFORMATION_LEN;
  547. osal_memcpy( pValue, pAttr->pValue, HID_INFORMATION_LEN );
  548. }
  549. else if ( uuid == GATT_REPORT_REF_UUID )
  550. {
  551. *pLen = HID_REPORT_REF_LEN;
  552. osal_memcpy( pValue, pAttr->pValue, HID_REPORT_REF_LEN );
  553. }
  554. else if ( uuid == PROTOCOL_MODE_UUID )
  555. {
  556. *pLen = HID_PROTOCOL_MODE_LEN;
  557. pValue[0] = pAttr->pValue[0];
  558. }
  559. else if ( uuid == GATT_EXT_REPORT_REF_UUID )
  560. {
  561. *pLen = HID_EXT_REPORT_REF_LEN;
  562. osal_memcpy( pValue, pAttr->pValue, HID_EXT_REPORT_REF_LEN );
  563. }
  564. // restart idle timer
  565. if ( status == SUCCESS )
  566. {
  567. hidDevStartIdleTimer();
  568. }
  569. return ( status );
  570. }
  571. /*********************************************************************
  572. * @fn HidDev_WriteAttrCB
  573. *
  574. * @brief HID Dev attribute read callback.
  575. *
  576. * @param connHandle - connection message was received on
  577. * @param pAttr - pointer to attribute
  578. * @param pValue - pointer to data to be written
  579. * @param len - length of data
  580. * @param offset - offset of the first octet to be written
  581. *
  582. * @return Success or Failure
  583. */
  584. bStatus_t HidDev_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  585. uint8 *pValue, uint8 len, uint16 offset )
  586. {
  587. bStatus_t status = SUCCESS;
  588. hidRptMap_t *pRpt;
  589. // Make sure it's not a blob operation (no attributes in the profile are long)
  590. if ( offset > 0 )
  591. {
  592. return ( ATT_ERR_ATTR_NOT_LONG );
  593. }
  594. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  595. if ( uuid == REPORT_UUID ||
  596. uuid == BOOT_KEY_OUTPUT_UUID )
  597. {
  598. // find report ID in table
  599. if ((pRpt = hidDevRptByHandle(pAttr->handle)) != NULL)
  600. {
  601. // execute report callback
  602. status = (*pHidDevCB->reportCB)( pRpt->id, pRpt->type, uuid,
  603. HID_DEV_OPER_WRITE, &len, pValue );
  604. }
  605. }
  606. else if ( uuid == HID_CTRL_PT_UUID )
  607. {
  608. // Validate length and value range
  609. if ( len == 1 )
  610. {
  611. if ( pValue[0] == HID_CMD_SUSPEND || pValue[0] == HID_CMD_EXIT_SUSPEND )
  612. {
  613. // execute HID app event callback
  614. (*pHidDevCB->evtCB)( (pValue[0] == HID_CMD_SUSPEND) ?
  615. HID_DEV_SUSPEND_EVT : HID_DEV_EXIT_SUSPEND_EVT );
  616. }
  617. else
  618. {
  619. status = ATT_ERR_INVALID_VALUE;
  620. }
  621. }
  622. else
  623. {
  624. status = ATT_ERR_INVALID_VALUE_SIZE;
  625. }
  626. }
  627. else if ( uuid == GATT_CLIENT_CHAR_CFG_UUID )
  628. {
  629. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  630. offset, GATT_CLIENT_CFG_NOTIFY );
  631. if ( status == SUCCESS )
  632. {
  633. uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  634. // find report ID in table
  635. if ( (pRpt = hidDevRptByCccdHandle(pAttr->handle)) != NULL )
  636. {
  637. if(hidRptMap[5].cccdHandle==pRpt->cccdHandle)
  638. {
  639. LOG("Audio cfg%4X\n\r",charCfg);
  640. }
  641. // execute report callback
  642. (*pHidDevCB->reportCB)( pRpt->id, pRpt->type, uuid,
  643. (charCfg == GATT_CLIENT_CFG_NOTIFY) ?
  644. HID_DEV_OPER_ENABLE : HID_DEV_OPER_DISABLE,
  645. &len, pValue );
  646. }
  647. }
  648. }
  649. else if ( uuid == PROTOCOL_MODE_UUID )
  650. {
  651. if ( len == HID_PROTOCOL_MODE_LEN )
  652. {
  653. if ( pValue[0] == HID_PROTOCOL_MODE_BOOT ||
  654. pValue[0] == HID_PROTOCOL_MODE_REPORT )
  655. {
  656. pAttr->pValue[0] = pValue[0];
  657. // execute HID app event callback
  658. (*pHidDevCB->evtCB)( (pValue[0] == HID_PROTOCOL_MODE_BOOT) ?
  659. HID_DEV_SET_BOOT_EVT : HID_DEV_SET_REPORT_EVT );
  660. }
  661. else
  662. {
  663. status = ATT_ERR_INVALID_VALUE;
  664. }
  665. }
  666. else
  667. {
  668. status = ATT_ERR_INVALID_VALUE_SIZE;
  669. }
  670. }
  671. // restart idle timer
  672. if (status == SUCCESS)
  673. {
  674. hidDevStartIdleTimer();
  675. }
  676. return ( status );
  677. }
  678. /*********************************************************************
  679. * @fn hidDev_ProcessOSALMsg
  680. *
  681. * @brief Process an incoming task message.
  682. *
  683. * @param pMsg - message to process
  684. *
  685. * @return none
  686. */
  687. static void hidDev_ProcessOSALMsg( osal_event_hdr_t *pMsg )
  688. {
  689. switch ( pMsg->event )
  690. {
  691. case GATT_MSG_EVENT:
  692. hidDevProcessGattMsg( (gattMsgEvent_t *) pMsg );
  693. break;
  694. default:
  695. break;
  696. }
  697. }
  698. /*********************************************************************
  699. * @fn hidDevProcessGattMsg
  700. *
  701. * @brief Process GATT messages
  702. *
  703. * @return none
  704. */
  705. static void hidDevProcessGattMsg( gattMsgEvent_t *pMsg )
  706. {
  707. }
  708. /*********************************************************************
  709. * @fn hidDevHandleConnStatusCB
  710. *
  711. * @brief Reset client char config.
  712. *
  713. * @param connHandle - connection handle
  714. * @param changeType - type of change
  715. *
  716. * @return none
  717. */
  718. static void hidDevHandleConnStatusCB( uint16 connHandle, uint8 changeType )
  719. {
  720. uint8 i;
  721. hidRptMap_t *p = pHidDevRptTbl;
  722. uint16 retHandle;
  723. gattAttribute_t *pAttr;
  724. // Make sure this is not loopback connection
  725. if ( connHandle != LOOPBACK_CONNHANDLE )
  726. {
  727. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  728. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  729. ( !linkDB_Up( connHandle ) ) ) )
  730. {
  731. for ( i = hidDevRptTblLen; i > 0; i--, p++ )
  732. {
  733. if ( p->cccdHandle != 0 )
  734. {
  735. if ( (pAttr = GATT_FindHandle(p->cccdHandle, &retHandle)) != NULL )
  736. {
  737. GATTServApp_InitCharCfg( connHandle, (gattCharCfg_t *) pAttr->pValue );
  738. }
  739. }
  740. }
  741. }
  742. }
  743. }
  744. /*********************************************************************
  745. * @fn hidDevDisconnected
  746. *
  747. * @brief Handle disconnect.
  748. *
  749. * @return none
  750. */
  751. static void hidDevDisconnected( void )
  752. {
  753. // Stop idle timer
  754. hidDevStopIdleTimer();
  755. // Reset client characteristic configuration descriptors
  756. Batt_HandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED );
  757. //ScanParam_HandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED );
  758. hidDevHandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED );
  759. // Reset state variables
  760. hidDevConnSecure = FALSE;
  761. hidProtocolMode = HID_PROTOCOL_MODE_REPORT;
  762. hidDevPairingStarted = FALSE;
  763. // Reset last report sent out
  764. osal_memset( &lastNoti, 0, sizeof( attHandleValueNoti_t ) );
  765. // if bonded and normally connectable start advertising
  766. if ( ( hidDevBondCount() > 0 ) &&
  767. ( pHidDevCfg->hidFlags & HID_FLAGS_NORMALLY_CONNECTABLE ) )
  768. {
  769. hidDevLowAdvertising();
  770. }
  771. }
  772. /*********************************************************************
  773. * @fn hidDevGapStateCB
  774. *
  775. * @brief Notification from the profile of a state change.
  776. *
  777. * @param newState - new state
  778. *
  779. * @return none
  780. */
  781. void hidDevGapStateCB( gaprole_States_t newState )
  782. {
  783. LOG("%s, %d\n",__FUNCTION__, newState);
  784. // if connected
  785. if ( newState == GAPROLE_CONNECTED )
  786. {
  787. // get connection handle
  788. GAPRole_GetParameter( GAPROLE_CONNHANDLE, &gapConnHandle );
  789. // connection not secure yet
  790. hidDevConnSecure = FALSE;
  791. // don't start advertising when connection is closed
  792. uint8 param = FALSE;
  793. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &param );
  794. uint8 peerAddress[B_ADDR_LEN];
  795. GAPRole_GetParameter(GAPROLE_CONN_BD_ADDR, peerAddress);
  796. LOG("Master Mac:%02X,%02X,%02X,%02X,%02X,%02X\n\r",peerAddress[5],peerAddress[4],peerAddress[3],peerAddress[2],peerAddress[1],peerAddress[0]);
  797. // start idle timer
  798. hidDevStartIdleTimer();
  799. }
  800. // if disconnected
  801. else if ( hidDevGapState == GAPROLE_CONNECTED &&
  802. newState != GAPROLE_CONNECTED )
  803. {
  804. // GAPBondMgr_SetParameter(GAPBOND_ERASE_ALLBONDS,0,NULL);
  805. hidDevDisconnected();
  806. updateConnParams = TRUE;
  807. if ( pairingStatus == SMP_PAIRING_FAILED_CONFIRM_VALUE )
  808. {
  809. // bonding failed due to mismatched confirm values
  810. hidDevInitialAdvertising();
  811. pairingStatus = SUCCESS;
  812. }
  813. }
  814. // if started
  815. else if ( newState == GAPROLE_STARTED )
  816. {
  817. // nothing to do for now!
  818. }
  819. hidDevGapState = newState;
  820. }
  821. /*********************************************************************
  822. * @fn hidDevPairStateCB
  823. *
  824. * @brief Pairing state callback.
  825. *
  826. * @return none
  827. */
  828. static void hidDevPairStateCB( uint16 connHandle, uint8 state, uint8 status )
  829. {
  830. if ( state == GAPBOND_PAIRING_STATE_STARTED )
  831. {
  832. hidDevPairingStarted = TRUE;
  833. }
  834. else if ( state == GAPBOND_PAIRING_STATE_COMPLETE )
  835. {
  836. hidDevPairingStarted = FALSE;
  837. if ( status == SUCCESS )
  838. {
  839. hidDevConnSecure = TRUE;
  840. LOG("Pair Success\n\r");
  841. }
  842. else
  843. {
  844. LOG("Pair Fail\n\r");
  845. }
  846. pairingStatus = status;
  847. }
  848. else if ( state == GAPBOND_PAIRING_STATE_BONDED )
  849. {
  850. if ( status == SUCCESS )
  851. {
  852. hidDevConnSecure = TRUE;
  853. }
  854. }
  855. if ( !reportQEmpty() && hidDevConnSecure )
  856. {
  857. LOG("Set Send Report EVENT\n\r");
  858. // Notify our task to send out pending reports
  859. osal_set_event( hidDevTaskId, HID_SEND_REPORT_EVT );
  860. }
  861. }
  862. /*********************************************************************
  863. * @fn hidDevPasscodeCB
  864. *
  865. * @brief Passcode callback.
  866. *
  867. * @param deviceAddr - address of device to pair with, and could be either public or random.
  868. * @param connectionHandle - connection handle
  869. * @param uiInputs - pairing User Interface Inputs - Ask user to input passcode
  870. * @param uiOutputs - pairing User Interface Outputs - Display passcode
  871. *
  872. * @return none
  873. */
  874. void hidDevPasscodeCB( uint8 *deviceAddr, uint16 connectionHandle,
  875. uint8 uiInputs, uint8 uiOutputs )
  876. {
  877. if ( pHidDevCB && pHidDevCB->passcodeCB )
  878. {
  879. // execute HID app passcode callback
  880. (*pHidDevCB->passcodeCB)( deviceAddr, connectionHandle, uiInputs, uiOutputs );
  881. }
  882. else
  883. {
  884. // Send passcode response
  885. GAPBondMgr_PasscodeRsp( connectionHandle, SUCCESS, 0 );
  886. }
  887. }
  888. /*********************************************************************
  889. * @fn hidDevBattCB
  890. *
  891. * @brief Callback function for battery service.
  892. *
  893. * @param event - service event
  894. *
  895. * @return none
  896. */
  897. void hidDevBattCB( uint8 event )
  898. {
  899. if ( event == BATT_LEVEL_NOTI_ENABLED )
  900. {
  901. // if connected start periodic measurement
  902. if ( hidDevGapState == GAPROLE_CONNECTED )
  903. {
  904. osal_start_timerEx( hidDevTaskId, BATT_PERIODIC_EVT, DEFAULT_BATT_PERIOD );
  905. }
  906. }
  907. else if ( event == BATT_LEVEL_NOTI_DISABLED )
  908. {
  909. // stop periodic measurement
  910. osal_stop_timerEx( hidDevTaskId, BATT_PERIODIC_EVT );
  911. }
  912. }
  913. /*********************************************************************
  914. * @fn hidDevScanParamCB
  915. *
  916. * @brief Callback function for scan parameter service.
  917. *
  918. * @param event - service event
  919. *
  920. * @return none
  921. */
  922. void hidDevScanParamCB( uint8 event )
  923. {
  924. }
  925. /*********************************************************************
  926. * @fn hidDevBattPeriodicTask
  927. *
  928. * @brief Perform a periodic task for battery measurement.
  929. *
  930. * @param none
  931. *
  932. * @return none
  933. */
  934. static void hidDevBattPeriodicTask( void )
  935. {
  936. if ( hidDevGapState == GAPROLE_CONNECTED )
  937. {
  938. // perform battery level check
  939. Batt_MeasLevel( );
  940. // Restart timer
  941. osal_start_timerEx( hidDevTaskId, BATT_PERIODIC_EVT, DEFAULT_BATT_PERIOD );
  942. }
  943. }
  944. /*********************************************************************
  945. * @fn hidDevRptByHandle
  946. *
  947. * @brief Find the HID report structure for the given handle.
  948. *
  949. * @param handle - ATT handle
  950. *
  951. * @return Pointer to HID report structure
  952. */
  953. static hidRptMap_t *hidDevRptByHandle( uint16 handle )
  954. {
  955. uint8 i;
  956. hidRptMap_t *p = pHidDevRptTbl;
  957. for ( i = hidDevRptTblLen; i > 0; i--, p++ )
  958. {
  959. if ( p->handle == handle && p->mode == hidProtocolMode)
  960. {
  961. return p;
  962. }
  963. }
  964. return NULL;
  965. }
  966. /*********************************************************************
  967. * @fn hidDevRptByCccdHandle
  968. *
  969. * @brief Find the HID report structure for the given CCC handle.
  970. *
  971. * @param handle - ATT handle
  972. *
  973. * @return Pointer to HID report structure
  974. */
  975. static hidRptMap_t *hidDevRptByCccdHandle( uint16 handle )
  976. {
  977. uint8 i;
  978. hidRptMap_t *p = pHidDevRptTbl;
  979. for ( i = hidDevRptTblLen; i > 0; i--, p++ )
  980. {
  981. if ( p->cccdHandle == handle)
  982. {
  983. if(i==HID_VOICE_START_IN_CCCD_IDX)
  984. {
  985. LOG("Voice Notify Enable!!!!!\n\r");
  986. }
  987. return p;
  988. }
  989. }
  990. return NULL;
  991. }
  992. /*********************************************************************
  993. * @fn hidDevRptById
  994. *
  995. * @brief Find the HID report structure for the Report ID and type.
  996. *
  997. * @param id - HID report ID
  998. * @param type - HID report type
  999. *
  1000. * @return Pointer to HID report structure
  1001. */
  1002. static hidRptMap_t *hidDevRptById( uint8 id, uint8 type )
  1003. {
  1004. uint8 i;
  1005. hidRptMap_t *p = pHidDevRptTbl;
  1006. for ( i = hidDevRptTblLen; i > 0; i--, p++ )
  1007. {
  1008. if ( p->id == id && p->type == type && p->mode == hidProtocolMode )
  1009. {
  1010. return p;
  1011. }
  1012. }
  1013. return NULL;
  1014. }
  1015. /*********************************************************************
  1016. * @fn hidDevSendReport
  1017. *
  1018. * @brief Send a HID report.
  1019. *
  1020. * @param id - HID report ID.
  1021. * @param type - HID report type.
  1022. * @param len - Length of report.
  1023. * @param pData - Report data.
  1024. *
  1025. * @return None.
  1026. */
  1027. static void hidDevSendReport( uint8 id, uint8 type, uint8 len, uint8 *pData )
  1028. {
  1029. hidRptMap_t *pRpt;
  1030. gattAttribute_t *pAttr;
  1031. uint16 retHandle;
  1032. LOG("%s\n",__FUNCTION__);
  1033. // Get ATT handle for report
  1034. if ( (pRpt = hidDevRptById(id, type)) != NULL )
  1035. {
  1036. // if notifications are enabled
  1037. if ( (pAttr = GATT_FindHandle(pRpt->cccdHandle, &retHandle)) != NULL )
  1038. {
  1039. uint16 value;
  1040. value = GATTServApp_ReadCharCfg( gapConnHandle, (gattCharCfg_t *) pAttr->pValue );
  1041. if ( value & GATT_CLIENT_CFG_NOTIFY )
  1042. {
  1043. // After service discovery and encryption, the HID Device should request to
  1044. // change to the preferred connection parameters that best suit its use case.
  1045. if ( updateConnParams )
  1046. {
  1047. GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_REQ, sizeof( uint8 ), &updateConnParams );
  1048. updateConnParams = FALSE;
  1049. }
  1050. // send notification
  1051. lastNoti.handle = pRpt->handle;
  1052. lastNoti.len = len;
  1053. osal_memcpy(lastNoti.value, pData, len);
  1054. GATT_Notification( gapConnHandle, &lastNoti, FALSE );
  1055. // start idle timer
  1056. hidDevStartIdleTimer();
  1057. }
  1058. else
  1059. {
  1060. LOG("notify fail\n\r");
  1061. }
  1062. }
  1063. }
  1064. }
  1065. /*********************************************************************
  1066. * @fn hidDevEnqueueReport
  1067. *
  1068. * @brief Enqueue a HID report to be sent later.
  1069. *
  1070. * @param id - HID report ID.
  1071. * @param type - HID report type.
  1072. * @param len - Length of report.
  1073. * @param pData - Report data.
  1074. *
  1075. * @return None.
  1076. */
  1077. static void hidDevEnqueueReport( uint8 id, uint8 type, uint8 len, uint8 *pData )
  1078. {
  1079. // Enqueue only if bonded
  1080. if ( hidDevBondCount() > 0 )
  1081. {
  1082. // Update last index
  1083. lastQIdx = ( lastQIdx + 1 ) % HID_DEV_REPORT_Q_SIZE;
  1084. if ( lastQIdx == firstQIdx )
  1085. {
  1086. // Queue overflow; discard oldest report
  1087. firstQIdx = ( firstQIdx + 1 ) % HID_DEV_REPORT_Q_SIZE;
  1088. }
  1089. // Save report
  1090. hidDevReportQ[lastQIdx].id = id;
  1091. hidDevReportQ[lastQIdx].type = type;
  1092. hidDevReportQ[lastQIdx].len = len;
  1093. osal_memcpy( hidDevReportQ[lastQIdx].data, pData, len );
  1094. if ( hidDevConnSecure )
  1095. {
  1096. // Notify our task to send out pending reports
  1097. osal_set_event( hidDevTaskId, HID_SEND_REPORT_EVT );
  1098. }
  1099. }
  1100. }
  1101. /*********************************************************************
  1102. * @fn hidDevDequeueReport
  1103. *
  1104. * @brief Dequeue a HID report to be sent out.
  1105. *
  1106. * @param id - HID report ID.
  1107. * @param type - HID report type.
  1108. * @param len - Length of report.
  1109. * @param pData - Report data.
  1110. *
  1111. * @return None.
  1112. */
  1113. static hidDevReport_t *hidDevDequeueReport( void )
  1114. {
  1115. if ( reportQEmpty() )
  1116. {
  1117. return NULL;
  1118. }
  1119. // Update first index
  1120. firstQIdx = ( firstQIdx + 1 ) % HID_DEV_REPORT_Q_SIZE;
  1121. return ( &(hidDevReportQ[firstQIdx]) );
  1122. }
  1123. /*********************************************************************
  1124. * @fn hidDevHighAdvertising
  1125. *
  1126. * @brief Start advertising at a high duty cycle.
  1127. * @param None.
  1128. *
  1129. * @return None.
  1130. */
  1131. static void hidDevHighAdvertising( void )
  1132. {
  1133. uint8 param;
  1134. VOID GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, HID_HIGH_ADV_INT_MIN );
  1135. VOID GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, HID_HIGH_ADV_INT_MAX );
  1136. VOID GAP_SetParamValue( TGAP_LIM_ADV_TIMEOUT, HID_HIGH_ADV_TIMEOUT );
  1137. // Setup adverstising filter policy first
  1138. param = GAP_FILTER_POLICY_WHITE;
  1139. VOID GAPRole_SetParameter( GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &param );
  1140. param = TRUE;
  1141. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &param );
  1142. }
  1143. /*********************************************************************
  1144. * @fn hidDevLowAdvertising
  1145. *
  1146. * @brief Start advertising at a low duty cycle.
  1147. *
  1148. * @param None.
  1149. *
  1150. * @return None.
  1151. */
  1152. static void hidDevLowAdvertising( void )
  1153. {
  1154. uint8 param;
  1155. VOID GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, HID_LOW_ADV_INT_MIN );
  1156. VOID GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, HID_LOW_ADV_INT_MAX );
  1157. VOID GAP_SetParamValue( TGAP_LIM_ADV_TIMEOUT, HID_LOW_ADV_TIMEOUT );
  1158. // Setup adverstising filter policy first
  1159. param = GAP_FILTER_POLICY_ALL;//GAP_FILTER_POLICY_WHITE teddy modify
  1160. VOID GAPRole_SetParameter( GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &param );
  1161. param = TRUE;
  1162. VOID GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &param );
  1163. }
  1164. /*********************************************************************
  1165. * @fn hidDevInitialAdvertising
  1166. *
  1167. * @brief Start advertising for initial connection
  1168. *
  1169. * @return None.
  1170. */
  1171. static void hidDevInitialAdvertising( void )
  1172. {
  1173. uint8 param;
  1174. VOID GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, HID_INITIAL_ADV_INT_MIN );
  1175. VOID GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, HID_INITIAL_ADV_INT_MAX );
  1176. VOID GAP_SetParamValue( TGAP_LIM_ADV_TIMEOUT, HID_INITIAL_ADV_TIMEOUT );
  1177. // Setup adverstising filter policy first
  1178. param = GAP_FILTER_POLICY_ALL;
  1179. VOID GAPRole_SetParameter( GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &param );
  1180. param = TRUE;
  1181. VOID GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &param );
  1182. }
  1183. /*********************************************************************
  1184. * @fn hidDevBondCount
  1185. *
  1186. * @brief Gets the total number of bonded devices.
  1187. *
  1188. * @param None.
  1189. *
  1190. * @return number of bonded devices.
  1191. */
  1192. static uint8 hidDevBondCount( void )
  1193. {
  1194. uint8 bondCnt = 0;
  1195. VOID GAPBondMgr_GetParameter( GAPBOND_BOND_COUNT, &bondCnt );
  1196. return ( bondCnt );
  1197. }
  1198. /*********************************************************************
  1199. * @fn hidDevStartIdleTimer
  1200. *
  1201. * @brief Start the idle timer.
  1202. *
  1203. * @return None.
  1204. */
  1205. static void hidDevStartIdleTimer( void )
  1206. {
  1207. if ( pHidDevCfg->idleTimeout > 0 )
  1208. {
  1209. osal_start_timerEx( hidDevTaskId, HID_IDLE_EVT, pHidDevCfg->idleTimeout );
  1210. }
  1211. }
  1212. /*********************************************************************
  1213. * @fn hidDevStopIdleTimer
  1214. *
  1215. * @brief Stop the idle timer.
  1216. *
  1217. * @return None.
  1218. */
  1219. static void hidDevStopIdleTimer( void )
  1220. {
  1221. osal_stop_timerEx( hidDevTaskId, HID_IDLE_EVT );
  1222. }
  1223. /*********************************************************************
  1224. * @fn HidDev_scanParamCB
  1225. *
  1226. * @brief Callback function for scan parameter service.
  1227. *
  1228. * @param event - service event
  1229. *
  1230. * @return none
  1231. */
  1232. static void HidDev_scanParamCB(uint8_t event)
  1233. {
  1234. // Do nothing.
  1235. }
  1236. /*********************************************************************
  1237. *********************************************************************/