softdevice: add address of connecting device

I thought it wasn't available, but in fact it is. So let's make it
available in the connect handler.
This commit is contained in:
Ayke van Laethem 2023-12-25 14:19:23 +01:00 committed by Ron Evans
parent 735333aa1a
commit 6e0df0ec3c
4 changed files with 25 additions and 6 deletions

View file

@ -43,7 +43,9 @@ func handleEvent() {
switch id { switch id {
case C.BLE_GAP_EVT_CONNECTED: case C.BLE_GAP_EVT_CONNECTED:
currentConnection.handle.Reg = uint16(gapEvent.conn_handle) currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
DefaultAdapter.connectHandler(Address{}, true) connectEvent := gapEvent.params.unionfield_connected()
address := Address{makeMACAddress(connectEvent.peer_addr)}
DefaultAdapter.connectHandler(address, true)
case C.BLE_GAP_EVT_DISCONNECTED: case C.BLE_GAP_EVT_DISCONNECTED:
if defaultAdvertisement.isAdvertising.Get() != 0 { if defaultAdvertisement.isAdvertising.Get() != 0 {
// The advertisement was running but was automatically stopped // The advertisement was running but was automatically stopped
@ -111,3 +113,11 @@ func (a *Adapter) Address() (MACAddress, error) {
} }
return MACAddress{MAC: makeAddress(addr.addr)}, nil return MACAddress{MAC: makeAddress(addr.addr)}, nil
} }
// Convert a C.ble_gap_addr_t to a MACAddress struct.
func makeMACAddress(addr C.ble_gap_addr_t) MACAddress {
return MACAddress{
MAC: makeAddress(addr.addr),
isRandom: addr.addr_type != 0,
}
}

View file

@ -25,20 +25,21 @@ func handleEvent() {
switch id { switch id {
case C.BLE_GAP_EVT_CONNECTED: case C.BLE_GAP_EVT_CONNECTED:
connectEvent := gapEvent.params.unionfield_connected() connectEvent := gapEvent.params.unionfield_connected()
address := Address{makeMACAddress(connectEvent.peer_addr)}
switch connectEvent.role { switch connectEvent.role {
case C.BLE_GAP_ROLE_PERIPH: case C.BLE_GAP_ROLE_PERIPH:
if debug { if debug {
println("evt: connected in peripheral role") println("evt: connected in peripheral role")
} }
currentConnection.handle.Reg = uint16(gapEvent.conn_handle) currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
DefaultAdapter.connectHandler(Address{}, true) DefaultAdapter.connectHandler(address, true)
case C.BLE_GAP_ROLE_CENTRAL: case C.BLE_GAP_ROLE_CENTRAL:
if debug { if debug {
println("evt: connected in central role") println("evt: connected in central role")
} }
connectionAttempt.connectionHandle = gapEvent.conn_handle connectionAttempt.connectionHandle = gapEvent.conn_handle
connectionAttempt.state.Set(2) // connection was successful connectionAttempt.state.Set(2) // connection was successful
DefaultAdapter.connectHandler(Address{}, true) DefaultAdapter.connectHandler(address, true)
} }
case C.BLE_GAP_EVT_DISCONNECTED: case C.BLE_GAP_EVT_DISCONNECTED:
if debug { if debug {
@ -81,8 +82,7 @@ func handleEvent() {
scanReportBuffer.len = byte(advReport.data.len) scanReportBuffer.len = byte(advReport.data.len)
globalScanResult.RSSI = int16(advReport.rssi) globalScanResult.RSSI = int16(advReport.rssi)
globalScanResult.Address = Address{ globalScanResult.Address = Address{
MACAddress{MAC: makeAddress(advReport.peer_addr.addr), makeMACAddress(advReport.peer_addr),
isRandom: advReport.peer_addr.bitfield_addr_type() != 0},
} }
globalScanResult.AdvertisementPayload = &scanReportBuffer globalScanResult.AdvertisementPayload = &scanReportBuffer
// Signal to the main thread that there was a scan report. // Signal to the main thread that there was a scan report.

View file

@ -28,7 +28,8 @@ func handleEvent() {
println("evt: connected in peripheral role") println("evt: connected in peripheral role")
} }
currentConnection.handle.Reg = uint16(gapEvent.conn_handle) currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
DefaultAdapter.connectHandler(Address{}, true) connectEvent := gapEvent.params.unionfield_connected()
DefaultAdapter.connectHandler(Address{makeMACAddress(connectEvent.peer_addr)}, true)
case C.BLE_GAP_EVT_DISCONNECTED: case C.BLE_GAP_EVT_DISCONNECTED:
if debug { if debug {
println("evt: disconnected") println("evt: disconnected")

View file

@ -59,3 +59,11 @@ func (a *Adapter) Address() (MACAddress, error) {
} }
return MACAddress{MAC: makeAddress(addr.addr)}, nil return MACAddress{MAC: makeAddress(addr.addr)}, nil
} }
// Convert a C.ble_gap_addr_t to a MACAddress struct.
func makeMACAddress(addr C.ble_gap_addr_t) MACAddress {
return MACAddress{
MAC: makeAddress(addr.addr),
isRandom: addr.bitfield_addr_type() != 0,
}
}