1 /*
2 * Copyright 2021 HIMSA II K/S - www.himsa.com.
3 * Represented by EHIMA - www.ehima.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <base/bind.h>
19 #include <base/location.h>
20 #include <base/logging.h>
21 #include <hardware/bluetooth.h>
22 #include <hardware/bt_csis.h>
23
24 #include "bind_helpers.h"
25 #include "bta_csis_api.h"
26 #include "btif_common.h"
27 #include "btif_storage.h"
28 #include "stack/include/btu.h"
29
30 using base::Bind;
31 using base::Owned;
32 using base::Passed;
33 using base::Unretained;
34 using bluetooth::csis::ConnectionState;
35 using bluetooth::csis::CsisClientCallbacks;
36 using bluetooth::csis::CsisClientInterface;
37 using bluetooth::csis::CsisGroupLockStatus;
38
39 using bluetooth::csis::CsisClient;
40
41 namespace {
42 std::unique_ptr<CsisClientInterface> csis_client_instance;
43 class CsipSetCoordinatorServiceInterfaceImpl : public CsisClientInterface,
44 public CsisClientCallbacks {
45 ~CsipSetCoordinatorServiceInterfaceImpl() override = default;
46
Init(CsisClientCallbacks * callbacks)47 void Init(CsisClientCallbacks* callbacks) override {
48 DVLOG(2) << __func__;
49 this->callbacks_ = callbacks;
50
51 do_in_main_thread(
52 FROM_HERE,
53 Bind(&CsisClient::Initialize, this,
54 jni_thread_wrapper(FROM_HERE,
55 Bind(&btif_storage_load_bonded_csis_devices))));
56 }
57
Connect(const RawAddress & addr)58 void Connect(const RawAddress& addr) override {
59 DVLOG(2) << __func__ << " addr: " << addr;
60 do_in_main_thread(FROM_HERE, Bind(&CsisClient::Connect,
61 Unretained(CsisClient::Get()), addr));
62 }
63
Disconnect(const RawAddress & addr)64 void Disconnect(const RawAddress& addr) override {
65 DVLOG(2) << __func__ << " addr: " << addr;
66 do_in_main_thread(FROM_HERE, Bind(&CsisClient::Disconnect,
67 Unretained(CsisClient::Get()), addr));
68 }
69
RemoveDevice(const RawAddress & addr)70 void RemoveDevice(const RawAddress& addr) override {
71 DVLOG(2) << __func__ << " addr: " << addr;
72 do_in_main_thread(FROM_HERE, Bind(&CsisClient::RemoveDevice,
73 Unretained(CsisClient::Get()), addr));
74 }
75
LockGroup(int group_id,bool lock)76 void LockGroup(int group_id, bool lock) override {
77 DVLOG(2) << __func__ << " group id: " << group_id << " lock: " << lock;
78
79 do_in_main_thread(
80 FROM_HERE, Bind(&CsisClient::LockGroup, Unretained(CsisClient::Get()),
81 group_id, lock, base::DoNothing()));
82 }
83
Cleanup(void)84 void Cleanup(void) override {
85 DVLOG(2) << __func__;
86 do_in_main_thread(FROM_HERE, Bind(&CsisClient::CleanUp));
87 }
88
OnConnectionState(const RawAddress & addr,ConnectionState state)89 void OnConnectionState(const RawAddress& addr,
90 ConnectionState state) override {
91 DVLOG(2) << __func__ << " addr: " << addr;
92 do_in_jni_thread(FROM_HERE, Bind(&CsisClientCallbacks::OnConnectionState,
93 Unretained(callbacks_), addr, state));
94 }
95
OnDeviceAvailable(const RawAddress & addr,int group_id,int group_size,int rank,const bluetooth::Uuid & uuid)96 void OnDeviceAvailable(const RawAddress& addr, int group_id, int group_size,
97 int rank, const bluetooth::Uuid& uuid) override {
98 DVLOG(2) << __func__ << " addr: " << addr << " group_id: " << group_id;
99
100 do_in_jni_thread(FROM_HERE, Bind(&CsisClientCallbacks::OnDeviceAvailable,
101 Unretained(callbacks_), addr, group_id,
102 group_size, rank, uuid));
103 }
104
OnSetMemberAvailable(const RawAddress & addr,int group_id)105 void OnSetMemberAvailable(const RawAddress& addr, int group_id) override {
106 DVLOG(2) << __func__ << " addr: " << addr << " group id: " << group_id;
107
108 do_in_jni_thread(FROM_HERE, Bind(&CsisClientCallbacks::OnSetMemberAvailable,
109 Unretained(callbacks_), addr, group_id));
110 }
111
112 /* Callback for lock changed in the group */
OnGroupLockChanged(int group_id,bool locked,CsisGroupLockStatus status)113 virtual void OnGroupLockChanged(int group_id, bool locked,
114 CsisGroupLockStatus status) override {
115 DVLOG(2) << __func__ << " group id: " << group_id << " lock: " << locked
116 << " status: " << int(status);
117
118 do_in_jni_thread(FROM_HERE,
119 Bind(&CsisClientCallbacks::OnGroupLockChanged,
120 Unretained(callbacks_), group_id, locked, status));
121 }
122
123 private:
124 CsisClientCallbacks* callbacks_;
125 };
126
127 } /* namespace */
128
btif_csis_client_get_interface(void)129 CsisClientInterface* btif_csis_client_get_interface(void) {
130 if (!csis_client_instance)
131 csis_client_instance.reset(new CsipSetCoordinatorServiceInterfaceImpl());
132
133 return csis_client_instance.get();
134 }
135