simplernble 1.2.1-dev129 → 1.2.1-dev131

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/native/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1-dev129
1
+ 1.2.1-dev131
@@ -52,10 +52,12 @@ class VM {
52
52
  case JNI_OK:
53
53
  break;
54
54
  case JNI_EDETACHED: {
55
+ // NOTE: Threads attached here are never detached. A desktop JVM only exits once its non-daemon
56
+ // threads finish, so they attach as daemons there.
55
57
  #ifdef __ANDROID__
56
58
  auto result = jvm->AttachCurrentThread(&env, nullptr);
57
59
  #else
58
- auto result = jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr);
60
+ auto result = jvm->AttachCurrentThreadAsDaemon(reinterpret_cast<void**>(&env), nullptr);
59
61
  #endif
60
62
  if (result != JNI_OK) {
61
63
  throw std::runtime_error("Failed to attach the current thread to the JVM");
@@ -57,6 +57,20 @@ void SIMPLEBLE_EXPORT set_passkey_display_callback(Peripheral& peripheral,
57
57
  void SIMPLEBLE_EXPORT set_numeric_comparison_callback(Peripheral& peripheral,
58
58
  const std::function<bool(const std::string& passkey)>& callback);
59
59
 
60
+ /**
61
+ * Secure the connection with LE Secure Connections.
62
+ *
63
+ * Pairs with the connected peer and waits until the link is encrypted using LE
64
+ * Secure Connections and a 128-bit key. With mitm set, the peer must also be
65
+ * authenticated through the passkey or numeric comparison callbacks. If the
66
+ * link already meets these requirements, for example through an existing bond,
67
+ * the call returns right away. Pairing started by this call creates no bond.
68
+ *
69
+ * Throws if the link can't be secured as requested, in which case the Dongl
70
+ * disconnects. The Dongl gives up after 30 seconds.
71
+ */
72
+ void SIMPLEBLE_EXPORT secure_connection(Peripheral& peripheral, bool mitm = false);
73
+
60
74
  } // namespace SimpleBLE::Advanced::Dongl
61
75
 
62
76
  /**
@@ -344,5 +344,13 @@ void AdapterDongl::_on_simpleble_event(const simpleble_Event& event) {
344
344
  }
345
345
  break;
346
346
  }
347
+
348
+ case simpleble_Event_security_complete_evt_tag: {
349
+ // Routed by request id: the link might be gone by the time the result arrives.
350
+ for (auto& [address, peripheral] : this->peripherals_) {
351
+ peripheral->notify_security_complete(event.evt.security_complete_evt);
352
+ }
353
+ break;
354
+ }
347
355
  }
348
356
  }
@@ -19,4 +19,8 @@ void set_numeric_comparison_callback(Peripheral& peripheral,
19
19
  Factory::get_internal<PeripheralDongl>(peripheral).set_numeric_comparison_callback(callback);
20
20
  }
21
21
 
22
+ void secure_connection(Peripheral& peripheral, bool mitm) {
23
+ Factory::get_internal<PeripheralDongl>(peripheral).secure_connection(mitm);
24
+ }
25
+
22
26
  } // namespace SimpleBLE::Advanced::Dongl
@@ -8,6 +8,7 @@
8
8
 
9
9
  #include <algorithm>
10
10
  #include <cstdint>
11
+ #include <cstdlib>
11
12
  #include <functional>
12
13
  #include <memory>
13
14
  #include <thread>
@@ -186,6 +187,53 @@ void PeripheralDongl::set_numeric_comparison_callback(const std::function<bool(c
186
187
  }
187
188
  }
188
189
 
190
+ void PeripheralDongl::secure_connection(bool mitm) {
191
+ if (!is_connected()) throw Exception::NotConnected();
192
+
193
+ // The result can arrive before the response to the command, so register the request first. The id is random so
194
+ // that a late result from an earlier request isn't taken for this one.
195
+ const uint8_t request_id = std::rand() % 255 + 1;
196
+ {
197
+ std::lock_guard<std::mutex> lock(connection_mutex_);
198
+ _security_result.reset();
199
+ _security_request_id = request_id;
200
+ }
201
+
202
+ simpleble_SecureConnectionRsp response;
203
+ try {
204
+ response = _serial_protocol->simpleble_secure_connection(_conn_handle, request_id, mitm);
205
+ } catch (...) {
206
+ std::lock_guard<std::mutex> lock(connection_mutex_);
207
+ _security_request_id = 0;
208
+ throw;
209
+ }
210
+ if (response.ret_code != 0) {
211
+ std::lock_guard<std::mutex> lock(connection_mutex_);
212
+ _security_request_id = 0;
213
+ throw Exception::OperationFailed(fmt::format("Failed to secure the connection: {}", response.ret_code));
214
+ }
215
+
216
+ simpleble_SecurityCompleteEvt result;
217
+ {
218
+ std::unique_lock<std::mutex> lock(connection_mutex_);
219
+ connection_cv_.wait_for(lock, SECURITY_RESULT_TIMEOUT,
220
+ [this]() { return _security_result.has_value() || _lost; });
221
+ _security_request_id = 0;
222
+ if (!_security_result.has_value()) {
223
+ if (_lost) throw Exception::OperationFailed("Lost the connection to the Dongl");
224
+ throw Exception::OperationFailed("Dongl did not report the security result");
225
+ }
226
+ result = *_security_result;
227
+ }
228
+
229
+ if (result.ret_code != 0) {
230
+ // The Dongl disconnects when it can't secure the connection, so wait for the disconnection.
231
+ std::unique_lock<std::mutex> lock(disconnection_mutex_);
232
+ disconnection_cv_.wait_for(lock, 500ms, [this]() { return !is_connected(); });
233
+ throw Exception::OperationFailed(fmt::format("Failed to secure the connection: {}", result.ret_code));
234
+ }
235
+ }
236
+
189
237
  SharedPtrVector<ServiceBase> PeripheralDongl::available_services() {
190
238
  SharedPtrVector<ServiceBase> service_list;
191
239
  for (auto& service : _services) {
@@ -626,6 +674,16 @@ void PeripheralDongl::notify_auth_key_request(simpleble_AuthKeyRequestEvt const&
626
674
  });
627
675
  }
628
676
 
677
+ void PeripheralDongl::notify_security_complete(simpleble_SecurityCompleteEvt const& evt) {
678
+ {
679
+ std::lock_guard<std::mutex> lock(connection_mutex_);
680
+ // Every peripheral receives this event, so only take the result of our own request.
681
+ if (evt.request_id != _security_request_id) return;
682
+ _security_result = evt;
683
+ }
684
+ connection_cv_.notify_all();
685
+ }
686
+
629
687
  void PeripheralDongl::_enqueue(std::function<void()> func) {
630
688
  std::lock_guard<std::mutex> lock(scheduler_mutex_);
631
689
  if (!scheduler_) {
@@ -50,6 +50,7 @@ class PeripheralDongl : public PeripheralBase {
50
50
  void set_passkey_request_callback(const std::function<std::optional<std::string>()>& callback);
51
51
  void set_passkey_display_callback(const std::function<void(const std::string& passkey)>& callback);
52
52
  void set_numeric_comparison_callback(const std::function<bool(const std::string& passkey)>& callback);
53
+ void secure_connection(bool mitm);
53
54
 
54
55
  virtual std::vector<std::shared_ptr<ServiceBase>> available_services() override;
55
56
  virtual std::vector<std::shared_ptr<ServiceBase>> advertised_services() override;
@@ -85,6 +86,7 @@ class PeripheralDongl : public PeripheralBase {
85
86
  void notify_value_changed(simpleble_ValueChangedEvt const& value_changed_evt);
86
87
  void notify_passkey_display(simpleble_PasskeyDisplayEvt const& passkey_display_evt);
87
88
  void notify_auth_key_request(simpleble_AuthKeyRequestEvt const& auth_key_request_evt);
89
+ void notify_security_complete(simpleble_SecurityCompleteEvt const& security_complete_evt);
88
90
 
89
91
  const uint16_t BLE_CONN_HANDLE_INVALID = 0xFFFF;
90
92
  const uint16_t BLE_CONN_HANDLE_PENDING = 0xFFFE;
@@ -118,6 +120,8 @@ class PeripheralDongl : public PeripheralBase {
118
120
 
119
121
  static constexpr std::chrono::milliseconds CONNECT_TIMEOUT{10000};
120
122
  static constexpr std::chrono::milliseconds CONNECT_RESULT_MARGIN{5000};
123
+ // The Dongl gives up on securing a connection after 30 seconds.
124
+ static constexpr std::chrono::milliseconds SECURITY_RESULT_TIMEOUT{35000};
121
125
 
122
126
  void _resolve_missing_uuids();
123
127
 
@@ -156,6 +160,8 @@ class PeripheralDongl : public PeripheralBase {
156
160
  std::optional<simpleble_ConnectCompleteEvt> _connect_result;
157
161
  bool _connect_pending = false; // Guarded by connection_mutex_.
158
162
  bool _lost = false; // Guarded by connection_mutex_.
163
+ std::optional<simpleble_SecurityCompleteEvt> _security_result;
164
+ uint8_t _security_request_id = 0; // Guarded by connection_mutex_. Zero when no request is pending.
159
165
 
160
166
  std::shared_ptr<Dongl::Serial::Protocol> _serial_protocol;
161
167
 
@@ -78,6 +78,9 @@ PB_BIND(simpleble_GetPairedPeripheralCmd, simpleble_GetPairedPeripheralCmd, AUTO
78
78
  PB_BIND(simpleble_GetPairedPeripheralCountCmd, simpleble_GetPairedPeripheralCountCmd, AUTO)
79
79
 
80
80
 
81
+ PB_BIND(simpleble_SecureConnectionCmd, simpleble_SecureConnectionCmd, AUTO)
82
+
83
+
81
84
  PB_BIND(simpleble_InitRsp, simpleble_InitRsp, AUTO)
82
85
 
83
86
 
@@ -117,6 +120,9 @@ PB_BIND(simpleble_GetPairedPeripheralRsp, simpleble_GetPairedPeripheralRsp, AUTO
117
120
  PB_BIND(simpleble_GetPairedPeripheralCountRsp, simpleble_GetPairedPeripheralCountRsp, AUTO)
118
121
 
119
122
 
123
+ PB_BIND(simpleble_SecureConnectionRsp, simpleble_SecureConnectionRsp, AUTO)
124
+
125
+
120
126
  PB_BIND(simpleble_AdvEvt, simpleble_AdvEvt, 2)
121
127
 
122
128
 
@@ -147,6 +153,9 @@ PB_BIND(simpleble_PasskeyDisplayEvt, simpleble_PasskeyDisplayEvt, AUTO)
147
153
  PB_BIND(simpleble_AuthKeyRequestEvt, simpleble_AuthKeyRequestEvt, AUTO)
148
154
 
149
155
 
156
+ PB_BIND(simpleble_SecurityCompleteEvt, simpleble_SecurityCompleteEvt, AUTO)
157
+
158
+
150
159
  PB_BIND(simpleble_Command, simpleble_Command, 2)
151
160
 
152
161
 
@@ -182,6 +182,13 @@ typedef struct _simpleble_GetPairedPeripheralCountCmd {
182
182
  char dummy_field;
183
183
  } simpleble_GetPairedPeripheralCountCmd;
184
184
 
185
+ /* Secures the current link without creating/replacing a bond. One explicit request at a time. */
186
+ typedef struct _simpleble_SecureConnectionCmd {
187
+ uint16_t conn_handle;
188
+ uint8_t request_id; /* Random, nonzero correlation key. */
189
+ bool mitm;
190
+ } simpleble_SecureConnectionCmd;
191
+
185
192
  typedef struct _simpleble_InitRsp {
186
193
  uint32_t ret_code;
187
194
  } simpleble_InitRsp;
@@ -240,6 +247,10 @@ typedef struct _simpleble_GetPairedPeripheralCountRsp {
240
247
  uint16_t count;
241
248
  } simpleble_GetPairedPeripheralCountRsp;
242
249
 
250
+ typedef struct _simpleble_SecureConnectionRsp {
251
+ uint32_t ret_code;
252
+ } simpleble_SecureConnectionRsp;
253
+
243
254
  typedef struct _simpleble_AdvEvt {
244
255
  char identifier[32];
245
256
  simpleble_BluetoothAddressType address_type;
@@ -321,6 +332,17 @@ typedef struct _simpleble_AuthKeyRequestEvt {
321
332
  uint32_t request_id;
322
333
  } simpleble_AuthKeyRequestEvt;
323
334
 
335
+ /* Exactly one completion for an accepted request, within 30 seconds. Failure disconnects the link. */
336
+ typedef struct _simpleble_SecurityCompleteEvt {
337
+ uint16_t conn_handle;
338
+ uint8_t request_id;
339
+ uint32_t ret_code;
340
+ bool encrypted;
341
+ bool lesc;
342
+ bool mitm;
343
+ uint8_t key_size;
344
+ } simpleble_SecurityCompleteEvt;
345
+
324
346
  typedef struct _simpleble_Command {
325
347
  pb_size_t which_cmd;
326
348
  union {
@@ -337,6 +359,7 @@ typedef struct _simpleble_Command {
337
359
  simpleble_AuthKeyReplyCmd auth_key_reply;
338
360
  simpleble_GetPairedPeripheralCmd get_paired_peripheral;
339
361
  simpleble_GetPairedPeripheralCountCmd get_paired_peripheral_count;
362
+ simpleble_SecureConnectionCmd secure_connection;
340
363
  } cmd;
341
364
  } simpleble_Command;
342
365
 
@@ -356,6 +379,7 @@ typedef struct _simpleble_Response {
356
379
  simpleble_AuthKeyReplyRsp auth_key_reply;
357
380
  simpleble_GetPairedPeripheralRsp get_paired_peripheral;
358
381
  simpleble_GetPairedPeripheralCountRsp get_paired_peripheral_count;
382
+ simpleble_SecureConnectionRsp secure_connection;
359
383
  } rsp;
360
384
  } simpleble_Response;
361
385
 
@@ -372,6 +396,7 @@ typedef struct _simpleble_Event {
372
396
  simpleble_ValueChangedEvt value_changed_evt;
373
397
  simpleble_PasskeyDisplayEvt passkey_display_evt;
374
398
  simpleble_AuthKeyRequestEvt auth_key_request_evt;
399
+ simpleble_SecurityCompleteEvt security_complete_evt;
375
400
  } evt;
376
401
  } simpleble_Event;
377
402
 
@@ -440,9 +465,11 @@ extern "C" {
440
465
 
441
466
 
442
467
 
468
+
443
469
  #define simpleble_GetPairedPeripheralRsp_address_type_ENUMTYPE simpleble_BluetoothAddressType
444
470
 
445
471
 
472
+
446
473
  #define simpleble_AdvEvt_address_type_ENUMTYPE simpleble_BluetoothAddressType
447
474
 
448
475
 
@@ -461,6 +488,7 @@ extern "C" {
461
488
 
462
489
 
463
490
 
491
+
464
492
  /* Initializer values for message structs */
465
493
  #define simpleble_UUID16Bit_init_default {0}
466
494
  #define simpleble_UUID32Bit_init_default {0}
@@ -486,6 +514,7 @@ extern "C" {
486
514
  #define simpleble_UnpairCmd_init_default {_simpleble_BluetoothAddressType_MIN, ""}
487
515
  #define simpleble_GetPairedPeripheralCmd_init_default {0}
488
516
  #define simpleble_GetPairedPeripheralCountCmd_init_default {0}
517
+ #define simpleble_SecureConnectionCmd_init_default {0, 0, 0}
489
518
  #define simpleble_InitRsp_init_default {0}
490
519
  #define simpleble_ScanStartRsp_init_default {0}
491
520
  #define simpleble_ScanStopRsp_init_default {0}
@@ -499,6 +528,7 @@ extern "C" {
499
528
  #define simpleble_UnpairRsp_init_default {0}
500
529
  #define simpleble_GetPairedPeripheralRsp_init_default {0, _simpleble_BluetoothAddressType_MIN, ""}
501
530
  #define simpleble_GetPairedPeripheralCountRsp_init_default {0}
531
+ #define simpleble_SecureConnectionRsp_init_default {0}
502
532
  #define simpleble_AdvEvt_init_default {"", _simpleble_BluetoothAddressType_MIN, "", 0, 0, 0, 0, {simpleble_ManufacturerDataEntry_init_default, simpleble_ManufacturerDataEntry_init_default, simpleble_ManufacturerDataEntry_init_default, simpleble_ManufacturerDataEntry_init_default}, 0, {simpleble_ServiceDataEntry_init_default, simpleble_ServiceDataEntry_init_default, simpleble_ServiceDataEntry_init_default, simpleble_ServiceDataEntry_init_default}, 0, 0, 0}
503
533
  #define simpleble_ConnectionEvt_init_default {"", 0}
504
534
  #define simpleble_DisconnectionEvt_init_default {0}
@@ -509,6 +539,7 @@ extern "C" {
509
539
  #define simpleble_ValueChangedEvt_init_default {0, 0, _simpleble_ValueChangedType_MIN, {0, {0}}}
510
540
  #define simpleble_PasskeyDisplayEvt_init_default {0, "", 0, 0}
511
541
  #define simpleble_AuthKeyRequestEvt_init_default {0, _simpleble_PairingAuthKeyType_MIN, 0}
542
+ #define simpleble_SecurityCompleteEvt_init_default {0, 0, 0, 0, 0, 0, 0}
512
543
  #define simpleble_Command_init_default {0, {simpleble_InitCmd_init_default}}
513
544
  #define simpleble_Response_init_default {0, {simpleble_InitRsp_init_default}}
514
545
  #define simpleble_Event_init_default {0, {simpleble_AdvEvt_init_default}}
@@ -536,6 +567,7 @@ extern "C" {
536
567
  #define simpleble_UnpairCmd_init_zero {_simpleble_BluetoothAddressType_MIN, ""}
537
568
  #define simpleble_GetPairedPeripheralCmd_init_zero {0}
538
569
  #define simpleble_GetPairedPeripheralCountCmd_init_zero {0}
570
+ #define simpleble_SecureConnectionCmd_init_zero {0, 0, 0}
539
571
  #define simpleble_InitRsp_init_zero {0}
540
572
  #define simpleble_ScanStartRsp_init_zero {0}
541
573
  #define simpleble_ScanStopRsp_init_zero {0}
@@ -549,6 +581,7 @@ extern "C" {
549
581
  #define simpleble_UnpairRsp_init_zero {0}
550
582
  #define simpleble_GetPairedPeripheralRsp_init_zero {0, _simpleble_BluetoothAddressType_MIN, ""}
551
583
  #define simpleble_GetPairedPeripheralCountRsp_init_zero {0}
584
+ #define simpleble_SecureConnectionRsp_init_zero {0}
552
585
  #define simpleble_AdvEvt_init_zero {"", _simpleble_BluetoothAddressType_MIN, "", 0, 0, 0, 0, {simpleble_ManufacturerDataEntry_init_zero, simpleble_ManufacturerDataEntry_init_zero, simpleble_ManufacturerDataEntry_init_zero, simpleble_ManufacturerDataEntry_init_zero}, 0, {simpleble_ServiceDataEntry_init_zero, simpleble_ServiceDataEntry_init_zero, simpleble_ServiceDataEntry_init_zero, simpleble_ServiceDataEntry_init_zero}, 0, 0, 0}
553
586
  #define simpleble_ConnectionEvt_init_zero {"", 0}
554
587
  #define simpleble_DisconnectionEvt_init_zero {0}
@@ -559,6 +592,7 @@ extern "C" {
559
592
  #define simpleble_ValueChangedEvt_init_zero {0, 0, _simpleble_ValueChangedType_MIN, {0, {0}}}
560
593
  #define simpleble_PasskeyDisplayEvt_init_zero {0, "", 0, 0}
561
594
  #define simpleble_AuthKeyRequestEvt_init_zero {0, _simpleble_PairingAuthKeyType_MIN, 0}
595
+ #define simpleble_SecurityCompleteEvt_init_zero {0, 0, 0, 0, 0, 0, 0}
562
596
  #define simpleble_Command_init_zero {0, {simpleble_InitCmd_init_zero}}
563
597
  #define simpleble_Response_init_zero {0, {simpleble_InitRsp_init_zero}}
564
598
  #define simpleble_Event_init_zero {0, {simpleble_AdvEvt_init_zero}}
@@ -612,6 +646,9 @@ extern "C" {
612
646
  #define simpleble_UnpairCmd_address_type_tag 1
613
647
  #define simpleble_UnpairCmd_address_tag 2
614
648
  #define simpleble_GetPairedPeripheralCmd_index_tag 1
649
+ #define simpleble_SecureConnectionCmd_conn_handle_tag 1
650
+ #define simpleble_SecureConnectionCmd_request_id_tag 2
651
+ #define simpleble_SecureConnectionCmd_mitm_tag 3
615
652
  #define simpleble_InitRsp_ret_code_tag 1
616
653
  #define simpleble_ScanStartRsp_ret_code_tag 1
617
654
  #define simpleble_ScanStopRsp_ret_code_tag 1
@@ -630,6 +667,7 @@ extern "C" {
630
667
  #define simpleble_GetPairedPeripheralRsp_address_type_tag 2
631
668
  #define simpleble_GetPairedPeripheralRsp_address_tag 3
632
669
  #define simpleble_GetPairedPeripheralCountRsp_count_tag 1
670
+ #define simpleble_SecureConnectionRsp_ret_code_tag 1
633
671
  #define simpleble_AdvEvt_identifier_tag 1
634
672
  #define simpleble_AdvEvt_address_type_tag 2
635
673
  #define simpleble_AdvEvt_address_tag 3
@@ -672,6 +710,13 @@ extern "C" {
672
710
  #define simpleble_AuthKeyRequestEvt_conn_handle_tag 1
673
711
  #define simpleble_AuthKeyRequestEvt_key_type_tag 2
674
712
  #define simpleble_AuthKeyRequestEvt_request_id_tag 3
713
+ #define simpleble_SecurityCompleteEvt_conn_handle_tag 1
714
+ #define simpleble_SecurityCompleteEvt_request_id_tag 2
715
+ #define simpleble_SecurityCompleteEvt_ret_code_tag 3
716
+ #define simpleble_SecurityCompleteEvt_encrypted_tag 4
717
+ #define simpleble_SecurityCompleteEvt_lesc_tag 5
718
+ #define simpleble_SecurityCompleteEvt_mitm_tag 6
719
+ #define simpleble_SecurityCompleteEvt_key_size_tag 7
675
720
  #define simpleble_Command_init_tag 1
676
721
  #define simpleble_Command_scan_start_tag 2
677
722
  #define simpleble_Command_scan_stop_tag 3
@@ -685,6 +730,7 @@ extern "C" {
685
730
  #define simpleble_Command_auth_key_reply_tag 11
686
731
  #define simpleble_Command_get_paired_peripheral_tag 12
687
732
  #define simpleble_Command_get_paired_peripheral_count_tag 13
733
+ #define simpleble_Command_secure_connection_tag 14
688
734
  #define simpleble_Response_init_tag 1
689
735
  #define simpleble_Response_scan_start_tag 2
690
736
  #define simpleble_Response_scan_stop_tag 3
@@ -698,6 +744,7 @@ extern "C" {
698
744
  #define simpleble_Response_auth_key_reply_tag 11
699
745
  #define simpleble_Response_get_paired_peripheral_tag 12
700
746
  #define simpleble_Response_get_paired_peripheral_count_tag 13
747
+ #define simpleble_Response_secure_connection_tag 14
701
748
  #define simpleble_Event_adv_evt_tag 1
702
749
  #define simpleble_Event_connection_evt_tag 2
703
750
  #define simpleble_Event_disconnection_evt_tag 3
@@ -708,6 +755,7 @@ extern "C" {
708
755
  #define simpleble_Event_value_changed_evt_tag 8
709
756
  #define simpleble_Event_passkey_display_evt_tag 9
710
757
  #define simpleble_Event_auth_key_request_evt_tag 10
758
+ #define simpleble_Event_security_complete_evt_tag 11
711
759
 
712
760
  /* Struct field encoding specification for nanopb */
713
761
  #define simpleble_UUID16Bit_FIELDLIST(X, a) \
@@ -870,6 +918,13 @@ X(a, STATIC, SINGULAR, UINT32, index, 1)
870
918
  #define simpleble_GetPairedPeripheralCountCmd_CALLBACK NULL
871
919
  #define simpleble_GetPairedPeripheralCountCmd_DEFAULT NULL
872
920
 
921
+ #define simpleble_SecureConnectionCmd_FIELDLIST(X, a) \
922
+ X(a, STATIC, SINGULAR, UINT32, conn_handle, 1) \
923
+ X(a, STATIC, SINGULAR, UINT32, request_id, 2) \
924
+ X(a, STATIC, SINGULAR, BOOL, mitm, 3)
925
+ #define simpleble_SecureConnectionCmd_CALLBACK NULL
926
+ #define simpleble_SecureConnectionCmd_DEFAULT NULL
927
+
873
928
  #define simpleble_InitRsp_FIELDLIST(X, a) \
874
929
  X(a, STATIC, SINGULAR, UINT32, ret_code, 1)
875
930
  #define simpleble_InitRsp_CALLBACK NULL
@@ -940,6 +995,11 @@ X(a, STATIC, SINGULAR, UINT32, count, 1)
940
995
  #define simpleble_GetPairedPeripheralCountRsp_CALLBACK NULL
941
996
  #define simpleble_GetPairedPeripheralCountRsp_DEFAULT NULL
942
997
 
998
+ #define simpleble_SecureConnectionRsp_FIELDLIST(X, a) \
999
+ X(a, STATIC, SINGULAR, UINT32, ret_code, 1)
1000
+ #define simpleble_SecureConnectionRsp_CALLBACK NULL
1001
+ #define simpleble_SecureConnectionRsp_DEFAULT NULL
1002
+
943
1003
  #define simpleble_AdvEvt_FIELDLIST(X, a) \
944
1004
  X(a, STATIC, SINGULAR, STRING, identifier, 1) \
945
1005
  X(a, STATIC, SINGULAR, UENUM, address_type, 2) \
@@ -1028,6 +1088,17 @@ X(a, STATIC, SINGULAR, UINT32, request_id, 3)
1028
1088
  #define simpleble_AuthKeyRequestEvt_CALLBACK NULL
1029
1089
  #define simpleble_AuthKeyRequestEvt_DEFAULT NULL
1030
1090
 
1091
+ #define simpleble_SecurityCompleteEvt_FIELDLIST(X, a) \
1092
+ X(a, STATIC, SINGULAR, UINT32, conn_handle, 1) \
1093
+ X(a, STATIC, SINGULAR, UINT32, request_id, 2) \
1094
+ X(a, STATIC, SINGULAR, UINT32, ret_code, 3) \
1095
+ X(a, STATIC, SINGULAR, BOOL, encrypted, 4) \
1096
+ X(a, STATIC, SINGULAR, BOOL, lesc, 5) \
1097
+ X(a, STATIC, SINGULAR, BOOL, mitm, 6) \
1098
+ X(a, STATIC, SINGULAR, UINT32, key_size, 7)
1099
+ #define simpleble_SecurityCompleteEvt_CALLBACK NULL
1100
+ #define simpleble_SecurityCompleteEvt_DEFAULT NULL
1101
+
1031
1102
  #define simpleble_Command_FIELDLIST(X, a) \
1032
1103
  X(a, STATIC, ONEOF, MESSAGE, (cmd,init,cmd.init), 1) \
1033
1104
  X(a, STATIC, ONEOF, MESSAGE, (cmd,scan_start,cmd.scan_start), 2) \
@@ -1041,7 +1112,8 @@ X(a, STATIC, ONEOF, MESSAGE, (cmd,is_paired,cmd.is_paired), 9) \
1041
1112
  X(a, STATIC, ONEOF, MESSAGE, (cmd,unpair,cmd.unpair), 10) \
1042
1113
  X(a, STATIC, ONEOF, MESSAGE, (cmd,auth_key_reply,cmd.auth_key_reply), 11) \
1043
1114
  X(a, STATIC, ONEOF, MESSAGE, (cmd,get_paired_peripheral,cmd.get_paired_peripheral), 12) \
1044
- X(a, STATIC, ONEOF, MESSAGE, (cmd,get_paired_peripheral_count,cmd.get_paired_peripheral_count), 13)
1115
+ X(a, STATIC, ONEOF, MESSAGE, (cmd,get_paired_peripheral_count,cmd.get_paired_peripheral_count), 13) \
1116
+ X(a, STATIC, ONEOF, MESSAGE, (cmd,secure_connection,cmd.secure_connection), 14)
1045
1117
  #define simpleble_Command_CALLBACK NULL
1046
1118
  #define simpleble_Command_DEFAULT NULL
1047
1119
  #define simpleble_Command_cmd_init_MSGTYPE simpleble_InitCmd
@@ -1057,6 +1129,7 @@ X(a, STATIC, ONEOF, MESSAGE, (cmd,get_paired_peripheral_count,cmd.get_pair
1057
1129
  #define simpleble_Command_cmd_auth_key_reply_MSGTYPE simpleble_AuthKeyReplyCmd
1058
1130
  #define simpleble_Command_cmd_get_paired_peripheral_MSGTYPE simpleble_GetPairedPeripheralCmd
1059
1131
  #define simpleble_Command_cmd_get_paired_peripheral_count_MSGTYPE simpleble_GetPairedPeripheralCountCmd
1132
+ #define simpleble_Command_cmd_secure_connection_MSGTYPE simpleble_SecureConnectionCmd
1060
1133
 
1061
1134
  #define simpleble_Response_FIELDLIST(X, a) \
1062
1135
  X(a, STATIC, ONEOF, MESSAGE, (rsp,init,rsp.init), 1) \
@@ -1071,7 +1144,8 @@ X(a, STATIC, ONEOF, MESSAGE, (rsp,is_paired,rsp.is_paired), 9) \
1071
1144
  X(a, STATIC, ONEOF, MESSAGE, (rsp,unpair,rsp.unpair), 10) \
1072
1145
  X(a, STATIC, ONEOF, MESSAGE, (rsp,auth_key_reply,rsp.auth_key_reply), 11) \
1073
1146
  X(a, STATIC, ONEOF, MESSAGE, (rsp,get_paired_peripheral,rsp.get_paired_peripheral), 12) \
1074
- X(a, STATIC, ONEOF, MESSAGE, (rsp,get_paired_peripheral_count,rsp.get_paired_peripheral_count), 13)
1147
+ X(a, STATIC, ONEOF, MESSAGE, (rsp,get_paired_peripheral_count,rsp.get_paired_peripheral_count), 13) \
1148
+ X(a, STATIC, ONEOF, MESSAGE, (rsp,secure_connection,rsp.secure_connection), 14)
1075
1149
  #define simpleble_Response_CALLBACK NULL
1076
1150
  #define simpleble_Response_DEFAULT NULL
1077
1151
  #define simpleble_Response_rsp_init_MSGTYPE simpleble_InitRsp
@@ -1087,6 +1161,7 @@ X(a, STATIC, ONEOF, MESSAGE, (rsp,get_paired_peripheral_count,rsp.get_pair
1087
1161
  #define simpleble_Response_rsp_auth_key_reply_MSGTYPE simpleble_AuthKeyReplyRsp
1088
1162
  #define simpleble_Response_rsp_get_paired_peripheral_MSGTYPE simpleble_GetPairedPeripheralRsp
1089
1163
  #define simpleble_Response_rsp_get_paired_peripheral_count_MSGTYPE simpleble_GetPairedPeripheralCountRsp
1164
+ #define simpleble_Response_rsp_secure_connection_MSGTYPE simpleble_SecureConnectionRsp
1090
1165
 
1091
1166
  #define simpleble_Event_FIELDLIST(X, a) \
1092
1167
  X(a, STATIC, ONEOF, MESSAGE, (evt,adv_evt,evt.adv_evt), 1) \
@@ -1098,7 +1173,8 @@ X(a, STATIC, ONEOF, MESSAGE, (evt,descriptor_discovered_evt,evt.descriptor
1098
1173
  X(a, STATIC, ONEOF, MESSAGE, (evt,connect_complete_evt,evt.connect_complete_evt), 7) \
1099
1174
  X(a, STATIC, ONEOF, MESSAGE, (evt,value_changed_evt,evt.value_changed_evt), 8) \
1100
1175
  X(a, STATIC, ONEOF, MESSAGE, (evt,passkey_display_evt,evt.passkey_display_evt), 9) \
1101
- X(a, STATIC, ONEOF, MESSAGE, (evt,auth_key_request_evt,evt.auth_key_request_evt), 10)
1176
+ X(a, STATIC, ONEOF, MESSAGE, (evt,auth_key_request_evt,evt.auth_key_request_evt), 10) \
1177
+ X(a, STATIC, ONEOF, MESSAGE, (evt,security_complete_evt,evt.security_complete_evt), 11)
1102
1178
  #define simpleble_Event_CALLBACK NULL
1103
1179
  #define simpleble_Event_DEFAULT NULL
1104
1180
  #define simpleble_Event_evt_adv_evt_MSGTYPE simpleble_AdvEvt
@@ -1111,6 +1187,7 @@ X(a, STATIC, ONEOF, MESSAGE, (evt,auth_key_request_evt,evt.auth_key_reques
1111
1187
  #define simpleble_Event_evt_value_changed_evt_MSGTYPE simpleble_ValueChangedEvt
1112
1188
  #define simpleble_Event_evt_passkey_display_evt_MSGTYPE simpleble_PasskeyDisplayEvt
1113
1189
  #define simpleble_Event_evt_auth_key_request_evt_MSGTYPE simpleble_AuthKeyRequestEvt
1190
+ #define simpleble_Event_evt_security_complete_evt_MSGTYPE simpleble_SecurityCompleteEvt
1114
1191
 
1115
1192
  extern const pb_msgdesc_t simpleble_UUID16Bit_msg;
1116
1193
  extern const pb_msgdesc_t simpleble_UUID32Bit_msg;
@@ -1136,6 +1213,7 @@ extern const pb_msgdesc_t simpleble_IsPairedCmd_msg;
1136
1213
  extern const pb_msgdesc_t simpleble_UnpairCmd_msg;
1137
1214
  extern const pb_msgdesc_t simpleble_GetPairedPeripheralCmd_msg;
1138
1215
  extern const pb_msgdesc_t simpleble_GetPairedPeripheralCountCmd_msg;
1216
+ extern const pb_msgdesc_t simpleble_SecureConnectionCmd_msg;
1139
1217
  extern const pb_msgdesc_t simpleble_InitRsp_msg;
1140
1218
  extern const pb_msgdesc_t simpleble_ScanStartRsp_msg;
1141
1219
  extern const pb_msgdesc_t simpleble_ScanStopRsp_msg;
@@ -1149,6 +1227,7 @@ extern const pb_msgdesc_t simpleble_IsPairedRsp_msg;
1149
1227
  extern const pb_msgdesc_t simpleble_UnpairRsp_msg;
1150
1228
  extern const pb_msgdesc_t simpleble_GetPairedPeripheralRsp_msg;
1151
1229
  extern const pb_msgdesc_t simpleble_GetPairedPeripheralCountRsp_msg;
1230
+ extern const pb_msgdesc_t simpleble_SecureConnectionRsp_msg;
1152
1231
  extern const pb_msgdesc_t simpleble_AdvEvt_msg;
1153
1232
  extern const pb_msgdesc_t simpleble_ConnectionEvt_msg;
1154
1233
  extern const pb_msgdesc_t simpleble_DisconnectionEvt_msg;
@@ -1159,6 +1238,7 @@ extern const pb_msgdesc_t simpleble_ConnectCompleteEvt_msg;
1159
1238
  extern const pb_msgdesc_t simpleble_ValueChangedEvt_msg;
1160
1239
  extern const pb_msgdesc_t simpleble_PasskeyDisplayEvt_msg;
1161
1240
  extern const pb_msgdesc_t simpleble_AuthKeyRequestEvt_msg;
1241
+ extern const pb_msgdesc_t simpleble_SecurityCompleteEvt_msg;
1162
1242
  extern const pb_msgdesc_t simpleble_Command_msg;
1163
1243
  extern const pb_msgdesc_t simpleble_Response_msg;
1164
1244
  extern const pb_msgdesc_t simpleble_Event_msg;
@@ -1188,6 +1268,7 @@ extern const pb_msgdesc_t simpleble_Event_msg;
1188
1268
  #define simpleble_UnpairCmd_fields &simpleble_UnpairCmd_msg
1189
1269
  #define simpleble_GetPairedPeripheralCmd_fields &simpleble_GetPairedPeripheralCmd_msg
1190
1270
  #define simpleble_GetPairedPeripheralCountCmd_fields &simpleble_GetPairedPeripheralCountCmd_msg
1271
+ #define simpleble_SecureConnectionCmd_fields &simpleble_SecureConnectionCmd_msg
1191
1272
  #define simpleble_InitRsp_fields &simpleble_InitRsp_msg
1192
1273
  #define simpleble_ScanStartRsp_fields &simpleble_ScanStartRsp_msg
1193
1274
  #define simpleble_ScanStopRsp_fields &simpleble_ScanStopRsp_msg
@@ -1201,6 +1282,7 @@ extern const pb_msgdesc_t simpleble_Event_msg;
1201
1282
  #define simpleble_UnpairRsp_fields &simpleble_UnpairRsp_msg
1202
1283
  #define simpleble_GetPairedPeripheralRsp_fields &simpleble_GetPairedPeripheralRsp_msg
1203
1284
  #define simpleble_GetPairedPeripheralCountRsp_fields &simpleble_GetPairedPeripheralCountRsp_msg
1285
+ #define simpleble_SecureConnectionRsp_fields &simpleble_SecureConnectionRsp_msg
1204
1286
  #define simpleble_AdvEvt_fields &simpleble_AdvEvt_msg
1205
1287
  #define simpleble_ConnectionEvt_fields &simpleble_ConnectionEvt_msg
1206
1288
  #define simpleble_DisconnectionEvt_fields &simpleble_DisconnectionEvt_msg
@@ -1211,6 +1293,7 @@ extern const pb_msgdesc_t simpleble_Event_msg;
1211
1293
  #define simpleble_ValueChangedEvt_fields &simpleble_ValueChangedEvt_msg
1212
1294
  #define simpleble_PasskeyDisplayEvt_fields &simpleble_PasskeyDisplayEvt_msg
1213
1295
  #define simpleble_AuthKeyRequestEvt_fields &simpleble_AuthKeyRequestEvt_msg
1296
+ #define simpleble_SecurityCompleteEvt_fields &simpleble_SecurityCompleteEvt_msg
1214
1297
  #define simpleble_Command_fields &simpleble_Command_msg
1215
1298
  #define simpleble_Response_fields &simpleble_Response_msg
1216
1299
  #define simpleble_Event_fields &simpleble_Event_msg
@@ -1255,6 +1338,9 @@ extern const pb_msgdesc_t simpleble_Event_msg;
1255
1338
  #define simpleble_ScanStartRsp_size 6
1256
1339
  #define simpleble_ScanStopCmd_size 0
1257
1340
  #define simpleble_ScanStopRsp_size 6
1341
+ #define simpleble_SecureConnectionCmd_size 9
1342
+ #define simpleble_SecureConnectionRsp_size 6
1343
+ #define simpleble_SecurityCompleteEvt_size 22
1258
1344
  #define simpleble_ServiceDataEntry_size 51
1259
1345
  #define simpleble_ServiceDiscoveredEvt_size 34
1260
1346
  #define simpleble_Service_size 30
@@ -236,3 +236,16 @@ simpleble_GetPairedPeripheralCountRsp Protocol::simpleble_get_paired_peripheral_
236
236
  dongl_Response response = exchange(command);
237
237
  return response.rsp.simpleble.rsp.get_paired_peripheral_count;
238
238
  }
239
+
240
+ simpleble_SecureConnectionRsp Protocol::simpleble_secure_connection(uint16_t conn_handle, uint8_t request_id,
241
+ bool mitm) {
242
+ dongl_Command command = dongl_Command_init_zero;
243
+ command.which_cmd = dongl_Command_simpleble_tag;
244
+ command.cmd.simpleble.which_cmd = simpleble_Command_secure_connection_tag;
245
+ command.cmd.simpleble.cmd.secure_connection.conn_handle = conn_handle;
246
+ command.cmd.simpleble.cmd.secure_connection.request_id = request_id;
247
+ command.cmd.simpleble.cmd.secure_connection.mitm = mitm;
248
+
249
+ dongl_Response response = exchange(command);
250
+ return response.rsp.simpleble.rsp.secure_connection;
251
+ }
@@ -42,6 +42,7 @@ class Protocol : public ProtocolBase {
42
42
  const std::vector<uint8_t>& key, bool accept);
43
43
  simpleble_GetPairedPeripheralRsp simpleble_get_paired_peripheral(uint16_t index);
44
44
  simpleble_GetPairedPeripheralCountRsp simpleble_get_paired_peripheral_count();
45
+ simpleble_SecureConnectionRsp simpleble_secure_connection(uint16_t conn_handle, uint8_t request_id, bool mitm);
45
46
  };
46
47
 
47
48
  } // namespace Serial
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplernble",
3
- "version": "1.2.1-dev129",
3
+ "version": "1.2.1-dev131",
4
4
  "description": "SimpleBLE for React Native: a cross-platform Bluetooth Low Energy library for iOS and Android",
5
5
  "keywords": [
6
6
  "react-native",
@@ -8,32 +8,32 @@
8
8
  <key>BinaryPath</key>
9
9
  <string>libsimpleble.a</string>
10
10
  <key>LibraryIdentifier</key>
11
- <string>ios-arm64_x86_64-simulator</string>
11
+ <string>ios-arm64</string>
12
12
  <key>LibraryPath</key>
13
13
  <string>libsimpleble.a</string>
14
14
  <key>SupportedArchitectures</key>
15
15
  <array>
16
16
  <string>arm64</string>
17
- <string>x86_64</string>
18
17
  </array>
19
18
  <key>SupportedPlatform</key>
20
19
  <string>ios</string>
21
- <key>SupportedPlatformVariant</key>
22
- <string>simulator</string>
23
20
  </dict>
24
21
  <dict>
25
22
  <key>BinaryPath</key>
26
23
  <string>libsimpleble.a</string>
27
24
  <key>LibraryIdentifier</key>
28
- <string>ios-arm64</string>
25
+ <string>ios-arm64_x86_64-simulator</string>
29
26
  <key>LibraryPath</key>
30
27
  <string>libsimpleble.a</string>
31
28
  <key>SupportedArchitectures</key>
32
29
  <array>
33
30
  <string>arm64</string>
31
+ <string>x86_64</string>
34
32
  </array>
35
33
  <key>SupportedPlatform</key>
36
34
  <string>ios</string>
35
+ <key>SupportedPlatformVariant</key>
36
+ <string>simulator</string>
37
37
  </dict>
38
38
  </array>
39
39
  <key>CFBundlePackageType</key>
@@ -57,6 +57,20 @@ void SIMPLEBLE_EXPORT set_passkey_display_callback(Peripheral& peripheral,
57
57
  void SIMPLEBLE_EXPORT set_numeric_comparison_callback(Peripheral& peripheral,
58
58
  const std::function<bool(const std::string& passkey)>& callback);
59
59
 
60
+ /**
61
+ * Secure the connection with LE Secure Connections.
62
+ *
63
+ * Pairs with the connected peer and waits until the link is encrypted using LE
64
+ * Secure Connections and a 128-bit key. With mitm set, the peer must also be
65
+ * authenticated through the passkey or numeric comparison callbacks. If the
66
+ * link already meets these requirements, for example through an existing bond,
67
+ * the call returns right away. Pairing started by this call creates no bond.
68
+ *
69
+ * Throws if the link can't be secured as requested, in which case the Dongl
70
+ * disconnects. The Dongl gives up after 30 seconds.
71
+ */
72
+ void SIMPLEBLE_EXPORT secure_connection(Peripheral& peripheral, bool mitm = false);
73
+
60
74
  } // namespace SimpleBLE::Advanced::Dongl
61
75
 
62
76
  /**