forked from Elara6331/itd
Compare commits
No commits in common. "008f6b35a991bd653a833c658ce87eed6b2b6566" and "1a1bc30df991c396b039957581e8964dc882f256" have entirely different histories.
008f6b35a9
...
1a1bc30df9
61
internal/fusefs/converters.go
Normal file
61
internal/fusefs/converters.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package fusefs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.arsenm.dev/infinitime"
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func converterU8(ctx context.Context, in <-chan uint8) <-chan []byte {
|
||||||
|
out := make(chan []byte, 2)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <- ctx.Done():
|
||||||
|
return
|
||||||
|
case event := <-in:
|
||||||
|
out <- []byte(strconv.Itoa(int(event)) + "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func converterU32(ctx context.Context, in <-chan uint32) <-chan []byte {
|
||||||
|
out := make(chan []byte, 2)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <- ctx.Done():
|
||||||
|
return
|
||||||
|
case event := <-in:
|
||||||
|
out <- []byte(strconv.Itoa(int(event)) + "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func converterMotionValues(ctx context.Context, in <-chan infinitime.MotionValues) <-chan []byte {
|
||||||
|
out := make(chan []byte, 2)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <- ctx.Done():
|
||||||
|
return
|
||||||
|
case event := <-in:
|
||||||
|
out <- []byte(strconv.Itoa(int(event.X)) + " " + strconv.Itoa(int(event.Y)) + " " + strconv.Itoa(int(event.Z)) + "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func converter1String(ctx context.Context, in string) <-chan []byte {
|
||||||
|
out := make(chan []byte, 2)
|
||||||
|
out <- []byte(in + "\n")
|
||||||
|
close(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -10,13 +10,13 @@ import (
|
|||||||
"github.com/hanwen/go-fuse/v2/fuse"
|
"github.com/hanwen/go-fuse/v2/fuse"
|
||||||
"io"
|
"io"
|
||||||
"bytes"
|
"bytes"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ITProperty struct {
|
type ITProperty struct {
|
||||||
name string
|
name string
|
||||||
Ino uint64
|
Ino uint64
|
||||||
gen func() ([]byte, error)
|
oneshot bool
|
||||||
|
gen func(context.Context) (<-chan []byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -55,35 +55,40 @@ func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
|
|||||||
var properties = make([]ITProperty, 6)
|
var properties = make([]ITProperty, 6)
|
||||||
|
|
||||||
func BuildProperties(dev *infinitime.Device) {
|
func BuildProperties(dev *infinitime.Device) {
|
||||||
properties[0] = ITProperty{"heartrate", 2,
|
properties[0] = ITProperty{"heartrate", 2, true,
|
||||||
func() ([]byte, error) {
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
ans, err := dev.HeartRate()
|
ans, err := dev.WatchHeartRate(ctx)
|
||||||
return []byte(strconv.Itoa(int(ans)) + "\n"), err
|
return converterU8(ctx, ans), err
|
||||||
}}
|
}}
|
||||||
properties[1] = ITProperty{"battery", 3,
|
properties[1] = ITProperty{"battery", 3, true,
|
||||||
func() ([]byte, error) {
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
ans, err := dev.BatteryLevel()
|
ans, err := dev.WatchBatteryLevel(ctx)
|
||||||
return []byte(strconv.Itoa(int(ans)) + "\n"), err
|
return converterU8(ctx, ans), err
|
||||||
}}
|
}}
|
||||||
properties[2] = ITProperty{"motion", 4,
|
properties[2] = ITProperty{"motion", 4, true,
|
||||||
func() ([]byte, error) {
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
ans, err := dev.Motion()
|
ans, err := dev.WatchMotion(ctx)
|
||||||
return []byte(strconv.Itoa(int(ans.X)) + " " + strconv.Itoa(int(ans.Y)) + " " + strconv.Itoa(int(ans.Z)) + "\n"), err
|
return converterMotionValues(ctx, ans), err
|
||||||
}}
|
}}
|
||||||
properties[3] = ITProperty{"stepcount", 6,
|
properties[3] = ITProperty{"motion", 5, true,
|
||||||
func() ([]byte, error) {
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
ans, err := dev.StepCount()
|
ans, err := dev.WatchMotion(ctx)
|
||||||
return []byte(strconv.Itoa(int(ans)) + "\n"), err
|
return converterMotionValues(ctx, ans), err
|
||||||
}}
|
}}
|
||||||
properties[4] = ITProperty{"version", 7,
|
properties[4] = ITProperty{"stepcount", 6, true,
|
||||||
func() ([]byte, error) {
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
|
ans, err := dev.WatchStepCount(ctx)
|
||||||
|
return converterU32(ctx, ans), err
|
||||||
|
}}
|
||||||
|
properties[5] = ITProperty{"version", 7, true,
|
||||||
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
ans, err := dev.Version()
|
ans, err := dev.Version()
|
||||||
return []byte(ans + "\n"), err
|
return converter1String(ctx, ans), err
|
||||||
}}
|
}}
|
||||||
properties[5] = ITProperty{"address", 8,
|
properties[6] = ITProperty{"address", 8, true,
|
||||||
func() ([]byte, error) {
|
func(ctx context.Context) (<-chan []byte, error) {
|
||||||
ans := dev.Address()
|
ans := dev.Address()
|
||||||
return []byte(ans + "\n"), nil
|
return converter1String(ctx, ans), nil
|
||||||
}}
|
}}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -265,20 +270,18 @@ func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64)
|
|||||||
}
|
}
|
||||||
|
|
||||||
type sensorFileReadHandle struct {
|
type sensorFileReadHandle struct {
|
||||||
content []byte
|
ch <-chan []byte
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
var _ fs.FileReader = (*sensorFileReadHandle)(nil)
|
var _ fs.FileReader = (*sensorFileReadHandle)(nil)
|
||||||
func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
||||||
log.Info("Executing Read").Int("size", len(fh.content)).Send()
|
content := <-fh.ch
|
||||||
end := off + int64(len(dest))
|
return fuse.ReadResultData(content), 0
|
||||||
if end > int64(len(fh.content)) {
|
|
||||||
end = int64(len(fh.content))
|
|
||||||
}
|
|
||||||
return fuse.ReadResultData(fh.content[off:end]), 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ fs.FileFlusher = (*sensorFileReadHandle)(nil)
|
var _ fs.FileFlusher = (*sensorFileReadHandle)(nil)
|
||||||
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
|
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
|
||||||
|
fh.cancel()
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -430,13 +433,15 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
|
|||||||
|
|
||||||
for _, value := range properties {
|
for _, value := range properties {
|
||||||
if value.Ino == f.Ino {
|
if value.Ino == f.Ino {
|
||||||
ans, err := value.gen()
|
sub_ctx, cancel := context.WithCancel(ctx)
|
||||||
|
ans, err := value.gen(sub_ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, syscallErr(err)
|
return nil, 0, syscallErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fh = &sensorFileReadHandle{
|
fh = &sensorFileReadHandle{
|
||||||
content : ans,
|
ch: ans,
|
||||||
|
cancel : cancel,
|
||||||
}
|
}
|
||||||
return fh, fuse.FOPEN_DIRECT_IO, 0
|
return fh, fuse.FOPEN_DIRECT_IO, 0
|
||||||
}
|
}
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
package fusefs
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "unsafe"
|
|
||||||
"github.com/hanwen/go-fuse/v2/fuse"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Unmount(mountPoint string) error {
|
|
||||||
return unmount(mountPoint, &fuse.MountOptions{DirectMount: false})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unfortunately, the FUSE library does not export its unmount function,
|
|
||||||
// so this is required until that changes
|
|
||||||
//go:linkname unmount github.com/hanwen/go-fuse/v2/fuse.unmount
|
|
||||||
func unmount(mountPoint string, opts *fuse.MountOptions) error
|
|
Loading…
Reference in New Issue
Block a user