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 #pragma once
20 
21 #include <hardware/bluetooth.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 
25 typedef struct buffer_t buffer_t;
26 typedef struct l2cap_client_t l2cap_client_t;
27 
28 typedef struct {
29   void (*connected)(l2cap_client_t *client, void *context);
30   void (*disconnected)(l2cap_client_t *client, void *context);
31   void (*read_ready)(l2cap_client_t *client, buffer_t *packet, void *context);
32   void (*write_ready)(l2cap_client_t *client, void *context);
33 } l2cap_client_callbacks_t;
34 
35 // Returns a new buffer with enough space for |size| bytes of L2CAP payload.
36 // |size| must be greater than zero. This function returns NULL if the buffer
37 // could not be allocated. The returned buffer must be freed with |buffer_free|
38 // when it is no longer needed.
39 buffer_t *l2cap_buffer_new(size_t size);
40 
41 // Creates and returns a new L2CAP client object. |callbacks| must not be NULL and
42 // must specify a set of functions that should be called back when events occur
43 // on the L2CAP connection. |context| may be NULL and will be passed as the argument
44 // to all callbacks in |l2cap_client_callbacks_t|. The returned object must be freed
45 // with |l2cap_client_free|.
46 l2cap_client_t *l2cap_client_new(const l2cap_client_callbacks_t *callbacks, void *context);
47 
48 // Frees the L2CAP client object allocated with |l2cap_client_new|. |client| may be NULL.
49 void l2cap_client_free(l2cap_client_t *client);
50 
51 // Attempts to connect the |client| to a peer device specified by |remote_bdaddr|
52 // using the |psm| protocol specifier. This function returns true if the connect
53 // operation could be started and will indicate completion with either a 'connected'
54 // callback (success) or a 'disconnected' callback (failure).
55 //
56 // This function must not be called while a connect operation is in progress or
57 // while |l2cap_client_is_connected|. |client| and |remote_bdaddr| must not be NULL.
58 // |psm| must be greater than zero.
59 bool l2cap_client_connect(l2cap_client_t *client, const bt_bdaddr_t *remote_bdaddr, uint16_t psm);
60 
61 // Disconnects a connected |client|. This function is asynchronous and idempotent. It
62 // will indicate completion with a 'disconnected' callback. |client| must not be NULL.
63 void l2cap_client_disconnect(l2cap_client_t *client);
64 
65 // Returns true if |client| is connected and is ready to accept data written to it.
66 // |client| must not be NULL.
67 bool l2cap_client_is_connected(const l2cap_client_t *client);
68 
69 // Writes data contained in |packet| to a connected |client|. This function returns
70 // true if the packet was successfully queued for delivery, false if the client cannot
71 // accept more data at this time. If this function returns false, the caller must wait
72 // for the 'write_ready' callback to write additional data to the client. Neither
73 // |client| nor |packet| may be NULL.
74 bool l2cap_client_write(l2cap_client_t *client, buffer_t *packet);
75