func ensureFeatureEnabled(id string) error { glog.V(3).Infof("bbb: enabling feature %v", id) pattern := "/sys/devices/bone_capemgr.*/slots" file, err := embd.FindFirstMatchingFile(pattern) if err != nil { return err } bytes, err := ioutil.ReadFile(file) if err != nil { return err } str := string(bytes) if strings.Contains(str, id) { glog.V(3).Infof("bbb: feature %v already enabled", id) return nil } slots, err := os.OpenFile(file, os.O_WRONLY, os.ModeExclusive) if err != nil { return err } defer slots.Close() glog.V(3).Infof("bbb: writing %v to slots file", id) _, err = slots.WriteString(id) return err }
// This cannot be currently used to disable things like the // analog and pwm modules. Removing them from slots file can // potentially cause a kernel panic and unsettle things. So the // recommended thing to do is to simply reboot. func ensureFeatureDisabled(id string) error { pattern := "/sys/devices/bone_capemgr.*/slots" file, err := embd.FindFirstMatchingFile(pattern) if err != nil { return err } slots, err := os.OpenFile(file, os.O_RDWR, os.ModeExclusive) if err != nil { return err } defer slots.Close() scanner := bufio.NewScanner(slots) for scanner.Scan() { text := scanner.Text() if !strings.Contains(text, id) { continue } // Extract the id from the line idx := strings.Index(text, ":") if idx < 0 { // Something is off, bail continue } dis := strings.TrimSpace(text[:idx]) slots.Seek(0, 0) _, err = slots.WriteString("-" + dis) return err } // Could not disable the feature return fmt.Errorf("embd: could not disable feature %q", id) }
func (p *pwmPin) basePath() (string, error) { pattern := "/sys/devices/ocp.*/pwm_test_" + p.n + ".*" return embd.FindFirstMatchingFile(pattern) }
func (p *analogPin) valueFilePath() (string, error) { pattern := fmt.Sprintf("/sys/devices/ocp.*/helper.*/AIN%v", p.n) return embd.FindFirstMatchingFile(pattern) }