currantlabs / ble

BSD 3-Clause "New" or "Revised" License
210 stars 177 forks source link

Add convenience finder methods by uuid and handle #68

Open silviucm opened 6 years ago

silviucm commented 6 years ago

This pull request adds several convenience methods for identifying services, characteristics or descriptors, based on their uuid or handle, in addition to the original Find method.

// FindWithUUID searches discovered profile for the specified UUID.
// The target must has the type of *Service, *Characteristic, or *Descriptor.
func (p *Profile) FindWithUUID(uuidVal UUID) interface{}

// FindByHandle searches discovered profile for the specified target's type and handle.
// The target must has the type of *Service, *Characteristic, or *Descriptor.
// It also must have the Handle property set to the the right value.
//
// Example:
//  dummyCharacteristic := ble.NewCharacteristic((ble.UUID)(nil))
//  dummyCharacteristic.Handle = curr.handle
//  if u := curr.profile.FindByHandle(dummyCharacteristic); u != nil {
//  ...
//  }
func (p *Profile) FindByHandle(target interface{}) interface{}

// FindWithHandle searches the discovered profile for the item the matches
// the given handle, in the following order: services, characteristics, descriptors.
//
// Example:
//  if u := curr.profile.FindWithHandle(1024); u != nil {
//  ...
//  }
func (p *Profile) FindWithHandle(handle uint16) interface{}

// FindServiceWithHandle searches discovered profile to find any matching *Service
// for the specified handle. It returns nil if none found.
func (p *Profile) FindServiceWithHandle(handle uint16) *Service
{ ... }

// FindCharacteristicWithHandle searches discovered profile to find any
// matching *Characteristic for the specified handle. It returns nil if none found.
func (p *Profile) FindCharacteristicWithHandle(handle uint16) *Characteristic
{ ... }

// FindDescriptorWithHandle searches discovered profile to find any
// matching *Descriptor for the specified handle. It returns nil if none found.
func (p *Profile) FindDescriptorWithHandle(handle uint16) *Descriptor
{ ... }

// FindWithHandleStr searches the discovered profile for the item whose handle
// matches the handleHexOrDec string value, with an optional base specified.
//
// When handleHexOrDec is converted to uint16, and is prefixed by "0x", it is
// assumed to be in base 16; otherwise, it is assumed to be in base 10, unless the
// optional base argument is explicitly passed.
// The search is performedin the following order: services, characteristics, descriptors.
//
// Example using a hexadecimal value
//  if u, err := curr.profile.FindWithHandleStr("0x10B4"); u != nil {
//  ...
//  }
// Example using a decimal value
//  if u, err := curr.profile.FindWithHandleStr("2048"); u != nil {
//  ...
//  }
// Example using a decimal value with explicit base 10
//  if u, err := curr.profile.FindWithHandleStr("745", 10); u != nil {
//  ...
//  }
func (p *Profile) FindWithHandleStr(handleStr string, base ...int) (r interface{}, err error)
silviucm commented 6 years ago

Hi @roylee17 - when you have a chance could you review this ? if approved, I would go on and fix the write part of the blesh tool

roylee17 commented 6 years ago

@silviucm sorry for the late response.

Thanks for the input. The implementation looks good to me if these are internal utility functions. But, since these broaden the API too much, do you want to take on another direction?

Something like how Sort package works.

  1. Define the finder interface
  2. Add just one API to take the finder as input
  3. Add few common/default finders so user can grab and use, or define their own criteria.

// (details or naming are left out)

type interface xxFinder {
  Find( XXX ) interface {}
}
type ByHandle struct { }

func (f *ByHandle) Find ( XXX ) interface{} {
}

type ByUUID struct { }

func (f *ByUUID) Find ( XXX ) interface{} {
}
func FindBy( f xxFinder) interface {} {

   // Traverse svcs, chars, descs, ... and see if f apply

}
silviucm commented 6 years ago

@roylee17 fair enough. Changes updated

roylee17 commented 6 years ago

LGTM, thanks.

silviucm commented 6 years ago

@roylee17 if you could approve this merge at your convenience, I could get going on the second part, fixing the blesh tool write part + a bit of documentation for it

thanks

roylee17 commented 6 years ago

ping @hasty