/*---------------------------------------------------------------------------*/ int c_multihop_send(struct pipe *p, struct stackmodule_i *module) { PRINTF("c_multihop_send \n"); printaddr(module->stack_id); packetbuf_compact(); rimeaddr_t *nexthop; rimeaddr_t *tmpaddr = get_node_addr(module->stack_id, 0, 3); nexthop = c_forward(stack[module->stack_id].pip, stack[module->stack_id].amodule, module->module_id); if (nexthop == NULL) { PRINTF("multihop_send: no route\n"); return 0; } else { set_node_addr(module->stack_id, 0, 2, nexthop); PRINTF("multihop_send: sending data towards %d.%d\n", nexthop->u8[0], nexthop->u8[1]); packetbuf_set_addr(PACKETBUF_ADDR_ERECEIVER, tmpaddr); rimeaddr_t *tmpaddr1 = get_node_addr(module->stack_id, 0, 1); packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, tmpaddr1); rimeaddr_t esender, ereceiver; rimeaddr_copy(&esender, packetbuf_addr(PACKETBUF_ADDR_ESENDER)); rimeaddr_copy(&ereceiver, packetbuf_addr(PACKETBUF_ADDR_ERECEIVER)); packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1); return 1; } }
/* * Function: get * Desc: 获取数据 * In: * void * data 键值 * Out: * void * data 数据 * Return code: * -1 出错 * 0 成功 */ int CMBHash::get(const void * key, void * data) { int id = m_get_bucket_id(key) % get_bucket_size(); recMBHashBucket * bucket = m_hash->bucket_ + id; /* 数据地址 */ char * ptr = (char *)(bucket->addr + get_node_addr()); /* 做二分查找 */ int low = 0; int high = bucket->num - 1; while (low <= high) { int mid = (low + high) / 2; char * addr = ptr + mid * m_data_size; int ret = m_key_cmp(addr, key); if (ret == 0) /* 键值相同 */ { memcpy(data, addr, m_data_size); return 0; } if (ret > 0) high = mid - 1; else low = mid + 1; } return -1; }
rimeaddr_t * c_multihop_forward(struct pipe *p, struct stackmodule_i *module) { printf("multihop forward \n"); struct route_entry *rt, *previous_rt; rimeaddr_t *tmpaddr = get_node_addr(module->stack_id, 0, 3); rt = route_lookup(tmpaddr); if (rt == NULL) { return NULL; } else { /*if(clock_time()*1000/CLOCK_SECOND < 50100 && rimeaddr_node_addr.u8[0] == 1) { previous_rt->nexthop.u8[0] = rt->nexthop.u8[0]; previous_rt->cost = rt->cost; } if(clock_time()*1000/CLOCK_SECOND > 50100 && rimeaddr_node_addr.u8[0] == 1 && rt->nexthop.u8[0] == previous_rt->nexthop.u8[0] && previous_rt->cost == rt->cost) { route_remove(rt); rt = route_lookup(tmpaddr); }*/ /*if(clock_time()*1000/CLOCK_SECOND < 50100) { previous_rt->nexthop.u8[0] = rt->nexthop.u8[0]; previous_rt->cost = rt->cost; } if(clock_time()*1000/CLOCK_SECOND > 50100 && rt->nexthop.u8[0] == previous_rt->nexthop.u8[0] && previous_rt->cost == rt->cost) { route_remove(rt); return NULL; }*/ route_refresh(rt); } PRINTF("~c_multihop_forward \n"); return &rt->nexthop; }
int c_unicast_send(struct pipe *p, struct stackmodule_i *module) { PRINTF("c_unicast_send \n"); rimeaddr_t *tmpaddr = get_node_addr(module->stack_id, 0, 2); packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, tmpaddr); PRINTF("c_unicast_send to %d.%d \n", tmpaddr->u8[0], tmpaddr->u8[1]); printaddr(module->stack_id); return 1; }
/* * Function: add_finish * Desc: 新增完成,进行排序 * In: * none * Out: * none * Return code: * -1 出错 * 0 成功 */ int CMBHash::add_finish() { /* 数据地址 */ size_t addr = get_node_addr(); for (int i=0; i<m_bucket_size; i++) { recMBHashBucket * bucket = m_hash->bucket_ + i; if (bucket->num > 0) { /* 排序 */ qsort((void *)(bucket->addr + addr), bucket->num, m_data_size, CMBHash::m_data_cmp); } } return 0; }
/* * Function: add * Desc: 新增数据 * In: * void * data 数据 * int data_size 数据长度 * Out: * none * Return code: * -1 出错 * 0 成功 */ int CMBHash::add(const void * key, const void * data) { int id = m_get_bucket_id(key) % get_bucket_size(); recMBHashBucket * bucket = m_hash->bucket_ + id; /* 数据地址 */ char * ptr = (char *)(bucket->addr + get_node_addr()); for (int i=0; i<bucket->num; i++) { if (!m_chk_data(ptr)) { /* 存放数据 */ memcpy(ptr, data, m_data_size); return 0; } ptr += m_data_size; } return -1; }
/* * Function: pre_finish * Desc: 准备完成,分配数据空间 * In: * none * Out: * none * Return code: * -1 出错 * 0 成功 */ int CMBHash::pre_finish() { size_t offset = 0; /* 记载偏移地址 */ for (int i=0; i<m_bucket_size; i++) { recMBHashBucket * bucket = m_hash->bucket_ + i; if (bucket->num) { /* 桶对应起始地址 */ bucket->addr = offset; /* 下一个桶起始地址 */ offset += bucket->num * m_data_size; /* 清缓冲 */ size_t addr = get_node_addr() + bucket->addr; memset((char *)addr, 0, bucket->num * m_data_size); } } return 0; }
void uuid_create(afsUUID * uuid) { static int uuid_inited = 0; struct timeval tv; int ret, got_time; uint64_t dce_time; if (uuid_inited == 0) { gettimeofday(&last_time, NULL); seq_num = arc4random(); get_node_addr(nodeaddr); uuid_inited = 1; } gettimeofday(&tv, NULL); got_time = 0; do { ret = time_cmp(&tv, &last_time); if (ret < 0) { /* Time went backward, just inc seq_num and be done. * seq_num is 6 + 8 bit field it the uuid, so let it wrap * around. don't let it be zero. */ seq_num = (seq_num + 1) & 0x3fff; if (seq_num == 0) seq_num++; got_time = 1; counter = 0; last_time = tv; } else if (ret > 0) { /* time went forward, reset counter and be happy */ last_time = tv; counter = 0; got_time = 1; } else { #define UUID_MAX_HZ (1) /* make this bigger fix you have larger tickrate */ #define MULTIPLIER_100_NANO_SEC 10 if (++counter < UUID_MAX_HZ * MULTIPLIER_100_NANO_SEC) got_time = 1; } } while (!got_time); /* * now shift time to dce_time, epoch 00:00:00:00, 15 October 1582 * dce time ends year ~3400, so start to worry now */ dce_time = tv.tv_usec * MULTIPLIER_100_NANO_SEC + counter; dce_time += ((uint64_t) tv.tv_sec) * 10000000; dce_time += (((uint64_t) 0x01b21dd2) << 32) + 0x13814000; uuid->time_low = dce_time & 0xffffffff; uuid->time_mid = 0xffff & (dce_time >> 32); uuid->time_hi_and_version = 0x0fff & (dce_time >> 48); uuid->time_hi_and_version |= (1 << 12); uuid->clock_seq_low = seq_num & 0xff; uuid->clock_seq_hi_and_reserved = (seq_num >> 8) & 0x3f; uuid->clock_seq_hi_and_reserved |= 0x80; /* dce variant */ memcpy(uuid->node, nodeaddr, 6); }
/* * Solution: * http://www.prenhall.com/divisions/bp/app/russellcd/PROTECT/CHAPTERS/CHAP10/REVIEWIT.HTM * y = a + bx - the regression * a = (sum (xy) - n * x_mean * y_mean) / (sum x*x - n * x_mean * x_mean) * b = y_mean - b x_mean * * We define: * y = estimated prr (packet received ratio) = cost * x = current rssi * x_mean = mean of rssi over a pre-defined time window * y_mean = mean prr over a pre-defined time wondow * * * */ void c_lqe_linregr_recv(struct pipe *p, struct stackmodule_i *module){ PRINTF("c_linregr_recv \n"); uint16_t crtseqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); uint16_t rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI); int16_t l = 0; uint8_t list_len = list_length(p->neighbor_list); struct c_neighbor *n; clock_time_t last_heard = 100000; struct c_neighbor *new_n = memb_alloc(&p->neighbor_mem);; rimeaddr_copy(&new_n->addr, get_node_addr(0, 1, 0)); /* Find the neighbor. */ for(n = list_head(p->neighbor_list); n != NULL; n = list_item_next(n)) { if(rimeaddr_cmp(&new_n->addr, &n->addr)) { memcpy(new_n, n, sizeof (struct c_neighbor)); PRINTF("node found \n"); new_n->m = crtseqno - n->last_seq_no - 1; //the current seq number - last seq number - crt packet no if (new_n->m > n->k) { l = new_n->m - n->k; new_n->k = n->k; } else { l = 0; new_n->k = 0; } uint8_t lost_p; double delta; for(lost_p = 0; lost_p < l; lost_p++) { delta = 0 - n->prr; n->prr = n->prr + delta / (n->count + lost_p); } delta = 1 - n->prr; new_n->prr = n->prr + delta / (n->count + l); new_n->xy = n->xy + rssi * n->count / (n->count + n->m ); new_n->x_2 = n->x_2 + rssi * rssi; double b = (new_n->xy - new_n->count * new_n->rssi * new_n->prr) / (new_n->x_2 - new_n->count * new_n->rssi); double a = new_n->prr - b * n->rssi; new_n->cost = a + b * rssi; PRINTF("a %f b %f new_n_cost %f rssi %d\n", a, b, new_n->cost, rssi); list_remove(p->neighbor_list, n); memb_free(&p->neighbor_mem, n); list_push(p->neighbor_list, new_n); return; } } /*If neighbor is not in the list and list is full, remove from list before adding*/ if (list_len >= NUM_NEIGHBOR_ENTRIES) { for(n = list_head(p->neighbor_list); n != NULL; n = list_item_next(n)) { if (last_heard == n->last_time_stamp) { list_remove(p->neighbor_list, n); memb_free(&p->neighbor_mem, n); } } } list_push(p->neighbor_list, new_n); }
void c_lqe_ewma_recv(struct pipe *p, struct stackmodule_i *module){ PRINTF("c_lqe_ewma_recv seqno %u \n", packetbuf_attr(PACKETBUF_ATTR_PACKET_ID)); uint16_t crtseqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); int16_t l = 0; struct c_neighbor *n; struct c_neighbor *new_n = memb_alloc(&p->neighbor_mem);; rimeaddr_copy(&new_n->addr, get_node_addr(0, 1, 0)); double alpha = p->lqe_ewma_param.alpha; uint8_t list_len = list_length(p->neighbor_list); double highest_cost = 100.0; /* Find the neighbor. */ for(n = list_head(p->neighbor_list); n != NULL; n = list_item_next(n)) { if(rimeaddr_cmp(&new_n->addr, &n->addr)) { if (highest_cost > n->cost) { highest_cost = n->cost; } PRINTF("node found \n"); PRINTF("n-lastseqno %d n-m %d n-k %d\n", n->last_seq_no, n->m, n->k); new_n->m = crtseqno - n->last_seq_no - 1; //the current seq number - last seq number - crt packet no if (new_n->m > n->k) { l = new_n->m - n->k; new_n->k = n->k; } else { l = 0; new_n->k = 0; } PRINTF("newn-lastseqno %d newn-m %d newn-k %d\n", new_n->last_seq_no, new_n->m, new_n->k); uint8_t lost_p; for(lost_p = 0; lost_p < l; lost_p++) { n->cost *= alpha; } new_n->cost = n->cost * alpha + (1 - alpha); PRINTF("l %d cost %f\n", l, new_n->cost); if (n->count_lock == 1) { new_n->count = n->count; } else { new_n->count = n->count + 1; } new_n->first_time_stamp = n->first_time_stamp; new_n->last_seq_no = crtseqno; new_n->last_time_stamp = clock_time(); new_n->rate = (new_n->count + new_n->m) / (new_n->last_time_stamp - new_n->first_time_stamp); new_n->cost_lock = 1; list_remove(p->neighbor_list, n); memb_free(&p->neighbor_mem, n); list_push(p->neighbor_list, new_n); print_list(p->neighbor_list); return; } } new_n->count = 1; new_n->cost = 1; new_n->m = 0; new_n->k = 0; new_n->last_seq_no = crtseqno; new_n->rate = 1 / clock_time(); new_n->last_time_stamp = clock_time(); new_n->cost_lock = 1; new_n->first_time_stamp = new_n->last_time_stamp; /*If neighbor is not in the list and list is full, remove from list before adding*/ if (list_len >= NUM_NEIGHBOR_ENTRIES) { for(n = list_head(p->neighbor_list); n != NULL; n = list_item_next(n)) { if (highest_cost == n->cost) { list_remove(p->neighbor_list, n); memb_free(&p->neighbor_mem, n); } } } list_push(p->neighbor_list, new_n); print_list(p->neighbor_list); }