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
|
// package i2c wraps the [i2c package] to interact with the i2c
|
||||||
// with devices on the buss
|
// 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
|
package i2c
|
||||||
|
|
||||||
import (
|
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
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"net"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetId() (int, error) {
|
var (
|
||||||
// gets the mac address and hashes into consistent id
|
ErrBusNotFound = errors.New("bus not found for device")
|
||||||
maccmd := fmt.Sprintf("ifconfig %v | awk '/ether / {print $2}'", et)
|
ErrNoNetworkInterface = errors.New("no default network found")
|
||||||
var stderr bytes.Buffer
|
)
|
||||||
var out bytes.Buffer
|
|
||||||
cmd := exec.Command("bash", "-c", maccmd)
|
var HardwareInfo = &hardwareInfo{}
|
||||||
cmd.Stdout = &out
|
|
||||||
cmd.Stderr = &stderr
|
type hardwareInfo struct {
|
||||||
if err := cmd.Run(); err != nil {
|
MAC string
|
||||||
return 0, err
|
IP string
|
||||||
}
|
ID int
|
||||||
hash := fnv.New32a()
|
Model string
|
||||||
hash.Write(out.Bytes())
|
}
|
||||||
id := hash.Sum32()
|
|
||||||
return int(id), nil
|
// 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) {
|
func getInfo() error {
|
||||||
ipcmd := "ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q'"
|
|
||||||
var stderr bytes.Buffer
|
var stderr, stdout bytes.Buffer
|
||||||
var out bytes.Buffer
|
cmd := exec.Command("ip", "-j", "a")
|
||||||
cmd := exec.Command("bash", "-c", ipcmd)
|
cmd.Stdout = &stdout
|
||||||
cmd.Stdout = &out
|
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
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) {
|
if err := json.Unmarshal(stdout.Bytes(), &networks); err != nil {
|
||||||
// obsolete
|
return err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
func GetID() (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 {
|
if HardwareInfo.ID == 0 {
|
||||||
return bus, err
|
if err := getInfo(); err != nil {
|
||||||
} else if bus, ok = busList[b]; !ok {
|
return 0, err
|
||||||
return 0, errors.New(fmt.Sprintf("No bus for dev %s", b))
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns correct bus
|
return HardwareInfo.ID, nil
|
||||||
return bus, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetModel() (string, error) {
|
func GetIP() (string, error) {
|
||||||
var stderr, out bytes.Buffer
|
|
||||||
cmd := exec.Command("bash", "-c", "hostname")
|
if HardwareInfo.IP == "" {
|
||||||
cmd.Stdout = &out
|
if err := getInfo(); err != nil {
|
||||||
cmd.Stderr = &stderr
|
return "", err
|
||||||
if err := cmd.Run(); err != nil {
|
}
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
b := out.String()
|
|
||||||
b = strings.Trim(b, " \n")
|
return HardwareInfo.IP, nil
|
||||||
return b, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get() error {
|
func GetModel() (string, error) {
|
||||||
// responsible for filling out struct
|
|
||||||
//bus := map[string]int{"raspberrypi":1,"beaglebone":2} // eventually will replace this with a config file
|
|
||||||
|
|
||||||
ipcmd := "ifconfig eth0 | awk '/inet / {print $2}'"
|
if HardwareInfo.Model == "" {
|
||||||
maccmd := "ifconfig eth0 | awk '/ether / {print $2}'"
|
|
||||||
devcmd := "lshw -C system 2>/dev/null | head -n 1"
|
|
||||||
|
|
||||||
res := [3]bytes.Buffer{}
|
var stderr, out bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
cmd := exec.Command("uname", "-n")
|
||||||
cmds := []string{ipcmd, maccmd, devcmd}
|
cmd.Stdout = &out
|
||||||
for i, c := range cmds {
|
|
||||||
cmd := exec.Command("bash", "-c", c)
|
|
||||||
cmd.Stdout = &res[i]
|
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b := out.String()
|
||||||
|
HardwareInfo.Model = strings.Trim(b, " \n")
|
||||||
}
|
}
|
||||||
// formatting
|
|
||||||
ip := res[0].String()
|
|
||||||
ip = strings.Trim(ip, " \n")
|
|
||||||
|
|
||||||
hash := fnv.New32a()
|
return HardwareInfo.Model, nil
|
||||||
hash.Write(res[1].Bytes())
|
}
|
||||||
|
|
||||||
b := res[2].String()
|
func GetBus() (int, error) {
|
||||||
b = strings.Trim(b, " \n")
|
// preset busses
|
||||||
return nil
|
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