Code Search for Developers
 
 
  

leds.c from EmStar at Krugle


Show leds.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.
 *
 */
 

char leds_c_id[] = "$Id: leds.c,v 1.5 2004/09/14 17:01:26 girod Exp $";

#include <mod/mod_emtos.h>
#include <emview/emview.h>
#include <libmisc/misc.h>
#include <mote/emtos.h>

/* the global module variable */
static emview_module_t *mod = NULL;


char *emtos_viz_name(int index, char *suffix)
{
  char *name = emview_mod_inst_index(mod, index)->name;
  DECLARE_STATIC_BUF_RING(buf, 10, 256);
  if (suffix)
    sprintf(buf, "%s/%s/%s", "Emtos", name, suffix);
  else
    sprintf(buf, "%s/%s", "Emtos", name);
  return buf;
}

char *emtos_box_name(int index)
{ return emtos_viz_name(index, NULL); }

GQuark emtos_box_name_q(int index)
{ return g_quark_from_string(emtos_viz_name(index, NULL)); }


/*
 *  handle new status 
 */

static
int leds_handle_data(emview_device_t *dev, emproxy_reply_hdr_t *reply, 
		      emview_dev_node_t *node)
{
  emview_module_inst_t *inst = (emview_module_inst_t *)emview_get_device_opts(dev)->private_data;
  int i = inst->index;
  leds_status_t *msg;

  elog(LOG_DEBUG(10), "Got Leds data for node %d", emview_dn_get_node_id(node));
  
  /* copy the opp struct data */
  if (node->data) free(node->data);
  node->data = g_new0(leds_status_t, 1);
  memmove(node->data, reply->data, sizeof(leds_status_t));

  /* push data to core */
  msg = (leds_status_t *)node->data;  
  emview_update_source_int(emtos_viz_name(i, EMTOS_LEDS_RED), emview_dn_get_node_id(node), msg->red);
  emview_update_source_int(emtos_viz_name(i, EMTOS_LEDS_GREEN), emview_dn_get_node_id(node), msg->green);
  emview_update_source_int(emtos_viz_name(i, EMTOS_LEDS_YELLOW), emview_dn_get_node_id(node), msg->yellow);

  return 0;
}


static
int leds_timeout(emview_device_t *dev, emview_dev_node_t *node)
{
  elog(LOG_DEBUG(10), "Node %d timed out!", emview_dn_get_node_id(node));
  return 0;
}



static
int leds_config_device(emview_module_t *mod, emview_module_inst_t *instance,
		       char *device, node_id_t node)
{
  char buf[255];
  char *devname;
  int i = instance->index;
  
  emview_device_opts_t opts = {
    name: TOS_LEDS_STATUS_DEV,
    private_data: instance,
    proxy_string: buf,
    nominal_data_length: sizeof(leds_status_t),
    data_handler: leds_handle_data,
    node_timeout: leds_timeout,
    parent: mod
  };
  
  /* construct proxy string */
  devname = emtos_name(TOS_LEDS_STATUS_DEV, instance->name);
  sprintf(buf, "dev=%s:reread=5000", device ? device : devname);
  
  /* register device */
  instance->dev = emview_register_device(&opts);
  
  /* register sources */
  emview_register_source(instance->dev, emtos_viz_name(i, EMTOS_LEDS_RED), NULL);
  emview_register_source(instance->dev, emtos_viz_name(i, EMTOS_LEDS_GREEN), NULL);
  emview_register_source(instance->dev, emtos_viz_name(i, EMTOS_LEDS_YELLOW), NULL);

  return 0;
}


static
int leds_config_assign(emview_module_t *mod, emview_module_inst_t *inst,
		       node_id_t node, parser_state_t *ps)
{
  int i = inst->index;
  
  /*
   *  Register a GUI component
   */
  
  /* Install a new "mote" component for this node, if needed */
  emview_component_register(emtos_box_name_q(i), node, EMVIEW_COMP_TYPE_BOX);
  
  /*
   *  Register all our option classes.
   */

  /* LEDS class */
  emview_assign_source(node, emtos_box_name(i), EMVIEW_BOX_FLAG1, 
		       emtos_viz_name(i, EMTOS_LEDS_RED),
		       emtos_viz_name(i, EMTOS_LEDS_OPTION), EMVIEW_ENABLE);
  emview_assign_source(node, emtos_box_name(i), EMVIEW_BOX_FLAG3, 
		       emtos_viz_name(i, EMTOS_LEDS_GREEN),
		       emtos_viz_name(i, EMTOS_LEDS_OPTION), EMVIEW_ENABLE);
  emview_assign_source(node, emtos_box_name(i), EMVIEW_BOX_FLAG2, 
		       emtos_viz_name(i, EMTOS_LEDS_YELLOW),
		       emtos_viz_name(i, EMTOS_LEDS_OPTION), EMVIEW_ENABLE);
  emview_document_option_class
    (mod, emtos_viz_name(i, EMTOS_LEDS_OPTION), EMTOS_LEDS_OPTION, 
     "Display the state of the Emtos LEDs", NULL);		  

  return 0;
}


static
int leds_config_options(emview_module_t *mod, emview_module_inst_t *inst,
			node_id_t node, parser_state_t *ps)
{
  int i = inst->index;
  
  /*
   * parse enables and other config.. 
   */
  
  while (misc_parse_next_kvp(ps) >= 0) {
    
    if (strcmp(ps->key, "leds") == 0) {
      emview_option_class_set_enable
	(node, emtos_viz_name(i, EMTOS_LEDS_OPTION), 1);
      continue;
    }
    
    elog(LOG_WARNING, "Unexpected key: %s", ps->key);
  }
  return 0;
}


/*
 *  Main
 */


int leds_main(int *argc, char **argv)
{
  static emview_module_opts_t mod_opts = {
    name: EMTOS_LEDS_MODULE,
    description: "EmTOS 'LEDs' display",
    usage: "leds",
    config_new_instance: leds_config_device,
    config_assign: leds_config_assign,
    config_opts: leds_config_options  
  };

  /* register with emview and get context structure */
  mod = emview_register(&mod_opts);

  return 0;
}





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

  leds.c