static void test_fib_15_get_lifetime(void) { timex_t lifetime, now; kernel_pid_t iface_id = 1; char addr_dst[] = "Test address151"; char addr_nxt[] = "Test address152"; size_t add_buf_size = 16; uint32_t addr_dst_flags = 0x77777777; uint32_t addr_nxt_flags = 0x77777777; TEST_ASSERT_EQUAL_INT(0, fib_add_entry(iface_id, (uint8_t *)addr_dst, add_buf_size - 1, addr_dst_flags, (uint8_t *)addr_nxt, add_buf_size - 1, addr_nxt_flags, 1000)); TEST_ASSERT_EQUAL_INT(0, fib_devel_get_lifetime(&lifetime, (uint8_t *)addr_dst, add_buf_size - 1)); /* assuming some ms passed during these operations... */ vtimer_now(&now); timex_t cmp_lifetime = timex_add(now, timex_set(0, 900000)); timex_t cmp_max_lifetime = timex_add(now, timex_set(1,1)); TEST_ASSERT_EQUAL_INT(1, timex_cmp(lifetime, cmp_lifetime)); /* make sure lifetime hasn't grown magically either */ TEST_ASSERT_EQUAL_INT(-1, timex_cmp(lifetime, cmp_max_lifetime)); fib_deinit(); }
static void test_timex_add(void) { timex_t time; time = timex_add(timex_set(100, 100), timex_set(40, 10)); TEST_ASSERT_EQUAL_INT(0, timex_cmp(time, timex_set(140, 110))); time = timex_add(timex_set(100, 700000), timex_set(40, 800000)); TEST_ASSERT_EQUAL_INT(0, timex_cmp(time, timex_set(141, 500000))); }
/* * Check if entry at index i is stale as described in Section 6.3. * and clear the struct it fills if it is */ static void _reset_entry_if_stale(uint8_t i) { vtimer_now(&now); timex_t lastUsed, expirationTime; if (timex_cmp(routing_table[i].expirationTime, null_time) == 0) { return; } int state = routing_table[i].state; lastUsed = routing_table[i].lastUsed; expirationTime = routing_table[i].expirationTime; /* an Active route is considered to remain Active as long as it is used at least once * during every ACTIVE_INTERVAL. When a route is no longer Active, it becomes an Idle route. */ /* if the node is younger than the active interval, don't bother */ if (timex_cmp(now, active_interval) < 0) { return; } if ((state == ROUTE_STATE_ACTIVE) && (timex_cmp(timex_sub(now, active_interval), lastUsed) == 1)) { DEBUG("\t[routing] route towards %s Idle\n", netaddr_to_string(&nbuf, &routing_table[i].addr)); routing_table[i].state = ROUTE_STATE_IDLE; routing_table[i].lastUsed = now; /* mark the time entry was set to Idle */ } /* After an Idle route remains Idle for MAX_IDLETIME, it becomes an Invalid route. */ /* if the node is younger than the expiration time, don't bother */ if (timex_cmp(now, expirationTime) < 0) { return; } /* If Current_Time > Route.ExpirationTime, set Route.State := Invalid. */ if ((state == ROUTE_STATE_IDLE) && (timex_cmp(now, expirationTime) > 0)) { DEBUG("\t[routing] route towards %s became Invalid\n", netaddr_to_string(&nbuf, &routing_table[i].addr)); routing_table[i].state = ROUTE_STATE_INVALID; routing_table[i].lastUsed = now; /* mark the time entry was set to Invalid */ } /* If (Current_Time - Route.LastUsed) > (ACTIVE_INTERVAL + MAX_IDLETIME), * and if (Route.Timed == FALSE), set Route.State := Invalid. */ if ((timex_cmp(timex_sub(now, lastUsed), timex_add(active_interval, max_idletime)) > 0) && (state != ROUTE_STATE_TIMED)) { routing_table[i].state = ROUTE_STATE_INVALID; } /* After that time, old sequence number information is considered no longer * valid and the Invalid route MUST BE expunged */ if (timex_cmp(timex_sub(now, lastUsed), max_seqnum_lifetime) >= 0) { DEBUG("\t[routing] Expunged routing table entry for %s at %i\n", netaddr_to_string(&nbuf, &routing_table[i].addr), i); memset(&routing_table[i], 0, sizeof(routing_table[i])); } }
void set_remaining_time(timex_t *t, uint32_t time) { timex_t tmp = {time, 0}; timex_t now; vtimer_now(&now); *t = timex_add(now, tmp); }
void routingtable_fill_routing_entry_t_rrep(struct aodvv2_packet_data *packet_data, struct aodvv2_routing_entry_t *rt_entry) { rt_entry->addr = packet_data->targNode.addr; rt_entry->seqnum = packet_data->targNode.seqnum; rt_entry->nextHopAddr = packet_data->sender; rt_entry->lastUsed = packet_data->timestamp; rt_entry->expirationTime = timex_add(packet_data->timestamp, validity_t); rt_entry->metricType = packet_data->metricType; rt_entry->metric = packet_data->targNode.metric; rt_entry->state = ROUTE_STATE_ACTIVE; }
static void calc_rtt(void) { timex_t rtt = timex_sub(end, start); rtt_sum = timex_add(rtt_sum, rtt); l2_ping_stats.last_rtt = rtt; l2_ping_stats.avg_rtt = timex_from_uint64(timex_uint64(rtt_sum) / l2_ping_stats.pong_count); if (timex_cmp(rtt, l2_ping_stats.max_rtt) > 0) { l2_ping_stats.max_rtt = rtt; } if (timex_cmp(rtt, l2_ping_stats.min_rtt) < 0) { l2_ping_stats.min_rtt = rtt; } }
/* * Check if entry at index i is stale and clear the struct it fills if it is */ static void _reset_entry_if_stale(uint8_t i) { vtimer_now(&now); if (timex_cmp(rreq_table[i].timestamp, null_time) == 0) { return; } timex_t expiration_time = timex_add(rreq_table[i].timestamp, _max_idletime); if (timex_cmp(expiration_time, now) < 0) { /* timestamp+expiration time is in the past: this entry is stale */ DEBUG("\treset rreq table entry %s\n", netaddr_to_string(&nbuf, &rreq_table[i].origNode)); memset(&rreq_table[i], 0, sizeof(rreq_table[i])); } }
void def_rtr_lst_add(ipv6_addr_t *ipaddr, uint32_t rtr_ltime) { if (def_rtr_count == DEF_RTR_LST_SIZE) { DEBUG("ERROR: default router list full\n"); } else { memcpy(&(def_rtr_lst[def_rtr_count].addr), ipaddr, 16); timex_t rltime = {rtr_ltime, 0}; timex_t now; vtimer_now(&now); def_rtr_lst[def_rtr_count].inval_time = timex_add(now, rltime); def_rtr_count++; } }
static int vtimer_set(vtimer_t *timer) { DEBUG("vtimer_set(): New timer. Offset: %" PRIu32 " %" PRIu32 "\n", timer->absolute.seconds, timer->absolute.microseconds); timex_t now; vtimer_now(&now); timer->absolute = timex_add(now, timer->absolute); normalize_to_tick(&(timer->absolute)); DEBUG("vtimer_set(): Absolute: %" PRIu32 " %" PRIu32 "\n", timer->absolute.seconds, timer->absolute.microseconds); DEBUG("vtimer_set(): NOW: %" PRIu32 " %" PRIu32 "\n", now.seconds, now.microseconds); int result = 0; if (timer->absolute.seconds == 0) { if (timer->absolute.microseconds > 10) { timer->absolute.microseconds -= 10; } } unsigned state = disableIRQ(); if (timer->absolute.seconds != longterm_tick_timer.absolute.seconds) { /* we're long-term */ DEBUG("vtimer_set(): setting long_term\n"); result = set_longterm(timer); } else { DEBUG("vtimer_set(): setting short_term\n"); if (set_shortterm(timer)) { /* delay update of next shortterm timer if we * are called from within vtimer_callback. */ if (!in_callback) { result = update_shortterm(); } } } restoreIRQ(state); return result; }
int ipv6_net_if_add_addr(int if_id, const ipv6_addr_t *addr, ndp_addr_state_t state, uint32_t val_ltime, uint32_t pref_ltime, uint8_t is_anycast) { ipv6_net_if_addr_t *addr_entry; ipv6_net_if_hit_t hit; if (ipv6_addr_is_unspecified(addr) == 128) { DEBUG("ERROR: unspecified address (::) can't be assigned to interface.\n"); return 0; } if (ipv6_addr_is_multicast(addr) && is_anycast) { DEBUG("ERROR: anycast addresses must not be multicast addresses " "(i.e. start with ff::/2)\n"); return 0; } if (ipv6_net_if_addr_match(&hit, addr)) { return 1; } if (ipv6_net_if_addr_buffer_count < IPV6_NET_IF_ADDR_BUFFER_LEN) { timex_t valtime = {val_ltime, 0}; timex_t preftime = {pref_ltime, 0}; timex_t now; vtimer_now(&now); ipv6_addr_t *addr_data = &ipv6_addr_buffer[ipv6_net_if_addr_buffer_count]; memcpy(addr_data, addr, sizeof(ipv6_addr_t)); addr_entry = &ipv6_net_if_addr_buffer[ipv6_net_if_addr_buffer_count]; addr_entry->addr_data = addr_data; addr_entry->addr_len = 128; if (is_anycast) { addr_entry->addr_protocol = NET_IF_L3P_IPV6_ANYCAST; } else if (ipv6_addr_is_multicast(addr_data)) { addr_entry->addr_protocol = NET_IF_L3P_IPV6_MULTICAST; } else { addr_entry->addr_protocol = NET_IF_L3P_IPV6_UNICAST; } addr_entry->ndp_state = state; addr_entry->valid_lifetime = timex_add(now, valtime); addr_entry->preferred_lifetime = timex_add(now, preftime); addr_entry->is_anycast = is_anycast; ipv6_net_if_addr_buffer_count++; net_if_add_address(if_id, (net_if_addr_t *)addr_entry); /* Register to Solicited-Node multicast address according to RFC 4291 */ if (is_anycast || !ipv6_addr_is_multicast(addr)) { ipv6_addr_t sol_node_mcast_addr; ipv6_addr_set_solicited_node_addr(&sol_node_mcast_addr, addr); if (ipv6_net_if_addr_match(&hit, &sol_node_mcast_addr) == NULL) { ipv6_net_if_add_addr(if_id, &sol_node_mcast_addr, state, val_ltime, pref_ltime, 0); } } return 1; } return 0; }