Skip to content

Commit

Permalink
all: don't use a pointer receiver for many method calls
Browse files Browse the repository at this point in the history
This is unnecessary because the values are passed by value in other
cases, and can in some cases lead to more (heap or stack) allocation
than is necessary.
  • Loading branch information
aykevl authored and deadprogram committed Jan 4, 2024
1 parent 83fba1b commit 5746ccf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions gattc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type deviceService struct {
}

// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuidWrapper
}

Expand All @@ -89,7 +89,7 @@ func (s *DeviceService) UUID() UUID {
//
// Passing a nil slice of UUIDs will return a complete list of
// characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
cbuuids := []cbgo.UUID{}

s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
}

// Small helper to create a DeviceCharacteristic object.
func (s *DeviceService) makeCharacteristic(uuid UUID, dchar cbgo.Characteristic) DeviceCharacteristic {
func (s DeviceService) makeCharacteristic(uuid UUID, dchar cbgo.Characteristic) DeviceCharacteristic {
char := DeviceCharacteristic{
deviceCharacteristic: &deviceCharacteristic{
uuidWrapper: uuid,
Expand All @@ -163,15 +163,15 @@ type DeviceCharacteristic struct {
type deviceCharacteristic struct {
uuidWrapper

service *DeviceService
service DeviceService

characteristic cbgo.Characteristic
callback func(buf []byte)
readChan chan error
}

// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuidWrapper
}

Expand Down
12 changes: 6 additions & 6 deletions gattc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type DeviceService struct {
}

// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuidWrapper
}

Expand All @@ -41,7 +41,7 @@ func (s *DeviceService) UUID() UUID {
//
// On Linux with BlueZ, this just waits for the ServicesResolved signal (if
// services haven't been resolved yet) and uses this list of cached services.
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
func (d Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
start := time.Now()

for {
Expand Down Expand Up @@ -137,7 +137,7 @@ type DeviceCharacteristic struct {
}

// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuidWrapper
}

Expand All @@ -150,7 +150,7 @@ func (c *DeviceCharacteristic) UUID() UUID {
//
// Passing a nil slice of UUIDs will return a complete
// list of characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
var chars []DeviceCharacteristic
if len(uuids) > 0 {
// The caller wants to get a list of characteristics in a specific
Expand Down Expand Up @@ -235,7 +235,7 @@ func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (n int, err error)
// changes.
//
// Users may call EnableNotifications with a nil callback to disable notifications.
func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error {
func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error {
switch callback {
default:
if c.property != nil {
Expand Down Expand Up @@ -295,7 +295,7 @@ func (c DeviceCharacteristic) GetMTU() (uint16, error) {
}

// Read reads the current characteristic value.
func (c *DeviceCharacteristic) Read(data []byte) (int, error) {
func (c DeviceCharacteristic) Read(data []byte) (int, error) {
options := make(map[string]interface{})
var result []byte
err := c.characteristic.Call("org.bluez.GattCharacteristic1.ReadValue", 0, options).Store(&result)
Expand Down
8 changes: 4 additions & 4 deletions gattc_sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type DeviceService struct {
}

// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuid.UUID()
}

Expand Down Expand Up @@ -167,7 +167,7 @@ type DeviceCharacteristic struct {
}

// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuid.UUID()
}

Expand All @@ -188,7 +188,7 @@ var discoveringCharacteristic struct {
//
// Passing a nil slice of UUIDs will return a complete
// list of characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
if discoveringCharacteristic.handle_value.Get() != 0 {
return nil, errAlreadyDiscovering
}
Expand Down Expand Up @@ -424,7 +424,7 @@ var readingCharacteristic struct {
// Read reads the current characteristic value up to MTU length.
// A future enhancement would be to be able to retrieve a longer
// value by making multiple calls.
func (c *DeviceCharacteristic) Read(data []byte) (n int, err error) {
func (c DeviceCharacteristic) Read(data []byte) (n int, err error) {
// global will copy bytes from read operation into data slice
readingCharacteristic.value = data

Expand Down
14 changes: 7 additions & 7 deletions gattc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type DeviceService struct {
}

// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuidWrapper
}

Expand All @@ -150,7 +150,7 @@ func (s *DeviceService) UUID() UUID {
//
// Passing a nil slice of UUIDs will return a complete
// list of characteristics.
func (s *DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceCharacteristic, error) {
getCharacteristicsOp, err := s.service.GetCharacteristicsWithCacheModeAsync(bluetooth.BluetoothCacheModeUncached)
if err != nil {
return nil, err
Expand Down Expand Up @@ -234,20 +234,20 @@ type DeviceCharacteristic struct {
characteristic *genericattributeprofile.GattCharacteristic
properties genericattributeprofile.GattCharacteristicProperties

service *DeviceService
service DeviceService
}

// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuidWrapper
}

func (c *DeviceCharacteristic) Properties() uint32 {
func (c DeviceCharacteristic) Properties() uint32 {
return uint32(c.properties)
}

// GetMTU returns the MTU for the characteristic.
func (c *DeviceCharacteristic) GetMTU() (uint16, error) {
func (c DeviceCharacteristic) GetMTU() (uint16, error) {
return c.service.device.session.GetMaxPduSize()
}

Expand Down Expand Up @@ -314,7 +314,7 @@ func (c DeviceCharacteristic) write(p []byte, mode genericattributeprofile.GattW
}

// Read reads the current characteristic value.
func (c *DeviceCharacteristic) Read(data []byte) (int, error) {
func (c DeviceCharacteristic) Read(data []byte) (int, error) {
if c.properties&genericattributeprofile.GattCharacteristicPropertiesRead == 0 {
return 0, errNoRead
}
Expand Down

2 comments on commit 5746ccf

@raifrg
Copy link

@raifrg raifrg commented on 5746ccf Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) needs pointer receiver, because the channel c.property created when notifications were enabled (i.e. with non-nil callback) isn't saved in the structure after the method returns and is therefore not available when the same method is used to disable notifications (i.e. with nil callback).

@aykevl
Copy link
Member Author

@aykevl aykevl commented on 5746ccf Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raifrg not necessarily! See for example how this is done on MacOS:

bluetooth/gap_darwin.go

Lines 87 to 103 in a668e1b

type Device struct {
Address Address
*deviceInternal
}
type deviceInternal struct {
delegate *peripheralDelegate
cm cbgo.CentralManager
prph cbgo.Peripheral
servicesChan chan error
charsChan chan error
services map[UUID]DeviceService
}

Please sign in to comment.