forked from akshmakov/bossac-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bossac-server.sh
executable file
·422 lines (320 loc) · 9.43 KB
/
bossac-server.sh
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
#!/bin/bash
#
# Author: Andrey Shmakov
#
# https://github.com/akshmakov/serialport-server
#
# Simple bossac-based atmel programming server using socat
#
#####################################################################
##################### Function Defs Global Vars #####################
#####################################################################
PROGNAME=$(basename $0)
INVOKE_TS=$(date +"%s")
function error_exit
{
#----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
#----------------------------------------------------------------
echo "${PROGNAME}: ${1-\"Unknown Error\"}" 1>&2
exit ${2-1}
}
function usage
{
cat <<EOF
Usage: $PROGNAME [OPTIONS] device
options:
{SERVER}
-p/--port <PORT> : exposed TCP Port
(default = 2000)
-l/--logfile <FNAME> : save output to file
-h/--help : print this usage help
-d/--daemon : daemonize (background)
-v/--verbose : more (debug)
-R/--server-root : Server Root (var files)
(default = /var/opt/bossac-server/)
-b/--bins-dir <DIR> : directory where bins are stored
(default = /var/opt/bossac-server/bins)
{BOSSAC TUNING}
--bossac-device <DEVICE> : alternative way to specify device
--bossac-bin <BOSSACEXE> : path or name of bossac executable
(default = bossac)
--bossac-usb true/false : force usb device
(default = true)
--bossac-info <STRING> : arguments passed to bossac under command INFO
(default = "--info")
--bossac-write <STRING> : arguments passed to bossac under command WRITE
(default = "-i -e -R -w -v")
--bossac-read <STRING> : arguments passed to bossac under command READ
(default = "-i -r")
device: local socket or device intended for programmer
use (e.g. defualt is /dev/ttyUSB0)
The following Environment Variables can be used in lieu of args
SERVER_PORT - -p/--p
SERVER_BINS_DIR- -b/--bins-dir
SERVER_VERBOSE - -v/--verbose
SERVER_LOGFILE - -l/--logfile
SERVER_ROOT - -R/--server-root
SERVER_DAEMON - -d/--daemon
BOSSAC_DEVICE - device\--bossac-device
BOSSAC_BIN - --bossac-bin
BOSSAC_USB - --bossac-usb
BOSSAC_INFO - --bossac-info
BOSSAC_READ - --bossac-read
BOSSAC_WRITE - --bossac-write
---------------------------
SERVER API V1 DOCUMENTATION
---------------------------
(W)RITE <FILEN> - write local file to device
(R)EAD <FILEN> - read device to local file
(L)IST - list avaialble bins
(I)NFO - Connected Device Info
\\not-yet-implemented
\\ WRITES - write stdin stream to device (for piping)
\\ READS - read device to stdout (for piping)
=== EXAMPLES ===
start default server for ACM0:
$PROGNAME /dev/ttyACM0
start server on alt port:
$PROGNAME -p 2002 /dev/ttyACM0
start server with tftp as bin dir:
$PROGNAME --bins-dir /srv/tftp /dev/ttyACM0
environment config
SERVER_PORT=2002 BOSSAC_DEVICE=/dev/ttyACM0 $PROGNAME
EOF
exit 0
}
#####################################################################
##################### Env Configurable Vars #####################
#####################################################################
#Configurable Vars
SERVER_PORT=${SERVER_PORT-2000}
SERVER_DAEMON=${SERVER_DAEMON-}
SERVER_VERBOSE=${SERVER_VERBOSE-}
SERVER_LOGFILE=${SERVER_LOGFILE-}
SERVER_ROOT=${SERVER_ROOT-/var/opt/bossac-server}
## files to write/read
SERVER_BINS_DIR=${SERVER_BINS_DIR-"$SERVER_ROOT/bins"}
## Binary for bossac
BOSSAC_BIN=${BOSSAC_BIN-"bossac"}
## Device file
BOSSAC_DEVICE=${BOSSAC_DEVICE-}
## USB Force
BOSSAC_USB=${BOSSAC_USB-"true"}
## Write command flags
BOSSAC_WRITE=${BOSSAC_WRITE-"-i -d -e -R -w -v "}
## info command
BOSSAC_INFO=${BOSSAC_INFO-"--info"}
## Read command Flags
BOSSAC_READ=${BOSSAC_READ-"-i -d -r "}
#####################################################################
##################### Options Processing #####################
#####################################################################
if [[ $# = 0 && -z $BOSSAC_DEVICE ]]; then
usage
error_exit "Should Not Get Here"
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-p|--port)
SERVER_PORT=$2
shift
shift
;;
-h|--help)
usage
error_exit "Should Not Get Here"
;;
-l|--logfile)
SERVER_LOGFILE=$2
shift
shift
;;
-R|--server-root)
SERVER_ROOT=$2
shift
shift
;;
-d|--daemon)
SERVER_DAEMON=TRUE
shift
;;
-v|--verbose)
SERVER_VERBOSE=TRUE
shift
;;
-b|--bins-dir)
SERVER_BINS_DIR=$2
shift
shift
;;
--bossac-bin)
BOSSAC_BIN=$2
shift
shift
;;
--bossac-device)
BOSSAC_DEVICE=$2
shift
shift
;;
--bossac-usb)
BOSSAC_USB=$2
shift
shift
;;
--bossac-write)
BOSSAC_WRITE=$2
shift
shift
;;
--bossac-info)
BOSSAC_INFO=$2
shift
shift
;;
--bossac-read)
BOSSAC_READ=$2
shift
shift
;;
-*|--*)
# unknown option
error_exit "Unknown Option $1"
;;
*)
break
;;
esac
done
## All options have "-" or "--"
## First string after options is our device
if [[ -n $1 ]]; then
BOSSAC_DEVICE=$1
elif [[ -z $BOSSAC_DEVICE ]]; then
error_exit "You need to specify a device"
fi
### setup verbose mode
# Nifty Trick found on SO
# You can write your own verbose only
# log string by repacing your
# echo "text string here"
# with
# echo "text string here" >&3
# for stdout (verbose)
# and >&4 for stderr (verbose)
###
if [ ! -z $SERVER_VERBOSE ]; then
exec 4>&2 3>&1
else
exec 4>/dev/null 3>/dev/null
fi
##debug msg
cat <<EOF >&3
--server--
External Port : $SERVER_PORT
Host Device : $BOSSAC_DEVICE
Log File : ${SERVER_LOGFILE-NONE}
Server vars : $SERVER_ROOT
Bins Dir : $SERVER_BINS_DIR
Daemon : `if [ -z $SERVER_DAEMON ]; then echo "no"; else echo "yes"; fi`
--bossac--
Device : $BOSSAC_DEVICE
USB : $BOSSAC_USB
bossac-exe : $BOSSAC_BIN
Write Command : $BOSSAC_WRITE
Read Command : $BOSSAC_READ
Info Command : $BOSSAC_INFO
EOF
#####################################################################
##################### bossac wrapper heredoc #####################
#####################################################################
#Derived Var
BOSSAC_EXEC="$BOSSAC_BIN --port=$BOSSAC_DEVICE --usb-port=$BOSSAC_USB"
mkdir -p $SERVER_ROOT
mkdir -p $SERVER_BINS_DIR
## heredoc bossac wrapper
cat <<EOF > $SERVER_ROOT/wrapper
#!/bin/bash
# Temporary Wrapper for bossac-server
function exec_bossac_write {
local FILE=\${1-"latestw.bin"}
$BOSSAC_EXEC $BOSSAC_WRITE $SERVER_BINS_DIR/\$FILE
cp $SERVER_BINS_DIR/\$FILE $SERVER_BINS_DIR/latestw.bin
}
function exec_bossac_info {
$BOSSAC_EXEC $BOSSAC_INFO
}
function exec_bossac_read {
local FILE=\${1-"latestr.bin"}
$BOSSAC_EXEC $BOSSAC_READ $SERVER_BINS_DIR/\$FILE
cp $SERVER_BINS_DIR/\$FILE $SERVER_BINS_DIR/latestr.bin
}
function exec_list_bins {
find $SERVER_BINS_DIR -type f -printf "%f\n"
}
function exec_usage {
cat <<EOG
BOSSAC SERVER - command list
WRITE <FILE> - write file to device
READ <FILE> - read device to file
LIST - list avaialble bins
INFO - Connected Device Info
EOG
}
read CMD ARGS
case \${CMD,,} in
w|write)
exec_bossac_write \$ARGS
PID=$!
;;
l|list)
exec_list_bins
PID=$!
;;
i|info)
exec_bossac_info
PID=$!
;;
r|read)
exec_bossac_read \$ARGS
PID=$!
;;
h|help|*)
exec_usage
PID=$!
;;
esac
exit 0
EOF
chmod 750 $SERVER_ROOT/wrapper
#####################################################################
##################### socat server ################################
#####################################################################
SOCAT_BIN=socat
SOCAT_OPTS="-v"
SOCAT_1_TARGET="tcp4-listen:$SERVER_PORT"
SOCAT_1_OPTS=",reuseaddr,fork,ignoreeof"
SOCAT_2_TARGET="SYSTEM:$SERVER_ROOT/wrapper"
SOCAT_2_OPTS=""
if [ -n $VERBOSE ]; then
SOCAT_OPTS="-d -d -v"
fi
SOCAT_INVOCATION="$SOCAT_BIN $SOCAT_OPTS \
$SOCAT_1_TARGET$SOCAT_1_OPTS \
$SOCAT_2_TARGET$SOCAT_2_OPTS"
echo $SOCAT_INVOCATION >&3
if [ -z $LOGFILE]; then
$SOCAT_INVOCATION
else
$SOCAT_INVOCATION 2>&1 > $LOGFILE
fi
#socat -d -d -v tcp4-listen:2000,reuseaddr,fork,ignoreeof SYSTEM:"$SERVER_ROOT/wrapper"
#####################################################################
##################### cleanup ################################
#####################################################################
rm $SERVER_ROOT//wrapper