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 (
|
2022-04-24 07:54:04 +00:00
|
|
|
"bytes"
|
2022-04-23 00:12:30 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-11-23 06:04:09 +00:00
|
|
|
"io"
|
2021-08-21 08:19:49 +00:00
|
|
|
"net"
|
2022-04-24 07:54:04 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-08-21 08:19:49 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-12-13 01:08:48 +00:00
|
|
|
"strings"
|
2021-08-21 08:19:49 +00:00
|
|
|
"time"
|
|
|
|
|
2021-10-24 01:03:17 +00:00
|
|
|
"github.com/google/uuid"
|
2021-08-21 08:19:49 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-04-30 10:25:27 +00:00
|
|
|
"github.com/smallnest/rpcxlite/server"
|
|
|
|
"github.com/smallnest/rpcxlite/share"
|
2022-04-23 00:12:30 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
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"
|
2022-04-23 00:12:30 +00:00
|
|
|
"go.arsenm.dev/itd/api"
|
|
|
|
)
|
|
|
|
|
2022-04-23 01:43:13 +00:00
|
|
|
// This type signifies an unneeded value.
|
2022-04-23 00:12:30 +00:00
|
|
|
// A struct{} is used as it takes no space in memory.
|
2022-04-23 01:43:13 +00:00
|
|
|
// This exists for readability purposes
|
2022-04-23 00:12:30 +00:00
|
|
|
type none = struct{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrDFUInvalidFile = errors.New("provided file is invalid for given upgrade type")
|
|
|
|
ErrDFUNotEnoughFiles = errors.New("not enough files provided for given upgrade type")
|
|
|
|
ErrDFUInvalidUpgType = errors.New("invalid upgrade type")
|
2022-04-24 07:54:04 +00:00
|
|
|
ErrRPCXNoReturnURL = errors.New("bidirectional requests over gateway require a returnURL field in the metadata")
|
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
|
2022-02-21 19:20:02 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(k.String("socket.path")), 0755)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old socket if it exists
|
2022-02-21 19:20:02 +00:00
|
|
|
err = os.RemoveAll(k.String("socket.path"))
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen on socket path
|
2022-02-21 19:20:02 +00:00
|
|
|
ln, err := net.Listen("unix", k.String("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")
|
|
|
|
}
|
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
srv := server.NewServer()
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
itdAPI := &ITD{
|
|
|
|
dev: dev,
|
|
|
|
srv: srv,
|
|
|
|
}
|
2022-04-23 01:43:13 +00:00
|
|
|
err = srv.Register(itdAPI, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
fsAPI := &FS{
|
|
|
|
dev: dev,
|
|
|
|
fs: fs,
|
|
|
|
srv: srv,
|
|
|
|
}
|
2022-04-23 01:43:13 +00:00
|
|
|
err = srv.Register(fsAPI, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2022-04-30 10:25:27 +00:00
|
|
|
go srv.ServeListener("unix", ln)
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
// Log socket start
|
2022-02-21 19:20:02 +00:00
|
|
|
log.Info().Str("path", k.String("socket.path")).Msg("Started control socket")
|
2021-08-21 08:19:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
type ITD struct {
|
|
|
|
dev *infinitime.Device
|
|
|
|
srv *server.Server
|
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (i *ITD) HeartRate(_ context.Context, _ none, out *uint8) error {
|
|
|
|
heartRate, err := i.dev.HeartRate()
|
|
|
|
*out = heartRate
|
|
|
|
return err
|
|
|
|
}
|
2021-11-26 04:35:03 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (i *ITD) WatchHeartRate(ctx context.Context, _ none, out *string) error {
|
2022-04-24 07:54:04 +00:00
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, i.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if !ok {
|
2022-04-24 07:54:04 +00:00
|
|
|
return ErrRPCXNoReturnURL
|
2022-04-23 18:29:16 +00:00
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
heartRateCh, cancel, err := i.dev.WatchHeartRate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-24 08:09:27 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
id := uuid.New().String()
|
|
|
|
go func() {
|
|
|
|
done.Create(id)
|
|
|
|
// For every heart rate value
|
|
|
|
for heartRate := range heartRateCh {
|
|
|
|
select {
|
|
|
|
case <-done[id]:
|
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
|
|
|
done.Remove(id)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
data, err := msgpack.Marshal(heartRate)
|
|
|
|
if err != nil {
|
2022-04-23 01:43:13 +00:00
|
|
|
log.Error().Err(err).Msg("Error encoding heart rate")
|
2022-04-23 00:12:30 +00:00
|
|
|
continue
|
2021-10-22 20:21:14 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
// Send response to connection if no done signal received
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "HeartRateSample", nil, data)
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
*out = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) BatteryLevel(_ context.Context, _ none, out *uint8) error {
|
|
|
|
battLevel, err := i.dev.BatteryLevel()
|
|
|
|
*out = battLevel
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) WatchBatteryLevel(ctx context.Context, _ none, out *string) error {
|
2022-04-24 07:54:04 +00:00
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, i.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if !ok {
|
2022-04-24 07:54:04 +00:00
|
|
|
return ErrRPCXNoReturnURL
|
2022-04-23 18:29:16 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
battLevelCh, cancel, err := i.dev.WatchBatteryLevel()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
id := uuid.New().String()
|
|
|
|
go func() {
|
|
|
|
done.Create(id)
|
|
|
|
// For every heart rate value
|
|
|
|
for battLevel := range battLevelCh {
|
|
|
|
select {
|
|
|
|
case <-done[id]:
|
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
|
|
|
done.Remove(id)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
data, err := msgpack.Marshal(battLevel)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2022-04-23 01:43:13 +00:00
|
|
|
log.Error().Err(err).Msg("Error encoding battery level")
|
2022-04-23 00:12:30 +00:00
|
|
|
continue
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
// Send response to connection if no done signal received
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "BatteryLevelSample", nil, data)
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
*out = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) Motion(_ context.Context, _ none, out *infinitime.MotionValues) error {
|
|
|
|
motionVals, err := i.dev.Motion()
|
|
|
|
*out = motionVals
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) WatchMotion(ctx context.Context, _ none, out *string) error {
|
2022-04-24 07:54:04 +00:00
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, i.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if !ok {
|
2022-04-24 07:54:04 +00:00
|
|
|
return ErrRPCXNoReturnURL
|
2022-04-23 18:29:16 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
motionValsCh, cancel, err := i.dev.WatchMotion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := uuid.New().String()
|
|
|
|
go func() {
|
|
|
|
done.Create(id)
|
|
|
|
// For every heart rate value
|
|
|
|
for motionVals := range motionValsCh {
|
|
|
|
select {
|
|
|
|
case <-done[id]:
|
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
|
|
|
done.Remove(id)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
data, err := msgpack.Marshal(motionVals)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2022-04-23 01:43:13 +00:00
|
|
|
log.Error().Err(err).Msg("Error encoding motion values")
|
2022-04-23 00:12:30 +00:00
|
|
|
continue
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
// Send response to connection if no done signal received
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "MotionSample", nil, data)
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
*out = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) StepCount(_ context.Context, _ none, out *uint32) error {
|
|
|
|
stepCount, err := i.dev.StepCount()
|
|
|
|
*out = stepCount
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) WatchStepCount(ctx context.Context, _ none, out *string) error {
|
2022-04-24 07:54:04 +00:00
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, i.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if !ok {
|
2022-04-24 07:54:04 +00:00
|
|
|
return ErrRPCXNoReturnURL
|
2022-04-23 18:29:16 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
stepCountCh, cancel, err := i.dev.WatchStepCount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := uuid.New().String()
|
|
|
|
go func() {
|
|
|
|
done.Create(id)
|
|
|
|
// For every heart rate value
|
|
|
|
for stepCount := range stepCountCh {
|
|
|
|
select {
|
|
|
|
case <-done[id]:
|
|
|
|
// Stop notifications if done signal received
|
|
|
|
cancel()
|
|
|
|
done.Remove(id)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
data, err := msgpack.Marshal(stepCount)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2022-04-23 01:43:13 +00:00
|
|
|
log.Error().Err(err).Msg("Error encoding step count")
|
2022-04-23 00:12:30 +00:00
|
|
|
continue
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
// Send response to connection if no done signal received
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "StepCountSample", nil, data)
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
*out = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) Version(_ context.Context, _ none, out *string) error {
|
|
|
|
version, err := i.dev.Version()
|
|
|
|
*out = version
|
|
|
|
return err
|
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (i *ITD) Address(_ context.Context, _ none, out *string) error {
|
|
|
|
addr := i.dev.Address()
|
|
|
|
*out = addr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) Notify(_ context.Context, data api.NotifyData, _ *none) error {
|
|
|
|
return i.dev.Notify(data.Title, data.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) SetTime(_ context.Context, t time.Time, _ *none) error {
|
|
|
|
return i.dev.SetTime(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) WeatherUpdate(_ context.Context, _ none, _ *none) error {
|
|
|
|
sendWeatherCh <- struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ITD) FirmwareUpgrade(ctx context.Context, reqData api.FwUpgradeData, out *string) error {
|
|
|
|
i.dev.DFU.Reset()
|
|
|
|
|
|
|
|
switch reqData.Type {
|
|
|
|
case api.UpgradeTypeArchive:
|
|
|
|
// If less than one file, return error
|
|
|
|
if len(reqData.Files) < 1 {
|
|
|
|
return ErrDFUNotEnoughFiles
|
|
|
|
}
|
|
|
|
// If file is not zip archive, return error
|
|
|
|
if filepath.Ext(reqData.Files[0]) != ".zip" {
|
|
|
|
return ErrDFUInvalidFile
|
|
|
|
}
|
|
|
|
// Load DFU archive
|
|
|
|
err := i.dev.DFU.LoadArchive(reqData.Files[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case api.UpgradeTypeFiles:
|
|
|
|
// If less than two files, return error
|
|
|
|
if len(reqData.Files) < 2 {
|
|
|
|
return ErrDFUNotEnoughFiles
|
|
|
|
}
|
|
|
|
// If first file is not init packet, return error
|
|
|
|
if filepath.Ext(reqData.Files[0]) != ".dat" {
|
|
|
|
return ErrDFUInvalidFile
|
|
|
|
}
|
|
|
|
// If second file is not firmware image, return error
|
|
|
|
if filepath.Ext(reqData.Files[1]) != ".bin" {
|
|
|
|
return ErrDFUInvalidFile
|
|
|
|
}
|
|
|
|
// Load individual DFU files
|
|
|
|
err := i.dev.DFU.LoadFiles(reqData.Files[0], reqData.Files[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return ErrDFUInvalidUpgType
|
|
|
|
}
|
|
|
|
|
|
|
|
id := uuid.New().String()
|
|
|
|
*out = id
|
|
|
|
|
2022-04-24 07:54:04 +00:00
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, i.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if ok {
|
|
|
|
go func() {
|
|
|
|
// For every progress event
|
|
|
|
for event := range i.dev.DFU.Progress() {
|
|
|
|
data, err := msgpack.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error encoding DFU progress event")
|
|
|
|
continue
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "DFUProgress", nil, data)
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2022-04-23 18:29:16 +00:00
|
|
|
firmwareUpdating = false
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "Done", nil, nil)
|
2022-04-23 18:29:16 +00:00
|
|
|
}()
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
// Set firmwareUpdating
|
|
|
|
firmwareUpdating = true
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Start DFU
|
|
|
|
err := i.dev.DFU.Start()
|
|
|
|
if err != nil {
|
2022-04-23 01:43:13 +00:00
|
|
|
log.Error().Err(err).Msg("Error while upgrading firmware")
|
2021-08-21 08:19:49 +00:00
|
|
|
firmwareUpdating = false
|
2022-04-23 00:12:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2021-12-13 01:08:48 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-13 01:08:48 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (i *ITD) Done(_ context.Context, id string, _ *none) error {
|
|
|
|
done.Done(id)
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-13 01:08:48 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
type FS struct {
|
|
|
|
dev *infinitime.Device
|
|
|
|
fs *blefs.FS
|
|
|
|
srv *server.Server
|
|
|
|
}
|
2021-12-13 01:08:48 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (fs *FS) Remove(_ context.Context, paths []string, _ *none) error {
|
|
|
|
fs.updateFS()
|
|
|
|
for _, path := range paths {
|
|
|
|
err := fs.fs.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-12 06:11:01 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (fs *FS) Rename(_ context.Context, paths [2]string, _ *none) error {
|
|
|
|
fs.updateFS()
|
|
|
|
return fs.fs.Rename(paths[0], paths[1])
|
|
|
|
}
|
2021-12-12 06:11:01 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (fs *FS) Mkdir(_ context.Context, paths []string, _ *none) error {
|
|
|
|
fs.updateFS()
|
|
|
|
for _, path := range paths {
|
|
|
|
err := fs.fs.Mkdir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-12 06:11:01 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (fs *FS) ReadDir(_ context.Context, dir string, out *[]api.FileInfo) error {
|
|
|
|
fs.updateFS()
|
2021-12-12 06:11:01 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
entries, err := fs.fs.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var fileInfo []api.FileInfo
|
|
|
|
for _, entry := range entries {
|
|
|
|
info, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileInfo = append(fileInfo, api.FileInfo{
|
|
|
|
Name: info.Name(),
|
|
|
|
Size: info.Size(),
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = fileInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Upload(ctx context.Context, paths [2]string, out *string) error {
|
|
|
|
fs.updateFS()
|
|
|
|
|
|
|
|
localFile, err := os.Open(paths[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
localInfo, err := localFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteFile, err := fs.fs.Create(paths[0], uint32(localInfo.Size()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := uuid.New().String()
|
|
|
|
*out = id
|
|
|
|
|
2022-04-24 07:54:04 +00:00
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, fs.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if ok {
|
|
|
|
go func() {
|
|
|
|
// For every progress event
|
|
|
|
for sent := range remoteFile.Progress() {
|
|
|
|
data, err := msgpack.Marshal(api.FSTransferProgress{
|
|
|
|
Total: remoteFile.Size(),
|
|
|
|
Sent: sent,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error encoding filesystem transfer progress event")
|
|
|
|
continue
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "FSProgress", nil, data)
|
2022-04-23 18:29:16 +00:00
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
2022-04-24 07:54:04 +00:00
|
|
|
msgSender.SendMessage(id, "Done", nil, nil)
|
2022-04-23 18:29:16 +00:00
|
|
|
}()
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
io.Copy(remoteFile, localFile)
|
|
|
|
localFile.Close()
|
|
|
|
remoteFile.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
func (fs *FS) Download(ctx context.Context, paths [2]string, out *string) error {
|
|
|
|
fs.updateFS()
|
2022-04-24 07:54:04 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
localFile, err := os.Create(paths[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-24 07:54:04 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
remoteFile, err := fs.fs.Open(paths[1])
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
2022-04-23 00:12:30 +00:00
|
|
|
return err
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2022-04-24 07:54:04 +00:00
|
|
|
|
2022-04-23 00:12:30 +00:00
|
|
|
id := uuid.New().String()
|
|
|
|
*out = id
|
2022-04-24 07:54:04 +00:00
|
|
|
|
|
|
|
// Get client message sender
|
|
|
|
msgSender, ok := getMsgSender(ctx, fs.srv)
|
2022-04-23 18:29:16 +00:00
|
|
|
// If user is using gateway, the client connection will not be available
|
|
|
|
if ok {
|
|
|
|
go func() {
|
|
|
|
// For every progress event
|
|
|
|
for rcvd := range remoteFile.Progress() {
|
|
|
|
data, err := msgpack.Marshal(api.FSTransferProgress{
|
|
|
|
Total: remoteFile.Size(),
|
|
|
|
Sent: rcvd,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error encoding filesystem transfer progress event")
|
|
|
|
continue
|
|
|
|
}
|
2022-04-24 07:54:04 +00:00
|
|
|
|
|
|
|
msgSender.SendMessage(id, "FSProgress", nil, data)
|
2022-04-23 00:12:30 +00:00
|
|
|
}
|
2022-04-24 07:54:04 +00:00
|
|
|
|
|
|
|
msgSender.SendMessage(id, "Done", nil, nil)
|
2022-04-23 18:29:16 +00:00
|
|
|
localFile.Close()
|
|
|
|
remoteFile.Close()
|
|
|
|
}()
|
|
|
|
}
|
2022-04-23 00:12:30 +00:00
|
|
|
|
|
|
|
go io.Copy(localFile, remoteFile)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) updateFS() {
|
|
|
|
if fs.fs == nil || updateFS {
|
|
|
|
// Get new FS
|
|
|
|
newFS, err := fs.dev.FS()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Error updating BLE filesystem")
|
|
|
|
} else {
|
|
|
|
// Set FS pointer to new FS
|
|
|
|
fs.fs = newFS
|
|
|
|
// Reset updateFS
|
|
|
|
updateFS = false
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
2021-12-13 01:08:48 +00:00
|
|
|
|
|
|
|
// cleanPaths runs strings.TrimSpace and filepath.Clean
|
|
|
|
// on all inputs, and returns the updated slice
|
|
|
|
func cleanPaths(paths []string) []string {
|
|
|
|
for index, path := range paths {
|
|
|
|
newPath := strings.TrimSpace(path)
|
|
|
|
paths[index] = filepath.Clean(newPath)
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
2022-04-24 07:54:04 +00:00
|
|
|
|
|
|
|
func getMsgSender(ctx context.Context, srv *server.Server) (MessageSender, bool) {
|
|
|
|
// Get client message sender
|
|
|
|
clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn)
|
|
|
|
// If the connection exists, use rpcMsgSender
|
|
|
|
if ok {
|
|
|
|
return &rpcMsgSender{srv, clientConn}, true
|
|
|
|
} else {
|
|
|
|
// Get metadata if it exists
|
|
|
|
metadata, ok := ctx.Value(share.ReqMetaDataKey).(map[string]string)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
// Get returnURL field from metadata if it exists
|
|
|
|
returnURL, ok := metadata["returnURL"]
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
// Use httpMsgSender
|
|
|
|
return &httpMsgSender{returnURL}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// The MessageSender interface sends messages to the client
|
2022-04-24 07:54:04 +00:00
|
|
|
type MessageSender interface {
|
|
|
|
SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// rpcMsgSender sends messages using RPCX, for clients that support it
|
2022-04-24 07:54:04 +00:00
|
|
|
type rpcMsgSender struct {
|
|
|
|
srv *server.Server
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// SendMessage uses the server to send an RPCX message back to the client
|
2022-04-24 07:54:04 +00:00
|
|
|
func (r *rpcMsgSender) SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error {
|
|
|
|
return r.srv.SendMessage(r.conn, servicePath, serviceMethod, metadata, data)
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// httpMsgSender sends messages to the given return URL, for clients that provide it
|
2022-04-24 07:54:04 +00:00
|
|
|
type httpMsgSender struct {
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// SendMessage uses HTTP to send a message back to the client
|
2022-04-24 07:54:04 +00:00
|
|
|
func (h *httpMsgSender) SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error {
|
2022-04-24 07:58:39 +00:00
|
|
|
// Create new POST request with provided URL
|
2022-04-24 07:54:04 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, h.url, bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// Set service path and method headers
|
2022-04-24 07:54:04 +00:00
|
|
|
req.Header.Set("X-RPCX-ServicePath", servicePath)
|
|
|
|
req.Header.Set("X-RPCX-ServiceMethod", serviceMethod)
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// Create new URL query values
|
2022-04-24 07:54:04 +00:00
|
|
|
query := url.Values{}
|
2022-04-24 07:58:39 +00:00
|
|
|
// Transfer values from metadata to query
|
2022-04-24 07:54:04 +00:00
|
|
|
for k, v := range metadata {
|
|
|
|
query.Set(k, v)
|
|
|
|
}
|
2022-04-24 07:58:39 +00:00
|
|
|
// Set metadata header by encoding query values
|
2022-04-24 07:54:04 +00:00
|
|
|
req.Header.Set("X-RPCX-Meta", query.Encode())
|
|
|
|
|
2022-04-24 07:58:39 +00:00
|
|
|
// Perform request
|
2022-04-24 07:54:04 +00:00
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-24 07:58:39 +00:00
|
|
|
// Close body
|
2022-04-24 07:54:04 +00:00
|
|
|
return res.Body.Close()
|
|
|
|
}
|