• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C++ random_rand函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中random_rand函数的典型用法代码示例。如果您正苦于以下问题:C++ random_rand函数的具体用法?C++ random_rand怎么用?C++ random_rand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了random_rand函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: recv

/*---------------------------------------------------------------------------*/
static void
recv(struct broadcast_conn *bc, const rimeaddr_t *from)
{
    struct trickle_conn *c = (struct trickle_conn *)bc;
    uint16_t seqno = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);

    PRINTF("%d.%d: trickle recv seqno %d from %d.%d our %d data len %d channel %d\n",
           rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
           seqno,
           from->u8[0], from->u8[1],
           c->seqno,
           packetbuf_datalen(),
           packetbuf_attr(PACKETBUF_ATTR_CHANNEL));

    if(seqno == c->seqno)
    {
        /*    c->cb->recv(c);*/
        ++c->duplicates;
    }
    else if(SEQNO_LT(seqno, c->seqno))
    {
        c->interval_scaling = 0;
        send(c);
    }
    else     /* hdr->seqno > c->seqno */
    {
#if CONTIKI_TARGET_NETSIM
        /*    ether_set_line(from->u8[0], from->u8[1]);*/
#endif /* CONTIKI_TARGET_NETSIM */
        c->seqno = seqno;
        /* Store the incoming data in the queuebuf */
        if(c->q != NULL)
        {
            queuebuf_free(c->q);
        }
        c->q = queuebuf_new_from_packetbuf();
        c->interval_scaling = 0;
        reset_interval(c);
        ctimer_set(&c->first_transmission_timer, random_rand() % c->interval,
                   send, c);
        c->cb->recv(c);
    }
}
开发者ID:aiss83,项目名称:nucbit,代码行数:44,代码来源:trickle.c


示例2: handler_802154_frame_received

/*---------------------------------------------------------------------------*/
int
handler_802154_frame_received(frame802154_t *frame)
{
  if(answer_beacon_requests &&
     frame->fcf.frame_type == FRAME802154_CMDFRAME &&
     frame->payload[0] == FRAME802154_BEACONREQ) {
    others_beacon_reply = 0;
    ctimer_set(NULL, &beacon_send_timer,
               CLOCK_SECOND / 16 +
               (CLOCK_SECOND * (random_rand() & 0xff)) / 0x200,
               &handle_beacon_send_timer, NULL);
    return 1;
  }
  if(frame->fcf.frame_type == FRAME802154_BEACONFRAME) {
    HANDLER_802154_STAT(handler_802154_stats.beacons_received++);
    handle_beacon(frame);
    return 1;
  }
  return 0;
}
开发者ID:CurieBSP,项目名称:zephyr,代码行数:21,代码来源:handler-802154.c


示例3: PROCESS_THREAD

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(multicast_example_process, ev, data)
{
  PROCESS_BEGIN();

  /* Create a linkl-local multicast addresses. */
  uip_ip6addr(&addr, 0xff02, 0, 0, 0, 0, 0, 0x1337, 0x0001);

  /* Join local group. */
  if(uip_ds6_maddr_add(&addr) == NULL) {
    printf("Error: could not join local multicast group.\n");
  }

  /* Register UDP socket callback */
  udp_socket_register(&s, NULL, receiver);

  /* Bind UDP socket to local port */
  udp_socket_bind(&s, PORT);

  /* Connect UDP socket to remote port */
  udp_socket_connect(&s, NULL, PORT);

  while(1) {

    /* Set up two timers, one for keeping track of the send interval,
       which is periodic, and one for setting up a randomized send time
       within that interval. */
    etimer_set(&periodic_timer, SEND_INTERVAL);
    etimer_set(&send_timer, (random_rand() % SEND_INTERVAL));

    PROCESS_WAIT_UNTIL(etimer_expired(&send_timer));

    printf("Sending multicast\n");
    udp_socket_sendto(&s,
                      "hello", 6,
                      &addr, PORT);

    PROCESS_WAIT_UNTIL(etimer_expired(&periodic_timer));
  }

  PROCESS_END();
}
开发者ID:ADVANSEE,项目名称:mist,代码行数:42,代码来源:udp-multicast.c


示例4: new_dio_interval

static void
new_dio_interval(rpl_dag_t *dag)
{
  uint32_t time;

  /* TODO: too small timer intervals for many cases */
  time = 1UL << dag->dio_intcurrent;

  /* Convert from milliseconds to CLOCK_TICKS. */
  time = (time * CLOCK_SECOND) / 1000;

  dag->dio_next_delay = time;

  /* random number between I/2 and I */
  time = time >> 1;
  time += (time * random_rand()) / RANDOM_RAND_MAX;

  /*
   * The intervals must be equally long among the nodes for Trickle to
   * operate efficiently. Therefore we need to calculate the delay between
   * the randomized time and the start time of the next interval.
   */
  dag->dio_next_delay -= time;
  dag->dio_send = 1;

#if RPL_CONF_STATS
  /* keep some stats */
  dag->dio_totint++;
  dag->dio_totrecv += dag->dio_counter;
  ANNOTATE("#A rank=Xu.Xu(Xu),stats=Xd Xd Xd Xd,color=Xs");//, DAG_RANK(dag->rank, dag), (10 * (dag->rank % dag->min_hoprankinc)) / dag->min_hoprankinc, dag->version, dag->dio_totint, dag->dio_totsend, dag->dio_totrecv,dag->dio_intcurrent, dag->rank == ROOT_RANK(dag) ? "BLUE" : "ORANGE");
#endif /* RPL_CONF_STATS */

  /* reset the redundancy counter */
  dag->dio_counter = 0;

  /* schedule the timer */
  PRINTF("RPL: Scheduling DIO timer Xlu ticks in future (Interval)");
  PRINTF_DEC(time);
  ctimer_set(&dag->dio_timer, time, &handle_dio_timer, dag);
}
开发者ID:bearxiong99,项目名称:Arduino-IPv6Stack,代码行数:40,代码来源:rpl_timers.c


示例5: assemble_data

/**
 * subscribe node will assemble the data according to the conditions
 */
void assemble_data(){
    uint8_t value = random_rand()%10;
    assemble_total+=value;
    assemble_count++;
    if(value>max){
        max = value;
    }
    if(assemble_count>= local_node.assemble_count){
        Runicast_msg runicast_msg;
        runicast_msg.native_sen_type = local_node.sen_type;
        runicast_msg.seqno = clct_data_seqno;
        //runicast_msg.timestamp = CLOCK_SECOND;
        if(local_node.condition == MAX){
            runicast_msg.value = max;
        }else if(local_node.condition == AVERAGE ){
            runicast_msg.value = (assemble_total)/(assemble_count);
        }
        rimeaddr_copy(&runicast_msg.last_hop,&rimeaddr_node_addr);
        rimeaddr_copy(&runicast_msg.source,&rimeaddr_node_addr);
        //add_to_RU_list(runicast_msg);

        if(!runicast_is_transmitting(&runicast)){
            printf("[PUBLISH] publish data value->%u seqno %u\n",runicast_msg.value,runicast_msg.seqno);
            //single_publish(&runicast,runicast_msg);
            printf("[RU] timestamp %d seqno %u last hop address %u.%u\n", runicast_msg.timestamp,runicast_msg.seqno,runicast_msg.last_hop.u8[0],runicast_msg.last_hop.u8[1]);
            packetbuf_copyfrom(&runicast_msg, sizeof(Runicast_msg));
            runicast_send(&runicast,&find_best_route()->rt_entry.next_hop,MAX_RETRANSMISSIONS);
            packetbuf_clear();
        }


        if(clct_data_seqno>254){
            clct_data_seqno = 0;
        }
        clct_data_seqno++;
        assemble_count = 0;
        assemble_total = 0.0f;
        max = 0.0f;
    }
}
开发者ID:xizhejiang,项目名称:Contiki-Wireless-Sensor-Network,代码行数:43,代码来源:Light.c


示例6: uip_ds6_init

/*---------------------------------------------------------------------------*/
void
uip_ds6_init(void)
{

  uip_ds6_neighbors_init();
  uip_ds6_route_init();

  PRINTF("Init of IPv6 data structures\n");
  PRINTF("%u neighbors\n%u default routers\n%u prefixes\n%u routes\n%u unicast addresses\n%u multicast addresses\n%u anycast addresses\n",
     NBR_TABLE_MAX_NEIGHBORS, UIP_DS6_DEFRT_NB, UIP_DS6_PREFIX_NB, UIP_DS6_ROUTE_NB,
     UIP_DS6_ADDR_NB, UIP_DS6_MADDR_NB, UIP_DS6_AADDR_NB);
  memset(uip_ds6_prefix_list, 0, sizeof(uip_ds6_prefix_list));
  memset(&uip_ds6_if, 0, sizeof(uip_ds6_if));
  uip_ds6_addr_size = sizeof(struct uip_ds6_addr);
  uip_ds6_netif_addr_list_offset = offsetof(struct uip_ds6_netif, addr_list);

  /* Set interface parameters */
  uip_ds6_if.link_mtu = UIP_LINK_MTU;
  uip_ds6_if.cur_hop_limit = UIP_TTL;
  uip_ds6_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
  uip_ds6_if.reachable_time = uip_ds6_compute_reachable_time();
  uip_ds6_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
  uip_ds6_if.maxdadns = UIP_ND6_DEF_MAXDADNS;

  uip_ds6_set_lladdr(&uip_lladdr);

#if UIP_CONF_ROUTER
#if UIP_ND6_SEND_RA
  stimer_set(&uip_ds6_timer_ra, 2);     /* wait to have a link local IP address */
#endif /* UIP_ND6_SEND_RA */
#else /* UIP_CONF_ROUTER */
  etimer_set(&uip_ds6_timer_rs,
             random_rand() % (UIP_ND6_MAX_RTR_SOLICITATION_DELAY *
                              CLOCK_SECOND),
	     &tcpip_process);
#endif /* UIP_CONF_ROUTER */
  etimer_set(&uip_ds6_timer_periodic, UIP_DS6_PERIOD, &tcpip_process);

  return;
}
开发者ID:CurieBSP,项目名称:zephyr,代码行数:41,代码来源:uip-ds6.c


示例7: generate_random_pan_id_and_aes_key

void generate_random_pan_id_and_aes_key(void) {
	/* PAN ID and AES key is only generated once at first startup */
	if (eeprom_read_byte ((const void *)EE_FIRST_RUN_FLAG))
	{
		uint8_t key[16];
		//AES key
		rf212_generate_key(key);
		eeprom_write_block(key, (void *)EE_ENCRYPTION_KEY, EE_ENCRYPTION_KEY_SIZE);

		//PAN ID
		uint16_t rand_pan_id = 0xFFFF;
		//init random number generator with first 2 bytes of AES key
		random_init(eeprom_read_word ((const void *)EE_ENCRYPTION_KEY));
		//generate random pan_id which is not the broadcast_pan_id and not the bootloader/provisioning pan_id
		while (rand_pan_id == 0xFFFF || rand_pan_id == 0x0001)
		{
			rand_pan_id = random_rand();
		}
		eeprom_write_word ((void *)EE_PAN_ID, rand_pan_id);
		eeprom_write_byte ((void *)EE_FIRST_RUN_FLAG, 0x00);
	}
}
开发者ID:C3MA,项目名称:hexabus,代码行数:22,代码来源:contiki-hexabus-main.c


示例8: on_interval_expired

/* Corresponds to Rule 6 of Trickle */
static void
on_interval_expired(void *ptr)
{
    clock_time_t half_interval_size;

    if(trickle_doublings < IMAX) {
        trickle_doublings++;
        PRINTF("akes-trickle: Doubling interval size\n");
    }

    half_interval_size = interval_size() / 2;
    new_nbrs_count = 0;
    counter = 0;

    ctimer_set(&trickle_timer,
               half_interval_size + ((half_interval_size * random_rand()) / RANDOM_RAND_MAX),
               on_timeout,
               NULL);
    PRINTF("akes-trickle: I=%lus t=%lus\n",
           interval_size()/CLOCK_SECOND,
           trickle_timer.etimer.timer.interval/CLOCK_SECOND);
}
开发者ID:kkrentz,项目名称:contiki,代码行数:23,代码来源:akes-trickle.c


示例9: PROCESS_THREAD

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(collectd_process, ev, data)
{
	static struct etimer period_timer, wait_timer;
	PROCESS_BEGIN();

	collectd_conf_init(&collectd_conf);

	/* new connection with remote host */
	client_conn = udp_new(NULL, UIP_HTONS(0), NULL);
	udp_bind(client_conn, UIP_HTONS(COLLECTD_CLIENT_PORT));

	PRINTF("Created a connection with the server ");

	/* Send a packet every 60-62 seconds. */
	etimer_set(&period_timer, CLOCK_SECOND * DEFAULT_UPDATE_PERIOD);

	while(1) {
		PROCESS_WAIT_EVENT();
		//send update (collected data)
		if(ev == PROCESS_EVENT_TIMER) {
			if (data == &period_timer) {
				etimer_reset(&period_timer);		//TODO: reset the period from collectd_conf.update freq
				if (collectd_conf.send_active == SEND_ACTIVE_YES)
					etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT));

			} else if(data == &wait_timer) {
				/* Time to send the data */
				PRINTF("Time to send the data\n");
				collectd_common_send(client_conn, &collectd_conf);
			}
		}
		//receive a request (here, expected a request to begin to send update)
		if(ev == tcpip_event) {
			collectd_udp_handler();
		}
	}

	PROCESS_END();
}
开发者ID:anhquang,项目名称:contiki-vmote,代码行数:40,代码来源:collectd.c


示例10: receiver

/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
  printf("Data (%d) received on port %d from port %d with length %d\n", 
	 *data, receiver_port, sender_port, datalen);

#if RANDOM_TOKEN_ERROR
  if (0 == random_rand() % RANDOM_ERROR) {
    // this is written just to introduce random error to the algorithm
    printf("RANDOM Error Occurred\n");
    token = 1;
  }
#endif

  if ( (node_id - ((*data) % NUM_MOTES)) == 1)
    token = 1;
}
开发者ID:kincki,项目名称:contiki,代码行数:24,代码来源:broadcast-example.c


示例11: uip_ds6_send_ra_periodic

/*---------------------------------------------------------------------------*/
void
uip_ds6_send_ra_periodic(void)
{
  if(racount > 0) {
    /* send previously scheduled RA */
    uip_nd6_ra_output(NULL);
    PRINTF("Sending periodic RA\n");
  }

  rand_time = UIP_ND6_MIN_RA_INTERVAL + random_rand() %
    (uint16_t) (UIP_ND6_MAX_RA_INTERVAL - UIP_ND6_MIN_RA_INTERVAL);
  PRINTF("Random time 1 = %u\n", rand_time);

  if(racount < UIP_ND6_MAX_INITIAL_RAS) {
    if(rand_time > UIP_ND6_MAX_INITIAL_RA_INTERVAL) {
      rand_time = UIP_ND6_MAX_INITIAL_RA_INTERVAL;
      PRINTF("Random time 2 = %u\n", rand_time);
    }
    racount++;
  }
  PRINTF("Random time 3 = %u\n", rand_time);
  stimer_set(&uip_ds6_timer_ra, rand_time);
}
开发者ID:ChristianKniep,项目名称:hexabus,代码行数:24,代码来源:uip-ds6.c


示例12: servreg_hack_register

/*---------------------------------------------------------------------------*/
void
servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr)
{
  static servreg_hack_item_t *t;
  static struct servreg_hack_registration *r;
  /* Walk through list, see if we already have a service ID
     registered. If not, allocate a new registration and put it on our
     list. If we cannot allocate a service registration, we reuse one
     from the service registrations made by others. */

  servreg_hack_init();

  for(t = list_head(own_services);
      t != NULL;
      t = list_item_next(t)) {
    if(servreg_hack_item_id(t) == id) {
      return;
    }
  }

  r = memb_alloc(&registrations);
  if(r == NULL) {
    printf("servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
    return;
  }
  r->id = id;
  r->seqno = 1;
  uip_ipaddr_copy(&r->addr, addr);
  timer_set(&r->timer, LIFETIME / 2);
  list_push(own_services, r);


  PROCESS_CONTEXT_BEGIN(&servreg_hack_process);
  etimer_set(&sendtimer, random_rand() % (NEW_REG_TIME));
  PROCESS_CONTEXT_END(&servreg_hack_process);

}
开发者ID:EmuxEvans,项目名称:contiki-cc2530eb,代码行数:38,代码来源:servreg-hack.c


示例13: run_trickle

/*---------------------------------------------------------------------------*/
static int
run_trickle(struct trickle_conn *c)
{
  clock_time_t interval;
  PT_BEGIN(&c->pt);

  while(1) {
    interval = c->interval << c->interval_scaling;
    set_timer(c, &c->interval_timer, interval);
    set_timer(c, &c->t, interval / 2 + (random_rand() % (interval / 2)));

    c->duplicates = 0;
    PT_YIELD(&c->pt); /* Wait until listen timeout */
    if(c->duplicates < DUPLICATE_THRESHOLD) {
      send(c);
    }
    PT_YIELD(&c->pt); /* Wait until interval timer expired. */
    if(c->interval_scaling < INTERVAL_MAX) {
      c->interval_scaling++;
    }
  }
  
  PT_END(&c->pt);
}
开发者ID:Inscribe,项目名称:msp430xf1611,代码行数:25,代码来源:trickle.c


示例14: ipolite_send

/*---------------------------------------------------------------------------*/
int
ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
{
  if(c->q != NULL) {
    /* If we are already about to send a packet, we cancel the old one. */
    PRINTF("%d.%d: ipolite_send: cancel old send\n",
	   linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
    queuebuf_free(c->q);
    c->q = NULL;
    ctimer_stop(&c->t);
  }
  c->dups = 0;
  c->hdrsize = hdrsize;
  if(interval == 0) {
    PRINTF("%d.%d: ipolite_send: interval 0\n",
	   linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
    if(broadcast_send(&c->c)) {
      if(c->cb->sent) {
	c->cb->sent(c);
      }
      return 1;
    }

  } else {
    c->q = queuebuf_new_from_packetbuf();
    if(c->q != NULL) {
      ctimer_set(&c->t,
		 interval / 2 + (random_rand() % (interval / 2)),
		 send, c);
      return 1;
    }
    PRINTF("%d.%d: ipolite_send: could not allocate queue buffer\n",
	   linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
  }
  return 0;
}
开发者ID:Anagalcala,项目名称:contiki-upstream-unstable,代码行数:37,代码来源:ipolite.c


示例15: storage_generate_file

char *
storage_generate_file(char *prefix, unsigned long size)
{
  static char filename[ATTRIBUTE_NAME_LENGTH + sizeof(".ffff")];
#if !DB_FEATURE_COFFEE
  int fd;
#endif

  snprintf(filename, sizeof(filename), "%s.%x", prefix,
           (unsigned)(random_rand() & 0xffff));

#if DB_FEATURE_COFFEE
  PRINTF("DB: Reserving %lu bytes in %s\n", size, filename);
  if(cfs_coffee_reserve(filename, size) < 0) {
    PRINTF("DB: Failed to reserve\n");
    return NULL;
  }
  return filename;
#else
  fd = cfs_open(filename, CFS_WRITE);
  cfs_close(fd);
  return fd < 0 ? NULL : filename;
#endif /* DB_FEATURE_COFFEE */
}
开发者ID:1uk3,项目名称:contiki,代码行数:24,代码来源:storage-cfs.c


示例16: uip_ds6_addr_add

/*---------------------------------------------------------------------------*/
uip_ds6_addr_t *
uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
{
  if(uip_ds6_list_loop
     ((uip_ds6_element_t *)uip_ds6_if.addr_list, UIP_DS6_ADDR_NB,
      sizeof(uip_ds6_addr_t), ipaddr, 128,
      (uip_ds6_element_t **)&locaddr) == FREESPACE) {
    locaddr->isused = 1;
    uip_ipaddr_copy(&locaddr->ipaddr, ipaddr);
    locaddr->type = type;
    if(vlifetime == 0) {
      locaddr->isinfinite = 1;
    } else {
      locaddr->isinfinite = 0;
      stimer_set(&(locaddr->vlifetime), vlifetime);
    }
#if UIP_ND6_DEF_MAXDADNS > 0
    locaddr->state = ADDR_TENTATIVE;
    timer_set(&locaddr->dadtimer,
              random_rand() % (UIP_ND6_MAX_RTR_SOLICITATION_DELAY *
                               CLOCK_SECOND));
    locaddr->dadnscount = 0;
#else /* UIP_ND6_DEF_MAXDADNS > 0 */
#if UIP_CONF_6LN || UIP_CONF_6LR
    locaddr->state = uip_is_addr_link_local(ipaddr) ?
      ADDR_PREFERRED : ADDR_TENTATIVE;
#else /* UIP_CONF_6LN || UIP_CONF_6LR */
    locaddr->state = ADDR_PREFERRED;
#endif /* UIP_CONF_6LN || UIP_CONF_6LR */
#endif /* UIP_ND6_DEF_MAXDADNS > 0 */
    uip_create_solicited_node(ipaddr, &loc_fipaddr);
    uip_ds6_maddr_add(&loc_fipaddr);
    return locaddr;
  }
  return NULL;
}
开发者ID:sdefauw,项目名称:contiki,代码行数:37,代码来源:uip-ds6.c


示例17: PROCESS_THREAD


//.........这里部分代码省略.........
          goto LEVL;
#endif
        }

        pch = strtok (NULL, ".");
        if(strncmp(pch, "x", 1) != 0) {
          r = atoi(pch);
          multicast_flag = 0;
        }
        else{
          r = 0;
          multicast_flag = 1;
#if SCENARIO
          goto ROM;
#endif
        }

        pch = strtok (NULL, ".");
        if(strncmp(pch, "x", 1) != 0) {
          sid = atoi(pch);
          multicast_flag = 0;
        }
        else{
          sid = 0;
          multicast_flag = 1;
#if SCENARIO
          goto SENSR;
#endif
        }
        goto SEND_QR;
#if SCENARIO
BUILD:  for(b=1;b<=3;b++)
         for(f=1;f<=3;f++)
          for(r=1;r<=3;r++)
            for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;

LEVL:      for(f=1;f<=3;f++)
              for(r=1;r<=3;r++)
                for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;

ROM:          for(r=1;r<=3;r++)
                for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;

SENSR:          for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;
        
#endif    
SEND_QR:                query_send_sink(b,f,r,sid);
JUMP:{}

      } else if(strncmp(line, "Region-",7) == 0){   //Region of the node
        strcpy(reg, line+8);

        if(strncmp(reg, "B", 1) == 0){
          region = BUILDING;
        }
        else if(strncmp(reg, "L", 1) == 0){
          region = LEVEL;
        }
        else if(strncmp(reg, "R", 1) == 0){
          region = ROOM;
        }
        else if(strncmp(reg, "S", 1) == 0){
          region = SENSOR;
        }
      }
      else {
        //printf("unhandled command: %s\n", line);
      }
    }
    if(ev == PROCESS_EVENT_TIMER) {
      if(data == &period_timer) {
        etimer_reset(&period_timer);
        etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT));
      } else if(data == &wait_timer) {
        if(send_active) {
          /* Time to send the data */
          /* Broadcast its own region rank */
          if( region == ROOM )          
            broadcast_identity(ROOM);
          else if ( region == BUILDING )
            broadcast_identity(BUILDING);
          else if ( region == LEVEL )
            broadcast_identity(LEVEL);
          else if ( region == SENSOR )
            broadcast_identity(SENSOR);
        }
      }
    }
  }

  PROCESS_END();
}
开发者ID:engalex,项目名称:652Final,代码行数:101,代码来源:collect-common.c


示例18: send_packet

/*---------------------------------------------------------------------------*/
static void
send_packet(mac_callback_t sent, void *ptr)
{
  struct rdc_buf_list *q;
  struct neighbor_queue *n;
  static uint8_t initialized = 0;
  static uint16_t seqno;
  const rimeaddr_t *addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);

  if(!initialized) {
    initialized = 1;
    /* Initialize the sequence number to a random value as per 802.15.4. */
    seqno = random_rand();
  }

  if(seqno == 0) {
    /* PACKETBUF_ATTR_MAC_SEQNO cannot be zero, due to a pecuilarity
       in framer-802154.c. */
    seqno++;
  }
  packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, seqno++);

  /* Look for the neighbor entry */
  n = neighbor_queue_from_addr(addr);
  if(n == NULL) {
    /* Allocate a new neighbor entry */
    n = memb_alloc(&neighbor_memb);
    if(n != NULL) {
      /* Init neighbor entry */
      rimeaddr_copy(&n->addr, addr);
      n->transmissions = 0;
      n->collisions = 0;
      n->deferrals = 0;
      /* Init packet list for this neighbor */
      LIST_STRUCT_INIT(n, queued_packet_list);
      /* Add neighbor to the list */
      list_add(neighbor_list, n);
    }
  }

  if(n != NULL) {
    /* Add packet to the neighbor's queue */
    q = memb_alloc(&packet_memb);
    if(q != NULL) {
      q->ptr = memb_alloc(&metadata_memb);
      if(q->ptr != NULL) {
	q->buf = queuebuf_new_from_packetbuf();
	if(q->buf != NULL) {
	  struct qbuf_metadata *metadata = (struct qbuf_metadata *)q->ptr;
	  /* Neighbor and packet successfully allocated */
	  if(packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS) == 0) {
	    /* Use default configuration for max transmissions */
	    metadata->max_transmissions = CSMA_MAX_MAC_TRANSMISSIONS;
	  } else {
	    metadata->max_transmissions =
                  packetbuf_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS);
	  }
	  metadata->sent = sent;
	  metadata->cptr = ptr;

	  if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
	     PACKETBUF_ATTR_PACKET_TYPE_ACK) {
	    list_push(n->queued_packet_list, q);
	  } else {
	    list_add(n->queued_packet_list, q);
	  }

	  /* If q is the first packet in the neighbor's queue, send asap */
	  if(list_head(n->queued_packet_list) == q) {
	    ctimer_set(&n->transmit_timer, 0, transmit_packet_list, n);
	  }
	  return;
	}
	memb_free(&metadata_memb, q->ptr);
	PRINTF("csma: could not allocate queuebuf, dropping packet\n");
      }
      memb_free(&packet_memb, q);
      PRINTF("csma: could not allocate queuebuf, dropping packet\n");
    }
    /* The packet allocation failed. Remove and free neighbor entry if empty. */
    if(list_length(n->queued_packet_list) == 0) {
      list_remove(neighbor_list, n);
      memb_free(&neighbor_memb, n);
    }
    PRINTF("csma: could not allocate packet, dropping packet\n");
  } else {
    PRINTF("csma: could not allocate neighbor, dropping packet\n");
  }
  mac_call_sent_callback(sent, ptr, MAC_TX_ERR, 1);
}
开发者ID:AlexandreRio,项目名称:contiki,代码行数:91,代码来源:csma.c


示例19: init_net

/*---------------------------------------------------------------------------*/
void
init_net(uint8_t node_id)
{
  uint16_t shortaddr;
  uint64_t longaddr;
  linkaddr_t addr;
#if WITH_UIP6
  uip_ds6_addr_t *lladdr;
  uip_ipaddr_t ipaddr;
#endif

  uint8_t i;

  memset(&shortaddr, 0, sizeof(shortaddr));
  memset(&longaddr, 0, sizeof(longaddr));
  *((uint8_t *)&shortaddr) = node_id >> 8;
  *((uint8_t *)&shortaddr + 1) = node_id;
  *((uint8_t *)&longaddr) = node_id >> 8;
  *((uint8_t *)&longaddr + 1) = node_id;
  for(i = 2; i < sizeof(longaddr); ++i) {
    ((uint8_t *)&longaddr)[i] = random_rand();
  }

  PRINTF("SHORT MAC ADDRESS %02x:%02x\n",
         *((uint8_t *) & shortaddr), *((uint8_t *) & shortaddr + 1));

  PRINTF("EXTENDED MAC ADDRESS %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
         *((uint8_t *)&longaddr),
         *((uint8_t *)&longaddr + 1),
         *((uint8_t *)&longaddr + 2),
         *((uint8_t *)&longaddr + 3),
         *((uint8_t *)&longaddr + 4),
         *((uint8_t *)&longaddr + 5),
         *((uint8_t *)&longaddr + 6),
         *((uint8_t *)&longaddr + 7));

  memset(&addr, 0, sizeof(linkaddr_t));

  for(i = 0; i < sizeof(addr.u8); ++i) {
    addr.u8[i] = ((uint8_t *)&longaddr)[i];
  }

  linkaddr_set_node_addr(&addr);

  PRINTF("Rime started with address: ");
  for(i = 0; i < sizeof(addr.u8) - 1; ++i) {
    PRINTF("%d.", addr.u8[i]);
  }
  PRINTF("%d\n", addr.u8[i]);

  queuebuf_init();

  NETSTACK_RADIO.init();

  mrf24j40_set_channel(RF_CHANNEL);
  mrf24j40_set_panid(IEEE802154_PANID);
  mrf24j40_set_short_mac_addr(shortaddr);
  mrf24j40_set_extended_mac_addr(longaddr);

  NETSTACK_RDC.init();
  NETSTACK_MAC.init();
  NETSTACK_NETWORK.init();

  PRINTF("%s %s, channel check rate %d Hz, radio channel %u\n",
         NETSTACK_MAC.name, NETSTACK_RDC.name,
         CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
                         NETSTACK_RDC.channel_check_interval()), RF_CHANNEL);

#if WITH_UIP6

#if LINKADDR_CONF_SIZE == 2
  memset(&uip_lladdr.addr, 0, sizeof(uip_lladdr.addr));
  uip_lladdr.addr[3] = 0xff;
  uip_lladdr.addr[4]= 0xfe;
  memcpy(&uip_lladdr.addr[6], &shortaddr, sizeof(shortaddr));
#else
  memcpy(&uip_lladdr.addr, &longaddr, sizeof(uip_lladdr.addr));
#endif

  process_start(&tcpip_process, NULL);

  lladdr = uip_ds6_get_link_local(-1);

  PRINTF("Tentative link-local IPv6 address ");

  for(i = 0; i < 7; ++i) {
    PRINTF("%02x%02x:", lladdr->ipaddr.u8[i * 2], lladdr->ipaddr.u8[i * 2 + 1]);
  }

  PRINTF("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);

  if(!UIP_CONF_IPV6_RPL) {
    uip_ip6addr(&ipaddr, 0x2001, 0x1418, 0x100, 0x823c, 0, 0, 0, 0);
    uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
    uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);

    PRINTF("Tentative global IPv6 address ");

    for(i = 0; i < 7; ++i) {
//.........这里部分代码省略.........
开发者ID:200018171,项目名称:contiki,代码行数:101,代码来源:init-net.c


示例20: packet_sent

/*---------------------------------------------------------------------------*/
static void
packet_sent(void *ptr, int status, int num_transmissions)
{
  struct neighbor_queue *n;
  struct rdc_buf_list *q;
  struct qbuf_metadata *metadata;
  clock_time_t time = 0;
  mac_callback_t sent;
  void *cptr;
  int num_tx;
  int backoff_exponent;
  int backoff_transmissions;

  n = ptr;
  if(n == NULL) {
    return;
  }
  switch(status) {
  case MAC_TX_OK:
  case MAC_TX_NOACK:
    n->transmissions++;
    break;
  case MAC_TX_COLLISION:
    n->collisions++;
    break;
  case MAC_TX_DEFERRED:
    n->deferrals++;
    break;
  }

  for(q = list_head(n->queued_packet_list);
      q != NULL; q = list_item_next(q)) {
    if(queuebuf_attr(q->buf, PACKETBUF_ATTR_MAC_SEQNO) ==
       packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
      break;
    }
  }

  if(q != NULL) {
    metadata = (struct qbuf_metadata *)q->ptr;

    if(metadata != NULL) {
      sent = metadata->sent;
      cptr = metadata->cptr;
      num_tx = n->transmissions;
      if(status == MAC_TX_COLLISION ||
         status == MAC_TX_NOACK) {

        /* If the transmission was not performed because of a
           collision or noack, we must retransmit the packet. */

        switch(status) {
        case MAC_TX_COLLISION:
          PRINTF("csma: rexmit collision %d\n", n->transmissions);
          break;
        case MAC_TX_NOACK:
          PRINTF("csma: rexmit noack %d\n", n->transmissions);
          break;
        default:
          PRINTF("csma: rexmit err %d, %d\n", status, n->transmissions);
        }

        /* The retransmission time must be proportional to the channel
           check interval of the underlying radio duty cycling layer. */
        time = default_timebase();

        /* The retransmission time uses a truncated exponential backoff
         * so that the interval between the transmissions increase with
         * each retransmit. */
        backoff_exponent = num_tx;

        /* Truncate the exponent if needed. */
        if(backoff_exponent > CSMA_MAX_BACKOFF_EXPONENT) {
          backoff_exponent = CSMA_MAX_BACKOFF_EXPONENT;
        }

        /* Proceed to exponentiation. */
        backoff_transmissions = 1 << backoff_exponent;

        /* Pick a time for next transmission, within the interval:
         * [time, time + 2^backoff_exponent * time[ */
        time = time + (random_rand() % (backoff_transmissions * time));

        if(n->transmissions < metadata->max_transmissions) {
          PRINTF("csma: retransmitting with time %lu %p\n", time, q);
          ctimer_set(&n->transmit_timer, time,
                     transmit_packet_list, n);
          /* This is needed to correctly attribute energy that we spent
             transmitting this packet. */
          queuebuf_update_attr_from_packetbuf(q->buf);
        } else {
          PRINTF("csma: drop with status %d after %d transmissions, %d collisions\n",
                 status, n->transmissions, n->collisions);
          free_packet(n, q);
          mac_call_sent_callback(sent, cptr, status, num_tx);
        }
      } else {
        if(status == MAC_TX_OK) {
          PRINTF("csma: rexmit ok %d\n", n->transmissions);
//.........这里部分代码省略.........
开发者ID:AlexandreRio,项目名称:contiki,代码行数:101,代码来源:csma.c



注:本文中的random_rand函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ random_range函数代码示例发布时间:2022-05-30
下一篇:
C++ random_number函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap