summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/console.c
blob: 17364c48706c1f0fd59f4207b881a0d8326f3caf (plain)
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
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <kernel.h>
#include <shell/shell.h>
#include <string.h>
#include <sys/printk.h>
#include <zephyr.h>

#include "console.h"

int cputs(enum console_channel channel, const char *str)
{
	return cprintf(channel, "%s\n", str);
}

static const struct shell *current_shell;

static void console_vprintf(enum console_channel channel, const char *format,
			    va_list args)
{
	if (current_shell && channel == CC_COMMAND) {
		shell_vfprintf(current_shell, SHELL_NORMAL, format, args);
		return;
	}

	/*
	 * TODO(jrosenth): investigate using the logging subsystem
	 * and generating modules for the channels instead of printk
	 */
	vprintk(format, args);
}

__attribute__((__format__(__printf__, 2, 3))) int
cprintf(enum console_channel channel, const char *format, ...)
{
	va_list args;

	va_start(args, format);
	console_vprintf(channel, format, args);
	va_end(args);
	return 0;
}

__attribute__((__format__(__printf__, 2, 3))) int
cprints(enum console_channel channel, const char *format, ...)
{
	va_list args;

	cprintf(channel, "[%lld ", k_uptime_get());
	va_start(args, format);
	console_vprintf(channel, format, args);
	va_end(args);
	cprintf(channel, "]\n");
	return 0;
}

void cflush(void)
{
	/*
	 * Do nothing.  Output is sent immediately without buffering
	 * from a printk() in Zephyr.
	 */
}

int zshim_run_ec_console_command(int (*handler)(int argc, char **argv),
				 const struct shell *shell, size_t argc,
				 char **argv, const char *help_str,
				 const char *argdesc)
{
	for (int i = 1; i < argc; i++) {
		if (!help_str && !argdesc)
			break;
		if (!strcmp(argv[i], "-h")) {
			if (help_str)
				shell_fprintf(shell, SHELL_NORMAL, "%s\n",
					      help_str);
			if (argdesc)
				shell_fprintf(shell, SHELL_NORMAL,
					      "Usage: %s\n", argdesc);
			return 0;
		}
	}

	current_shell = shell;
	return handler(argc, argv);
}