-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (208 loc) · 5.45 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
/*
#cgo CFLAGS: -Ilib/SDL2/include -Ilib/lvgl
#cgo LDFLAGS: -L./lib/SDL2/lib/x64 -lSDL2 -L./lib/lvgl -l lvgl
#include "mcujs.h"
*/
import "C"
import (
"fmt"
"io/ioutil"
"log"
"net"
"runtime"
"time"
"github.com/dop251/goja"
"github.com/google/uuid"
)
const (
OPCODE_REPORT_ID = 0x01
OPCODE_CHALLENGE_RESPONSE = 0x02
OPCODE_STATE_CHANGE = 0x03
OPCODE_RPC_CALL = 0x04
OPCODE_RPC_RESPONSE = 0x05
)
type JSCallback func(*goja.Runtime)
var MainLoopChan chan JSCallback = make(chan JSCallback, 1024)
var ticker *time.Ticker = time.NewTicker(100 * time.Millisecond)
var vm *goja.Runtime
var msgBuilder = MsgBuilder{}
var deviceId = uuid.New()
// The steps of attaching the device to the server are:
// 1. The device sends a OPCODE_REPORT_ID packet to the server.
// 2. The server sends a OPCODE_CHALLENGE_RESPONSE packet to the device.
// 3. The device sends a OPCODE_CHALLENGE_RESPONSE packet to the server.
// 4. If the server accepts the device, it sends a OPCODE_STATE_CHANGE packet to the device.
// 5. The device sends a OPCODE_STATE_CHANGE packet to the server.
// 6. The device enters the main loop to receive and handle OPCODE_RPC_CALL packets.
func attachDevice() {
// Create a UDP connection to the server.
conn, err := net.Dial("udp", "127.0.0.1:8801")
if err != nil {
log.Fatal("dialing:", err)
}
defer conn.Close()
// Send the message to the server.
reportDeviceId(conn)
// Handle the response.
handleChallengeResponse(conn)
// Handle the state change.
handleStateChange(conn)
// Enter the main loop.
for {
// Handle the RPC call.
payload, err := udpGetPayload(conn)
if err != nil {
log.Fatal("udpGetPayload:", err)
}
opcode := payload[0]
if opcode != OPCODE_RPC_CALL {
log.Fatal("unexpected opcode:", opcode)
}
// Currently not implemented.
log.Println("RPC call:", payload[1:])
// Send the response.
response := make([]byte, 1+32)
response[0] = OPCODE_RPC_RESPONSE
copy(response[1:], payload[1:])
pkt, err := msgBuilder.BuildMsg(response, deviceId)
if err != nil {
log.Fatal("BuildMsg:", err)
}
_, err = conn.Write(pkt)
if err != nil {
log.Fatal("conn.Write:", err)
}
}
}
func udpGetPayload(conn net.Conn) ([]byte, error) {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
return nil, err
}
pkt := buf[:n]
payload, deviceId, err := msgBuilder.ParseMsg(pkt)
if err != nil {
return nil, err
}
log.Println("deviceId:", deviceId)
return payload, nil
}
func reportDeviceId(conn net.Conn) {
// Payload format:
// 1 byte: OPCODE_REPORT_ID
// 32 bytes: Device ID
payload := make([]byte, 1+32)
payload[0] = OPCODE_REPORT_ID
copy(payload[1:], deviceId[:])
pkt, err := msgBuilder.BuildMsg(payload, deviceId)
if err != nil {
log.Fatal("build msg:", err)
}
_, err = conn.Write(pkt)
if err != nil {
log.Fatal("send msg:", err)
}
}
func handleChallengeResponse(conn net.Conn) {
// Payload format:
// 1 byte: OPCODE_CHALLENGE_RESPONSE
// 32 bytes: challenge content
payload, err := udpGetPayload(conn)
if err != nil {
log.Fatal("get payload:", err)
}
opcode := payload[0]
if opcode != OPCODE_CHALLENGE_RESPONSE {
log.Fatal("unexpected opcode:", opcode)
}
challenge := payload[1:]
// The challenge content should starts with "What's your name" and padded with 0.
if string(challenge[:15]) != "What's your name" {
log.Fatal("unexpected challenge content:", challenge)
}
// Send the response to the server.
response := make([]byte, 1+32)
response[0] = OPCODE_CHALLENGE_RESPONSE
copy(response[1:], challenge)
pkt, err := msgBuilder.BuildMsg(response, deviceId)
if err != nil {
log.Fatal("build msg:", err)
}
_, err = conn.Write(pkt)
}
func handleStateChange(conn net.Conn) {
// Payload format:
// 1 byte: OPCODE_STATE_CHANGE
// 1 byte: state
payload, err := udpGetPayload(conn)
if err != nil {
log.Fatal("get payload:", err)
}
opcode := payload[0]
if opcode != OPCODE_STATE_CHANGE {
log.Fatal("unexpected opcode:", opcode)
}
state := payload[1]
if state != 1 {
log.Fatal("unexpected state:", state)
}
// Respond to the server.
response := make([]byte, 1+1)
response[0] = OPCODE_STATE_CHANGE
response[1] = 1
pkt, err := msgBuilder.BuildMsg(response, deviceId)
if err != nil {
log.Fatal("build msg:", err)
}
_, err = conn.Write(pkt)
}
func print(call goja.FunctionCall) goja.Value {
s := make([]interface{}, len(call.Arguments))
for i, v := range call.Arguments {
s[i] = v.String()
}
fmt.Println(s...)
return nil
}
func runFile(path string) {
buf, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(path, err)
}
_, err = vm.RunScript(path, string(buf))
if err != nil {
log.Fatal(path, err)
}
}
func CallInJSLoop(f JSCallback) {
MainLoopChan <- f
}
func main() {
runtime.LockOSThread()
msgBuilder.Init()
go attachDevice()
C.mjsInit()
vm = goja.New()
vm.SetFieldNameMapper(goja.UncapFieldNameMapper())
vm.Set("print", print)
HttpInit()
LvglInit()
runFile("js/underscore.js")
runFile("js/devserver.js")
runFile("js/boot.js")
go PromptLoop()
for {
select {
case f := <-MainLoopChan:
f(vm)
case <-ticker.C:
//fmt.Println("tick")
if C.mjsEventLoop() == 1 {
log.Println("SDL_QUIT received, exiting...")
return
}
}
}
}