ble_gls.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /**
  2. * Copyright (c) 2012 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "sdk_common.h"
  45. #if NRF_MODULE_ENABLED(BLE_GLS)
  46. #include "ble_gls.h"
  47. #include <string.h>
  48. #include "ble_gls_db.h"
  49. #include "ble_racp.h"
  50. #include "ble_srv_common.h"
  51. #define OPERAND_FILTER_TYPE_SEQ_NUM 0x01 /**< Filter data using Sequence Number criteria. */
  52. #define OPERAND_FILTER_TYPE_FACING_TIME 0x02 /**< Filter data using User Facing Time criteria. */
  53. #define OPERAND_FILTER_TYPE_RFU_START 0x07 /**< Start of filter types reserved For Future Use range */
  54. #define OPERAND_FILTER_TYPE_RFU_END 0xFF /**< End of filter types reserved For Future Use range */
  55. #define OPCODE_LENGTH 1 /**< Length of opcode inside Glucose Measurement packet. */
  56. #define HANDLE_LENGTH 2 /**< Length of handle inside Glucose Measurement packet. */
  57. #define MAX_GLM_LEN (BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - \
  58. HANDLE_LENGTH) /**< Maximum size of a transmitted Glucose Measurement. */
  59. #define RACP_OPERAND_LEN 2 /**< Record Access Control Point operand length. */
  60. #define GLS_NACK_PROC_ALREADY_IN_PROGRESS BLE_GATT_STATUS_ATTERR_APP_BEGIN + 0 /**< Reply when a requested procedure is already in progress. */
  61. #define GLS_NACK_CCCD_IMPROPERLY_CONFIGURED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1 /**< Reply when the a s CCCD is improperly configured. */
  62. static bool m_procesing_active; /**< Processing requested data. */
  63. static uint16_t m_next_seq_num; /**< Sequence number of the next database record. */
  64. static uint8_t m_racp_proc_operator; /**< Operator of current request. */
  65. static uint16_t m_racp_proc_seq_num; /**< Sequence number of current request. */
  66. static uint8_t m_racp_proc_record_ndx; /**< Current record index. */
  67. static uint8_t m_racp_proc_records_reported; /**< Number of reported records. */
  68. /**@brief Function for interception of GATT errors and @ref nrf_ble_gq errors.
  69. *
  70. * @param[in] nrf_error Error code.
  71. * @param[in] p_ctx Parameter from the event handler.
  72. * @param[in] conn_handle Connection handle.
  73. */
  74. static void gatt_error_handler(uint32_t nrf_error,
  75. void * p_ctx,
  76. uint16_t conn_handle)
  77. {
  78. ble_gls_t * p_gls = (ble_gls_t *)p_ctx;
  79. if ((p_gls->error_handler) != NULL && (nrf_error != NRF_ERROR_INVALID_STATE))
  80. {
  81. p_gls->error_handler(nrf_error);
  82. }
  83. }
  84. /**@brief Function for setting the next sequence number by reading the last record in the data base.
  85. *
  86. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  87. */
  88. static uint32_t next_sequence_number_set(void)
  89. {
  90. uint16_t num_records;
  91. ble_gls_rec_t rec;
  92. num_records = ble_gls_db_num_records_get();
  93. if (num_records > 0)
  94. {
  95. // Get last record
  96. uint32_t err_code = ble_gls_db_record_get(num_records - 1, &rec);
  97. if (err_code != NRF_SUCCESS)
  98. {
  99. return err_code;
  100. }
  101. m_next_seq_num = rec.meas.sequence_number + 1;
  102. }
  103. else
  104. {
  105. m_next_seq_num = 0;
  106. }
  107. return NRF_SUCCESS;
  108. }
  109. /**@brief Function for encoding a Glucose measurement.
  110. *
  111. * @param[in] p_meas Measurement to be encoded.
  112. * @param[out] p_encoded_buffer Pointer to buffer where the encoded measurement is to be stored.
  113. *
  114. * @return Size of encoded measurement.
  115. */
  116. static uint8_t gls_meas_encode(const ble_gls_meas_t * p_meas, uint8_t * p_encoded_buffer)
  117. {
  118. uint8_t len = 0;
  119. p_encoded_buffer[len++] = p_meas->flags;
  120. len += uint16_encode(p_meas->sequence_number, &p_encoded_buffer[len]);
  121. len += ble_date_time_encode(&p_meas->base_time, &p_encoded_buffer[len]);
  122. if (p_meas->flags & BLE_GLS_MEAS_FLAG_TIME_OFFSET)
  123. {
  124. len += uint16_encode(p_meas->time_offset, &p_encoded_buffer[len]);
  125. }
  126. if (p_meas->flags & BLE_GLS_MEAS_FLAG_CONC_TYPE_LOC)
  127. {
  128. uint16_t encoded_concentration;
  129. encoded_concentration = ((p_meas->glucose_concentration.exponent << 12) & 0xF000) |
  130. ((p_meas->glucose_concentration.mantissa << 0) & 0x0FFF);
  131. p_encoded_buffer[len++] = (uint8_t)(encoded_concentration);
  132. p_encoded_buffer[len++] = (uint8_t)(encoded_concentration >> 8);
  133. p_encoded_buffer[len++] = (p_meas->sample_location << 4) | (p_meas->type & 0x0F);
  134. }
  135. if (p_meas->flags & BLE_GLS_MEAS_FLAG_SENSOR_STATUS)
  136. {
  137. len += uint16_encode(p_meas->sensor_status_annunciation, &p_encoded_buffer[len]);
  138. }
  139. return len;
  140. }
  141. uint32_t ble_gls_init(ble_gls_t * p_gls, const ble_gls_init_t * p_gls_init)
  142. {
  143. VERIFY_PARAM_NOT_NULL(p_gls);
  144. VERIFY_PARAM_NOT_NULL(p_gls_init);
  145. VERIFY_PARAM_NOT_NULL(p_gls_init->p_gatt_queue);
  146. uint32_t err_code;
  147. uint8_t num_recs;
  148. uint8_t init_value_encoded[MAX_GLM_LEN];
  149. ble_uuid_t ble_uuid;
  150. ble_add_char_params_t add_char_params;
  151. ble_gls_rec_t initial_gls_rec_value;
  152. // Initialize data base
  153. err_code = ble_gls_db_init();
  154. if (err_code != NRF_SUCCESS)
  155. {
  156. return err_code;
  157. }
  158. err_code = next_sequence_number_set();
  159. if (err_code != NRF_SUCCESS)
  160. {
  161. return err_code;
  162. }
  163. // Initialize service structure
  164. p_gls->evt_handler = p_gls_init->evt_handler;
  165. p_gls->error_handler = p_gls_init->error_handler;
  166. p_gls->feature = p_gls_init->feature;
  167. p_gls->is_context_supported = p_gls_init->is_context_supported;
  168. p_gls->conn_handle = BLE_CONN_HANDLE_INVALID;
  169. p_gls->p_gatt_queue = p_gls_init->p_gatt_queue;
  170. // Add service
  171. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_GLUCOSE_SERVICE);
  172. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  173. &ble_uuid,
  174. &p_gls->service_handle);
  175. if (err_code != NRF_SUCCESS)
  176. {
  177. return err_code;
  178. }
  179. // Add glucose measurement characteristic
  180. memset(&add_char_params, 0, sizeof(add_char_params));
  181. memset(&initial_gls_rec_value, 0, sizeof(initial_gls_rec_value));
  182. num_recs = ble_gls_db_num_records_get();
  183. if (num_recs > 0)
  184. {
  185. err_code = ble_gls_db_record_get(num_recs - 1, &initial_gls_rec_value);
  186. if (err_code != NRF_SUCCESS)
  187. {
  188. return err_code;
  189. }
  190. }
  191. add_char_params.uuid = BLE_UUID_GLUCOSE_MEASUREMENT_CHAR;
  192. add_char_params.max_len = MAX_GLM_LEN;
  193. add_char_params.init_len = gls_meas_encode(&initial_gls_rec_value.meas,
  194. init_value_encoded);
  195. add_char_params.is_var_len = true;
  196. add_char_params.char_props.notify = 1;
  197. add_char_params.cccd_write_access = p_gls_init->gl_meas_cccd_wr_sec;
  198. add_char_params.p_init_value = init_value_encoded;
  199. err_code = characteristic_add(p_gls->service_handle, &add_char_params, &p_gls->glm_handles);
  200. if (err_code != NRF_SUCCESS)
  201. {
  202. return err_code;
  203. }
  204. // Add glucose measurement feature characteristic
  205. memset(&add_char_params, 0, sizeof(add_char_params));
  206. add_char_params.uuid = BLE_UUID_GLUCOSE_FEATURE_CHAR;
  207. add_char_params.max_len = sizeof(uint16_t);
  208. add_char_params.init_len = uint16_encode(p_gls->feature, init_value_encoded);
  209. add_char_params.p_init_value = init_value_encoded;
  210. add_char_params.char_props.read = 1;
  211. add_char_params.read_access = p_gls_init->gl_feature_rd_sec;
  212. err_code = characteristic_add(p_gls->service_handle, &add_char_params, &p_gls->glf_handles);
  213. if (err_code != NRF_SUCCESS)
  214. {
  215. return err_code;
  216. }
  217. // Add record control access point characteristic
  218. memset(&add_char_params, 0, sizeof(add_char_params));
  219. add_char_params.uuid = BLE_UUID_RECORD_ACCESS_CONTROL_POINT_CHAR;
  220. add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT;
  221. add_char_params.is_var_len = true;
  222. add_char_params.char_props.indicate = 1;
  223. add_char_params.char_props.write = 1;
  224. add_char_params.cccd_write_access = p_gls_init->racp_cccd_wr_sec;
  225. add_char_params.write_access = p_gls_init->racp_wr_sec;
  226. add_char_params.is_defered_write = true;
  227. err_code = characteristic_add(p_gls->service_handle,
  228. &add_char_params,
  229. &p_gls->racp_handles);
  230. if (err_code != NRF_SUCCESS)
  231. {
  232. return err_code;
  233. }
  234. return NRF_SUCCESS;
  235. }
  236. /**@brief Function for sending a response from the Record Access Control Point.
  237. *
  238. * @param[in] p_gls Service instance.
  239. * @param[in] p_racp_val RACP value to be sent.
  240. */
  241. static void racp_send(ble_gls_t * p_gls, ble_racp_value_t * p_racp_val)
  242. {
  243. uint32_t err_code;
  244. uint8_t encoded_resp[25];
  245. uint16_t len;
  246. // Send indication
  247. len = ble_racp_encode(p_racp_val, encoded_resp);
  248. nrf_ble_gq_req_t gls_req;
  249. memset(&gls_req, 0, sizeof(nrf_ble_gq_req_t));
  250. gls_req.type = NRF_BLE_GQ_REQ_GATTS_HVX;
  251. gls_req.error_handler.cb = gatt_error_handler;
  252. gls_req.error_handler.p_ctx = p_gls;
  253. gls_req.params.gatts_hvx.handle = p_gls->racp_handles.value_handle;
  254. gls_req.params.gatts_hvx.type = BLE_GATT_HVX_INDICATION;
  255. gls_req.params.gatts_hvx.offset = 0;
  256. gls_req.params.gatts_hvx.p_data = encoded_resp;
  257. gls_req.params.gatts_hvx.p_len = &len;
  258. err_code = nrf_ble_gq_item_add(p_gls->p_gatt_queue, &gls_req, p_gls->conn_handle);
  259. // Report error to application
  260. if ((p_gls->error_handler != NULL) &&
  261. (err_code != NRF_SUCCESS) &&
  262. (err_code != NRF_ERROR_INVALID_STATE))
  263. {
  264. p_gls->error_handler(err_code);
  265. }
  266. }
  267. /**@brief Function for sending a RACP response containing a Response Code Op Code and a Response Code Value.
  268. *
  269. * @param[in] p_gls Service instance.
  270. * @param[in] opcode RACP Op Code.
  271. * @param[in] value RACP Response Code Value.
  272. */
  273. static void racp_response_code_send(ble_gls_t * p_gls, uint8_t opcode, uint8_t value)
  274. {
  275. ble_racp_value_t racp_response;
  276. uint8_t pending_racp_response_operand[RACP_OPERAND_LEN] = {0};
  277. memset(&racp_response, 0, sizeof(racp_response));
  278. racp_response.opcode = RACP_OPCODE_RESPONSE_CODE;
  279. racp_response.operator = RACP_OPERATOR_NULL;
  280. racp_response.operand_len = 2;
  281. racp_response.p_operand = pending_racp_response_operand;
  282. pending_racp_response_operand[0] = opcode;
  283. pending_racp_response_operand[1] = value;
  284. racp_send(p_gls, &racp_response);
  285. }
  286. /**@brief Function for sending a glucose measurement/context.
  287. *
  288. * @param[in] p_gls Service instance.
  289. * @param[in] p_rec Measurement to be sent.
  290. *
  291. * @return NRF_SUCCESS on success, otherwise an error code.
  292. */
  293. static uint32_t glucose_meas_send(ble_gls_t * p_gls, ble_gls_rec_t * p_rec)
  294. {
  295. uint32_t err_code;
  296. uint8_t encoded_glm[MAX_GLM_LEN];
  297. uint16_t len;
  298. uint16_t hvx_len;
  299. ble_gatts_hvx_params_t hvx_params;
  300. len = gls_meas_encode(&p_rec->meas, encoded_glm);
  301. hvx_len = len;
  302. memset(&hvx_params, 0, sizeof(hvx_params));
  303. hvx_params.handle = p_gls->glm_handles.value_handle;
  304. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  305. hvx_params.offset = 0;
  306. hvx_params.p_len = &hvx_len;
  307. hvx_params.p_data = encoded_glm;
  308. err_code = sd_ble_gatts_hvx(p_gls->conn_handle, &hvx_params);
  309. if (err_code == NRF_SUCCESS)
  310. {
  311. if (hvx_len != len)
  312. {
  313. err_code = NRF_ERROR_DATA_SIZE;
  314. }
  315. else
  316. {
  317. // Measurement successfully sent
  318. m_racp_proc_records_reported++;
  319. }
  320. }
  321. return err_code;
  322. }
  323. /**@brief Function for responding to the ALL operation.
  324. *
  325. * @param[in] p_gls Service instance.
  326. *
  327. * @return NRF_SUCCESS on success, otherwise an error code.
  328. */
  329. static uint32_t racp_report_records_all(ble_gls_t * p_gls)
  330. {
  331. uint16_t total_records = ble_gls_db_num_records_get();
  332. if (m_racp_proc_record_ndx >= total_records)
  333. {
  334. m_procesing_active = false;
  335. }
  336. else
  337. {
  338. uint32_t err_code;
  339. ble_gls_rec_t rec;
  340. err_code = ble_gls_db_record_get(m_racp_proc_record_ndx, &rec);
  341. if (err_code != NRF_SUCCESS)
  342. {
  343. return err_code;
  344. }
  345. err_code = glucose_meas_send(p_gls, &rec);
  346. if (err_code != NRF_SUCCESS)
  347. {
  348. return err_code;
  349. }
  350. }
  351. return NRF_SUCCESS;
  352. }
  353. /**@brief Function for responding to the FIRST or the LAST operation.
  354. *
  355. * @param[in] p_gls Service instance.
  356. *
  357. * @return NRF_SUCCESS on success, otherwise an error code.
  358. */
  359. static uint32_t racp_report_records_first_last(ble_gls_t * p_gls)
  360. {
  361. uint32_t err_code;
  362. ble_gls_rec_t rec;
  363. uint16_t total_records;
  364. total_records = ble_gls_db_num_records_get();
  365. if ((m_racp_proc_records_reported != 0) || (total_records == 0))
  366. {
  367. m_procesing_active = false;
  368. }
  369. else
  370. {
  371. if (m_racp_proc_operator == RACP_OPERATOR_FIRST)
  372. {
  373. err_code = ble_gls_db_record_get(0, &rec);
  374. if (err_code != NRF_SUCCESS)
  375. {
  376. return err_code;
  377. }
  378. }
  379. else if (m_racp_proc_operator == RACP_OPERATOR_LAST)
  380. {
  381. err_code = ble_gls_db_record_get(total_records - 1, &rec);
  382. if (err_code != NRF_SUCCESS)
  383. {
  384. return err_code;
  385. }
  386. }
  387. err_code = glucose_meas_send(p_gls, &rec);
  388. if (err_code != NRF_SUCCESS)
  389. {
  390. return err_code;
  391. }
  392. }
  393. return NRF_SUCCESS;
  394. }
  395. /**@brief Function for responding to the GREATER_OR_EQUAL operation.
  396. *
  397. * @param[in] p_gls Service instance.
  398. *
  399. * @return NRF_SUCCESS on success, otherwise an error code.
  400. */
  401. static uint32_t racp_report_records_greater_or_equal(ble_gls_t * p_gls)
  402. {
  403. uint16_t total_records = ble_gls_db_num_records_get();
  404. while (m_racp_proc_record_ndx < total_records)
  405. {
  406. uint32_t err_code;
  407. ble_gls_rec_t rec;
  408. err_code = ble_gls_db_record_get(m_racp_proc_record_ndx, &rec);
  409. if (err_code != NRF_SUCCESS)
  410. {
  411. return err_code;
  412. }
  413. if (rec.meas.sequence_number >= m_racp_proc_seq_num)
  414. {
  415. err_code = glucose_meas_send(p_gls, &rec);
  416. if (err_code != NRF_SUCCESS)
  417. {
  418. return err_code;
  419. }
  420. break;
  421. }
  422. m_racp_proc_record_ndx++;
  423. }
  424. if (m_racp_proc_record_ndx == total_records)
  425. {
  426. m_procesing_active = false;
  427. }
  428. return NRF_SUCCESS;
  429. }
  430. /**@brief Function for informing that the REPORT RECORDS procedure is completed.
  431. *
  432. * @param[in] p_gls Service instance.
  433. */
  434. static void racp_report_records_completed(ble_gls_t * p_gls)
  435. {
  436. uint8_t resp_code_value;
  437. if (m_racp_proc_records_reported > 0)
  438. {
  439. resp_code_value = RACP_RESPONSE_SUCCESS;
  440. }
  441. else
  442. {
  443. resp_code_value = RACP_RESPONSE_NO_RECORDS_FOUND;
  444. }
  445. racp_response_code_send(p_gls, RACP_OPCODE_REPORT_RECS, resp_code_value);
  446. }
  447. /**@brief Function for the RACP report records procedure.
  448. *
  449. * @param[in] p_gls Service instance.
  450. */
  451. static void racp_report_records_procedure(ble_gls_t * p_gls)
  452. {
  453. uint32_t err_code;
  454. while (m_procesing_active)
  455. {
  456. // Execute requested procedure
  457. switch (m_racp_proc_operator)
  458. {
  459. case RACP_OPERATOR_ALL:
  460. err_code = racp_report_records_all(p_gls);
  461. break;
  462. case RACP_OPERATOR_FIRST:
  463. case RACP_OPERATOR_LAST:
  464. err_code = racp_report_records_first_last(p_gls);
  465. break;
  466. case RACP_OPERATOR_GREATER_OR_EQUAL:
  467. err_code = racp_report_records_greater_or_equal(p_gls);
  468. break;
  469. default:
  470. // Report error to application
  471. if (p_gls->error_handler != NULL)
  472. {
  473. p_gls->error_handler(NRF_ERROR_INTERNAL);
  474. }
  475. return;
  476. }
  477. // Error handling
  478. switch (err_code)
  479. {
  480. case NRF_SUCCESS:
  481. if (m_procesing_active)
  482. {
  483. m_racp_proc_record_ndx++;
  484. }
  485. else
  486. {
  487. racp_report_records_completed(p_gls);
  488. }
  489. break;
  490. case NRF_ERROR_RESOURCES:
  491. // Wait for TX_COMPLETE event to resume transmission
  492. return;
  493. case NRF_ERROR_INVALID_STATE:
  494. // Notification is probably not enabled. Ignore request.
  495. m_procesing_active = false;
  496. return;
  497. default:
  498. // Report error to application
  499. if (p_gls->error_handler != NULL)
  500. {
  501. p_gls->error_handler(err_code);
  502. }
  503. return;
  504. }
  505. }
  506. }
  507. /**@brief Function for testing if the received request is to be executed.
  508. *
  509. * @param[in] p_racp_request Request to be checked.
  510. * @param[out] p_response_code Response code to be sent in case the request is rejected.
  511. * RACP_RESPONSE_RESERVED is returned if the received message is
  512. * to be rejected without sending a response.
  513. *
  514. * @return TRUE if the request is to be executed, FALSE if it is to be rejected.
  515. * If it is to be rejected, p_response_code will contain the response code to be
  516. * returned to the central.
  517. */
  518. static bool is_request_to_be_executed(ble_racp_value_t const * p_racp_request,
  519. uint8_t * p_response_code)
  520. {
  521. *p_response_code = RACP_RESPONSE_RESERVED;
  522. if (p_racp_request->opcode == RACP_OPCODE_ABORT_OPERATION)
  523. {
  524. if (m_procesing_active)
  525. {
  526. if (p_racp_request->operator != RACP_OPERATOR_NULL)
  527. {
  528. *p_response_code = RACP_RESPONSE_INVALID_OPERATOR;
  529. }
  530. else if (p_racp_request->operand_len != 0)
  531. {
  532. *p_response_code = RACP_RESPONSE_INVALID_OPERAND;
  533. }
  534. else
  535. {
  536. *p_response_code = RACP_RESPONSE_SUCCESS;
  537. }
  538. }
  539. else
  540. {
  541. *p_response_code = RACP_RESPONSE_ABORT_FAILED;
  542. }
  543. }
  544. else if (m_procesing_active)
  545. {
  546. return false;
  547. }
  548. // Supported opcodes.
  549. else if ((p_racp_request->opcode == RACP_OPCODE_REPORT_RECS) ||
  550. (p_racp_request->opcode == RACP_OPCODE_REPORT_NUM_RECS))
  551. {
  552. switch (p_racp_request->operator)
  553. {
  554. // Operators WITHOUT a filter.
  555. case RACP_OPERATOR_ALL:
  556. case RACP_OPERATOR_FIRST:
  557. case RACP_OPERATOR_LAST:
  558. if (p_racp_request->operand_len != 0)
  559. {
  560. *p_response_code = RACP_RESPONSE_INVALID_OPERAND;
  561. }
  562. break;
  563. // Operators WITH a filter.
  564. case RACP_OPERATOR_GREATER_OR_EQUAL:
  565. if (p_racp_request->p_operand[0] == OPERAND_FILTER_TYPE_SEQ_NUM)
  566. {
  567. if (p_racp_request->operand_len != 3)
  568. {
  569. *p_response_code = RACP_RESPONSE_INVALID_OPERAND;
  570. }
  571. }
  572. else if (p_racp_request->p_operand[0] == OPERAND_FILTER_TYPE_FACING_TIME)
  573. {
  574. *p_response_code = RACP_RESPONSE_OPERAND_UNSUPPORTED;
  575. }
  576. else if (p_racp_request->p_operand[0] >= OPERAND_FILTER_TYPE_RFU_START)
  577. {
  578. *p_response_code = RACP_RESPONSE_OPERAND_UNSUPPORTED;
  579. }
  580. else
  581. {
  582. *p_response_code = RACP_RESPONSE_INVALID_OPERAND;
  583. }
  584. break;
  585. // Unsupported operators.
  586. case RACP_OPERATOR_LESS_OR_EQUAL:
  587. case RACP_OPERATOR_RANGE:
  588. *p_response_code = RACP_RESPONSE_OPERATOR_UNSUPPORTED;
  589. break;
  590. // Invalid operators.
  591. case RACP_OPERATOR_NULL:
  592. default:
  593. if (p_racp_request->operator >= RACP_OPERATOR_RFU_START)
  594. {
  595. *p_response_code = RACP_RESPONSE_OPERATOR_UNSUPPORTED;
  596. }
  597. else
  598. {
  599. *p_response_code = RACP_RESPONSE_INVALID_OPERATOR;
  600. }
  601. break;
  602. }
  603. }
  604. // Unsupported opcodes,
  605. else if (p_racp_request->opcode == RACP_OPCODE_DELETE_RECS)
  606. {
  607. *p_response_code = RACP_RESPONSE_OPCODE_UNSUPPORTED;
  608. }
  609. // Unknown opcodes.
  610. else
  611. {
  612. *p_response_code = RACP_RESPONSE_OPCODE_UNSUPPORTED;
  613. }
  614. // NOTE: The computation of the return value will change slightly when deferred write has been
  615. // implemented in the stack.
  616. return (*p_response_code == RACP_RESPONSE_RESERVED);
  617. }
  618. /**@brief Function for processing a REPORT RECORDS request.
  619. *
  620. * @param[in] p_gls Service instance.
  621. * @param[in] p_racp_request Request to be executed.
  622. */
  623. static void report_records_request_execute(ble_gls_t * p_gls, ble_racp_value_t * p_racp_request)
  624. {
  625. uint16_t seq_num = (p_racp_request->p_operand[2] << 8) | p_racp_request->p_operand[1];
  626. m_procesing_active = true;
  627. m_racp_proc_record_ndx = 0;
  628. m_racp_proc_operator = p_racp_request->operator;
  629. m_racp_proc_records_reported = 0;
  630. m_racp_proc_seq_num = seq_num;
  631. racp_report_records_procedure(p_gls);
  632. }
  633. /**@brief Function for processing a REPORT NUM RECORDS request.
  634. *
  635. * @param[in] p_gls Service instance.
  636. * @param[in] p_racp_request Request to be executed.
  637. */
  638. static void report_num_records_request_execute(ble_gls_t * p_gls, ble_racp_value_t * p_racp_request)
  639. {
  640. uint16_t total_records;
  641. uint16_t num_records;
  642. total_records = ble_gls_db_num_records_get();
  643. num_records = 0;
  644. if (p_racp_request->operator == RACP_OPERATOR_ALL)
  645. {
  646. num_records = total_records;
  647. }
  648. else if (p_racp_request->operator == RACP_OPERATOR_GREATER_OR_EQUAL)
  649. {
  650. uint16_t seq_num;
  651. uint16_t i;
  652. seq_num = (p_racp_request->p_operand[2] << 8) | p_racp_request->p_operand[1];
  653. for (i = 0; i < total_records; i++)
  654. {
  655. uint32_t err_code;
  656. ble_gls_rec_t rec;
  657. err_code = ble_gls_db_record_get(i, &rec);
  658. if (err_code != NRF_SUCCESS)
  659. {
  660. if (p_gls->error_handler != NULL)
  661. {
  662. p_gls->error_handler(err_code);
  663. }
  664. return;
  665. }
  666. if (rec.meas.sequence_number >= seq_num)
  667. {
  668. num_records++;
  669. }
  670. }
  671. }
  672. else if ((p_racp_request->operator == RACP_OPERATOR_FIRST) ||
  673. (p_racp_request->operator == RACP_OPERATOR_LAST))
  674. {
  675. if (total_records > 0)
  676. {
  677. num_records = 1;
  678. }
  679. }
  680. ble_racp_value_t racp_response;
  681. uint8_t pending_racp_response_operand[RACP_OPERAND_LEN] = {0};
  682. memset(&racp_response, 0, sizeof(racp_response));
  683. racp_response.opcode = RACP_OPCODE_NUM_RECS_RESPONSE;
  684. racp_response.operator = RACP_OPERATOR_NULL;
  685. racp_response.operand_len = sizeof(uint16_t);
  686. racp_response.p_operand = pending_racp_response_operand;
  687. pending_racp_response_operand[0] = num_records & 0xFF;
  688. pending_racp_response_operand[1] = num_records >> 8;
  689. racp_send(p_gls, &racp_response);
  690. }
  691. /**@brief Function for checking if the CCCDs are configured.
  692. *
  693. * @param[in] p_gls Service instance.
  694. * @param[in] p_are_cccd_configured boolean indicating if both cccds are configured
  695. */
  696. uint32_t ble_gls_are_cccd_configured(ble_gls_t * p_gls, bool * p_are_cccd_configured)
  697. {
  698. uint32_t err_code;
  699. uint8_t cccd_value_buf[BLE_CCCD_VALUE_LEN];
  700. bool is_glm_notif_enabled = false;
  701. bool is_racp_indic_enabled = false;
  702. ble_gatts_value_t gatts_value;
  703. // Initialize value struct.
  704. memset(&gatts_value, 0, sizeof(gatts_value));
  705. gatts_value.len = BLE_CCCD_VALUE_LEN;
  706. gatts_value.offset = 0;
  707. gatts_value.p_value = cccd_value_buf;
  708. err_code = sd_ble_gatts_value_get(p_gls->conn_handle,
  709. p_gls->glm_handles.cccd_handle,
  710. &gatts_value);
  711. if (err_code != NRF_SUCCESS)
  712. {
  713. return err_code;
  714. }
  715. is_glm_notif_enabled = ble_srv_is_notification_enabled(cccd_value_buf);
  716. err_code = sd_ble_gatts_value_get(p_gls->conn_handle,
  717. p_gls->racp_handles.cccd_handle,
  718. &gatts_value);
  719. if (err_code != NRF_SUCCESS)
  720. {
  721. return err_code;
  722. }
  723. is_racp_indic_enabled = ble_srv_is_indication_enabled(cccd_value_buf);
  724. if (is_racp_indic_enabled & is_glm_notif_enabled)
  725. {
  726. *p_are_cccd_configured = true;
  727. }
  728. else
  729. {
  730. *p_are_cccd_configured = false;
  731. }
  732. return NRF_SUCCESS;
  733. }
  734. /**@brief Function for handling a write event to the Record Access Control Point.
  735. *
  736. * @param[in] p_gls Service instance.
  737. * @param[in] p_evt_write WRITE event to be handled.
  738. */
  739. static void on_racp_value_write(ble_gls_t * p_gls, ble_gatts_evt_write_t const * p_evt_write)
  740. {
  741. ble_racp_value_t racp_request;
  742. uint8_t response_code;
  743. ble_gatts_rw_authorize_reply_params_t auth_reply;
  744. bool are_cccd_configured;
  745. uint32_t err_code;
  746. auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  747. auth_reply.params.write.offset = 0;
  748. auth_reply.params.write.len = 0;
  749. auth_reply.params.write.p_data = NULL;
  750. err_code = ble_gls_are_cccd_configured(p_gls, &are_cccd_configured);
  751. if (err_code != NRF_SUCCESS)
  752. {
  753. if (p_gls->error_handler != NULL)
  754. {
  755. p_gls->error_handler(err_code);
  756. }
  757. return;
  758. }
  759. if (!are_cccd_configured)
  760. {
  761. auth_reply.params.write.gatt_status = GLS_NACK_CCCD_IMPROPERLY_CONFIGURED;
  762. err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle,
  763. &auth_reply);
  764. if (err_code != NRF_SUCCESS)
  765. {
  766. if (p_gls->error_handler != NULL)
  767. {
  768. p_gls->error_handler(err_code);
  769. }
  770. }
  771. return;
  772. }
  773. // Decode request.
  774. ble_racp_decode(p_evt_write->len, (uint8_t *)p_evt_write->data, &racp_request);
  775. // Check if request is to be executed.
  776. if (is_request_to_be_executed(&racp_request, &response_code))
  777. {
  778. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  779. auth_reply.params.write.update = 1;
  780. err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle,
  781. &auth_reply);
  782. if (err_code != NRF_SUCCESS)
  783. {
  784. if (p_gls->error_handler != NULL)
  785. {
  786. p_gls->error_handler(err_code);
  787. }
  788. return;
  789. }
  790. // Execute request.
  791. if (racp_request.opcode == RACP_OPCODE_REPORT_RECS)
  792. {
  793. report_records_request_execute(p_gls, &racp_request);
  794. }
  795. else if (racp_request.opcode == RACP_OPCODE_REPORT_NUM_RECS)
  796. {
  797. report_num_records_request_execute(p_gls, &racp_request);
  798. }
  799. }
  800. else if (response_code != RACP_RESPONSE_RESERVED)
  801. {
  802. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  803. auth_reply.params.write.update = 1;
  804. err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle,
  805. &auth_reply);
  806. if (err_code != NRF_SUCCESS)
  807. {
  808. if (p_gls->error_handler != NULL)
  809. {
  810. p_gls->error_handler(err_code);
  811. }
  812. return;
  813. }
  814. // Abort any running procedure.
  815. m_procesing_active = false;
  816. // Respond with error code.
  817. racp_response_code_send(p_gls, racp_request.opcode, response_code);
  818. }
  819. else
  820. {
  821. auth_reply.params.write.gatt_status = GLS_NACK_PROC_ALREADY_IN_PROGRESS;
  822. err_code = sd_ble_gatts_rw_authorize_reply(p_gls->conn_handle,
  823. &auth_reply);
  824. if (err_code != NRF_SUCCESS)
  825. {
  826. if (p_gls->error_handler != NULL)
  827. {
  828. p_gls->error_handler(err_code);
  829. }
  830. return;
  831. }
  832. }
  833. }
  834. /**@brief Function for handling the Glucose measurement CCCD write event.
  835. *
  836. * @param[in] p_gls Service instance.
  837. * @param[in] p_evt_write WRITE event to be handled.
  838. */
  839. static void on_glm_cccd_write(ble_gls_t * p_gls, ble_gatts_evt_write_t const * p_evt_write)
  840. {
  841. if (p_evt_write->len == 2)
  842. {
  843. // CCCD written, update notification state
  844. ble_gls_evt_t evt;
  845. if (ble_srv_is_notification_enabled(p_evt_write->data))
  846. {
  847. evt.evt_type = BLE_GLS_EVT_NOTIFICATION_ENABLED;
  848. }
  849. else
  850. {
  851. evt.evt_type = BLE_GLS_EVT_NOTIFICATION_DISABLED;
  852. }
  853. if (p_gls->evt_handler != NULL)
  854. {
  855. p_gls->evt_handler(p_gls, &evt);
  856. }
  857. }
  858. }
  859. /**@brief Function for handling the WRITE event.
  860. *
  861. * @details Handles WRITE events from the BLE stack.
  862. *
  863. * @param[in] p_gls Glucose Service structure.
  864. * @param[in] p_ble_evt Event received from the BLE stack.
  865. */
  866. static void on_write(ble_gls_t * p_gls, ble_evt_t const * p_ble_evt)
  867. {
  868. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  869. if (p_evt_write->handle == p_gls->glm_handles.cccd_handle)
  870. {
  871. on_glm_cccd_write(p_gls, p_evt_write);
  872. }
  873. else if (p_evt_write->handle == p_gls->racp_handles.value_handle)
  874. {
  875. on_racp_value_write(p_gls, p_evt_write);
  876. }
  877. }
  878. /**@brief Function for handling the TX_COMPLETE event.
  879. *
  880. * @details Handles TX_COMPLETE events from the BLE stack.
  881. *
  882. * @param[in] p_gls Glucose Service structure.
  883. * @param[in] p_ble_evt Event received from the BLE stack.
  884. */
  885. static void on_tx_complete(ble_gls_t * p_gls, ble_evt_t const * p_ble_evt)
  886. {
  887. if (m_procesing_active)
  888. {
  889. racp_report_records_procedure(p_gls);
  890. }
  891. }
  892. static void on_rw_authorize_request(ble_gls_t * p_gls, ble_gatts_evt_t const * p_gatts_evt)
  893. {
  894. ble_gatts_evt_rw_authorize_request_t const * p_auth_req =
  895. &p_gatts_evt->params.authorize_request;
  896. if (p_auth_req->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
  897. {
  898. if ( (p_gatts_evt->params.authorize_request.request.write.op
  899. != BLE_GATTS_OP_PREP_WRITE_REQ)
  900. && (p_gatts_evt->params.authorize_request.request.write.op
  901. != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
  902. && (p_gatts_evt->params.authorize_request.request.write.op
  903. != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
  904. )
  905. {
  906. if (p_auth_req->request.write.handle == p_gls->racp_handles.value_handle)
  907. {
  908. on_racp_value_write(p_gls, &p_auth_req->request.write);
  909. }
  910. }
  911. }
  912. }
  913. void ble_gls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  914. {
  915. ble_gls_t * p_gls = (ble_gls_t *)p_context;
  916. ret_code_t err_code;
  917. switch (p_ble_evt->header.evt_id)
  918. {
  919. case BLE_GAP_EVT_CONNECTED:
  920. p_gls->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  921. err_code = nrf_ble_gq_conn_handle_register(p_gls->p_gatt_queue,
  922. p_ble_evt->evt.gap_evt.conn_handle);
  923. if ((err_code != NRF_SUCCESS) && (p_gls->error_handler != NULL))
  924. {
  925. p_gls->error_handler(err_code);
  926. }
  927. break;
  928. case BLE_GAP_EVT_DISCONNECTED:
  929. p_gls->conn_handle = BLE_CONN_HANDLE_INVALID;
  930. break;
  931. case BLE_GATTS_EVT_WRITE:
  932. on_write(p_gls, p_ble_evt);
  933. break;
  934. case BLE_GATTS_EVT_HVN_TX_COMPLETE:
  935. on_tx_complete(p_gls, p_ble_evt);
  936. break;
  937. case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
  938. on_rw_authorize_request(p_gls, &p_ble_evt->evt.gatts_evt);
  939. break;
  940. default:
  941. // No implementation needed.
  942. break;
  943. }
  944. }
  945. uint32_t ble_gls_glucose_new_meas(ble_gls_t * p_gls, ble_gls_rec_t * p_rec)
  946. {
  947. p_rec->meas.sequence_number = m_next_seq_num++;
  948. return ble_gls_db_record_add(p_rec);
  949. }
  950. #endif // NRF_MODULE_ENABLED(BLE_GLS)