mirror of
https://github.com/nodemcu/nodemcu-firmware.git
synced 2025-01-16 20:52:57 +08:00
526d21dab4
The PR removed the bulk of non-newlib headers from the NodeMCU source base. app/libc has now been cut down to the bare minimum overrides to shadow the corresponding functions in the SDK's libc. The old c_xyz.h headerfiles have been nuked in favour of the standard <xyz.h> headers, with a few exceptions over in sdk-overrides. Again, shipping a libc.a without headers is a terrible thing to do. We're still living on a prayer that libc was configured the same was as a default-configured xtensa gcc toolchain assumes it is. That part I cannot do anything about, unfortunately, but it's no worse than it has been before. This enables our source files to compile successfully using the standard header files, and use the typical malloc()/calloc()/realloc()/free(), the strwhatever()s and memwhatever()s. These end up, through macro and linker magic, mapped to the appropriate SDK or ROM functions.
83 lines
2.4 KiB
C
83 lines
2.4 KiB
C
#include "node.h"
|
|
#include "coap_timer.h"
|
|
#include "os_type.h"
|
|
#include "osapi.h"
|
|
#include "pm/swtimer.h"
|
|
|
|
static os_timer_t coap_timer;
|
|
static coap_tick_t basetime = 0;
|
|
|
|
void coap_timer_elapsed(coap_tick_t *diff){
|
|
coap_tick_t now = system_get_time() / 1000; // coap_tick_t is in ms. also sys_timer
|
|
if(now>=basetime){
|
|
*diff = now-basetime;
|
|
} else {
|
|
*diff = now + SYS_TIME_MAX -basetime;
|
|
}
|
|
basetime = now;
|
|
}
|
|
|
|
void coap_timer_tick(void *arg){
|
|
if( !arg )
|
|
return;
|
|
coap_queue_t **queue = (coap_queue_t **)arg;
|
|
if( !(*queue) )
|
|
return;
|
|
|
|
coap_queue_t *node = coap_pop_next( queue );
|
|
/* re-initialize timeout when maximum number of retransmissions are not reached yet */
|
|
if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT) {
|
|
node->retransmit_cnt++;
|
|
node->t = node->timeout << node->retransmit_cnt;
|
|
|
|
NODE_DBG("** retransmission #%d of transaction %d\n",
|
|
node->retransmit_cnt, (((uint16_t)(node->pdu->pkt->hdr.id[0]))<<8)+node->pdu->pkt->hdr.id[1]);
|
|
node->id = coap_send(node->pconn, node->pdu);
|
|
if (COAP_INVALID_TID == node->id) {
|
|
NODE_DBG("retransmission: error sending pdu\n");
|
|
coap_delete_node(node);
|
|
} else {
|
|
coap_insert_node(queue, node);
|
|
}
|
|
} else {
|
|
/* And finally delete the node */
|
|
coap_delete_node( node );
|
|
}
|
|
|
|
coap_timer_start(queue);
|
|
}
|
|
|
|
void coap_timer_setup(coap_queue_t ** queue, coap_tick_t t){
|
|
os_timer_disarm(&coap_timer);
|
|
os_timer_setfn(&coap_timer, (os_timer_func_t *)coap_timer_tick, queue);
|
|
SWTIMER_REG_CB(coap_timer_tick, SWTIMER_RESUME);
|
|
//coap_timer_tick processes a queue, my guess is that it is ok to resume the timer from where it left off
|
|
os_timer_arm(&coap_timer, t, 0); // no repeat
|
|
}
|
|
|
|
void coap_timer_stop(void){
|
|
os_timer_disarm(&coap_timer);
|
|
}
|
|
|
|
void coap_timer_update(coap_queue_t ** queue){
|
|
if (!queue)
|
|
return;
|
|
coap_tick_t diff = 0;
|
|
coap_queue_t *first = *queue;
|
|
coap_timer_elapsed(&diff); // update: basetime = now, diff = now - oldbase, means time elapsed
|
|
if (first) {
|
|
// diff ms time is elapsed, re-calculate the first node->t
|
|
if (first->t >= diff){
|
|
first->t -= diff;
|
|
} else {
|
|
first->t = 0; // when timer enabled, time out almost immediately
|
|
}
|
|
}
|
|
}
|
|
|
|
void coap_timer_start(coap_queue_t ** queue){
|
|
if(*queue){ // if there is node in the queue, set timeout to its ->t.
|
|
coap_timer_setup(queue, (*queue)->t);
|
|
}
|
|
}
|