forked from Elara6331/itd
Compare commits
6 Commits
dc53ead339
...
fuse
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
008f6b35a9 | ||
|
|
98775600af | ||
|
|
613d33ff4d | ||
|
|
1a1bc30df9 | ||
|
|
c5a6e0d298 | ||
|
|
84c7a33c40 |
10
fuse.go
10
fuse.go
@@ -11,7 +11,15 @@ import (
|
||||
|
||||
func startFUSE(ctx context.Context, dev *infinitime.Device) error {
|
||||
// This is where we'll mount the FS
|
||||
os.Mkdir(k.String("fuse.mountpoint"), 0755)
|
||||
err := os.MkdirAll(k.String("fuse.mountpoint"), 0755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ignore the error because nothing might be mounted on the mountpoint
|
||||
_ = fusefs.Unmount(k.String("fuse.mountpoint"))
|
||||
|
||||
|
||||
root, err := fusefs.BuildRootNode(dev)
|
||||
if err != nil {
|
||||
log.Error("Building root node failed").
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
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,12 +10,13 @@ import (
|
||||
"github.com/hanwen/go-fuse/v2/fuse"
|
||||
"io"
|
||||
"bytes"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ITProperty struct {
|
||||
name string
|
||||
Ino uint64
|
||||
gen func(context.Context) (<-chan []byte, error)
|
||||
gen func() ([]byte, error)
|
||||
}
|
||||
|
||||
|
||||
@@ -55,34 +56,34 @@ var properties = make([]ITProperty, 6)
|
||||
|
||||
func BuildProperties(dev *infinitime.Device) {
|
||||
properties[0] = ITProperty{"heartrate", 2,
|
||||
func(ctx context.Context) (<-chan []byte, error) {
|
||||
ans, err := dev.WatchHeartRate(ctx)
|
||||
return converterU8(ctx, ans), err
|
||||
func() ([]byte, error) {
|
||||
ans, err := dev.HeartRate()
|
||||
return []byte(strconv.Itoa(int(ans)) + "\n"), err
|
||||
}}
|
||||
properties[1] = ITProperty{"battery", 3,
|
||||
func(ctx context.Context) (<-chan []byte, error) {
|
||||
ans, err := dev.WatchBatteryLevel(ctx)
|
||||
return converterU8(ctx, ans), err
|
||||
func() ([]byte, error) {
|
||||
ans, err := dev.BatteryLevel()
|
||||
return []byte(strconv.Itoa(int(ans)) + "\n"), err
|
||||
}}
|
||||
properties[2] = ITProperty{"motion", 4,
|
||||
func(ctx context.Context) (<-chan []byte, error) {
|
||||
ans, err := dev.WatchMotion(ctx)
|
||||
return converterMotionValues(ctx, ans), err
|
||||
func() ([]byte, error) {
|
||||
ans, err := dev.Motion()
|
||||
return []byte(strconv.Itoa(int(ans.X)) + " " + strconv.Itoa(int(ans.Y)) + " " + strconv.Itoa(int(ans.Z)) + "\n"), err
|
||||
}}
|
||||
properties[3] = ITProperty{"stepcount", 5,
|
||||
func(ctx context.Context) (<-chan []byte, error) {
|
||||
ans, err := dev.WatchStepCount(ctx)
|
||||
return converterU32(ctx, ans), err
|
||||
properties[3] = ITProperty{"stepcount", 6,
|
||||
func() ([]byte, error) {
|
||||
ans, err := dev.StepCount()
|
||||
return []byte(strconv.Itoa(int(ans)) + "\n"), err
|
||||
}}
|
||||
properties[4] = ITProperty{"version", 6,
|
||||
func(ctx context.Context) (<-chan []byte, error) {
|
||||
properties[4] = ITProperty{"version", 7,
|
||||
func() ([]byte, error) {
|
||||
ans, err := dev.Version()
|
||||
return converter1String(ctx, ans), err
|
||||
return []byte(ans + "\n"), err
|
||||
}}
|
||||
properties[5] = ITProperty{"address", 7,
|
||||
func(ctx context.Context) (<-chan []byte, error) {
|
||||
properties[5] = ITProperty{"address", 8,
|
||||
func() ([]byte, error) {
|
||||
ans := dev.Address()
|
||||
return converter1String(ctx, ans), nil
|
||||
return []byte(ans + "\n"), nil
|
||||
}}
|
||||
|
||||
}
|
||||
@@ -97,7 +98,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
||||
// root folder
|
||||
r := make([]fuse.DirEntry, 2)
|
||||
r[0] = fuse.DirEntry{
|
||||
Name: "device",
|
||||
Name: "info",
|
||||
Ino: 0,
|
||||
Mode: fuse.S_IFDIR,
|
||||
}
|
||||
@@ -109,7 +110,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
||||
return fs.NewListDirStream(r), 0
|
||||
|
||||
case 1:
|
||||
// device folder
|
||||
// info folder
|
||||
r := make([]fuse.DirEntry, 6)
|
||||
for ind, value := range properties {
|
||||
r[ind] = fuse.DirEntry{
|
||||
@@ -122,7 +123,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
||||
return fs.NewListDirStream(r), 0
|
||||
|
||||
case 2:
|
||||
// on device
|
||||
// on info
|
||||
files, err := myfs.ReadDir(n.path)
|
||||
if err != nil {
|
||||
log.Error("FUSE ReadDir failed").Str("path", n.path).Err(err).Send()
|
||||
@@ -179,7 +180,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
|
||||
switch n.kind {
|
||||
case 0:
|
||||
// root folder
|
||||
if name == "device" {
|
||||
if name == "info" {
|
||||
stable := fs.StableAttr{
|
||||
Mode: fuse.S_IFDIR,
|
||||
Ino: uint64(0),
|
||||
@@ -197,7 +198,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
|
||||
return child, 0
|
||||
}
|
||||
case 1:
|
||||
// device folder
|
||||
// info folder
|
||||
for _, value := range properties {
|
||||
if value.name == name {
|
||||
stable := fs.StableAttr{
|
||||
@@ -264,18 +265,20 @@ func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64)
|
||||
}
|
||||
|
||||
type sensorFileReadHandle struct {
|
||||
ch <-chan []byte
|
||||
cancel context.CancelFunc
|
||||
content []byte
|
||||
}
|
||||
var _ fs.FileReader = (*sensorFileReadHandle)(nil)
|
||||
func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
||||
content := <-fh.ch
|
||||
return fuse.ReadResultData(content), 0
|
||||
log.Info("Executing Read").Int("size", len(fh.content)).Send()
|
||||
end := off + int64(len(dest))
|
||||
if end > int64(len(fh.content)) {
|
||||
end = int64(len(fh.content))
|
||||
}
|
||||
return fuse.ReadResultData(fh.content[off:end]), 0
|
||||
}
|
||||
|
||||
var _ fs.FileFlusher = (*sensorFileReadHandle)(nil)
|
||||
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
|
||||
fh.cancel()
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -427,15 +430,13 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
|
||||
|
||||
for _, value := range properties {
|
||||
if value.Ino == f.Ino {
|
||||
sub_ctx, cancel := context.WithCancel(ctx)
|
||||
ans, err := value.gen(sub_ctx)
|
||||
ans, err := value.gen()
|
||||
if err != nil {
|
||||
return nil, 0, syscallErr(err)
|
||||
}
|
||||
|
||||
fh = &sensorFileReadHandle{
|
||||
ch: ans,
|
||||
cancel : cancel,
|
||||
content : ans,
|
||||
}
|
||||
return fh, fuse.FOPEN_DIRECT_IO, 0
|
||||
}
|
||||
|
||||
15
internal/fusefs/unmount.go
Normal file
15
internal/fusefs/unmount.go
Normal file
@@ -0,0 +1,15 @@
|
||||
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
|
||||
Reference in New Issue
Block a user