1
/*
2
 * Copyright (C) 2009-2010 Nokia Corporation
3
 *
4
 * Author: Felipe Contreras <felipe.contreras@nokia.com>
5
 *
6
 * This file may be used under the terms of the GNU Lesser General Public
7
 * License version 2.1, a copy of which is found in LICENSE included in the
8
 * packaging of this file.
9
 */
10
11
#include <stdio.h>
12
#include <stdbool.h>
13
#include <string.h>
14
15
#include "dsp_bridge.h"
16
#include "log.h"
17
18
static int dsp_handle;
19
20
static inline const char *
21
node_type_to_str(enum dsp_node_type type)
22
{
23
	switch (type) {
24
	case DSP_NODE_DEVICE:
25
		return "device";
26
	case DSP_NODE_MESSAGE:
27
		return "message";
28
	case DSP_NODE_TASK:
29
		return "task";
30
	case DSP_NODE_DAISSOCKET:
31
		return "dais socket";
32
	default:
33
		return NULL;
34
	}
35
}
36
37
static inline const char *
38
node_status_to_str(enum dsp_node_state state)
39
{
40
	switch (state) {
41
	case NODE_ALLOCATED:
42
		return "allocated";
43
	case NODE_CREATED:
44
		return "created";
45
	case NODE_RUNNING:
46
		return "running";
47
	case NODE_PAUSED:
48
		return "paused";
49
	case NODE_DONE:
50
		return "done";
51
	default:
52
		return NULL;
53
	}
54
}
55
56
struct node_info {
57
	struct dsp_uuid id;
58
	enum dsp_node_type type;
59
	char name[32];
60
	enum dsp_node_state state;
61
};
62
63
static inline bool uuidcmp(struct dsp_uuid *u1, struct dsp_uuid *u2)
64
{
65
	if (memcmp(u1, u2, sizeof(struct dsp_uuid)) == 0)
66
		return true;
67
	return false;
68
}
69
70
static bool do_list(void)
71
{
72
	struct dsp_ndb_props props;
73
	unsigned num = 0, i;
74
	void *proc_handle;
75
	struct node_info *node_table;
76
	void **tmp_table;
77
	unsigned node_count = 0, allocated_count = 0;
78
79
	if (!dsp_enum(dsp_handle, 0, &props, sizeof(props), &num)) {
80
		pr_err("failed to enumerate nodes");
81
		return false;
82
	}
83
84
	if (!dsp_attach(dsp_handle, 0, NULL, &proc_handle)) {
85
		pr_err("dsp attach failed");
86
		return false;
87
	}
88
89
	node_table = calloc(num, sizeof(*node_table));
90
	for (i = 0; i < num; i++) {
91
		if (dsp_enum(dsp_handle, i, &props, sizeof(props), &num)) {
92
			memcpy(&node_table[i].id, &props.node_id, sizeof(props.node_id));
93
			memcpy(&node_table[i].name, props.ac_name, sizeof(props.ac_name));
94
			node_table[i].type = props.ntype;
95
			node_table[i].state = -1;
96
		}
97
	}
98
99
	tmp_table = calloc(num, sizeof(*tmp_table));
100
	if (dsp_enum_nodes(dsp_handle, proc_handle, tmp_table, num,
101
				&node_count, &allocated_count))
102
	{
103
		for (i = 0; i < node_count; i++) {
104
			struct dsp_node_attr attr;
105
			struct dsp_node node = { .handle = tmp_table[i] };
106
			if (dsp_node_get_attr(dsp_handle, &node, &attr, sizeof(attr))) {
107
				unsigned j;
108
				for (j = 0; j < num; j++) {
109
					if (uuidcmp(&node_table[j].id, &attr.info.props.node_id)) {
110
						node_table[j].state = attr.info.state;
111
						break;
112
					}
113
				}
114
			}
115
		}
116
	} else {
117
		pr_err("failed to list active nodes");
118
	}
119
120
	for (i = 0; i < num; i++) {
121
		const char *state = node_status_to_str(node_table[i].state);
122
		if (state)
123
			printf("%s: %s (%s)\n",
124
					node_type_to_str(node_table[i].type),
125
					node_table[i].name,
126
					state);
127
		else
128
			printf("%s: %s\n",
129
					node_type_to_str(node_table[i].type),
130
					node_table[i].name);
131
	}
132
133
	if (!dsp_detach(dsp_handle, proc_handle))
134
		pr_err("dsp detach failed");
135
136
	free(tmp_table);
137
	free(node_table);
138
139
	return true;
140
}
141
142
int main(int argc, char *argv[])
143
{
144
	bool ok;
145
146
	dsp_handle = dsp_open();
147
	if (dsp_handle < 0) {
148
		pr_err("failed to open DSP");
149
		return -1;
150
	}
151
152
	ok = do_list();
153
154
	return ok ? 0 : -1;
155
}