@microbit/microbit-connection 0.9.0-apps.alpha.16 → 0.9.0-apps.alpha.18

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 (457) hide show
  1. package/build/cjs/async-util.d.ts +3 -1
  2. package/build/cjs/async-util.js +5 -1
  3. package/build/cjs/async-util.js.map +1 -1
  4. package/build/cjs/bluetooth/ble-error.d.ts +10 -0
  5. package/build/cjs/bluetooth/ble-error.js +47 -0
  6. package/build/cjs/bluetooth/ble-error.js.map +1 -0
  7. package/build/cjs/bluetooth/connection.d.ts +137 -0
  8. package/build/cjs/{bluetooth.js → bluetooth/connection.js} +158 -106
  9. package/build/cjs/bluetooth/connection.js.map +1 -0
  10. package/build/cjs/{bluetooth.test.js → bluetooth/connection.test.js} +9 -9
  11. package/build/cjs/bluetooth/connection.test.js.map +1 -0
  12. package/build/cjs/bluetooth/device-bond-state.js.map +1 -0
  13. package/build/cjs/{bluetooth-device-wrapper.d.ts → bluetooth/device-wrapper.d.ts} +11 -11
  14. package/build/cjs/{bluetooth-device-wrapper.js → bluetooth/device-wrapper.js} +38 -42
  15. package/build/cjs/bluetooth/device-wrapper.js.map +1 -0
  16. package/build/cjs/{flashing → bluetooth/flashing}/flashing-full.d.ts +2 -2
  17. package/build/cjs/{flashing → bluetooth/flashing}/flashing-full.js +6 -5
  18. package/build/cjs/bluetooth/flashing/flashing-full.js.map +1 -0
  19. package/build/cjs/{flashing → bluetooth/flashing}/flashing-makecode.d.ts +1 -1
  20. package/build/cjs/bluetooth/flashing/flashing-makecode.js.map +1 -0
  21. package/build/cjs/{flashing → bluetooth/flashing}/flashing-makecode.test.js +3 -3
  22. package/build/cjs/bluetooth/flashing/flashing-makecode.test.js.map +1 -0
  23. package/build/cjs/{flashing → bluetooth/flashing}/flashing-partial.d.ts +2 -2
  24. package/build/cjs/{flashing → bluetooth/flashing}/flashing-partial.js +7 -6
  25. package/build/cjs/bluetooth/flashing/flashing-partial.js.map +1 -0
  26. package/build/cjs/bluetooth/flashing/flashing-v1.js.map +1 -0
  27. package/build/cjs/{flashing → bluetooth/flashing}/nordic-dfu.d.ts +2 -2
  28. package/build/cjs/{flashing → bluetooth/flashing}/nordic-dfu.js +15 -11
  29. package/build/cjs/bluetooth/flashing/nordic-dfu.js.map +1 -0
  30. package/build/cjs/bluetooth/flashing/zip.js.map +1 -0
  31. package/build/cjs/bluetooth/flashing/zip.test.js.map +1 -0
  32. package/build/cjs/bluetooth/index.d.ts +5 -0
  33. package/build/cjs/bluetooth/index.js +9 -0
  34. package/build/cjs/bluetooth/index.js.map +1 -0
  35. package/build/cjs/{bluetooth-profile.js → bluetooth/profile.js} +1 -1
  36. package/build/cjs/bluetooth/profile.js.map +1 -0
  37. package/build/cjs/{accelerometer-service.d.ts → bluetooth/services/accelerometer-service.d.ts} +2 -3
  38. package/build/cjs/{accelerometer-service.js → bluetooth/services/accelerometer-service.js} +17 -12
  39. package/build/cjs/bluetooth/services/accelerometer-service.js.map +1 -0
  40. package/build/{esm → cjs/bluetooth/services}/button-service.d.ts +2 -3
  41. package/build/cjs/{button-service.js → bluetooth/services/button-service.js} +24 -14
  42. package/build/cjs/bluetooth/services/button-service.js.map +1 -0
  43. package/build/{esm → cjs/bluetooth/services}/device-information-service.d.ts +1 -1
  44. package/build/cjs/bluetooth/services/device-information-service.js +29 -0
  45. package/build/cjs/bluetooth/services/device-information-service.js.map +1 -0
  46. package/build/cjs/{dfu-service.js → bluetooth/services/dfu-service.js} +2 -2
  47. package/build/cjs/bluetooth/services/dfu-service.js.map +1 -0
  48. package/build/{esm → cjs/bluetooth/services}/led-service.d.ts +2 -3
  49. package/build/cjs/{led-service.js → bluetooth/services/led-service.js} +10 -7
  50. package/build/cjs/bluetooth/services/led-service.js.map +1 -0
  51. package/build/cjs/{magnetometer-service.d.ts → bluetooth/services/magnetometer-service.d.ts} +2 -3
  52. package/build/cjs/{magnetometer-service.js → bluetooth/services/magnetometer-service.js} +19 -14
  53. package/build/cjs/bluetooth/services/magnetometer-service.js.map +1 -0
  54. package/build/{esm → cjs/bluetooth/services}/partial-flashing-service.d.ts +1 -1
  55. package/build/cjs/{partial-flashing-service.js → bluetooth/services/partial-flashing-service.js} +8 -8
  56. package/build/cjs/bluetooth/services/partial-flashing-service.js.map +1 -0
  57. package/build/cjs/{uart-service.d.ts → bluetooth/services/uart-service.d.ts} +2 -2
  58. package/build/cjs/{uart-service.js → bluetooth/services/uart-service.js} +21 -9
  59. package/build/cjs/bluetooth/services/uart-service.js.map +1 -0
  60. package/build/cjs/board-id.d.ts +1 -1
  61. package/build/cjs/board-id.js.map +1 -1
  62. package/build/cjs/device.d.ts +145 -123
  63. package/build/cjs/device.js +47 -128
  64. package/build/cjs/device.js.map +1 -1
  65. package/build/cjs/events.d.ts +8 -108
  66. package/build/cjs/events.js +27 -88
  67. package/build/cjs/events.js.map +1 -1
  68. package/build/cjs/events.test.js +54 -48
  69. package/build/cjs/events.test.js.map +1 -1
  70. package/build/cjs/hex-util.d.ts +1 -0
  71. package/build/cjs/hex-util.js +15 -0
  72. package/build/cjs/hex-util.js.map +1 -0
  73. package/build/cjs/index.d.ts +7 -17
  74. package/build/cjs/index.js +6 -28
  75. package/build/cjs/index.js.map +1 -1
  76. package/build/cjs/radio-bridge/connection.d.ts +52 -0
  77. package/build/cjs/{usb-radio-bridge.js → radio-bridge/connection.js} +69 -37
  78. package/build/cjs/radio-bridge/connection.js.map +1 -0
  79. package/build/cjs/radio-bridge/index.d.ts +4 -0
  80. package/build/cjs/radio-bridge/index.js +9 -0
  81. package/build/cjs/radio-bridge/index.js.map +1 -0
  82. package/build/cjs/{usb-serial-protocol.js → radio-bridge/serial-protocol.js} +1 -1
  83. package/build/cjs/radio-bridge/serial-protocol.js.map +1 -0
  84. package/build/cjs/service-events.d.ts +43 -14
  85. package/build/cjs/service-events.js +6 -36
  86. package/build/cjs/service-events.js.map +1 -1
  87. package/build/{esm/hex-flash-data-source.d.ts → cjs/universal-hex/flash-data-source.d.ts} +1 -2
  88. package/build/cjs/{hex-flash-data-source.js → universal-hex/flash-data-source.js} +7 -15
  89. package/build/cjs/universal-hex/flash-data-source.js.map +1 -0
  90. package/build/cjs/universal-hex/index.d.ts +4 -0
  91. package/build/cjs/universal-hex/index.js +9 -0
  92. package/build/cjs/universal-hex/index.js.map +1 -0
  93. package/build/cjs/usb/arm-debug.d.ts +137 -0
  94. package/build/cjs/usb/arm-debug.js +414 -0
  95. package/build/cjs/usb/arm-debug.js.map +1 -0
  96. package/build/cjs/usb/arm-debug.test.js +333 -0
  97. package/build/cjs/usb/arm-debug.test.js.map +1 -0
  98. package/build/{esm → cjs/usb}/board-serial-info.d.ts +2 -1
  99. package/build/cjs/{board-serial-info.js → usb/board-serial-info.js} +10 -7
  100. package/build/cjs/usb/board-serial-info.js.map +1 -0
  101. package/build/cjs/{board-serial-info.test.js → usb/board-serial-info.test.js} +2 -4
  102. package/build/cjs/usb/board-serial-info.test.js.map +1 -0
  103. package/build/cjs/usb/cdc-saturation.d.ts +36 -0
  104. package/build/cjs/usb/cdc-saturation.js +151 -0
  105. package/build/cjs/usb/cdc-saturation.js.map +1 -0
  106. package/build/cjs/usb/cmsis-dap.d.ts +113 -0
  107. package/build/cjs/usb/cmsis-dap.js +349 -0
  108. package/build/cjs/usb/cmsis-dap.js.map +1 -0
  109. package/build/cjs/usb/cmsis-dap.test.js +350 -0
  110. package/build/cjs/usb/cmsis-dap.test.js.map +1 -0
  111. package/build/cjs/usb/connection.d.ts +96 -0
  112. package/build/cjs/{usb.js → usb/connection.js} +257 -173
  113. package/build/cjs/usb/connection.js.map +1 -0
  114. package/build/cjs/{usb.test.js → usb/connection.test.js} +52 -42
  115. package/build/cjs/usb/connection.test.js.map +1 -0
  116. package/build/cjs/usb/cortex-m.d.ts +72 -0
  117. package/build/cjs/usb/cortex-m.js +165 -0
  118. package/build/cjs/usb/cortex-m.js.map +1 -0
  119. package/build/cjs/usb/cortex-m.test.js +217 -0
  120. package/build/cjs/usb/cortex-m.test.js.map +1 -0
  121. package/build/cjs/usb/daplink.d.ts +69 -0
  122. package/build/cjs/usb/daplink.js +238 -0
  123. package/build/cjs/usb/daplink.js.map +1 -0
  124. package/build/cjs/usb/daplink.test.js +290 -0
  125. package/build/cjs/usb/daplink.test.js.map +1 -0
  126. package/build/cjs/usb/device-wrapper.d.ts +34 -0
  127. package/build/cjs/usb/device-wrapper.js +102 -0
  128. package/build/cjs/usb/device-wrapper.js.map +1 -0
  129. package/build/cjs/usb/index.d.ts +5 -0
  130. package/build/cjs/usb/index.js +10 -0
  131. package/build/cjs/usb/index.js.map +1 -0
  132. package/build/{esm/usb-partial-flashing-utils.d.ts → cjs/usb/partial-flashing-utils.d.ts} +6 -9
  133. package/build/cjs/{usb-partial-flashing-utils.js → usb/partial-flashing-utils.js} +6 -49
  134. package/build/cjs/usb/partial-flashing-utils.js.map +1 -0
  135. package/build/{esm/usb-partial-flashing.d.ts → cjs/usb/partial-flashing.d.ts} +37 -11
  136. package/build/cjs/{usb-partial-flashing.js → usb/partial-flashing.js} +170 -135
  137. package/build/cjs/usb/partial-flashing.js.map +1 -0
  138. package/build/cjs/usb/promise-queue.js.map +1 -0
  139. package/build/cjs/usb/promise-queue.test.js.map +1 -0
  140. package/build/cjs/usb/serial-events.d.ts +16 -0
  141. package/build/cjs/{led.js → usb/serial-events.js} +1 -1
  142. package/build/cjs/usb/serial-events.js.map +1 -0
  143. package/build/cjs/usb/transport.d.ts +42 -0
  144. package/build/cjs/usb/transport.js +155 -0
  145. package/build/cjs/usb/transport.js.map +1 -0
  146. package/build/esm/async-util.d.ts +3 -1
  147. package/build/esm/async-util.js +5 -1
  148. package/build/esm/async-util.js.map +1 -1
  149. package/build/esm/bluetooth/ble-error.d.ts +10 -0
  150. package/build/esm/bluetooth/ble-error.js +42 -0
  151. package/build/esm/bluetooth/ble-error.js.map +1 -0
  152. package/build/esm/bluetooth/connection.d.ts +137 -0
  153. package/build/esm/{bluetooth.js → bluetooth/connection.js} +152 -100
  154. package/build/esm/bluetooth/connection.js.map +1 -0
  155. package/build/esm/{bluetooth.test.js → bluetooth/connection.test.js} +9 -9
  156. package/build/esm/bluetooth/connection.test.js.map +1 -0
  157. package/build/esm/bluetooth/device-bond-state.js.map +1 -0
  158. package/build/esm/{bluetooth-device-wrapper.d.ts → bluetooth/device-wrapper.d.ts} +11 -11
  159. package/build/esm/{bluetooth-device-wrapper.js → bluetooth/device-wrapper.js} +35 -39
  160. package/build/esm/bluetooth/device-wrapper.js.map +1 -0
  161. package/build/esm/{flashing → bluetooth/flashing}/flashing-full.d.ts +2 -2
  162. package/build/esm/{flashing → bluetooth/flashing}/flashing-full.js +6 -5
  163. package/build/esm/bluetooth/flashing/flashing-full.js.map +1 -0
  164. package/build/esm/{flashing → bluetooth/flashing}/flashing-makecode.d.ts +1 -1
  165. package/build/esm/bluetooth/flashing/flashing-makecode.js.map +1 -0
  166. package/build/esm/{flashing → bluetooth/flashing}/flashing-makecode.test.js +2 -2
  167. package/build/esm/bluetooth/flashing/flashing-makecode.test.js.map +1 -0
  168. package/build/esm/{flashing → bluetooth/flashing}/flashing-partial.d.ts +2 -2
  169. package/build/esm/{flashing → bluetooth/flashing}/flashing-partial.js +6 -5
  170. package/build/esm/bluetooth/flashing/flashing-partial.js.map +1 -0
  171. package/build/esm/bluetooth/flashing/flashing-v1.js.map +1 -0
  172. package/build/esm/{flashing → bluetooth/flashing}/nordic-dfu.d.ts +2 -2
  173. package/build/esm/{flashing → bluetooth/flashing}/nordic-dfu.js +15 -11
  174. package/build/esm/bluetooth/flashing/nordic-dfu.js.map +1 -0
  175. package/build/esm/bluetooth/flashing/zip.js.map +1 -0
  176. package/build/esm/bluetooth/flashing/zip.test.d.ts +1 -0
  177. package/build/esm/bluetooth/flashing/zip.test.js.map +1 -0
  178. package/build/esm/bluetooth/index.d.ts +5 -0
  179. package/build/esm/bluetooth/index.js +5 -0
  180. package/build/esm/bluetooth/index.js.map +1 -0
  181. package/build/esm/{bluetooth-profile.js → bluetooth/profile.js} +1 -1
  182. package/build/{cjs/bluetooth-profile.js.map → esm/bluetooth/profile.js.map} +1 -1
  183. package/build/esm/{accelerometer-service.d.ts → bluetooth/services/accelerometer-service.d.ts} +2 -3
  184. package/build/esm/{accelerometer-service.js → bluetooth/services/accelerometer-service.js} +11 -6
  185. package/build/esm/bluetooth/services/accelerometer-service.js.map +1 -0
  186. package/build/{cjs → esm/bluetooth/services}/button-service.d.ts +2 -3
  187. package/build/esm/{button-service.js → bluetooth/services/button-service.js} +20 -10
  188. package/build/esm/bluetooth/services/button-service.js.map +1 -0
  189. package/build/{cjs → esm/bluetooth/services}/device-information-service.d.ts +1 -1
  190. package/build/esm/bluetooth/services/device-information-service.js +25 -0
  191. package/build/esm/bluetooth/services/device-information-service.js.map +1 -0
  192. package/build/esm/{dfu-service.js → bluetooth/services/dfu-service.js} +1 -1
  193. package/build/esm/bluetooth/services/dfu-service.js.map +1 -0
  194. package/build/{cjs → esm/bluetooth/services}/led-service.d.ts +2 -3
  195. package/build/esm/{led-service.js → bluetooth/services/led-service.js} +4 -1
  196. package/build/esm/bluetooth/services/led-service.js.map +1 -0
  197. package/build/esm/{magnetometer-service.d.ts → bluetooth/services/magnetometer-service.d.ts} +2 -3
  198. package/build/esm/{magnetometer-service.js → bluetooth/services/magnetometer-service.js} +11 -6
  199. package/build/esm/bluetooth/services/magnetometer-service.js.map +1 -0
  200. package/build/{cjs → esm/bluetooth/services}/partial-flashing-service.d.ts +1 -1
  201. package/build/esm/{partial-flashing-service.js → bluetooth/services/partial-flashing-service.js} +4 -4
  202. package/build/esm/bluetooth/services/partial-flashing-service.js.map +1 -0
  203. package/build/esm/{uart-service.d.ts → bluetooth/services/uart-service.d.ts} +2 -2
  204. package/build/esm/{uart-service.js → bluetooth/services/uart-service.js} +18 -6
  205. package/build/esm/bluetooth/services/uart-service.js.map +1 -0
  206. package/build/esm/board-id.d.ts +1 -1
  207. package/build/esm/board-id.js.map +1 -1
  208. package/build/esm/device.d.ts +145 -123
  209. package/build/esm/device.js +45 -122
  210. package/build/esm/device.js.map +1 -1
  211. package/build/esm/events.d.ts +8 -108
  212. package/build/esm/events.js +26 -86
  213. package/build/esm/events.js.map +1 -1
  214. package/build/esm/events.test.js +55 -49
  215. package/build/esm/events.test.js.map +1 -1
  216. package/build/esm/hex-util.d.ts +1 -0
  217. package/build/esm/hex-util.js +11 -0
  218. package/build/esm/hex-util.js.map +1 -0
  219. package/build/esm/index.d.ts +7 -17
  220. package/build/esm/index.js +6 -11
  221. package/build/esm/index.js.map +1 -1
  222. package/build/esm/radio-bridge/connection.d.ts +52 -0
  223. package/build/esm/{usb-radio-bridge.js → radio-bridge/connection.js} +69 -37
  224. package/build/esm/radio-bridge/connection.js.map +1 -0
  225. package/build/esm/radio-bridge/index.d.ts +4 -0
  226. package/build/esm/radio-bridge/index.js +5 -0
  227. package/build/esm/radio-bridge/index.js.map +1 -0
  228. package/build/esm/{usb-serial-protocol.js → radio-bridge/serial-protocol.js} +1 -1
  229. package/build/esm/radio-bridge/serial-protocol.js.map +1 -0
  230. package/build/esm/service-events.d.ts +43 -14
  231. package/build/esm/service-events.js +5 -34
  232. package/build/esm/service-events.js.map +1 -1
  233. package/build/{cjs/hex-flash-data-source.d.ts → esm/universal-hex/flash-data-source.d.ts} +1 -2
  234. package/build/esm/{hex-flash-data-source.js → universal-hex/flash-data-source.js} +6 -13
  235. package/build/esm/universal-hex/flash-data-source.js.map +1 -0
  236. package/build/esm/universal-hex/index.d.ts +4 -0
  237. package/build/esm/universal-hex/index.js +5 -0
  238. package/build/esm/universal-hex/index.js.map +1 -0
  239. package/build/esm/usb/arm-debug.d.ts +137 -0
  240. package/build/esm/usb/arm-debug.js +409 -0
  241. package/build/esm/usb/arm-debug.js.map +1 -0
  242. package/build/esm/usb/arm-debug.test.d.ts +1 -0
  243. package/build/esm/usb/arm-debug.test.js +331 -0
  244. package/build/esm/usb/arm-debug.test.js.map +1 -0
  245. package/build/{cjs → esm/usb}/board-serial-info.d.ts +2 -1
  246. package/build/esm/{board-serial-info.js → usb/board-serial-info.js} +10 -7
  247. package/build/esm/usb/board-serial-info.js.map +1 -0
  248. package/build/esm/usb/board-serial-info.test.d.ts +1 -0
  249. package/build/esm/{board-serial-info.test.js → usb/board-serial-info.test.js} +2 -4
  250. package/build/esm/usb/board-serial-info.test.js.map +1 -0
  251. package/build/esm/usb/cdc-saturation.d.ts +36 -0
  252. package/build/esm/usb/cdc-saturation.js +148 -0
  253. package/build/esm/usb/cdc-saturation.js.map +1 -0
  254. package/build/esm/usb/cmsis-dap.d.ts +113 -0
  255. package/build/esm/usb/cmsis-dap.js +342 -0
  256. package/build/esm/usb/cmsis-dap.js.map +1 -0
  257. package/build/esm/usb/cmsis-dap.test.d.ts +1 -0
  258. package/build/esm/usb/cmsis-dap.test.js +348 -0
  259. package/build/esm/usb/cmsis-dap.test.js.map +1 -0
  260. package/build/esm/usb/connection.d.ts +96 -0
  261. package/build/esm/{usb.js → usb/connection.js} +253 -168
  262. package/build/esm/usb/connection.js.map +1 -0
  263. package/build/esm/usb/connection.test.d.ts +1 -0
  264. package/build/esm/{usb.test.js → usb/connection.test.js} +47 -37
  265. package/build/esm/usb/connection.test.js.map +1 -0
  266. package/build/esm/usb/cortex-m.d.ts +72 -0
  267. package/build/esm/usb/cortex-m.js +161 -0
  268. package/build/esm/usb/cortex-m.js.map +1 -0
  269. package/build/esm/usb/cortex-m.test.d.ts +1 -0
  270. package/build/esm/usb/cortex-m.test.js +215 -0
  271. package/build/esm/usb/cortex-m.test.js.map +1 -0
  272. package/build/esm/usb/daplink.d.ts +69 -0
  273. package/build/esm/usb/daplink.js +231 -0
  274. package/build/esm/usb/daplink.js.map +1 -0
  275. package/build/esm/usb/daplink.test.d.ts +1 -0
  276. package/build/esm/usb/daplink.test.js +288 -0
  277. package/build/esm/usb/daplink.test.js.map +1 -0
  278. package/build/esm/usb/device-wrapper.d.ts +34 -0
  279. package/build/esm/usb/device-wrapper.js +98 -0
  280. package/build/esm/usb/device-wrapper.js.map +1 -0
  281. package/build/esm/usb/index.d.ts +5 -0
  282. package/build/esm/usb/index.js +5 -0
  283. package/build/esm/usb/index.js.map +1 -0
  284. package/build/{cjs/usb-partial-flashing-utils.d.ts → esm/usb/partial-flashing-utils.d.ts} +6 -9
  285. package/build/esm/{usb-partial-flashing-utils.js → usb/partial-flashing-utils.js} +4 -44
  286. package/build/esm/usb/partial-flashing-utils.js.map +1 -0
  287. package/build/{cjs/usb-partial-flashing.d.ts → esm/usb/partial-flashing.d.ts} +37 -11
  288. package/build/esm/{usb-partial-flashing.js → usb/partial-flashing.js} +169 -111
  289. package/build/esm/usb/partial-flashing.js.map +1 -0
  290. package/build/esm/usb/promise-queue.js.map +1 -0
  291. package/build/esm/usb/promise-queue.test.d.ts +1 -0
  292. package/build/esm/usb/promise-queue.test.js.map +1 -0
  293. package/build/esm/usb/serial-events.d.ts +16 -0
  294. package/build/esm/usb/serial-events.js +2 -0
  295. package/build/esm/usb/serial-events.js.map +1 -0
  296. package/build/esm/usb/transport.d.ts +42 -0
  297. package/build/esm/usb/transport.js +151 -0
  298. package/build/esm/usb/transport.js.map +1 -0
  299. package/package.json +43 -2
  300. package/build/cjs/accelerometer-service.js.map +0 -1
  301. package/build/cjs/accelerometer.d.ts +0 -9
  302. package/build/cjs/accelerometer.js +0 -16
  303. package/build/cjs/accelerometer.js.map +0 -1
  304. package/build/cjs/bluetooth-device-wrapper.js.map +0 -1
  305. package/build/cjs/bluetooth.d.ts +0 -114
  306. package/build/cjs/bluetooth.js.map +0 -1
  307. package/build/cjs/bluetooth.test.js.map +0 -1
  308. package/build/cjs/board-serial-info.js.map +0 -1
  309. package/build/cjs/board-serial-info.test.js.map +0 -1
  310. package/build/cjs/button-service.js.map +0 -1
  311. package/build/cjs/buttons.d.ts +0 -10
  312. package/build/cjs/buttons.js +0 -22
  313. package/build/cjs/buttons.js.map +0 -1
  314. package/build/cjs/constants.d.ts +0 -48
  315. package/build/cjs/constants.js +0 -73
  316. package/build/cjs/constants.js.map +0 -1
  317. package/build/cjs/device-bond-state.js.map +0 -1
  318. package/build/cjs/device-information-service.js +0 -34
  319. package/build/cjs/device-information-service.js.map +0 -1
  320. package/build/cjs/dfu-service.js.map +0 -1
  321. package/build/cjs/flashing/flashing-full.js.map +0 -1
  322. package/build/cjs/flashing/flashing-makecode.js.map +0 -1
  323. package/build/cjs/flashing/flashing-makecode.test.js.map +0 -1
  324. package/build/cjs/flashing/flashing-partial.js.map +0 -1
  325. package/build/cjs/flashing/flashing-v1.js.map +0 -1
  326. package/build/cjs/flashing/nordic-dfu.js.map +0 -1
  327. package/build/cjs/flashing/zip.js.map +0 -1
  328. package/build/cjs/flashing/zip.test.js.map +0 -1
  329. package/build/cjs/hex-flash-data-source.js.map +0 -1
  330. package/build/cjs/led-service.js.map +0 -1
  331. package/build/cjs/led.d.ts +0 -6
  332. package/build/cjs/led.js.map +0 -1
  333. package/build/cjs/magnetometer-service.js.map +0 -1
  334. package/build/cjs/magnetometer.d.ts +0 -9
  335. package/build/cjs/magnetometer.js +0 -16
  336. package/build/cjs/magnetometer.js.map +0 -1
  337. package/build/cjs/partial-flashing-service.js.map +0 -1
  338. package/build/cjs/promise-queue.js.map +0 -1
  339. package/build/cjs/promise-queue.test.js.map +0 -1
  340. package/build/cjs/serial-events.d.ts +0 -20
  341. package/build/cjs/serial-events.js +0 -69
  342. package/build/cjs/serial-events.js.map +0 -1
  343. package/build/cjs/uart-service.js.map +0 -1
  344. package/build/cjs/uart.d.ts +0 -4
  345. package/build/cjs/uart.js +0 -16
  346. package/build/cjs/uart.js.map +0 -1
  347. package/build/cjs/usb-device-wrapper.d.ts +0 -47
  348. package/build/cjs/usb-device-wrapper.js +0 -437
  349. package/build/cjs/usb-device-wrapper.js.map +0 -1
  350. package/build/cjs/usb-partial-flashing-utils.js.map +0 -1
  351. package/build/cjs/usb-partial-flashing.js.map +0 -1
  352. package/build/cjs/usb-radio-bridge.d.ts +0 -24
  353. package/build/cjs/usb-radio-bridge.js.map +0 -1
  354. package/build/cjs/usb-serial-protocol.js.map +0 -1
  355. package/build/cjs/usb.d.ts +0 -66
  356. package/build/cjs/usb.js.map +0 -1
  357. package/build/cjs/usb.test.js.map +0 -1
  358. package/build/esm/accelerometer-service.js.map +0 -1
  359. package/build/esm/accelerometer.d.ts +0 -9
  360. package/build/esm/accelerometer.js +0 -12
  361. package/build/esm/accelerometer.js.map +0 -1
  362. package/build/esm/bluetooth-device-wrapper.js.map +0 -1
  363. package/build/esm/bluetooth-profile.js.map +0 -1
  364. package/build/esm/bluetooth.d.ts +0 -114
  365. package/build/esm/bluetooth.js.map +0 -1
  366. package/build/esm/bluetooth.test.js.map +0 -1
  367. package/build/esm/board-serial-info.js.map +0 -1
  368. package/build/esm/board-serial-info.test.js.map +0 -1
  369. package/build/esm/button-service.js.map +0 -1
  370. package/build/esm/buttons.d.ts +0 -10
  371. package/build/esm/buttons.js +0 -18
  372. package/build/esm/buttons.js.map +0 -1
  373. package/build/esm/constants.d.ts +0 -48
  374. package/build/esm/constants.js +0 -70
  375. package/build/esm/constants.js.map +0 -1
  376. package/build/esm/device-bond-state.js.map +0 -1
  377. package/build/esm/device-information-service.js +0 -30
  378. package/build/esm/device-information-service.js.map +0 -1
  379. package/build/esm/dfu-service.js.map +0 -1
  380. package/build/esm/flashing/flashing-full.js.map +0 -1
  381. package/build/esm/flashing/flashing-makecode.js.map +0 -1
  382. package/build/esm/flashing/flashing-makecode.test.js.map +0 -1
  383. package/build/esm/flashing/flashing-partial.js.map +0 -1
  384. package/build/esm/flashing/flashing-v1.js.map +0 -1
  385. package/build/esm/flashing/nordic-dfu.js.map +0 -1
  386. package/build/esm/flashing/zip.js.map +0 -1
  387. package/build/esm/flashing/zip.test.js.map +0 -1
  388. package/build/esm/hex-flash-data-source.js.map +0 -1
  389. package/build/esm/led-service.js.map +0 -1
  390. package/build/esm/led.d.ts +0 -6
  391. package/build/esm/led.js +0 -2
  392. package/build/esm/led.js.map +0 -1
  393. package/build/esm/magnetometer-service.js.map +0 -1
  394. package/build/esm/magnetometer.d.ts +0 -9
  395. package/build/esm/magnetometer.js +0 -12
  396. package/build/esm/magnetometer.js.map +0 -1
  397. package/build/esm/partial-flashing-service.js.map +0 -1
  398. package/build/esm/promise-queue.js.map +0 -1
  399. package/build/esm/promise-queue.test.js.map +0 -1
  400. package/build/esm/serial-events.d.ts +0 -20
  401. package/build/esm/serial-events.js +0 -61
  402. package/build/esm/serial-events.js.map +0 -1
  403. package/build/esm/uart-service.js.map +0 -1
  404. package/build/esm/uart.d.ts +0 -4
  405. package/build/esm/uart.js +0 -12
  406. package/build/esm/uart.js.map +0 -1
  407. package/build/esm/usb-device-wrapper.d.ts +0 -47
  408. package/build/esm/usb-device-wrapper.js +0 -410
  409. package/build/esm/usb-device-wrapper.js.map +0 -1
  410. package/build/esm/usb-partial-flashing-utils.js.map +0 -1
  411. package/build/esm/usb-partial-flashing.js.map +0 -1
  412. package/build/esm/usb-radio-bridge.d.ts +0 -24
  413. package/build/esm/usb-radio-bridge.js.map +0 -1
  414. package/build/esm/usb-serial-protocol.js.map +0 -1
  415. package/build/esm/usb.d.ts +0 -66
  416. package/build/esm/usb.js.map +0 -1
  417. package/build/esm/usb.test.js.map +0 -1
  418. /package/build/cjs/{bluetooth.test.d.ts → bluetooth/connection.test.d.ts} +0 -0
  419. /package/build/cjs/{device-bond-state.d.ts → bluetooth/device-bond-state.d.ts} +0 -0
  420. /package/build/cjs/{device-bond-state.js → bluetooth/device-bond-state.js} +0 -0
  421. /package/build/cjs/{flashing → bluetooth/flashing}/flashing-makecode.js +0 -0
  422. /package/build/cjs/{flashing → bluetooth/flashing}/flashing-makecode.test.d.ts +0 -0
  423. /package/build/cjs/{flashing → bluetooth/flashing}/flashing-v1.d.ts +0 -0
  424. /package/build/cjs/{flashing → bluetooth/flashing}/flashing-v1.js +0 -0
  425. /package/build/cjs/{flashing → bluetooth/flashing}/zip.d.ts +0 -0
  426. /package/build/cjs/{flashing → bluetooth/flashing}/zip.js +0 -0
  427. /package/build/cjs/{flashing → bluetooth/flashing}/zip.test.d.ts +0 -0
  428. /package/build/cjs/{flashing → bluetooth/flashing}/zip.test.js +0 -0
  429. /package/build/cjs/{bluetooth-profile.d.ts → bluetooth/profile.d.ts} +0 -0
  430. /package/build/cjs/{dfu-service.d.ts → bluetooth/services/dfu-service.d.ts} +0 -0
  431. /package/build/cjs/{usb-serial-protocol.d.ts → radio-bridge/serial-protocol.d.ts} +0 -0
  432. /package/build/cjs/{board-serial-info.test.d.ts → usb/arm-debug.test.d.ts} +0 -0
  433. /package/build/{esm → cjs/usb}/board-serial-info.test.d.ts +0 -0
  434. /package/build/cjs/{promise-queue.test.d.ts → usb/cmsis-dap.test.d.ts} +0 -0
  435. /package/build/cjs/{usb.test.d.ts → usb/connection.test.d.ts} +0 -0
  436. /package/build/{esm/bluetooth.test.d.ts → cjs/usb/cortex-m.test.d.ts} +0 -0
  437. /package/build/{esm/flashing/flashing-makecode.test.d.ts → cjs/usb/daplink.test.d.ts} +0 -0
  438. /package/build/cjs/{promise-queue.d.ts → usb/promise-queue.d.ts} +0 -0
  439. /package/build/cjs/{promise-queue.js → usb/promise-queue.js} +0 -0
  440. /package/build/{esm → cjs/usb}/promise-queue.test.d.ts +0 -0
  441. /package/build/cjs/{promise-queue.test.js → usb/promise-queue.test.js} +0 -0
  442. /package/build/esm/{flashing/zip.test.d.ts → bluetooth/connection.test.d.ts} +0 -0
  443. /package/build/esm/{device-bond-state.d.ts → bluetooth/device-bond-state.d.ts} +0 -0
  444. /package/build/esm/{device-bond-state.js → bluetooth/device-bond-state.js} +0 -0
  445. /package/build/esm/{flashing → bluetooth/flashing}/flashing-makecode.js +0 -0
  446. /package/build/esm/{usb.test.d.ts → bluetooth/flashing/flashing-makecode.test.d.ts} +0 -0
  447. /package/build/esm/{flashing → bluetooth/flashing}/flashing-v1.d.ts +0 -0
  448. /package/build/esm/{flashing → bluetooth/flashing}/flashing-v1.js +0 -0
  449. /package/build/esm/{flashing → bluetooth/flashing}/zip.d.ts +0 -0
  450. /package/build/esm/{flashing → bluetooth/flashing}/zip.js +0 -0
  451. /package/build/esm/{flashing → bluetooth/flashing}/zip.test.js +0 -0
  452. /package/build/esm/{bluetooth-profile.d.ts → bluetooth/profile.d.ts} +0 -0
  453. /package/build/esm/{dfu-service.d.ts → bluetooth/services/dfu-service.d.ts} +0 -0
  454. /package/build/esm/{usb-serial-protocol.d.ts → radio-bridge/serial-protocol.d.ts} +0 -0
  455. /package/build/esm/{promise-queue.d.ts → usb/promise-queue.d.ts} +0 -0
  456. /package/build/esm/{promise-queue.js → usb/promise-queue.js} +0 -0
  457. /package/build/esm/{promise-queue.test.js → usb/promise-queue.test.js} +0 -0
@@ -3,50 +3,49 @@
3
3
  *
4
4
  * SPDX-License-Identifier: MIT
5
5
  */
6
- import { TimeoutError, withTimeout } from "./async-util.js";
7
- import { throwIfUnavailable } from "./availability.js";
8
- import { AfterRequestDevice, BeforeRequestDevice, ConnectionStatus, ConnectionStatusEvent, DeviceError, FlashDataError, ProgressStage, } from "./device.js";
9
- import { TypedEventTarget } from "./events.js";
10
- import { ConsoleLogging } from "./logging.js";
6
+ import { withTimeout } from "../async-util.js";
7
+ import { throwIfUnavailable } from "../availability.js";
8
+ import { ConnectionStatus, DeviceError, FlashDataError, ProgressStage, assertConnected, } from "../device.js";
9
+ import { TypedEventTarget } from "../events.js";
10
+ import { ConsoleLogging } from "../logging.js";
11
11
  import { PromiseQueue } from "./promise-queue.js";
12
- import { FlashEvent, SerialDataEvent, SerialErrorEvent, SerialResetEvent, } from "./serial-events.js";
13
- import { DAPWrapper } from "./usb-device-wrapper.js";
14
- import { PartialFlashing } from "./usb-partial-flashing.js";
15
- // Temporary workaround for ChromeOS 105 bug.
16
- // See https://bugs.chromium.org/p/chromium/issues/detail?id=1363712&q=usb&can=2
17
- export const isChromeOS105 = () => {
18
- const userAgent = navigator.userAgent;
19
- return /CrOS/.test(userAgent) && /Chrome\/105\b/.test(userAgent);
20
- };
12
+ import { USBDeviceWrapper, } from "./device-wrapper.js";
13
+ import { PartialFlashing } from "./partial-flashing.js";
14
+ import { saturateCdcPipeline } from "./cdc-saturation.js";
21
15
  const defaultFilters = [{ vendorId: 0x0d28, productId: 0x0204 }];
22
- export var DeviceSelectionMode;
23
- (function (DeviceSelectionMode) {
16
+ export const DeviceSelectionMode = {
24
17
  /**
25
18
  * Attempts to connect to known device, otherwise asks which device to
26
19
  * connect to.
27
20
  */
28
- DeviceSelectionMode["AlwaysAsk"] = "AlwaysAsk";
21
+ AlwaysAsk: "AlwaysAsk",
29
22
  /**
30
23
  * Attempts to connect to known device, otherwise attempts to connect to any
31
24
  * allowed devices. If that fails, asks which device to connect to.
32
25
  */
33
- DeviceSelectionMode["UseAnyAllowed"] = "UseAnyAllowed";
34
- })(DeviceSelectionMode || (DeviceSelectionMode = {}));
26
+ UseAnyAllowed: "UseAnyAllowed",
27
+ };
35
28
  /**
36
29
  * A WebUSB connection factory.
37
30
  */
38
- export const createWebUSBConnection = (options) => new MicrobitWebUSBConnectionImpl(options);
31
+ export const createUSBConnection = (options) => new MicrobitUSBConnectionImpl(options);
39
32
  /**
40
33
  * A WebUSB connection to a micro:bit device.
41
34
  */
42
- class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
35
+ class MicrobitUSBConnectionImpl extends TypedEventTarget {
43
36
  constructor(options = {}) {
44
37
  super();
38
+ Object.defineProperty(this, "type", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: "usb"
43
+ });
45
44
  Object.defineProperty(this, "status", {
46
45
  enumerable: true,
47
46
  configurable: true,
48
47
  writable: true,
49
- value: ConnectionStatus.NO_AUTHORIZED_DEVICE
48
+ value: ConnectionStatus.NoAuthorizedDevice
50
49
  });
51
50
  Object.defineProperty(this, "exclusionFilters", {
52
51
  enumerable: true,
@@ -55,9 +54,18 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
55
54
  value: void 0
56
55
  });
57
56
  /**
58
- * The USB device we last connected to.
57
+ * The raw USB device we last connected to.
59
58
  * Cleared if it is disconnected.
60
59
  */
60
+ Object.defineProperty(this, "usbDevice", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ /**
67
+ * Device-specific state. Created on connect, cleared on disconnect.
68
+ */
61
69
  Object.defineProperty(this, "device", {
62
70
  enumerable: true,
63
71
  configurable: true,
@@ -65,14 +73,37 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
65
73
  value: void 0
66
74
  });
67
75
  /**
68
- * The connection to the device.
76
+ * Cached device properties that persist across reconnections until clearDevice.
69
77
  */
70
- Object.defineProperty(this, "connection", {
78
+ Object.defineProperty(this, "cachedConnectionInfo", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: void 0
83
+ });
84
+ Object.defineProperty(this, "loggedBoardSerialInfo", {
71
85
  enumerable: true,
72
86
  configurable: true,
73
87
  writable: true,
74
88
  value: void 0
75
89
  });
90
+ /**
91
+ * Whether CDC pipeline saturation has been performed for this physical
92
+ * USB connection. Once saturated, the buffers stay full until the device
93
+ * is physically disconnected.
94
+ */
95
+ Object.defineProperty(this, "cdcSaturated", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: false
100
+ });
101
+ /**
102
+ * Whether the serial read loop is running.
103
+ *
104
+ * This is false even if we have serial listeners when we're disconnected or flashing.
105
+ * The serial reads interfere with the flash process.
106
+ */
76
107
  Object.defineProperty(this, "serialState", {
77
108
  enumerable: true,
78
109
  configurable: true,
@@ -90,7 +121,7 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
90
121
  configurable: true,
91
122
  writable: true,
92
123
  value: (data) => {
93
- this.dispatchTypedEvent("serialdata", new SerialDataEvent(data));
124
+ this.dispatchEvent("serialdata", { data });
94
125
  }
95
126
  });
96
127
  Object.defineProperty(this, "flashing", {
@@ -113,19 +144,20 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
113
144
  if (document.visibilityState === "visible") {
114
145
  // We may not have actually paused when we became hidden due to an in-progress flash.
115
146
  this.pauseAfterFlash = false;
116
- if (this.status === ConnectionStatus.PAUSED) {
147
+ if (this.status === ConnectionStatus.Paused) {
117
148
  if (!this.flashing) {
118
149
  this.log("Reconnecting visible tab");
119
- this.connect();
150
+ // withEnrichedErrors already disconnects and logs on failure.
151
+ this.connect().catch(() => { });
120
152
  }
121
153
  }
122
154
  }
123
155
  else {
124
- if (!this.unloading && this.status === ConnectionStatus.CONNECTED) {
156
+ if (!this.unloading && this.status === ConnectionStatus.Connected) {
125
157
  if (!this.flashing) {
126
158
  this.log("Pausing connection for hidden tab");
127
159
  // Transition to PAUSED not DISCONNECTED
128
- this.disconnect(false, ConnectionStatus.PAUSED);
160
+ this.disconnect(false, ConnectionStatus.Paused);
129
161
  }
130
162
  else {
131
163
  this.log("Scheduling disconnect of hidden tab for after flash");
@@ -156,9 +188,9 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
156
188
  window.addEventListener("focus", () => {
157
189
  const assumePageIsStayingOpenDelay = 1000;
158
190
  setTimeout(() => {
159
- if (this.status === ConnectionStatus.CONNECTED) {
191
+ if (this.status === ConnectionStatus.Connected) {
160
192
  this.unloading = false;
161
- if (this.addedListeners.serialdata) {
193
+ if (this.hasSerialEventListeners()) {
162
194
  this.startSerialInternal();
163
195
  }
164
196
  }
@@ -178,29 +210,29 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
178
210
  writable: true,
179
211
  value: void 0
180
212
  });
181
- Object.defineProperty(this, "addedListeners", {
213
+ Object.defineProperty(this, "pauseOnHidden", {
182
214
  enumerable: true,
183
215
  configurable: true,
184
216
  writable: true,
185
- value: {
186
- serialdata: 0,
187
- }
217
+ value: void 0
188
218
  });
189
219
  Object.defineProperty(this, "handleDisconnect", {
190
220
  enumerable: true,
191
221
  configurable: true,
192
222
  writable: true,
193
223
  value: (event) => {
194
- if (event.device === this.device) {
195
- this.connection = undefined;
224
+ if (event.device === this.usbDevice) {
225
+ this.cdcSaturated = false;
196
226
  this.device = undefined;
197
- this.setStatus(ConnectionStatus.NO_AUTHORIZED_DEVICE);
227
+ this.usbDevice = undefined;
228
+ this.setStatus(ConnectionStatus.NoAuthorizedDevice);
198
229
  }
199
230
  }
200
231
  });
201
232
  this.logging = options.logging || new ConsoleLogging();
202
233
  this.deviceSelectionMode =
203
234
  options.deviceSelectionMode || DeviceSelectionMode.AlwaysAsk;
235
+ this.pauseOnHidden = options.pauseOnHidden ?? true;
204
236
  }
205
237
  log(v) {
206
238
  this.logging.log(v);
@@ -211,13 +243,13 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
211
243
  }
212
244
  if (typeof window !== "undefined") {
213
245
  window.addEventListener("beforeunload", this.beforeUnloadListener);
214
- if (window.document) {
246
+ if (this.pauseOnHidden && window.document) {
215
247
  window.document.addEventListener("visibilitychange", this.visibilityChangeListener);
216
248
  }
217
249
  }
218
250
  }
219
251
  async checkAvailability() {
220
- if (!navigator.usb || isChromeOS105()) {
252
+ if (!navigator.usb) {
221
253
  return "unsupported";
222
254
  }
223
255
  return "available";
@@ -228,7 +260,7 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
228
260
  }
229
261
  if (typeof window !== "undefined") {
230
262
  window.removeEventListener("beforeunload", this.beforeUnloadListener);
231
- if (window.document) {
263
+ if (this.pauseOnHidden && window.document) {
232
264
  window.document.removeEventListener("visibilitychange", this.visibilityChangeListener);
233
265
  }
234
266
  }
@@ -243,20 +275,22 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
243
275
  });
244
276
  }
245
277
  getDeviceId() {
246
- return this.connection?.deviceId;
278
+ assertConnected(this.cachedConnectionInfo);
279
+ return this.cachedConnectionInfo.deviceId;
247
280
  }
248
281
  getDevice() {
249
- return this.device;
282
+ return this.usbDevice;
250
283
  }
251
284
  getBoardVersion() {
252
- return this.connection?.boardSerialInfo?.id.toBoardVersion();
285
+ assertConnected(this.cachedConnectionInfo);
286
+ return this.cachedConnectionInfo.boardSerialInfo.id.toBoardVersion();
253
287
  }
254
288
  async flash(dataSource, options) {
255
289
  this.flashing = true;
256
290
  try {
257
291
  const startTime = new Date().getTime();
258
292
  await this.withEnrichedErrors(() => this.flashInternal(dataSource, options));
259
- this.dispatchTypedEvent("flash", new FlashEvent());
293
+ this.dispatchEvent("flash");
260
294
  const flashTime = new Date().getTime() - startTime;
261
295
  this.logging.event({
262
296
  type: "WebUSB-time",
@@ -277,16 +311,41 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
277
311
  await this.stopSerialInternal();
278
312
  this.log("Reconnecting before flash");
279
313
  await this.connectInternal(progress);
280
- if (!this.connection) {
314
+ if (!this.device) {
281
315
  throw new DeviceError({
282
- code: "device-disconnected",
316
+ code: "connection-error",
283
317
  message: "Must be connected now",
284
318
  });
285
319
  }
286
- const boardId = this.connection.boardSerialInfo.id;
287
- const boardVersion = boardId.toBoardVersion();
320
+ // Saturate DAPLink's CDC buffers on the first flash after physical
321
+ // connection, but only if someone is listening for serial data.
322
+ // Once saturated, the buffers stay full until physical disconnect.
323
+ if (!this.cdcSaturated && this.hasSerialEventListeners()) {
324
+ // Ensure DAPLink's UART is at 115200 before saturation.
325
+ // V1 DAPLink defaults to 9600 on fresh connection.
326
+ // setBaudrate may reset the target, so reinit SWD afterwards.
327
+ const currentBaud = await this.device.serial.getBaudrate();
328
+ if (currentBaud !== 115200) {
329
+ this.log(`Setting DAPLink baud from ${currentBaud} to 115200`);
330
+ await this.device.serial.setBaudrate(115200);
331
+ await this.device.adi.reinit();
332
+ }
333
+ this.log("Halting target");
334
+ await this.device.cortexM.halt();
335
+ const boardVersion = this.cachedConnectionInfo.boardSerialInfo.id.toBoardVersion();
336
+ await saturateCdcPipeline(this.device, boardVersion, this.logging);
337
+ this.cdcSaturated = true;
338
+ await this.device.serial.drain();
339
+ }
340
+ // Halt unconditionally — flash needs the target stopped.
341
+ if (!(await this.device.cortexM.isHalted())) {
342
+ this.log("Halting target");
343
+ await this.device.cortexM.halt();
344
+ }
345
+ const { boardSerialInfo, pageSize, numPages } = this.cachedConnectionInfo;
346
+ const boardVersion = boardSerialInfo.id.toBoardVersion();
288
347
  const data = await dataSource(boardVersion);
289
- const flashing = new PartialFlashing(this.connection, this.logging, boardVersion);
348
+ const flashing = new PartialFlashing(this.device, this.logging, boardVersion, pageSize, numPages);
290
349
  let wasPartial = false;
291
350
  try {
292
351
  if (partial) {
@@ -301,14 +360,27 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
301
360
  if (this.pauseAfterFlash) {
302
361
  this.log("Disconnecting after flash due to tab visibility");
303
362
  this.pauseAfterFlash = false;
304
- await this.disconnect(false, ConnectionStatus.PAUSED);
363
+ await this.disconnect(false, ConnectionStatus.Paused);
305
364
  }
306
365
  else {
307
- if (this.addedListeners.serialdata) {
366
+ await this.device.adi.reinit();
367
+ // Start serial before resetting so we capture startup output.
368
+ // For full flash FLASH_CLOSE already reset the target, so its
369
+ // early output accumulates in DAPLink's 512-byte serial ring
370
+ // buffer until the first read here.
371
+ if (this.hasSerialEventListeners()) {
308
372
  this.log("Reinstating serial after flash");
309
- if (this.connection.daplink) {
310
- await this.connection.daplink.connect();
311
- await this.startSerialInternal();
373
+ await this.startSerialInternal();
374
+ }
375
+ if (wasPartial) {
376
+ // Partial flash writes pages via SWD without resetting.
377
+ // Full flash already resets via FLASH_CLOSE.
378
+ this.log("Resetting micro:bit to run new program");
379
+ try {
380
+ await this.device.cortexM.reset();
381
+ }
382
+ catch (e) {
383
+ // Allow errors on resetting, user can always manually reset if necessary.
312
384
  }
313
385
  }
314
386
  }
@@ -316,38 +388,51 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
316
388
  }
317
389
  async startSerialInternal() {
318
390
  return this.serialStateChangeQueue.add(async () => {
319
- if (!this.connection || this.serialState) {
391
+ if (!this.device || this.serialState) {
320
392
  return;
321
393
  }
322
394
  this.log("Starting serial");
323
395
  this.serialState = true;
324
- this.connection
325
- .startSerial(this.serialListener)
396
+ const serial = this.device.serial;
397
+ const currentBaud = await serial.getBaudrate();
398
+ if (currentBaud !== 115200) {
399
+ // Changing the baud rate causes a micro:bit reset, so only do it if necessary
400
+ await serial.setBaudrate(115200);
401
+ }
402
+ serial
403
+ .startPolling(this.serialListener, 1)
326
404
  .then(() => {
327
405
  this.log("Finished listening for serial data");
328
406
  })
329
407
  .catch((e) => {
330
- this.dispatchTypedEvent("serialerror", new SerialErrorEvent(e));
408
+ this.dispatchEvent("backgrounderror", {
409
+ error: enrichedError(e),
410
+ event: "serialdata",
411
+ });
331
412
  })
332
413
  .finally(() => {
333
414
  this.serialState = false;
415
+ // Don't reset when paused — the same program is still running and
416
+ // serial will resume when the tab becomes visible again.
417
+ if (this.status !== ConnectionStatus.Paused) {
418
+ this.dispatchEvent("serialreset");
419
+ }
334
420
  });
335
421
  });
336
422
  }
337
423
  async stopSerialInternal() {
338
424
  return this.serialStateChangeQueue.add(async () => {
339
- if (!this.connection || !this.serialState) {
425
+ if (!this.device || !this.serialState) {
340
426
  return;
341
427
  }
342
- this.connection.stopSerial(this.serialListener);
343
- this.dispatchTypedEvent("serialreset", new SerialResetEvent());
428
+ this.device.serial.stopPolling();
344
429
  });
345
430
  }
346
- async disconnect(quiet, finalStatus = ConnectionStatus.DISCONNECTED) {
431
+ async disconnect(quiet, finalStatus = ConnectionStatus.Disconnected) {
347
432
  try {
348
- if (this.connection) {
433
+ if (this.device) {
349
434
  await this.stopSerialInternal();
350
- await this.connection.disconnectAsync();
435
+ await this.device.disconnect();
351
436
  }
352
437
  }
353
438
  catch (e) {
@@ -360,7 +445,7 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
360
445
  }
361
446
  }
362
447
  finally {
363
- this.connection = undefined;
448
+ this.device = undefined;
364
449
  this.setStatus(finalStatus);
365
450
  if (!quiet) {
366
451
  this.logging.log("Disconnection complete");
@@ -375,7 +460,27 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
375
460
  const previousStatus = this.status;
376
461
  this.status = newStatus;
377
462
  this.log("USB connection status " + newStatus);
378
- this.dispatchTypedEvent("status", new ConnectionStatusEvent(newStatus, previousStatus));
463
+ this.dispatchEvent("status", {
464
+ status: newStatus,
465
+ previousStatus,
466
+ });
467
+ }
468
+ logBoardInfo(info) {
469
+ this.logging.event({
470
+ type: "WebUSB-info",
471
+ message: "connected",
472
+ });
473
+ if (!this.loggedBoardSerialInfo || !this.loggedBoardSerialInfo.eq(info)) {
474
+ this.loggedBoardSerialInfo = info;
475
+ this.logging.event({
476
+ type: "WebUSB-info",
477
+ message: "board-id/" + info.id,
478
+ });
479
+ this.logging.event({
480
+ type: "WebUSB-info",
481
+ message: "board-family-hic/" + info.familyId + info.hic,
482
+ });
483
+ }
379
484
  }
380
485
  async withEnrichedErrors(f) {
381
486
  try {
@@ -409,70 +514,75 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
409
514
  }
410
515
  }
411
516
  serialWrite(data) {
517
+ assertConnected(this.device);
518
+ const device = this.device;
412
519
  return this.withEnrichedErrors(async () => {
413
- if (this.connection) {
414
- // Using WebUSB/DAPJs we're limited to 64 byte packet size with a two byte header.
415
- // https://github.com/microbit-foundation/python-editor-v3/issues/215
416
- const maxSerialWrite = 62;
417
- let start = 0;
418
- while (start < data.length) {
419
- const end = Math.min(start + maxSerialWrite, data.length);
420
- const chunkData = data.slice(start, end);
421
- await this.connection.daplink.serialWrite(chunkData);
422
- start = end;
423
- }
520
+ // WebUSB packets are 64 bytes with a two byte header.
521
+ // https://github.com/microbit-foundation/python-editor-v3/issues/215
522
+ const maxSerialWrite = 62;
523
+ let start = 0;
524
+ while (start < data.length) {
525
+ const end = Math.min(start + maxSerialWrite, data.length);
526
+ const chunkData = data.slice(start, end);
527
+ await device.serial.write(chunkData);
528
+ start = end;
424
529
  }
425
530
  });
426
531
  }
427
532
  async softwareReset() {
428
- return this.serialStateChangeQueue.add(async () => await this.connection?.softwareReset());
533
+ assertConnected(this.device);
534
+ const device = this.device;
535
+ return this.serialStateChangeQueue.add(async () => await device.cortexM.softwareReset());
429
536
  }
430
537
  async clearDevice() {
431
538
  await this.disconnect();
432
- this.device = undefined;
433
- this.setStatus(ConnectionStatus.NO_AUTHORIZED_DEVICE);
539
+ this.usbDevice = undefined;
540
+ this.cachedConnectionInfo = undefined;
541
+ this.cdcSaturated = false;
542
+ this.setStatus(ConnectionStatus.NoAuthorizedDevice);
434
543
  }
435
544
  async connectInternal(progress) {
436
545
  const reportProgress = progress ?? (() => { });
437
546
  reportProgress(ProgressStage.Initializing);
438
547
  throwIfUnavailable(await this.checkAvailability());
439
- if (!this.connection && this.device) {
548
+ if (!this.device && this.usbDevice) {
440
549
  reportProgress(ProgressStage.Connecting);
441
- this.connection = new DAPWrapper(this.device, this.logging);
442
- await withTimeout(this.connection.reconnectAsync(), 10_000);
550
+ this.device = new USBDeviceWrapper(this.usbDevice, this.logging);
551
+ this.cachedConnectionInfo = await withTimeout(this.device.reconnect(), 10_000);
443
552
  }
444
- else if (!this.connection) {
553
+ else if (!this.device) {
445
554
  await this.connectWithOtherDevice(reportProgress);
446
555
  }
447
556
  else {
448
557
  reportProgress(ProgressStage.Connecting);
449
- await withTimeout(this.connection.reconnectAsync(), 10_000);
558
+ this.cachedConnectionInfo = await withTimeout(this.device.reconnect(), 10_000);
450
559
  }
451
- if (this.addedListeners.serialdata && !this.flashing) {
560
+ this.logBoardInfo(this.cachedConnectionInfo.boardSerialInfo);
561
+ if (this.hasSerialEventListeners() && !this.flashing) {
452
562
  this.startSerialInternal();
453
563
  }
454
- this.setStatus(ConnectionStatus.CONNECTED);
564
+ this.setStatus(ConnectionStatus.Connected);
455
565
  }
456
566
  async connectWithOtherDevice(progress) {
457
567
  if (this.deviceSelectionMode === DeviceSelectionMode.UseAnyAllowed) {
458
568
  await this.attemptConnectAllowedDevices();
459
569
  }
460
- if (!this.connection) {
570
+ if (!this.device) {
461
571
  progress(ProgressStage.FindingDevice);
462
- this.device = await this.chooseDevice();
572
+ this.usbDevice = await this.chooseDevice();
463
573
  progress(ProgressStage.Connecting);
464
- this.connection = new DAPWrapper(this.device, this.logging);
465
- await withTimeout(this.connection.reconnectAsync(), 10_000);
574
+ this.device = new USBDeviceWrapper(this.usbDevice, this.logging);
575
+ this.cachedConnectionInfo = await withTimeout(this.device.reconnect(), 10_000);
466
576
  }
467
577
  }
468
578
  // Based on: https://github.com/microsoft/pxt/blob/ab97a2422879824c730f009b15d4bf446b0e8547/pxtlib/webusb.ts#L361
469
579
  async attemptConnectAllowedDevices() {
470
580
  const pairedDevices = await this.getFilteredAllowedDevices();
471
- for (const device of pairedDevices) {
472
- const connection = await this.attemptDeviceConnection(device);
473
- if (connection) {
581
+ for (const usbDevice of pairedDevices) {
582
+ const device = await this.attemptDeviceConnection(usbDevice);
583
+ if (device) {
584
+ this.usbDevice = usbDevice;
474
585
  this.device = device;
475
- this.connection = connection;
476
586
  return;
477
587
  }
478
588
  }
@@ -487,34 +597,33 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
487
597
  const filteredDevices = devices.filter((device) => applyDeviceFilters(device, defaultFilters, this.exclusionFilters ?? []));
488
598
  return filteredDevices;
489
599
  }
490
- async attemptDeviceConnection(device) {
491
- this.log(`Attempting connection to: ${device.manufacturerName} ${device.productName}`);
492
- this.log(`Serial number: ${device.serialNumber}`);
493
- const connection = new DAPWrapper(device, this.logging);
494
- await withTimeout(connection.reconnectAsync(), 10_000);
495
- return connection;
600
+ async attemptDeviceConnection(usbDevice) {
601
+ this.log(`Attempting connection to: ${usbDevice.manufacturerName} ${usbDevice.productName}`);
602
+ this.log(`Serial number: ${usbDevice.serialNumber}`);
603
+ const device = new USBDeviceWrapper(usbDevice, this.logging);
604
+ this.cachedConnectionInfo = await withTimeout(device.reconnect(), 10_000);
605
+ return device;
496
606
  }
497
607
  async chooseDevice() {
498
- this.dispatchTypedEvent("beforerequestdevice", new BeforeRequestDevice());
608
+ this.dispatchEvent("beforerequestdevice");
499
609
  try {
500
- this.device = await navigator.usb.requestDevice({
610
+ this.usbDevice = await navigator.usb.requestDevice({
501
611
  exclusionFilters: this.exclusionFilters,
502
612
  filters: defaultFilters,
503
613
  });
504
614
  }
505
615
  finally {
506
- this.dispatchTypedEvent("afterrequestdevice", new AfterRequestDevice());
616
+ this.dispatchEvent("afterrequestdevice");
507
617
  }
508
- return this.device;
618
+ return this.usbDevice;
509
619
  }
510
620
  eventActivated(type) {
511
621
  switch (type) {
512
622
  case "serialdata": {
513
- // Prevent starting serial if already started and when flashing.
514
- if (!this.addedListeners.serialdata && !this.flashing) {
623
+ // Prevent starting serial when flashing. We'll reinstate later.
624
+ if (!this.flashing) {
515
625
  this.startSerialInternal();
516
626
  }
517
- this.addedListeners.serialdata++;
518
627
  break;
519
628
  }
520
629
  }
@@ -522,14 +631,14 @@ class MicrobitWebUSBConnectionImpl extends TypedEventTarget {
522
631
  async eventDeactivated(type) {
523
632
  switch (type) {
524
633
  case "serialdata": {
525
- this.addedListeners.serialdata--;
526
- if (!this.addedListeners.serialdata) {
527
- this.stopSerialInternal();
528
- }
634
+ this.stopSerialInternal();
529
635
  break;
530
636
  }
531
637
  }
532
638
  }
639
+ hasSerialEventListeners() {
640
+ return this.getActiveEvents().includes("serialdata");
641
+ }
533
642
  }
534
643
  /**
535
644
  * Applying WebUSB device filter. Exported for testing.
@@ -574,66 +683,42 @@ const hasMatchingInterface = (device, filter) => {
574
683
  });
575
684
  });
576
685
  };
577
- const genericErrorSuggestingReconnect = (e) => new DeviceError({
578
- code: "reconnect-microbit",
579
- message: e.message,
580
- });
581
- // tslint:disable-next-line: no-any
582
686
  const enrichedError = (err) => {
583
687
  if (err instanceof DeviceError) {
584
688
  return err;
585
689
  }
586
- if (err instanceof TimeoutError) {
587
- return new DeviceError({
588
- code: "timeout-error",
589
- message: err.message,
590
- });
591
- }
592
- switch (typeof err) {
593
- case "object":
594
- // We might get Error objects as Promise rejection arguments
595
- if (!err.message && err.promise && err.reason) {
596
- err = err.reason;
597
- }
598
- // This is somewhat fragile but worth it for scenario specific errors.
599
- // These messages changed to be prefixed in 2023 so we've relaxed the checks.
600
- if (/No valid interfaces found/.test(err.message)) {
601
- // This comes from DAPjs's WebUSB open.
602
- return new DeviceError({
603
- code: "update-req",
604
- message: err.message,
605
- });
606
- }
607
- else if (/No device selected/.test(err.message)) {
608
- return new DeviceError({
609
- code: "no-device-selected",
610
- message: err.message,
611
- });
612
- }
613
- else if (/Unable to claim interface/.test(err.message)) {
614
- return new DeviceError({
615
- code: "clear-connect",
616
- message: err.message,
617
- });
618
- }
619
- else if (err.name === "device-disconnected") {
620
- return new DeviceError({
621
- code: "device-disconnected",
622
- message: err.message,
623
- });
624
- }
625
- else {
626
- // Unhandled error. User will need to reconnect their micro:bit
627
- return genericErrorSuggestingReconnect(err);
628
- }
629
- case "string": {
630
- // Caught a string. Example case: "Flash error" from DAPjs
631
- return genericErrorSuggestingReconnect(err);
690
+ if (err instanceof Error) {
691
+ // Match Chromium WebUSB DOMException messages for user-friendly error codes.
692
+ // These are hardcoded English strings in Chromium (not localized).
693
+ // https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/modules/webusb/usb.cc
694
+ if (/No device selected/.test(err.message)) {
695
+ return new DeviceError({
696
+ code: "no-device-selected",
697
+ message: err.message,
698
+ cause: err,
699
+ });
700
+ }
701
+ // https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/modules/webusb/usb_device.cc
702
+ if (/Unable to claim interface/.test(err.message)) {
703
+ return new DeviceError({
704
+ code: "device-in-use",
705
+ message: err.message,
706
+ cause: err,
707
+ });
632
708
  }
633
- default: {
634
- return genericErrorSuggestingReconnect(err);
709
+ if (/The device was disconnected/.test(err.message)) {
710
+ return new DeviceError({
711
+ code: "device-disconnected",
712
+ message: err.message,
713
+ cause: err,
714
+ });
635
715
  }
636
716
  }
717
+ return new DeviceError({
718
+ code: "connection-error",
719
+ message: err.message,
720
+ cause: err,
721
+ });
637
722
  };
638
723
  const rateLimitProgress = (minimumProgressIncrement, callback) => {
639
724
  let lastCallValue = -1;
@@ -650,4 +735,4 @@ const rateLimitProgress = (minimumProgressIncrement, callback) => {
650
735
  }
651
736
  };
652
737
  };
653
- //# sourceMappingURL=usb.js.map
738
+ //# sourceMappingURL=connection.js.map