NCDModule.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 (*NCDModuleInst_func_event) (void *user, int event);
  39. typedef int (*NCDModuleInst_func_getvar) (void *user, const char *varname, NCDValue *out);
  40. typedef struct NCDModuleInst_s * (*NCDModuleInst_func_getobj) (void *user, const char *objname);
  41. typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name, NCDValue args);
  42. #define NCDMODULEPROCESS_EVENT_UP 1
  43. #define NCDMODULEPROCESS_EVENT_DOWN 2
  44. #define NCDMODULEPROCESS_EVENT_TERMINATED 3
  45. typedef void (*NCDModuleProcess_handler_event) (void *user, int event);
  46. #define NCDMODULEPROCESS_INTERP_EVENT_CONTINUE 1
  47. #define NCDMODULEPROCESS_INTERP_EVENT_TERMINATE 2
  48. typedef void (*NCDModuleProcess_interp_func_event) (void *user, int event);
  49. typedef int (*NCDModuleProcess_interp_func_getvar) (void *user, const char *name, NCDValue *out);
  50. typedef struct NCDModuleInst_s * (*NCDModuleProcess_interp_func_getobj) (void *user, const char *name);
  51. struct NCDModule;
  52. struct NCDModuleInitParams {
  53. BReactor *reactor;
  54. BProcessManager *manager;
  55. NCDUdevManager *umanager;
  56. };
  57. typedef struct NCDModuleInst_s {
  58. const struct NCDModule *m;
  59. struct NCDModuleInst_s *method_object;
  60. NCDValue *args;
  61. BReactor *reactor;
  62. BProcessManager *manager;
  63. NCDUdevManager *umanager;
  64. void *user;
  65. NCDModuleInst_func_event func_event;
  66. NCDModuleInst_func_getvar func_getvar;
  67. NCDModuleInst_func_getobj func_getobj;
  68. NCDModuleInst_func_initprocess func_initprocess;
  69. BLog_logfunc logfunc;
  70. BPending init_job;
  71. BPending uninit_job;
  72. BPending die_job;
  73. BPending clean_job;
  74. int state;
  75. void *inst_user;
  76. int is_error;
  77. DebugObject d_obj;
  78. } NCDModuleInst;
  79. typedef struct NCDModuleProcess_s {
  80. NCDModuleInst *n;
  81. void *user;
  82. NCDModuleProcess_handler_event handler_event;
  83. BPending event_job;
  84. int state;
  85. void *interp_user;
  86. NCDModuleProcess_interp_func_event interp_func_event;
  87. NCDModuleProcess_interp_func_getvar interp_func_getvar;
  88. NCDModuleProcess_interp_func_getobj interp_func_getobj;
  89. DebugObject d_obj;
  90. } NCDModuleProcess;
  91. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, BReactor *reactor, BProcessManager *manager, NCDUdevManager *umanager, void *user,
  92. NCDModuleInst_func_event func_event,
  93. NCDModuleInst_func_getvar func_getvar,
  94. NCDModuleInst_func_getobj func_getobj,
  95. NCDModuleInst_func_initprocess func_initprocess,
  96. BLog_logfunc logfunc);
  97. void NCDModuleInst_Free (NCDModuleInst *n);
  98. void NCDModuleInst_Event (NCDModuleInst *n, int event);
  99. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out) WARN_UNUSED;
  100. NCDModuleInst * NCDModuleInst_GetObj (NCDModuleInst *n, const char *objname) WARN_UNUSED;
  101. int NCDModuleInst_HaveError (NCDModuleInst *n);
  102. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  103. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event);
  104. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *varname, NCDValue *out) WARN_UNUSED;
  105. NCDModuleInst * NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *objname) WARN_UNUSED;
  106. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  107. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  108. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_event handler_event);
  109. void NCDModuleProcess_Free (NCDModuleProcess *o);
  110. void NCDModuleProcess_Continue (NCDModuleProcess *o);
  111. void NCDModuleProcess_Terminate (NCDModuleProcess *o);
  112. int NCDModuleProcess_GetVar (NCDModuleProcess *o, const char *name, NCDValue *out) WARN_UNUSED;
  113. NCDModuleInst * NCDModuleProcess_GetObj (NCDModuleProcess *o, const char *name) WARN_UNUSED;
  114. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user,
  115. NCDModuleProcess_interp_func_event interp_func_event,
  116. NCDModuleProcess_interp_func_getvar interp_func_getvar,
  117. NCDModuleProcess_interp_func_getobj interp_func_getobj);
  118. void NCDModuleProcess_Interp_Up (NCDModuleProcess *o);
  119. void NCDModuleProcess_Interp_Down (NCDModuleProcess *o);
  120. void NCDModuleProcess_Interp_Terminated (NCDModuleProcess *o);
  121. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  122. typedef void (*NCDModule_func_globalfree) (void);
  123. typedef void (*NCDModule_func_new) (NCDModuleInst *params);
  124. typedef void (*NCDModule_func_die) (void *o);
  125. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValue *out);
  126. typedef NCDModuleInst * (*NCDModule_func_getobj) (void *o, const char *objname);
  127. typedef void (*NCDModule_func_clean) (void *o);
  128. struct NCDModule {
  129. const char *type;
  130. NCDModule_func_new func_new;
  131. NCDModule_func_die func_die;
  132. NCDModule_func_getvar func_getvar;
  133. NCDModule_func_getobj func_getobj;
  134. NCDModule_func_clean func_clean;
  135. };
  136. struct NCDModuleGroup {
  137. NCDModule_func_globalinit func_globalinit;
  138. NCDModule_func_globalfree func_globalfree;
  139. const struct NCDModule *modules;
  140. };
  141. #endif