1 /*
2  * Copyright (C) 2015 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 <bluetooth/uuid.h>
20 #include <raw_address.h>
21 
22 #include "bluetooth.h"
23 
24 #define SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH 15
25 
26 __BEGIN_DECLS
27 
28 /**
29  * These events are handled by the state machine
30  */
31 typedef enum {
32   SDP_TYPE_RAW,         // Used to carry raw SDP search data for unknown UUIDs
33   SDP_TYPE_MAP_MAS,     // Message Access Profile - Server
34   SDP_TYPE_MAP_MNS,     // Message Access Profile - Client (Notification Server)
35   SDP_TYPE_PBAP_PSE,    // Phone Book Profile - Server
36   SDP_TYPE_PBAP_PCE,    // Phone Book Profile - Client
37   SDP_TYPE_OPP_SERVER,  // Object Push Profile
38   SDP_TYPE_SAP_SERVER,  // SIM Access Profile
39   SDP_TYPE_DIP,         // Device Identification Profile
40   SDP_TYPE_MPS          // Multi-Profile Specification
41 } bluetooth_sdp_types;
42 
43 typedef struct _bluetooth_sdp_hdr {
44   bluetooth_sdp_types type;
45   bluetooth::Uuid uuid;
46   uint32_t service_name_length;
47   char* service_name;
48   int32_t rfcomm_channel_number;
49   int32_t l2cap_psm;
50   int32_t profile_version;
51 } bluetooth_sdp_hdr;
52 
53 /**
54  * Some signals need additional pointers, hence we introduce a
55  * generic way to handle these pointers.
56  */
57 typedef struct _bluetooth_sdp_hdr_overlay {
58   bluetooth_sdp_types type;
59   bluetooth::Uuid uuid;
60   uint32_t service_name_length;
61   const char* service_name;
62   int32_t rfcomm_channel_number;
63   int32_t l2cap_psm;
64   int32_t profile_version;
65 
66   // User pointers, only used for some signals - see bluetooth_sdp_ops_record
67   int user1_ptr_len;
68   const uint8_t* user1_ptr;
69   int user2_ptr_len;
70   const uint8_t* user2_ptr;
71 } bluetooth_sdp_hdr_overlay;
72 
73 typedef struct _bluetooth_sdp_mas_record {
74   bluetooth_sdp_hdr_overlay hdr;
75   uint32_t mas_instance_id;
76   uint32_t supported_features;
77   uint32_t supported_message_types;
78 } bluetooth_sdp_mas_record;
79 
80 typedef struct _bluetooth_sdp_mns_record {
81   bluetooth_sdp_hdr_overlay hdr;
82   uint32_t supported_features;
83 } bluetooth_sdp_mns_record;
84 
85 typedef struct _bluetooth_sdp_pse_record {
86   bluetooth_sdp_hdr_overlay hdr;
87   uint32_t supported_features;
88   uint32_t supported_repositories;
89 } bluetooth_sdp_pse_record;
90 
91 typedef struct _bluetooth_sdp_pce_record {
92   bluetooth_sdp_hdr_overlay hdr;
93 } bluetooth_sdp_pce_record;
94 
95 typedef struct _bluetooth_sdp_ops_record {
96   bluetooth_sdp_hdr_overlay hdr;
97   int supported_formats_list_len;
98   uint8_t supported_formats_list[SDP_OPP_SUPPORTED_FORMATS_MAX_LENGTH];
99 } bluetooth_sdp_ops_record;
100 
101 typedef struct _bluetooth_sdp_sap_record {
102   bluetooth_sdp_hdr_overlay hdr;
103 } bluetooth_sdp_sap_record;
104 
105 typedef struct _bluetooth_sdp_dip_record {
106   bluetooth_sdp_hdr_overlay hdr;
107   uint16_t spec_id;
108   uint16_t vendor;
109   uint16_t vendor_id_source;
110   uint16_t product;
111   uint16_t version;
112   bool primary_record;
113 } bluetooth_sdp_dip_record;
114 
115 typedef struct _bluetooth_sdp_mps_record {
116   bluetooth_sdp_hdr_overlay hdr;
117   uint8_t supported_scenarios_mpsd[8];
118   uint8_t supported_scenarios_mpmd[8];
119   uint8_t supported_dependencies[2];
120 } bluetooth_sdp_mps_record;
121 
122 typedef union {
123   bluetooth_sdp_hdr_overlay hdr;
124   bluetooth_sdp_mas_record mas;
125   bluetooth_sdp_mns_record mns;
126   bluetooth_sdp_pse_record pse;
127   bluetooth_sdp_pce_record pce;
128   bluetooth_sdp_ops_record ops;
129   bluetooth_sdp_sap_record sap;
130   bluetooth_sdp_dip_record dip;
131   bluetooth_sdp_mps_record mps;
132 } bluetooth_sdp_record;
133 
134 /** Callback for SDP search */
135 typedef void (*btsdp_search_callback)(bt_status_t status,
136                                       const RawAddress& bd_addr,
137                                       const bluetooth::Uuid& uuid,
138                                       int num_records,
139                                       bluetooth_sdp_record* records);
140 
141 typedef struct {
142   /** Set to sizeof(btsdp_callbacks_t) */
143   size_t size;
144   btsdp_search_callback sdp_search_cb;
145 } btsdp_callbacks_t;
146 
147 typedef struct {
148   /** Set to size of this struct */
149   size_t size;
150 
151   /** Register BT SDP search callbacks */
152   bt_status_t (*init)(btsdp_callbacks_t* callbacks);
153 
154   /** Unregister BT SDP */
155   bt_status_t (*deinit)();
156 
157   /** Search for SDP records with specific uuid on remote device */
158   bt_status_t (*sdp_search)(RawAddress* bd_addr, const bluetooth::Uuid& uuid);
159 
160   /**
161    * Use listen in the socket interface to create rfcomm and/or l2cap PSM
162    * channels, (without UUID and service_name and set the BTSOCK_FLAG_NO_SDP
163    * flag in flags). Then use createSdpRecord to create the SDP record
164    * associated with the rfcomm/l2cap channels.
165    *
166    * Returns a handle to the SDP record, which can be parsed to
167    * remove_sdp_record.
168    *
169    * record           (in) The SDP record to create
170    * record_handle    (out)The corresponding record handle will be written to
171    * this pointer.
172    */
173   bt_status_t (*create_sdp_record)(bluetooth_sdp_record* record,
174                                    int* record_handle);
175 
176   /** Remove a SDP record created by createSdpRecord */
177   bt_status_t (*remove_sdp_record)(int sdp_handle);
178 } btsdp_interface_t;
179 
180 __END_DECLS
181 
182 #if __has_include(<bluetooth/log.h>)
183 #include <bluetooth/log.h>
184 
185 namespace fmt {
186 template <>
187 struct formatter<bluetooth_sdp_types> : enum_formatter<bluetooth_sdp_types> {};
188 }  // namespace fmt
189 
190 #endif  // __has_include(<bluetooth/log.h>)
191