From 4a18622d37ec429a763eec73bf14ea146ae40dc7 Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sun, 19 Feb 2023 12:59:40 +0000 Subject: [PATCH] Added functions for variables --- fuse/main.go | 56 +++++++++++++++++++++++++++++++++++++++++++++------- fuse/mock.go | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 fuse/mock.go diff --git a/fuse/main.go b/fuse/main.go index b6c762f..938ac04 100644 --- a/fuse/main.go +++ b/fuse/main.go @@ -7,11 +7,39 @@ import ( "syscall" "github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fuse" + "strconv" ) +func (i *Device) HeartRateBytes() ([]byte, error) { + v, err := i.HeartRate() + return []byte(strconv.Itoa(int(v)) + "\n"), err +} +func (i *Device) BatteryLevelBytes() ([]byte, error) { + v, err := i.BatteryLevel() + return []byte(strconv.Itoa(int(v)) + "\n"), err +} +func (i *Device) StepCountBytes() ([]byte, error) { + v, err := i.StepCount() + return []byte(strconv.Itoa(int(v)) + "\n"), err +} +func (i *Device) MotionBytes() ([]byte, error) { + v, err := i.Motion() + return []byte(strconv.Itoa(int(v.X)) + " " + strconv.Itoa(int(v.Y)) + " " + strconv.Itoa(int(v.Z)) + "\n"), err +} +func (i *Device) AddressBytes() ([]byte, error) { + v := i.Address() + return []byte(v + "\n"), nil +} +func (i *Device) VersionBytes() ([]byte, error) { + v, err := i.Version() + return []byte(v + "\n"), err +} + + type ITProperty struct { name string Ino uint64 + f func() ([]byte, error) } type ITNode struct { @@ -21,12 +49,12 @@ type ITNode struct { } var properties = []ITProperty { - ITProperty{"heartrate", 2}, - ITProperty{"battery", 3}, - ITProperty{"motion", 4}, - ITProperty{"stepcount", 5}, - ITProperty{"version", 6}, - ITProperty{"address", 7}, + ITProperty{"heartrate", 2, nil}, + ITProperty{"battery", 3, nil}, + ITProperty{"motion", 4, nil}, + ITProperty{"stepcount", 5, nil}, + ITProperty{"version", 6, nil}, + ITProperty{"address", 7, nil}, } var _ = (fs.NodeReaddirer)((*ITNode)(nil)) @@ -132,8 +160,13 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, for _, value := range properties { if value.Ino == f.Ino { + ans, err := value.f() + if err != nil { + return nil, 0, syscall.EROFS + } + fh = &bytesFileHandle{ - content: []byte(value.name), + content: ans, } return fh, fuse.FOPEN_DIRECT_IO, 0 } @@ -157,6 +190,15 @@ func main() { log.Panic(err) } + dev := Device{}; + + properties[0].f = dev.HeartRateBytes; + properties[1].f = dev.BatteryLevelBytes; + properties[2].f = dev.MotionBytes; + properties[3].f = dev.StepCountBytes; + properties[4].f = dev.VersionBytes; + properties[5].f = dev.AddressBytes; + log.Printf("Mounted on %s", mntDir) log.Printf("Unmount by calling 'fusermount -u %s'", mntDir) diff --git a/fuse/mock.go b/fuse/mock.go new file mode 100644 index 0000000..7df66b1 --- /dev/null +++ b/fuse/mock.go @@ -0,0 +1,35 @@ +package main + +type Device struct { + +} + +func (i *Device) HeartRate() (uint8, error) { + return 10, nil +} + +func (i *Device) BatteryLevel() (uint8, error) { + return 95, nil +} + +func (i *Device) StepCount() (uint32, error) { + return 27000, nil +} + +type MotionValues struct { + X int16 + Y int16 + Z int16 +} + +func (i *Device) Motion() (MotionValues, error) { + return MotionValues{-12, +64, -19}, nil +} + +func (i *Device) Address() string { + return "FA:E4:00:00:00:00" +} + +func (i *Device) Version() (string, error) { + return "1.11.0", nil +}