| 1 | package gatt |
| 2 | |
| 3 | import "errors" |
| 4 | |
| 5 | var notImplemented = errors.New("not implemented") |
| 6 | |
| 7 | type State int |
| 8 | |
| 9 | const ( |
| 10 | StateUnknown State = 0 |
| 11 | StateResetting State = 1 |
| 12 | StateUnsupported State = 2 |
| 13 | StateUnauthorized State = 3 |
| 14 | StatePoweredOff State = 4 |
| 15 | StatePoweredOn State = 5 |
| 16 | ) |
| 17 | |
| 18 | func (s State) String() string { |
| 19 | str := []string{ |
| 20 | "Unknown", |
| 21 | "Resetting", |
| 22 | "Unsupported", |
| 23 | "Unauthorized", |
| 24 | "PoweredOff", |
| 25 | "PoweredOn", |
| 26 | } |
| 27 | return str[int(s)] |
| 28 | } |
| 29 | |
| 30 | // Device defines the interface for a BLE device. |
| 31 | // Since an interface can't define fields(properties). To implement the |
| 32 | // callback support for cerntain events, deviceHandler is defined and |
| 33 | // implementation of Device on different platforms should embed it in |
| 34 | // order to keep have keep compatible in API level. |
| 35 | // Package users can use the Handler to set these handlers. |
| 36 | type Device interface { |
| 37 | Init(stateChanged func(Device, State)) error |
| 38 | |
| 39 | // Advertise advertise AdvPacket |
| 40 | Advertise(a *AdvPacket) error |
| 41 | |
| 42 | // AdvertiseNameAndServices advertises device name, and specified service UUIDs. |
| 43 | // It tres to fit the UUIDs in the advertising packet as much as possible. |
| 44 | // If name doesn't fit in the advertising packet, it will be put in scan response. |
| 45 | AdvertiseNameAndServices(name string, ss []UUID) error |
| 46 | |
| 47 | // AdvertiseIBeaconData advertise iBeacon with given manufacturer data. |
| 48 | AdvertiseIBeaconData(b []byte) error |
| 49 | |
| 50 | // AdvertisingIbeacon advertises iBeacon with specified parameters. |
| 51 | AdvertiseIBeacon(u UUID, major, minor uint16, pwr int8) error |
| 52 | |
| 53 | // StopAdvertising stops advertising. |
| 54 | StopAdvertising() error |
| 55 | |
| 56 | // RemoveAllServices removes all services that are currently in the database. |
| 57 | RemoveAllServices() error |
| 58 | |
| 59 | // Add Service add a service to database. |
| 60 | AddService(s *Service) error |
| 61 | |
| 62 | // SetServices set the specified service to the database. |
| 63 | // It removes all currently added services, if any. |
| 64 | SetServices(ss []*Service) error |
| 65 | |
| 66 | // Scan discovers surounding remote peripherals that have the Service UUID specified in ss. |
| 67 | // If ss is set to nil, all devices scanned are reported. |
| 68 | // dup specifies weather duplicated advertisement should be reported or not. |
| 69 | // When a remote peripheral is discovered, the PeripheralDiscovered Handler is called. |
| 70 | Scan(ss []UUID, dup bool) |
| 71 | |
| 72 | // StopScanning stops scanning. |
| 73 | StopScanning() |
| 74 | |
| 75 | // Connect connects to a remote peripheral. |
| 76 | Connect(p Peripheral) |
| 77 | |
| 78 | // CancelConnection disconnects a remote peripheral. |
| 79 | CancelConnection(p Peripheral) |
| 80 | |
| 81 | // Handle registers the specified handlers. |
| 82 | Handle(h ...Handler) |
| 83 | |
| 84 | // Option sets the options specified. |
| 85 | Option(o ...Option) error |
| 86 | } |
| 87 | |
| 88 | // deviceHandler is the handlers(callbacks) of the Device. |
| 89 | type deviceHandler struct { |
| 90 | // stateChanged is called when the device states changes. |
| 91 | stateChanged func(d Device, s State) |
| 92 | |
| 93 | // connect is called when a remote central device connects to the device. |
| 94 | centralConnected func(c Central) |
| 95 | |
| 96 | // disconnect is called when a remote central device disconnects to the device. |
| 97 | centralDisconnected func(c Central) |
| 98 | |
| 99 | // peripheralDiscovered is called when a remote peripheral device is found during scan procedure. |
| 100 | peripheralDiscovered func(p Peripheral, a *Advertisement, rssi int) |
| 101 | |
| 102 | // peripheralConnected is called when a remote peripheral is conneted. |
| 103 | peripheralConnected func(p Peripheral, err error) |
| 104 | |
| 105 | // peripheralConnected is called when a remote peripheral is disconneted. |
| 106 | peripheralDisconnected func(p Peripheral, err error) |
| 107 | } |
| 108 | |
| 109 | // A Handler is a self-referential function, which registers the options specified. |
| 110 | // See http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html for more discussion. |
| 111 | type Handler func(Device) |
| 112 | |
| 113 | // Handle registers the specified handlers. |
| 114 | func (d *device) Handle(hh ...Handler) { |
| 115 | for _, h := range hh { |
| 116 | h(d) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // CentralConnected returns a Handler, which sets the specified function to be called when a device connects to the server. |
| 121 | func CentralConnected(f func(Central)) Handler { |
| 122 | return func(d Device) { d.(*device).centralConnected = f } |
| 123 | } |
| 124 | |
| 125 | // CentralDisconnected returns a Handler, which sets the specified function to be called when a device disconnects from the server. |
| 126 | func CentralDisconnected(f func(Central)) Handler { |
| 127 | return func(d Device) { d.(*device).centralDisconnected = f } |
| 128 | } |
| 129 | |
| 130 | // PeripheralDiscovered returns a Handler, which sets the specified function to be called when a remote peripheral device is found during scan procedure. |
| 131 | func PeripheralDiscovered(f func(Peripheral, *Advertisement, int)) Handler { |
| 132 | return func(d Device) { d.(*device).peripheralDiscovered = f } |
| 133 | } |
| 134 | |
| 135 | // PeripheralConnected returns a Handler, which sets the specified function to be called when a remote peripheral device connects. |
| 136 | func PeripheralConnected(f func(Peripheral, error)) Handler { |
| 137 | return func(d Device) { d.(*device).peripheralConnected = f } |
| 138 | } |
| 139 | |
| 140 | // PeripheralDisconnected returns a Handler, which sets the specified function to be called when a remote peripheral device disconnects. |
| 141 | func PeripheralDisconnected(f func(Peripheral, error)) Handler { |
| 142 | return func(d Device) { d.(*device).peripheralDisconnected = f } |
| 143 | } |
| 144 | |
| 145 | // An Option is a self-referential function, which sets the option specified. |
| 146 | // Most Options are platform-specific, which gives more fine-grained control over the device at a cost of losing portibility. |
| 147 | // See http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html for more discussion. |
| 148 | type Option func(Device) error |
| 149 | |
| 150 | // Option sets the options specified. |
| 151 | // Some options can only be set before the device is initialized; they are best used with NewDevice instead of Option. |
| 152 | func (d *device) Option(opts ...Option) error { |
| 153 | var err error |
| 154 | for _, opt := range opts { |
| 155 | err = opt(d) |
| 156 | } |
| 157 | return err |
| 158 | } |