Code Search for Developers
 
 
  

as_process.cc from EmStar at Krugle


Show as_process.cc syntax highlighted

#include "devel/as-emstar/ds.h"

__BEGIN_DECLS
#include <libdev/query_dev.h>
#include <libmisc/misc_buf.h>
__END_DECLS

typedef enum as_command_type {
  AS_INVALID = -1,
  AS_TASK
} as_command_t;

int as_process(query_context_t *q, char *command, size_t size,
	      buf_t *print, buf_t *bin)
{
  int     xmin_para = false;
  int     xmax_para = false;
  int     ymin_para = false;
  int     ymax_para = false;
  float   xmin, xmax, ymin, ymax;

  parser_state_t p_state = {
    input: command,
    input_len: size
  };
  Adaptive_Sampling *as;

  loc_world   upper_l, lower_r;
  as_command_t  command_type = AS_INVALID;
  int  ret  = -1;

  elog( LOG_ERR,  "as_process called, '%s'\n", command);

  //initialize them to negative value, suppose 
  //valid target coordinated are non-negative.    

  as = (Adaptive_Sampling *) qdev_data(q);

  while (misc_parse_next_kvp(&p_state) >= 0) {
    /* check for the key 'move' */
    if (strcmp("task", p_state.key) == 0) 
    {
	//FIXME
	command_type = AS_TASK;
    }

    if ( strcmp("xmin", p_state.key) == 0) {
      if (p_state.value) {
        xmin = (float) atof(p_state.value);
	xmin_para= true;
      }
    }

    if ( strcmp("xmax", p_state.key) == 0) {
      if (p_state.value) {
        xmax = (float) atof(p_state.value);
	xmax_para= true;
      }
    }

    if ( strcmp("ymin", p_state.key) == 0) {
      if (p_state.value) {
        ymin = (float) atof(p_state.value);
	ymin_para= true;
      }
    }

    if ( strcmp("ymax", p_state.key) == 0) {
      if (p_state.value) {
        ymax = (float) atof(p_state.value);
	ymax_para= true;
      }
    }

  }

  if ( (command_type == AS_TASK) && xmin_para && xmax_para && ymin_para && ymax_para )  
  {
    if  ( as->state == AS_IDLE )
    {

	upper_l.x = xmin;
	upper_l.y = ymin;
	lower_r.x = xmax;
	lower_r.y = ymax;

        as->state = AS_BUSY;
  	g_status_dev_notify( as->status_ref );

//temp
/*
as_one_frame_done( as );
return QUERY_NO_REPLY;
*/

elog(LOG_ERR, "receive TASK from TA\n" );
	if  ( as_restart_prep(as, upper_l, lower_r) )
	{
		elog(LOG_ERR, "the target region is too small, no samples\n");
		as_one_frame_done( as );
		return QUERY_NO_REPLY;
	}
     } else {

	elog( LOG_ERR, "Sorry, I am busy, send me command later.\n" );
  	buf_init(bin);
	bufcpy( bin, &ret, sizeof(int) );
  	bufprintf( print, "Sorry, I am busy, send me command later.\n" );
        return QUERY_DONE;
     }
  }  else  {

  	buf_init(bin);
	bufcpy( bin, &ret, sizeof(int) );
  	bufprintf( print, "invalid command, try again\n" );
        return QUERY_DONE;
  }

  return QUERY_NO_REPLY;
}

void  as_one_frame_done(Adaptive_Sampling *as)
{
  query_context_t  * context = as->query_ctx_ref;
  buf_t *print = qdev_curr_print(context);
  buf_t *bin = qdev_curr_bin(context);

  int  ret = 0; //success 

elog(LOG_ERR, "DONE in AS\n" );
  buf_init(bin);
  bufcpy( bin, &ret, sizeof(int) );
  bufprintf( print, "done\n" );
  qdev_reply(context, QUERY_DONE);
  
  as->state = AS_IDLE;
  g_status_dev_notify( as->status_ref );
}

int as_status_print(status_context_t *info, buf_t *buf)
{
  Adaptive_Sampling * as = (Adaptive_Sampling *) sd_data(info);

  bufprintf(buf, "Adaptive sampling is currently %s\n", (as->state == AS_IDLE) ? "idle": "busy" );
    
  return STATUS_MSG_COMPLETE;
}

int as_status_binary(status_context_t *info, buf_t *buf)
{
  Adaptive_Sampling * as = (Adaptive_Sampling *) sd_data(info);
  bufcpy(buf, &(as->state), sizeof(as_state_t));
  return STATUS_MSG_COMPLETE;
}




See more files for this project here

EmStar

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

  as-misc.cc
  as-restart.cc
  as-with-ta.cc
  as.cc
  as_process.cc
  move_n_sample.cc
  sim_sensor_client.cc
  socket_server.cc
  tcp_client.cc
  tcp_func.c