2023-02-19 11:50:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"context"
|
|
|
|
"syscall"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2023-02-19 12:59:40 +00:00
|
|
|
"strconv"
|
2023-02-19 11:50:02 +00:00
|
|
|
)
|
|
|
|
|
2023-02-19 12:59:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-19 12:08:56 +00:00
|
|
|
type ITProperty struct {
|
|
|
|
name string
|
|
|
|
Ino uint64
|
2023-02-19 12:59:40 +00:00
|
|
|
f func() ([]byte, error)
|
2023-02-19 12:08:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 11:50:02 +00:00
|
|
|
type ITNode struct {
|
|
|
|
fs.Inode
|
|
|
|
kind int
|
2023-02-19 12:23:18 +00:00
|
|
|
Ino uint64
|
2023-02-19 11:50:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 12:08:56 +00:00
|
|
|
var properties = []ITProperty {
|
2023-02-19 12:59:40 +00:00
|
|
|
ITProperty{"heartrate", 2, nil},
|
|
|
|
ITProperty{"battery", 3, nil},
|
|
|
|
ITProperty{"motion", 4, nil},
|
|
|
|
ITProperty{"stepcount", 5, nil},
|
|
|
|
ITProperty{"version", 6, nil},
|
|
|
|
ITProperty{"address", 7, nil},
|
2023-02-19 12:08:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 11:50:02 +00:00
|
|
|
var _ = (fs.NodeReaddirer)((*ITNode)(nil))
|
|
|
|
|
|
|
|
// Readdir is part of the NodeReaddirer interface
|
|
|
|
func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
2023-02-19 12:08:45 +00:00
|
|
|
switch n.kind {
|
|
|
|
case 0:
|
2023-02-19 11:50:02 +00:00
|
|
|
// root folder
|
|
|
|
r := make([]fuse.DirEntry, 2)
|
|
|
|
r[0] = fuse.DirEntry{
|
|
|
|
Name: "device",
|
|
|
|
Ino: 0,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
}
|
|
|
|
r[1] = fuse.DirEntry{
|
|
|
|
Name: "fs",
|
|
|
|
Ino: 1,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
}
|
|
|
|
return fs.NewListDirStream(r), 0
|
2023-02-19 12:08:56 +00:00
|
|
|
|
|
|
|
case 1:
|
|
|
|
// device folder
|
|
|
|
r := make([]fuse.DirEntry, 6)
|
|
|
|
for ind, value := range properties {
|
|
|
|
r[ind] = fuse.DirEntry{
|
|
|
|
Name: value.name,
|
|
|
|
Ino: value.Ino,
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.NewListDirStream(r), 0
|
2023-02-19 11:50:02 +00:00
|
|
|
}
|
|
|
|
r := make([]fuse.DirEntry, 0)
|
|
|
|
return fs.NewListDirStream(r), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = (fs.NodeLookuper)((*ITNode)(nil))
|
|
|
|
func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
|
2023-02-19 12:08:45 +00:00
|
|
|
switch n.kind {
|
|
|
|
case 0:
|
|
|
|
// root folder
|
2023-02-19 11:50:02 +00:00
|
|
|
if name == "device" {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: uint64(0),
|
|
|
|
}
|
2023-02-19 12:23:18 +00:00
|
|
|
operations := &ITNode{kind: 1, Ino: 0}
|
2023-02-19 11:50:02 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
} else if name == "fs" {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: uint64(1),
|
|
|
|
}
|
2023-02-19 12:23:18 +00:00
|
|
|
operations := &ITNode{kind: 2, Ino: 1}
|
2023-02-19 11:50:02 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
}
|
2023-02-19 12:08:56 +00:00
|
|
|
case 1:
|
|
|
|
// device folder
|
|
|
|
for _, value := range properties {
|
|
|
|
if value.name == name {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
Ino: uint64(value.Ino),
|
|
|
|
}
|
2023-02-19 12:23:18 +00:00
|
|
|
operations := &ITNode{kind: 3, Ino: value.Ino}
|
2023-02-19 12:08:56 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, syscall.ENOENT
|
2023-02-19 11:50:02 +00:00
|
|
|
}
|
|
|
|
return nil, syscall.ENOENT
|
|
|
|
}
|
|
|
|
|
2023-02-19 12:23:44 +00:00
|
|
|
type bytesFileHandle struct {
|
|
|
|
content []byte
|
|
|
|
}
|
|
|
|
var _ = (fs.FileReader)((*bytesFileHandle)(nil))
|
|
|
|
|
|
|
|
func (fh *bytesFileHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
|
|
|
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.NodeOpener)((*ITNode)(nil))
|
|
|
|
func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
|
|
|
|
switch f.kind {
|
|
|
|
case 3:
|
|
|
|
// Device file
|
|
|
|
|
|
|
|
// disallow writes
|
|
|
|
if fuseFlags&(syscall.O_RDWR|syscall.O_WRONLY) != 0 {
|
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range properties {
|
|
|
|
if value.Ino == f.Ino {
|
2023-02-19 12:59:40 +00:00
|
|
|
ans, err := value.f()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
|
|
|
|
2023-02-19 12:23:44 +00:00
|
|
|
fh = &bytesFileHandle{
|
2023-02-19 12:59:40 +00:00
|
|
|
content: ans,
|
2023-02-19 12:23:44 +00:00
|
|
|
}
|
|
|
|
return fh, fuse.FOPEN_DIRECT_IO, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
2023-02-19 11:50:02 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
// This is where we'll mount the FS
|
|
|
|
mntDir := "/tmp/x"
|
|
|
|
os.Mkdir(mntDir, 0755)
|
|
|
|
root := &ITNode{kind: 0}
|
|
|
|
server, err := fs.Mount(mntDir, root, &fs.Options{
|
|
|
|
MountOptions: fuse.MountOptions{
|
|
|
|
// Set to true to see how the file system works.
|
|
|
|
Debug: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2023-02-19 12:59:40 +00:00
|
|
|
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;
|
|
|
|
|
2023-02-19 11:50:02 +00:00
|
|
|
log.Printf("Mounted on %s", mntDir)
|
|
|
|
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)
|
|
|
|
|
|
|
|
// Wait until unmount before exiting
|
|
|
|
server.Wait()
|
|
|
|
}
|