test3.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 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 #define NAME "test3"
00040 
00041 static int test3_add_device(ddf_dev_t *dev);
00042 
00043 static driver_ops_t driver_ops = {
00044         .add_device = &test3_add_device
00045 };
00046 
00047 static driver_t test3_driver = {
00048         .name = NAME,
00049         .driver_ops = &driver_ops
00050 };
00051 
00052 static int register_fun_and_add_to_class(ddf_dev_t *parent,
00053      const char *base_name, size_t index, const char *class_name)
00054 {
00055         ddf_fun_t *fun = NULL;
00056         int rc;
00057         char *fun_name = NULL;
00058         
00059         rc = asprintf(&fun_name, "%s%zu", base_name, index);
00060         if (rc < 0) {
00061                 ddf_msg(LVL_ERROR, "Failed to format string: %s", str_error(rc));
00062                 goto leave;
00063         }
00064         
00065         fun = ddf_fun_create(parent, fun_exposed, fun_name);
00066         if (fun == NULL) {
00067                 ddf_msg(LVL_ERROR, "Failed creating function %s", fun_name);
00068                 rc = ENOMEM;
00069                 goto leave;
00070         }
00071 
00072         rc = ddf_fun_bind(fun);
00073         if (rc != EOK) {
00074                 ddf_msg(LVL_ERROR, "Failed binding function %s: %s", fun_name,
00075                     str_error(rc));
00076                 goto leave;
00077         }
00078         
00079         ddf_fun_add_to_class(fun, class_name);
00080 
00081         ddf_msg(LVL_NOTE, "Registered exposed function `%s'.", fun_name);
00082 
00083 leave:  
00084         free(fun_name);
00085         
00086         if ((rc != EOK) && (fun != NULL)) {
00087                 ddf_fun_destroy(fun);
00088         }
00089         
00090         return rc;
00091 }
00092 
00093 static int test3_add_device(ddf_dev_t *dev)
00094 {
00095         int rc = EOK;
00096 
00097         ddf_msg(LVL_DEBUG, "add_device(name=\"%s\", handle=%d)",
00098             dev->name, (int) dev->handle);
00099 
00100         size_t i;
00101         for (i = 0; i < 20; i++) {
00102                 rc = register_fun_and_add_to_class(dev,
00103                     "test3_", i, "test3");
00104                 if (rc != EOK) {
00105                         break;
00106                 }
00107         }
00108         
00109         return rc;
00110 }
00111 
00112 int main(int argc, char *argv[])
00113 {
00114         printf(NAME ": HelenOS test3 virtual device driver\n");
00115         ddf_log_init(NAME, LVL_ERROR);
00116         return ddf_driver_main(&test3_driver);
00117 }
00118 

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