-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tbs
716 lines (608 loc) · 16.4 KB
/
main.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
include "global.tbh"
const DNS_SERVER="8.8.4.4"
'------------------------------------------------------------------------------
public dim param_dh as no_yes
public dim param_ar as byte
public dim interface_ready(MAX_NUM_INTERFACES) as no_yes
public dim current_interface as pl_sock_interfaces
public dim ip_configured(MAX_NUM_INTERFACES) as no_yes
public dim wln_password as string(32)
public dim num_wln_failures as byte
public dim gprs_already_started,wln_already_started as no_yes
public dim wln_error,gprs_error as no_yes
public dim sock_http(NUM_HTTP_SOCKETS) as byte
public dim sock_dns as byte
public dim reboot_flag as no_yes
public dim server_ip_obtain as no_yes
public dim agg_port_fallback_count as byte
public dim io_index as byte
public dim enter_menu as no_yes
#if SUPPORTS_GPRS
public dim param_ge as en_param_ge
public dim param_ga as string(16)
public dim param_gn as string(32)
public dim param_gu as string(16)
public dim param_gp as string(16)
#endif
#if WLN_AVAILABLE
public dim param_ws as pl_wln_security_modes
public dim param_we as en_param_we
public dim param_wa as string(32)
public dim param_wd as no_yes
Const PAT_BT_DISABLED="-~"
Const PAT_BT_ENABLED="G-~"
Const PAT_BT_CONNECTED="G~"
dim bt_enabled as no_yes=NO
dim bt_connected as no_yes=NO
#endif
dim divider_by_5 as byte
dim divider_by_2 as no_yes
dim net_linkstate as byte
dim running_light as byte
dim agg_port as word
declare agg_sock as byte
'==============================================================================
sub on_sys_init()
divider_by_5 = 0
boot()
net_linkstate=1
#if SUPPORTS_DISPLAY
io_index=0
enter_menu=NO
while true
doevents
if enter_menu=YES then
exit while
end if
wend
lcd.forecolor=LCD_FORECOLOR
lcd.backcolor=LCD_BACKCOLOR
lcd_clear(0,0,lcd.width,lcd.height)
logout()
login(CMD_MODE_TERMINAL,"",0)
pat.play(PAT_BLINK_BOTH_IN_TURNS,PL_PAT_CANINT)
ui_menu(MENU_X,MENU_Y,MENU_W, MENU_H,"",MENU_SCRIPT_FILE,3,LCD_MENU_TOUT)
sys.reboot
#endif
end sub
'------------------------------------------------------------------------------
sub on_sys_timer()
dim x as byte
dim link_state as en_agg_link_states
dim sock_state as pl_sock_state_simple
dim s1,s2,s3,s4,s5 as string(32)
dim w1 as word
dim f as byte
digital_loop()
bp_loop()
oneWire_Loop()
if analog_period_counter >= analog_period then
analog_loop()
analog_period_counter = 1
else
analog_period_counter = analog_period_counter + 1
end if
if divider_by_5 < 9 then
divider_by_5 = divider_by_5 + 1
exit sub
end if
divider_by_5 = 0
if reboot_timer>0 then
reboot_timer=reboot_timer-1
if reboot_timer=0 then
sys.reboot
end if
end if
dhcp_proc_timer()
agg_proc_timer()
agg_keep_alive()
#if WLN_AVAILABLE
wln_proc_timer()
device_update_rssi()
device_check_for_better_ap
#endif
#if SUPPORTS_GPRS
gprs_proc_timer()
#endif
#if USE_DNS
dns_proc_timer()
#endif
'the following must be called once every second
if divider_by_2=NO then
divider_by_2=YES
exit sub
end if
'we reach this point once/second
divider_by_2=NO
'--- HOOK UP THINGS THAT MUST BE CALLED EVERY SECOND ---
'take care of login timeout
login_timer_proc()
#if SIGNAL_STRENGTH_INDICATION
'wireless signal strength indication
#if SUPPORTS_GPRS
if current_interface=PL_SOCK_INTERFACE_PPP or wln_already_started=NO then
if gprs_already_started=YES then
if gprs_error<>NO then
if running_light=0 then
signal_strength_set(&b10101)
running_light=1
else
signal_strength_set(0)
running_light=0
end if
else
if interface_ready(PL_SOCK_INTERFACE_PPP)=NO then
signal_strength_set(val("&b"+strgen(4-running_light,"1")+"0"+strgen(running_light,"1")))
running_light=running_light+1
if running_light>4 then
running_light=0
end if
else
signal_strength_set("&b10101")
end if
end if
else
signal_strength_set(0)
end if
goto exit_ss_indication
end if
#endif
#if WLN_AVAILABLE
if wln_already_started=YES then
if wln_error<>NO then
if running_light=0 then
signal_strength_set(&b11111)
running_light=1
else
signal_strength_set(0)
running_light=0
end if
else
if interface_ready(PL_SOCK_INTERFACE_WLN)=NO then
signal_strength_set(val("&b1"+strgen(running_light,"0")))
running_light=running_light+1
if running_light>4 then
running_light=0
end if
else
x=(wln.rssi/40)+1
if x>5 then x=5
signal_strength_set("&b"+strgen(x,"1"))
end if
end if
else
signal_strength_set(0)
end if
goto exit_ss_indication
#endif
#endif
exit_ss_indication:
if login_mode=CMD_MODE_NONE and param_ar=1 then
if interface_ready(current_interface)=YES then
if (current_interface=PL_SOCK_INTERFACE_PPP or ip_configured(current_interface)=YES) then
#if USE_DNS
if server_ip_obtain=NO then
dns_connect(current_interface,DNS_SERVER,0)
goto skip_aggregate
end if
#endif
s1=stg_get("CT",0)
w1=val(s1)
w1=w1*120
s1=stg_get("ON",0)
s2=stg_get("DN",0)
s3=stg_get("PW",0)
s4=stg_get("TI",0)
s5=stg_get("TP",0)
if agg_port_fallback_count>0 then
agg_port=val(s5)
end if
agg_start(current_interface,s1,s2,s3,s4,agg_port,w1,YES)
skip_aggregate:
end if
end if
end if
'sntp
if param_te=1 then
rtc.getdata(w1,0,0)
if last_sntp_date<>w1 then
if ip_obtained(PL_SOCK_INTERFACE_NET) then
sntp_start(PL_SOCK_INTERFACE_NET,stg_get("TS",0),0,s_sock)
else if ip_obtained(PL_SOCK_INTERFACE_WLN) then
sntp_start(PL_SOCK_INTERFACE_WLN,stg_get("TS",0),0,s_sock)
end if
sntp_proc_timer()
end if
end if
#if SUPPORTS_DISPLAY
print_time()
print_wifi()
#endif
if net.failure=YES or wln_error=YES or gprs_error=YES then
led_pat_to_play=PAT_TWO_SHORT_RED
goto play_pat
end if
if error_mode=YES then
led_pat_to_play=PAT_BLINK_RED_SLOW
goto play_pat
end if
if current_interface=PL_SOCK_INTERFACE_NET and ip_configured(current_interface)=NO then
led_pat_to_play=PAT_ONE_SHORT_GREEN
goto play_pat
end if
#if WLN_AVAILABLE
if current_interface=PL_SOCK_INTERFACE_WLN and ip_configured(current_interface)=NO then
led_pat_to_play=PAT_ONE_SHORT_GREEN
goto play_pat
end if
#endif
if login_mode=CMD_MODE_HTTP then
led_pat_to_play=PAT_BLINK_BOTH_IN_TURNS
else
'AggreGate comms status
agg_get_connection_state(link_state,sock_state)
select case link_state
case EN_AGG_LINK_STATE_IDLE:
led_pat_to_play=PAT_TWO_SHORT_GREEN
case EN_AGG_LINK_STATE_CONNECTING:
led_pat_to_play=PAT_THREE_SHORT_GREEN
case EN_AGG_LINK_STATE_SYNCRONIZING:
led_pat_to_play=PAT_BLINK_GREEN
case EN_AGG_LINK_STATE_ESTABLISHED:
led_pat_to_play=PAT_GREEN_ON
case else:
led_pat_to_play=PAT_THREE_SHORT_RED
end select
end if
play_pat:
if led_pat_to_play<>led_pat_to_play_prev then
led_pat_to_play_prev=led_pat_to_play
pat.play(PAT_GAP,PL_PAT_CANINT)
end if
end sub
#if WLN_AVAILABLE
'------------------------------------------------------------------------------
sub on_wln_event(wln_event as pl_wln_events)
wln_proc_event(wln_event)
end sub
'------------------------------------------------------------------------------
sub on_wln_task_complete(completed_task as pl_wln_tasks)
wln_proc_task_complete(completed_task)
end sub
#endif
'------------------------------------------------------------------------------
sub on_sock_data_arrival()
dim f as byte
' SNMP
if snmp_set_enable = true then
' #if DEV_DEBUG_PRINT
' dev_debugprint("ON_SOCK_DATA_ARRIVAL SNMP Socket")
' #endif
if sock.num = sock_snmp then
snmp_receive(sock.getdata(255))
end if
end if
' MODBUS
for f = 0 to MODBUS_SOCKS_MAX - 1
if sock.num = modbus_socks(f).socket then
' #if DEV_DEBUG_PRINT
' dev_debugprint("ON_SOCK_DATA_ARRIVAL ModBus Socket")
' #endif
modbus_tcp_receive(sock.getdata(255), sock.num)
end if
next f
#if WLN_AVAILABLE
wln_proc_data()
#endif
#if SUPPORTS_GPRS
gprs_proc_sock_data()
#endif
#if USE_DNS
dns_proc_data()
#endif
#if SUPPORTS_DISPLAY
if sock.num=agg_sock then
agg_cmd_received=YES
end if
#endif
dhcp_proc_data()
agg_proc_data()
net_cmd_proc(CMD_MODE_UDP,error_mode)
if param_te=1 then
sntp_proc_data()
end if
end sub
'------------------------------------------------------------------------------
sub on_sock_data_sent()
end sub
'------------------------------------------------------------------------------
sub on_sock_event(newstate as pl_sock_state, newstatesimple as pl_sock_state_simple)
dim s as string(32)
agg_proc_sock_event(newstate,newstatesimple)
if sock.num=agg_sock then
if sock.statesimple=PL_SSTS_EST then
if agg_port_fallback_count=0 then
stg_set("TP",0,"6480")
else
agg_port_fallback_count=AGG_PORT_FALLBACK_MAX
end if
end if
select case sock.state
case PL_SST_CL_ARESET_RE_PO:
goto a1
case PL_SST_CL_ARESET_RE_AO:
goto a1
case PL_SST_CL_ARESET_RE_EST:
goto a1
case PL_SST_CL_ARESET_RE_PC:
goto a1
case PL_SST_CL_ARESET_RE_AC:
goto a1
case PL_SST_CL_ARESET_TOUT:
goto a1
case PL_SST_CL_ARESET_DERR:
goto a1
a1:
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_NET, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
#if WLN_AVAILABLE
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
wln_rescanning=YES
wln_start_rescan()
#endif
if agg_port_fallback_count>0 then
agg_port_fallback_count=agg_port_fallback_count-1
if agg_port_fallback_count=0 then
agg_port=6480
end if
end if
end select
end if
if sock.num=s_sock and param_te=1 then
sntp_proc_sock_event(newstatesimple)
end if
#if USE_DNS
if sock.num=sock_dns and newstatesimple=PL_SSTS_EST then
s=stg_get("TN",0)
if s<>"" then
dns_query(s)
end if
end if
#endif
end sub
'------------------------------------------------------------------------------
#if SIMULATE_ETHER_OFF_BY_BUTTON=0
sub on_net_link_change()
#else
sub on_button_pressed()
#endif
#if SIMULATE_ETHER_OFF_BY_BUTTON=0
net_linkstate=net.linkstate
#else
if net_linkstate=1 then
net_linkstate=0
else
net_linkstate=1
end if
#endif
ethernet_link_state_change(net_linkstate)
end sub
'------------------------------------------------------------------------------
#if SIMULATE_ETHER_OFF_BY_BUTTON=0
sub on_button_pressed()
dim i,j as word
beep.play("B*",PL_BEEP_CANINT)
'is this a setting init attempt? We need to wait for a while -- the button must be "long-pressed"
i=sys.timercount
while button.pressed=YES
j=sys.timercount
'the code below takes into the account that the sys.timercount can wrap around
if j>=i then
if j-i>=BUTTON_CONST_QUICK_INIT then
goto initialize
end if
else
if (65536-i)+j>=BUTTON_CONST_QUICK_INIT then
initialize: 'OK, initialize now!
device_setting_init(EN_STG_INIT_MODE_OVERRIDE)
sys.reboot()
end if
end if
doevents
wend
enter_menu=YES
end sub
#endif
function agg_set_data(data as string) as boolean
dim timeOut as word
agg_set_data = false
if agg_sock <> 255 then
sock.num = agg_sock
if sock.statesimple = PL_SSTS_EST then
timeOut = sys.timercount
while sock.txfree < len(data) and sock.statesimple = PL_SSTS_EST
if sys.timercount - timeOut > AGG_SEND_WAIT_TIME then exit function
wend
sock.setdata(data)
agg_set_data = true
end if
end if
end function
'------------------------------------------------------------------------------
sub on_ser_data_arrival()
dim param as string
dim i as byte
if bp_ports(ser.num).enable then
bp_data_arrival(ser.num)
exit sub
end if
#if SUPPORTS_GPRS
if ser.num=GPRS_SER_PORT then
gprs_proc_ser_data()
exit sub
end if
#endif
const AGG_COMMAND_START = &h02
const AGG_COMMAND_END = &h0d
const AGG_COMMAND_PARAMETERS_SEPARATOR = &h17
const AGG_ELEMENT_START = &h1c
const AGG_ELEMENT_END = &h1d
const AGG_ELEMENT_NAME_VALUE_SEPARATOR = &h1e
const BUFFER_AGG_SEND_SIZE = 479
const AGG_EVENT_HEADER_SIZE = 22
const STRING_LENGTH_HEX = 127
if agg_sock <> 255 then
sock.num = agg_sock
if sock.statesimple = PL_SSTS_EST then
if ser.rxlen > 0 then
' Send event to AGG
dim event_data as string
dim hex_b as string(2)
dim lenDataForSend as word
dim lenSerData as word
dim lenDataForOperate as word
event_data = chr(AGG_COMMAND_START) + "M" + chr(AGG_COMMAND_PARAMETERS_SEPARATOR) + chr(AGG_COMMAND_PARAMETERS_SEPARATOR)
+ "E" + chr(AGG_COMMAND_PARAMETERS_SEPARATOR) + chr(AGG_COMMAND_PARAMETERS_SEPARATOR) + "ser" + chr(AGG_COMMAND_PARAMETERS_SEPARATOR)
+ "2" + chr(AGG_COMMAND_PARAMETERS_SEPARATOR) + chr(AGG_COMMAND_PARAMETERS_SEPARATOR) + chr(AGG_COMMAND_PARAMETERS_SEPARATOR)
+ chr(AGG_ELEMENT_START) + "R" +chr(AGG_ELEMENT_NAME_VALUE_SEPARATOR)
+ chr(AGG_ELEMENT_START) + str(ser.num) + chr(AGG_ELEMENT_END)
+ chr(AGG_ELEMENT_START)
if agg_set_data(event_data) = true then
lenDataForSend = AGG_EVENT_HEADER_SIZE
lenSerData = ser.rxlen
while lenSerData > 0
if lenSerData < (BUFFER_AGG_SEND_SIZE - lenDataForSend) / 2 then
' Serial Data
lenDataForOperate = lenSerData
else
' AGG Buffer
lenDataForOperate = (BUFFER_AGG_SEND_SIZE - lenDataForSend) / 2
end if
if STRING_LENGTH_HEX < lenDataForOperate then
' String HEX
lenDataForOperate = STRING_LENGTH_HEX
end if
param = ser.getdata(lenDataForOperate)
lenSerData = lenSerData - lenDataForOperate
event_data = ""
for i = 1 to len(param)
hex_b = mid(hex(asc(mid(param, i, 1))), 3, 2)
if len(hex_b) = 1 then
event_data = event_data + "0" + hex_b
else
event_data = event_data + hex_b
end if
next i
if agg_set_data(event_data) = false then
sock.txclear
exit sub
end if
lenDataForSend = lenDataForSend + len(event_data)
if lenDataForSend = BUFFER_AGG_SEND_SIZE or lenDataForSend = BUFFER_AGG_SEND_SIZE - 1 then
' Need send AGG buffer
sock.send()
lenDataForSend = 0
end if
wend
event_data = chr(AGG_ELEMENT_END)
+ chr(AGG_ELEMENT_END)
+ chr(AGG_COMMAND_END)
sock.setdata(event_data)
sock.send()
end if
end if
else
ser.rxclear()
end if
else
ser.rxclear()
end if
end sub
'------------------------------------------------------------------------------
sub on_pat()
if pat.channel=0 then
pat.play(led_pat_to_play,PL_PAT_CANINT)
end if
end sub
#if SUPPORTS_DISPLAY
sub on_kp(key_event as pl_kp_event_codes, key_code as byte)
' if key_event=PL_KP_EVENT_PRESSED then
' beep.play("B*",PL_BEEP_CANINT)
' select case key_code
' case KEY_F1:
' if io_index>0 then
' io_index=io_index-1
' else
' io_index=NUM_OF_IO-1
' end if
' print_io_cursor(io_index)
' case KEY_F2:
' if param_io(io_index).io_enabled=0 then
' stg_set("I"+str(io_index),0,"1")
' else
' stg_set("I"+str(io_index),0,"0")
' end if
' case KEY_F3:
' if param_io(io_index).io_output=0 then
' stg_set("I"+str(io_index),1,"1")
' else
' stg_set("I"+str(io_index),1,"0")
' end if
' case KEY_F4:
' if io_index<NUM_OF_IO-1 then
' io_index=io_index+1
' else
' io_index=0
' end if
' print_io_cursor(io_index)
' end select
' end if
end sub
sub on_kp_overflow()
kp_init
end sub
#endif
sub on_ser_data_sent()
' TODO: place "on_ser_data_sent" event handler code here...
end sub
sub on_ser_esc()
' TODO: place "on_ser_esc" event handler code here...
end sub
sub on_ser_overrun()
' TODO: place "on_ser_overrun" event handler code here...
end sub
sub on_sock_postdata()
' TODO: place "on_sock_postdata" event handler code here...
end sub
sub on_bt_event(bt_event as enum pl_bt_events)
luis_on_bt_event(bt_event)
end sub
sub on_bt_data_sent()
luis_on_bt_data_sent()
end sub
sub on_bt_data_arrival()
luis_bt_data_arrival()
end sub
sub callback_luis_bt_connected()
bt_connected=YES
'pat.play(PAT_BT_CONNECTED,PL_PAT_CANINT)
end sub
sub callback_luis_bt_disconnected()
bt_connected=NO
'pat.play(PAT_BT_ENABLED,PL_PAT_CANINT)
end sub
sub callback_luis_bt_enabled()
bt_enabled=YES
'pat.play(PAT_BT_ENABLED,PL_PAT_CANINT)
end sub
sub callback_luis_bt_disabled()
bt_enabled=NO
bt_connected=NO
'pat.play(PAT_BT_DISABLED,PL_PAT_CANINT)
end sub
sub callback_luis_buzz_command()
'pat.play("-GRGRGRGR-**",PL_PAT_CANINT)
end sub