src/module.h
branchmodules
changeset 55 6f7f6ae729d0
parent 54 9f74e924b01a
child 56 942370000450
equal deleted inserted replaced
54:9f74e924b01a 55:6f7f6ae729d0
     2 #define MODULE_H
     2 #define MODULE_H
     3 
     3 
     4 /**
     4 /**
     5  * @file
     5  * @file
     6  *
     6  *
     7  * Dynamically loadable modules for use with nexus
     7  * Dynamically loadable modules for use with nexus.
       
     8  *
       
     9  * The modules are loaded using dlopen(), and hence should be standard dynamic libraries. Module initialization happens
       
    10  * using a module_init_func_t named "<name>_init", which should return some kind of context pointer, which can later be
       
    11  * used to perform other operations on the module.
     8  */
    12  */
     9 #include "nexus.h"
    13 #include "nexus.h"
    10 #include "error.h"
    14 #include "error.h"
    11 
    15 
    12 #include <sys/queue.h>
    16 #include <sys/queue.h>
    27  */
    31  */
    28 struct module {
    32 struct module {
    29     /** The identifying info for the module */
    33     /** The identifying info for the module */
    30     struct module_info info;
    34     struct module_info info;
    31 
    35 
       
    36     /** The dlopen handle */
       
    37     void *handle;
       
    38 
    32     /** The module context object */
    39     /** The module context object */
    33     void *ctx;
    40     void *ctx;
    34 
    41 
       
    42     /** Pointer back to the modules struct used to load this */
       
    43     struct modules *modules;
       
    44 
    35     /** Our entry in the list of modules */
    45     /** Our entry in the list of modules */
    36     TAILQ_ENTRY(module) ctx_modules;
    46     TAILQ_ENTRY(module) modules_list;
    37 };
    47 };
    38 
    48 
    39 /**
    49 /**
    40  * A set of loaded modules, and functionality to load more
    50  * A set of loaded modules, and functionality to load more
    41  */
    51  */
    42 struct modules {
    52 struct modules {
    43     /** The nexus in use */
    53     /** The nexus in use */
    44     struct nexus *nexus;
    54     struct nexus *nexus;
    45 
    55 
    46     /** List of loaded modules */
    56     /** List of loaded modules */
    47     TAILQ_HEAD(module_ctx_modules, module) modules;
    57     TAILQ_HEAD(module_ctx_modules, module) list;
       
    58 };
       
    59 
       
    60 /**
       
    61  * Possible error codes
       
    62  */
       
    63 enum module_error_code {
       
    64     _ERR_MODULE_BEGIN = _ERR_MODULE,
       
    65     
       
    66     ERR_MODULE_OPEN,        ///< dlopen() failed
       
    67     ERR_MODULE_NAME,        ///< invalid module_info.name
       
    68     ERR_MODULE_SYM,         ///< invalid symbol
       
    69     ERR_MODULE_INIT_FUNC,   ///< invalid module_init_func_t
    48 };
    70 };
    49 
    71 
    50 /**
    72 /**
    51  * Module initialization function type
    73  * Module initialization function type
       
    74  *
       
    75  * @param modules the module-loading context, containing the nexus
       
    76  * @param err returned error info
       
    77  * @return context pointer, or NULL on errors
    52  */
    78  */
    53 typedef void* (*module_init_func_t) (struct nexus *nexus, struct error_info *err);
    79 typedef void* (*module_init_func_t) (struct modules *modules, struct error_info *err);
       
    80 
       
    81 /**
       
    82  * Module configuration function type
       
    83  *
       
    84  * @param ctx the module's context pointer
       
    85  * @param name the name of the configuration setting
       
    86  * @param value the value of the configuration setting
       
    87  * @return error code
       
    88  */
       
    89 typedef err_t (*module_conf_func_t) (struct module *module, const char *name, char *value);
       
    90 
       
    91 /**
       
    92  * Maximum length of a module name
       
    93  */
       
    94 #define MODULE_NAME_MAX 24
       
    95 
       
    96 /**
       
    97  * Maximum length of module symbol suffix
       
    98  */
       
    99 #define MODULE_SUFFIX_MAX 16
       
   100 
       
   101 /**
       
   102  * Maximum length of symbol name name, including terminating NUL
       
   103  */
       
   104 #define MODULE_SYMBOL_MAX (MODULE_NAME_MAX + 1 + MODULE_SUFFIX_MAX + 1)
    54 
   105 
    55 /**
   106 /**
    56  * Create a new modules state
   107  * Create a new modules state
    57  */
   108  */
    58 err_t modules_create (struct modules **modules_ptr, struct nexus *nexus);
   109 err_t modules_create (struct modules **modules_ptr, struct nexus *nexus);
    59 
   110 
    60 /**
   111 /**
    61  * Load a new module.
   112  * Load a new module
    62  */
   113  */
    63 err_t module_load (struct moduels *modules, struct module **module, const struct module_info *info, struct error_info *err);
   114 err_t module_load (struct modules *modules, struct module **module, const struct module_info *info, struct error_info *err);
       
   115 
       
   116 /**
       
   117  * Set a module configuration option
       
   118  */
       
   119 err_t module_conf (struct module *module, const char *name, char *value);
    64 
   120 
    65 /**
   121 /**
    66  * Unload a module
   122  * Unload a module
       
   123  *
       
   124  * XXX: not implemented
    67  */
   125  */
    68 err_t module_unload (struct module *module);
   126 err_t module_unload (struct module *module);
    69 
   127 
    70 #endif
   128 #endif