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,598 @@
1
+ #pragma once
2
+
3
+ #include <jni.h>
4
+ #include <condition_variable>
5
+ #include <cstdint>
6
+ #include <functional>
7
+ #include <mutex>
8
+ #include <queue>
9
+ #include <string>
10
+ #include <thread>
11
+ #include <type_traits>
12
+ #include <vector>
13
+
14
+ #include "References.hpp"
15
+ #include "Registry.hpp"
16
+ #include "VM.hpp"
17
+ #include "kvn/kvn_bytearray.h"
18
+
19
+ namespace SimpleJNI {
20
+
21
+ // TODO: Implement a base class that handles common functionality of complicated Objects (see the ones in
22
+ // org/simplejavable)
23
+
24
+ class Exception : public std::runtime_error {
25
+ public:
26
+ Exception(jthrowable obj) : std::runtime_error("Java Exception"), _ref(), _cls() {
27
+ _ref = LocalRef<jthrowable>(obj);
28
+
29
+ JNIEnv* env = SimpleJNI::VM::env();
30
+ _cls = LocalRef<jclass>(env->GetObjectClass(_ref.get()));
31
+
32
+ jmethodID method_get_name = env->GetMethodID(_cls.get(), "getName", "()Ljava/lang/String;");
33
+ jmethodID method_get_message = env->GetMethodID(_cls.get(), "getMessage", "()Ljava/lang/String;");
34
+
35
+ std::string exception_name = call_string_method(env, _ref.get(), method_get_name);
36
+ std::string exception_message = call_string_method(env, _ref.get(), method_get_message);
37
+
38
+ _what = "Java Exception: " + exception_name + ": " + exception_message;
39
+ }
40
+
41
+ const char* what() const noexcept override { return _what.c_str(); }
42
+
43
+ private:
44
+ std::string _what;
45
+ LocalRef<jthrowable> _ref;
46
+ LocalRef<jclass> _cls;
47
+
48
+ static std::string call_string_method(JNIEnv* env, jthrowable obj, jmethodID method) {
49
+ auto jstr = static_cast<jstring>(env->CallObjectMethod(obj, method));
50
+ if (!jstr) return "";
51
+ const char* c_str = env->GetStringUTFChars(jstr, nullptr);
52
+ std::string result(c_str);
53
+ env->ReleaseStringUTFChars(jstr, c_str);
54
+ return result;
55
+ }
56
+ };
57
+
58
+ template <template <typename> class RefType, typename JniType = jobject>
59
+ class Object {
60
+ public:
61
+ Object() = default;
62
+
63
+ explicit Object(JniType obj) : _ref(obj) {
64
+ if (obj) {
65
+ JNIEnv* env = VM::env();
66
+ _cls = RefType<jclass>(env->GetObjectClass(_ref.get()));
67
+ }
68
+ }
69
+
70
+ // Constructor with pre-fetched jclass
71
+ Object(JniType obj, jclass cls) : _ref(obj), _cls(cls) {}
72
+
73
+ // Move semantics
74
+ Object(Object&& other) noexcept : _ref(std::move(other._ref)), _cls(std::move(other._cls)) {}
75
+ Object& operator=(Object&& other) noexcept {
76
+ if (this != &other) {
77
+ _ref = std::move(other._ref);
78
+ _cls = std::move(other._cls);
79
+ }
80
+ return *this;
81
+ }
82
+
83
+ // Copying depends on RefType's behavior (enabled by default)
84
+ Object(const Object&) = default;
85
+ Object& operator=(const Object&) = default;
86
+
87
+ // Template constructor for converting between different RefType templates
88
+ template <template <typename> class OtherRefType>
89
+ Object(const Object<OtherRefType, JniType>& other) {
90
+ if (other.get()) {
91
+ JNIEnv* env = VM::env();
92
+ _ref = RefType<JniType>(other.get());
93
+ _cls = RefType<jclass>(env->GetObjectClass(_ref.get()));
94
+ }
95
+ }
96
+
97
+ // Template assignment operator for converting between different RefType templates
98
+ template <template <typename> class OtherRefType>
99
+ Object& operator=(const Object<OtherRefType, JniType>& other) {
100
+ // Don't use pointer comparison for different template types
101
+ // Instead, check if the underlying JNI objects are the same
102
+ JNIEnv* env = VM::env();
103
+
104
+ // Only proceed with assignment if the objects are different
105
+ // or if other is null (in which case we reset this object)
106
+ if (!other.get() || !_ref.get() || !env->IsSameObject(_ref.get(), other.get())) {
107
+ if (other.get()) {
108
+ _ref = RefType<JniType>(other.get());
109
+ _cls = RefType<jclass>(env->GetObjectClass(_ref.get()));
110
+ } else {
111
+ _ref = RefType<JniType>();
112
+ _cls = RefType<jclass>();
113
+ }
114
+ }
115
+ return *this;
116
+ }
117
+
118
+ // Conversion methods
119
+ Object<LocalRef, JniType> to_local() const {
120
+ if (!*this) return Object<LocalRef, JniType>();
121
+ JNIEnv* env = VM::env();
122
+ return Object<LocalRef, JniType>(_ref.get(), _cls.get());
123
+ }
124
+
125
+ Object<GlobalRef, JniType> to_global() const {
126
+ if (!*this) return Object<GlobalRef, JniType>();
127
+ JNIEnv* env = VM::env();
128
+ return Object<GlobalRef, JniType>(_ref.get(), _cls.get());
129
+ }
130
+
131
+ Object<WeakRef, jweak> to_weak() const {
132
+ if (!*this) return Object<WeakRef, jweak>();
133
+ JNIEnv* env = VM::env();
134
+ return Object<WeakRef, jweak>(WeakRef<jweak>(_ref.get()));
135
+ }
136
+
137
+ // Access raw jobject
138
+ JniType get() const { return _ref.get(); }
139
+
140
+ // Release ownership of the underlying reference
141
+ JniType release() noexcept { return _ref.release(); }
142
+
143
+ explicit operator bool() const { return _ref.get() != nullptr; }
144
+
145
+ bool is_valid() const { return _ref.is_valid(); }
146
+
147
+ jmethodID get_method(const char* name, const char* signature) const {
148
+ JNIEnv* env = VM::env();
149
+ jmethodID method = env->GetMethodID(_cls.get(), name, signature);
150
+ check_exception(env);
151
+ return method;
152
+ }
153
+
154
+ template <typename... Args>
155
+ Object<LocalRef, JniType> call_object_method(jmethodID method, Args&&... args) const {
156
+ JNIEnv* env = VM::env();
157
+ JniType result = env->CallObjectMethod(_ref.get(), method, std::forward<Args>(args)...);
158
+ check_exception(env);
159
+ return Object<LocalRef, JniType>(result);
160
+ }
161
+
162
+ template <typename... Args>
163
+ void call_void_method(jmethodID method, Args&&... args) const {
164
+ JNIEnv* env = VM::env();
165
+ env->CallVoidMethod(_ref.get(), method, std::forward<Args>(args)...);
166
+ check_exception(env);
167
+ }
168
+
169
+ template <typename... Args>
170
+ bool call_boolean_method(jmethodID method, Args&&... args) const {
171
+ JNIEnv* env = VM::env();
172
+ bool result = env->CallBooleanMethod(_ref.get(), method, std::forward<Args>(args)...);
173
+ check_exception(env);
174
+ return result;
175
+ }
176
+
177
+ template <typename... Args>
178
+ int call_int_method(jmethodID method, Args&&... args) const {
179
+ JNIEnv* env = VM::env();
180
+ int result = env->CallIntMethod(_ref.get(), method, std::forward<Args>(args)...);
181
+ check_exception(env);
182
+ return result;
183
+ }
184
+
185
+ template <typename... Args>
186
+ std::string call_string_method(jmethodID method, Args&&... args) const {
187
+ JNIEnv* env = VM::env();
188
+ auto jstr = static_cast<jstring>(env->CallObjectMethod(_ref.get(), method, std::forward<Args>(args)...));
189
+ if (!jstr) return "";
190
+ const char* c_str = env->GetStringUTFChars(jstr, nullptr);
191
+ std::string result(c_str);
192
+ env->ReleaseStringUTFChars(jstr, c_str);
193
+ return result;
194
+ }
195
+
196
+ template <typename... Args>
197
+ kvn::bytearray call_byte_array_method(jmethodID method, Args&&... args) const {
198
+ JNIEnv* env = VM::env();
199
+ auto jarr = static_cast<jbyteArray>(env->CallObjectMethod(_ref.get(), method, std::forward<Args>(args)...));
200
+ check_exception(env);
201
+ if (!jarr) return {};
202
+ jsize len = env->GetArrayLength(jarr);
203
+ jbyte* arr = env->GetByteArrayElements(jarr, nullptr);
204
+ kvn::bytearray result(arr, arr + len);
205
+ env->ReleaseByteArrayElements(jarr, arr, JNI_ABORT);
206
+ return result;
207
+ }
208
+
209
+ template <typename... Args>
210
+ static Object<LocalRef, JniType> call_new_object(jclass cls, jmethodID method, Args&&... args) {
211
+ JNIEnv* env = VM::env();
212
+ JniType result = env->NewObject(cls, method, std::forward<Args>(args)...);
213
+ check_exception(env);
214
+ return Object<LocalRef, JniType>(result);
215
+ }
216
+
217
+ static void check_exception(JNIEnv* env) {
218
+ if (env->ExceptionCheck()) {
219
+ Object<LocalRef, jthrowable> exception(env->ExceptionOccurred());
220
+ env->ExceptionClear();
221
+ throw Exception(exception.get());
222
+ }
223
+ }
224
+
225
+ protected:
226
+ RefType<JniType> _ref; // Holds LocalRef<JniType> or GlobalRef<JniType>
227
+ RefType<jclass> _cls; // Holds LocalRef<jclass> or GlobalRef<jclass>
228
+ };
229
+
230
+ template <template <typename> class RefType>
231
+ class ByteArray {
232
+ public:
233
+ ByteArray() = default;
234
+
235
+ // NOTE: The user is responsible for ensuring that the jobject is a jbyteArray
236
+ explicit ByteArray(jobject obj) : _ref(static_cast<jbyteArray>(obj)) {
237
+ JNIEnv* env = VM::env();
238
+ jclass cls = env->FindClass("java/lang/Object");
239
+ this->_cls = RefType<jclass>(cls);
240
+ env->DeleteLocalRef(cls);
241
+ }
242
+
243
+ explicit ByteArray(jbyteArray obj) : _ref(obj) {
244
+ JNIEnv* env = VM::env();
245
+ jclass cls = env->FindClass("java/lang/Object");
246
+ this->_cls = RefType<jclass>(cls);
247
+ env->DeleteLocalRef(cls);
248
+ }
249
+
250
+ ByteArray(const kvn::bytearray& data) : _ref() {
251
+ JNIEnv* env = VM::env();
252
+ jbyteArray jarr = env->NewByteArray(data.size());
253
+ env->SetByteArrayRegion(jarr, 0, data.size(), reinterpret_cast<const jbyte*>(data.data()));
254
+
255
+ this->_ref = RefType<jbyteArray>(jarr);
256
+ jclass cls = env->FindClass("java/lang/Object");
257
+ this->_cls = RefType<jclass>(cls);
258
+ env->DeleteLocalRef(cls);
259
+ }
260
+
261
+ template <template <typename> class OtherRefType>
262
+ ByteArray(const Object<OtherRefType, jbyteArray>& obj) : _ref(obj.get()) {}
263
+
264
+ // Add implicit conversion to Object<RefType, jobject>
265
+ operator Object<RefType, jobject>() const {
266
+ return Object<RefType, jobject>(static_cast<jobject>(this->get()), this->_cls.get());
267
+ }
268
+
269
+ // Access raw jobject
270
+ jbyteArray get() const { return _ref.get(); }
271
+
272
+ // Release ownership of the underlying reference
273
+ jbyteArray release() noexcept { return _ref.release(); }
274
+
275
+ // Conversion methods
276
+ ByteArray<LocalRef> to_local() const {
277
+ if (!*this) return ByteArray<LocalRef>();
278
+ return ByteArray<LocalRef>(this->get());
279
+ }
280
+
281
+ ByteArray<GlobalRef> to_global() const {
282
+ if (!*this) return ByteArray<GlobalRef>();
283
+ return ByteArray<GlobalRef>(this->get());
284
+ }
285
+
286
+ explicit operator bool() const { return _ref.get() != nullptr; }
287
+
288
+ // Get the raw byte array data
289
+ kvn::bytearray bytes() const {
290
+ JNIEnv* env = VM::env();
291
+ jbyteArray jarr = this->get();
292
+
293
+ if (jarr == nullptr) {
294
+ return {};
295
+ }
296
+
297
+ jsize len = env->GetArrayLength(jarr);
298
+ kvn::bytearray result(len);
299
+
300
+ env->GetByteArrayRegion(jarr, 0, len, reinterpret_cast<jbyte*>(result.data()));
301
+
302
+ return result;
303
+ }
304
+
305
+ // Get the length of the byte array
306
+ size_t length() const {
307
+ JNIEnv* env = VM::env();
308
+ return env->GetArrayLength(this->get());
309
+ }
310
+
311
+ protected:
312
+ RefType<jbyteArray> _ref;
313
+ RefType<jclass> _cls;
314
+ };
315
+
316
+ template <template <typename> class RefType>
317
+ class LongArray {
318
+ public:
319
+ LongArray() = default;
320
+
321
+ explicit LongArray(jlongArray obj) : _ref(obj) {
322
+ JNIEnv* env = VM::env();
323
+ jclass cls = env->FindClass("java/lang/Object");
324
+ this->_cls = RefType<jclass>(cls);
325
+ env->DeleteLocalRef(cls);
326
+ }
327
+
328
+ LongArray(const std::vector<int64_t>& data) : _ref() {
329
+ JNIEnv* env = VM::env();
330
+ jlongArray jarr = env->NewLongArray(data.size());
331
+ env->SetLongArrayRegion(jarr, 0, data.size(), reinterpret_cast<const jlong*>(data.data()));
332
+
333
+ this->_ref = RefType<jlongArray>(jarr);
334
+ jclass cls = env->FindClass("java/lang/Object");
335
+ this->_cls = RefType<jclass>(cls);
336
+ env->DeleteLocalRef(cls);
337
+ }
338
+
339
+ template <template <typename> class OtherRefType>
340
+ LongArray(const Object<OtherRefType, jlongArray>& obj) : _ref(obj.get()) {}
341
+
342
+ // Add implicit conversion to Object<RefType, jobject>
343
+ operator Object<RefType, jobject>() const {
344
+ return Object<RefType, jobject>(static_cast<jobject>(this->get()), this->_cls.get());
345
+ }
346
+
347
+ // Access raw jobject
348
+ jlongArray get() const { return _ref.get(); }
349
+
350
+ // Release ownership of the underlying reference
351
+ jlongArray release() noexcept { return _ref.release(); }
352
+
353
+ // Conversion methods
354
+ LongArray<LocalRef> to_local() const {
355
+ if (!*this) return LongArray<LocalRef>();
356
+ return LongArray<LocalRef>(this->get());
357
+ }
358
+
359
+ LongArray<GlobalRef> to_global() const {
360
+ if (!*this) return LongArray<GlobalRef>();
361
+ return LongArray<GlobalRef>(this->get());
362
+ }
363
+
364
+ explicit operator bool() const { return _ref.get() != nullptr; }
365
+
366
+ // Get the raw long array data
367
+ std::vector<int64_t> longs() const {
368
+ JNIEnv* env = VM::env();
369
+ jlongArray jarr = this->get();
370
+
371
+ if (jarr == nullptr) {
372
+ return {};
373
+ }
374
+
375
+ jsize len = env->GetArrayLength(jarr);
376
+ std::vector<int64_t> result(len);
377
+
378
+ env->GetLongArrayRegion(jarr, 0, len, reinterpret_cast<jlong*>(result.data()));
379
+
380
+ return result;
381
+ }
382
+
383
+ // Get the length of the long array
384
+ size_t length() const {
385
+ JNIEnv* env = VM::env();
386
+ jlongArray jarr = this->get();
387
+
388
+ if (jarr == nullptr) {
389
+ return 0; // Return 0 for null arrays
390
+ }
391
+
392
+ return env->GetArrayLength(jarr);
393
+ }
394
+
395
+ protected:
396
+ RefType<jlongArray> _ref;
397
+ RefType<jclass> _cls;
398
+ };
399
+
400
+ template <template <typename> class RefType>
401
+ class String {
402
+ public:
403
+ String() = default;
404
+
405
+ explicit String(jstring obj) : _ref(obj) {
406
+ JNIEnv* env = VM::env();
407
+ jclass cls = env->FindClass("java/lang/Object");
408
+ this->_cls = RefType<jclass>(cls);
409
+ env->DeleteLocalRef(cls);
410
+ }
411
+
412
+ String(const std::string& data) : _ref() {
413
+ JNIEnv* env = VM::env();
414
+ jstring jstr = env->NewStringUTF(data.c_str());
415
+
416
+ this->_ref = RefType<jstring>(jstr);
417
+ jclass cls = env->FindClass("java/lang/Object");
418
+ this->_cls = RefType<jclass>(cls);
419
+ env->DeleteLocalRef(cls);
420
+ }
421
+
422
+ template <template <typename> class OtherRefType>
423
+ String(const Object<OtherRefType, jstring>& obj) : _ref(obj.get()) {}
424
+
425
+ // Add implicit conversion to Object<RefType, jobject>
426
+ operator Object<RefType, jobject>() const {
427
+ return Object<RefType, jobject>(static_cast<jobject>(this->get()), this->_cls.get());
428
+ }
429
+
430
+ explicit operator bool() const { return _ref.get() != nullptr; }
431
+
432
+ // Access raw jobject
433
+ jstring get() const { return _ref.get(); }
434
+
435
+ // Release ownership of the underlying reference
436
+ jstring release() noexcept { return _ref.release(); }
437
+
438
+ // Conversion methods
439
+ String<LocalRef> to_local() const {
440
+ if (!*this) return String<LocalRef>();
441
+ return String<LocalRef>(this->get());
442
+ }
443
+
444
+ String<GlobalRef> to_global() const {
445
+ if (!*this) return String<GlobalRef>();
446
+ return String<GlobalRef>(this->get());
447
+ }
448
+
449
+ // Get the raw string data
450
+ std::string str() const {
451
+ JNIEnv* env = VM::env();
452
+ jstring jstr = this->get();
453
+
454
+ if (jstr == nullptr) {
455
+ return {};
456
+ }
457
+
458
+ const char* c_str = env->GetStringUTFChars(jstr, nullptr);
459
+ std::string result(c_str);
460
+ env->ReleaseStringUTFChars(jstr, c_str);
461
+
462
+ return result;
463
+ }
464
+
465
+ // Get the length of the string
466
+ size_t length() const {
467
+ JNIEnv* env = VM::env();
468
+ return env->GetStringUTFLength(this->get());
469
+ }
470
+
471
+ protected:
472
+ RefType<jstring> _ref;
473
+ RefType<jclass> _cls;
474
+ };
475
+
476
+ class Env {
477
+ public:
478
+ Env() { _env = VM::env(); }
479
+ virtual ~Env() = default;
480
+ Env(Env& other) = delete; // Remove the copy constructor
481
+ void operator=(const Env&) = delete; // Remove the copy assignment
482
+
483
+ JNIEnv* operator->() { return _env; }
484
+
485
+ operator JNIEnv*() { return _env; }
486
+
487
+ // Class find_class(const std::string& name) {
488
+ // jclass jcls = _env->FindClass(name.c_str());
489
+ // if (jcls == nullptr) {
490
+ // throw std::runtime_error("Failed to find class: " + name);
491
+ // }
492
+ // Class cls(jcls);
493
+ // _env->DeleteLocalRef(jcls);
494
+ // return cls;
495
+ // }
496
+
497
+ private:
498
+ JNIEnv* _env = nullptr;
499
+ };
500
+
501
+ class Runner {
502
+ public:
503
+ // Delete copy constructor and assignment
504
+ Runner(const Runner&) = delete;
505
+ Runner& operator=(const Runner&) = delete;
506
+
507
+ static Runner& get() {
508
+ static Runner instance;
509
+ return instance;
510
+ }
511
+
512
+ virtual ~Runner() {
513
+ {
514
+ std::unique_lock<std::mutex> lock(_mutex);
515
+ _stop = true;
516
+ _cv.notify_one();
517
+ }
518
+ _thread.join();
519
+ }
520
+
521
+ void enqueue(std::function<void()> func) {
522
+ {
523
+ std::unique_lock<std::mutex> lock(_mutex);
524
+ _queue.push(std::move(func));
525
+ lock.unlock();
526
+ _cv.notify_one();
527
+ }
528
+ }
529
+
530
+ protected:
531
+ void thread_func() {
532
+ VM::attach();
533
+ while (true) {
534
+ std::function<void()> func;
535
+ {
536
+ std::unique_lock<std::mutex> lock(_mutex);
537
+ _cv.wait(lock, [this] { return _stop || !_queue.empty(); });
538
+ if (_stop && _queue.empty()) {
539
+ break;
540
+ }
541
+ func = std::move(_queue.front());
542
+ _queue.pop();
543
+ }
544
+ func();
545
+ }
546
+ VM::detach();
547
+ }
548
+
549
+ private:
550
+ // Move constructor to private
551
+ Runner() : _stop(false) { _thread = std::thread(&Runner::thread_func, this); }
552
+
553
+ std::thread _thread;
554
+ std::mutex _mutex;
555
+ std::condition_variable _cv;
556
+ std::queue<std::function<void()>> _queue;
557
+ bool _stop;
558
+ };
559
+
560
+ template <template <typename> class RefType, typename JniType = jobject>
561
+ struct ObjectComparator {
562
+
563
+ bool operator()(const Object<RefType, JniType>& lhs, const Object<RefType, JniType>& rhs) const {
564
+ // Handle null object comparisons
565
+ if (!lhs && !rhs) {
566
+ return false; // Both are null, considered equal
567
+ }
568
+ if (!lhs) {
569
+ return true; // lhs is null, rhs is not, lhs < rhs
570
+ }
571
+ if (!rhs) {
572
+ return false; // rhs is null, lhs is not, lhs > rhs
573
+ }
574
+
575
+ JNIEnv* env = VM::env();
576
+
577
+ // Access the underlying jobject handles from Object instances
578
+ JniType lhsObject = lhs.get();
579
+ JniType rhsObject = rhs.get();
580
+
581
+ // Check if both jobject handles refer to the same object
582
+ if (env->IsSameObject(lhsObject, rhsObject)) {
583
+ return false; // Both objects are the same
584
+ }
585
+
586
+ jmethodID method_hashCode = Registrar::get().get_method("java/lang/Object", "hashCode");
587
+ jint lhsHashCode = env->CallIntMethod(lhsObject, method_hashCode);
588
+ jint rhsHashCode = env->CallIntMethod(rhsObject, method_hashCode);
589
+
590
+ if (lhsHashCode != rhsHashCode) {
591
+ return lhsHashCode < rhsHashCode; // Use hash code for initial comparison
592
+ }
593
+
594
+ // Use a direct pointer comparison as a fallback for objects with identical hash codes
595
+ return lhsObject < rhsObject; // This comparison is consistent within the same execution
596
+ }
597
+ };
598
+ } // namespace SimpleJNI