vcs.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2020-2021 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_VCS_H_
  7. #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_VCS_H_
  8. /**
  9. * @brief Volume Control Service (VCS)
  10. *
  11. * @defgroup bt_gatt_vcs Volume Control Service (VCS)
  12. *
  13. * @ingroup bluetooth
  14. * @{
  15. *
  16. * [Experimental] Users should note that the APIs can change
  17. * as a part of ongoing development.
  18. */
  19. #include <zephyr/types.h>
  20. #include <bluetooth/audio/aics.h>
  21. #include <bluetooth/audio/vocs.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #if defined(CONFIG_BT_VCS)
  26. #define BT_VCS_VOCS_CNT CONFIG_BT_VCS_VOCS_INSTANCE_COUNT
  27. #define BT_VCS_AICS_CNT CONFIG_BT_VCS_AICS_INSTANCE_COUNT
  28. #else
  29. #define BT_VCS_VOCS_CNT 0
  30. #define BT_VCS_AICS_CNT 0
  31. #endif /* CONFIG_BT_VCS */
  32. /** Volume Control Service Error codes */
  33. #define BT_VCS_ERR_INVALID_COUNTER 0x80
  34. #define BT_VCS_ERR_OP_NOT_SUPPORTED 0x81
  35. /** Volume Control Service Mute Values */
  36. #define BT_VCS_STATE_UNMUTED 0x00
  37. #define BT_VCS_STATE_MUTED 0x01
  38. /** @brief Opaque Volume Control Service instance. */
  39. struct bt_vcs;
  40. /** Register structure for Volume Control Service */
  41. struct bt_vcs_register_param {
  42. /** Initial step size (1-255) */
  43. uint8_t step;
  44. /** Initial mute state (0-1) */
  45. uint8_t mute;
  46. /** Initial volume level (0-255) */
  47. uint8_t volume;
  48. /** Register parameters for Volume Offset Control Services */
  49. struct bt_vocs_register_param vocs_param[BT_VCS_VOCS_CNT];
  50. /** Register parameters for Audio Input Control Services */
  51. struct bt_aics_register_param aics_param[BT_VCS_AICS_CNT];
  52. /** Volume Control Service callback structure. */
  53. struct bt_vcs_cb *cb;
  54. };
  55. /**
  56. * @brief Volume Control Service included services
  57. *
  58. * Used for to represent the Volume Control Service included service instances,
  59. * for either a client or a server. The instance pointers either represent
  60. * local server instances, or remote service instances.
  61. */
  62. struct bt_vcs_included {
  63. /** Number of Volume Offset Control Service instances */
  64. uint8_t vocs_cnt;
  65. /** Array of pointers to Volume Offset Control Service instances */
  66. struct bt_vocs **vocs;
  67. /** Number of Audio Input Control Service instances */
  68. uint8_t aics_cnt;
  69. /** Array of pointers to Audio Input Control Service instances */
  70. struct bt_aics **aics;
  71. };
  72. /**
  73. * @brief Register the Volume Control Service.
  74. *
  75. * This will register and enable the service and make it discoverable by
  76. * clients.
  77. *
  78. * @param param Volume Control Service register parameters.
  79. * @param[out] vcs Pointer to the registered Volume Control Service.
  80. * This will still be valid if the return value is -EALREADY.
  81. *
  82. * @return 0 if success, errno on failure.
  83. */
  84. int bt_vcs_register(struct bt_vcs_register_param *param, struct bt_vcs **vcs);
  85. /**
  86. * @brief Get Volume Control Service included services.
  87. *
  88. * Returns a pointer to a struct that contains information about the
  89. * Volume Control Service included service instances, such as pointers to the
  90. * Volume Offset Control Service (Volume Offset Control Service) or
  91. * Audio Input Control Service (AICS) instances.
  92. *
  93. * @param vcs Volume Control Service instance pointer.
  94. * @param[out] included Pointer to store the result in.
  95. *
  96. * @return 0 if success, errno on failure.
  97. */
  98. int bt_vcs_included_get(struct bt_vcs *vcs, struct bt_vcs_included *included);
  99. /**
  100. * @brief Get the connection pointer of a client instance
  101. *
  102. * Get the Bluetooth connection pointer of a Volume Control Service
  103. * client instance.
  104. *
  105. * @param vcs Volume Control Service client instance pointer.
  106. * @param[out] conn Connection pointer.
  107. *
  108. * @return 0 if success, errno on failure.
  109. */
  110. int bt_vcs_client_conn_get(const struct bt_vcs *vcs, struct bt_conn **conn);
  111. /**
  112. * @brief Callback function for bt_vcs_discover.
  113. *
  114. * This callback is only used for the client.
  115. *
  116. * @param vcs Volume Control Service instance pointer.
  117. * @param err Error value. 0 on success, GATT error on positive value
  118. * or errno on negative value.
  119. * @param vocs_count Number of Volume Offset Control Service instances
  120. * on peer device.
  121. * @param aics_count Number of Audio Input Control Service instances on
  122. * peer device.
  123. */
  124. typedef void (*bt_vcs_discover_cb)(struct bt_vcs *vcs, int err,
  125. uint8_t vocs_count, uint8_t aics_count);
  126. /**
  127. * @brief Callback function for Volume Control Service volume state.
  128. *
  129. * Called when the value is locally read as the server.
  130. * Called when the value is remotely read as the client.
  131. * Called if the value is changed by either the server or client.
  132. *
  133. * @param vcs Volume Control Service instance pointer.
  134. * @param err Error value. 0 on success, GATT error on positive value
  135. * or errno on negative value.
  136. * @param volume The volume of the Volume Control Service server.
  137. * @param mute The mute setting of the Volume Control Service server.
  138. */
  139. typedef void (*bt_vcs_state_cb)(struct bt_vcs *vcs, int err, uint8_t volume,
  140. uint8_t mute);
  141. /**
  142. * @brief Callback function for Volume Control Service flags.
  143. *
  144. * Called when the value is locally read as the server.
  145. * Called when the value is remotely read as the client.
  146. * Called if the value is changed by either the server or client.
  147. *
  148. * @param vcs Volume Control Service instance pointer.
  149. * @param err Error value. 0 on success, GATT error on positive value
  150. * or errno on negative value.
  151. * @param flags The flags of the Volume Control Service server.
  152. */
  153. typedef void (*bt_vcs_flags_cb)(struct bt_vcs *vcs, int err, uint8_t flags);
  154. /**
  155. * @brief Callback function for writes.
  156. *
  157. * This callback is only used for the client.
  158. *
  159. * @param vcs Volume Control Service instance pointer.
  160. * @param err Error value. 0 on success, GATT error on fail.
  161. */
  162. typedef void (*bt_vcs_write_cb)(struct bt_vcs *vcs, int err);
  163. struct bt_vcs_cb {
  164. /* Volume Control Service */
  165. bt_vcs_state_cb state;
  166. bt_vcs_flags_cb flags;
  167. #if defined(CONFIG_BT_VCS_CLIENT)
  168. bt_vcs_discover_cb discover;
  169. bt_vcs_write_cb vol_down;
  170. bt_vcs_write_cb vol_up;
  171. bt_vcs_write_cb mute;
  172. bt_vcs_write_cb unmute;
  173. bt_vcs_write_cb vol_down_unmute;
  174. bt_vcs_write_cb vol_up_unmute;
  175. bt_vcs_write_cb vol_set;
  176. /* Volume Offset Control Service */
  177. struct bt_vocs_cb vocs_cb;
  178. /* Audio Input Control Service */
  179. struct bt_aics_cb aics_cb;
  180. #endif /* CONFIG_BT_VCS_CLIENT */
  181. };
  182. /**
  183. * @brief Discover Volume Control Service and included services.
  184. *
  185. * This will start a GATT discovery and setup handles and subscriptions.
  186. * This shall be called once before any other actions can be
  187. * executed for the peer device, and the @ref bt_vcs_discover_cb callback
  188. * will notify when it is possible to start remote operations.
  189. *
  190. * This shall only be done as the client,
  191. *
  192. * @param conn The connection to discover Volume Control Service for.
  193. * @param[out] vcs Valid remote instance object on success.
  194. *
  195. * @return 0 if success, errno on failure.
  196. */
  197. int bt_vcs_discover(struct bt_conn *conn, struct bt_vcs **vcs);
  198. /**
  199. * @brief Set the Volume Control Service volume step size.
  200. *
  201. * Set the value that the volume changes, when changed relatively with e.g.
  202. * @ref bt_vcs_vol_down or @ref bt_vcs_vol_up.
  203. *
  204. * This can only be done as the server.
  205. *
  206. * @param volume_step The volume step size (1-255).
  207. *
  208. * @return 0 if success, errno on failure.
  209. */
  210. int bt_vcs_vol_step_set(uint8_t volume_step);
  211. /**
  212. * @brief Read the Volume Control Service volume state.
  213. *
  214. * @param vcs Volume Control Service instance pointer.
  215. *
  216. * @return 0 if success, errno on failure.
  217. */
  218. int bt_vcs_vol_get(struct bt_vcs *vcs);
  219. /**
  220. * @brief Read the Volume Control Service flags.
  221. *
  222. * @param vcs Volume Control Service instance pointer.
  223. *
  224. * @return 0 if success, errno on failure.
  225. */
  226. int bt_vcs_flags_get(struct bt_vcs *vcs);
  227. /**
  228. * @brief Turn the volume down by one step on the server.
  229. *
  230. * @param vcs Volume Control Service instance pointer.
  231. *
  232. * @return 0 if success, errno on failure.
  233. */
  234. int bt_vcs_vol_down(struct bt_vcs *vcs);
  235. /**
  236. * @brief Turn the volume up by one step on the server.
  237. *
  238. * @param vcs Volume Control Service instance pointer.
  239. *
  240. * @return 0 if success, errno on failure.
  241. */
  242. int bt_vcs_vol_up(struct bt_vcs *vcs);
  243. /**
  244. * @brief Turn the volume down and unmute the server.
  245. *
  246. * @param vcs Volume Control Service instance pointer.
  247. *
  248. * @return 0 if success, errno on failure.
  249. */
  250. int bt_vcs_unmute_vol_down(struct bt_vcs *vcs);
  251. /**
  252. * @brief Turn the volume up and unmute the server.
  253. *
  254. * @param vcs Volume Control Service instance pointer.
  255. *
  256. * @return 0 if success, errno on failure.
  257. */
  258. int bt_vcs_unmute_vol_up(struct bt_vcs *vcs);
  259. /**
  260. * @brief Set the volume on the server
  261. *
  262. * @param vcs Volume Control Service instance pointer.
  263. * @param volume The absolute volume to set.
  264. *
  265. * @return 0 if success, errno on failure.
  266. */
  267. int bt_vcs_vol_set(struct bt_vcs *vcs, uint8_t volume);
  268. /**
  269. * @brief Unmute the server.
  270. *
  271. * @param vcs Volume Control Service instance pointer.
  272. *
  273. * @return 0 if success, errno on failure.
  274. */
  275. int bt_vcs_unmute(struct bt_vcs *vcs);
  276. /**
  277. * @brief Mute the server.
  278. *
  279. * @param vcs Volume Control Service instance pointer.
  280. *
  281. * @return 0 if success, errno on failure.
  282. */
  283. int bt_vcs_mute(struct bt_vcs *vcs);
  284. /**
  285. * @brief Read the Volume Offset Control Service offset state.
  286. *
  287. * @param vcs Volume Control Service instance pointer.
  288. * @param inst Pointer to the Volume Offset Control Service instance.
  289. *
  290. * @return 0 if success, errno on failure.
  291. */
  292. int bt_vcs_vocs_state_get(struct bt_vcs *vcs, struct bt_vocs *inst);
  293. /**
  294. * @brief Read the Volume Offset Control Service location.
  295. *
  296. * @param vcs Volume Control Service instance pointer.
  297. * @param inst Pointer to the Volume Offset Control Service instance.
  298. *
  299. * @return 0 if success, errno on failure.
  300. */
  301. int bt_vcs_vocs_location_get(struct bt_vcs *vcs, struct bt_vocs *inst);
  302. /**
  303. * @brief Set the Volume Offset Control Service location.
  304. *
  305. * @param vcs Volume Control Service instance pointer.
  306. * @param inst Pointer to the Volume Offset Control Service instance.
  307. * @param location The location to set.
  308. *
  309. * @return 0 if success, errno on failure.
  310. */
  311. int bt_vcs_vocs_location_set(struct bt_vcs *vcs, struct bt_vocs *inst,
  312. uint8_t location);
  313. /**
  314. * @brief Set the Volume Offset Control Service offset state.
  315. *
  316. * @param vcs Volume Control Service instance pointer.
  317. * @param inst Pointer to the Volume Offset Control Service instance.
  318. * @param offset The offset to set (-255 to 255).
  319. *
  320. * @return 0 if success, errno on failure.
  321. */
  322. int bt_vcs_vocs_state_set(struct bt_vcs *vcs, struct bt_vocs *inst,
  323. int16_t offset);
  324. /**
  325. * @brief Read the Volume Offset Control Service output description.
  326. *
  327. * @param vcs Volume Control Service instance pointer.
  328. * @param inst Pointer to the Volume Offset Control Service instance.
  329. *
  330. * @return 0 if success, errno on failure.
  331. */
  332. int bt_vcs_vocs_description_get(struct bt_vcs *vcs, struct bt_vocs *inst);
  333. /**
  334. * @brief Set the Volume Offset Control Service description.
  335. *
  336. * @param vcs Volume Control Service instance pointer.
  337. * @param inst Pointer to the Volume Offset Control Service instance.
  338. * @param description The description to set.
  339. *
  340. * @return 0 if success, errno on failure.
  341. */
  342. int bt_vcs_vocs_description_set(struct bt_vcs *vcs, struct bt_vocs *inst,
  343. const char *description);
  344. /**
  345. * @brief Deactivates an Audio Input Control Service instance.
  346. *
  347. * Audio Input Control Services are activated by default, but this will allow
  348. * the server to deactivate an Audio Input Control Service.
  349. *
  350. * @param vcs Volume Control Service instance pointer.
  351. * @param inst Pointer to the Audio Input Control Service instance.
  352. *
  353. * @return 0 if success, errno on failure.
  354. */
  355. int bt_vcs_aics_deactivate(struct bt_vcs *vcs, struct bt_aics *inst);
  356. /**
  357. * @brief Activates an Audio Input Control Service instance.
  358. *
  359. * Audio Input Control Services are activated by default, but this will allow
  360. * the server to reactivate an Audio Input Control Service instance after it has
  361. * been deactivated with @ref bt_vcs_aics_deactivate.
  362. *
  363. * @param vcs Volume Control Service instance pointer.
  364. * @param inst Pointer to the Audio Input Control Service instance.
  365. *
  366. * @return 0 if success, errno on failure.
  367. */
  368. int bt_vcs_aics_activate(struct bt_vcs *vcs, struct bt_aics *inst);
  369. /**
  370. * @brief Read the Audio Input Control Service input state.
  371. *
  372. * @param vcs Volume Control Service instance pointer.
  373. * @param inst Pointer to the Audio Input Control Service instance.
  374. *
  375. * @return 0 if success, errno on failure.
  376. */
  377. int bt_vcs_aics_state_get(struct bt_vcs *vcs, struct bt_aics *inst);
  378. /**
  379. * @brief Read the Audio Input Control Service gain settings.
  380. *
  381. * @param vcs Volume Control Service instance pointer.
  382. * @param inst Pointer to the Audio Input Control Service instance.
  383. *
  384. * @return 0 if success, errno on failure.
  385. */
  386. int bt_vcs_aics_gain_setting_get(struct bt_vcs *vcs, struct bt_aics *inst);
  387. /**
  388. * @brief Read the Audio Input Control Service input type.
  389. *
  390. * @param vcs Volume Control Service instance pointer.
  391. * @param inst Pointer to the Audio Input Control Service instance.
  392. *
  393. * @return 0 if success, errno on failure.
  394. */
  395. int bt_vcs_aics_type_get(struct bt_vcs *vcs, struct bt_aics *inst);
  396. /**
  397. * @brief Read the Audio Input Control Service input status.
  398. *
  399. * @param vcs Volume Control Service instance pointer.
  400. * @param inst Pointer to the Audio Input Control Service instance.
  401. *
  402. * @return 0 if success, errno on failure.
  403. */
  404. int bt_vcs_aics_status_get(struct bt_vcs *vcs, struct bt_aics *inst);
  405. /**
  406. * @brief Mute the Audio Input Control Service input.
  407. *
  408. * @param vcs Volume Control Service instance pointer.
  409. * @param inst Pointer to the Audio Input Control Service instance.
  410. *
  411. * @return 0 if success, errno on failure.
  412. */
  413. int bt_vcs_aics_mute(struct bt_vcs *vcs, struct bt_aics *inst);
  414. /**
  415. * @brief Unmute the Audio Input Control Service input.
  416. *
  417. * @param vcs Volume Control Service instance pointer.
  418. * @param inst Pointer to the Audio Input Control Service instance.
  419. *
  420. * @return 0 if success, errno on failure.
  421. */
  422. int bt_vcs_aics_unmute(struct bt_vcs *vcs, struct bt_aics *inst);
  423. /**
  424. * @brief Set input gain to manual.
  425. *
  426. * @param vcs Volume Control Service instance pointer.
  427. * @param inst Pointer to the Audio Input Control Service instance.
  428. *
  429. * @return 0 if success, errno on failure.
  430. */
  431. int bt_vcs_aics_manual_gain_set(struct bt_vcs *vcs, struct bt_aics *inst);
  432. /**
  433. * @brief Set the input gain to automatic.
  434. *
  435. * @param vcs Volume Control Service instance pointer.
  436. * @param inst Pointer to the Audio Input Control Service instance.
  437. *
  438. * @return 0 if success, errno on failure.
  439. */
  440. int bt_vcs_aics_automatic_gain_set(struct bt_vcs *vcs, struct bt_aics *inst);
  441. /**
  442. * @brief Set the input gain.
  443. *
  444. * @param vcs Volume Control Service instance pointer.
  445. * @param inst Pointer to the Audio Input Control Service instance.
  446. * @param gain The gain in dB to set (-128 to 127).
  447. *
  448. * @return 0 if success, errno on failure.
  449. */
  450. int bt_vcs_aics_gain_set(struct bt_vcs *vcs, struct bt_aics *inst,
  451. int8_t gain);
  452. /**
  453. * @brief Read the Audio Input Control Service description.
  454. *
  455. * @param vcs Volume Control Service instance pointer.
  456. * @param inst Pointer to the Audio Input Control Service instance.
  457. *
  458. * @return 0 if success, errno on failure.
  459. */
  460. int bt_vcs_aics_description_get(struct bt_vcs *vcs, struct bt_aics *inst);
  461. /**
  462. * @brief Set the Audio Input Control Service description.
  463. *
  464. * @param vcs Volume Control Service instance pointer.
  465. * @param inst Pointer to the Audio Input Control Service instance.
  466. * @param description The description to set.
  467. *
  468. * @return 0 if success, errno on failure.
  469. */
  470. int bt_vcs_aics_description_set(struct bt_vcs *vcs, struct bt_aics *inst,
  471. const char *description);
  472. /**
  473. * @brief Registers the callbacks used by the Volume Control Service client.
  474. *
  475. * @param cb The callback structure.
  476. *
  477. * @return 0 if success, errno on failure.
  478. */
  479. int bt_vcs_client_cb_register(struct bt_vcs_cb *cb);
  480. #ifdef __cplusplus
  481. }
  482. #endif
  483. /**
  484. * @}
  485. */
  486. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_VCS_H_ */