Skip to content

Commit

Permalink
refactor for command struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Feb 12, 2018
1 parent 80e76f3 commit 7ad3938
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
26 changes: 17 additions & 9 deletions iotwifi/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.
Expand All @@ -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
Expand Down
16 changes: 11 additions & 5 deletions iotwifi/iotwifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
}
})

Expand All @@ -83,7 +89,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
cmd.Wait()

// re-check
CheckInterface(cmdRunner)
command.CheckInterface()
return
}

Expand All @@ -102,7 +108,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
cmd.Start()
cmd.Wait()

StartHostapd(cmdRunner)
command.StartHostapd()
}
})

Expand All @@ -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)
Expand Down

0 comments on commit 7ad3938

Please sign in to comment.