This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneardconfs.go
166 lines (131 loc) · 3.42 KB
/
neardconfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"flag"
"time"
"strings"
"ostro/neard"
"ostro/confs"
)
var (
wifiPath = "/local/device/wifi"
)
type adapterEntry struct {
path string
enabled bool
active bool
}
var (
adapters = map[string]*adapterEntry{}
)
func main() {
flag.Parse()
neard.Initialize()
if server := neard.NewServer([]string{"text", "wifi"}); server != nil {
timer := time.NewTimer(1 * time.Hour)
timer.Stop()
for {
select {
case ev := <- server.Evchan:
eventHandler(server, ev, timer)
case <- timer.C:
if name, ad := getAdapter(); ad != nil && !ad.active {
server.Activate(name)
}
}
}
}
}
func eventHandler(server *neard.Server, ev neard.Message, timer *time.Timer) {
switch ev.Id {
case neard.Adapter:
adev := ev.AdapterEvent()
confs.Debugf("adapter event: %v", adev)
updateAdapter(&adev)
if _, ad := getAdapter(); ad != nil {
if !ad.active {
timer.Reset(2 * time.Second)
}
}
case neard.State:
state := ev.String()
confs.Debugf("state changed to '%s'", state)
case neard.Error:
err := ev.Error()
confs.Errorf("%v", err)
case neard.WlanHandover:
handleWlanHandoverRequest(ev.Credentials())
case neard.ConfigData:
handleConfigData(ev.String())
case neard.StartApplication:
default:
confs.Debugf("received unsupported event %d", ev.Id)
}
}
func updateAdapter(ev *neard.AdapterEvent) {
if ev.Event == "destroyed" {
delete(adapters, ev.Name)
} else {
if ad := adapters[ev.Name]; ad == nil {
ad = &adapterEntry{path : ev.Path, enabled: ev.Enabled, active: ev.Active}
adapters[ev.Name] = ad
} else {
ad.enabled = ev.Enabled
ad.active = ev.Active
}
}
}
func getAdapter() (string, *adapterEntry) {
for name, entry := range adapters {
return name, entry
}
return "", nil
}
func handleWlanHandoverRequest(cred neard.Credentials) {
var frag *confs.ConfFragment
var err error
jsn := fmt.Sprintf("{\"Enable\":true,\"Name\":\"%s\",\"IPv4\":\"DHCP\",\"Mode\":\"EndPoint\",",cred.SSID)
jsn += fmt.Sprintf("\"Security\":{\"Mode\":\"%s\",\"PreSharedKey\":\"%s\"}}", cred.AuthType, cred.Key)
if frag, err = confs.NewConfFragment(wifiPath, neard.NeardOriginated, []byte(jsn)); err != nil {
confs.Errorf("Failed to make ConfFragment (path:'%s', data:'%s'): %v", wifiPath, jsn, err)
return
}
if ferr := frag.WriteDropZone(); ferr != nil {
confs.Errorf("Failed to write ConfFragement to DropZone: %v", ferr)
return
}
confs.Infof("%s config data written to %s", frag.Type(), frag.Path())
}
func handleConfigData(data string) {
var (
frag *confs.ConfFragment
path, conf string
err error
ok bool
)
if path, conf, ok = parseConfigData(data); !ok {
confs.Errorf("Failed to parse configuration data: '%s'", data)
return
}
if frag, err = confs.NewConfFragment(path, neard.NeardOriginated, []byte(conf)); err != nil {
confs.Errorf("Failed to make ConfFragment (path:'%s', data:'%s'): %v", path, conf, err)
return
}
if ferr := frag.WriteDropZone(); ferr != nil {
confs.Errorf("Failed to write ConfFragement to DropZone: %v", ferr)
return
}
confs.Infof("%s config data written to %s", frag.Type(), frag.Path())
}
func parseConfigData(data string) (string, string, bool) {
ws := " \t\n\r"
arr := strings.Split(data, "=")
if len(arr) > 1 {
path := strings.Trim(arr[0], ws)
conf := strings.Trim(strings.Join(arr[1:], "="), ws)
if len(path) > 0 && len(conf) > 0 {
return path, conf, true
}
}
return "", "", false
}