Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
beres committed Mar 15, 2019
1 parent 119d56d commit b68393b
Show file tree
Hide file tree
Showing 13 changed files with 847 additions and 459 deletions.
24 changes: 0 additions & 24 deletions Dockerfile

This file was deleted.

30 changes: 8 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
IMAGE ?= cjimti/iotwifi
NAME ?= txwifi
VERSION ?= 1.0.4

all: build push

dev: dev_build dev_run

build:
docker build -t $(IMAGE):latest .
docker build -t $(IMAGE):arm32v6-$(VERSION) .

push:
docker build -t $(IMAGE):latest .
docker build -t $(IMAGE):arm32v6-$(VERSION) .

dev_build:
docker build -t $(IMAGE) ./dev/

dev_run:
sudo docker run --rm -it --privileged --network=host \
-v $(CURDIR):/go/src/github.com/txn2/txwifi \
-w /go/src/github.com/txn2/txwifi \
--name=$(NAME) $(IMAGE):latest
build_arm:
GOARCH=arm GOARM=5 GOOS=linux go build

build_x86: clean
go build -o server_gorilla examples/server_gorilla.go
go build -o server_gin examples/server_gin.go

clean:
@rm -f server_gorilla
@rm -f server_gin
4 changes: 3 additions & 1 deletion cfg/wificfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
},
"wpa_supplicant_cfg": {
"cfg_file": "/etc/wpa_supplicant/wpa_supplicant.conf"
}
},
"dont_fallback_to_ap_mode": true,
"allow_start_stop_mode": true
}
72 changes: 72 additions & 0 deletions examples/server_gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// IoT Wifi Management

// todo: update documentation!!!!
// todo: update Dockerfile
// todo: listen for shutdown signal, remove uap0, kill wpa,apd,dnsmasq

package main

import (
"os"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/txn2/txwifi/iotwifi"
)

func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}

func NewSetupCfg() *iotwifi.SetupCfg {
cfg := iotwifi.SetupCfg{
iotwifi.DnsmasqCfg{
"/#/10.10.10.1", "10.10.10.10,10.10.10.20,1h", "set:device,IoT",
},
iotwifi.HostApdCfg{
"Test AP", "", "6", "10.10.10.1",
},
iotwifi.WpaSupplicantCfg{
"/etc/wpa_supplicant/wpa_supplicant.conf",
},
true,
true,
}
return &cfg
}

func main() {
log.Info("Starting IoT Wifi...")
port := "8080"

setupCfg := NewSetupCfg()
h := iotwifi.NewHttpHandler(setupCfg, true)

r := gin.Default()

// set app routes
r.GET("/status", gin.WrapF(h.StatusHandler))
r.POST("/connect", gin.WrapF(h.ConnectHandler))
r.GET("/scan", gin.WrapF(h.ScanHandler))

// ---
if setupCfg.DontFallBackToApMode {
r.GET("/reset", gin.WrapF(h.ResetHandler))
}

if setupCfg.AllowStartStop {
r.GET("/start", gin.WrapF(h.StartHandler))
r.GET("/stop", gin.WrapF(h.StopHandler))
}

// CORS
// headersOk := handlers.AllowedHeaders([]string{"Content-Type", "Authorization", "Content-Length", "X-Requested-With", "Accept", "Origin"})
// originsOk := handlers.AllowedOrigins([]string{"*"})
// methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE"})

// serve http
log.Info("HTTP Listening on " + port)
// http.ListenAndServe(":"+port, handlers.CORS(originsOk, headersOk, methodsOk)(r))
r.Run(":" + port)
}
90 changes: 90 additions & 0 deletions examples/server_gorilla.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// IoT Wifi Management

// todo: update documentation!!!!
// todo: update Dockerfile
// todo: listen for shutdown signal, remove uap0, kill wpa,apd,dnsmasq

package main

import (
"net/http"
"os"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/txn2/txwifi/iotwifi"
)

func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}

func main() {
log.Info("Starting IoT Wifi...")

cfgUrl := setEnvIfEmpty("IOTWIFI_CFG", "cfg/wificfg.json")
port := setEnvIfEmpty("IOTWIFI_PORT", "8080")
static := setEnvIfEmpty("IOTWIFI_STATIC", "/static/")

setupCfg, err := iotwifi.LoadCfg(cfgUrl)
if err != nil {
log.Error("Could not load config: %s", err.Error())
panic(err)
}

h := iotwifi.NewHttpHandler(setupCfg, true)

// setup router and middleware
r := mux.NewRouter()
//r.Use(h.LogHandler)

// set app routes
r.HandleFunc("/status", h.StatusHandler)
r.HandleFunc("/connect", h.ConnectHandler).Methods("POST")
r.HandleFunc("/scan", h.ScanHandler)

// ---
if setupCfg.DontFallBackToApMode {
r.HandleFunc("/reset", h.ResetHandler)
}

if setupCfg.AllowStartStop {
r.HandleFunc("/start", h.StartHandler)
r.HandleFunc("/stop", h.StopHandler)
}

r.PathPrefix("/").Handler(http.FileServer(http.Dir(static)))
http.Handle("/", r)

// CORS
headersOk := handlers.AllowedHeaders([]string{"Content-Type", "Authorization", "Content-Length", "X-Requested-With", "Accept", "Origin"})
originsOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE"})

// serve http
log.Info("HTTP Listening on " + port)
http.ListenAndServe(":"+port, handlers.CORS(originsOk, headersOk, methodsOk)(r))

}

// getEnv gets an environment variable or sets a default if
// one does not exist.
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}

return value
}

// setEnvIfEmp<ty sets an environment variable to itself or
// fallback if empty.
func setEnvIfEmpty(env string, fallback string) (envVal string) {
envVal = getEnv(env, fallback)
os.Setenv(env, envVal)

return envVal
}
25 changes: 21 additions & 4 deletions iotwifi/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package iotwifi

import (
"os/exec"

"github.com/bhoriuchi/go-bunyan/bunyan"
)

// Command for device network commands.
type Command struct {
Log bunyan.Logger
Runner CmdRunner
SetupCfg *SetupCfg
}
Expand Down Expand Up @@ -51,7 +48,6 @@ func (c *Command) CheckApInterface() {
func (c *Command) StartWpaSupplicant() {

args := []string{
"-d",
"-Dnl80211",
"-iwlan0",
"-c" + c.SetupCfg.WpaSupplicantCfg.CfgFile,
Expand All @@ -74,8 +70,29 @@ func (c *Command) StartDnsmasq() {
"--dhcp-vendorclass=" + c.SetupCfg.DnsmasqCfg.VendorClass,
"--dhcp-authoritative",
"--log-facility=-",
"--interface=uap0",
"--port=0",
}

cmd := exec.Command("dnsmasq", args...)
go c.Runner.ProcessCmd("dnsmasq", cmd)
}

func (c *Command) StartHostAPD() {
args := []string{
"/etc/hostapd/hostapd.conf",
}

cmd := exec.Command("hostapd", args...)
go c.Runner.ProcessCmd("hostapd", cmd)
}

func (c *Command) killIt(it string) {
args := []string{
it,
}

cmd := exec.Command("killall", args...)
cmdId := "killall " + it
c.Runner.ProcessCmd(cmdId, cmd)
}
Loading

0 comments on commit b68393b

Please sign in to comment.