ots.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Copyright (c) 2020 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_OTS_H_
  7. #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_OTS_H_
  8. /**
  9. * @brief Object Transfer Service (OTS)
  10. * @defgroup bt_ots Object Transfer Service (OTS)
  11. * @ingroup bluetooth
  12. * @{
  13. *
  14. * [Experimental] Users should note that the APIs can change
  15. * as a part of ongoing development.
  16. */
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <zephyr/types.h>
  21. #include <sys/byteorder.h>
  22. #include <sys/types.h>
  23. #include <sys/util.h>
  24. #include <bluetooth/conn.h>
  25. #include <bluetooth/uuid.h>
  26. /** @brief Size of OTS object ID (in bytes). */
  27. #define BT_OTS_OBJ_ID_SIZE 6
  28. /** @brief Minimum allowed value for object ID (except ID for directory listing) */
  29. #define BT_OTS_OBJ_ID_MIN 0x000000000100
  30. /** @brief Maximum allowed value for object ID (except ID for directory listing) */
  31. #define BT_OTS_OBJ_ID_MAX 0xFFFFFFFFFFFF
  32. /** @brief ID of the Directory Listing Object */
  33. #define OTS_OBJ_ID_DIR_LIST 0x000000000000
  34. /** @brief Mask for OTS object IDs, preserving the 48 bits */
  35. #define BT_OTS_OBJ_ID_MASK BIT64_MASK(48)
  36. /** @brief Length of OTS object ID string (in bytes). */
  37. #define BT_OTS_OBJ_ID_STR_LEN 15
  38. /** @brief Type of an OTS object. */
  39. struct bt_ots_obj_type {
  40. union {
  41. /* Used to indicate UUID type */
  42. struct bt_uuid uuid;
  43. /* 16-bit UUID value */
  44. struct bt_uuid_16 uuid_16;
  45. /* 128-bit UUID value */
  46. struct bt_uuid_128 uuid_128;
  47. };
  48. };
  49. /** @brief Properties of an OTS object. */
  50. enum {
  51. /** Bit 0 Deletion of this object is permitted */
  52. BT_OTS_OBJ_PROP_DELETE = 0,
  53. /** Bit 1 Execution of this object is permitted */
  54. BT_OTS_OBJ_PROP_EXECUTE = 1,
  55. /** Bit 2 Reading this object is permitted */
  56. BT_OTS_OBJ_PROP_READ = 2,
  57. /** Bit 3 Writing data to this object is permitted */
  58. BT_OTS_OBJ_PROP_WRITE = 3,
  59. /** @brief Bit 4 Appending data to this object is permitted.
  60. *
  61. * Appending data increases its Allocated Size.
  62. */
  63. BT_OTS_OBJ_PROP_APPEND = 4,
  64. /** Bit 5 Truncation of this object is permitted */
  65. BT_OTS_OBJ_PROP_TRUNCATE = 5,
  66. /** @brief Bit 6 Patching this object is permitted
  67. *
  68. * Patching this object overwrites some of
  69. * the object's existing contents.
  70. */
  71. BT_OTS_OBJ_PROP_PATCH = 6,
  72. /** Bit 7 This object is a marked object */
  73. BT_OTS_OBJ_PROP_MARKED = 7,
  74. };
  75. /** @brief Set @ref BT_OTS_OBJ_PROP_DELETE property.
  76. *
  77. * @param prop Object properties.
  78. */
  79. #define BT_OTS_OBJ_SET_PROP_DELETE(prop) \
  80. WRITE_BIT(prop, BT_OTS_OBJ_PROP_DELETE, 1)
  81. /** @brief Set @ref BT_OTS_OBJ_PROP_EXECUTE property.
  82. *
  83. * @param prop Object properties.
  84. */
  85. #define BT_OTS_OBJ_SET_PROP_EXECUTE(prop) \
  86. WRITE_BIT(prop, BT_OTS_OBJ_PROP_EXECUTE, 1)
  87. /** @brief Set @ref BT_OTS_OBJ_PROP_READ property.
  88. *
  89. * @param prop Object properties.
  90. */
  91. #define BT_OTS_OBJ_SET_PROP_READ(prop) \
  92. WRITE_BIT(prop, BT_OTS_OBJ_PROP_READ, 1)
  93. /** @brief Set @ref BT_OTS_OBJ_PROP_WRITE property.
  94. *
  95. * @param prop Object properties.
  96. */
  97. #define BT_OTS_OBJ_SET_PROP_WRITE(prop) \
  98. WRITE_BIT(prop, BT_OTS_OBJ_PROP_WRITE, 1)
  99. /** @brief Set @ref BT_OTS_OBJ_PROP_APPEND property.
  100. *
  101. * @param prop Object properties.
  102. */
  103. #define BT_OTS_OBJ_SET_PROP_APPEND(prop) \
  104. WRITE_BIT(prop, BT_OTS_OBJ_PROP_APPEND, 1)
  105. /** @brief Set @ref BT_OTS_OBJ_PROP_TRUNCATE property.
  106. *
  107. * @param prop Object properties.
  108. */
  109. #define BT_OTS_OBJ_SET_PROP_TRUNCATE(prop) \
  110. WRITE_BIT(prop, BT_OTS_OBJ_PROP_TRUNCATE, 1)
  111. /** @brief Set @ref BT_OTS_OBJ_PROP_PATCH property.
  112. *
  113. * @param prop Object properties.
  114. */
  115. #define BT_OTS_OBJ_SET_PROP_PATCH(prop) \
  116. WRITE_BIT(prop, BT_OTS_OBJ_PROP_PATCH, 1)
  117. /** @brief Set @ref BT_OTS_OBJ_SET_PROP_MARKED property.
  118. *
  119. * @param prop Object properties.
  120. */
  121. #define BT_OTS_OBJ_SET_PROP_MARKED(prop) \
  122. WRITE_BIT(prop, BT_OTS_OBJ_PROP_MARKED, 1)
  123. /** @brief Get @ref BT_OTS_OBJ_PROP_DELETE property.
  124. *
  125. * @param prop Object properties.
  126. */
  127. #define BT_OTS_OBJ_GET_PROP_DELETE(prop) \
  128. ((prop) & BIT(BT_OTS_OBJ_PROP_DELETE))
  129. /** @brief Get @ref BT_OTS_OBJ_PROP_EXECUTE property.
  130. *
  131. * @param prop Object properties.
  132. */
  133. #define BT_OTS_OBJ_GET_PROP_EXECUTE(prop) \
  134. ((prop) & BIT(BT_OTS_OBJ_PROP_EXECUTE))
  135. /** @brief Get @ref BT_OTS_OBJ_PROP_READ property.
  136. *
  137. * @param prop Object properties.
  138. */
  139. #define BT_OTS_OBJ_GET_PROP_READ(prop) \
  140. ((prop) & BIT(BT_OTS_OBJ_PROP_READ))
  141. /** @brief Get @ref BT_OTS_OBJ_PROP_WRITE property.
  142. *
  143. * @param prop Object properties.
  144. */
  145. #define BT_OTS_OBJ_GET_PROP_WRITE(prop) \
  146. ((prop) & BIT(BT_OTS_OBJ_PROP_WRITE))
  147. /** @brief Get @ref BT_OTS_OBJ_PROP_APPEND property.
  148. *
  149. * @param prop Object properties.
  150. */
  151. #define BT_OTS_OBJ_GET_PROP_APPEND(prop) \
  152. ((prop) & BIT(BT_OTS_OBJ_PROP_APPEND))
  153. /** @brief Get @ref BT_OTS_OBJ_PROP_TRUNCATE property.
  154. *
  155. * @param prop Object properties.
  156. */
  157. #define BT_OTS_OBJ_GET_PROP_TRUNCATE(prop) \
  158. ((prop) & BIT(BT_OTS_OBJ_PROP_TRUNCATE))
  159. /** @brief Get @ref BT_OTS_OBJ_PROP_PATCH property.
  160. *
  161. * @param prop Object properties.
  162. */
  163. #define BT_OTS_OBJ_GET_PROP_PATCH(prop) \
  164. ((prop) & BIT(BT_OTS_OBJ_PROP_PATCH))
  165. /** @brief Get @ref BT_OTS_OBJ_PROP_MARKED property.
  166. *
  167. * @param prop Object properties.
  168. */
  169. #define BT_OTS_OBJ_GET_PROP_MARKED(prop) \
  170. ((prop) & BIT(BT_OTS_OBJ_PROP_MARKED))
  171. /** @brief Descriptor for OTS Object Size parameter. */
  172. struct bt_ots_obj_size {
  173. /* Current Size */
  174. uint32_t cur;
  175. /* Allocated Size */
  176. uint32_t alloc;
  177. } __packed;
  178. /** @brief Descriptor for OTS object initialization. */
  179. struct bt_ots_obj_metadata {
  180. /* Object Name */
  181. char *name;
  182. /* Object Type */
  183. struct bt_ots_obj_type type;
  184. /* Object Size */
  185. struct bt_ots_obj_size size;
  186. /* Object Properties */
  187. uint32_t props;
  188. };
  189. /** @brief Object Action Control Point Feature bits. */
  190. enum {
  191. /** Bit 0 OACP Create Op Code Supported */
  192. BT_OTS_OACP_FEAT_CREATE = 0,
  193. /** Bit 1 OACP Delete Op Code Supported */
  194. BT_OTS_OACP_FEAT_DELETE = 1,
  195. /** Bit 2 OACP Calculate Checksum Op Code Supported */
  196. BT_OTS_OACP_FEAT_CHECKSUM = 2,
  197. /** Bit 3 OACP Execute Op Code Supported */
  198. BT_OTS_OACP_FEAT_EXECUTE = 3,
  199. /** Bit 4 OACP Read Op Code Supported */
  200. BT_OTS_OACP_FEAT_READ = 4,
  201. /** Bit 5 OACP Write Op Code Supported */
  202. BT_OTS_OACP_FEAT_WRITE = 5,
  203. /** Bit 6 Appending Additional Data to Objects Supported */
  204. BT_OTS_OACP_FEAT_APPEND = 6,
  205. /** Bit 7 Truncation of Objects Supported */
  206. BT_OTS_OACP_FEAT_TRUNCATE = 7,
  207. /** Bit 8 Patching of Objects Supported */
  208. BT_OTS_OACP_FEAT_PATCH = 8,
  209. /** Bit 9 OACP Abort Op Code Supported */
  210. BT_OTS_OACP_FEAT_ABORT = 9,
  211. };
  212. /** @brief Set @ref BT_OTS_OACP_SET_FEAT_CREATE feature.
  213. *
  214. * @param feat OTS features.
  215. */
  216. #define BT_OTS_OACP_SET_FEAT_CREATE(feat) \
  217. WRITE_BIT(feat, BT_OTS_OACP_FEAT_CREATE, 1)
  218. /** @brief Set @ref BT_OTS_OACP_FEAT_DELETE feature.
  219. *
  220. * @param feat OTS features.
  221. */
  222. #define BT_OTS_OACP_SET_FEAT_DELETE(feat) \
  223. WRITE_BIT(feat, BT_OTS_OACP_FEAT_DELETE, 1)
  224. /** @brief Set @ref BT_OTS_OACP_FEAT_CHECKSUM feature.
  225. *
  226. * @param feat OTS features.
  227. */
  228. #define BT_OTS_OACP_SET_FEAT_CHECKSUM(feat) \
  229. WRITE_BIT(feat, BT_OTS_OACP_FEAT_CHECKSUM, 1)
  230. /** @brief Set @ref BT_OTS_OACP_FEAT_EXECUTE feature.
  231. *
  232. * @param feat OTS features.
  233. */
  234. #define BT_OTS_OACP_SET_FEAT_EXECUTE(feat) \
  235. WRITE_BIT(feat, BT_OTS_OACP_FEAT_EXECUTE, 1)
  236. /** @brief Set @ref BT_OTS_OACP_FEAT_READ feature.
  237. *
  238. * @param feat OTS features.
  239. */
  240. #define BT_OTS_OACP_SET_FEAT_READ(feat) \
  241. WRITE_BIT(feat, BT_OTS_OACP_FEAT_READ, 1)
  242. /** @brief Set @ref BT_OTS_OACP_FEAT_WRITE feature.
  243. *
  244. * @param feat OTS features.
  245. */
  246. #define BT_OTS_OACP_SET_FEAT_WRITE(feat) \
  247. WRITE_BIT(feat, BT_OTS_OACP_FEAT_WRITE, 1)
  248. /** @brief Set @ref BT_OTS_OACP_FEAT_APPEND feature.
  249. *
  250. * @param feat OTS features.
  251. */
  252. #define BT_OTS_OACP_SET_FEAT_APPEND(feat) \
  253. WRITE_BIT(feat, BT_OTS_OACP_FEAT_APPEND, 1)
  254. /** @brief Set @ref BT_OTS_OACP_FEAT_TRUNCATE feature.
  255. *
  256. * @param feat OTS features.
  257. */
  258. #define BT_OTS_OACP_SET_FEAT_TRUNCATE(feat) \
  259. WRITE_BIT(feat, BT_OTS_OACP_FEAT_TRUNCATE, 1)
  260. /** @brief Set @ref BT_OTS_OACP_FEAT_PATCH feature.
  261. *
  262. * @param feat OTS features.
  263. */
  264. #define BT_OTS_OACP_SET_FEAT_PATCH(feat) \
  265. WRITE_BIT(feat, BT_OTS_OACP_FEAT_PATCH, 1)
  266. /** @brief Set @ref BT_OTS_OACP_FEAT_ABORT feature.
  267. *
  268. * @param feat OTS features.
  269. */
  270. #define BT_OTS_OACP_SET_FEAT_ABORT(feat) \
  271. WRITE_BIT(feat, BT_OTS_OACP_FEAT_ABORT, 1)
  272. /** @brief Get @ref BT_OTS_OACP_FEAT_CREATE feature.
  273. *
  274. * @param feat OTS features.
  275. */
  276. #define BT_OTS_OACP_GET_FEAT_CREATE(feat) \
  277. ((feat) & BIT(BT_OTS_OACP_FEAT_CREATE))
  278. /** @brief Get @ref BT_OTS_OACP_FEAT_DELETE feature.
  279. *
  280. * @param feat OTS features.
  281. */
  282. #define BT_OTS_OACP_GET_FEAT_DELETE(feat) \
  283. ((feat) & BIT(BT_OTS_OACP_FEAT_DELETE))
  284. /** @brief Get @ref BT_OTS_OACP_FEAT_CHECKSUM feature.
  285. *
  286. * @param feat OTS features.
  287. */
  288. #define BT_OTS_OACP_GET_FEAT_CHECKSUM(feat) \
  289. ((feat) & BIT(BT_OTS_OACP_FEAT_CHECKSUM))
  290. /** @brief Get @ref BT_OTS_OACP_FEAT_EXECUTE feature.
  291. *
  292. * @param feat OTS features.
  293. */
  294. #define BT_OTS_OACP_GET_FEAT_EXECUTE(feat) \
  295. ((feat) & BIT(BT_OTS_OACP_FEAT_EXECUTE))
  296. /** @brief Get @ref BT_OTS_OACP_FEAT_READ feature.
  297. *
  298. * @param feat OTS features.
  299. */
  300. #define BT_OTS_OACP_GET_FEAT_READ(feat) \
  301. ((feat) & BIT(BT_OTS_OACP_FEAT_READ))
  302. /** @brief Get @ref BT_OTS_OACP_FEAT_WRITE feature.
  303. *
  304. * @param feat OTS features.
  305. */
  306. #define BT_OTS_OACP_GET_FEAT_WRITE(feat) \
  307. ((feat) & BIT(BT_OTS_OACP_FEAT_WRITE))
  308. /** @brief Get @ref BT_OTS_OACP_FEAT_APPEND feature.
  309. *
  310. * @param feat OTS features.
  311. */
  312. #define BT_OTS_OACP_GET_FEAT_APPEND(feat) \
  313. ((feat) & BIT(BT_OTS_OACP_FEAT_APPEND))
  314. /** @brief Get @ref BT_OTS_OACP_FEAT_TRUNCATE feature.
  315. *
  316. * @param feat OTS features.
  317. */
  318. #define BT_OTS_OACP_GET_FEAT_TRUNCATE(feat) \
  319. ((feat) & BIT(BT_OTS_OACP_FEAT_TRUNCATE))
  320. /** @brief Get @ref BT_OTS_OACP_FEAT_PATCH feature.
  321. *
  322. * @param feat OTS features.
  323. */
  324. #define BT_OTS_OACP_GET_FEAT_PATCH(feat) \
  325. ((feat) & BIT(BT_OTS_OACP_FEAT_PATCH))
  326. /** @brief Get @ref BT_OTS_OACP_FEAT_ABORT feature.
  327. *
  328. * @param feat OTS features.
  329. */
  330. #define BT_OTS_OACP_GET_FEAT_ABORT(feat) \
  331. ((feat) & BIT(BT_OTS_OACP_FEAT_ABORT))
  332. /** @brief Object List Control Point Feature bits. */
  333. enum {
  334. /** Bit 0 OLCP Go To Op Code Supported */
  335. BT_OTS_OLCP_FEAT_GO_TO = 0,
  336. /** Bit 1 OLCP Order Op Code Supported */
  337. BT_OTS_OLCP_FEAT_ORDER = 1,
  338. /** Bit 2 OLCP Request Number of Objects Op Code Supported */
  339. BT_OTS_OLCP_FEAT_NUM_REQ = 2,
  340. /** Bit 3 OLCP Clear Marking Op Code Supported*/
  341. BT_OTS_OLCP_FEAT_CLEAR = 3,
  342. };
  343. /** @brief Set @ref BT_OTS_OLCP_FEAT_GO_TO feature.
  344. *
  345. * @param feat OTS features.
  346. */
  347. #define BT_OTS_OLCP_SET_FEAT_GO_TO(feat) \
  348. WRITE_BIT(feat, BT_OTS_OLCP_FEAT_GO_TO, 1)
  349. /** @brief Set @ref BT_OTS_OLCP_FEAT_ORDER feature.
  350. *
  351. * @param feat OTS features.
  352. */
  353. #define BT_OTS_OLCP_SET_FEAT_ORDER(feat) \
  354. WRITE_BIT(feat, BT_OTS_OLCP_FEAT_ORDER, 1)
  355. /** @brief Set @ref BT_OTS_OLCP_FEAT_NUM_REQ feature.
  356. *
  357. * @param feat OTS features.
  358. */
  359. #define BT_OTS_OLCP_SET_FEAT_NUM_REQ(feat) \
  360. WRITE_BIT(feat, BT_OTS_OLCP_FEAT_NUM_REQ, 1)
  361. /** @brief Set @ref BT_OTS_OLCP_FEAT_CLEAR feature.
  362. *
  363. * @param feat OTS features.
  364. */
  365. #define BT_OTS_OLCP_SET_FEAT_CLEAR(feat) \
  366. WRITE_BIT(feat, BT_OTS_OLCP_FEAT_CLEAR, 1)
  367. /** @brief Get @ref BT_OTS_OLCP_GET_FEAT_GO_TO feature.
  368. *
  369. * @param feat OTS features.
  370. */
  371. #define BT_OTS_OLCP_GET_FEAT_GO_TO(feat) \
  372. ((feat) & BIT(BT_OTS_OLCP_FEAT_GO_TO))
  373. /** @brief Get @ref BT_OTS_OLCP_GET_FEAT_ORDER feature.
  374. *
  375. * @param feat OTS features.
  376. */
  377. #define BT_OTS_OLCP_GET_FEAT_ORDER(feat) \
  378. ((feat) & BIT(BT_OTS_OLCP_FEAT_ORDER))
  379. /** @brief Get @ref BT_OTS_OLCP_GET_FEAT_NUM_REQ feature.
  380. *
  381. * @param feat OTS features.
  382. */
  383. #define BT_OTS_OLCP_GET_FEAT_NUM_REQ(feat) \
  384. ((feat) & BIT(BT_OTS_OLCP_FEAT_NUM_REQ))
  385. /** @brief Get @ref BT_OTS_OLCP_GET_FEAT_CLEAR feature.
  386. *
  387. * @param feat OTS features.
  388. */
  389. #define BT_OTS_OLCP_GET_FEAT_CLEAR(feat) \
  390. ((feat) & BIT(BT_OTS_OLCP_FEAT_CLEAR))
  391. /**@brief Features of the OTS. */
  392. struct bt_ots_feat {
  393. /* OACP Features */
  394. uint32_t oacp;
  395. /* OLCP Features */
  396. uint32_t olcp;
  397. } __packed;
  398. /** @brief Opaque OTS instance. */
  399. struct bt_ots;
  400. /** @brief OTS callback structure. */
  401. struct bt_ots_cb {
  402. /** @brief Object created callback
  403. *
  404. * This callback is called whenever a new object is created.
  405. * Application can reject this request by returning an error
  406. * when it does not have necessary resources to hold this new
  407. * object. This callback is also triggered when the server
  408. * creates a new object with bt_ots_obj_add() API.
  409. *
  410. * @param ots OTS instance.
  411. * @param conn The connection that is requesting object creation or
  412. * NULL if object is created by the following function:
  413. * bt_ots_obj_add().
  414. * @param id Object ID.
  415. * @param init Object initialization metadata.
  416. *
  417. * @return 0 in case of success or negative value in case of error.
  418. * Possible return values:
  419. * -ENOMEM if no available space for new object.
  420. */
  421. int (*obj_created)(struct bt_ots *ots, struct bt_conn *conn,
  422. uint64_t id,
  423. const struct bt_ots_obj_metadata *init);
  424. /** @brief Object deleted callback
  425. *
  426. * This callback is called whenever an object is deleted. It is
  427. * also triggered when the server deletes an object with
  428. * bt_ots_obj_delete() API.
  429. *
  430. * @param ots OTS instance.
  431. * @param conn The connection that deleted the object or NULL if
  432. * this request came from the server.
  433. * @param id Object ID.
  434. */
  435. void (*obj_deleted)(struct bt_ots *ots, struct bt_conn *conn,
  436. uint64_t id);
  437. /** @brief Object selected callback
  438. *
  439. * This callback is called on successful object selection.
  440. *
  441. * @param ots OTS instance.
  442. * @param conn The connection that selected new object.
  443. * @param id Object ID.
  444. */
  445. void (*obj_selected)(struct bt_ots *ots, struct bt_conn *conn,
  446. uint64_t id);
  447. /** @brief Object read callback
  448. *
  449. * This callback is called multiple times during the Object read
  450. * operation. OTS module will keep requesting successive Object
  451. * fragments from the application until the read operation is
  452. * completed. The end of read operation is indicated by NULL data
  453. * parameter.
  454. *
  455. * @param ots OTS instance.
  456. * @param conn The connection that read object.
  457. * @param id Object ID.
  458. * @param data In: NULL once the read operations is completed.
  459. * Out: Next chunk of data to be sent.
  460. * @param len Remaining length requested by the client.
  461. * @param offset Object data offset.
  462. *
  463. * @return Data length to be sent via data parameter. This value
  464. * shall be smaller or equal to the len parameter.
  465. * @return Negative value in case of an error.
  466. */
  467. ssize_t (*obj_read)(struct bt_ots *ots, struct bt_conn *conn,
  468. uint64_t id, void **data, size_t len,
  469. off_t offset);
  470. /** @brief Object write callback
  471. *
  472. * This callback is called multiple times during the Object write
  473. * operation. OTS module will keep providing successive Object
  474. * fragments to the application until the write operation is
  475. * completed. The offset and length of each write fragment is
  476. * validated by the OTS module to be within the allocated size
  477. * of the object. The remaining length indicates data length
  478. * remaining to be written and will decrease each write iteration
  479. * until it reaches 0 in the last write fragment.
  480. *
  481. * @param ots OTS instance.
  482. * @param conn The connection that wrote object.
  483. * @param id Object ID.
  484. * @param data Next chunk of data to be written.
  485. * @param len Length of the current chunk of data in the buffer.
  486. * @param offset Object data offset.
  487. * @param rem Remaining length in the write operation.
  488. *
  489. * @return Number of bytes written in case of success, if the number
  490. * of bytes written does not match len, -EIO is returned to
  491. * the L2CAP layer.
  492. * @return A negative value in case of an error.
  493. * @return -EINPROGRESS has a special meaning and is unsupported at
  494. * the moment. It should not be returned.
  495. */
  496. ssize_t (*obj_write)(struct bt_ots *ots, struct bt_conn *conn, uint64_t id,
  497. const void *data, size_t len, off_t offset,
  498. size_t rem);
  499. /** @brief Object name written callback
  500. *
  501. * This callback is called when the object name is written.
  502. * This is a notification to the application that the object name
  503. * has been updated by the OTS service implementation.
  504. *
  505. * @param ots OTS instance.
  506. * @param conn The connection that wrote object name.
  507. * @param id Object ID.
  508. * @param name Object name.
  509. */
  510. void (*obj_name_written)(struct bt_ots *ots, struct bt_conn *conn,
  511. uint64_t id, const char *name);
  512. };
  513. /** @brief Descriptor for OTS initialization. */
  514. struct bt_ots_init {
  515. /* OTS features */
  516. struct bt_ots_feat features;
  517. /* Callbacks */
  518. struct bt_ots_cb *cb;
  519. };
  520. /** @brief Add an object to the OTS instance.
  521. *
  522. * This function adds an object to the OTS database. When the
  523. * object is being added, a callback obj_created() is called
  524. * to notify the user about a new object ID.
  525. *
  526. * @param ots OTS instance.
  527. * @param obj_init Meta data of the object.
  528. *
  529. * @return 0 in case of success or negative value in case of error.
  530. */
  531. int bt_ots_obj_add(struct bt_ots *ots, struct bt_ots_obj_metadata *obj_init);
  532. /** @brief Delete an object from the OTS instance.
  533. *
  534. * This function deletes an object from the OTS database. When the
  535. * object is deleted a callback obj_deleted() is called
  536. * to notify the user about this event. At this point, it is possible
  537. * to free allocated buffer for object data.
  538. *
  539. * @param ots OTS instance.
  540. * @param id ID of the object to be deleted (uint48).
  541. *
  542. * @return 0 in case of success or negative value in case of error.
  543. */
  544. int bt_ots_obj_delete(struct bt_ots *ots, uint64_t id);
  545. /** @brief Get the service declaration attribute.
  546. *
  547. * This function is enabled for CONFIG_BT_OTS_SECONDARY_SVC configuration.
  548. * The first service attribute can be included in any other GATT service.
  549. *
  550. * @param ots OTS instance.
  551. *
  552. * @return The first OTS attribute instance.
  553. */
  554. void *bt_ots_svc_decl_get(struct bt_ots *ots);
  555. /** @brief Initialize the OTS instance.
  556. *
  557. * @param ots OTS instance.
  558. * @param ots_init OTS initialization descriptor.
  559. *
  560. * @return 0 in case of success or negative value in case of error.
  561. */
  562. int bt_ots_init(struct bt_ots *ots, struct bt_ots_init *ots_init);
  563. /** @brief Get a free instance of OTS from the pool.
  564. *
  565. * @return OTS instance in case of success or NULL in case of error.
  566. */
  567. struct bt_ots *bt_ots_free_instance_get(void);
  568. /** @brief Converts binary OTS Object ID to string.
  569. *
  570. * @param obj_id Object ID.
  571. * @param str Address of user buffer with enough room to store
  572. * formatted string containing binary Object ID.
  573. * @param len Length of data to be copied to user string buffer.
  574. * Refer to BT_OTS_OBJ_ID_STR_LEN about
  575. * recommended value.
  576. *
  577. * @return Number of successfully formatted bytes from binary ID.
  578. */
  579. static inline int bt_ots_obj_id_to_str(uint64_t obj_id, char *str, size_t len)
  580. {
  581. uint8_t id[6];
  582. sys_put_le48(obj_id, id);
  583. return snprintk(str, len, "0x%02X%02X%02X%02X%02X%02X",
  584. id[5], id[4], id[3], id[2], id[1], id[0]);
  585. }
  586. #ifdef __cplusplus
  587. }
  588. #endif
  589. /**
  590. * @}
  591. */
  592. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_OTS_H_ */