1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup devctl
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Control device framework (devman server).
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <devman.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <sys/typefmt.h>
|
---|
41 |
|
---|
42 | #define NAME "devctl"
|
---|
43 |
|
---|
44 | #define MAX_NAME_LENGTH 1024
|
---|
45 |
|
---|
46 | static int fun_subtree_print(devman_handle_t funh, int lvl)
|
---|
47 | {
|
---|
48 | char name[MAX_NAME_LENGTH];
|
---|
49 | devman_handle_t devh;
|
---|
50 | devman_handle_t *cfuns;
|
---|
51 | size_t count, i;
|
---|
52 | int rc;
|
---|
53 | int j;
|
---|
54 |
|
---|
55 | for (j = 0; j < lvl; j++)
|
---|
56 | printf(" ");
|
---|
57 |
|
---|
58 | rc = devman_fun_get_name(funh, name, MAX_NAME_LENGTH);
|
---|
59 | if (rc != EOK) {
|
---|
60 | str_cpy(name, MAX_NAME_LENGTH, "unknown");
|
---|
61 | return ENOMEM;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (name[0] == '\0')
|
---|
65 | str_cpy(name, MAX_NAME_LENGTH, "/");
|
---|
66 |
|
---|
67 | printf("%s (%" PRIun ")\n", name, funh);
|
---|
68 |
|
---|
69 | rc = devman_fun_get_child(funh, &devh);
|
---|
70 | if (rc == ENOENT)
|
---|
71 | return EOK;
|
---|
72 |
|
---|
73 | if (rc != EOK) {
|
---|
74 | printf(NAME ": Failed getting child device for function "
|
---|
75 | "%s.\n", "xxx");
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | rc = devman_dev_get_functions(devh, &cfuns, &count);
|
---|
80 | if (rc != EOK) {
|
---|
81 | printf(NAME ": Failed getting list of functions for "
|
---|
82 | "device %s.\n", "xxx");
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 |
|
---|
86 | for (i = 0; i < count; i++)
|
---|
87 | fun_subtree_print(cfuns[i], lvl + 1);
|
---|
88 |
|
---|
89 | free(cfuns);
|
---|
90 | return EOK;
|
---|
91 | }
|
---|
92 |
|
---|
93 | static int fun_tree_print(void)
|
---|
94 | {
|
---|
95 | devman_handle_t root_fun;
|
---|
96 | int rc;
|
---|
97 |
|
---|
98 | rc = devman_fun_get_handle("/", &root_fun, 0);
|
---|
99 | if (rc != EOK) {
|
---|
100 | printf(NAME ": Error resolving root function.\n");
|
---|
101 | return EIO;
|
---|
102 | }
|
---|
103 |
|
---|
104 | rc = fun_subtree_print(root_fun, 0);
|
---|
105 | if (rc != EOK)
|
---|
106 | return EIO;
|
---|
107 |
|
---|
108 | return EOK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static int fun_online(const char *path)
|
---|
112 | {
|
---|
113 | devman_handle_t funh;
|
---|
114 | int rc;
|
---|
115 |
|
---|
116 | rc = devman_fun_get_handle(path, &funh, 0);
|
---|
117 | if (rc != EOK) {
|
---|
118 | printf(NAME ": Error resolving device function '%s' (%s)\n",
|
---|
119 | path, str_error(rc));
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | rc = devman_fun_online(funh);
|
---|
124 | if (rc != EOK) {
|
---|
125 | printf(NAME ": Failed to online function '%s'.\n", path);
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 | return EOK;
|
---|
130 | }
|
---|
131 |
|
---|
132 | static int fun_offline(const char *path)
|
---|
133 | {
|
---|
134 | devman_handle_t funh;
|
---|
135 | int rc;
|
---|
136 |
|
---|
137 | rc = devman_fun_get_handle(path, &funh, 0);
|
---|
138 | if (rc != EOK) {
|
---|
139 | printf(NAME ": Error resolving device function '%s' (%s)\n",
|
---|
140 | path, str_error(rc));
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 | rc = devman_fun_offline(funh);
|
---|
145 | if (rc != EOK) {
|
---|
146 | printf(NAME ": Failed to offline function '%s'.\n", path);
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return EOK;
|
---|
151 | }
|
---|
152 |
|
---|
153 | static void print_syntax(void)
|
---|
154 | {
|
---|
155 | printf("syntax: devctl [(online|offline) <function>]\n");
|
---|
156 | }
|
---|
157 |
|
---|
158 | int main(int argc, char *argv[])
|
---|
159 | {
|
---|
160 | int rc;
|
---|
161 |
|
---|
162 | if (argc == 1) {
|
---|
163 | rc = fun_tree_print();
|
---|
164 | if (rc != EOK)
|
---|
165 | return 2;
|
---|
166 | } else if (str_cmp(argv[1], "online") == 0) {
|
---|
167 | if (argc < 3) {
|
---|
168 | printf(NAME ": Argument missing.\n");
|
---|
169 | print_syntax();
|
---|
170 | return 1;
|
---|
171 | }
|
---|
172 |
|
---|
173 | rc = fun_online(argv[2]);
|
---|
174 | if (rc != EOK) {
|
---|
175 | return 2;
|
---|
176 | }
|
---|
177 | } else if (str_cmp(argv[1], "offline") == 0) {
|
---|
178 | if (argc < 3) {
|
---|
179 | printf(NAME ": Argument missing.\n");
|
---|
180 | print_syntax();
|
---|
181 | return 1;
|
---|
182 | }
|
---|
183 |
|
---|
184 | rc = fun_offline(argv[2]);
|
---|
185 | if (rc != EOK) {
|
---|
186 | return 2;
|
---|
187 | }
|
---|
188 | } else {
|
---|
189 | printf(NAME ": Invalid argument '%s'.\n", argv[1]);
|
---|
190 | print_syntax();
|
---|
191 | return 1;
|
---|
192 | }
|
---|
193 |
|
---|
194 | return 0;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** @}
|
---|
198 | */
|
---|