-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemleak-test.cpp
More file actions
1575 lines (1432 loc) · 41.1 KB
/
memleak-test.cpp
File metadata and controls
1575 lines (1432 loc) · 41.1 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
// system includes
#include <sys/types.h>
#include <syslog.h>
#include <dirent.h>
// lib includes
#include <iostream>
#include <libconfig.h++>
// local includes
#define EOC_DEBUG
#include <generic/EOC_generic.h>
#include <snmp/snmp-generic.h>
#include <utils/hash_table.h>
#include <config/EOC_config.h>
#include <devs/EOC_dev.h>
#include <devs/EOC_dev_terminal.h>
#include <devs/EOC_mr17h.h>
#include <devs/EOC_mr16h.h>
#include <EOC_main.h>
#include <EOC_pci.h>
#include <EOC_cfg_options.h>
#include <app-if/err_codes.h>
#define CONF_FILE_NAME "/etc/eocd/eocd.conf"
using namespace libconfig;
using namespace std;
//---------------------------------------------------------------------//
EOC_dev_terminal *init_dev(char *name);
void delete_dev(EOC_dev *dev);
static void del_channel_elem(hash_elem *h) {
delete (channel_elem*) h;
}
static void del_conf_profile(hash_elem *h) {
delete (conf_profile*) h;
}
int main() {
debug_lev = DFULL;
// 3. Channels creation/deleteion
hash_table conf_profs(SNMP_ADMIN_LEN), channels(IF_NAME_LEN);
for (int i = 0; i < 200; i++) {
char pname[256];
sprintf(pname, "def%d", i);
conf_profile *cprof = new conf_profile;
memset(cprof, 0, sizeof(conf_profile));
cprof->name = strdup(pname);
cprof->nsize = strlen(cprof->name);
cprof->access = conf_profile::profRW;
// Profile settings
cprof->conf.annex = annex_a;
cprof->conf.wires = twoWire;
cprof->conf.power = noPower;
cprof->conf.clk = localClk;
cprof->conf.line_probe = disable;
cprof->conf.rate = 2304;
cprof->conf.tcpam = tcpam16;
// compatibility: default is basic
cprof->comp = EOC_dev::comp_base;
conf_profs.add(cprof);
}
while (1) {
for (int i = 2; i < 4; i++) {
char name1[64],*name;
char *cprof = strdup("def10");
sprintf(name1, "dsl%d", i);
name = strdup(name1);
PDEBUG(DFULL, "Initialize device");
EOC_dev_terminal *dev = init_dev(name);
if (!dev) {
PDEBUG(DERR, "dev = %p", dev);
return -1;
}
PDEBUG(DFULL, "Initialize config");
EOC_config *cfg = new EOC_config(&conf_profs, cprof, 1);
PDEBUG(DERR, "CONFIG=%p,cprof=%s", cfg, cfg->cprof());
channel_elem *el = new channel_elem(dev, cfg,delete_dev);
el->name = name;
el->nsize = strlen(name);
//el->is_updated = 1;
PDEBUG(DFULL, "%s pointer = %p", el->name, el);
channels.add(el);
channels.sort();
}
channels.clear(del_channel_elem);
}
/*
// 2.(OK) Conf profiles table management
hash_table conf_profs(SNMP_ADMIN_LEN);
while (1) {
printf("\n-----------------ADD--------------------\n");
for (int i = 0; i < 2000; i++) {
char pname[256];
sprintf(pname, "def%d", i);
conf_profile *cprof = new conf_profile;
memset(cprof, 0, sizeof(conf_profile));
cprof->name = strdup(pname);
cprof->nsize = strlen(cprof->name);
cprof->access = conf_profile::profRW;
// Profile settings
cprof->conf.annex = annex_a;
cprof->conf.wires = twoWire;
cprof->conf.power = noPower;
cprof->conf.clk = localClk;
cprof->conf.line_probe = disable;
cprof->conf.rate = 2304;
cprof->conf.tcpam = tcpam16;
// compatibility: default is basic
cprof->comp = EOC_dev::comp_base;
conf_profs.add(cprof);
}
printf("\n-----------------CLEAR------------------\n");
sleep(1);
conf_profs.clear(del_conf_profile);
}
/*
// 1. (OK) Test profile createion/destruction
while (1) {
char *defname = "default";
conf_profile *cprof = new conf_profile;
memset(cprof, 0, sizeof(conf_profile));
cprof->name = strdup("default");
cprof->nsize = strlen(cprof->name);
cprof->access = conf_profile::profRO;
// Profile settings
cprof->conf.annex = annex_a;
cprof->conf.wires = twoWire;
cprof->conf.power = noPower;
cprof->conf.clk = localClk;
cprof->conf.line_probe = disable;
cprof->conf.rate = 2304;
cprof->conf.tcpam = tcpam16;
// compatibility: default is basic
cprof->comp = EOC_dev::comp_base;
// Check compatibility. Should never be erroneous
if (cprof->check_comp()) {
syslog(
LOG_ERR,"eocd(read_config): Fatal error: \"default\" profile is not compatible with basical level");
PDEBUG(
DERR,
"eocd(read_config): Fatal error: \"default\" profile is not compatible with basical level");
exit(1);
}
delete cprof;
}
*/
}
/*
int EOC_main::
read_config()
{
char *name;
int i;
tick_per_min = TICK_PER_MINUTE_DEF;
hash_elem *el;
char *opt;
char *endp;
unsigned long tmp_val;
int ret;
int retval = 0;
PDEBUG(DFULL,"Start");
// Free KDB
kdbinit();
//----------- read tick per minute value ------------------//
// 1. Check that conf_profile group exist
if( ret = kdb_appget("sys_eocd_actpermin",&opt) ) {
if( ret> 1 ) {
syslog(LOG_ERR,"eocd(read_config): Found several options containing \"sys_eocd_actpermin\". Use first: %s",opt);
PDEBUG(DERR,"eocd(read_config): Found several options containing \"sys_eocd_actpermin\". Use first: %s",opt);
}
tmp_val = strtoul(opt,&endp,0);
if( opt != endp )
tick_per_min = tmp_val;
}
PDEBUG(DINFO,"tick_p_min = %d",tick_per_min);
//----------- read span configuration profiles ------------------//
do {
char *saveptr, *token = NULL;
char *prof_prefix = "sys_eocd_sprof";
char *popt;
char *topt = opt;
// 1. Add default configuration profile compatible with all devices
{
}
// 2. read all elements of conf_profiles
#define OPTINDEX(grp,grpsize,name,idx) \
idx = -1; \
for(int k=0;k<grpsize;k++){ \
if( !strncmp(grp[k].optname(),name,MAX_OPTSTR) ){ \
idx = k; \
break; \
} \
}
// Changing profile index till fault
for(int i=0;;i++) {
char option[256];
cfg_option prof_opts[] = {cfg_option(1,"name"),cfg_option(1,"tcpam",tcpam_vals,tcpam_vsize),
cfg_option(1,"annex",annex_vals,annex_vsize),cfg_option(1,"power",power_vals,power_vsize),
cfg_option(1,"rate",0,0,1,64),cfg_option(0,"mrate",0,0,1,64),cfg_option(1,"comp",comp_vals,comp_vsize)};
int prof_optsnum = sizeof(prof_opts)/sizeof(cfg_option);
char *str1,*str2;
char *token,*subtoken;
char *saveptr1,*saveptr2;
int j;
sprintf(option,"sys_eocd_sprof_%d",i);
if( !(ret = kdb_appget(option,&opt)) ) {
break;
} else if( ret> 1 ) {
syslog(LOG_ERR,"eocd(read_config): Found several options containing \"%s\". Use first: %s",option,opt);
PDEBUG(DERR,"eocd(read_config): Found several options containing \"%s\". Use first: %s",option,opt);
}
str1 = opt;
for (j = 1;; j++, str1 = NULL) {
token = strtok_r(str1, " ", &saveptr1);
if (token == NULL)
break;
printf("%d: %s\n", j, token);
char ptrs[2][MAX_OPTSTR];
int i;
for (i=0,str2 = token; i<2; str2 = NULL,i++) {
subtoken = strtok_r(str2,"=", &saveptr2);
if (subtoken == NULL)
break;
//printf(" --> %s\n", subtoken);
strncpy(ptrs[i],subtoken,MAX_OPTSTR);
}
printf("%s = %s\n",ptrs[0],ptrs[1]);
for(i=0;i<prof_optsnum;i++) {
cfg_option::chk_res ret;
ret = prof_opts[i].chk_option(ptrs[0],ptrs[1]);
if( ret == cfg_option::Accept || ret == cfg_option::Exist )
break;
}
}
int errflg = 0;
for(j=0;j<prof_optsnum;j++) {
if( !prof_opts[j].is_valid() && prof_opts[j].is_basic() ) {
syslog(LOG_ERR,"eocd(read_config): profile \"%s\", wrong \"%s\" value",option,prof_opts[j].optname());
PDEBUG(DERR,"eocd(read_config): profile \"%s\", wrong \"%s\" value",option,prof_opts[j].optname());
errflg = 1;
}
if( !strncmp(prof_opts[j].optname(),"name",5) && prof_opts[j].is_int() ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR \"%s\" should be string value",prof_opts[j].optname());
PDEBUG(DERR,"eocd(read_config): FATAL ERROR \"%s\" should be string value",prof_opts[j].optname());
exit(0);
} else if( strncmp(prof_opts[j].optname(),"name",5) && !prof_opts[j].is_int() ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR \"%s\" should be int value",prof_opts[j].optname());
PDEBUG(DERR,"eocd(read_config): FATAL ERROR \"%s\" should be int value",prof_opts[j].optname());
exit(0);
}
// DEBUG
if( prof_opts[j].is_int() ) {
PDEBUG(DFULL," %s = %d",prof_opts[j].optname(),prof_opts[j].value());
} else {
PDEBUG(DFULL," %s = %s",prof_opts[j].optname(),prof_opts[j].svalue());
}
}
if( errflg ) { // Skip prifile
syslog(LOG_ERR,"eocd(read_config): Skip profile \"%s\"",option);
PDEBUG(DERR,"eocd(read_config): Skip profile \"%s\"",option);
continue;
}
// Get profile name
int ind;
char *curopt;
// Profile name
curopt = "name";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
// Find profile
int namelen = strnlen(prof_opts[ind].svalue(),256);
conf_profile *cprof = (conf_profile *)conf_profs.find(prof_opts[ind].svalue(),namelen);
if( !cprof ) {
cprof = new conf_profile;
memset(cprof,0,sizeof(conf_profile));
cprof->name = strndup(prof_opts[ind].svalue(),256);
cprof->nsize = namelen;
// Profile service info
cprof->access = conf_profile::profRW;
// Profile content
cprof->conf.wires = twoWire; // nprof->conf.wires = (wires_t)wires;
cprof->conf.line_probe = disable;
conf_profs.add(cprof);
} else if( cprof->access == conf_profile::profRO ) {
continue;
}
// Profile annex value
curopt = "annex";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
cprof->conf.annex = (annex_t)prof_opts[ind].value();
// Profile power value
curopt = "power";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
cprof->conf.power = (power_t)prof_opts[ind].value();
// Profile tcpam val
curopt = "tcpam";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
cprof->conf.tcpam = (tcpam_t)prof_opts[ind].value();
// Profile tcpam val
curopt = "rate";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
cprof->conf.rate = prof_opts[ind].value();
if( (int)cprof->conf.rate < 0 ) {
// Get mrate value
PDEBUG(DFULL,"Get mrate option value");
curopt = "mrate";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
cprof->conf.rate = prof_opts[ind].value();
}
// compatibility
curopt = "comp";
OPTINDEX(prof_opts,prof_optsnum,curopt,ind);
if( ind < 0 ) {
syslog(LOG_ERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
PDEBUG(DERR,"eocd(read_config): FATAL ERROR cannot find \"%s\" index",curopt);
exit(0);
}
cprof->comp = (EOC_dev::compatibility_t)prof_opts[ind].value();
// Check compatibility
if( cprof->check_comp() ) {
PDEBUG(DERR,"Not compatible!");
char buf[256];
EOC_dev::comp_name(cprof->comp,buf);
syslog(LOG_ERR,"eocd(read_config): Skip profile \"%s\", settings not compatible with %s level",
cprof->name,buf);
PDEBUG(DERR,"After syslog!");
PDEBUG(DERR,"eocd(read_config): Skip profile \"%s\", settings not compatible with %s level",
cprof->name,buf);
continue;
}
PDEBUG(DERR,"Save profile %s",cprof->name);
cprof->is_updated = 1;
}
}while(0);
PDEBUG(DFULL,"Clear conf table from old profiles");
conf_profs.clear(del_conf_profile);
conf_profs.sort();
//----------- read channels configuration ------------------//
do {
char *saveptr = NULL;
char *chan_prefix = "sys_eocd_chan";
char *popt;
char *topt;
char chlist[512];
if( !(ret = kdb_appget("sys_eocd_channels",&opt) ) ) {
break;
} else if( ret> 1 ) {
syslog(LOG_ERR,"eocd(read_config): Found several options containing \"sys_eocd_channels\". Use first: %s",opt);
PDEBUG(DERR,"eocd(read_config): Found several options containing \"sys_eocd_channels\". Use first: %s",opt);
}
strncpy(chlist,opt,512);
PDEBUG(DFULL,"start channels processing, opt=\"%s\"",chlist);
for(topt=chlist;;topt=NULL) {
char pci_pair[256],*ptoken,*tpair=NULL;
char *saveptr1 = NULL;
int pcislot = -1;
int pcidev = -1;
int valid = 1;
char option[256];
char *option_val;
if( !(ptoken = strtok_r(topt," ",&saveptr)) ) {
PDEBUG(DFULL,"ptoken = 0");
break;
}
strncpy(pci_pair,ptoken,256);
PDEBUG(DFULL,"Process channel: pci_pair(%p)=%s",pci_pair,pci_pair);
int cnt = 0;
for(tpair=pci_pair;;tpair=NULL,cnt++) {
char *token = strtok_r(tpair,":",&saveptr1);
PDEBUG(DFULL,"pci_pair token=%s",token);
if( !token )
break;
tmp_val = strtoul(token,&endp,0);
if( token == endp ) {
break;
}
switch(cnt) {
case 0:
pcislot = tmp_val;
break;
case 1:
pcidev = tmp_val;
break;
}
}
if( cnt != 2 ) {
syslog(LOG_ERR,"eocd(read_config): Error channel identification: %s",pci_pair);
PDEBUG(DERR,"eocd(read_config): Error channel identification: %s, slot=%d, dev=%d",pci_pair,pcislot,pcidev);
continue;
}
name = pci2dname(pcislot,pcidev);
if( !name ) {
syslog(LOG_ERR,"eocd(read_config): Error channel identification: %s, no correspond iface",pci_pair);
PDEBUG(DERR,"eocd(read_config): Error channel identification: %s, no correspond iface",pci_pair);
continue;
}
PDEBUG(DINFO,"pci2dname=%s",name);
// Master/Slave option. default = slave
int master = 0;
snprintf(option,256,"%s_s%04d_%d_master",chan_prefix,pcislot,pcidev);
PDEBUG(DFULL,"master option=%s",option);
if( ret = kdb_appget(option,&option_val)) {
master = atoi(option_val);
} else {
master = -1;
}
if( master != 1 && master != 0) {
syslog(LOG_ERR,"eocd(read_config): wrong \"%s\" value in %s channel: %d , may be 0,1. Will set default",
option,name,master);
PDEBUG(DERR,"eocd(read_config): wrong \"%s\" value in %s channel: %d , may be 0,1. Will set default",
option,name,master);
master = -1;
}
// Configuration profile option
// if configuration profile not exist or option is missing set cprof to "default"
snprintf(option,256,"%s_s%04d_%d_confprof",chan_prefix,pcislot,pcidev);
char *cprof;
if( ret = kdb_appget(option,&option_val)) {
cprof = strndup(option_val,SNMP_ADMIN_LEN);
} else {
cprof = strdup("default");
}
PDEBUG(DFULL,"CPROF=%s",cprof);
// check that strdup succeed
if( !cprof ) {
syslog(LOG_ERR,"eocd(read_config): Not enought mmory");
PDEBUG(DERR,"eocd(read_config): Not enought memory");
exit(1);
}
PDEBUG(DFULL,"Check CPROF=%s",cprof);
// check that profile exist
if( conf_profs.find((char*)cprof,strlen(cprof)) == NULL ) {
PDEBUG(DERR,"Error in find cprof");
syslog(LOG_ERR,"eocd(read_config): wrong \"%s\" value in %s channel: no such profile \"%s\", set \"default\"",
option,name,cprof);
PDEBUG(DERR,"eocd(read_config): wrong \"%s\" value in %s channel: no such profile \"%s\", set \"default\"",
option,name,cprof);
free(cprof);
cprof = strdup("default");
}
PDEBUG(DFULL,"CPROF=%s is exist",cprof);
// Apply configuration to channel. We now can always apply
int eocd_apply = 1;
// Check existence of channel
channel_elem *el = (channel_elem *)channels.find(name,strlen(name));
PDEBUG(DFULL,"el=%p",el);
if( master < 0 ) {
if( !el ) {
// if master option is not specified and channel not exist
// setup it as slave
master = 0;
} else {
if( el->eng->get_type() == slave ) {
master = 0;
} else {
master = 1;
}
}
}
// If channel is slave OR
// master option is broken but channel exist & it is slave
// setup only responder part
if( !master ) {
PDEBUG(DINFO,"Add slave");
if( name ) {
if( add_slave(name,cprof) ) {
syslog(LOG_ERR,"eocd(read_config): cannot add channel \"%s\" - no such device",name);
PDEBUG(DERR,"eocd(read_config): cannot add channel \"%s\" - no such device",name);
}
}
continue;
}
// PBO options
int pbomode = 0;
snprintf(option,256,"%s_s%04d_%d_pbomode",chan_prefix,pcislot,pcidev);
if( ret = kdb_appget(option,&option_val)) {
pbomode = atoi(option_val);
}
snprintf(option,256,"%s_s%04d_%d_pboval",chan_prefix,pcislot,pcidev);
char *pboval;
if( ret = kdb_appget(option,&option_val)) {
pboval = strndup(option_val,128);
} else {
pboval = strdup("");
}
// check that strdup succeed
if( !cprof ) {
syslog(LOG_ERR,"eocd(read_config): Not enought mmory");
PDEBUG(DERR,"eocd(read_config): Not enought memory");
continue;
}
// Repeaters option
int repeaters = 0;
snprintf(option,256,"%s_s%04d_%d_regs",chan_prefix,pcislot,pcidev);
if( ret = kdb_appget(option,&option_val)) {
repeaters = atoi(option_val);
}
if( repeaters <0 || repeaters> MAX_REPEATERS ) {
syslog(LOG_ERR,"eocd(read_config): wrong \"%s\" value in %s channel: %d , may be 0-%d",
option,name,repeaters,MAX_REPEATERS);
PDEBUG(DERR,"eocd(read_config): wrong \"%s\" value in %s channel: %d , may be 0-%d",
option,name,repeaters,MAX_REPEATERS);
continue;
}
PDEBUG(DINFO,"%s: apply config from cfg-file = %d",name,eocd_apply);
// TODO: Add alarm handling
PDEBUG(DERR,"Add master");
if( name ) {
PDEBUG(DERR,"Call add_master");
if( add_master(name,cprof,NULL,repeaters,tick_per_min,pbomode,pboval,eocd_apply) ) {
syslog(LOG_ERR,"(%s): cannot add channel \"%s\" - no such device",
config_file,name);
PDEBUG(DERR,"(%s): cannot add channel \"%s\" - no such device",
config_file,name);
free(pboval);
continue;
}
}
free(pboval);
}
}while(0);
PDEBUG(DFULL,"Clear Channels table from old channels");
channels.clear(del_channel_elem);
channels.sort();
// Close KDB data base
db_close();
exit:
PDEBUG(DFULL,"retval=%d",retval);
return retval;
}
// Write command is not implemented in this variant of configuration
int EOC_main::
write_config() {return -ENIMPL;}
#endif // KDB_CONFIG
int EOC_main::add_slave(char *name, char *cprof, int app_cfg) {
PDEBUG(DERR, "Add slave %s", name);
do {
channel_elem *el = (channel_elem *) channels.find(name, strlen(name));
if (el) { // If this device exist in current configuration
if (el->eng->get_type() != slave) {
PDEBUG(DERR, "Reinit device %s", name);
PDEBUG(DFULL,"%s pointer = %p",el->name,el);
channel_elem *d = (channel_elem*) channels.del(name, strlen(
name));
delete d;
break;
}
// Check if some of optins changed
EOC_config *cfg = ((EOC_engine_act*) el->eng)->config();
// Configuration profile name
cfg->cprof(cprof);
if (el->eng->check_compat()) {
char *p = strdup("default");
cfg->cprof(p);
syslog(
LOG_ERR,
"eocd(add_master): Device \"%s\": not compatible with profile \"%s\", set profile=\"default\"",
el->eng->dev1_name(), cprof);
PDEBUG(
DERR,
"eocd(add_master): Device \"%s\": not compatible with profile \"%s\", set profile=\"default\"",
el->eng->dev1_name(), cprof);
}
el->is_updated = 1;
return 0;
}
} while (0);
PDEBUG(DFULL,"Initialize device");
EOC_dev_terminal *dev = init_dev(name);
if (!dev){
PDEBUG(DERR,"dev = %p",dev);
return -1;
}
PDEBUG(DFULL,"Initialize config");
EOC_config *cfg = new EOC_config(&conf_profs, cprof, app_cfg);
PDEBUG(DERR, "CONFIG=%p,cprof=%s", cfg, cfg->cprof());
channel_elem *el = new channel_elem(dev, cfg);
el->name = name;
el->nsize = strlen(name);
el->is_updated = 1;
PDEBUG(DFULL,"%s pointer = %p",el->name,el);
channels.add(el);
channels.sort();
return 0;
}
int EOC_main::add_master(char *name, char *cprof, char *aprof, int reps,
int tick_per_min, int pbomode, char *pboval, int app_cfg) {
PDEBUG(DERR, "start");
{
PDEBUG(DFULL,"Try to opendir");
DIR *dir;
if ( dir = opendir(OS_IF_PATH)) {
PDEBUG(DFULL,"opendir - success; close");
closedir(dir);
}
}
do {
PDEBUG(DERR, "call channels.find");
channel_elem *el = (channel_elem*) channels.find(name, strlen(name));
PDEBUG(DERR, "el=%p", el);
if (el) { // If this device exist in current configuration
// If type of interface is changed
if (el->eng->get_type() != master) {
PDEBUG(DERR, "Reinit device %s", name);
PDEBUG(DFULL,"%s pointer = %p",el->name,el);
channel_elem *d = (channel_elem*) channels.del(name, strlen(
name));
delete d;
break;
}
// Check if some of optins changed
EOC_config *cfg = ((EOC_engine_act*) el->eng)->config();
// Repeaters number
cfg->repeaters(reps);
// Configuration profile name
cfg->cprof(cprof);
if (el->eng->check_compat()) {
char *p = strdup("default");
cfg->cprof(p);
syslog(
LOG_ERR,
"eocd(add_master): Device \"%s\": not compatible with profile \"%s\", set profile=\"default\"",
el->eng->dev1_name(), cprof);
PDEBUG(
DERR,
"eocd(add_master): Device \"%s\": not compatible with profile \"%s\", set profile=\"default\"",
el->eng->dev1_name(), cprof);
}
// Alarm profile name
cfg->aprof(aprof);
// eocd apply capability
cfg->can_apply(app_cfg);
el->is_updated = 1;
return 0;
}
} while (0);
{
PDEBUG(DFULL,"Try to opendir");
DIR *dir;
if ( dir = opendir(OS_IF_PATH)) {
PDEBUG(DFULL,"opendir - success; close");
closedir(dir);
}
}
PDEBUG(DERR, "call init_dev");
EOC_dev_terminal *dev = (EOC_dev_terminal *) init_dev(name);
PDEBUG(DERR, "dev=%p", dev);
if (!dev) {
syslog(LOG_ERR, "eocd(add_master): Cannot initialize device \"%s\"",
name);
PDEBUG(DERR, "eocd(add_master): Cannot initialize device \"%s\"",
name);
return -1;
}
EOC_config *cfg = new EOC_config(&conf_profs, &alarm_profs, cprof, aprof,
reps, app_cfg);
// Check compatibility
conf_profile *prof = (conf_profile*) cfg->conf();
if (dev->comp() < prof->comp) {
syslog(
LOG_ERR,
"eocd(add_master): Device \"%s\": not compatible with profile \"%s\", set profile=\"default\"",
dev->if_name(), cprof);
PDEBUG(
DERR,
"eocd(add_master): Device \"%s\": not compatible with profile \"%s\", set profile=\"default\"",
dev->if_name(), cprof);
char *p = strdup("default");
cfg->cprof(p);
}
PDEBUG(DERR, "CONFIG=%p", cfg);
channel_elem *el = new channel_elem(dev, cfg, tick_per_min);
el->name = name;
el->nsize = strlen(name);
el->is_updated = 1;
PDEBUG(DFULL,"%s pointer = %p",el->name,el);
((EOC_engine_act*) el->eng)->set_pbo(pbomode, pboval);
channels.add(el);
channels.sort();
PDEBUG(DERR, "FINISH");
return 0;
}
// NOTE: this function is only used in non-KDB mode
// at this time KDB mode is preferred so code can be out of date
int EOC_main::add_nexist_slave(int pcislot, int pcidev, char *cprof,
int app_cfg) {
list<dev_settings_t>::iterator it = nexist_devs.begin();
for (; it != nexist_devs.end(); it++) {
if (it->pcislot == pcislot && it->pcidev == pcidev) {
// Devise already exist in list - IGNORE
return -1;
}
}
dev_settings_t tmp;
tmp.pcislot = pcislot;
tmp.pcidev = pcidev;
tmp.cprof = cprof;
tmp.master = 0;
tmp.can_apply = app_cfg;
nexist_devs.push_back(tmp);
return 0;
}
// NOTE: this function is only used in non-KDB mode
// at this time KDB mode is preferred so code can be out of date
int EOC_main::add_nexist_master(int pcislot, int pcidev, char *cprof,
char *aprof, int repeaters, int pbomode, char *pboval, int eocd_apply) {
list<dev_settings_t>::iterator it = nexist_devs.begin();
for (; it != nexist_devs.end(); it++) {
if (it->pcislot == pcislot && it->pcidev == pcidev) {
// Devise already exist in list - IGNORE
return -1;
}
}
dev_settings_t tmp;
tmp.pcislot = pcislot;
tmp.pcidev = pcidev;
tmp.master = 1;
tmp.cprof = cprof;
tmp.aprof = aprof;
tmp.reps = repeaters;
tmp.can_apply = eocd_apply;
tmp.pbomode = pbomode;
strncpy(tmp.pboval, pboval, PBO_SETTING_LEN);
nexist_devs.push_back(tmp);
return 0;
}
int EOC_main::configure_channels() {
channel_elem *el = (channel_elem*) channels.first();
int ret = 0;
err_cfgchans_cnt = 0;
while (el) {
int tmp = 0;
PDEBUG(DERR, "Configure %s channel", el->name);
// Configure interface
if ((tmp = el->eng->configure(el->name)) && err_cfgchans_cnt
< SPAN_NAMES_NUM) {
err_cfgchans[err_cfgchans_cnt++] = el;
}
ret += tmp;
el = (channel_elem*) channels.next(el->name, el->nsize);
}
return ret;
}
int EOC_main::poll_channels() {
channel_elem *el = (channel_elem*) channels.first();
while (el) {
el->eng->schedule(el->name);
el = (channel_elem*) channels.next(el->name, el->nsize);
}
}
// ------------ Application requests ------------------------------//
void EOC_main::app_listen() {
char *buff;
int size, conn;
int ret;
time_t start, cur;
time(&start);
cur = start;
int seconds = 60 / tick_per_min; // Count number of seconds need to wait
while (time(&cur) > 0 && time(&cur) - start < seconds) {
int to_wait = seconds - (time(&cur) - start);
if (!app_srv.wait(to_wait)) {
continue;
}
PDEBUG(DERR, "Get new message:");
while ((size = app_srv.recv(conn, buff))) {
PDEBUG(DERR, "Recv new message:");
// Form & check incoming request
app_frame fr(buff, size);
PDEBUG(DERR, "Process new message:");
// Fill response payload
if (!fr.is_negative()) {
if (ret = app_request(&fr)) {
fr.negative();
PDEBUG(DERR, "Failed to serv app request");
}
}
// Form response
fr.response();
// Send response
ret = app_srv.send(conn, fr.frame_ptr(), fr.frame_size());
}
time(&cur);
}
}
int EOC_main::app_request(app_frame *fr) {
if (fr->role() != app_frame::REQUEST)
return -1;
PDEBUG(DERR, "App request, ID = %d", fr->id());
switch (fr->id()) {
case APP_SPAN_NAME:
return app_spanname(fr);
case APP_SPAN_CONF:
PDEBUG(DERR, "Span Conf case");
return app_spanconf(fr);
case APP_CPROF:
PDEBUG(DINFO, "APP_SPAN_CPROF");
return app_cprof(fr);
case APP_LIST_CPROF:
PDEBUG(DINFO, "APP_SPAN_CPROF_LIST");
return app_list_cprof(fr);
case APP_PBO:
PDEBUG(DINFO, "APP_PBO");
return app_chan_pbo(fr);
#ifndef KDB_CONFIG
// Now this code is old and need revision to work properly
case APP_ADD_CPROF:
PDEBUG(DINFO, "APP_ADD_CPROF");
return app_add_cprof(fr);
case APP_DEL_CPROF:
PDEBUG(DINFO, "APP_DEL_CPROF");
return app_del_cprof(fr);
case APP_ADD_CHAN:
PDEBUG(DINFO, "APP_ADD_CHAN");
return app_add_chan(fr);
case APP_DEL_CHAN:
PDEBUG(DINFO, "APP_DEL_CHAN");
return app_del_chan(fr);
case APP_CHNG_CHAN:
PDEBUG(DINFO, "APP_CHANG_CHAN");
return app_chng_chan(fr);
case APP_DUMP_CFG: {
int ret;
PDEBUG(DERR, "APP_DUMP_CFG");
ret = write_config();
if (ret) {
fr->negative(-ret);
}
return 0;
}
#endif // KDB_CONFIG
}
PDEBUG(DINFO, "Channel request");
if (fr->chan_name())
return app_chann_request(fr);
return -1;
}
int EOC_main::app_spanname(app_frame *fr) {
span_name_payload *p = (span_name_payload*) fr->payload_ptr();
switch (fr->type()) {
case APP_GET:
case APP_GET_NEXT: {
channel_elem *el = (channel_elem*) channels.first();
if (!el) {
fr->negative(ERCHNEXIST);
return 0;
}
if (strnlen(fr->chan_name(), SPAN_NAME_LEN)) {
// if first name isn't zero
el = (channel_elem *) channels.find((char*) fr->chan_name(),
strnlen(fr->chan_name(), SPAN_NAME_LEN));
if (!el) {
fr->negative(ERCHNEXIST);
return 0;
}
el = (channel_elem*) channels.next(el->name, el->nsize);
}
int filled = 0;
while (el && filled < SPAN_NAMES_NUM) {
int cp_len = (el->nsize > SPAN_NAME_LEN) ? SPAN_NAME_LEN
: el->nsize;
strncpy(p->spans[filled].name, el->name, cp_len);
p->spans[filled].t = el->eng->get_type();
p->spans[filled].comp = el->eng->get_compat();
filled++;
el = (channel_elem*) channels.next(el->name, el->nsize);
}
p->filled = filled;
p->last_msg = (el && (filled = SPAN_NAMES_NUM)) ? 0 : 1;
return 0;
}
}
fr->negative(ERTYPE);
return 0;
}
int EOC_main::app_spanconf(app_frame *fr) {
span_conf_payload *p = (span_conf_payload*) fr->payload_ptr();
channel_elem *el;
if (strnlen(fr->chan_name(), SPAN_NAME_LEN)) {
// if first name isn't zero
el = (channel_elem *) channels.find((char*) fr->chan_name(), strnlen(
fr->chan_name(), SPAN_NAME_LEN));