-
Notifications
You must be signed in to change notification settings - Fork 22
/
wifiip.go
100 lines (82 loc) · 2.16 KB
/
wifiip.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
package ble
import (
"encoding/json"
"errors"
"fmt"
"github.com/digital-dream-labs/vector-bluetooth/rts"
)
// WifiIPResponse is the unified response for wifi ip messages
type WifiIPResponse struct {
IPv4 string
IPv6 string
}
// Marshal converts a WifiIPResponse message to bytes
func (sr *WifiIPResponse) Marshal() ([]byte, error) {
return json.Marshal(sr)
}
// Unmarshal converts a WifiIPResponse byte slice to a WifiIPResponse
func (sr *WifiIPResponse) Unmarshal(b []byte) error {
return json.Unmarshal(b, sr)
}
// WifiIP sends a WifiIP message to the vector robot
func (v *VectorBLE) WifiIP() (*WifiIPResponse, error) {
if !v.state.getAuth() {
return nil, errors.New(errNotAuthorized)
}
msg, err := rts.BuildWifiIPMessage(v.ble.Version())
if err != nil {
return nil, err
}
if err := v.ble.Send(msg); err != nil {
return nil, err
}
b, err := v.watch()
resp := WifiIPResponse{}
if err := resp.Unmarshal(b); err != nil {
return nil, err
}
return &resp, err
}
func handleRSTWifiIPResponse(v *VectorBLE, msg interface{}) (data []byte, cont bool, err error) {
var sr *rts.RtsWifiIpResponse
switch v.ble.Version() {
case rtsV2:
t, ok := msg.(*rts.RtsConnection_2)
if !ok {
return handlerUnsupportedTypeError()
}
sr = t.GetRtsWifiIpResponse()
case rtsV3:
t, ok := msg.(*rts.RtsConnection_3)
if !ok {
return handlerUnsupportedTypeError()
}
sr = t.GetRtsWifiIpResponse()
case rtsV4:
t, ok := msg.(*rts.RtsConnection_4)
if !ok {
return handlerUnsupportedTypeError()
}
sr = t.GetRtsWifiIpResponse()
case rtsV5:
t, ok := msg.(*rts.RtsConnection_5)
if !ok {
return handlerUnsupportedTypeError()
}
sr = t.GetRtsWifiIpResponse()
default:
return handlerUnsupportedVersionError()
}
resp := WifiIPResponse{
IPv4: fmt.Sprintf("%d.%d.%d.%d", sr.IpV4[0], sr.IpV4[1], sr.IpV4[2], sr.IpV4[3]),
IPv6: fmt.Sprintf(
"%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x",
sr.IpV6[0], sr.IpV6[1], sr.IpV6[2], sr.IpV6[3],
sr.IpV6[4], sr.IpV6[5], sr.IpV6[6], sr.IpV6[7],
sr.IpV6[8], sr.IpV6[9], sr.IpV6[10], sr.IpV6[11],
sr.IpV6[12], sr.IpV6[13], sr.IpV6[4], sr.IpV6[15],
),
}
b, err := resp.Marshal()
return b, false, err
}