simplejsble 0.0.3 → 0.0.5

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/NitroSimplejsble.podspec +9 -4
  2. package/VERSION +1 -0
  3. package/android/CMakeLists.txt +62 -20
  4. package/cmake/epilogue.cmake +81 -0
  5. package/cmake/find/FindDBus1.cmake +20 -0
  6. package/cmake/find/Findfmt.cmake +41 -0
  7. package/cmake/find/Findgoogletest.cmake +18 -0
  8. package/cmake/parse_version.cmake +45 -0
  9. package/cmake/prelude.cmake +8 -0
  10. package/dependencies/README +4 -0
  11. package/dependencies/external/kvn/kvn_bytearray.h +297 -0
  12. package/dependencies/external/kvn/kvn_safe_callback.hpp +66 -0
  13. package/dependencies/external/kvn/logfwd.hpp +35 -0
  14. package/dependencies/internal/include/fmt/LICENSE +27 -0
  15. package/dependencies/internal/include/fmt/args.h +220 -0
  16. package/dependencies/internal/include/fmt/base.h +2962 -0
  17. package/dependencies/internal/include/fmt/chrono.h +2338 -0
  18. package/dependencies/internal/include/fmt/color.h +610 -0
  19. package/dependencies/internal/include/fmt/compile.h +539 -0
  20. package/dependencies/internal/include/fmt/core.h +5 -0
  21. package/dependencies/internal/include/fmt/format-inl.h +1949 -0
  22. package/dependencies/internal/include/fmt/format.h +4244 -0
  23. package/dependencies/internal/include/fmt/os.h +427 -0
  24. package/dependencies/internal/include/fmt/ostream.h +166 -0
  25. package/dependencies/internal/include/fmt/printf.h +633 -0
  26. package/dependencies/internal/include/fmt/ranges.h +850 -0
  27. package/dependencies/internal/include/fmt/std.h +726 -0
  28. package/dependencies/internal/include/fmt/xchar.h +373 -0
  29. package/dependencies/internal/include/nanopb/pb.h +948 -0
  30. package/dependencies/internal/include/nanopb/pb_common.h +49 -0
  31. package/dependencies/internal/include/nanopb/pb_decode.h +204 -0
  32. package/dependencies/internal/include/nanopb/pb_encode.h +195 -0
  33. package/dependencies/internal/include/simplejni/Common.hpp +598 -0
  34. package/dependencies/internal/include/simplejni/References.hpp +151 -0
  35. package/dependencies/internal/include/simplejni/Registry.hpp +249 -0
  36. package/dependencies/internal/include/simplejni/VM.hpp +93 -0
  37. package/dependencies/internal/src/nanopb/pb_common.c +388 -0
  38. package/dependencies/internal/src/nanopb/pb_decode.c +1763 -0
  39. package/dependencies/internal/src/nanopb/pb_encode.c +1009 -0
  40. package/dependencies/internal/src/simplejni/Common.cpp +4 -0
  41. package/package.json +5 -1
  42. package/simpleble/CMakeLists.txt +519 -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,117 @@
1
+ #pragma once
2
+
3
+ #include <functional>
4
+ #include <memory>
5
+ #include <optional>
6
+ #include <vector>
7
+
8
+ #include <chrono>
9
+ #include <condition_variable>
10
+ #include <mutex>
11
+
12
+ #include "Wire.h"
13
+ #include "ProtocolBase.h"
14
+
15
+ #include "protocol/d2h.pb.h"
16
+ #include "protocol/h2d.pb.h"
17
+
18
+ namespace SimpleBLE {
19
+ namespace Dongl {
20
+ namespace Serial {
21
+
22
+ class Protocol : public ProtocolBase {
23
+ public:
24
+ Protocol(const std::string& device_path);
25
+ ~Protocol();
26
+
27
+ basic_WhoamiRsp basic_whoami();
28
+ basic_ResetRsp basic_reset();
29
+ basic_DfuStartRsp basic_dfu_start();
30
+
31
+ simpleble_InitRsp simpleble_init();
32
+ simpleble_ScanStartRsp simpleble_scan_start();
33
+ simpleble_ScanStopRsp simpleble_scan_stop();
34
+ simpleble_ConnectRsp simpleble_connect(simpleble_BluetoothAddressType address_type, const std::string& address);
35
+ simpleble_DisconnectRsp simpleble_disconnect(uint16_t conn_handle);
36
+ simpleble_ReadRsp simpleble_read(uint16_t conn_handle, uint16_t handle);
37
+ simpleble_WriteRsp simpleble_write(uint16_t conn_handle, uint16_t handle, simpleble_WriteOperation operation, const std::vector<uint8_t>& data);
38
+
39
+ // GAP commands
40
+ sd_GapAddrSetRsp sd_gap_addr_set(bool has_addr, sd_types_BleGapAddr addr);
41
+ sd_GapAddrGetRsp sd_gap_addr_get();
42
+ sd_GapWhitelistSetRsp sd_gap_whitelist_set(pb_size_t wl_addrs_count, sd_types_BleGapAddr wl_addrs[8], uint8_t len);
43
+ sd_GapDeviceIdentitiesSetRsp sd_gap_device_identities_set(pb_size_t id_keys_count, sd_types_BleGapIdKey id_keys[8], pb_size_t local_irks_count, sd_types_BleGapIrk local_irks[8], uint8_t len);
44
+ sd_GapPrivacySetRsp sd_gap_privacy_set(bool has_privacy_params, sd_types_BleGapPrivacyParams privacy_params);
45
+ sd_GapPrivacyGetRsp sd_gap_privacy_get();
46
+ sd_GapAdvSetConfigureRsp sd_gap_adv_set_configure(uint8_t adv_handle, bool has_adv_data, sd_types_BleGapAdvData adv_data, bool has_adv_params, sd_types_BleGapAdvParams adv_params);
47
+ sd_GapAdvStartRsp sd_gap_adv_start(uint8_t adv_handle, uint8_t conn_cfg_tag);
48
+ sd_GapAdvStopRsp sd_gap_adv_stop(uint8_t adv_handle);
49
+ sd_GapConnParamUpdateRsp sd_gap_conn_param_update(uint16_t conn_handle, bool has_conn_params, sd_types_BleGapConnParams conn_params);
50
+ sd_GapDisconnectRsp sd_gap_disconnect(uint16_t conn_handle, uint8_t hci_ret_code_code);
51
+ sd_GapTxPowerSetRsp sd_gap_tx_power_set(uint8_t role, uint16_t handle, int8_t tx_power);
52
+ sd_GapAppearanceSetRsp sd_gap_appearance_set(uint16_t appearance);
53
+ sd_GapAppearanceGetRsp sd_gap_appearance_get();
54
+ sd_GapPpcpSetRsp sd_gap_ppcp_set(bool has_conn_params, sd_types_BleGapConnParams conn_params);
55
+ sd_GapPpcpGetRsp sd_gap_ppcp_get();
56
+ sd_GapDeviceNameSetRsp sd_gap_device_name_set(bool has_write_perm, sd_types_BleGapConnSecMode write_perm, sd_GapDeviceNameSetCmd_dev_name_t dev_name, uint16_t len);
57
+ sd_GapDeviceNameGetRsp sd_gap_device_name_get();
58
+ sd_GapAuthenticateRsp sd_gap_authenticate(uint16_t conn_handle, bool has_sec_params, sd_types_BleGapSecParams sec_params);
59
+ sd_GapSecParamsReplyRsp sd_gap_sec_params_reply(uint16_t conn_handle, sd_types_BleGapSecStatus sec_ret_code, bool has_sec_params, sd_types_BleGapSecParams sec_params, bool has_sec_keyset, sd_types_BleGapSecKeyset sec_keyset);
60
+ sd_GapAuthKeyReplyRsp sd_gap_auth_key_reply(uint16_t conn_handle, sd_types_BleGapAuthKeyType key_type, sd_GapAuthKeyReplyCmd_key_t key);
61
+ sd_GapLescDhkeyReplyRsp sd_gap_lesc_dhkey_reply(uint16_t conn_handle, bool has_dhkey, sd_types_BleGapLescDhkey dhkey);
62
+ sd_GapKeypressNotifyRsp sd_gap_keypress_notify(uint16_t conn_handle, sd_types_BleGapKpNotType kp_not);
63
+ sd_GapLescOobDataGetRsp sd_gap_lesc_oob_data_get(uint16_t conn_handle, bool has_pk_own, sd_types_BleGapLescP256Pk pk_own);
64
+ sd_GapLescOobDataSetRsp sd_gap_lesc_oob_data_set(uint16_t conn_handle, bool has_oobd_own, sd_types_BleGapLescOobData oobd_own, bool has_oobd_peer, sd_types_BleGapLescOobData oobd_peer);
65
+ sd_GapEncryptRsp sd_gap_encrypt(uint16_t conn_handle, bool has_master_id, sd_types_BleGapMasterId master_id, bool has_enc_info, sd_types_BleGapEncInfo enc_info);
66
+ sd_GapSecInfoReplyRsp sd_gap_sec_info_reply(uint16_t conn_handle, bool has_enc_info, sd_types_BleGapEncInfo enc_info, bool has_id_info, sd_types_BleGapIrk id_info, bool has_sign_info, sd_types_BleGapSignInfo sign_info);
67
+ sd_GapConnSecGetRsp sd_gap_conn_sec_get(uint16_t conn_handle);
68
+ sd_GapRssiStartRsp sd_gap_rssi_start(uint16_t conn_handle, uint8_t threshold_dbm, uint8_t skip_count);
69
+ sd_GapRssiStopRsp sd_gap_rssi_stop(uint16_t conn_handle);
70
+ sd_GapScanStartRsp sd_gap_scan_start(sd_types_BleGapScanParams scan_params);
71
+ sd_GapScanStopRsp sd_gap_scan_stop();
72
+ sd_GapConnectRsp sd_gap_connect(bool has_peer_addr, sd_types_BleGapAddr peer_addr, bool has_scan_params, sd_types_BleGapScanParams scan_params, bool has_conn_params, sd_types_BleGapConnParams conn_params, uint8_t conn_cfg_tag);
73
+ sd_GapConnectCancelRsp sd_gap_connect_cancel();
74
+ sd_GapRssiGetRsp sd_gap_rssi_get(uint16_t conn_handle);
75
+ sd_GapPhyUpdateRsp sd_gap_phy_update(uint16_t conn_handle, bool has_phys, sd_BleGapPhysCmd phys);
76
+ sd_GapDataLengthUpdateRsp sd_gap_data_length_update(uint16_t conn_handle, bool has_dl_params, sd_types_BleGapDataLengthParams dl_params, bool has_dl_limitation, sd_types_BleGapDataLengthLimitation dl_limitation);
77
+ sd_GapQosChannelSurveyStartRsp sd_gap_qos_channel_survey_start(uint32_t interval_us);
78
+ sd_GapQosChannelSurveyStopRsp sd_gap_qos_channel_survey_stop();
79
+ sd_GapAdvAddrGetRsp sd_gap_adv_addr_get(uint8_t adv_handle);
80
+ sd_GapNextConnEvtCounterGetRsp sd_gap_next_conn_evt_counter_get(uint16_t conn_handle);
81
+ sd_GapConnEvtTriggerStartRsp sd_gap_conn_evt_trigger_start(uint16_t conn_handle, bool has_params, sd_types_BleGapConnEventTrigger params);
82
+ sd_GapConnEvtTriggerStopRsp sd_gap_conn_evt_trigger_stop(uint16_t conn_handle);
83
+
84
+ // GATTC commands
85
+ sd_GattcPrimaryServicesDiscoverRsp sd_gattc_primary_services_discover(uint16_t conn_handle, uint16_t start_handle, bool has_srvc_uuid, sd_types_BleUuid srvc_uuid);
86
+ sd_GattcRelationshipsDiscoverRsp sd_gattc_relationships_discover(uint16_t conn_handle, bool has_handle_range, sd_types_BleGattcHandleRange handle_range);
87
+ sd_GattcCharacteristicsDiscoverRsp sd_gattc_characteristics_discover(uint16_t conn_handle, bool has_handle_range, sd_types_BleGattcHandleRange handle_range);
88
+ sd_GattcDescriptorsDiscoverRsp sd_gattc_descriptors_discover(uint16_t conn_handle, bool has_handle_range, sd_types_BleGattcHandleRange handle_range);
89
+ sd_GattcAttrInfoDiscoverRsp sd_gattc_attr_info_discover(uint16_t conn_handle, bool has_handle_range, sd_types_BleGattcHandleRange handle_range);
90
+ sd_GattcCharValueByUuidReadRsp sd_gattc_char_value_by_uuid_read(uint16_t conn_handle, bool has_uuid, sd_types_BleUuid uuid, bool has_handle_range, sd_types_BleGattcHandleRange handle_range);
91
+ sd_GattcReadRsp sd_gattc_read(uint16_t conn_handle, uint16_t handle, uint16_t offset);
92
+ sd_GattcCharValuesReadRsp sd_gattc_char_values_read(uint16_t conn_handle, pb_callback_t handles, uint8_t handle_count);
93
+ sd_GattcWriteRsp sd_gattc_write(uint16_t conn_handle, bool has_write_params, sd_types_BleGattcWriteParams write_params);
94
+ sd_GattcHvConfirmRsp sd_gattc_hv_confirm(uint16_t conn_handle, uint16_t handle);
95
+ sd_GattcExchangeMtuRequestRsp sd_gattc_exchange_mtu_request(uint16_t conn_handle, uint16_t client_rx_mtu);
96
+
97
+ // GATTS commands
98
+ sd_GattsServiceAddRsp sd_gatts_service_add(uint8_t type, bool has_uuid, sd_types_BleUuid uuid);
99
+ sd_GattsIncludeAddRsp sd_gatts_include_add(uint16_t service_handle, uint16_t inc_srvc_handle);
100
+ sd_GattsCharacteristicAddRsp sd_gatts_characteristic_add(uint16_t service_handle, bool has_char_md, sd_types_BleGattsCharMd char_md, bool has_attr_char_value, sd_types_BleGattsAttr attr_char_value);
101
+ sd_GattsDescriptorAddRsp sd_gatts_descriptor_add(uint16_t char_handle, bool has_attr, sd_types_BleGattsAttr attr);
102
+ sd_GattsValueSetRsp sd_gatts_value_set(uint16_t conn_handle, uint16_t handle, bool has_value, sd_types_BleGattsValue value);
103
+ sd_GattsValueGetRsp sd_gatts_value_get(uint16_t conn_handle, uint16_t handle);
104
+ sd_GattsHvxRsp sd_gatts_hvx(uint16_t conn_handle, bool has_hvx_params, sd_types_BleGattsHvxParams hvx_params);
105
+ sd_GattsServiceChangedRsp sd_gatts_service_changed(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle);
106
+ sd_GattsRwAuthorizeReplyRsp sd_gatts_rw_authorize_reply(uint16_t conn_handle, bool has_rw_authorize_reply_params, sd_types_BleGattsRwAuthorizeReplyParams rw_authorize_reply_params);
107
+ sd_GattsSysAttrSetRsp sd_gatts_sys_attr_set(uint16_t conn_handle, sd_GattsSysAttrSetCmd_sys_attr_data_t sys_attr_data, uint16_t len, uint32_t flags);
108
+ sd_GattsSysAttrGetRsp sd_gatts_sys_attr_get(uint16_t conn_handle, uint32_t flags);
109
+ sd_GattsInitialUserHandleGetRsp sd_gatts_initial_user_handle_get();
110
+ sd_GattsAttrGetRsp sd_gatts_attr_get(uint16_t handle);
111
+ sd_GattsExchangeMtuReplyRsp sd_gatts_exchange_mtu_reply(uint16_t conn_handle, uint16_t server_rx_mtu);
112
+
113
+ };
114
+
115
+ } // namespace Serial
116
+ } // namespace Dongl
117
+ } // namespace SimpleBLE
@@ -0,0 +1,95 @@
1
+ #include "ProtocolBase.h"
2
+
3
+ using namespace SimpleBLE::Dongl::Serial;
4
+
5
+ #include "nanopb/pb_decode.h"
6
+ #include "nanopb/pb_encode.h"
7
+
8
+ #include <fmt/core.h>
9
+
10
+ ProtocolBase::ProtocolBase(const std::string& device_path) : _wire(std::make_unique<Wire>(device_path)) {
11
+ // Set up the Wire packet callback to handle incoming packets
12
+ _wire->set_packet_callback([this](const std::vector<uint8_t>& packet) {
13
+ dongl_D2H d2h;
14
+ pb_istream_t stream = pb_istream_from_buffer(packet.data(), packet.size());
15
+ if (!pb_decode(&stream, dongl_D2H_fields, &d2h)) {
16
+ // TODO: Handle decoding failure
17
+ fmt::print("Failed to decode D2H: {}\n", PB_GET_ERROR(&stream));
18
+ return;
19
+ }
20
+
21
+ if (d2h.which_type == dongl_D2H_rsp_tag) {
22
+ // First, try to fulfill any pending sync request
23
+ {
24
+ std::lock_guard<std::mutex> lock(_pending_mutex);
25
+ if (!_pending_response.has_value()) {
26
+ _pending_response = d2h.type.rsp;
27
+ _response_cv.notify_one();
28
+ }
29
+ }
30
+
31
+ } else if (d2h.which_type == dongl_D2H_evt_tag) {
32
+ if (_event_callback) {
33
+ _event_callback(d2h.type.evt);
34
+ }
35
+ }
36
+ });
37
+
38
+ _wire->set_error_callback([this](const Wire::Error& error) {
39
+ fmt::print("Error: {}\n", (int)error);
40
+ });
41
+ }
42
+
43
+ ProtocolBase::~ProtocolBase() {}
44
+
45
+ dongl_Response ProtocolBase::exchange(const dongl_Command& command) {
46
+ // Check if there's already a pending sync operation
47
+ {
48
+ std::lock_guard<std::mutex> lock(_pending_mutex);
49
+ if (_pending_response.has_value()) {
50
+ throw std::runtime_error("Another sync command is already pending");
51
+ }
52
+ }
53
+
54
+ // Clear any previous response and send the command
55
+ {
56
+ std::unique_lock<std::mutex> lock(_pending_mutex);
57
+ _pending_response.reset(); // Ensure it's empty to indicate we're waiting
58
+ }
59
+
60
+ try {
61
+ size_t command_size = 0;
62
+ pb_get_encoded_size(&command_size, dongl_Command_fields, &command);
63
+
64
+ std::vector<uint8_t> tx_buffer_raw(command_size);
65
+ pb_ostream_t stream = pb_ostream_from_buffer(tx_buffer_raw.data(), tx_buffer_raw.size());
66
+ bool status = pb_encode(&stream, dongl_Command_fields, &command);
67
+ if (!status) {
68
+ // TODO: Handle encoding failure
69
+ throw std::runtime_error("Failed to encode command");
70
+ }
71
+
72
+ _wire->send_packet(tx_buffer_raw);
73
+ } catch (const std::runtime_error& e) {
74
+ // If sending fails, no cleanup needed since we didn't set pending_response
75
+ throw;
76
+ }
77
+
78
+ // Wait for the response with 1 second timeout
79
+ {
80
+ std::unique_lock<std::mutex> lock(_pending_mutex);
81
+ if (_response_cv.wait_for(lock, std::chrono::seconds(1), [this]() { return _pending_response.has_value(); })) {
82
+ dongl_Response response = *_pending_response;
83
+ _pending_response.reset();
84
+ return response;
85
+ } else {
86
+ // Timeout occurred, reset pending state
87
+ _pending_response.reset();
88
+ throw std::runtime_error("Timeout waiting for response");
89
+ }
90
+ }
91
+ }
92
+
93
+ void ProtocolBase::set_event_callback(std::function<void(const dongl_Event&)> callback) {
94
+ _event_callback = std::move(callback);
95
+ }
@@ -0,0 +1,54 @@
1
+ #pragma once
2
+
3
+ #include <functional>
4
+ #include <memory>
5
+ #include <optional>
6
+ #include <vector>
7
+
8
+ #include <chrono>
9
+ #include <condition_variable>
10
+ #include <mutex>
11
+
12
+ #include "Wire.h"
13
+
14
+ #include "protocol/d2h.pb.h"
15
+ #include "protocol/h2d.pb.h"
16
+
17
+ namespace SimpleBLE {
18
+ namespace Dongl {
19
+ namespace Serial {
20
+
21
+ class ProtocolBase {
22
+ public:
23
+ ProtocolBase(const std::string& device_path);
24
+ ~ProtocolBase();
25
+
26
+ /**
27
+ * @brief Sends a command synchronously and waits for the response.
28
+ * Only one exchange can be pending at a time.
29
+ *
30
+ * @param command The command to send.
31
+ * @return The response when it arrives.
32
+ * @throws std::runtime_error if another exchange is already pending or if timeout occurs.
33
+ */
34
+ dongl_Response exchange(const dongl_Command& command);
35
+
36
+ /**
37
+ * @brief Sets the callback for received events.
38
+ *
39
+ * @param callback Function to call when an event is received.
40
+ */
41
+ void set_event_callback(std::function<void(const dongl_Event&)> callback);
42
+
43
+ private:
44
+ std::unique_ptr<Wire> _wire;
45
+ std::function<void(const dongl_Event&)> _event_callback;
46
+
47
+ std::optional<dongl_Response> _pending_response;
48
+ std::condition_variable _response_cv;
49
+ std::mutex _pending_mutex;
50
+ };
51
+
52
+ } // namespace Serial
53
+ } // namespace Dongl
54
+ } // namespace SimpleBLE
@@ -0,0 +1,133 @@
1
+ #include "Wire.h"
2
+
3
+ #include <cstring>
4
+ #include <kvn/kvn_bytearray.h>
5
+ #include <fmt/core.h>
6
+
7
+ using namespace SimpleBLE::Dongl::Serial;
8
+
9
+ Wire::Wire(const std::string& device_path)
10
+ : _usb_helper(std::make_unique<USB::UsbHelper>(device_path))
11
+ , _state(State::IDLE)
12
+ , _length(0)
13
+ , _checksum(0)
14
+ , _buffer_index(0)
15
+ , _buffer(MAX_PAYLOAD_SIZE) {
16
+
17
+ // Set up the USB receive callback to process incoming bytes
18
+ _usb_helper->set_rx_callback([this](const kvn::bytearray& data) {
19
+ for (size_t i = 0; i < data.size(); ++i) {
20
+ process_byte(data[i]);
21
+ }
22
+ });
23
+ }
24
+
25
+ Wire::~Wire() {}
26
+
27
+ void Wire::send_packet(const std::vector<uint8_t>& payload) {
28
+ send_packet(payload.data(), payload.size());
29
+ }
30
+
31
+ void Wire::send_packet(const uint8_t* data, size_t length) {
32
+ if (length > MAX_PAYLOAD_SIZE) {
33
+ throw std::runtime_error("Payload length exceeds maximum allowed");
34
+ }
35
+
36
+ // Create the encoded packet
37
+ std::vector<uint8_t> packet;
38
+ packet.reserve(5 + length); // sync + length(2) + payload + crc(2)
39
+
40
+ packet.push_back(SYNC_BYTE);
41
+ packet.push_back(length & 0xFF);
42
+ packet.push_back((length >> 8) & 0xFF);
43
+ packet.insert(packet.end(), data, data + length);
44
+
45
+ uint16_t crc = crc16(data, length);
46
+ packet.push_back(crc & 0xFF);
47
+ packet.push_back((crc >> 8) & 0xFF);
48
+
49
+ // Send via USB
50
+ kvn::bytearray usb_data(packet.data(), packet.size());
51
+ _usb_helper->tx(usb_data);
52
+ }
53
+
54
+ void Wire::set_packet_callback(PacketCallback callback) {
55
+ _packet_callback = std::move(callback);
56
+ }
57
+
58
+ void Wire::set_error_callback(ErrorCallback callback) {
59
+ _error_callback = std::move(callback);
60
+ }
61
+
62
+ void Wire::process_byte(uint8_t byte) {
63
+ switch (_state) {
64
+ case State::IDLE:
65
+ if (byte == SYNC_BYTE) {
66
+ _state = State::HEADER_LOW;
67
+ _buffer_index = 0;
68
+ }
69
+ break;
70
+
71
+ case State::HEADER_LOW:
72
+ _length = byte;
73
+ _state = State::HEADER_HIGH;
74
+ break;
75
+
76
+ case State::HEADER_HIGH:
77
+ _length |= (uint16_t)byte << 8;
78
+ if (_length > MAX_PAYLOAD_SIZE) {
79
+ _state = State::IDLE;
80
+ if (_error_callback) {
81
+ _error_callback(Error::INVALID_LENGTH);
82
+ }
83
+ } else if (_length == 0) {
84
+ _state = State::CHECKSUM_LOW;
85
+ _buffer_index = 0;
86
+ } else {
87
+ _state = State::PAYLOAD;
88
+ _buffer_index = 0;
89
+ }
90
+ break;
91
+
92
+ case State::PAYLOAD:
93
+ _buffer[_buffer_index++] = byte;
94
+ if (_buffer_index == _length) {
95
+ _state = State::CHECKSUM_LOW;
96
+ }
97
+ break;
98
+
99
+ case State::CHECKSUM_LOW:
100
+ _checksum = byte;
101
+ _state = State::CHECKSUM_HIGH;
102
+ break;
103
+
104
+ case State::CHECKSUM_HIGH:
105
+ _checksum |= (uint16_t)byte << 8;
106
+ uint16_t computed_crc = crc16(_buffer.data(), _length);
107
+ if (_checksum == computed_crc) {
108
+ if (_packet_callback) {
109
+ std::vector<uint8_t> packet(_buffer.begin(), _buffer.begin() + _length);
110
+ _packet_callback(packet);
111
+ }
112
+ } else if (_error_callback) {
113
+ _error_callback(Error::CRC_FAILURE);
114
+ }
115
+ _state = State::IDLE;
116
+ break;
117
+ }
118
+ }
119
+
120
+ uint16_t Wire::crc16(const uint8_t* data, size_t len) {
121
+ uint16_t crc = 0xFFFF;
122
+ for (size_t i = 0; i < len; i++) {
123
+ crc ^= (uint16_t)data[i] << 8;
124
+ for (uint8_t bit = 0; bit < 8; bit++) {
125
+ if (crc & 0x8000) {
126
+ crc = (crc << 1) ^ 0x8005;
127
+ } else {
128
+ crc <<= 1;
129
+ }
130
+ }
131
+ }
132
+ return crc;
133
+ }
@@ -0,0 +1,116 @@
1
+ #pragma once
2
+
3
+ #include <functional>
4
+ #include <memory>
5
+ #include <vector>
6
+
7
+ #include "../usb/UsbHelper.h"
8
+
9
+ namespace SimpleBLE {
10
+ namespace Dongl {
11
+ namespace Serial {
12
+
13
+ class Wire {
14
+ public:
15
+ /**
16
+ * @brief Error codes for the serial protocol.
17
+ */
18
+ enum class Error {
19
+ INVALID_LENGTH, /**< Length exceeds maximum payload size. */
20
+ CRC_FAILURE /**< CRC-16 check failed. */
21
+ };
22
+
23
+ /**
24
+ * @brief State machine states.
25
+ */
26
+ enum class State {
27
+ IDLE, /**< Waiting for sync byte. */
28
+ HEADER_LOW, /**< Reading low byte of length. */
29
+ HEADER_HIGH, /**< Reading high byte of length. */
30
+ PAYLOAD, /**< Reading payload bytes. */
31
+ CHECKSUM_LOW, /**< Reading low byte of CRC-16. */
32
+ CHECKSUM_HIGH /**< Reading high byte of CRC-16. */
33
+ };
34
+
35
+ /**
36
+ * @brief Callback function type for received packets.
37
+ */
38
+ using PacketCallback = std::function<void(const std::vector<uint8_t>&)>;
39
+
40
+ /**
41
+ * @brief Callback function type for errors.
42
+ */
43
+ using ErrorCallback = std::function<void(Error)>;
44
+
45
+ /**
46
+ * @brief Maximum payload size in bytes.
47
+ */
48
+ static constexpr size_t MAX_PAYLOAD_SIZE = 571;
49
+
50
+ /**
51
+ * @brief Sync byte value marking packet start.
52
+ */
53
+ static constexpr uint8_t SYNC_BYTE = 0xAA;
54
+
55
+ Wire(const std::string& device_path);
56
+ ~Wire();
57
+
58
+ /**
59
+ * @brief Sends a payload as an encoded packet.
60
+ *
61
+ * @param payload The data to send.
62
+ */
63
+ void send_packet(const std::vector<uint8_t>& payload);
64
+
65
+ /**
66
+ * @brief Sends a payload as an encoded packet.
67
+ *
68
+ * @param data Pointer to the data to send.
69
+ * @param length Length of the data.
70
+ */
71
+ void send_packet(const uint8_t* data, size_t length);
72
+
73
+ /**
74
+ * @brief Sets the callback for received packets.
75
+ *
76
+ * @param callback Function to call when a valid packet is received.
77
+ */
78
+ void set_packet_callback(PacketCallback callback);
79
+
80
+ /**
81
+ * @brief Sets the callback for protocol errors.
82
+ *
83
+ * @param callback Function to call on protocol errors.
84
+ */
85
+ void set_error_callback(ErrorCallback callback);
86
+
87
+ private:
88
+ /**
89
+ * @brief Processes a single incoming byte through the protocol state machine.
90
+ *
91
+ * @param byte The incoming byte to process.
92
+ */
93
+ void process_byte(uint8_t byte);
94
+
95
+ /**
96
+ * @brief Computes CRC-16 checksum.
97
+ *
98
+ * @param data Pointer to the data.
99
+ * @param len Length of the data.
100
+ * @return The computed CRC-16 value.
101
+ */
102
+ static uint16_t crc16(const uint8_t* data, size_t len);
103
+
104
+ std::unique_ptr<USB::UsbHelper> _usb_helper;
105
+ State _state;
106
+ uint16_t _length;
107
+ uint16_t _checksum;
108
+ size_t _buffer_index;
109
+ std::vector<uint8_t> _buffer;
110
+ PacketCallback _packet_callback;
111
+ ErrorCallback _error_callback;
112
+ };
113
+
114
+ } // namespace Serial
115
+ } // namespace Dongl
116
+ } // namespace SimpleBLE
@@ -0,0 +1,53 @@
1
+ #include "UsbHelper.h"
2
+
3
+ #if SIMPLEBLE_BACKEND_LINUX
4
+ #include "UsbHelperLinux.h"
5
+ #elif SIMPLEBLE_BACKEND_WINDOWS
6
+ #include "UsbHelperWindows.h"
7
+ #elif SIMPLEBLE_BACKEND_MACOS
8
+ #include "UsbHelperApple.h"
9
+ #else
10
+ #include "UsbHelperNull.h"
11
+ #endif
12
+
13
+ namespace SimpleBLE {
14
+ namespace Dongl {
15
+ namespace USB {
16
+
17
+ UsbHelper::UsbHelper(const std::string& device_path) : _impl(nullptr) {
18
+ #if SIMPLEBLE_BACKEND_LINUX
19
+ _impl = std::make_unique<UsbHelperLinux>(device_path);
20
+ #elif SIMPLEBLE_BACKEND_WINDOWS
21
+ _impl = std::make_unique<UsbHelperWindows>(device_path);
22
+ #elif SIMPLEBLE_BACKEND_MACOS
23
+ _impl = std::make_unique<UsbHelperApple>(device_path);
24
+ #else
25
+ _impl = std::make_unique<UsbHelperNull>(device_path);
26
+ #endif
27
+ }
28
+
29
+ UsbHelper::~UsbHelper() = default;
30
+
31
+ void UsbHelper::tx(const kvn::bytearray& data) {
32
+ _impl->tx(data);
33
+ }
34
+
35
+ void UsbHelper::set_rx_callback(std::function<void(const kvn::bytearray&)> callback) {
36
+ _impl->set_rx_callback(callback);
37
+ }
38
+
39
+ std::vector<std::string> UsbHelper::get_dongl_devices() {
40
+ #if SIMPLEBLE_BACKEND_LINUX
41
+ return UsbHelperLinux::get_dongl_devices();
42
+ #elif SIMPLEBLE_BACKEND_WINDOWS
43
+ return UsbHelperWindows::get_dongl_devices();
44
+ #elif SIMPLEBLE_BACKEND_MACOS
45
+ return UsbHelperApple::get_dongl_devices();
46
+ #else
47
+ return UsbHelperNull::get_dongl_devices();
48
+ #endif
49
+ }
50
+
51
+ } // namespace USB
52
+ } // namespace Dongl
53
+ } // namespace SimpleBLE
@@ -0,0 +1,32 @@
1
+ #pragma once
2
+
3
+ #include <vector>
4
+ #include <string>
5
+ #include <memory>
6
+
7
+ #include <kvn/kvn_safe_callback.hpp>
8
+ #include <kvn/kvn_bytearray.h>
9
+
10
+ namespace SimpleBLE {
11
+ namespace Dongl {
12
+ namespace USB {
13
+
14
+ class UsbHelperImpl;
15
+
16
+ class UsbHelper {
17
+ public:
18
+ UsbHelper(const std::string& device_path);
19
+ ~UsbHelper();
20
+
21
+ void tx(const kvn::bytearray& data);
22
+ void set_rx_callback(std::function<void(const kvn::bytearray&)> callback);
23
+
24
+ static std::vector<std::string> get_dongl_devices();
25
+
26
+ protected:
27
+ std::unique_ptr<UsbHelperImpl> _impl;
28
+ };
29
+
30
+ } // namespace USB
31
+ } // namespace Dongl
32
+ } // namespace SimpleBLE