-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplumb.rkt
550 lines (440 loc) · 17 KB
/
plumb.rkt
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
;; The MIT License (MIT)
;;
;; Copyright (c) 2013 Matthew C. Jadud
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
#lang racket
(require racket/cmdline
net/url
net/base64
net/dns
json
racket/gui
mrlib/path-dialog
)
(require "response-handling.rkt"
"path-handling.rkt"
"util.rkt"
"debug.rkt"
"upload.rkt"
"session-management.rkt"
"app-type.rkt"
"seq.rkt"
)
(define VERSION "1.0.0")
(define verbose-mode (make-parameter false))
(define session-id (make-parameter false))
(define timeout (make-parameter 15))
(define first-compilation? (make-parameter true))
(define HOST (make-parameter false))
(define PORT (make-parameter 9000))
(define arduino-ports (make-parameter (list)))
(define (json-encode h)
(let ([os (open-output-string)])
(write-json h os)
(get-output-string os)))
(define (add-file file-path)
(define result (make-parameter (get-response 'OK)))
;; Check the file exists
(try/catch result success-response?
(get-response 'ERROR-NO-FILE)
(unless (file-exists? file-path) (error)))
;; Read the file
(set/catch result success-response?
(get-response 'ERROR-CANNOT-READ)
(file->string file-path))
(set/catch result string?
(get-response 'ERROR)
(make-hash `((filename . ,(extract-filename file-path))
(code . ,(result))
(sessionid . ,(session-id))
(action . "add-file"))))
(debug 'ADD-FILE "~a~n" (result))
;; Encode the jsexpr
(set/catch result hash?
(get-response 'ERROR-JSON-ENCODE)
(jsexpr->string (result)))
;; Base64 encode the JSON string
(set/catch result string?
(get-response 'ERROR-JSON-ENCODE)
(base64-encode (string->bytes/utf-8 (result))))
;; Do an HTTP GET
(set/catch result bytes?
(get-response 'ERROR-HTTP-GET)
(get-pure-port
(make-server-url (HOST) (PORT) "add-file" (result))))
;; Process the result
(set/catch result port?
(get-response 'ERROR-PROCESS-RESPONSE)
(process-response (result)))
(result))
(define (compile-code id board main)
(debug 'COMPILE "Compiling on HOST ~a PORT ~a~n" (HOST) (PORT))
(let* ([url (make-server-url (HOST) (PORT) "compile" id board (extract-filename main))]
[resp-port (get-pure-port url)]
[content (make-parameter (process-response resp-port))])
(close-input-port resp-port)
(debug 'COMPILE "CONTENT RESPONSE~n*****~n~a~n*****~n" (filter-hash (content) 'hex))
(cond
[(or (error-response? (content))
(eof-object? (content)))
(content)]
[else
(hash-ref (content) 'hex)])
))
(define (show-response res)
(printf "[~a] ~a~n"
(hash-ref res 'code)
(hash-ref res 'message)))
(define (build board dir main)
(parameterize ([current-directory dir])
(define p (new process% [context 'BUILD-BOARD]))
(seq p
[(initial? 'ERROR-START-SESSION)
(start-session HOST PORT)]
[(string? 'ERROR-STORE-SESSION-ID)
(session-id (send p get))
NO-CHANGE]
[(pass 'ERROR-LISTING-FILES)
(filter (λ (f)
(member (->sym (file-extension f))
'(occ inc module)))
(filter file-exists? (directory-list)))]
[(list? 'ERROR-ADDING-FILES)
(for ([f (send p get)])
(add-file f))
NO-CHANGE]
[(list? 'ERROR-COMPILING-CODE)
(compile-code (session-id) board main)])
))
(define-syntax-rule (while test body ...)
(let loop ()
(when test
body ...
(loop))))
(define (retrieve-board-config board)
(define p (new process% [context 'RETRIEVE-BOARD-CONFIG]))
(seq p
[(initial? 'ERROR-GENERATING-URL)
(make-server-url (HOST) (PORT) "board" board)]
[(url? 'ERROR-CREATING-PORT)
(get-pure-port (send p get))]
[(port? 'ERROR-PARSING-RESPONSE)
(process-response (send p get))]
[(hash? 'ERROR-STORING-BOARD-CONFIG)
(debug 'BOARD-CONFIG "~a" (filter-hash (send p get) 'hex))
(add-config (config) 'BOARD (send p get))
NO-CHANGE])
(send p get)
)
;tvm-avr-atmega328p-16000000-arduino.hex
(define (retrieve-board-firmware board)
;; First, get the board config
(retrieve-board-config board)
;; Now, fetch the firmware
(let* ([url (make-server-url (HOST) (PORT) "firmware" (hash-ref (get-config 'BOARD) 'firmware))]
[resp-port (get-pure-port url)]
[content (make-parameter (process-response resp-port))])
(try/catch content hash?
(get-response 'ERROR)
(debug 'FIRMWARE "~a" (string-length (hash-ref (content) 'hex)))
)
(content)))
(define plumb
(command-line
#:program "plumb"
#:multi
[("-d" "--debug") flag
"Enable debug flag."
(enable-debug! (->sym flag))]
#:once-each
[("-v" "--version") "Display current plumb version and exit."
(printf "plumb version ~a~n" VERSION)
(exit)]
[("--verbose") "Set maximum verbosity."
(verbose-mode true)]
[("--server") host
"Set the server address."
(HOST host)
]
[("--start-session") "Start a session."
(session-id (start-session HOST PORT))
(printf "~a~n" (session-id))
(exit)]
[("-s" "--session-id") id
"The session ID to operate under."
(session-id id)]
[("-a" "--add-file") filename
"Add a single file."
(show-response (add-file filename))]
[("-c" "--compile") main
"Compile <main-file>."
(compile-code (session-id) main)]
[("-t" "--timeout") sec
"Set the timeout in seconds."
(timeout (string->number sec))]
[("--build") dir board main
"Compile project <dir>, for <board>, using <main> as the start."
(build dir board main)]
[("--board-config") board
"Fetch configuration data for a given board."
(retrieve-board-config board)]
[("--get-firmware") board
"Retrieve firmware for board."
(retrieve-board-firmware board)]
#:args filenames
(for-each (λ (f)
(show-response (add-file f)))
filenames)
;;(plumb-repl)
))
(define (options serial.port)
(printf "Options ~a~n"
(list-intersperse (append
(if (serial.port)
'(f b r)
'())
'(h a d p)) ",")))
;; I need a non-stateless thing.
(define (plumb-repl)
(define board.config (make-parameter false))
(define serial.port (make-parameter false))
(define code.hex (make-parameter false))
(define firmware.hex (make-parameter false))
(load-config (system-type))
;; If no host is specified, use localhost
;; Otherwise, pull from the command line
(if (equal? (HOST) false)
(HOST "127.0.0.1")
(add-config (config) 'SERVER-HOST (HOST)))
(HOST (dns-get-address
(dns-find-nameserver)
(get-config 'SERVER-HOST)))
(PORT (get-config 'SERVER-PORT))
;; Needed for firmware
(unless (directory-exists? (get-config 'TEMPDIR))
(make-directory (get-config 'TEMPDIR)))
(define session-id (make-parameter (start-session HOST PORT)))
;; Check the file exists
(try/catch session-id success-response?
(get-response 'ERROR-NO-CONNECTION)
(begin
;(printf "plumb repl session: ~a~n" (session-id))
(options serial.port)
(printf "> ")
(let main-loop ([cmd (read)])
;; Input handler
(case (->sym cmd)
[(h help)
(printf "HELP~n")]
[(d debug)
(let ([flag (read)])
(printf "Enabling debug flag: ~a~n" flag)
(enable-debug! (->sym flag)))]
[(a add-file)
(printf "session-id: ~a~n" (session-id))
(let ([filename (read)])
(show-response (add-file (->string filename))))]
[(b build)
(let* ([board (read)]
[dir (read)]
[main-file (read)]
;; Need to pass the board type here -- fix the server
[hex (build (->string board) (->string dir) (->string main-file))]
[full-config (retrieve-board-config board)]
)
(board.config full-config)
(debug 'USER-CODE "Board Config: ~a~n" (filter-hash (board.config) 'hex))
(code.hex hex)
(debug 'USER-CODE "LENGTH: ~a" (string-length (code.hex)))
(avrdude-code (serial.port) (code.hex))
)]
[(f firmware)
(let* ([board (read)]
[full-config (retrieve-board-firmware (->string board))])
(board.config full-config)
(debug 'FIRMWARE "Board Config: ~a~n" (filter-hash (board.config) 'hex))
(firmware.hex (hash-ref (board.config) 'hex))
(avrdude-firmware (serial.port)))]
[(p port)
(newline)
(for ([a (list-arduinos)]
[n (length (list-arduinos))])
(printf "[~a] ~a~n" n (build-port a)))
(newline)
(printf "Select serial port~n[port] ")
(let ([port (read)])
(serial.port
(build-port
(list-ref
(list-arduinos)
(string->number (->string port))))))]
[(q quit) (printf "Exiting...~n")
(sleep 1)
(exit)])
(options serial.port)
(printf "> ")
(main-loop (read))
)))
)
(define main-file (make-parameter false))
(define (board-choice->board-type choice)
(case choice
[("Arduino Duemilanove") "arduino"]
[("Arduino Uno") "uno"]
[else "arduino"]))
(define (do-compilation win)
(define board.config (make-parameter false))
(define serial.port (make-parameter
(build-port
(list-ref
(arduino-ports)
(send
(hash-ref (win) 'serial-port)
get-selection)))))
(define code.hex (make-parameter false))
(define firmware.hex (make-parameter false))
(load-config (system-type))
(debug 'COMPILE "Serial Port: ~a" (serial.port))
;; If no host is specified, use localhost
;; Otherwise, pull from the command line
(HOST (send (hash-ref (win) 'server) get-value))
(add-config (config) 'SERVER-HOST (HOST))
(HOST (dns-get-address
(dns-find-nameserver)
(get-config 'SERVER-HOST)))
(PORT (get-config 'SERVER-PORT))
;; If this is the first compilation, upload the firmware
(when (first-compilation?)
(debug 'FIRMWARE "Uploading firmware on first compilation.")
(first-compilation? false)
(let* ([board (board-choice->board-type
(hash-ref (win) 'board))]
[full-config (retrieve-board-firmware (->string board))])
(board.config full-config)
(debug 'FIRMWARE "Board Config: ~a~n" (filter-hash (board.config) 'hex))
(firmware.hex (hash-ref (board.config) 'hex))
(avrdude-firmware (serial.port))
))
;; Needed for firmware
(unless (directory-exists? (get-config 'TEMPDIR))
(make-directory (get-config 'TEMPDIR)))
(session-id (start-session HOST PORT))
(debug 'COMPILE "Session ID: ~a~n" (session-id))
(debug 'COMPILE "Board: ~a~nDir: ~a~nName: ~a~n"
(board-choice->board-type
(hash-ref (win) 'board))
(extract-filedir (main-file))
(extract-filename (main-file)))
(let* ([board (board-choice->board-type
(hash-ref (win) 'board))]
;; Need to pass the board type here -- fix the server
[hex (build board
(extract-filedir (main-file))
(extract-filename (main-file))
)]
[full-config (retrieve-board-config board)]
)
(send (hash-ref ((hash-ref (win) 'compiler-response-window)) 'f) show true)
(board.config full-config)
(debug 'USER-CODE "Board Config: ~a~n" (filter-hash (board.config) 'hex))
(code.hex (hash-ref (board.config) 'hex))
(debug 'USER-CODE "LENGTH: ~a" (string-length (code.hex)))
(avrdude-code (serial.port) (code.hex))
))
(define (compiler-response-window)
(define win (make-parameter (make-hash)))
(define f (new frame%
[label "Messages"]
[width 300]
[height 400]))
(define editor-canvas (new editor-canvas%
(parent f)
(label "Editor Canvas")))
(define text (new text%))
(send text insert "Response from server...")
(send editor-canvas set-editor text)
(let ([w (make-hash `((f . ,f)
(editor-canvas . ,editor-canvas)
(text . ,text)))])
(win w)
win)
)
(define (main-frame)
(define win (make-parameter (make-hash)))
(define f (new frame%
[label "Plumb GUI"]
[width 400]
[height 200]
))
(define server (new text-field%
[parent f]
[label "Server"]
[init-value "ec2-54-226-131-120.compute-1.amazonaws.com"]
[stretchable-width true]
))
(define serial-port (new choice%
[parent f]
[label "Arduino Port"]
[choices
(let ()
(arduino-ports (map ->string (list-arduinos)))
(arduino-ports))]))
(define board (new choice%
[parent f]
[label "Board Type"]
[choices (list "Arduino Duemilanove" "Arduino Uno")]))
(define hortz (new horizontal-panel%
[parent f]))
(define choose-file (new button%
[parent hortz]
[label "Choose Code"]
[stretchable-width true]
[callback (λ (b e)
(let ([d (new path-dialog%
[label "occam code chooser"]
[message "Choose your main .occ file."]
[parent f]
[existing? true]
[filters (list (list "occam files" "*.occ"))]
[dir? false])])
(main-file (send d run))))]
))
(define compile (new button%
[parent hortz]
[label "Compile"]
[stretchable-width true]
[callback (λ (b e)
(do-compilation win))]
))
(win (make-hash `((frame . ,f)
(server . ,server)
(serial-port . ,serial-port)
(board . ,board)
(choose-file . ,choose-file)
(compile . ,compile)
(compiler-response-window . ,(compiler-response-window))
)))
win)
(when (not GUI)
(plumb-repl))
(when GUI
(enable-debug! 'ALL)
(define window (main-frame))
(send (hash-ref (window) 'frame) show true))