• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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
42 // and must specify a set of functions that should be called back when events
43 // occur on the L2CAP connection. |context| may be NULL and will be passed as
44 // the argument to all callbacks in |l2cap_client_callbacks_t|. The returned
45 // object must be freed with |l2cap_client_free|.
46 l2cap_client_t* l2cap_client_new(const l2cap_client_callbacks_t* callbacks,
47                                  void* context);
48 
49 // Frees the L2CAP client object allocated with |l2cap_client_new|. |client| may
50 // be NULL.
51 void l2cap_client_free(l2cap_client_t* client);
52 
53 // Attempts to connect the |client| to a peer device specified by
54 // |remote_bdaddr| using the |psm| protocol specifier. This function returns
55 // true if the connect operation could be started and will indicate completion
56 // with either a 'connected' callback (success) or a 'disconnected' callback
57 // (failure).
58 //
59 // This function must not be called while a connect operation is in progress or
60 // while |l2cap_client_is_connected|. |client| and |remote_bdaddr| must not be
61 // NULL. |psm| must be greater than zero.
62 bool l2cap_client_connect(l2cap_client_t* client,
63                           const RawAddress& remote_bdaddr, uint16_t psm);
64 
65 // Disconnects a connected |client|. This function is asynchronous and
66 // idempotent. It will indicate completion with a 'disconnected' callback.
67 // |client| must not be NULL.
68 void l2cap_client_disconnect(l2cap_client_t* client);
69 
70 // Returns true if |client| is connected and is ready to accept data written
71 // to it. |client| must not be NULL.
72 bool l2cap_client_is_connected(const l2cap_client_t* client);
73 
74 // Writes data contained in |packet| to a connected |client|. This function
75 // returns true if the packet was successfully queued for delivery, false if the
76 // client cannot accept more data at this time. If this function returns false,
77 // the caller must wait for the 'write_ready' callback to write additional data
78 // to the client. Neither |client| nor |packet| may be NULL.
79 bool l2cap_client_write(l2cap_client_t* client, buffer_t* packet);
80