test1.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00032 #include <assert.h>
00033 #include <stdio.h>
00034 #include <errno.h>
00035 #include <str_error.h>
00036 #include <ddf/driver.h>
00037 #include <ddf/log.h>
00038 
00039 #include "test1.h"
00040 
00041 static int test1_add_device(ddf_dev_t *dev);
00042 
00043 static driver_ops_t driver_ops = {
00044         .add_device = &test1_add_device
00045 };
00046 
00047 static driver_t test1_driver = {
00048         .name = NAME,
00049         .driver_ops = &driver_ops
00050 };
00051 
00060 static int register_fun_verbose(ddf_dev_t *parent, const char *message,
00061     const char *name, const char *match_id, int match_score,
00062     int expected_rc)
00063 {
00064         ddf_fun_t *fun = NULL;
00065         int rc;
00066 
00067         ddf_msg(LVL_DEBUG, "Registering function `%s': %s.", name, message);
00068 
00069         fun = ddf_fun_create(parent, fun_inner, name);
00070         if (fun == NULL) {
00071                 ddf_msg(LVL_ERROR, "Failed creating function %s", name);
00072                 rc = ENOMEM;
00073                 goto leave;
00074         }
00075 
00076         rc = ddf_fun_add_match_id(fun, str_dup(match_id), match_score);
00077         if (rc != EOK) {
00078                 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
00079                     name);
00080                 goto leave;
00081         }
00082 
00083         rc = ddf_fun_bind(fun);
00084         if (rc != EOK) {
00085                 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", name,
00086                     str_error(rc));
00087                 goto leave;
00088         }
00089 
00090         ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
00091         rc = EOK;
00092 
00093 leave:
00094         if (rc != expected_rc) {
00095                 fprintf(stderr,
00096                     NAME ": Unexpected error registering function `%s'.\n" 
00097                     NAME ":     Expected \"%s\" but got \"%s\".\n",
00098                     name, str_error(expected_rc), str_error(rc));
00099         }
00100 
00101         if ((rc != EOK) && (fun != NULL)) {
00102                 ddf_fun_destroy(fun);
00103         }
00104 
00105         return rc;
00106 }
00107 
00125 static int test1_add_device(ddf_dev_t *dev)
00126 {
00127         ddf_fun_t *fun_a;
00128         int rc;
00129 
00130         ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
00131             dev->name, (int) dev->handle);
00132 
00133         fun_a = ddf_fun_create(dev, fun_exposed, "a");
00134         if (fun_a == NULL) {
00135                 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
00136                 return ENOMEM;
00137         }
00138 
00139         rc = ddf_fun_bind(fun_a);
00140         if (rc != EOK) {
00141                 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
00142                 return rc;
00143         }
00144 
00145         ddf_fun_add_to_class(fun_a, "virtual");
00146 
00147         if (str_cmp(dev->name, "null") == 0) {
00148                 fun_a->ops = &char_device_ops;
00149                 ddf_fun_add_to_class(fun_a, "virt-null");
00150         } else if (str_cmp(dev->name, "test1") == 0) {
00151                 (void) register_fun_verbose(dev,
00152                     "cloning myself ;-)", "clone",
00153                     "virtual&test1", 10, EOK);
00154                 (void) register_fun_verbose(dev,
00155                     "cloning myself twice ;-)", "clone",
00156                     "virtual&test1", 10, EEXISTS);
00157         } else if (str_cmp(dev->name, "clone") == 0) {
00158                 (void) register_fun_verbose(dev,
00159                     "run by the same task", "child",
00160                     "virtual&test1&child", 10, EOK);
00161         }
00162 
00163         ddf_msg(LVL_DEBUG, "Device `%s' accepted.", dev->name);
00164 
00165         return EOK;
00166 }
00167 
00168 int main(int argc, char *argv[])
00169 {
00170         printf(NAME ": HelenOS test1 virtual device driver\n");
00171         ddf_log_init(NAME, LVL_ERROR);
00172         return ddf_driver_main(&test1_driver);
00173 }
00174 

Generated on Thu Jun 2 07:45:44 2011 for HelenOS/USB by  doxygen 1.4.7