1 /*
2  * Copyright (c) 2020-2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef BT_GATT_OTS_INTERNAL_H_
8 #define BT_GATT_OTS_INTERNAL_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include <zephyr/types.h>
15 #include "ots_l2cap_internal.h"
16 #include "ots_oacp_internal.h"
17 #include "ots_olcp_internal.h"
18 
19 /**
20  * Both OACP and OLCP have same max size of 7 bytes
21  *
22  * Table 3.10: Format of OACP Response Value
23  * OACP Response Value contains
24  * 1 octet Procedure code
25  * 1 octet Request op code
26  * 1 octet Result Code
27  * 4 octet CRC checksum (if present)
28  *
29  * Table 3.24: Format of the OLCP Response Value
30  * 1 octet Procedure code
31  * 1 octet Request op code
32  * 1 octet Result Code
33  * 0 or 4 octets Response Parameter
34  *
35  **/
36 #define OACP_OLCP_RES_MAX_SIZE	7
37 
38 #define BT_OTS_VALID_OBJ_ID(id) (IN_RANGE((id), BT_OTS_OBJ_ID_MIN, BT_OTS_OBJ_ID_MAX) || \
39 				 (id) == OTS_OBJ_ID_DIR_LIST)
40 
41 #define BT_OTS_SET_METADATA_REQ_NAME(metadata) \
42 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_NAME)
43 #define BT_OTS_SET_METADATA_REQ_TYPE(metadata) \
44 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_TYPE)
45 #define BT_OTS_SET_METADATA_REQ_SIZE(metadata) \
46 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_SIZE)
47 #define BT_OTS_SET_METADATA_REQ_CREATED(metadata) \
48 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_CREATED)
49 #define BT_OTS_SET_METADATA_REQ_MODIFIED(metadata) \
50 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_MODIFIED)
51 #define BT_OTS_SET_METADATA_REQ_ID(metadata) \
52 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_ID)
53 #define BT_OTS_SET_METADATA_REQ_PROPS(metadata) \
54 	((metadata) = (metadata) | BT_OTS_METADATA_REQ_PROPS)
55 
56 #define BT_OTS_GET_METADATA_REQ_NAME(metadata) \
57 	((metadata) & BT_OTS_METADATA_REQ_NAME)
58 #define BT_OTS_GET_METADATA_REQ_TYPE(metadata) \
59 	((metadata) & BT_OTS_METADATA_REQ_TYPE)
60 #define BT_OTS_GET_METADATA_REQ_SIZE(metadata) \
61 	((metadata) & BT_OTS_METADATA_REQ_SIZE)
62 #define BT_OTS_GET_METADATA_REQ_CREATED(metadata) \
63 	((metadata) & BT_OTS_METADATA_REQ_CREATED)
64 #define BT_OTS_GET_METADATA_REQ_MODIFIED(metadata) \
65 	((metadata) & BT_OTS_METADATA_REQ_MODIFIED)
66 #define BT_OTS_GET_METADATA_REQ_ID(metadata) \
67 	((metadata) & BT_OTS_METADATA_REQ_ID)
68 #define BT_OTS_GET_METADATA_REQ_PROPS(metadata) \
69 	((metadata) & BT_OTS_METADATA_REQ_PROPS)
70 
71 /**@brief OTS Attribute Protocol Application Error codes. */
72 enum bt_gatt_ots_att_err_codes {
73 	/** An attempt was made to write a value that is invalid or
74 	 *  not supported by this Server for a reason other than
75 	 *  the attribute permissions.
76 	 */
77 	BT_GATT_OTS_WRITE_REQUEST_REJECTED     = 0x80,
78 	/** An attempt was made to read or write to an Object Metadata
79 	 *  characteristic while the Current Object was an Invalid Object.
80 	 */
81 	BT_GATT_OTS_OBJECT_NOT_SELECTED        = 0x81,
82 	/** The Server is unable to service the Read Request or Write Request
83 	 *  because it exceeds the concurrency limit of the service.
84 	 */
85 	BT_GATT_OTS_CONCURRENCY_LIMIT_EXCEEDED = 0x82,
86 	/** The requested object name was rejected because
87 	 *  the name was already in use by an existing object on the Server.
88 	 */
89 	BT_GATT_OTS_OBJECT_NAME_ALREADY_EXISTS = 0x83,
90 };
91 
92 enum bt_gatt_ots_object_state_type {
93 	BT_GATT_OTS_OBJECT_IDLE_STATE,
94 
95 	BT_GATT_OTS_OBJECT_READ_OP_STATE,
96 
97 	BT_GATT_OTS_OBJECT_WRITE_OP_STATE,
98 };
99 
100 struct bt_gatt_ots_object_state {
101 	enum bt_gatt_ots_object_state_type type;
102 	union {
103 		struct bt_gatt_ots_object_read_op {
104 			struct bt_gatt_ots_oacp_read_params oacp_params;
105 			uint32_t sent_len;
106 		} read_op;
107 		struct bt_gatt_ots_object_write_op {
108 			struct bt_gatt_ots_oacp_write_params oacp_params;
109 			uint32_t recv_len;
110 		} write_op;
111 	};
112 };
113 
114 struct bt_gatt_ots_object {
115 	uint64_t id;
116 	struct bt_ots_obj_metadata metadata;
117 	struct bt_gatt_ots_object_state state;
118 };
119 
120 struct bt_gatt_ots_indicate {
121 	struct bt_gatt_indicate_params params;
122 	struct bt_gatt_attr attr;
123 	struct _bt_gatt_ccc ccc;
124 	bool is_enabled;
125 	struct k_work work;
126 	uint8_t res[OACP_OLCP_RES_MAX_SIZE];
127 };
128 
129 struct bt_ots {
130 	struct bt_ots_feat features;
131 	struct bt_gatt_ots_object *cur_obj;
132 	struct bt_gatt_service *service;
133 	struct bt_gatt_ots_indicate oacp_ind;
134 	struct bt_gatt_ots_indicate olcp_ind;
135 	struct bt_gatt_ots_l2cap l2cap;
136 	struct bt_ots_cb *cb;
137 	struct bt_ots_dir_list *dir_list;
138 	void *obj_manager;
139 };
140 
141 int bt_ots_obj_add_internal(struct bt_ots *ots, struct bt_conn *conn,
142 			    const struct bt_ots_obj_add_param *param,
143 			    struct bt_gatt_ots_object **obj);
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif /* BT_GATT_OTS_INTERNAL_H_ */
149