[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 |
|
---|
[0c0f823b] | 43 | static int test3_dev_add(ddf_dev_t *dev);
|
---|
[b29bb09] | 44 | static int test3_dev_remove(ddf_dev_t *dev);
|
---|
| 45 | static int test3_fun_online(ddf_fun_t *fun);
|
---|
| 46 | static int test3_fun_offline(ddf_fun_t *fun);
|
---|
[5d1b3aa] | 47 |
|
---|
| 48 | static 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 |
|
---|
| 55 | static driver_t test3_driver = {
|
---|
| 56 | .name = NAME,
|
---|
| 57 | .driver_ops = &driver_ops
|
---|
| 58 | };
|
---|
| 59 |
|
---|
[b29bb09] | 60 | typedef struct {
|
---|
| 61 | ddf_dev_t *dev;
|
---|
| 62 | ddf_fun_t *fun[NUM_FUNCS];
|
---|
| 63 | } test3_t;
|
---|
| 64 |
|
---|
[1dc4a5e] | 65 | static int 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;
|
---|
| 70 | int rc;
|
---|
| 71 | char *fun_name = NULL;
|
---|
| 72 |
|
---|
| 73 | rc = asprintf(&fun_name, "%s%zu", base_name, index);
|
---|
| 74 | if (rc < 0) {
|
---|
| 75 | ddf_msg(LVL_ERROR, "Failed to format string: %s", str_error(rc));
|
---|
| 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 |
|
---|
[1dc4a5e] | 93 | ddf_fun_add_to_category(fun, class_name);
|
---|
[5d1b3aa] | 94 |
|
---|
| 95 | ddf_msg(LVL_NOTE, "Registered exposed function `%s'.", fun_name);
|
---|
| 96 |
|
---|
| 97 | leave:
|
---|
| 98 | free(fun_name);
|
---|
| 99 |
|
---|
| 100 | if ((rc != EOK) && (fun != NULL)) {
|
---|
| 101 | ddf_fun_destroy(fun);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[b29bb09] | 104 | *pfun = fun;
|
---|
[5d1b3aa] | 105 | return rc;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[b29bb09] | 108 | static int fun_remove(ddf_fun_t *fun, const char *name)
|
---|
| 109 | {
|
---|
| 110 | int rc;
|
---|
| 111 |
|
---|
[deac215e] | 112 | ddf_msg(LVL_DEBUG, "fun_remove(%p, '%s')", fun, name);
|
---|
[b29bb09] | 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 |
|
---|
[0c0f823b] | 129 | static int test3_dev_add(ddf_dev_t *dev)
|
---|
[5d1b3aa] | 130 | {
|
---|
| 131 | int rc = EOK;
|
---|
[b29bb09] | 132 | test3_t *test3;
|
---|
[5d1b3aa] | 133 |
|
---|
[0c0f823b] | 134 | ddf_msg(LVL_DEBUG, "dev_add(name=\"%s\", handle=%d)",
|
---|
[56fd7cf] | 135 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
---|
[5d1b3aa] | 136 |
|
---|
[b29bb09] | 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 |
|
---|
[5d1b3aa] | 143 | size_t i;
|
---|
[b29bb09] | 144 | for (i = 0; i < NUM_FUNCS; i++) {
|
---|
[1dc4a5e] | 145 | rc = register_fun_and_add_to_category(dev,
|
---|
[b29bb09] | 146 | "test3_", i, "test3", &test3->fun[i]);
|
---|
[5d1b3aa] | 147 | if (rc != EOK) {
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | return rc;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[b29bb09] | 155 | static int test3_dev_remove(ddf_dev_t *dev)
|
---|
| 156 | {
|
---|
[56fd7cf] | 157 | test3_t *test3 = (test3_t *)ddf_dev_data_get(dev);
|
---|
[b29bb09] | 158 | char *fun_name;
|
---|
| 159 | int rc;
|
---|
| 160 | size_t i;
|
---|
| 161 |
|
---|
| 162 | for (i = 0; i < NUM_FUNCS; i++) {
|
---|
| 163 | rc = asprintf(&fun_name, "test3_%zu", i);
|
---|
| 164 | if (rc < 0) {
|
---|
| 165 | ddf_msg(LVL_ERROR, "Failed to format string: %s", str_error(rc));
|
---|
| 166 | return ENOMEM;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | rc = fun_remove(test3->fun[i], fun_name);
|
---|
| 170 | if (rc != EOK)
|
---|
| 171 | return rc;
|
---|
| 172 |
|
---|
| 173 | free(fun_name);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | return EOK;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | static int test3_fun_online(ddf_fun_t *fun)
|
---|
| 180 | {
|
---|
| 181 | ddf_msg(LVL_DEBUG, "test3_fun_online()");
|
---|
| 182 | return ddf_fun_online(fun);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | static int test3_fun_offline(ddf_fun_t *fun)
|
---|
| 186 | {
|
---|
| 187 | ddf_msg(LVL_DEBUG, "test3_fun_offline()");
|
---|
| 188 | return ddf_fun_offline(fun);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
[5d1b3aa] | 192 | int main(int argc, char *argv[])
|
---|
| 193 | {
|
---|
| 194 | printf(NAME ": HelenOS test3 virtual device driver\n");
|
---|
[267f235] | 195 | ddf_log_init(NAME);
|
---|
[5d1b3aa] | 196 | return ddf_driver_main(&test3_driver);
|
---|
| 197 | }
|
---|
| 198 |
|
---|