aics.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (c) 2020 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_AICS_H_
  7. #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_AICS_H_
  8. /**
  9. * @brief Audio Input Control Service (AICS)
  10. *
  11. * @defgroup bt_gatt_aics Audio Input Control Service (AICS)
  12. *
  13. * @ingroup bluetooth
  14. * @{
  15. *
  16. * The Audio Input Control Service is a secondary service, and as such should not be used on its
  17. * own, but rather in the context of another (primary) service.
  18. *
  19. * This API implements both the server and client functionality.
  20. * Note that the API abstracts away the change counter in the audio input control state and will
  21. * automatically handle any changes to that. If out of date, the client implementation will
  22. * autonomously read the change counter value when executing a write request.
  23. *
  24. * [Experimental] Users should note that the APIs can change as a part of ongoing development.
  25. */
  26. #include <zephyr/types.h>
  27. #include <bluetooth/bluetooth.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /** Audio Input Control Service mute states */
  32. #define BT_AICS_STATE_UNMUTED 0x00
  33. #define BT_AICS_STATE_MUTED 0x01
  34. #define BT_AICS_STATE_MUTE_DISABLED 0x02
  35. /** Audio Input Control Service input modes */
  36. #define BT_AICS_MODE_MANUAL_ONLY 0x00
  37. #define BT_AICS_MODE_AUTO_ONLY 0x01
  38. #define BT_AICS_MODE_MANUAL 0x02
  39. #define BT_AICS_MODE_AUTO 0x03
  40. /** Audio Input Control Service input types */
  41. #define BT_AICS_INPUT_TYPE_UNSPECIFIED 0x00
  42. #define BT_AICS_INPUT_TYPE_BLUETOOTH 0x01
  43. #define BT_AICS_INPUT_TYPE_MICROPHONE 0x02
  44. #define BT_AICS_INPUT_TYPE_ANALOG 0x03
  45. #define BT_AICS_INPUT_TYPE_DIGITAL 0x04
  46. #define BT_AICS_INPUT_TYPE_RADIO 0x05
  47. #define BT_AICS_INPUT_TYPE_STREAMING 0x06
  48. /** Audio Input Control Service Error codes */
  49. #define BT_AICS_ERR_INVALID_COUNTER 0x80
  50. #define BT_AICS_ERR_OP_NOT_SUPPORTED 0x81
  51. #define BT_AICS_ERR_MUTE_DISABLED 0x82
  52. #define BT_AICS_ERR_OUT_OF_RANGE 0x83
  53. #define BT_AICS_ERR_GAIN_MODE_NOT_ALLOWED 0x84
  54. /** @brief Opaque Audio Input Control Service instance. */
  55. struct bt_aics;
  56. /** @brief Structure for initializing a Audio Input Control Service instance. */
  57. struct bt_aics_register_param {
  58. /** Initial audio input gain (-128 to 127) */
  59. int8_t gain;
  60. /** Initial audio input mute state */
  61. uint8_t mute;
  62. /** Initial audio input mode */
  63. uint8_t gain_mode;
  64. /** Initial audio input gain units (N * 0.1 dB) */
  65. uint8_t units;
  66. /** Initial audio input minimum gain */
  67. int8_t min_gain;
  68. /** Initial audio input maximum gain */
  69. int8_t max_gain;
  70. /** Initial audio input type */
  71. uint8_t type;
  72. /** Initial audio input status (active/inactive) */
  73. bool status;
  74. /** Boolean to set whether the description is writable by clients */
  75. bool desc_writable;
  76. /** Initial audio input description */
  77. char *description;
  78. /** Pointer to the callback structure. */
  79. struct bt_aics_cb *cb;
  80. };
  81. /** @brief Structure for discovering a Audio Input Control Service instance. */
  82. struct bt_aics_discover_param {
  83. /** @brief The start handle of the discovering.
  84. *
  85. * Typically the @p start_handle of a @ref bt_gatt_include.
  86. */
  87. uint16_t start_handle;
  88. /** @brief The end handle of the discovering.
  89. *
  90. * Typically the @p end_handle of a @ref bt_gatt_include.
  91. */
  92. uint16_t end_handle;
  93. };
  94. /**
  95. * @brief Get a free instance of Audio Input Control Service from the pool.
  96. *
  97. * @return Audio Input Control Service instance in case of success or NULL in case of error.
  98. */
  99. struct bt_aics *bt_aics_free_instance_get(void);
  100. /**
  101. * @brief Get the service declaration attribute.
  102. *
  103. * The first service attribute returned can be included in any other GATT service.
  104. *
  105. * @param aics Audio Input Control Service instance.
  106. *
  107. * @return Pointer to the attributes of the service.
  108. */
  109. void *bt_aics_svc_decl_get(struct bt_aics *aics);
  110. /**
  111. * @brief Get the connection pointer of a client instance
  112. *
  113. * Get the Bluetooth connection pointer of a Audio Input Control Service
  114. * client instance.
  115. *
  116. * @param aics Audio Input Control Service client instance pointer.
  117. * @param conn Connection pointer.
  118. *
  119. * @return 0 if success, errno on failure.
  120. */
  121. int bt_aics_client_conn_get(const struct bt_aics *aics, struct bt_conn **conn);
  122. /**
  123. * @brief Initialize the Audio Input Control Service instance.
  124. *
  125. * @param aics Audio Input Control Service instance.
  126. * @param param Audio Input Control Service register parameters.
  127. *
  128. * @return 0 if success, errno on failure.
  129. */
  130. int bt_aics_register(struct bt_aics *aics, struct bt_aics_register_param *param);
  131. /**
  132. * @brief Callback function for writes.
  133. *
  134. * @param inst The instance pointer.
  135. * @param err Error value. 0 on success, GATT error on positive value
  136. * or errno on negative value.
  137. */
  138. typedef void (*bt_aics_write_cb)(struct bt_aics *inst, int err);
  139. /**
  140. * @brief Callback function for the input state.
  141. *
  142. * Called when the value is read,
  143. * or if the value is changed by either the server or client.
  144. *
  145. * @param inst The instance pointer.
  146. * @param err Error value. 0 on success, GATT error on positive value
  147. * or errno on negative value.
  148. * For notifications, this will always be 0.
  149. * @param gain The gain setting value.
  150. * @param mute The mute value.
  151. * @param mode The mode value.
  152. */
  153. typedef void (*bt_aics_state_cb)(struct bt_aics *inst, int err, int8_t gain,
  154. uint8_t mute, uint8_t mode);
  155. /**
  156. * @brief Callback function for the gain settings.
  157. *
  158. * Called when the value is read,
  159. * or if the value is changed by either the server or client.
  160. *
  161. * @param inst The instance pointer.
  162. * @param err Error value. 0 on success, GATT error on positive value
  163. * or errno on negative value.
  164. * For notifications, this will always be 0.
  165. * @param units The value that reflect the size of a single increment or decrement of the
  166. * Gain Setting value in 0.1 decibel units.
  167. * @param minimum The minimum gain allowed for the gain setting.
  168. * @param maximum The maximum gain allowed for the gain setting.
  169. */
  170. typedef void (*bt_aics_gain_setting_cb)(struct bt_aics *inst, int err,
  171. uint8_t units, int8_t minimum,
  172. int8_t maximum);
  173. /**
  174. * @brief Callback function for the input type.
  175. *
  176. * Called when the value is read, or if the value is changed by either the server or client.
  177. *
  178. * @param inst The instance pointer.
  179. * @param err Error value. 0 on success, GATT error on positive value
  180. * or errno on negative value.
  181. * For notifications, this will always be 0.
  182. * @param type The input type.
  183. */
  184. typedef void (*bt_aics_type_cb)(struct bt_aics *inst, int err, uint8_t type);
  185. /**
  186. * @brief Callback function for the input status.
  187. *
  188. * Called when the value is read, or if the value is changed by either the server or client.
  189. *
  190. * @param inst The instance pointer.
  191. * @param err Error value. 0 on success, GATT error on positive value
  192. * or errno on negative value.
  193. * For notifications, this will always be 0.
  194. * @param active Whether the instance is active or inactive.
  195. */
  196. typedef void (*bt_aics_status_cb)(struct bt_aics *inst, int err, bool active);
  197. /**
  198. * @brief Callback function for the description.
  199. *
  200. * Called when the value is read, or if the value is changed by either the server or client.
  201. *
  202. * @param inst The instance pointer.
  203. * @param err Error value. 0 on success, GATT error on positive value
  204. * or errno on negative value.
  205. * For notifications, this will always be 0.
  206. * @param description The description as an UTF-8 encoded string (may have been clipped).
  207. */
  208. typedef void (*bt_aics_description_cb)(struct bt_aics *inst, int err,
  209. char *description);
  210. /**
  211. * @brief Callback function for bt_aics_discover.
  212. *
  213. * This callback will usually be overwritten by the primary service that
  214. * includes the Audio Input Control Service client.
  215. *
  216. * @param inst The instance pointer.
  217. * @param err Error value. 0 on success, GATT error on positive value
  218. * or errno on negative value.
  219. * For notifications, this will always be 0.
  220. */
  221. typedef void (*bt_aics_discover_cb)(struct bt_aics *inst, int err);
  222. struct bt_aics_cb {
  223. bt_aics_state_cb state;
  224. bt_aics_gain_setting_cb gain_setting;
  225. bt_aics_type_cb type;
  226. bt_aics_status_cb status;
  227. bt_aics_description_cb description;
  228. #if defined(CONFIG_BT_AICS_CLIENT)
  229. bt_aics_discover_cb discover;
  230. bt_aics_write_cb set_gain;
  231. bt_aics_write_cb unmute;
  232. bt_aics_write_cb mute;
  233. bt_aics_write_cb set_manual_mode;
  234. bt_aics_write_cb set_auto_mode;
  235. #endif /* CONFIG_BT_AICS_CLIENT */
  236. };
  237. /**
  238. * @brief Discover a Audio Input Control Service.
  239. *
  240. * Attempts to discover a Audio Input Control Service on a server given the
  241. * @p param.
  242. *
  243. * @param conn Connection to the peer with the Audio Input Control Service.
  244. * @param inst The instance pointer.
  245. * @param param Pointer to the parameters.
  246. *
  247. * @return 0 on success, errno on fail.
  248. */
  249. int bt_aics_discover(struct bt_conn *conn, struct bt_aics *inst,
  250. const struct bt_aics_discover_param *param);
  251. /**
  252. * @brief Deactivates a Audio Input Control Service instance.
  253. *
  254. * Audio Input Control Services are activated by default, but this will allow
  255. * the server to deactivate an Audio Input Control Service.
  256. *
  257. * @param inst The instance pointer.
  258. *
  259. * @return 0 if success, errno on failure.
  260. */
  261. int bt_aics_deactivate(struct bt_aics *inst);
  262. /**
  263. * @brief Activates a Audio Input Control Service instance.
  264. *
  265. * Audio Input Control Services are activated by default, but this will allow
  266. * the server reactivate a Audio Input Control Service instance after it has
  267. * been deactivated with @ref bt_aics_deactivate.
  268. *
  269. * @param inst The instance pointer.
  270. *
  271. * @return 0 if success, errno on failure.
  272. */
  273. int bt_aics_activate(struct bt_aics *inst);
  274. /**
  275. * @brief Read the Audio Input Control Service input state.
  276. *
  277. * @param inst The instance pointer.
  278. *
  279. * @return 0 on success, GATT error value on fail.
  280. */
  281. int bt_aics_state_get(struct bt_aics *inst);
  282. /**
  283. * @brief Read the Audio Input Control Service gain settings.
  284. *
  285. * @param inst The instance pointer.
  286. *
  287. * @return 0 on success, GATT error value on fail.
  288. */
  289. int bt_aics_gain_setting_get(struct bt_aics *inst);
  290. /**
  291. * @brief Read the Audio Input Control Service input type.
  292. *
  293. * @param inst The instance pointer.
  294. *
  295. * @return 0 on success, GATT error value on fail.
  296. */
  297. int bt_aics_type_get(struct bt_aics *inst);
  298. /**
  299. * @brief Read the Audio Input Control Service input status.
  300. *
  301. * @param inst The instance pointer.
  302. *
  303. * @return 0 on success, GATT error value on fail.
  304. */
  305. int bt_aics_status_get(struct bt_aics *inst);
  306. /**
  307. * @brief Unmute the Audio Input Control Service input.
  308. *
  309. * @param inst The instance pointer.
  310. *
  311. * @return 0 on success, GATT error value on fail.
  312. */
  313. int bt_aics_unmute(struct bt_aics *inst);
  314. /**
  315. * @brief Mute the Audio Input Control Service input.
  316. *
  317. * @param inst The instance pointer.
  318. *
  319. * @return 0 on success, GATT error value on fail.
  320. */
  321. int bt_aics_mute(struct bt_aics *inst);
  322. /**
  323. * @brief Set input gain to manual.
  324. *
  325. * @param inst The instance pointer.
  326. *
  327. * @return 0 on success, GATT error value on fail.
  328. */
  329. int bt_aics_manual_gain_set(struct bt_aics *inst);
  330. /**
  331. * @brief Set the input gain to automatic.
  332. *
  333. * @param inst The instance pointer.
  334. *
  335. * @return 0 on success, GATT error value on fail.
  336. */
  337. int bt_aics_automatic_gain_set(struct bt_aics *inst);
  338. /**
  339. * @brief Set the input gain.
  340. *
  341. * @param inst The instance pointer.
  342. * @param gain The gain to set (-128 to 127) in gain setting units
  343. * (see @ref bt_aics_gain_setting_cb).
  344. *
  345. * @return 0 on success, GATT error value on fail.
  346. */
  347. int bt_aics_gain_set(struct bt_aics *inst, int8_t gain);
  348. /**
  349. * @brief Read the Audio Input Control Service description.
  350. *
  351. * @param inst The instance pointer.
  352. *
  353. * @return 0 on success, GATT error value on fail.
  354. */
  355. int bt_aics_description_get(struct bt_aics *inst);
  356. /**
  357. * @brief Set the Audio Input Control Service description.
  358. *
  359. * @param inst The instance pointer.
  360. * @param description The description as an UTF-8 encoded string.
  361. *
  362. * @return 0 on success, GATT error value on fail.
  363. */
  364. int bt_aics_description_set(struct bt_aics *inst, const char *description);
  365. /**
  366. * @brief Get a new Audio Input Control Service client instance.
  367. *
  368. * @return Pointer to the instance, or NULL if no free instances are left.
  369. */
  370. struct bt_aics *bt_aics_client_free_instance_get(void);
  371. /**
  372. * @brief Registers the callbacks for the Audio Input Control Service client.
  373. *
  374. * @param inst The instance pointer.
  375. * @param cb Pointer to the callback structure.
  376. */
  377. void bt_aics_client_cb_register(struct bt_aics *inst, struct bt_aics_cb *cb);
  378. #ifdef __cplusplus
  379. }
  380. #endif
  381. /**
  382. * @}
  383. */
  384. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_AICS_H_ */