Skip to content

Commit

Permalink
Noescape workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
HattoriHanzo031 authored and aykevl committed Sep 1, 2023
1 parent 20ccbeb commit d34d15d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion gap_nrf51.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ package bluetooth

/*
#include "ble_gap.h"
// Workaround wrapper function to avoid pointer arguments escaping to heap
static inline uint32_t sd_ble_gap_adv_start_noescape(ble_gap_adv_params_t const p_adv_params) {
return sd_ble_gap_adv_start(&p_adv_params);
}
*/
import "C"

Expand Down Expand Up @@ -75,5 +80,5 @@ func (a *Advertisement) start() uint32 {
interval: uint16(a.interval),
timeout: 0, // no timeout
}
return C.sd_ble_gap_adv_start(&params)
return C.sd_ble_gap_adv_start_noescape(params)
}
25 changes: 18 additions & 7 deletions gatts_sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ package bluetooth
/*
#include "ble_gap.h"
#include "ble_gatts.h"
// Workaround wrapper functions which prevent pointer arguments escaping to heap
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) {
ble_gatts_hvx_params_t p_hvx_params = {handle, type, offset, &len, p_data};
return sd_ble_gatts_hvx(conn_handle, &p_hvx_params);
}
static inline uint32_t sd_ble_gatts_value_set_noescape(uint16_t conn_handle, uint16_t handle, ble_gatts_value_t p_value) {
return sd_ble_gatts_value_set(conn_handle, handle, &p_value);
}
*/
import "C"

Expand Down Expand Up @@ -110,12 +120,13 @@ func (c *Characteristic) Write(p []byte) (n int, err error) {
if connHandle != C.BLE_CONN_HANDLE_INVALID {
// There is a connected central.
p_len := uint16(len(p))
errCode := C.sd_ble_gatts_hvx(connHandle, &C.ble_gatts_hvx_params_t{
handle: c.handle,
_type: C.BLE_GATT_HVX_NOTIFICATION,
p_len: &p_len,
p_data: &p[0],
})
errCode := C.sd_ble_gatts_hvx_noescape(connHandle,
c.handle,
C.BLE_GATT_HVX_NOTIFICATION,
0,
p_len,
&p[0],
)

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

errCode := C.sd_ble_gatts_value_set(C.BLE_CONN_HANDLE_INVALID, c.handle, &C.ble_gatts_value_t{
errCode := C.sd_ble_gatts_value_set_noescape(C.BLE_CONN_HANDLE_INVALID, c.handle, C.ble_gatts_value_t{
len: uint16(len(p)),
p_value: &p[0],
})
Expand Down

0 comments on commit d34d15d

Please sign in to comment.