Beispiel #1
0
// TODO support 64 bits values
void timer_msleep(int milliseconds) {
  // Implements busy waiting
  int startTime = timer_get_timestamp();
  int elapseTime = timer_get_timestamp() - startTime;
  while (milliseconds > elapseTime) {
      elapseTime = timer_get_timestamp() - startTime;
  }
}
Beispiel #2
0
void my_timer_msleep(int milliseconds) {
  // Implements busy waiting
  int startTime = timer_get_timestamp();
  int elapseTime = timer_get_timestamp() - startTime;
  while (milliseconds > elapseTime) {
    if (thread_current()->status == THREAD_RUNNING)
      sema_down(&sleep_sema);
    elapseTime = timer_get_timestamp() - startTime;
  }
}
void transport_control_step(void)
{
    uint64_t next_time;
    uint64_t time_now = timer_get_timestamp() + global_time;
    trickle_time_update(time_now);
    uint32_t error_code = mesh_srv_get_next_processing_time(&next_time);
    if (error_code != NRF_SUCCESS)
    {
        return;
    }

    if (next_time < time_now)
    {
        async_event_t async_evt;
        async_evt.callback.generic = trickle_step_callback;
        async_evt.type = EVENT_TYPE_GENERIC;
        event_handler_push(&async_evt);
    }
    else
    {
        if (next_time < global_time + timeslot_get_end_time())
        {
            timer_abort(step_timer_index);
            step_timer_index = timer_order_cb(next_time - global_time, trickle_step_callback);
        }
    }
}
Beispiel #4
0
int logfile_del(char *filename)
{
	logfile_t *log;
	int i;

	log = NULL;
	for (i = 0; i < nlogfiles; i++) {
		log = &logfiles[i];
		if (!strcmp(log->filename, filename)) break;
		log = NULL;
	}

	if (log == NULL) return(-1);
		
	if (log->fp) {
		flushlog(log, timer_get_timestamp());
		fclose(log->fp);
	}

	if (log->last_msg) free(log->last_msg);
	if (log->filename) free(log->filename);

	if (nlogfiles == 1) {
		free(logfiles);
		logfiles = NULL;
	} else {		
		memmove(logfiles + i, logfiles + i + 1, (nlogfiles - i - 1) * sizeof(logfile_t));
		logfiles = realloc(logfiles, (nlogfiles - 1) * sizeof(logfile_t));
	}

	nlogfiles--;

	return(0);
}
Beispiel #5
0
/* radio callback, executed in STACK_LOW */
static void tx_cb(uint8_t* data)
{
    rbc_mesh_event_t tx_event;
    mesh_adv_data_t* p_adv_data = mesh_packet_adv_data_get((mesh_packet_t*) data);
    bool doing_tx_event = false;
    if (p_adv_data != NULL && 
        vh_tx_event_flag_get(p_adv_data->handle, &doing_tx_event) == NRF_SUCCESS 
        && doing_tx_event
    )
    {
        tx_event.event_type = RBC_MESH_EVENT_TYPE_TX;
        tx_event.value_handle = p_adv_data->handle;
        tx_event.data = p_adv_data->data;
        tx_event.data_len = p_adv_data->adv_data_length - MESH_PACKET_ADV_OVERHEAD;
        tx_event.version_delta = 0;

        APP_ERROR_CHECK(rbc_mesh_event_push(&tx_event));
#if RBC_MESH_SERIAL
        mesh_aci_rbc_event_handler(&tx_event);
#endif
    }

    mesh_packet_ref_count_dec((mesh_packet_t*) data); /* radio ref removed (pushed in tc_tx) */
    vh_order_update(timer_get_timestamp()); /* tell the vh, so that it can push more updates */
}
Beispiel #6
0
/* Flush the logfiles to disk
 */
void flushlogs()
{
	char *ts;
	int i;

	ts = timer_get_timestamp();
	for (i = 0; i < nlogfiles; i++) {
		flushlog(&logfiles[i], ts);
	}
}
Beispiel #7
0
uint32_t timeslot_get_remaining_time(void)
{
    if (!g_is_in_timeslot)
    {
        return 0;
    }

    uint32_t timestamp = timer_get_timestamp();
    if (timestamp > g_timeslot_length - TIMESLOT_END_SAFETY_MARGIN_US)
    {
        return 0;
    }
    else
    {
        return (g_timeslot_length - TIMESLOT_END_SAFETY_MARGIN_US - timestamp);
    }
}
Beispiel #8
0
/* radio callback, executed in STACK_LOW */
static void rx_cb(uint8_t* data, bool success, uint32_t crc)
{
    if (success)
    {
        async_event_t evt;
        evt.type = EVENT_TYPE_PACKET;
        evt.callback.packet.payload = data;
        evt.callback.packet.crc = crc;
        evt.callback.packet.timestamp = timer_get_timestamp();
        mesh_packet_ref_count_inc((mesh_packet_t*) data); /* event handler has a ref */
        if (event_handler_push(&evt) != NRF_SUCCESS)
        {
            g_state.queue_saturation = true;
            mesh_packet_ref_count_dec((mesh_packet_t*) data); /* event handler lost its ref */
        }
    }

    /* no longer needed in this context */
    mesh_packet_ref_count_dec((mesh_packet_t*) data);
}
/**
* @brief Handle trickle timing events
*/
static void trickle_step_callback(void)
{
    TICK_PIN(6);
    /* check if timeslot is about to end */
    if (timeslot_get_remaining_time() < RADIO_SAFETY_TIMING_US)
        return;

    uint64_t time_now = global_time + timer_get_timestamp();
    trickle_time_update(time_now);

    packet_t packet;
    bool has_anything_to_send = false;

    mesh_srv_packet_assemble(&packet, PACKET_DATA_MAX_LEN * PACKET_MAX_CHAIN_LEN,
        &has_anything_to_send);

    if (has_anything_to_send)
    {
        TICK_PIN(PIN_MESH_TX);
        radio_disable();

        uint8_t packet_and_addr_type = PACKET_TYPE_ADV_NONCONN |
            ((packet.sender.addr_type == BLE_GAP_ADDR_TYPE_PUBLIC)?
            0 :
            PACKET_ADDR_TYPE_MASK);

        uint8_t* temp_data_ptr = &packet.data[0];
        uint8_t* tx_data_ptr = &tx_data[0];
        tx_data_ptr[PACKET_TYPE_POS] = packet_and_addr_type;

        /* Code structured for packet chaining, although this is yet
         to be implemented. */
        do
        {
            uint8_t min_len = ((packet.length > PACKET_DATA_MAX_LEN)?
                PACKET_DATA_MAX_LEN :
                packet.length);

            tx_data_ptr[PACKET_PADDING_POS] = 0;
            tx_data_ptr[PACKET_LENGTH_POS] = (min_len + PACKET_ADDR_LEN);
            tx_data_ptr[PACKET_TYPE_POS] = packet_and_addr_type;

            memcpy(&tx_data_ptr[PACKET_ADDR_POS], packet.sender.addr, PACKET_ADDR_LEN);
            memcpy(&tx_data_ptr[PACKET_DATA_POS], &temp_data_ptr[0], min_len);

            radio_event_t tx_event;
            tx_event.access_address = 0;
            rbc_mesh_channel_get(&tx_event.channel);
            tx_event.event_type = RADIO_EVENT_TYPE_TX;
            tx_event.packet_ptr = &tx_data_ptr[0];
            tx_event.start_time = 0;
            tx_event.callback.tx = NULL;

            radio_order(&tx_event);
            TICK_PIN(0);
        } while (0);

        order_search(); /* search for the rest of the timeslot */
    }

    /* order next processing */
    uint64_t next_time;
    uint64_t end_time = timeslot_get_end_time();
    uint32_t error_code = mesh_srv_get_next_processing_time(&next_time);

    if (error_code == NRF_SUCCESS && next_time < global_time + end_time)
    {
        timer_abort(step_timer_index);
        step_timer_index = timer_order_cb(next_time - global_time, trickle_step_callback);
    }
}
Beispiel #10
0
static int on_putlog(int flags, const char *chan, const char *text, int len)
{
	char *ts;
	int i;

	ts = timer_get_timestamp();
	for (i = nlogfiles - 1; i >= 0; i--) {
		logfile_t *log = &logfiles[i];
		
		/* If this log is disabled, skip it */
		if (log->state != LOG_STATE_ENABLED)
			continue;

		/* If this log doesn't match, skip it. */
		if (!(log->mask & flags)) {
			continue;
		}

		if (chan[0] != '*' && log->chname[0] != '*' && irccmp(chan, log->chname)) continue;
		
		/* If it's a repeat message, don't write it again. */
		if (log->last_msg && !strcasecmp(text, log->last_msg)) {
			log->repeats++;
			continue;
		}


		/* If there was a repeated message, write the count. */
		if (log->repeats) {
			fprintf(log->fp, "%s", ts);
			fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
			log->repeats = 0;
		}

		/* Save this msg to check for repeats next time. */
		str_redup(&log->last_msg, text);

		if (log->fp == NULL) {
			if (log->fname == NULL) {
				char buf[1024];
				time_t now;

				now = time(NULL);
				strftime(buf, sizeof(buf), log->filename, localtime(&now));
				log->fname = strdup(buf);
			}	

			log->fp = fopen(log->fname, "a+");
			if (log->fp == NULL) {
				log->state = LOG_STATE_DISABLED;
				putlog(LOG_MISC, "*", _("Failed to open log file: %s"), log->fname);
				putlog(LOG_MISC, "*", _("  Check if directory (if any) exists and is read- and writeable."));
				continue;
			}
		}

		/* Now output to the file. */
		fprintf(log->fp, "%s%s\n", ts, text);
	}

	if (!backgrd || use_stderr) {
	
		if (terminal_mode) {
			/* check if HQ is on console. If yes we disable
			 * output to stdout since otherwise everything would
			 * be printed out twice. */		
			if (!terminal_enabled) {
				terminal_enabled = (partymember_lookup(TERMINAL_NICK, NULL, -1) != NULL);
			}
			if (terminal_enabled)
				return 0;

		}
		
		fprintf (stdout, "%s %s%s\n", chan, ts, text);
	}
		
	return(0);
}