1 /**
2  * @file baresip.c Top-level baresip struct
3  *
4  * Copyright (C) 2010 - 2016 Creytiv.com
5  */
6 #include <re.h>
7 #include <baresip.h>
8 #include "core.h"
9 
10 
11 /*
12  * Top-level struct that holds all other subsystems
13  * (move this instance to main.c later)
14  */
15 static struct baresip {
16 	struct network *net;
17 	struct contacts contacts;
18 	struct commands *commands;
19 	struct player *player;
20 	struct message *message;
21 	struct list mnatl;
22 	struct list mencl;
23 	struct list aucodecl;
24 	struct list ausrcl;
25 	struct list auplayl;
26 	struct list aufiltl;
27 	struct list vidcodecl;
28 	struct list vidsrcl;
29 	struct list vidispl;
30 	struct list vidfiltl;
31 	struct ui_sub uis;
32 } baresip;
33 
34 
cmd_quit(struct re_printf * pf,void * unused)35 static int cmd_quit(struct re_printf *pf, void *unused)
36 {
37 	int err;
38 
39 	(void)unused;
40 
41 	err = re_hprintf(pf, "Quit\n");
42 
43 	ua_stop_all(false);
44 
45 	return err;
46 }
47 
48 
insmod_handler(struct re_printf * pf,void * arg)49 static int insmod_handler(struct re_printf *pf, void *arg)
50 {
51        const struct cmd_arg *carg = arg;
52        int err;
53 
54        err = module_load(carg->prm);
55        if (err) {
56                return re_hprintf(pf, "insmod: ERROR: could not load module"
57                                  " '%s': %m\n", carg->prm, err);
58        }
59 
60        return re_hprintf(pf, "loaded module %s\n", carg->prm);
61 }
62 
63 
rmmod_handler(struct re_printf * pf,void * arg)64 static int rmmod_handler(struct re_printf *pf, void *arg)
65 {
66        const struct cmd_arg *carg = arg;
67        (void)pf;
68 
69        module_unload(carg->prm);
70 
71        return 0;
72 }
73 
74 
75 static const struct cmd corecmdv[] = {
76 	{"quit", 'q', 0, "Quit",                     cmd_quit             },
77 	{"insmod", 0, CMD_PRM, "Load module",        insmod_handler       },
78 	{"rmmod",  0, CMD_PRM, "Unload module",      rmmod_handler        },
79 };
80 
81 
baresip_init(struct config * cfg,bool prefer_ipv6)82 int baresip_init(struct config *cfg, bool prefer_ipv6)
83 {
84 	int err;
85 
86 	if (!cfg)
87 		return EINVAL;
88 
89 	baresip.net = mem_deref(baresip.net);
90 
91 	list_init(&baresip.mnatl);
92 	list_init(&baresip.mencl);
93 	list_init(&baresip.aucodecl);
94 	list_init(&baresip.ausrcl);
95 	list_init(&baresip.auplayl);
96 	list_init(&baresip.vidcodecl);
97 	list_init(&baresip.vidsrcl);
98 	list_init(&baresip.vidispl);
99 	list_init(&baresip.vidfiltl);
100 
101 	/* Initialise Network */
102 	err = net_alloc(&baresip.net, &cfg->net,
103 			prefer_ipv6 ? AF_INET6 : AF_INET);
104 	if (err) {
105 		warning("ua: network init failed: %m\n", err);
106 		return err;
107 	}
108 
109 	err = contact_init(&baresip.contacts);
110 	if (err)
111 		return err;
112 
113 	err = cmd_init(&baresip.commands);
114 	if (err)
115 		return err;
116 
117 	err = play_init(&baresip.player);
118 	if (err)
119 		return err;
120 
121 	err = message_init(&baresip.message);
122 	if (err) {
123 		warning("baresip: message init failed: %m\n", err);
124 		return err;
125 	}
126 
127 	err = cmd_register(baresip.commands, corecmdv, ARRAY_SIZE(corecmdv));
128 	if (err)
129 		return err;
130 
131 	return 0;
132 }
133 
134 
baresip_close(void)135 void baresip_close(void)
136 {
137 	cmd_unregister(baresip.commands, corecmdv);
138 
139 	baresip.message = mem_deref(baresip.message);
140 	baresip.player = mem_deref(baresip.player);
141 	baresip.commands = mem_deref(baresip.commands);
142 	contact_close(&baresip.contacts);
143 
144 	baresip.net = mem_deref(baresip.net);
145 
146 	ui_reset(&baresip.uis);
147 }
148 
149 
baresip_network(void)150 struct network *baresip_network(void)
151 {
152 	return baresip.net;
153 }
154 
155 
baresip_contacts(void)156 struct contacts *baresip_contacts(void)
157 {
158 	return &baresip.contacts;
159 }
160 
161 
baresip_commands(void)162 struct commands *baresip_commands(void)
163 {
164 	return baresip.commands;
165 }
166 
167 
baresip_player(void)168 struct player *baresip_player(void)
169 {
170 	return baresip.player;
171 }
172 
173 
baresip_mnatl(void)174 struct list *baresip_mnatl(void)
175 {
176 	return &baresip.mnatl;
177 }
178 
179 
baresip_mencl(void)180 struct list *baresip_mencl(void)
181 {
182 	return &baresip.mencl;
183 }
184 
185 
baresip_message(void)186 struct message *baresip_message(void)
187 {
188 	return baresip.message;
189 }
190 
191 
192 /**
193  * Get the list of Audio Codecs
194  *
195  * @return List of audio-codecs
196  */
baresip_aucodecl(void)197 struct list *baresip_aucodecl(void)
198 {
199 	return &baresip.aucodecl;
200 }
201 
202 
baresip_ausrcl(void)203 struct list *baresip_ausrcl(void)
204 {
205 	return &baresip.ausrcl;
206 }
207 
208 
baresip_auplayl(void)209 struct list *baresip_auplayl(void)
210 {
211 	return &baresip.auplayl;
212 }
213 
214 
baresip_aufiltl(void)215 struct list *baresip_aufiltl(void)
216 {
217 	return &baresip.aufiltl;
218 }
219 
220 
baresip_vidcodecl(void)221 struct list *baresip_vidcodecl(void)
222 {
223 	return &baresip.vidcodecl;
224 }
225 
226 
baresip_vidsrcl(void)227 struct list *baresip_vidsrcl(void)
228 {
229 	return &baresip.vidsrcl;
230 }
231 
232 
baresip_vidispl(void)233 struct list *baresip_vidispl(void)
234 {
235 	return &baresip.vidispl;
236 }
237 
238 
baresip_vidfiltl(void)239 struct list *baresip_vidfiltl(void)
240 {
241 	return &baresip.vidfiltl;
242 }
243 
244 
baresip_uis(void)245 struct ui_sub *baresip_uis(void)
246 {
247 	return &baresip.uis;
248 }
249