eeb38b4181040225bda64c2d82e37d17df4d252f
[openwrt.git] / package / tapi_sip / src / contact.c
1 #include <malloc.h>
2 #include <string.h>
3
4 #include <uci.h>
5 #include <ucimap.h>
6
7 #include "list.h"
8 #include "contact.h"
9
10 static struct uci_context *ctx;
11 static struct uci_package *pkg;
12 static struct list_head contact_list;
13 static struct list_head account_list;
14
15 static int contact_init(struct uci_map *map, void *section,
16         struct uci_section *s)
17 {
18     struct contact *p = section;
19         p->name = strdup(s->e.name);
20         return 0;
21 }
22
23 static int contact_add(struct uci_map *map, void *section)
24 {
25     struct contact *c = section;
26         printf("add contact: %s\n", c->name);
27         list_add_tail(&c->head, &contact_list);
28         return 0;
29 }
30
31 static struct uci_optmap contact_uci_map[] = {
32         {
33                 UCIMAP_OPTION(struct contact, identifier),
34                 .type = UCIMAP_STRING,
35                 .name = "identifier",
36         },
37         {
38                 UCIMAP_OPTION(struct contact, number),
39                 .type = UCIMAP_STRING,
40                 .name = "number",
41         },
42 };
43
44 static struct uci_sectionmap contact_sectionmap = {
45         UCIMAP_SECTION(struct contact, map),
46         .type = "contact",
47         .init = contact_init,
48         .add = contact_add,
49         .options = contact_uci_map,
50         .n_options = ARRAY_SIZE(contact_uci_map),
51         .options_size = sizeof(struct uci_optmap),
52 };
53
54 static int account_init(struct uci_map *map, void *section,
55         struct uci_section *s)
56 {
57     struct account *a = section;
58         a->name = strdup(s->e.name);
59         return 0;
60 }
61
62 static int account_add(struct uci_map *map, void *section)
63 {
64     struct account *a = section;
65         list_add_tail(&a->head, &account_list);
66         return 0;
67 }
68
69 static struct uci_optmap account_uci_map[] = {
70         {
71                 UCIMAP_OPTION(struct account, realm),
72                 .type = UCIMAP_STRING,
73                 .name = "realm",
74         },
75         {
76                 UCIMAP_OPTION(struct account, username),
77                 .type = UCIMAP_STRING,
78                 .name = "username",
79         },
80         {
81                 UCIMAP_OPTION(struct account, sip_port),
82                 .type = UCIMAP_INT,
83                 .name = "sip_port",
84         },
85         {
86                 UCIMAP_OPTION(struct account, password),
87                 .type = UCIMAP_STRING,
88                 .name = "password",
89         },
90         {
91                 UCIMAP_OPTION(struct account, stun_host),
92                 .type = UCIMAP_STRING,
93                 .name = "stun_host",
94         },
95         {
96                 UCIMAP_OPTION(struct account, stun_port),
97                 .type = UCIMAP_INT,
98                 .name = "stun_port",
99         },
100 };
101
102 static struct uci_sectionmap account_sectionmap = {
103         UCIMAP_SECTION(struct account, map),
104         .type = "account",
105         .init = account_init,
106         .add = account_add,
107         .options = account_uci_map,
108         .n_options = ARRAY_SIZE(account_uci_map),
109         .options_size = sizeof(struct uci_optmap),
110 };
111
112 static struct uci_sectionmap *network_smap[] = {
113     &contact_sectionmap,
114         &account_sectionmap,
115 };
116
117 static struct uci_map contact_map = {
118         .sections = network_smap,
119         .n_sections = ARRAY_SIZE(network_smap),
120 };
121
122 int contacts_init(void)
123 {
124         int ret;
125
126         INIT_LIST_HEAD(&contact_list);
127         INIT_LIST_HEAD(&account_list);
128         ctx = uci_alloc_context();
129
130         ucimap_init(&contact_map);
131         ret = uci_load(ctx, "telephony", &pkg);
132         if (ret)
133                 return ret;
134
135         ucimap_parse(&contact_map, pkg);
136
137         return 0;
138 }
139
140 void contacts_free(void)
141 {
142 }
143
144 struct contact *contact_get(const char *number)
145 {
146         struct contact *contact;
147         list_for_each_entry(contact, &contact_list, head)
148         {
149                 if (strcmp(contact->number, number) == 0)
150                         return contact;
151         }
152
153         return NULL;
154 }
155
156 struct account *get_account(void)
157 {
158         if (list_empty(&account_list))
159                 return NULL;
160
161         return list_first_entry(&account_list, struct account, head);
162 }