source: mainline/uspace/drv/test/test3/test3.c

Last change on this file was 4f87a85a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Check return code from ddf_add_fun_to_category

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