xref: /btstack/src/classic/goep_server.h (revision ce6f85e79d1d141c1b45dfa16b2671762457cbb4)
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 <stdint.h>
47 
48 #ifdef ENABLE_GOEP_L2CAP
49 #ifndef GOEP_SERVER_ERTM_BUFFER
50 #define GOEP_SERVER_ERTM_BUFFER 1000
51 #endif
52 #endif
53 
54 /* API_START */
55 
56 typedef enum {
57     GOEP_SERVER_IDLE,
58     GOEP_SERVER_W4_ACCEPT_REJECT,
59     GOEP_SERVER_W4_CONNECTED,
60     GOEP_SERVER_CONNECTED,
61     GOEP_SERVER_OUTGOING_BUFFER_RESERVED,
62 } goep_server_state_t;
63 
64 typedef enum {
65     GOEP_CONNECTION_RFCOMM = 0,
66     GOEP_CONNECTION_L2CAP,
67 } goep_connection_type_t;
68 
69 typedef struct {
70     // linked list - assert: first field
71     btstack_linked_item_t    item;
72 
73     btstack_packet_handler_t callback;
74     uint8_t  rfcomm_channel;
75     uint16_t l2cap_psm;
76 } goep_server_service_t;
77 
78 // type
79 typedef struct {
80     // linked list - assert: first field
81     btstack_linked_item_t    item;
82 
83     uint16_t goep_cid;
84     goep_server_state_t     state;
85     goep_connection_type_t  type;
86     uint16_t bearer_cid;
87     uint16_t bearer_mtu;
88     btstack_packet_handler_t callback;
89     uint32_t obex_connection_id;
90 
91 #ifdef ENABLE_GOEP_L2CAP
92     uint8_t ertm_buffer[GOEP_SERVER_ERTM_BUFFER];
93 #endif
94 
95 } goep_server_connection_t;
96 
97 /**
98  * Init GOEP Server
99  */
100 void goep_server_init(void);
101 
102 
103 /**
104  * Register OBEX Service
105  * @param packet_handler
106  * @param rfcomm_channel
107  * @param rfcomm_max_frame_size
108  * @param l2cap_psm
109  * @param l2cap_mtu
110  * @param security_level
111  * @return status
112  */
113 uint8_t goep_server_register_service(btstack_packet_handler_t packet_handler, uint8_t rfcomm_channel, uint16_t rfcomm_max_frame_size,
114                 uint16_t l2cap_psm, uint16_t l2cap_mtu, gap_security_level_t security_level);
115 
116 /**
117  * @brief Accepts incoming GOEP connection.
118  * @param goep_cid
119  * @return status
120  */
121 uint8_t goep_server_accept_connection(uint16_t goep_cid);
122 
123 /**
124  * @brief Deny incoming GOEP connection.
125  * @param goep_cid
126  * @return status
127  */
128 uint8_t goep_server_decline_connection(uint16_t goep_cid);
129 
130 /**
131  * @brief Get max size of GOEP message
132  * @param goep_cid
133  * @return size in bytes or 0
134  */
135 uint16_t goep_server_response_get_max_message_size(uint16_t goep_cid);
136 
137 /**
138  * Request GOEP_SUBEVENT_CAN_SEND_NOW
139  * @param goep_cid
140  * @return status
141  */
142 uint8_t goep_server_request_can_send_now(uint16_t goep_cid);
143 
144 /**
145  * @brief Start Connect response
146  * @param goep_cid
147  * @param obex_version_number
148  * @param flags
149  * @param maximum_obex_packet_length
150  * @return status
151  */
152 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);
153 
154 /**
155  * @brief Start General response
156  * @note response code is set by goep_server_execute
157  * @param goep_cid
158  * @param opcode
159  * @return status
160  */
161 uint8_t goep_server_response_create_general(uint16_t goep_cid);
162 
163 /**
164  * @brief Add who header to current response
165  * @param goep_cid
166  * @param who 16 bytes
167  * @return status
168  */
169 uint8_t goep_server_header_add_who(uint16_t goep_cid, const uint8_t * who);
170 
171 /**
172  * @brief Add end_of_body header to current response
173  * @param goep_cid
174  * @param end_of_body
175  * @param length
176  * @return status
177  */
178 uint8_t goep_server_header_add_end_of_body(uint16_t goep_cid, const uint8_t * end_of_body, uint16_t length);
179 
180 /**
181  * @brief Add SRM ENABLE header to current response
182  * @param goep_cid
183  * @return
184  */
185 uint8_t goep_server_header_add_srm_enable(uint16_t goep_cid);
186 
187 
188 /**
189  * @brief Add application parameters header to current request
190  * @param goep_cid
191  * @param data
192  * @param length
193  * @return
194  */
195 uint8_t goep_server_header_add_application_parameters(uint16_t goep_cid, const uint8_t * data, uint16_t length);
196 
197 /**
198  * @brief Send prepared response
199  * @param goep_cid
200  * @param response_code
201  * @return status
202  */
203 uint8_t goep_server_execute(uint16_t goep_cid, uint8_t response_code);
204 
205 /**
206  * De-Init
207  */
208 void geop_server_deinit(void);
209 
210 /* API_END */
211 
212 #if defined __cplusplus
213 }
214 #endif
215 #endif
216