The NV-Load callback function is a user callback that is invoked when the running configuration needs to be read from non-volatile storage. A configuration filespec is passed to this callback that contains the configuration that needs to be saved to non-volatile storage.
The following function template definition is used for NV-Load callback functions:
/* Typedef of the callback */ typedef status_t (*agt_nvload_fn_t) (ncx_display_mode_t *encoding, xmlChar **filespec);
The agt_register_local_nv_handler function is used to declare the NV-Load callback. The registration can be done during the Initialization Phase 2, after the running configuration has been loaded from the startup file.
extern status_t agt_register_local_nv_handler (agt_nvload_fn_t load_fn, agt_nvsave_fn_t store_fn);
Initialization function with the NV-Load callback registration may look as follows:
#define EXAMPLECONFIG_SPEC (const xmlChar *)"/tmp/example-config.xml" /******************************************************************** * FUNCTION interfaces_init2 * * initialize the server instrumentation library. * Initialization Phase 2 * *********************************************************************/ static status_t interfaces_init2 (void) { ... /* register NV-storage handler to load/save config * uncomment following to enable */ res = agt_register_local_nv_handler(nvload_callback, nvsave_callback); if (res != NO_ERR) { return res; } ... }
There is no cleanup function for this callback. The cleanup will be done automatically by the server.
The following example code illustrates how the NV-Load callback may look like.
/******************************************************************** * FUNCTION nvload_callback * * this callback is invoked when some config needs to be read * from non-volatile storage * * INPUTS: * encoding == address of encoding for the config * filespec == address of filespec containing the config that was loaded * * OUTPUTS: * *encoding == set to the enum for the encoding used in the config * *filespec == malloced filespec containing the config that was loaded * * RETURNS: * status; error indicates NV-load failed somehow * If return NO_ERR and *filespec == NULL then use the factory config * *********************************************************************/ static status_t nvload_callback (ncx_display_mode_t *encoding, xmlChar **filespec) { log_debug("\nEnter nvload_callback "); *filespec = NULL; *encoding = NCX_DISPLAY_MODE_XML; status_t res = NO_ERR; if (ncxmod_test_filespec(EXAMPLE_CONFIG_SPEC)) { /* file exists so copy the filespec */ *filespec = xml_strdup(EXAMPLE_CONFIG_SPEC); if (*filespec == NULL) { res = ERR_INTERNAL_MEM; } } return res; } /* nvload_callback */
Now, when some configuration needs to be read from Non-Volatile storage this callback function will try to find a file specification and copy it into the buffer.
If the callback returns NO_ERR and *filespec is empty then use the factory configuration. Only XML encoding is supported. The server will save the configuration only in XML.