summaryrefslogtreecommitdiff
path: root/system/btif/src/btif_csis_client.cc
blob: 6e87d1fd968c7f96f63bdb9d9f416c4abe87c9d4 (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
/*
 * Copyright 2021 HIMSA II K/S - www.himsa.com.
 * Represented by EHIMA - www.ehima.com
 *
 * 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.
 */

#include <base/bind.h>
#include <base/location.h>
#include <base/logging.h>
#include <hardware/bluetooth.h>
#include <hardware/bt_csis.h>

#include "bind_helpers.h"
#include "bta_csis_api.h"
#include "btif_common.h"
#include "btif_storage.h"
#include "stack/include/btu.h"

using base::Bind;
using base::Owned;
using base::Passed;
using base::Unretained;
using bluetooth::csis::ConnectionState;
using bluetooth::csis::CsisClientCallbacks;
using bluetooth::csis::CsisClientInterface;
using bluetooth::csis::CsisGroupLockStatus;

using bluetooth::csis::CsisClient;

namespace {
std::unique_ptr<CsisClientInterface> csis_client_instance;
class CsipSetCoordinatorServiceInterfaceImpl : public CsisClientInterface,
                                               public CsisClientCallbacks {
  ~CsipSetCoordinatorServiceInterfaceImpl() override = default;

  void Init(CsisClientCallbacks* callbacks) override {
    DVLOG(2) << __func__;
    this->callbacks_ = callbacks;

    do_in_main_thread(
        FROM_HERE,
        Bind(&CsisClient::Initialize, this,
             jni_thread_wrapper(FROM_HERE,
                                Bind(&btif_storage_load_bonded_csis_devices))));
  }

  void Connect(const RawAddress& addr) override {
    DVLOG(2) << __func__ << " addr: " << addr;
    do_in_main_thread(FROM_HERE, Bind(&CsisClient::Connect,
                                      Unretained(CsisClient::Get()), addr));
  }

  void Disconnect(const RawAddress& addr) override {
    DVLOG(2) << __func__ << " addr: " << addr;
    do_in_main_thread(FROM_HERE, Bind(&CsisClient::Disconnect,
                                      Unretained(CsisClient::Get()), addr));
  }

  void RemoveDevice(const RawAddress& addr) override {
    DVLOG(2) << __func__ << " addr: " << addr;
    do_in_main_thread(FROM_HERE, Bind(&CsisClient::RemoveDevice,
                                      Unretained(CsisClient::Get()), addr));
  }

  void LockGroup(int group_id, bool lock) override {
    DVLOG(2) << __func__ << " group id: " << group_id << " lock: " << lock;

    do_in_main_thread(
        FROM_HERE, Bind(&CsisClient::LockGroup, Unretained(CsisClient::Get()),
                        group_id, lock, base::DoNothing()));
  }

  void Cleanup(void) override {
    DVLOG(2) << __func__;
    do_in_main_thread(FROM_HERE, Bind(&CsisClient::CleanUp));
  }

  void OnConnectionState(const RawAddress& addr,
                         ConnectionState state) override {
    DVLOG(2) << __func__ << " addr: " << addr;
    do_in_jni_thread(FROM_HERE, Bind(&CsisClientCallbacks::OnConnectionState,
                                     Unretained(callbacks_), addr, state));
  }

  void OnDeviceAvailable(const RawAddress& addr, int group_id, int group_size,
                         int rank, const bluetooth::Uuid& uuid) override {
    DVLOG(2) << __func__ << " addr: " << addr << " group_id: " << group_id;

    do_in_jni_thread(FROM_HERE, Bind(&CsisClientCallbacks::OnDeviceAvailable,
                                     Unretained(callbacks_), addr, group_id,
                                     group_size, rank, uuid));
  }

  void OnSetMemberAvailable(const RawAddress& addr, int group_id) override {
    DVLOG(2) << __func__ << " addr: " << addr << " group id: " << group_id;

    do_in_jni_thread(FROM_HERE, Bind(&CsisClientCallbacks::OnSetMemberAvailable,
                                     Unretained(callbacks_), addr, group_id));
  }

  /* Callback for lock changed in the group */
  virtual void OnGroupLockChanged(int group_id, bool locked,
                                  CsisGroupLockStatus status) override {
    DVLOG(2) << __func__ << " group id: " << group_id << " lock: " << locked
             << " status: " << int(status);

    do_in_jni_thread(FROM_HERE,
                     Bind(&CsisClientCallbacks::OnGroupLockChanged,
                          Unretained(callbacks_), group_id, locked, status));
  }

 private:
  CsisClientCallbacks* callbacks_;
};

} /* namespace */

CsisClientInterface* btif_csis_client_get_interface(void) {
  if (!csis_client_instance)
    csis_client_instance.reset(new CsipSetCoordinatorServiceInterfaceImpl());

  return csis_client_instance.get();
}