simplejsble 0.0.4 → 0.0.6

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.
Files changed (273) hide show
  1. package/VERSION +1 -0
  2. package/android/CMakeLists.txt +62 -20
  3. package/cmake/epilogue.cmake +81 -0
  4. package/cmake/find/FindDBus1.cmake +20 -0
  5. package/cmake/find/Findfmt.cmake +41 -0
  6. package/cmake/find/Findgoogletest.cmake +18 -0
  7. package/cmake/parse_version.cmake +45 -0
  8. package/cmake/prelude.cmake +8 -0
  9. package/dependencies/README +4 -0
  10. package/dependencies/external/kvn/kvn_bytearray.h +297 -0
  11. package/dependencies/external/kvn/kvn_safe_callback.hpp +66 -0
  12. package/dependencies/external/kvn/logfwd.hpp +35 -0
  13. package/dependencies/internal/include/fmt/LICENSE +27 -0
  14. package/dependencies/internal/include/fmt/args.h +220 -0
  15. package/dependencies/internal/include/fmt/base.h +2962 -0
  16. package/dependencies/internal/include/fmt/chrono.h +2338 -0
  17. package/dependencies/internal/include/fmt/color.h +610 -0
  18. package/dependencies/internal/include/fmt/compile.h +539 -0
  19. package/dependencies/internal/include/fmt/core.h +5 -0
  20. package/dependencies/internal/include/fmt/format-inl.h +1949 -0
  21. package/dependencies/internal/include/fmt/format.h +4244 -0
  22. package/dependencies/internal/include/fmt/os.h +427 -0
  23. package/dependencies/internal/include/fmt/ostream.h +166 -0
  24. package/dependencies/internal/include/fmt/printf.h +633 -0
  25. package/dependencies/internal/include/fmt/ranges.h +850 -0
  26. package/dependencies/internal/include/fmt/std.h +726 -0
  27. package/dependencies/internal/include/fmt/xchar.h +373 -0
  28. package/dependencies/internal/include/nanopb/pb.h +948 -0
  29. package/dependencies/internal/include/nanopb/pb_common.h +49 -0
  30. package/dependencies/internal/include/nanopb/pb_decode.h +204 -0
  31. package/dependencies/internal/include/nanopb/pb_encode.h +195 -0
  32. package/dependencies/internal/include/simplejni/Common.hpp +598 -0
  33. package/dependencies/internal/include/simplejni/References.hpp +151 -0
  34. package/dependencies/internal/include/simplejni/Registry.hpp +249 -0
  35. package/dependencies/internal/include/simplejni/VM.hpp +93 -0
  36. package/dependencies/internal/src/nanopb/pb_common.c +388 -0
  37. package/dependencies/internal/src/nanopb/pb_decode.c +1763 -0
  38. package/dependencies/internal/src/nanopb/pb_encode.c +1009 -0
  39. package/dependencies/internal/src/simplejni/Common.cpp +4 -0
  40. package/package.json +5 -1
  41. package/simpleble/CMakeLists.txt +519 -0
  42. package/simpleble/cmake/simpleble.pc.in +11 -0
  43. package/simpleble/include/simpleble/Adapter.h +102 -0
  44. package/simpleble/include/simpleble/AdapterSafe.h +58 -0
  45. package/simpleble/include/simpleble/Advanced.h +50 -0
  46. package/simpleble/include/simpleble/Characteristic.h +39 -0
  47. package/simpleble/include/simpleble/Config.h +64 -0
  48. package/simpleble/include/simpleble/Descriptor.h +30 -0
  49. package/simpleble/include/simpleble/Exceptions.h +72 -0
  50. package/simpleble/include/simpleble/Logging.h +73 -0
  51. package/simpleble/include/simpleble/Peripheral.h +82 -0
  52. package/simpleble/include/simpleble/PeripheralSafe.h +64 -0
  53. package/simpleble/include/simpleble/Service.h +34 -0
  54. package/simpleble/include/simpleble/SimpleBLE.h +8 -0
  55. package/simpleble/include/simpleble/Types.h +49 -0
  56. package/simpleble/include/simpleble/Utils.h +13 -0
  57. package/simpleble/include/simpleble_c/DEPRECATED +2 -0
  58. package/simpleble/include/simpleble_c/adapter.h +188 -0
  59. package/simpleble/include/simpleble_c/logging.h +37 -0
  60. package/simpleble/include/simpleble_c/peripheral.h +304 -0
  61. package/simpleble/include/simpleble_c/simpleble.h +21 -0
  62. package/simpleble/include/simpleble_c/types.h +73 -0
  63. package/simpleble/include/simpleble_c/utils.h +27 -0
  64. package/simpleble/src/CommonUtils.h +63 -0
  65. package/simpleble/src/Config.cpp +25 -0
  66. package/simpleble/src/Exceptions.cpp +31 -0
  67. package/simpleble/src/Logging.cpp +136 -0
  68. package/simpleble/src/LoggingInternal.h +85 -0
  69. package/simpleble/src/Utils.cpp +24 -0
  70. package/simpleble/src/backends/android/AdapterAndroid.cpp +101 -0
  71. package/simpleble/src/backends/android/AdapterAndroid.h +67 -0
  72. package/simpleble/src/backends/android/BackendAndroid.cpp +41 -0
  73. package/simpleble/src/backends/android/BackendAndroid.h +26 -0
  74. package/simpleble/src/backends/android/PeripheralAndroid.cpp +365 -0
  75. package/simpleble/src/backends/android/PeripheralAndroid.h +90 -0
  76. package/simpleble/src/backends/android/bridge/BluetoothGattCallback.cpp +432 -0
  77. package/simpleble/src/backends/android/bridge/BluetoothGattCallback.h +102 -0
  78. package/simpleble/src/backends/android/bridge/ScanCallback.cpp +142 -0
  79. package/simpleble/src/backends/android/bridge/ScanCallback.h +55 -0
  80. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.cpp +107 -0
  81. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.h +55 -0
  82. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.cpp +68 -0
  83. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.h +54 -0
  84. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.cpp +115 -0
  85. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.h +75 -0
  86. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.cpp +142 -0
  87. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.h +66 -0
  88. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.cpp +67 -0
  89. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.h +46 -0
  90. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.cpp +106 -0
  91. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.h +47 -0
  92. package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.cpp +47 -0
  93. package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.h +37 -0
  94. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.cpp +69 -0
  95. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.h +41 -0
  96. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.cpp +63 -0
  97. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.h +42 -0
  98. package/simpleble/src/backends/android/types/android/os/ParcelUUID.cpp +32 -0
  99. package/simpleble/src/backends/android/types/android/os/ParcelUUID.h +30 -0
  100. package/simpleble/src/backends/android/types/android/util/SparseArray.cpp +54 -0
  101. package/simpleble/src/backends/android/types/android/util/SparseArray.h +37 -0
  102. package/simpleble/src/backends/android/types/java/util/Iterator.cpp +36 -0
  103. package/simpleble/src/backends/android/types/java/util/Iterator.h +28 -0
  104. package/simpleble/src/backends/android/types/java/util/List.cpp +29 -0
  105. package/simpleble/src/backends/android/types/java/util/List.h +27 -0
  106. package/simpleble/src/backends/android/types/java/util/Set.cpp +33 -0
  107. package/simpleble/src/backends/android/types/java/util/Set.h +28 -0
  108. package/simpleble/src/backends/android/types/java/util/UUID.cpp +26 -0
  109. package/simpleble/src/backends/android/types/java/util/UUID.h +29 -0
  110. package/simpleble/src/backends/common/AdapterBase.cpp +53 -0
  111. package/simpleble/src/backends/common/AdapterBase.h +81 -0
  112. package/simpleble/src/backends/common/AdapterBaseTypes.h +22 -0
  113. package/simpleble/src/backends/common/BackendBase.h +20 -0
  114. package/simpleble/src/backends/common/BackendUtils.h +33 -0
  115. package/simpleble/src/backends/common/CharacteristicBase.cpp +28 -0
  116. package/simpleble/src/backends/common/CharacteristicBase.h +38 -0
  117. package/simpleble/src/backends/common/DescriptorBase.cpp +7 -0
  118. package/simpleble/src/backends/common/DescriptorBase.h +19 -0
  119. package/simpleble/src/backends/common/PeripheralBase.h +82 -0
  120. package/simpleble/src/backends/common/ServiceBase.cpp +18 -0
  121. package/simpleble/src/backends/common/ServiceBase.h +28 -0
  122. package/simpleble/src/backends/dongl/AdapterBaseTypes.h +22 -0
  123. package/simpleble/src/backends/dongl/AdapterDongl.cpp +369 -0
  124. package/simpleble/src/backends/dongl/AdapterDongl.h +60 -0
  125. package/simpleble/src/backends/dongl/BackendDongl.cpp +35 -0
  126. package/simpleble/src/backends/dongl/PeripheralDongl.cpp +562 -0
  127. package/simpleble/src/backends/dongl/PeripheralDongl.h +136 -0
  128. package/simpleble/src/backends/dongl/protocol/basic.pb.c +33 -0
  129. package/simpleble/src/backends/dongl/protocol/basic.pb.h +172 -0
  130. package/simpleble/src/backends/dongl/protocol/d2h.pb.c +18 -0
  131. package/simpleble/src/backends/dongl/protocol/d2h.pb.h +118 -0
  132. package/simpleble/src/backends/dongl/protocol/h2d.pb.c +12 -0
  133. package/simpleble/src/backends/dongl/protocol/h2d.pb.h +69 -0
  134. package/simpleble/src/backends/dongl/protocol/simpleble.pb.c +123 -0
  135. package/simpleble/src/backends/dongl/protocol/simpleble.pb.h +908 -0
  136. package/simpleble/src/backends/dongl/protocol/softdevice.pb.c +18 -0
  137. package/simpleble/src/backends/dongl/protocol/softdevice.pb.h +815 -0
  138. package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.c +339 -0
  139. package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.h +2086 -0
  140. package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.c +114 -0
  141. package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.h +772 -0
  142. package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.c +117 -0
  143. package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.h +766 -0
  144. package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.c +207 -0
  145. package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.h +1686 -0
  146. package/simpleble/src/backends/dongl/serial/Protocol.cpp +887 -0
  147. package/simpleble/src/backends/dongl/serial/Protocol.h +117 -0
  148. package/simpleble/src/backends/dongl/serial/ProtocolBase.cpp +95 -0
  149. package/simpleble/src/backends/dongl/serial/ProtocolBase.h +54 -0
  150. package/simpleble/src/backends/dongl/serial/Wire.cpp +133 -0
  151. package/simpleble/src/backends/dongl/serial/Wire.h +116 -0
  152. package/simpleble/src/backends/dongl/usb/UsbHelper.cpp +53 -0
  153. package/simpleble/src/backends/dongl/usb/UsbHelper.h +32 -0
  154. package/simpleble/src/backends/dongl/usb/UsbHelperApple.cpp +266 -0
  155. package/simpleble/src/backends/dongl/usb/UsbHelperApple.h +34 -0
  156. package/simpleble/src/backends/dongl/usb/UsbHelperImpl.h +29 -0
  157. package/simpleble/src/backends/dongl/usb/UsbHelperLinux.cpp +21 -0
  158. package/simpleble/src/backends/dongl/usb/UsbHelperLinux.h +22 -0
  159. package/simpleble/src/backends/dongl/usb/UsbHelperNull.cpp +21 -0
  160. package/simpleble/src/backends/dongl/usb/UsbHelperNull.h +22 -0
  161. package/simpleble/src/backends/dongl/usb/UsbHelperWindows.cpp +21 -0
  162. package/simpleble/src/backends/dongl/usb/UsbHelperWindows.h +22 -0
  163. package/simpleble/src/backends/linux/AdapterLinux.cpp +102 -0
  164. package/simpleble/src/backends/linux/AdapterLinux.h +55 -0
  165. package/simpleble/src/backends/linux/BackendBluez.cpp +90 -0
  166. package/simpleble/src/backends/linux/PeripheralLinux.cpp +394 -0
  167. package/simpleble/src/backends/linux/PeripheralLinux.h +90 -0
  168. package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.cpp +102 -0
  169. package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.h +55 -0
  170. package/simpleble/src/backends/linux_legacy/BackendBluezLegacy.cpp +87 -0
  171. package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.cpp +376 -0
  172. package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.h +90 -0
  173. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Adapter.h +46 -0
  174. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Agent.h +52 -0
  175. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Bluez.h +37 -0
  176. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrg.h +22 -0
  177. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrgBluez.h +26 -0
  178. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezRoot.h +32 -0
  179. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Characteristic.h +47 -0
  180. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Descriptor.h +33 -0
  181. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Device.h +64 -0
  182. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Exceptions.h +43 -0
  183. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Service.h +27 -0
  184. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Types.h +9 -0
  185. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Adapter1.h +49 -0
  186. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Agent1.h +100 -0
  187. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/AgentManager1.h +24 -0
  188. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Battery1.h +32 -0
  189. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Device1.h +62 -0
  190. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattCharacteristic1.h +48 -0
  191. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattDescriptor1.h +41 -0
  192. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattService1.h +28 -0
  193. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Interface.h +67 -0
  194. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/InterfaceRegistry.h +62 -0
  195. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Proxy.h +117 -0
  196. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Connection.h +50 -0
  197. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Exceptions.h +56 -0
  198. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Holder.h +147 -0
  199. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Logging.h +57 -0
  200. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Message.h +89 -0
  201. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Path.h +24 -0
  202. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/interfaces/ObjectManager.h +26 -0
  203. package/simpleble/src/backends/linux_legacy/src/Adapter.cpp +78 -0
  204. package/simpleble/src/backends/linux_legacy/src/Agent.cpp +72 -0
  205. package/simpleble/src/backends/linux_legacy/src/Bluez.cpp +45 -0
  206. package/simpleble/src/backends/linux_legacy/src/BluezOrg.cpp +20 -0
  207. package/simpleble/src/backends/linux_legacy/src/BluezOrgBluez.cpp +23 -0
  208. package/simpleble/src/backends/linux_legacy/src/BluezRoot.cpp +44 -0
  209. package/simpleble/src/backends/linux_legacy/src/Characteristic.cpp +64 -0
  210. package/simpleble/src/backends/linux_legacy/src/Descriptor.cpp +27 -0
  211. package/simpleble/src/backends/linux_legacy/src/Device.cpp +104 -0
  212. package/simpleble/src/backends/linux_legacy/src/Exceptions.cpp +29 -0
  213. package/simpleble/src/backends/linux_legacy/src/Service.cpp +33 -0
  214. package/simpleble/src/backends/linux_legacy/src/advanced/Interface.cpp +155 -0
  215. package/simpleble/src/backends/linux_legacy/src/advanced/Proxy.cpp +374 -0
  216. package/simpleble/src/backends/linux_legacy/src/base/Connection.cpp +219 -0
  217. package/simpleble/src/backends/linux_legacy/src/base/Exceptions.cpp +39 -0
  218. package/simpleble/src/backends/linux_legacy/src/base/Holder.cpp +739 -0
  219. package/simpleble/src/backends/linux_legacy/src/base/Message.cpp +622 -0
  220. package/simpleble/src/backends/linux_legacy/src/base/Path.cpp +129 -0
  221. package/simpleble/src/backends/linux_legacy/src/interfaces/Adapter1.cpp +123 -0
  222. package/simpleble/src/backends/linux_legacy/src/interfaces/Agent1.cpp +143 -0
  223. package/simpleble/src/backends/linux_legacy/src/interfaces/AgentManager1.cpp +34 -0
  224. package/simpleble/src/backends/linux_legacy/src/interfaces/Battery1.cpp +30 -0
  225. package/simpleble/src/backends/linux_legacy/src/interfaces/Device1.cpp +170 -0
  226. package/simpleble/src/backends/linux_legacy/src/interfaces/GattCharacteristic1.cpp +118 -0
  227. package/simpleble/src/backends/linux_legacy/src/interfaces/GattDescriptor1.cpp +78 -0
  228. package/simpleble/src/backends/linux_legacy/src/interfaces/GattService1.cpp +28 -0
  229. package/simpleble/src/backends/linux_legacy/src/interfaces/ObjectManager.cpp +58 -0
  230. package/simpleble/src/backends/macos/AdapterBaseMacOS.h +29 -0
  231. package/simpleble/src/backends/macos/AdapterBaseMacOS.mm +235 -0
  232. package/simpleble/src/backends/macos/AdapterMac.h +78 -0
  233. package/simpleble/src/backends/macos/AdapterMac.mm +156 -0
  234. package/simpleble/src/backends/macos/BackendCoreBluetooth.mm +45 -0
  235. package/simpleble/src/backends/macos/PeripheralBaseMacOS.h +49 -0
  236. package/simpleble/src/backends/macos/PeripheralBaseMacOS.mm +714 -0
  237. package/simpleble/src/backends/macos/PeripheralMac.h +81 -0
  238. package/simpleble/src/backends/macos/PeripheralMac.mm +229 -0
  239. package/simpleble/src/backends/macos/Utils.h +9 -0
  240. package/simpleble/src/backends/macos/Utils.mm +23 -0
  241. package/simpleble/src/backends/plain/AdapterPlain.cpp +65 -0
  242. package/simpleble/src/backends/plain/AdapterPlain.h +49 -0
  243. package/simpleble/src/backends/plain/BackendPlain.cpp +30 -0
  244. package/simpleble/src/backends/plain/PeripheralPlain.cpp +159 -0
  245. package/simpleble/src/backends/plain/PeripheralPlain.h +72 -0
  246. package/simpleble/src/backends/windows/AdapterWindows.cpp +338 -0
  247. package/simpleble/src/backends/windows/AdapterWindows.h +89 -0
  248. package/simpleble/src/backends/windows/BackendWinRT.cpp +67 -0
  249. package/simpleble/src/backends/windows/BackendWinRT.h +18 -0
  250. package/simpleble/src/backends/windows/MtaManager.cpp +49 -0
  251. package/simpleble/src/backends/windows/MtaManager.h +90 -0
  252. package/simpleble/src/backends/windows/PeripheralWindows.cpp +487 -0
  253. package/simpleble/src/backends/windows/PeripheralWindows.h +129 -0
  254. package/simpleble/src/backends/windows/Utils.cpp +146 -0
  255. package/simpleble/src/backends/windows/Utils.h +47 -0
  256. package/simpleble/src/builders/BuildVec.h +32 -0
  257. package/simpleble/src/builders/BuilderBase.h +87 -0
  258. package/simpleble/src/external/TaskRunner.hpp +99 -0
  259. package/simpleble/src/external/ThreadRunner.h +52 -0
  260. package/simpleble/src/external/kvn_safe_callback.hpp +66 -0
  261. package/simpleble/src/external/kvn_safe_map.hpp +94 -0
  262. package/simpleble/src/external/kvn_threadrunner.hpp +70 -0
  263. package/simpleble/src/external/logfwd.hpp +35 -0
  264. package/simpleble/src/frontends/base/Adapter.cpp +111 -0
  265. package/simpleble/src/frontends/base/Advanced.cpp +34 -0
  266. package/simpleble/src/frontends/base/Backend.cpp +95 -0
  267. package/simpleble/src/frontends/base/Backend.h +76 -0
  268. package/simpleble/src/frontends/base/Characteristic.cpp +56 -0
  269. package/simpleble/src/frontends/base/Descriptor.cpp +21 -0
  270. package/simpleble/src/frontends/base/Peripheral.cpp +113 -0
  271. package/simpleble/src/frontends/base/Service.cpp +26 -0
  272. package/simpleble/src/frontends/safe/AdapterSafe.cpp +158 -0
  273. package/simpleble/src/frontends/safe/PeripheralSafe.cpp +219 -0
@@ -0,0 +1,562 @@
1
+ #include "PeripheralDongl.h"
2
+
3
+ #include "CharacteristicBase.h"
4
+ #include "DescriptorBase.h"
5
+ #include "ServiceBase.h"
6
+
7
+ #include <simpleble/Exceptions.h>
8
+
9
+ #include <cstdint>
10
+ #include <memory>
11
+ #include <thread>
12
+
13
+ #include "CommonUtils.h"
14
+ #include "LoggingInternal.h"
15
+ #include "fmt/chrono.h"
16
+ #include "protocol/simpleble.pb.h"
17
+ #include "simpleble/Types.h"
18
+
19
+ #include <fmt/core.h>
20
+
21
+ using namespace SimpleBLE;
22
+ using namespace std::chrono_literals;
23
+
24
+ PeripheralDongl::PeripheralDongl(std::shared_ptr<Dongl::Serial::Protocol> serial_protocol,
25
+ advertising_data_t advertising_data) {
26
+ _serial_protocol = serial_protocol;
27
+ _address_type = advertising_data.address_type;
28
+ _identifier = advertising_data.identifier;
29
+ _address = advertising_data.mac_address;
30
+ _connectable = advertising_data.connectable;
31
+ update_advertising_data(advertising_data);
32
+ }
33
+
34
+ PeripheralDongl::~PeripheralDongl() {}
35
+
36
+ void* PeripheralDongl::underlying() const { return nullptr; }
37
+
38
+ std::string PeripheralDongl::identifier() { return _identifier; }
39
+
40
+ BluetoothAddress PeripheralDongl::address() { return _address; }
41
+
42
+ BluetoothAddressType PeripheralDongl::address_type() { return _address_type; }
43
+
44
+ int16_t PeripheralDongl::rssi() { return _rssi; }
45
+
46
+ int16_t PeripheralDongl::tx_power() { return _tx_power; }
47
+
48
+ uint16_t PeripheralDongl::mtu() { return 0; }
49
+
50
+ void PeripheralDongl::connect() {
51
+ if (is_connected()) {
52
+ return;
53
+ }
54
+
55
+ bool connection_successful = false;
56
+ for (int i = 0; i < 10; i++) {
57
+ fmt::print("PeripheralDongl::connect: attempt {}\n", i);
58
+ connection_successful = _attempt_connect();
59
+ fmt::print("PeripheralDongl::connect: attempt {} - success: {}\n", i, connection_successful);
60
+ if (connection_successful) {
61
+ break;
62
+ } else {
63
+ std::this_thread::sleep_for(750ms);
64
+ }
65
+ }
66
+
67
+ if (!connection_successful) {
68
+ throw Exception::OperationFailed(fmt::format("Connection failed to be established"));
69
+ }
70
+
71
+ SAFE_CALLBACK_CALL(this->_callback_on_connected);
72
+ fmt::print("PeripheralDongl::connect: connected\n");
73
+ }
74
+
75
+ void PeripheralDongl::disconnect() {
76
+ if (!is_connected()) {
77
+ return;
78
+ }
79
+
80
+ auto response = _serial_protocol->simpleble_disconnect(_conn_handle);
81
+ if (response.ret_code != 0) {
82
+ throw Exception::OperationFailed(fmt::format("Failed to disconnect: {}", response.ret_code));
83
+ }
84
+
85
+ // Wait for the disconnection to be confirmed.
86
+ std::unique_lock<std::mutex> lock(disconnection_mutex_);
87
+ disconnection_cv_.wait_for(lock, 500ms, [this]() { return !is_connected(); });
88
+
89
+ if (is_connected()) {
90
+ _conn_handle = BLE_CONN_HANDLE_INVALID;
91
+ throw Exception::OperationFailed(fmt::format("Timeout while waiting for disconnection confirmation"));
92
+ }
93
+ SAFE_CALLBACK_CALL(this->_callback_on_disconnected);
94
+ }
95
+
96
+ bool PeripheralDongl::is_connected() { return _conn_handle != BLE_CONN_HANDLE_INVALID; }
97
+
98
+ bool PeripheralDongl::is_connectable() { return _connectable; }
99
+
100
+ bool PeripheralDongl::is_paired() { return false; }
101
+
102
+ void PeripheralDongl::unpair() {}
103
+
104
+ SharedPtrVector<ServiceBase> PeripheralDongl::available_services() {
105
+ SharedPtrVector<ServiceBase> service_list;
106
+ for (auto& service : _services) {
107
+ fmt::print("PeripheralDongl::available_services: Service UUID: {}\n", service.uuid);
108
+ SharedPtrVector<CharacteristicBase> characteristic_list;
109
+ for (auto& characteristic : service.characteristics) {
110
+ fmt::print("PeripheralDongl::available_services: Characteristic UUID: {}\n", characteristic.uuid);
111
+ SharedPtrVector<DescriptorBase> descriptor_list;
112
+ for (auto& descriptor : characteristic.descriptors) {
113
+ descriptor_list.push_back(std::make_shared<DescriptorBase>(descriptor.uuid));
114
+ }
115
+ characteristic_list.push_back(std::make_shared<CharacteristicBase>(
116
+ characteristic.uuid, descriptor_list, characteristic.can_read, characteristic.can_write_request,
117
+ characteristic.can_write_command, characteristic.can_notify, characteristic.can_indicate));
118
+ }
119
+ service_list.push_back(std::make_shared<ServiceBase>(service.uuid, characteristic_list));
120
+ }
121
+
122
+ return service_list;
123
+ }
124
+
125
+ SharedPtrVector<ServiceBase> PeripheralDongl::advertised_services() {
126
+ SharedPtrVector<ServiceBase> service_list;
127
+ for (auto& [service_uuid, data] : _service_data) {
128
+ service_list.push_back(std::make_shared<ServiceBase>(service_uuid, data));
129
+ }
130
+
131
+ return service_list;
132
+ }
133
+
134
+ std::map<uint16_t, ByteArray> PeripheralDongl::manufacturer_data() { return _manufacturer_data; }
135
+
136
+ ByteArray PeripheralDongl::read(BluetoothUUID const& service, BluetoothUUID const& characteristic) {
137
+ auto& char_def = _characteristic_definition(service, characteristic);
138
+
139
+ if (!char_def.can_read) {
140
+ throw Exception::OperationFailed(fmt::format("Characteristic {} is not readable", characteristic));
141
+ }
142
+
143
+ simpleble_ReadRsp rsp = _serial_protocol->simpleble_read(_conn_handle, char_def.handle_value);
144
+ if (rsp.ret_code != 0) {
145
+ throw Exception::OperationFailed(
146
+ fmt::format("Failed to read characteristic {} - ret_code: {}", characteristic, rsp.ret_code));
147
+ }
148
+
149
+ return ByteArray(rsp.data.bytes, rsp.data.size);
150
+ }
151
+
152
+ void PeripheralDongl::write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic,
153
+ ByteArray const& data) {
154
+ auto& char_def = _characteristic_definition(service, characteristic);
155
+
156
+ if (!char_def.can_write_request) {
157
+ throw Exception::OperationFailed(fmt::format("Characteristic {} is not writable", characteristic));
158
+ }
159
+
160
+ simpleble_WriteRsp rsp = _serial_protocol->simpleble_write(_conn_handle, char_def.handle_value,
161
+ simpleble_WriteOperation_WRITE_REQ, data);
162
+ if (rsp.ret_code != 0) {
163
+ throw Exception::OperationFailed(
164
+ fmt::format("Failed to write characteristic {} - ret_code: {}", characteristic, rsp.ret_code));
165
+ }
166
+ }
167
+
168
+ void PeripheralDongl::write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic,
169
+ ByteArray const& data) {
170
+ auto& char_def = _characteristic_definition(service, characteristic);
171
+
172
+ if (!char_def.can_write_command) {
173
+ throw Exception::OperationFailed(fmt::format("Characteristic {} is not writable", characteristic));
174
+ }
175
+
176
+ simpleble_WriteRsp rsp = _serial_protocol->simpleble_write(_conn_handle, char_def.handle_value,
177
+ simpleble_WriteOperation_WRITE_CMD, data);
178
+ if (rsp.ret_code != 0) {
179
+ throw Exception::OperationFailed(
180
+ fmt::format("Failed to write characteristic {} - ret_code: {}", characteristic, rsp.ret_code));
181
+ }
182
+ }
183
+
184
+ void PeripheralDongl::notify(BluetoothUUID const& service, BluetoothUUID const& characteristic,
185
+ std::function<void(ByteArray payload)> callback) {
186
+ auto& char_def = _characteristic_definition(service, characteristic);
187
+
188
+ if (!char_def.can_notify) {
189
+ throw Exception::OperationFailed(fmt::format("Characteristic {} is not notifyable", characteristic));
190
+ }
191
+
192
+ // Find the CCCD for the characteristic
193
+ for (auto& descriptor : char_def.descriptors) {
194
+ if (descriptor.uuid == "00002902-0000-1000-8000-00805f9b34fb") {
195
+ ByteArray data = {0x01, 0x00};
196
+ simpleble_WriteRsp rsp = _serial_protocol->simpleble_write(_conn_handle, descriptor.handle,
197
+ simpleble_WriteOperation_WRITE_REQ, data);
198
+ if (rsp.ret_code != 0) {
199
+ throw Exception::OperationFailed(
200
+ fmt::format("Failed to write characteristic {} - ret_code: {}", characteristic, rsp.ret_code));
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ void PeripheralDongl::indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic,
207
+ std::function<void(ByteArray payload)> callback) {
208
+ auto& char_def = _characteristic_definition(service, characteristic);
209
+
210
+ if (!char_def.can_indicate) {
211
+ throw Exception::OperationFailed(fmt::format("Characteristic {} is not indicateable", characteristic));
212
+ }
213
+
214
+ // Find the CCCD for the characteristic
215
+ for (auto& descriptor : char_def.descriptors) {
216
+ if (descriptor.uuid == "00002902-0000-1000-8000-00805f9b34fb") {
217
+ ByteArray data = {0x02, 0x00};
218
+ simpleble_WriteRsp rsp = _serial_protocol->simpleble_write(_conn_handle, descriptor.handle,
219
+ simpleble_WriteOperation_WRITE_REQ, data);
220
+ if (rsp.ret_code != 0) {
221
+ throw Exception::OperationFailed(
222
+ fmt::format("Failed to write characteristic {} - ret_code: {}", characteristic, rsp.ret_code));
223
+ }
224
+ }
225
+ }
226
+ }
227
+
228
+ void PeripheralDongl::unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) {
229
+ auto& char_def = _characteristic_definition(service, characteristic);
230
+
231
+ // Find the CCCD for the characteristic
232
+ for (auto& descriptor : char_def.descriptors) {
233
+ if (descriptor.uuid == "00002902-0000-1000-8000-00805f9b34fb") {
234
+ ByteArray data = {0x00, 0x00};
235
+ simpleble_WriteRsp rsp = _serial_protocol->simpleble_write(_conn_handle, descriptor.handle,
236
+ simpleble_WriteOperation_WRITE_REQ, data);
237
+ if (rsp.ret_code != 0) {
238
+ throw Exception::OperationFailed(
239
+ fmt::format("Failed to write characteristic {} - ret_code: {}", characteristic, rsp.ret_code));
240
+ }
241
+ }
242
+ }
243
+ }
244
+
245
+ ByteArray PeripheralDongl::read(BluetoothUUID const& service, BluetoothUUID const& characteristic,
246
+ BluetoothUUID const& descriptor) {
247
+ return {};
248
+ }
249
+
250
+ void PeripheralDongl::write(BluetoothUUID const& service, BluetoothUUID const& characteristic,
251
+ BluetoothUUID const& descriptor, ByteArray const& data) {}
252
+
253
+ void PeripheralDongl::set_callback_on_connected(std::function<void()> on_connected) {
254
+ if (on_connected) {
255
+ _callback_on_connected.load(std::move(on_connected));
256
+ } else {
257
+ _callback_on_connected.unload();
258
+ }
259
+ }
260
+
261
+ void PeripheralDongl::set_callback_on_disconnected(std::function<void()> on_disconnected) {
262
+ if (on_disconnected) {
263
+ _callback_on_disconnected.load(std::move(on_disconnected));
264
+ } else {
265
+ _callback_on_disconnected.unload();
266
+ }
267
+ }
268
+
269
+ uint16_t PeripheralDongl::conn_handle() const { return _conn_handle; }
270
+
271
+ void PeripheralDongl::update_advertising_data(advertising_data_t advertising_data) {
272
+ if (advertising_data.identifier != "") {
273
+ _identifier = advertising_data.identifier;
274
+ }
275
+ _rssi = advertising_data.rssi;
276
+ _tx_power = advertising_data.tx_power;
277
+ _manufacturer_data = advertising_data.manufacturer_data;
278
+
279
+ advertising_data.service_data.merge(_service_data);
280
+ _service_data = advertising_data.service_data;
281
+ }
282
+
283
+ bool PeripheralDongl::_attempt_connect() {
284
+ if (_conn_handle != BLE_CONN_HANDLE_INVALID) {
285
+ auto response = _serial_protocol->simpleble_disconnect(_conn_handle);
286
+ if (response.ret_code != 0) {
287
+ fmt::println("Failed to disconnect during connect attempt: {}", response.ret_code);
288
+ }
289
+
290
+ // Wait for the disconnection to be confirmed.
291
+ std::unique_lock<std::mutex> lock(disconnection_mutex_);
292
+ disconnection_cv_.wait_for(lock, 500ms, [this]() { return !is_connected(); });
293
+ }
294
+
295
+ fmt::println("Disconnection confirmed");
296
+ _conn_handle = BLE_CONN_HANDLE_INVALID;
297
+
298
+ auto response = _serial_protocol->simpleble_connect(static_cast<simpleble_BluetoothAddressType>(_address_type),
299
+ _address);
300
+ if (response.ret_code != 0) {
301
+ throw Exception::OperationFailed(fmt::format("Error when attempting to connect: {}", response.ret_code));
302
+ }
303
+
304
+ // NOTE: Bluetooth connections are non-acknowledged by the peripheral. The connected event that we get
305
+ // is just the confirmation that the connection packet was sent, not that it was received.
306
+ // Our only option is to wait a bit after the connection event is received to see if maybe a
307
+ // disconnection event arises too, in which case the connection failed.
308
+
309
+ // Wait for the connection to be confirmed.
310
+ {
311
+ std::unique_lock<std::mutex> lock(connection_mutex_);
312
+ connection_cv_.wait_for(lock, 5000ms, [this]() { return _conn_handle != BLE_CONN_HANDLE_INVALID; });
313
+ if (_conn_handle == BLE_CONN_HANDLE_INVALID) {
314
+ // SIMPLEBLE_LOG_ERROR("Timeout while waiting for connection confirmation");
315
+ fmt::println("Timeout while waiting for connection confirmation");
316
+ return false;
317
+ }
318
+ }
319
+
320
+ fmt::println("Connection confirmed");
321
+
322
+ // Wait for the attributes to be discovered.
323
+ {
324
+ std::unique_lock<std::mutex> lock(attributes_discovered_mutex_);
325
+ attributes_discovered_cv_.wait_for(
326
+ lock, 15000ms, [this]() { return !_services.empty() || _conn_handle == BLE_CONN_HANDLE_INVALID; });
327
+ if (_services.empty()) {
328
+ fmt::println("Timeout while waiting for attributes to be discovered");
329
+ return false;
330
+ }
331
+
332
+ if (_conn_handle == BLE_CONN_HANDLE_INVALID) {
333
+ fmt::println("Connection lost during attribute discovery");
334
+ return false;
335
+ }
336
+ }
337
+
338
+ fmt::println("Attributes discovered. {} services found", _services.size());
339
+
340
+ // Retrieve any missing 128-bit UUIDs.
341
+ for (auto& service : _services) {
342
+ // Fetch the service UUID if missing.
343
+ if (service.uuid.empty()) {
344
+ simpleble_ReadRsp rsp = _serial_protocol->simpleble_read(_conn_handle, service.start_handle);
345
+ if (rsp.ret_code != 0) {
346
+ fmt::println("Failed to read UUID for service {} - ret_code: {}", service.start_handle, rsp.ret_code);
347
+ continue;
348
+ }
349
+
350
+ if (rsp.data.size == 2) {
351
+ service.uuid = _uuid_from_uuid16(rsp.data.bytes[1] << 8 | rsp.data.bytes[0]);
352
+ } else if (rsp.data.size == 16) {
353
+ uint8_t uuid_128[16];
354
+ for (int i = 0; i < 16; i++) {
355
+ uuid_128[i] = rsp.data.bytes[15 - i];
356
+ }
357
+ service.uuid = _uuid_from_uuid128(uuid_128);
358
+ } else {
359
+ fmt::println("Unexpected UUID size: {}", rsp.data.size);
360
+ continue;
361
+ }
362
+ }
363
+
364
+ for (auto& characteristic : service.characteristics) {
365
+ // Fetch the characteristic UUID if missing.
366
+ if (characteristic.uuid.empty()) {
367
+ simpleble_ReadRsp rsp = _serial_protocol->simpleble_read(_conn_handle, characteristic.handle_decl);
368
+ if (rsp.ret_code != 0) {
369
+ fmt::println("Failed to read UUID for characteristic {} - ret_code: {}", characteristic.handle_decl,
370
+ rsp.ret_code);
371
+ continue;
372
+ }
373
+
374
+ if (rsp.data.size == 5) {
375
+ characteristic.uuid = _uuid_from_uuid16(rsp.data.bytes[4] << 8 | rsp.data.bytes[3]);
376
+ } else if (rsp.data.size == 19) {
377
+ uint8_t uuid_128[16];
378
+ for (int i = 0; i < 16; i++) {
379
+ uuid_128[i] = rsp.data.bytes[15 - i + 3];
380
+ }
381
+ characteristic.uuid = _uuid_from_uuid128(uuid_128);
382
+ }
383
+ }
384
+ }
385
+ }
386
+
387
+ fmt::println("Service discovery complete");
388
+
389
+ return true;
390
+ }
391
+
392
+ void PeripheralDongl::notify_connected(uint16_t conn_handle) {
393
+ fmt::print("PeripheralDongl::notify_connected: {}\n", conn_handle);
394
+ _conn_handle = conn_handle;
395
+ connection_cv_.notify_all();
396
+ }
397
+
398
+ void PeripheralDongl::notify_disconnected() {
399
+ fmt::print("PeripheralDongl::notify_disconnected: {}\n", _conn_handle);
400
+ _conn_handle = BLE_CONN_HANDLE_INVALID;
401
+ disconnection_cv_.notify_all();
402
+ attributes_discovered_cv_.notify_all();
403
+
404
+ // TODO: Only throw the callback if the disconection was unexpected.
405
+ // SAFE_CALLBACK_CALL(this->_callback_on_disconnected);
406
+ }
407
+
408
+ void PeripheralDongl::notify_service_discovered(simpleble_ServiceDiscoveredEvt const& evt) {
409
+ fmt::print(
410
+ "PeripheralDongl::notify_service_discovered: conn_handle={}, uuid16={:04x}, start_handle={}, end_handle={}\n",
411
+ evt.conn_handle, evt.uuid16.uuid, evt.start_handle, evt.end_handle);
412
+
413
+ BluetoothUUID uuid;
414
+ if (evt.has_uuid16) {
415
+ uuid = _uuid_from_uuid16(evt.uuid16.uuid);
416
+ }
417
+
418
+ _services.emplace_back(ServiceDefinition{
419
+ uuid,
420
+ evt.start_handle,
421
+ evt.end_handle,
422
+ });
423
+ }
424
+
425
+ void PeripheralDongl::notify_characteristic_discovered(simpleble_CharacteristicDiscoveredEvt const& evt) {
426
+ fmt::print(
427
+ "PeripheralDongl::notify_characteristic_discovered: conn_handle={}, uuid16={:04x}, handle_decl={}, "
428
+ "handle_value={}, can_read={}, can_write_wo_resp={}, can_write_cmd={}, can_notify={}, can_indicate={}\n",
429
+ evt.conn_handle, evt.uuid16.uuid, evt.handle_decl, evt.handle_value, evt.props.read, evt.props.write_wo_resp,
430
+ evt.props.write, evt.props.notify, evt.props.indicate);
431
+
432
+ auto& service = _service_definition(evt.handle_decl);
433
+
434
+ BluetoothUUID uuid;
435
+ if (evt.has_uuid16) {
436
+ uuid = _uuid_from_uuid16(evt.uuid16.uuid);
437
+ }
438
+
439
+ service.characteristics.emplace_back(CharacteristicDefinition{
440
+ uuid,
441
+ evt.handle_decl,
442
+ evt.handle_value,
443
+ evt.props.read,
444
+ evt.props.write,
445
+ evt.props.write_wo_resp,
446
+ evt.props.notify,
447
+ evt.props.indicate,
448
+ });
449
+ }
450
+
451
+ void PeripheralDongl::notify_descriptor_discovered(simpleble_DescriptorDiscoveredEvt const& evt) {
452
+ auto& service = _service_definition(evt.handle);
453
+
454
+ for (auto& characteristic : service.characteristics) {
455
+ // If the descriptor matches the characteristic declaration handle or value handle, we can ignore it.
456
+ if (characteristic.handle_decl == evt.handle || characteristic.handle_value == evt.handle) {
457
+ return;
458
+ }
459
+ }
460
+
461
+ // At this point we know we have a real descriptor that we shouldn't ignore.
462
+ fmt::print("PeripheralDongl::notify_descriptor_discovered: conn_handle={}, uuid16={:04x}, handle={}\n",
463
+ evt.conn_handle, evt.uuid16.uuid, evt.handle);
464
+
465
+ // For the given service handle, loop the characteristics backwards and select the first characteristic where the
466
+ // handle_value is less than the descriptor handle.
467
+ for (auto it = service.characteristics.rbegin(); it != service.characteristics.rend(); ++it) {
468
+ if (it->handle_value < evt.handle) {
469
+ it->descriptors.emplace_back(DescriptorDefinition{
470
+ _uuid_from_uuid16(evt.uuid16.uuid),
471
+ evt.handle,
472
+ });
473
+ break;
474
+ }
475
+ }
476
+ }
477
+
478
+ void PeripheralDongl::notify_attribute_discovery_complete() {
479
+ fmt::print("PeripheralDongl::notify_attribute_discovery_complete: {}\n", _conn_handle);
480
+ attributes_discovered_cv_.notify_all();
481
+ }
482
+
483
+ BluetoothUUID PeripheralDongl::_uuid_from_uuid16(uint16_t uuid16) {
484
+ return BluetoothUUID(fmt::format("0000{:04X}-0000-1000-8000-00805F9B34FB", uuid16));
485
+ }
486
+
487
+ BluetoothUUID PeripheralDongl::_uuid_from_uuid32(uint32_t uuid32) {
488
+ return BluetoothUUID(fmt::format("{:08X}-0000-1000-8000-00805F9B34FB", uuid32));
489
+ }
490
+
491
+ BluetoothUUID PeripheralDongl::_uuid_from_uuid128(const uint8_t id[16]) {
492
+ std::string uuid_str;
493
+ uuid_str.reserve(36); // Pre-allocate memory for the UUID string
494
+
495
+ uuid_str += fmt::format("{:02X}{:02X}{:02X}{:02X}", id[0], id[1], id[2], id[3]);
496
+ uuid_str += "-";
497
+ uuid_str += fmt::format("{:02X}{:02X}", id[4], id[5]);
498
+ uuid_str += "-";
499
+ uuid_str += fmt::format("{:02X}{:02X}", id[6], id[7]);
500
+ uuid_str += "-";
501
+ uuid_str += fmt::format("{:02X}{:02X}", id[8], id[9]);
502
+ uuid_str += "-";
503
+ uuid_str += fmt::format("{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}", id[10], id[11], id[12], id[13], id[14], id[15]);
504
+
505
+ return BluetoothUUID(uuid_str);
506
+ }
507
+
508
+ BluetoothUUID PeripheralDongl::_uuid_from_proto(simpleble_UUID const& uuid) {
509
+ switch (uuid.which_uuid) {
510
+ case simpleble_UUID_uuid16_tag:
511
+ return BluetoothUUID(fmt::format("0000{:04X}-0000-1000-8000-00805F9B34FB", uuid.uuid.uuid16.uuid));
512
+ case simpleble_UUID_uuid32_tag:
513
+ return BluetoothUUID(fmt::format("{:08X}-0000-1000-8000-00805F9B34FB", uuid.uuid.uuid32.uuid));
514
+ case simpleble_UUID_uuid128_tag: {
515
+ const auto& bytes = uuid.uuid.uuid128.uuid;
516
+ std::string uuid_str;
517
+ uuid_str.reserve(36); // Pre-allocate memory for the UUID string
518
+
519
+ uuid_str += fmt::format("{:02X}{:02X}{:02X}{:02X}", bytes[0], bytes[1], bytes[2], bytes[3]);
520
+ uuid_str += "-";
521
+ uuid_str += fmt::format("{:02X}{:02X}", bytes[4], bytes[5]);
522
+ uuid_str += "-";
523
+ uuid_str += fmt::format("{:02X}{:02X}", bytes[6], bytes[7]);
524
+ uuid_str += "-";
525
+ uuid_str += fmt::format("{:02X}{:02X}", bytes[8], bytes[9]);
526
+ uuid_str += "-";
527
+ uuid_str += fmt::format("{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}", bytes[10], bytes[11], bytes[12], bytes[13],
528
+ bytes[14], bytes[15]);
529
+
530
+ return BluetoothUUID(uuid_str);
531
+ }
532
+ }
533
+
534
+ fmt::print("Unknown UUID type: {}\n", uuid.which_uuid);
535
+ // Should not be reached
536
+ return BluetoothUUID();
537
+ }
538
+
539
+ PeripheralDongl::ServiceDefinition& PeripheralDongl::_service_definition(uint16_t handle) {
540
+ for (auto& service : _services) {
541
+ if (service.start_handle <= handle && service.end_handle >= handle) {
542
+ return service;
543
+ }
544
+ }
545
+
546
+ throw std::runtime_error("Service not found");
547
+ }
548
+
549
+ PeripheralDongl::CharacteristicDefinition& PeripheralDongl::_characteristic_definition(
550
+ BluetoothUUID const& service_uuid, BluetoothUUID const& characteristic_uuid) {
551
+ for (auto& service : _services) {
552
+ if (service.uuid == service_uuid) {
553
+ for (auto& characteristic : service.characteristics) {
554
+ if (characteristic.uuid == characteristic_uuid) {
555
+ return characteristic;
556
+ }
557
+ }
558
+ }
559
+ }
560
+
561
+ throw std::runtime_error("Characteristic not found");
562
+ }
@@ -0,0 +1,136 @@
1
+ #pragma once
2
+
3
+ #include <simpleble/Exceptions.h>
4
+ #include <simpleble/Service.h>
5
+ #include <simpleble/Types.h>
6
+ #include "PeripheralBase.h"
7
+
8
+ #include <TaskRunner.hpp>
9
+ #include <kvn_safe_callback.hpp>
10
+
11
+ #include <atomic>
12
+ #include <map>
13
+ #include <memory>
14
+ #include <mutex>
15
+
16
+ #include "AdapterBaseTypes.h"
17
+ #include "protocol/d2h.pb.h"
18
+ #include "serial/Protocol.h"
19
+
20
+ namespace SimpleBLE {
21
+
22
+ class PeripheralDongl : public PeripheralBase {
23
+ public:
24
+ PeripheralDongl(std::shared_ptr<Dongl::Serial::Protocol> serial_protocol, advertising_data_t advertising_data);
25
+ virtual ~PeripheralDongl();
26
+
27
+ void* underlying() const override;
28
+
29
+ virtual std::string identifier() override;
30
+ virtual BluetoothAddress address() override;
31
+ virtual BluetoothAddressType address_type() override;
32
+ virtual int16_t rssi() override;
33
+ virtual int16_t tx_power() override;
34
+ virtual uint16_t mtu() override;
35
+
36
+ virtual void connect() override;
37
+ virtual void disconnect() override;
38
+ virtual bool is_connected() override;
39
+ virtual bool is_connectable() override;
40
+ virtual bool is_paired() override;
41
+ virtual void unpair() override;
42
+
43
+ virtual std::vector<std::shared_ptr<ServiceBase>> available_services() override;
44
+ virtual std::vector<std::shared_ptr<ServiceBase>> advertised_services() override;
45
+
46
+ virtual std::map<uint16_t, ByteArray> manufacturer_data() override;
47
+
48
+ // clang-format off
49
+ virtual ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic) override;
50
+ virtual void write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) override;
51
+ virtual void write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) override;
52
+ virtual void notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) override;
53
+ virtual void indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) override;
54
+ virtual void unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) override;
55
+
56
+ virtual ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor) override;
57
+ virtual void write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data) override;
58
+ // clang-format on
59
+
60
+ virtual void set_callback_on_connected(std::function<void()> on_connected) override;
61
+ virtual void set_callback_on_disconnected(std::function<void()> on_disconnected) override;
62
+
63
+ // Internal methods not exposed to the user.
64
+ uint16_t conn_handle() const;
65
+ void update_advertising_data(advertising_data_t advertising_data);
66
+ void notify_connected(uint16_t conn_handle);
67
+ void notify_disconnected();
68
+ void notify_service_discovered(simpleble_ServiceDiscoveredEvt const& service_discovered_evt);
69
+ void notify_characteristic_discovered(simpleble_CharacteristicDiscoveredEvt const& characteristic_discovered_evt);
70
+ void notify_descriptor_discovered(simpleble_DescriptorDiscoveredEvt const& descriptor_discovered_evt);
71
+ void notify_attribute_discovery_complete();
72
+
73
+ const uint16_t BLE_CONN_HANDLE_INVALID = 0xFFFF;
74
+ const uint16_t BLE_CONN_HANDLE_PENDING = 0xFFFE;
75
+
76
+ private:
77
+ struct DescriptorDefinition {
78
+ BluetoothUUID uuid;
79
+ uint16_t handle;
80
+ };
81
+
82
+ struct CharacteristicDefinition {
83
+ BluetoothUUID uuid;
84
+ uint16_t handle_decl;
85
+ uint16_t handle_value;
86
+ bool can_read;
87
+ bool can_write_request;
88
+ bool can_write_command;
89
+ bool can_notify;
90
+ bool can_indicate;
91
+
92
+ std::vector<DescriptorDefinition> descriptors;
93
+ };
94
+
95
+ struct ServiceDefinition {
96
+ BluetoothUUID uuid;
97
+ uint16_t start_handle;
98
+ uint16_t end_handle;
99
+ std::vector<CharacteristicDefinition> characteristics;
100
+ };
101
+
102
+ bool _attempt_connect();
103
+ BluetoothUUID _uuid_from_uuid16(uint16_t uuid16);
104
+ BluetoothUUID _uuid_from_uuid32(uint32_t uuid32);
105
+ BluetoothUUID _uuid_from_uuid128(const uint8_t uuid[16]);
106
+ BluetoothUUID _uuid_from_proto(simpleble_UUID const& uuid);
107
+
108
+ ServiceDefinition& _service_definition(uint16_t handle);
109
+ CharacteristicDefinition& _characteristic_definition(BluetoothUUID const& service, BluetoothUUID const& characteristic);
110
+
111
+ uint16_t _conn_handle = BLE_CONN_HANDLE_INVALID;
112
+ std::string _identifier;
113
+ BluetoothAddress _address;
114
+ BluetoothAddressType _address_type;
115
+ int16_t _rssi;
116
+ int16_t _tx_power;
117
+ bool _connectable;
118
+ std::map<uint16_t, ByteArray> _manufacturer_data;
119
+ std::map<BluetoothUUID, ByteArray> _service_data;
120
+
121
+ std::vector<ServiceDefinition> _services;
122
+
123
+ std::shared_ptr<Dongl::Serial::Protocol> _serial_protocol;
124
+
125
+ std::condition_variable connection_cv_;
126
+ std::mutex connection_mutex_;
127
+ std::condition_variable disconnection_cv_;
128
+ std::mutex disconnection_mutex_;
129
+ std::condition_variable attributes_discovered_cv_;
130
+ std::mutex attributes_discovered_mutex_;
131
+
132
+ kvn::safe_callback<void()> _callback_on_connected;
133
+ kvn::safe_callback<void()> _callback_on_disconnected;
134
+ };
135
+
136
+ } // namespace SimpleBLE