test2.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 <async.h>
00034 #include <stdio.h>
00035 #include <errno.h>
00036 #include <str_error.h>
00037 #include <ddf/driver.h>
00038 #include <ddf/log.h>
00039 
00040 #define NAME "test2"
00041 
00042 static int test2_add_device(ddf_dev_t *dev);
00043 
00044 static driver_ops_t driver_ops = {
00045         .add_device = &test2_add_device
00046 };
00047 
00048 static driver_t test2_driver = {
00049         .name = NAME,
00050         .driver_ops = &driver_ops
00051 };
00052 
00061 static int register_fun_verbose(ddf_dev_t *parent, const char *message,
00062     const char *name, const char *match_id, int match_score)
00063 {
00064         ddf_fun_t *fun;
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                 return ENOMEM;
00073         }
00074 
00075         rc = ddf_fun_add_match_id(fun, match_id, match_score);
00076         if (rc != EOK) {
00077                 ddf_msg(LVL_ERROR, "Failed adding match IDs to function %s",
00078                     name);
00079                 ddf_fun_destroy(fun);
00080                 return rc;
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                 ddf_fun_destroy(fun);
00088                 return rc;
00089         }
00090 
00091         ddf_msg(LVL_NOTE, "Registered child device `%s'", name);
00092         return EOK;
00093 }
00094 
00100 static int postponed_birth(void *arg)
00101 {
00102         ddf_dev_t *dev = (ddf_dev_t *) arg;
00103         ddf_fun_t *fun_a;
00104         int rc;
00105 
00106         async_usleep(1000);
00107 
00108         (void) register_fun_verbose(dev, "child driven by the same task",
00109             "child", "virtual&test2", 10);
00110         (void) register_fun_verbose(dev, "child driven by test1",
00111             "test1", "virtual&test1", 10);
00112 
00113         fun_a = ddf_fun_create(dev, fun_exposed, "a");
00114         if (fun_a == NULL) {
00115                 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
00116                 return ENOMEM;
00117         }
00118 
00119         rc = ddf_fun_bind(fun_a);
00120         if (rc != EOK) {
00121                 ddf_msg(LVL_ERROR, "Failed binding function 'a'.");
00122                 return rc;
00123         }
00124 
00125         ddf_fun_add_to_class(fun_a, "virtual");
00126 
00127         return EOK;
00128 }
00129 
00130 static int test2_add_device(ddf_dev_t *dev)
00131 {
00132         ddf_msg(LVL_DEBUG, "test2_add_device(name=\"%s\", handle=%d)",
00133             dev->name, (int) dev->handle);
00134 
00135         if (str_cmp(dev->name, "child") != 0) {
00136                 fid_t postpone = fibril_create(postponed_birth, dev);
00137                 if (postpone == 0) {
00138                         ddf_msg(LVL_ERROR, "fibril_create() failed.");
00139                         return ENOMEM;
00140                 }
00141                 fibril_add_ready(postpone);
00142         } else {
00143                 (void) register_fun_verbose(dev, "child without available driver",
00144                     "ERROR", "non-existent.match.id", 10);
00145         }
00146 
00147         return EOK;
00148 }
00149 
00150 int main(int argc, char *argv[])
00151 {
00152         printf(NAME ": HelenOS test2 virtual device driver\n");
00153         ddf_log_init(NAME, LVL_ERROR);
00154         return ddf_driver_main(&test2_driver);
00155 }
00156 
00157 

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