Exemplo n.º 1
0
int main(void)
{

    xtimer_t timer;
    timer.callback = time_evt;
    timer.arg = (void *)sched_active_thread;
    uint32_t last = xtimer_now_usec();

    puts("\nTest setting thread flags from (x)timer callback");
    printf("You should see the message '+++ timeout XX +++' printed to the \n"
           "screen %i times, once every %lu milliseconds\n\n",
            REPEAT, (TIMEOUT / US_PER_MS));

    for (int i = 1; i <= REPEAT; i++) {
        xtimer_set(&timer, TIMEOUT);
        thread_flags_wait_any(0x1);
        printf("+++ timeout %2i +++\n", i);
    }

    /* we consider the test successful, if the runtime was above 500ms */
    uint32_t runtime = xtimer_now_usec() - last;
    if (runtime > RUNTIME) {
        puts("Test finished: SUCCESS\n");
    }
    else {
        puts("Test finished: FAILED\n");
    }

    return 0;
}
Exemplo n.º 2
0
static void run_client(int max_cnt)
{
    printf("client (pid=%" PRIkernel_pid "): start\n", thread_getpid());

    handle = ndn_app_create();
    if (handle == NULL) {
	printf("client (pid=%" PRIkernel_pid "): cannot create app handle\n",
	       thread_getpid());
	return;
    }

    max_count = max_cnt;
    count = 0;
    begin = xtimer_now_usec();

    if (send_interest() == NDN_APP_ERROR) {
	printf("client (pid=%" PRIkernel_pid "): cannot send interest"
	       " (%"PRIu32")\n", handle->id, count);
	ndn_app_destroy(handle);
	return;
    }

    ndn_app_run(handle);

    ndn_app_destroy(handle);
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: jthacker/RIOT
int main(void)
{
    puts("[START] event test application.\n");

    event_queue_t queue = { .waiter = (thread_t *)sched_active_thread };
    printf("posting 0x%08x\n", (unsigned)&event);
    event_post(&queue, &event);

    printf("posting 0x%08x\n", (unsigned)&event2);
    event_post(&queue, &event2);
    printf("canceling 0x%08x\n", (unsigned)&event2);
    event_cancel(&queue, &event2);

    printf("posting custom event\n");
    event_post(&queue, (event_t *)&custom_event);

    event_timeout_t event_timeout;

    printf("posting timed callback with timeout 1sec\n");
    event_timeout_init(&event_timeout, &queue, (event_t *)&event_callback);
    before = xtimer_now_usec();
    event_timeout_set(&event_timeout, 100000LU);

    printf("launching event queue\n");
    event_loop(&queue);

    return 0;
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: OTAkeys/RIOT
int main(void)
{
#if defined(MAIN_THREAD_PIN)
    gpio_t main_pin = MAIN_THREAD_PIN;
    gpio_init(main_pin, GPIO_OUT);
#endif

#if defined(WORKER_THREAD_PIN_1) && defined(WORKER_THREAD_PIN_2)
    timer_arg_t sleep_timer1 = { TEST_SLEEP_TIME_1, WORKER_THREAD_PIN_1 };
    timer_arg_t sleep_timer2 = { TEST_SLEEP_TIME_2, WORKER_THREAD_PIN_2 };
#else
    uint32_t sleep_timer1 = TEST_SLEEP_TIME_1;
    uint32_t sleep_timer2 = TEST_SLEEP_TIME_2;
#endif
    LOG_DEBUG("[INIT]\n");


    thread_create(stack_timer1, TEST_TIMER_STACKSIZE,
                  2, THREAD_CREATE_STACKTEST,
                  timer_func, &sleep_timer1, "timer1");
    thread_create(stack_timer2, TEST_TIMER_STACKSIZE,
                  3, THREAD_CREATE_STACKTEST,
                  timer_func, &sleep_timer2, "timer2");

    uint32_t now = 0;
    uint32_t start = xtimer_now_usec();
    uint32_t until = start + (TEST_TIME_S * US_PER_SEC);
    uint32_t interval = TEST_INTERVAL_MS * US_PER_MS;
    xtimer_ticks32_t last_wakeup = xtimer_now();

    puts("[START]");
    while ((now = xtimer_now_usec()) < until) {
        unsigned percent = (100 * (now - start)) / (until - start);
#if defined(MAIN_THREAD_PIN)
        gpio_set(main_pin);
#endif
        xtimer_periodic_wakeup(&last_wakeup, interval);
#if defined(MAIN_THREAD_PIN)
        gpio_clear(main_pin);
#endif
        printf("Testing (%3u%%)\n", percent);
    }
    puts("Testing (100%)");
    puts("[SUCCESS]");
    return 0;
}
Exemplo n.º 5
0
static void _set_timer(xtimer_t *timer, uint32_t offset_ms)
{
    uint64_t offset_us = (uint64_t)offset_ms * US_PER_MS;

    DEBUG("evtimer: now=%" PRIu32 " us setting xtimer to %" PRIu32 ":%" PRIu32 " us\n",
          xtimer_now_usec(), (uint32_t)(offset_us >> 32), (uint32_t)(offset_us));

    xtimer_set64(timer, offset_us);
}
Exemplo n.º 6
0
Arquivo: main.c Projeto: jthacker/RIOT
static void timed_callback(void *arg)
{
    order++;
    assert(order == 3);
    assert(arg == event_callback.arg);
    uint32_t now = xtimer_now_usec();
    assert((now - before >= 100000LU));
    printf("triggered timed callback with arg 0x%08x after %" PRIu32 "us\n", (unsigned)arg, now - before);
    printf("[SUCCESS]\n");
}
Exemplo n.º 7
0
/*
 * Main request handler: generates response PDU in the provided buffer.
 *
 * Caller must finish the PDU and send it.
 *
 * return length of response pdu, or < 0 if can't handle
 */
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
                                                         sock_udp_ep_t *remote)
{
    coap_resource_t *resource;
    gcoap_listener_t *listener;
    sock_udp_ep_t *observer    = NULL;
    gcoap_observe_memo_t *memo = NULL;
    gcoap_observe_memo_t *resource_memo = NULL;

    _find_resource(pdu, &resource, &listener);
    if (resource == NULL) {
        return gcoap_response(pdu, buf, len, COAP_CODE_PATH_NOT_FOUND);
    }
    else {
        /* used below to ensure a memo not already recorded for the resource */
        _find_obs_memo_resource(&resource_memo, resource);
    }

    if (coap_get_observe(pdu) == COAP_OBS_REGISTER) {
        int empty_slot = _find_obs_memo(&memo, remote, pdu);
        /* record observe memo */
        if (memo == NULL) {
            if (empty_slot >= 0 && resource_memo == NULL) {

                int obs_slot = _find_observer(&observer, remote);
                /* cache new observer */
                if (observer == NULL) {
                    if (obs_slot >= 0) {
                        observer = &_coap_state.observers[obs_slot];
                        memcpy(observer, remote, sizeof(sock_udp_ep_t));
                    } else {
                        DEBUG("gcoap: can't register observer\n");
                    }
                }
                if (observer != NULL) {
                    memo = &_coap_state.observe_memos[empty_slot];
                }
            }
            if (memo == NULL) {
                coap_clear_observe(pdu);
                DEBUG("gcoap: can't register observe memo\n");
            }
        }
        if (memo != NULL) {
            memo->observer  = observer;
            memo->resource  = resource;
            memo->token_len = coap_get_token_len(pdu);
            if (memo->token_len) {
                memcpy(&memo->token[0], pdu->token, memo->token_len);
            }
            DEBUG("gcoap: Registered observer for: %s\n", memo->resource->path);
            /* generate initial notification value */
            uint32_t now       = xtimer_now_usec();
            pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF;
        }
Exemplo n.º 8
0
void _update_lifetime(void)
{
    uint32_t now = xtimer_now_usec();
    uint16_t now_sec = now / SEC_IN_USEC;

    gnrc_rpl_parent_t *parent;
    gnrc_rpl_instance_t *inst;

    for (uint8_t i = 0; i < GNRC_RPL_PARENTS_NUMOF; ++i) {
        parent = &gnrc_rpl_parents[i];
        if (parent->state != 0) {
            if ((int32_t)(parent->lifetime - now_sec) <= GNRC_RPL_LIFETIME_UPDATE_STEP) {
                gnrc_rpl_dodag_t *dodag = parent->dodag;
                gnrc_rpl_parent_remove(parent);
                gnrc_rpl_parent_update(dodag, NULL);
                continue;
            }
            else if ((int32_t)(parent->lifetime - now_sec) <= (GNRC_RPL_LIFETIME_UPDATE_STEP * 2)) {
                gnrc_rpl_send_DIS(parent->dodag->instance, &parent->addr);
            }
        }
    }

    for (int i = 0; i < GNRC_RPL_INSTANCES_NUMOF; ++i) {
        inst = &gnrc_rpl_instances[i];
        if (inst->state != 0) {
            if ((inst->cleanup > 0) && (inst->dodag.parents == NULL) &&
                    (inst->dodag.my_rank == GNRC_RPL_INFINITE_RANK)) {
                inst->cleanup -= GNRC_RPL_LIFETIME_UPDATE_STEP;
                if (inst->cleanup <= 0) {
                    /* no parents - delete this instance and DODAG */
                    gnrc_rpl_instance_remove(inst);
                    continue;
                }
            }

            if (inst->dodag.dao_time > GNRC_RPL_LIFETIME_UPDATE_STEP) {
                inst->dodag.dao_time -= GNRC_RPL_LIFETIME_UPDATE_STEP;
            }
            else {
                _dao_handle_send(&inst->dodag);
            }
        }
    }

#ifdef MODULE_GNRC_RPL_P2P
    gnrc_rpl_p2p_update();
#endif

    xtimer_set_msg(&_lt_timer, _lt_time, &_lt_msg, gnrc_rpl_pid);
}
Exemplo n.º 9
0
int main(void)
{
    puts("START");

    kernel_pid_t pid = thread_create(stack,
                  sizeof(stack),
                  THREAD_PRIORITY_MAIN - 1,
                  THREAD_CREATE_STACKTEST,
                  _thread,
                  NULL,
                  "second_thread");

    thread_t *thread = (thread_t*) thread_get(pid);
    _set(thread, 0x1);
    _set(thread, 0x64);
    _set(thread, 0x1);
    _set(thread, 0x8);
    _set(thread, 0x2);
    _set(thread, 0x4);

    while(!done) {};

    puts("main: setting 100ms timeout...");
    xtimer_t t;
    uint32_t before = xtimer_now_usec();
    xtimer_set_timeout_flag(&t, TIMEOUT);
    thread_flags_wait_any(THREAD_FLAG_TIMEOUT);
    uint32_t diff = xtimer_now_usec() - before;
    printf("main: timeout triggered. time passed: %uus\n", (unsigned)diff);

    if (diff < (TIMEOUT + THRESHOLD)) {
        puts("SUCCESS");
        return 0;
    }
    puts("FAILURE");

    return 1;
}
Exemplo n.º 10
0
static int check_and_read(const void *dev, phydat_t *res, int16_t *val, uint8_t unit)
{
    uint32_t now = xtimer_now_usec();

    if ((now - last) > DHT_SAUL_HOLD_TIME) {
        dht_read((const dht_t *)dev, &temp, &hum);
        last = now;
    }

    res->val[0] = *val;
    memset(&res->val[1], 0, 2 * sizeof(int16_t));
    res->unit = unit;
    res->scale = -1;
    return 1;
}
Exemplo n.º 11
0
static int on_data(ndn_block_t* interest, ndn_block_t* data)
{
    (void)interest;
    (void)data;
/*
    ndn_block_t name;
    int r = ndn_data_get_name(data, &name);
    assert(r == 0);
    printf("client (pid=%" PRIkernel_pid "): data received, name=",
	   handle->id);
    ndn_name_print(&name);
    putchar('\n');

    ndn_block_t content;
    r = ndn_data_get_content(data, &content);
    assert(r == 0);
    assert(content.len == 6);

    printf("client (pid=%" PRIkernel_pid "): content=%02X%02X%02X%02X\n",
	   handle->id, *(content.buf + 2), *(content.buf + 3),
	   *(content.buf + 4), *(content.buf + 5));

    r = ndn_data_verify_signature(data, ecc_key_pub, sizeof(ecc_key_pub));
    if (r != 0)
	printf("client (pid=%" PRIkernel_pid "): fail to verify signature\n",
	       handle->id);
    else
	printf("client (pid=%" PRIkernel_pid "): signature valid\n",
	       handle->id);
*/
    if (++count != max_count) {
	if (send_interest() == NDN_APP_ERROR) {
	    printf("client (pid=%" PRIkernel_pid "): cannot send interest"
		   " (%"PRIu32")\n", handle->id, count);
	    ndn_app_destroy(handle);
	    return NDN_APP_ERROR;
	}
    }
    else {
	uint32_t end = xtimer_now_usec();
	printf("client (pid=%"PRIkernel_pid"): total time = %"PRIu32
	       ", average = %"PRIu32"\n", handle->id, end - begin,
	       (end-begin) / max_count);
	return NDN_APP_STOP;
    }
    return NDN_APP_CONTINUE;
}
Exemplo n.º 12
0
void emcute_run(uint16_t port, const char *id)
{
    assert(strlen(id) < EMCUTE_ID_MAXLEN);

    sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
    sock_udp_ep_t remote;
    local.port = port;
    cli_id = id;
    timer.callback = time_evt;
    timer.arg = NULL;
    mutex_init(&txlock);

    if (sock_udp_create(&sock, &local, NULL, 0) < 0) {
        LOG_ERROR("[emcute] unable to open UDP socket on port %i\n", (int)port);
        return;
    }

    uint32_t start = xtimer_now_usec();
    uint32_t t_out = (EMCUTE_KEEPALIVE * US_PER_SEC);

    while (1) {
        ssize_t len = sock_udp_recv(&sock, rbuf, sizeof(rbuf), t_out, &remote);

        if ((len < 0) && (len != -ETIMEDOUT)) {
            LOG_ERROR("[emcute] error while receiving UDP packet\n");
            return;
        }

        if (len >= 2) {
            /* handle the packet */
            uint16_t pkt_len;
            /* catch invalid length field */
            if ((len == 2) && (rbuf[0] == 0x01)) {
                continue;
            }
            /* parse length field */
            size_t pos = get_len(rbuf, &pkt_len);
            /* verify length to prevent overflows */
            if (((size_t)pkt_len > (size_t)len) || (pos >= (size_t)len)) {
                continue;
            }
            /* get packet type */
            uint8_t type = rbuf[pos];

            switch (type) {
                case CONNACK:       on_ack(type, 0, 2, 0);              break;
                case WILLTOPICREQ:  on_ack(type, 0, 0, 0);              break;
                case WILLMSGREQ:    on_ack(type, 0, 0, 0);              break;
                case REGACK:        on_ack(type, 4, 6, 2);              break;
                case PUBLISH:       on_publish((size_t)pkt_len, pos);   break;
                case PUBACK:        on_ack(type, 4, 6, 0);              break;
                case SUBACK:        on_ack(type, 5, 7, 3);              break;
                case UNSUBACK:      on_ack(type, 2, 0, 0);              break;
                case PINGREQ:       on_pingreq(&remote);                break;
                case PINGRESP:      on_pingresp();                      break;
                case DISCONNECT:    on_disconnect();                    break;
                case WILLTOPICRESP: on_ack(type, 0, 0, 0);              break;
                case WILLMSGRESP:   on_ack(type, 0, 0, 0);              break;
                default:
                    LOG_DEBUG("[emcute] received unexpected type [%s]\n",
                              emcute_type_str(type));
            }
        }

        uint32_t now = xtimer_now_usec();
        if ((now - start) >= (EMCUTE_KEEPALIVE * US_PER_SEC)) {
            send_ping();
            start = now;
            t_out = (EMCUTE_KEEPALIVE * US_PER_SEC);
        }
        else {
            t_out = (EMCUTE_KEEPALIVE * US_PER_SEC) - (now - start);
        }
    }
}
Exemplo n.º 13
0
Arquivo: target.c Projeto: A-Paul/RIOT
clock_time_t hal_getSec(void)
{
    return (clock_time_t)xtimer_now_usec() / US_PER_SEC;
}
Exemplo n.º 14
0
Arquivo: target.c Projeto: A-Paul/RIOT
clock_time_t hal_getTick(void)
{
    return (clock_time_t)xtimer_now_usec();
}
Exemplo n.º 15
0
/*
 * Main request handler: generates response PDU in the provided buffer.
 *
 * Caller must finish the PDU and send it.
 *
 * return length of response pdu, or < 0 if can't handle
 */
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
                                                         sock_udp_ep_t *remote)
{
    coap_resource_t *resource  = NULL;
    gcoap_listener_t *listener = NULL;
    sock_udp_ep_t *observer    = NULL;
    gcoap_observe_memo_t *memo = NULL;
    gcoap_observe_memo_t *resource_memo = NULL;

    switch (_find_resource(pdu, &resource, &listener)) {
        case GCOAP_RESOURCE_WRONG_METHOD:
            return gcoap_response(pdu, buf, len, COAP_CODE_METHOD_NOT_ALLOWED);
        case GCOAP_RESOURCE_NO_PATH:
            return gcoap_response(pdu, buf, len, COAP_CODE_PATH_NOT_FOUND);
        case GCOAP_RESOURCE_FOUND:
            _find_obs_memo_resource(&resource_memo, resource);
            break;
    }

    if (coap_get_observe(pdu) == COAP_OBS_REGISTER) {
        int empty_slot = _find_obs_memo(&memo, remote, pdu);
        /* record observe memo */
        if (memo == NULL) {
            if ((resource_memo != NULL)
                    && _endpoints_equal(remote, resource_memo->observer)) {
                /* observer re-registering with new token */
                memo = resource_memo;
                observer = resource_memo->observer;
            }
            else if ((empty_slot >= 0) && (resource_memo == NULL)) {
                int obs_slot = _find_observer(&observer, remote);
                /* cache new observer */
                if (observer == NULL) {
                    if (obs_slot >= 0) {
                        observer = &_coap_state.observers[obs_slot];
                        memcpy(observer, remote, sizeof(sock_udp_ep_t));
                    } else {
                        DEBUG("gcoap: can't register observer\n");
                    }
                }
                if (observer != NULL) {
                    memo = &_coap_state.observe_memos[empty_slot];
                }
            }
            if (memo == NULL) {
                coap_clear_observe(pdu);
                DEBUG("gcoap: can't register observe memo\n");
            }
        }
        if (memo != NULL) {
            memo->observer  = observer;
            memo->resource  = resource;
            memo->token_len = coap_get_token_len(pdu);
            if (memo->token_len) {
                memcpy(&memo->token[0], pdu->token, memo->token_len);
            }
            DEBUG("gcoap: Registered observer for: %s\n", memo->resource->path);
            /* generate initial notification value */
            uint32_t now       = xtimer_now_usec();
            pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF;
        }