1 /*
2  * Copyright (c) 2021 - 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef BT_OTS_DIR_LIST_INTERNAL_H_
8 #define BT_OTS_DIR_LIST_INTERNAL_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include <sys/types.h>
15 #include <zephyr/types.h>
16 #include <zephyr/bluetooth/gatt.h>
17 #include <zephyr/bluetooth/services/ots.h>
18 
19 /** Maximum size of the Directory Listing Object Record. Table 4.1 in the OTS spec. */
20 #define DIR_LIST_OBJ_RECORD_MIN_SIZE       13
21 #define DIR_LIST_OBJ_RECORD_MAX_SIZE       172
22 #define DIR_LIST_MAX_SIZE (DIR_LIST_OBJ_RECORD_MAX_SIZE * CONFIG_BT_OTS_MAX_OBJ_CNT)
23 
24 /** @brief Directory Listing Buffer Size
25  *
26  * In order to transmit the maximum size we choose CONFIG_BT_OTS_L2CAP_CHAN_TX_MTU
27  * as the buffer size. However, dir_list_object_encode function encodes one full
28  * object record at a time so the buffer must be a multiple of object record length.
29  */
30 #define OTS_DIR_LIST_BUFFER_SIZE (DIR_LIST_OBJ_RECORD_MAX_SIZE * \
31 	DIV_ROUND_UP(CONFIG_BT_OTS_L2CAP_CHAN_TX_MTU, DIR_LIST_OBJ_RECORD_MAX_SIZE))
32 
33 struct bt_ots_dir_list {
34 	struct net_buf_simple net_buf;
35 	struct bt_gatt_ots_object *dir_list_obj;
36 	off_t anchor_offset;
37 	struct bt_gatt_ots_object *anchor_object;
38 	uint8_t _content[OTS_DIR_LIST_BUFFER_SIZE];
39 };
40 
41 /** @brief Directory Listing record flag field. */
42 enum {
43 	/** Bit 0 Object Type UUID Size 0: 16bit 1: 128bit*/
44 	BT_OTS_DIR_LIST_FLAG_TYPE_128            = 0,
45 	/** Bit 1 Current Size Present*/
46 	BT_OTS_DIR_LIST_FLAG_CUR_SIZE            = 1,
47 	/** Bit 2 Allocated Size Present */
48 	BT_OTS_DIR_LIST_FLAG_ALLOC_SIZE          = 2,
49 	/** Bit 3 Object First-Created Present*/
50 	BT_OTS_DIR_LIST_FLAG_FIRST_CREATED       = 3,
51 	/** Bit 4 Object Last-Modified Present*/
52 	BT_OTS_DIR_LIST_FLAG_LAST_MODIFIED       = 4,
53 	/** Bit 5 Object Properties Present*/
54 	BT_OTS_DIR_LIST_FLAG_PROPERTIES          = 5,
55 	/** Bit 6 RFU*/
56 	BT_OTS_DIR_LIST_FLAG_RFU                 = 6,
57 	/** Bit 7 Extended Flags Present*/
58 	BT_OTS_DIR_LIST_FLAG_EXTENDED            = 7,
59 };
60 
61 /** @brief Set @ref BT_OTS_DIR_LIST_SET_FLAG_TYPE_128 flag.
62  *
63  *  @param flags Directory Listing Object flags.
64  */
65 #define BT_OTS_DIR_LIST_SET_FLAG_TYPE_128(flags) \
66 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_TYPE_128, 1)
67 
68 /** @brief Set @ref BT_OTS_DIR_LIST_FLAG_CUR_SIZE flag.
69  *
70  *  @param flags Directory Listing Object flags.
71  */
72 #define BT_OTS_DIR_LIST_SET_FLAG_CUR_SIZE(flags) \
73 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_CUR_SIZE, 1)
74 
75 /** @brief Set @ref BT_OTS_DIR_LIST_FLAG_ALLOC_SIZE flag.
76  *
77  *  @param flags Directory Listing Object flags.
78  */
79 #define BT_OTS_DIR_LIST_SET_FLAG_ALLOC_SIZE(flags) \
80 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_ALLOC_SIZE, 1)
81 
82 /** @brief Set @ref BT_OTS_DIR_LIST_FLAG_FIRST_CREATED flag.
83  *
84  *  @param flags Directory Listing Object flags.
85  */
86 #define BT_OTS_DIR_LIST_SET_FLAG_FIRST_CREATED(flags) \
87 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_FIRST_CREATED, 1)
88 
89 /** @brief Set @ref BT_OTS_DIR_LIST_FLAG_LAST_MODIFIED flag.
90  *
91  *  @param flags Directory Listing Object flags.
92  */
93 #define BT_OTS_DIR_LIST_SET_FLAG_LAST_MODIFIED(flags) \
94 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_LAST_MODIFIED, 1)
95 
96 /** @brief Set @ref BT_OTS_DIR_LIST_FLAG_PROPERTIES flag.
97  *
98  *  @param flags Directory Listing Object flags.
99  */
100 #define BT_OTS_DIR_LIST_SET_FLAG_PROPERTIES(flags) \
101 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_PROPERTIES, 1)
102 
103 /** @brief Set @ref BT_OTS_DIR_LIST_FLAG_EXTENDED flag.
104  *
105  *  @param flags Directory Listing Object flags.
106  */
107 #define BT_OTS_DIR_LIST_SET_FLAG_EXTENDED(flags) \
108 	WRITE_BIT((flags), BT_OTS_DIR_LIST_FLAG_EXTENDED, 1)
109 
110 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_TYPE_128 flag.
111  *
112  *  @param flags Directory Listing Object flags.
113  */
114 #define BT_OTS_DIR_LIST_GET_FLAG_TYPE_128(flags) \
115 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_TYPE_128))
116 
117 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_CUR_SIZE flag.
118  *
119  *  @param flags Directory Listing Object flags.
120  */
121 #define BT_OTS_DIR_LIST_GET_FLAG_CUR_SIZE(flags) \
122 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_CUR_SIZE))
123 
124 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_ALLOC_SIZE flag.
125  *
126  *  @param flags Directory Listing Object flags.
127  */
128 #define BT_OTS_DIR_LIST_GET_FLAG_ALLOC_SIZE(flags) \
129 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_ALLOC_SIZE))
130 
131 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_FIRST_CREATED flag.
132  *
133  *  @param flags Directory Listing Object flags.
134  */
135 #define BT_OTS_DIR_LIST_GET_FLAG_FIRST_CREATED(flags) \
136 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_FIRST_CREATED))
137 
138 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_LAST_MODIFIED flag.
139  *
140  *  @param flags Directory Listing Object flags.
141  */
142 #define BT_OTS_DIR_LIST_GET_FLAG_LAST_MODIFIED(flags) \
143 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_LAST_MODIFIED))
144 
145 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_PROPERTIES flag.
146  *
147  *  @param flags Directory Listing Object flags.
148  */
149 #define BT_OTS_DIR_LIST_GET_FLAG_PROPERTIES(flags) \
150 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_PROPERTIES))
151 
152 /** @brief Get @ref BT_OTS_DIR_LIST_GET_FLAG_EXTENDED flag.
153  *
154  *  @param flags Directory Listing Object flags.
155  */
156 #define BT_OTS_DIR_LIST_GET_FLAG_EXTENDED(flags) \
157 	((flags) & BIT(BT_OTS_DIR_LIST_FLAG_EXTENDED))
158 
159 void bt_ots_dir_list_selected(struct bt_ots_dir_list *dir_list, void *obj_manager,
160 			      struct bt_gatt_ots_object *cur_obj);
161 void bt_ots_dir_list_init(struct bt_ots_dir_list **dir_list, void *obj_manager);
162 ssize_t bt_ots_dir_list_content_get(struct bt_ots_dir_list *dir_list, void *obj_manager,
163 				    void **data, size_t len, off_t offset);
164 bool bt_ots_dir_list_is_idle(const struct bt_ots_dir_list *dir_list);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif /* BT_OTS_DIR_LIST_INTERNAL_H_ */
171