Skip to content

Commit

Permalink
config and stability, scan every 10 seconds forever
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Feb 15, 2018
1 parent 2a068e4 commit 4943198
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cfg/wificfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"host_apd_cfg": {
"ip": "192.168.27.1",
"ssid": "iot-wifi",
"ssid": "iot-wifi-cfg",
"wpa_passphrase":"iotwifipass",
"channel": "6"
},
Expand Down
33 changes: 5 additions & 28 deletions iotwifi/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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...)
Expand All @@ -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()

}
25 changes: 19 additions & 6 deletions iotwifi/iotwifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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...")

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

Expand Down
23 changes: 16 additions & 7 deletions iotwifi/wpacfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type WpaCfg struct {
Log bunyan.Logger
WpaCmd []string
WpaCfg *SetupCfg
}

type WpaNetwork struct {
Expand All @@ -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,
}
}

Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 4943198

Please sign in to comment.