TR181-XPON  1.4.0
TR-181 PON manager.
persistency.c File Reference
#include "persistency.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <amxc/amxc_macros.h>
#include "dm_xpon_mngr.h"
#include "xpon_trace.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static char * get_storage_dir (void)
 
void persistency_init (void)
 
void persistency_cleanup (void)
 
const char * persistency_get_folder (void)
 
static void touch_file (const char *const file)
 
static void create_enabled_file_path (amxc_string_t *const file_path, const char *const object)
 
void persistency_enable (const char *const object, bool enable)
 
bool persistency_is_enabled (const char *const object)
 

Variables

static const char *const STORAGE_DIR = "/etc/config/tr181-xpon/"
 
static char * s_storage_dir = NULL
 

Detailed Description

Functionality related to saving and querying the reboot persistent settings. The only read-write parameters in the BBF XPON DM which need to be reboot persistent are:

  • XPON.ONU.{i}.Enable and
  • XPON.ONU.{i}.ANI.{i}.Enable

The object instances do not exist right after the plugin has loaded the odl files defining the DM, and the _defaults.odl file. The plugin creates them afterwards, while querying the state in the HAL, or when it receives a notification from the HAL to create such an instance. Therefore the plugin does not use the odl mechanism (via a _save.odl file) to save and restore the values of the Enable parameters above.

If one of the Enable parameters is set to true, the persistency part creates an empty file in the storage dir of this plugin. E.g., if XPON.ONU.1.Enable is set to true, it creates XPON.ONU.1_enabled.txt. If the plugin after reboot creates the instance XPON.ONU.1, it checks if that file exists to determine if it should set XPON.ONU.1.Enable to true.

Definition in file persistency.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Define _GNU_SOURCE to avoid following error: implicit declaration of function ‘strdup’

Definition at line 92 of file persistency.c.

Function Documentation

◆ create_enabled_file_path()

static void create_enabled_file_path ( amxc_string_t *const  file_path,
const char *const  object 
)
static

Definition at line 234 of file persistency.c.

235  {
236 
237  amxc_string_set(file_path, s_storage_dir);
238  amxc_string_appendf(file_path, "%s", object);
239  amxc_string_append(file_path, "_enabled.txt", 12);
240 }
static char * s_storage_dir
Definition: persistency.c:118
Here is the caller graph for this function:

◆ get_storage_dir()

static char* get_storage_dir ( void  )
static

Return the storage dir.

The returned pointer must be freed.

Returns
the storage dir upon success, else NULL

Definition at line 127 of file persistency.c.

127  {
128 
129  char* result = NULL;
130  amxo_parser_t* parser = xpon_mngr_get_parser();
131  when_null(parser, exit);
132 
133  amxc_string_t dir;
134  amxc_string_init(&dir, 0);
135  const char* storage_dir = GETP_CHAR(&parser->config, "storage-path");
136  if(!storage_dir) {
137  amxc_string_clean(&dir);
138  goto exit;
139  }
140  amxc_string_setf(&dir, "%s", storage_dir);
141  amxc_string_resolve(&dir, &parser->config);
142  result = amxc_string_take_buffer(&dir);
143 
144 exit:
145  return result;
146 }
amxo_parser_t *PRIVATE xpon_mngr_get_parser(void)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ persistency_cleanup()

void persistency_cleanup ( void  )

Clean up the persistency part.

The plugin must call this function once when stopping.

Definition at line 198 of file persistency.c.

198  {
199  if(s_storage_dir) {
200  free(s_storage_dir);
201  s_storage_dir = NULL;
202  }
203 }
Here is the caller graph for this function:

◆ persistency_enable()

void persistency_enable ( const char *const  object,
bool  enable 
)

Save whether object is enabled or disabled.

Parameters
[in]objectpath to object in DM being enabled/disabled, e.g., "XPON.ONU.1"
[in]enabletrue if object is enabled, else false

Definition at line 248 of file persistency.c.

248  {
249 
250  when_null_trace(s_storage_dir, exit, DEBUG, "No persistency");
251  when_null(object, exit);
252 
253  amxc_string_t file_path;
254  amxc_string_init(&file_path, 0);
255  create_enabled_file_path(&file_path, object);
256 
257  const char* const file_path_cstr = amxc_string_get(&file_path, 0);
258 
259  SAH_TRACEZ_DEBUG(ME, "object='%s enable=%d", object, enable);
260 
261  if(enable) {
262  touch_file(file_path_cstr);
263  } else {
264  unlink(file_path_cstr);
265  }
266 
267  amxc_string_clean(&file_path);
268 
269 exit:
270  return;
271 }
static void create_enabled_file_path(amxc_string_t *const file_path, const char *const object)
Definition: persistency.c:234
static void touch_file(const char *const file)
Definition: persistency.c:219
#define SAH_TRACEZ_DEBUG(zone, format,...)
Definition: xpon_trace.h:115
#define ME
Definition: xpon_trace.h:78
Here is the call graph for this function:
Here is the caller graph for this function:

◆ persistency_get_folder()

const char* persistency_get_folder ( void  )

Get the folder for reboot persistent settings for this plugin.

It typically returns "/etc/config/tr181-xpon/".

Definition at line 210 of file persistency.c.

210  {
211  return s_storage_dir;
212 }
Here is the caller graph for this function:

◆ persistency_init()

void persistency_init ( void  )

Initialize the persistency part.

Ask the parser for the storage dir. If the parser returns NULL, take STORAGE_DIR as storage dir. Create the storage dir if it does not exist. Store the selected dir in s_storage_dir upon success, else set s_storage_dir to NULL.

Under normal circumstances the parser returns the storage dir, the dir exists, and this function sets s_storage_dir to that value.

The plugin must call this function once at startup.

mkdir() can not create all folders in a path at once. But we assume that all folders except for the last one in s_storage_dir exist. It's also unlikely we end up here. amxrt normally has already created the folder.

Definition at line 161 of file persistency.c.

161  {
162 
163  char* dir = get_storage_dir();
164  SAH_TRACEZ_DEBUG(ME, "dir='%s'", dir ? dir : "NULL");
165 
166  s_storage_dir = strdup(dir ? dir : STORAGE_DIR);
167 
168  when_null_trace(s_storage_dir, exit, ERROR,
169  "Failed to allocate mem for s_storage_dir");
170 
171  if(access(s_storage_dir, R_OK | W_OK | X_OK) == 0) {
172  goto exit;
173  }
174 
175  SAH_TRACEZ_DEBUG(ME, "Create %s", s_storage_dir);
176 
183  if(mkdir(s_storage_dir, 0777) == -1) {
184  SAH_TRACEZ_ERROR(ME, "Failed to create %s", s_storage_dir);
185  free(s_storage_dir);
186  s_storage_dir = NULL;
187  }
188 
189 exit:
190  free(dir);
191 }
static char * get_storage_dir(void)
Definition: persistency.c:127
static const char *const STORAGE_DIR
Definition: persistency.c:115
Here is the call graph for this function:
Here is the caller graph for this function:

◆ persistency_is_enabled()

bool persistency_is_enabled ( const char *const  object)

Return true if object identified by 'object' is enabled.

Parameters
[in]objectpath to object in DM being queried, e.g. "XPON.ONU.1".
Returns
true if object is enabled, else false.

Definition at line 280 of file persistency.c.

280  {
281 
282  bool rv = false;
283  when_null_trace(s_storage_dir, exit, DEBUG, "No persistency");
284  when_null(object, exit);
285 
286  amxc_string_t file_path;
287  amxc_string_init(&file_path, 0);
288  create_enabled_file_path(&file_path, object);
289 
290  const char* const file_path_cstr = amxc_string_get(&file_path, 0);
291 
292  rv = (access(file_path_cstr, F_OK) == 0) ? true : false;
293 
294  amxc_string_clean(&file_path);
295 
296 exit:
297  return rv;
298 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ touch_file()

static void touch_file ( const char *const  file)
static

Touch file if it does not exist yet.

Parameters
[in]filefile path

Definition at line 219 of file persistency.c.

219  {
220  /* Avoid writing the file if it already exists */
221  if(access(file, F_OK) == 0) {
222  SAH_TRACEZ_DEBUG2(ME, "%s already exists", file);
223  return;
224  }
225 
226  FILE* f = fopen(file, "w");
227  if(NULL == f) {
228  SAH_TRACEZ_ERROR(ME, "Failed to create '%s': %s", file, strerror(errno));
229  } else {
230  fclose(f);
231  }
232 }
#define SAH_TRACEZ_DEBUG2(zone, format,...)
Definition: xpon_trace.h:127
Here is the caller graph for this function:

Variable Documentation

◆ s_storage_dir

char* s_storage_dir = NULL
static

Definition at line 118 of file persistency.c.

◆ STORAGE_DIR

const char* const STORAGE_DIR = "/etc/config/tr181-xpon/"
static

Definition at line 115 of file persistency.c.