app_wrist.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include <string.h>
  2. #include "bcomdef.h"
  3. #include "OSAL.h"
  4. #include "linkdb.h"
  5. #include "gatt.h"
  6. #include "gapgattserver.h"
  7. #include "gattservapp.h"
  8. #include "ota_app_service.h"
  9. #include "gatt_profile_uuid.h"
  10. // #include "wristservice.h"
  11. #include "devinfoservice.h"
  12. #include "peripheral.h"
  13. #include "gapbondmgr.h"
  14. // #include "led_light.h"
  15. #include "kscan.h"
  16. #include "log.h"
  17. #include "ll.h"
  18. #include "light.h"
  19. #include "app_wrist.h"
  20. #include "wrist_service.h"
  21. // GAP connection handle
  22. // 事件处理Id
  23. uint8 AppWrist_TaskID;
  24. static uint16 gapConnHandle;
  25. static gaprole_States_t gapProfileState = GAPROLE_INIT;
  26. static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = BleName;
  27. // Advertising user-cancelled state
  28. static bool WristAdvCancelled = FALSE;
  29. static uint8 scanData[] =
  30. {
  31. 0xa, // length of this data
  32. GAP_ADTYPE_LOCAL_NAME_COMPLETE,
  33. 'b',
  34. 'l',
  35. 'e',
  36. 'L',
  37. 'i',
  38. 'g',
  39. 'h',
  40. 't',
  41. ' ',
  42. };
  43. static uint8 advertData[] =
  44. {
  45. // flags
  46. 0x02,
  47. GAP_ADTYPE_FLAGS,
  48. GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
  49. // service UUIDs
  50. 0x03,
  51. GAP_ADTYPE_16BIT_MORE,
  52. LO_UINT16(HEARTRATE_SERV_UUID),
  53. HI_UINT16(HEARTRATE_SERV_UUID),
  54. };
  55. /**
  56. * @fn appProcOSALMsg
  57. *
  58. * @brief Process an incoming task message.
  59. *
  60. * @param pMsg - message to process
  61. *
  62. * @return none
  63. */
  64. static void appWristProcOSALMsg( osal_event_hdr_t *pMsg );
  65. static void WristGapStateCB( gaprole_States_t newState);
  66. // GAP Role Callbacks
  67. static gapRolesCBs_t WristPeripheralCB =
  68. {
  69. WristGapStateCB, // Profile State Change Callbacks
  70. NULL // When a valid RSSI is read from controller
  71. };
  72. static void appWristProcOSALMsg( osal_event_hdr_t *pMsg )
  73. {
  74. LOG("[fn : appWristProcOSALMsg] run event is: %d \n", pMsg->event);
  75. }
  76. // Notification from the profile of a state change
  77. static void WristGapStateCB(gaprole_States_t newState)
  78. {
  79. LOG("app is start gapProfileState: %d WristGapStateCB: %d \n", gapProfileState, newState);
  80. // if connected
  81. if (newState == GAPROLE_CONNECTED)
  82. {
  83. // get connection handle
  84. GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle);
  85. }
  86. // if disconnected
  87. else if (gapProfileState == GAPROLE_CONNECTED &&
  88. newState != GAPROLE_CONNECTED)
  89. {
  90. uint8 advState = TRUE;
  91. wristProfile_HandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED );
  92. if ( newState == GAPROLE_WAITING_AFTER_TIMEOUT )
  93. {
  94. // link loss timeout-- use fast advertising
  95. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL );
  96. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL );
  97. GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION );
  98. }
  99. else
  100. {
  101. // Else use slow advertising
  102. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL );
  103. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL );
  104. GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION );
  105. }
  106. // Enable advertising
  107. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState );
  108. }
  109. // if advertising stopped
  110. else if ( gapProfileState == GAPROLE_ADVERTISING &&
  111. newState == GAPROLE_WAITING )
  112. {
  113. // if advertising stopped by user
  114. if ( WristAdvCancelled )
  115. {
  116. WristAdvCancelled = FALSE;
  117. }
  118. // if fast advertising switch to slow
  119. else if ( GAP_GetParamValue( TGAP_GEN_DISC_ADV_INT_MIN ) == DEFAULT_FAST_ADV_INTERVAL )
  120. {
  121. uint8 advState = TRUE;
  122. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL );
  123. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL );
  124. GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION );
  125. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState );
  126. }
  127. }
  128. // if started
  129. else if (newState == GAPROLE_STARTED)
  130. {
  131. // Set the system ID from the bd addr
  132. uint8 systemId[DEVINFO_SYSTEM_ID_LEN];
  133. GAPRole_GetParameter(GAPROLE_BD_ADDR, systemId);
  134. // shift three bytes up
  135. systemId[7] = systemId[5];
  136. systemId[6] = systemId[4];
  137. systemId[5] = systemId[3];
  138. // set middle bytes to zero
  139. systemId[4] = 0;
  140. systemId[3] = 0;
  141. DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);
  142. }
  143. gapProfileState = newState;
  144. }
  145. static void wristCB(uint8 event, uint8 param_size, uint8* param)
  146. {
  147. switch(event){
  148. case WRIST_NOTI_ENABLED:
  149. // if connected start periodic measurement
  150. if (gapProfileState == GAPROLE_CONNECTED)
  151. {
  152. //osal_start_timerEx( AppWrist_TaskID, HEART_PERIODIC_EVT, DEFAULT_HEARTRATE_PERIOD );
  153. }
  154. break;
  155. case WRIST_NOTI_DISABLED:
  156. // stop periodic measurement
  157. //osal_stop_timerEx( AppWrist_TaskID, HEART_PERIODIC_EVT );
  158. break;
  159. }
  160. }
  161. static void user_key_1_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  162. {
  163. LOG("user key 1 handler:%d %d\n", pin, type);
  164. }
  165. static void user_key_2_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  166. {
  167. LOG("user key 2 handler:%d %d\n", pin, type);
  168. }
  169. static void user_key_3_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  170. {
  171. LOG("user key 3 handler:%d %d\n", pin, type);
  172. }
  173. static void user_key_4_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  174. {
  175. LOG("user key 4 handler:%d %d\n", pin, type);
  176. }
  177. static void user_key_1_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  178. {
  179. LOG("+++ 1 handler ppp:%d %d\n", pin, type);
  180. }
  181. static void user_key_2_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  182. {
  183. LOG("+++ 2 handler ppp:%d %d\n", pin, type);
  184. }
  185. static void user_key_3_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  186. {
  187. LOG("+++ 3 handler ppp:%d %d\n", pin, type);
  188. }
  189. static void user_key_4_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  190. {
  191. LOG("+++ 4 handler ppp:%d %d\n", pin, type);
  192. }
  193. static void getPinState(){
  194. LOG("---------------------------------\n\n\n");
  195. LOG("P01:%d\n",hal_gpio_read(GPIO_P01));
  196. LOG("P02:%d\n",hal_gpio_read(GPIO_P02));
  197. LOG("P03:%d\n",hal_gpio_read(GPIO_P03));
  198. LOG("P04:%d\n",hal_gpio_read(GPIO_P04));
  199. LOG("P05:%d\n",hal_gpio_read(GPIO_P05));
  200. LOG("---------------------------------\n");
  201. }
  202. void appWristInit( uint8 task_id)
  203. {
  204. AppWrist_TaskID = task_id;
  205. LOG("\n\n\nappWristInit\n\n\n");
  206. // Setup the GAP Peripheral Role Profile
  207. {
  208. uint8 initial_advertising_enable = TRUE;
  209. uint8 advChnMap = GAP_ADVCHAN_37 | GAP_ADVCHAN_38 | GAP_ADVCHAN_39;
  210. // By setting this to zero, the device will go into the waiting state after
  211. // being discoverable for 30.72 second, and will not being advertising again
  212. // until the enabler is set back to TRUE
  213. uint16 gapRole_AdvertOffTime = 0;
  214. uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST;
  215. uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
  216. uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
  217. uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY;
  218. uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT;
  219. uint8 peerPublicAddr[] = {
  220. 0x01,
  221. 0x02,
  222. 0x03,
  223. 0x04,
  224. 0x05,
  225. 0x06
  226. };
  227. GAPRole_SetParameter(GAPROLE_ADV_DIRECT_ADDR, sizeof(peerPublicAddr), peerPublicAddr);
  228. // set adv channel map
  229. GAPRole_SetParameter(GAPROLE_ADV_CHANNEL_MAP, sizeof(uint8), &advChnMap);
  230. // Set the GAP Role Parameters
  231. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
  232. GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );
  233. GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanData ), scanData );
  234. GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
  235. GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
  236. GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
  237. GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
  238. GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
  239. GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );
  240. }
  241. // Set the GAP Characteristics
  242. GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );
  243. // Set advertising interval
  244. {
  245. uint16 advInt = 400; // actual time = advInt * 625us
  246. GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
  247. GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
  248. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
  249. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
  250. }
  251. // Initialize GATT attributes
  252. GGS_AddService( GATT_ALL_SERVICES ); // GAP
  253. GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes
  254. DevInfo_AddService( );
  255. ota_app_AddService();
  256. // 初始化蓝牙模块
  257. wristProfile_AddService(wristCB);
  258. app_datetime_init();
  259. // Setup a delayed profile startup
  260. osal_set_event( AppWrist_TaskID, START_DEVICE_EVT );
  261. // light_init(-1);
  262. LOG("appWristInit end\n");
  263. // 初始化按键
  264. temp_set(5500, 0, NULL);
  265. LOG("temp_set end\n");
  266. light_set(10, 0, NULL);
  267. LOG("appWristInit end\n");
  268. }
  269. // 事件处理器
  270. uint16 appWristProcEvt( uint8 task_id, uint16 events )
  271. {
  272. if ( events & SYS_EVENT_MSG )
  273. {
  274. uint8 *pMsg;
  275. if ( (pMsg = osal_msg_receive( AppWrist_TaskID )) != NULL )
  276. {
  277. appWristProcOSALMsg( (osal_event_hdr_t *)pMsg );
  278. // Release the OSAL message
  279. VOID osal_msg_deallocate( pMsg );
  280. }
  281. // return unprocessed events
  282. return (events ^ SYS_EVENT_MSG);
  283. }
  284. if ( events & START_DEVICE_EVT )
  285. {
  286. // LOG("START_DEVICE_EVT\n");
  287. // Start the Device
  288. VOID GAPRole_StartDevice( &WristPeripheralCB );
  289. // Register with bond manager after starting device
  290. // GAPBondMgr_Register( (gapBondCBs_t *) &WristBondCB );
  291. return ( events ^ START_DEVICE_EVT );
  292. }
  293. if ( events & TIMER_LIGHT_EVT )
  294. {
  295. LOG("TIMER_LIGHT_EVT\n");
  296. getPinState();
  297. return ( events ^ TIMER_LIGHT_EVT );
  298. }
  299. return 0;
  300. }