-
Notifications
You must be signed in to change notification settings - Fork 3
/
net_cmd.tbs
494 lines (397 loc) · 13.2 KB
/
net_cmd.tbs
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
'***********************************************************************************************************
' NETWORK-SIDE COMMANDS/REPLIES
' depending libraries:
' settings
'***********************************************************************************************************
include "global.tbh"
const UDP_SOCK_GET_SIGNATURE="UDP"
dim net_cmd_bcast_sel as no_yes
dim fware_block_ctr as word
public dim udp_sock as byte
public dim num_sdf as byte
public dim sdf_addr(MAX_NUM_SDF_LINES) as word
'-------------------------------------------------------------
public sub net_cmd_init()
'Must be called prior to using network commands!
dim str_start,str_end as word
udp_sock=sock_get(UDP_SOCK_GET_SIGNATURE)
'UDP65535
sock.num=udp_sock
sock.txbuffrq(NCMD_TX_BUF_SIZE)
sock.rxbuffrq(NCMD_RX_BUF_SIZE)
#if NET_AVAILABLE
sock.allowedinterfaces="NET"
#else
#if WLN_AVAILABLE
sock.allowedinterfaces="WLN"
#endif
#endif
sys.buffalloc
'UDP65535
sock.num=udp_sock
sock.protocol=PL_SOCK_PROTOCOL_UDP
sock.localportlist="65535"
sock.inconmode=PL_SOCK_INCONMODE_ANY_IP_ANY_PORT
sock.reconmode=PL_SOCK_RECONMODE_3
sock.acceptbcast=YES
'Init sdf_addr array with offsets for lines from SDF_FILE
num_sdf=0
romfile.open(SDF_FILE) 'setting definition file
str_start=1
str_end=romfile.find(str_start,chr(13),1)
while str_end<>0
if num_sdf>=MAX_NUM_SDF_LINES then
sys.halt
num_sdf=0 'you need to increase MAX_NUM_SDF_LINES to >= actual num of lines in SDF_FILE
end if
sdf_addr(num_sdf)=str_start
num_sdf=num_sdf+1
str_start=str_end+2 'because need to skip over LF
str_end=romfile.find(str_start,chr(13),1)
wend
end sub
'-------------------------------------------------------------
public sub net_cmd_proc(cmd_mode as cmd_modes, error_mode as no_yes)
dim ss as string (1)
dim stg_data as string(32)
dim x,pos,pos2,f as byte
dim cmd_src_valid as no_yes
dim index as byte
dim s2 as string(64)
dim reply_str as string(MAX_NET_RPL_LEN)
dim ip_change_needed as no_yes
dim curr_block_num,num_sectors as word
dim setting_name as string(STG_MAX_SETTING_NAME_LEN)
dim explicit_index as no_yes
dim sock_interface as pl_sock_interfaces
dim i as word
dim ser_num as string(8)
dim s as string(MAX_NET_CMD_LEN)
dim cmd_data as string(32)
dim cmd_bcast as no_yes
if sock.num<>udp_sock then exit sub
sock_interface=sock.currentinterface
s=sock.getdata(255)
cmd_data="U"+sock.remoteip
cmd_bcast=sock.bcast
ip_change_needed=NO
reply_str="" 'will prepare our reply here
'is command coming from the same source that has performed the most recent login?
if login_data=cmd_data then
cmd_src_valid=YES
else
cmd_src_valid=NO
end if
'is this a broadcast without pre-selection?
if cmd_bcast = YES and net_cmd_bcast_sel = NO then
'YES, so see if this is allowed for the current command
ss=left(s,1)
if ss<>"X" and ss<>"U" and ss<>"A" and ss<>"W" and ss<>"V" and ss<>"T" then
goto reply_null
end if
end if
'see if command got serial number, separate it if so (special processing for "D")
ser_num=""
if left(s,1)="D" then
x=instr(132,s,"|",1)
else
x=instr(1,s,"|",1)
end if
if x>0 then
ser_num=right(s,len(s)-x+1)
s=left(s,x-1)
end if
'see of command has escape sequence included (except for "D" command)
explicit_index=NO
if left(s,1)<>"D" then
x=instr(1,s,CMD_ESC_CHAR,1)
if x>0 then
'yes, the sequence is included -- extract it
setting_name=mid(s,2,x-2)
stg_data=mid(s,x+2,len(s)-x)
index=val(mid(s,x+1,1))
s2=right(s,len(s)-x-1)
s=left(s,x-1)+s2
if index>0 then index=index-1
explicit_index=YES
end if
end if
if cmd_src_valid=YES then login_timeout_reset() 'resets timeouts
select case left(s,1)
case "X": 'echo command (device info) -----------------------
#if NET_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_NET then
reply_str=net.mac+"/00000"+str(sock.localport)+get_x_flags_net(sock.currentinterface)+stg_get("ON",0)+"/"+stg_get("DN",0)+"/"+str(NUM_VSPD_SER_PORTS)
end if
#endif
#if WLN_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_WLN then
reply_str=wln.mac+"/00000"+str(sock.localport)+get_x_flags_net(sock.currentinterface)+stg_get("ON",0)+"/"+stg_get("DN",0)+"/"+str(NUM_VSPD_SER_PORTS)
end if
#endif
goto reply_ok_have_data
case "G": 'get setting --------------------------------------
if cmd_src_valid=NO then goto reply_denied
setting_name=mid(s,2,STG_MAX_SETTING_NAME_LEN)
if stg_get_num_members(setting_name,x)<>EN_STG_STATUS_OK then goto reply_err
if index>=x then
'index is out of range
if explicit_index=YES then
goto reply_err 'index was specified, so this is an error
else
'index was "figured out automatically", but this setting has a single value
'for the entire device. We simply reset the index
index=0
end if
end if
select case stg_sg(setting_name,index,reply_str,EN_STG_GET)
case EN_STG_STATUS_OK:
goto reply_ok_have_data
case EN_STG_STATUS_UNKNOWN:
goto reply_err
case EN_STG_STATUS_INVALID_INDEX:
goto reply_err
case else:
goto reply_failed
end select
case "S": 'set setting --------------------------------------
if cmd_src_valid=NO then goto reply_denied
if explicit_index=NO then
setting_name=mid(s,2,2)
stg_data=right(s,len(s)-3)
end if
if stg_get_num_members(setting_name,x)<>EN_STG_STATUS_OK then goto reply_err
if index>=x then
'index is out of range
if explicit_index=YES then
goto reply_err 'index was specified, so this is an error
else
'index was "figured out automatically", but this setting has a single value
'for the entire device. We simply reset the index
index=0
end if
end if
select case stg_sg(setting_name,index,stg_data,EN_STG_SET)
case EN_STG_STATUS_OK:
goto reply_ok_have_data
case EN_STG_STATUS_INVALID:
goto reply_err
case EN_STG_STATUS_INVALID_INDEX:
goto reply_err
case EN_STG_STATUS_UNKNOWN:
goto reply_err
case else:
goto reply_failed
end select
case "T": 'return own IP ------------------------------------
reply_str=sock.remoteip
goto reply_ok_have_data
case "B": 'buzz the device ---------------------------------- OK
pat.channel=0
pat.play(PAT_BLINK_BOTH_IN_TURNS_FAST,YES)
goto reply_ok
case "A": 'assign new IP-address ----------------------------
'extract MAC field and compare it to ours
pos=instr(1,s,"/",1)
if pos=0 then goto reply_null
s2=mid(s,2,pos-2)
#if NET_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_NET then
if ddval(s2)<>ddval(net.mac) then goto reply_null
end if
#endif
#if WLN_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_WLN then
if ddval(s2)<>ddval(wln.mac) then goto reply_null
end if
#endif
if login(cmd_mode,cmd_data,sock.num)=NG then goto reply_denied
logout()
'extract password field and compare to ours (unless in error_mode)
pos2=instr(1,s,"/",2)
if pos2=0 then goto reply_err
s2=mid(s,pos+1,pos2-pos-1)
if error_mode=NO then
if stg_sg("PW",0,reply_str,EN_STG_GET)=EN_STG_STATUS_OK then
if s2<>reply_str then goto reply_denied
end if
end if
'set new setting value
s2=right(s,len(s)-pos2)
#if NET_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_NET then
if stg_sg("IP",0,s2,EN_STG_SET)<>EN_STG_STATUS_OK then goto reply_failed
end if
#endif
#if WLN_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_WLN then
if stg_sg("WI",0,s2,EN_STG_SET)<>EN_STG_STATUS_OK then goto reply_failed
end if
#endif
'set the flag to remember that we will need to change IP!
ip_change_needed=YES
goto reply_ok
case "L": 'login --------------------------------------------
'verify password
stg_data=stg_get("PW",0)
'we verify password only when NOT in the error mode
if error_mode=NO then
if right(s,len(s)-1)<> stg_data then goto reply_denied
end if
'can we accept the login in principle (means, no higher-priority session in progress)?
if login(cmd_mode,cmd_data,sock.num)=NG then goto reply_rejected
goto reply_ok
case "O": 'logout -------------------------------------------
if cmd_src_valid=NO then goto reply_denied
logout()
goto reply_ok
case "I": 'initialize settings --------------------------
if cmd_src_valid=NO then goto reply_denied
if stg_restore_multiple(EN_STG_INIT_MODE_NORMAL)<>EN_STG_STATUS_OK then goto reply_failed
goto reply_ok
case "E": 'reboot -------------------------------------------
if cmd_src_valid=NO then goto reply_denied
sys.reboot
case "V": 'version string -----------------------------------
reply_str=FIRMWARE_VERSION
goto reply_ok_have_data
case "W": 'select in broadcast mode -------------------------
net_cmd_bcast_sel=NO
reply_str=right(s,len(s)-1)
#if NET_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_NET then
if ddval(reply_str)<>ddval(net.mac) then goto reply_null
end if
#endif
#if WLN_AVAILABLE
if sock.currentinterface=PL_SOCK_INTERFACE_WLN then
if ddval(reply_str)<>ddval(wln.mac) then goto reply_null
end if
#endif
net_cmd_bcast_sel=YES
goto reply_ok
case "H": 'hardware version (legacy command) ----------------
reply_str="1A"
goto reply_ok_have_data
case "C": 'number of SDF lines to load (new command) --------
select case mid(s,2,1)
case "S","":
reply_str=str(num_sdf)
case "U":
reply_str=str(8)
case else:
goto reply_err
end select
goto reply_ok_have_data
case "F": 'get specified SDF line ---------------------------
f=asc(mid(s,2,1))
if f>=&h30 and f<=&h39 then
x=val(right(s,len(s)-1)) 'one char in command
goto d1
else
x=val(right(s,len(s)-2)) 'two chars in command
end if
select case mid(s,2,1)
case "S":
d1: if x>=num_sdf then goto reply_err
romfile.open(SDF_FILE)
romfile.pointer=sdf_addr(x)
if x=num_sdf-1 then
reply_str=romfile.getdata(romfile.size-sdf_addr(x))
else
reply_str=romfile.getdata(sdf_addr(x+1)-sdf_addr(x))
end if
case "U":
if x = 0 then reply_str = "unavailable"
case else:
goto reply_err
end select
sock.num=udp_sock
goto reply_ok_have_data
case "Q" 'reset firmware upload process ---------------------
fware_block_ctr=0
goto reply_ok
case "D" 'upload one data block of firmware -----------------
if len(s)<>131 then goto reply_err
curr_block_num=256*asc(mid(s,2,1))+asc(mid(s,3,1))
if curr_block_num>fware_block_ctr then goto reply_out_of_seq 'out of sequence
if curr_block_num<fware_block_ctr then goto reply_ok 'repeated block
if curr_block_num>=fd.availableflashspace*2 then goto reply_out_of_mem 'no space to store new file
'we fit two 128-byte packets into each sector
fd.buffernum=0
if (curr_block_num and 1)=0 then
'fill the first half of the buffer
fd.setbuffer(mid(s,4,128),0)
else
'fill the second half of the buffer and program the sector
fd.setbuffer(mid(s,4,128),128)
fd.setsector(curr_block_num/2)
end if
'next block number
fware_block_ctr=fware_block_ctr+1
x=fware_block_ctr/256
reply_str=chr(x)+chr(fware_block_ctr-x*256)
goto reply_ok_have_data
case "N" 'firmware programming request ----------------------
if cmd_src_valid=NO then goto reply_denied
pat.channel=0
pat.play(PAT_GREEN_LOOP,PL_PAT_CANINT)
'the following delay will make sure that the pattern will be displayed
'we do this because fd.copyfirmware will disable interrupts
i=sys.timercount
while i=sys.timercount
wend
'we might have one unsaved sector!
if (curr_block_num and 1)=0 then
fd.setsector(curr_block_num/2)
end if
num_sectors=fware_block_ctr/2
if (fware_block_ctr and 1)<>0 then
num_sectors=num_sectors+1
end if
'copy between areas of flash
fd.copyfirmware(num_sectors)
case else: 'invalid command ---------------------------------
if cmd_bcast=YES and net_cmd_bcast_sel=NO then goto reply_null 'important!
goto reply_err
end select
'---------- after processing command we are landing here to generate different reply codes
reply_out_of_mem:
reply_str="O"
goto add_seq_num
reply_out_of_seq:
reply_str="S"
goto add_seq_num
reply_rejected:
reply_str="R"
goto add_seq_num
reply_err:
reply_str="C"
goto add_seq_num
reply_denied:
reply_str="D"
goto add_seq_num
reply_failed:
reply_str="F"
goto add_seq_num
reply_ok:
reply_str="A"
goto add_seq_num
reply_ok_have_data:
reply_str="A"+reply_str
goto add_seq_num
add_seq_num:
reply_str=reply_str+ser_num
sock.num=udp_sock 'restore socket (we may have changed it)
sock.targetinterface=sock_interface
sock.setdata(reply_str)
sock.send
'do we need to change the IP?
if ip_change_needed=YES then set_net_ip(sock.currentinterface)
reply_null:
end sub
'-------------------------------------------------------------
sub sock_closed_check_net_login(sock_num as byte)
'Socket connection has been closed. Was somebody logged in on this socket?
if login_sock=sock_num then logout()
end sub