00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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