Skip to content

Commit 0124318

Browse files
aykevldeadprogram
authored andcommitted
sd: update to prepare for changes in the TinyGo CGo implementation
For details, see: tinygo-org/tinygo#3927 I ran the smoke tests and the binaries are exactly identical to what they were before, so this change cannot have had an effect on these smoke tests (which is expected, as this is mostly just changing some types without changing the machine data type).
1 parent d0c7887 commit 0124318

14 files changed

+127
-107
lines changed

adapter_nrf51.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func handleEvent() {
4242
gapEvent := eventBuf.evt.unionfield_gap_evt()
4343
switch id {
4444
case C.BLE_GAP_EVT_CONNECTED:
45-
currentConnection.Reg = gapEvent.conn_handle
45+
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
4646
DefaultAdapter.connectHandler(Address{}, true)
4747
case C.BLE_GAP_EVT_DISCONNECTED:
4848
if defaultAdvertisement.isAdvertising.Get() != 0 {
@@ -54,7 +54,7 @@ func handleEvent() {
5454
// necessary.
5555
defaultAdvertisement.start()
5656
}
57-
currentConnection.Reg = C.BLE_CONN_HANDLE_INVALID
57+
currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID
5858
DefaultAdapter.connectHandler(Address{}, false)
5959
case C.BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
6060
// Respond with the default PPCP connection parameters by passing
@@ -109,5 +109,5 @@ func (a *Adapter) Address() (MACAddress, error) {
109109
if errCode != 0 {
110110
return MACAddress{}, Error(errCode)
111111
}
112-
return MACAddress{MAC: addr.addr}, nil
112+
return MACAddress{MAC: makeAddress(addr.addr)}, nil
113113
}

adapter_nrf528xx-full.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func handleEvent() {
3030
if debug {
3131
println("evt: connected in peripheral role")
3232
}
33-
currentConnection.Reg = gapEvent.conn_handle
33+
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
3434
DefaultAdapter.connectHandler(Address{}, true)
3535
case C.BLE_GAP_ROLE_CENTRAL:
3636
if debug {
@@ -46,11 +46,11 @@ func handleEvent() {
4646
}
4747
// Clean up state for this connection.
4848
for i, cb := range gattcNotificationCallbacks {
49-
if cb.connectionHandle == currentConnection.Reg {
49+
if uint16(cb.connectionHandle) == currentConnection.handle.Reg {
5050
gattcNotificationCallbacks[i].valueHandle = 0 // 0 means invalid
5151
}
5252
}
53-
currentConnection.Reg = C.BLE_CONN_HANDLE_INVALID
53+
currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID
5454
// Auto-restart advertisement if needed.
5555
if defaultAdvertisement.isAdvertising.Get() != 0 {
5656
// The advertisement was running but was automatically stopped
@@ -64,7 +64,7 @@ func handleEvent() {
6464
DefaultAdapter.connectHandler(Address{}, false)
6565
case C.BLE_GAP_EVT_ADV_REPORT:
6666
advReport := gapEvent.params.unionfield_adv_report()
67-
if debug && &scanReportBuffer.data[0] != advReport.data.p_data {
67+
if debug && &scanReportBuffer.data[0] != (*byte)(unsafe.Pointer(advReport.data.p_data)) {
6868
// Sanity check.
6969
panic("scanReportBuffer != advReport.p_data")
7070
}
@@ -73,7 +73,7 @@ func handleEvent() {
7373
scanReportBuffer.len = byte(advReport.data.len)
7474
globalScanResult.RSSI = int16(advReport.rssi)
7575
globalScanResult.Address = Address{
76-
MACAddress{MAC: advReport.peer_addr.addr,
76+
MACAddress{MAC: makeAddress(advReport.peer_addr.addr),
7777
isRandom: advReport.peer_addr.bitfield_addr_type() != 0},
7878
}
7979
globalScanResult.AdvertisementPayload = &scanReportBuffer

adapter_nrf528xx-peripheral.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func handleEvent() {
2727
if debug {
2828
println("evt: connected in peripheral role")
2929
}
30-
currentConnection.Reg = gapEvent.conn_handle
30+
currentConnection.handle.Reg = uint16(gapEvent.conn_handle)
3131
DefaultAdapter.connectHandler(Address{}, true)
3232
case C.BLE_GAP_EVT_DISCONNECTED:
3333
if debug {
3434
println("evt: disconnected")
3535
}
36-
currentConnection.Reg = C.BLE_CONN_HANDLE_INVALID
36+
currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID
3737
// Auto-restart advertisement if needed.
3838
if defaultAdvertisement.isAdvertising.Get() != 0 {
3939
// The advertisement was running but was automatically stopped

adapter_nrf528xx.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (a *Adapter) enable() error {
4646
}
4747

4848
// Enable the BLE stack.
49-
appRAMBase := uint32(uintptr(unsafe.Pointer(&appRAMBase)))
49+
appRAMBase := C.uint32_t(uintptr(unsafe.Pointer(&appRAMBase)))
5050
errCode = C.sd_ble_enable(&appRAMBase)
5151
return makeError(errCode)
5252
}
@@ -57,5 +57,5 @@ func (a *Adapter) Address() (MACAddress, error) {
5757
if errCode != 0 {
5858
return MACAddress{}, Error(errCode)
5959
}
60-
return MACAddress{MAC: addr.addr}, nil
60+
return MACAddress{MAC: makeAddress(addr.addr)}, nil
6161
}

adapter_sd.go

+45-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
)
2929

3030
// There can only be one connection at a time in the default configuration.
31-
var currentConnection = volatile.Register16{C.BLE_CONN_HANDLE_INVALID}
31+
var currentConnection = volatileHandle{handle: volatile.Register16{C.BLE_CONN_HANDLE_INVALID}}
3232

3333
// Globally allocated buffer for incoming SoftDevice events.
3434
var eventBuf struct {
@@ -60,7 +60,7 @@ var DefaultAdapter = &Adapter{isDefault: true,
6060
return
6161
}}
6262

63-
var eventBufLen uint16
63+
var eventBufLen C.uint16_t
6464

6565
// Enable configures the BLE stack. It must be called before any
6666
// Bluetooth-related calls (unless otherwise indicated).
@@ -72,8 +72,8 @@ func (a *Adapter) Enable() error {
7272
// Enable the IRQ that handles all events.
7373
intr := interrupt.New(nrf.IRQ_SWI2, func(interrupt.Interrupt) {
7474
for {
75-
eventBufLen = uint16(unsafe.Sizeof(eventBuf))
76-
errCode := C.sd_ble_evt_get((*uint8)(unsafe.Pointer(&eventBuf)), &eventBufLen)
75+
eventBufLen = C.uint16_t(unsafe.Sizeof(eventBuf))
76+
errCode := C.sd_ble_evt_get((*C.uint8_t)(unsafe.Pointer(&eventBuf)), &eventBufLen)
7777
if errCode != 0 {
7878
// Possible error conditions:
7979
// * NRF_ERROR_NOT_FOUND: no events left, break
@@ -97,7 +97,7 @@ func (a *Adapter) Enable() error {
9797
return err
9898
}
9999

100-
errCode := C.sd_ble_gap_device_name_set(&secModeOpen, &defaultDeviceName[0], uint16(len(defaultDeviceName)))
100+
errCode := C.sd_ble_gap_device_name_set(&secModeOpen, (*C.uint8_t)(unsafe.Pointer(&defaultDeviceName[0])), C.uint16_t(len(defaultDeviceName)))
101101
if errCode != 0 {
102102
return Error(errCode)
103103
}
@@ -116,7 +116,7 @@ func (a *Adapter) Enable() error {
116116
// play well with the SoftDevice. Restore interrupts to the previous state with
117117
// RestoreInterrupts.
118118
func DisableInterrupts() uintptr {
119-
var is_nested_critical_region uint8
119+
var is_nested_critical_region C.uint8_t
120120
C.sd_nvic_critical_region_enter(&is_nested_critical_region)
121121
return uintptr(is_nested_critical_region)
122122
}
@@ -125,5 +125,43 @@ func DisableInterrupts() uintptr {
125125
// DisableInterrupts. The mask parameter must be the value returned by
126126
// DisableInterrupts.
127127
func RestoreInterrupts(mask uintptr) {
128-
C.sd_nvic_critical_region_exit(uint8(mask))
128+
C.sd_nvic_critical_region_exit(C.uint8_t(mask))
129+
}
130+
131+
// Wrapper for volatile.Register16 that uses C.uint16_t instead of uint16, for
132+
// easier interoperability with C.
133+
type volatileHandle struct {
134+
handle volatile.Register16
135+
}
136+
137+
func (a *volatileHandle) Set(handle C.uint16_t) {
138+
a.handle.Set(uint16(handle))
139+
}
140+
141+
func (a *volatileHandle) Get() C.uint16_t {
142+
return C.uint16_t(a.handle.Get())
143+
}
144+
145+
// Convert a SoftDevice MAC address into a Go MAC address.
146+
func makeAddress(mac [6]C.uint8_t) MAC {
147+
return MAC{
148+
uint8(mac[0]),
149+
uint8(mac[1]),
150+
uint8(mac[2]),
151+
uint8(mac[3]),
152+
uint8(mac[4]),
153+
uint8(mac[5]),
154+
}
155+
}
156+
157+
// Convert a Go MAC address into a SoftDevice MAC Address.
158+
func makeSDAddress(mac MAC) [6]C.uint8_t {
159+
return [6]C.uint8_t{
160+
C.uint8_t(mac[0]),
161+
C.uint8_t(mac[1]),
162+
C.uint8_t(mac[2]),
163+
C.uint8_t(mac[3]),
164+
C.uint8_t(mac[4]),
165+
C.uint8_t(mac[5]),
166+
}
129167
}

error_sd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package bluetooth
44

5+
// #include <stdint.h>
56
// #include "nrf_error.h"
67
// #include "nrf_error_sdm.h"
78
import "C"
@@ -83,7 +84,7 @@ func (e Error) Error() string {
8384

8485
// makeError returns an error (using the Error type) if the error code is
8586
// non-zero, otherwise it returns nil. It is used with internal API calls.
86-
func makeError(code uint32) error {
87+
func makeError(code C.uint32_t) error {
8788
if code != 0 {
8889
return Error(code)
8990
}

gap_nrf51.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import "C"
1515
import (
1616
"runtime/volatile"
1717
"time"
18+
"unsafe"
1819
)
1920

2021
// Address contains a Bluetooth MAC address.
@@ -52,7 +53,7 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
5253
return errAdvertisementPacketTooBig
5354
}
5455

55-
errCode := C.sd_ble_gap_adv_data_set(&payload.data[0], payload.len, nil, 0)
56+
errCode := C.sd_ble_gap_adv_data_set((*C.uint8_t)(unsafe.Pointer(&payload.data[0])), C.uint8_t(payload.len), nil, 0)
5657
a.interval = options.Interval
5758
return makeError(errCode)
5859
}
@@ -73,11 +74,11 @@ func (a *Advertisement) Stop() error {
7374

7475
// Low-level version of Start. Used to restart advertisement when a connection
7576
// is lost.
76-
func (a *Advertisement) start() uint32 {
77+
func (a *Advertisement) start() C.uint32_t {
7778
params := C.ble_gap_adv_params_t{
7879
_type: C.BLE_GAP_ADV_TYPE_ADV_IND,
7980
fp: C.BLE_GAP_ADV_FP_ANY,
80-
interval: uint16(a.interval),
81+
interval: C.uint16_t(a.interval),
8182
timeout: 0, // no timeout
8283
}
8384
return C.sd_ble_gap_adv_start_noescape(params)

gap_nrf528xx-advertisement.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package bluetooth
55
import (
66
"runtime/volatile"
77
"time"
8+
"unsafe"
89
)
910

1011
/*
@@ -19,7 +20,7 @@ type Address struct {
1920

2021
// Advertisement encapsulates a single advertisement instance.
2122
type Advertisement struct {
22-
handle uint8
23+
handle C.uint8_t
2324
isAdvertising volatile.Register8
2425
payload rawAdvertisementPayload
2526
}
@@ -57,14 +58,14 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
5758

5859
data := C.ble_gap_adv_data_t{}
5960
data.adv_data = C.ble_data_t{
60-
p_data: &a.payload.data[0],
61-
len: uint16(a.payload.len),
61+
p_data: (*C.uint8_t)(unsafe.Pointer(&a.payload.data[0])),
62+
len: C.uint16_t(a.payload.len),
6263
}
6364
params := C.ble_gap_adv_params_t{
6465
properties: C.ble_gap_adv_properties_t{
6566
_type: C.BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED,
6667
},
67-
interval: uint32(options.Interval),
68+
interval: C.uint32_t(options.Interval),
6869
}
6970
errCode := C.sd_ble_gap_adv_set_configure(&a.handle, &data, &params)
7071
return makeError(errCode)

gap_nrf528xx-central.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"runtime/volatile"
99
"time"
10+
"unsafe"
1011
)
1112

1213
/*
@@ -40,12 +41,12 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
4041
scanParams := C.ble_gap_scan_params_t{}
4142
scanParams.set_bitfield_extended(0)
4243
scanParams.set_bitfield_active(0)
43-
scanParams.interval = uint16(NewDuration(40 * time.Millisecond))
44-
scanParams.window = uint16(NewDuration(30 * time.Millisecond))
44+
scanParams.interval = C.uint16_t(NewDuration(40 * time.Millisecond))
45+
scanParams.window = C.uint16_t(NewDuration(30 * time.Millisecond))
4546
scanParams.timeout = C.BLE_GAP_SCAN_TIMEOUT_UNLIMITED
4647
scanReportBufferInfo := C.ble_data_t{
47-
p_data: &scanReportBuffer.data[0],
48-
len: uint16(len(scanReportBuffer.data)),
48+
p_data: (*C.uint8_t)(unsafe.Pointer(&scanReportBuffer.data[0])),
49+
len: C.uint16_t(len(scanReportBuffer.data)),
4950
}
5051
errCode := C.sd_ble_gap_scan_start(&scanParams, &scanReportBufferInfo)
5152
if errCode != 0 {
@@ -93,13 +94,13 @@ func (a *Adapter) StopScan() error {
9394

9495
// Device is a connection to a remote peripheral.
9596
type Device struct {
96-
connectionHandle uint16
97+
connectionHandle C.uint16_t
9798
}
9899

99100
// In-progress connection attempt.
100101
var connectionAttempt struct {
101102
state volatile.Register8 // 0 means unused, 1 means connecting, 2 means ready (connected or timeout)
102-
connectionHandle uint16
103+
connectionHandle C.uint16_t
103104
}
104105

105106
// Connect starts a connection attempt to the given peripheral device address.
@@ -111,7 +112,7 @@ var connectionAttempt struct {
111112
func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, error) {
112113
// Construct an address object as used in the SoftDevice.
113114
var addr C.ble_gap_addr_t
114-
addr.addr = address.MAC
115+
addr.addr = makeSDAddress(address.MAC)
115116
if address.IsRandom() {
116117
switch address.MAC[5] >> 6 {
117118
case 0b11:
@@ -142,13 +143,13 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er
142143
scanParams := C.ble_gap_scan_params_t{}
143144
scanParams.set_bitfield_extended(0)
144145
scanParams.set_bitfield_active(0)
145-
scanParams.interval = uint16(NewDuration(40 * time.Millisecond))
146-
scanParams.window = uint16(NewDuration(30 * time.Millisecond))
147-
scanParams.timeout = uint16(params.ConnectionTimeout)
146+
scanParams.interval = C.uint16_t(NewDuration(40 * time.Millisecond))
147+
scanParams.window = C.uint16_t(NewDuration(30 * time.Millisecond))
148+
scanParams.timeout = C.uint16_t(params.ConnectionTimeout)
148149

149150
connectionParams := C.ble_gap_conn_params_t{
150-
min_conn_interval: uint16(params.MinInterval) / 2,
151-
max_conn_interval: uint16(params.MaxInterval) / 2,
151+
min_conn_interval: C.uint16_t(params.MinInterval) / 2,
152+
max_conn_interval: C.uint16_t(params.MaxInterval) / 2,
152153
slave_latency: 0, // mostly relevant to connected keyboards etc
153154
conn_sup_timeout: 200, // 2 seconds (in 10ms units), the minimum recommended by Apple
154155
}

0 commit comments

Comments
 (0)