Code Search for Developers
 
 
  

mh_devs.c from EmStar at Krugle


Show mh_devs.c syntax highlighted

/*
 *
 * Copyright (c) 2004 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.
 *
 */
 
#include "mh_i.h"


static
int mh_maybe_assign_index(source_t *s)
{
  mh_state_t *mh = (mh_state_t *)ssync_source_get_global_data(s);
  per_flow_state_t *pf = mh_maybe_init_source(s);

  /* if this source has not been assigned an index, assign one */
  if (pf->latest.flow_index == 0) {
    int index;
    
    for (index=1; index<256; index++) {
      if (mh_lookup_source_by_local_index(mh, index) == NULL)
	goto found;
    }
    
    elog(LOG_WARNING, "can't assign flow index!");
    return 0;

  found:
    pf->latest.flow_index = index;
    mh_schedule_flow_map_update(mh);
  }

  return pf->latest.flow_index;
}


per_flow_state_t *mh_maybe_init_source(source_t *s)
{
  per_flow_state_t *pf = (per_flow_state_t *)ssync_source_get_data(s);

  if (pf == NULL) {
    pf = g_new0(per_flow_state_t, 1);
    pf->parent = s;
    ssync_source_set_data(s, pf);
    mh_maybe_assign_index(s);

    /* init routing vectors if local */
    if (ssync_source_is_local(s)) {
      memset(pf->forward_vector, 1, sizeof(pf->forward_vector));
      mh_state_t *mh = (mh_state_t *)ssync_source_get_global_data(s);
      if (mh->flood_mode)
	memset(pf->force_flood, 1, sizeof(pf->force_flood));
    }
  }

  return pf;
}


source_t *mh_lookup_source_by_local_index(mh_state_t *mh, int index)
{
  source_t *ptr;
  for (ptr=ssync_sources_top(&(mh->ref->sources)); ptr;
       ptr=ssync_sources_next(ptr)) {  
    per_flow_state_t *pf = mh_maybe_init_source(ptr);
    if (pf->latest.flow_index == index)
      return ptr;
  }
  return NULL;
}
    

static
uint8_t *mh_get_routing_table(source_t *s)
{
  per_flow_state_t *pf = mh_maybe_init_source(s);
  return pf->forward_vector;
}


static
int mh_push_seqno(void *data, int interval, g_event_t *ev)
{
  source_t *src = (source_t *)data;
  mh_state_t *mh = (mh_state_t *)ssync_source_get_global_data(src);
  per_flow_state_t *pf = mh_maybe_init_source(src);
  
  g_event_destroy(ev);
  
  /* update refresh data from source info */
  src->log.refreshed_seqno = src->log.next_publish_seqno;
  src->log.refreshed_list_index = src->log.log_index & 0xFF;

  /* update and push */
  pf->refreshed_seqno = src->log.refreshed_seqno;
  pf->refreshed_log_index = src->log.refreshed_list_index;    
  mh_net_schedule_reeval(mh);

  return TIMER_DONE;
}


static
void mh_reset_seqno_timer(source_t *src)
{
  per_flow_state_t *pf = mh_maybe_init_source(src);

  /* reset timer */
  g_event_destroy(pf->seqno_update_timer);
  g_timer_add(MH_SEQNO_UPDATE_HOLDOFF, mh_push_seqno, src, NULL, 
	      &(pf->seqno_update_timer));
}


static
int mh_publish(source_t *src)
{
  mh_state_t *mh = (mh_state_t *)ssync_source_get_global_data(src);
  
  /* force init */
  mh_maybe_init_source(src);

  /* update the local source's refresh info */
  if (ssync_source_is_local(src)) {
    mh_reset_seqno_timer(src);
  }

  mh_net_schedule_reeval(mh);

  return 0;
}


void mh_devs_init(mh_state_t *mh, int *argc, char **argv)
{
  state_sync_dev_opts_t opts = {
    version: SSYNC_RETRANS_V2,
    source_update_cb: mh_publish,
    new_source_cb: mh_publish,
    forward_vector_cb: mh_get_routing_table,
    prefix: SSYNC_MULTIHOP_PREFIX,
    cb_data: mh,
    no_proactive: misc_parse_out_switch(argc, argv, "no_proactive", 0)
  };
  
  /* start up devices */
  ssync_dev_new(&opts, &(mh->ref));

}





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

  mh_devs.c
  mh_flow_table.c
  mh_i.h
  mh_main.c
  mh_net.c