fixed hwinfo to use global var and repaired reactor side interfaces
parent
88e70503d1
commit
9392892ede
@ -1,7 +1,7 @@
|
||||
// package i2c wraps the [i2c-tools] commands to interact
|
||||
// with devices on the buss
|
||||
// package i2c wraps the [i2c package] to interact with the i2c
|
||||
// with devices on the bus
|
||||
//
|
||||
// [i2c-tools]: https://manpages.debian.org/unstable/i2c-tools/index.html
|
||||
// [i2c package]: https://pkg.go.dev/periph.io/x/conn/v3/i2c#pkg-overview
|
||||
package i2c
|
||||
|
||||
import (
|
@ -1,118 +1,145 @@
|
||||
// package system uses linux commands to get hardware info from devices
|
||||
// package system uses linux ip command to get hardware info from devices
|
||||
package system
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetId() (int, error) {
|
||||
// gets the mac address and hashes into consistent id
|
||||
maccmd := fmt.Sprintf("ifconfig %v | awk '/ether / {print $2}'", et)
|
||||
var stderr bytes.Buffer
|
||||
var out bytes.Buffer
|
||||
cmd := exec.Command("bash", "-c", maccmd)
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
hash := fnv.New32a()
|
||||
hash.Write(out.Bytes())
|
||||
id := hash.Sum32()
|
||||
return int(id), nil
|
||||
var (
|
||||
ErrBusNotFound = errors.New("bus not found for device")
|
||||
ErrNoNetworkInterface = errors.New("no default network found")
|
||||
)
|
||||
|
||||
var HardwareInfo = &hardwareInfo{}
|
||||
|
||||
type hardwareInfo struct {
|
||||
MAC string
|
||||
IP string
|
||||
ID int
|
||||
Model string
|
||||
}
|
||||
|
||||
// NetInterfaces is a struct to unmarshal the ip command json.
|
||||
type netInterfaces []network
|
||||
|
||||
// Networks holds relevant information for each network interface.
|
||||
// Used to unmarshal ip command json.
|
||||
type network struct {
|
||||
Subnets []netInfo `json:"addr_info"`
|
||||
Mac string `json:"address"`
|
||||
Group string `json:"group"`
|
||||
State string `json:"operstate"`
|
||||
}
|
||||
|
||||
type netInfo struct {
|
||||
Family string `json:"family"`
|
||||
Ip string `json:"local"`
|
||||
}
|
||||
|
||||
func GetIp() (string, error) {
|
||||
ipcmd := "ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q'"
|
||||
var stderr bytes.Buffer
|
||||
var out bytes.Buffer
|
||||
cmd := exec.Command("bash", "-c", ipcmd)
|
||||
cmd.Stdout = &out
|
||||
func getInfo() error {
|
||||
|
||||
var stderr, stdout bytes.Buffer
|
||||
cmd := exec.Command("ip", "-j", "a")
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
ip := strings.Trim(out.String(), " \n")
|
||||
return ip, nil
|
||||
|
||||
}
|
||||
var networks netInterfaces
|
||||
|
||||
func GetPort() (int, error) {
|
||||
// obsolete
|
||||
if addr, err := net.ResolveTCPAddr("tcp", ":0"); err != nil {
|
||||
return 0, err
|
||||
} else if lis, err := net.ListenTCP("tcp", addr); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
defer lis.Close()
|
||||
return lis.Addr().(*net.TCPAddr).Port, nil
|
||||
if err := json.Unmarshal(stdout.Bytes(), &networks); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// loop over found networks finding first default, IPv4 network currently
|
||||
// UP (active).
|
||||
// Eventually need to look only at wg interface which simplifies
|
||||
// implementation.
|
||||
|
||||
for _, network := range networks {
|
||||
if network.Group == "default" && network.State == "UP" {
|
||||
|
||||
for _, subnet := range network.Subnets {
|
||||
if subnet.Family == "inet" {
|
||||
|
||||
hash := fnv.New32a()
|
||||
hash.Write([]byte(network.Mac))
|
||||
id := hash.Sum32()
|
||||
|
||||
HardwareInfo.MAC = network.Mac
|
||||
HardwareInfo.IP = subnet.Ip
|
||||
HardwareInfo.ID = int(id)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetBus() (int, error) {
|
||||
// preset busses
|
||||
busList := map[string]int{"raspberrypi": 1, "beaglebone": 2}
|
||||
// vars
|
||||
var bus int
|
||||
var ok bool
|
||||
func GetID() (int, error) {
|
||||
|
||||
if name, err =: GetModel(); err != nil {
|
||||
return bus, err
|
||||
} else if bus, ok = busList[b]; !ok {
|
||||
return 0, errors.New(fmt.Sprintf("No bus for dev %s", b))
|
||||
if HardwareInfo.ID == 0 {
|
||||
if err := getInfo(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
// returns correct bus
|
||||
return bus, nil
|
||||
return HardwareInfo.ID, nil
|
||||
}
|
||||
|
||||
func GetModel() (string, error) {
|
||||
var stderr, out bytes.Buffer
|
||||
cmd := exec.Command("bash", "-c", "hostname")
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
func GetIP() (string, error) {
|
||||
|
||||
if HardwareInfo.IP == "" {
|
||||
if err := getInfo(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
b := out.String()
|
||||
b = strings.Trim(b, " \n")
|
||||
return b, nil
|
||||
|
||||
return HardwareInfo.IP, nil
|
||||
}
|
||||
|
||||
func Get() error {
|
||||
// responsible for filling out struct
|
||||
//bus := map[string]int{"raspberrypi":1,"beaglebone":2} // eventually will replace this with a config file
|
||||
func GetModel() (string, error) {
|
||||
|
||||
ipcmd := "ifconfig eth0 | awk '/inet / {print $2}'"
|
||||
maccmd := "ifconfig eth0 | awk '/ether / {print $2}'"
|
||||
devcmd := "lshw -C system 2>/dev/null | head -n 1"
|
||||
if HardwareInfo.Model == "" {
|
||||
|
||||
res := [3]bytes.Buffer{}
|
||||
var stderr bytes.Buffer
|
||||
cmds := []string{ipcmd, maccmd, devcmd}
|
||||
for i, c := range cmds {
|
||||
cmd := exec.Command("bash", "-c", c)
|
||||
cmd.Stdout = &res[i]
|
||||
var stderr, out bytes.Buffer
|
||||
cmd := exec.Command("uname", "-n")
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b := out.String()
|
||||
HardwareInfo.Model = strings.Trim(b, " \n")
|
||||
}
|
||||
// formatting
|
||||
ip := res[0].String()
|
||||
ip = strings.Trim(ip, " \n")
|
||||
|
||||
hash := fnv.New32a()
|
||||
hash.Write(res[1].Bytes())
|
||||
return HardwareInfo.Model, nil
|
||||
}
|
||||
|
||||
b := res[2].String()
|
||||
b = strings.Trim(b, " \n")
|
||||
return nil
|
||||
func GetBus() (int, error) {
|
||||
// preset busses
|
||||
busList := map[string]int{"raspberrypi": 1, "beaglebone": 2}
|
||||
// vars
|
||||
var bus int
|
||||
var ok bool
|
||||
|
||||
if name, err := GetModel(); err != nil {
|
||||
return bus, err
|
||||
} else if bus, ok = busList[name]; !ok {
|
||||
return 0, ErrBusNotFound
|
||||
}
|
||||
|
||||
return bus, nil
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetInfo(t *testing.T) {
|
||||
info, err := GetInfo()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue