summaryrefslogtreecommitdiffstats
path: root/package/tapi_sip/src/contact.c
blob: eeb38b4181040225bda64c2d82e37d17df4d252f (plain)
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
#include <malloc.h>
#include <string.h>

#include <uci.h>
#include <ucimap.h>

#include "list.h"
#include "contact.h"

static struct uci_context *ctx;
static struct uci_package *pkg;
static struct list_head contact_list;
static struct list_head account_list;

static int contact_init(struct uci_map *map, void *section,
	struct uci_section *s)
{
    struct contact *p = section;
	p->name = strdup(s->e.name);
	return 0;
}

static int contact_add(struct uci_map *map, void *section)
{
    struct contact *c = section;
	printf("add contact: %s\n", c->name);
	list_add_tail(&c->head, &contact_list);
	return 0;
}

static struct uci_optmap contact_uci_map[] = {
	{
		UCIMAP_OPTION(struct contact, identifier),
		.type = UCIMAP_STRING,
		.name = "identifier",
	},
	{
		UCIMAP_OPTION(struct contact, number),
		.type = UCIMAP_STRING,
		.name = "number",
	},
};

static struct uci_sectionmap contact_sectionmap = {
	UCIMAP_SECTION(struct contact, map),
	.type = "contact",
	.init = contact_init,
	.add = contact_add,
	.options = contact_uci_map,
	.n_options = ARRAY_SIZE(contact_uci_map),
	.options_size = sizeof(struct uci_optmap),
};

static int account_init(struct uci_map *map, void *section,
	struct uci_section *s)
{
    struct account *a = section;
	a->name = strdup(s->e.name);
	return 0;
}

static int account_add(struct uci_map *map, void *section)
{
    struct account *a = section;
	list_add_tail(&a->head, &account_list);
	return 0;
}

static struct uci_optmap account_uci_map[] = {
	{
		UCIMAP_OPTION(struct account, realm),
		.type = UCIMAP_STRING,
		.name = "realm",
	},
	{
		UCIMAP_OPTION(struct account, username),
		.type = UCIMAP_STRING,
		.name = "username",
	},
	{
		UCIMAP_OPTION(struct account, sip_port),
		.type = UCIMAP_INT,
		.name = "sip_port",
	},
	{
		UCIMAP_OPTION(struct account, password),
		.type = UCIMAP_STRING,
		.name = "password",
	},
	{
		UCIMAP_OPTION(struct account, stun_host),
		.type = UCIMAP_STRING,
		.name = "stun_host",
	},
	{
		UCIMAP_OPTION(struct account, stun_port),
		.type = UCIMAP_INT,
		.name = "stun_port",
	},
};

static struct uci_sectionmap account_sectionmap = {
	UCIMAP_SECTION(struct account, map),
	.type = "account",
	.init = account_init,
	.add = account_add,
	.options = account_uci_map,
	.n_options = ARRAY_SIZE(account_uci_map),
	.options_size = sizeof(struct uci_optmap),
};

static struct uci_sectionmap *network_smap[] = {
    &contact_sectionmap,
	&account_sectionmap,
};

static struct uci_map contact_map = {
	.sections = network_smap,
	.n_sections = ARRAY_SIZE(network_smap),
};

int contacts_init(void)
{
	int ret;

	INIT_LIST_HEAD(&contact_list);
	INIT_LIST_HEAD(&account_list);
	ctx = uci_alloc_context();

	ucimap_init(&contact_map);
	ret = uci_load(ctx, "telephony", &pkg);
	if (ret)
		return ret;

	ucimap_parse(&contact_map, pkg);

	return 0;
}

void contacts_free(void)
{
}

struct contact *contact_get(const char *number)
{
	struct contact *contact;
	list_for_each_entry(contact, &contact_list, head)
	{
		if (strcmp(contact->number, number) == 0)
			return contact;
	}

	return NULL;
}

struct account *get_account(void)
{
	if (list_empty(&account_list))
		return NULL;

	return list_first_entry(&account_list, struct account, head);
}