1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef _L2CAP_CLIENT_H_
20 #define _L2CAP_CLIENT_H_
21 #if (defined(L2CAP_CLIENT_INCLUDED) && L2CAP_CLIENT_INCLUDED == TRUE)
22 
23 //#include <hardware/bluetooth.h>
24 #include <stdbool.h>
25 #include <stdint.h>
26 
27 typedef struct buffer_t buffer_t;
28 typedef struct l2cap_client_t l2cap_client_t;
29 
30 typedef struct {
31     void (*connected)(l2cap_client_t *client, void *context);
32     void (*disconnected)(l2cap_client_t *client, void *context);
33     void (*read_ready)(l2cap_client_t *client, buffer_t *packet, void *context);
34     void (*write_ready)(l2cap_client_t *client, void *context);
35 } l2cap_client_callbacks_t;
36 
37 // Returns a new buffer with enough space for |size| bytes of L2CAP payload.
38 // |size| must be greater than zero. This function returns NULL if the buffer
39 // could not be allocated. The returned buffer must be freed with |buffer_free|
40 // when it is no longer needed.
41 buffer_t *l2cap_buffer_new(size_t size);
42 
43 // Creates and returns a new L2CAP client object. |callbacks| must not be NULL and
44 // must specify a set of functions that should be called back when events occur
45 // on the L2CAP connection. |context| may be NULL and will be passed as the argument
46 // to all callbacks in |l2cap_client_callbacks_t|. The returned object must be freed
47 // with |l2cap_client_free|.
48 l2cap_client_t *l2cap_client_new(const l2cap_client_callbacks_t *callbacks, void *context);
49 
50 // Frees the L2CAP client object allocated with |l2cap_client_new|. |client| may be NULL.
51 void l2cap_client_free(l2cap_client_t *client);
52 
53 // Attempts to connect the |client| to a peer device specified by |remote_bdaddr|
54 // using the |psm| protocol specifier. This function returns true if the connect
55 // operation could be started and will indicate completion with either a 'connected'
56 // callback (success) or a 'disconnected' callback (failure).
57 //
58 // This function must not be called while a connect operation is in progress or
59 // while |l2cap_client_is_connected|. |client| and |remote_bdaddr| must not be NULL.
60 // |psm| must be greater than zero.
61 bool l2cap_client_connect(l2cap_client_t *client, const bt_bdaddr_t *remote_bdaddr, uint16_t psm);
62 
63 // Disconnects a connected |client|. This function is asynchronous and idempotent. It
64 // will indicate completion with a 'disconnected' callback. |client| must not be NULL.
65 void l2cap_client_disconnect(l2cap_client_t *client);
66 
67 // Returns true if |client| is connected and is ready to accept data written to it.
68 // |client| must not be NULL.
69 bool l2cap_client_is_connected(const l2cap_client_t *client);
70 
71 // Writes data contained in |packet| to a connected |client|. This function returns
72 // true if the packet was successfully queued for delivery, false if the client cannot
73 // accept more data at this time. If this function returns false, the caller must wait
74 // for the 'write_ready' callback to write additional data to the client. Neither
75 // |client| nor |packet| may be NULL.
76 bool l2cap_client_write(l2cap_client_t *client, buffer_t *packet);
77 
78 #endif  ///(defined(L2CAP_CLIENT_INCLUDED) && L2CAP_CLIENT_INCLUDED == TRUE)
79 
80 #endif /*_L2CAP_CLIENT_H_*/
81