Directory of Sample Code

by The EmStar Team
2005-06-30

Site and Mailing List Search:

This is a collection of EmStar code samples that are very useful for cutting and pasting. Can't remember the usual arguments to an options structure or a callback function, and too lazy to look it up? Me too! So just cut and paste from these samples!

Contents:

Timers and Timing
Link Device Clients: Sending and Receving Packets
Status Devices
Query Device
Live Command Inputs to EmStar Processes
Status Clients
Client-configurable Status Devices
Locking Query Servers
Query Clients
Observing Neighbor and Routing Tables
Providing and Using Sensor Data
Creating a Socket Server
Using and Configuring a Serial Port
Parsing Command Line Options
Emstar Application Initialization

Timers and Timing

How to run timer:

$ obj.i686-linux/skeletons/timer
Sun Oct 10 12:03:45.445: cb_func: 4 more timeouts
Sun Oct 10 12:03:46.455: cb_func: 3 more timeouts
Sun Oct 10 12:03:47.465: cb_func: 2 more timeouts
Sun Oct 10 12:03:48.475: cb_func: 1 more timeouts
Sun Oct 10 12:03:49.485: cb_func: 0 more timeouts
Sun Oct 10 12:03:50.495: cb_func: No more timeouts
^C

Link Device Clients: Sending and Receving Packets

coming soon.. how to run this

Status Devices

How to run the triggered_status example program.

### Run trig_status
$ obj.i686-linux/skeletons/trig_status &
Sun Oct 10 12:04:58.307: main: Triggered status example starting

### Cat the device a few times.
### You should notice the status report changes every second
$ cat /dev/samples/trig
The current state is: Crash!
$ cat /dev/samples/trig
The current state is: Crash!
$ cat /dev/samples/trig
The current state is: Bam!

### Use echocat to see the state change periodically:
$ obj.i686-linux/bin/echocat -w /dev/samples/trig
The current state is: Bam!
The current state is: Bash!
The current state is: Crash!
The current state is: Boom!
Sun Oct 10 12:05:46.613: echocat_shutdown: echocat shutting down

### Use echocat to observe the output as binary:
$ obj.i686-linux/bin/echocat -w -b -H /dev/samples/trig
1097435158.700440: 4 bytes
   0000: 00 00 00 00                                      ....
1097435160.615427: 4 bytes
   0000: 01 00 00 00                                      ....
1097435162.629647: 4 bytes
   0000: 02 00 00 00                                      ....
1097435164.636950: 4 bytes
   0000: 03 00 00 00                                      ....
1097435166.646140: 4 bytes
   0000: 04 00 00 00                                      ....
Sun Oct 10 12:06:07.086: echocat_shutdown: echocat shutting down

Query Device

How to run the query_example program.

### Run query_example, no args.
$ obj.i686-linux/skeletons/query_example &
Sun Oct 10 12:07:14.147: main: Starting up query_example

### Then cat /dev/samples/query for more information
$ cat /dev/samples/query
Usage for this example query device.
Accepts commands:
   reply=<reply text>
   delay=<response delay in ms>

 try the following:
  echocat /dev/samples/query test
  echocat /dev/samples/query delay=2000
  echocat /dev/samples/query delay=2000:reply=this is my reply
  echocat /dev/samples/query " "

NOTE: echocat is in obj.<platform>/bin

### Try those suggestions
$ obj.i686-linux/bin/echocat /dev/samples/query test
Sun Oct 10 12:07:56.444: sample_process: In process callback.. command string is 'test'
Sun Oct 10 12:07:56.444: sample_process: unknown key: test
Immediate response: 'No reply requested'
Uptime is 42 seconds

$ obj.i686-linux/bin/echocat /dev/samples/query delay=2000
Sun Oct 10 12:08:33.114: sample_process: In process callback.. command string is 'delay=2000'
Sun Oct 10 12:08:33.114: sample_process: In process callback.. delaying for 2000 ms
Sun Oct 10 12:08:35.125: complete_transaction: In timer callback..
Delayed response (2000 ms): 'No reply requested'

$ obj.i686-linux/bin/echocat /dev/samples/query delay=2000:reply=this is my reply
Sun Oct 10 12:09:00.772: sample_process: In process callback.. command string is 'delay=2000:reply=this is my reply'
Sun Oct 10 12:09:00.772: sample_process: In process callback.. delaying for 2000 ms
Sun Oct 10 12:09:02.775: complete_transaction: In timer callback..
Delayed response (2000 ms): 'this is my reply'

$ obj.i686-linux/bin/echocat /dev/samples/query " "
Sun Oct 10 12:09:21.902: sample_process: In process callback.. command string is ' '
Sun Oct 10 12:09:21.902: sample_process: unknown key:
Immediate response: 'this is my reply'
Uptime is 127 seconds

Live Command Inputs to EmStar Processes

coming soon.. command device.. status write callback.. more parsing

Status Clients

coming soon..

Client-configurable Status Devices

coming soon...

Locking Query Servers

coming soon..

Query Clients

coming soon..

Observing Neighbor and Routing Tables

coming soon..

Providing and Using Sensor Data

coming soon..

Creating a Socket Server

The libevent library contains various helper functions for interfacing I/O to the EmStar event system. Among other things it provides a fully non-blocking sockets library that enables both creating TCP servers and clients. There is a sample program libevent/util/serial_server.c that is a very simple example of how to create a socket server. This program opens a serial port, configures it, and exposes the raw byte stream via a tcp server.

How to run serial_server:

$ obj.i686-linux/libevent/serial_server --device /dev/ttyS0 --port 8001 --baud 57600 &
Thu Jun 30 09:22:14.568 2005: main: serial_server (connected to '/dev/ttyS0', exposed on port 8001) starting...

Then, you can use telnet to connect to that port:

$ telnet localhost 8001
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
....now you can read/write to the serial port

This program supports multiple clients; all received data will be copied to each, but there is no locking to prevent writers from interleaving their data.

Looking at the code:

For further information:

Using and Configuring a Serial Port

There are several helper functions in libmisc to open and configure a serial port. The serial_server utility program provides a simple example of these functions. See Creating a Socket Server for instructions on running an interacting serial_server.

Parsing Command Line Option

EmStar provides some useful helper functions for parsing command line options and providing standard usage information. All EmStar programs support a certain set of standard command line options that are parsed by the call to misc_init(). These include options that configure various simulation parameters (optional node and group ID overrides) and a host of other standard options.

You can parse additional options after that call using a suite of functions in libmisc/misc_opt.h. These options parse "out" options, meaning that they remove them from the argument list. This means they can be parsed in any order and by several different non-coordinating components. There are various functions for parsing strings, "switches", integers, etc.

An example of the use of these functions can be found in serial_server. Instructions for running the program are above in Creating a Socket Server.

Emstar Application Initialization

When you write a new EmStar application, there is a standard form you use to construct the main() function. We will show this form using the example of serial_server (see instructions in Creating a Socket Server to run it):

Back to the Tutorials Page