Anyone know of a programmatic way (as in within C or Go code) to

Anyone know of a programmatic way (as in within C or Go code) to apply a device tree overlay, i.e., “echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots” (which works fine in the shell)? I’ve tried pipes and file copies, etc. but nothing seems to be working…

Here’s my code:

package bbbdevtreeovly

import (
// “io”
“bytes”
“fmt”
“io/ioutil”
“log”
“os”
“os/exec”
“strings”
“path/filepath”
)

const (
slotsDir string = /sys/devices/ // Find BBB’s slots
)

func Load_dto(dtoFileName string, dtoFilePath string) (err error) {

log.SetPrefix(`BBB Device Tree Overlay: `)

slotFiles := make([]os.FileInfo, 100)

slotFiles, err = ioutil.ReadDir(slotsDir)

var i int

for i = 0; i < len(slotFiles); i++ {
	//log.Println(slotFiles[i].Name())
	found, _ := filepath.Match(`bone_capemgr.*`, slotFiles[i].Name())
	if found {
		break
	}
}

slotsLoc := filepath.Join(slotsDir, slotFiles[i].Name(), `slots`)

log.Println("DTO File: ", dtoFileName, "\n")

slots, err := os.Open(slotsLoc)
if err != nil {
	log.Println(err)
	return err
}
defer slots.Close()

log.Println("Slots location: ", slots.Name(), "\n")

cmd := exec.Command(slotsLoc)

cmd.Stdin = strings.NewReader(dtoFileName)

var out bytes.Buffer
cmd.Stdout = &out

// cmd.StdinPipe(dtoFile)

err = cmd.Run()
if err != nil {
	log.Println(err)
	return err
}

/*
	io.Copy(slots, dtoFile)
	if err != nil {
		log.Println(err)
		return err
	}
*/

fmt.Println(out.String())

return nil

}

I get “permission denied”… See below…

root@beaglebone:~#./fmserve
BBB Device Tree Overlay: 2013/12/01 17:08:47 DTO File: cape-bone-iio

BBB Device Tree Overlay: 2013/12/01 17:08:47 Slots location: /sys/devices/bone_capemgr.9/slots

BBB Device Tree Overlay: 2013/12/01 17:08:47 exec: “/sys/devices/bone_capemgr.9/slots”: permission denied
panic: exec: “/sys/devices/bone_capemgr.9/slots”: permission denied

goroutine 1 [running]:
main.main()
/home/dad/go/src/davidsonff/fmserve/fmserve.go:16 +0x94

goroutine 2 [runnable]:

Based on the error message it looks like you are trying to run “/sys/devices/bone_capemgr.9/slots” as an executable program.

@Johan_Myreen Yes, I suspect, but I’ve also tried a programatic file copy and pipe to no effect. $SLOTS is not just a file, right? Is there a better explanation of what this is somewhere?

@MirandaSoft I saw this, thanks! I am trying to figure out how to do this from Go, actually, rather than a shell script. Perhaps it is not possible or I have simply not exploited all of the file methods yet…

@Frank_Davidson If you are trying to achieve the equivalent of the “echo” command you should not try to cmd.Run() the target file, just write to it. I.e. something like

slots,err := os.Open(slotsLoc)
slots.Write(thestring)

OK, tried writing out directly, still “Permission Denied”…

Here’s the code:

package bbbdevtreeovly

import (
// “io”
// “bytes”
// “fmt”
“io/ioutil”
“log”
“os”
// “os/exec”
// “strings”
“path/filepath”
)

const (
slotsDir string = /sys/devices/ // Find BBB’s slots
)

func Load_dto(dtoFileName string, dtoFilePath string) (err error) {

log.SetPrefix(`BBB Device Tree Overlay: `)

slotFiles := make([]os.FileInfo, 100)

slotFiles, err = ioutil.ReadDir(slotsDir)

var i int

for i = 0; i < len(slotFiles); i++ {
	found, _ := filepath.Match(`bone_capemgr.*`, slotFiles[i].Name())
	if found {
		break
	}
}

slotsLoc := filepath.Join(slotsDir, slotFiles[i].Name(), `slots`)

log.Println("DTO File: ", dtoFileName, "\n")

slots, err := os.Open(slotsLoc)
if err != nil {
	log.Println(err)
	return err
}
defer slots.Close()

log.Println("Slots location: ", slots.Name(), "\n")

slots.Write([]byte(dtoFileName))

return nil

}

Here’s the output:

root@beaglebone:~#./fmserve
BBB Device Tree Overlay: 2013/12/14 19:31:35 DTO File: cape-bone-iio

BBB Device Tree Overlay: 2013/12/14 19:31:35 Slots location: /sys/devices/bone_capemgr.9/slots

BBB Device Tree Overlay: 2013/12/14 19:31:35 exec: “/sys/devices/bone_capemgr.9/slots”: permission denied
panic: exec: “/sys/devices/bone_capemgr.9/slots”: permission denied

goroutine 1 [running]:
main.main()
/home/dad/go/src/davidsonff/fmserve/fmserve.go:16 +0x94
root@beaglebone:~#

Any ideas are welcome!

Are you sure you are running this version of the program, and not some older one? The output is identical, except for the timestamps.

OK. That was a problem! Now I’m not getting the “permissions denied” error, but it is still not loading the device tree overlay…

Here is the code:

package bbbdevtreeovly

import (
// “io”
// “bytes”
// “fmt”
“io/ioutil”
“log”
“os”
// “os/exec”
// “strings”
“path/filepath”
)

const (
slotsDir string = /sys/devices/ // Find BBB’s slots
)

func Load_dto(dtoFileName string, dtoFilePath string) (err error) {

log.SetPrefix(`BBB Device Tree Overlay: `)

slotFiles := make([]os.FileInfo, 100)

slotFiles, err = ioutil.ReadDir(slotsDir)

var i int

for i = 0; i < len(slotFiles); i++ {
	found, _ := filepath.Match(`bone_capemgr.*`, slotFiles[i].Name())
	if found {
		break
	}
}

log.Println("This is the new program...", "\n")

slotsLoc := filepath.Join(slotsDir, slotFiles[i].Name(), `slots`)

log.Println("DTO File: ", dtoFileName, "\n")

slots, err := os.Open(slotsLoc)
if err != nil {
	log.Println(err)
	return err
}
defer slots.Close()

log.Println("Slots location: ", slots.Name(), "\n")

slots.Write([]byte(dtoFileName))

return nil

}

Here is the output:

root@beaglebone:~#./fmserve
BBB Device Tree Overlay: 2013/12/15 16:18:59 This is the new program…

BBB Device Tree Overlay: 2013/12/15 16:18:59 DTO File: cape-bone-iio

BBB Device Tree Overlay: 2013/12/15 16:18:59 Slots location: /sys/devices/bone_capemgr.9/slots

Success!
root@beaglebone:~# cat /sys/devices/bone_capemgr.8/slots
cat: /sys/devices/bone_capemgr.8/slots: No such file or directory
root@beaglebone:~# cat /sys/devices/bone_capemgr.9/slots
0: 54:PF—
1: 55:PF—
2: 56:PF—
3: 57:PF—
4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G
5: ff:P-O-L Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI
root@beaglebone:~#