Make res_snmp fit general coding style (issue #7192 reported by Mithraen)
[asterisk/asterisk.git] / res / res_snmp.c
1 /*
2  * Copyright (C) 2006 Voop as
3  * Thorsten Lockert <tholo@voop.as>
4  *
5  * This program is free software, distributed under the terms of
6  * the GNU General Public License Version 2. See the LICENSE file
7  * at the top of the source tree.
8  */
9
10 /*! \file
11  *
12  * \brief SNMP Agent / SubAgent support for Asterisk
13  *
14  * \author Thorsten Lockert <tholo@voop.as>
15  */
16
17 /*** MODULEINFO
18         <depend>netsnmp</depend>
19  ***/
20
21 #include "asterisk.h"
22
23 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
24
25 #include "asterisk/channel.h"
26 #include "asterisk/module.h"
27 #include "asterisk/logger.h"
28 #include "asterisk/options.h"
29
30 #include "snmp/agent.h"
31
32 #define MODULE_DESCRIPTION      "SNMP [Sub]Agent for Asterisk"
33
34 int res_snmp_agentx_subagent;
35 int res_snmp_dont_stop;
36 int res_snmp_enabled;
37
38 static pthread_t thread = AST_PTHREADT_NULL;
39
40 static int load_config(void)
41 {
42         struct ast_variable *var;
43         struct ast_config *cfg;
44         char *cat;
45
46         res_snmp_enabled = 0;
47         res_snmp_agentx_subagent = 1;
48         cfg = ast_config_load("res_snmp.conf");
49         if (cfg) {
50                 cat = ast_category_browse(cfg, NULL);
51                 while (cat) {
52                         var = ast_variable_browse(cfg, cat);
53
54                         if (strcasecmp(cat, "general") == 0) {
55                                 while (var) {
56                                         if (strcasecmp(var->name, "subagent") == 0) {
57                                                 if (ast_true(var->value))
58                                                         res_snmp_agentx_subagent = 1;
59                                                 else if (ast_false(var->value))
60                                                         res_snmp_agentx_subagent = 0;
61                                                 else {
62                                                         ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
63                                                         ast_config_destroy(cfg);
64                                                         return 1;
65                                                 }
66                                         } else if (strcasecmp(var->name, "enabled") == 0) {
67                                                 res_snmp_enabled = ast_true(var->value);
68                                         } else {
69                                                 ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
70                                                 ast_config_destroy(cfg);
71                                                 return 1;
72                                         }
73                                         var = var->next;
74                                 }
75                         } else {
76                                 ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
77                                 ast_config_destroy(cfg);
78                                 return 1;
79                         }
80
81                         cat = ast_category_browse(cfg, cat);
82                 }
83                 ast_config_destroy(cfg);
84         }
85
86         return 0;
87 }
88
89 static int load_module(void *mod)
90 {
91         load_config();
92
93         ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
94
95         res_snmp_dont_stop = 1;
96         if (res_snmp_enabled)
97                 return ast_pthread_create(&thread, NULL, agent_thread, NULL);
98         else
99                 return 0;
100 }
101
102 static int unload_module(void *mod)
103 {
104         ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
105
106         res_snmp_dont_stop = 0;
107         return pthread_join(thread, NULL);
108 }
109
110 static int reload(void *mod)
111 {
112         ast_verbose(VERBOSE_PREFIX_1 "Reloading [Sub]Agent Module\n");
113
114         res_snmp_dont_stop = 0;
115         if (thread != AST_PTHREADT_NULL)
116                 pthread_join(thread, NULL);
117         thread = AST_PTHREADT_NULL;
118         load_config();
119
120         res_snmp_dont_stop = 1;
121         if (res_snmp_enabled)
122                 return ast_pthread_create(&thread, NULL, agent_thread, NULL);
123         else
124                 return 0;
125 }
126
127 static const char *key(void)
128 {
129         return ASTERISK_GPL_KEY;
130 }
131
132 static const char *description(void)
133 {
134         return MODULE_DESCRIPTION;
135 }
136
137 STD_MOD(MOD_0, reload, NULL, NULL);