-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
52 lines (42 loc) · 1.06 KB
/
event.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
package wpaclient
import (
"fmt"
"strconv"
"strings"
)
// AuthReq represents data received with "CTRL-REQ-" requests
type AuthReq struct {
ID int
Type string
Text string
}
// Event represends events received from wpa_supplicant
type Event struct {
Sev int
Message string
AuthReq *AuthReq
Err error
}
func parseEvent(b []byte) *Event {
if len(b) < 5 {
msg := strings.TrimSuffix(string(b), "\n")
return &Event{Err: fmt.Errorf("message too short: %s", msg)}
}
sb, err := strconv.Atoi(string(b[1]))
if err != nil {
return &Event{Err: fmt.Errorf("parse severity: %w", err)}
}
msg := strings.TrimSuffix(string(b[3:]), "\n")
if strings.HasPrefix(msg, WpaCtrlReq) {
msg = strings.TrimPrefix(msg, WpaCtrlReq)
i := strings.Index(msg, "-")
j := strings.Index(msg, ":")
id, err := strconv.Atoi(msg[i+1 : j])
if err != nil {
return &Event{Err: fmt.Errorf("parse networkID: %w", err)}
}
return &Event{Sev: sb, Message: WpaCtrlReq,
AuthReq: &AuthReq{ID: id, Type: msg[:i], Text: msg[j+1:]}}
}
return &Event{Sev: sb, Message: msg}
}