diff --git a/cfg/wificfg.json b/cfg/wificfg.json index 985869b..984194a 100644 --- a/cfg/wificfg.json +++ b/cfg/wificfg.json @@ -6,7 +6,7 @@ }, "host_apd_cfg": { "ip": "192.168.27.1", - "ssid": "iot-wifi", + "ssid": "iot-wifi-cfg", "wpa_passphrase":"iotwifipass", "channel": "6" }, diff --git a/iotwifi/commands.go b/iotwifi/commands.go index b54185b..460f236 100644 --- a/iotwifi/commands.go +++ b/iotwifi/commands.go @@ -45,6 +45,9 @@ func (c *Command) RemoveApInterface() { // ConfigureApInterface func (c *Command) ConfigureApInterface() { + c.Log.Info("GOt HERE %s", c.SetupCfg.HostApdCfg.Ip) + + cmd := exec.Command("ifconfig", "uap0", c.SetupCfg.HostApdCfg.Ip) cmd.Start() cmd.Wait() @@ -72,11 +75,12 @@ func (c *Command) CheckApInterface() { // StartWpaSupplicant func (c *Command) StartWpaSupplicant() { + args := []string{ "-d", "-Dnl80211", "-iwlan0", - "-c" + c.SetupCfg.WpaSupplicantCfg.CfgFile, + "-c/etc/wpa_supplicant/wpa_supplicant.conf", } cmd := exec.Command("wpa_supplicant", args...) @@ -101,30 +105,3 @@ func (c *Command) StartDnsmasq() { cmd := exec.Command("dnsmasq", args...) go c.Runner.ProcessCmd("dnsmasq", cmd) } - -// StartHostapd -func (c *Command) StartHostapd() { - - c.Runner.Log.Info("Starting hostapd.") - - cmd := exec.Command("hostapd", "-d", "/dev/stdin") - hostapdPipe, _ := cmd.StdinPipe() - c.Runner.ProcessCmd("hostapd", cmd) - - cfg := `interface=uap0 -ssid=` + c.SetupCfg.HostApdCfg.Ssid + ` -hw_mode=g -channel=` + c.SetupCfg.HostApdCfg.Channel + ` -macaddr_acl=0 -auth_algs=1 -ignore_broadcast_ssid=0 -wpa=2 -wpa_passphrase=` + c.SetupCfg.HostApdCfg.WpaPassphrase + ` -wpa_key_mgmt=WPA-PSK -wpa_pairwise=TKIP -rsn_pairwise=CCMP` - - hostapdPipe.Write([]byte(cfg)) - hostapdPipe.Close() - -} diff --git a/iotwifi/iotwifi.go b/iotwifi/iotwifi.go index a43eb1e..a5dc3b8 100644 --- a/iotwifi/iotwifi.go +++ b/iotwifi/iotwifi.go @@ -34,8 +34,8 @@ type CmdMessage struct { Stdin *io.WriteCloser } -func loadCfg() (*SetupCfg, error) { - fileData, err := ioutil.ReadFile("./cfg/wificfg.json") +func loadCfg(cfgLocation string) (*SetupCfg, error) { + fileData, err := ioutil.ReadFile(cfgLocation) if err != nil { panic(err) } @@ -48,7 +48,7 @@ func loadCfg() (*SetupCfg, error) { } // RunWifi starts AP and Station -func RunWifi(log bunyan.Logger, messages chan CmdMessage) { +func RunWifi(log bunyan.Logger, messages chan CmdMessage, cfgLocation string) { log.Info("Loading IoT Wifi...") @@ -59,7 +59,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { Commands: make(map[string]*exec.Cmd, 0), } - setupCfg, err := loadCfg() + setupCfg, err := loadCfg(cfgLocation) if err != nil { log.Error("Could not load config: %s", err.Error()) return @@ -77,14 +77,27 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) { os.Exit(1) }) - wpacfg := NewWpaCfg(log) - wpacfg.StartAP() + wpacfg := NewWpaCfg(log, cfgLocation) + wpacfg.StartAP() + time.Sleep(10 * time.Second) command.StartWpaSupplicant() + + // Scan + time.Sleep(5 * time.Second) + wpacfg.ScanNetworks() + command.StartDnsmasq() + go func() { + for { + wpacfg.ScanNetworks() + time.Sleep(10 * time.Second) + } + }() + // staticFields for logger staticFields := make(map[string]interface{}) diff --git a/iotwifi/wpacfg.go b/iotwifi/wpacfg.go index 0cc58d7..58662cc 100644 --- a/iotwifi/wpacfg.go +++ b/iotwifi/wpacfg.go @@ -15,6 +15,7 @@ import ( type WpaCfg struct { Log bunyan.Logger WpaCmd []string + WpaCfg *SetupCfg } type WpaNetwork struct { @@ -37,10 +38,17 @@ type WpaConnection struct { Message string `json:"message"` } -func NewWpaCfg(log bunyan.Logger) *WpaCfg { +func NewWpaCfg(log bunyan.Logger, cfgLocation string) *WpaCfg { + + setupCfg, err := loadCfg(cfgLocation) + if err != nil { + log.Error("Could not load config: %s", err.Error()) + panic(err) + } return &WpaCfg{ Log: log, + WpaCfg: setupCfg, } } @@ -49,7 +57,8 @@ func (wpa *WpaCfg) StartAP() { wpa.Log.Info("Starting Hostapd.") command := &Command{ - Log: wpa.Log, + Log: wpa.Log, + SetupCfg: wpa.WpaCfg, } command.RemoveApInterface() @@ -71,25 +80,25 @@ func (wpa *WpaCfg) StartAP() { stdOutScanner := bufio.NewScanner(cmdStdoutReader) go func() { for stdOutScanner.Scan() { - wpa.Log.Info("GOT: %s", stdOutScanner.Text()) + wpa.Log.Info("HOSTAPD GOT: %s", stdOutScanner.Text()) messages <- stdOutScanner.Text() } }() cfg := `interface=uap0 -ssid=iotwifi2 +ssid=` + wpa.WpaCfg.HostApdCfg.Ssid + ` hw_mode=g -channel=6 +channel=` + wpa.WpaCfg.HostApdCfg.Channel + ` macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 -wpa_passphrase=iotwifipass +wpa_passphrase=` + wpa.WpaCfg.HostApdCfg.WpaPassphrase + ` wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP` - + wpa.Log.Info("Hostapd CFG: %s", cfg) hostapdPipe.Write([]byte(cfg)) cmd.Start() diff --git a/main.go b/main.go index e4b8fa0..4870afb 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,9 @@ func main() { messages := make(chan iotwifi.CmdMessage, 1) - go iotwifi.RunWifi(blog, messages) + go iotwifi.RunWifi(blog, messages, "./cfg/wificfg.json") - wpacfg := iotwifi.NewWpaCfg(blog) + wpacfg := iotwifi.NewWpaCfg(blog, "./cfg/wificfg.json") apiPayloadReturn := func(w http.ResponseWriter, message string, payload interface{}) { apiReturn := &ApiReturn{