Indicates that the Watchdog program is created from the object file watchdog_proc.o (which is compiled from watchdog_proc.c)

In document Openrg Programmer Guide 5.5 LATEST (Page 76-80)

Development Tutorial

Line 10 Indicates that the Watchdog program is created from the object file watchdog_proc.o (which is compiled from watchdog_proc.c)

5.6.2 A Simple Example

To become familiar with running external application with OpenRG, you will start with a simple example.

1. Create the file watchdog_proc.c under the process directory, and type in the following:

rg/pkg/watchdog/process/watchdog_proc.c

#include <unistd.h>

#include <util/rg_print.h>

int main(void) {

console_printf("Hello World!\n");

while (1)

This simple program does not do much, however compiling, burning and running it will make you familiar with the development process. The program simply prints a "Hello World!" message, waits for 10 seconds and prints "I'm still alive.." repeatedly, every 10 seconds.

2. Compile the program and the image. This is done in the exact same method described in Stage 1 of the tutorial. Use 'make' from the root directory to compile, and transfer the image to the board using the Remote Update feature from the Web-based management.

3. Type the following at the CLI prompt:

OpenRG> system shell

BusyBox v1.01 (2005.09.07-07:38+0000) Built-in shell (lash) Enter 'help' for a list of built-in commands.

/ #

You have now entered the BusyBox shell, granting access to the system's file system (for more information, refer to Chapter 22). You can find all available applications under the bin directory:

init ping rmmod umount

4. The simple application at the top of this section is called 'watchdog', according to the application file name. Activate the program by typing the application name at the prompt:

/bin # watchdog Hello world!

I'm still alive..

5.6.3 The Watchdog Process

After becoming familiar with these surroundings, you are ready to continue with the Watchdog example. The Watchdog is an external application, that communicates with Main Task using OpenRG's local management API. During several short local management sessions, the application samples the Watchdog configuration file entries. The application continues its operation, using the Watchdog configuration file values as parameters. The Watchdog checks for a new margin time and ending message and sends a keep-alive signal accordingly.

5.6.3.1 Integration with OpenRG

Replace the contents of watchdog_proc.c with the following code:

#include <unistd.h>

set_t *rg_conf_set, *rg_conf_ram_set;

int is_disabled, cur_margin = DEFAULT_MARGIN, mt_margin;

msg_t cur_msg, mt_msg;

console_printf("Watchdog process control started...\n");

MZERO(cur_msg);

while (1) {

The following code opens a local management session, where the application communicates with Main Task, and uses its API (the command line interface) to retrieve data from the configuration database. It reads the entire 'watchdog' set in the persistent and volatile configuration databases.

/* Read Watchdog sections */

rg_conf_set = mgt_rg_conf_get(Swatchdog, 0);

rg_conf_ram_set = mgt_rg_conf_get(Swatchdog, 1);

In the volatile configuration database (rg_conf_ram), check whether the Watchdog process should be active or not, and copy the retrieved information into a local variable.

is_disabled = set_get_path_int(&rg_conf_ram_set, Sdisabled);

Copy information retrieved from the persistent database (using local management) into the corresponding local variables.

mt_margin = set_get_path_int(&rg_conf_set, Smargin);

strncpy(mt_msg.msg, set_get_path_strz(&rg_conf_set, Sboot_msg), MAX_BOOT_MSG);

Check to see if there was a change in the 'keep-alive' signal margin. In case a change occurred, communicate with the kernel module using an IOCTL thats sets the updated margin in the module accordingly.

/* Check new margin */

if (mt_margin && cur_margin != mt_margin) {

console_printf("Got new margin: [%d]\n", mt_margin);

cur_margin = mt_margin;

/* Send IOCTL that changes the margin in the kernel module */

openrg_module_ctrl(KOS_CDT_WATCHDOG, WATCHDOG_IOCTL_SIG_MARGIN, &mt_margin);

}

Check to see if there was a change in the 'Ending Message'. In case a change occured, communicate with the kernel module using a IOCTL thats sets the updated message in the module accordingly.

/* Check new boot message */

if (strncmp(cur_msg.msg, mt_msg.msg, MAX_BOOT_MSG)) {

console_printf("Got new boot message: [%s]\n", mt_msg.msg);

strncpy(cur_msg.msg, mt_msg.msg, MAX_BOOT_MSG);

/* Send IOCTL that changes the boot message in the kernel module */

openrg_module_ctrl(KOS_CDT_WATCHDOG, WATCHDOG_IOCTL_BOOT_MSG, &mt_msg);

}

The final portion of the application code sends a 'keep-alive' signal to the kernel module, as long as the is_disabled variable is false.

if (!is_disabled) {

/* Sending Keep-alive signal */

openrg_module_ctrl(KOS_CDT_WATCHDOG, WATCHDOG_IOCTL_KEEPALIVE, NULL);

}

Free the local allocated set structure, and wait for half of the Watchdog margin.

set_free(&rg_conf_set);

set_free(&rg_conf_ram_set);

sleep(cur_margin/2);

This termination code will never be called since it is outside of the while(1) infinite loop. It is added to prevent compilation warnings only.

}

return 0;

}

5.6.3.2 Running the Application with rg_system

To load the application during the boot sequence, you must use rg_system(), which runs an external process application from within OpenRG code (either in foreground or background). The function resides in libopenrg (#include <misc_funcs.h>), and is compiled by JMK_LIBS+=$(OPENRG_LIBS) which is a flag defined during 'make config' that includes all OpenRG libraries. The function's format is:

int rg_system(char *ext_cmd, int flags, pid_t *ret_pid);

Its parameters are:

ext_cmd The command to be executed.

int flags

SYSTEM_LOG Add a record about running the ext_cmd command to the system log.

SYSTEM_DAEMON Run the command in the background (do not wait until it exits).

ret_pid If not NULL, will contain the process ID of the executed command.

Its possible return values are:

exit status If SYSTEM_DAEMON is not set.

-2, -1, 0 If SYSTEM_DAEMON is set.

-2 If no child was spawned due to an error.

-1 If an error occurred after the child was spawned.

0 On success.

To use this function in order to load the Watchdog during the boot sequence, add the following code to rg/pkg/main/run_ext_proc.c:

pid_t ret_pid;

rg_system("/bin/watchdog", SYSTEM_LOG | SYSTEM_DAEMON, &ret_pid);

5.6.4 Using the Local Management Utilities

Accessing the Watchdog configuration from an external process is done by the local management, which uses Inter Process Communication (IPC). The mgt utilities should be linked with the Watchdog process.

1. The first step is to copy the local management wrapper files, mgt_utils.c and mgt_utils.h, from the rg/pkg/samples/tutorial/process directory to your working directory rg/pkg/

watchdog/process. As you can see, two configuration access functions are supported by the wrapper, mgt_rg_conf_get and mgt_rg_conf_set. In the Watchdog process, only the first one is used since information is only read from the Watchdog configuration database. If you need to update the configuration database in your application, use the set function to do so.

set_t *mgt_rg_conf_get(char *path);

int mgt_rg_conf_set(char *path, set_t **set);

mgt_rg_conf_get This function allocates the whole set in the persistent configuration file (rg_conf) or in the volatile configuration file (rg_conf_ram) located under 'path' Note that the returned set is a duplication of the original set, and therefore must be freed using the set_free function.

mgt_rg_conf_set This function replaces the whole set in the persistent configuration file (rg_conf) located under 'path' with the supplied set parameter.

2. In order to link the mgt utilities with the Watchdog process, you need to update the Makefile that builds the Watchdog external application. It should also compile the mgt utilities mgt_utils.c, and link with the appropriate libraries. You should replace the JMK_O_OBJS directive and add the JMK_LIBS directive in the Makefile you have created:

rg/pkg/watchdog/process/Makefile

1 JMK_O_OBJS=watchdog_proc.o mgt_utils.o 2

3 JMK_LIBS+=$(JMKE_BUILDDIR)/pkg/util/libmgt_client.a $(OPENRG_LIBS)

Following is a line-by-line explanation of the Watchdog process Makefile:

Line 1 Adds mgt_utils.o to the JMK_O_OBJS variable to be compiled and linked with

In document Openrg Programmer Guide 5.5 LATEST (Page 76-80)