Skip to content

Commit 633d9b5

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent 69aae6c commit 633d9b5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

gap.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ func (buf *rawAdvertisementPayload) HasServiceUUID(uuid UUID) bool {
254254
}
255255
}
256256

257+
// reset restores this buffer to the original state.
258+
func (buf *rawAdvertisementPayload) reset() {
259+
// The data is not reset (only the length), because with a zero length the
260+
// data is undefined.
261+
buf.len = 0
262+
}
263+
257264
// addFromOptions constructs a new advertisement payload (assumed to be empty
258265
// before the call) from the advertisement options. It returns true if it fits,
259266
// false otherwise.

gap_nrf528xx.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Address struct {
3636
type Advertisement struct {
3737
handle uint8
3838
isAdvertising volatile.Register8
39+
payload rawAdvertisementPayload
3940
}
4041

4142
// The nrf528xx devices only seem to support one advertisement instance. The way
@@ -62,15 +63,17 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
6263
}
6364

6465
// Construct payload.
65-
var payload rawAdvertisementPayload
66-
if !payload.addFromOptions(options) {
66+
// Note that the payload needs to be part of the Advertisement object as the
67+
// memory is still used after sd_ble_gap_adv_set_configure returns.
68+
a.payload.reset()
69+
if !a.payload.addFromOptions(options) {
6770
return errAdvertisementPacketTooBig
6871
}
6972

7073
data := C.ble_gap_adv_data_t{}
7174
data.adv_data = C.ble_data_t{
72-
p_data: &payload.data[0],
73-
len: uint16(payload.len),
75+
p_data: &a.payload.data[0],
76+
len: uint16(a.payload.len),
7477
}
7578
params := C.ble_gap_adv_params_t{
7679
properties: C.ble_gap_adv_properties_t{

0 commit comments

Comments
 (0)