Newer
Older
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
/*
* config.c - controller configuration handling
*
* Copyright (C) 2020 IOPSYS Software Solutions AB. All rights reserved.
*
* Author: anjan.chanda@iopsys.eu
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <json-c/json.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/uloop.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <uci.h>
#include "debug.h"
#include "utils.h"
#include "config.h"
//#include "steer_rules.h"
#include "comm.h"
#include "msgqueue.h"
#include "worker.h"
#include "cntlr.h"
//TODO: remove
struct stax{
char macstring[32]; /* ':' separated mac address string */
struct list_head list;
};
////////////
static struct netif_fhcfg *get_netif_fhcfg_by_name(struct cntlr_config *c,
const char *name)
{
struct netif_fhcfg *p;
list_for_each_entry(p, &c->fhlist, list) {
if (!strcmp(name, p->name))
return p;
}
return NULL;
}
static struct steer_policy *get_steer_policy_by_name(struct netif_fhcfg *c,
const char *name)
{
struct steer_policy *p;
if (!c)
return NULL;
list_for_each_entry(p, &c->steer_policylist, list) {
if (!strcmp(name, p->name))
return p;
}
return NULL;
}
void stax_add_entry(struct list_head *h, char *sta_macstr)
{
struct stax *n;
n = calloc(1, sizeof(struct stax));
if (n) {
snprintf(n->macstring, 18, "%s", sta_macstr);
list_add(&n->list, h);
}
}
void stax_del_entry(struct list_head *h, char *sta_macstr)
{
struct stax *s, *tmp;
list_for_each_entry_safe(s, tmp, h, list) {
if (!strncmp(s->macstring, sta_macstr, sizeof(s->macstring))) {
list_del(&s->list);
free(s);
return;
}
}
}
void cntlr_config_dump(struct cntlr_config *cfg)
{
struct netif_fhcfg *n;
struct steer_policy *pol;
struct stax *x;
if (!cfg)
return;
list_for_each_entry(n, &cfg->fhlist, list) {
dbg("name: %s\n", n->name);
dbg(" enabled : %s\n", n->enabled ? "true" : "false");
dbg(" assocctrl: %s\n", n->assoc_control ? "true" : "false");
dbg(" Policies -------\n");
list_for_each_entry(pol, &n->steer_policylist, list) {
dbg(" name: %s\n", pol->name);
dbg(" enabled : %s\n", pol->enabled ? "true" : "false");
/* if (pol->dump_config)
pol->dump_config(pol, pol->policy); */
}
dbg(" Steer Exclude Lists -------\n");
list_for_each_entry(x, &n->steer_excludelist, list) {
dbg(" mac: %s\n", x->macstring);
}
dbg(" Steer BTM Exclude Lists -------\n");
list_for_each_entry(x, &n->steer_btm_excludelist, list) {
dbg(" mac: %s\n", x->macstring);
}
dbg(" Assoc Ctrl Lists -------\n");
list_for_each_entry(x, &n->assoc_ctrllist, list) {
dbg(" mac: %s\n", x->macstring);
}
}
}
int cntlr_config_defaults(struct cntlr *a, struct cntlr_config *cfg)
{
struct list_head *p, *tmp;
struct netif_fh *ifptr;
struct netif_fhcfg *new;
if (!cfg)
return -1;
cfg->enabled = false;
cfg->runfreq = AGENT_RUN_AUTO;
INIT_LIST_HEAD(&cfg->fhlist);
INIT_LIST_HEAD(&cfg->bklist);
list_for_each_entry(ifptr, &a->fhlist, list) {
/* struct list_head pollist; */
struct steer_rule *r;
new = calloc(1, sizeof(struct netif_fhcfg));
if (!new) {
warn("OOM! config\n");
goto err_alloc;
}
snprintf(new->name, 16, "%s", ifptr->name);
new->enabled = false;
new->steer_btm_retry = STEER_BTM_RETRY;
new->steer_btm_retry_secs = STEER_BTM_RETRY_INT;
new->fallback_legacy = STEER_LEGACY_FALLBACK_INT;
new->steer_legacy_reassoc_secs = STEER_LEGACY_REASSOC_INT;
new->steer_legacy_retry_secs = STEER_LEGACY_RETRY_INT;
new->assoc_control_time = ASSOC_CONTROL_INT;
INIT_LIST_HEAD(&new->steer_policylist);
/* nrules = get_registered_steer_rules(&pollist); */ /* TODO */
list_for_each_entry(r, ®d_steer_rules, list) {
struct steer_policy *pol;
pol = calloc(1, sizeof(struct steer_policy));
if (!pol)
goto err_alloc_policy;
snprintf(pol->name, 16, "%s", r->name);
pol->enabled = false;
if (r->init_config)
r->init_config(r, &pol->policy);
list_add(&pol->list, &new->steer_policylist);
}
INIT_LIST_HEAD(&new->steer_excludelist);
INIT_LIST_HEAD(&new->steer_btm_excludelist);
INIT_LIST_HEAD(&new->assoc_ctrllist);
ifptr->cfg = new;
dbg("%s: %s netif_fh->cfg = %p\n", __func__, ifptr->name, ifptr->cfg);
list_add(&new->list, &cfg->fhlist);
}
return 0;
err_alloc_policy:
/* TODO */
err_alloc:
list_for_each_safe(p, tmp, &cfg->fhlist) {
list_del(p);
free(p);
}
return -1;
}
/* create fh-iface config and initialize with default values */
struct netif_fhcfg *create_fronthaul_iface_config(struct cntlr_config *cfg,
const char *ifname)
{
struct netif_fhcfg *new;
struct steer_rule *r;
if (!cfg)
return NULL;
new = calloc(1, sizeof(struct netif_fhcfg));
if (!new) {
warn("OOM! config\n");
return NULL;
}
snprintf(new->name, 16, "%s", ifname);
new->enabled = true;
new->fallback_legacy = STEER_LEGACY_FALLBACK_INT;
new->steer_btm_retry_secs = STEER_BTM_RETRY_INT;
new->steer_legacy_reassoc_secs = STEER_LEGACY_REASSOC_INT;
new->steer_legacy_retry_secs = STEER_LEGACY_RETRY_INT;
new->assoc_control_time = ASSOC_CONTROL_INT;
INIT_LIST_HEAD(&new->steer_policylist);
/* nrules = get_registered_steer_rules(&pollist); */ /* TODO */
list_for_each_entry(r, ®d_steer_rules, list) {
struct steer_policy *pol;
pol = calloc(1, sizeof(struct steer_policy));
if (!pol) {
goto err_oom;
}
snprintf(pol->name, 16, "%s", r->name);
pol->enabled = false;
if (r->init_config)
r->init_config(r, &pol->policy);
list_add(&pol->list, &new->steer_policylist);
}
INIT_LIST_HEAD(&new->steer_excludelist);
INIT_LIST_HEAD(&new->steer_btm_excludelist);
INIT_LIST_HEAD(&new->assoc_ctrllist);
/* f->cfg = new; */
dbg("%s: %s netif_fh->cfg = %p\n", __func__, new->name, new);
list_add(&new->list, &cfg->fhlist);
return new;
err_oom:
list_flush(&new->steer_policylist, struct steer_policy, list);
free(new);
return NULL;
}
int cntlr_config_reload(const char *confname, struct cntlr_config *cfg)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg = NULL;
struct uci_element *e;
ctx = uci_alloc_context();
if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
err("config file '%s' not found!\n", confname);
uci_free_context(ctx);
return -1;
}
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
if (!strcmp(s->type, "wificntlr")) {
struct uci_element *x;
struct uci_option *op;
bool enabled = false;
uci_foreach_element(&s->options, x) {
op = uci_to_option(x);
if (!strncmp(x->name, "enabled", 7) &&
op->type == UCI_TYPE_STRING) {
dbg("wificntlr: enabled = %d\n", atoi(op->v.string));
enabled = atoi(op->v.string) == 1 ?
true : false;
cfg->enabled = enabled;
} else if (!strncmp(x->name, "runfreq", 7) &&
op->type == UCI_TYPE_STRING) {
dbg("wificntlr: speed = %s\n", op->v.string);
if (!strncasecmp(op->v.string, "auto", 4))
cfg->runfreq = AGENT_RUN_AUTO;
else if (!strncasecmp(op->v.string, "low", 3))
cfg->runfreq = AGENT_RUN_LOW;
else if (!strncasecmp(op->v.string, "high", 4))
cfg->runfreq = AGENT_RUN_HIGH;
else if (!strncasecmp(op->v.string, "off", 3))
cfg->runfreq = AGENT_RUN_OFF;
} /* else if (!strncmp(x->name, "ifname", 6) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("wificntlr: ifname: ");
uci_foreach_element(&op->v.list, xi) {
dbg("%s ", xi->name);
}
dbg("\n");
} */
}
} else if (!strcmp(s->type, "fh-iface")) {
struct uci_element *x;
struct uci_option *op;
bool disabled = false;
unsigned int steer_btm_retry = STEER_BTM_RETRY;
unsigned int steer_btm_retry_secs = STEER_BTM_RETRY_INT;
unsigned int legacy = STEER_LEGACY_FALLBACK_INT;
unsigned int steer_legacy_reassoc_secs = STEER_LEGACY_REASSOC_INT;
unsigned int steer_legacy_retry_secs = STEER_LEGACY_RETRY_INT;
unsigned int assoc_ctrl_secs = ASSOC_CONTROL_INT;
struct netif_fhcfg *ifcfg = NULL;
uci_foreach_element(&s->options, x) {
op = uci_to_option(x);
if (!strncmp(x->name, "disabled", 8) &&
op->type == UCI_TYPE_STRING) {
dbg("wificntlr: disabled = %d\n", atoi(op->v.string));
disabled = atoi(op->v.string) == 1 ?
true : false;
} else if (!strncmp(x->name, "ifname", 6) &&
op->type == UCI_TYPE_STRING) {
/* struct uci_element *xi; */
ifcfg = get_netif_fhcfg_by_name(cfg, op->v.string);
if (!ifcfg) {
ifcfg = create_fronthaul_iface_config(cfg, op->v.string);
if (!ifcfg) {
warn("%s: OOM!\n", __func__);
break;
}
} else {
warn("Duplicate 'fh-iface %s' config!! ignore\n",
op->v.string);
}
list_flush(&ifcfg->steer_excludelist, struct stax, list);
list_flush(&ifcfg->steer_btm_excludelist, struct stax, list);
list_flush(&ifcfg->assoc_ctrllist, struct stax, list);
} else if (!strncmp(x->name, "btm_retry", 9) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: btm_retry = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
steer_btm_retry = atoi(op->v.string);
} else if (!strncmp(x->name, "btm_retry_secs", 14) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: btm_retry_secs = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
steer_btm_retry_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "fallback_legacy", 15) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: legacy = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
legacy = atoi(op->v.string);
} else if (!strncmp(x->name, "steer_legacy_reassoc_secs", 25) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: steer_legacy_rassoc_secs = %d\n",
atoi(op->v.string));
steer_legacy_reassoc_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "steer_legacy_retry_secs", 23) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: steer_legacy_retry_secs = %d\n",
atoi(op->v.string));
steer_legacy_retry_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "assoc_ctrl", 10) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: assoc_ctlr = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
assoc_ctrl_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "steer", 5) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("Steer: param: ");
uci_foreach_element(&op->v.list, xi) {
struct steer_policy *p = NULL;
struct steer_rule *r;
dbg("%s ", xi->name);
p = get_steer_policy_by_name(ifcfg, xi->name);
if (!p) {
/* TODO? */
dbg("TODO!! steer before ifname\n");
continue;
}
p->enabled = true;
r = get_steer_rule_by_name(xi->name);
if (r)
r->enabled = true;
}
dbg("\n");
} else if (!strncmp(x->name, "exclude_btm", 11) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("Steer: exclude_btm: ");
uci_foreach_element(&op->v.list, xi) {
dbg("%s ", xi->name);
stax_add_entry(&ifcfg->steer_btm_excludelist, xi->name);
}
dbg("\n");
} else if (!strncmp(x->name, "exclude", 7) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("Steer: exclude: ");
uci_foreach_element(&op->v.list, xi) {
dbg("%s ", xi->name);
stax_add_entry(&ifcfg->steer_excludelist, xi->name);
}
dbg("\n");
}
}
if (ifcfg) {
ifcfg->enabled = !disabled;
ifcfg->steer_btm_retry = steer_btm_retry;
ifcfg->steer_btm_retry_secs = steer_btm_retry_secs;
if (legacy) {
/* wait atleast these many secs before
* falling to legacy steering.
*/
ifcfg->fallback_legacy = 1 +
steer_btm_retry * steer_btm_retry_secs +
legacy;
}
ifcfg->steer_legacy_reassoc_secs = steer_legacy_reassoc_secs;
ifcfg->steer_legacy_retry_secs = steer_legacy_retry_secs;
ifcfg->assoc_control_time = assoc_ctrl_secs;
}
}
#if 0
else if (!strcmp(s->type, "steer")) {
struct uci_element *x, *tmp;
struct uci_option *op;
bool enabled = false;
unsigned int legacy = STEER_LEGACY_FALLBACK_INT;
unsigned int steer_legacy_reassoc_secs = STEER_LEGACY_REASSOC_INT;
unsigned int steer_legacy_retry_secs = STEER_LEGACY_RETRY_INT;
unsigned int steer_btm_retry = STEER_BTM_RETRY;
unsigned int steer_btm_retry_secs = STEER_BTM_RETRY_INT;
struct netif_fhcfg *ifcfg = NULL;
uci_foreach_element_safe(&s->options, tmp, x) {
op = uci_to_option(x);
if (!strncmp(x->name, "enabled", 7) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: enabled = %d\n", atoi(op->v.string));
enabled = atoi(op->v.string) == 1 ?
true : false;
} else if (!strncmp(x->name, "btm_retry", 9) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: btm_retry = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
steer_btm_retry = atoi(op->v.string);
} else if (!strncmp(x->name, "btm_retry_secs", 14) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: btm_retry_secs = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
steer_btm_retry_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "fallback_legacy", 15) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: legacy = %d\n",
atoi(op->v.string));
if (atoi(op->v.string) >= 0)
legacy = atoi(op->v.string);
} else if (!strncmp(x->name, "steer_legacy_reassoc_secs", 25) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: steer_legacy_rassoc_secs = %d\n",
atoi(op->v.string));
steer_legacy_reassoc_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "steer_legacy_retry_secs", 23) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: steer_legacy_retry_secs = %d\n",
atoi(op->v.string));
steer_legacy_retry_secs = atoi(op->v.string);
} else if (!strncmp(x->name, "ifname", 6) &&
op->type == UCI_TYPE_STRING) {
dbg("Steer: ifname = %s\n", op->v.string);
ifcfg = get_netif_fhcfg_by_name(cfg, op->v.string);
if (!ifcfg) {
warn("stale config for '%s'\n", op->v.string);
break;
}
list_flush(&ifcfg->steer_excludelist, struct stax, list);
list_flush(&ifcfg->steer_btm_excludelist, struct stax, list);
list_flush(&ifcfg->assoc_ctrllist, struct stax, list);
} else if (!strncmp(x->name, "param", 5) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("Steer: param: ");
uci_foreach_element(&op->v.list, xi) {
struct steer_policy *p = NULL;
struct steer_rule *r;
dbg("%s ", xi->name);
p = get_steer_policy_by_name(ifcfg, xi->name);
if (!p) {
dbg("TODO ?");
continue;
}
p->enabled = enabled;
r = get_steer_rule_by_name(xi->name);
if (r)
r->enabled = enabled;
}
dbg("\n");
} else if (!strncmp(x->name, "exclude_btm", 11) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("Steer: exclude_btm: ");
uci_foreach_element(&op->v.list, xi) {
dbg("%s ", xi->name);
stax_add_entry(&ifcfg->steer_btm_excludelist, xi->name);
}
dbg("\n");
} else if (!strncmp(x->name, "exclude", 7) &&
op->type == UCI_TYPE_LIST) {
struct uci_element *xi;
dbg("Steer: exclude: ");
uci_foreach_element(&op->v.list, xi) {
dbg("%s ", xi->name);
stax_add_entry(&ifcfg->steer_excludelist, xi->name);
}
dbg("\n");
}
}
if (ifcfg) {
ifcfg->enabled = enabled;
ifcfg->steer_btm_retry = steer_btm_retry;
ifcfg->steer_btm_retry_secs = steer_btm_retry_secs;
if (legacy) {
/* wait atleast these many secs before
* falling to legacy steering.
*/
ifcfg->fallback_legacy = 1 +
steer_btm_retry * steer_btm_retry_secs +
legacy;
}
ifcfg->steer_legacy_reassoc_secs = steer_legacy_reassoc_secs;
ifcfg->steer_legacy_retry_secs = steer_legacy_retry_secs;
}
}
#endif
else if (!strcmp(s->type, "steer-param")) {
/* For every steer-param name, find matching rule
* module. If one is available, then the rule module
* takes care of handling the get/set config for this
* parameter specific optins.
*/
struct steer_rule *r;
dbg("Steer-param: %s\n", s->e.name);
r = get_steer_rule_by_name(s->e.name);
if (r) {
dbg("Rule to handle steer-param '%s' available\n", s->e.name);
r->config(r, cfg, s);
}
} else if (!strncmp(s->type, "wifi-iface", 10)) {
struct uci_element *x;
int mob_id = 500;
uci_foreach_element(&s->options, x) {
struct uci_option *op;
op = uci_to_option(x);
if (!strncmp(x->name, "mobility_domain", 7) &&
op->type == UCI_TYPE_STRING)
mob_id = atoi(op->v.string);
}
dbg("wireless: mobility_domain = %d\n", mob_id);
memcpy(cfg->mobility_domain, &mob_id, 2);
dbg("wireless: mobility_domain hex = %02x:%02x\n",
cfg->mobility_domain[0],
cfg->mobility_domain[1]);
break;
}
}
uci_unload(ctx, pkg);
uci_free_context(ctx);
return 0;
}
static void config_update_entry(struct uci_context *ctx, struct uci_package *p,
struct uci_section *s, const char *optname,
int add, void *val, int len)
{
struct uci_ptr ptr;
memset(&ptr, 0, sizeof(struct uci_ptr));
ptr.p = p;
ptr.s = s;
ptr.package = p->e.name;
ptr.section = s->e.name;
ptr.option = optname;
ptr.target = UCI_TYPE_OPTION;
ptr.flags |= UCI_LOOKUP_EXTENDED;
ptr.value = (char *)val;
if (add) {
dbg("config: add list option: %s\n", (char *)val);
uci_add_list(ctx, &ptr);
} else {
dbg("config: del list option: %s\n", (char *)val);
uci_del_list(ctx, &ptr);
}
uci_commit(ctx, &p, false);
}
int config_update(const char *confname, struct cntlr_config *cfg,
const char *section, const char *option,
int add,
void *value, int len)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg = NULL;
struct uci_element *e;
ctx = uci_alloc_context();
if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
dbg("config file '%s' not found!\n", confname);
free(ctx);
return -1;
}
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
struct uci_element *x, *tmp;
struct uci_option *op;
if (strcmp(s->type, section))
continue;
/* iter through matched 'section' for the 'option' */
uci_foreach_element_safe(&s->options, tmp, x) {
if (strcmp(x->name, option))
continue;
op = uci_to_option(x);
if (op->type == UCI_TYPE_LIST) {
uci_foreach_element(&op->v.list, x) {
if (!strncmp(x->name, value, len)) {
if (!add)
config_update_entry(ctx,
pkg, s, option, 0, value, len);
goto out_exit;
}
}
/* add new exclude at end of list */
if (add)
config_update_entry(ctx, pkg, s, option, 1, value, len);
goto out_exit;
}
}
/* 'option' name not present in 'section'
* Create a new one at end of 'section'.
*/
if (add)
config_update_entry(ctx, pkg, s, option, 1, value, len);
goto out_exit;
}
out_exit:
uci_free_context(ctx);
return 0;
}
int config_update2(const char *confname, struct cntlr_config *cfg,
const char *section_type,
const char *match_option,
const char *match_option_value,
const char *option,
int add,
void *value, int len)
{
struct uci_context *ctx = NULL;
struct uci_package *pkg = NULL;
struct uci_element *e;
ctx = uci_alloc_context();
if (ctx && uci_load(ctx, confname, &pkg) != UCI_OK) {
dbg("config file '%s' not found!\n", confname);
free(ctx);
return -1;
}
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
struct uci_element *x, *tmp;
struct uci_option *op;
const char *optstring;
if (strcmp(s->type, section_type))
continue;
if (match_option && match_option_value) {
optstring = uci_lookup_option_string(ctx, s, match_option);
if (!optstring || strcmp(optstring, match_option_value))
continue;
}
/* iter through matched 'section' for the 'option' */
uci_foreach_element_safe(&s->options, tmp, x) {
if (strcmp(x->name, option))
continue;
op = uci_to_option(x);
if (op->type == UCI_TYPE_LIST) {
uci_foreach_element(&op->v.list, x) {
if (!strncmp(x->name, value, len)) {
if (!add) {
config_update_entry(ctx,
pkg, s, option,
0, value, len);
}
goto out_exit;
}
}
/* add new 'option' at end of list */
if (add) {
config_update_entry(ctx, pkg, s, option,
1, value, len);
}
goto out_exit;
}
}
/* 'option' name not present in 'section'
* Create a new one at end of 'section'.
*/
if (add)
config_update_entry(ctx, pkg, s, option,
1, value, len);
goto out_exit;
}
out_exit:
uci_free_context(ctx);
return 0;
}
#if 0
struct config uci_config = {
.name = "uci",
.priv = uci_ctx;
.get = uci_get_config,
.set = uci_set_config,
.init = uci_setup,
.exit = uci_exit,
};
#define priv_get_config(priv) container_of(priv, struct config, priv)
void register_config(struct config *c)
{
static struct uci_context *ctx;
static struct uci_package *pkg;
struct uci_element *e;
int ret = 0;
if (uci_ctx)
return priv_get_config(uci_ctx);
ctx = uci_alloc_context();
if (ctx) {
uci_ctx = ctx;
memcpy(c, &uci_config, sizeof(*cfg));
}
if (uci_load(ctx, "wificntlr", &pkg))
return -1;
uci_foreach_element(&pkg->sections, e) {
struct uci_section *s = uci_to_section(e);
const char *option_val;
if (strcmp(s->type, "wificntlr"))
continue;
option_val = uci_lookup_option_string(ctx, s, name);
if (option_val)
sprintf(val, "%s", option_val);
else
ret = -1;
}
uci_free_context(ctx);
return ret;
}
#endif