Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience finder methods by uuid and handle #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,74 @@ func (p *Profile) Find(target interface{}) interface{} {
return nil
}

func (p *Profile) findWithUUID(uuidVal UUID) interface{} {
for _, s := range p.Services {
if s.UUID.Equal(uuidVal) {
return s
}
for _, c := range s.Characteristics {
if c.UUID.Equal(uuidVal) {
return c
}
for _, d := range c.Descriptors {
if d.UUID.Equal(uuidVal) {
return d
}
}
}
}
return nil
}

func (p *Profile) findWithHandle(handle uint16) interface{} {
for _, s := range p.Services {
if s.Handle == handle {
return s
}
for _, c := range s.Characteristics {
if c.Handle == handle {
return c
}
for _, d := range c.Descriptors {
if d.Handle == handle {
return d
}
}
}
}
return nil
}

// FindBy takes any type that implements the Finder interface and delegates the
// matching algorithm to it to return a *Service, *Characteristic, or *Descriptor.
func (p *Profile) FindBy(f Finder) interface{} {
return f.Find(p)
}

type Finder interface {
Find(p *Profile) interface{}
}

// FindByHandle is a predefined type to quickly search for a matching uint16 handle value.
type FindByHandle uint16

// Finds a Service, Characteristic or Descriptor based on the intrinsic handle value.
// Example:
// result := p.FindBy(FindByHandle(yourUint16Val))
func (h FindByHandle) Find(p *Profile) interface{} {
return p.findWithHandle(uint16(h))
}

// FindByUUID is a predefined type to quickly search for a matching UUID value.
type FindByUUID UUID

// Finds a Service, Characteristic or Descriptor based on the intrinsic UUID value.
// Example:
// result := p.FindBy(FindByUUID(yourUUIDVal))
func (u FindByUUID) Find(p *Profile) interface{} {
return p.findWithUUID(UUID(u))
}

// A Service is a BLE service.
type Service struct {
UUID UUID
Expand Down