1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef BT_GATT_OTS_OACP_H_
8 #define BT_GATT_OTS_OACP_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 /* Types of Object Action Control Point Procedures. */
20 enum bt_gatt_ots_oacp_proc_type {
21 	/** Create object.*/
22 	BT_GATT_OTS_OACP_PROC_CREATE        = 0x01,
23 	/** Delete object.*/
24 	BT_GATT_OTS_OACP_PROC_DELETE        = 0x02,
25 	/** Calculate Checksum.*/
26 	BT_GATT_OTS_OACP_PROC_CHECKSUM_CALC = 0x03,
27 	/** Execute Object.*/
28 	BT_GATT_OTS_OACP_PROC_EXECUTE       = 0x04,
29 	/** Read object.*/
30 	BT_GATT_OTS_OACP_PROC_READ          = 0x05,
31 	/** Write object.*/
32 	BT_GATT_OTS_OACP_PROC_WRITE         = 0x06,
33 	/** Abort object.*/
34 	BT_GATT_OTS_OACP_PROC_ABORT         = 0x07,
35 	/** Procedure response.*/
36 	BT_GATT_OTS_OACP_PROC_RESP          = 0x60
37 };
38 
39 /* Object Action Control Point return codes. */
40 enum bt_gatt_ots_oacp_res_code {
41 	/** Success.*/
42 	BT_GATT_OTS_OACP_RES_SUCCESS        = 0x01,
43 	/** Not supported*/
44 	BT_GATT_OTS_OACP_RES_OPCODE_NOT_SUP = 0x02,
45 	/** Invalid parameter*/
46 	BT_GATT_OTS_OACP_RES_INV_PARAM      = 0x03,
47 	/** Insufficient resources.*/
48 	BT_GATT_OTS_OACP_RES_INSUFF_RES     = 0x04,
49 	/** Invalid object.*/
50 	BT_GATT_OTS_OACP_RES_INV_OBJ        = 0x05,
51 	/** Channel unavailable.*/
52 	BT_GATT_OTS_OACP_RES_CHAN_UNAVAIL   = 0x06,
53 	/** Unsupported procedure.*/
54 	BT_GATT_OTS_OACP_RES_UNSUP_TYPE     = 0x07,
55 	/** Procedure not permitted.*/
56 	BT_GATT_OTS_OACP_RES_NOT_PERMITTED  = 0x08,
57 	/** Object locked.*/
58 	BT_GATT_OTS_OACP_RES_OBJ_LOCKED     = 0x09,
59 	/** Operation Failed.*/
60 	BT_GATT_OTS_OACP_RES_OPER_FAILED    = 0x0A
61 };
62 
63 #define BT_GATT_OTS_OACP_PROC_WRITE_MODE_TRUNC 1
64 
65 #define BT_GATT_OTS_OACP_PROC_WRITE_MODE_GET_TRUNC(mode) \
66 	((mode) & BIT(BT_GATT_OTS_OACP_PROC_WRITE_MODE_TRUNC))
67 
68 #define BT_GATT_OTS_OACP_PROC_WRITE_MODE_GET_RFU(mode) \
69 	((mode) & ~BIT(BT_GATT_OTS_OACP_PROC_WRITE_MODE_TRUNC))
70 
71 /* Object Action Control Point procedure definition. */
72 struct bt_gatt_ots_oacp_proc {
73 	enum bt_gatt_ots_oacp_proc_type type;
74 	union {
75 		struct bt_gatt_ots_oacp_create_params {
76 			uint32_t size;
77 			struct bt_ots_obj_type type;
78 		} create_params;
79 		struct bt_gatt_ots_oacp_cs_calc_params {
80 			uint32_t offset;
81 			uint32_t len;
82 		} cs_calc_params;
83 		struct bt_gatt_ots_oacp_read_params {
84 			uint32_t offset;
85 			uint32_t len;
86 		} read_params;
87 		struct bt_gatt_ots_oacp_write_params {
88 			uint32_t offset;
89 			uint32_t len;
90 			uint8_t mode;
91 		} write_params;
92 	};
93 };
94 
95 /* Size of the generic part of the Object Action Control Point create procedure */
96 #define BT_GATT_OTS_OACP_CREATE_GENERIC_PARAMS_SIZE 4
97 
98 /* Size of Object Action Control Point checksum calculation procedure */
99 #define BT_GATT_OTS_OACP_CS_CALC_PARAMS_SIZE 8
100 
101 /* Size of Object Action Control Point read procedure */
102 #define BT_GATT_OTS_OACP_READ_PARAMS_SIZE 8
103 
104 /* Size of Object Action Control Point write procedure */
105 #define BT_GATT_OTS_OACP_WRITE_PARAMS_SIZE 9
106 
107 ssize_t bt_gatt_ots_oacp_write(struct bt_conn *conn,
108 				   const struct bt_gatt_attr *attr,
109 				   const void *buf, uint16_t len,
110 				   uint16_t offset, uint8_t flags);
111 
112 void bt_gatt_ots_oacp_cfg_changed(const struct bt_gatt_attr *attr,
113 				      uint16_t value);
114 
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 #endif /* BT_GATT_OTS_OACP_H_ */
120