NCDModule.h 6.4 KB

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