Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.41 KB | None | 0 0
  1. /*! *********************************************************************************
  2. * \addtogroup Wireless UART Application
  3. * @{
  4. ********************************************************************************** */
  5. /*! *********************************************************************************
  6. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  7. * Copyright 2016-2018 NXP
  8. * All rights reserved.
  9. *
  10. * \file
  11. *
  12. * This file is the source file for the Wireless UART Application
  13. *
  14. * SPDX-License-Identifier: BSD-3-Clause
  15. ********************************************************************************** */
  16.  
  17. /************************************************************************************
  18. *************************************************************************************
  19. * Include
  20. *************************************************************************************
  21. ************************************************************************************/
  22. /* Framework / Drivers */
  23. #include "EmbeddedTypes.h"
  24. #include "RNG_Interface.h"
  25. #include "Keyboard.h"
  26. #include "LED.h"
  27. #include "TimersManager.h"
  28. #include "FunctionLib.h"
  29. #include "Panic.h"
  30. #include "SerialManager.h"
  31. #include "MemManager.h"
  32. #include "board.h"
  33.  
  34. /* BLE Host Stack */
  35. #include "gatt_interface.h"
  36. #include "gatt_server_interface.h"
  37. #include "gatt_client_interface.h"
  38. #include "gatt_database.h"
  39. #include "gap_interface.h"
  40. #include "gatt_db_app_interface.h"
  41.  
  42. #if !defined(MULTICORE_APPLICATION_CORE) || (!MULTICORE_APPLICATION_CORE)
  43. #include "gatt_db_handles.h"
  44. #else
  45. #define UUID128(name, ...)\
  46. extern uint8_t name[16];
  47. #include "gatt_uuid128.h"
  48. #undef UUID128
  49. #endif
  50.  
  51. /* Profile / Services */
  52. #include "wireless_uart_interface.h"
  53. #include "battery_interface.h"
  54. /* Wrappers */
  55. #include "ble_conn_manager.h"
  56. #include "ble_service_discovery.h"
  57.  
  58. #include "board.h"
  59. #include "ApplMain.h"
  60. #include "wireless_uart.h"
  61.  
  62. #if defined(MULTICORE_APPLICATION_CORE) && (MULTICORE_APPLICATION_CORE)
  63. #include "erpc_host.h"
  64. #include "dynamic_gatt_database.h"
  65. #endif
  66.  
  67.  
  68. /************************************************************************************
  69. *************************************************************************************
  70. * Private macros
  71. *************************************************************************************
  72. ************************************************************************************/
  73.  
  74. #define mAppUartBufferSize_c gAttMaxWriteDataSize_d(gAttMaxMtu_c) /* Local Buffer Size */
  75.  
  76. #define mAppUartFlushIntervalInMs_c (7) /* Flush Timeout in Ms */
  77.  
  78. #define mBatteryLevelReportInterval_c (10) /* battery level report interval in seconds */
  79. /************************************************************************************
  80. *************************************************************************************
  81. * Private type definitions
  82. *************************************************************************************
  83. ************************************************************************************/
  84. typedef enum appEvent_tag
  85. {
  86. mAppEvt_PeerConnected_c,
  87. mAppEvt_PairingComplete_c,
  88. mAppEvt_ServiceDiscoveryComplete_c,
  89. mAppEvt_ServiceDiscoveryNotFound_c,
  90. mAppEvt_ServiceDiscoveryFailed_c,
  91. mAppEvt_GattProcComplete_c,
  92. mAppEvt_GattProcError_c
  93. } appEvent_t;
  94.  
  95. typedef enum appState_tag
  96. {
  97. mAppIdle_c,
  98. mAppExchangeMtu_c,
  99. mAppServiceDisc_c,
  100. mAppServiceDiscRetry_c,
  101. mAppRunning_c
  102. } appState_t;
  103.  
  104. typedef struct appPeerInfo_tag
  105. {
  106. deviceId_t deviceId;
  107. bool_t isBonded;
  108. wucConfig_t clientInfo;
  109. appState_t appState;
  110. gapRole_t gapRole;
  111. } appPeerInfo_t;
  112.  
  113. typedef struct advState_tag
  114. {
  115. bool_t advOn;
  116. } advState_t;
  117. /************************************************************************************
  118. *************************************************************************************
  119. * Private memory declarations
  120. *************************************************************************************
  121. ************************************************************************************/
  122.  
  123. static appPeerInfo_t maPeerInformation[gAppMaxConnections_c];
  124. static gapRole_t mGapRole;
  125.  
  126. /* Adv Parameters */
  127. static advState_t mAdvState;
  128. static bool_t mScanningOn = FALSE;
  129.  
  130. static uint16_t mCharMonitoredHandles[1] = { (uint16_t)value_uart_stream };
  131.  
  132. /* Service Data*/
  133. static wusConfig_t mWuServiceConfig;
  134. static bool_t mBasValidClientList[gAppMaxConnections_c] = {FALSE};
  135. static basConfig_t mBasServiceConfig = {(uint16_t)service_battery, 0, mBasValidClientList, gAppMaxConnections_c};
  136.  
  137. static tmrTimerID_t mAppTimerId = gTmrInvalidTimerID_c;
  138. static tmrTimerID_t mUartStreamFlushTimerId = gTmrInvalidTimerID_c;
  139. static tmrTimerID_t mBatteryMeasurementTimerId = gTmrInvalidTimerID_c;
  140.  
  141. static uint8_t gAppSerMgrIf;
  142. static uint16_t mAppUartBufferSize = mAppUartBufferSize_c;
  143. static volatile bool_t mAppUartNewLine = FALSE;
  144. static volatile bool_t mAppDapaPending = FALSE;
  145.  
  146. /************************************************************************************
  147. *************************************************************************************
  148. * Private functions prototypes
  149. *************************************************************************************
  150. ************************************************************************************/
  151.  
  152. /* Gatt and Att callbacks */
  153. static void BleApp_AdvertisingCallback(gapAdvertisingEvent_t *pAdvertisingEvent);
  154. static void BleApp_ScanningCallback(gapScanningEvent_t *pScanningEvent);
  155. static void BleApp_ConnectionCallback
  156. (
  157. deviceId_t peerDeviceId,
  158. gapConnectionEvent_t *pConnectionEvent
  159. );
  160. static void BleApp_GattServerCallback
  161. (
  162. deviceId_t deviceId,
  163. gattServerEvent_t *pServerEvent
  164. );
  165.  
  166. static void BleApp_GattClientCallback
  167. (
  168. deviceId_t serverDeviceId,
  169. gattProcedureType_t procedureType,
  170. gattProcedureResult_t procedureResult,
  171. bleResult_t error
  172. );
  173.  
  174. static void BleApp_ServiceDiscoveryCallback
  175. (
  176. deviceId_t peerDeviceId,
  177. servDiscEvent_t *pEvent
  178. );
  179.  
  180. static void BleApp_Config(void);
  181. static void BleApp_Advertise(void);
  182.  
  183. void BleApp_StateMachineHandler
  184. (
  185. deviceId_t peerDeviceId,
  186. appEvent_t event
  187. );
  188.  
  189. static void BleApp_StoreServiceHandles
  190. (
  191. deviceId_t peerDeviceId,
  192. gattService_t *pService
  193. );
  194. static bool_t BleApp_CheckScanEvent(gapScannedDevice_t *pData);
  195.  
  196. /* Timer Callbacks */
  197. static void ScanningTimerCallback(void *pParam);
  198. static void UartStreamFlushTimerCallback(void *pData);
  199. static void BatteryMeasurementTimerCallback(void *pParam);
  200.  
  201. /* Uart Tx/Rx Callbacks*/
  202. static void Uart_RxCallBack(void *pData);
  203. static void Uart_TxCallBack(void *pBuffer);
  204.  
  205. static void BleApp_FlushUartStream(void *pParam);
  206. static void BleApp_ReceivedUartStream(deviceId_t peerDeviceId, uint8_t *pStream, uint16_t streamLength);
  207. /************************************************************************************
  208. *************************************************************************************
  209. * Public functions
  210. *************************************************************************************
  211. ************************************************************************************/
  212.  
  213. /*! *********************************************************************************
  214. * \brief Initializes application specific functionality before the BLE stack init.
  215. *
  216. ********************************************************************************** */
  217. void BleApp_Init(void)
  218. {
  219. /* Initialize application support for drivers */
  220. BOARD_InitAdc();
  221.  
  222. /* UI */
  223. #if (defined(KW37A4_SERIES) || defined(KW37Z4_SERIES) || defined(KW38A4_SERIES) || defined(KW38Z4_SERIES) || defined(KW39A4_SERIES))
  224. Serial_InitManager();
  225. #else
  226. SerialManager_Init();
  227. #endif
  228.  
  229. /* Register Serial Manager interface */
  230. (void)Serial_InitInterface(&gAppSerMgrIf, APP_SERIAL_INTERFACE_TYPE, APP_SERIAL_INTERFACE_INSTANCE);
  231.  
  232. (void)Serial_SetBaudRate(gAppSerMgrIf, (uint32_t)gUARTBaudRate115200_c);
  233.  
  234. /* Install Controller Events Callback handler */
  235. (void)Serial_SetRxCallBack(gAppSerMgrIf, Uart_RxCallBack, NULL);
  236.  
  237. #if defined(MULTICORE_APPLICATION_CORE) && (MULTICORE_APPLICATION_CORE)
  238. /* Init eRPC host */
  239. init_erpc_host();
  240. #endif
  241. }
  242.  
  243. /*! *********************************************************************************
  244. * \brief Starts the BLE application.
  245. *
  246. * \param[in] gapRole GAP Start Role (Central or Peripheral).
  247. ********************************************************************************** */
  248. void BleApp_Start(gapRole_t gapRole)
  249. {
  250. switch (gapRole)
  251. {
  252. case gGapCentral_c:
  253. {
  254. (void)Serial_Print(gAppSerMgrIf, "\n\rScanning...\n\r", gAllowToBlock_d);
  255. mAppUartNewLine = TRUE;
  256. #if gUseControllerNotifications_c
  257. Gap_ControllerEnhancedNotification(gNotifScanEventOver_c | gNotifScanAdvPktRx_c |
  258. gNotifScanRspRx_c | gNotifScanReqTx_c, 0);
  259. #endif
  260. gPairingParameters.localIoCapabilities = gIoKeyboardDisplay_c;
  261. (void)App_StartScanning(&gScanParams, BleApp_ScanningCallback, gGapDuplicateFilteringEnable_c, gGapScanContinuously_d, gGapScanPeriodicDisabled_d);
  262. break;
  263. }
  264.  
  265. case gGapPeripheral_c:
  266. {
  267. (void)Serial_Print(gAppSerMgrIf, "\n\rAdvertising...\n\r", gAllowToBlock_d);
  268. mAppUartNewLine = TRUE;
  269. #if gUseControllerNotifications_c
  270. Gap_ControllerEnhancedNotification(gNotifAdvEventOver_c | gNotifAdvTx_c |
  271. gNotifAdvScanReqRx_c | gNotifAdvConnReqRx_c, 0);
  272. #endif
  273. gPairingParameters.localIoCapabilities = gIoDisplayOnly_c;
  274. BleApp_Advertise();
  275. break;
  276. }
  277.  
  278. default:
  279. {
  280. ; /* No action required */
  281. break;
  282. }
  283. }
  284. }
  285.  
  286. /*! *********************************************************************************
  287. * \brief Handles keyboard events.
  288. *
  289. * \param[in] events Key event structure.
  290. ********************************************************************************** */
  291. void BleApp_HandleKeys(key_event_t events)
  292. {
  293. uint8_t mPeerId = 0;
  294.  
  295. switch (events)
  296. {
  297. case gKBD_EventPressPB1_c:
  298. {
  299. LED_StopFlashingAllLeds();
  300. Led1Flashing();
  301.  
  302. BleApp_Start(mGapRole);
  303. break;
  304. }
  305.  
  306. case gKBD_EventLongPB1_c:
  307. {
  308. for (mPeerId = 0; mPeerId < (uint8_t)gAppMaxConnections_c; mPeerId++)
  309. {
  310. if (maPeerInformation[mPeerId].deviceId != gInvalidDeviceId_c)
  311. {
  312. (void)Gap_Disconnect(maPeerInformation[mPeerId].deviceId);
  313. }
  314. }
  315.  
  316. break;
  317. }
  318.  
  319. case gKBD_EventPressPB2_c:
  320. {
  321. /* Switch current role */
  322. if (mGapRole == gGapCentral_c)
  323. {
  324. (void)Serial_Print(gAppSerMgrIf, "\n\rSwitched role to GAP Peripheral.\n\r", gAllowToBlock_d);
  325. mAppUartNewLine = TRUE;
  326. mGapRole = gGapPeripheral_c;
  327. }
  328. else
  329. {
  330. (void)Serial_Print(gAppSerMgrIf, "\n\rSwitched role to GAP Central.\n\r", gAllowToBlock_d);
  331. mAppUartNewLine = TRUE;
  332. mGapRole = gGapCentral_c;
  333. }
  334.  
  335. break;
  336. }
  337.  
  338. case gKBD_EventLongPB2_c:
  339. break;
  340.  
  341. default:
  342. {
  343. ; /* No action required */
  344. break;
  345. }
  346. }
  347. }
  348.  
  349. /*! *********************************************************************************
  350. * \brief Handles BLE generic callback.
  351. *
  352. * \param[in] pGenericEvent Pointer to gapGenericEvent_t.
  353. ********************************************************************************** */
  354. void BleApp_GenericCallback(gapGenericEvent_t *pGenericEvent)
  355. {
  356. /* Call BLE Conn Manager */
  357. BleConnManager_GenericEvent(pGenericEvent);
  358.  
  359. switch (pGenericEvent->eventType)
  360. {
  361. case gInitializationComplete_c:
  362. {
  363. BleApp_Config();
  364. }
  365. break;
  366.  
  367. case gAdvertisingParametersSetupComplete_c:
  368. {
  369. (void)Gap_SetAdvertisingData(&gAppAdvertisingData, &gAppScanRspData);
  370. }
  371. break;
  372.  
  373. case gAdvertisingDataSetupComplete_c:
  374. {
  375. (void)App_StartAdvertising(BleApp_AdvertisingCallback, BleApp_ConnectionCallback);
  376. }
  377. break;
  378.  
  379. case gAdvertisingSetupFailed_c:
  380. {
  381. panic(0,0,0,0);
  382. }
  383. break;
  384.  
  385. #if gUseControllerNotifications_c
  386. case gControllerNotificationEvent_c:
  387. {
  388. switch(pGenericEvent->eventData.notifEvent.eventType)
  389. {
  390. case gNotifEventNone_c:
  391. Serial_Print(gAppSerMgrIf, "\n\rConfigured notification status ", gAllowToBlock_d);
  392. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.status);
  393. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  394. break;
  395.  
  396. case gNotifConnEventOver_c:
  397. Serial_Print(gAppSerMgrIf, "\n\r[EN] CONN Event Over device ", gAllowToBlock_d);
  398. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.deviceId);
  399. Serial_Print(gAppSerMgrIf, " on channel ", gAllowToBlock_d);
  400. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  401. Serial_Print(gAppSerMgrIf, " with RSSI ", gAllowToBlock_d);
  402. Serial_PrintDec(gAppSerMgrIf, (uint8_t)pGenericEvent->eventData.notifEvent.rssi);
  403. Serial_Print(gAppSerMgrIf, " and event counter ", gAllowToBlock_d);
  404. Serial_PrintDec(gAppSerMgrIf, (uint16_t)pGenericEvent->eventData.notifEvent.ce_counter);
  405. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  406. break;
  407.  
  408. case gNotifConnRxPdu_c:
  409. Serial_Print(gAppSerMgrIf, "\n\r[EN] CONN Rx PDU from device ", gAllowToBlock_d);
  410. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.deviceId);
  411. Serial_Print(gAppSerMgrIf, " on channel ", gAllowToBlock_d);
  412. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  413. Serial_Print(gAppSerMgrIf, " with RSSI ", gAllowToBlock_d);
  414. Serial_PrintDec(gAppSerMgrIf, (uint8_t)pGenericEvent->eventData.notifEvent.rssi);
  415. Serial_Print(gAppSerMgrIf, " and event counter ", gAllowToBlock_d);
  416. Serial_PrintDec(gAppSerMgrIf, (uint16_t)pGenericEvent->eventData.notifEvent.ce_counter);
  417. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  418. break;
  419.  
  420. case gNotifAdvEventOver_c:
  421. Serial_Print(gAppSerMgrIf, "\n\r[EN] ADV Event Over.\n\r", gAllowToBlock_d);
  422. break;
  423.  
  424. case gNotifAdvTx_c:
  425. Serial_Print(gAppSerMgrIf, "\n\r[EN] ADV Tx on channel ", gAllowToBlock_d);
  426. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  427. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  428. break;
  429.  
  430. case gNotifAdvScanReqRx_c:
  431. Serial_Print(gAppSerMgrIf, "\n\r[EN] ADV Rx Scan Req on channel ", gAllowToBlock_d);
  432. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  433. Serial_Print(gAppSerMgrIf, " with RSSI ", gAllowToBlock_d);
  434. Serial_PrintDec(gAppSerMgrIf, (uint8_t)pGenericEvent->eventData.notifEvent.rssi);
  435. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  436. break;
  437.  
  438. case gNotifAdvConnReqRx_c:
  439. Serial_Print(gAppSerMgrIf, "\n\r[EN] ADV Rx Conn Req on channel ", gAllowToBlock_d);
  440. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  441. Serial_Print(gAppSerMgrIf, " with RSSI ", gAllowToBlock_d);
  442. Serial_PrintDec(gAppSerMgrIf, (uint8_t)pGenericEvent->eventData.notifEvent.rssi);
  443. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  444. break;
  445.  
  446. case gNotifScanEventOver_c:
  447. Serial_Print(gAppSerMgrIf, "\n\r[EN] SCAN Event Over on channel ", gAllowToBlock_d);
  448. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  449. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  450. break;
  451.  
  452. case gNotifScanAdvPktRx_c:
  453. Serial_Print(gAppSerMgrIf, "\n\r[EN] SCAN Rx Adv Pkt on channel ", gAllowToBlock_d);
  454. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  455. Serial_Print(gAppSerMgrIf, " with RSSI ", gAllowToBlock_d);
  456. Serial_PrintDec(gAppSerMgrIf, (uint8_t)pGenericEvent->eventData.notifEvent.rssi);
  457. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  458. break;
  459.  
  460. case gNotifScanRspRx_c:
  461. Serial_Print(gAppSerMgrIf, "\n\r[EN] SCAN Rx Scan Rsp on channel ", gAllowToBlock_d);
  462. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  463. Serial_Print(gAppSerMgrIf, " with RSSI ", gAllowToBlock_d);
  464. Serial_PrintDec(gAppSerMgrIf, (uint8_t)pGenericEvent->eventData.notifEvent.rssi);
  465. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  466. break;
  467.  
  468. case gNotifScanReqTx_c:
  469. Serial_Print(gAppSerMgrIf, "\n\r[EN] SCAN Tx Scan Req on channel ", gAllowToBlock_d);
  470. Serial_PrintDec(gAppSerMgrIf, pGenericEvent->eventData.notifEvent.channel);
  471. Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  472. break;
  473. }
  474. }
  475. break;
  476. #endif
  477.  
  478. default:
  479. {
  480. ; /* No action required */
  481. }
  482. break;
  483. }
  484. }
  485.  
  486. /************************************************************************************
  487. *************************************************************************************
  488. * Private functions
  489. *************************************************************************************
  490. ************************************************************************************/
  491.  
  492. /*! *********************************************************************************
  493. * \brief Configures BLE Stack after initialization. Usually used for
  494. * configuring advertising, scanning, white list, services, et al.
  495. *
  496. ********************************************************************************** */
  497. static void BleApp_Config(void)
  498. {
  499. uint8_t mPeerId = 0;
  500.  
  501. #if defined(MULTICORE_APPLICATION_CORE) && (MULTICORE_APPLICATION_CORE)
  502.  
  503. if (GattDbDynamic_CreateDatabase() != gBleSuccess_c)
  504. {
  505. panic(0, 0, 0, 0);
  506. return;
  507. }
  508.  
  509. #endif /* MULTICORE_APPLICATION_CORE */
  510.  
  511. /* Common GAP configuration */
  512. BleConnManager_GapCommonConfig();
  513.  
  514. /* Register for callbacks */
  515. (void)App_RegisterGattServerCallback(BleApp_GattServerCallback);
  516. (void)App_RegisterGattClientProcedureCallback(BleApp_GattClientCallback);
  517. (void)GattServer_RegisterHandlesForWriteNotifications(NumberOfElements(mCharMonitoredHandles), mCharMonitoredHandles);
  518. BleServDisc_RegisterCallback(BleApp_ServiceDiscoveryCallback);
  519.  
  520. for (mPeerId = 0; mPeerId < (uint8_t)gAppMaxConnections_c; mPeerId++)
  521. {
  522. maPeerInformation[mPeerId].appState = mAppIdle_c;
  523. maPeerInformation[mPeerId].deviceId = gInvalidDeviceId_c;
  524. maPeerInformation[mPeerId].clientInfo.hService = gGattDbInvalidHandleIndex_d;
  525. maPeerInformation[mPeerId].clientInfo.hUartStream = gGattDbInvalidHandleIndex_d;
  526. }
  527.  
  528. /* By default, always start node as GAP central */
  529. mGapRole = gGapCentral_c;
  530.  
  531. (void)Serial_Print(gAppSerMgrIf, "\n\rWireless UART starting as GAP Central, press the role switch to change it.\n\r", gAllowToBlock_d);
  532.  
  533. mAdvState.advOn = FALSE;
  534. mScanningOn = FALSE;
  535.  
  536. /* Start services */
  537. (void)Wus_Start(&mWuServiceConfig);
  538.  
  539. mBasServiceConfig.batteryLevel = BOARD_GetBatteryLevel();
  540. (void)Bas_Start(&mBasServiceConfig);
  541.  
  542. /* Allocate application timer */
  543. mAppTimerId = TMR_AllocateTimer();
  544. mUartStreamFlushTimerId = TMR_AllocateTimer();
  545. mBatteryMeasurementTimerId = TMR_AllocateTimer();
  546. }
  547.  
  548. /*! *********************************************************************************
  549. * \brief Configures GAP Advertise parameters. Advertise will start after
  550. * the parameters are set.
  551. *
  552. ********************************************************************************** */
  553. static void BleApp_Advertise(void)
  554. {
  555. /* Set advertising parameters*/
  556. (void)Gap_SetAdvertisingParameters(&gAdvParams);
  557. }
  558.  
  559. /*! *********************************************************************************
  560. * \brief Handles BLE Scanning callback from host stack.
  561. *
  562. * \param[in] pScanningEvent Pointer to gapScanningEvent_t.
  563. ********************************************************************************** */
  564. static void BleApp_ScanningCallback(gapScanningEvent_t *pScanningEvent)
  565. {
  566. switch (pScanningEvent->eventType)
  567. {
  568. case gDeviceScanned_c:
  569. {
  570. if (BleApp_CheckScanEvent(&pScanningEvent->eventData.scannedDevice))
  571. {
  572. gConnReqParams.peerAddressType = pScanningEvent->eventData.scannedDevice.addressType;
  573. FLib_MemCpy(gConnReqParams.peerAddress,
  574. pScanningEvent->eventData.scannedDevice.aAddress,
  575. sizeof(bleDeviceAddress_t));
  576.  
  577. (void)Gap_StopScanning();
  578. #if gAppUsePrivacy_d
  579. gConnReqParams.usePeerIdentityAddress = pScanningEvent->eventData.scannedDevice.advertisingAddressResolved;
  580. #endif
  581. (void)App_Connect(&gConnReqParams, BleApp_ConnectionCallback);
  582. }
  583. }
  584. break;
  585.  
  586. case gScanStateChanged_c:
  587. {
  588. mScanningOn = !mScanningOn;
  589.  
  590. /* Node starts scanning */
  591. if (mScanningOn)
  592. {
  593. /* Start advertising timer */
  594. (void)TMR_StartLowPowerTimer(mAppTimerId,
  595. gTmrLowPowerSecondTimer_c,
  596. TmrSeconds(gScanningTime_c),
  597. ScanningTimerCallback, NULL);
  598.  
  599. Led1Flashing();
  600. }
  601. /* Node is not scanning */
  602. else
  603. {
  604. (void)TMR_StopTimer(mAppTimerId);
  605.  
  606. Led1Flashing();
  607. Led2Flashing();
  608. Led3Flashing();
  609. Led4Flashing();
  610. }
  611. }
  612. break;
  613.  
  614. case gScanCommandFailed_c:
  615. {
  616. panic(0, 0, 0, 0);
  617. break;
  618. }
  619.  
  620. default:
  621. {
  622. ; /* No action required */
  623. break;
  624. }
  625. }
  626. }
  627.  
  628. /*! *********************************************************************************
  629. * \brief Handles BLE Advertising callback from host stack.
  630. *
  631. * \param[in] pAdvertisingEvent Pointer to gapAdvertisingEvent_t.
  632. ********************************************************************************** */
  633. static void BleApp_AdvertisingCallback(gapAdvertisingEvent_t *pAdvertisingEvent)
  634. {
  635. switch (pAdvertisingEvent->eventType)
  636. {
  637. case gAdvertisingStateChanged_c:
  638. {
  639. mAdvState.advOn = !mAdvState.advOn;
  640. LED_StopFlashingAllLeds();
  641. Led1Flashing();
  642.  
  643. if (!mAdvState.advOn)
  644. {
  645. Led2Flashing();
  646. Led3Flashing();
  647. Led4Flashing();
  648. }
  649. }
  650. break;
  651.  
  652. case gAdvertisingCommandFailed_c:
  653. {
  654. panic(0, 0, 0, 0);
  655. }
  656. break;
  657.  
  658. default:
  659. {
  660. ; /* No action required */
  661. }
  662. break;
  663. }
  664. }
  665.  
  666. /*! *********************************************************************************
  667. * \brief Handles BLE Connection callback from host stack.
  668. *
  669. * \param[in] peerDeviceId Peer device ID.
  670. * \param[in] pConnectionEvent Pointer to gapConnectionEvent_t.
  671. ********************************************************************************** */
  672. static void BleApp_ConnectionCallback(deviceId_t peerDeviceId, gapConnectionEvent_t *pConnectionEvent)
  673. {
  674. switch (pConnectionEvent->eventType)
  675. {
  676. case gConnEvtConnected_c:
  677. {
  678. /* Save peer device ID */
  679. maPeerInformation[peerDeviceId].deviceId = peerDeviceId;
  680.  
  681. /* Advertising stops when connected */
  682. mAdvState.advOn = FALSE;
  683.  
  684. /* Subscribe client*/
  685. (void)Wus_Subscribe(peerDeviceId);
  686. (void)Bas_Subscribe(&mBasServiceConfig, peerDeviceId);
  687.  
  688. /* UI */
  689. LED_StopFlashingAllLeds();
  690. Led1On();
  691.  
  692. /* Stop Advertising Timer*/
  693. mAdvState.advOn = FALSE;
  694. (void)TMR_StopTimer(mAppTimerId);
  695.  
  696. if (!TMR_IsTimerActive(mBatteryMeasurementTimerId))
  697. {
  698. /* Start battery measurements */
  699. (void)TMR_StartLowPowerTimer(mBatteryMeasurementTimerId, gTmrLowPowerIntervalMillisTimer_c,
  700. TmrSeconds(mBatteryLevelReportInterval_c), BatteryMeasurementTimerCallback, NULL);
  701. }
  702.  
  703. #if gAppUsePairing_d
  704. #if gAppUseBonding_d
  705.  
  706. if (mGapRole == gGapCentral_c)
  707. {
  708. (void)Gap_CheckIfBonded(peerDeviceId, &maPeerInformation[peerDeviceId].isBonded);
  709.  
  710. if ((maPeerInformation[peerDeviceId].isBonded) &&
  711. (gBleSuccess_c == Gap_LoadCustomPeerInformation(peerDeviceId,
  712. (void *) &maPeerInformation[peerDeviceId].clientInfo, 0, sizeof(wucConfig_t))))
  713. {
  714. /* Restored custom connection information. Encrypt link */
  715. (void)Gap_EncryptLink(peerDeviceId);
  716. }
  717. }
  718.  
  719. #endif /* gAppUseBonding_d*/
  720. #endif /* gAppUsePairing_d */
  721.  
  722. (void)Serial_Print(gAppSerMgrIf, "Connected to device ", gAllowToBlock_d);
  723. (void)Serial_PrintDec(gAppSerMgrIf, peerDeviceId);
  724.  
  725. if (mGapRole == gGapCentral_c)
  726. {
  727. (void)Serial_Print(gAppSerMgrIf, " as master.\n\r", gAllowToBlock_d);
  728. }
  729. else
  730. {
  731. (void)Serial_Print(gAppSerMgrIf, " as slave.\n\r", gAllowToBlock_d);
  732. }
  733.  
  734. mAppUartNewLine = TRUE;
  735.  
  736. #if gUseControllerNotifications_c
  737. Gap_ControllerEnhancedNotification(/*gNotifConnEventOver_c |*/ gNotifConnRxPdu_c, peerDeviceId);
  738. #endif
  739.  
  740. maPeerInformation[peerDeviceId].gapRole = mGapRole;
  741.  
  742. /* run the state machine */
  743. BleApp_StateMachineHandler(peerDeviceId, mAppEvt_PeerConnected_c);
  744. }
  745. break;
  746.  
  747. case gConnEvtDisconnected_c:
  748. {
  749. (void)Serial_Print(gAppSerMgrIf, "Disconnected from device ", gAllowToBlock_d);
  750. (void)Serial_PrintDec(gAppSerMgrIf, peerDeviceId);
  751. (void)Serial_Print(gAppSerMgrIf, ".\n\r", gAllowToBlock_d);
  752.  
  753. maPeerInformation[peerDeviceId].appState = mAppIdle_c;
  754. maPeerInformation[peerDeviceId].clientInfo.hService = gGattDbInvalidHandleIndex_d;
  755. maPeerInformation[peerDeviceId].clientInfo.hUartStream = gGattDbInvalidHandleIndex_d;
  756.  
  757. /* Unsubscribe client */
  758. (void)Wus_Unsubscribe();
  759. (void)Bas_Unsubscribe(&mBasServiceConfig, peerDeviceId);
  760. (void)TMR_StopTimer(mBatteryMeasurementTimerId);
  761.  
  762. /* Reset Service Discovery to be sure*/
  763. BleServDisc_Stop(peerDeviceId);
  764.  
  765. /* UI */
  766. LED_TurnOffAllLeds();
  767. LED_StartFlash(LED_ALL);
  768.  
  769. /* mark device id as invalid */
  770. maPeerInformation[peerDeviceId].deviceId = gInvalidDeviceId_c;
  771.  
  772. /* recalculate minimum of maximum MTU's of all connected devices */
  773. mAppUartBufferSize = mAppUartBufferSize_c;
  774.  
  775. for (uint8_t mPeerId = 0; mPeerId < (uint8_t)gAppMaxConnections_c; mPeerId++)
  776. {
  777. if (gInvalidDeviceId_c != maPeerInformation[mPeerId].deviceId)
  778. {
  779. uint16_t tempMtu;
  780.  
  781. (void)Gatt_GetMtu(mPeerId, &tempMtu);
  782. tempMtu = gAttMaxWriteDataSize_d(tempMtu);
  783.  
  784. if (tempMtu < mAppUartBufferSize)
  785. {
  786. mAppUartBufferSize = tempMtu;
  787. }
  788. }
  789. }
  790.  
  791. if (mGapRole == gGapPeripheral_c)
  792. {
  793. BleApp_Start(mGapRole);
  794. }
  795. }
  796. break;
  797.  
  798. #if gAppUsePairing_d
  799.  
  800. case gConnEvtPairingComplete_c:
  801. {
  802. if (pConnectionEvent->eventData.pairingCompleteEvent.pairingSuccessful)
  803. {
  804. BleApp_StateMachineHandler(peerDeviceId,
  805. mAppEvt_PairingComplete_c);
  806. }
  807. }
  808. break;
  809. #endif /* gAppUsePairing_d */
  810.  
  811. default:
  812. {
  813. ; /* No action required */
  814. }
  815. break;
  816. }
  817.  
  818. /* Connection Manager to handle Host Stack interactions */
  819. switch (maPeerInformation[peerDeviceId].gapRole)
  820. {
  821. case gGapCentral_c:
  822. BleConnManager_GapCentralEvent(peerDeviceId, pConnectionEvent);
  823. break;
  824.  
  825. case gGapPeripheral_c:
  826. BleConnManager_GapPeripheralEvent(peerDeviceId, pConnectionEvent);
  827. break;
  828.  
  829. default:
  830. ; /* No action required */
  831. break;
  832. }
  833. }
  834.  
  835. static void BleApp_ServiceDiscoveryCallback(deviceId_t peerDeviceId, servDiscEvent_t *pEvent)
  836. {
  837. switch (pEvent->eventType)
  838. {
  839. case gServiceDiscovered_c:
  840. {
  841. if (pEvent->eventData.pService->uuidType == gBleUuidType128_c)
  842. {
  843. if (FLib_MemCmp((void *)&uuid_service_wireless_uart, (void *)&pEvent->eventData.pService->uuid, sizeof(bleUuid_t)))
  844. {
  845. BleApp_StoreServiceHandles(peerDeviceId, pEvent->eventData.pService);
  846. }
  847. }
  848. }
  849. break;
  850.  
  851. case gDiscoveryFinished_c:
  852. {
  853. if (pEvent->eventData.success)
  854. {
  855. if (gGattDbInvalidHandleIndex_d != maPeerInformation[peerDeviceId].clientInfo.hService)
  856. {
  857. BleApp_StateMachineHandler(peerDeviceId,
  858. mAppEvt_ServiceDiscoveryComplete_c);
  859. }
  860. else
  861. {
  862. BleApp_StateMachineHandler(peerDeviceId,
  863. mAppEvt_ServiceDiscoveryNotFound_c);
  864. }
  865. }
  866. else
  867. {
  868. BleApp_StateMachineHandler(peerDeviceId,
  869. mAppEvt_ServiceDiscoveryFailed_c);
  870. }
  871. }
  872. break;
  873.  
  874. default:
  875. {
  876. ; /* No action required */
  877. }
  878. break;
  879. }
  880. }
  881.  
  882. /*! *********************************************************************************
  883. * \brief Handles GATT client callback from host stack.
  884. *
  885. * \param[in] serverDeviceId GATT Server device ID.
  886. * \param[in] procedureType Procedure type.
  887. * \param[in] procedureResult Procedure result.
  888. * \param[in] error Callback result.
  889. ********************************************************************************** */
  890. static void BleApp_GattClientCallback(
  891. deviceId_t serverDeviceId,
  892. gattProcedureType_t procedureType,
  893. gattProcedureResult_t procedureResult,
  894. bleResult_t error
  895. )
  896. {
  897. switch (procedureResult)
  898. {
  899. case gGattProcError_c:
  900. BleApp_StateMachineHandler(serverDeviceId, mAppEvt_GattProcError_c);
  901. break;
  902.  
  903. case gGattProcSuccess_c:
  904. BleApp_StateMachineHandler(serverDeviceId, mAppEvt_GattProcComplete_c);
  905. break;
  906.  
  907. default:
  908. ; /* No action required */
  909. break;
  910. }
  911.  
  912. /* Signal Service Discovery Module */
  913. BleServDisc_SignalGattClientEvent(serverDeviceId, procedureType, procedureResult, error);
  914. }
  915.  
  916. /*! *********************************************************************************
  917. * \brief Handles GATT server callback from host stack.
  918. *
  919. * \param[in] deviceId Client peer device ID.
  920. * \param[in] pServerEvent Pointer to gattServerEvent_t.
  921. ********************************************************************************** */
  922. static void BleApp_GattServerCallback(
  923. deviceId_t deviceId,
  924. gattServerEvent_t *pServerEvent)
  925. {
  926. uint16_t tempMtu = 0;
  927.  
  928. switch (pServerEvent->eventType)
  929. {
  930. case gEvtAttributeWrittenWithoutResponse_c:
  931. {
  932. if (pServerEvent->eventData.attributeWrittenEvent.handle == (uint16_t)value_uart_stream)
  933. {
  934. BleApp_ReceivedUartStream(deviceId, pServerEvent->eventData.attributeWrittenEvent.aValue,
  935. pServerEvent->eventData.attributeWrittenEvent.cValueLength);
  936. }
  937.  
  938. break;
  939. }
  940.  
  941. case gEvtMtuChanged_c:
  942. {
  943. /* update stream length with minimum of new MTU */
  944. (void)Gatt_GetMtu(deviceId, &tempMtu);
  945. tempMtu = gAttMaxWriteDataSize_d(tempMtu);
  946.  
  947. mAppUartBufferSize = mAppUartBufferSize <= tempMtu ? mAppUartBufferSize : tempMtu;
  948. }
  949. break;
  950.  
  951. default:
  952. {
  953. ; /* No action required */
  954. }
  955. break;
  956. }
  957. }
  958.  
  959.  
  960. static bool_t MatchDataInAdvElementList(gapAdStructure_t *pElement,
  961. void *pData,
  962. uint8_t iDataLen)
  963. {
  964. uint8_t i;
  965. bool_t status = FALSE;
  966.  
  967. for (i = 0; i < (pElement->length - 1U); i += iDataLen)
  968. {
  969. if (FLib_MemCmp(pData, &pElement->aData[i], iDataLen))
  970. {
  971. status = TRUE;
  972. break;
  973. }
  974. }
  975.  
  976. return status;
  977. }
  978.  
  979. /*! *********************************************************************************
  980. * \brief Checks Scan data for a device to connect.
  981. *
  982. * \param[in] pData Pointer to gapScannedDevice_t.
  983. ********************************************************************************** */
  984. static bool_t BleApp_CheckScanEvent(gapScannedDevice_t *pData)
  985. {
  986. uint8_t index = 0;
  987. bool_t foundMatch = FALSE;
  988.  
  989. while (index < pData->dataLength)
  990. {
  991. gapAdStructure_t adElement;
  992.  
  993. adElement.length = pData->data[index];
  994. adElement.adType = (gapAdType_t) pData->data[index + 1U];
  995. adElement.aData = &pData->data[index + 2U];
  996.  
  997. /* Search for Wireless UART Service */
  998. if ((adElement.adType == gAdIncomplete128bitServiceList_c)
  999. || (adElement.adType == gAdComplete128bitServiceList_c))
  1000. {
  1001. foundMatch = MatchDataInAdvElementList(&adElement,
  1002. &uuid_service_wireless_uart, 16);
  1003. }
  1004.  
  1005. /* Move on to the next AD element type */
  1006. index += adElement.length + sizeof(uint8_t);
  1007. }
  1008.  
  1009. return foundMatch;
  1010. }
  1011.  
  1012. /*! *********************************************************************************
  1013. * \brief Stores handles used by the application.
  1014. *
  1015. * \param[in] pService Pointer to gattService_t.
  1016. ********************************************************************************** */
  1017. static void BleApp_StoreServiceHandles(deviceId_t peerDeviceId, gattService_t *pService)
  1018. {
  1019. /* Found Wireless UART Service */
  1020. maPeerInformation[peerDeviceId].clientInfo.hService = pService->startHandle;
  1021.  
  1022. if (pService->cNumCharacteristics > 0U &&
  1023. pService->aCharacteristics != NULL)
  1024. {
  1025. /* Found Uart Characteristic */
  1026. maPeerInformation[peerDeviceId].clientInfo.hUartStream =
  1027. pService->aCharacteristics[0].value.handle;
  1028. }
  1029. }
  1030.  
  1031. static void BleApp_SendUartStream(uint8_t *pRecvStream, uint8_t streamSize)
  1032. {
  1033. gattCharacteristic_t characteristic = {gGattCharPropNone_c, {0}, 0, 0};
  1034. uint8_t mPeerId = 0;
  1035.  
  1036. /* send UART stream to all peers */
  1037. for (mPeerId = 0; mPeerId < (uint8_t)gAppMaxConnections_c; mPeerId++)
  1038. {
  1039. if (gInvalidDeviceId_c != maPeerInformation[mPeerId].deviceId &&
  1040. mAppRunning_c == maPeerInformation[mPeerId].appState)
  1041. {
  1042. characteristic.value.handle = maPeerInformation[mPeerId].clientInfo.hUartStream;
  1043. (void)GattClient_WriteCharacteristicValue(mPeerId, &characteristic,
  1044. streamSize, pRecvStream, TRUE,
  1045. FALSE, FALSE, NULL);
  1046. }
  1047. }
  1048. }
  1049.  
  1050. void BleApp_StateMachineHandler(deviceId_t peerDeviceId, appEvent_t event)
  1051. {
  1052. uint16_t tempMtu = 0;
  1053. union
  1054. {
  1055. uint8_t *pUuidArray;
  1056. bleUuid_t *pUuidObj;
  1057. } temp; /* MISRA rule 11.3 */
  1058.  
  1059. temp.pUuidArray = uuid_service_wireless_uart;
  1060.  
  1061. /* invalid client information */
  1062. if (gInvalidDeviceId_c == maPeerInformation[peerDeviceId].deviceId)
  1063. {
  1064. return;
  1065. }
  1066.  
  1067. switch (maPeerInformation[peerDeviceId].appState)
  1068. {
  1069. case mAppIdle_c:
  1070. {
  1071. if (event == mAppEvt_PeerConnected_c)
  1072. {
  1073. /* Let the central device initiate the Exchange MTU procedure*/
  1074. if (mGapRole == gGapCentral_c)
  1075. {
  1076. /* Moving to Exchange MTU State */
  1077. maPeerInformation[peerDeviceId].appState = mAppExchangeMtu_c;
  1078. (void)GattClient_ExchangeMtu(peerDeviceId);
  1079. }
  1080. else
  1081. {
  1082. /* Moving to Service Discovery State*/
  1083. maPeerInformation[peerDeviceId].appState = mAppServiceDisc_c;
  1084.  
  1085. /* Start Service Discovery*/
  1086. (void)BleServDisc_FindService(peerDeviceId,
  1087. gBleUuidType128_c,
  1088. temp.pUuidObj);
  1089. }
  1090. }
  1091. }
  1092. break;
  1093.  
  1094. case mAppExchangeMtu_c:
  1095. {
  1096. if (event == mAppEvt_GattProcComplete_c)
  1097. {
  1098. /* update stream length with minimum of maximum MTU's of connected devices */
  1099. (void)Gatt_GetMtu(peerDeviceId, &tempMtu);
  1100. tempMtu = gAttMaxWriteDataSize_d(tempMtu);
  1101.  
  1102. mAppUartBufferSize = mAppUartBufferSize <= tempMtu ? mAppUartBufferSize : tempMtu;
  1103.  
  1104. /* Moving to Service Discovery State*/
  1105. maPeerInformation[peerDeviceId].appState = mAppServiceDisc_c;
  1106.  
  1107. /* Start Service Discovery*/
  1108. (void)BleServDisc_FindService(peerDeviceId,
  1109. gBleUuidType128_c,
  1110. temp.pUuidObj);
  1111. }
  1112. else
  1113. {
  1114. if (event == mAppEvt_GattProcError_c)
  1115. {
  1116. (void)Gap_Disconnect(peerDeviceId);
  1117. }
  1118. }
  1119. }
  1120. break;
  1121.  
  1122. case mAppServiceDisc_c:
  1123. {
  1124. if (event == mAppEvt_ServiceDiscoveryComplete_c)
  1125. {
  1126. /* Moving to Running State*/
  1127. maPeerInformation[peerDeviceId].appState = mAppRunning_c;
  1128. #if gAppUseBonding_d
  1129. /* Write data in NVM */
  1130. (void)Gap_SaveCustomPeerInformation(maPeerInformation[peerDeviceId].deviceId,
  1131. (void *) &maPeerInformation[peerDeviceId].clientInfo, 0,
  1132. sizeof(wucConfig_t));
  1133. #endif
  1134. }
  1135. else if (event == mAppEvt_ServiceDiscoveryNotFound_c)
  1136. {
  1137. /* Moving to Service discovery Retry State*/
  1138. maPeerInformation[peerDeviceId].appState = mAppServiceDiscRetry_c;
  1139. /* Restart Service Discovery for all services */
  1140. (void)BleServDisc_Start(peerDeviceId);
  1141. }
  1142. else if (event == mAppEvt_ServiceDiscoveryFailed_c)
  1143. {
  1144. (void)Gap_Disconnect(peerDeviceId);
  1145. }
  1146. else
  1147. {
  1148. /* ignore other event types */
  1149. }
  1150. }
  1151. break;
  1152.  
  1153. case mAppServiceDiscRetry_c:
  1154. {
  1155. if (event == mAppEvt_ServiceDiscoveryComplete_c)
  1156. {
  1157. /* Moving to Running State*/
  1158. maPeerInformation[peerDeviceId].appState = mAppRunning_c;
  1159. }
  1160. else if ((event == mAppEvt_ServiceDiscoveryNotFound_c) ||
  1161. (event == mAppEvt_ServiceDiscoveryFailed_c))
  1162. {
  1163. (void)Gap_Disconnect(peerDeviceId);
  1164. }
  1165. else
  1166. {
  1167. /* ignore other event types */
  1168. }
  1169. }
  1170. break;
  1171.  
  1172. case mAppRunning_c:
  1173. break;
  1174.  
  1175. default:
  1176. {
  1177. ; /* No action required */
  1178. }
  1179. break;
  1180. }
  1181. }
  1182.  
  1183. /*! *********************************************************************************
  1184. * \brief Handles scanning timer callback.
  1185. *
  1186. * \param[in] pParam Callback parameters.
  1187. ********************************************************************************** */
  1188. static void ScanningTimerCallback(void *pParam)
  1189. {
  1190. /* Stop scanning */
  1191. (void)Gap_StopScanning();
  1192. }
  1193.  
  1194. static void BleApp_FlushUartStream(void *pParam)
  1195. {
  1196. uint8_t *pMsg = NULL;
  1197. uint16_t bytesRead = 0;
  1198. uint8_t mPeerId = 0;
  1199. bool_t mValidDevices = FALSE;
  1200.  
  1201. /* Valid devices are in Running state */
  1202. for (mPeerId = 0; mPeerId < (uint8_t)gAppMaxConnections_c; mPeerId++)
  1203. {
  1204. if ((gInvalidDeviceId_c != maPeerInformation[mPeerId].deviceId) &&
  1205. (mAppRunning_c == maPeerInformation[mPeerId].appState))
  1206. {
  1207. mValidDevices = TRUE;
  1208. break;
  1209. }
  1210. }
  1211.  
  1212. if (mValidDevices)
  1213. {
  1214. /* Allocate buffer for GATT Write */
  1215. pMsg = MEM_BufferAlloc(mAppUartBufferSize);
  1216.  
  1217. if (pMsg != NULL)
  1218. {
  1219. /* Collect the data from the serial manager buffer */
  1220. if (Serial_Read(gAppSerMgrIf, pMsg, mAppUartBufferSize, &bytesRead) == gSerial_Success_c)
  1221. {
  1222. if (bytesRead != 0U)
  1223. {
  1224. /* Send data over the air */
  1225. BleApp_SendUartStream(pMsg, (uint8_t)bytesRead);
  1226. }
  1227. }
  1228.  
  1229. /* Free Buffer */
  1230. (void)MEM_BufferFree(pMsg);
  1231. }
  1232. }
  1233.  
  1234. mAppDapaPending = FALSE;
  1235. }
  1236.  
  1237. static void BleApp_ReceivedUartStream(deviceId_t peerDeviceId, uint8_t *pStream, uint16_t streamLength)
  1238. {
  1239. static deviceId_t previousDeviceId = gInvalidDeviceId_c;
  1240.  
  1241. char additionalInfoBuff[10] = { '\r', '\n', '[', '0', '0', '-', 'M', ']', ':', ' '};
  1242. uint8_t *pBuffer = NULL;
  1243. uint32_t messageHeaderSize = 0;
  1244.  
  1245. if (mAppUartNewLine || (previousDeviceId != peerDeviceId))
  1246. {
  1247. streamLength += (uint32_t)sizeof(additionalInfoBuff);
  1248. }
  1249.  
  1250. /* Allocate buffer for asynchronous write */
  1251. pBuffer = MEM_BufferAlloc(streamLength);
  1252.  
  1253. if (pBuffer != NULL)
  1254. {
  1255. /* if this is a message from a previous device, print device ID */
  1256. if (mAppUartNewLine || (previousDeviceId != peerDeviceId))
  1257. {
  1258. messageHeaderSize = sizeof(additionalInfoBuff);
  1259.  
  1260. if (mAppUartNewLine)
  1261. {
  1262. mAppUartNewLine = FALSE;
  1263. }
  1264.  
  1265. additionalInfoBuff[3] = '0' + (peerDeviceId / 10U);
  1266. additionalInfoBuff[4] = '0' + (peerDeviceId % 10U);
  1267.  
  1268. if (gGapCentral_c != maPeerInformation[peerDeviceId].gapRole)
  1269. {
  1270. additionalInfoBuff[6] = 'S';
  1271. }
  1272.  
  1273. FLib_MemCpy(pBuffer, additionalInfoBuff, sizeof(additionalInfoBuff));
  1274. }
  1275.  
  1276. FLib_MemCpy(pBuffer + messageHeaderSize, pStream, (uint32_t)streamLength - messageHeaderSize);
  1277. (void)Serial_AsyncWrite(gAppSerMgrIf, pBuffer, streamLength, Uart_TxCallBack, pBuffer);
  1278. }
  1279.  
  1280. /* update the previous device ID */
  1281. previousDeviceId = peerDeviceId;
  1282. }
  1283.  
  1284. static void UartStreamFlushTimerCallback(void *pData)
  1285. {
  1286. if (!mAppDapaPending)
  1287. {
  1288. mAppDapaPending = TRUE;
  1289. (void)App_PostCallbackMessage(BleApp_FlushUartStream, NULL);
  1290. }
  1291. }
  1292.  
  1293. /*! *********************************************************************************
  1294. * \brief Handles UART Receive callback.
  1295. *
  1296. * \param[in] pData Parameters.
  1297. ********************************************************************************** */
  1298. static void Uart_RxCallBack(void *pData)
  1299. {
  1300. uint16_t byteCount = 0;
  1301.  
  1302. (void)Serial_RxBufferByteCount(gAppSerMgrIf, &byteCount);
  1303.  
  1304. if (byteCount < mAppUartBufferSize)
  1305. {
  1306. /* Restart flush timer */
  1307. (void)TMR_StartLowPowerTimer(mUartStreamFlushTimerId,
  1308. gTmrLowPowerSingleShotMillisTimer_c,
  1309. mAppUartFlushIntervalInMs_c,
  1310. UartStreamFlushTimerCallback, NULL);
  1311. }
  1312. else
  1313. {
  1314. /* Post App Msg only one at a time */
  1315. if (!mAppDapaPending)
  1316. {
  1317. mAppDapaPending = TRUE;
  1318. (void)App_PostCallbackMessage(BleApp_FlushUartStream, NULL);
  1319. }
  1320. }
  1321. }
  1322.  
  1323. /*! *********************************************************************************
  1324. * \brief Handles UART Transmit callback.
  1325. *
  1326. * \param[in] pData Parameters.
  1327. ********************************************************************************** */
  1328. static void Uart_TxCallBack(void *pBuffer)
  1329. {
  1330. (void)MEM_BufferFree(pBuffer);
  1331. }
  1332.  
  1333.  
  1334. /*! *********************************************************************************
  1335. * \brief Handles battery measurement timer callback.
  1336. *
  1337. * \param[in] pParam Callback parameters.
  1338. ********************************************************************************** */
  1339. static void BatteryMeasurementTimerCallback(void *pParam)
  1340. {
  1341. mBasServiceConfig.batteryLevel = BOARD_GetBatteryLevel();
  1342. (void)Bas_RecordBatteryMeasurement(&mBasServiceConfig);
  1343. }
  1344.  
  1345. /*! *********************************************************************************
  1346. * @}
  1347. ********************************************************************************** */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement