-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice.tbs
More file actions
3059 lines (2613 loc) · 89.3 KB
/
device.tbs
File metadata and controls
3059 lines (2613 loc) · 89.3 KB
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
include "global.tbh"
const DEV_STAMP = "DEVICE> "
const AGG_MAX_RETRY=4
'------------------------------------------------------------------------------
declare sub device_stg_mod_sg(byref stg_name as string, index as byte, byref stg_val as string, op as en_stg_rdwr)
'------------------------------------------------------------------------------
public dim led_pat_to_play,led_pat_to_play_prev as string(18)
public dim reboot_timer as byte
public dim agg_retry_counter as byte
public dim last_sntp_date as word
public dim param_st(NUM_OF_SLOTS) as en_slot_type
public dim param_io(NUM_OF_IO) as io_type
public dim param_sp(4) as byte
public dim param_se(4) as byte
public dim param_fc(4) as byte
public dim param_br(4) as byte
public dim param_pr(4) as byte
public dim param_bb(4) as byte
public dim param_bp(4) as boolean
public dim param_te as byte
public dim param_aggEvents as agg_events
public dim s_sock as byte
public dim lcd_error_mode as boolean
dim dhcp_failed(2) as no_yes
dim prev_login_mode as cmd_modes
#if WLN_AVAILABLE
public dim wln_dev_rescan_tmr as byte
public dim wln_want_to_change_ap as no_yes
public dim wln_rescanning as no_yes
public dim rssi_update_timer as byte
#if SUPPORTS_DISPLAY
public dim wln_network_state as en_network_states
#endif
#endif
#if SUPPORTS_DISPLAY
public dim agg_cmd_received as no_yes
public dim lan_network_state as en_network_states
dim ib_chr_timer as byte
dim input_box_mode as en_ib_mode
dim ib_init as no_yes
dim chr_init as no_yes 'flag used to decide if add a new character, or replace the current character.
dim agg_cmd_progress as byte
dim prev_icon as byte
#endif
declare agg_sock as byte
declare agg_sychronized as no_yes
declare sub agg_data_send(s as string)
declare sub gprs_start_over()
declare function agg_stg_msg(stg_result as en_stg_status_codes) as string
declare sub agg_invisible_separators(byref s as string)
'==============================================================================
sub callback_agg_pre_buffrq(x as byte)
end sub
'------------------------------------------------------------------------------
sub callback_agg_buff_released()
if reboot_flag=YES then sys.reboot
end sub
'------------------------------------------------------------------------------
sub callback_agg_error(failure_code as en_agg_status_codes, byref error_item as string)
#if SUPPORTS_GPRS
if failure_code=EN_AGG_STATUS_CONNECTION_LOST then
if agg_retry_counter>0 then
agg_retry_counter=agg_retry_counter-1
if agg_retry_counter=0 then
agg_stop()
select case current_interface
case PL_SOCK_INTERFACE_WLN:
case PL_SOCK_INTERFACE_PPP:
if current_interface=PL_SOCK_INTERFACE_PPP then
#if DEV_DEBUG_PRINT
dev_debugprint("Restart GPRS")
#endif
interface_ready(PL_SOCK_INTERFACE_PPP)=NO
gprs_start_over()
end if
case else
end select
end if
else
agg_retry_counter=AGG_MAX_RETRY
end if
end if
#endif
end sub
'------------------------------------------------------------------------------
function callback_agg_external_data_source(byref source_type as string, byref parameter1 as string, byref parameter2 as string, byref parameter3 as string, rw as en_agg_rdwr)as ok_ng
callback_agg_external_data_source=OK
end function
'------------------------------------------------------------------------------
function callback_agg_external_data_modtime_sg(byref source_type as string, byref item_name as string, byref date_time as string, op as en_agg_rdwr) as ok_ng
callback_agg_external_data_modtime_sg=OK
end function
'------------------------------------------------------------------------------
function callback_agg_setting_saved(byref setting_name as string, index as byte, byref setting_val as string) as en_stg_status_codes
select case setting_name
' PWM Pulse Values
case "EV":
if index >= 0 or index < MAX_ANALOG_LINES then
analog_pwm_pulse(index) = val(setting_val)
end if
' PWM Period Values
case "FV":
if index >= 0 or index < MAX_ANALOG_LINES then
analog_pwm_period(index) = val(setting_val)
end if
' PWM Prescaler Values
case "GV":
if index >= 0 or index < MAX_ANALOG_LINES then
analog_pwm_prescaler(index) = val(setting_val)
end if
' PWM Channel Enable
case "HV":
if index >= 0 or index < MAX_ANALOG_LINES then
analog_pwm_enable(index) = val(setting_val)
end if
case "BO":
if index = 0 then
bp_setupPollingTime = val(setting_val)
end if
case "AA":
if index = 0 then
param_aggEvents.agg_eventAnalog = val(setting_val)
end if
case "AD":
if index = 0 then
param_aggEvents.agg_eventDigital = val(setting_val)
end if
case "AC":
if index = 0 then
param_aggEvents.agg_eventCounter = val(setting_val)
end if
case "AB":
if index = 0 then
param_aggEvents.agg_eventBP = val(setting_val)
end if
end select
end function
'------------------------------------------------------------------------------
sub callback_agg_setting_error(byref setting_name as string, error_code as en_stg_status_codes)
end sub
'------------------------------------------------------------------------------
function utf8_translate(byref data as string) as string
dim result as string
dim i, j as byte
dim sym as byte
dim big_sym as dword
result = ""
j = 0
for i = 1 to len(data)
sym = asc(mid(data, i, 1))
if j = 0 then
if (sym and 128) = 0 then
' Pure ASCII
result = result + chr(sym)
else
if (sym and 64) <> 0 then
if (sym and 32) = 0 then
' 110xxxxx 10xxxxxx
j = 1
big_sym = sym and 31
else
if (sym and 16) = 0 then
' 1110xxxx 10xxxxxx 10xxxxxx
j = 2
big_sym = sym and 15
else
if (sym and 8) = 0 then
' 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
j = 3
big_sym = sym and 7
else
if (sym and 4) = 0 then
' 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
j = 4
big_sym = sym and 3
else
if (sym and 2) = 0 then
' 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
j = 5
big_sym = sym and 1
end if
end if
end if
end if
end if
end if
end if
else
j = j - 1
' get 6 bits
if (sym and 192) = 128 then
big_sym = (big_sym shl 6) + (sym and 63)
end if
if j = 0 then
result = result + chr(big_sym and 255)
end if
end if
next i
utf8_translate = result
end function
function callback_agg_device_function(byref function_name as string, byref context_name as string, byref function_input as string, byref function_output as string) as ok_ng
dim param as string
dim channel as byte
select case function_name
case "init":
if proc_device_op("I", "") = "A" then
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case "reboot":
if proc_device_op("R", "") = "A" then
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case "si":
channel = val(agg_record_decode(function_input, 0))
if bp_ports(channel).enable = false then
ser.num = channel
if ser.enabled = NO then
callback_agg_device_function = NG
function_output = "Port disabled"
else
param = agg_record_decode(function_input, 1)
param = utf8_translate(param)
ser.setdata(param)
ser.send()
callback_agg_device_function = OK
end if
else
callback_agg_device_function = NG
function_output = "Port disabled"
end if
case "sao": ' Set Analog Output
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 48 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
if analog_set(channel, val(agg_record_decode(function_input, 1)), false) then
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
else
callback_agg_device_function = NG
end if
case "gai": ' Get Analog Input
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 48 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
agg_record_encode(function_output, ftostr(analog_float_values(channel), FTOSTR_MODE_AUTO, 5))
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case "sdo": ' Set Digital Output
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 49 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
if io_set(channel, val(agg_record_decode(function_input, 1))) then
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
else
callback_agg_device_function = NG
end if
case "gdi": ' Get Digital Input
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 49 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
agg_record_encode(function_output, str(param_io(channel).io_state))
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case "spwm": ' Set PWM Values
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 48 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
if analog_pwm_set(channel, val(agg_record_decode(function_input, 1))) then
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
else
callback_agg_device_function = NG
end if
case "spwme": ' Set PWM Enable
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 48 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
if analog_pwm_onoff(channel, val(agg_record_decode(function_input, 1))) then
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
else
callback_agg_device_function = NG
end if
case "gpwme": ' Get PWN Enable
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 48 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
agg_record_encode(function_output, str(analog_pwm_enable(channel)))
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case "gc": ' Get Counter
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 49 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
agg_record_encode(function_output, str(io_cn(channel).counter))
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case "cc": ' Clear Counter
channel = val(agg_record_decode(function_input, 0)) - 1
#if PLATFORM_ID = TPP3WG2
if channel >= 0 and channel < 48 then
#endif
#if PLATFORM_ID = TPP2WG2
if channel >= 0 and channel < 24 then
#endif
io_cn(channel).counter = 0
callback_agg_device_function = OK
else
callback_agg_device_function = NG
end if
case else:
callback_agg_device_function = NG
end select
end function
'------------------------------------------------------------------------------
sub callback_agg_rtc_sg(byref wdaycount as word, byref wmincount as word, byref bsec as byte, byref wmilsec as word, op as en_agg_rdwr)
select case op
case EN_AGG_GET:
rtc.getdata(wdaycount,wmincount,bsec)
wmilsec=0
case EN_AGG_SET:
rtc.setdata(wdaycount,wmincount,bsec)
end select
end sub
'------------------------------------------------------------------------------
sub callback_agg_convert_table_field(byref table_name as string, byref field_name as string, byref field_value as string, op as en_agg_rdwr)
end sub
'------------------------------------------------------------------------------
function callback_agg_convert_setting(byref setting_name as string, index as byte, byref setting_value as string, op as en_agg_rdwr) as ok_ng
callback_agg_convert_setting=OK
device_stg_mod_sg(setting_name,index,setting_value,op)
end function
'------------------------------------------------------------------------------
sub callback_agg_synchronized()
login_timeout_reset()
agg_retry_counter=AGG_MAX_RETRY
end sub
'------------------------------------------------------------------------------
function callback_agg_get_firmware_version() as string
callback_agg_get_firmware_version=FIRMWARE_VERSION
end function
'------------------------------------------------------------------------------
sub callback_agg_firmware_upload_progress(total_size as dword, remain_size as dword)
end sub
'------------------------------------------------------------------------------
sub ethernet_link_state_change(new_state as pl_net_linkstate)
if new_state=PL_NET_LINKSTAT_NOLINK then
'Ethernet is gone
#if DEV_DEBUG_PRINT
dev_debugprint("Ethernet FAILED")
#endif
interface_ready(PL_SOCK_INTERFACE_NET)=NO
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_NET, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
'See if we can replace it with something...
#if WLN_AVAILABLE
if param_we<>WE_DISABLED then
'Try to replace with Wi-Fi
if param_we=WE_ENABLED_ONDEMAND and wln_already_started=NO then
#if DEV_DEBUG_PRINT
dev_debugprint("Start Wi-Fi")
#endif
num_wln_failures=0
wln_result = wln_start(param_wa,param_ws,wln_password,PL_WLN_DOMAIN_FCC,no,PL_WLN_ASCAN_INFRASTRUCTURE)
if wln_result <> WLN_STATUS_OK then
#if DEV_DEBUG_PRINT
dev_debugprint("Wi-Fi MALFUNCTIONED (could not start)")
#endif
wln_error=YES
else
wln_error=NO
wln_already_started=YES
end if
end if
if interface_ready(PL_SOCK_INTERFACE_WLN)=YES then
change_current_interface(PL_SOCK_INTERFACE_WLN)
end if
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
'255 means Wi-Fi already fail and then we should allow GPRS to start (if enabled)
if num_wln_failures<>255 and wln_error=NO then
exit sub
end if
end if
#endif
#if SUPPORTS_GPRS
if param_ge<>GE_DISABLED then
'Try to replace with GPRS
if param_ge=GE_ENABLED_ONDEMAND and gprs_already_started=NO then
#if DEV_DEBUG_PRINT
dev_debugprint("Start GPRS")
#endif
if gprs_start(param_ga,param_gn,param_gu,param_gp)<>GPRS_STATUS_OK then
#if DEV_DEBUG_PRINT
dev_debugprint("GPRS MALFUNCTIONED (could not start)")
#endif
gprs_error=YES
else
gprs_error=NO
gprs_already_started=YES
end if
end if
if interface_ready(PL_SOCK_INTERFACE_PPP)=YES then
change_current_interface(PL_SOCK_INTERFACE_PPP)
end if
end if
#endif
else
'Ethernet will be used from now on
#if DEV_DEBUG_PRINT
dev_debugprint("Ethernet OK")
#endif
interface_ready(PL_SOCK_INTERFACE_NET)=YES
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_NET, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
#if WLN_AVAILABLE
'Stop Wi-Fi if it is in the on-demand mode
if param_we=WE_ENABLED_ONDEMAND then
#if DEV_DEBUG_PRINT
dev_debugprint("Stop Wi-Fi")
#endif
wln_stop()
interface_ready(PL_SOCK_INTERFACE_WLN)=NO
wln_already_started=NO
end if
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
#endif
#if SUPPORTS_GPRS
'Stop GPRS if it is in the on-demand mode
if param_ge=GE_ENABLED_ONDEMAND and gprs_already_started=YES then
#if DEV_DEBUG_PRINT
dev_debugprint("Stop GPRS")
#endif
gprs_stop()
interface_ready(PL_SOCK_INTERFACE_PPP)=NO
gprs_already_started=NO
end if
#endif
change_current_interface(PL_SOCK_INTERFACE_NET)
end if
end sub
'------------------------------------------------------------------------------
sub close_interface_sockets(interface as pl_sock_interfaces)
dim f,sock_num_bup as byte
dim i as word
sock_num_bup=sock.num
for f=0 to sock.numofsock-1
sock.num=f
if sock.currentinterface=interface and sock.statesimple<>PL_SSTS_CLOSED then
sock.discard
end if
next f
i=sys.timercount
wait_close_interface_sockets:
for f=0 to sock.numofsock-1
sock.num=f
if sock.currentinterface=interface and sock.statesimple<>PL_SSTS_CLOSED and sys.timercount-i<3 and sys.timercount>=i then
goto wait_close_interface_sockets
end if
next f
sock.num=sock_num_bup
end sub
'------------------------------------------------------------------------------
sub change_current_interface(new_interface as pl_sock_interfaces)
if current_interface<>new_interface then
'this will allow immediate logins on the new current interface
if login_mode=CMD_MODE_AGGREGATE then
agg_stop()
end if
if login_mode=CMD_MODE_HTTP or login_mode=CMD_MODE_UDP then
login_mode=CMD_MODE_NONE
end if
sock.inconenabledmaster=NO
close_interface_sockets(current_interface)
current_interface=new_interface
'receive UDP commands on the new current interface only
sock.num=udp_sock
select case current_interface
case PL_SOCK_INTERFACE_NET: sock.allowedinterfaces="NET"
#if WLN_AVAILABLE
case PL_SOCK_INTERFACE_WLN: sock.allowedinterfaces="WLN"
#endif
case PL_SOCK_INTERFACE_PPP: sock.allowedinterfaces="PPP"
end select
sock.inconenabledmaster=YES
#if DEV_DEBUG_PRINT
select case current_interface
case PL_SOCK_INTERFACE_NET: dev_debugprint("Set current interface to Ethernet")
#if WLN_AVAILABLE
case PL_SOCK_INTERFACE_WLN: dev_debugprint("Set current interface to Wi-Fi")
#endif
case PL_SOCK_INTERFACE_PPP: dev_debugprint("Set current interface to GPRS")
end select
#endif
end if
end sub
'------------------------------------------------------------------------------
#if WLN_AVAILABLE
sub callback_wln_ok()
#if DEV_DEBUG_PRINT
dev_debugprint("Wi-Fi OK")
#endif
interface_ready(PL_SOCK_INTERFACE_WLN)=YES
num_wln_failures=0
'Should Wi-Fi become our current interface?
if interface_ready(PL_SOCK_INTERFACE_NET)=NO then
change_current_interface(PL_SOCK_INTERFACE_WLN)
#if SUPPORTS_GPRS
'Stop GPRS if it is in the on-demand mode
if param_ge=GE_ENABLED_ONDEMAND and gprs_already_started=YES then
#if DEV_DEBUG_PRINT
dev_debugprint("Stop GPRS")
#endif
gprs_stop()
interface_ready(PL_SOCK_INTERFACE_PPP)=NO
gprs_already_started=NO
end if
#endif
end if
#if SUPPORTS_DISPLAY
if param_wd=NO then
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
end if
wln_dev_rescan_tmr=WLN_RESCAN_TMR_CTR
if wln_network_state<>NETWORK_DHCP_IP_OBTAINED and wln_network_state<>NETWORK_DHCP_FIXED_IP then
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
end if
#endif
end sub
#endif
'------------------------------------------------------------------------------
#if WLN_AVAILABLE
sub callback_wln_failure(wln_state as en_wln_status_codes)
dim new_state as en_network_states
if num_wln_failures=255 then exit sub
if num_wln_failures<MAX_NUM_WLN_FAILURES then
num_wln_failures=num_wln_failures+1
else
num_wln_failures=255
#if DEV_DEBUG_PRINT
dev_debugprint("Wi-Fi FAILED")
#endif
interface_ready(PL_SOCK_INTERFACE_WLN)=NO
#if SUPPORTS_GPRS
if param_ge<>GE_DISABLED and interface_ready(PL_SOCK_INTERFACE_NET)=NO then
'Try to replace with GPRS
if param_ge=GE_ENABLED_ONDEMAND and gprs_already_started=NO then
#if DEV_DEBUG_PRINT
dev_debugprint("Start GPRS")
#endif
if gprs_start(param_ga,param_gn,param_gu,param_gp)<>GPRS_STATUS_OK then
#if DEV_DEBUG_PRINT
dev_debugprint("GPRS MALFUNCTIONED (could not start)")
#endif
gprs_error=YES
else
gprs_error=NO
gprs_already_started=YES
end if
end if
if interface_ready(PL_SOCK_INTERFACE_PPP)=YES and interface_ready(PL_SOCK_INTERFACE_NET)=NO then
change_current_interface(PL_SOCK_INTERFACE_PPP)
end if
end if
#endif
end if
#if SUPPORTS_DISPLAY
select case wln_state
case WLN_STATUS_SCANNING_FAILURE:
new_state=NETWORK_WLN_SCANNING_FAIL
case WLN_STATUS_ASSOCIATION_FAILURE:
new_state=NETWORK_WLN_ASSOCIATION_FAIL
case WLN_STATUS_DISASSOCIATION:
new_state=NETWORK_WLN_DISASSOCIATE
case else:
new_state=NETWORK_WLN_FATAL_ERROR
end select
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
end sub
'------------------------------------------------------------------------------
sub callback_wln_mkey_progress_update(progress as byte)
#if SUPPORTS_DISPLAY
lcd_print_msg("Calculating... "+str(progress)+"%",0,lcd.height-lcd.fontheight*2,lcd.width,lcd.fontheight+2,PL_LCD_TEXT_ALIGNMENT_TOP_CENTER,NO)
#endif
end sub
'------------------------------------------------------------------------------
sub callback_wln_pre_buffrq(required_buff_pages as byte)
end sub
'------------------------------------------------------------------------------
sub callback_wln_rescan_result(current_rssi as byte, scan_rssi as byte, different_ap as no_yes)
dim x,f as byte
dim s1, s2, s3, s4 as string(32)
wln_rescanning=NO
if different_ap=NO then
goto reprint_ip
end if
if scan_rssi<current_rssi then
goto reprint_ip
end if
if scan_rssi-current_rssi<5 then
reprint_ip:
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
exit sub
end if
x=val(stg_get("WE",0))
s1=stg_get("WA",0)
select case x
case WLN_SECURITY_MODE_DISABLED:
s2=""
case WLN_SECURITY_MODE_WEP64,WLN_SECURITY_MODE_WEP128:
s2=stg_get("WP",0)
if x=WLN_SECURITY_MODE_WEP64 and len(s2)=5 then
s4=""
for f=1 to 5
s3=mid(s2,f,1)
s3=hex(asc(s3))
s3=right(s3,len(s3)-2)
if len(s3)<2 then s3="0"+s3
s4=s4+s3
next f
s2=s4
end if
if x=WLN_SECURITY_MODE_WEP128 and len(s2)=13 then
s4=""
for f=1 to 13
s3=mid(s2,f,1)
s3=hex(asc(s3))
s3=right(s3,len(s3)-2)
if len(s3)<2 then s3="0"+s3
s4=s4+s3
next f
s2=s4
end if
case else:
s2=stg_get("WK",0)
end select
wln_want_to_change_ap=YES
wln_change(s1,x,s2) 'this could fail but we don't care
end sub
'----------------------------------------------------------------------------
sub callback_wln_starting_association()
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
end sub
'------------------------------------------------------------------------------
sub callback_wln_rescan_for_better_ap()
end sub
'------------------------------------------------------------------------------
public sub device_update_rssi()
if wln_check_association()<>WLN_ASSOCIATION_RETVAL_YES then exit sub
rssi_update_timer=rssi_update_timer-1
if rssi_update_timer=0 then
wln_update_rssi()
rssi_update_timer=RSSI_UPDATE_TIMER_CTR
end if
end sub
'------------------------------------------------------------------------------
public sub device_check_for_better_ap()
if wln_check_association()<>WLN_ASSOCIATION_RETVAL_YES then
wln_dev_rescan_tmr=WLN_RESCAN_TMR_CTR
else
'when we are associated...
if wln_dev_rescan_tmr>0 then
wln_dev_rescan_tmr=wln_dev_rescan_tmr-1
if wln_dev_rescan_tmr=0 then
sock.num=agg_sock
if sock.statesimple=PL_SSTS_EST then
wln_dev_rescan_tmr=WLN_RESCAN_TMR_CTR
if wln.rssi>=WLN_RSSI_1 then
exit sub
else
goto rescan 'if TCP established we rescan only if current signal is weak
end if
else
wln_dev_rescan_tmr=WLN_RESCAN_TMR_CTR2
rescan: wln_rescanning=YES
#if SUPPORTS_DISPLAY
lcd_display_page(LCD_PAGE_WLN, LCD_PRINT_MODE_GENTLE, LCD_PRINT_ELEMENT_ALL)
#endif
wln_start_rescan() 'if TCP not established we rescan periodically no matter whata
end if
end if
end if
end if
end sub
#endif
'---------------------------------------------------------------------------------------------------
#if WLN_AVAILABLE and SUPPORTS_DISPLAY
function get_ap_name() as string
get_ap_name=stg_get("WA",0)
if len(get_ap_name)>9 then
get_ap_name=left(get_ap_name,8)
get_ap_name=get_ap_name+"."
end if
end function
#endif
'----------------------------------------------------------------------------
#if SUPPORTS_DISPLAY
'----------------------------------------------------------------------------
public sub print_wifi()
dim icon as byte
dim s as string(8)
if enter_menu=YES then exit sub
#if WLN_AVAILABLE
if wln.enabled=YES then
select case wln_network_state
case NETWORK_WLN_DISABLED:
exit sub
case NETWORK_DHCP_FIXED_IP, NETWORK_DHCP_OBTAINING_IP, NETWORK_DHCP_FAIL_OBTAINING_IP, NETWORK_DHCP_IP_OBTAINED, NETWORK_WLN_JOINED:
if wln.rssi<WLN_RSSI_1 then
icon=0
end if
if wln.rssi<WLN_RSSI_2 and wln.rssi>=WLN_RSSI_1 then
icon=1
end if
if wln.rssi<WLN_RSSI_3 and wln.rssi>=WLN_RSSI_2 then
icon=2
end if
if wln.rssi<WLN_RSSI_4 and wln.rssi>=WLN_RSSI_3 then
icon=3
end if
if wln.rssi>=WLN_RSSI_4 then
icon=4
end if
case NETWORK_WLN_SCANNING_FAIL, NETWORK_WLN_JOINING:
icon=5
case NETWORK_WLN_DISASSOCIATE, NETWORK_WLN_ASSOCIATION_FAIL:
icon=6
case NETWORK_WLN_INVALID_WKEY:
icon=8
case else
icon=7
end select
else
icon=7
end if
#else
icon=7
#endif
if icon<>prev_icon then
romfile.open("wifi.bmp")
lcd.bmp(romfile.offset,78,0,icon*24,0,24,18)
prev_icon=icon
end if
romfile.open("Tibbo-5x7(V).bin")
lcd.setfont(romfile.offset)
lcd.texthorizontalspacing=1
lcd.forecolor=TXT_GRAY
lcd.backcolor=LCD_BACKCOLOR
select case login_mode
case CMD_MODE_AGGREGATE:
s="AGG"
lcd.print(s,107,1)
sock.num=agg_sock
select case sock.statesimple
case PL_SSTS_CLOSED:
select case sock.state
case PL_SST_CL_PCLOSED,PL_SST_CL_PRESET_POPENING, PL_SST_CL_PRESET_AOPENING,
PL_SST_CL_PRESET_EST,PL_SST_CL_PRESET_PCLOSING,PL_SST_CL_PRESET_ACLOSING,
PL_SST_CL_PRESET_STRANGE,PL_SST_PC:
s="XXXX"
case else:
s="----"
end select
case PL_SSTS_ARP:
s="O)))"
case PL_SSTS_PO,PL_SSTS_AO:
s="O==>"
case PL_SSTS_EST:
if agg_cmd_received=YES then
agg_cmd_received=NO
agg_cmd_progress=agg_cmd_progress+1
if agg_cmd_progress>3 then agg_cmd_progress=0
select case agg_cmd_progress
case 0:s=">==="
case 1:s="=>=="
case 2:s="==>="
case 3:s="===>"
end select
else
s="===="
end if