Show sample_gw.c syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* A sample gateway program that hooks up to emproxy and outputs some junk
*/
#include <emproxy/emproxy.h>
#include <libmisc/misc.h>
#include <link/neighbor.h>
#include <emrun/emrun.h>
/*
* when the timer fires, emit a new message to emproxy
* this code is _inventing_ messages.. a real gateway would
* receive data from someplace and translate them
*/
int sample_gw_emit(void *data, int interval, g_event_t *event)
{
buf_t *output_buf;
int i;
int target_node;
static int max_target_node=0;
/* cycle thru nodes to generate something interesting */
static int nodeid = 0;
target_node = (nodeid % 10) + 100;
nodeid++;
elog(LOG_WARNING, "passing info for node %d\n", target_node);
/* if this is a new node, pass along the emview config info */
/* in real life this is more complex ;) */
if (target_node > max_target_node) {
max_target_node = target_node;
output_buf = buf_new();
/* NOTE: DO NOT sim_path the device name provided as an argument to "dev="
* but DO use sim_path when specifying the device name to emit_buf (later
* on ) */
bufprintf(output_buf,
"module=Link/Radio:dev=remote_mote:leds\n"
"module=Link/Neighbors:dev=remote_mote,/dev/link/remote_mote/neighbors:links=>80,80,1,blue,show:\n");
/* sim_path the emview_config device so that this will work in simulation */
emproxy_emit_buf_to_gw(sim_path("/dev/emrun/emview_config"), target_node, NULL, output_buf);
buf_free(output_buf);
}
/* build a neighbors message */
output_buf = buf_new();
for (i=0; i<3; i++) {
int nn = (random() % 10) + 100;
neighbor_t n = {
node_id: nn,
if_id: nn,
state: ACTIVE,
conn_from: 100,
conn_to: 100,
};
bufcpy(output_buf, (char*)&n, sizeof(n));
}
{
neighbor_t n = {};
bufcpy(output_buf, (char*)&n, sizeof(n));
}
/* NOTE: use sim_path HERE when providing the device name, however do NOT
* sim_path this device name when specifying the emview command! */
emproxy_emit_buf_to_gw(sim_path("/dev/link/remote_mote/neighbors"),
target_node, NULL, output_buf);
buf_free(output_buf);
return EVENT_RENEW;
}
static void gw_shutdown(void *data)
{
elog(LOG_NOTICE, "Sample GW service shutting down...");
exit(0);
}
int main(int argc, char *argv[])
{
/* emrun will trigger this callback to run on shutdown */
emrun_opts_t emrun_opts = {
shutdown: gw_shutdown, /* this function implements our shutdown handler */
data: NULL /* this pointer will be passed to our shutdown handler */
};
/* generic init */
misc_init(&argc, argv, CVSTAG);
/* parse args... */
/* advance past the parsed options */
argc -= optind;
argv += optind;
/* *** here we could process additional command line items if we cared to! *** */
/*
* here we could put some kind of event to read from a link device.
* for now we've just got a timer that emits some stuff
*/
/* set timer to emit stuff 1x per second */
g_timer_add(1000, sample_gw_emit, NULL, NULL, NULL);
/*
* Register with emrun, and GO
*/
emrun_init(&emrun_opts);
g_main();
/* this should never be reached */
elog(LOG_ALERT, "event system terminated abnormally");
return 1;
}
See more files for this project here
EmStar is a software system for developing and deploying wireless sensor networks involving Linux-based platforms. As the wireless sensor network community has attempted to deploy more complex designs---large-scale, long-lived systems that need self-organization and adaptivity---a number of difficult software design issues have arisen. Advances in software design have not kept pace with the capabilities of hardware. This is because designing for an adaptive, efficient, and useful sensor network has turned out to be surprisingly complex and difficult. EmStar is a Linux-based software framework, whose goal is to dramatically reduce this complexity, enabling work to be shared and reused, and simplifying and speeding the design of new sensor network applications.
Project homepage:
http://cvs.cens.ucla.edu/emstar/
Programming language(s): C,Shell Script
License: other
BUILD
rbsh.c
rechocat.c
sample_gw.c