Show mdiff_filter.c syntax highlighted
#include "emrun/emrun.h"
#include "libdev/packet_client.h"
#include "libdev/status_client.h"
#include "libmisc/misc.h"
#include "../src/Mdiff.h"
#include "mdiff_filter.h"
static int status_print(status_context_t *info, buf_t *buf);
static int status_binary(status_context_t *info, buf_t *buf);
static int md_pd_receive(void *pkt, ssize_t len, pd_client_context_t *pd_client);
static int md_filter_command(status_context_t * info, char * command, size_t buf_size);
static void send_priority(md_filter_state_t * state, uint8_t priority);
int main(int argc, char * argv[])
{
md_filter_state_t state = {
foo: 0
};
misc_init(&argc, argv, CVSTAG);
// setup status
{
status_dev_opts_t status_opts = {
device: {
devname: sim_path("/dev/mdiff_filter/status"),
device_info: &state
},
printable: status_print,
binary: status_binary,
write: md_filter_command
};
if (g_status_dev(&status_opts, &(state.status_ref)) < 0) {
elog(LOG_CRIT, "unable to create status device: %m");
exit(1);
}
}
// set up packet device client
{
pd_client_opts_t opts = {
devname: MDIFF_FILTER_PACKET_DEV,
data: &state,
receive: md_pd_receive
};
if (pd_client_open(&opts, &(state.pd_ref)) < 0) {
elog(LOG_CRIT, "can't open %s: %m", MDIFF_APP_PACKET_DEV);
exit(1);
}
}
// set up emrun
{
emrun_opts_t emrun_opts = {
data: &state
};
emrun_init(&emrun_opts);
}
g_status_dev_notify(state.status_ref);
g_main();
elog(LOG_ALERT, "Event system terminated abnormally.");
return 1;
}
static int status_print(status_context_t *info, buf_t *buf)
{
bufprintf(buf, "Hi\n");
return STATUS_MSG_COMPLETE;
}
static int status_binary(status_context_t *info, buf_t *buf)
{
md_filter_state_t * state = (md_filter_state_t*)sd_data(info);
bufcpy(buf,state,sizeof(md_filter_state_t));
return STATUS_MSG_COMPLETE;
}
static int md_pd_receive(void *pkt, ssize_t len, pd_client_context_t *pd_client)
{
MdMsg_t * msg = (MdMsg_t*)pkt;
md_filter_state_t * state = (md_filter_state_t*)pdc_data(pd_client);
elog(LOG_INFO, "md_pd_receive");
switch(msg->type)
{
case MdRecv:
elog(LOG_INFO, "Snooped a msg!!\n");
// send it back.
//++(((IntDiffMsg_t*)(msg->data))->m_uiPriority);
msg->type = MdRecvFromFilter;
pd_client_send(state->pd_ref, pkt, len);
break;
default:
elog(LOG_CRIT, "unknown msg type: %d", msg->type);
}
free(pkt);
return EVENT_RENEW;
}
static int md_filter_command(status_context_t * info, char * command, size_t buf_size)
{
uint8_t priority=0;
md_filter_state_t * state = (md_filter_state_t *)sd_data(info);
parser_state_t p_state = {
input: command,
input_len: buf_size
};
elog(LOG_INFO," ");
while (misc_parse_next_kvp(&p_state) >= 0) {
// check for the key 'start'
if (strcmp("priority", p_state.key) == 0) {
if (p_state.value)
priority = atoi(p_state.value);
}
}
if (priority) {
send_priority(state, priority);
}
return EVENT_RENEW;
}
static void send_priority(md_filter_state_t * state, uint8_t priority)
{
// craft a priority packet.
size_t length = sizeof(MdMsg_t) + sizeof(uint8_t);
MdMsg_t * msg = (MdMsg_t *)malloc(length);
elog(LOG_INFO," ");
memset(msg,0,length);
msg->type = MdSetPriority;
msg->length = sizeof(uint8_t);
*(uint8_t*)msg->data = priority;
// use the state to get the pd_context.
pd_client_send(state->pd_ref, msg, length);
// send packet.
free (msg);
}
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
Makefile
commands.txt
mdiff_filter.c
mdiff_filter.h
mdiff_test.c
mdiff_test.h