141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati/****************************************************************************** 241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * 341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * Copyright (C) 2014 Google, Inc. 441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * 541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * Licensed under the Apache License, Version 2.0 (the "License"); 641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * you may not use this file except in compliance with the License. 741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * You may obtain a copy of the License at: 841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * 941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * http://www.apache.org/licenses/LICENSE-2.0 1041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * 1141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * Unless required by applicable law or agreed to in writing, software 1241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * distributed under the License is distributed on an "AS IS" BASIS, 1341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * See the License for the specific language governing permissions and 1541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * limitations under the License. 1641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati * 1741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati ******************************************************************************/ 1841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 1941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati#pragma once 2041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 2141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati#include <hardware/bluetooth.h> 2241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati#include <stdbool.h> 2341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati#include <stdint.h> 2441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 2541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatitypedef struct buffer_t buffer_t; 2641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatitypedef struct l2cap_client_t l2cap_client_t; 2741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 2841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatitypedef struct { 2941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati void (*connected)(l2cap_client_t *client, void *context); 3041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati void (*disconnected)(l2cap_client_t *client, void *context); 3141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati void (*read_ready)(l2cap_client_t *client, buffer_t *packet, void *context); 3241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati void (*write_ready)(l2cap_client_t *client, void *context); 3341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati} l2cap_client_callbacks_t; 3441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 3541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Returns a new buffer with enough space for |size| bytes of L2CAP payload. 3641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// |size| must be greater than zero. This function returns NULL if the buffer 3741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// could not be allocated. The returned buffer must be freed with |buffer_free| 3841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// when it is no longer needed. 3941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatibuffer_t *l2cap_buffer_new(size_t size); 4041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 4141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Creates and returns a new L2CAP client object. |callbacks| must not be NULL and 4241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// must specify a set of functions that should be called back when events occur 4341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// on the L2CAP connection. |context| may be NULL and will be passed as the argument 4441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// to all callbacks in |l2cap_client_callbacks_t|. The returned object must be freed 4541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// with |l2cap_client_free|. 4641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatil2cap_client_t *l2cap_client_new(const l2cap_client_callbacks_t *callbacks, void *context); 4741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 4841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Frees the L2CAP client object allocated with |l2cap_client_new|. |client| may be NULL. 4941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavativoid l2cap_client_free(l2cap_client_t *client); 5041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 5141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Attempts to connect the |client| to a peer device specified by |remote_bdaddr| 5241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// using the |psm| protocol specifier. This function returns true if the connect 5341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// operation could be started and will indicate completion with either a 'connected' 5441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// callback (success) or a 'disconnected' callback (failure). 5541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// 5641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// This function must not be called while a connect operation is in progress or 5741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// while |l2cap_client_is_connected|. |client| and |remote_bdaddr| must not be NULL. 5841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// |psm| must be greater than zero. 5941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatibool l2cap_client_connect(l2cap_client_t *client, const bt_bdaddr_t *remote_bdaddr, uint16_t psm); 6041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 6141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Disconnects a connected |client|. This function is asynchronous and idempotent. It 6241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// will indicate completion with a 'disconnected' callback. |client| must not be NULL. 6341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavativoid l2cap_client_disconnect(l2cap_client_t *client); 6441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 6541e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Returns true if |client| is connected and is ready to accept data written to it. 6641e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// |client| must not be NULL. 6741e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatibool l2cap_client_is_connected(const l2cap_client_t *client); 6841e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati 6941e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// Writes data contained in |packet| to a connected |client|. This function returns 7041e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// true if the packet was successfully queued for delivery, false if the client cannot 7141e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// accept more data at this time. If this function returns false, the caller must wait 7241e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// for the 'write_ready' callback to write additional data to the client. Neither 7341e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavati// |client| nor |packet| may be NULL. 7441e319644db0b951c8c08fb3b50cbb0e1778b381Sharvil Nanavatibool l2cap_client_write(l2cap_client_t *client, buffer_t *packet); 75