vocs.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2020 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_VOCS_H_
  7. #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_VOCS_H_
  8. /**
  9. * @brief Volume Offset Control Service (VOCS)
  10. *
  11. * @defgroup bt_gatt_vocs Volume Offset Control Service (VOCS)
  12. *
  13. * @ingroup bluetooth
  14. * @{
  15. *
  16. * The Volume Offset Control Service is a secondary service, and as such should not be used own 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 volume offset 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. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /** Volume Offset Control Service Error codes */
  31. #define BT_VOCS_ERR_INVALID_COUNTER 0x80
  32. #define BT_VOCS_ERR_OP_NOT_SUPPORTED 0x81
  33. #define BT_VOCS_ERR_OUT_OF_RANGE 0x82
  34. #define BT_VOCS_MIN_OFFSET -255
  35. #define BT_VOCS_MAX_OFFSET 255
  36. /** @brief Opaque Volume Offset Control Service instance. */
  37. struct bt_vocs;
  38. /** @brief Structure for registering a Volume Offset Control Service instance. */
  39. struct bt_vocs_register_param {
  40. /** Audio Location bitmask */
  41. uint32_t location;
  42. /** Boolean to set whether the location is writable by clients */
  43. bool location_writable;
  44. /** Initial volume offset (-255 to 255) */
  45. int16_t offset;
  46. /** Initial audio output description */
  47. char *output_desc;
  48. /** Boolean to set whether the description is writable by clients */
  49. bool desc_writable;
  50. /** Pointer to the callback structure. */
  51. struct bt_vocs_cb *cb;
  52. };
  53. /** @brief Structure for discovering a Volume Offset Control Service instance. */
  54. struct bt_vocs_discover_param {
  55. /**
  56. * @brief The start handle of the discovering.
  57. *
  58. * Typically the @p start_handle of a @ref bt_gatt_include.
  59. */
  60. uint16_t start_handle;
  61. /**
  62. * @brief The end handle of the discovering.
  63. *
  64. * Typically the @p end_handle of a @ref bt_gatt_include.
  65. */
  66. uint16_t end_handle;
  67. };
  68. /**
  69. * @brief Get a free service instance of Volume Offset Control Service from the pool.
  70. *
  71. * @return Volume Offset Control Service instance in case of success or NULL in case of error.
  72. */
  73. struct bt_vocs *bt_vocs_free_instance_get(void);
  74. /**
  75. * @brief Get the service declaration attribute.
  76. *
  77. * The first service attribute returned can be included in any other GATT service.
  78. *
  79. * @param vocs Volume Offset Control Service instance.
  80. *
  81. * @return Pointer to the attributes of the service.
  82. */
  83. void *bt_vocs_svc_decl_get(struct bt_vocs *vocs);
  84. /**
  85. * @brief Get the connection pointer of a client instance
  86. *
  87. * Get the Bluetooth connection pointer of a Audio Input Control Service
  88. * client instance.
  89. *
  90. * @param vocs Audio Input Control Service client instance pointer.
  91. * @param conn Connection pointer.
  92. *
  93. * @return 0 if success, errno on failure.
  94. */
  95. int bt_vocs_client_conn_get(const struct bt_vocs *vocs, struct bt_conn **conn);
  96. /**
  97. * @brief Register the Volume Offset Control Service instance.
  98. *
  99. * @param vocs Volume Offset Control Service instance.
  100. * @param param Volume Offset Control Service register parameters.
  101. *
  102. * @return 0 if success, errno on failure.
  103. */
  104. int bt_vocs_register(struct bt_vocs *vocs,
  105. const struct bt_vocs_register_param *param);
  106. /**
  107. * @brief Callback function for the offset state.
  108. *
  109. * Called when the value is read, or if the value is changed by either the server or client.
  110. *
  111. * @param inst The instance pointer.
  112. * @param err Error value. 0 on success, GATT error on positive value
  113. * or errno on negative value.
  114. * For notifications, this will always be 0.
  115. * @param offset The offset value.
  116. */
  117. typedef void (*bt_vocs_state_cb)(struct bt_vocs *inst, int err, int16_t offset);
  118. /**
  119. * @brief Callback function for setting offset.
  120. *
  121. * @param inst The instance pointer.
  122. * @param err Error value. 0 on success, GATT error on positive value
  123. * or errno on negative value.
  124. */
  125. typedef void (*bt_vocs_set_offset_cb)(struct bt_vocs *inst, int err);
  126. /**
  127. * @brief Callback function for the location.
  128. *
  129. * Called when the value is read, or if the value is changed by either the server or client.
  130. *
  131. * @param inst The instance pointer.
  132. * @param err Error value. 0 on success, GATT error on positive value
  133. * or errno on negative value.
  134. * For notifications, this will always be 0.
  135. * @param location The location value.
  136. */
  137. typedef void (*bt_vocs_location_cb)(struct bt_vocs *inst, int err,
  138. uint32_t location);
  139. /**
  140. * @brief Callback function for the description.
  141. *
  142. * Called when the value is read, or if the value is changed by either the server or client.
  143. *
  144. * @param inst The instance pointer.
  145. * @param err Error value. 0 on success, GATT error on positive value
  146. * or errno on negative value.
  147. * For notifications, this will always be 0.
  148. * @param description The description as an UTF-8 encoded string.
  149. */
  150. typedef void (*bt_vocs_description_cb)(struct bt_vocs *inst, int err,
  151. char *description);
  152. /**
  153. * @brief Callback function for bt_vocs_discover.
  154. *
  155. * This callback should be overwritten by the primary service that
  156. * includes the Volume Control Offset Service client, and should thus not be
  157. * set by the application.
  158. *
  159. * @param inst The instance pointer.
  160. * @param err Error value. 0 on success, GATT error on positive value
  161. * or errno on negative value.
  162. * For notifications, this will always be 0.
  163. */
  164. typedef void (*bt_vocs_discover_cb)(struct bt_vocs *inst, int err);
  165. struct bt_vocs_cb {
  166. bt_vocs_state_cb state;
  167. bt_vocs_location_cb location;
  168. bt_vocs_description_cb description;
  169. #if defined(CONFIG_BT_VOCS_CLIENT)
  170. /* Client only */
  171. bt_vocs_discover_cb discover;
  172. bt_vocs_set_offset_cb set_offset;
  173. #endif /* CONFIG_BT_VOCS_CLIENT */
  174. };
  175. /**
  176. * @brief Read the Volume Offset Control Service offset state.
  177. *
  178. * The value is returned in the bt_vocs_cb.state callback.
  179. *
  180. * @param inst Pointer to the Volume Offset Control Service instance.
  181. *
  182. * @return 0 on success, GATT error value on fail.
  183. */
  184. int bt_vocs_state_get(struct bt_vocs *inst);
  185. /**
  186. * @brief Set the Volume Offset Control Service offset state.
  187. *
  188. * @param inst Pointer to the Volume Offset Control Service instance.
  189. * @param offset The offset to set (-255 to 255).
  190. *
  191. * @return 0 on success, GATT error value on fail.
  192. */
  193. int bt_vocs_state_set(struct bt_vocs *inst, int16_t offset);
  194. /**
  195. * @brief Read the Volume Offset Control Service location.
  196. *
  197. * The value is returned in the bt_vocs_cb.location callback.
  198. *
  199. * @param inst Pointer to the Volume Offset Control Service instance.
  200. *
  201. * @return 0 on success, GATT error value on fail.
  202. */
  203. int bt_vocs_location_get(struct bt_vocs *inst);
  204. /**
  205. * @brief Set the Volume Offset Control Service location.
  206. *
  207. * @param inst Pointer to the Volume Offset Control Service instance.
  208. * @param location The location to set.
  209. *
  210. * @return 0 on success, GATT error value on fail.
  211. */
  212. int bt_vocs_location_set(struct bt_vocs *inst, uint32_t location);
  213. /**
  214. * @brief Read the Volume Offset Control Service output description.
  215. *
  216. * The value is returned in the bt_vocs_cb.description callback.
  217. *
  218. * @param inst Pointer to the Volume Offset Control Service instance.
  219. *
  220. * @return 0 on success, GATT error value on fail.
  221. */
  222. int bt_vocs_description_get(struct bt_vocs *inst);
  223. /**
  224. * @brief Set the Volume Offset Control Service description.
  225. *
  226. * @param inst Pointer to the Volume Offset Control Service instance.
  227. * @param description The UTF-8 encoded string description to set.
  228. *
  229. * @return 0 on success, GATT error value on fail.
  230. */
  231. int bt_vocs_description_set(struct bt_vocs *inst, const char *description);
  232. /**
  233. * @brief Registers the callbacks for the Volume Offset Control Service client.
  234. *
  235. * @param inst Pointer to the Volume Offset Control Service client instance.
  236. * @param cb Pointer to the callback structure.
  237. */
  238. void bt_vocs_client_cb_register(struct bt_vocs *inst, struct bt_vocs_cb *cb);
  239. /**
  240. * @brief Returns a pointer to a Volume Offset Control Service client instance.
  241. *
  242. * @return Pointer to the instance, or NULL if no free instances are left.
  243. */
  244. struct bt_vocs *bt_vocs_client_free_instance_get(void);
  245. /**
  246. * @brief Discover a Volume Offset Control Service.
  247. *
  248. * Attempts to discover a Volume Offset Control Service on a server given the @p param.
  249. *
  250. * @param conn Connection to the peer with the Volume Offset Control Service.
  251. * @param inst Pointer to the Volume Offset Control Service client instance.
  252. * @param param Pointer to the parameters.
  253. *
  254. * @return 0 on success, errno on fail.
  255. */
  256. int bt_vocs_discover(struct bt_conn *conn, struct bt_vocs *inst,
  257. const struct bt_vocs_discover_param *param);
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. /**
  262. * @}
  263. */
  264. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_VOCS_H_ */