2021-08-21 08:19:49 +00:00
|
|
|
/*
|
|
|
|
* itd uses bluetooth low energy to communicate with InfiniTime devices
|
|
|
|
* Copyright (C) 2021 Arsen Musayelyan
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-11-23 06:04:09 +00:00
|
|
|
"io"
|
2021-08-21 08:19:49 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2021-10-24 01:03:17 +00:00
|
|
|
"github.com/google/uuid"
|
2021-08-21 08:19:49 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-08-24 15:33:41 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-08-21 08:19:49 +00:00
|
|
|
"go.arsenm.dev/infinitime"
|
2021-11-26 03:41:44 +00:00
|
|
|
"go.arsenm.dev/infinitime/blefs"
|
2021-08-21 08:19:49 +00:00
|
|
|
"go.arsenm.dev/itd/internal/types"
|
2021-10-04 08:05:01 +00:00
|
|
|
"go.arsenm.dev/itd/translit"
|
2021-08-21 08:19:49 +00:00
|
|
|
)
|
|
|
|
|
2021-10-24 01:03:17 +00:00
|
|
|
type DoneMap map[string]chan struct{}
|
|
|
|
|
|
|
|
func (dm DoneMap) Exists(key string) bool {
|
|
|
|
_, ok := dm[key]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm DoneMap) Done(key string) {
|
|
|
|
ch := dm[key]
|
|
|
|
ch <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm DoneMap) Create(key string) {
|
|
|
|
dm[key] = make(chan struct{}, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm DoneMap) Remove(key string) {
|
|
|
|
close(dm[key])
|
|
|
|
delete(dm, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var done = DoneMap{}
|
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
func startSocket(dev *infinitime.Device) error {
|
2021-10-07 20:38:13 +00:00
|
|
|
// Make socket directory if non-existant
|
2021-08-24 15:33:41 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(viper.GetString("socket.path")), 0755)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old socket if it exists
|
2021-08-24 15:33:41 +00:00
|
|
|
err = os.RemoveAll(viper.GetString("socket.path"))
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen on socket path
|
2021-08-24 15:33:41 +00:00
|
|
|
ln, err := net.Listen("unix", viper.GetString("socket.path"))
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-26 03:41:44 +00:00
|
|
|
fs, err := dev.FS()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Error getting BLE filesystem")
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// Accept socket connection
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error accepting connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concurrently handle connection
|
2021-11-26 03:41:44 +00:00
|
|
|
go handleConnection(conn, dev, fs)
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Log socket start
|
2021-08-24 15:33:41 +00:00
|
|
|
log.Info().Str("path", viper.GetString("socket.path")).Msg("Started control socket")
|
2021-08-21 08:19:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-26 03:41:44 +00:00
|
|
|
func handleConnection(conn net.Conn, dev *infinitime.Device, fs *blefs.FS) {
|
2021-08-21 08:19:49 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Create new scanner on connection
|
|
|
|
scanner := bufio.NewScanner(conn)
|
|
|
|
for scanner.Scan() {
|
|
|
|
var req types.Request
|
|
|
|
// Decode scanned message into types.Request
|
|
|
|
err := json.Unmarshal(scanner.Bytes(), &req)
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error decoding JSON input")
|
2021-08-21 08:19:49 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-24 08:09:27 +00:00
|
|
|
// If firmware is updating, return error
|
|
|
|
if firmwareUpdating {
|
|
|
|
connErr(conn, req.Type, nil, "Firmware update in progress")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
switch req.Type {
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.ReqTypeHeartRate:
|
2021-08-21 08:19:49 +00:00
|
|
|
// Get heart rate from watch
|
|
|
|
heartRate, err := dev.HeartRate()
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting heart rate")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode heart rate to connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-08-21 08:19:49 +00:00
|
|
|
Value: heartRate,
|
|
|
|
})
|
2021-08-26 04:18:24 +00:00
|
|
|
case types.ReqTypeWatchHeartRate:
|
2021-10-23 05:14:01 +00:00
|
|
|
heartRateCh, cancel, err := dev.WatchHeartRate()
|
2021-08-26 04:18:24 +00:00
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting heart rate channel")
|
2021-08-26 04:18:24 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-24 01:03:17 +00:00
|
|
|
reqID := uuid.New().String()
|
2021-08-26 04:18:24 +00:00
|
|
|
go func() {
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Create(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
// For every heart rate value
|
2021-08-26 04:18:24 +00:00
|
|
|
for heartRate := range heartRateCh {
|
2021-10-23 05:14:01 +00:00
|
|
|
select {
|
2021-10-24 01:03:17 +00:00
|
|
|
case <-done[reqID]:
|
2021-10-23 05:14:01 +00:00
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Remove(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Encode response to connection if no done signal received
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-24 01:03:17 +00:00
|
|
|
ID: reqID,
|
2021-10-23 05:14:01 +00:00
|
|
|
Value: heartRate,
|
|
|
|
})
|
|
|
|
}
|
2021-08-26 04:18:24 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-10-22 20:21:14 +00:00
|
|
|
case types.ReqTypeBattLevel:
|
|
|
|
// Get battery level from watch
|
|
|
|
battLevel, err := dev.BatteryLevel()
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting battery level")
|
2021-10-22 20:21:14 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode battery level to connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-22 20:21:14 +00:00
|
|
|
Value: battLevel,
|
|
|
|
})
|
2021-08-26 04:18:24 +00:00
|
|
|
case types.ReqTypeWatchBattLevel:
|
2021-10-23 05:14:01 +00:00
|
|
|
battLevelCh, cancel, err := dev.WatchBatteryLevel()
|
2021-08-26 04:18:24 +00:00
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting battery level channel")
|
2021-08-26 04:18:24 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-24 01:03:17 +00:00
|
|
|
reqID := uuid.New().String()
|
2021-08-26 04:18:24 +00:00
|
|
|
go func() {
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Create(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
// For every battery level value
|
2021-08-26 04:18:24 +00:00
|
|
|
for battLevel := range battLevelCh {
|
2021-10-23 05:14:01 +00:00
|
|
|
select {
|
2021-10-24 01:03:17 +00:00
|
|
|
case <-done[reqID]:
|
2021-10-23 05:14:01 +00:00
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Remove(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Encode response to connection if no done signal received
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-24 01:03:17 +00:00
|
|
|
ID: reqID,
|
2021-10-23 05:14:01 +00:00
|
|
|
Value: battLevel,
|
|
|
|
})
|
|
|
|
}
|
2021-08-26 04:18:24 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-10-22 20:21:14 +00:00
|
|
|
case types.ReqTypeMotion:
|
2021-08-21 08:19:49 +00:00
|
|
|
// Get battery level from watch
|
2021-10-22 20:21:14 +00:00
|
|
|
motionVals, err := dev.Motion()
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting motion values")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode battery level to connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-22 20:21:14 +00:00
|
|
|
Value: motionVals,
|
|
|
|
})
|
|
|
|
case types.ReqTypeWatchMotion:
|
2021-10-23 05:14:01 +00:00
|
|
|
motionValCh, cancel, err := dev.WatchMotion()
|
2021-10-22 20:21:14 +00:00
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting heart rate channel")
|
2021-10-22 20:21:14 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-24 01:03:17 +00:00
|
|
|
reqID := uuid.New().String()
|
2021-10-22 20:21:14 +00:00
|
|
|
go func() {
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Create(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
// For every motion event
|
2021-10-22 20:21:14 +00:00
|
|
|
for motionVals := range motionValCh {
|
2021-10-23 05:14:01 +00:00
|
|
|
select {
|
2021-10-24 01:03:17 +00:00
|
|
|
case <-done[reqID]:
|
2021-10-23 05:14:01 +00:00
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Remove(reqID)
|
|
|
|
|
2021-10-23 05:14:01 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Encode response to connection if no done signal received
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-24 01:03:17 +00:00
|
|
|
ID: reqID,
|
2021-10-23 05:14:01 +00:00
|
|
|
Value: motionVals,
|
|
|
|
})
|
|
|
|
}
|
2021-10-22 20:21:14 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
case types.ReqTypeStepCount:
|
|
|
|
// Get battery level from watch
|
|
|
|
stepCount, err := dev.StepCount()
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting step count")
|
2021-10-22 20:21:14 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode battery level to connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-22 20:21:14 +00:00
|
|
|
Value: stepCount,
|
2021-08-21 08:19:49 +00:00
|
|
|
})
|
2021-10-22 20:21:14 +00:00
|
|
|
case types.ReqTypeWatchStepCount:
|
2021-10-23 05:14:01 +00:00
|
|
|
stepCountCh, cancel, err := dev.WatchStepCount()
|
2021-10-22 20:21:14 +00:00
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting heart rate channel")
|
2021-10-22 20:21:14 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-24 01:03:17 +00:00
|
|
|
reqID := uuid.New().String()
|
2021-10-22 20:21:14 +00:00
|
|
|
go func() {
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Create(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
// For every step count value
|
2021-10-22 20:21:14 +00:00
|
|
|
for stepCount := range stepCountCh {
|
2021-10-23 05:14:01 +00:00
|
|
|
select {
|
2021-10-24 01:03:17 +00:00
|
|
|
case <-done[reqID]:
|
2021-10-23 05:14:01 +00:00
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
2021-10-24 01:03:17 +00:00
|
|
|
done.Remove(reqID)
|
2021-10-23 05:14:01 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Encode response to connection if no done signal received
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-10-24 01:03:17 +00:00
|
|
|
ID: reqID,
|
2021-10-23 05:14:01 +00:00
|
|
|
Value: stepCount,
|
|
|
|
})
|
|
|
|
}
|
2021-10-22 20:21:14 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.ReqTypeFwVersion:
|
2021-08-21 08:19:49 +00:00
|
|
|
// Get firmware version from watch
|
|
|
|
version, err := dev.Version()
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error getting firmware version")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode version to connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-08-21 08:19:49 +00:00
|
|
|
Value: version,
|
|
|
|
})
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.ReqTypeBtAddress:
|
2021-08-21 08:19:49 +00:00
|
|
|
// Encode bluetooth address to connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-08-21 08:19:49 +00:00
|
|
|
Value: dev.Address(),
|
|
|
|
})
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.ReqTypeNotify:
|
2021-08-21 08:19:49 +00:00
|
|
|
// If no data, return error
|
|
|
|
if req.Data == nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Data required for notify request")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
var reqData types.ReqDataNotify
|
2021-08-22 20:53:32 +00:00
|
|
|
// Decode data map to notify request data
|
2021-08-21 08:19:49 +00:00
|
|
|
err = mapstructure.Decode(req.Data, &reqData)
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error decoding request data")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-05 02:07:54 +00:00
|
|
|
maps := viper.GetStringSlice("notifs.translit.use")
|
2021-10-05 03:23:54 +00:00
|
|
|
translit.Transliterators["custom"] = translit.Map(viper.GetStringSlice("notifs.translit.custom"))
|
2021-10-05 00:45:26 +00:00
|
|
|
title := translit.Transliterate(reqData.Title, maps...)
|
|
|
|
body := translit.Transliterate(reqData.Body, maps...)
|
2021-08-21 08:19:49 +00:00
|
|
|
// Send notification to watch
|
2021-10-05 00:45:26 +00:00
|
|
|
err = dev.Notify(title, body)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error sending notification")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode empty types.Response to connection
|
2021-10-24 07:45:50 +00:00
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.ReqTypeSetTime:
|
2021-08-21 08:19:49 +00:00
|
|
|
// If no data, return error
|
|
|
|
if req.Data == nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Data required for settime request")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Get string from data or return error
|
|
|
|
reqTimeStr, ok := req.Data.(string)
|
|
|
|
if !ok {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Data for settime request must be RFC3339 formatted time string")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqTime time.Time
|
|
|
|
if reqTimeStr == "now" {
|
|
|
|
reqTime = time.Now()
|
|
|
|
} else {
|
2021-08-22 20:53:32 +00:00
|
|
|
// Parse time as RFC3339/ISO8601
|
2021-08-21 08:19:49 +00:00
|
|
|
reqTime, err = time.Parse(time.RFC3339, reqTimeStr)
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Invalid time format. Time string must be formatted as ISO8601 or the word `now`")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set time on watch
|
|
|
|
err = dev.SetTime(reqTime)
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error setting device time")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Encode empty types.Response to connection
|
2021-10-24 07:45:50 +00:00
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.ReqTypeFwUpgrade:
|
2021-08-21 08:19:49 +00:00
|
|
|
// If no data, return error
|
|
|
|
if req.Data == nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Data required for firmware upgrade request")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
var reqData types.ReqDataFwUpgrade
|
2021-08-22 20:53:32 +00:00
|
|
|
// Decode data map to firmware upgrade request data
|
2021-08-21 08:19:49 +00:00
|
|
|
err = mapstructure.Decode(req.Data, &reqData)
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error decoding request data")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-22 03:27:58 +00:00
|
|
|
// Reset DFU to prepare for next update
|
|
|
|
dev.DFU.Reset()
|
2021-08-21 08:19:49 +00:00
|
|
|
switch reqData.Type {
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.UpgradeTypeArchive:
|
2021-08-21 08:19:49 +00:00
|
|
|
// If less than one file, return error
|
|
|
|
if len(reqData.Files) < 1 {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Archive upgrade requires one file with .zip extension")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// If file is not zip archive, return error
|
|
|
|
if filepath.Ext(reqData.Files[0]) != ".zip" {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Archive upgrade file must be a zip archive")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Load DFU archive
|
|
|
|
err := dev.DFU.LoadArchive(reqData.Files[0])
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error loading archive file")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
2021-08-25 03:32:17 +00:00
|
|
|
case types.UpgradeTypeFiles:
|
2021-08-21 08:19:49 +00:00
|
|
|
// If less than two files, return error
|
|
|
|
if len(reqData.Files) < 2 {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Files upgrade requires two files. First with .dat and second with .bin extension.")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// If first file is not init packet, return error
|
|
|
|
if filepath.Ext(reqData.Files[0]) != ".dat" {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "First file must be a .dat file")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// If second file is not firmware image, return error
|
|
|
|
if filepath.Ext(reqData.Files[1]) != ".bin" {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Second file must be a .bin file")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
// Load individual DFU files
|
|
|
|
err := dev.DFU.LoadFiles(reqData.Files[0], reqData.Files[1])
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error loading firmware files")
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Get progress
|
|
|
|
progress := dev.DFU.Progress()
|
|
|
|
// For every progress event
|
|
|
|
for event := range progress {
|
|
|
|
// Encode event on connection
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
2021-10-24 07:45:50 +00:00
|
|
|
Type: req.Type,
|
2021-08-21 08:19:49 +00:00
|
|
|
Value: event,
|
|
|
|
})
|
|
|
|
}
|
2021-10-24 05:03:33 +00:00
|
|
|
firmwareUpdating = false
|
2021-08-21 08:19:49 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Set firmwareUpdating
|
|
|
|
firmwareUpdating = true
|
|
|
|
// Start DFU
|
|
|
|
err = dev.DFU.Start()
|
|
|
|
if err != nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, err, "Error performing upgrade")
|
2021-08-22 20:13:37 +00:00
|
|
|
firmwareUpdating = false
|
2021-08-21 08:19:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
firmwareUpdating = false
|
2021-11-23 06:04:09 +00:00
|
|
|
case types.ReqTypeFS:
|
2021-11-26 03:41:44 +00:00
|
|
|
if fs == nil {
|
|
|
|
connErr(conn, req.Type, nil, "BLE filesystem is not available")
|
|
|
|
break
|
|
|
|
}
|
2021-11-23 06:04:09 +00:00
|
|
|
// If no data, return error
|
|
|
|
if req.Data == nil {
|
|
|
|
connErr(conn, req.Type, nil, "Data required for firmware upgrade request")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var reqData types.ReqDataFS
|
|
|
|
// Decode data map to firmware upgrade request data
|
|
|
|
err = mapstructure.Decode(req.Data, &reqData)
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error decoding request data")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch reqData.Type {
|
|
|
|
case types.FSTypeDelete:
|
|
|
|
for _, file := range reqData.Files {
|
|
|
|
err := fs.Remove(file)
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error removing file")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 03:46:04 +00:00
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
2021-11-23 06:04:09 +00:00
|
|
|
case types.FSTypeMove:
|
|
|
|
if len(reqData.Files) != 2 {
|
|
|
|
connErr(conn, req.Type, nil, "Move FS command requires an old path and new path in the files list")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err := fs.Rename(reqData.Files[0], reqData.Files[1])
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error moving file")
|
|
|
|
break
|
|
|
|
}
|
2021-11-26 03:46:04 +00:00
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
2021-11-23 06:04:09 +00:00
|
|
|
case types.FSTypeMkdir:
|
|
|
|
for _, file := range reqData.Files {
|
|
|
|
err := fs.Mkdir(file)
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error creating directory")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 03:46:04 +00:00
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
2021-11-23 06:04:09 +00:00
|
|
|
case types.FSTypeList:
|
|
|
|
if len(reqData.Files) != 1 {
|
|
|
|
connErr(conn, req.Type, nil, "List FS command requires a path to list in the files list")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
entries, err := fs.ReadDir(reqData.Files[0])
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error reading directory")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var out []types.FileInfo
|
|
|
|
for _, entry := range entries {
|
|
|
|
info, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error getting file info")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
out = append(out, types.FileInfo{
|
|
|
|
Name: info.Name(),
|
|
|
|
Size: info.Size(),
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
|
|
|
Type: req.Type,
|
|
|
|
Value: out,
|
|
|
|
})
|
|
|
|
case types.FSTypeWrite:
|
|
|
|
if len(reqData.Files) != 1 {
|
|
|
|
connErr(conn, req.Type, nil, "Write FS command requires a path to the file to write")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
file, err := fs.Create(reqData.Files[0], uint32(len(reqData.Data)))
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error creating file")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err = file.WriteString(reqData.Data)
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error writing to file")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
|
|
|
case types.FSTypeRead:
|
|
|
|
if len(reqData.Files) != 1 {
|
|
|
|
connErr(conn, req.Type, nil, "Read FS command requires a path to the file to read")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
file, err := fs.Open(reqData.Files[0])
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error opening file")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
data, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
connErr(conn, req.Type, err, "Error reading from file")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
json.NewEncoder(conn).Encode(types.Response{
|
|
|
|
Type: req.Type,
|
|
|
|
Value: string(data),
|
|
|
|
})
|
|
|
|
}
|
2021-10-24 01:03:17 +00:00
|
|
|
case types.ReqTypeCancel:
|
|
|
|
if req.Data == nil {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "No data provided. Cancel request requires request ID string as data.")
|
2021-10-24 01:03:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
reqID, ok := req.Data.(string)
|
|
|
|
if !ok {
|
2021-10-24 08:09:27 +00:00
|
|
|
connErr(conn, req.Type, nil, "Invalid data. Cancel request required request ID string as data.")
|
2021-10-24 01:03:17 +00:00
|
|
|
}
|
|
|
|
// Stop notifications
|
|
|
|
done.Done(reqID)
|
2021-10-24 07:45:50 +00:00
|
|
|
json.NewEncoder(conn).Encode(types.Response{Type: req.Type})
|
2021-10-24 08:11:57 +00:00
|
|
|
default:
|
|
|
|
connErr(conn, req.Type, nil, fmt.Sprintf("Unknown request type %d", req.Type))
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 08:09:27 +00:00
|
|
|
func connErr(conn net.Conn, resType int, err error, msg string) {
|
2021-08-21 08:19:49 +00:00
|
|
|
var res types.Response
|
|
|
|
// If error exists, add to types.Response, otherwise don't
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
res = types.Response{Message: fmt.Sprintf("%s: %s", msg, err)}
|
|
|
|
} else {
|
|
|
|
log.Error().Msg(msg)
|
2021-10-24 08:09:27 +00:00
|
|
|
res = types.Response{Message: msg, Type: resType}
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
res.Error = true
|
|
|
|
|
|
|
|
// Encode error to connection
|
|
|
|
json.NewEncoder(conn).Encode(res)
|
|
|
|
}
|