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 "bluetooth/uuid.h"
23 #include "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 /** Represents the standard BT SOCKET interface. */
42 typedef struct {
43   short size;
44   RawAddress bd_addr;
45   int channel;
46   int status;
47 
48   // The writer must make writes using a buffer of this maximum size
49   // to avoid loosing data. (L2CAP only)
50   unsigned short max_tx_packet_size;
51 
52   // The reader must read using a buffer of at least this size to avoid
53   // loosing data. (L2CAP only)
54   unsigned short max_rx_packet_size;
55 
56   // The connection uuid. (L2CAP only)
57   uint64_t conn_uuid_lsb;
58   uint64_t conn_uuid_msb;
59 } __attribute__((packed)) sock_connect_signal_t;
60 
61 typedef struct {
62   /** set to size of this struct*/
63   size_t size;
64 
65   /**
66    * Listen to a RFCOMM UUID or channel. It returns the socket fd from which
67    * btsock_connect_signal can be read out when a remote device connected.
68    * If neither a UUID nor a channel is provided, a channel will be allocated
69    * and a service record can be created providing the channel number to
70    * create_sdp_record(...) in bt_sdp.
71    * The callingUid is the UID of the application which is requesting the
72    * socket. This is used for traffic accounting purposes.
73    */
74   bt_status_t (*listen)(btsock_type_t type, const char* service_name,
75                         const bluetooth::Uuid* service_uuid, int channel,
76                         int* sock_fd, int flags, int callingUid);
77 
78   /**
79    * Connect to a RFCOMM UUID channel of remote device, It returns the socket fd
80    * from which the btsock_connect_signal and a new socket fd to be accepted can
81    * be read out when connected. The callingUid is the UID of the application
82    * which is requesting the socket. This is used for traffic accounting
83    * purposes.
84    */
85   bt_status_t (*connect)(const RawAddress* bd_addr, btsock_type_t type,
86                          const bluetooth::Uuid* uuid, int channel, int* sock_fd,
87                          int flags, int callingUid);
88 
89   /**
90    * Set the LE Data Length value to this connected peer to the
91    * maximum supported by this BT controller. This command
92    * suggests to the BT controller to set its maximum transmission
93    * packet size.
94    */
95   void (*request_max_tx_data_length)(const RawAddress& bd_addr);
96 
97   /**
98    * Send control parameters to the peer. So far only for qualification use.
99    * RFCOMM layer starts the control request only when it is the client.
100    * This API allows the host to start the control request while it works as an
101    * RFCOMM server.
102    */
103   bt_status_t (*control_req)(uint8_t dlci, const RawAddress& bd_addr,
104                              uint8_t modem_signal, uint8_t break_signal,
105                              uint8_t discard_buffers, uint8_t break_signal_seq,
106                              bool fc);
107 
108   /**
109    * Disconnect all RFCOMM and L2CAP socket connections with the associated
110    * device address.
111    */
112   bt_status_t (*disconnect_all)(const RawAddress* bd_addr);
113 
114   /**
115    * Get L2CAP local channel ID with the associated connection uuid.
116    */
117   bt_status_t (*get_l2cap_local_cid)(bluetooth::Uuid& conn_uuid, uint16_t* cid);
118 
119   /**
120    * Get L2CAP remote channel ID with the associated connection uuid.
121    */
122   bt_status_t (*get_l2cap_remote_cid)(bluetooth::Uuid& conn_uuid,
123                                       uint16_t* cid);
124 
125 } btsock_interface_t;
126 
127 __END_DECLS
128 
129 #if __has_include(<bluetooth/log.h>)
130 #include <bluetooth/log.h>
131 
132 namespace fmt {
133 template <>
134 struct formatter<btsock_type_t> : enum_formatter<btsock_type_t> {};
135 }  // namespace fmt
136 
137 #endif  // __has_include(<bluetooth/log.h>)
138