source: mainline/uspace/app/devctl/devctl.c@ 271e24a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 271e24a was ce1df04, checked in by Jakub Jermar <jakub@…>, 13 years ago

Print also the actual error message when offline fails.

  • Property mode set to 100644
File size: 4.6 KB
Line 
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
46char name[MAX_NAME_LENGTH];
47char drv_name[MAX_NAME_LENGTH];
48
49static int fun_subtree_print(devman_handle_t funh, int lvl)
50{
51 devman_handle_t devh;
52 devman_handle_t *cfuns;
53 size_t count, i;
54 int rc;
55 int j;
56
57 for (j = 0; j < lvl; j++)
58 printf(" ");
59
60 rc = devman_fun_get_name(funh, name, MAX_NAME_LENGTH);
61 if (rc != EOK)
62 return ELIMIT;
63
64 if (name[0] == '\0')
65 str_cpy(name, MAX_NAME_LENGTH, "/");
66
67 rc = devman_fun_get_driver_name(funh, drv_name, MAX_NAME_LENGTH);
68 if (rc != EOK && rc != EINVAL)
69 return ELIMIT;
70
71 if (rc == EINVAL)
72 printf("%s\n", name);
73 else
74 printf("%s : %s\n", name, drv_name);
75
76 rc = devman_fun_get_child(funh, &devh);
77 if (rc == ENOENT)
78 return EOK;
79
80 if (rc != EOK) {
81 printf(NAME ": Failed getting child device for function "
82 "%s.\n", "xxx");
83 return rc;
84 }
85
86 rc = devman_dev_get_functions(devh, &cfuns, &count);
87 if (rc != EOK) {
88 printf(NAME ": Failed getting list of functions for "
89 "device %s.\n", "xxx");
90 return rc;
91 }
92
93 for (i = 0; i < count; i++)
94 fun_subtree_print(cfuns[i], lvl + 1);
95
96 free(cfuns);
97 return EOK;
98}
99
100static int fun_tree_print(void)
101{
102 devman_handle_t root_fun;
103 int rc;
104
105 rc = devman_fun_get_handle("/", &root_fun, 0);
106 if (rc != EOK) {
107 printf(NAME ": Error resolving root function.\n");
108 return EIO;
109 }
110
111 rc = fun_subtree_print(root_fun, 0);
112 if (rc != EOK)
113 return EIO;
114
115 return EOK;
116}
117
118static int fun_online(const char *path)
119{
120 devman_handle_t funh;
121 int rc;
122
123 rc = devman_fun_get_handle(path, &funh, 0);
124 if (rc != EOK) {
125 printf(NAME ": Error resolving device function '%s' (%s)\n",
126 path, str_error(rc));
127 return rc;
128 }
129
130 rc = devman_fun_online(funh);
131 if (rc != EOK) {
132 printf(NAME ": Failed to online function '%s'.\n", path);
133 return rc;
134 }
135
136 return EOK;
137}
138
139static int fun_offline(const char *path)
140{
141 devman_handle_t funh;
142 int rc;
143
144 rc = devman_fun_get_handle(path, &funh, 0);
145 if (rc != EOK) {
146 printf(NAME ": Error resolving device function '%s' (%s)\n",
147 path, str_error(rc));
148 return rc;
149 }
150
151 rc = devman_fun_offline(funh);
152 if (rc != EOK) {
153 printf(NAME ": Failed to offline function '%s' (%s)\n", path,
154 str_error(rc));
155 return rc;
156 }
157
158 return EOK;
159}
160
161static void print_syntax(void)
162{
163 printf("syntax: devctl [(online|offline) <function>]\n");
164}
165
166int main(int argc, char *argv[])
167{
168 int rc;
169
170 if (argc == 1) {
171 rc = fun_tree_print();
172 if (rc != EOK)
173 return 2;
174 } else if (str_cmp(argv[1], "online") == 0) {
175 if (argc < 3) {
176 printf(NAME ": Argument missing.\n");
177 print_syntax();
178 return 1;
179 }
180
181 rc = fun_online(argv[2]);
182 if (rc != EOK) {
183 return 2;
184 }
185 } else if (str_cmp(argv[1], "offline") == 0) {
186 if (argc < 3) {
187 printf(NAME ": Argument missing.\n");
188 print_syntax();
189 return 1;
190 }
191
192 rc = fun_offline(argv[2]);
193 if (rc != EOK) {
194 return 2;
195 }
196 } else {
197 printf(NAME ": Invalid argument '%s'.\n", argv[1]);
198 print_syntax();
199 return 1;
200 }
201
202 return 0;
203}
204
205/** @}
206 */
Note: See TracBrowser for help on using the repository browser.