File: bluetooth_remote_gatt_service_android.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (203 lines) | stat: -rw-r--r-- 7,745 bytes parent folder | download | duplicates (3)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/bluetooth/bluetooth_remote_gatt_service_android.h"

#include <memory>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/notimplemented.h"
#include "device/bluetooth/bluetooth_adapter_android.h"
#include "device/bluetooth/bluetooth_device_android.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "device/bluetooth/jni_headers/ChromeBluetoothRemoteGattService_jni.h"

using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::JavaRef;

namespace device {

// static
std::unique_ptr<BluetoothRemoteGattServiceAndroid>
BluetoothRemoteGattServiceAndroid::Create(
    BluetoothAdapterAndroid* adapter,
    BluetoothDeviceAndroid* device,
    const JavaRef<jobject>&
        bluetooth_gatt_service_wrapper,  // BluetoothGattServiceWrapper
    const std::string& instance_id,
    const JavaRef<jobject>& chrome_bluetooth_device) {  // ChromeBluetoothDevice
  std::unique_ptr<BluetoothRemoteGattServiceAndroid> service(
      new BluetoothRemoteGattServiceAndroid(adapter, device, instance_id));

  JNIEnv* env = AttachCurrentThread();
  service->j_service_.Reset(Java_ChromeBluetoothRemoteGattService_create(
      env, reinterpret_cast<intptr_t>(service.get()),
      bluetooth_gatt_service_wrapper,
      base::android::ConvertUTF8ToJavaString(env, instance_id),
      chrome_bluetooth_device));

  return service;
}

BluetoothRemoteGattServiceAndroid::~BluetoothRemoteGattServiceAndroid() {
  Java_ChromeBluetoothRemoteGattService_onBluetoothRemoteGattServiceAndroidDestruction(
      AttachCurrentThread(), j_service_);
}

base::android::ScopedJavaLocalRef<jobject>
BluetoothRemoteGattServiceAndroid::GetJavaObject() {
  return base::android::ScopedJavaLocalRef<jobject>(j_service_);
}

// static
BluetoothGattService::GattErrorCode
BluetoothRemoteGattServiceAndroid::GetGattErrorCode(int bluetooth_gatt_code) {
  DCHECK(bluetooth_gatt_code != 0) << "Only errors valid. 0 == GATT_SUCCESS.";

  // TODO(scheib) Create new BluetoothGattService::GattErrorCode enums for
  // android values not yet represented. http://crbug.com/548498
  switch (bluetooth_gatt_code) {  // android.bluetooth.BluetoothGatt values:
    case 0x00000101:              // GATT_FAILURE
      return GattErrorCode::kFailed;
    case 0x0000000d:  // GATT_INVALID_ATTRIBUTE_LENGTH
      return GattErrorCode::kInvalidLength;
    case 0x00000002:  // GATT_READ_NOT_PERMITTED
      return GattErrorCode::kNotPermitted;
    case 0x00000006:  // GATT_REQUEST_NOT_SUPPORTED
      return GattErrorCode::kNotSupported;
    case 0x00000003:  // GATT_WRITE_NOT_PERMITTED
      return GattErrorCode::kNotPermitted;
    default:
      DVLOG(1) << "Unhandled status: " << bluetooth_gatt_code;
      return BluetoothGattService::GattErrorCode::kUnknown;
  }
}

// static
int BluetoothRemoteGattServiceAndroid::GetAndroidErrorCode(
    BluetoothGattService::GattErrorCode error_code) {
  // TODO(scheib) Create new BluetoothGattService::GattErrorCode enums for
  // android values not yet represented. http://crbug.com/548498
  switch (error_code) {  // Return values from android.bluetooth.BluetoothGatt:
    case GattErrorCode::kUnknown:
      return 0x00000101;  // GATT_FAILURE. No good match.
    case GattErrorCode::kFailed:
      return 0x00000101;  // GATT_FAILURE
    case GattErrorCode::kInProgress:
      return 0x00000101;  // GATT_FAILURE. No good match.
    case GattErrorCode::kInvalidLength:
      return 0x0000000d;  // GATT_INVALID_ATTRIBUTE_LENGTH
    case GattErrorCode::kNotPermitted:
      // Can't distinguish between:
      // 0x00000002:  // GATT_READ_NOT_PERMITTED
      // 0x00000003:  // GATT_WRITE_NOT_PERMITTED
      return 0x00000101;  // GATT_FAILURE. No good match.
    case GattErrorCode::kNotAuthorized:
      return 0x00000101;  // GATT_FAILURE. No good match.
    case GattErrorCode::kNotPaired:
      return 0x00000101;  // GATT_FAILURE. No good match.
    case GattErrorCode::kNotSupported:
      return 0x00000006;  // GATT_REQUEST_NOT_SUPPORTED
  }
  DVLOG(1) << "Unhandled error_code: " << static_cast<int>(error_code);
  return 0x00000101;  // GATT_FAILURE. No good match.
}

std::string BluetoothRemoteGattServiceAndroid::GetIdentifier() const {
  return instance_id_;
}

device::BluetoothUUID BluetoothRemoteGattServiceAndroid::GetUUID() const {
  return device::BluetoothUUID(base::android::ConvertJavaStringToUTF8(
      Java_ChromeBluetoothRemoteGattService_getUUID(AttachCurrentThread(),
                                                    j_service_)));
}

bool BluetoothRemoteGattServiceAndroid::IsPrimary() const {
  NOTIMPLEMENTED();
  return true;
}

device::BluetoothDevice* BluetoothRemoteGattServiceAndroid::GetDevice() const {
  return device_;
}

std::vector<device::BluetoothRemoteGattCharacteristic*>
BluetoothRemoteGattServiceAndroid::GetCharacteristics() const {
  EnsureCharacteristicsCreated();
  return BluetoothRemoteGattService::GetCharacteristics();
}

std::vector<device::BluetoothRemoteGattService*>
BluetoothRemoteGattServiceAndroid::GetIncludedServices() const {
  NOTIMPLEMENTED();
  return std::vector<device::BluetoothRemoteGattService*>();
}

device::BluetoothRemoteGattCharacteristic*
BluetoothRemoteGattServiceAndroid::GetCharacteristic(
    const std::string& identifier) const {
  EnsureCharacteristicsCreated();
  return BluetoothRemoteGattService::GetCharacteristic(identifier);
}

std::vector<BluetoothRemoteGattCharacteristic*>
BluetoothRemoteGattServiceAndroid::GetCharacteristicsByUUID(
    const BluetoothUUID& characteristic_uuid) const {
  EnsureCharacteristicsCreated();
  return BluetoothRemoteGattService::GetCharacteristicsByUUID(
      characteristic_uuid);
}

bool BluetoothRemoteGattServiceAndroid::IsDiscoveryComplete() const {
  // Not used on Android, because Android sends an event when service discovery
  // is complete for the entire device.
  NOTIMPLEMENTED();
  return true;
}

void BluetoothRemoteGattServiceAndroid::SetDiscoveryComplete(bool complete) {
  // Not used on Android, because Android sends an event when service discovery
  // is complete for the entire device.
  NOTIMPLEMENTED();
}

void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic(
    JNIEnv* env,
    const JavaParamRef<jobject>& caller,
    const JavaParamRef<jstring>& instance_id,
    const JavaParamRef<jobject>& /* BluetoothGattCharacteristicWrapper */
        bluetooth_gatt_characteristic_wrapper,
    const JavaParamRef<jobject>& /* ChromeBluetoothDevice */
        chrome_bluetooth_device) {
  std::string instance_id_string =
      base::android::ConvertJavaStringToUTF8(env, instance_id);

  DCHECK(!base::Contains(characteristics_, instance_id_string));
  AddCharacteristic(BluetoothRemoteGattCharacteristicAndroid::Create(
      adapter_, this, instance_id_string, bluetooth_gatt_characteristic_wrapper,
      chrome_bluetooth_device));
}

BluetoothRemoteGattServiceAndroid::BluetoothRemoteGattServiceAndroid(
    BluetoothAdapterAndroid* adapter,
    BluetoothDeviceAndroid* device,
    const std::string& instance_id)
    : adapter_(adapter), device_(device), instance_id_(instance_id) {}

void BluetoothRemoteGattServiceAndroid::EnsureCharacteristicsCreated() const {
  if (!characteristics_.empty())
    return;

  // Java call
  Java_ChromeBluetoothRemoteGattService_createCharacteristics(
      AttachCurrentThread(), j_service_);
}

}  // namespace device