nrf: keep advertisement payload alive

The memory from the advertisement payload must be kept alive until the
advertisement is stopped. Therefore, it is not allowed to use a local
variable for it. Instead, it is now part of the defaultAdvertisement
global which of course is alive as long as the program runs.
This commit is contained in:
Ayke van Laethem 2020-09-16 15:50:26 +02:00 committed by Ron Evans
parent 69aae6c6b9
commit 633d9b5aea
2 changed files with 14 additions and 4 deletions

7
gap.go

@ -254,6 +254,13 @@ func (buf *rawAdvertisementPayload) HasServiceUUID(uuid UUID) bool {
}
}
// reset restores this buffer to the original state.
func (buf *rawAdvertisementPayload) reset() {
// The data is not reset (only the length), because with a zero length the
// data is undefined.
buf.len = 0
}
// addFromOptions constructs a new advertisement payload (assumed to be empty
// before the call) from the advertisement options. It returns true if it fits,
// false otherwise.

@ -36,6 +36,7 @@ type Address struct {
type Advertisement struct {
handle uint8
isAdvertising volatile.Register8
payload rawAdvertisementPayload
}
// The nrf528xx devices only seem to support one advertisement instance. The way
@ -62,15 +63,17 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
}
// Construct payload.
var payload rawAdvertisementPayload
if !payload.addFromOptions(options) {
// Note that the payload needs to be part of the Advertisement object as the
// memory is still used after sd_ble_gap_adv_set_configure returns.
a.payload.reset()
if !a.payload.addFromOptions(options) {
return errAdvertisementPacketTooBig
}
data := C.ble_gap_adv_data_t{}
data.adv_data = C.ble_data_t{
p_data: &payload.data[0],
len: uint16(payload.len),
p_data: &a.payload.data[0],
len: uint16(a.payload.len),
}
params := C.ble_gap_adv_params_t{
properties: C.ble_gap_adv_properties_t{