source: mainline/uspace/drv/test/test3/test3.c@ 3f932a7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3f932a7e was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
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/** @file
30 */
31
32#include <assert.h>
33#include <stdio.h>
34#include <errno.h>
35#include <str_error.h>
36#include <ddf/driver.h>
37#include <ddf/log.h>
38
39#define NAME "test3"
40
41#define NUM_FUNCS 20
42
43static errno_t test3_dev_add(ddf_dev_t *dev);
44static errno_t test3_dev_remove(ddf_dev_t *dev);
45static errno_t test3_fun_online(ddf_fun_t *fun);
46static errno_t test3_fun_offline(ddf_fun_t *fun);
47
48static driver_ops_t driver_ops = {
49 .dev_add = &test3_dev_add,
50 .dev_remove = &test3_dev_remove,
51 .fun_online = &test3_fun_online,
52 .fun_offline = &test3_fun_offline,
53};
54
55static driver_t test3_driver = {
56 .name = NAME,
57 .driver_ops = &driver_ops
58};
59
60typedef struct {
61 ddf_dev_t *dev;
62 ddf_fun_t *fun[NUM_FUNCS];
63} test3_t;
64
65static errno_t register_fun_and_add_to_category(ddf_dev_t *parent,
66 const char *base_name, size_t index, const char *class_name,
67 ddf_fun_t **pfun)
68{
69 ddf_fun_t *fun = NULL;
70 errno_t rc;
71 char *fun_name = NULL;
72
73 if (asprintf(&fun_name, "%s%zu", base_name, index) < 0) {
74 ddf_msg(LVL_ERROR, "Failed to format string: No memory");
75 rc = ENOMEM;
76 goto leave;
77 }
78
79 fun = ddf_fun_create(parent, fun_exposed, fun_name);
80 if (fun == NULL) {
81 ddf_msg(LVL_ERROR, "Failed creating function %s", fun_name);
82 rc = ENOMEM;
83 goto leave;
84 }
85
86 rc = ddf_fun_bind(fun);
87 if (rc != EOK) {
88 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", fun_name,
89 str_error(rc));
90 goto leave;
91 }
92
93 ddf_fun_add_to_category(fun, class_name);
94
95 ddf_msg(LVL_NOTE, "Registered exposed function `%s'.", fun_name);
96
97leave:
98 free(fun_name);
99
100 if ((rc != EOK) && (fun != NULL)) {
101 ddf_fun_destroy(fun);
102 }
103
104 *pfun = fun;
105 return rc;
106}
107
108static errno_t fun_remove(ddf_fun_t *fun, const char *name)
109{
110 errno_t rc;
111
112 ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
113 rc = ddf_fun_offline(fun);
114 if (rc != EOK) {
115 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", name);
116 return rc;
117 }
118
119 rc = ddf_fun_unbind(fun);
120 if (rc != EOK) {
121 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", name);
122 return rc;
123 }
124
125 ddf_fun_destroy(fun);
126 return EOK;
127}
128
129static errno_t test3_dev_add(ddf_dev_t *dev)
130{
131 errno_t rc = EOK;
132 test3_t *test3;
133
134 ddf_msg(LVL_DEBUG, "dev_add(name=\"%s\", handle=%d)",
135 ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
136
137 test3 = ddf_dev_data_alloc(dev, sizeof(test3_t));
138 if (test3 == NULL) {
139 ddf_msg(LVL_ERROR, "Failed allocating soft state.");
140 return ENOMEM;
141 }
142
143 size_t i;
144 for (i = 0; i < NUM_FUNCS; i++) {
145 rc = register_fun_and_add_to_category(dev,
146 "test3_", i, "test3", &test3->fun[i]);
147 if (rc != EOK) {
148 break;
149 }
150 }
151
152 return rc;
153}
154
155static errno_t test3_dev_remove(ddf_dev_t *dev)
156{
157 test3_t *test3 = (test3_t *)ddf_dev_data_get(dev);
158 char *fun_name;
159 errno_t rc;
160 size_t i;
161
162 for (i = 0; i < NUM_FUNCS; i++) {
163 if (asprintf(&fun_name, "test3_%zu", i) < 0) {
164 ddf_msg(LVL_ERROR, "Failed to format string: No memory");
165 return ENOMEM;
166 }
167
168 rc = fun_remove(test3->fun[i], fun_name);
169 if (rc != EOK)
170 return rc;
171
172 free(fun_name);
173 }
174
175 return EOK;
176}
177
178static errno_t test3_fun_online(ddf_fun_t *fun)
179{
180 ddf_msg(LVL_DEBUG, "test3_fun_online()");
181 return ddf_fun_online(fun);
182}
183
184static errno_t test3_fun_offline(ddf_fun_t *fun)
185{
186 ddf_msg(LVL_DEBUG, "test3_fun_offline()");
187 return ddf_fun_offline(fun);
188}
189
190
191int main(int argc, char *argv[])
192{
193 printf(NAME ": HelenOS test3 virtual device driver\n");
194 ddf_log_init(NAME);
195 return ddf_driver_main(&test3_driver);
196}
197
Note: See TracBrowser for help on using the repository browser.