From 01243181c313b7a5be0db7af44acf39e9c049c29 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 22 Sep 2023 17:48:50 +0200 Subject: [PATCH] sd: update to prepare for changes in the TinyGo CGo implementation For details, see: https://github.com/tinygo-org/tinygo/pull/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). --- adapter_nrf51.go | 6 ++-- adapter_nrf528xx-full.go | 10 +++---- adapter_nrf528xx-peripheral.go | 4 +-- adapter_nrf528xx.go | 4 +-- adapter_sd.go | 52 +++++++++++++++++++++++++++++----- error_sd.go | 3 +- gap_nrf51.go | 7 +++-- gap_nrf528xx-advertisement.go | 9 +++--- gap_nrf528xx-central.go | 25 ++++++++-------- gattc_sd.go | 43 ++++++++++++++-------------- gatts_sd.go | 35 ++++++++++++----------- go.mod | 2 +- go.sum | 28 ++---------------- uuid_sd.go | 6 ++-- 14 files changed, 127 insertions(+), 107 deletions(-) diff --git a/adapter_nrf51.go b/adapter_nrf51.go index a46efb3..2b98cc1 100644 --- a/adapter_nrf51.go +++ b/adapter_nrf51.go @@ -42,7 +42,7 @@ func handleEvent() { gapEvent := eventBuf.evt.unionfield_gap_evt() switch id { case C.BLE_GAP_EVT_CONNECTED: - currentConnection.Reg = gapEvent.conn_handle + currentConnection.handle.Reg = uint16(gapEvent.conn_handle) DefaultAdapter.connectHandler(Address{}, true) case C.BLE_GAP_EVT_DISCONNECTED: if defaultAdvertisement.isAdvertising.Get() != 0 { @@ -54,7 +54,7 @@ func handleEvent() { // necessary. defaultAdvertisement.start() } - currentConnection.Reg = C.BLE_CONN_HANDLE_INVALID + currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID DefaultAdapter.connectHandler(Address{}, false) case C.BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: // Respond with the default PPCP connection parameters by passing @@ -109,5 +109,5 @@ func (a *Adapter) Address() (MACAddress, error) { if errCode != 0 { return MACAddress{}, Error(errCode) } - return MACAddress{MAC: addr.addr}, nil + return MACAddress{MAC: makeAddress(addr.addr)}, nil } diff --git a/adapter_nrf528xx-full.go b/adapter_nrf528xx-full.go index 64dc954..6ce048b 100644 --- a/adapter_nrf528xx-full.go +++ b/adapter_nrf528xx-full.go @@ -30,7 +30,7 @@ func handleEvent() { if debug { println("evt: connected in peripheral role") } - currentConnection.Reg = gapEvent.conn_handle + currentConnection.handle.Reg = uint16(gapEvent.conn_handle) DefaultAdapter.connectHandler(Address{}, true) case C.BLE_GAP_ROLE_CENTRAL: if debug { @@ -46,11 +46,11 @@ func handleEvent() { } // Clean up state for this connection. for i, cb := range gattcNotificationCallbacks { - if cb.connectionHandle == currentConnection.Reg { + if uint16(cb.connectionHandle) == currentConnection.handle.Reg { gattcNotificationCallbacks[i].valueHandle = 0 // 0 means invalid } } - currentConnection.Reg = C.BLE_CONN_HANDLE_INVALID + currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID // Auto-restart advertisement if needed. if defaultAdvertisement.isAdvertising.Get() != 0 { // The advertisement was running but was automatically stopped @@ -64,7 +64,7 @@ func handleEvent() { DefaultAdapter.connectHandler(Address{}, false) case C.BLE_GAP_EVT_ADV_REPORT: advReport := gapEvent.params.unionfield_adv_report() - if debug && &scanReportBuffer.data[0] != advReport.data.p_data { + if debug && &scanReportBuffer.data[0] != (*byte)(unsafe.Pointer(advReport.data.p_data)) { // Sanity check. panic("scanReportBuffer != advReport.p_data") } @@ -73,7 +73,7 @@ func handleEvent() { scanReportBuffer.len = byte(advReport.data.len) globalScanResult.RSSI = int16(advReport.rssi) globalScanResult.Address = Address{ - MACAddress{MAC: advReport.peer_addr.addr, + MACAddress{MAC: makeAddress(advReport.peer_addr.addr), isRandom: advReport.peer_addr.bitfield_addr_type() != 0}, } globalScanResult.AdvertisementPayload = &scanReportBuffer diff --git a/adapter_nrf528xx-peripheral.go b/adapter_nrf528xx-peripheral.go index 3b4ab51..5e9f181 100644 --- a/adapter_nrf528xx-peripheral.go +++ b/adapter_nrf528xx-peripheral.go @@ -27,13 +27,13 @@ func handleEvent() { if debug { println("evt: connected in peripheral role") } - currentConnection.Reg = gapEvent.conn_handle + currentConnection.handle.Reg = uint16(gapEvent.conn_handle) DefaultAdapter.connectHandler(Address{}, true) case C.BLE_GAP_EVT_DISCONNECTED: if debug { println("evt: disconnected") } - currentConnection.Reg = C.BLE_CONN_HANDLE_INVALID + currentConnection.handle.Reg = C.BLE_CONN_HANDLE_INVALID // Auto-restart advertisement if needed. if defaultAdvertisement.isAdvertising.Get() != 0 { // The advertisement was running but was automatically stopped diff --git a/adapter_nrf528xx.go b/adapter_nrf528xx.go index 7c47f4e..d05ab02 100644 --- a/adapter_nrf528xx.go +++ b/adapter_nrf528xx.go @@ -46,7 +46,7 @@ func (a *Adapter) enable() error { } // Enable the BLE stack. - appRAMBase := uint32(uintptr(unsafe.Pointer(&appRAMBase))) + appRAMBase := C.uint32_t(uintptr(unsafe.Pointer(&appRAMBase))) errCode = C.sd_ble_enable(&appRAMBase) return makeError(errCode) } @@ -57,5 +57,5 @@ func (a *Adapter) Address() (MACAddress, error) { if errCode != 0 { return MACAddress{}, Error(errCode) } - return MACAddress{MAC: addr.addr}, nil + return MACAddress{MAC: makeAddress(addr.addr)}, nil } diff --git a/adapter_sd.go b/adapter_sd.go index 24963b3..44590ab 100644 --- a/adapter_sd.go +++ b/adapter_sd.go @@ -28,7 +28,7 @@ var ( ) // There can only be one connection at a time in the default configuration. -var currentConnection = volatile.Register16{C.BLE_CONN_HANDLE_INVALID} +var currentConnection = volatileHandle{handle: volatile.Register16{C.BLE_CONN_HANDLE_INVALID}} // Globally allocated buffer for incoming SoftDevice events. var eventBuf struct { @@ -60,7 +60,7 @@ var DefaultAdapter = &Adapter{isDefault: true, return }} -var eventBufLen uint16 +var eventBufLen C.uint16_t // Enable configures the BLE stack. It must be called before any // Bluetooth-related calls (unless otherwise indicated). @@ -72,8 +72,8 @@ func (a *Adapter) Enable() error { // Enable the IRQ that handles all events. intr := interrupt.New(nrf.IRQ_SWI2, func(interrupt.Interrupt) { for { - eventBufLen = uint16(unsafe.Sizeof(eventBuf)) - errCode := C.sd_ble_evt_get((*uint8)(unsafe.Pointer(&eventBuf)), &eventBufLen) + eventBufLen = C.uint16_t(unsafe.Sizeof(eventBuf)) + errCode := C.sd_ble_evt_get((*C.uint8_t)(unsafe.Pointer(&eventBuf)), &eventBufLen) if errCode != 0 { // Possible error conditions: // * NRF_ERROR_NOT_FOUND: no events left, break @@ -97,7 +97,7 @@ func (a *Adapter) Enable() error { return err } - errCode := C.sd_ble_gap_device_name_set(&secModeOpen, &defaultDeviceName[0], uint16(len(defaultDeviceName))) + errCode := C.sd_ble_gap_device_name_set(&secModeOpen, (*C.uint8_t)(unsafe.Pointer(&defaultDeviceName[0])), C.uint16_t(len(defaultDeviceName))) if errCode != 0 { return Error(errCode) } @@ -116,7 +116,7 @@ func (a *Adapter) Enable() error { // play well with the SoftDevice. Restore interrupts to the previous state with // RestoreInterrupts. func DisableInterrupts() uintptr { - var is_nested_critical_region uint8 + var is_nested_critical_region C.uint8_t C.sd_nvic_critical_region_enter(&is_nested_critical_region) return uintptr(is_nested_critical_region) } @@ -125,5 +125,43 @@ func DisableInterrupts() uintptr { // DisableInterrupts. The mask parameter must be the value returned by // DisableInterrupts. func RestoreInterrupts(mask uintptr) { - C.sd_nvic_critical_region_exit(uint8(mask)) + C.sd_nvic_critical_region_exit(C.uint8_t(mask)) +} + +// Wrapper for volatile.Register16 that uses C.uint16_t instead of uint16, for +// easier interoperability with C. +type volatileHandle struct { + handle volatile.Register16 +} + +func (a *volatileHandle) Set(handle C.uint16_t) { + a.handle.Set(uint16(handle)) +} + +func (a *volatileHandle) Get() C.uint16_t { + return C.uint16_t(a.handle.Get()) +} + +// Convert a SoftDevice MAC address into a Go MAC address. +func makeAddress(mac [6]C.uint8_t) MAC { + return MAC{ + uint8(mac[0]), + uint8(mac[1]), + uint8(mac[2]), + uint8(mac[3]), + uint8(mac[4]), + uint8(mac[5]), + } +} + +// Convert a Go MAC address into a SoftDevice MAC Address. +func makeSDAddress(mac MAC) [6]C.uint8_t { + return [6]C.uint8_t{ + C.uint8_t(mac[0]), + C.uint8_t(mac[1]), + C.uint8_t(mac[2]), + C.uint8_t(mac[3]), + C.uint8_t(mac[4]), + C.uint8_t(mac[5]), + } } diff --git a/error_sd.go b/error_sd.go index 16c43ee..ad1f371 100644 --- a/error_sd.go +++ b/error_sd.go @@ -2,6 +2,7 @@ package bluetooth +// #include // #include "nrf_error.h" // #include "nrf_error_sdm.h" import "C" @@ -83,7 +84,7 @@ func (e Error) Error() string { // makeError returns an error (using the Error type) if the error code is // non-zero, otherwise it returns nil. It is used with internal API calls. -func makeError(code uint32) error { +func makeError(code C.uint32_t) error { if code != 0 { return Error(code) } diff --git a/gap_nrf51.go b/gap_nrf51.go index f37f59b..9ec6d7f 100644 --- a/gap_nrf51.go +++ b/gap_nrf51.go @@ -15,6 +15,7 @@ import "C" import ( "runtime/volatile" "time" + "unsafe" ) // Address contains a Bluetooth MAC address. @@ -52,7 +53,7 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error { return errAdvertisementPacketTooBig } - errCode := C.sd_ble_gap_adv_data_set(&payload.data[0], payload.len, nil, 0) + errCode := C.sd_ble_gap_adv_data_set((*C.uint8_t)(unsafe.Pointer(&payload.data[0])), C.uint8_t(payload.len), nil, 0) a.interval = options.Interval return makeError(errCode) } @@ -73,11 +74,11 @@ func (a *Advertisement) Stop() error { // Low-level version of Start. Used to restart advertisement when a connection // is lost. -func (a *Advertisement) start() uint32 { +func (a *Advertisement) start() C.uint32_t { params := C.ble_gap_adv_params_t{ _type: C.BLE_GAP_ADV_TYPE_ADV_IND, fp: C.BLE_GAP_ADV_FP_ANY, - interval: uint16(a.interval), + interval: C.uint16_t(a.interval), timeout: 0, // no timeout } return C.sd_ble_gap_adv_start_noescape(params) diff --git a/gap_nrf528xx-advertisement.go b/gap_nrf528xx-advertisement.go index 9b3a653..1d72604 100644 --- a/gap_nrf528xx-advertisement.go +++ b/gap_nrf528xx-advertisement.go @@ -5,6 +5,7 @@ package bluetooth import ( "runtime/volatile" "time" + "unsafe" ) /* @@ -19,7 +20,7 @@ type Address struct { // Advertisement encapsulates a single advertisement instance. type Advertisement struct { - handle uint8 + handle C.uint8_t isAdvertising volatile.Register8 payload rawAdvertisementPayload } @@ -57,14 +58,14 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error { data := C.ble_gap_adv_data_t{} data.adv_data = C.ble_data_t{ - p_data: &a.payload.data[0], - len: uint16(a.payload.len), + p_data: (*C.uint8_t)(unsafe.Pointer(&a.payload.data[0])), + len: C.uint16_t(a.payload.len), } params := C.ble_gap_adv_params_t{ properties: C.ble_gap_adv_properties_t{ _type: C.BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED, }, - interval: uint32(options.Interval), + interval: C.uint32_t(options.Interval), } errCode := C.sd_ble_gap_adv_set_configure(&a.handle, &data, ¶ms) return makeError(errCode) diff --git a/gap_nrf528xx-central.go b/gap_nrf528xx-central.go index d3faa4a..44fe683 100644 --- a/gap_nrf528xx-central.go +++ b/gap_nrf528xx-central.go @@ -7,6 +7,7 @@ import ( "errors" "runtime/volatile" "time" + "unsafe" ) /* @@ -40,12 +41,12 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error { scanParams := C.ble_gap_scan_params_t{} scanParams.set_bitfield_extended(0) scanParams.set_bitfield_active(0) - scanParams.interval = uint16(NewDuration(40 * time.Millisecond)) - scanParams.window = uint16(NewDuration(30 * time.Millisecond)) + scanParams.interval = C.uint16_t(NewDuration(40 * time.Millisecond)) + scanParams.window = C.uint16_t(NewDuration(30 * time.Millisecond)) scanParams.timeout = C.BLE_GAP_SCAN_TIMEOUT_UNLIMITED scanReportBufferInfo := C.ble_data_t{ - p_data: &scanReportBuffer.data[0], - len: uint16(len(scanReportBuffer.data)), + p_data: (*C.uint8_t)(unsafe.Pointer(&scanReportBuffer.data[0])), + len: C.uint16_t(len(scanReportBuffer.data)), } errCode := C.sd_ble_gap_scan_start(&scanParams, &scanReportBufferInfo) if errCode != 0 { @@ -93,13 +94,13 @@ func (a *Adapter) StopScan() error { // Device is a connection to a remote peripheral. type Device struct { - connectionHandle uint16 + connectionHandle C.uint16_t } // In-progress connection attempt. var connectionAttempt struct { state volatile.Register8 // 0 means unused, 1 means connecting, 2 means ready (connected or timeout) - connectionHandle uint16 + connectionHandle C.uint16_t } // Connect starts a connection attempt to the given peripheral device address. @@ -111,7 +112,7 @@ var connectionAttempt struct { func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, error) { // Construct an address object as used in the SoftDevice. var addr C.ble_gap_addr_t - addr.addr = address.MAC + addr.addr = makeSDAddress(address.MAC) if address.IsRandom() { switch address.MAC[5] >> 6 { case 0b11: @@ -142,13 +143,13 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er scanParams := C.ble_gap_scan_params_t{} scanParams.set_bitfield_extended(0) scanParams.set_bitfield_active(0) - scanParams.interval = uint16(NewDuration(40 * time.Millisecond)) - scanParams.window = uint16(NewDuration(30 * time.Millisecond)) - scanParams.timeout = uint16(params.ConnectionTimeout) + scanParams.interval = C.uint16_t(NewDuration(40 * time.Millisecond)) + scanParams.window = C.uint16_t(NewDuration(30 * time.Millisecond)) + scanParams.timeout = C.uint16_t(params.ConnectionTimeout) connectionParams := C.ble_gap_conn_params_t{ - min_conn_interval: uint16(params.MinInterval) / 2, - max_conn_interval: uint16(params.MaxInterval) / 2, + min_conn_interval: C.uint16_t(params.MinInterval) / 2, + max_conn_interval: C.uint16_t(params.MaxInterval) / 2, slave_latency: 0, // mostly relevant to connected keyboards etc conn_sup_timeout: 200, // 2 seconds (in 10ms units), the minimum recommended by Apple } diff --git a/gattc_sd.go b/gattc_sd.go index 018d398..9f8fb8f 100644 --- a/gattc_sd.go +++ b/gattc_sd.go @@ -11,6 +11,7 @@ import ( "device/arm" "errors" "runtime/volatile" + "unsafe" ) const ( @@ -28,8 +29,8 @@ var ( // program and the event handler. var discoveringService struct { state volatile.Register8 // 0 means nothing happening, 1 means in progress, 2 means found something - startHandle volatile.Register16 - endHandle volatile.Register16 + startHandle volatileHandle + endHandle volatileHandle uuid C.ble_uuid_t } @@ -38,9 +39,9 @@ var discoveringService struct { type DeviceService struct { uuid shortUUID - connectionHandle uint16 - startHandle uint16 - endHandle uint16 + connectionHandle C.uint16_t + startHandle C.uint16_t + endHandle C.uint16_t } // UUID returns the UUID for this DeviceService. @@ -76,7 +77,7 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { if len(uuids) > 0 { shortUUIDs = make([]C.ble_uuid_t, sz) for i, uuid := range uuids { - var errCode uint32 + var errCode C.uint32_t shortUUIDs[i], errCode = uuid.shortUUID() if errCode != 0 { return nil, Error(errCode) @@ -86,7 +87,7 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { numFound := 0 - var startHandle uint16 = 1 + var startHandle C.uint16_t = 1 for i := 0; i < sz; i++ { var suuid C.ble_uuid_t @@ -96,7 +97,7 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { // Start discovery of this service. discoveringService.state.Set(1) - var errCode uint32 + var errCode C.uint32_t if len(uuids) > 0 { errCode = C.sd_ble_gattc_primary_services_discover(d.connectionHandle, startHandle, &suuid) } else { @@ -159,9 +160,9 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { type DeviceCharacteristic struct { uuid shortUUID - connectionHandle uint16 - valueHandle uint16 - cccdHandle uint16 + connectionHandle C.uint16_t + valueHandle C.uint16_t + cccdHandle C.uint16_t permissions CharacteristicPermissions } @@ -175,7 +176,7 @@ func (c *DeviceCharacteristic) UUID() UUID { var discoveringCharacteristic struct { uuid C.ble_uuid_t char_props C.ble_gatt_char_props_t - handle_value volatile.Register16 + handle_value volatileHandle } // DiscoverCharacteristics discovers characteristics in this service. Pass a @@ -204,7 +205,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter if len(uuids) > 0 { shortUUIDs = make([]C.ble_uuid_t, sz) for i, uuid := range uuids { - var errCode uint32 + var errCode C.uint32_t shortUUIDs[i], errCode = uuid.shortUUID() if errCode != 0 { return nil, Error(errCode) @@ -324,8 +325,8 @@ func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (n int, err error) write_op: C.BLE_GATT_OP_WRITE_CMD, handle: c.valueHandle, offset: 0, - len: uint16(len(p)), - p_value: &p[0], + len: C.uint16_t(len(p)), + p_value: (*C.uint8_t)(unsafe.Pointer(&p[0])), }) if errCode != 0 { return 0, Error(errCode) @@ -334,8 +335,8 @@ func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (n int, err error) } type gattcNotificationCallback struct { - connectionHandle uint16 - valueHandle uint16 // may be 0 if the slot is empty + connectionHandle C.uint16_t + valueHandle C.uint16_t // may be 0 if the slot is empty callback func([]byte) } @@ -400,7 +401,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err } // Write to the CCCD to enable notifications. Don't wait for a response. - value := [2]byte{0x01, 0x00} // 0x0001 enables notifications (and disables indications) + value := [2]C.uint8_t{0x01, 0x00} // 0x0001 enables notifications (and disables indications) errCode := C.sd_ble_gattc_write(c.connectionHandle, &C.ble_gattc_write_params_t{ write_op: C.BLE_GATT_OP_WRITE_CMD, handle: c.cccdHandle, @@ -414,9 +415,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err // A global used to pass information from the event handler back to the // Read function below. var readingCharacteristic struct { - handle_value volatile.Register16 - offset uint16 - length uint16 + handle_value volatileHandle + offset C.uint16_t + length C.uint16_t value []byte } diff --git a/gatts_sd.go b/gatts_sd.go index 970f776..afd05be 100644 --- a/gatts_sd.go +++ b/gatts_sd.go @@ -17,11 +17,12 @@ static inline uint32_t sd_ble_gatts_value_set_noescape(uint16_t conn_handle, uin } */ import "C" +import "unsafe" // Characteristic is a single characteristic in a service. It has an UUID and a // value. type Characteristic struct { - handle uint16 + handle C.uint16_t permissions CharacteristicPermissions } @@ -32,18 +33,18 @@ func (a *Adapter) AddService(service *Service) error { if errCode != 0 { return Error(errCode) } - errCode = C.sd_ble_gatts_service_add(C.BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, &service.handle) + errCode = C.sd_ble_gatts_service_add(C.BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, (*C.uint16_t)(unsafe.Pointer(&service.handle))) if errCode != 0 { return Error(errCode) } for _, char := range service.Characteristics { metadata := C.ble_gatts_char_md_t{} - metadata.char_props.set_bitfield_broadcast(uint8(char.Flags>>0) & 1) - metadata.char_props.set_bitfield_read(uint8(char.Flags>>1) & 1) - metadata.char_props.set_bitfield_write_wo_resp(uint8(char.Flags>>2) & 1) - metadata.char_props.set_bitfield_write(uint8(char.Flags>>3) & 1) - metadata.char_props.set_bitfield_notify(uint8(char.Flags>>4) & 1) - metadata.char_props.set_bitfield_indicate(uint8(char.Flags>>5) & 1) + metadata.char_props.set_bitfield_broadcast(C.uint8_t(char.Flags>>0) & 1) + metadata.char_props.set_bitfield_read(C.uint8_t(char.Flags>>1) & 1) + metadata.char_props.set_bitfield_write_wo_resp(C.uint8_t(char.Flags>>2) & 1) + metadata.char_props.set_bitfield_write(C.uint8_t(char.Flags>>3) & 1) + metadata.char_props.set_bitfield_notify(C.uint8_t(char.Flags>>4) & 1) + metadata.char_props.set_bitfield_indicate(C.uint8_t(char.Flags>>5) & 1) handles := C.ble_gatts_char_handles_t{} charUUID, errCode := char.UUID.shortUUID() if errCode != 0 { @@ -55,16 +56,16 @@ func (a *Adapter) AddService(service *Service) error { read_perm: secModeOpen, write_perm: secModeOpen, }, - init_len: uint16(len(char.Value)), + init_len: C.uint16_t(len(char.Value)), init_offs: 0, max_len: 20, // This is a conservative maximum length. } if len(char.Value) != 0 { - value.p_value = &char.Value[0] + value.p_value = (*C.uint8_t)(unsafe.Pointer(&char.Value[0])) } value.p_attr_md.set_bitfield_vloc(C.BLE_GATTS_VLOC_STACK) value.p_attr_md.set_bitfield_vlen(1) - errCode = C.sd_ble_gatts_characteristic_add(service.handle, &metadata, &value, &handles) + errCode = C.sd_ble_gatts_characteristic_add(C.uint16_t(service.handle), &metadata, &value, &handles) if errCode != 0 { return Error(errCode) } @@ -88,13 +89,13 @@ func (a *Adapter) AddService(service *Service) error { // charWriteHandler contains a handler->callback mapping for characteristic // writes. type charWriteHandler struct { - handle uint16 + handle C.uint16_t callback func(connection Connection, offset int, value []byte) } // getCharWriteHandler returns a characteristic write handler if one matches the // handle, or nil otherwise. -func (a *Adapter) getCharWriteHandler(handle uint16) *charWriteHandler { +func (a *Adapter) getCharWriteHandler(handle C.uint16_t) *charWriteHandler { // Look through all handlers for a match. // There is probably a way to do this more efficiently (with a hashmap for // example) but the number of event handlers is likely low and improving @@ -124,8 +125,8 @@ func (c *Characteristic) Write(p []byte) (n int, err error) { c.handle, C.BLE_GATT_HVX_NOTIFICATION, 0, - p_len, - &p[0], + C.uint16_t(p_len), + (*C.uint8_t)(unsafe.Pointer(&p[0])), ) // Check for some expected errors. Don't report them as errors, but @@ -145,8 +146,8 @@ func (c *Characteristic) Write(p []byte) (n int, err error) { } 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], + len: C.uint16_t(len(p)), + p_value: (*C.uint8_t)(unsafe.Pointer(&p[0])), }) if errCode != 0 { return 0, Error(errCode) diff --git a/go.mod b/go.mod index ae68982..85559c2 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/saltosystems/winrt-go v0.0.0-20230921082907-2ab5b7d431e1 github.com/tinygo-org/cbgo v0.0.4 golang.org/x/crypto v0.12.0 - tinygo.org/x/drivers v0.25.0 + tinygo.org/x/drivers v0.26.1-0.20230922160320-ed51435c2ef6 tinygo.org/x/tinyfont v0.4.0 tinygo.org/x/tinyterm v0.3.0 ) diff --git a/go.sum b/go.sum index 57a0225..8dd63d1 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,18 @@ -github.com/bgould/http v0.0.0-20190627042742-d268792bdee7/go.mod h1:BTqvVegvwifopl4KTEDth6Zezs9eR+lCWhvGKvkxJHE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hajimehoshi/go-jisx0208 v1.0.0/go.mod h1:yYxEStHL7lt9uL+AbdWgW9gBumwieDoZCiB1f/0X0as= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/muka/go-bluetooth v0.0.0-20221213043340-85dc80edc4e1 h1:BuVRHr4HHJbk1DHyWkArJ7E8J/VA8ncCr/VLnQFazBo= @@ -28,7 +22,6 @@ github.com/paypal/gatt v0.0.0-20151011220935-4ae819d591cf/go.mod h1:+AwQL2mK3Pd3 github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sago35/go-bdf v0.0.0-20200313142241-6c17821c91c4/go.mod h1:rOebXGuMLsXhZAC6mF/TjxONsm45498ZyzVhel++6KM= github.com/saltosystems/winrt-go v0.0.0-20230921082907-2ab5b7d431e1 h1:L2YoWezgwpAZ2SEKjXk6yLnwOkM3u7mXq/mKuJeEpFM= github.com/saltosystems/winrt-go v0.0.0-20230921082907-2ab5b7d431e1/go.mod h1:CIltaIm7qaANUIvzr0Vmz71lmQMAIbGJ7cvgzX7FMfA= github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= @@ -43,19 +36,16 @@ github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324 github.com/suapapa/go_eddystone v1.3.1/go.mod h1:bXC11TfJOS+3g3q/Uzd7FKd5g62STQEfeEIhcKe4Qy8= github.com/tinygo-org/cbgo v0.0.4 h1:3D76CRYbH03Rudi8sEgs/YO0x3JIMdyq8jlQtk/44fU= github.com/tinygo-org/cbgo v0.0.4/go.mod h1:7+HgWIHd4nbAz0ESjGlJ1/v9LDU1Ox8MGzP9mah/fLk= -github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= -golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -64,38 +54,24 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200925191224-5d1fdd8fa346/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI= -tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI= -tinygo.org/x/drivers v0.16.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI= -tinygo.org/x/drivers v0.19.0/go.mod h1:uJD/l1qWzxzLx+vcxaW0eY464N5RAgFi1zTVzASFdqI= -tinygo.org/x/drivers v0.25.0 h1:MFnec5lY8Sxk1bIfqQWsflIbxcpAFbohWhg/qZ7psdM= -tinygo.org/x/drivers v0.25.0/go.mod h1:v+mXaA4cgpz/YZJ3ZPm/86bYQJAXTaYtMkHlVwbodbw= -tinygo.org/x/tinyfont v0.2.1/go.mod h1:eLqnYSrFRjt5STxWaMeOWJTzrKhXqpWw7nU3bPfKOAM= -tinygo.org/x/tinyfont v0.3.0/go.mod h1:+TV5q0KpwSGRWnN+ITijsIhrWYJkoUCp9MYELjKpAXk= +tinygo.org/x/drivers v0.26.1-0.20230922160320-ed51435c2ef6 h1:w18u47MirULgAl+bP0piUGu5VUZDs7TvXwHASEVXqHk= +tinygo.org/x/drivers v0.26.1-0.20230922160320-ed51435c2ef6/go.mod h1:X7utcg3yfFUFuKLOMTZD56eztXMjpkcf8OHldfTBsjw= tinygo.org/x/tinyfont v0.4.0 h1:XexPKEKiHInf6p4CMCJwsIheVPY0T46HUs6ictYyZfE= tinygo.org/x/tinyfont v0.4.0/go.mod h1:7nVj3j3geqBoPDzpFukAhF1C8AP9YocMsZy0HSAcGCA= -tinygo.org/x/tinyfs v0.1.0/go.mod h1:ysc8Y92iHfhTXeyEM9+c7zviUQ4fN9UCFgSOFfMWv20= -tinygo.org/x/tinyterm v0.1.0/go.mod h1:/DDhNnGwNF2/tNgHywvyZuCGnbH3ov49Z/6e8LPLRR4= tinygo.org/x/tinyterm v0.3.0 h1:4MMZoMyrbWbjru1KP/Z2TGhaguy/Uh5Mdhf/niemM8c= tinygo.org/x/tinyterm v0.3.0/go.mod h1:F1pQjxEwNZQIc5czeJSBtk57ucEvbR4u7vHaLhWhHtg= diff --git a/uuid_sd.go b/uuid_sd.go index b8b28fe..e9deb8b 100644 --- a/uuid_sd.go +++ b/uuid_sd.go @@ -10,9 +10,9 @@ import "unsafe" type shortUUID C.ble_uuid_t -func (uuid UUID) shortUUID() (C.ble_uuid_t, uint32) { +func (uuid UUID) shortUUID() (C.ble_uuid_t, C.uint32_t) { var short C.ble_uuid_t - short.uuid = uint16(uuid[3]) + short.uuid = C.uint16_t(uuid[3]) if uuid.Is16Bit() { short._type = C.BLE_UUID_TYPE_BLE return short, 0 @@ -24,7 +24,7 @@ func (uuid UUID) shortUUID() (C.ble_uuid_t, uint32) { // UUID returns the full length UUID for this short UUID. func (s shortUUID) UUID() UUID { if s._type == C.BLE_UUID_TYPE_BLE { - return New16BitUUID(s.uuid) + return New16BitUUID(uint16(s.uuid)) } var outLen C.uint8_t var outUUID UUID