• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typedef enum {
42   BTSOCK_ERROR_NONE = 0,
43   BTSOCK_ERROR_SERVER_START_FAILURE = 1,
44   BTSOCK_ERROR_CLIENT_INIT_FAILURE = 2,
45   BTSOCK_ERROR_LISTEN_FAILURE = 3,
46   BTSOCK_ERROR_CONNECTION_FAILURE = 4,
47   BTSOCK_ERROR_OPEN_FAILURE = 5,
48   BTSOCK_ERROR_OFFLOAD_SERVER_NOT_ACCEPTING = 6,
49   BTSOCK_ERROR_OFFLOAD_HAL_OPEN_FAILURE = 7,
50   BTSOCK_ERROR_SEND_TO_APP_FAILURE = 8,
51   BTSOCK_ERROR_RECEIVE_DATA_FAILURE = 9,
52   BTSOCK_ERROR_READ_SIGNALED_FAILURE = 10,
53   BTSOCK_ERROR_WRITE_SIGNALED_FAILURE = 11,
54   BTSOCK_ERROR_SEND_SCN_FAILURE = 12,
55   BTSOCK_ERROR_SCN_ALLOCATION_FAILURE = 13,
56   BTSOCK_ERROR_ADD_SDP_FAILURE = 14,
57   BTSOCK_ERROR_SDP_DISCOVERY_FAILURE = 15,
58 } btsock_error_code_t;
59 
60 /**
61  * Data path used for Bluetooth socket communication.
62  *
63  * NOTE: The values must be same as:
64  *    - BluetoothSocketSettings.DATA_PATH_NO_OFFLOAD = 0
65  *    - BluetoothSocketSettings.DATA_PATH_HARDWARE_OFFLOAD = 1
66  */
67 typedef enum {
68   BTSOCK_DATA_PATH_NO_OFFLOAD = 0,
69   BTSOCK_DATA_PATH_HARDWARE_OFFLOAD = 1,
70 } btsock_data_path_t;
71 
72 /** Represents the standard BT SOCKET interface. */
73 typedef struct {
74   int16_t size;
75   RawAddress bd_addr;
76   int channel;
77   int status;
78 
79   // The writer must make writes using a buffer of this maximum size
80   // to avoid loosing data. (L2CAP only)
81   uint16_t max_tx_packet_size;
82 
83   // The reader must read using a buffer of at least this size to avoid
84   // loosing data. (L2CAP only)
85   uint16_t max_rx_packet_size;
86 
87   // The connection uuid. (L2CAP only)
88   uint64_t conn_uuid_lsb;
89   uint64_t conn_uuid_msb;
90 
91   // Socket ID in connected state
92   uint64_t socket_id;
93 } __attribute__((packed)) sock_connect_signal_t;
94 
95 typedef struct {
96   uint16_t size;
97   uint16_t is_accepting;
98 } __attribute__((packed)) sock_accept_signal_t;
99 
100 typedef struct {
101   /** set to size of this struct*/
102   size_t size;
103 
104   /**
105    * Listen to a RFCOMM UUID or channel. It returns the socket fd from which
106    * btsock_connect_signal can be read out when a remote device connected.
107    * If neither a UUID nor a channel is provided, a channel will be allocated
108    * and a service record can be created providing the channel number to
109    * create_sdp_record(...) in bt_sdp.
110    * The callingUid is the UID of the application which is requesting the
111    * socket. This is used for traffic accounting purposes.
112    */
113   bt_status_t (*listen)(btsock_type_t type, const char* service_name,
114                         const bluetooth::Uuid* service_uuid, int channel, int* sock_fd, int flags,
115                         int callingUid, btsock_data_path_t data_path, const char* socket_name,
116                         uint64_t hub_id, uint64_t endpoint_id, int max_rx_packet_size);
117 
118   /**
119    * Connect to a RFCOMM UUID channel of remote device, It returns the socket fd
120    * from which the btsock_connect_signal and a new socket fd to be accepted can
121    * be read out when connected. The callingUid is the UID of the application
122    * which is requesting the socket. This is used for traffic accounting
123    * purposes.
124    */
125   bt_status_t (*connect)(const RawAddress* bd_addr, btsock_type_t type, const bluetooth::Uuid* uuid,
126                          int channel, int* sock_fd, int flags, int callingUid,
127                          btsock_data_path_t data_path, const char* socket_name, uint64_t hub_id,
128                          uint64_t endpoint_id, int max_rx_packet_size);
129 
130   /**
131    * Set the LE Data Length value to this connected peer to the
132    * maximum supported by this BT controller. This command
133    * suggests to the BT controller to set its maximum transmission
134    * packet size.
135    */
136   void (*request_max_tx_data_length)(const RawAddress& bd_addr);
137 
138   /**
139    * Send control parameters to the peer. So far only for qualification use.
140    * RFCOMM layer starts the control request only when it is the client.
141    * This API allows the host to start the control request while it works as an
142    * RFCOMM server.
143    */
144   bt_status_t (*control_req)(uint8_t dlci, const RawAddress& bd_addr, uint8_t modem_signal,
145                              uint8_t break_signal, uint8_t discard_buffers,
146                              uint8_t break_signal_seq, bool fc);
147 
148   /**
149    * Disconnect all RFCOMM and L2CAP socket connections with the associated
150    * device address.
151    */
152   bt_status_t (*disconnect_all)(const RawAddress* bd_addr);
153 
154   /**
155    * Get L2CAP local channel ID with the associated connection uuid.
156    */
157   bt_status_t (*get_l2cap_local_cid)(bluetooth::Uuid& conn_uuid, uint16_t* cid);
158 
159   /**
160    * Get L2CAP remote channel ID with the associated connection uuid.
161    */
162   bt_status_t (*get_l2cap_remote_cid)(bluetooth::Uuid& conn_uuid, uint16_t* cid);
163 } btsock_interface_t;
164 
165 __END_DECLS
166 
167 #if __has_include(<bluetooth/log.h>)
168 #include <bluetooth/log.h>
169 
170 namespace std {
171 template <>
172 struct formatter<btsock_type_t> : enum_formatter<btsock_type_t> {};
173 
174 template <>
175 struct formatter<btsock_data_path_t> : enum_formatter<btsock_data_path_t> {};
176 }  // namespace std
177 
178 #endif  // __has_include(<bluetooth/log.h>)
179