<commit_before>/******************************************************************************
Copyright 2008, Freie Universitaet Berlin (FUB). All rights reserved.

These sources were developed at the Freie Universitaet Berlin, Computer Systems
and Telematics group (http://cst.mi.fu-berlin.de).
-------------------------------------------------------------------------------
This file is part of RIOT.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

RIOT is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see http://www.gnu.org/licenses/ .
--------------------------------------------------------------------------------
For further information and questions please use the web site
	http://scatterweb.mi.fu-berlin.de
and the mailinglist (subscription via web site)
	<EMAIL>-<EMAIL>.de
*******************************************************************************/

/**
 * @file
 * @ingroup		dev_cc110x
 * @brief		ScatterWeb MSB-A2 mac-layer
 *
 * @author      Freie Universität Berlin, Computer Systems & Telematics
 * @author		<NAME> <<EMAIL>bra@inf.fu-berlin.de>
 * @author		<NAME> <hwill@inf.fu-berlin.de>
 * @version     $Revision: 2128 $
 *
 * @note    	$Id: cc1100-csmaca-mac.c 2128 2010-05-12 12:07:59Z hillebra $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cc1100.h>
#include <cc1100_phy.h>
#include <cc1100-csmaca-mac.h>
#include <protocol-multiplex.h>

#include "hwtimer.h"
#include <vtimer.h>

/*---------------------------------------------------------------------------*/

#define COLLISION_STATE_INITIAL 1
#define COLLISION_STATE_MEASURE 2
#define COLLISION_STATE_KEEP    3

static uint32_t send_csmaca_calls = 0;
static uint32_t send_csmaca_calls_cs_timeout = 0;
static int collision_count;
static double collisions_per_sec = 0;
static int collision_state = COLLISION_STATE_INITIAL;
static uint64_t collision_measurement_start = 0;

volatile static int cs_hwtimer_id = -1;
volatile static int cs_timeout_flag = 0;

/*---------------------------------------------------------------------------*/
static void cs_timeout_cb(void* ptr)
{
	cs_timeout_flag = 1;
}

/*---------------------------------------------------------------------------*/
int cc1100_send_csmaca(radio_address_t address, protocol_t protocol, int priority, char *payload, int payload_len)
{
	uint16_t min_window_size;
	uint16_t max_window_size;
	uint16_t difs;
	uint16_t slottime;

	switch (priority)
	{
		case PRIORITY_ALARM:
			min_window_size = PRIO_ALARM_MIN_WINDOW_SIZE;
			max_window_size = PRIO_ALARM_MAX_WINDOW_SIZE;
			difs = PRIO_ALARM_DIFS;
			slottime = PRIO_ALARM_SLOTTIME;
			break;
		case PRIORITY_WARNING:
			min_window_size = PRIO_WARN_MIN_WINDOW_SIZE;
			max_window_size = PRIO_WARN_MAX_WINDOW_SIZE;
			difs = PRIO_WARN_DIFS;
			slottime = PRIO_WARN_SLOTTIME;
			break;
		default:
			min_window_size = PRIO_DATA_MIN_WINDOW_SIZE;
			max_window_size = PRIO_DATA_MAX_WINDOW_SIZE;
			difs = PRIO_DATA_DIFS;
			slottime = PRIO_DATA_SLOTTIME;
	}

	// Calculate collisions per second
	if (collision_state == COLLISION_STATE_INITIAL) {
		timex_t now;
		vtimer_now(&now);
		collision_measurement_start =  now.microseconds;
		collision_count = 0;
		collisions_per_sec = 0;
		collision_state = COLLISION_STATE_MEASURE;
	} else if (collision_state == COLLISION_STATE_MEASURE) {
			timex_t now;
			vtimer_now(&now);
			uint64_t timespan = now.microseconds - collision_measurement_start;
			if (timespan > 1000000) {
				collisions_per_sec = (collision_count * 1000000) / (double) timespan;
			if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
				timex_t now;
				vtimer_now(&now);
				collision_measurement_start = now.microseconds;
				collision_state = COLLISION_STATE_KEEP;
			} else if (collisions_per_sec > 2.2) {
				timex_t now;
				vtimer_now(now);
				collision_measurement_start = now.microseconds;
				collision_state = COLLISION_STATE_KEEP;
			} else {
				collision_state = COLLISION_STATE_INITIAL;
			}
		}
	} else if (collision_state == COLLISION_STATE_KEEP) {
		timex_t now;
		vtimer_now(&now);
        uint64_t timespan = now.microseconds - collision_measurement_start;
        if (timespan > 5000000) {
			collision_state = COLLISION_STATE_INITIAL;
		}
	}

	// Adjust initial window size according to collision rate
	if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
		min_window_size *= 2;
	} else if (collisions_per_sec > 2.2) {
		min_window_size *= 4;
	}

	uint16_t windowSize = min_window_size;		// Start with window size of PRIO_XXX_MIN_WINDOW_SIZE
	uint16_t backoff = 0;						// Backoff between 1 and windowSize
	uint32_t total;								// Holds the total wait time before send try<commit_msg>add missing &<commit_after>/******************************************************************************
Copyright 2008, Freie Universitaet Berlin (FUB). All rights reserved.

These sources were developed at the Freie Universitaet Berlin, Computer Systems
and Telematics group (http://cst.mi.fu-berlin.de).
-------------------------------------------------------------------------------
This file is part of RIOT.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

RIOT is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see http://www.gnu.org/licenses/ .
--------------------------------------------------------------------------------
For further information and questions please use the web site
	http://scatterweb.mi.fu-berlin.de
and the mailinglist (subscription via web site)
	<EMAIL>-<EMAIL>lin.de
*******************************************************************************/

/**
 * @file
 * @ingroup		dev_cc110x
 * @brief		ScatterWeb MSB-A2 mac-layer
 *
 * @author      Freie Universität Berlin, Computer Systems & Telematics
 * @author		<NAME> <<EMAIL>bra<EMAIL>.fu-berlin.de>
 * @author		<NAME> <<EMAIL>.fu-berlin.de>
 * @version     $Revision: 2128 $
 *
 * @note    	$Id: cc1100-csmaca-mac.c 2128 2010-05-12 12:07:59Z hillebra $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cc1100.h>
#include <cc1100_phy.h>
#include <cc1100-csmaca-mac.h>
#include <protocol-multiplex.h>

#include "hwtimer.h"
#include <vtimer.h>

/*---------------------------------------------------------------------------*/

#define COLLISION_STATE_INITIAL 1
#define COLLISION_STATE_MEASURE 2
#define COLLISION_STATE_KEEP    3

static uint32_t send_csmaca_calls = 0;
static uint32_t send_csmaca_calls_cs_timeout = 0;
static int collision_count;
static double collisions_per_sec = 0;
static int collision_state = COLLISION_STATE_INITIAL;
static uint64_t collision_measurement_start = 0;

volatile static int cs_hwtimer_id = -1;
volatile static int cs_timeout_flag = 0;

/*---------------------------------------------------------------------------*/
static void cs_timeout_cb(void* ptr)
{
	cs_timeout_flag = 1;
}

/*---------------------------------------------------------------------------*/
int cc1100_send_csmaca(radio_address_t address, protocol_t protocol, int priority, char *payload, int payload_len)
{
	uint16_t min_window_size;
	uint16_t max_window_size;
	uint16_t difs;
	uint16_t slottime;

	switch (priority)
	{
		case PRIORITY_ALARM:
			min_window_size = PRIO_ALARM_MIN_WINDOW_SIZE;
			max_window_size = PRIO_ALARM_MAX_WINDOW_SIZE;
			difs = PRIO_ALARM_DIFS;
			slottime = PRIO_ALARM_SLOTTIME;
			break;
		case PRIORITY_WARNING:
			min_window_size = PRIO_WARN_MIN_WINDOW_SIZE;
			max_window_size = PRIO_WARN_MAX_WINDOW_SIZE;
			difs = PRIO_WARN_DIFS;
			slottime = PRIO_WARN_SLOTTIME;
			break;
		default:
			min_window_size = PRIO_DATA_MIN_WINDOW_SIZE;
			max_window_size = PRIO_DATA_MAX_WINDOW_SIZE;
			difs = PRIO_DATA_DIFS;
			slottime = PRIO_DATA_SLOTTIME;
	}

	// Calculate collisions per second
	if (collision_state == COLLISION_STATE_INITIAL) {
		timex_t now;
		vtimer_now(&now);
		collision_measurement_start =  now.microseconds;
		collision_count = 0;
		collisions_per_sec = 0;
		collision_state = COLLISION_STATE_MEASURE;
	} else if (collision_state == COLLISION_STATE_MEASURE) {
			timex_t now;
			vtimer_now(&now);
			uint64_t timespan = now.microseconds - collision_measurement_start;
			if (timespan > 1000000) {
				collisions_per_sec = (collision_count * 1000000) / (double) timespan;
			if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
				timex_t now;
				vtimer_now(&now);
				collision_measurement_start = now.microseconds;
				collision_state = COLLISION_STATE_KEEP;
			} else if (collisions_per_sec > 2.2) {
				timex_t now;
				vtimer_now(&now);
				collision_measurement_start = now.microseconds;
				collision_state = COLLISION_STATE_KEEP;
			} else {
				collision_state = COLLISION_STATE_INITIAL;
			}
		}
	} else if (collision_state == COLLISION_STATE_KEEP) {
		timex_t now;
		vtimer_now(&now);
        uint64_t timespan = now.microseconds - collision_measurement_start;
        if (timespan > 5000000) {
			collision_state = COLLISION_STATE_INITIAL;
		}
	}

	// Adjust initial window size according to collision rate
	if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
		min_window_size *= 2;
	} else if (collisions_per_sec > 2.2) {
		min_window_size *= 4;
	}

	uint16_t windowSize = min_window_size;		// Start with window size of PRIO_XXX_MIN_WINDOW_SIZE
	uint16_t backoff = 0;						// Backoff between 1 and windowSize
	uint32_t total;								// Holds the total wait time before send try
{'fraction_non_alphanumeric': 0.18117991223793273, 'fraction_numerical': 0.024183325207215993, 'mean_word_length': 3.120530333467256, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3811335', 'n_tokens_mistral': 3740, 'n_tokens_neox': 3289, 'n_words': 847}