Skip to content

Commit 89c1806

Browse files
Noescape workaround
1 parent 20ccbeb commit 89c1806

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

gap_nrf51.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ package bluetooth
44

55
/*
66
#include "ble_gap.h"
7+
8+
// Workaround wrapper function to avoid pointer arguments escaping to heap
9+
static inline uint32_t sd_ble_gap_adv_start_noescape(ble_gap_adv_params_t const p_adv_params) {
10+
return sd_ble_gap_adv_start(&p_adv_params);
11+
}
712
*/
813
import "C"
914

@@ -75,5 +80,5 @@ func (a *Advertisement) start() uint32 {
7580
interval: uint16(a.interval),
7681
timeout: 0, // no timeout
7782
}
78-
return C.sd_ble_gap_adv_start(&params)
83+
return C.sd_ble_gap_adv_start_noescape(params)
7984
}

gatts_sd.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ package bluetooth
55
/*
66
#include "ble_gap.h"
77
#include "ble_gatts.h"
8+
9+
// Workaround wrapper functions which prevent pointer arguments escaping to heap
10+
static inline uint32_t sd_ble_gatts_hvx_noescape(uint16_t conn_handle, uint16_t handle, uint8_t type, uint16_t offset, uint16_t len, uint8_t *p_data) {
11+
ble_gatts_hvx_params_t p_hvx_params = {handle, type, offset, &len, p_data};
12+
return sd_ble_gatts_hvx(conn_handle, &p_hvx_params);
13+
}
14+
15+
static inline uint32_t sd_ble_gatts_value_set_noescape(uint16_t conn_handle, uint16_t handle, ble_gatts_value_t p_value) {
16+
return sd_ble_gatts_value_set(conn_handle, handle, &p_value);
17+
}
818
*/
919
import "C"
1020

@@ -110,12 +120,13 @@ func (c *Characteristic) Write(p []byte) (n int, err error) {
110120
if connHandle != C.BLE_CONN_HANDLE_INVALID {
111121
// There is a connected central.
112122
p_len := uint16(len(p))
113-
errCode := C.sd_ble_gatts_hvx(connHandle, &C.ble_gatts_hvx_params_t{
114-
handle: c.handle,
115-
_type: C.BLE_GATT_HVX_NOTIFICATION,
116-
p_len: &p_len,
117-
p_data: &p[0],
118-
})
123+
errCode := C.sd_ble_gatts_hvx_noescape(connHandle,
124+
c.handle,
125+
C.BLE_GATT_HVX_NOTIFICATION,
126+
0,
127+
p_len,
128+
&p[0],
129+
)
119130

120131
// Check for some expected errors. Don't report them as errors, but
121132
// instead fall through and do a normal characteristic value update.
@@ -133,7 +144,7 @@ func (c *Characteristic) Write(p []byte) (n int, err error) {
133144
}
134145
}
135146

136-
errCode := C.sd_ble_gatts_value_set(C.BLE_CONN_HANDLE_INVALID, c.handle, &C.ble_gatts_value_t{
147+
errCode := C.sd_ble_gatts_value_set_noescape(C.BLE_CONN_HANDLE_INVALID, c.handle, C.ble_gatts_value_t{
137148
len: uint16(len(p)),
138149
p_value: &p[0],
139150
})

0 commit comments

Comments
 (0)