aboutsummaryrefslogtreecommitdiff
path: root/le_audio/system/bt/bta/csip/bta_csip_api.cc
blob: 73a9054c9667a37c8d347608e4dc424ea57d5340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/******************************************************************************
 *
 *  Copyright 2021 The Android Open Source Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/


/******************************************************************************
 *
 *  This file contains the CSIP API in the subsystem of BTA.
 *
 ******************************************************************************/

#define LOG_TAG "bt_bta_csip"

#include <base/bind.h>
#include <base/bind_helpers.h>
#include <base/callback.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bta_csip_api.h"
#include "bta_csip_int.h"
#include "bta_gatt_queue.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"

/*****************************************************************************
 *  Constants
 ****************************************************************************/
static const tBTA_SYS_REG bta_csip_reg = {bta_csip_hdl_event, BTA_CsipDisable};

/*********************************************************************************
 *
 * Function         BTA_RegisterCsipApp
 *
 * Description      This function is called to register application or module to
 *                  to register with CSIP for using CSIP functionalities.
 *
 * Parameters       p_csip_cb: callback to be received in registering app when
 *                             required CSIP operation is completed.
 *                  reg_cb   : callback when app/module is registered with CSIP.
 *
 * Returns          None
 *
 *********************************************************************************/
void BTA_RegisterCsipApp(tBTA_CSIP_CBACK* p_csip_cb,
                              BtaCsipAppRegisteredCb reg_cb) {
  do_in_bta_thread(FROM_HERE, base::Bind(&bta_csip_app_register, Uuid::GetRandom(),
                                         p_csip_cb, std::move(reg_cb)));
}

/*********************************************************************************
 *
 * Function         BTA_UnregisterCsipApp
 *
 * Description      This function is called to unregister application or module.
 *
 * Parameters       app_id: id of the app/module that needs to be unregistered.
 *
 * Returns          None
 *
 *********************************************************************************/

void BTA_UnregisterCsipApp(uint8_t app_id) {
  do_in_bta_thread(FROM_HERE, base::Bind(&bta_csip_app_unregister, app_id));
}

/*********************************************************************************
 *
 * Function         BTA_CsipSetLockValue
 *
 * Description      This function is called to request or release lock for the
 *                  coordinated set.
 *
 * Parameters       lock_param: parameters to acquire or release lock.
 *                                (tBTA_SET_LOCK_PARAMS).
 *
 * Returns          None
 *
 *********************************************************************************/
void BTA_CsipSetLockValue(tBTA_SET_LOCK_PARAMS lock_params) {
  tBTA_CSIP_LOCK_PARAMS* p_buf =
      (tBTA_CSIP_LOCK_PARAMS*)osi_calloc(sizeof(tBTA_CSIP_LOCK_PARAMS));

  p_buf->hdr.event = BTA_CSIP_SET_LOCK_VALUE_EVT;
  p_buf->lock_req = lock_params;

  bta_sys_sendmsg(p_buf);
}

/*********************************************************************************
 *
 * Function         BTA_CsipGetCoordinatedSet
 *
 * Description      This function is called to fetch details of the coordinated set.
 *
 * Parameters       set_id: identifier of the coordinated set whose details are
 *                          required to be fetched.
 *
 * Returns          tBTA_CSIP_CSET (containing details of coordinated set).
 *
 *********************************************************************************/
tBTA_CSIP_CSET BTA_CsipGetCoordinatedSet(uint8_t set_id) {
  APPL_TRACE_DEBUG("%s: set_id = %d", __func__, set_id);
  return bta_csip_get_coordinated_set(set_id);
}

/*********************************************************************************
 *
 * Function         BTA_CsipSetLockValue
 *
 * Description      This function is called to request or release lock for the
 *                  coordinated set.
 *
 * Parameters       None.
 *
 * Returns          vector<tBTIF_CSIP_CSET>: (all discovered coordinated set)
 *
 *********************************************************************************/
std::vector<tBTA_CSIP_CSET> BTA_CsipGetDiscoveredSets() {
  return bta_csip_cb.csets;
}

/*********************************************************************************
 *
 * Function         BTA_CsipConnect
 *
 * Description      This function is called to establish GATT Connection.
 *
 * Parameters       bd_addr : Address of the remote device.
 *
 * Returns          None.
 *
 *********************************************************************************/
void BTA_CsipConnect (uint8_t app_id, const RawAddress& bd_addr) {
  tBTA_CSIP_API_CONN* p_buf =
    (tBTA_CSIP_API_CONN*)osi_calloc(sizeof(tBTA_CSIP_API_CONN));
  p_buf->hdr.event = BTA_CSIP_API_OPEN_EVT;
  p_buf->bd_addr = bd_addr;
  p_buf->app_id = app_id;

  bta_sys_sendmsg(p_buf);
}

/*********************************************************************************
 *
 * Function         BTA_CsipConnect
 *
 * Description      This function is called to establish GATT Connection.
 *
 * Parameters       bd_addr : Address of the remote device.
 *
 * Returns          None.
 *
 *********************************************************************************/
void BTA_CsipDisconnect (uint8_t app_id, const RawAddress& bd_addr) {
  tBTA_CSIP_API_CONN* p_buf =
    (tBTA_CSIP_API_CONN*)osi_calloc(sizeof(tBTA_CSIP_API_CONN));
  p_buf->hdr.event = BTA_CSIP_API_CLOSE_EVT;
  p_buf->bd_addr = bd_addr;
  p_buf->app_id = app_id;

  bta_sys_sendmsg(p_buf);
}

/*********************************************************************************
 *
 * Function         BTA_CsipFindCsisInstance
 *
 * Description      This function is called to find presence of CSIS service on
 *                  remote device.
 *
 * Parameters       coon_id : Connection ID of the GATT Connection at DM Layer.
 *
 * Returns          None.
 *
 *********************************************************************************/
void BTA_CsipFindCsisInstance(uint16_t conn_id, tGATT_STATUS status,
                              RawAddress& bd_addr) {
  APPL_TRACE_DEBUG("%s ", __func__);

  tBTA_CSIP_DISC_SET* p_buf =
    (tBTA_CSIP_DISC_SET*)osi_calloc(sizeof(tBTA_CSIP_DISC_SET));
  p_buf->hdr.event = BTA_CSIP_DISC_CMPL_EVT;
  p_buf->conn_id = conn_id;
  p_buf->status = status;
  p_buf->addr = bd_addr;

  bta_sys_sendmsg(p_buf);
}

/*********************************************************************************
 *
 * Function         BTA_CsipInit
 *
 * Description      This function is invoked to initialize CSIP in BTA layer.
 *
 * Parameters       None.
 *
 * Returns          None.
 *
 *********************************************************************************/
void BTA_CsipEnable(tBTA_CSIP_CBACK *p_cback) {
  tBTA_CSIP_ENABLE* p_buf =
    (tBTA_CSIP_ENABLE*)osi_calloc(sizeof(tBTA_CSIP_ENABLE));

  /* register with BTA system manager */
  bta_sys_register(BTA_ID_GROUP, &bta_csip_reg);

  p_buf->hdr.event = BTA_CSIP_API_ENABLE_EVT;
  p_buf->p_cback = p_cback;

  bta_sys_sendmsg(p_buf);
}

/*********************************************************************************
 *
 * Function         BTA_CsipDisable
 *
 * Description      This function is called for deinitialization.
 *
 * Parameters       None.
 *
 * Returns          None.
 *
 *********************************************************************************/
void BTA_CsipDisable() {
  tBTA_CSIP_ENABLE* p_buf =
    (tBTA_CSIP_ENABLE*)osi_calloc(sizeof(tBTA_CSIP_ENABLE));

  p_buf->hdr.event = BTA_CSIP_API_DISABLE_EVT;

  bta_sys_sendmsg(p_buf);
}

/*********************************************************************************
 *
 * Function         BTA_CsipRemoveUnpairedSetMember
 *
 * Description      This function is called when a given set member is unpaired.
 *
 * Parameters       addr: BD Address of the set member.
 *
 * Returns          None.
 *
 *********************************************************************************/
void BTA_CsipRemoveUnpairedSetMember(RawAddress addr) {
  do_in_bta_thread(FROM_HERE, base::Bind(&bta_csip_remove_set_member, addr));
}

/*********************************************************************************
 *
 * Function         BTA_CsipGetDeviceSetId
 *
 * Description      This API is used to get set id of the remote device.
 *
 * Parameters       addr: BD Address of the set member.
 *                  uuid: UUID of the service which includes CSIS service.
 *
 * Returns          None.
 *
 *********************************************************************************/
uint8_t BTA_CsipGetDeviceSetId(RawAddress addr, bluetooth::Uuid uuid) {
  for (tBTA_CSIP_CSET cset: bta_csip_cb.csets) {
    for (RawAddress bd_addr: cset.set_members) {
      if (bd_addr == addr && (cset.p_srvc_uuid == uuid)) {
        return cset.set_id;
      }
    }
  }

  return BTA_MAX_SUPPORTED_SETS;
}