The large GULP->PJSIP renaming effort.
[asterisk/asterisk.git] / res / res_pjsip / config_system.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24 #include "asterisk/res_pjsip.h"
25 #include "asterisk/sorcery.h"
26 #include "include/res_pjsip_private.h"
27
28 #define TIMER_T1_MIN 100
29 #define DEFAULT_TIMER_T1 500
30 #define DEFAULT_TIMER_B 32000
31
32 struct system_config {
33         SORCERY_OBJECT(details);
34         /*! Transaction Timer T1 value */
35         unsigned int timert1;
36         /*! Transaction Timer B value */
37         unsigned int timerb;
38         /*! Should we use short forms for headers? */
39         unsigned int compactheaders;
40 };
41
42 static struct ast_sorcery *system_sorcery;
43
44 static void *system_alloc(const char *name)
45 {
46         struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
47
48         if (!system) {
49                 return NULL;
50         }
51
52         return system;
53 }
54
55 static int system_apply(const struct ast_sorcery *system_sorcery, void *obj)
56 {
57         struct system_config *system = obj;
58         int min_timerb;
59
60         if (system->timert1 < TIMER_T1_MIN) {
61                 ast_log(LOG_WARNING, "Timer T1 setting is too low. Setting to %d\n", TIMER_T1_MIN);
62                 system->timert1 = TIMER_T1_MIN;
63         }
64
65         min_timerb = 64 * system->timert1;
66
67         if (system->timerb < min_timerb) {
68                 ast_log(LOG_WARNING, "Timer B setting is too low. Setting to %d\n", min_timerb);
69                 system->timerb = min_timerb;
70         }
71
72         pjsip_cfg()->tsx.t1 = system->timert1;
73         pjsip_cfg()->tsx.td = system->timerb;
74
75         if (system->compactheaders) {
76                 extern pj_bool_t pjsip_use_compact_form;
77                 pjsip_use_compact_form = PJ_TRUE;
78         }
79
80         return 0;
81 }
82
83 int ast_sip_initialize_system(void)
84 {
85         system_sorcery = ast_sorcery_open();
86         if (!system_sorcery) {
87                 ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
88                 return -1;
89         }
90
91         ast_sorcery_apply_config(system_sorcery, "res_pjsip");
92
93         ast_sorcery_apply_default(system_sorcery, "system", "config", "pjsip.conf,criteria=type=system");
94
95         if (ast_sorcery_object_register(system_sorcery, "system", system_alloc, NULL, system_apply)) {
96                 ast_sorcery_unref(system_sorcery);
97                 system_sorcery = NULL;
98                 return -1;
99         }
100
101         ast_sorcery_object_field_register(system_sorcery, "system", "type", "", OPT_NOOP_T, 0, 0);
102         ast_sorcery_object_field_register(system_sorcery, "system", "timert1", __stringify(DEFAULT_TIMER_T1),
103                         OPT_UINT_T, 0, FLDSET(struct system_config, timert1));
104         ast_sorcery_object_field_register(system_sorcery, "system", "timerb", __stringify(DEFAULT_TIMER_B),
105                         OPT_UINT_T, 0, FLDSET(struct system_config, timerb));
106         ast_sorcery_object_field_register(system_sorcery, "system", "compactheaders", "no",
107                         OPT_BOOL_T, 1, FLDSET(struct system_config, compactheaders));
108
109         ast_sorcery_load(system_sorcery);
110
111         return 0;
112 }