aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothLeAdvertiser.java
blob: 5853071dd85b369b7c6f8c7d30248fd16e5914f1 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.IBluetoothGatt;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertisingSet;
import android.bluetooth.le.AdvertisingSetCallback;
import android.bluetooth.le.AdvertisingSetParameters;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.PeriodicAdvertisingParameters;
import android.content.AttributionSource;
import android.os.Handler;
import android.os.ParcelUuid;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.ReflectorObject;
import org.robolectric.util.PerfStatsCollector;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.reflector.Constructor;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.versioning.AndroidVersions.V;

/** Shadow implementation of {@link BluetoothLeAdvertiser}. */
@Implements(value = BluetoothLeAdvertiser.class, minSdk = O)
public class ShadowBluetoothLeAdvertiser {

  private static final String CALLBACK_NULL_MESSAGE = "callback cannot be null.";
  private static final int MAX_LEGACY_ADVERTISING_DATA_BYTES = 31;
  private static final int OVERHEAD_BYTES_PER_FIELD = 2;
  private static final int FLAGS_FIELD_BYTES = 3;
  private static final int MANUFACTURER_SPECIFIC_DATA_LENGTH = 2;
  private static final int SERVICE_DATA_UUID_LENGTH = 2;

  private BluetoothAdapter bluetoothAdapter;
  private final Set<AdvertiseCallback> advertisements = new HashSet<>();
  private final Map<AdvertisingSetCallback, AdvertisingSet> advertisingSetMap = new HashMap<>();
  private final AtomicInteger advertiserId = new AtomicInteger(0);
  @ReflectorObject protected BluetoothLeAdvertiserReflector bluetoothLeAdvertiserReflector;

  @Implementation(maxSdk = R)
  protected void __constructor__(IBluetoothManager bluetoothManager) {
    bluetoothLeAdvertiserReflector.__constructor__(bluetoothManager);
    PerfStatsCollector.getInstance().incrementCount("constructShadowBluetoothLeAdvertiser");
    this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  }

  @Implementation(minSdk = S)
  protected void __constructor__(BluetoothAdapter bluetoothAdapter) {
    bluetoothLeAdvertiserReflector.__constructor__(bluetoothAdapter);
    PerfStatsCollector.getInstance().incrementCount("constructShadowBluetoothLeAdvertiser");
    this.bluetoothAdapter = bluetoothAdapter;
  }

  /**
   * Start Bluetooth LE Advertising. This method returns immediately, the operation status is
   * delivered through {@code callback}.
   *
   * @param settings Settings for Bluetooth LE advertising.
   * @param advertiseData Advertisement data to be broadcasted.
   * @param callback Callback for advertising status.
   */
  @Implementation
  protected void startAdvertising(
      AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseCallback callback) {
    startAdvertising(settings, advertiseData, null, callback);
  }

  /**
   * Start Bluetooth LE Advertising. This method returns immediately, the operation status is
   * delivered through {@code callback}.
   *
   * @param settings Settings for Bluetooth LE advertising.
   * @param advertiseData Advertisement data to be broadcasted.
   * @param scanResponse Scan response associated with the advertisement data.
   * @param callback Callback for advertising status.
   * @throws IllegalArgumentException When {@code callback} is not present.
   */
  @Implementation
  protected void startAdvertising(
      AdvertiseSettings settings,
      AdvertiseData advertiseData,
      AdvertiseData scanResponse,
      AdvertiseCallback callback) {

    if (callback == null) {
      throw new IllegalArgumentException(CALLBACK_NULL_MESSAGE);
    }

    boolean isConnectable = settings.isConnectable();

    if (this.getTotalBytes(advertiseData, isConnectable) > MAX_LEGACY_ADVERTISING_DATA_BYTES
        || this.getTotalBytes(scanResponse, false) > MAX_LEGACY_ADVERTISING_DATA_BYTES) {
      callback.onStartFailure(AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE);
      return;
    }

    if (advertisements.contains(callback)) {
      callback.onStartFailure(AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED);
      return;
    }

    this.advertisements.add(callback);
    callback.onStartSuccess(settings);
  }

  /**
   * Stop Bluetooth LE advertising. The {@code callback} must be the same one use in {@link
   * ShadowBluetoothLeAdvertiser#startAdvertising}.
   *
   * @param callback {@link AdvertiseCallback} identifies the advertising instance to stop.
   * @throws IllegalArgumentException When the {@code callback} is not a key present in {@code
   *     advertisements}.
   */
  @Implementation
  protected void stopAdvertising(AdvertiseCallback callback) {
    if (callback == null) {
      throw new IllegalArgumentException(CALLBACK_NULL_MESSAGE);
    }
    this.advertisements.remove(callback);
  }

  /**
   * Start Bluetooth LE Advertising Set. This method returns immediately, the operation status is
   * delivered through {@code callback}.
   *
   * @param parameters Advertising set parameters.
   * @param advertiseData Advertisement data to be broadcasted.
   * @param scanResponse Scan response associated with the advertisement data.
   * @param periodicParameters Periodic advertisng parameters.
   * @param periodicData Periodic advertising data.
   * @param duration Advertising duration, in 10ms unit.
   * @param maxExtendedAdvertisingEvents Maximum number of extended advertising events the
   *     controller shall attempt to send prior to terminating the extended advertising, even if the
   *     duration has not expired.
   * @param gattServer GattServer the GATT server that will "own" connections derived from this
   *     advertising.
   * @param callback Callback for advertising set.
   * @param handler Thread upon which the callbacks will be invoked.
   * @throws IllegalArgumentException When {@code callback} is not present.
   */
  @Implementation(minSdk = UPSIDE_DOWN_CAKE)
  protected void startAdvertisingSet(
      AdvertisingSetParameters parameters,
      AdvertiseData advertiseData,
      AdvertiseData scanResponse,
      PeriodicAdvertisingParameters periodicParameters,
      AdvertiseData periodicData,
      int duration,
      int maxExtendedAdvertisingEvents,
      BluetoothGattServer gattServer,
      AdvertisingSetCallback callback,
      Handler handler) {
    if (callback == null) {
      throw new IllegalArgumentException("callback cannot be null");
    }

    boolean isConnectable = parameters.isConnectable();
    boolean isDiscoverable = parameters.isDiscoverable();
    boolean hasFlags = isConnectable && isDiscoverable;
    if (parameters.isLegacy()) {
      if (getTotalBytes(advertiseData, hasFlags) > MAX_LEGACY_ADVERTISING_DATA_BYTES) {
        throw new IllegalArgumentException("Legacy advertising data too big");
      }

      if (getTotalBytes(scanResponse, false) > MAX_LEGACY_ADVERTISING_DATA_BYTES) {
        throw new IllegalArgumentException("Legacy scan response data too big");
      }
    } else {
      boolean supportCodedPhy = bluetoothAdapter.isLeCodedPhySupported();
      boolean support2MPhy = bluetoothAdapter.isLe2MPhySupported();
      int pphy = parameters.getPrimaryPhy();
      int sphy = parameters.getSecondaryPhy();
      if (pphy == BluetoothDevice.PHY_LE_CODED && !supportCodedPhy) {
        throw new IllegalArgumentException("Unsupported primary PHY selected");
      }

      if ((sphy == BluetoothDevice.PHY_LE_CODED && !supportCodedPhy)
          || (sphy == BluetoothDevice.PHY_LE_2M && !support2MPhy)) {
        throw new IllegalArgumentException("Unsupported secondary PHY selected");
      }

      int maxData = bluetoothAdapter.getLeMaximumAdvertisingDataLength();
      if (getTotalBytes(advertiseData, hasFlags) > maxData) {
        throw new IllegalArgumentException("Advertising data too big");
      }

      if (getTotalBytes(scanResponse, false) > maxData) {
        throw new IllegalArgumentException("Scan response data too big");
      }

      if (getTotalBytes(periodicData, false) > maxData) {
        throw new IllegalArgumentException("Periodic advertising data too big");
      }
    }

    if (maxExtendedAdvertisingEvents < 0 || maxExtendedAdvertisingEvents > 255) {
      throw new IllegalArgumentException(
          "maxExtendedAdvertisingEvents out of range: " + maxExtendedAdvertisingEvents);
    }

    if (maxExtendedAdvertisingEvents != 0 && !bluetoothAdapter.isLePeriodicAdvertisingSupported()) {
      throw new IllegalArgumentException(
          "Can't use maxExtendedAdvertisingEvents with controller that don't support "
              + "LE Extended Advertising");
    }

    if (duration < 0 || duration > 65535) {
      throw new IllegalArgumentException("duration out of range: " + duration);
    }

    if (advertisingSetMap.containsKey(callback)) {
      callback.onAdvertisingSetStarted(
          /* advertisingSet= */ null,
          parameters.getTxPowerLevel(),
          AdvertisingSetCallback.ADVERTISE_FAILED_ALREADY_STARTED);
      return;
    }

    AdvertisingSet advertisingSet;
    if (RuntimeEnvironment.getApiLevel() >= V.SDK_INT) {
      IBluetoothGatt gatt =
          ReflectionHelpers.callInstanceMethod(bluetoothAdapter, "getBluetoothGatt");

      advertisingSet =
          reflector(AdvertisingSetReflector.class)
              .__constructor__(
                  gatt == null ? ReflectionHelpers.createNullProxy(IBluetoothGatt.class) : gatt,
                  advertiserId.getAndAdd(1),
                  bluetoothAdapter,
                  bluetoothAdapter.getAttributionSource());
    } else {
      advertisingSet =
          reflector(AdvertisingSetReflector.class)
              .__constructor__(
                  advertiserId.getAndAdd(1),
                  ReflectionHelpers.createNullProxy(IBluetoothManager.class),
                  (AttributionSource)
                      ReflectionHelpers.callInstanceMethod(
                          bluetoothAdapter, "getAttributionSource"));
    }

    callback.onAdvertisingSetStarted(
        advertisingSet, parameters.getTxPowerLevel(), AdvertisingSetCallback.ADVERTISE_SUCCESS);

    advertisingSetMap.put(callback, advertisingSet);
  }

  /**
   * Used to dispose of a {@link AdvertisingSet} object, obtained with {@link
   * BluetoothLeAdvertiser#startAdvertisingSet}.
   *
   * @param callback Callback for advertising set.
   * @throws IllegalArgumentException When {@code callback} is not present.
   */
  @Implementation(minSdk = UPSIDE_DOWN_CAKE)
  protected void stopAdvertisingSet(AdvertisingSetCallback callback) {
    if (callback == null) {
      throw new IllegalArgumentException("callback cannot be null");
    }

    if (!advertisingSetMap.containsKey(callback)) {
      throw new IllegalArgumentException("callback not found");
    }

    callback.onAdvertisingSetStopped(advertisingSetMap.get(callback));

    advertisingSetMap.remove(callback);
  }

  /** Returns the count of current ongoing Bluetooth LE advertising requests. */
  public int getAdvertisementRequestCount() {
    return this.advertisements.size();
  }

  /** Returns the count of current ongoing Bluetooth LE advertising set requests. */
  public int getAdvertisingSetRequestCount() {
    return this.advertisingSetMap.size();
  }

  private int getTotalBytes(AdvertiseData data, boolean isConnectable) {
    if (data == null) {
      return 0;
    }
    // Flags field is omitted if the advertising is not connectable.
    int size = isConnectable ? FLAGS_FIELD_BYTES : 0;
    if (data.getServiceUuids() != null) {
      int num16BitUuids = 0;
      int num32BitUuids = 0;
      int num128BitUuids = 0;
      for (ParcelUuid uuid : data.getServiceUuids()) {
        if (BluetoothUuid.is16BitUuid(uuid)) {
          ++num16BitUuids;
        } else if (BluetoothUuid.is32BitUuid(uuid)) {
          ++num32BitUuids;
        } else {
          ++num128BitUuids;
        }
      }
      // 16 bit service uuids are grouped into one field when doing advertising.
      if (num16BitUuids != 0) {
        size += OVERHEAD_BYTES_PER_FIELD + num16BitUuids * BluetoothUuid.UUID_BYTES_16_BIT;
      }
      // 32 bit service uuids are grouped into one field when doing advertising.
      if (num32BitUuids != 0) {
        size += OVERHEAD_BYTES_PER_FIELD + num32BitUuids * BluetoothUuid.UUID_BYTES_32_BIT;
      }
      // 128 bit service uuids are grouped into one field when doing advertising.
      if (num128BitUuids != 0) {
        size += OVERHEAD_BYTES_PER_FIELD + num128BitUuids * BluetoothUuid.UUID_BYTES_128_BIT;
      }
    }

    for (byte[] value : data.getServiceData().values()) {
      size += OVERHEAD_BYTES_PER_FIELD + SERVICE_DATA_UUID_LENGTH + getByteLength(value);
    }
    for (int i = 0; i < data.getManufacturerSpecificData().size(); ++i) {
      size +=
          OVERHEAD_BYTES_PER_FIELD
              + MANUFACTURER_SPECIFIC_DATA_LENGTH
              + getByteLength(data.getManufacturerSpecificData().valueAt(i));
    }
    if (data.getIncludeTxPowerLevel()) {
      size += OVERHEAD_BYTES_PER_FIELD + 1; // tx power level value is one byte.
    }
    if (data.getIncludeDeviceName() && bluetoothAdapter.getName() != null) {
      size += OVERHEAD_BYTES_PER_FIELD + bluetoothAdapter.getName().length();
    }
    return size;
  }

  private static int getByteLength(byte[] array) {
    return array == null ? 0 : array.length;
  }

  @ForType(BluetoothLeAdvertiser.class)
  private interface BluetoothLeAdvertiserReflector {
    @Direct
    void __constructor__(IBluetoothManager bluetoothManager);

    @Direct
    void __constructor__(BluetoothAdapter bluetoothAdapter);
  }

  @ForType(AdvertisingSet.class)
  interface AdvertisingSetReflector {
    @Constructor
    AdvertisingSet __constructor__(
        IBluetoothGatt bluetoothGatt,
        int advertiserId,
        BluetoothAdapter bluetoothAdapter,
        AttributionSource attributionSource);

    @Constructor
    AdvertisingSet __constructor__(
        int advertiserId, IBluetoothManager bluetoothManager, AttributionSource attributionSource);
  }
}