gapbondmgr.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. /**
  2. * @file
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /**************************************************************************************************
  11. Filename: gapbondmgr.c
  12. Revised:
  13. Revision:
  14. Description: GAP peripheral profile manages bonded connections
  15. **************************************************************************************************/
  16. #include "sdk_config.h"
  17. #include "bcomdef.h"
  18. #if ( HOST_CONFIG & ( CENTRAL_CFG | PERIPHERAL_CFG ) )
  19. /*********************************************************************
  20. * INCLUDES
  21. */
  22. #include "OSAL.h"
  23. #include "osal_snv.h"
  24. #include "gap.h"
  25. #include "linkdb.h"
  26. #include "gatt.h"
  27. #include "gatt_uuid.h"
  28. #include "hci.h"
  29. #include "gattservapp.h"
  30. #include "gapgattserver.h"
  31. #include "gapbondmgr.h"
  32. // temp set
  33. // #define osal_snv_write( a, b, c ) (1)
  34. // #define osal_snv_read( a, b, c ) (1)
  35. // #define osal_snv_compact( a ) (1)
  36. #define BOND_LOG(...)
  37. /*********************************************************************
  38. * MACROS
  39. */
  40. /*********************************************************************
  41. * CONSTANTS
  42. */
  43. // Task event types
  44. #define GAP_BOND_SYNC_CC_EVT 0x0001 // Sync char config
  45. #define GAP_BOND_SAVE_REC_EVT 0x0002 // Save bond record in NV
  46. // Once NV usage reaches this percentage threshold, NV compaction gets triggered.
  47. #define NV_COMPACT_THRESHOLD 80
  48. // Bonded State Flags
  49. #define GAP_BONDED_STATE_AUTHENTICATED 0x0001
  50. #define GAP_BONDED_STATE_SERVICE_CHANGED 0x0002
  51. /**
  52. * GAP Bond Manager NV layout
  53. *
  54. * The NV definitions:
  55. * BLE_NVID_GAP_BOND_START - starting NV ID
  56. * GAP_BONDINGS_MAX - Maximum number of bonding allowed (10 is max for number of NV IDs allocated in bcomdef.h).
  57. *
  58. * A single bonding entry consists of 6 components (NV items):
  59. * Bond Record - defined as gapBondRec_t and uses GAP_BOND_REC_ID_OFFSET for an NV ID
  60. * local LTK Info - defined as gapBondLTK_t and uses GAP_BOND_LOCAL_LTK_OFFSET for an NV ID
  61. * device LTK Info - defined as gapBondLTK_t and uses GAP_BOND_DEV_LTK_OFFSET for an NV ID
  62. * device IRK - defined as "uint8 devIRK[KEYLEN]" and uses GAP_BOND_DEV_IRK_OFFSET for an NV ID
  63. * device CSRK - defined as "uint8 devCSRK[KEYLEN]" and uses GAP_BOND_DEV_CSRK_OFFSET for an NV ID
  64. * device Sign Counter - defined as a uint32 and uses GAP_BOND_DEV_SIGN_COUNTER_OFFSET for an NV ID
  65. *
  66. * When the device is initialized for the first time, all (GAP_BONDINGS_MAX) NV items are created and
  67. * initialized to all 0xFF's. A bonding record of all 0xFF's indicates that the bonding record is empty
  68. * and free to use.
  69. *
  70. * The calculation for each bonding records NV IDs:
  71. * mainRecordNvID = ((bondIdx * GAP_BOND_REC_IDS) + BLE_NVID_GAP_BOND_START)
  72. * localLTKNvID = (((bondIdx * GAP_BOND_REC_IDS) + GAP_BOND_LOCAL_LTK_OFFSET) + BLE_NVID_GAP_BOND_START)
  73. *
  74. */
  75. #define GAP_BOND_REC_ID_OFFSET 0 //!< NV ID for the main bonding record
  76. #define GAP_BOND_LOCAL_LTK_OFFSET 1 //!< NV ID for the bonding record's local LTK information
  77. #define GAP_BOND_DEV_LTK_OFFSET 2 //!< NV ID for the bonding records' device LTK information
  78. #define GAP_BOND_DEV_IRK_OFFSET 3 //!< NV ID for the bonding records' device IRK
  79. #define GAP_BOND_DEV_CSRK_OFFSET 4 //!< NV ID for the bonding records' device CSRK
  80. #define GAP_BOND_DEV_SIGN_COUNTER_OFFSET 5 //!< NV ID for the bonding records' device Sign Counter
  81. #define GAP_BOND_REC_IDS 6
  82. // Macros to calculate the index/offset in to NV space
  83. #define calcNvID(Idx, offset) (((((Idx) * GAP_BOND_REC_IDS) + (offset))) + BLE_NVID_GAP_BOND_START)
  84. #define mainRecordNvID(bondIdx) (calcNvID((bondIdx), GAP_BOND_REC_ID_OFFSET))
  85. #define localLTKNvID(bondIdx) (calcNvID((bondIdx), GAP_BOND_LOCAL_LTK_OFFSET))
  86. #define devLTKNvID(bondIdx) (calcNvID((bondIdx), GAP_BOND_DEV_LTK_OFFSET))
  87. #define devIRKNvID(bondIdx) (calcNvID((bondIdx), GAP_BOND_DEV_IRK_OFFSET))
  88. #define devCSRKNvID(bondIdx) (calcNvID((bondIdx), GAP_BOND_DEV_CSRK_OFFSET))
  89. #define devSignCounterNvID(bondIdx) (calcNvID((bondIdx), GAP_BOND_DEV_SIGN_COUNTER_OFFSET))
  90. // Macros to calculate the GATT index/offset in to NV space
  91. #define gattCfgNvID(Idx) ((Idx) + BLE_NVID_GATT_CFG_START)
  92. // Key Size Limits
  93. #define MIN_ENC_KEYSIZE 7 //!< Minimum number of bytes for the encryption key
  94. #define MAX_ENC_KEYSIZE 16 //!< Maximum number of bytes for the encryption key
  95. /*********************************************************************
  96. * TYPEDEFS
  97. */
  98. // Structure of NV data for the connected device's encryption information
  99. typedef struct
  100. {
  101. uint8 LTK[KEYLEN]; // Long Term Key (LTK)
  102. uint16 div; //lint -e754 // LTK eDiv
  103. uint8 rand[B_RANDOM_NUM_SIZE]; // LTK random number
  104. uint8 keySize; // LTK key size
  105. } gapBondLTK_t;
  106. // Structure of NV data for the connected device's address information
  107. typedef struct
  108. {
  109. uint8 publicAddr[B_ADDR_LEN]; // Master's address
  110. uint8 reconnectAddr[B_ADDR_LEN]; // Privacy Reconnection Address
  111. uint16 stateFlags; // State flags: SM_AUTH_STATE_AUTHENTICATED & SM_AUTH_STATE_BONDING
  112. } gapBondRec_t;
  113. // Structure of NV data for the connected device's characteristic configuration
  114. typedef struct
  115. {
  116. uint16 attrHandle; // attribute handle
  117. uint8 value; // attribute value for this device
  118. } gapBondCharCfg_t;
  119. /*********************************************************************
  120. * GLOBAL VARIABLES
  121. */
  122. /*********************************************************************
  123. * EXTERNAL VARIABLES
  124. */
  125. /*********************************************************************
  126. * EXTERNAL FUNCTIONS
  127. */
  128. /*********************************************************************
  129. * LOCAL VARIABLES
  130. */
  131. static uint8 gapBondMgr_TaskID = INVALID_TASK_ID; // Task ID for internal task/event processing
  132. // GAPBonding Parameters
  133. static uint8 gapBond_PairingMode[MAX_NUM_LL_CONN] = {GAPBOND_PAIRING_MODE_WAIT_FOR_REQ};
  134. static uint16 gapBond_InitiateWait = 1000; // Default to 1 second
  135. static uint8 gapBond_MITM = FALSE;
  136. static uint8 gapBond_IOCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
  137. static uint8 gapBond_OOBDataFlag = FALSE;
  138. static uint8 gapBond_OOBData[KEYLEN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  139. static uint8 gapBond_Bonding = FALSE;
  140. // 2020-04-22 support secure connection pairing
  141. static uint8 gapBond_SC = FALSE;
  142. static uint8 gapBond_keyPress = FALSE;
  143. static uint8 gapBond_CT2 = FALSE;
  144. static uint8 gapBond_AutoFail = FALSE;
  145. static uint8 gapBond_AutoFailReason = SMP_PAIRING_FAILED_NOT_SUPPORTED;
  146. static uint8 gapBond_KeyDistList =
  147. (
  148. GAPBOND_KEYDIST_SENCKEY // sEncKey enabled, to send the encryption key
  149. | GAPBOND_KEYDIST_SIDKEY // sIdKey enabled, to send the IRK, and BD_ADDR
  150. | GAPBOND_KEYDIST_SSIGN // sSign enabled, to send the CSRK
  151. | GAPBOND_KEYDIST_MENCKEY // mEncKey enabled, to get the master's encryption key
  152. | GAPBOND_KEYDIST_MIDKEY // mIdKey enabled, to get the master's IRK and BD_ADDR
  153. | GAPBOND_KEYDIST_MSIGN // mSign enabled, to get the master's CSRK
  154. );
  155. static uint32 gapBond_Passcode = 0;
  156. static uint8 gapBond_KeySize = MAX_ENC_KEYSIZE;
  157. #if (GAP_BOND_MGR_INDEX_REPLACE)
  158. static uint8 bondReplaceCnt=0;
  159. #endif
  160. #if ( HOST_CONFIG & CENTRAL_CFG )
  161. static uint8 gapBond_BondFailOption = GAPBOND_FAIL_TERMINATE_LINK;
  162. #endif
  163. static const gapBondCBs_t *pGapBondCB = NULL;
  164. // Local RAM shadowed bond records
  165. static gapBondRec_t bonds[GAP_BONDINGS_MAX] = {0};
  166. static uint8 autoSyncWhiteList = FALSE;
  167. static uint8 eraseAllBonds = FALSE;
  168. static uint8 bondsToDelete[GAP_BONDINGS_MAX] = {FALSE};
  169. // Globals used for saving bond record and CCC values in NV
  170. static uint8 bondIdx = GAP_BONDINGS_MAX;
  171. static gapAuthCompleteEvent_t *pAuthEvt[MAX_NUM_LL_CONN] = {NULL};
  172. /*********************************************************************
  173. * LOCAL FUNCTIONS
  174. */
  175. static uint8 gapBondMgrChangeState( uint8 idx, uint16 state, uint8 set );
  176. static uint8 gapBondMgrUpdateCharCfg( uint8 idx, uint16 attrHandle, uint16 value );
  177. static gapBondCharCfg_t *gapBondMgrFindCharCfgItem( uint16 attrHandle,
  178. gapBondCharCfg_t *charCfgTbl );
  179. static void gapBondMgrInvertCharCfgItem( gapBondCharCfg_t *charCfgTbl );
  180. static uint8 gapBondMgrAddBond( gapBondRec_t *pBondRec, gapAuthCompleteEvent_t *pPkt );
  181. static uint8 gapBondMgrGetStateFlags( uint8 idx );
  182. static bStatus_t gapBondMgrGetPublicAddr( uint8 idx, uint8 *pAddr );
  183. static uint8 gapBondMgrFindReconnectAddr( uint8 *pReconnectAddr );
  184. static uint8 gapBondMgrFindAddr( uint8 *pDevAddr );
  185. static uint8 gapBondMgrResolvePrivateAddr( uint8 *pAddr );
  186. static void gapBondMgrReadBonds( void );
  187. static uint8 gapBondMgrFindEmpty( void );
  188. static uint8 gapBondMgrBondTotal( void );
  189. static bStatus_t gapBondMgrEraseAllBondings( void );
  190. static bStatus_t gapBondMgrEraseBonding( uint8 idx );
  191. static uint8 gapBondMgr_ProcessOSALMsg( osal_event_hdr_t *pMsg );
  192. static void gapBondMgrSendServiceChange( linkDBItem_t *pLinkItem );
  193. static void gapBondMgr_ProcessGATTMsg( gattMsgEvent_t *pMsg );
  194. static void gapBondMgr_ProcessGATTServMsg( gattEventHdr_t *pMsg );
  195. static void gapBondSetupPrivFlag( void );
  196. static void gapBondMgrBondReq( uint16 connHandle, uint8 idx, uint8 stateFlags,
  197. uint8 role, uint8 startEncryption );
  198. static void gapBondMgrAuthenticate( uint16 connHandle, uint8 addrType,
  199. gapPairingReq_t *pPairReq );
  200. static void gapBondMgr_SyncWhiteList( void );
  201. static uint8 gapBondMgr_SyncCharCfg( uint16 connHandle );
  202. static void gapBondFreeAuthEvt( uint16 connHandle );
  203. static void gapBondRecvEvt(uint16 connHandle,gapBondRec_t* pBondRec, gapAuthCompleteEvent_t* pPkt );
  204. #if ( HOST_CONFIG & PERIPHERAL_CFG )
  205. static void gapBondMgrSlaveSecurityReq( uint16 connHandle );
  206. #endif
  207. /*********************************************************************
  208. * NETWORK LAYER CALLBACKS
  209. */
  210. /*********************************************************************
  211. * PUBLIC FUNCTIONS
  212. */
  213. static void gapBondRecvEvt(uint16 connHandle, gapBondRec_t* pBondRec, gapAuthCompleteEvent_t* pPkt )
  214. {
  215. if ( gapBondMgrAddBond( pBondRec, pPkt ) )
  216. {
  217. // Update NV to have same CCC values as GATT database
  218. // Note: pAuthEvt is a global variable used for deferring the storage
  219. if ( gapBondMgr_SyncCharCfg( pAuthEvt[connHandle]->connectionHandle ) )
  220. {
  221. if ( pGapBondCB && pGapBondCB->pairStateCB )
  222. {
  223. // Assume SUCCESS since we got this far.
  224. pGapBondCB->pairStateCB( pAuthEvt[connHandle]->connectionHandle, GAPBOND_PAIRING_STATE_COMPLETE, SUCCESS );
  225. }
  226. // We're done storing bond record and CCC values in NV
  227. gapBondFreeAuthEvt(connHandle);
  228. }
  229. }
  230. }
  231. /*********************************************************************
  232. * @brief Set a GAP Bond Manager parameter.
  233. *
  234. * Public function defined in gapbondmgr.h.
  235. */
  236. bStatus_t GAPBondMgr_SetParameter( uint16 param, uint8 len, void *pValue )
  237. {
  238. bStatus_t ret = SUCCESS; // return value
  239. switch ( param )
  240. {
  241. case GAPBOND_PAIRING_MODE:
  242. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAPBOND_PAIRING_MODE_INITIATE) )
  243. {
  244. // gapBond_PairingMode = *((uint8*)pValue);
  245. osal_memset(gapBond_PairingMode,*((uint8*)pValue),sizeof(gapBond_PairingMode));
  246. }
  247. else
  248. {
  249. ret = bleInvalidRange;
  250. }
  251. break;
  252. case GAPBOND_INITIATE_WAIT:
  253. if ( len == sizeof ( uint16 ) )
  254. {
  255. gapBond_InitiateWait = *((uint16*)pValue);
  256. }
  257. else
  258. {
  259. ret = bleInvalidRange;
  260. }
  261. break;
  262. case GAPBOND_MITM_PROTECTION:
  263. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
  264. {
  265. gapBond_MITM = *((uint8*)pValue);
  266. }
  267. else
  268. {
  269. ret = bleInvalidRange;
  270. }
  271. break;
  272. case GAPBOND_IO_CAPABILITIES:
  273. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAPBOND_IO_CAP_KEYBOARD_DISPLAY) )
  274. {
  275. gapBond_IOCap = *((uint8*)pValue);
  276. }
  277. else
  278. {
  279. ret = bleInvalidRange;
  280. }
  281. break;
  282. case GAPBOND_OOB_ENABLED:
  283. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
  284. {
  285. gapBond_OOBDataFlag = *((uint8*)pValue);
  286. }
  287. else
  288. {
  289. ret = bleInvalidRange;
  290. }
  291. break;
  292. case GAPBOND_OOB_DATA:
  293. if ( len == KEYLEN )
  294. {
  295. VOID osal_memcpy( gapBond_OOBData, pValue, KEYLEN ) ;
  296. }
  297. else
  298. {
  299. ret = bleInvalidRange;
  300. }
  301. break;
  302. case GAPBOND_BONDING_ENABLED:
  303. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
  304. {
  305. gapBond_Bonding = *((uint8*)pValue);
  306. }
  307. else
  308. {
  309. ret = bleInvalidRange;
  310. }
  311. break;
  312. case GAPBOND_KEY_DIST_LIST:
  313. if ( len == sizeof ( uint8 ) )
  314. {
  315. gapBond_KeyDistList = *((uint8*)pValue);
  316. }
  317. else
  318. {
  319. ret = bleInvalidRange;
  320. }
  321. break;
  322. case GAPBOND_DEFAULT_PASSCODE:
  323. if ( (len == sizeof ( uint32 ))
  324. && (*((uint32*)pValue) <= GAP_PASSCODE_MAX) )
  325. {
  326. gapBond_Passcode = *((uint32*)pValue);
  327. }
  328. else
  329. {
  330. ret = bleInvalidRange;
  331. }
  332. break;
  333. case GAPBOND_ERASE_ALLBONDS:
  334. if ( len == 0 )
  335. {
  336. // Make sure there's no active connection
  337. if ( GAP_NumActiveConnections() == 0 )
  338. {
  339. // Erase all bonding records
  340. VOID gapBondMgrEraseAllBondings();
  341. // See if NV needs a compaction
  342. VOID osal_snv_compact( NV_COMPACT_THRESHOLD );
  343. // Make sure Bond RAM Shadow is up-to-date
  344. gapBondMgrReadBonds();
  345. }
  346. else
  347. {
  348. eraseAllBonds = TRUE;
  349. }
  350. }
  351. else
  352. {
  353. ret = bleInvalidRange;
  354. }
  355. break;
  356. case GAPBOND_ERASE_SINGLEBOND:
  357. if ( len == (1 + B_ADDR_LEN) )
  358. {
  359. uint8 idx;
  360. uint8 devAddr[B_ADDR_LEN];
  361. // Reverse bytes
  362. VOID osal_revmemcpy( devAddr, (uint8 *)pValue+1, B_ADDR_LEN );
  363. // Resolve address and find index
  364. idx = GAPBondMgr_ResolveAddr( *((uint8 *)pValue), devAddr, NULL );
  365. if ( idx < GAP_BONDINGS_MAX )
  366. {
  367. // Make sure there's no active connection
  368. if ( GAP_NumActiveConnections() == 0 )
  369. {
  370. // Erase bond
  371. gapBondMgrEraseBonding( idx );
  372. // See if NV needs a compaction
  373. VOID osal_snv_compact( NV_COMPACT_THRESHOLD );
  374. // Make sure Bond RAM Shadow is up-to-date
  375. gapBondMgrReadBonds();
  376. }
  377. else
  378. {
  379. // Mark entry to be deleted when disconnected
  380. bondsToDelete[idx] = TRUE;
  381. }
  382. }
  383. else
  384. {
  385. ret = INVALIDPARAMETER;
  386. }
  387. }
  388. else
  389. {
  390. // Parameter is not the correct length
  391. ret = bleInvalidRange;
  392. }
  393. break;
  394. case GAPBOND_AUTO_FAIL_PAIRING:
  395. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
  396. {
  397. gapBond_AutoFail = *((uint8*)pValue);
  398. }
  399. else
  400. {
  401. ret = bleInvalidRange;
  402. }
  403. break;
  404. case GAPBOND_AUTO_FAIL_REASON:
  405. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= SMP_PAIRING_FAILED_REPEATED_ATTEMPTS) )
  406. {
  407. gapBond_AutoFailReason = *((uint8*)pValue);
  408. }
  409. else
  410. {
  411. ret = bleInvalidRange;
  412. }
  413. break;
  414. case GAPBOND_KEYSIZE:
  415. if ( (len == sizeof ( uint8 ))
  416. && ((*((uint8*)pValue) >= MIN_ENC_KEYSIZE) && (*((uint8*)pValue) <= MAX_ENC_KEYSIZE)) )
  417. {
  418. gapBond_KeySize = *((uint8*)pValue);
  419. }
  420. else
  421. {
  422. ret = bleInvalidRange;
  423. }
  424. break;
  425. case GAPBOND_AUTO_SYNC_WL:
  426. if ( len == sizeof( uint8 ) )
  427. {
  428. uint8 oldVal = autoSyncWhiteList;
  429. autoSyncWhiteList = *((uint8 *)pValue);
  430. // only call if parameter changes from FALSE to TRUE
  431. if ( ( oldVal == FALSE ) && ( autoSyncWhiteList == TRUE ) )
  432. {
  433. // make sure bond is updated from NV
  434. gapBondMgrReadBonds();
  435. }
  436. }
  437. else
  438. {
  439. ret = bleInvalidRange;
  440. }
  441. break;
  442. #if ( HOST_CONFIG & CENTRAL_CFG )
  443. case GAPBOND_BOND_FAIL_ACTION:
  444. if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAPBOND_FAIL_TERMINATE_ERASE_BONDS) )
  445. {
  446. gapBond_BondFailOption = *((uint8*)pValue);
  447. }
  448. else
  449. {
  450. ret = bleInvalidRange;
  451. }
  452. break;
  453. #endif
  454. default:
  455. // The param value isn't part of this profile, try the GAP.
  456. if ( (param < TGAP_PARAMID_MAX) && (len == sizeof ( uint16 )) )
  457. {
  458. ret = GAP_SetParamValue( param, *((uint16*)pValue) );
  459. }
  460. else
  461. {
  462. ret = INVALIDPARAMETER;
  463. }
  464. break;
  465. }
  466. return ( ret );
  467. }
  468. /*********************************************************************
  469. * @brief Get a GAP Bond Manager parameter.
  470. *
  471. * Public function defined in gapbondmgr.h.
  472. */
  473. bStatus_t GAPBondMgr_GetParameter( uint16 param, void *pValue )
  474. {
  475. bStatus_t ret = SUCCESS; // return value
  476. switch ( param )
  477. {
  478. case GAPBOND_PAIRING_MODE:
  479. // *((uint8*)pValue) = gapBond_PairingMode;
  480. break;
  481. case GAPBOND_INITIATE_WAIT:
  482. *((uint16*)pValue) = gapBond_InitiateWait;
  483. break;
  484. case GAPBOND_MITM_PROTECTION:
  485. *((uint8*)pValue) = gapBond_MITM;
  486. break;
  487. case GAPBOND_IO_CAPABILITIES:
  488. *((uint8*)pValue) = gapBond_IOCap;
  489. break;
  490. case GAPBOND_OOB_ENABLED:
  491. *((uint8*)pValue) = gapBond_OOBDataFlag;
  492. break;
  493. case GAPBOND_OOB_DATA:
  494. VOID osal_memcpy( pValue, gapBond_OOBData, KEYLEN ) ;
  495. break;
  496. case GAPBOND_BONDING_ENABLED:
  497. *((uint8*)pValue) = gapBond_Bonding;
  498. break;
  499. case GAPBOND_KEY_DIST_LIST:
  500. *((uint8*)pValue) = gapBond_KeyDistList;
  501. break;
  502. case GAPBOND_DEFAULT_PASSCODE:
  503. *((uint32*)pValue) = gapBond_Passcode;
  504. break;
  505. case GAPBOND_AUTO_FAIL_PAIRING:
  506. *((uint8*)pValue) = gapBond_AutoFail;
  507. break;
  508. case GAPBOND_AUTO_FAIL_REASON:
  509. *((uint8*)pValue) = gapBond_AutoFailReason;
  510. break;
  511. case GAPBOND_KEYSIZE:
  512. *((uint8*)pValue) = gapBond_KeySize;
  513. break;
  514. case GAPBOND_AUTO_SYNC_WL:
  515. *((uint8*)pValue) = autoSyncWhiteList;
  516. break;
  517. case GAPBOND_BOND_COUNT:
  518. *((uint8*)pValue) = gapBondMgrBondTotal();
  519. break;
  520. default:
  521. // The param value isn't part of this profile, try the GAP.
  522. if ( param < TGAP_PARAMID_MAX )
  523. {
  524. *((uint16*)pValue) = GAP_GetParamValue( param );
  525. }
  526. else
  527. {
  528. ret = INVALIDPARAMETER;
  529. }
  530. break;
  531. }
  532. return ( ret );
  533. }
  534. /*********************************************************************
  535. * @brief Notify the Bond Manager that a connection has been made.
  536. *
  537. * Public function defined in gapbondmgr.h.
  538. */
  539. bStatus_t GAPBondMgr_LinkEst( uint8 addrType, uint8 *pDevAddr, uint16 connHandle, uint8 role )
  540. {
  541. uint8 idx; // NV Index
  542. uint8 publicAddr[B_ADDR_LEN] // Place to put the public address
  543. = {0, 0, 0, 0, 0, 0};
  544. idx = GAPBondMgr_ResolveAddr( addrType, pDevAddr, publicAddr );
  545. if ( idx < GAP_BONDINGS_MAX )
  546. {
  547. uint8 stateFlags = gapBondMgrGetStateFlags( idx );
  548. smSigningInfo_t signingInfo;
  549. gapBondCharCfg_t charCfg[GAP_CHAR_CFG_MAX]; // Space to read a char cfg record from NV
  550. // On peripheral, load the key information for the bonding
  551. // On central and initiaiting security, load key to initiate encyption
  552. gapBondMgrBondReq( connHandle, idx, stateFlags, role,
  553. ((role == GAP_PROFILE_CENTRAL ) ? TRUE : FALSE) );
  554. // Load the Signing Key
  555. VOID osal_memset( &signingInfo, 0, sizeof ( smSigningInfo_t ) );
  556. if ( osal_snv_read( devCSRKNvID(idx), KEYLEN, signingInfo.srk ) == SUCCESS )
  557. {
  558. if ( osal_isbufset( signingInfo.srk, 0xFF, KEYLEN ) == FALSE )
  559. {
  560. // Load the signing information for this connection
  561. VOID osal_snv_read( devSignCounterNvID(idx), sizeof ( uint32 ), &(signingInfo.signCounter) );
  562. VOID GAP_Signable( connHandle,
  563. ((stateFlags & GAP_BONDED_STATE_AUTHENTICATED) ? TRUE : FALSE),
  564. &signingInfo );
  565. }
  566. }
  567. // Load the characteristic configuration
  568. if ( osal_snv_read( gattCfgNvID(idx), sizeof ( charCfg ), charCfg ) == SUCCESS )
  569. {
  570. gapBondMgrInvertCharCfgItem( charCfg );
  571. for ( uint8 i = 0; i < GAP_CHAR_CFG_MAX; i++ )
  572. {
  573. gapBondCharCfg_t *pItem = &(charCfg[i]);
  574. // Apply the characteristic configuration for this connection
  575. if ( pItem->attrHandle != GATT_INVALID_HANDLE )
  576. {
  577. VOID GATTServApp_UpdateCharCfg( connHandle, pItem->attrHandle,
  578. (uint16)(pItem->value) );
  579. }
  580. }
  581. }
  582. // Has there been a service change?
  583. if ( stateFlags & GAP_BONDED_STATE_SERVICE_CHANGED )
  584. {
  585. VOID GATTServApp_SendServiceChangedInd( connHandle, gapBondMgr_TaskID );
  586. }
  587. }
  588. #if ( HOST_CONFIG & CENTRAL_CFG )
  589. else if ( role == GAP_PROFILE_CENTRAL &&
  590. gapBond_PairingMode[connHandle] == GAPBOND_PAIRING_MODE_INITIATE )
  591. {
  592. // If Central and initiating and not bonded, then initiate pairing
  593. gapBondMgrAuthenticate( connHandle, addrType, NULL );
  594. // Call app state callback
  595. if ( pGapBondCB && pGapBondCB->pairStateCB )
  596. {
  597. pGapBondCB->pairStateCB( connHandle, GAPBOND_PAIRING_STATE_STARTED, SUCCESS );
  598. }
  599. }
  600. #endif
  601. #if ( HOST_CONFIG & PERIPHERAL_CFG )
  602. // If Peripheral and initiating, send a slave security request to
  603. // initiate either pairing or encryption
  604. if ( role == GAP_PROFILE_PERIPHERAL &&
  605. gapBond_PairingMode[connHandle] == GAPBOND_PAIRING_MODE_INITIATE )
  606. {
  607. gapBondMgrSlaveSecurityReq( connHandle );
  608. }
  609. #endif
  610. return ( SUCCESS );
  611. }
  612. /*********************************************************************
  613. * @brief Resolve an address from bonding information.
  614. *
  615. * Public function defined in gapbondmgr.h.
  616. */
  617. uint8 GAPBondMgr_ResolveAddr( uint8 addrType, uint8 *pDevAddr, uint8 *pResolvedAddr )
  618. {
  619. uint8 idx = GAP_BONDINGS_MAX;
  620. switch ( addrType )
  621. {
  622. case ADDRTYPE_PUBLIC:
  623. case ADDRTYPE_STATIC:
  624. idx = gapBondMgrFindAddr( pDevAddr );
  625. if ( (idx < GAP_BONDINGS_MAX) && (pResolvedAddr) )
  626. {
  627. VOID osal_memcpy( pResolvedAddr, pDevAddr, B_ADDR_LEN );
  628. }
  629. break;
  630. case ADDRTYPE_PRIVATE_NONRESOLVE:
  631. // This could be a reconnection address
  632. idx = gapBondMgrFindReconnectAddr( pDevAddr );
  633. if ( (idx < GAP_BONDINGS_MAX) && (pResolvedAddr) )
  634. {
  635. VOID gapBondMgrGetPublicAddr( idx, pResolvedAddr );
  636. }
  637. break;
  638. case ADDRTYPE_PRIVATE_RESOLVE:
  639. // Master's don't use Private Resolvable addresses but just in case
  640. idx = gapBondMgrResolvePrivateAddr( pDevAddr );
  641. if ( (idx < GAP_BONDINGS_MAX) && (pResolvedAddr) )
  642. {
  643. VOID gapBondMgrGetPublicAddr( idx, pResolvedAddr );
  644. }
  645. break;
  646. default:
  647. break;
  648. }
  649. return ( idx );
  650. }
  651. /*********************************************************************
  652. * @brief Set/clear the service change indication in a bond record.
  653. *
  654. * Public function defined in gapbondmgr.h.
  655. */
  656. bStatus_t GAPBondMgr_ServiceChangeInd( uint16 connectionHandle, uint8 setParam )
  657. {
  658. bStatus_t ret = bleNoResources; // return value
  659. if ( connectionHandle == 0xFFFF )
  660. {
  661. uint8 idx; // loop counter
  662. // Run through the bond database and update the Service Change indication
  663. for ( idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  664. {
  665. if ( gapBondMgrChangeState( idx, GAP_BONDED_STATE_SERVICE_CHANGED, setParam ) )
  666. {
  667. ret = SUCCESS;
  668. }
  669. }
  670. // If the service change indication is TRUE, tell the connected devices
  671. if ( setParam )
  672. {
  673. // Run connected database
  674. linkDB_PerformFunc( gapBondMgrSendServiceChange );
  675. }
  676. }
  677. else
  678. {
  679. // Find connection information
  680. linkDBItem_t *pLinkItem = linkDB_Find( connectionHandle );
  681. if ( pLinkItem )
  682. {
  683. uint8 idx; // loop counter
  684. idx = GAPBondMgr_ResolveAddr( pLinkItem->addrType, pLinkItem->addr, NULL );
  685. if ( idx < GAP_BONDINGS_MAX )
  686. {
  687. // Bond found, update it.
  688. VOID gapBondMgrChangeState( idx, GAP_BONDED_STATE_SERVICE_CHANGED, setParam );
  689. ret = SUCCESS;
  690. }
  691. // If the service change indication is TRUE, tell the connected device
  692. if ( setParam )
  693. {
  694. gapBondMgrSendServiceChange( pLinkItem );
  695. }
  696. }
  697. else
  698. {
  699. ret = bleNotConnected;
  700. }
  701. }
  702. return ( ret );
  703. }
  704. /*********************************************************************
  705. * @brief Update the Characteristic Configuration in a bond record.
  706. *
  707. * Public function defined in gapbondmgr.h.
  708. */
  709. bStatus_t GAPBondMgr_UpdateCharCfg( uint16 connectionHandle, uint16 attrHandle, uint16 value )
  710. {
  711. bStatus_t ret = bleNoResources; // return value
  712. if ( connectionHandle == INVALID_CONNHANDLE )
  713. {
  714. uint8 idx; // loop counter
  715. // Run through the bond database and update the Characteristic Configuration
  716. for ( idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  717. {
  718. if ( gapBondMgrUpdateCharCfg( idx, attrHandle, value ) )
  719. {
  720. ret = SUCCESS;
  721. }
  722. }
  723. }
  724. else
  725. {
  726. // Find connection information
  727. linkDBItem_t *pLinkItem = linkDB_Find( connectionHandle );
  728. if ( pLinkItem )
  729. {
  730. uint8 idx = GAPBondMgr_ResolveAddr( pLinkItem->addrType, pLinkItem->addr, NULL );
  731. if ( idx < GAP_BONDINGS_MAX )
  732. {
  733. // Bond found, update it.
  734. VOID gapBondMgrUpdateCharCfg( idx, attrHandle, value );
  735. ret = SUCCESS;
  736. }
  737. }
  738. else
  739. {
  740. ret = bleNotConnected;
  741. }
  742. }
  743. return ( ret );
  744. }
  745. /*********************************************************************
  746. * @brief Register callback functions with the bond manager.
  747. *
  748. * Public function defined in gapbondmgr.h.
  749. */
  750. void GAPBondMgr_Register( gapBondCBs_t *pCB )
  751. {
  752. pGapBondCB = pCB;
  753. if(gapBondMgr_TaskID != INVALID_TASK_ID)
  754. {
  755. // Take over the processing of Authentication messages
  756. VOID GAP_SetParamValue( TGAP_AUTH_TASK_ID, gapBondMgr_TaskID );
  757. // Register with GATT Server App for event messages
  758. GATTServApp_RegisterForMsg( gapBondMgr_TaskID );
  759. }
  760. }
  761. /*********************************************************************
  762. * @brief Respond to a passcode request.
  763. *
  764. * Public function defined in gapbondmgr.h.
  765. */
  766. bStatus_t GAPBondMgr_PasscodeRsp( uint16 connectionHandle, uint8 status, uint32 passcode )
  767. {
  768. bStatus_t ret = SUCCESS;
  769. if ( status == SUCCESS )
  770. {
  771. // Truncate the passcode
  772. passcode = passcode % (GAP_PASSCODE_MAX + 1);
  773. ret = GAP_PasscodeUpdate( passcode, connectionHandle );
  774. if ( ret != SUCCESS )
  775. {
  776. VOID GAP_TerminateAuth( connectionHandle, SMP_PAIRING_FAILED_PASSKEY_ENTRY_FAILED );
  777. }
  778. }
  779. else
  780. {
  781. VOID GAP_TerminateAuth( connectionHandle, status );
  782. }
  783. return ret;
  784. }
  785. /*********************************************************************
  786. * @brief This is a bypass mechanism to allow the bond manager to process
  787. * GAP messages.
  788. *
  789. * Public function defined in gapbondmgr.h.
  790. */
  791. uint8 GAPBondMgr_ProcessGAPMsg( gapEventHdr_t *pMsg )
  792. {
  793. switch ( pMsg->opcode )
  794. {
  795. case GAP_PASSKEY_NEEDED_EVENT:
  796. {
  797. gapPasskeyNeededEvent_t *pPkt = (gapPasskeyNeededEvent_t *)pMsg;
  798. if ( pGapBondCB && pGapBondCB->passcodeCB )
  799. {
  800. // Ask app for a passcode
  801. pGapBondCB->passcodeCB( pPkt->deviceAddr, pPkt->connectionHandle, pPkt->uiInputs, pPkt->uiOutputs );
  802. }
  803. else
  804. {
  805. // No app support, use the default passcode
  806. if ( GAP_PasscodeUpdate( gapBond_Passcode, pPkt->connectionHandle ) != SUCCESS )
  807. {
  808. VOID GAP_TerminateAuth( pPkt->connectionHandle, SMP_PAIRING_FAILED_PASSKEY_ENTRY_FAILED );
  809. }
  810. }
  811. }
  812. break;
  813. case GAP_AUTHENTICATION_COMPLETE_EVENT:
  814. {
  815. gapAuthCompleteEvent_t *pPkt = (gapAuthCompleteEvent_t *)pMsg;
  816. // Should we save bonding information (one save at a time)
  817. if ( (pPkt->hdr.status == SUCCESS) &&
  818. (pPkt->authState & SM_AUTH_STATE_BONDING) &&
  819. (pAuthEvt[pPkt->connectionHandle] == NULL) )
  820. {
  821. gapBondRec_t bondRec;
  822. VOID osal_memset( &bondRec, 0, sizeof ( gapBondRec_t ) ) ;
  823. // Do we have a public address in the data?
  824. if ( pPkt->pIdentityInfo )
  825. {
  826. VOID osal_memcpy( bondRec.publicAddr, pPkt->pIdentityInfo->bd_addr, B_ADDR_LEN );
  827. }
  828. else
  829. {
  830. linkDBItem_t *pLinkItem = linkDB_Find( pPkt->connectionHandle );
  831. if ( pLinkItem )
  832. {
  833. VOID osal_memcpy( bondRec.publicAddr, pLinkItem->addr, B_ADDR_LEN );
  834. }
  835. else
  836. {
  837. // We don't have an address, so ignore the message.
  838. break;
  839. }
  840. }
  841. // Save off of the authentication state
  842. bondRec.stateFlags |= (pPkt->authState & SM_AUTH_STATE_AUTHENTICATED) ? GAP_BONDED_STATE_AUTHENTICATED : 0;
  843. if ( !gapBondMgrAddBond( &bondRec, pPkt ) )
  844. {
  845. // Notify our task to save bonding information in NV
  846. // osal_set_event( gapBondMgr_TaskID, GAP_BOND_SAVE_REC_EVT );
  847. gapBondRecvEvt( pPkt->connectionHandle,&bondRec, pPkt );
  848. // We're not done with this message; it will be freed later
  849. return ( FALSE );
  850. }
  851. }
  852. // Call app state callback in the fail case. Success is handled after GAP_BOND_SAVE_REC_EVT.
  853. if ( pGapBondCB && pGapBondCB->pairStateCB )
  854. {
  855. pGapBondCB->pairStateCB( pPkt->connectionHandle, GAPBOND_PAIRING_STATE_COMPLETE, pPkt->hdr.status );
  856. }
  857. }
  858. break;
  859. case GAP_BOND_COMPLETE_EVENT:
  860. // This message is received when the bonding is complete. If hdr.status is SUCCESS
  861. // then call app state callback. If hdr.status is NOT SUCCESS, the connection will be
  862. // dropped at the LL because of a MIC failure, so again nothing to do.
  863. {
  864. gapBondCompleteEvent_t *pPkt = (gapBondCompleteEvent_t *)pMsg;
  865. #if ( HOST_CONFIG & CENTRAL_CFG )
  866. if ( pPkt->hdr.status == LL_ENC_KEY_REQ_REJECTED )
  867. {
  868. // LTK not found on peripheral device (Pin or Key Missing). See which
  869. // option was configured for unsuccessful bonding.
  870. linkDBItem_t *pLinkItem = linkDB_Find( pPkt->connectionHandle );
  871. if ( pLinkItem )
  872. {
  873. switch ( gapBond_BondFailOption )
  874. {
  875. case GAPBOND_FAIL_INITIATE_PAIRING:
  876. // Initiate pairing
  877. gapBondMgrAuthenticate( pPkt->connectionHandle, pLinkItem->addrType, NULL );
  878. break;
  879. case GAPBOND_FAIL_TERMINATE_LINK:
  880. // Drop connection
  881. GAP_TerminateLinkReq( pLinkItem->taskID, pPkt->connectionHandle, HCI_DISCONNECT_AUTH_FAILURE );
  882. break;
  883. case GAPBOND_FAIL_TERMINATE_ERASE_BONDS:
  884. // Set up bond manager to erase all existing bonds after connection terminates
  885. VOID GAPBondMgr_SetParameter( GAPBOND_ERASE_ALLBONDS, 0, NULL );
  886. // Drop connection
  887. GAP_TerminateLinkReq( pLinkItem->taskID, pPkt->connectionHandle, HCI_DISCONNECT_AUTH_FAILURE );
  888. break;
  889. case GAPBOND_FAIL_NO_ACTION:
  890. // fall through
  891. default:
  892. // do nothing
  893. break;
  894. }
  895. }
  896. }
  897. #endif
  898. if ( pGapBondCB && pGapBondCB->pairStateCB )
  899. {
  900. pGapBondCB->pairStateCB( pPkt->connectionHandle, GAPBOND_PAIRING_STATE_BONDED, pMsg->hdr.status );
  901. }
  902. }
  903. break;
  904. case GAP_SIGNATURE_UPDATED_EVENT:
  905. {
  906. uint8 idx;
  907. gapSignUpdateEvent_t *pPkt = (gapSignUpdateEvent_t *)pMsg;
  908. idx = GAPBondMgr_ResolveAddr( pPkt->addrType, pPkt->devAddr, NULL );
  909. if ( idx < GAP_BONDINGS_MAX )
  910. {
  911. // Save the sign counter
  912. VOID osal_snv_write( devSignCounterNvID(idx), sizeof ( uint32 ), &(pPkt->signCounter) );
  913. }
  914. }
  915. break;
  916. #if ( HOST_CONFIG & PERIPHERAL_CFG )
  917. case GAP_PAIRING_REQ_EVENT:
  918. {
  919. gapPairingReqEvent_t *pPkt = (gapPairingReqEvent_t *)pMsg;
  920. if ( gapBond_AutoFail != FALSE )
  921. {
  922. // Auto Fail TEST MODE (DON'T USE THIS) - Sends pre-setup reason
  923. VOID GAP_TerminateAuth( pPkt->connectionHandle, gapBond_AutoFailReason );
  924. }
  925. else if ( gapBond_PairingMode[pPkt->connectionHandle] == GAPBOND_PAIRING_MODE_NO_PAIRING )
  926. {
  927. // No Pairing - Send error
  928. VOID GAP_TerminateAuth( pPkt->connectionHandle, SMP_PAIRING_FAILED_NOT_SUPPORTED );
  929. }
  930. else
  931. {
  932. linkDBItem_t *pLinkItem = linkDB_Find( pPkt->connectionHandle );
  933. // Requesting bonding?
  934. if ( pPkt->pairReq.authReq & SM_AUTH_STATE_BONDING )
  935. {
  936. if ( pLinkItem )
  937. {
  938. if ( (pLinkItem->addrType != ADDRTYPE_PUBLIC) && (pPkt->pairReq.keyDist.mIdKey == FALSE) )
  939. {
  940. uint8 publicAddr[B_ADDR_LEN];
  941. // Check if we already have the public address in NV
  942. if ( GAPBondMgr_ResolveAddr(pLinkItem->addrType, pLinkItem->addr, publicAddr ) == FALSE )
  943. {
  944. // Can't bond to a non-public address if we don't know the public address
  945. VOID GAP_TerminateAuth( pPkt->connectionHandle, SMP_PAIRING_FAILED_AUTH_REQ );
  946. break;
  947. }
  948. }
  949. }
  950. else
  951. {
  952. // Can't find the connection, ignore the message
  953. break;
  954. }
  955. }
  956. // Send pairing response
  957. gapBondMgrAuthenticate( pPkt->connectionHandle, pLinkItem->addrType, &(pPkt->pairReq) );
  958. // Call app state callback
  959. if ( pGapBondCB && pGapBondCB->pairStateCB )
  960. {
  961. pGapBondCB->pairStateCB( pPkt->connectionHandle, GAPBOND_PAIRING_STATE_STARTED, SUCCESS );
  962. }
  963. }
  964. }
  965. break;
  966. #endif
  967. #if ( HOST_CONFIG & CENTRAL_CFG )
  968. case GAP_SLAVE_REQUESTED_SECURITY_EVENT:
  969. {
  970. uint16 connHandle = ((gapSlaveSecurityReqEvent_t *)pMsg)->connectionHandle;
  971. uint8 idx;
  972. uint8 publicAddr[B_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
  973. linkDBItem_t *pLink = linkDB_Find( connHandle );
  974. // If link found and not already initiating security
  975. if (pLink != NULL && gapBond_PairingMode[connHandle] != GAPBOND_PAIRING_MODE_INITIATE)
  976. {
  977. // If already bonded initiate encryption
  978. idx = GAPBondMgr_ResolveAddr( pLink->addrType, pLink->addr, publicAddr );
  979. if ( idx < GAP_BONDINGS_MAX )
  980. {
  981. gapBondMgrBondReq( connHandle, idx, gapBondMgrGetStateFlags( idx ),
  982. GAP_PROFILE_CENTRAL, TRUE );
  983. }
  984. // Else if no pairing allowed
  985. else if ( gapBond_PairingMode[connHandle] == GAPBOND_PAIRING_MODE_NO_PAIRING )
  986. {
  987. // Send error
  988. VOID GAP_TerminateAuth( connHandle, SMP_PAIRING_FAILED_NOT_SUPPORTED );
  989. }
  990. // Else if waiting for request
  991. else if (gapBond_PairingMode[connHandle] == GAPBOND_PAIRING_MODE_WAIT_FOR_REQ)
  992. {
  993. // Initiate pairing
  994. gapBondMgrAuthenticate( connHandle, pLink->addrType, NULL );
  995. }
  996. }
  997. }
  998. break;
  999. #endif
  1000. case GAP_LINK_TERMINATED_EVENT:
  1001. if ( GAP_NumActiveConnections() == 0 )
  1002. {
  1003. // See if we're asked to erase all bonding records
  1004. if ( eraseAllBonds == TRUE )
  1005. {
  1006. VOID gapBondMgrEraseAllBondings();
  1007. eraseAllBonds = FALSE;
  1008. // Reset bonds to delete table
  1009. osal_memset( bondsToDelete, FALSE, sizeof( bondsToDelete ) );
  1010. }
  1011. else
  1012. {
  1013. // See if we're asked to erase any single bonding records
  1014. for (uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++)
  1015. {
  1016. if ( bondsToDelete[idx] == TRUE )
  1017. {
  1018. VOID gapBondMgrEraseBonding( idx );
  1019. bondsToDelete[idx] = FALSE;
  1020. }
  1021. }
  1022. }
  1023. // See if NV needs a compaction
  1024. VOID osal_snv_compact( NV_COMPACT_THRESHOLD );
  1025. // Make sure Bond RAM Shadow is up-to-date
  1026. gapBondMgrReadBonds();
  1027. }
  1028. break;
  1029. default:
  1030. break;
  1031. }
  1032. return ( TRUE );
  1033. }
  1034. /*********************************************************************
  1035. * LOCAL FUNCTION PROTOTYPES
  1036. */
  1037. /*********************************************************************
  1038. * @fn gapBondMgrChangeState
  1039. *
  1040. * @brief Change a state flag in the stateFlags field of the bond record.
  1041. *
  1042. * @param idx - Bond NV index
  1043. * @param state - state flage to set or clear
  1044. * @param set - TRUE to set the flag, FALSE to clear the flag
  1045. *
  1046. * @return TRUE if NV Record exists, FALSE if NV Record is empty
  1047. */
  1048. static uint8 gapBondMgrChangeState( uint8 idx, uint16 state, uint8 set )
  1049. {
  1050. gapBondRec_t bondRec; // Space to read a Bond record from NV
  1051. // Look for public address that is used (not all 0xFF's)
  1052. if ( (osal_snv_read( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec ) == SUCCESS)
  1053. && (osal_isbufset( bondRec.publicAddr, 0xFF, B_ADDR_LEN ) == FALSE) )
  1054. {
  1055. // Update the state of the bonded device.
  1056. uint8 stateFlags = bondRec.stateFlags;
  1057. if ( set )
  1058. {
  1059. stateFlags |= state;
  1060. }
  1061. else
  1062. {
  1063. stateFlags &= ~(state);
  1064. }
  1065. if ( stateFlags != bondRec.stateFlags )
  1066. {
  1067. bondRec.stateFlags = stateFlags;
  1068. VOID osal_snv_write( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec );
  1069. }
  1070. return ( TRUE );
  1071. }
  1072. return ( FALSE );
  1073. }
  1074. /*********************************************************************
  1075. * @fn gapBondMgrUpdateCharCfg
  1076. *
  1077. * @brief Update the Characteristic Configuration of the bond record.
  1078. *
  1079. * @param idx - Bond NV index
  1080. * @param attrHandle - attribute handle (0 means all handles)
  1081. * @param value - characteristic configuration value
  1082. *
  1083. * @return TRUE if NV Record exists, FALSE if NV Record is empty
  1084. */
  1085. static uint8 gapBondMgrUpdateCharCfg( uint8 idx, uint16 attrHandle, uint16 value )
  1086. {
  1087. gapBondRec_t bondRec; // Space to read a Bond record from NV
  1088. // Look for public address that is used (not all 0xFF's)
  1089. if ( ( osal_snv_read( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec ) == SUCCESS )
  1090. && ( osal_isbufset( bondRec.publicAddr, 0xFF, B_ADDR_LEN ) == FALSE ) )
  1091. {
  1092. gapBondCharCfg_t charCfg[GAP_CHAR_CFG_MAX]; // Space to read a char cfg record from NV
  1093. if ( osal_snv_read( gattCfgNvID(idx), sizeof ( charCfg ), charCfg ) == SUCCESS )
  1094. {
  1095. uint8 update = FALSE;
  1096. gapBondMgrInvertCharCfgItem( charCfg );
  1097. if ( attrHandle == GATT_INVALID_HANDLE )
  1098. {
  1099. if ( osal_isbufset( (uint8 *)charCfg, 0x00, sizeof ( charCfg ) ) == FALSE )
  1100. {
  1101. // Clear all characteristic configuration for this device
  1102. VOID osal_memset( (void *)charCfg, 0x00, sizeof ( charCfg ) );
  1103. update = TRUE;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. gapBondCharCfg_t *pItem = gapBondMgrFindCharCfgItem( attrHandle, charCfg );
  1109. if ( pItem == NULL )
  1110. {
  1111. // Must be a new item; ignore if the value is no operation (default)
  1112. if ( ( value == GATT_CFG_NO_OPERATION ) ||
  1113. ( ( pItem = gapBondMgrFindCharCfgItem( GATT_INVALID_HANDLE, charCfg ) ) == NULL ) )
  1114. {
  1115. return ( FALSE ); // No empty entry found
  1116. }
  1117. pItem->attrHandle = attrHandle;
  1118. }
  1119. if ( pItem->value != value )
  1120. {
  1121. // Update characteristic configuration
  1122. pItem->value = (uint8)value;
  1123. if ( value == GATT_CFG_NO_OPERATION )
  1124. {
  1125. // Erease the item
  1126. pItem->attrHandle = GATT_INVALID_HANDLE;
  1127. }
  1128. update = TRUE;
  1129. }
  1130. }
  1131. // Update the characteristic configuration of the bonded device.
  1132. if ( update )
  1133. {
  1134. gapBondMgrInvertCharCfgItem( charCfg );
  1135. VOID osal_snv_write( gattCfgNvID(idx), sizeof( charCfg ), charCfg );
  1136. }
  1137. }
  1138. return ( TRUE );
  1139. }
  1140. return ( FALSE );
  1141. }
  1142. /*********************************************************************
  1143. * @fn gapBondMgrFindCharCfgItem
  1144. *
  1145. * @brief Find the Characteristic Configuration for a given attribute.
  1146. * Uses the attribute handle to search the charactersitic
  1147. * configuration table of a bonded device.
  1148. *
  1149. * @param attrHandle - attribute handle.
  1150. * @param charCfgTbl - characteristic configuration table.
  1151. *
  1152. * @return pointer to the found item. NULL, otherwise.
  1153. */
  1154. static gapBondCharCfg_t *gapBondMgrFindCharCfgItem( uint16 attrHandle,
  1155. gapBondCharCfg_t *charCfgTbl )
  1156. {
  1157. for ( uint8 i = 0; i < GAP_CHAR_CFG_MAX; i++ )
  1158. {
  1159. if ( charCfgTbl[i].attrHandle == attrHandle )
  1160. {
  1161. return ( &(charCfgTbl[i]) );
  1162. }
  1163. }
  1164. return ( (gapBondCharCfg_t *)NULL );
  1165. }
  1166. /*********************************************************************
  1167. * @fn gapBondMgrFindCharCfgItem
  1168. *
  1169. * @brief Invert the Characteristic Configuration for a given client.
  1170. *
  1171. * @param charCfgTbl - characteristic configuration table.
  1172. *
  1173. * @return none.
  1174. */
  1175. static void gapBondMgrInvertCharCfgItem( gapBondCharCfg_t *charCfgTbl )
  1176. {
  1177. for ( uint8 i = 0; i < GAP_CHAR_CFG_MAX; i++ )
  1178. {
  1179. charCfgTbl[i].attrHandle = ~(charCfgTbl[i].attrHandle);
  1180. charCfgTbl[i].value = ~(charCfgTbl[i].value);
  1181. }
  1182. }
  1183. /*********************************************************************
  1184. * @fn gapBondMgrAddBond
  1185. *
  1186. * @brief Save a bond from a GAP Auth Complete Event
  1187. *
  1188. * @param pBondRec - basic bond record
  1189. * @param pLocalLTK - LTK used by this device during pairing
  1190. * @param pDevLTK - LTK used by the connected device during pairing
  1191. * @param pIRK - IRK used by the connected device during pairing
  1192. * @param pSRK - SRK used by the connected device during pairing
  1193. * @param signCounter - Sign counter used by the connected device during pairing
  1194. *
  1195. * @return TRUE, if done processing bond record. FALSE, otherwise.
  1196. */
  1197. static uint8 gapBondMgrAddBond( gapBondRec_t *pBondRec, gapAuthCompleteEvent_t *pPkt )
  1198. {
  1199. // See if this is a new bond record
  1200. if ( pAuthEvt[pPkt->connectionHandle] == NULL )
  1201. {
  1202. // Make sure we have bonding info
  1203. if ( ( pBondRec == NULL ) || ( pPkt == NULL ) )
  1204. {
  1205. return ( TRUE );
  1206. }
  1207. // First see if we already have an existing bond for this device
  1208. bondIdx = gapBondMgrFindAddr( pBondRec->publicAddr );
  1209. if ( bondIdx >= GAP_BONDINGS_MAX )
  1210. {
  1211. bondIdx = gapBondMgrFindEmpty();
  1212. }
  1213. #if(GAP_BOND_MGR_INDEX_REPLACE)
  1214. /*replace bondIdx*/
  1215. if(bondIdx==GAP_BONDINGS_MAX)
  1216. {
  1217. bondIdx = (bondReplaceCnt++)%GAP_BONDINGS_MAX;
  1218. BOND_LOG("replace BondIdx %d \n",bondIdx);
  1219. }
  1220. #endif
  1221. BOND_LOG("BondMgrAdd idx=%03d\n",bondIdx);
  1222. }
  1223. if ( bondIdx < GAP_BONDINGS_MAX )
  1224. {
  1225. // See if this is a new bond record
  1226. if ( pAuthEvt[pPkt->connectionHandle] == NULL )
  1227. {
  1228. gapBondCharCfg_t charCfg[GAP_CHAR_CFG_MAX];
  1229. // Save the main information
  1230. osal_snv_write( mainRecordNvID(bondIdx), sizeof ( gapBondRec_t ), pBondRec );
  1231. // Write out FF's over the charactersitic configuration entry, to overwrite
  1232. // any previous bond data that may have been stored
  1233. VOID osal_memset( charCfg, 0xFF, sizeof ( charCfg ) );
  1234. VOID osal_snv_write( gattCfgNvID(bondIdx), sizeof ( charCfg ), charCfg );
  1235. // Update Bond RAM Shadow just with the newly added bond entry
  1236. VOID osal_memcpy( &(bonds[bondIdx]), pBondRec, sizeof ( gapBondRec_t ) );
  1237. // Keep the OSAL message to store the security keys later - will be freed then
  1238. pAuthEvt[pPkt->connectionHandle] = pPkt;
  1239. }
  1240. else
  1241. {
  1242. // If available, save the LTK information
  1243. if ( pAuthEvt[pPkt->connectionHandle]->pSecurityInfo )
  1244. {
  1245. VOID osal_snv_write( localLTKNvID(bondIdx), sizeof ( gapBondLTK_t ), pAuthEvt[pPkt->connectionHandle]->pSecurityInfo );
  1246. pAuthEvt[pPkt->connectionHandle]->pSecurityInfo = NULL;
  1247. }
  1248. // If availabe, save the connected device's LTK information
  1249. if ( pAuthEvt[pPkt->connectionHandle]->pDevSecInfo )
  1250. {
  1251. VOID osal_snv_write( devLTKNvID(bondIdx), sizeof ( gapBondLTK_t ), pAuthEvt[pPkt->connectionHandle]->pDevSecInfo );
  1252. pAuthEvt[pPkt->connectionHandle]->pDevSecInfo = NULL;
  1253. }
  1254. // If available, save the connected device's IRK
  1255. if ( pAuthEvt[pPkt->connectionHandle]->pIdentityInfo )
  1256. {
  1257. VOID osal_snv_write( devIRKNvID(bondIdx), KEYLEN, pAuthEvt[pPkt->connectionHandle]->pIdentityInfo->irk );
  1258. pAuthEvt[pPkt->connectionHandle]->pIdentityInfo = NULL;
  1259. }
  1260. // If available, save the connected device's Signature information
  1261. if ( pAuthEvt[pPkt->connectionHandle]->pSigningInfo )
  1262. {
  1263. VOID osal_snv_write( devCSRKNvID(bondIdx), KEYLEN, pAuthEvt[pPkt->connectionHandle]->pSigningInfo->srk );
  1264. VOID osal_snv_write( devSignCounterNvID(bondIdx), sizeof ( uint32 ), &(pAuthEvt[pPkt->connectionHandle]->pSigningInfo->signCounter) );
  1265. pAuthEvt[pPkt->connectionHandle]->pSigningInfo = NULL;
  1266. }
  1267. // else
  1268. {
  1269. if ( autoSyncWhiteList )
  1270. {
  1271. gapBondMgr_SyncWhiteList();
  1272. }
  1273. // Update the GAP Privacy Flag Properties
  1274. gapBondSetupPrivFlag();
  1275. return ( TRUE );
  1276. }
  1277. }
  1278. // We have more info to store
  1279. return ( FALSE );
  1280. }
  1281. return ( TRUE );
  1282. }
  1283. /*********************************************************************
  1284. * @fn gapBondMgrGetStateFlags
  1285. *
  1286. * @brief Gets the state flags field of a bond record in NV
  1287. *
  1288. * @param idx
  1289. *
  1290. * @return stateFlags field
  1291. */
  1292. static uint8 gapBondMgrGetStateFlags( uint8 idx )
  1293. {
  1294. gapBondRec_t bondRec;
  1295. if ( osal_snv_read( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec ) == SUCCESS )
  1296. {
  1297. return ( bondRec.stateFlags );
  1298. }
  1299. return ( 0 );
  1300. }
  1301. /*********************************************************************
  1302. * @fn gapBondMgrGetPublicAddr
  1303. *
  1304. * @brief Copy the public Address from a bonding record
  1305. *
  1306. * @param idx - Bond record index
  1307. * @param pAddr - a place to put the public address from NV
  1308. *
  1309. * @return SUCCESS if successful.
  1310. * Otherwise failure.
  1311. */
  1312. static bStatus_t gapBondMgrGetPublicAddr( uint8 idx, uint8 *pAddr )
  1313. {
  1314. bStatus_t stat; // return value
  1315. gapBondRec_t bondRec; // Work space for main bond record
  1316. // Check parameters
  1317. if ( (idx >= GAP_BONDINGS_MAX) || (pAddr == NULL) )
  1318. {
  1319. return ( INVALIDPARAMETER );
  1320. }
  1321. stat = osal_snv_read( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec );
  1322. if ( stat == SUCCESS )
  1323. {
  1324. VOID osal_memcpy( pAddr, bondRec.publicAddr, B_ADDR_LEN );
  1325. }
  1326. return ( stat );
  1327. }
  1328. /*********************************************************************
  1329. * @fn gapBondMgrFindReconnectAddr
  1330. *
  1331. * @brief Look through the bonding entries to find a
  1332. * reconnection address.
  1333. *
  1334. * @param pReconnectAddr - device address to look for
  1335. *
  1336. * @return index to found bonding (0 - (GAP_BONDINGS_MAX-1),
  1337. * GAP_BONDINGS_MAX if no empty entries
  1338. */
  1339. static uint8 gapBondMgrFindReconnectAddr( uint8 *pReconnectAddr )
  1340. {
  1341. // Item doesn't exist, so create all the items
  1342. for ( uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  1343. {
  1344. // compare reconnection address
  1345. if ( osal_memcmp( bonds[idx].reconnectAddr, pReconnectAddr, B_ADDR_LEN ) )
  1346. {
  1347. return ( idx ); // Found it
  1348. }
  1349. }
  1350. return ( GAP_BONDINGS_MAX );
  1351. }
  1352. /*********************************************************************
  1353. * @fn gapBondMgrFindAddr
  1354. *
  1355. * @brief Look through the bonding entries to find an address.
  1356. *
  1357. * @param pDevAddr - device address to look for
  1358. *
  1359. * @return index to empty bonding (0 - (GAP_BONDINGS_MAX-1),
  1360. * GAP_BONDINGS_MAX if no empty entries
  1361. */
  1362. static uint8 gapBondMgrFindAddr( uint8 *pDevAddr )
  1363. {
  1364. // Item doesn't exist, so create all the items
  1365. for ( uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  1366. {
  1367. // Read in NV Main Bond Record and compare public address
  1368. if ( osal_memcmp( bonds[idx].publicAddr, pDevAddr, B_ADDR_LEN ) )
  1369. {
  1370. return ( idx ); // Found it
  1371. }
  1372. }
  1373. return ( GAP_BONDINGS_MAX );
  1374. }
  1375. /*********************************************************************
  1376. * @fn gapBondMgrResolvePrivateAddr
  1377. *
  1378. * @brief Look through the NV bonding entries to resolve a private
  1379. * address.
  1380. *
  1381. * @param pDevAddr - device address to look for
  1382. *
  1383. * @return index to found bonding (0 - (GAP_BONDINGS_MAX-1),
  1384. * GAP_BONDINGS_MAX if no entry found
  1385. */
  1386. static uint8 gapBondMgrResolvePrivateAddr( uint8 *pDevAddr )
  1387. {
  1388. for ( uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  1389. {
  1390. uint8 IRK[KEYLEN];
  1391. // Read in NV IRK Record and compare resovable address
  1392. if ( osal_snv_read( devIRKNvID(idx), KEYLEN, IRK ) == SUCCESS )
  1393. {
  1394. if ( ( osal_isbufset( IRK, 0xFF, KEYLEN ) == FALSE ) &&
  1395. ( GAP_ResolvePrivateAddr( IRK, pDevAddr ) == SUCCESS ) )
  1396. {
  1397. return ( idx ); // Found it
  1398. }
  1399. }
  1400. }
  1401. return ( GAP_BONDINGS_MAX );
  1402. }
  1403. /*********************************************************************
  1404. * @fn gapBondMgrReadBonds
  1405. *
  1406. * @brief Read through NV and store them in RAM.
  1407. *
  1408. * @param none
  1409. *
  1410. * @return none
  1411. */
  1412. static void gapBondMgrReadBonds( void )
  1413. {
  1414. for ( uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  1415. {
  1416. // See if the entry exists in NV
  1417. if ( osal_snv_read( mainRecordNvID(idx), sizeof( gapBondRec_t ), &(bonds[idx]) ) != SUCCESS )
  1418. {
  1419. // Can't read the entry, assume that it doesn't exist
  1420. VOID osal_memset( bonds[idx].publicAddr, 0xFF, B_ADDR_LEN );
  1421. VOID osal_memset( bonds[idx].reconnectAddr, 0xFF, B_ADDR_LEN );
  1422. bonds[idx].stateFlags = 0;
  1423. }
  1424. }
  1425. if ( autoSyncWhiteList )
  1426. {
  1427. gapBondMgr_SyncWhiteList();
  1428. }
  1429. // Update the GAP Privacy Flag Properties
  1430. gapBondSetupPrivFlag();
  1431. }
  1432. /*********************************************************************
  1433. * @fn gapBondMgrFindEmpty
  1434. *
  1435. * @brief Look through the bonding NV entries to find an empty.
  1436. *
  1437. * @param none
  1438. *
  1439. * @return index to empty bonding (0 - (GAP_BONDINGS_MAX-1),
  1440. * GAP_BONDINGS_MAX if no empty entries
  1441. */
  1442. static uint8 gapBondMgrFindEmpty( void )
  1443. {
  1444. // Item doesn't exist, so create all the items
  1445. for ( uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  1446. {
  1447. // Look for public address of all 0xFF's
  1448. if ( osal_isbufset( bonds[idx].publicAddr, 0xFF, B_ADDR_LEN ) )
  1449. {
  1450. return ( idx ); // Found one
  1451. }
  1452. }
  1453. return ( GAP_BONDINGS_MAX );
  1454. }
  1455. /*********************************************************************
  1456. * @fn gapBondMgrBondTotal
  1457. *
  1458. * @brief Look through the bonding NV entries calculate the number
  1459. * entries.
  1460. *
  1461. * @param none
  1462. *
  1463. * @return total number of bonds found
  1464. */
  1465. static uint8 gapBondMgrBondTotal( void )
  1466. {
  1467. uint8 numBonds = 0;
  1468. // Item doesn't exist, so create all the items
  1469. for ( uint8 idx = 0; idx < GAP_BONDINGS_MAX; idx++ )
  1470. {
  1471. // Look for public address that are not 0xFF's
  1472. if ( osal_isbufset( bonds[idx].publicAddr, 0xFF, B_ADDR_LEN ) == FALSE )
  1473. {
  1474. numBonds++; // Found one
  1475. }
  1476. }
  1477. return ( numBonds );
  1478. }
  1479. /*********************************************************************
  1480. * @fn gapBondMgrEraseAllBondings
  1481. *
  1482. * @brief Write all 0xFF's to all of the bonding entries
  1483. *
  1484. * @param none
  1485. *
  1486. * @return SUCCESS if successful.
  1487. * Otherwise, NV_OPER_FAILED for failure.
  1488. */
  1489. static bStatus_t gapBondMgrEraseAllBondings( void )
  1490. {
  1491. bStatus_t stat = SUCCESS; // return value
  1492. // Item doesn't exist, so create all the items
  1493. for ( uint8 idx = 0; (idx < GAP_BONDINGS_MAX) && (stat == SUCCESS); idx++ )
  1494. {
  1495. // Erasing will write/create a bonding entry
  1496. stat = gapBondMgrEraseBonding( idx );
  1497. }
  1498. return ( stat );
  1499. }
  1500. /*********************************************************************
  1501. * @fn gapBondMgrEraseBonding
  1502. *
  1503. * @brief Write all 0xFF's to the complete bonding record
  1504. *
  1505. * @param idx - bonding index
  1506. *
  1507. * @return SUCCESS if successful.
  1508. * Otherwise, NV_OPER_FAILED for failure.
  1509. */
  1510. static bStatus_t gapBondMgrEraseBonding( uint8 idx )
  1511. {
  1512. bStatus_t ret;
  1513. gapBondRec_t bondRec;
  1514. if ( idx == bondIdx )
  1515. {
  1516. // Stop ongoing bond store process to prevent any invalid data be written.
  1517. // osal_clear_event( gapBondMgr_TaskID, GAP_BOND_SYNC_CC_EVT );
  1518. // osal_clear_event( gapBondMgr_TaskID, GAP_BOND_SAVE_REC_EVT );
  1519. // gapBondFreeAuthEvt();
  1520. }
  1521. // First see if bonding record exists in NV, then write all 0xFF's to it
  1522. if ( ( osal_snv_read( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec ) == SUCCESS )
  1523. && (osal_isbufset( bondRec.publicAddr, 0xFF, B_ADDR_LEN ) == FALSE) )
  1524. {
  1525. gapBondLTK_t ltk;
  1526. gapBondCharCfg_t charCfg[GAP_CHAR_CFG_MAX];
  1527. VOID osal_memset( &bondRec, 0xFF, sizeof ( gapBondRec_t ) );
  1528. VOID osal_memset( &ltk, 0xFF, sizeof ( gapBondLTK_t ) );
  1529. VOID osal_memset( charCfg, 0xFF, sizeof ( charCfg ) );
  1530. // Write out FF's over the entire bond entry.
  1531. ret = osal_snv_write( mainRecordNvID(idx), sizeof ( gapBondRec_t ), &bondRec );
  1532. ret |= osal_snv_write( localLTKNvID(idx), sizeof ( gapBondLTK_t ), &ltk );
  1533. ret |= osal_snv_write( devLTKNvID(idx), sizeof ( gapBondLTK_t ), &ltk );
  1534. ret |= osal_snv_write( devIRKNvID(idx), KEYLEN, ltk.LTK );
  1535. ret |= osal_snv_write( devCSRKNvID(idx), KEYLEN, ltk.LTK );
  1536. ret |= osal_snv_write( devSignCounterNvID(idx), sizeof ( uint32 ), ltk.LTK );
  1537. // Write out FF's over the charactersitic configuration entry.
  1538. ret |= osal_snv_write( gattCfgNvID(idx), sizeof ( charCfg ), charCfg );
  1539. }
  1540. else
  1541. {
  1542. ret = SUCCESS;
  1543. }
  1544. return ( ret );
  1545. }
  1546. /*********************************************************************
  1547. * @brief Task Initialization function.
  1548. *
  1549. * Internal function defined in gapbondmgr.h.
  1550. */
  1551. void GAPBondMgr_Init( uint8 task_id )
  1552. {
  1553. gapBondMgr_TaskID = task_id; // Save task ID
  1554. // Setup Bond RAM Shadow
  1555. gapBondMgrReadBonds();
  1556. }
  1557. /*********************************************************************
  1558. * @brief Task Event Processor function.
  1559. *
  1560. * Internal function defined in gapbondmgr.h.
  1561. */
  1562. uint16 GAPBondMgr_ProcessEvent( uint8 task_id, uint16 events )
  1563. {
  1564. VOID task_id; // OSAL required parameter that isn't used in this function
  1565. if ( events & SYS_EVENT_MSG )
  1566. {
  1567. uint8 *pMsg;
  1568. if ( (pMsg = osal_msg_receive( gapBondMgr_TaskID )) != NULL )
  1569. {
  1570. if ( gapBondMgr_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ) )
  1571. {
  1572. // Release the OSAL message
  1573. VOID osal_msg_deallocate( pMsg );
  1574. }
  1575. }
  1576. // return unprocessed events
  1577. return (events ^ SYS_EVENT_MSG);
  1578. }
  1579. // if ( events & GAP_BOND_SAVE_REC_EVT )
  1580. // {
  1581. // // Save bonding record in NV
  1582. // if ( gapBondMgrAddBond( NULL, NULL ) )
  1583. // {
  1584. // // Notify our task to update NV with CCC values stored in GATT database
  1585. // osal_set_event( gapBondMgr_TaskID, GAP_BOND_SYNC_CC_EVT );
  1586. // return (events ^ GAP_BOND_SAVE_REC_EVT);
  1587. // }
  1588. // return ( GAP_BOND_SAVE_REC_EVT );
  1589. // }
  1590. // if ( events & GAP_BOND_SYNC_CC_EVT )
  1591. // {
  1592. // // Update NV to have same CCC values as GATT database
  1593. // // Note: pAuthEvt is a global variable used for deferring the storage
  1594. // if ( gapBondMgr_SyncCharCfg( pAuthEvt->connectionHandle ) )
  1595. // {
  1596. // if ( pGapBondCB && pGapBondCB->pairStateCB )
  1597. // {
  1598. // // Assume SUCCESS since we got this far.
  1599. // pGapBondCB->pairStateCB( pAuthEvt->connectionHandle, GAPBOND_PAIRING_STATE_COMPLETE, SUCCESS );
  1600. // }
  1601. // // We're done storing bond record and CCC values in NV
  1602. // gapBondFreeAuthEvt();
  1603. // return (events ^ GAP_BOND_SYNC_CC_EVT);
  1604. // }
  1605. // return ( GAP_BOND_SYNC_CC_EVT );
  1606. // }
  1607. // Discard unknown events
  1608. return 0;
  1609. }
  1610. /*********************************************************************
  1611. * @fn gapBondMgr_ProcessOSALMsg
  1612. *
  1613. * @brief Process an incoming task message.
  1614. *
  1615. * @param pMsg - message to process
  1616. *
  1617. * @return TRUE if safe to deallocate incoming message, FALSE otherwise.
  1618. */
  1619. static uint8 gapBondMgr_ProcessOSALMsg( osal_event_hdr_t *pMsg )
  1620. {
  1621. uint8 safeToDealloc = TRUE;
  1622. switch ( pMsg->event )
  1623. {
  1624. case GAP_MSG_EVENT:
  1625. safeToDealloc = GAPBondMgr_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
  1626. break;
  1627. case GATT_MSG_EVENT:
  1628. gapBondMgr_ProcessGATTMsg( (gattMsgEvent_t *)pMsg );
  1629. break;
  1630. case GATT_SERV_MSG_EVENT:
  1631. gapBondMgr_ProcessGATTServMsg( (gattEventHdr_t *)pMsg );
  1632. break;
  1633. default:
  1634. break;
  1635. }
  1636. return ( safeToDealloc );
  1637. }
  1638. /*********************************************************************
  1639. * @fn GAPBondMgr_CheckNVLen
  1640. *
  1641. * @brief This function will check the length of an NV Item.
  1642. *
  1643. * @param id - NV ID.
  1644. * @param len - lengths in bytes of item.
  1645. *
  1646. * @return SUCCESS or FAILURE
  1647. */
  1648. uint8 GAPBondMgr_CheckNVLen( uint8 id, uint8 len )
  1649. {
  1650. uint8 stat = FAILURE;
  1651. // Convert to index
  1652. switch ( (id - BLE_NVID_GAP_BOND_START) % GAP_BOND_REC_IDS )
  1653. {
  1654. case GAP_BOND_REC_ID_OFFSET:
  1655. if ( len == sizeof ( gapBondRec_t ) )
  1656. {
  1657. stat = SUCCESS;
  1658. }
  1659. break;
  1660. case GAP_BOND_LOCAL_LTK_OFFSET:
  1661. case GAP_BOND_DEV_LTK_OFFSET:
  1662. if ( len == sizeof ( gapBondLTK_t ) )
  1663. {
  1664. stat = SUCCESS;
  1665. }
  1666. break;
  1667. case GAP_BOND_DEV_IRK_OFFSET:
  1668. case GAP_BOND_DEV_CSRK_OFFSET:
  1669. if ( len == KEYLEN )
  1670. {
  1671. stat = SUCCESS;
  1672. }
  1673. break;
  1674. case GAP_BOND_DEV_SIGN_COUNTER_OFFSET:
  1675. if ( len == sizeof ( uint32 ) )
  1676. {
  1677. stat = SUCCESS;
  1678. }
  1679. break;
  1680. default:
  1681. break;
  1682. }
  1683. return ( stat );
  1684. }
  1685. /*********************************************************************
  1686. * @fn gapBondMgr_ProcessGATTMsg
  1687. *
  1688. * @brief Process an incoming GATT message.
  1689. *
  1690. * @param pMsg - pointer to received message
  1691. *
  1692. * @return none
  1693. */
  1694. static void gapBondMgr_ProcessGATTMsg( gattMsgEvent_t *pMsg )
  1695. {
  1696. // Process the GATT message
  1697. switch ( pMsg->method )
  1698. {
  1699. case ATT_HANDLE_VALUE_CFM:
  1700. // Clear Service Changed flag for this client
  1701. VOID GAPBondMgr_ServiceChangeInd( pMsg->connHandle, 0x00 );
  1702. break;
  1703. default:
  1704. // Unknown message
  1705. break;
  1706. }
  1707. }
  1708. /*********************************************************************
  1709. * @fn gapBondMgr_ProcessGATTServMsg
  1710. *
  1711. * @brief Process an incoming GATT Server App message.
  1712. *
  1713. * @param pMsg - pointer to received message
  1714. *
  1715. * @return none
  1716. */
  1717. static void gapBondMgr_ProcessGATTServMsg( gattEventHdr_t *pMsg )
  1718. {
  1719. // Process the GATT Server App message
  1720. switch ( pMsg->method )
  1721. {
  1722. case GATT_CLIENT_CHAR_CFG_UPDATED_EVENT:
  1723. {
  1724. gattClientCharCfgUpdatedEvent_t *pEvent = (gattClientCharCfgUpdatedEvent_t *)pMsg;
  1725. VOID GAPBondMgr_UpdateCharCfg( pEvent->connHandle, pEvent->attrHandle, pEvent->value );
  1726. }
  1727. break;
  1728. default:
  1729. // Unknown message
  1730. break;
  1731. }
  1732. }
  1733. /*********************************************************************
  1734. * @fn gapBondMgrSendServiceChange
  1735. *
  1736. * @brief Tell the GATT that a service change is needed.
  1737. *
  1738. * @param pLinkItem - pointer to connection information
  1739. *
  1740. * @return none
  1741. */
  1742. static void gapBondMgrSendServiceChange( linkDBItem_t *pLinkItem )
  1743. {
  1744. VOID GATTServApp_SendServiceChangedInd( pLinkItem->connectionHandle,
  1745. gapBondMgr_TaskID );
  1746. }
  1747. /*********************************************************************
  1748. * @fn gapBondSetupPrivFlag
  1749. *
  1750. * @brief Setup the GAP Privacy Flag properties.
  1751. *
  1752. * @param none
  1753. *
  1754. * @return none
  1755. */
  1756. static void gapBondSetupPrivFlag( void )
  1757. {
  1758. uint8 privFlagProp;
  1759. if ( gapBondMgrBondTotal() > 1 )
  1760. {
  1761. privFlagProp = GATT_PROP_READ;
  1762. }
  1763. else
  1764. {
  1765. privFlagProp = GATT_PROP_READ | GATT_PROP_WRITE;
  1766. }
  1767. // Setup the
  1768. VOID GGS_SetParameter( GGS_PERI_PRIVACY_FLAG_PROPS, sizeof ( uint8 ), &privFlagProp );
  1769. }
  1770. /*********************************************************************
  1771. * @fn gapBondMgrAuthenticate
  1772. *
  1773. * @brief Initiate authentication
  1774. *
  1775. * @param connHandle - connection handle
  1776. * @param addrType - peer address type
  1777. * @param pPairReq - Enter these parameters if the Pairing Request was already received.
  1778. * NULL, if waiting for Pairing Request or if initiating.
  1779. *
  1780. * @return none
  1781. */
  1782. static void gapBondMgrAuthenticate( uint16 connHandle, uint8 addrType,
  1783. gapPairingReq_t *pPairReq )
  1784. {
  1785. gapAuthParams_t params;
  1786. VOID osal_memset( &params, 0, sizeof ( gapAuthParams_t ) );
  1787. // Setup the pairing parameters
  1788. params.connectionHandle = connHandle;
  1789. params.secReqs.ioCaps = gapBond_IOCap;
  1790. params.secReqs.oobAvailable = gapBond_OOBDataFlag;
  1791. params.secReqs.maxEncKeySize = gapBond_KeySize;
  1792. params.secReqs.keyDist.sEncKey = (gapBond_KeyDistList & GAPBOND_KEYDIST_SENCKEY) ? TRUE : FALSE;
  1793. params.secReqs.keyDist.sIdKey = (gapBond_KeyDistList & GAPBOND_KEYDIST_SIDKEY) ? TRUE : FALSE;
  1794. params.secReqs.keyDist.mEncKey = (gapBond_KeyDistList & GAPBOND_KEYDIST_MENCKEY) ? TRUE : FALSE;
  1795. params.secReqs.keyDist.mIdKey = (gapBond_KeyDistList & GAPBOND_KEYDIST_MIDKEY) ? TRUE : FALSE;
  1796. params.secReqs.keyDist.mSign = (gapBond_KeyDistList & GAPBOND_KEYDIST_MSIGN) ? TRUE : FALSE;
  1797. params.secReqs.keyDist.sSign = (gapBond_KeyDistList & GAPBOND_KEYDIST_SSIGN) ? TRUE : FALSE;
  1798. // Is bond manager setup for OOB data?
  1799. if ( gapBond_OOBDataFlag )
  1800. {
  1801. VOID osal_memcpy( params.secReqs.oob, gapBond_OOBData, KEYLEN );
  1802. }
  1803. if ( gapBond_Bonding && addrType != ADDRTYPE_PUBLIC )
  1804. {
  1805. // Force a slave ID key
  1806. params.secReqs.keyDist.sIdKey = TRUE;
  1807. }
  1808. params.secReqs.authReq |= (gapBond_Bonding) ? SM_AUTH_STATE_BONDING : 0;
  1809. params.secReqs.authReq |= (gapBond_MITM) ? SM_AUTH_STATE_AUTHENTICATED : 0;
  1810. params.secReqs.authReq |= (gapBond_SC) ? SM_AUTH_STATE_SC : 0;
  1811. params.secReqs.authReq |= (gapBond_keyPress) ? SM_AUTH_STATE_KEYPRESS : 0;
  1812. params.secReqs.authReq |= (gapBond_CT2) ? SM_AUTH_STATE_CT2 : 0;
  1813. GAP_Authenticate( &params, pPairReq );
  1814. }
  1815. #if ( HOST_CONFIG & PERIPHERAL_CFG )
  1816. /*********************************************************************
  1817. * @fn gapBondMgrSlaveSecurityReq
  1818. *
  1819. * @brief Send a slave security request
  1820. *
  1821. * @param connHandle - connection handle
  1822. *
  1823. * @return none
  1824. */
  1825. static void gapBondMgrSlaveSecurityReq( uint16 connHandle )
  1826. {
  1827. uint8 authReq = 0;
  1828. authReq |= (gapBond_Bonding) ? SM_AUTH_STATE_BONDING : 0;
  1829. authReq |= (gapBond_MITM) ? SM_AUTH_STATE_AUTHENTICATED : 0;
  1830. authReq |= (gapBond_SC) ? SM_AUTH_STATE_SC : 0;
  1831. authReq |= (gapBond_keyPress) ? SM_AUTH_STATE_KEYPRESS : 0;
  1832. authReq |= (gapBond_CT2) ? SM_AUTH_STATE_CT2 : 0;
  1833. GAP_SendSlaveSecurityRequest( connHandle, authReq );
  1834. }
  1835. #endif
  1836. /*********************************************************************
  1837. * @fn gapBondMgrBondReq
  1838. *
  1839. * @brief Initiate a GAP bond request
  1840. *
  1841. * @param connHandle - connection handle
  1842. * @param idx - NV index of bond entry
  1843. * @param stateFlags - bond state flags
  1844. * @param role - master or slave role
  1845. * @param startEncryption - whether or not to start encryption
  1846. *
  1847. * @return none
  1848. */
  1849. static void gapBondMgrBondReq( uint16 connHandle, uint8 idx, uint8 stateFlags,
  1850. uint8 role, uint8 startEncryption )
  1851. {
  1852. smSecurityInfo_t ltk;
  1853. osalSnvId_t nvId;
  1854. if ( role == GAP_PROFILE_CENTRAL )
  1855. {
  1856. nvId = devLTKNvID( idx );
  1857. }
  1858. else
  1859. {
  1860. nvId = localLTKNvID( idx );
  1861. }
  1862. // Initialize the NV structures
  1863. VOID osal_memset( &ltk, 0, sizeof ( smSecurityInfo_t ) );
  1864. if ( osal_snv_read( nvId, sizeof ( smSecurityInfo_t ), &ltk ) == SUCCESS )
  1865. {
  1866. if ( (ltk.keySize >= MIN_ENC_KEYSIZE) && (ltk.keySize <= MAX_ENC_KEYSIZE) )
  1867. {
  1868. bStatus_t ret = GAP_Bond( connHandle,
  1869. ((stateFlags & GAP_BONDED_STATE_AUTHENTICATED) ? TRUE : FALSE),
  1870. &ltk, startEncryption );
  1871. }
  1872. }
  1873. }
  1874. /*********************************************************************
  1875. * @fn gapBondMgr_SyncWhiteList
  1876. *
  1877. * @brief syncronize the White List with the bonds
  1878. *
  1879. * @param none
  1880. *
  1881. * @return none
  1882. */
  1883. static void gapBondMgr_SyncWhiteList( void )
  1884. {
  1885. //erase the White List
  1886. VOID HCI_LE_ClearWhiteListCmd();
  1887. // Write bond addresses into the White List
  1888. for( uint8 i = 0; i < GAP_BONDINGS_MAX; i++)
  1889. {
  1890. // Make sure empty addresses are not added to the White List
  1891. if ( osal_isbufset( bonds[i].publicAddr, 0xFF, B_ADDR_LEN ) == FALSE )
  1892. {
  1893. VOID HCI_LE_AddWhiteListCmd( HCI_PUBLIC_DEVICE_ADDRESS, bonds[i].publicAddr );
  1894. }
  1895. }
  1896. }
  1897. /*********************************************************************
  1898. * @fn gapBondMgr_SyncCharCfg
  1899. *
  1900. * @brief Update the Bond Manager to have the same configurations as
  1901. * the GATT database.
  1902. *
  1903. * @param connHandle - the current connection handle to find client configurations for
  1904. *
  1905. * @return TRUE if sync done. FALSE, otherwise.
  1906. */
  1907. static uint8 gapBondMgr_SyncCharCfg( uint16 connHandle )
  1908. {
  1909. static gattAttribute_t *pAttr = NULL;
  1910. static uint16 service;
  1911. // Only attributes with attribute handles between and including the Starting
  1912. // Handle parameter and the Ending Handle parameter that match the requested
  1913. // attribute type and the attribute value will be returned.
  1914. // All attribute types are effectively compared as 128-bit UUIDs,
  1915. // even if a 16-bit UUID is provided in this request or defined
  1916. // for an attribute.
  1917. if ( pAttr == NULL )
  1918. {
  1919. pAttr = GATT_FindHandleUUID( GATT_MIN_HANDLE, GATT_MAX_HANDLE,
  1920. clientCharCfgUUID, ATT_BT_UUID_SIZE, &service );
  1921. }
  1922. while ( pAttr != NULL )
  1923. {
  1924. uint16 len;
  1925. uint8 attrVal[ATT_BT_UUID_SIZE];
  1926. // It is not possible to use this request on an attribute that has a value
  1927. // that is longer than 2.
  1928. if ( GATTServApp_ReadAttr( connHandle, pAttr, service, attrVal,
  1929. &len, 0, ATT_BT_UUID_SIZE ) == SUCCESS )
  1930. {
  1931. uint16 value = BUILD_UINT16(attrVal[0], attrVal[1]);
  1932. if ( value != GATT_CFG_NO_OPERATION )
  1933. {
  1934. // NV must be updated to meet configuration of the database
  1935. VOID GAPBondMgr_UpdateCharCfg( connHandle, pAttr->handle, value );
  1936. }
  1937. }
  1938. // Try to find the next attribute
  1939. pAttr = GATT_FindNextAttr( pAttr, GATT_MAX_HANDLE, service, NULL );
  1940. }
  1941. return ( pAttr == NULL );
  1942. }
  1943. /*********************************************************************
  1944. * @fn gapBondFreeAuthEvt
  1945. *
  1946. * @brief Free GAP Authentication Complete event.
  1947. *
  1948. * @param none
  1949. *
  1950. * @return none
  1951. */
  1952. static void gapBondFreeAuthEvt( uint16 connHandle )
  1953. {
  1954. if ( pAuthEvt[connHandle] != NULL )
  1955. {
  1956. // Release the OSAL message
  1957. VOID osal_msg_deallocate( (uint8 *)pAuthEvt[connHandle] );
  1958. pAuthEvt[connHandle] = NULL;
  1959. }
  1960. bondIdx = GAP_BONDINGS_MAX;
  1961. }
  1962. #endif // ( CENTRAL_CFG | PERIPHERAL_CFG )
  1963. /*********************************************************************
  1964. *********************************************************************/