ble_ancs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #ifndef __BLE_ANCS_HD_
  29. #define __BLE_ANCS_HD_
  30. #include "stdint.h"
  31. #define ANCS_ATTR_DATA_MAX 32
  32. #define ANCS_NB_OF_CATEGORY_ID 12
  33. #define ANCS_NB_OF_NOTIF_ATTR 8
  34. #define ANCS_NB_OF_APP_ATTR 1
  35. #define ANCS_NB_OF_EVT_ID 3
  36. #define ANCS_APP_ATTR_TX_SIZE (32+16)
  37. // ANCS: 7905F431-B5CE-4E99-A40F-4B1E122D00D0
  38. #define ANCSAPP_ANCS_SVC_UUID 0xD0, 0x00, 0x2D, 0x12, 0x1E, 0x4B, 0x0F, 0xA4, 0x99, 0x4E, 0xCE, 0xB5, 0x31, 0xF4, 0x05, 0x79
  39. // Notification Source: UUID 9FBF120D-6301-42D9-8C58-25E699A21DBD (notifiable)
  40. #define ANCSAPP_NOTIF_SRC_CHAR_UUID 0x1DBD
  41. // Control point: UUID 69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9 (writable with response)
  42. #define ANCSAPP_CTRL_PT_CHAR_UUID 0xD9D9
  43. // Data Source: UUID 22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB (notifiable)
  44. #define ANCSAPP_DATA_SRC_CHAR_UUID 0x7BFB
  45. #define CHAR_DESC_HDL_UUID128_LEN 21 // (5 + 16) bytes = 21 bytes.
  46. #define NUMBER_OF_ANCS_CHARS 3
  47. #define ANCS_NOTIF_UID_LENGTH 4
  48. typedef enum
  49. {
  50. BLE_ANCS_CATEGORY_ID_OTHER, /**< The iOS notification belongs to the "other" category. */
  51. BLE_ANCS_CATEGORY_ID_INCOMING_CALL, /**< The iOS notification belongs to the "Incoming Call" category. */
  52. BLE_ANCS_CATEGORY_ID_MISSED_CALL, /**< The iOS notification belongs to the "Missed Call" category. */
  53. BLE_ANCS_CATEGORY_ID_VOICE_MAIL, /**< The iOS notification belongs to the "Voice Mail" category. */
  54. BLE_ANCS_CATEGORY_ID_SOCIAL, /**< The iOS notification belongs to the "Social" category. */
  55. BLE_ANCS_CATEGORY_ID_SCHEDULE, /**< The iOS notification belongs to the "Schedule" category. */
  56. BLE_ANCS_CATEGORY_ID_EMAIL, /**< The iOS notification belongs to the "E-mail" category. */
  57. BLE_ANCS_CATEGORY_ID_NEWS, /**< The iOS notification belongs to the "News" category. */
  58. BLE_ANCS_CATEGORY_ID_HEALTH_AND_FITNESS, /**< The iOS notification belongs to the "Health and Fitness" category. */
  59. BLE_ANCS_CATEGORY_ID_BUSINESS_AND_FINANCE, /**< The iOS notification belongs to the "Buisness and Finance" category. */
  60. BLE_ANCS_CATEGORY_ID_LOCATION, /**< The iOS notification belongs to the "Location" category. */
  61. BLE_ANCS_CATEGORY_ID_ENTERTAINMENT /**< The iOS notification belongs to the "Entertainment" category. */
  62. }ancs_notif_category_id;
  63. /**@brief Event IDs for iOS notifications. */
  64. typedef enum
  65. {
  66. BLE_ANCS_EVENT_ID_NOTIFICATION_ADDED, /**< The iOS notification was added. */
  67. BLE_ANCS_EVENT_ID_NOTIFICATION_MODIFIED, /**< The iOS notification was modified. */
  68. BLE_ANCS_EVENT_ID_NOTIFICATION_REMOVED /**< The iOS notification was removed. */
  69. } ancs_notif_evt_id;
  70. #define EVENT_FLAG_silent (1<<0) //!< If this flag is set, the notification has a low priority.
  71. #define EVENT_FLAG_important (1<<1) //!< If this flag is set, the notification has a high priority.
  72. #define EVENT_FLAG_pre_existing (1<<2) //!< If this flag is set, the notification is pre-existing.
  73. #define EVENT_FLAG_positive_action (1<<3) //!< If this flag is set, the notification has a positive action that can be taken.
  74. #define EVENT_FLAG_negative_action (1<<4) //!< If this flag is set, the notification has a negative action that can be taken.
  75. #define BLE_ANCS_APP_ATTR_ID_DISPLAY_NAME 0
  76. typedef enum
  77. {
  78. BLE_ANCS_NOTIF_ATTR_ID_APP_IDENTIFIER = 0, /**< Identifies that the attribute data is of an "App Identifier" type. */
  79. BLE_ANCS_NOTIF_ATTR_ID_TITLE, /**< Identifies that the attribute data is a "Title". */
  80. BLE_ANCS_NOTIF_ATTR_ID_SUBTITLE, /**< Identifies that the attribute data is a "Subtitle". */
  81. BLE_ANCS_NOTIF_ATTR_ID_MESSAGE, /**< Identifies that the attribute data is a "Message". */
  82. BLE_ANCS_NOTIF_ATTR_ID_MESSAGE_SIZE, /**< Identifies that the attribute data is a "Message Size". */
  83. BLE_ANCS_NOTIF_ATTR_ID_DATE, /**< Identifies that the attribute data is a "Date". */
  84. BLE_ANCS_NOTIF_ATTR_ID_POSITIVE_ACTION_LABEL, /**< The notification has a "Positive action" that can be executed associated with it. */
  85. BLE_ANCS_NOTIF_ATTR_ID_NEGATIVE_ACTION_LABEL, /**< The notification has a "Negative action" that can be executed associated with it. */
  86. } ancs_notif_attr_id;
  87. typedef enum
  88. {
  89. BLE_ANCS_EVT_DISCOVERY_COMPLETE,
  90. BLE_ANCS_EVT_DISCOVERY_FAILED,
  91. BLE_ANCS_EVT_NOTIF,
  92. BLE_ANCS_EVT_NOTIF_ATTRIBUTE,
  93. BLE_ANCS_EVT_APP_ATTRIBUTE,
  94. BLE_ANCS_EVT_NP_ERROR,
  95. } ancs_evt_type_t;
  96. typedef struct {
  97. bool en; //enable flag
  98. uint32_t attr_id;
  99. uint16_t attr_len;
  100. uint8_t *p_attr_data;
  101. }ancs_attr_list_t;
  102. enum
  103. {
  104. ANCS_NOTIF_SCR_HDL_START, // ANCS Notification Source characteristic start handle.
  105. ANCS_NOTIF_SCR_HDL_END, // ANCS Notification Source characteristic end handle.
  106. ANCS_NOTIF_SCR_HDL_CCCD, // ANCS Notification Source CCCD handle.
  107. ANCS_CTRL_POINT_HDL_START, // ANCS Control Point characteristic start handle.
  108. ANCS_CTRL_POINT_HDL_END, // ANCS Control Point characteristic end handle.
  109. ANCS_DATA_SRC_HDL_START, // ANCS Data Source characteristic start handle.
  110. ANCS_DATA_SRC_HDL_END, // ANCS Data Source characteristic end handle.
  111. ANCS_DATA_SRC_HDL_CCCD, // ANCS Data Source CCCD handle.
  112. };
  113. // Cache array length.
  114. #define HDL_CACHE_LEN 8
  115. enum
  116. {
  117. ANCS_UNINIT, // uninitial idle state.
  118. ANCS_DISC_SERVICE, // Discover the ANCS service by UUID.
  119. ANCS_STORE_SERVICE_HANDLES, // Store the ANCS service handles in the cache.
  120. ANCS_DISC_CHARS, // Discover the three characteristics: Notification Source, Control Point, and Data Source.
  121. ANCS_STORE_CHARS_HANDLES, // Store the handles of each characteristic in the handle cache.
  122. ANCS_DISC_NS_DESCS, // Discover the descriptors of the Notification Source (Trying to locate the CCCD).
  123. ANCS_STORE_NS_DESCS_HANDLES, // Store the descriptor's handles in the handle cache (ANCS_NOTIF_SCR_HDL_CCCD).
  124. ANCS_DISC_DS_DESCS, // Discover the descriptors of the Data Source (Trying to locate the CCCD).
  125. ANCS_STORE_DS_DESCS_HANDLES, // Store the descriptor's handles in the handle cache (ANCS_DATA_SRC_HDL_CCCD).
  126. ANCS_ENABLE_NS_CCCD,
  127. ANCS_ENABLE_DS_CCCD,
  128. ANCS_WAIT_CCCD_READY,
  129. ANCS_DISC_FINISH, // Final state signifying the end of the discovery process.
  130. ANCS_DISC_FAILED = 0xFF // A failure state reached only if an error occurs.
  131. };
  132. enum
  133. {
  134. ANCS_STATE_IDLE = 0,
  135. ANCS_STATE_DISCOVERY,
  136. ANCS_STATE_READY,
  137. };
  138. typedef struct{
  139. uint16_t conn_hdl;
  140. uint8_t expect_type_value_num;
  141. uint16_t service_hdl[2];
  142. uint8_t chars_disc_num;
  143. uint16_t chars_hdl[HDL_CACHE_LEN];
  144. }ancs_service_t;
  145. typedef enum
  146. {
  147. COMMAND_ID, /**< Parsing the command ID. */
  148. NOTIF_UID, /**< Parsing the notification UID. */
  149. APP_ID, /**< Parsing app ID. */
  150. ATTR_ID, /**< Parsing attribute ID. */
  151. ATTR_LEN1, /**< Parsing the LSB of the attribute length. */
  152. ATTR_LEN2, /**< Parsing the MSB of the attribute length. */
  153. ATTR_DATA, /**< Parsing the attribute data. */
  154. ATTR_SKIP, /**< Parsing is skipped for the rest (or entire) of an attribute. */
  155. DONE, /**< Parsing for one attribute is done. */
  156. } ancs_parse_state_t;
  157. typedef enum
  158. {
  159. ANCS_COMMAND_ID_GET_NOTIF_ATTRIBUTES, /**< Requests attributes to be sent from the NP to the NC for a given notification. */
  160. ANCS_COMMAND_ID_GET_APP_ATTRIBUTES, /**< Requests attributes to be sent from the NP to the NC for a given iOS app. */
  161. ANCS_COMMAND_ID_GET_PERFORM_NOTIF_ACTION, /**< Requests an action to be performed on a given notification, for example, dismiss an alarm. */
  162. } ancs_cmd_id_val_t;
  163. typedef struct
  164. {
  165. ancs_attr_list_t * p_attr_list; //!< The current list of attributes being parsed. This field will point to either @ref ble_ancs_c_t::ancs_notif_attr_list or @ref ble_ancs_c_t::ancs_app_attr_list.
  166. uint32_t nb_of_attr; //!< Number of possible attributes. When parsing begins, it is set to either @ref ANCS_NB_OF_NOTIF_ATTR or @ref ANCS_NB_OF_APP_ATTR.
  167. uint32_t expected_number_of_attrs; //!< The number of attributes expected upon receiving attributes. Keeps track of when to stop reading incoming attributes.
  168. ancs_parse_state_t parse_state; //!< ANCS notification attribute parsing state.
  169. ancs_cmd_id_val_t command_id; //!< Variable to keep track of what command type we are currently parsing ( @ref BLE_ANCS_COMMAND_ID_GET_NOTIF_ATTRIBUTES or @ref BLE_ANCS_COMMAND_ID_GET_APP_ATTRIBUTES.
  170. uint8_t * p_data_dest; //!< Attribute that the parsed data will be copied into.
  171. uint16_t current_attr_index; //!< Variable to keep track of how much (for a given attribute) we are done parsing.
  172. uint32_t current_app_id_index; //!< Variable to keep track of how much (for a given app identifier) we are done parsing.
  173. } ble_ancs_parse_fsm_t;
  174. typedef struct{
  175. uint32_t notif_uid;
  176. uint8_t app_id[ANCS_ATTR_DATA_MAX];
  177. uint16_t attr_len; //!< Length of the received attribute data.
  178. uint32_t attr_id; //!< Classification of the attribute type, for example, title or date.
  179. uint8_t *p_attr_data; //!< Pointer to where the memory is allocated for storing incoming attributes.
  180. }ancs_attr_evt_t;
  181. typedef struct{
  182. uint8_t type;
  183. uint8_t len;
  184. void* msg;
  185. }ancs_evt_t;
  186. typedef void (*ancs_evt_hdl_t) (ancs_evt_t * p_evt);
  187. typedef struct{
  188. uint8_t app_task_ID;
  189. uint8_t disc_state;
  190. uint8_t app_state;
  191. ancs_attr_list_t notif_attr_list[ANCS_NB_OF_NOTIF_ATTR];
  192. ancs_attr_list_t app_attr_list[ANCS_NB_OF_APP_ATTR];
  193. ble_ancs_parse_fsm_t parse_info;
  194. ancs_service_t ancs_service;
  195. ancs_attr_evt_t attr_evt_msg;
  196. ancs_evt_t attr_rsp_evt;
  197. uint8_t app_attr_tx_buf[ANCS_APP_ATTR_TX_SIZE];
  198. ancs_evt_hdl_t callback;
  199. }ancs_ctx_t;
  200. typedef struct{
  201. uint8_t eventID;
  202. // Store the ANCS notification's eventFlag
  203. uint8_t eventFlag;
  204. // Store the ANCS notification's categoryID
  205. uint8_t categoryID;
  206. uint8_t categoryCount;
  207. uint8_t notifUID[4];
  208. }ancs_notify_evt_t;
  209. bStatus_t ble_ancs_attr_add(const ancs_notif_attr_id id, uint8_t * p_data, const uint16_t len);
  210. bStatus_t ble_ancs_get_notif_attrs(const uint8_t * pNotificationUID);
  211. bStatus_t ble_ancs_get_app_attrs(const uint8_t * p_app_id, uint8_t app_id_len);
  212. bStatus_t ble_ancs_start_descovery(uint16_t conn_handle);
  213. bStatus_t ble_ancs_handle_gatt_event(gattMsgEvent_t* pMsg);
  214. bStatus_t ble_ancs_init(ancs_evt_hdl_t evt_hdl, uint8_t task_ID);
  215. #endif //__BLE_ANCS_HD_