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,714 @@
1
+ #import "PeripheralBaseMacOS.h"
2
+ #import "CharacteristicBase.h"
3
+ #import "CommonUtils.h"
4
+ #import "DescriptorBase.h"
5
+ #import "LoggingInternal.h"
6
+ #import "ServiceBase.h"
7
+ #import "Utils.h"
8
+
9
+ #import <simpleble/Characteristic.h>
10
+ #import <simpleble/Descriptor.h>
11
+ #import <simpleble/Exceptions.h>
12
+ #import <simpleble/Service.h>
13
+ #import <optional>
14
+
15
+ #define WAIT_UNTIL_FALSE(obj, var) \
16
+ do { \
17
+ BOOL _tmpVar = YES; \
18
+ while (_tmpVar) { \
19
+ [NSThread sleepForTimeInterval:0.01]; \
20
+ @synchronized(obj) { \
21
+ _tmpVar = (var); \
22
+ } \
23
+ } \
24
+ } while (0)
25
+
26
+ #define WAIT_UNTIL_FALSE_WITH_TIMEOUT(obj, var, timeout) \
27
+ do { \
28
+ BOOL _tmpVar = YES; \
29
+ NSDate* endDate = [NSDate dateWithTimeInterval:timeout sinceDate:NSDate.now]; \
30
+ while (_tmpVar && [NSDate.now compare:endDate] == NSOrderedAscending) { \
31
+ [NSThread sleepForTimeInterval:0.01]; \
32
+ @synchronized(obj) { \
33
+ _tmpVar = (var); \
34
+ } \
35
+ } \
36
+ } while (0)
37
+
38
+ // --------------------------------------------------
39
+
40
+ @interface BleTask : NSObject
41
+ @property(strong, atomic) NSError* error;
42
+ @property(atomic) BOOL pending;
43
+ @end
44
+
45
+ @implementation BleTask
46
+ - (instancetype)init {
47
+ self = [super init];
48
+ if (self) {
49
+ _pending = NO;
50
+ }
51
+ return self;
52
+ }
53
+ @end
54
+
55
+ @interface DescriptorExtras : NSObject
56
+ @property(strong) BleTask* task;
57
+ @end
58
+
59
+ @implementation DescriptorExtras
60
+ - (instancetype)init {
61
+ self = [super init];
62
+ if (self) {
63
+ _task = [[BleTask alloc] init];
64
+ }
65
+ return self;
66
+ }
67
+ @end
68
+
69
+ @interface CharacteristicExtras : NSObject {
70
+ @public
71
+ std::function<void(SimpleBLE::ByteArray)> valueChangedCallback;
72
+ }
73
+ @property(strong) BleTask* task;
74
+ @property(strong) NSData* value;
75
+ @property(strong) NSMutableDictionary<NSString*, DescriptorExtras*>* descriptorExtras;
76
+ @end
77
+
78
+ @implementation CharacteristicExtras
79
+ - (instancetype)init {
80
+ self = [super init];
81
+ if (self) {
82
+ _task = [[BleTask alloc] init];
83
+ _descriptorExtras = [[NSMutableDictionary alloc] init];
84
+ }
85
+ return self;
86
+ }
87
+ @end
88
+
89
+ // --------------------------------------------------
90
+
91
+ @interface PeripheralBaseMacOS () {
92
+ }
93
+
94
+ @property(strong) CBPeripheral* peripheral;
95
+ @property(strong) CBCentralManager* centralManager;
96
+ @property(strong, atomic) BleTask* task;
97
+ @property(strong, atomic) NSError* disconnectionError;
98
+ @property(strong) NSMutableDictionary<NSString*, CharacteristicExtras*>* characteristicExtras;
99
+
100
+ - (CBService*)findService:(NSString*)uuid;
101
+ - (CBCharacteristic*)findCharacteristic:(NSString*)uuid service:(CBService*)service;
102
+ - (std::pair<CBService*, CBCharacteristic*>)findServiceAndCharacteristic:(NSString*)service_uuid
103
+ characteristic_uuid:(NSString*)characteristic_uuid;
104
+ - (void)throwBasedOnError:(NSError*)error withFormat:(NSString*)format, ...;
105
+
106
+ @end
107
+
108
+ @implementation PeripheralBaseMacOS
109
+
110
+ - (instancetype)init:(CBPeripheral*)peripheral centralManager:(CBCentralManager*)centralManager {
111
+ self = [super init];
112
+ if (self) {
113
+ // NOTE: It's important to make a copy of the peripheral and central objects into
114
+ // a strong property to prevent them from being deallocated by ARC or the garbage collector.
115
+ _peripheral = [peripheral copy];
116
+ _centralManager = centralManager;
117
+
118
+ _peripheral.delegate = self;
119
+ _characteristicExtras = [[NSMutableDictionary alloc] init];
120
+ _task = [[BleTask alloc] init];
121
+ }
122
+ return self;
123
+ }
124
+
125
+ - (void*)underlying {
126
+ return (__bridge void*)self.peripheral;
127
+ }
128
+
129
+ - (NSString*)identifier {
130
+ if (self.peripheral.name != nil) {
131
+ return [self.peripheral.name copy];
132
+ } else {
133
+ return @"";
134
+ }
135
+ }
136
+
137
+ - (NSString*)address {
138
+ return [self.peripheral.identifier UUIDString];
139
+ }
140
+
141
+ - (uint16_t)mtu {
142
+ return [self.peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithoutResponse];
143
+ }
144
+
145
+ - (void)connect {
146
+ if (self.peripheral.state == CBPeripheralStateConnected) {
147
+ return;
148
+ }
149
+
150
+ @synchronized(_task) {
151
+ // --- Connect to the peripheral ---
152
+ @synchronized(self) {
153
+ _task.error = nil;
154
+ _task.pending = YES;
155
+ [self.centralManager connectPeripheral:self.peripheral options:@{}]; // TODO: Do we need to pass any options?
156
+ }
157
+
158
+ WAIT_UNTIL_FALSE_WITH_TIMEOUT(self, _task.pending, 5.0);
159
+
160
+ if (self.peripheral.state != CBPeripheralStateConnected || _task.error != nil) {
161
+ [self throwBasedOnError:_task.error withFormat:@"Peripheral Connection"];
162
+ }
163
+
164
+ // --- Discover services and characteristics ---
165
+
166
+ @synchronized(self) {
167
+ _task.error = nil;
168
+ _task.pending = YES;
169
+ [self.peripheral discoverServices:nil];
170
+ }
171
+
172
+ WAIT_UNTIL_FALSE(self, _task.pending);
173
+
174
+ if (self.peripheral.state != CBPeripheralStateConnected) {
175
+ [self throwBasedOnError:_disconnectionError withFormat:@"Service Discovery"];
176
+ }
177
+
178
+ if (self.peripheral.services == nil || _task.error != nil) {
179
+ [self throwBasedOnError:_task.error withFormat:@"Service Discovery"];
180
+ }
181
+
182
+ // For each service found, discover characteristics.
183
+ for (CBService* service in self.peripheral.services) {
184
+ @synchronized(self) {
185
+ _task.error = nil;
186
+ _task.pending = YES;
187
+ [self.peripheral discoverCharacteristics:nil forService:service];
188
+ }
189
+
190
+ WAIT_UNTIL_FALSE(self, _task.pending);
191
+
192
+ if (self.peripheral.state != CBPeripheralStateConnected) {
193
+ [self throwBasedOnError:_disconnectionError withFormat:@"Characteristic Discovery for service %@", service.UUID];
194
+ }
195
+
196
+ if (service.characteristics == nil || _task.error != nil) {
197
+ [self throwBasedOnError:_task.error withFormat:@"Characteristic Discovery for service %@", service.UUID];
198
+ }
199
+
200
+ // For each characteristic, create the associated extra properties and discover descriptors.
201
+ for (CBCharacteristic* characteristic in service.characteristics) {
202
+ CharacteristicExtras* characteristicExtras = [[CharacteristicExtras alloc] init];
203
+
204
+ @synchronized(self) {
205
+ _task.error = nil;
206
+ _task.pending = YES;
207
+ [self.peripheral discoverDescriptorsForCharacteristic:characteristic];
208
+ }
209
+
210
+ WAIT_UNTIL_FALSE(self, _task.pending);
211
+
212
+ if (self.peripheral.state != CBPeripheralStateConnected) {
213
+ [self throwBasedOnError:_disconnectionError
214
+ withFormat:@"Descriptor Discovery for characteristic %@", characteristic.UUID];
215
+ }
216
+
217
+ if (characteristic.descriptors == nil || _task.error != nil) {
218
+ [self throwBasedOnError:_task.error withFormat:@"Descriptor Discovery for characteristic %@", characteristic.UUID];
219
+ }
220
+
221
+ // For each descriptor, create the associated extra properties.
222
+ for (CBDescriptor* descriptor in characteristic.descriptors) {
223
+ @synchronized(self) {
224
+ [characteristicExtras.descriptorExtras setObject:[[DescriptorExtras alloc] init]
225
+ forKey:uuidToString(descriptor.UUID)];
226
+ }
227
+ }
228
+
229
+ @synchronized(self) {
230
+ [self.characteristicExtras setObject:characteristicExtras forKey:uuidToString(characteristic.UUID)];
231
+ }
232
+ }
233
+ }
234
+ }
235
+ }
236
+
237
+ - (void)disconnect {
238
+ if (self.peripheral.state == CBPeripheralStateDisconnected) {
239
+ return;
240
+ }
241
+
242
+ @synchronized(_task) {
243
+ @synchronized(self) {
244
+ self->_disconnectionError = nil;
245
+ _task.pending = YES;
246
+ [self.centralManager cancelPeripheralConnection:self.peripheral];
247
+ }
248
+
249
+ WAIT_UNTIL_FALSE(self, _task.pending);
250
+
251
+ if (self.peripheral.state != CBPeripheralStateDisconnected) {
252
+ [self throwBasedOnError:_disconnectionError withFormat:@"Peripheral Disconnection"];
253
+ }
254
+ }
255
+ }
256
+
257
+ - (bool)isConnected {
258
+ return self.peripheral.state == CBPeripheralStateConnected;
259
+ }
260
+
261
+ - (SimpleBLE::SharedPtrVector<SimpleBLE::ServiceBase>)getServices {
262
+ SimpleBLE::SharedPtrVector<SimpleBLE::ServiceBase> service_list;
263
+ for (CBService* service in self.peripheral.services) {
264
+ // Build the list of characteristics for the service.
265
+ SimpleBLE::SharedPtrVector<SimpleBLE::CharacteristicBase> characteristic_list;
266
+ for (CBCharacteristic* characteristic in service.characteristics) {
267
+ // Build the list of descriptors for the characteristic.
268
+ SimpleBLE::SharedPtrVector<SimpleBLE::DescriptorBase> descriptor_list;
269
+ for (CBDescriptor* descriptor in characteristic.descriptors) {
270
+ descriptor_list.push_back(std::make_shared<SimpleBLE::DescriptorBase>(uuidToSimpleBLE(descriptor.UUID)));
271
+ }
272
+
273
+ bool can_read = (characteristic.properties & CBCharacteristicPropertyRead) != 0;
274
+ bool can_write_request = (characteristic.properties & CBCharacteristicPropertyWrite) != 0;
275
+ bool can_write_command = (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) != 0;
276
+ bool can_notify = (characteristic.properties & CBCharacteristicPropertyNotify) != 0;
277
+ bool can_indicate = (characteristic.properties & CBCharacteristicPropertyIndicate) != 0;
278
+
279
+ characteristic_list.push_back(std::make_shared<SimpleBLE::CharacteristicBase>(uuidToSimpleBLE(characteristic.UUID),
280
+ descriptor_list, can_read, can_write_request,
281
+ can_write_command, can_notify, can_indicate));
282
+ }
283
+ service_list.push_back(std::make_shared<SimpleBLE::ServiceBase>(uuidToSimpleBLE(service.UUID), characteristic_list));
284
+ }
285
+
286
+ return service_list;
287
+ }
288
+
289
+ - (SimpleBLE::ByteArray)read:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid {
290
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
291
+ characteristic_uuid:characteristic_uuid];
292
+
293
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
294
+
295
+ // Check that the characteristic supports this feature.
296
+ if ((characteristic.properties & CBCharacteristicPropertyRead) == 0) {
297
+ NSLog(@"Characteristic does not support read.");
298
+ throw SimpleBLE::Exception::OperationNotSupported();
299
+ }
300
+
301
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
302
+ BleTask* task = characteristicExtras.task;
303
+
304
+ @synchronized(task) {
305
+ if (characteristic.isNotifying) {
306
+ // If the characteristic is already notifying, we'll just wait for the next notification.
307
+ @synchronized(self) {
308
+ characteristicExtras.value = nil;
309
+ }
310
+
311
+ WAIT_UNTIL_FALSE(self, (characteristicExtras.value == nil));
312
+
313
+ return SimpleBLE::ByteArray((const char*)characteristicExtras.value.bytes, characteristicExtras.value.length);
314
+
315
+ } else {
316
+ // Otherwise, we'll trigger a value read and wait for the response.
317
+ @synchronized(self) {
318
+ task.error = nil;
319
+ task.pending = YES;
320
+ [self.peripheral readValueForCharacteristic:characteristic];
321
+ }
322
+
323
+ WAIT_UNTIL_FALSE(self, task.pending);
324
+
325
+ if (task.error != nil) {
326
+ [self throwBasedOnError:task.error withFormat:@"Characteristic %@ Read", characteristic.UUID];
327
+ }
328
+
329
+ return SimpleBLE::ByteArray((const char*)characteristic.value.bytes, characteristic.value.length);
330
+ }
331
+ }
332
+ }
333
+
334
+ - (void)writeRequest:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid payload:(NSData*)payload {
335
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
336
+ characteristic_uuid:characteristic_uuid];
337
+
338
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
339
+
340
+ // Check that the characteristic supports this feature.
341
+ if ((characteristic.properties & CBCharacteristicPropertyWrite) == 0) {
342
+ NSLog(@"Characteristic does not support write with response.");
343
+ throw SimpleBLE::Exception::OperationNotSupported();
344
+ }
345
+
346
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
347
+ BleTask* task = characteristicExtras.task;
348
+
349
+ @synchronized(task) {
350
+ @synchronized(self) {
351
+ task.error = nil;
352
+ task.pending = YES;
353
+ [self.peripheral writeValue:payload forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
354
+ }
355
+
356
+ WAIT_UNTIL_FALSE(self, task.pending);
357
+
358
+ if (task.error != nil) {
359
+ [self throwBasedOnError:task.error withFormat:@"Characteristic %@ Write Request", characteristic.UUID];
360
+ }
361
+ }
362
+ }
363
+
364
+ - (void)writeCommand:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid payload:(NSData*)payload {
365
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
366
+ characteristic_uuid:characteristic_uuid];
367
+
368
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
369
+
370
+ // Check that the characteristic supports this feature.
371
+ if ((characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) == 0) {
372
+ NSLog(@"Characteristic does not support write without response.");
373
+ throw SimpleBLE::Exception::OperationNotSupported();
374
+ }
375
+
376
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
377
+ BleTask* task = characteristicExtras.task;
378
+
379
+ @synchronized(task) {
380
+ // NOTE: This write is unacknowledged.
381
+ @synchronized(self) {
382
+ [self.peripheral writeValue:payload forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
383
+ }
384
+ }
385
+ }
386
+
387
+ - (void)notify:(NSString*)service_uuid
388
+ characteristic_uuid:(NSString*)characteristic_uuid
389
+ callback:(std::function<void(SimpleBLE::ByteArray)>)callback {
390
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
391
+ characteristic_uuid:characteristic_uuid];
392
+
393
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
394
+
395
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
396
+ BleTask* task = characteristicExtras.task;
397
+
398
+ @synchronized(task) {
399
+ @synchronized(self) {
400
+ task.error = nil;
401
+ task.pending = YES;
402
+ characteristicExtras->valueChangedCallback = callback;
403
+ [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
404
+ }
405
+
406
+ WAIT_UNTIL_FALSE(self, task.pending);
407
+
408
+ if (!characteristic.isNotifying || task.error != nil) {
409
+ [self throwBasedOnError:task.error withFormat:@"Characteristic %@ Notify/Indicate", characteristic.UUID];
410
+ }
411
+ }
412
+ }
413
+
414
+ - (void)indicate:(NSString*)service_uuid
415
+ characteristic_uuid:(NSString*)characteristic_uuid
416
+ callback:(std::function<void(SimpleBLE::ByteArray)>)callback {
417
+ [self notify:service_uuid characteristic_uuid:characteristic_uuid callback:callback];
418
+ }
419
+
420
+ - (void)unsubscribe:(NSString*)service_uuid characteristic_uuid:(NSString*)characteristic_uuid {
421
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
422
+ characteristic_uuid:characteristic_uuid];
423
+
424
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
425
+
426
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
427
+ BleTask* task = characteristicExtras.task;
428
+
429
+ @synchronized(task) {
430
+ @synchronized(self) {
431
+ task.error = nil;
432
+ task.pending = YES;
433
+ characteristicExtras->valueChangedCallback = nil;
434
+ [self.peripheral setNotifyValue:NO forCharacteristic:characteristic];
435
+ }
436
+
437
+ WAIT_UNTIL_FALSE(self, task.pending);
438
+
439
+ if (characteristic.isNotifying || task.error != nil) {
440
+ [self throwBasedOnError:task.error withFormat:@"Characteristic %@ Unsubscribe", characteristic.UUID];
441
+ }
442
+ }
443
+ }
444
+
445
+ - (SimpleBLE::ByteArray)read:(NSString*)service_uuid
446
+ characteristic_uuid:(NSString*)characteristic_uuid
447
+ descriptor_uuid:(NSString*)descriptor_uuid {
448
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
449
+ characteristic_uuid:characteristic_uuid];
450
+
451
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
452
+ CBDescriptor* descriptor = [self findDescriptor:descriptor_uuid characteristic:characteristic];
453
+
454
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
455
+ DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:descriptor_uuid];
456
+ BleTask* task = descriptorExtras.task;
457
+
458
+ @synchronized(task) {
459
+ @synchronized(self) {
460
+ task.error = nil;
461
+ task.pending = YES;
462
+ [self.peripheral readValueForDescriptor:descriptor];
463
+ }
464
+
465
+ WAIT_UNTIL_FALSE(self, task.pending);
466
+
467
+ if (task.error != nil) {
468
+ [self throwBasedOnError:task.error withFormat:@"Descriptor %@ Read", descriptor.UUID];
469
+ }
470
+
471
+ const char* bytes = (const char*)[descriptor.value bytes];
472
+
473
+ return SimpleBLE::ByteArray(bytes, [descriptor.value length]);
474
+ }
475
+ }
476
+
477
+ - (void)write:(NSString*)service_uuid
478
+ characteristic_uuid:(NSString*)characteristic_uuid
479
+ descriptor_uuid:(NSString*)descriptor_uuid
480
+ payload:(NSData*)payload {
481
+ std::pair<CBService*, CBCharacteristic*> serviceAndCharacteristic = [self findServiceAndCharacteristic:service_uuid
482
+ characteristic_uuid:characteristic_uuid];
483
+
484
+ CBCharacteristic* characteristic = serviceAndCharacteristic.second;
485
+ CBDescriptor* descriptor = [self findDescriptor:descriptor_uuid characteristic:characteristic];
486
+
487
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:characteristic_uuid];
488
+ DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:descriptor_uuid];
489
+ BleTask* task = descriptorExtras.task;
490
+
491
+ @synchronized(task) {
492
+ @synchronized(self) {
493
+ task.error = nil;
494
+ task.pending = YES;
495
+ [self.peripheral writeValue:payload forDescriptor:descriptor];
496
+ }
497
+
498
+ WAIT_UNTIL_FALSE(self, task.pending);
499
+
500
+ if (task.error) {
501
+ [self throwBasedOnError:task.error withFormat:@"Descriptor %@ Write", descriptor.UUID];
502
+ }
503
+ }
504
+ }
505
+
506
+ #pragma mark - Auxiliary methods
507
+
508
+ - (CBService*)findService:(NSString*)uuid {
509
+ CBUUID* service_uuid = [CBUUID UUIDWithString:uuid];
510
+
511
+ for (CBService* service in self.peripheral.services) {
512
+ if ([service.UUID isEqual:service_uuid]) {
513
+ return service;
514
+ }
515
+ }
516
+
517
+ throw SimpleBLE::Exception::ServiceNotFound([uuid UTF8String]);
518
+ }
519
+
520
+ - (CBCharacteristic*)findCharacteristic:(NSString*)uuid service:(CBService*)service {
521
+ CBUUID* characteristic_uuid = [CBUUID UUIDWithString:uuid];
522
+
523
+ if (service == nil) {
524
+ throw SimpleBLE::Exception::BaseException("Invalid service parameter.");
525
+ }
526
+
527
+ for (CBCharacteristic* characteristic in service.characteristics) {
528
+ if ([characteristic.UUID isEqual:characteristic_uuid]) {
529
+ return characteristic;
530
+ }
531
+ }
532
+
533
+ throw SimpleBLE::Exception::CharacteristicNotFound([uuid UTF8String]);
534
+ }
535
+
536
+ - (CBDescriptor*)findDescriptor:(NSString*)uuid characteristic:(CBCharacteristic*)characteristic {
537
+ CBUUID* descriptor_uuid = [CBUUID UUIDWithString:uuid];
538
+
539
+ for (CBDescriptor* descriptor in characteristic.descriptors) {
540
+ if ([descriptor.UUID isEqual:descriptor_uuid]) {
541
+ return descriptor;
542
+ }
543
+ }
544
+
545
+ throw SimpleBLE::Exception::DescriptorNotFound([uuid UTF8String]);
546
+ }
547
+
548
+ - (std::pair<CBService*, CBCharacteristic*>)findServiceAndCharacteristic:(NSString*)service_uuid
549
+ characteristic_uuid:(NSString*)characteristic_uuid {
550
+ CBService* service = [self findService:service_uuid];
551
+ CBCharacteristic* characteristic = [self findCharacteristic:characteristic_uuid service:service];
552
+ return std::pair<CBService*, CBCharacteristic*>(service, characteristic);
553
+ }
554
+
555
+ - (void)throwBasedOnError:(NSError*)error withFormat:(NSString*)format, ... {
556
+ va_list argList;
557
+ va_start(argList, format);
558
+ NSString* formattedString = [[NSString alloc] initWithFormat:format arguments:argList];
559
+ va_end(argList);
560
+
561
+ if (error == nil) {
562
+ NSString* exceptionMessage = [NSString stringWithFormat:@"%@ failed", formattedString];
563
+ NSLog(@"%@", exceptionMessage);
564
+ throw SimpleBLE::Exception::OperationFailed([exceptionMessage UTF8String]);
565
+ } else {
566
+ NSString* errorMessage = [error localizedDescription];
567
+ NSString* exceptionMessage = [NSString stringWithFormat:@"%@ failed: %@", formattedString, errorMessage];
568
+ NSLog(@"%@", exceptionMessage);
569
+ throw SimpleBLE::Exception::OperationFailed([exceptionMessage UTF8String]);
570
+ }
571
+ }
572
+
573
+ #pragma mark - CBCentralManagerDelegate
574
+
575
+ - (void)delegateDidConnect {
576
+ @synchronized(self) {
577
+ _task.pending = NO;
578
+ }
579
+ }
580
+
581
+ - (void)delegateDidFailToConnect:(NSError*)error {
582
+ @synchronized(self) {
583
+ _task.error = error;
584
+ _task.pending = NO;
585
+ }
586
+ }
587
+
588
+ - (void)delegateDidDisconnect:(NSError*)error {
589
+ @synchronized(self) {
590
+ self->_disconnectionError = error;
591
+ _task.pending = NO;
592
+
593
+ for (CharacteristicExtras* characteristicExtras in self.characteristicExtras.allValues) {
594
+ characteristicExtras.task.pending = NO;
595
+ for (DescriptorExtras* descriptorExtras in characteristicExtras.descriptorExtras.allValues) {
596
+ descriptorExtras.task.pending = NO;
597
+ }
598
+ }
599
+ }
600
+ }
601
+
602
+ #pragma mark - CBPeripheralDelegate
603
+
604
+ - (void)peripheral:(CBPeripheral*)peripheral didModifyServices:(NSArray<CBService*>*)invalidatedServices {
605
+ // NOTE: Whenever this method is called, any pending operations are cancelled. In addition to that,
606
+ // the provided list of services does NOT include any characteristics or descriptors, so need to
607
+ // clear pending flags for those as well.
608
+ @synchronized(self) {
609
+ _task.pending = NO;
610
+
611
+ for (CharacteristicExtras* characteristicExtras in self.characteristicExtras.allValues) {
612
+ characteristicExtras.task.pending = NO;
613
+ for (DescriptorExtras* descriptorExtras in characteristicExtras.descriptorExtras.allValues) {
614
+ descriptorExtras.task.pending = NO;
615
+ }
616
+ }
617
+ }
618
+ }
619
+
620
+ - (void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error {
621
+ @synchronized(self) {
622
+ _task.error = error;
623
+ _task.pending = NO;
624
+ }
625
+ }
626
+
627
+ - (void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(CBService*)service error:(NSError*)error {
628
+ @synchronized(self) {
629
+ _task.error = error;
630
+ _task.pending = NO;
631
+ }
632
+ }
633
+
634
+ - (void)peripheral:(CBPeripheral*)peripheral
635
+ didDiscoverDescriptorsForCharacteristic:(CBCharacteristic*)characteristic
636
+ error:(NSError*)error {
637
+ @synchronized(self) {
638
+ _task.error = error;
639
+ _task.pending = NO;
640
+ }
641
+ }
642
+
643
+ - (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
644
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:uuidToString(characteristic.UUID)];
645
+
646
+ if (characteristic.isNotifying) {
647
+ // If the characteristic is notifying, just save the value and trigger the callback.
648
+ @synchronized(self) {
649
+ characteristicExtras.value = characteristic.value;
650
+ }
651
+
652
+ if (characteristicExtras->valueChangedCallback != nil) {
653
+ characteristicExtras->valueChangedCallback(
654
+ SimpleBLE::ByteArray((const char*)characteristic.value.bytes, characteristic.value.length));
655
+ }
656
+
657
+ } else {
658
+ // If the characteristic is not notifying, then this is a response to a read request.
659
+ BleTask* task = characteristicExtras.task;
660
+ @synchronized(self) {
661
+ task.error = error;
662
+ task.pending = NO;
663
+ }
664
+ }
665
+ }
666
+
667
+ - (void)peripheral:(CBPeripheral*)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
668
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:uuidToString(characteristic.UUID)];
669
+ BleTask* task = characteristicExtras.task;
670
+
671
+ @synchronized(self) {
672
+ task.error = error;
673
+ task.pending = NO;
674
+ }
675
+ }
676
+
677
+ - (void)peripheral:(CBPeripheral*)peripheral
678
+ didUpdateNotificationStateForCharacteristic:(CBCharacteristic*)characteristic
679
+ error:(NSError*)error {
680
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:uuidToString(characteristic.UUID)];
681
+ BleTask* task = characteristicExtras.task;
682
+
683
+ @synchronized(self) {
684
+ task.error = error;
685
+ task.pending = NO;
686
+ }
687
+ }
688
+
689
+ - (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral*)peripheral {
690
+ }
691
+
692
+ - (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForDescriptor:(CBDescriptor*)descriptor error:(NSError*)error {
693
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:uuidToString(descriptor.characteristic.UUID)];
694
+ DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:uuidToString(descriptor.UUID)];
695
+ BleTask* task = descriptorExtras.task;
696
+
697
+ @synchronized(self) {
698
+ task.error = error;
699
+ task.pending = NO;
700
+ }
701
+ }
702
+
703
+ - (void)peripheral:(CBPeripheral*)peripheral didWriteValueForDescriptor:(CBDescriptor*)descriptor error:(NSError*)error {
704
+ CharacteristicExtras* characteristicExtras = [self.characteristicExtras objectForKey:uuidToString(descriptor.characteristic.UUID)];
705
+ DescriptorExtras* descriptorExtras = [characteristicExtras.descriptorExtras objectForKey:uuidToString(descriptor.UUID)];
706
+ BleTask* task = descriptorExtras.task;
707
+
708
+ @synchronized(self) {
709
+ task.error = error;
710
+ task.pending = NO;
711
+ }
712
+ }
713
+
714
+ @end