2 * SMSD message monitor program
4 /* Copyright (c) 2009 - 2011 Michal Cihar <michal@cihar.com> */
5 /* Licensend under GNU GPL 2 */
7 #include <gammu-smsd.h>
14 #ifdef HAVE_GETOPT_LONG
20 #if !defined(WIN32) && (defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG))
21 #define HAVE_DEFAULT_CONFIG
22 const char default_config
[] = "/etc/gammu-smsdrc";
25 volatile gboolean terminate
= FALSE
;
26 int delay_seconds
= 20;
28 gboolean compact
= FALSE
;
30 void smsd_interrupt(int signum
)
35 NORETURN
void version(void)
37 printf("Gammu-smsd-monitor version %s\n", GAMMU_VERSION
);
38 printf("Built %s on %s using %s\n", __TIME__
, __DATE__
, GetCompiler());
39 printf("Compiled in features:\n");
40 printf("OS support:\n");
42 printf(" - %s\n", "SHM");
45 printf(" - %s\n", "GETOPT");
47 #ifdef HAVE_GETOPT_LONG
48 printf(" - %s\n", "GETOPT_LONG");
50 printf("Backend services:\n");
51 printf(" - %s\n", "NULL");
52 printf(" - %s\n", "FILES");
53 #ifdef HAVE_MYSQL_MYSQL_H
54 printf(" - %s\n", "MYSQL");
56 #ifdef HAVE_POSTGRESQL_LIBPQ_FE_H
57 printf(" - %s\n", "POSTGRESQL");
60 printf(" - %s\n", "DBI");
63 printf(" - %s\n", "ODBC");
66 printf("Copyright (C) 2003 - 2011 Michal Cihar <michal@cihar.com> and other authors.\n");
68 printf("License GPLv2: GNU GPL version 2 <http://creativecommons.org/licenses/GPL/2.0/>.\n");
69 printf("This is free software: you are free to change and redistribute it.\n");
70 printf("There is NO WARRANTY, to the extent permitted by law.\n");
72 printf("Check <http://wammu.eu/gammu/> for updates.\n");
77 #ifdef HAVE_GETOPT_LONG
78 #define print_option(name, longname, help) \
79 printf("-%s / --%s - %s\n", name, longname, help);
80 #define print_option_param(name, longname, paramname, help) \
81 printf("-%s / --%s %s - %s\n", name, longname, paramname, help);
83 #define print_option(name, longname, help) \
84 printf("-%s - %s\n", name, help);
85 #define print_option_param(name, longname, paramname, help) \
86 printf("-%s %s - %s\n", name, paramname, help);
91 printf("usage: gammu-smsd-monitor [OPTION]...\n");
93 print_option("h", "help", "shows this help");
94 print_option("v", "version", "shows version information");
95 print_option("C", "csv", "CSV output");
96 print_option_param("c", "config", "CONFIG_FILE",
97 "defines path to config file");
98 print_option_param("d", "delay", "DELAY",
99 "delay in seconds between loops");
100 print_option_param("n", "loops", "NUMBER",
101 "delay in seconds between loops");
102 print_option("l", "use-log", "use logging configuration from config file");
103 print_option("L", "no-use-log", "do not use logging configuration from config file (default)");
106 NORETURN
void wrong_params(void)
108 fprintf(stderr
, "Invalid parameter, use -h for help.\n");
112 int process_commandline(int argc
, char **argv
, SMSD_Parameters
* params
)
116 #ifdef HAVE_GETOPT_LONG
117 struct option long_options
[] = {
119 {"version", 0, 0, 'v'},
120 {"config", 1, 0, 'c'},
121 {"delay", 1, 0, 'd'},
122 {"loops", 1, 0, 'n'},
123 {"use-log", 0, 0, 'l'},
124 {"no-use-log", 0, 0, 'L'},
130 getopt_long(argc
, argv
, "+hvc:d:n:ClL", long_options
,
131 &option_index
)) != -1) {
132 #elif defined(HAVE_GETOPT)
133 while ((opt
= getopt(argc
, argv
, "+hvc:d:n:ClL")) != -1) {
135 /* Poor mans getopt replacement */
138 #define optarg argv[++i]
140 for (i
= 1; i
< argc
; i
++) {
141 if (strlen(argv
[i
]) != 2 || argv
[i
][0] != '-') {
148 params
->config_file
= optarg
;
157 delay_seconds
= atoi(optarg
);
160 limit_loops
= atoi(optarg
);
163 params
->use_log
= TRUE
;
166 params
->use_log
= FALSE
;
174 #if defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG)
181 #if !defined(HAVE_GETOPT) && !defined(HAVE_GETOPT_LONG)
182 if (optind
!= -1) break;
193 int main(int argc
, char **argv
)
196 GSM_SMSDConfig
*config
;
197 GSM_SMSDStatus status
;
198 const char program_name
[] = "gammu-smsd-monitor";
199 SMSD_Parameters params
= {
218 * We don't need gettext, but need to set locales so that
219 * charset conversion works.
221 GSM_InitLocales(NULL
);
223 process_commandline(argc
, argv
, ¶ms
);
225 if (params
.config_file
== NULL
) {
226 #ifdef HAVE_DEFAULT_CONFIG
227 params
.config_file
= default_config
;
229 fprintf(stderr
, "No config file specified!\n");
235 signal(SIGINT
, smsd_interrupt
);
236 signal(SIGTERM
, smsd_interrupt
);
238 config
= SMSD_NewConfig(program_name
);
239 assert(config
!= NULL
);
241 error
= SMSD_ReadConfig(params
.config_file
, config
, params
.use_log
);
242 if (error
!= ERR_NONE
) {
243 printf("Failed to read config: %s\n", GSM_ErrorString(error
));
244 SMSD_FreeConfig(config
);
248 while (!terminate
&& (limit_loops
== -1 || limit_loops
-- > 0)) {
249 error
= SMSD_GetStatus(config
, &status
);
250 if (error
!= ERR_NONE
) {
251 printf("Failed to get status: %s\n", GSM_ErrorString(error
));
252 SMSD_FreeConfig(config
);
256 printf("%s;%s;%s;%d;%d;%d;%d;%d\n",
263 status
.Charge
.BatteryPercent
,
264 status
.Network
.SignalPercent
);
266 printf("Client: %s\n", status
.Client
);
267 printf("PhoneID: %s\n", status
.PhoneID
);
268 printf("IMEI: %s\n", status
.IMEI
);
269 printf("Sent: %d\n", status
.Sent
);
270 printf("Received: %d\n", status
.Received
);
271 printf("Failed: %d\n", status
.Failed
);
272 printf("BatterPercent: %d\n", status
.Charge
.BatteryPercent
);
273 printf("NetworkSignal: %d\n", status
.Network
.SignalPercent
);
276 sleep(delay_seconds
);
279 SMSD_FreeConfig(config
);
284 /* How should editor hadle tabs in this file? Add editor commands here.
285 * vim: noexpandtab sw=8 ts=8 sts=8: