File: debug_cmd.c

package info (click to toggle)
baresip 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,328 kB
  • sloc: ansic: 53,606; cpp: 2,268; makefile: 332; objc: 320; python: 259; sh: 40; xml: 19
file content (411 lines) | stat: -rw-r--r-- 9,105 bytes parent folder | download
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
 * @file debug_cmd.c  Debug commands
 *
 * Copyright (C) 2010 - 2016 Alfred E. Heggestad
 */
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef USE_OPENSSL
#include <openssl/crypto.h>
#endif
#include <re.h>
#include <baresip.h>


/**
 * @defgroup debug_cmd debug_cmd
 *
 * Advanced debug commands
 */


static uint64_t start_ticks;          /**< Ticks when app started         */
static time_t start_time;             /**< Start time of application      */
static struct play *g_play;


static int cmd_net_debug(struct re_printf *pf, void *unused)
{
	(void)unused;
	return net_debug(pf, baresip_network());
}


static int print_system_info(struct re_printf *pf, void *arg)
{
	uint32_t uptime;
	int err = 0;

	(void)arg;

	uptime = (uint32_t)((long long)(tmr_jiffies() - start_ticks)/1000);

	err |= re_hprintf(pf, "\n--- System info: ---\n");

	err |= re_hprintf(pf, " Machine:  %s/%s\n", sys_arch_get(),
			  sys_os_get());
	err |= re_hprintf(pf, " Version:  %s (libre v%s)\n",
			  BARESIP_VERSION, sys_libre_version_get());
	err |= re_hprintf(pf, " Build:    %H\n", sys_build_get, NULL);
	err |= re_hprintf(pf, " Kernel:   %H\n", sys_kernel_get, NULL);
	err |= re_hprintf(pf, " Uptime:   %H\n", fmt_human_time, &uptime);
	err |= re_hprintf(pf, " Started:  %s", ctime(&start_time));

#ifdef __VERSION__
	err |= re_hprintf(pf, " Compiler: %s\n", __VERSION__);
#endif

#if defined (USE_OPENSSL) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
	err |= re_hprintf(pf, " OpenSSL:  %s\n",
			  SSLeay_version(SSLEAY_VERSION));
#endif

#if defined (USE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
	err |= re_hprintf(pf, " OpenSSL:  %s\n",
			  OpenSSL_version(OPENSSL_VERSION));
#endif

	return err;
}


static int cmd_config_print(struct re_printf *pf, void *unused)
{
	(void)unused;
	return config_print(pf, conf_config());
}


static int cmd_ua_debug(struct re_printf *pf, void *unused)
{
	struct le *le;
	int err;
	(void)unused;

	if (list_isempty(uag_list()))
		return re_hprintf(pf, "(no user-agent)\n");

	for (le = list_head(uag_list()); le; le = le->next) {
		struct ua *ua = le->data;

		err = ua_debug(pf, ua);
		if (err)
			return err;
	}

	return 0;
}


/**
 * Returns all the User-Agents and their general codec state.
 * Formatted as JSON, for use with TCP / MQTT API interface.
 * JSON object with 'cuser' as the key.
 *
 * @return All User-Agents available, NULL if none
 */
static int cmd_api_uastate(struct re_printf *pf, void *unused)
{
	struct odict *od = NULL;
	struct le *le;
	int err;
	(void)unused;

	err = odict_alloc(&od, 8);
	if (err)
		return err;

	for (le = list_head(uag_list()); le && !err; le = le->next) {
		const struct ua *ua = le->data;
		struct odict *odua;

		err = odict_alloc(&odua, 8);

		err |= ua_state_json_api(odua, ua);
		err |= odict_entry_add(od, account_aor(ua_account(ua)),
				       ODICT_OBJECT, odua);
		mem_deref(odua);
	}

	err |= json_encode_odict(pf, od);
	if (err)
		warning("debug: failed to encode json (%m)\n", err);

	mem_deref(od);

	return re_hprintf(pf, "\n");
}


static int cmd_play_file(struct re_printf *pf, void *arg)
{
	struct cmd_arg *carg = arg;
	struct config *cfg;
	const char *filename = carg->prm;
	int err = 0;

	cfg = conf_config();

	/* Stop the current tone, if any */
	g_play = mem_deref(g_play);

	if (str_isset(filename))
	{
		err = re_hprintf(pf, "playing audio file \"%s\" ..\n",
				 filename);
		if (err)
			return err;

		err = play_file(&g_play, baresip_player(), filename, 0,
                        cfg->audio.alert_mod, cfg->audio.alert_dev);
		if (err)
		{
			warning("debug_cmd: play_file(%s) failed (%m)\n",
					filename, err);
			return err;
		}
	}

	return err;
}


struct fileinfo_st {
	struct ausrc_st *ausrc;
	struct ausrc_prm prm;
	size_t sampc;
	struct tmr tmr;
	bool   finished;
};


static void fileinfo_destruct(void *arg)
{
	struct fileinfo_st *st = arg;

	tmr_cancel(&st->tmr);
	mem_deref(st->ausrc);
}


static void fileinfo_timeout(void *arg)
{
	struct fileinfo_st *st = arg;
	double s  = 0.;

	if (st->prm.ch && st->prm.srate)
		s = ((double) st->sampc)  / st->prm.ch / st->prm.srate;

	if (st->finished) {
		info("debug_cmd: length = %1.3lf seconds\n", s);
		ua_event(NULL, UA_EVENT_CUSTOM, NULL,
			 "debug_cmd: length = %lf seconds", s);
	}
	else if (s > 0.) {
		warning("debug_cmd: timeout, length > %1.3lf seconds\n", s);
		ua_event(NULL, UA_EVENT_CUSTOM, NULL,
			 "debug_cmd: timeout, length > %1.3lf seconds", s);
	}
	else {
		info("debug_cmd: timeout\n");
		ua_event(NULL, UA_EVENT_CUSTOM, NULL,
			 "debug_cmd: timeout");
	}

	mem_deref(st);
}


static void fileinfo_read_handler(struct auframe *af, void *arg)
{
	struct fileinfo_st *st = arg;

	if (!af || !arg)
		return;

	st->sampc += af->sampc;
}


static void fileinfo_err_handler(int err, const char *str, void *arg)
{
	struct fileinfo_st *st = arg;
	(void) str;

	st->finished = err ? false : true;
	tmr_start(&st->tmr, 0, fileinfo_timeout, st);
}


/**
 * Command aufileinfo reads given audio file with ausrc that is specified in
 * config file_ausrc, computes the length in milli seconds and sends a ua_event
 * to inform about the result. The file has to be located in the path specified
 * by audio_path.
 *
 * Usage:
 * /aufileinfo audiofile
 *
 * @param pf Print handler is used to return length.
 * @param arg Command argument contains the file name.
 *
 * @return 0 if success, otherwise errorcode
 */
/* ------------------------------------------------------------------------- */
static int cmd_aufileinfo(struct re_printf *pf, void *arg)
{
	const struct cmd_arg *carg = arg;
	int err = 0;
	char *path;
	char aumod[16];
	struct fileinfo_st *st = NULL;

	if (!str_isset(carg->prm)) {
		re_hprintf(pf, "fileplay: filename not specified\n");
		return EINVAL;
	}

	err = conf_get_str(conf_cur(), "file_ausrc", aumod, sizeof(aumod));
	if (err) {
		warning("debug_cmd: file_ausrc is not set\n");
		return EINVAL;
	}

	re_sdprintf(&path, "%s/%s",
			conf_config()->audio.audio_path,
			carg->prm);

	/* prm->ptime == 0 means blocking mode for ausrc */
	st = mem_zalloc(sizeof(*st), fileinfo_destruct);
	if (!st) {
		err = ENOMEM;
		goto out;
	}

	err = ausrc_alloc(&st->ausrc, baresip_ausrcl(),
			NULL, aumod,
			&st->prm, path,
			fileinfo_read_handler, fileinfo_err_handler, st);
	if (err) {
		warning("debug_cmd: %s - ausrc %s does not support zero ptime "
				"or reading source %s failed. (%m)\n",
				__func__, aumod, carg->prm, err);
		goto out;
	}

	tmr_start(&st->tmr, 5000, fileinfo_timeout, st);
out:
	if (err)
		mem_deref(st);

	mem_deref(path);
	return err;
}


static int cmd_sip_debug(struct re_printf *pf, void *unused)
{
	(void)unused;
	return sip_debug(pf, uag_sip());
}


static int reload_config(struct re_printf *pf, void *arg)
{
	int err;
	(void)arg;

	err = re_hprintf(pf, "reloading config file ..\n");
	if (err)
		return err;

	err = conf_configure();
	if (err) {
		(void)re_hprintf(pf, "reload_config failed: %m\n", err);
		return err;
	}

	(void)re_hprintf(pf, "done\n");

	return 0;
}


static int cmd_log_level(struct re_printf *pf, void *unused)
{
	int level;
	(void)unused;

	level = log_level_get();

	--level;

	if (level < LEVEL_DEBUG)
		level = LEVEL_ERROR;

	log_level_set(level);

	return re_hprintf(pf, "Log level '%s'\n", log_level_name(level));
}


static int print_uuid(struct re_printf *pf, void *arg)
{
	struct config *cfg = conf_config();
	(void)arg;

	if (cfg)
		re_hprintf(pf, "UUID: %s\n", cfg->sip.uuid);
	return 0;
}


static const struct cmd debugcmdv[] = {
{"conf_reload", 0,       0, "Reload config file",     reload_config       },
{"config",      0,       0, "Print configuration",    cmd_config_print    },
{"loglevel",   'v',      0, "Log level toggle",       cmd_log_level       },
{"main",        0,       0, "Main loop debug",        re_debug            },
{"memstat",    'y',      0, "Memory status",          mem_status          },
{"modules",     0,       0, "Module debug",           mod_debug           },
{"netstat",    'n',      0, "Network debug",          cmd_net_debug       },
{"play",        0, CMD_PRM, "Play audio file",        cmd_play_file       },
{"aufileinfo",  0, CMD_PRM, "Audio file info",        cmd_aufileinfo      },
{"sipstat",    'i',      0, "SIP debug",              cmd_sip_debug       },
{"sysinfo",    's',      0, "System info",            print_system_info   },
{"timers",      0,       0, "Timer debug",            tmr_status          },
{"uastat",     'u',      0, "UA debug",               cmd_ua_debug        },
{"uuid",        0,       0, "Print UUID",             print_uuid          },
{"apistate",    0,       0, "User Agent state",       cmd_api_uastate     },
};


static int module_init(void)
{
	int err;

	start_ticks = tmr_jiffies();
	(void)time(&start_time);

	err = cmd_register(baresip_commands(),
			   debugcmdv, ARRAY_SIZE(debugcmdv));

	return err;
}


static int module_close(void)
{
	cmd_unregister(baresip_commands(), debugcmdv);

	g_play = mem_deref(g_play);
	return 0;
}


const struct mod_export DECL_EXPORTS(debug_cmd) = {
	"debug_cmd",
	"application",
	module_init,
	module_close
};