summaryrefslogtreecommitdiff
path: root/system/stack/btm/btm_ble_multi_adv.cc
blob: 5b98508a1c4dd8a92ba718e70a4dcb0d97528935 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
/******************************************************************************
 *
 *  Copyright 2017  The Android Open Source Project
 *  Copyright 2014  Broadcom Corporation
 *
 *  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 <base/memory/weak_ptr.h>
#include <base/strings/string_number_conversions.h>
#include <base/time/time.h>
#include <string.h>

#include <queue>
#include <vector>

#include "bind_helpers.h"
#include "ble_advertiser.h"
#include "ble_advertiser_hci_interface.h"
#include "bt_target.h"
#include "btm_int_types.h"
#include "device/include/controller.h"
#include "osi/include/alarm.h"
#include "stack/btm/btm_ble_int.h"
#include "types/raw_address.h"

using base::Bind;
using base::TimeDelta;
using base::TimeTicks;
using RegisterCb =
    base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>;
using IdTxPowerStatusCb = base::Callback<void(
    uint8_t /* inst_id */, int8_t /* tx_power */, uint8_t /* status */)>;
using SetEnableData = BleAdvertiserHciInterface::SetEnableData;
extern void btm_gen_resolvable_private_addr(
    base::Callback<void(const RawAddress& rpa)> cb);

constexpr int ADV_DATA_LEN_MAX = 251;

namespace {

bool is_connectable(uint16_t advertising_event_properties) {
  return advertising_event_properties & 0x01;
}

struct AdvertisingInstance {
  uint8_t inst_id;
  bool in_use;
  uint8_t advertising_event_properties;
  alarm_t* adv_raddr_timer;
  int8_t tx_power;
  uint16_t duration;  // 1 unit is 10ms
  uint8_t maxExtAdvEvents;
  alarm_t* timeout_timer;
  uint8_t own_address_type;
  RawAddress own_address;
  MultiAdvCb timeout_cb;
  bool address_update_required;
  bool periodic_enabled;
  uint32_t advertising_interval;  // 1 unit is 0.625 ms

  /* When true, advertising set is enabled, or last scheduled call to "LE Set
   * Extended Advertising Set Enable" is to enable this advertising set. Any
   * command scheduled when in this state will execute when the set is enabled,
   * unless enabling fails.
   *
   * When false, advertising set is disabled, or last scheduled call to "LE Set
   * Extended Advertising Set Enable" is to disable this advertising set. Any
   * command scheduled when in this state will execute when the set is disabled.
   */
  bool enable_status;
  TimeTicks enable_time;

  bool IsEnabled() { return enable_status; }

  bool IsConnectable() { return is_connectable(advertising_event_properties); }

  AdvertisingInstance(int inst_id)
      : inst_id(inst_id),
        in_use(false),
        advertising_event_properties(0),
        tx_power(0),
        duration(0),
        timeout_timer(nullptr),
        own_address_type(0),
        own_address(RawAddress::kEmpty),
        address_update_required(false),
        periodic_enabled(false),
        enable_status(false) {
    adv_raddr_timer = alarm_new_periodic("btm_ble.adv_raddr_timer");
  }

  ~AdvertisingInstance() {
    alarm_free(adv_raddr_timer);
    adv_raddr_timer = nullptr;
    if (timeout_timer) {
      alarm_free(timeout_timer);
      timeout_timer = nullptr;
    }
  }
};

void btm_ble_adv_raddr_timer_timeout(void* data);

struct closure_data {
  base::Closure user_task;
  base::Location posted_from;
};

static void alarm_closure_cb(void* p) {
  closure_data* data = (closure_data*)p;
  VLOG(1) << "executing timer scheduled at %s" << data->posted_from.ToString();
  data->user_task.Run();
  delete data;
}

// Periodic alarms are not supported, because we clean up data in callback
static void alarm_set_closure(const base::Location& posted_from, alarm_t* alarm,
                              uint64_t interval_ms, base::Closure user_task) {
  closure_data* data = new closure_data;
  data->posted_from = posted_from;
  data->user_task = std::move(user_task);
  VLOG(1) << "scheduling timer %s" << data->posted_from.ToString();
  alarm_set_on_mloop(alarm, interval_ms, alarm_closure_cb, data);
}

class BleAdvertisingManagerImpl;

/* a temporary type for holding all the data needed in callbacks below*/
struct CreatorParams {
  uint8_t inst_id;
  base::WeakPtr<BleAdvertisingManagerImpl> self;
  IdTxPowerStatusCb cb;
  tBTM_BLE_ADV_PARAMS params;
  std::vector<uint8_t> advertise_data;
  std::vector<uint8_t> scan_response_data;
  tBLE_PERIODIC_ADV_PARAMS periodic_params;
  std::vector<uint8_t> periodic_data;
  uint16_t duration;
  uint8_t maxExtAdvEvents;
  RegisterCb timeout_cb;
};

using c_type = std::unique_ptr<CreatorParams>;

BleAdvertisingManager* instance;
base::WeakPtr<BleAdvertisingManagerImpl> instance_weakptr;

class BleAdvertisingManagerImpl
    : public BleAdvertisingManager,
      public BleAdvertiserHciInterface::AdvertisingEventObserver {
 public:
  BleAdvertisingManagerImpl(BleAdvertiserHciInterface* interface)
      : hci_interface(interface), weak_factory_(this) {
    hci_interface->ReadInstanceCount(
        base::Bind(&BleAdvertisingManagerImpl::ReadInstanceCountCb,
                   weak_factory_.GetWeakPtr()));
  }

  ~BleAdvertisingManagerImpl() override { adv_inst.clear(); }

  void GetOwnAddress(uint8_t inst_id, GetAddressCallback cb) override {
    cb.Run(adv_inst[inst_id].own_address_type, adv_inst[inst_id].own_address);
  }

  void ReadInstanceCountCb(uint8_t instance_count) {
    this->inst_count = instance_count;
    adv_inst.reserve(inst_count);
    /* Initialize adv instance indices and IDs. */
    for (uint8_t i = 0; i < inst_count; i++) {
      adv_inst.emplace_back(i);
    }
  }

  void GenerateRpa(base::Callback<void(const RawAddress&)> cb) {
    btm_gen_resolvable_private_addr(std::move(cb));
  }

  void ConfigureRpa(AdvertisingInstance* p_inst, MultiAdvCb configuredCb) {
    /* Connectable advertising set must be disabled when updating RPA */
    bool restart = p_inst->IsEnabled() && p_inst->IsConnectable();

    // If there is any form of timeout on the set, schedule address update when
    // the set stops, because there is no good way to compute new timeout value.
    // Maximum duration value is around 10 minutes, so this is safe.
    if (restart && (p_inst->duration || p_inst->maxExtAdvEvents)) {
      p_inst->address_update_required = true;
      configuredCb.Run(0x01);
      return;
    }

    GenerateRpa(Bind(
        [](AdvertisingInstance* p_inst, MultiAdvCb configuredCb,
           const RawAddress& bda) {
          /* Connectable advertising set must be disabled when updating RPA */
          bool restart = p_inst->IsEnabled() && p_inst->IsConnectable();

          if (!instance_weakptr.get()) return;
          auto hci_interface = instance_weakptr.get()->GetHciInterface();

          if (restart) {
            p_inst->enable_status = false;
            hci_interface->Enable(false, p_inst->inst_id, 0x00, 0x00,
                                  base::DoNothing());
          }

          /* set it to controller */
          hci_interface->SetRandomAddress(
              p_inst->inst_id, bda,
              Bind(
                  [](AdvertisingInstance* p_inst, RawAddress bda,
                     MultiAdvCb configuredCb, uint8_t status) {
                    p_inst->own_address = bda;
                    configuredCb.Run(0x00);
                  },
                  p_inst, bda, configuredCb));

          if (restart) {
            p_inst->enable_status = true;
            hci_interface->Enable(true, p_inst->inst_id, 0x00, 0x00,
                                  base::DoNothing());
          }
        },
        p_inst, std::move(configuredCb)));
  }

  void RegisterAdvertiser(
      base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)> cb)
      override {
    int own_address_type =
        BTM_BleLocalPrivacyEnabled() ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
    RegisterAdvertiserImpl(own_address_type, cb);
  }

  void RegisterAdvertiserImpl(
      int own_address_type,
      base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)> cb) {
    AdvertisingInstance* p_inst = &adv_inst[0];
    for (uint8_t i = 0; i < inst_count; i++, p_inst++) {
      if (p_inst->in_use) continue;

      p_inst->in_use = true;
      p_inst->own_address_type = own_address_type;

      // set up periodic timer to update address.
      if (own_address_type != BLE_ADDR_PUBLIC) {
        GenerateRpa(Bind(
            [](AdvertisingInstance* p_inst,
               base::Callback<void(uint8_t /* inst_id */, uint8_t /* status */)>
                   cb,
               const RawAddress& bda) {
              p_inst->own_address = bda;

              alarm_set_on_mloop(p_inst->adv_raddr_timer,
                                 btm_get_next_private_addrress_interval_ms(),
                                 btm_ble_adv_raddr_timer_timeout, p_inst);
              cb.Run(p_inst->inst_id, BTM_BLE_MULTI_ADV_SUCCESS);
            },
            p_inst, cb));
      } else {
        p_inst->own_address = *controller_get_interface()->get_address();

        cb.Run(p_inst->inst_id, BTM_BLE_MULTI_ADV_SUCCESS);
      }
      return;
    }

    LOG(INFO) << "no free advertiser instance";
    cb.Run(0xFF, ADVERTISE_FAILED_TOO_MANY_ADVERTISERS);
  }

  void StartAdvertising(uint8_t advertiser_id, MultiAdvCb cb,
                        tBTM_BLE_ADV_PARAMS* params,
                        std::vector<uint8_t> advertise_data,
                        std::vector<uint8_t> scan_response_data, int duration,
                        MultiAdvCb timeout_cb) override {
    /* a temporary type for holding all the data needed in callbacks below*/
    struct CreatorParams {
      uint8_t inst_id;
      base::WeakPtr<BleAdvertisingManagerImpl> self;
      MultiAdvCb cb;
      tBTM_BLE_ADV_PARAMS params;
      std::vector<uint8_t> advertise_data;
      std::vector<uint8_t> scan_response_data;
      int duration;
      MultiAdvCb timeout_cb;
    };

    std::unique_ptr<CreatorParams> c;
    c.reset(new CreatorParams());

    c->self = weak_factory_.GetWeakPtr();
    c->cb = std::move(cb);
    c->params = *params;
    c->advertise_data = std::move(advertise_data);
    c->scan_response_data = std::move(scan_response_data);
    c->duration = duration;
    c->timeout_cb = std::move(timeout_cb);
    c->inst_id = advertiser_id;

    using c_type = std::unique_ptr<CreatorParams>;

    // this code is intentionally left formatted this way to highlight the
    // asynchronous flow
    // clang-format off
    c->self->SetParameters(c->inst_id, &c->params, Bind(
      [](c_type c, uint8_t status, int8_t tx_power) {
        if (!c->self) {
          LOG(INFO) << "Stack was shut down";
          return;
        }

        if (status) {
          LOG(ERROR) << "setting parameters failed, status: " << +status;
          c->cb.Run(status);
          return;
        }

        c->self->adv_inst[c->inst_id].tx_power = tx_power;

        const RawAddress& rpa = c->self->adv_inst[c->inst_id].own_address;
        c->self->GetHciInterface()->SetRandomAddress(c->inst_id, rpa, Bind(
          [](c_type c, uint8_t status) {
            if (!c->self) {
              LOG(INFO) << "Stack was shut down";
              return;
            }

            if (status != 0) {
              LOG(ERROR) << "setting random address failed, status: " << +status;
              c->cb.Run(status);
              return;
            }

            c->self->SetData(c->inst_id, false, std::move(c->advertise_data), Bind(
              [](c_type c, uint8_t status) {
                if (!c->self) {
                  LOG(INFO) << "Stack was shut down";
                  return;
                }

                if (status != 0) {
                  LOG(ERROR) << "setting advertise data failed, status: " << +status;
                  c->cb.Run(status);
                  return;
                }

                c->self->SetData(c->inst_id, true, std::move(c->scan_response_data), Bind(
                  [](c_type c, uint8_t status) {
                    if (!c->self) {
                      LOG(INFO) << "Stack was shut down";
                      return;
                    }

                    if (status != 0) {
                      LOG(ERROR) << "setting scan response data failed, status: " << +status;
                      c->cb.Run(status);
                      return;
                    }

                    c->self->Enable(c->inst_id, true, c->cb, c->duration, 0, std::move(c->timeout_cb));

                }, base::Passed(&c)));
            }, base::Passed(&c)));
        }, base::Passed(&c)));
    }, base::Passed(&c)));
    // clang-format on
  }

  void StartAdvertisingSet(IdTxPowerStatusCb cb, tBTM_BLE_ADV_PARAMS* params,
                           std::vector<uint8_t> advertise_data,
                           std::vector<uint8_t> scan_response_data,
                           tBLE_PERIODIC_ADV_PARAMS* periodic_params,
                           std::vector<uint8_t> periodic_data,
                           uint16_t duration, uint8_t maxExtAdvEvents,
                           RegisterCb timeout_cb) override {
    std::unique_ptr<CreatorParams> c;
    c.reset(new CreatorParams());

    c->self = weak_factory_.GetWeakPtr();
    c->cb = std::move(cb);
    c->params = *params;
    c->advertise_data = std::move(advertise_data);
    c->scan_response_data = std::move(scan_response_data);
    c->periodic_params = *periodic_params;
    c->periodic_data = std::move(periodic_data);
    c->duration = duration;
    c->maxExtAdvEvents = maxExtAdvEvents;
    c->timeout_cb = std::move(timeout_cb);

    int own_address_type =
        BTM_BleLocalPrivacyEnabled() ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
    if (params->own_address_type != BLE_ADDR_ANONYMOUS) {
      own_address_type = params->own_address_type;
    }

    // this code is intentionally left formatted this way to highlight the
    // asynchronous flow
    // clang-format off
    c->self->RegisterAdvertiserImpl(own_address_type, Bind(
      [](c_type c, uint8_t advertiser_id, uint8_t status) {
        if (!c->self) {
          LOG(INFO) << "Stack was shut down";
          return;
        }

        if (status != 0) {
          LOG(ERROR) << " failed, status: " << +status;
          c->cb.Run(0, 0, status);
          return;
        }

        c->inst_id = advertiser_id;

        c->self->SetParameters(c->inst_id, &c->params, Bind(
          [](c_type c, uint8_t status, int8_t tx_power) {
            if (!c->self) {
              LOG(INFO) << "Stack was shut down";
              return;
            }

            if (status != 0) {
              c->self->Unregister(c->inst_id);
              LOG(ERROR) << "setting parameters failed, status: " << +status;
              c->cb.Run(0, 0, status);
              return;
            }

            c->self->adv_inst[c->inst_id].tx_power = tx_power;

            if (c->self->adv_inst[c->inst_id].own_address_type == BLE_ADDR_PUBLIC) {
              auto self = c->self;
              self->StartAdvertisingSetAfterAddressPart(std::move(c));
              return;
            }

            //own_address_type == BLE_ADDR_RANDOM
            const RawAddress& rpa = c->self->adv_inst[c->inst_id].own_address;
            c->self->GetHciInterface()->SetRandomAddress(c->inst_id, rpa, Bind(
              [](c_type c, uint8_t status) {
                if (!c->self) {
                  LOG(INFO) << "Stack was shut down";
                  return;
                }

                if (status != 0) {
                  c->self->Unregister(c->inst_id);
                  LOG(ERROR) << "setting random address failed, status: " << +status;
                  c->cb.Run(0, 0, status);
                  return;
                }

                auto self = c->self;
                self->StartAdvertisingSetAfterAddressPart(std::move(c));
          }, base::Passed(&c)));
        }, base::Passed(&c)));
    }, base::Passed(&c)));
    // clang-format on
  }

  void StartAdvertisingSetAfterAddressPart(c_type c) {
    c->self->SetData(
        c->inst_id, false, std::move(c->advertise_data),
        Bind(
            [](c_type c, uint8_t status) {
              if (!c->self) {
                LOG(INFO) << "Stack was shut down";
                return;
              }

              if (status != 0) {
                c->self->Unregister(c->inst_id);
                LOG(ERROR) << "setting advertise data failed, status: "
                           << +status;
                c->cb.Run(0, 0, status);
                return;
              }

              c->self->SetData(
                  c->inst_id, true, std::move(c->scan_response_data),
                  Bind(
                      [](c_type c, uint8_t status) {
                        if (!c->self) {
                          LOG(INFO) << "Stack was shut down";
                          return;
                        }

                        if (status != 0) {
                          c->self->Unregister(c->inst_id);
                          LOG(ERROR)
                              << "setting scan response data failed, status: "
                              << +status;
                          c->cb.Run(0, 0, status);
                          return;
                        }

                        auto self = c->self;
                        if (c->periodic_params.enable) {
                          self->StartAdvertisingSetPeriodicPart(std::move(c));
                        } else {
                          self->StartAdvertisingSetFinish(std::move(c));
                        }
                      },
                      base::Passed(&c)));
            },
            base::Passed(&c)));
  }

  void StartAdvertisingSetPeriodicPart(c_type c) {
    // this code is intentionally left formatted this way to highlight the
    // asynchronous flow
    // clang-format off
    c->self->SetPeriodicAdvertisingParameters(c->inst_id, &c->periodic_params, Bind(
      [](c_type c, uint8_t status) {
        if (!c->self) {
          LOG(INFO) << "Stack was shut down";
          return;
        }

        if (status != 0) {
          c->self->Unregister(c->inst_id);
          LOG(ERROR) << "setting periodic parameters failed, status: " << +status;
          c->cb.Run(0, 0, status);
          return;
        }

        c->self->SetPeriodicAdvertisingData(c->inst_id, std::move(c->periodic_data), Bind(
          [](c_type c, uint8_t status) {
            if (!c->self) {
              LOG(INFO) << "Stack was shut down";
              return;
            }

            if (status != 0) {
              c->self->Unregister(c->inst_id);
              LOG(ERROR) << "setting periodic parameters failed, status: " << +status;
              c->cb.Run(0, 0, status);
              return;
            }

            c->self->SetPeriodicAdvertisingEnable(c->inst_id, true, Bind(
              [](c_type c, uint8_t status) {
                if (!c->self) {
                  LOG(INFO) << "Stack was shut down";
                  return;
                }

                if (status != 0) {
                  c->self->Unregister(c->inst_id);
                  LOG(ERROR) << "enabling periodic advertising failed, status: " << +status;
                  c->cb.Run(0, 0, status);
                  return;
                }

                auto self = c->self;
                self->StartAdvertisingSetFinish(std::move(c));

              }, base::Passed(&c)));
        }, base::Passed(&c)));
    }, base::Passed(&c)));
    // clang-format on
  }

  void StartAdvertisingSetFinish(c_type c) {
    uint8_t inst_id = c->inst_id;
    uint16_t duration = c->duration;
    uint8_t maxExtAdvEvents = c->maxExtAdvEvents;
    RegisterCb timeout_cb = std::move(c->timeout_cb);
    base::WeakPtr<BleAdvertisingManagerImpl> self = c->self;
    MultiAdvCb enable_cb = Bind(
        [](c_type c, uint8_t status) {
          if (!c->self) {
            LOG(INFO) << "Stack was shut down";
            return;
          }

          if (status != 0) {
            c->self->Unregister(c->inst_id);
            LOG(ERROR) << "enabling advertiser failed, status: " << +status;
            c->cb.Run(0, 0, status);
            return;
          }
          int8_t tx_power = c->self->adv_inst[c->inst_id].tx_power;
          c->cb.Run(c->inst_id, tx_power, status);
        },
        base::Passed(&c));

    self->Enable(inst_id, true, std::move(enable_cb), duration, maxExtAdvEvents,
                 Bind(std::move(timeout_cb), inst_id));
  }

  void EnableWithTimerCb(uint8_t inst_id, MultiAdvCb enable_cb, int duration,
                         MultiAdvCb timeout_cb, uint8_t status) {
    VLOG(1) << __func__ << " inst_id: " << +inst_id;
    AdvertisingInstance* p_inst = &adv_inst[inst_id];

    // Run the regular enable callback
    enable_cb.Run(status);

    p_inst->timeout_timer = alarm_new("btm_ble.adv_timeout");

    base::Closure cb = Bind(
        &BleAdvertisingManagerImpl::Enable, weak_factory_.GetWeakPtr(), inst_id,
        0 /* disable */, std::move(timeout_cb), 0, 0, base::DoNothing());

    // schedule disable when the timeout passes
    alarm_set_closure(FROM_HERE, p_inst->timeout_timer, duration * 10,
                      std::move(cb));
  }

  void Enable(uint8_t inst_id, bool enable, MultiAdvCb cb, uint16_t duration,
              uint8_t maxExtAdvEvents, MultiAdvCb timeout_cb) override {
    VLOG(1) << __func__ << " inst_id: " << +inst_id;
    if (inst_id >= inst_count) {
      LOG(ERROR) << "bad instance id " << +inst_id;
      return;
    }

    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    VLOG(1) << __func__ << " enable: " << enable << ", duration: " << +duration;
    if (!p_inst->in_use) {
      LOG(ERROR) << "Invalid or no active instance";
      cb.Run(BTM_BLE_MULTI_ADV_FAILURE);
      return;
    }

    if (enable && (duration || maxExtAdvEvents)) {
      p_inst->timeout_cb = std::move(timeout_cb);
    }

    p_inst->duration = duration;
    p_inst->maxExtAdvEvents = maxExtAdvEvents;

    if (enable && p_inst->address_update_required) {
      p_inst->address_update_required = false;
      ConfigureRpa(p_inst, base::Bind(&BleAdvertisingManagerImpl::EnableFinish,
                                      weak_factory_.GetWeakPtr(), p_inst,
                                      enable, std::move(cb)));
      return;
    }

    EnableFinish(p_inst, enable, std::move(cb), 0);
  }

  void EnableFinish(AdvertisingInstance* p_inst, bool enable, MultiAdvCb cb,
                    uint8_t status) {
    MultiAdvCb myCb;
    if (enable && p_inst->duration) {
      // TODO(jpawlowski): HCI implementation that can't do duration should
      // emulate it, not EnableWithTimerCb.
      myCb = Bind(&BleAdvertisingManagerImpl::EnableWithTimerCb,
                  weak_factory_.GetWeakPtr(), p_inst->inst_id, std::move(cb),
                  p_inst->duration, p_inst->timeout_cb);
    } else {
      myCb = std::move(cb);

      if (p_inst->timeout_timer) {
        alarm_cancel(p_inst->timeout_timer);
        alarm_free(p_inst->timeout_timer);
        p_inst->timeout_timer = nullptr;
      }
    }

    if (enable) p_inst->enable_time = TimeTicks::Now();
    p_inst->enable_status = enable;
    GetHciInterface()->Enable(enable, p_inst->inst_id, p_inst->duration,
                              p_inst->maxExtAdvEvents, std::move(myCb));
  }

  void SetParameters(uint8_t inst_id, tBTM_BLE_ADV_PARAMS* p_params,
                     ParametersCb cb) override {
    VLOG(1) << __func__ << " inst_id: " << +inst_id;
    if (inst_id >= inst_count) {
      LOG(ERROR) << "bad instance id " << +inst_id;
      return;
    }

    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    if (!p_inst->in_use) {
      LOG(ERROR) << "adv instance not in use" << +inst_id;
      cb.Run(BTM_BLE_MULTI_ADV_FAILURE, 0);
      return;
    }

    // TODO: disable only if was enabled, currently no use scenario needs
    // that,
    // we always set parameters before enabling
    // GetHciInterface()->Enable(false, inst_id, base::DoNothing());
    p_inst->advertising_event_properties =
        p_params->advertising_event_properties;
    p_inst->tx_power = p_params->tx_power;
    p_inst->advertising_interval = p_params->adv_int_min;
    const RawAddress& peer_address = RawAddress::kEmpty;

    // sid must be in range 0x00 to 0x0F. Since no controller supports more than
    // 16 advertisers, it's safe to make sid equal to inst_id.
    uint8_t sid = p_inst->inst_id % 0x0F;

    GetHciInterface()->SetParameters(
        p_inst->inst_id, p_params->advertising_event_properties,
        p_params->adv_int_min, p_params->adv_int_max, p_params->channel_map,
        p_inst->own_address_type, p_inst->own_address, 0x00, peer_address,
        p_params->adv_filter_policy, p_inst->tx_power,
        p_params->primary_advertising_phy, 0x00,
        p_params->secondary_advertising_phy, sid,
        p_params->scan_request_notification_enable, cb);

    // TODO: re-enable only if it was enabled, properly call
    // SetParamsCallback
    // currently no use scenario needs that
    // GetHciInterface()->Enable(true, inst_id, BTM_BleUpdateAdvInstParamCb);
  }

  void SetData(uint8_t inst_id, bool is_scan_rsp, std::vector<uint8_t> data,
               MultiAdvCb cb) override {
    VLOG(1) << __func__ << " inst_id: " << +inst_id;
    if (inst_id >= inst_count) {
      LOG(ERROR) << "bad instance id " << +inst_id;
      return;
    }

    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    VLOG(1) << "is_scan_rsp = " << is_scan_rsp;

    if (!is_scan_rsp && is_connectable(p_inst->advertising_event_properties)) {
      uint8_t flags_val = BTM_GENERAL_DISCOVERABLE;

      if (p_inst->duration) flags_val = BTM_LIMITED_DISCOVERABLE;

      std::vector<uint8_t> flags;
      flags.push_back(2);  // length
      flags.push_back(HCI_EIR_FLAGS_TYPE);
      flags.push_back(flags_val);

      data.insert(data.begin(), flags.begin(), flags.end());
    }

    // Find and fill TX Power with the correct value.
    // The TX Power section is a 3 byte section.
    for (size_t i = 0; (i + 2) < data.size();) {
      if (data[i + 1] == HCI_EIR_TX_POWER_LEVEL_TYPE) {
        data[i + 2] = adv_inst[inst_id].tx_power;
      }
      i += data[i] + 1;
    }

    VLOG(1) << "data is: " << base::HexEncode(data.data(), data.size());
    DivideAndSendData(
        inst_id, data, cb,
        base::Bind(&BleAdvertisingManagerImpl::SetDataAdvDataSender,
                   weak_factory_.GetWeakPtr(), is_scan_rsp));
  }

  void SetDataAdvDataSender(uint8_t is_scan_rsp, uint8_t inst_id,
                            uint8_t operation, uint8_t length, uint8_t* data,
                            MultiAdvCb cb) {
    if (is_scan_rsp)
      GetHciInterface()->SetScanResponseData(inst_id, operation, 0x01, length,
                                             data, cb);
    else
      GetHciInterface()->SetAdvertisingData(inst_id, operation, 0x01, length,
                                            data, cb);
  }

  using DataSender = base::Callback<void(
      uint8_t /*inst_id*/, uint8_t /* operation */, uint8_t /* length */,
      uint8_t* /* data */, MultiAdvCb /* done */)>;

  void DivideAndSendData(int inst_id, std::vector<uint8_t> data,
                         MultiAdvCb done_cb, DataSender sender) {
    DivideAndSendDataRecursively(true, inst_id, std::move(data), 0,
                                 std::move(done_cb), std::move(sender), 0);
  }

  static void DivideAndSendDataRecursively(bool isFirst, int inst_id,
                                           std::vector<uint8_t> data,
                                           int offset, MultiAdvCb done_cb,
                                           DataSender sender, uint8_t status) {
    constexpr uint8_t INTERMEDIATE =
        0x00;                        // Intermediate fragment of fragmented data
    constexpr uint8_t FIRST = 0x01;  // First fragment of fragmented data
    constexpr uint8_t LAST = 0x02;   // Last fragment of fragmented data
    constexpr uint8_t COMPLETE = 0x03;  // Complete extended advertising data

    int dataSize = (int)data.size();
    if (status != 0 || (!isFirst && offset == dataSize)) {
      /* if we got error writing data, or reached the end of data */
      done_cb.Run(status);
      return;
    }

    bool moreThanOnePacket = dataSize - offset > ADV_DATA_LEN_MAX;
    uint8_t operation = isFirst ? moreThanOnePacket ? FIRST : COMPLETE
                                : moreThanOnePacket ? INTERMEDIATE : LAST;
    int length = moreThanOnePacket ? ADV_DATA_LEN_MAX : dataSize - offset;
    int newOffset = offset + length;

    auto dataData = data.data();
    sender.Run(
        inst_id, operation, length, dataData + offset,
        Bind(&BleAdvertisingManagerImpl::DivideAndSendDataRecursively, false,
             inst_id, std::move(data), newOffset, std::move(done_cb), sender));
  }

  void SetPeriodicAdvertisingParameters(uint8_t inst_id,
                                        tBLE_PERIODIC_ADV_PARAMS* params,
                                        MultiAdvCb cb) override {
    VLOG(1) << __func__ << " inst_id: " << +inst_id;

    GetHciInterface()->SetPeriodicAdvertisingParameters(
        inst_id, params->min_interval, params->max_interval,
        params->periodic_advertising_properties, cb);
  }

  void SetPeriodicAdvertisingData(uint8_t inst_id, std::vector<uint8_t> data,
                                  MultiAdvCb cb) override {
    VLOG(1) << __func__ << " inst_id: " << +inst_id;

    VLOG(1) << "data is: " << base::HexEncode(data.data(), data.size());

    DivideAndSendData(
        inst_id, data, cb,
        base::Bind(&BleAdvertiserHciInterface::SetPeriodicAdvertisingData,
                   base::Unretained(GetHciInterface())));
  }

  void SetPeriodicAdvertisingEnable(uint8_t inst_id, uint8_t enable,
                                    MultiAdvCb cb) override {
    VLOG(1) << __func__ << " inst_id: " << +inst_id << ", enable: " << +enable;

    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    if (!p_inst->in_use) {
      LOG(ERROR) << "Invalid or not active instance";
      cb.Run(BTM_BLE_MULTI_ADV_FAILURE);
      return;
    }

    MultiAdvCb enable_cb = Bind(
        [](AdvertisingInstance* p_inst, uint8_t enable, MultiAdvCb cb,
           uint8_t status) {
          VLOG(1) << "periodc adv enable cb: inst_id: " << +p_inst->inst_id
                  << ", enable: " << +enable << ", status: " << std::hex
                  << +status;
          if (!status) p_inst->periodic_enabled = enable;

          cb.Run(status);
        },
        p_inst, enable, std::move(cb));

    GetHciInterface()->SetPeriodicAdvertisingEnable(enable, inst_id,
                                                    std::move(enable_cb));
  }

  void Unregister(uint8_t inst_id) override {
    AdvertisingInstance* p_inst = &adv_inst[inst_id];

    VLOG(1) << __func__ << " inst_id: " << +inst_id;
    if (inst_id >= inst_count) {
      LOG(ERROR) << "bad instance id " << +inst_id;
      return;
    }

    if (adv_inst[inst_id].IsEnabled()) {
      p_inst->enable_status = false;
      GetHciInterface()->Enable(false, inst_id, 0x00, 0x00, base::DoNothing());
    }

    if (p_inst->periodic_enabled) {
      p_inst->periodic_enabled = false;
      GetHciInterface()->SetPeriodicAdvertisingEnable(false, inst_id,
                                                      base::DoNothing());
    }

    alarm_cancel(p_inst->adv_raddr_timer);
    p_inst->in_use = false;
    GetHciInterface()->RemoveAdvertisingSet(inst_id, base::DoNothing());
    p_inst->address_update_required = false;
  }

  void RecomputeTimeout(AdvertisingInstance* inst, TimeTicks now) {
    TimeDelta duration = now - inst->enable_time;
    bool cb_fired = false;
    if (inst->duration) {
      int durationDone = (duration.InMilliseconds() / 10);
      if (durationDone + 1 >= inst->duration) {
        inst->enable_status = false;
        inst->timeout_cb.Run(0 /* TODO: STATUS HERE?*/);
        cb_fired = true;
      } else {
        inst->duration = inst->duration - durationDone;
      }
    }

    if (inst->maxExtAdvEvents && !cb_fired) {
      int eventsDone =
          (duration.InMilliseconds() / (inst->advertising_interval * 5 / 8));

      if (eventsDone + 1 >= inst->maxExtAdvEvents) {
        inst->enable_status = false;
        inst->timeout_cb.Run(0 /* TODO: STATUS HERE?*/);
      } else {
        inst->maxExtAdvEvents = inst->maxExtAdvEvents - eventsDone;
      }
    }
  }

  void Suspend() override {
    std::vector<SetEnableData> sets;

    for (AdvertisingInstance& inst : adv_inst) {
      if (!inst.in_use || !inst.enable_status) continue;

      if (inst.duration || inst.maxExtAdvEvents)
        RecomputeTimeout(&inst, TimeTicks::Now());

      sets.emplace_back(SetEnableData{.handle = inst.inst_id});
    }

    if (!sets.empty())
      GetHciInterface()->Enable(false, sets, base::DoNothing());
  }

  void Resume() override {
    std::vector<SetEnableData> sets;

    for (const AdvertisingInstance& inst : adv_inst) {
      if (inst.in_use && inst.enable_status) {
        sets.emplace_back(SetEnableData{
            .handle = inst.inst_id,
            .duration = inst.duration,
            .max_extended_advertising_events = inst.maxExtAdvEvents});
      }
    }

    if (!sets.empty()) GetHciInterface()->Enable(true, sets, base::DoNothing());
  }

  void OnAdvertisingSetTerminated(
      uint8_t status, uint8_t advertising_handle, uint16_t connection_handle,
      uint8_t num_completed_extended_adv_events) override {
    AdvertisingInstance* p_inst = &adv_inst[advertising_handle];
    VLOG(1) << __func__ << "status: " << loghex(status)
            << ", advertising_handle: " << loghex(advertising_handle)
            << ", connection_handle: " << loghex(connection_handle);

    if (status == HCI_ERR_LIMIT_REACHED ||
        status == HCI_ERR_ADVERTISING_TIMEOUT) {
      // either duration elapsed, or maxExtAdvEvents reached
      p_inst->enable_status = false;

      if (p_inst->timeout_cb.is_null()) {
        LOG(INFO) << __func__ << "No timeout callback";
        return;
      }

      p_inst->timeout_cb.Run(status);
      return;
    }

    if (BTM_BleLocalPrivacyEnabled() &&
        advertising_handle <= BTM_BLE_MULTI_ADV_MAX) {
      btm_acl_update_conn_addr(connection_handle, p_inst->own_address);
    }

    VLOG(1) << "reneabling advertising";

    if (p_inst->in_use) {
      // TODO(jpawlowski): we don't really allow to do directed advertising
      // right now. This should probably be removed, check with Andre.
      if ((p_inst->advertising_event_properties & 0x0C) == 0) {
        /* directed advertising bits not set */

        RecomputeTimeout(p_inst, TimeTicks::Now());
        if (p_inst->enable_status) {
          GetHciInterface()->Enable(true, advertising_handle, p_inst->duration,
                                    p_inst->maxExtAdvEvents, base::DoNothing());
        }

      } else {
        /* mark directed adv as disabled if adv has been stopped */
        p_inst->in_use = false;
      }
    }
  }

  base::WeakPtr<BleAdvertisingManagerImpl> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

  void CancelAdvAlarms() {
    AdvertisingInstance* p_inst = &adv_inst[0];
    for (uint8_t i = 0; i < inst_count; i++, p_inst++) {
      if (p_inst->timeout_timer) {
        alarm_cancel(p_inst->timeout_timer);
      }
      if (p_inst->adv_raddr_timer) {
        alarm_cancel(p_inst->adv_raddr_timer);
      }
    }
  }

 private:
  BleAdvertiserHciInterface* GetHciInterface() { return hci_interface; }

  BleAdvertiserHciInterface* hci_interface = nullptr;
  std::vector<AdvertisingInstance> adv_inst;
  uint8_t inst_count;

  // Member variables should appear before the WeakPtrFactory, to ensure
  // that any WeakPtrs are invalidated before its members
  // variable's destructors are executed, rendering them invalid.
  base::WeakPtrFactory<BleAdvertisingManagerImpl> weak_factory_;
};

void btm_ble_adv_raddr_timer_timeout(void* data) {
  BleAdvertisingManagerImpl* ptr = instance_weakptr.get();
  if (ptr) ptr->ConfigureRpa((AdvertisingInstance*)data, base::DoNothing());
}
}  // namespace

void BleAdvertisingManager::Initialize(BleAdvertiserHciInterface* interface) {
  instance = new BleAdvertisingManagerImpl(interface);
  instance_weakptr = ((BleAdvertisingManagerImpl*)instance)->GetWeakPtr();
}

bool BleAdvertisingManager::IsInitialized() { return instance; }

base::WeakPtr<BleAdvertisingManager> BleAdvertisingManager::Get() {
  return instance_weakptr;
};

void BleAdvertisingManager::CleanUp() {
  if (instance_weakptr.get()) instance_weakptr.get()->CancelAdvAlarms();

  delete instance;
  instance = nullptr;
};

/**
 * This function initialize the advertising manager.
 **/
void btm_ble_adv_init() {
  BleAdvertiserHciInterface::Initialize();
  BleAdvertisingManager::Initialize(BleAdvertiserHciInterface::Get());
  BleAdvertiserHciInterface::Get()->SetAdvertisingEventObserver(
      (BleAdvertisingManagerImpl*)BleAdvertisingManager::Get().get());

  if (BleAdvertiserHciInterface::Get()->QuirkAdvertiserZeroHandle()) {
    // If handle 0 can't be used, register advertiser for it, but never use it.
    // TODO: avoid generating/rotating RPA for it
    BleAdvertisingManager::Get().get()->RegisterAdvertiser(base::DoNothing());
  }
}

/*******************************************************************************
 *
 * Function         btm_ble_multi_adv_cleanup
 *
 * Description      This function cleans up multi adv control block.
 *
 * Parameters
 * Returns          void
 *
 ******************************************************************************/
void btm_ble_multi_adv_cleanup(void) {
  BleAdvertisingManager::CleanUp();
  BleAdvertiserHciInterface::CleanUp();
}

// TODO(jpawlowski): Find a nicer way to test RecomputeTimeout without exposing
// AdvertisingInstance
bool timeout_triggered = false;
void test_timeout_cb(uint8_t status) { timeout_triggered = true; }

// verify that if duration passed, or is about to pass, recomputation will shut
// down the advertiser completly
void testRecomputeTimeout1() {
  auto manager = (BleAdvertisingManagerImpl*)BleAdvertisingManager::Get().get();

  TimeTicks start = TimeTicks::Now();
#if BASE_VER < 931007
  TimeTicks end = start + TimeDelta::FromMilliseconds(111);
#else
  TimeTicks end = start + base::Milliseconds(111);
#endif
  AdvertisingInstance test1(0);
  test1.enable_status = true;
  test1.enable_time = start;
  test1.duration = 12 /*120ms*/;
  test1.timeout_cb = Bind(&test_timeout_cb);

  manager->RecomputeTimeout(&test1, end);

  CHECK(timeout_triggered);
  timeout_triggered = false;
  CHECK(!test1.enable_status);
}

// verify that duration and maxExtAdvEvents are properly adjusted when
// recomputing.
void testRecomputeTimeout2() {
  auto manager = (BleAdvertisingManagerImpl*)BleAdvertisingManager::Get().get();

  TimeTicks start = TimeTicks::Now();
#if BASE_VER < 931007
  TimeTicks end = start + TimeDelta::FromMilliseconds(250);
#else
  TimeTicks end = start + base::Milliseconds(250);
#endif
  AdvertisingInstance test1(0);
  test1.enable_status = true;
  test1.enable_time = start;
  test1.duration = 50 /*500ms*/;
  test1.maxExtAdvEvents = 50;
  test1.advertising_interval = 16 /* 10 ms */;
  test1.timeout_cb = Bind(&test_timeout_cb);

  manager->RecomputeTimeout(&test1, end);

  CHECK(!timeout_triggered);
  CHECK(test1.enable_status);
  CHECK(test1.duration == 25);
  CHECK(test1.maxExtAdvEvents == 25);
}

// verify that if maxExtAdvEvents were sent, or are close to end, recomputation
// wil shut down the advertiser completly
void testRecomputeTimeout3() {
  auto manager = (BleAdvertisingManagerImpl*)BleAdvertisingManager::Get().get();

  TimeTicks start = TimeTicks::Now();
#if BASE_VER < 931007
  TimeTicks end = start + TimeDelta::FromMilliseconds(495);
#else
  TimeTicks end = start + base::Milliseconds(495);
#endif
  AdvertisingInstance test1(0);
  test1.enable_status = true;
  test1.enable_time = start;
  test1.maxExtAdvEvents = 50;
  test1.advertising_interval = 16 /* 10 ms */;
  test1.timeout_cb = Bind(&test_timeout_cb);

  manager->RecomputeTimeout(&test1, end);

  CHECK(timeout_triggered);
  timeout_triggered = false;
  CHECK(!test1.enable_status);
}