TR181-XPON  1.4.0
TR-181 PON manager.
module_mgmt.c File Reference
#include "module_mgmt.h"
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <amxc/amxc_macros.h>
#include <amxc/amxc.h>
#include <amxm/amxm.h>
#include <amxp/amxp_dir.h>
#include "data_model.h"
#include "pon_stat.h"
#include "pon_cfg.h"
#include "xpon_trace.h"

Go to the source code of this file.

Data Structures

struct  _pon_stat_function
 

Macros

#define MOD_PON_STAT   "pon_stat"
 
#define MOD_PON_CFG   "pon_cfg"
 

Typedefs

typedef struct _pon_stat_function pon_stat_function_t
 

Functions

static bool register_pon_stat_module (void)
 
static bool register_pon_cfg_module (void)
 
static int handle_vendor_module_match (const char *name, void *priv UNUSED)
 
static bool find_vendor_module (void)
 
static bool load_vendor_module (void)
 
bool mod_module_mgmt_init (bool *module_error)
 
const char * mod_get_vendor_module_loaded (void)
 
void mod_module_mgmt_cleanup (void)
 

Variables

static const char *const MODULES_PATH = "/usr/lib/amx/tr181-xpon/modules/"
 
static amxm_module_t * s_pon_stat_module = NULL
 
static amxm_module_t * s_pon_cfg_module = NULL
 
static amxc_string_t s_module_name
 
static amxm_shared_object_t * s_module_so = NULL
 
static const pon_stat_function_t PON_STAT_FUNCTIONS []
 
static const pon_stat_function_t PON_CFG_FUNCTIONS []
 

Macro Definition Documentation

◆ MOD_PON_CFG

#define MOD_PON_CFG   "pon_cfg"

Definition at line 84 of file module_mgmt.c.

◆ MOD_PON_STAT

#define MOD_PON_STAT   "pon_stat"

Definition at line 83 of file module_mgmt.c.

Typedef Documentation

◆ pon_stat_function_t

Function Documentation

◆ find_vendor_module()

static bool find_vendor_module ( void  )
static

Find vendor module in MODULES_PATH and assign its name to s_module_name.

Definition at line 213 of file module_mgmt.c.

213  {
214 
215  const char* const filter = "d_type == DT_REG && d_name matches 'mod-xpon-.*\\.so'";
216 
217  if(amxp_dir_scan(MODULES_PATH, filter, false, handle_vendor_module_match, NULL) != 0) {
218  /* handle_vendor_module_match() already logged error */
219  return false;
220  }
221 
222  if(amxc_string_is_empty(&s_module_name)) {
223  SAH_TRACEZ_ERROR(ME, "Failed to find vendor module");
224  return false;
225  }
226  return true;
227 }
static amxc_string_t s_module_name
Definition: module_mgmt.c:90
static const char *const MODULES_PATH
Definition: module_mgmt.c:86
static int handle_vendor_module_match(const char *name, void *priv UNUSED)
Definition: module_mgmt.c:183
#define ME
Definition: xpon_trace.h:78
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_vendor_module_match()

static int handle_vendor_module_match ( const char *  name,
void *priv  UNUSED 
)
static

Handle a module found by amxp_dir_scan() in find_vendor_module().

Parameters
[in]namepath to the vendor module, e.g., "/usr/lib/amx/tr181-xpon/modules/mod-xpon-prpl.so"
Returns
0 on success: amxp_dir_scan() should continue scanning
1 on error: amxp_dir_scan() should stop scanning

Definition at line 183 of file module_mgmt.c.

183  {
184  SAH_TRACEZ_DEBUG(ME, "match: '%s'", name);
185 
186  int rc = 0; /* keep scanning */
187 
188  amxc_string_t module;
189  amxc_string_init(&module, 0);
190  amxc_string_setf(&module, "%s", name);
191  amxc_string_replace(&module, MODULES_PATH, "", /*max= */ 1);
192  amxc_string_replace(&module, ".so", "", /*max= */ 1);
193  const char* const module_cstr = amxc_string_get(&module, 0);
194 
195  if(amxc_string_is_empty(&s_module_name)) {
196  amxc_string_copy(&s_module_name, &module);
197  SAH_TRACEZ_DEBUG(ME, "Found vendor module: '%s.so'", module_cstr);
198  dm_set_vendor_module(module_cstr);
199  } else {
200  SAH_TRACEZ_ERROR(ME, "Found more than one vendor module");
201  SAH_TRACEZ_ERROR(ME, " modules found: %s.so, %s.so",
202  amxc_string_get(&s_module_name, 0), module_cstr);
203  rc = 1; /* error, stop scanning */
204  }
205 
206  amxc_string_clean(&module);
207  return rc;
208 }
void dm_set_vendor_module(const char *name)
Definition: data_model.c:178
#define SAH_TRACEZ_DEBUG(zone, format,...)
Definition: xpon_trace.h:115
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_vendor_module()

static bool load_vendor_module ( void  )
static

Find module with pattern 'mod-xpon-*.so' in MODULES_PATH and load it.

If the function finds a module:

  • set s_module_name to the name of the module found, e.g. 'mod-xpon-prpl' if the function found 'mod-xpon-prpl.so' as vendor module in MODULES_PATH.
  • set XPON.ModuleName in DM to the same name.

Set XPON.ModuleError to true if the function encounters one of the following errors:

  • It fails to find a vendor module.
  • It finds more than one vendor module. An image must only install 1 vendor module.
  • It fails to load the module.
Returns
true on success, else false

Definition at line 246 of file module_mgmt.c.

246  {
247 
248  bool rv = false;
249 
250  amxc_string_t mod_path;
251  amxc_string_init(&mod_path, 0);
252 
253  if(!find_vendor_module()) {
254  goto exit;
255  }
256  const char* const module = amxc_string_get(&s_module_name, 0);
257  amxc_string_setf(&mod_path, "%s%s.so", MODULES_PATH, module);
258 
259  const char* const path_to_so = amxc_string_get(&mod_path, /*offset=*/ 0);
260 
261  if(access(path_to_so, F_OK) != 0) {
262  SAH_TRACEZ_ERROR(ME, "%s does not exist", path_to_so);
263  goto exit;
264  }
265 
266  if(amxm_so_open(&s_module_so, module, path_to_so)) {
267  const char* error = dlerror();
268  SAH_TRACEZ_ERROR(ME, "Failed to load %s module", module);
269  SAH_TRACEZ_ERROR(ME, "dlerror(): '%s'", error ? error : "No error");
270  goto exit;
271  }
272 
273  rv = true;
274 
275 exit:
276  if(!rv) {
278  }
279  amxc_string_clean(&mod_path);
280  return rv;
281 }
void dm_set_module_error(void)
Definition: data_model.c:192
static amxm_shared_object_t * s_module_so
Definition: module_mgmt.c:91
static bool find_vendor_module(void)
Definition: module_mgmt.c:213
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mod_get_vendor_module_loaded()

const char* mod_get_vendor_module_loaded ( void  )

Return the name of the vendor module the plugin loaded at startup.

Returns
the name of that vendor module upon success
NULL if the plugin failed to load the vendor module at startup

Definition at line 326 of file module_mgmt.c.

326  {
327 
328  return s_module_so ? amxc_string_get(&s_module_name, 0) : NULL;
329 }
Here is the caller graph for this function:

◆ mod_module_mgmt_cleanup()

void mod_module_mgmt_cleanup ( void  )

Clean up the module_mgmt part of this plugin.

The function does the reverse of mod_module_mgmt_init():

  • unload the vendor module
  • unregisters the namespaces 'pon_stat' and 'pon_cfg'

The plugin must call this function once when stopping.

Definition at line 340 of file module_mgmt.c.

340  {
341 
342  if(s_module_so) {
343  if(amxm_so_close(&s_module_so)) {
344  SAH_TRACEZ_ERROR(ME, "Failed to close module");
345  }
346  }
347 
348  if(s_pon_stat_module) {
349  amxm_module_deregister(&s_pon_stat_module);
350  }
351  if(s_pon_cfg_module) {
352  amxm_module_deregister(&s_pon_cfg_module);
353  }
354  amxc_string_clean(&s_module_name);
355 }
static amxm_module_t * s_pon_cfg_module
Definition: module_mgmt.c:89
static amxm_module_t * s_pon_stat_module
Definition: module_mgmt.c:88
Here is the caller graph for this function:

◆ mod_module_mgmt_init()

bool mod_module_mgmt_init ( bool *  module_error)

Initialize the module_mgmt part of this plugin.

The function:

  • registers the namespaces 'pon_stat' and 'pon_cfg'. The namespaces have functions which can be called by the vendor module.
  • loads the vendor module.

The plugin must call this function once at startup.

Parameters
[in,out]module_errorthe function sets it to true if it encounters an error regarding (the loading of) the vendor module
Returns
true on success, else false

Definition at line 299 of file module_mgmt.c.

299  {
300 
301  amxc_string_init(&s_module_name, 0);
302 
303  if(!register_pon_stat_module()) {
304  return false;
305  }
306 
307  if(!register_pon_cfg_module()) {
308  return false;
309  }
310 
311  if(!load_vendor_module()) {
312  if(module_error) {
313  *module_error = true;
314  }
315  return false;
316  }
317  return true;
318 }
static bool load_vendor_module(void)
Definition: module_mgmt.c:246
static bool register_pon_cfg_module(void)
Definition: module_mgmt.c:145
static bool register_pon_stat_module(void)
Definition: module_mgmt.c:117
Here is the call graph for this function:
Here is the caller graph for this function:

◆ register_pon_cfg_module()

static bool register_pon_cfg_module ( void  )
static

Definition at line 145 of file module_mgmt.c.

145  {
146 
147  bool rv = false;
148  amxm_shared_object_t* so = amxm_get_so("self");
149  if(!so) {
150  SAH_TRACEZ_ERROR(ME, "Failed to get amxm_shared_object_t for self");
151  goto exit;
152  }
153 
154  if(amxm_module_register(&s_pon_cfg_module, so, MOD_PON_CFG)) {
155  SAH_TRACEZ_ERROR(ME, "Failed to register %s module namespace", MOD_PON_CFG);
156  goto exit;
157  }
158 
159  int i;
160  for(i = 0; PON_CFG_FUNCTIONS[i].name != NULL; ++i) {
161  if(amxm_module_add_function(s_pon_cfg_module, PON_CFG_FUNCTIONS[i].name,
162  PON_CFG_FUNCTIONS[i].impl)) {
163  SAH_TRACEZ_ERROR(ME, "Failed to register self.%s.%s()", MOD_PON_CFG,
164  PON_CFG_FUNCTIONS[i].name);
165  }
166  }
167  rv = true;
168 
169 exit:
170  return rv;
171 }
static const pon_stat_function_t PON_CFG_FUNCTIONS[]
Definition: module_mgmt.c:111
#define MOD_PON_CFG
Definition: module_mgmt.c:84
const char * name
Definition: module_mgmt.c:94
Here is the caller graph for this function:

◆ register_pon_stat_module()

static bool register_pon_stat_module ( void  )
static

Definition at line 117 of file module_mgmt.c.

117  {
118 
119  bool rv = false;
120  amxm_shared_object_t* so = amxm_get_so("self");
121  if(!so) {
122  SAH_TRACEZ_ERROR(ME, "Failed to get amxm_shared_object_t for self");
123  goto exit;
124  }
125 
126  if(amxm_module_register(&s_pon_stat_module, so, MOD_PON_STAT)) {
127  SAH_TRACEZ_ERROR(ME, "Failed to register %s module namespace", MOD_PON_STAT);
128  goto exit;
129  }
130 
131  int i;
132  for(i = 0; PON_STAT_FUNCTIONS[i].name != NULL; ++i) {
133  if(amxm_module_add_function(s_pon_stat_module, PON_STAT_FUNCTIONS[i].name,
134  PON_STAT_FUNCTIONS[i].impl)) {
135  SAH_TRACEZ_ERROR(ME, "Failed to register self.%s.%s()", MOD_PON_STAT,
136  PON_STAT_FUNCTIONS[i].name);
137  }
138  }
139  rv = true;
140 
141 exit:
142  return rv;
143 }
#define MOD_PON_STAT
Definition: module_mgmt.c:83
static const pon_stat_function_t PON_STAT_FUNCTIONS[]
Definition: module_mgmt.c:98
Here is the caller graph for this function:

Variable Documentation

◆ MODULES_PATH

const char* const MODULES_PATH = "/usr/lib/amx/tr181-xpon/modules/"
static

Definition at line 86 of file module_mgmt.c.

◆ PON_CFG_FUNCTIONS

const pon_stat_function_t PON_CFG_FUNCTIONS[]
static
Initial value:
= {
{ .name = "pon_cfg_get_param_value", .impl = pon_cfg_get_param_value },
{ .name = NULL, .impl = NULL }
}
int pon_cfg_get_param_value(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
Definition: pon_cfg.c:85

Definition at line 111 of file module_mgmt.c.

◆ PON_STAT_FUNCTIONS

const pon_stat_function_t PON_STAT_FUNCTIONS[]
static
Initial value:
= {
{ .name = "dm_instance_added", .impl = dm_instance_added },
{ .name = "dm_instance_removed", .impl = dm_instance_removed },
{ .name = "dm_object_changed", .impl = dm_object_changed },
{ .name = "dm_add_or_change_instance", .impl = dm_add_or_change_instance },
{ .name = "omci_reset_mib", .impl = omci_reset_mib },
{ .name = "watch_file_descriptor_start", .impl = watch_file_descriptor_start },
{ .name = "watch_file_descriptor_stop", .impl = watch_file_descriptor_stop },
{ .name = "dm_set_xpon_parameter", .impl = dm_set_xpon_parameter },
{ .name = NULL, .impl = NULL }
}
int dm_instance_added(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int watch_file_descriptor_start(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int dm_object_changed(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int dm_instance_removed(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int dm_add_or_change_instance(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int watch_file_descriptor_stop(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int dm_set_xpon_parameter(const char *function_name, amxc_var_t *args, amxc_var_t *ret)
int omci_reset_mib(const char *function_name, amxc_var_t *args, amxc_var_t *ret)

Definition at line 98 of file module_mgmt.c.

◆ s_module_name

amxc_string_t s_module_name
static

Definition at line 90 of file module_mgmt.c.

◆ s_module_so

amxm_shared_object_t* s_module_so = NULL
static

Definition at line 91 of file module_mgmt.c.

◆ s_pon_cfg_module

amxm_module_t* s_pon_cfg_module = NULL
static

Definition at line 89 of file module_mgmt.c.

◆ s_pon_stat_module

amxm_module_t* s_pon_stat_module = NULL
static

Definition at line 88 of file module_mgmt.c.