app_wrist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "adc.h"
  19. #include "light.h"
  20. #include "app_wrist.h"
  21. #include "wrist_service.h"
  22. // GAP connection handle
  23. // 事件处理Id
  24. uint8 AppWrist_TaskID;
  25. static uint16 gapConnHandle;
  26. static gaprole_States_t gapProfileState = GAPROLE_INIT;
  27. static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = BleName;
  28. // Advertising user-cancelled state
  29. static bool WristAdvCancelled = FALSE;
  30. static uint8 scanData[] =
  31. {
  32. 0xa, // length of this data
  33. GAP_ADTYPE_LOCAL_NAME_COMPLETE,
  34. 'b',
  35. 'l',
  36. 'e',
  37. 'L',
  38. 'i',
  39. 'g',
  40. 'h',
  41. 't',
  42. ' ',
  43. };
  44. static uint8 advertData[] =
  45. {
  46. // flags
  47. 0x02,
  48. GAP_ADTYPE_FLAGS,
  49. GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
  50. // service UUIDs
  51. 0x03,
  52. GAP_ADTYPE_16BIT_MORE,
  53. LO_UINT16(HEARTRATE_SERV_UUID),
  54. HI_UINT16(HEARTRATE_SERV_UUID),
  55. };
  56. /**
  57. * @fn appProcOSALMsg
  58. *
  59. * @brief Process an incoming task message.
  60. *
  61. * @param pMsg - message to process
  62. *
  63. * @return none
  64. */
  65. static void appWristProcOSALMsg( osal_event_hdr_t *pMsg );
  66. static void WristGapStateCB( gaprole_States_t newState);
  67. // GAP Role Callbacks
  68. static gapRolesCBs_t WristPeripheralCB =
  69. {
  70. WristGapStateCB, // Profile State Change Callbacks
  71. NULL // When a valid RSSI is read from controller
  72. };
  73. static void appWristProcOSALMsg( osal_event_hdr_t *pMsg )
  74. {
  75. LOG("[fn : appWristProcOSALMsg] run event is: %d \n", pMsg->event);
  76. }
  77. // Notification from the profile of a state change
  78. static void WristGapStateCB(gaprole_States_t newState)
  79. {
  80. LOG("app is start gapProfileState: %d WristGapStateCB: %d \n", gapProfileState, newState);
  81. // if connected
  82. if (newState == GAPROLE_CONNECTED)
  83. {
  84. // get connection handle
  85. GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle);
  86. }
  87. // if disconnected
  88. else if (gapProfileState == GAPROLE_CONNECTED &&
  89. newState != GAPROLE_CONNECTED)
  90. {
  91. uint8 advState = TRUE;
  92. wristProfile_HandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED );
  93. if ( newState == GAPROLE_WAITING_AFTER_TIMEOUT )
  94. {
  95. // link loss timeout-- use fast advertising
  96. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL );
  97. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL );
  98. GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION );
  99. }
  100. else
  101. {
  102. // Else use slow advertising
  103. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL );
  104. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL );
  105. GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION );
  106. }
  107. // Enable advertising
  108. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState );
  109. }
  110. // if advertising stopped
  111. else if ( gapProfileState == GAPROLE_ADVERTISING &&
  112. newState == GAPROLE_WAITING )
  113. {
  114. // if advertising stopped by user
  115. if ( WristAdvCancelled )
  116. {
  117. WristAdvCancelled = FALSE;
  118. }
  119. // if fast advertising switch to slow
  120. else if ( GAP_GetParamValue( TGAP_GEN_DISC_ADV_INT_MIN ) == DEFAULT_FAST_ADV_INTERVAL )
  121. {
  122. uint8 advState = TRUE;
  123. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL );
  124. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL );
  125. GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION );
  126. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState );
  127. }
  128. }
  129. // if started
  130. else if (newState == GAPROLE_STARTED)
  131. {
  132. // Set the system ID from the bd addr
  133. uint8 systemId[DEVINFO_SYSTEM_ID_LEN];
  134. GAPRole_GetParameter(GAPROLE_BD_ADDR, systemId);
  135. // shift three bytes up
  136. systemId[7] = systemId[5];
  137. systemId[6] = systemId[4];
  138. systemId[5] = systemId[3];
  139. // set middle bytes to zero
  140. systemId[4] = 0;
  141. systemId[3] = 0;
  142. DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);
  143. }
  144. gapProfileState = newState;
  145. }
  146. static void wristCB(uint8 event, uint8 param_size, uint8* param)
  147. {
  148. switch(event){
  149. case WRIST_NOTI_ENABLED:
  150. // if connected start periodic measurement
  151. if (gapProfileState == GAPROLE_CONNECTED)
  152. {
  153. //osal_start_timerEx( AppWrist_TaskID, HEART_PERIODIC_EVT, DEFAULT_HEARTRATE_PERIOD );
  154. }
  155. break;
  156. case WRIST_NOTI_DISABLED:
  157. // stop periodic measurement
  158. //osal_stop_timerEx( AppWrist_TaskID, HEART_PERIODIC_EVT );
  159. break;
  160. }
  161. }
  162. static void user_key_1_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  163. {
  164. LOG("user key 1 handler:%d %d\n", pin, type);
  165. }
  166. static void user_key_2_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  167. {
  168. LOG("user key 2 handler:%d %d\n", pin, type);
  169. }
  170. static void user_key_3_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  171. {
  172. LOG("user key 3 handler:%d %d\n", pin, type);
  173. }
  174. static void user_key_4_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  175. {
  176. LOG("user key 4 handler:%d %d\n", pin, type);
  177. }
  178. static void user_key_1_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  179. {
  180. LOG("+++ 1 handler ppp:%d %d\n", pin, type);
  181. }
  182. static void user_key_2_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  183. {
  184. LOG("+++ 2 handler ppp:%d %d\n", pin, type);
  185. }
  186. static void user_key_3_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  187. {
  188. LOG("+++ 3 handler ppp:%d %d\n", pin, type);
  189. }
  190. static void user_key_4_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
  191. {
  192. LOG("+++ 4 handler ppp:%d %d\n", pin, type);
  193. }
  194. static void getPinState(){
  195. LOG("---------------------------------\n\n\n");
  196. LOG("P01:%d\n",hal_gpio_read(GPIO_P01));
  197. LOG("P02:%d\n",hal_gpio_read(GPIO_P02));
  198. LOG("P03:%d\n",hal_gpio_read(GPIO_P03));
  199. LOG("P04:%d\n",hal_gpio_read(GPIO_P04));
  200. LOG("P05:%d\n",hal_gpio_read(GPIO_P05));
  201. LOG("---------------------------------\n");
  202. }
  203. void test_adc_evt(adc_Evt_t* pev){
  204. int i = 0;
  205. LOG("test_adc_evt\n");
  206. if(pev->type == HAL_ADC_EVT_DATA){
  207. LOG("HAL_ADC_EVT_DATA\n");
  208. }
  209. // typedef struct _adc_Evt_t{
  210. // int type;
  211. // adc_CH_t ch;
  212. // uint16_t* data;
  213. // uint8_t size; //word size
  214. // }adc_Evt_t;
  215. LOG("adc event type:%d ch:%d\n", pev->type, pev->ch);
  216. // 输出adc数据
  217. LOG("[adc data] size:%d value: ", pev->size);
  218. for(i = 0; i < pev->size; i++){
  219. LOG("%02x", pev->data[i]);
  220. }
  221. LOG("\n");
  222. uint16 adc_buf[6][64];
  223. osal_memcpy(adc_buf[pev->ch-2], pev->data, 2*(pev->size));
  224. // 输出adcbuf数据
  225. for(i = 0; i < 6; i++){
  226. LOG("adc_buf[%d]: ", i);
  227. for(int j = 0; j < 64; j++){
  228. LOG("%02x", adc_buf[i][j]);
  229. }
  230. LOG("\n");
  231. }
  232. float value = 0;
  233. static uint8_t channel_done_flag = 0;
  234. channel_done_flag |= BIT(pev->ch);
  235. for(i=2;i<8;i++)
  236. {
  237. if(channel_done_flag & BIT(i))
  238. {
  239. // is_high_resolution = (adc_cfg.is_high_resolution & BIT(i))?TRUE:FALSE;
  240. // is_differential_mode = (adc_cfg.is_differential_mode & BIT(i))?TRUE:FALSE;
  241. LOG("PEV: %d %d %d\n", i, pev->size, pev->data[0]);
  242. value = hal_adc_value_cal((adc_CH_t)i, adc_buf[i-2], pev->size, TRUE, FALSE);
  243. // 最终电压值
  244. if(i<7)
  245. {
  246. LOG("P%d %d mv val: %d mv V: %d",(i+9),(int)(value*1000), (int)(value * 1000 * 4), (int)(value*1000) * 4 * 12);
  247. // 输出电压值 V 12倍 浮点数
  248. LOG("[BATT]: %dmV", (int)(value*1000) * 4 * 13);
  249. } else
  250. {
  251. LOG("P%d %d mv ",(20),(int)(value*1000));
  252. }
  253. }
  254. }
  255. // getPinState();
  256. }
  257. void test_adc_read(){
  258. LOG("[test_adc_read] read Read Adc");
  259. hal_adc_init();
  260. GPIO_Pin_e controlPin = P34;
  261. GPIO_Pin_e adcPin = P14;
  262. adc_Cfg_t adc_cfg = {
  263. .channel = ADC_BIT(ADC_CH2P_P14),
  264. .is_continue_mode = FALSE,
  265. .is_differential_mode = FALSE,
  266. .is_high_resolution = TRUE,
  267. };
  268. // 注册adc
  269. hal_adc_config_channel(adc_cfg, test_adc_evt);
  270. LOG("hal_adc_config_channel end\n");
  271. hal_adc_start();
  272. }
  273. void appWristInit( uint8 task_id)
  274. {
  275. AppWrist_TaskID = task_id;
  276. LOG("\n\n\nappWristInit\n\n\n");
  277. // Setup the GAP Peripheral Role Profile
  278. {
  279. uint8 initial_advertising_enable = TRUE;
  280. uint8 advChnMap = GAP_ADVCHAN_37 | GAP_ADVCHAN_38 | GAP_ADVCHAN_39;
  281. // By setting this to zero, the device will go into the waiting state after
  282. // being discoverable for 30.72 second, and will not being advertising again
  283. // until the enabler is set back to TRUE
  284. uint16 gapRole_AdvertOffTime = 0;
  285. uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST;
  286. uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
  287. uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
  288. uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY;
  289. uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT;
  290. uint8 peerPublicAddr[] = {
  291. 0x01,
  292. 0x02,
  293. 0x03,
  294. 0x04,
  295. 0x05,
  296. 0x06
  297. };
  298. GAPRole_SetParameter(GAPROLE_ADV_DIRECT_ADDR, sizeof(peerPublicAddr), peerPublicAddr);
  299. // set adv channel map
  300. GAPRole_SetParameter(GAPROLE_ADV_CHANNEL_MAP, sizeof(uint8), &advChnMap);
  301. // Set the GAP Role Parameters
  302. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
  303. GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );
  304. GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanData ), scanData );
  305. GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
  306. GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
  307. GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
  308. GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
  309. GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
  310. GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );
  311. }
  312. // Set the GAP Characteristics
  313. GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );
  314. // Set advertising interval
  315. {
  316. uint16 advInt = 400; // actual time = advInt * 625us
  317. GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
  318. GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
  319. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
  320. GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
  321. }
  322. // Initialize GATT attributes
  323. GGS_AddService( GATT_ALL_SERVICES ); // GAP
  324. GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes
  325. DevInfo_AddService( );
  326. ota_app_AddService();
  327. // 初始化蓝牙模块
  328. wristProfile_AddService(wristCB);
  329. app_datetime_init();
  330. // Setup a delayed profile startup
  331. osal_set_event( AppWrist_TaskID, START_DEVICE_EVT );
  332. // light_init(-1);
  333. LOG("appWristInit end\n");
  334. // 初始化按键
  335. // temp_set(5500, 0, NULL);
  336. LOG("temp_set end\n");
  337. // light_set(10, 0, NULL);
  338. LOG("appWristInit end\n");
  339. // test_adc_read();
  340. // 注册定时器. 持续读取数据
  341. // osal_start_reload_timer( AppWrist_TaskID, TIMER_LIGHT_EVT, 2000);
  342. }
  343. // 事件处理器
  344. uint16 appWristProcEvt( uint8 task_id, uint16 events )
  345. {
  346. if ( events & SYS_EVENT_MSG )
  347. {
  348. uint8 *pMsg;
  349. if ( (pMsg = osal_msg_receive( AppWrist_TaskID )) != NULL )
  350. {
  351. appWristProcOSALMsg( (osal_event_hdr_t *)pMsg );
  352. // Release the OSAL message
  353. VOID osal_msg_deallocate( pMsg );
  354. }
  355. // return unprocessed events
  356. return (events ^ SYS_EVENT_MSG);
  357. }
  358. if ( events & START_DEVICE_EVT )
  359. {
  360. // LOG("START_DEVICE_EVT\n");
  361. // Start the Device
  362. VOID GAPRole_StartDevice( &WristPeripheralCB );
  363. // Register with bond manager after starting device
  364. // GAPBondMgr_Register( (gapBondCBs_t *) &WristBondCB );
  365. return ( events ^ START_DEVICE_EVT );
  366. }
  367. if ( events & TIMER_LIGHT_EVT )
  368. {
  369. LOG("TIMER_LIGHT_EVT\n");
  370. return ( events ^ TIMER_LIGHT_EVT );
  371. }
  372. return 0;
  373. }