xref: /btstack/src/classic/goep_server.h (revision d5bacf302a965e1fda77ec7e9b7d953ea264c3e0)
1 /*
2  * Copyright (C) 2022 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #ifndef GOEP_SERVER_H
39 #define GOEP_SERVER_H
40 
41 #if defined __cplusplus
42 extern "C" {
43 #endif
44 
45 #include "btstack_config.h"
46 #include "gap.h"
47 #include <stdint.h>
48 
49 #ifdef ENABLE_GOEP_L2CAP
50 #ifndef GOEP_SERVER_ERTM_BUFFER
51 #define GOEP_SERVER_ERTM_BUFFER 2000
52 #endif
53 #endif
54 
55 /* API_START */
56 
57 typedef enum {
58     GOEP_SERVER_IDLE,
59     GOEP_SERVER_W4_ACCEPT_REJECT,
60     GOEP_SERVER_W4_CONNECTED,
61     GOEP_SERVER_CONNECTED,
62     GOEP_SERVER_OUTGOING_BUFFER_RESERVED,
63 } goep_server_state_t;
64 
65 typedef enum {
66     GOEP_CONNECTION_RFCOMM = 0,
67     GOEP_CONNECTION_L2CAP,
68 } goep_connection_type_t;
69 
70 typedef struct {
71     // linked list - assert: first field
72     btstack_linked_item_t    item;
73 
74     btstack_packet_handler_t callback;
75     uint8_t  rfcomm_channel;
76     uint16_t l2cap_psm;
77 } goep_server_service_t;
78 
79 // type
80 typedef struct {
81     // linked list - assert: first field
82     btstack_linked_item_t    item;
83 
84     uint16_t goep_cid;
85     goep_server_state_t     state;
86     goep_connection_type_t  type;
87     uint16_t bearer_cid;
88     uint16_t bearer_mtu;
89     btstack_packet_handler_t callback;
90     uint32_t obex_connection_id;
91 
92 #ifdef ENABLE_GOEP_L2CAP
93     uint8_t ertm_buffer[GOEP_SERVER_ERTM_BUFFER];
94 #endif
95 
96 } goep_server_connection_t;
97 
98 /**
99  * Init GOEP Server
100  */
101 void goep_server_init(void);
102 
103 
104 /**
105  * Register OBEX Service
106  * @param packet_handler
107  * @param rfcomm_channel
108  * @param rfcomm_max_frame_size
109  * @param l2cap_psm
110  * @param l2cap_mtu
111  * @param security_level
112  * @return status
113  */
114 uint8_t goep_server_register_service(btstack_packet_handler_t packet_handler, uint8_t rfcomm_channel, uint16_t rfcomm_max_frame_size,
115                 uint16_t l2cap_psm, uint16_t l2cap_mtu, gap_security_level_t security_level);
116 
117 /**
118  * @brief Accepts incoming GOEP connection.
119  * @param goep_cid
120  * @return status
121  */
122 uint8_t goep_server_accept_connection(uint16_t goep_cid);
123 
124 /**
125  * @brief Deny incoming GOEP connection.
126  * @param goep_cid
127  * @return status
128  */
129 uint8_t goep_server_decline_connection(uint16_t goep_cid);
130 
131 /**
132  * @brief Get max size of GOEP message
133  * @param goep_cid
134  * @return size in bytes or 0
135  */
136 uint16_t goep_server_response_get_max_message_size(uint16_t goep_cid);
137 
138 /**
139  * Request GOEP_SUBEVENT_CAN_SEND_NOW
140  * @param goep_cid
141  * @return status
142  */
143 uint8_t goep_server_request_can_send_now(uint16_t goep_cid);
144 
145 /**
146  * @brief Start Connect response
147  * @note Reserves outgoing packet buffer
148  * @note Must only be called after a 'can send now' check or event
149  * @note Asserts if packet buffer is already reserved
150  * @param goep_cid
151  * @param obex_version_number
152  * @param flags
153  * @param maximum_obex_packet_length
154  * @return status
155  */
156 uint8_t goep_server_response_create_connect(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length);
157 
158 /**
159  * @brief Start General response
160  * @note Reserves outgoing packet buffer
161  * @note Must only be called after a 'can send now' check or event
162  * @note Asserts if packet buffer is already reserved
163  * @note response code is set by goep_server_execute
164  * @param goep_cid
165  * @param opcode
166  * @return status
167  */
168 uint8_t goep_server_response_create_general(uint16_t goep_cid);
169 
170 /**
171  * @brief Add who header to current response
172  * @param goep_cid
173  * @param who 16 bytes
174  * @return status
175  */
176 uint8_t goep_server_header_add_who(uint16_t goep_cid, const uint8_t * who);
177 
178 /**
179  * @brief Add end_of_body header to current response
180  * @param goep_cid
181  * @param end_of_body
182  * @param length
183  * @return status
184  */
185 uint8_t goep_server_header_add_end_of_body(uint16_t goep_cid, const uint8_t * end_of_body, uint16_t length);
186 
187 /**
188  * @brief Add SRM ENABLE header to current response
189  * @param goep_cid
190  * @return
191  */
192 uint8_t goep_server_header_add_srm_enable(uint16_t goep_cid);
193 
194 /**
195  * @brief Add SRM ENABLE_WAIT header to current response
196  * @param goep_cid
197  * @return
198  */
199 uint8_t goep_server_header_add_srm_enable_wait(uint16_t goep_cid);
200 
201 /**
202  * @brief Add name header to current response
203  * @param goep_cid
204  * @param name \0 terminated string
205  * @return
206  */
207 uint8_t goep_server_header_add_name(uint16_t goep_cid, const char *name);
208 
209 /**
210  * @brief Add type header to current response
211  * @param goep_cid
212  * @param name \0 terminated string
213  * @return
214  */
215 uint8_t goep_server_header_add_type(uint16_t goep_cid, const char *type);
216 
217 /**
218  * @brief Add application parameters header to current request
219  * @param goep_cid
220  * @param data
221  * @param length
222  * @return
223  */
224 uint8_t goep_server_header_add_application_parameters(uint16_t goep_cid, const uint8_t * data, uint16_t length);
225 
226 /**
227  * @brief Send prepared response
228  * @param goep_cid
229  * @param response_code
230  * @return status
231  */
232 uint8_t goep_server_execute(uint16_t goep_cid, uint8_t response_code);
233 
234 /**
235  * @brief Disconnect client
236  * @param goep_cid
237  * @return status
238  */
239 uint8_t goep_server_disconnect(uint16_t goep_cid);
240 
241 /**
242  * De-Init
243  */
244 void geop_server_deinit(void);
245 
246 /* API_END */
247 
248 #if defined __cplusplus
249 }
250 #endif
251 #endif
252