| 1 | package gatt |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "sync" |
| 6 | ) |
| 7 | |
| 8 | // Peripheral is the interface that represent a remote peripheral device. |
| 9 | type Peripheral interface { |
| 10 | // Device returns the underlying device. |
| 11 | Device() Device |
| 12 | |
| 13 | // ID is the platform specific unique ID of the remote peripheral, e.g. MAC for Linux, Peripheral UUID for MacOS. |
| 14 | ID() string |
| 15 | |
| 16 | // Name returns the name of the remote peripheral. |
| 17 | // This can be the advertised name, if exists, or the GAP device name, which takes priority |
| 18 | Name() string |
| 19 | |
| 20 | // Services returnns the services of the remote peripheral which has been discovered. |
| 21 | Services() []*Service |
| 22 | |
| 23 | // DiscoverServices discover the specified services of the remote peripheral. |
| 24 | // If the specified services is set to nil, all the available services of the remote peripheral are returned. |
| 25 | DiscoverServices(s []UUID) ([]*Service, error) |
| 26 | |
| 27 | // DiscoverIncludedServices discovers the specified included services of a service. |
| 28 | // If the specified services is set to nil, all the included services of the service are returned. |
| 29 | DiscoverIncludedServices(ss []UUID, s *Service) ([]*Service, error) |
| 30 | |
| 31 | // DiscoverCharacteristics discovers the specified characteristics of a service. |
| 32 | // If the specified characterstics is set to nil, all the characteristic of the service are returned. |
| 33 | DiscoverCharacteristics(c []UUID, s *Service) ([]*Characteristic, error) |
| 34 | |
| 35 | // DiscoverDescriptors discovers the descriptors of a characteristic. |
| 36 | // If the specified descriptors is set to nil, all the descriptors of the characteristic are returned. |
| 37 | DiscoverDescriptors(d []UUID, c *Characteristic) ([]*Descriptor, error) |
| 38 | |
| 39 | // ReadCharacteristic retrieves the value of a specified characteristic. |
| 40 | ReadCharacteristic(c *Characteristic) ([]byte, error) |
| 41 | |
| 42 | // ReadLongCharacteristic retrieves the value of a specified characteristic that is longer than the |
| 43 | // MTU. |
| 44 | ReadLongCharacteristic(c *Characteristic) ([]byte, error) |
| 45 | |
| 46 | // ReadDescriptor retrieves the value of a specified characteristic descriptor. |
| 47 | ReadDescriptor(d *Descriptor) ([]byte, error) |
| 48 | |
| 49 | // WriteCharacteristic writes the value of a characteristic. |
| 50 | WriteCharacteristic(c *Characteristic, b []byte, noRsp bool) error |
| 51 | |
| 52 | // WriteDescriptor writes the value of a characteristic descriptor. |
| 53 | WriteDescriptor(d *Descriptor, b []byte) error |
| 54 | |
| 55 | // SetNotifyValue sets notifications for the value of a specified characteristic. |
| 56 | SetNotifyValue(c *Characteristic, f func(*Characteristic, []byte, error)) error |
| 57 | |
| 58 | // SetIndicateValue sets indications for the value of a specified characteristic. |
| 59 | SetIndicateValue(c *Characteristic, f func(*Characteristic, []byte, error)) error |
| 60 | |
| 61 | // ReadRSSI retrieves the current RSSI value for the remote peripheral. |
| 62 | ReadRSSI() int |
| 63 | |
| 64 | // SetMTU sets the mtu for the remote peripheral. |
| 65 | SetMTU(mtu uint16) error |
| 66 | } |
| 67 | |
| 68 | type subscriber struct { |
| 69 | sub map[uint16]subscribefn |
| 70 | mu *sync.Mutex |
| 71 | } |
| 72 | |
| 73 | type subscribefn func([]byte, error) |
| 74 | |
| 75 | func newSubscriber() *subscriber { |
| 76 | return &subscriber{ |
| 77 | sub: make(map[uint16]subscribefn), |
| 78 | mu: &sync.Mutex{}, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (s *subscriber) subscribe(h uint16, f subscribefn) { |
| 83 | s.mu.Lock() |
| 84 | s.sub[h] = f |
| 85 | s.mu.Unlock() |
| 86 | } |
| 87 | |
| 88 | func (s *subscriber) unsubscribe(h uint16) { |
| 89 | s.mu.Lock() |
| 90 | delete(s.sub, h) |
| 91 | s.mu.Unlock() |
| 92 | } |
| 93 | |
| 94 | func (s *subscriber) fn(h uint16) subscribefn { |
| 95 | s.mu.Lock() |
| 96 | defer s.mu.Unlock() |
| 97 | return s.sub[h] |
| 98 | } |
| 99 | |
| 100 | var ( |
| 101 | ErrInvalidLength = errors.New("invalid length") |
| 102 | ) |