1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stddef.h>
20 
21 #include "bluetooth.h"
22 #include "types/bluetooth/uuid.h"
23 #include "types/raw_address.h"
24 
25 __BEGIN_DECLS
26 
27 #define BTSOCK_FLAG_ENCRYPT 1
28 #define BTSOCK_FLAG_AUTH (1 << 1)
29 #define BTSOCK_FLAG_NO_SDP (1 << 2)
30 #define BTSOCK_FLAG_AUTH_MITM (1 << 3)
31 #define BTSOCK_FLAG_AUTH_16_DIGIT (1 << 4)
32 #define BTSOCK_FLAG_LE_COC (1 << 5)
33 
34 typedef enum {
35   BTSOCK_RFCOMM = 1,
36   BTSOCK_SCO = 2,
37   BTSOCK_L2CAP = 3,
38   BTSOCK_L2CAP_LE = 4
39 } btsock_type_t;
40 
41 /**
42  * Data path used for Bluetooth socket communication.
43  *
44  * NOTE: The values must be same as:
45  *    - BluetoothSocketSettings.DATA_PATH_NO_OFFLOAD = 0
46  *    - BluetoothSocketSettings.DATA_PATH_HARDWARE_OFFLOAD = 1
47  */
48 typedef enum {
49   BTSOCK_DATA_PATH_NO_OFFLOAD = 0,
50   BTSOCK_DATA_PATH_HARDWARE_OFFLOAD = 1,
51 } btsock_data_path_t;
52 
53 /** Represents the standard BT SOCKET interface. */
54 typedef struct {
55   int16_t size;
56   RawAddress bd_addr;
57   int channel;
58   int status;
59 
60   // The writer must make writes using a buffer of this maximum size
61   // to avoid loosing data. (L2CAP only)
62   uint16_t max_tx_packet_size;
63 
64   // The reader must read using a buffer of at least this size to avoid
65   // loosing data. (L2CAP only)
66   uint16_t max_rx_packet_size;
67 
68   // The connection uuid. (L2CAP only)
69   uint64_t conn_uuid_lsb;
70   uint64_t conn_uuid_msb;
71 
72   // Socket ID in connected state
73   uint64_t socket_id;
74 } __attribute__((packed)) sock_connect_signal_t;
75 
76 typedef struct {
77   uint16_t size;
78   uint16_t is_accepting;
79 } __attribute__((packed)) sock_accept_signal_t;
80 
81 typedef struct {
82   /** set to size of this struct*/
83   size_t size;
84 
85   /**
86    * Listen to a RFCOMM UUID or channel. It returns the socket fd from which
87    * btsock_connect_signal can be read out when a remote device connected.
88    * If neither a UUID nor a channel is provided, a channel will be allocated
89    * and a service record can be created providing the channel number to
90    * create_sdp_record(...) in bt_sdp.
91    * The callingUid is the UID of the application which is requesting the
92    * socket. This is used for traffic accounting purposes.
93    */
94   bt_status_t (*listen)(btsock_type_t type, const char* service_name,
95                         const bluetooth::Uuid* service_uuid, int channel, int* sock_fd, int flags,
96                         int callingUid, btsock_data_path_t data_path, const char* socket_name,
97                         uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size);
98 
99   /**
100    * Connect to a RFCOMM UUID channel of remote device, It returns the socket fd
101    * from which the btsock_connect_signal and a new socket fd to be accepted can
102    * be read out when connected. The callingUid is the UID of the application
103    * which is requesting the socket. This is used for traffic accounting
104    * purposes.
105    */
106   bt_status_t (*connect)(const RawAddress* bd_addr, btsock_type_t type, const bluetooth::Uuid* uuid,
107                          int channel, int* sock_fd, int flags, int callingUid,
108                          btsock_data_path_t data_path, const char* socket_name, uint64_t hub_id,
109                          uint64_t endpoint_id, int max_rx_packet_size);
110 
111   /**
112    * Set the LE Data Length value to this connected peer to the
113    * maximum supported by this BT controller. This command
114    * suggests to the BT controller to set its maximum transmission
115    * packet size.
116    */
117   void (*request_max_tx_data_length)(const RawAddress& bd_addr);
118 
119   /**
120    * Send control parameters to the peer. So far only for qualification use.
121    * RFCOMM layer starts the control request only when it is the client.
122    * This API allows the host to start the control request while it works as an
123    * RFCOMM server.
124    */
125   bt_status_t (*control_req)(uint8_t dlci, const RawAddress& bd_addr, uint8_t modem_signal,
126                              uint8_t break_signal, uint8_t discard_buffers,
127                              uint8_t break_signal_seq, bool fc);
128 
129   /**
130    * Disconnect all RFCOMM and L2CAP socket connections with the associated
131    * device address.
132    */
133   bt_status_t (*disconnect_all)(const RawAddress* bd_addr);
134 
135   /**
136    * Get L2CAP local channel ID with the associated connection uuid.
137    */
138   bt_status_t (*get_l2cap_local_cid)(bluetooth::Uuid& conn_uuid, uint16_t* cid);
139 
140   /**
141    * Get L2CAP remote channel ID with the associated connection uuid.
142    */
143   bt_status_t (*get_l2cap_remote_cid)(bluetooth::Uuid& conn_uuid, uint16_t* cid);
144 } btsock_interface_t;
145 
146 __END_DECLS
147 
148 #if __has_include(<bluetooth/log.h>)
149 #include <bluetooth/log.h>
150 
151 namespace std {
152 template <>
153 struct formatter<btsock_type_t> : enum_formatter<btsock_type_t> {};
154 
155 template <>
156 struct formatter<btsock_data_path_t> : enum_formatter<btsock_data_path_t> {};
157 }  // namespace std
158 
159 #endif  // __has_include(<bluetooth/log.h>)
160