Added functions for variables

This commit is contained in:
Yannick Ulrich 2023-02-19 12:59:40 +00:00
parent 5008cdd4f4
commit 4a18622d37
2 changed files with 84 additions and 7 deletions

View File

@ -7,11 +7,39 @@ import (
"syscall" "syscall"
"github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse" "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 { type ITProperty struct {
name string name string
Ino uint64 Ino uint64
f func() ([]byte, error)
} }
type ITNode struct { type ITNode struct {
@ -21,12 +49,12 @@ type ITNode struct {
} }
var properties = []ITProperty { var properties = []ITProperty {
ITProperty{"heartrate", 2}, ITProperty{"heartrate", 2, nil},
ITProperty{"battery", 3}, ITProperty{"battery", 3, nil},
ITProperty{"motion", 4}, ITProperty{"motion", 4, nil},
ITProperty{"stepcount", 5}, ITProperty{"stepcount", 5, nil},
ITProperty{"version", 6}, ITProperty{"version", 6, nil},
ITProperty{"address", 7}, ITProperty{"address", 7, nil},
} }
var _ = (fs.NodeReaddirer)((*ITNode)(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 { for _, value := range properties {
if value.Ino == f.Ino { if value.Ino == f.Ino {
ans, err := value.f()
if err != nil {
return nil, 0, syscall.EROFS
}
fh = &bytesFileHandle{ fh = &bytesFileHandle{
content: []byte(value.name), content: ans,
} }
return fh, fuse.FOPEN_DIRECT_IO, 0 return fh, fuse.FOPEN_DIRECT_IO, 0
} }
@ -157,6 +190,15 @@ func main() {
log.Panic(err) 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("Mounted on %s", mntDir)
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir) log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)

35
fuse/mock.go Normal file
View File

@ -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
}