NCDModule.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file NCDModule.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifndef BADVPN_NCD_NCDMODULE_H
  23. #define BADVPN_NCD_NCDMODULE_H
  24. #include <misc/debug.h>
  25. #include <system/BReactor.h>
  26. #include <base/BPending.h>
  27. #include <base/BLog.h>
  28. #include <system/BProcess.h>
  29. #include <udevmonitor/NCDUdevManager.h>
  30. #include <ncd/NCDValue.h>
  31. #define NCDMODULE_EVENT_UP 1
  32. #define NCDMODULE_EVENT_DOWN 2
  33. #define NCDMODULE_EVENT_DEAD 3
  34. #define NCDMODULE_TOEVENT_DIE 101
  35. #define NCDMODULE_TOEVENT_CLEAN 102
  36. struct NCDModuleInst_s;
  37. struct NCDModuleProcess_s;
  38. typedef void (*NCDModule_handler_event) (void *user, int event);
  39. typedef int (*NCDModule_handler_getvar) (void *user, const char *varname, NCDValue *out);
  40. typedef struct NCDModuleInst_s * (*NCDModule_handler_getobj) (void *user, const char *objname);
  41. typedef int (*NCDModule_handler_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name, NCDValue args);
  42. typedef void (*NCDModuleProcess_handler_dead) (void *user);
  43. typedef void (*NCDModuleProcess_interp_handler_die) (void *user);
  44. struct NCDModule;
  45. struct NCDModuleInitParams {
  46. BReactor *reactor;
  47. BProcessManager *manager;
  48. NCDUdevManager *umanager;
  49. };
  50. typedef struct NCDModuleInst_s {
  51. const struct NCDModule *m;
  52. struct NCDModuleInst_s *method_object;
  53. NCDValue *args;
  54. BReactor *reactor;
  55. BProcessManager *manager;
  56. NCDUdevManager *umanager;
  57. void *user;
  58. NCDModule_handler_event handler_event;
  59. NCDModule_handler_getvar handler_getvar;
  60. NCDModule_handler_getobj handler_getobj;
  61. NCDModule_handler_initprocess handler_initprocess;
  62. BLog_logfunc logfunc;
  63. BPending init_job;
  64. BPending uninit_job;
  65. BPending die_job;
  66. BPending clean_job;
  67. int state;
  68. void *inst_user;
  69. int is_error;
  70. DebugObject d_obj;
  71. } NCDModuleInst;
  72. typedef struct NCDModuleProcess_s {
  73. NCDModuleInst *n;
  74. void *user;
  75. NCDModuleProcess_handler_dead handler_dead;
  76. void *interp_user;
  77. NCDModuleProcess_interp_handler_die interp_handler_die;
  78. BPending die_job;
  79. BPending dead_job;
  80. int state;
  81. DebugObject d_obj;
  82. } NCDModuleProcess;
  83. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, BReactor *reactor, BProcessManager *manager, NCDUdevManager *umanager, void *user,
  84. NCDModule_handler_event handler_event,
  85. NCDModule_handler_getvar handler_getvar,
  86. NCDModule_handler_getobj handler_getobj,
  87. NCDModule_handler_initprocess handler_initprocess,
  88. BLog_logfunc logfunc);
  89. void NCDModuleInst_Free (NCDModuleInst *n);
  90. void NCDModuleInst_Event (NCDModuleInst *n, int event);
  91. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out) WARN_UNUSED;
  92. NCDModuleInst * NCDModuleInst_GetObj (NCDModuleInst *n, const char *objname) WARN_UNUSED;
  93. int NCDModuleInst_HaveError (NCDModuleInst *n);
  94. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  95. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event);
  96. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *varname, NCDValue *out) WARN_UNUSED;
  97. NCDModuleInst * NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *objname) WARN_UNUSED;
  98. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  99. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  100. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_dead handler_dead);
  101. void NCDModuleProcess_Free (NCDModuleProcess *o);
  102. void NCDModuleProcess_Die (NCDModuleProcess *o);
  103. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user, NCDModuleProcess_interp_handler_die interp_handler_die);
  104. void NCDModuleProcess_Interp_Dead (NCDModuleProcess *o);
  105. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  106. typedef void (*NCDModule_func_globalfree) (void);
  107. typedef void (*NCDModule_func_new) (NCDModuleInst *params);
  108. typedef void (*NCDModule_func_die) (void *o);
  109. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValue *out);
  110. typedef NCDModuleInst * (*NCDModule_func_getobj) (void *o, const char *objname);
  111. typedef void (*NCDModule_func_clean) (void *o);
  112. struct NCDModule {
  113. const char *type;
  114. NCDModule_func_new func_new;
  115. NCDModule_func_die func_die;
  116. NCDModule_func_getvar func_getvar;
  117. NCDModule_func_getobj func_getobj;
  118. NCDModule_func_clean func_clean;
  119. };
  120. struct NCDModuleGroup {
  121. NCDModule_func_globalinit func_globalinit;
  122. NCDModule_func_globalfree func_globalfree;
  123. const struct NCDModule *modules;
  124. };
  125. #endif