https://toitlang.org/ logo
BLE Adapter - can't close in the middle of a scrip...
# help
z
@floitsch I'm looking at https://libs.toit.io/ble/class-Adapter and seeing that it implements a method called
close
but can't call close using
ble.Adapter.close
or
ble.Adapter.central.close
I get a segfault
Copy code
/Users/fred/.cache/jaguar/sdk/bin/toit.run: Segmentation fault: 11
  0: run-program               <pkg:pkg-host>/pipe.toit:491:5
  1: run                       /Users/runner/work/toit/toit/tools/toit.toit:529:10
  2: compile-or-analyze-or-run /Users/runner/work/toit/toit/tools/toit.toit:618:16
  3: main.<lambda>             /Users/runner/work/toit/toit/tools/toit.toit:112:16
  4: Command.run.<block>       <pkg:pkg-cli>/cli.toit:308:40
  5: Parser_.parse             <pkg:pkg-cli>/parser_.toit:212:5
  6: Command.run               <pkg:pkg-cli>/cli.toit:306:12
  7: main                      /Users/runner/work/toit/toit/tools/toit.toit:514:16
Error: exit status 1
this is on Macos with
Copy code
(venv) ?1 o2r % jag version
Version:         v1.45.1
SDK version:     v2.0.0-alpha.170
Build date:      2024-11-29T20:49:14Z
there are some things marked in ble.toit about #primitive.ble.disconnect but I don't see much in the https://libs.toit.io/ble/class-Adapter documentation about how to disconnect BLE in the middle of a program
f
Thanks for the report. I don't own a MacOS machine and its BLE implementation clearly suffers from this... Maybe @kasperl can have a look and see if he can reproduce.
Closing should do the right thing (and we should definitely never have a segfault).
k
Hey @z3ugma! Is it possible for you to share the code you're trying to run, so I can try to reproduce the issue?
z
@kasperl here you go:
Copy code
import ble

DEV_NAME  ::= "MR"
SCAN_DURATION   ::= Duration --s=25
MONITOR_SERVICE ::= ble.BleUuid "f86afee0-8b6a-11e9-b0eb-80a589e0081a"
MONITOR_WRITE ::= ble.BleUuid "f86afee1-8b6a-11e9-b0eb-80a589e0081a"
MONITOR_NOTIFY ::= ble.BleUuid "f86afee2-8b6a-11e9-b0eb-80a589e0081a"
TOKEN_READ ::= ble.BleUuid "f86afee3-8b6a-11e9-b0eb-80a589e0081a"


find_by_name central/ble.Central name/string:
  central.scan --duration=SCAN_DURATION: | device/ble.RemoteScannedDevice |
    if device.identifier and device.data.name == name:
        return device.identifier
  throw "no ring device found"


class Ring: 
  adapter/ble.Adapter := ble.Adapter
  token_read/ble.RemoteCharacteristic := ?
  monitor_write/ble.RemoteCharacteristic := ?
  monitor_notify/ble.RemoteCharacteristic := ?

  constructor: 
    central := adapter.central
    identifier := find_by_name central DEV_NAME
    remote_device := central.connect identifier
    print "Address: $remote_device.identifier"
    services := remote_device.discover_services [MONITOR_SERVICE]
    monitor_service/ble.RemoteService := services.first
    ppg_service/ble.RemoteService := services.last
    monitor_characteristics := monitor_service.discover_characteristics [TOKEN_READ, MONITOR_WRITE, MONITOR_NOTIFY]
    token_read = monitor_characteristics[0]
    monitor_write = monitor_characteristics[1]
    monitor_notify = monitor_characteristics[2]

    monitor_write.subscribe
    monitor_notify.subscribe



main:
  c := Ring
  sleep --ms=2000
  c.adapter.close
  c.adapter.central.close
and here's the output from the command line:
Copy code
jag run -d host circul_kasper.toit

Address: 2ACB5B5E-2DC4-C265-1F05-19C40487DFC8
EXCEPTION error. 
/Users/fred/.cache/jaguar/sdk/bin/toit.run: Segmentation fault: 11
  0: run-program               <pkg:pkg-host>/pipe.toit:491:5
  1: run                       /Users/runner/work/toit/toit/tools/toit.toit:529:10
  2: compile-or-analyze-or-run /Users/runner/work/toit/toit/tools/toit.toit:618:16
  3: main.<lambda>             /Users/runner/work/toit/toit/tools/toit.toit:112:16
  4: Command.run.<block>       <pkg:pkg-cli>/cli.toit:308:40
  5: Parser_.parse             <pkg:pkg-cli>/parser_.toit:212:5
  6: Command.run               <pkg:pkg-cli>/cli.toit:306:12
  7: main                      /Users/runner/work/toit/toit/tools/toit.toit:514:16
Error: exit status 1
Whether I use either of these 2 options: c.adapter.close c.adapter.central.close I get a segfault
@kasperl above is the script that demonstrates the segfault