Skip to content

Commit

Permalink
housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Feb 12, 2018
1 parent 7ad3938 commit 4359802
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
30 changes: 29 additions & 1 deletion iotwifi/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,36 @@ type Command struct {
Runner CmdRunner
}

// RemoveApInterface
func (c *Command) RemoveApInterface() {
cmd := exec.Command("iw","dev","uap0","del")
cmd.Start()
cmd.Wait()
}

// ConfigureApInterface
func (c *Command) ConfigureApInterface() {
cmd := exec.Command("ifconfig","uap0","192.168.27.1")
cmd.Start()
cmd.Wait()
}

// UpApInterface
func (c *Command) UpApInterface() {
cmd := exec.Command("ifconfig","uap0","up");
cmd.Start()
cmd.Wait()
}

// AddInterface
func (c *Command) AddApInterface() {
cmd := exec.Command("iw", "phy", "phy0", "interface", "add", "uap0", "type", "__ap");
cmd.Start()
cmd.Wait()
}

// CheckInterface
func (c *Command) CheckInterface() {
func (c *Command) CheckApInterface() {
cmd := exec.Command("ifconfig", "uap0")
go c.Runner.ProcessCmd("ifconfig_uap0", cmd)
}
Expand Down
47 changes: 21 additions & 26 deletions iotwifi/iotwifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,23 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
Runner: cmdRunner,
}



// listen to kill messages
cmdRunner.HandleFunc("kill", func(cmsg CmdMessage) {
log.Error("GOT KILL")
//os.Exit(1)
os.Exit(1)
})

// listen to wpa_supplicant messages
cmdRunner.HandleFunc("wpa_supplicant", func(cmsg CmdMessage) {
if strings.Contains(cmsg.Message, "P2P: Update channel list") {
//ConnectWifi(cmdRunner)
// @TODO scan networks
}
})

// listen to hostapd and start dnsmasq
cmdRunner.HandleFunc("hostapd", func(cmsg CmdMessage) {

if strings.Contains(cmsg.Message, "uap0: AP-DISABLED") {
log.Error("CANNOT START AP")
cmsg.Cmd.Process.Kill()
Expand All @@ -77,19 +80,17 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
// check for the uap0 interface
//
cmdRunner.HandleFunc("ifconfig_uap0", func(cmsg CmdMessage) {
var cmd *exec.Cmd

if strings.Contains(cmsg.Message, "Device not found") {
// no uap so lets create it
log.Info("uap0 not found... starting one up.")
cmsg.Cmd.Wait()

cmd = exec.Command("iw", "phy", "phy0", "interface", "add", "uap0", "type", "__ap");
cmd.Start()
cmd.Wait()

// add interface
command.AddApInterface()

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

Expand All @@ -99,36 +100,30 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
cmsg.Cmd.Wait()

// up uap0
cmd = exec.Command("ifconfig","uap0","up");
cmd.Start()
cmd.Wait()

command.UpApInterface()

// configure uap0
cmd = exec.Command("ifconfig","uap0","192.168.27.1")
cmd.Start()
cmd.Wait()
command.ConfigureApInterface()

// start hostapd
command.StartHostapd()
}
})

// remove interface
cmd := exec.Command("iw","dev","uap0","del")
cmd.Start()
cmd.Wait()
// remove AP interface (if there is one) and start fresh
command.RemoveApInterface()

// start wpa_supplicant (wifi client)
command.StartWpaSupplicant()

// Chain of events starts here:
command.CheckInterface()
// start ap interface, chain of events for hostapd and dnsmasq starts here
command.CheckApInterface()

//cmd := exec.Command("ping","-c","10","8.8.8.8")
//go cmdRunner.ProcessCmd("ping", cmd)

// staticFields for logger
staticFields := make(map[string]interface{})

// command output loop
// command output loop (channel messages)
// loop, log and dispatch to handlers
//
for {
out := <-messages // Block until we receive a message on the channel
Expand Down

0 comments on commit 4359802

Please sign in to comment.