2021-08-19 19:41:09 -05:00
package infinitime
import (
"bytes"
2022-05-11 15:22:57 -05:00
"context"
2021-08-19 19:41:09 -05:00
"encoding/binary"
"errors"
2021-12-12 14:43:43 -06:00
"reflect"
2021-10-26 23:42:41 -05:00
"strings"
2021-08-19 19:41:09 -05:00
"time"
2021-12-12 14:43:43 -06:00
"github.com/fxamacker/cbor/v2"
2021-08-19 19:41:09 -05:00
bt "github.com/muka/go-bluetooth/api"
2022-02-21 04:46:20 -06:00
"github.com/muka/go-bluetooth/bluez"
2021-08-19 19:41:09 -05:00
"github.com/muka/go-bluetooth/bluez/profile/adapter"
"github.com/muka/go-bluetooth/bluez/profile/device"
"github.com/muka/go-bluetooth/bluez/profile/gatt"
2023-04-20 21:53:34 -05:00
"go.elara.ws/infinitime/blefs"
"go.elara.ws/logger"
2021-08-19 19:41:09 -05:00
)
2022-04-23 21:58:00 -05:00
// This global is used to store the logger.
// log.Logger is not used as it would interfere
// with the package importing the library
2023-01-04 17:00:15 -06:00
var log logger . Logger
2022-04-23 21:58:00 -05:00
2021-08-19 19:41:09 -05:00
const BTName = "InfiniTime"
const (
NewAlertChar = "00002a46-0000-1000-8000-00805f9b34fb"
2021-10-15 02:23:54 -05:00
NotifEventChar = "00020001-78fc-48fe-8e23-433b3a1942d0"
2021-10-22 14:59:51 -05:00
StepCountChar = "00030001-78fc-48fe-8e23-433b3a1942d0"
MotionValChar = "00030002-78fc-48fe-8e23-433b3a1942d0"
2021-08-19 19:41:09 -05:00
FirmwareVerChar = "00002a26-0000-1000-8000-00805f9b34fb"
CurrentTimeChar = "00002a2b-0000-1000-8000-00805f9b34fb"
2022-11-21 10:51:49 -06:00
LocalTimeChar = "00002a0f-0000-1000-8000-00805f9b34fb"
2021-08-19 19:41:09 -05:00
BatteryLvlChar = "00002a19-0000-1000-8000-00805f9b34fb"
HeartRateChar = "00002a37-0000-1000-8000-00805f9b34fb"
2021-11-22 23:19:30 -06:00
FSTransferChar = "adaf0200-4669-6c65-5472-616e73666572"
FSVersionChar = "adaf0100-4669-6c65-5472-616e73666572"
2021-12-12 14:43:43 -06:00
WeatherDataChar = "00040001-78fc-48fe-8e23-433b3a1942d0"
2021-08-19 19:41:09 -05:00
)
2022-05-11 15:22:57 -05:00
var charNames = map [ string ] string {
2022-11-05 22:33:30 -05:00
NewAlertChar : "New Alert" ,
NotifEventChar : "Notification Event" ,
StepCountChar : "Step Count" ,
MotionValChar : "Motion Values" ,
FirmwareVerChar : "Firmware Version" ,
CurrentTimeChar : "Current Time" ,
2022-11-21 10:51:49 -06:00
LocalTimeChar : "Local Time" ,
2022-11-05 22:33:30 -05:00
BatteryLvlChar : "Battery Level" ,
HeartRateChar : "Heart Rate" ,
FSTransferChar : "Filesystem Transfer" ,
FSVersionChar : "Filesystem Version" ,
WeatherDataChar : "Weather Data" ,
NavFlagsChar : "Navigation Icon" ,
NavNarrativeChar : "Navigation Instruction" ,
NavManDistChar : "Navigation Distance to next event" ,
NavProgressChar : "Navigation Progress" ,
2022-05-11 15:22:57 -05:00
}
2021-08-19 19:41:09 -05:00
type Device struct {
device * device . Device1
newAlertChar * gatt . GattCharacteristic1
2021-10-15 02:23:54 -05:00
notifEventChar * gatt . GattCharacteristic1
2021-10-22 14:59:51 -05:00
stepCountChar * gatt . GattCharacteristic1
motionValChar * gatt . GattCharacteristic1
2021-08-19 19:41:09 -05:00
fwVersionChar * gatt . GattCharacteristic1
currentTimeChar * gatt . GattCharacteristic1
2022-11-21 10:51:49 -06:00
localTimeChar * gatt . GattCharacteristic1
2021-08-19 19:41:09 -05:00
battLevelChar * gatt . GattCharacteristic1
heartRateChar * gatt . GattCharacteristic1
2021-11-22 23:19:30 -06:00
fsVersionChar * gatt . GattCharacteristic1
fsTransferChar * gatt . GattCharacteristic1
2021-12-12 14:43:43 -06:00
weatherDataChar * gatt . GattCharacteristic1
Added Navigation service (#5)
InfiniTime implements a [Navigation Service](https://github.com/InfiniTimeOrg/InfiniTime/blob/develop/doc/NavigationService.md). This pull request will add it to the go library by defining a function
```go
func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error {
...
}
```
From the InfiniTime manual
* `flag`: the graphic instruction as provided by [Pure Maps](https://github.com/rinigus/pure-maps/tree/master/qml/icons/navigation). A list of valid instruction icons can be found [here](https://github.com/rinigus/pure-maps/tree/master/qml/icons/navigation)
* `narrative`: the instruction in words, eg. "At the roundabout take the first exit".
* `dist`: a short string describing the distance to the upcoming instruction such as "50 m".
* `progress`: the percent complete in a `uint8`
Adding this to the `itd` daemon is straightforward
```patch
diff --git a/api/types.go b/api/types.go
index 281a85b..14c84de 100644
--- a/api/types.go
+++ b/api/types.go
@@ -22,6 +22,13 @@ type FwUpgradeData struct {
Files []string
}
+type NavigationData struct {
+ Flag string
+ Narrative string
+ Dist string
+ Progress uint8
+}
+
type NotifyData struct {
Title string
Body string
diff --git a/socket.go b/socket.go
index 6fcba5c..91b37c0 100644
--- a/socket.go
+++ b/socket.go
@@ -204,6 +204,10 @@ func (i *ITD) Address(_ *server.Context) string {
return i.dev.Address()
}
+func (i *ITD) Navigation(_ *server.Context, data api.NavigationData) error {
+ return i.dev.Navigation(data.Flag, data.Narrative, data.Dist, data.Progress)
+}
+
func (i *ITD) Notify(_ *server.Context, data api.NotifyData) error {
return i.dev.Notify(data.Title, data.Body)
}
```
Co-authored-by: Yannick Ulrich <yannick.ulrich@durham.ac.uk>
Reviewed-on: https://gitea.arsenm.dev/Arsen6331/infinitime/pulls/5
Co-authored-by: yannickulrich <yannick.ulrich@protonmail.com>
Co-committed-by: yannickulrich <yannick.ulrich@protonmail.com>
2022-11-03 14:09:06 -05:00
weatherdataChar * gatt . GattCharacteristic1
2021-11-25 14:39:43 -06:00
notifEventCh chan uint8
notifEventDone bool
2021-08-19 19:41:09 -05:00
Music MusicCtrl
2022-11-05 22:33:30 -05:00
Navigation NavigationService
2021-08-19 19:41:09 -05:00
DFU DFU
}
2021-11-25 14:39:43 -06:00
var (
2021-12-12 14:43:43 -06:00
ErrNoDevices = errors . New ( "no InfiniTime devices found" )
ErrNotFound = errors . New ( "could not find any advertising InfiniTime devices" )
ErrNotConnected = errors . New ( "not connected" )
ErrNoTimelineHeader = errors . New ( "events must contain the timeline header" )
2021-12-16 23:30:29 -06:00
ErrPairTimeout = errors . New ( "reached timeout while pairing" )
2021-11-25 14:39:43 -06:00
)
2021-08-19 19:41:09 -05:00
2022-05-11 15:22:57 -05:00
type ErrCharNotAvail struct {
uuid string
}
func ( e ErrCharNotAvail ) Error ( ) string {
return "characteristic " + e . uuid + " (" + charNames [ e . uuid ] + ") not available"
}
2021-08-19 19:41:09 -05:00
type Options struct {
AttemptReconnect bool
2021-10-15 02:23:54 -05:00
WhitelistEnabled bool
Whitelist [ ] string
2021-12-16 23:30:29 -06:00
OnReqPasskey func ( ) ( uint32 , error )
2022-02-21 04:46:20 -06:00
OnReconnect func ( )
2023-01-04 17:00:15 -06:00
Logger logger . Logger
LogLevel logger . LogLevel
2021-08-19 19:41:09 -05:00
}
var DefaultOptions = & Options {
AttemptReconnect : true ,
2021-10-15 02:23:54 -05:00
WhitelistEnabled : false ,
2023-01-04 17:00:15 -06:00
Logger : logger . NewNop ( ) ,
2021-08-19 19:41:09 -05:00
}
// Connect will attempt to connect to a
// paired InfiniTime device. If none are paired,
// it will attempt to discover and pair one.
//
// It will also attempt to reconnect to the device
2022-02-21 04:46:20 -06:00
// if it disconnects and that is enabled in the options.
2022-05-11 15:22:57 -05:00
func Connect ( ctx context . Context , opts * Options ) ( * Device , error ) {
2021-08-19 19:41:09 -05:00
if opts == nil {
opts = DefaultOptions
}
2022-02-21 04:46:20 -06:00
2023-01-04 17:00:15 -06:00
log = opts . Logger
opts . Logger . SetLevel ( opts . LogLevel )
2022-04-23 21:07:41 -05:00
2022-02-21 04:46:20 -06:00
// Set passkey request callback
2021-12-16 23:30:29 -06:00
setOnPasskeyReq ( opts . OnReqPasskey )
2022-02-21 04:46:20 -06:00
// Connect to bluetooth device
2022-05-11 15:22:57 -05:00
btDev , err := connect ( ctx , opts , true )
2021-08-19 19:41:09 -05:00
if err != nil {
return nil , err
}
2022-02-21 04:46:20 -06:00
// Create new device
out := & Device { device : btDev }
2022-11-06 22:08:13 -06:00
out . Navigation = NavigationService { dev : out }
2022-02-21 04:46:20 -06:00
// Resolve characteristics
err = out . resolveChars ( )
if err != nil {
return nil , err
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
return out , nil
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
// connect connects to the InfiniTime bluez device
2022-05-11 15:22:57 -05:00
func connect ( ctx context . Context , opts * Options , first bool ) ( dev * device . Device1 , err error ) {
2022-02-21 04:46:20 -06:00
// Get devices
2021-08-19 19:41:09 -05:00
devs , err := defaultAdapter . GetDevices ( )
if err != nil {
return nil , err
}
2022-02-21 04:46:20 -06:00
2021-08-19 19:41:09 -05:00
// For every device
2022-02-21 04:46:20 -06:00
for _ , listDev := range devs {
// If device name does not match, skip
if listDev . Properties . Name != BTName {
continue
}
// If whitelist enabled and doesn't contain
// device, skip
if opts . WhitelistEnabled &&
! contains ( opts . Whitelist , listDev . Properties . Address ) {
2023-01-04 17:00:15 -06:00
log . Debug ( "InfiniTime device skipped as it is not in whitelist" ) .
2022-04-23 21:58:00 -05:00
Str ( "mac" , listDev . Properties . Address ) .
2023-01-04 17:00:15 -06:00
Send ( )
2022-02-21 04:46:20 -06:00
continue
}
// Set device
dev = listDev
2022-04-23 21:58:00 -05:00
2023-01-04 17:00:15 -06:00
log . Debug ( "InfiniTime device found in list" ) .
2022-04-23 21:58:00 -05:00
Str ( "mac" , dev . Properties . Address ) .
2023-01-04 17:00:15 -06:00
Send ( )
2022-04-23 21:58:00 -05:00
2022-02-21 04:46:20 -06:00
break
}
// If device not set
if dev == nil {
2023-01-04 17:00:15 -06:00
log . Debug ( "No device found in list, attempting to discover" ) . Send ( )
2022-02-21 04:46:20 -06:00
// Discover devices on adapter
discoverCh , cancel , err := bt . Discover ( defaultAdapter , & adapter . DiscoveryFilter { Transport : "le" } )
if err != nil {
return nil , err
}
2022-05-11 15:22:57 -05:00
discoverLoop :
for {
select {
case event := <- discoverCh :
// If event type is not device added, skip
if event . Type != adapter . DeviceAdded {
continue
}
2022-02-21 04:46:20 -06:00
2022-05-11 15:22:57 -05:00
// Create new device from event path
discovered , err := device . NewDevice1 ( event . Path )
if err != nil {
return nil , err
}
2022-02-21 04:46:20 -06:00
2022-05-11 15:22:57 -05:00
// If device name does not match, skip
if discovered . Properties . Name != BTName {
continue
}
// If whitelist enabled and doesn't contain
// device, skip
if opts . WhitelistEnabled &&
! contains ( opts . Whitelist , discovered . Properties . Address ) {
2023-01-04 17:00:15 -06:00
log . Debug ( "Discovered InfiniTime device skipped as it is not in whitelist" ) .
2022-05-11 15:22:57 -05:00
Str ( "mac" , discovered . Properties . Address ) .
2023-01-04 17:00:15 -06:00
Send ( )
2022-05-11 15:22:57 -05:00
continue
}
2022-02-21 04:46:20 -06:00
2022-05-11 15:22:57 -05:00
// Set device
dev = discovered
2022-04-23 21:58:00 -05:00
2023-01-04 17:00:15 -06:00
log . Debug ( "InfiniTime device discovered" ) .
2022-05-11 15:22:57 -05:00
Str ( "mac" , dev . Properties . Address ) .
2023-01-04 17:00:15 -06:00
Send ( )
2022-05-11 15:22:57 -05:00
break discoverLoop
case <- ctx . Done ( ) :
break discoverLoop
}
2021-08-19 19:41:09 -05:00
}
2022-05-11 15:22:57 -05:00
// Cancel discovery
2022-02-21 04:46:20 -06:00
cancel ( )
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
// If device is still not set, return error
if dev == nil {
2021-08-19 19:41:09 -05:00
return nil , ErrNoDevices
}
2022-02-21 04:46:20 -06:00
// Create variable to track if reconnect
// was required
reconnRequired := false
// If device is not connected
if ! dev . Properties . Connected {
2023-01-04 17:00:15 -06:00
log . Debug ( "Device not connected, connecting" ) . Send ( )
2022-02-21 04:46:20 -06:00
// Connect to device
err = dev . Connect ( )
if err != nil {
return nil , err
}
// Set reconnect required to true
reconnRequired = true
2021-08-21 22:25:09 -05:00
}
2021-11-25 22:33:06 -06:00
2022-02-21 04:46:20 -06:00
// If device is not paired
if ! dev . Properties . Paired {
2023-01-04 17:00:15 -06:00
log . Debug ( "Device not paired, pairing" ) . Send ( )
2022-02-21 04:46:20 -06:00
// Pair device
err = dev . Pair ( )
if err != nil {
return nil , err
}
}
2021-11-25 22:33:06 -06:00
2022-02-21 04:46:20 -06:00
// If this is the first connection and reconnect
// is enabled, start reconnect goroutine
if first && opts . AttemptReconnect {
2022-05-11 15:22:57 -05:00
go reconnect ( ctx , opts , dev )
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
// If this is not the first connection, a reonnect
// was required, and the OnReconnect callback exists,
// run it
if ! first && reconnRequired && opts . OnReconnect != nil {
2023-01-04 17:00:15 -06:00
log . Debug ( "Reconnected to device, running OnReconnect callback" ) . Send ( )
2022-02-21 04:46:20 -06:00
opts . OnReconnect ( )
2021-10-15 02:23:54 -05:00
}
2022-02-21 04:46:20 -06:00
return dev , nil
2021-10-15 02:23:54 -05:00
}
2022-02-21 04:46:20 -06:00
// reconnect reconnects to a device if it disconnects
2022-05-11 15:22:57 -05:00
func reconnect ( ctx context . Context , opts * Options , dev * device . Device1 ) {
2022-02-21 04:46:20 -06:00
// Watch device properties
propCh := watchProps ( dev )
// Create variables to store time of last disconnect
// and amount of diconnects
lastDisconnect := time . Unix ( 0 , 0 )
amtDisconnects := 0
for event := range propCh {
// If event name is not Connected and value is not false, skip
if event . Name != "Connected" && event . Value != false {
2021-08-22 15:12:16 -05:00
continue
}
2022-02-21 04:46:20 -06:00
// Store seconds since last disconnect
secsSince := time . Since ( lastDisconnect ) . Seconds ( )
// If over 3 seconds have passed, reset disconnect count
if secsSince > 3 {
amtDisconnects = 0
2021-08-22 15:12:16 -05:00
}
2022-02-21 04:46:20 -06:00
2022-04-16 06:24:21 -05:00
// If less than 3 seconds have passed and more than 6
2022-02-21 04:46:20 -06:00
// disconnects have occurred, remove the device and reset
if secsSince <= 3 && amtDisconnects >= 6 {
2023-01-04 17:00:15 -06:00
opts . Logger . Warn ( "At least 6 disconnects have occurred in the last three seconds. If this continues, try removing the InfiniTime device from bluetooth." ) . Send ( )
2022-02-21 04:46:20 -06:00
lastDisconnect = time . Unix ( 0 , 0 )
amtDisconnects = 0
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
// Set disconnect variables
lastDisconnect = time . Now ( )
amtDisconnects ++
2021-08-19 19:41:09 -05:00
2022-02-21 04:46:20 -06:00
for i := 0 ; i < 6 ; i ++ {
// If three tries failed, remove device
if i == 3 {
2023-01-04 17:00:15 -06:00
opts . Logger . Warn ( "Multiple connection attempts have failed. If this continues, try removing the InfiniTime device from bluetooth." ) . Send ( )
2022-02-21 04:46:20 -06:00
}
// Connect to device
2022-05-11 15:22:57 -05:00
newDev , err := connect ( ctx , opts , false )
2022-02-21 04:46:20 -06:00
if err != nil {
time . Sleep ( time . Second )
continue
}
// Replace device with new device
* dev = * newDev
2021-08-19 19:41:09 -05:00
2022-02-21 04:46:20 -06:00
break
}
2021-12-16 23:30:29 -06:00
}
2022-02-21 04:46:20 -06:00
}
2021-11-25 22:33:06 -06:00
2022-02-21 04:46:20 -06:00
// bufferChannel writes all events on propCh to a new, buffered channel
func bufferChannel ( propCh chan * bluez . PropertyChanged ) <- chan * bluez . PropertyChanged {
out := make ( chan * bluez . PropertyChanged , 10 )
go func ( ) {
for event := range propCh {
out <- event
}
} ( )
return out
}
2021-08-19 19:41:09 -05:00
2022-02-21 04:46:20 -06:00
// watchProps returns a buffered channel for the device properties
func watchProps ( dev * device . Device1 ) <- chan * bluez . PropertyChanged {
uPropCh , err := dev . WatchProperties ( )
2021-08-19 19:41:09 -05:00
if err != nil {
2022-02-21 04:46:20 -06:00
panic ( err )
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
return bufferChannel ( uPropCh )
2021-08-19 19:41:09 -05:00
}
2021-12-16 23:30:29 -06:00
// setOnPasskeyReq sets the callback for a passkey request.
// It ensures the function will never be nil.
func setOnPasskeyReq ( onReqPasskey func ( ) ( uint32 , error ) ) {
itdAgent . ReqPasskey = onReqPasskey
if itdAgent . ReqPasskey == nil {
itdAgent . ReqPasskey = func ( ) ( uint32 , error ) {
return 0 , nil
}
2021-08-19 19:41:09 -05:00
}
2021-12-16 23:30:29 -06:00
}
2021-08-19 19:41:09 -05:00
2022-02-21 04:46:20 -06:00
// contains checks if s is contained within ss
func contains ( ss [ ] string , s string ) bool {
for _ , str := range ss {
if strings . EqualFold ( str , s ) {
return true
2021-12-16 23:30:29 -06:00
}
2021-08-19 19:41:09 -05:00
}
2022-02-21 04:46:20 -06:00
return false
2021-08-19 19:41:09 -05:00
}
// resolveChars attempts to set all required
// characteristics in an InfiniTime struct
func ( i * Device ) resolveChars ( ) error {
// Get device characteristics
chars , err := i . device . GetCharacteristics ( )
if err != nil {
return err
}
// While no characteristics found
for len ( chars ) == 0 {
// Sleep one second
time . Sleep ( time . Second )
// Attempt to retry getting characteristics
chars , err = i . device . GetCharacteristics ( )
if err != nil {
return err
}
}
// For every discovered characteristics
for _ , char := range chars {
2022-04-23 21:58:00 -05:00
charResolved := true
2021-08-19 19:41:09 -05:00
// Set correct characteristics
switch char . Properties . UUID {
Added Navigation service (#5)
InfiniTime implements a [Navigation Service](https://github.com/InfiniTimeOrg/InfiniTime/blob/develop/doc/NavigationService.md). This pull request will add it to the go library by defining a function
```go
func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error {
...
}
```
From the InfiniTime manual
* `flag`: the graphic instruction as provided by [Pure Maps](https://github.com/rinigus/pure-maps/tree/master/qml/icons/navigation). A list of valid instruction icons can be found [here](https://github.com/rinigus/pure-maps/tree/master/qml/icons/navigation)
* `narrative`: the instruction in words, eg. "At the roundabout take the first exit".
* `dist`: a short string describing the distance to the upcoming instruction such as "50 m".
* `progress`: the percent complete in a `uint8`
Adding this to the `itd` daemon is straightforward
```patch
diff --git a/api/types.go b/api/types.go
index 281a85b..14c84de 100644
--- a/api/types.go
+++ b/api/types.go
@@ -22,6 +22,13 @@ type FwUpgradeData struct {
Files []string
}
+type NavigationData struct {
+ Flag string
+ Narrative string
+ Dist string
+ Progress uint8
+}
+
type NotifyData struct {
Title string
Body string
diff --git a/socket.go b/socket.go
index 6fcba5c..91b37c0 100644
--- a/socket.go
+++ b/socket.go
@@ -204,6 +204,10 @@ func (i *ITD) Address(_ *server.Context) string {
return i.dev.Address()
}
+func (i *ITD) Navigation(_ *server.Context, data api.NavigationData) error {
+ return i.dev.Navigation(data.Flag, data.Narrative, data.Dist, data.Progress)
+}
+
func (i *ITD) Notify(_ *server.Context, data api.NotifyData) error {
return i.dev.Notify(data.Title, data.Body)
}
```
Co-authored-by: Yannick Ulrich <yannick.ulrich@durham.ac.uk>
Reviewed-on: https://gitea.arsenm.dev/Arsen6331/infinitime/pulls/5
Co-authored-by: yannickulrich <yannick.ulrich@protonmail.com>
Co-committed-by: yannickulrich <yannick.ulrich@protonmail.com>
2022-11-03 14:09:06 -05:00
case NavFlagsChar :
i . Navigation . flagsChar = char
case NavNarrativeChar :
i . Navigation . narrativeChar = char
case NavManDistChar :
i . Navigation . mandistChar = char
case NavProgressChar :
i . Navigation . progressChar = char
2021-08-19 19:41:09 -05:00
case NewAlertChar :
i . newAlertChar = char
2021-10-15 02:23:54 -05:00
case NotifEventChar :
i . notifEventChar = char
2021-10-22 14:59:51 -05:00
case StepCountChar :
i . stepCountChar = char
case MotionValChar :
i . motionValChar = char
2021-08-19 19:41:09 -05:00
case FirmwareVerChar :
i . fwVersionChar = char
case CurrentTimeChar :
i . currentTimeChar = char
2022-11-21 10:51:49 -06:00
case LocalTimeChar :
i . localTimeChar = char
2021-08-19 19:41:09 -05:00
case BatteryLvlChar :
i . battLevelChar = char
case HeartRateChar :
i . heartRateChar = char
case MusicEventChar :
i . Music . eventChar = char
case MusicStatusChar :
i . Music . statusChar = char
case MusicArtistChar :
i . Music . artistChar = char
case MusicTrackChar :
i . Music . trackChar = char
case MusicAlbumChar :
i . Music . albumChar = char
case DFUCtrlPointChar :
i . DFU . ctrlPointChar = char
case DFUPacketChar :
i . DFU . packetChar = char
2021-11-22 23:19:30 -06:00
case FSTransferChar :
i . fsTransferChar = char
case FSVersionChar :
i . fsVersionChar = char
2021-12-12 14:43:43 -06:00
case WeatherDataChar :
i . weatherDataChar = char
2022-04-23 21:58:00 -05:00
default :
charResolved = false
}
if charResolved {
2023-01-04 17:00:15 -06:00
log . Debug ( "Resolved characteristic" ) .
2022-04-23 21:58:00 -05:00
Str ( "uuid" , char . Properties . UUID ) .
2022-05-11 15:22:57 -05:00
Str ( "name" , charNames [ char . Properties . UUID ] ) .
2023-01-04 17:00:15 -06:00
Send ( )
2021-08-19 19:41:09 -05:00
}
}
return nil
}
// Address returns the InfiniTime's bluetooth address
func ( i * Device ) Address ( ) string {
return i . device . Properties . Address
}
// Version returns InfiniTime's reported firmware version string
func ( i * Device ) Version ( ) ( string , error ) {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . fwVersionChar , FirmwareVerChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return "" , err
2021-08-19 19:41:09 -05:00
}
ver , err := i . fwVersionChar . ReadValue ( nil )
return string ( ver ) , err
}
// BatteryLevel gets the watch's battery level via the Battery Service
func ( i * Device ) BatteryLevel ( ) ( uint8 , error ) {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . battLevelChar , BatteryLvlChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return 0 , err
2021-08-19 19:41:09 -05:00
}
battLevel , err := i . battLevelChar . ReadValue ( nil )
if err != nil {
return 0 , err
}
return uint8 ( battLevel [ 0 ] ) , nil
}
2021-10-22 14:59:51 -05:00
func ( i * Device ) StepCount ( ) ( uint32 , error ) {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . stepCountChar , StepCountChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return 0 , err
2021-10-22 14:59:51 -05:00
}
stepCountData , err := i . stepCountChar . ReadValue ( nil )
if err != nil {
return 0 , err
}
return binary . LittleEndian . Uint32 ( stepCountData ) , nil
}
type MotionValues struct {
X int16
Y int16
Z int16
}
func ( i * Device ) Motion ( ) ( MotionValues , error ) {
out := MotionValues { }
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . motionValChar , MotionValChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return out , err
2021-10-22 14:59:51 -05:00
}
motionVals , err := i . motionValChar . ReadValue ( nil )
if err != nil {
return out , nil
}
motionValReader := bytes . NewReader ( motionVals )
err = binary . Read ( motionValReader , binary . LittleEndian , & out )
if err != nil {
return out , err
}
return out , nil
}
2021-08-19 19:41:09 -05:00
func ( i * Device ) HeartRate ( ) ( uint8 , error ) {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . heartRateChar , HeartRateChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return 0 , err
2021-08-19 19:41:09 -05:00
}
heartRate , err := i . heartRateChar . ReadValue ( nil )
if err != nil {
return 0 , err
}
return uint8 ( heartRate [ 1 ] ) , nil
}
2022-05-11 15:22:57 -05:00
func ( i * Device ) WatchHeartRate ( ctx context . Context ) ( <- chan uint8 , error ) {
if err := i . checkStatus ( i . heartRateChar , HeartRateChar ) ; err != nil {
return nil , err
2021-08-19 19:41:09 -05:00
}
// Start notifications on heart rate characteristic
err := i . heartRateChar . StartNotify ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-08-19 19:41:09 -05:00
}
// Watch characteristics of heart rate characteristic
ch , err := i . heartRateChar . WatchProperties ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-08-19 19:41:09 -05:00
}
out := make ( chan uint8 , 2 )
2021-08-25 00:17:34 -05:00
currentHeartRate , err := i . HeartRate ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-08-25 00:17:34 -05:00
}
out <- currentHeartRate
2021-08-19 19:41:09 -05:00
go func ( ) {
// For every event
2022-04-23 20:40:25 -05:00
for {
2021-10-22 23:26:33 -05:00
select {
2022-05-11 15:22:57 -05:00
case <- ctx . Done ( ) :
2023-01-04 17:00:15 -06:00
log . Debug ( "Received done signal" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2021-10-22 23:26:33 -05:00
close ( out )
i . heartRateChar . StopNotify ( )
return
2022-04-23 20:40:25 -05:00
case event := <- ch :
2021-10-22 23:26:33 -05:00
// If value changed
if event . Name == "Value" {
// Send heart rate to channel
out <- uint8 ( event . Value . ( [ ] byte ) [ 1 ] )
2022-04-23 20:40:25 -05:00
} else if event . Name == "Notifying" && ! event . Value . ( bool ) {
2023-01-04 17:00:15 -06:00
log . Debug ( "Notifications stopped, restarting" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2022-04-23 20:40:25 -05:00
i . heartRateChar . StartNotify ( )
2021-10-22 23:26:33 -05:00
}
2021-08-19 19:41:09 -05:00
}
}
} ( )
2022-05-11 15:22:57 -05:00
return out , nil
2021-08-19 19:41:09 -05:00
}
2022-05-11 15:22:57 -05:00
func ( i * Device ) WatchBatteryLevel ( ctx context . Context ) ( <- chan uint8 , error ) {
if err := i . checkStatus ( i . battLevelChar , BatteryLvlChar ) ; err != nil {
return nil , err
2021-08-24 23:55:03 -05:00
}
// Start notifications on heart rate characteristic
err := i . battLevelChar . StartNotify ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-08-24 23:55:03 -05:00
}
// Watch characteristics of heart rate characteristic
ch , err := i . battLevelChar . WatchProperties ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-08-24 23:55:03 -05:00
}
out := make ( chan uint8 , 2 )
2021-08-25 00:17:34 -05:00
currentBattLevel , err := i . BatteryLevel ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-08-25 00:17:34 -05:00
}
out <- currentBattLevel
2021-08-24 23:55:03 -05:00
go func ( ) {
// For every event
2022-04-23 20:40:25 -05:00
for {
2021-10-22 23:26:33 -05:00
select {
2022-05-11 15:22:57 -05:00
case <- ctx . Done ( ) :
2023-01-04 17:00:15 -06:00
log . Debug ( "Received done signal" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2021-10-22 23:26:33 -05:00
close ( out )
i . battLevelChar . StopNotify ( )
return
2022-04-23 20:40:25 -05:00
case event := <- ch :
2021-10-22 23:26:33 -05:00
// If value changed
if event . Name == "Value" {
// Send heart rate to channel
out <- uint8 ( event . Value . ( [ ] byte ) [ 0 ] )
2022-04-23 20:40:25 -05:00
} else if event . Name == "Notifying" && ! event . Value . ( bool ) {
2023-01-04 17:00:15 -06:00
log . Debug ( "Notifications stopped, restarting" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2022-04-23 20:40:25 -05:00
i . battLevelChar . StartNotify ( )
2021-10-22 23:26:33 -05:00
}
2021-08-24 23:55:03 -05:00
}
}
} ( )
2022-05-11 15:22:57 -05:00
return out , nil
2021-08-24 23:55:03 -05:00
}
2022-05-11 15:22:57 -05:00
func ( i * Device ) WatchStepCount ( ctx context . Context ) ( <- chan uint32 , error ) {
if err := i . checkStatus ( i . stepCountChar , StepCountChar ) ; err != nil {
return nil , err
2021-10-22 14:59:51 -05:00
}
// Start notifications on step count characteristic
err := i . stepCountChar . StartNotify ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-10-22 14:59:51 -05:00
}
// Watch properties of step count characteristic
ch , err := i . stepCountChar . WatchProperties ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-10-22 14:59:51 -05:00
}
out := make ( chan uint32 , 2 )
currentStepCount , err := i . StepCount ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-10-22 14:59:51 -05:00
}
out <- currentStepCount
go func ( ) {
// For every event
2022-04-23 20:40:25 -05:00
for {
2021-10-22 14:59:51 -05:00
select {
2022-05-11 15:22:57 -05:00
case <- ctx . Done ( ) :
2023-01-04 17:00:15 -06:00
log . Debug ( "Received done signal" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2021-10-22 14:59:51 -05:00
close ( out )
i . stepCountChar . StopNotify ( )
return
2022-04-23 20:40:25 -05:00
case event := <- ch :
2021-10-22 14:59:51 -05:00
// If value changed
if event . Name == "Value" {
// Send step count to channel
out <- binary . LittleEndian . Uint32 ( event . Value . ( [ ] byte ) )
2022-04-23 20:40:25 -05:00
} else if event . Name == "Notifying" && ! event . Value . ( bool ) {
2023-01-04 17:00:15 -06:00
log . Debug ( "Notifications stopped, restarting" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2022-04-23 20:40:25 -05:00
i . stepCountChar . StartNotify ( )
2021-10-22 14:59:51 -05:00
}
}
}
} ( )
2022-05-11 15:22:57 -05:00
return out , nil
2021-10-22 14:59:51 -05:00
}
2022-05-11 15:22:57 -05:00
func ( i * Device ) WatchMotion ( ctx context . Context ) ( <- chan MotionValues , error ) {
if err := i . checkStatus ( i . motionValChar , MotionValChar ) ; err != nil {
return nil , err
2021-10-22 14:59:51 -05:00
}
// Start notifications on motion characteristic
err := i . motionValChar . StartNotify ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-10-22 14:59:51 -05:00
}
// Watch properties of motion characteristic
ch , err := i . motionValChar . WatchProperties ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-10-22 14:59:51 -05:00
}
out := make ( chan MotionValues , 2 )
motionVals , err := i . Motion ( )
if err != nil {
2022-05-11 15:22:57 -05:00
return nil , err
2021-10-22 14:59:51 -05:00
}
out <- motionVals
go func ( ) {
// For every event
2022-04-23 20:40:25 -05:00
for {
2021-10-22 14:59:51 -05:00
select {
2022-05-11 15:22:57 -05:00
case <- ctx . Done ( ) :
2023-01-04 17:00:15 -06:00
log . Debug ( "Received done signal" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2021-10-22 14:59:51 -05:00
close ( out )
i . motionValChar . StopNotify ( )
return
2022-04-23 20:40:25 -05:00
case event := <- ch :
2021-10-22 14:59:51 -05:00
// If value changed
if event . Name == "Value" {
// Read binary into MotionValues struct
binary . Read ( bytes . NewReader ( event . Value . ( [ ] byte ) ) , binary . LittleEndian , & motionVals )
// Send step count to channel
out <- motionVals
2022-04-23 20:40:25 -05:00
} else if event . Name == "Notifying" && ! event . Value . ( bool ) {
2023-01-04 17:00:15 -06:00
log . Debug ( "Notifications stopped, restarting" ) . Str ( "func" , "WatchMotion" ) . Send ( )
2022-04-23 20:40:25 -05:00
i . motionValChar . StartNotify ( )
2021-10-22 14:59:51 -05:00
}
}
}
} ( )
2022-05-11 15:22:57 -05:00
return out , nil
2021-10-22 14:59:51 -05:00
}
2022-11-21 10:51:49 -06:00
// SetTime sets the watch's
// * time using the Current Time Service's current time characteristic
// * timezone information using the CTS's local time characteristic
2021-08-19 19:41:09 -05:00
func ( i * Device ) SetTime ( t time . Time ) error {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . currentTimeChar , CurrentTimeChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return err
2021-08-19 19:41:09 -05:00
}
buf := & bytes . Buffer { }
binary . Write ( buf , binary . LittleEndian , uint16 ( t . Year ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( t . Month ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( t . Day ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( t . Hour ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( t . Minute ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( t . Second ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( t . Weekday ( ) ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( ( t . Nanosecond ( ) / 1000 ) / 1e6 * 256 ) )
binary . Write ( buf , binary . LittleEndian , uint8 ( 0b0001 ) )
2022-11-21 10:51:49 -06:00
if err := i . currentTimeChar . WriteValue ( buf . Bytes ( ) , nil ) ; err != nil {
return err
}
if err := i . checkStatus ( i . localTimeChar , LocalTimeChar ) ; err != nil {
// If the characteristic is unavailable,
// fail silently, as many people may be on
// older InfiniTime versions. A warning
// may be added later.
if _ , ok := err . ( ErrCharNotAvail ) ; ok {
return nil
} else {
return err
}
}
_ , offset := t . Zone ( )
dst := 0
// Local time expects two values: the timezone offset and the dst offset, both
// expressed in quarters of an hour.
// Timezone offset is to be constant over DST, with dst offset holding the offset != 0
// when DST is in effect.
// As there is no standard way in go to get the actual dst offset, we assume it to be 1h
// when DST is in effect
if t . IsDST ( ) {
dst = 3600
offset -= 3600
}
bufTz := & bytes . Buffer { }
2023-01-04 17:00:15 -06:00
binary . Write ( bufTz , binary . LittleEndian , uint8 ( offset / 3600 * 4 ) )
binary . Write ( bufTz , binary . LittleEndian , uint8 ( dst / 3600 * 4 ) )
2022-11-21 10:51:49 -06:00
return i . localTimeChar . WriteValue ( bufTz . Bytes ( ) , nil )
2021-08-19 19:41:09 -05:00
}
// Notify sends a notification to InfiniTime via
// the Alert Notification Service (ANS)
func ( i * Device ) Notify ( title , body string ) error {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . newAlertChar , NewAlertChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return err
2021-08-19 19:41:09 -05:00
}
return i . newAlertChar . WriteValue (
2021-10-15 02:23:54 -05:00
append ( [ ] byte { 0x00 , 0x01 , 0x00 } , [ ] byte ( title + "\x00" + body ) ... ) ,
2021-08-19 19:41:09 -05:00
nil ,
)
}
2021-10-15 02:23:54 -05:00
// These constants represent the possible call statuses selected by the user
const (
CallStatusDeclined uint8 = iota
CallStatusAccepted
CallStatusMuted
)
// NotifyCall sends a call notification to the PineTime and returns a channel.
2021-11-25 14:39:43 -06:00
// This channel will contain the user's response to the call notification.
2021-10-15 02:23:54 -05:00
func ( i * Device ) NotifyCall ( from string ) ( <- chan uint8 , error ) {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . newAlertChar , NewAlertChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return nil , err
2021-10-15 02:23:54 -05:00
}
// Write call notification to new alert characteristic
err := i . newAlertChar . WriteValue (
append ( [ ] byte { 0x03 , 0x01 , 0x00 } , [ ] byte ( from ) ... ) ,
nil ,
)
if err != nil {
return nil , err
}
2021-11-25 14:39:43 -06:00
if ! i . notifEventDone {
err = i . initNotifEvent ( )
if err != nil {
return nil , err
}
i . notifEventDone = true
}
return i . notifEventCh , nil
}
// initNotifEvent initializes the notification event channel
func ( i * Device ) initNotifEvent ( ) error {
2021-10-15 02:23:54 -05:00
// Start notifications on notification event characteristic
2021-11-25 14:39:43 -06:00
err := i . notifEventChar . StartNotify ( )
2021-10-15 02:23:54 -05:00
if err != nil {
2021-11-25 14:39:43 -06:00
return err
2021-10-15 02:23:54 -05:00
}
// Watch properties of notification event characteristic
ch , err := i . notifEventChar . WatchProperties ( )
if err != nil {
2021-11-25 14:39:43 -06:00
return err
2021-10-15 02:23:54 -05:00
}
// Create new output channel for status
2021-11-25 14:39:43 -06:00
i . notifEventCh = make ( chan uint8 , 1 )
2021-10-15 02:23:54 -05:00
go func ( ) {
// For every event
for event := range ch {
// If value changed
if event . Name == "Value" {
// Send status to channel
2021-11-25 14:39:43 -06:00
i . notifEventCh <- uint8 ( event . Value . ( [ ] byte ) [ 0 ] )
2021-10-15 02:23:54 -05:00
}
}
} ( )
2021-11-25 14:39:43 -06:00
return nil
2021-10-15 02:23:54 -05:00
}
2021-11-22 23:19:30 -06:00
// FS creates and returns a new filesystem from the device
func ( i * Device ) FS ( ) ( * blefs . FS , error ) {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . fsTransferChar , FSTransferChar ) ; err != nil {
2021-11-25 14:39:43 -06:00
return nil , err
}
2021-11-22 23:19:30 -06:00
return blefs . New ( i . fsTransferChar )
}
2021-11-25 14:39:43 -06:00
2021-12-12 14:43:43 -06:00
// AddWeatherEvent adds one of the event structs from
// the weather package to the timeline. Input must be
// a struct containing TimelineHeader.
func ( i * Device ) AddWeatherEvent ( event interface { } ) error {
2022-05-11 15:22:57 -05:00
if err := i . checkStatus ( i . weatherDataChar , WeatherDataChar ) ; err != nil {
2021-12-12 14:43:43 -06:00
return err
}
// Get type of input
inputType := reflect . TypeOf ( event )
// Check if input contains TimelineHeader
_ , hdrExists := inputType . FieldByName ( "TimelineHeader" )
// If header does not exist or input is not struct
if ! hdrExists || inputType . Kind ( ) != reflect . Struct {
return ErrNoTimelineHeader
}
// Encode event as CBOR
data , err := cbor . Marshal ( event )
if err != nil {
return err
}
2023-01-04 17:00:15 -06:00
log . Debug ( "Adding weather event" ) . Any ( "event" , event ) . Send ( )
2021-12-12 14:43:43 -06:00
// Write data to weather data characteristic
return i . weatherDataChar . WriteValue ( data , nil )
}
2022-05-11 15:22:57 -05:00
func ( i * Device ) checkStatus ( char * gatt . GattCharacteristic1 , uuid string ) error {
2023-01-04 17:00:15 -06:00
log . Debug ( "Checking characteristic status" ) . Send ( )
2021-12-16 23:30:29 -06:00
connected , err := i . device . GetConnected ( )
if err != nil {
return err
}
if ! connected {
2021-11-25 14:39:43 -06:00
return ErrNotConnected
}
if char == nil {
2023-01-04 17:00:15 -06:00
log . Debug ( "Characteristic not available (nil)" ) . Send ( )
2022-05-11 15:22:57 -05:00
return ErrCharNotAvail { uuid }
2021-11-25 14:39:43 -06:00
}
2023-01-04 17:00:15 -06:00
log . Debug ( "Characteristic available" ) .
2022-05-11 15:22:57 -05:00
Str ( "uuid" , char . Properties . UUID ) .
Str ( "name" , charNames [ char . Properties . UUID ] ) .
2023-01-04 17:00:15 -06:00
Send ( )
2021-11-25 14:39:43 -06:00
return nil
}