System Manager Service Example

PART 3. ESF CODE EXAMPLES
System Manager Service

 

Example

·            Overview

·            Prerequisites

·            System Manager Service Example

Example

 

Overview

This example shows how to use the System Manager Service as well as create a bundle that uses two services rather than just one.  We will start with a new project that is very similar to the project used in the Hello World Using the ESF Log Service example.

 

In this example, you will learn how to

Use the System Manager Service and the ESF Logger Service in the same bundle

See the information provided by the System Manager Service

           

Prerequisites

·            Installing the Eclipse IDE

·            Integrating ESF Tooling and Projects into Eclipse

or

·            Installing ESF Tooling into Wind River Workbench

and

·            Using Working Sets

·            Hello World Using the ESF Log Service

 

System Manager Service Example

The System Manager (SM) Service is described here.  Reading about the SM Service before proceeding is suggested so you have a basic understanding of what the SM Service does.  Then begin by creating a new Plug-in Project in Eclipse.  The basic information on the project is:

 

·            Project (bundle) Name

com.esf.example.system.manager

 

·            Imported Services (to be added to the Automated Management of Dependencies list in the Eclipse Manifest Editor)

1.    org.eclipse.osgi

2.    org.eclipse.soda.sat.core

3.    com.esf.core.logger.service

4.    com.esf.core.system.manager.service

 

·            Activator Class Name (with Package Name)

com.esf.example.system.manager.bundle.Activator

 

·            Implementation Class Name (with Package Name)

com.esf.example.system.manager.SystemManagerExample

 

The following is the Activator code for this project.

 

package com.esf.example.system.manager.bundle;

 

import org.eclipse.soda.sat.core.framework.BaseBundleActivator;

 

import com.esf.core.logger.service.IEsfLoggerService;

import com.esf.core.system.manager.service.ISystemManagerService;

import com.esf.example.system.manager.SystemManagerExample;

 

public class Activator extends BaseBundleActivator {

      private SystemManagerExample systemManagerExample;

     

      protected void activate() {

            systemManagerExample = new SystemManagerExample();

            systemManagerExample.bind(getIEsfLoggerService(), getISystemManagerService());

      }

 

      protected void deactivate() {

            systemManagerExample.unbind();

            systemManagerExample = null;

      }

 

      private IEsfLoggerService getIEsfLoggerService() {

            return (IEsfLoggerService) getImportedService(IEsfLoggerService.SERVICE_NAME);

      }

 

      private ISystemManagerService getISystemManagerService() {

            return (ISystemManagerService) getImportedService(ISystemManagerService.SERVICE_NAME);

      }

     

      protected String[] getImportedServiceNames() {

            return new String[] {

                        IEsfLoggerService.SERVICE_NAME,

                        ISystemManagerService.SERVICE_NAME

            };

      }

}

 

Notice a few changes from the hello world example.  The first is that we have added a private method called getISystemManagerService() which gets the ISystemManagerService from the OSGi framework.  We then call this method from the call to SystemManagerExample.bind() to pass in references to both of our required services.  The final change was to getImportedServiceNames().  Again, this method is called by SAT to allow us to tell SAT what we require in order to start.  Since we are now dependent on two services, we must add both to the list.

 

Now we can look at the main implementation class as shown.

 

package com.esf.example.system.manager;

 

import java.util.ArrayList;

 

import com.esf.core.logger.service.IEsfLoggerService;

import com.esf.core.system.manager.service.EsfEthernetInterface;

import com.esf.core.system.manager.service.EsfSerialPort;

import com.esf.core.system.manager.service.ISystemManagerService;

 

public class SystemManagerExample {

 

      private static final String LABEL = "com.esf.example.system.manager.SystemManagerExample: ";

     

      private IEsfLoggerService esfLoggerService;

      private ISystemManagerService systemManagerService;

     

      public void bind(IEsfLoggerService esfLoggerService, ISystemManagerService systemManagerService) {

            this.esfLoggerService = esfLoggerService;

            this.systemManagerService = systemManagerService;

           

            displayProperties();

      }

     

      public void unbind() {

            this.systemManagerService = null;

            this.esfLoggerService = null;

      }

     

      private void displayProperties() {

            try {

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getCompany(): " + systemManagerService.getCompany());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getEntity(): " + systemManagerService.getEntity());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getEsfBaseBundleDirectory(): " + systemManagerService.getEsfBaseBundleDirectory());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getEsfConfigDirectory(): " + systemManagerService.getEsfConfigDirectory());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getEsfHome(): " + systemManagerService.getEsfHome());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getFileSeparator(): " + systemManagerService.getFileSeparator());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getJavaHome(): " + systemManagerService.getJavaHome());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getJavaProfile(): " + systemManagerService.getJavaProfile());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getJavaVmName(): " + systemManagerService.getJavaVmName());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getJavaVmVersion(): " + systemManagerService.getJavaVmVersion());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getOsArch(): " + systemManagerService.getOsArch());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getOsDistro(): " + systemManagerService.getOsDistro());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getOsDistroVersion(): " + systemManagerService.getOsDistroVersion());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getOsName(): " + systemManagerService.getOsName());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getOsVersion(): " + systemManagerService.getOsVersion());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getPlatform(): " + systemManagerService.getPlatform());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getPrimaryMacAddress(): " + systemManagerService.getPrimaryMacAddress());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getProductVersion(): " + systemManagerService.getProductVersion());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getProject(): " + systemManagerService.getProject());

                  esfLoggerService.logInfo(LABEL + "systemManagerService.getTemporaryConfigDirectory(): " + systemManagerService.getTemporaryConfigDirectory());

     

                  ArrayList comPorts = systemManagerService.getAllComPorts();

                  ArrayList ethernetInterfaces = systemManagerService.getAllEthernetInterfaces();

 

                  for(int i=0; i<comPorts.size(); i++) {

                        EsfSerialPort port = (EsfSerialPort) comPorts.get(i);

                        esfLoggerService.logInfo(LABEL + "systemManagerService.getComPorts().get(" + i + ").getUsbPort(): " + port.getUsbPort());

                        esfLoggerService.logInfo(LABEL + "systemManagerService.getComPorts().get(" + i + ").getDeviceNode(): " + port.getDeviceNode());             

                  }

                 

                  for(int i=0; i<ethernetInterfaces.size(); i++) {

                        esfLoggerService.logInfo(LABEL + "systemManagerService.getEthernetInterfaces().get(" + i + "): " + ((EsfEthernetInterface)ethernetInterfaces.get(i)).getIfaceName());

                  }

            } catch(Exception e) {

                  e.printStackTrace();

            }

      }

}

 

This class has the bind() and unbind() methods as our hello world example did.  However, we are also calling a new private method called displayProperties() from bind.  This means upon bundle startup, when bind is called, this method will also be run.  It is a simple method to show the usage of the System Managers API.  All methods return Strings except the getComPorts() method.  It returns EsfSerialPort Objects to keep the relationship of USB ports to device nodes one to one.  In Wind River Linux (so in turn ESF), serial ports can be dynamic.  In most embedded systems, the link between the physical/static USB port is more important than the dynamic device node name.  Using this ensures that in application code you can reference by USB port number so you will know what is connected on the other side of the USB-to-Serial port.

 

Once the class is fully written, you must remember to set the Activator and update the manifest with the proper dependencies.  It is easy to forget these steps.  The steps are shown if figures one and two.

 

Figure 1 Adding the Activator to the Manifest

 

Figure 2 Adding the Required Dependencies to the Manifest

 

The following is the output after placing the exported bundle into the /etc/jvm/esf/dropins/ directory and starting ESF.  This output will vary from system to system, as it should, to reflect the state of the system.

 

[INFO] 2010-07-26 16:45:32.491 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getCompany(): eurotech

[INFO] 2010-07-26 16:45:32.493 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getEntity(): eurotech

[INFO] 2010-07-26 16:45:32.493 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getEsfBaseBundleDirectory(): /opt/jvm/esf/bundlefiles/

[INFO] 2010-07-26 16:45:32.494 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getEsfConfigDirectory(): /opt/jvm/esf/configuration/.esf/

[INFO] 2010-07-26 16:45:32.494 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getEsfHome(): /opt/jvm/esf/

[INFO] 2010-07-26 16:45:32.495 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getFileSeparator(): /

[INFO] 2010-07-26 16:45:32.495 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getJavaHome(): /opt/jvm/

[INFO] 2010-07-26 16:45:32.496 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getJavaProfile(): harmony

[INFO] 2010-07-26 16:45:32.496 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getJavaVmName(): J9

[INFO] 2010-07-26 16:45:32.497 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getJavaVmVersion(): 2.4

[INFO] 2010-07-26 16:45:32.497 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getOsArch(): x86

[INFO] 2010-07-26 16:45:32.498 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getOsDistro(): wrl

[INFO] 2010-07-26 16:45:32.499 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getOsDistroVersion(): 3.0.2

[INFO] 2010-07-26 16:45:32.499 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getOsName(): Linux

[INFO] 2010-07-26 16:45:32.500 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getOsVersion(): 2.6.27.25-WR3.0ar_standard

[INFO] 2010-07-26 16:45:32.501 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getPlatform(): helios

[INFO] 2010-07-26 16:45:32.502 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getPrimaryMacAddress(): 00600c01a916

[INFO] 2010-07-26 16:45:32.503 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getProductVersion(): 1.0.0

[INFO] 2010-07-26 16:45:32.503 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getProject(): denali

[INFO] 2010-07-26 16:45:32.504 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getTemporaryConfigDirectory(): /tmp/.esf/

[INFO] 2010-07-26 16:45:32.505 - com.esf.example.system.manager.SystemManagerExample: systemManagerService.getEthernetInterfaces().get(0): eth0

 

The code for this example can be downloaded here.