diff --git a/iotwifi/commands.go b/iotwifi/commands.go index b47da83..c44a761 100644 --- a/iotwifi/commands.go +++ b/iotwifi/commands.go @@ -2,16 +2,24 @@ package iotwifi import ( "os/exec" + + "github.com/bhoriuchi/go-bunyan/bunyan" ) +// Command for device network commands +type Command struct { + Log bunyan.Logger + Runner CmdRunner +} + // CheckInterface -func CheckInterface(cmdRunner CmdRunner) { +func (c *Command) CheckInterface() { cmd := exec.Command("ifconfig", "uap0") - go cmdRunner.ProcessCmd("ifconfig_uap0", cmd) + go c.Runner.ProcessCmd("ifconfig_uap0", cmd) } // StartWpaSupplicant -func StartWpaSupplicant(cmdRunner CmdRunner) { +func (c *Command) StartWpaSupplicant() { args := []string{ "-d", "-Dnl80211", @@ -20,11 +28,11 @@ func StartWpaSupplicant(cmdRunner CmdRunner) { } cmd := exec.Command("wpa_supplicant", args...) - go cmdRunner.ProcessCmd("wpa_supplicant", cmd) + go c.Runner.ProcessCmd("wpa_supplicant", cmd) } // StartDnsmasq -func StartDnsmasq(cmdRunner CmdRunner) { +func (c *Command) StartDnsmasq() { // hostapd is enabled, fire up dnsmasq args := []string{ "--no-hosts", // Don't read the hostnames in /etc/hosts. @@ -39,17 +47,17 @@ func StartDnsmasq(cmdRunner CmdRunner) { } cmd := exec.Command("dnsmasq", args...) - go cmdRunner.ProcessCmd("dnsmasq", cmd) + go c.Runner.ProcessCmd("dnsmasq", cmd) } // StartHostapd -func StartHostapd(cmdRunner CmdRunner) { +func (c *Command) StartHostapd() { - cmdRunner.Log.Info("Starting hostapd."); + c.Runner.Log.Info("Starting hostapd."); cmd := exec.Command("hostapd", "-d", "/dev/stdin") hostapdPipe, _ := cmd.StdinPipe() - cmdRunner.ProcessCmd("hostapd", cmd) + c.Runner.ProcessCmd("hostapd", cmd) cfg := `interface=uap0 ssid=iotwifi2 diff --git a/iotwifi/iotwifi.go b/iotwifi/iotwifi.go index c19df89..7236941 100644 --- a/iotwifi/iotwifi.go +++ b/iotwifi/iotwifi.go @@ -41,6 +41,12 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { Handlers: make(map[string]func(cmsg CmdMessage), 0), Commands: make(map[string]*exec.Cmd, 0), } + + command := &Command{ + Log: log, + Runner: cmdRunner, + } + cmdRunner.HandleFunc("kill", func(cmsg CmdMessage) { log.Error("GOT KILL") @@ -64,7 +70,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { if strings.Contains(cmsg.Message, "uap0: AP-ENABLED") { log.Info("Hostapd enabeled."); - StartDnsmasq(cmdRunner) + command.StartDnsmasq() } }) @@ -83,7 +89,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { cmd.Wait() // re-check - CheckInterface(cmdRunner) + command.CheckInterface() return } @@ -102,7 +108,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { cmd.Start() cmd.Wait() - StartHostapd(cmdRunner) + command.StartHostapd() } }) @@ -111,10 +117,10 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { cmd.Start() cmd.Wait() - StartWpaSupplicant(cmdRunner) + command.StartWpaSupplicant() // Chain of events starts here: - CheckInterface(cmdRunner) + command.CheckInterface() //cmd := exec.Command("ping","-c","10","8.8.8.8") //go cmdRunner.ProcessCmd("ping", cmd)