<commit_before>/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
 *
 * Copyright (c) 2018 <NAME>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <gpio.h>

#include "common.h"
#include "ble_mesh.h"
#include "device_composition.h"
#include "state_binding.h"
#include "transition.h"

u8_t transition_type, default_tt;
u32_t *ptr_counter;
struct k_timer *ptr_timer = &dummy_timer;

struct transition lightness_transition, temp_transition;

/* Function to calculate Remaining Time (Start) */

void calculate_rt(struct transition *transition)
{
	u8_t steps, resolution;
	s32_t duration_remainder;
	s64_t now;

	if (transition->just_started) {
		transition->rt = transition->tt;
	} else {
		now = k_uptime_get();
		duration_remainder = transition->total_duration -
				     (now - transition->start_timestamp);

		if (duration_remainder > 620000) {
			/* > 620 seconds -> resolution = 0b11 [10 minutes] */
			resolution = 0x03;
			steps = duration_remainder / 600000;
		} else if (duration_remainder > 62000) {
			/* > 62 seconds -> resolution = 0b10 [10 seconds] */
			resolution = 0x02;
			steps = duration_remainder / 10000;
		} else if (duration_remainder > 6200) {
			/* > 6.2 seconds -> resolution = 0b01 [1 seconds] */
			resolution = 0x01;
			steps = duration_remainder / 1000;
		} else if (duration_remainder > 0) {
			/* <= 6.2 seconds -> resolution = 0b00 [100 ms] */
			resolution = 0x00;
			steps = duration_remainder / 100;
		} else {
			resolution = 0x00;
			steps = 0x00;
		}

		transition->rt = (resolution << 6) | steps;
	}
}

/* Function to calculate Remaining Time (End) */

static void bound_states_transition_type_reassignment(u8_t type)
{
	switch (type) {
	case ONOFF:
	case LEVEL:
	case ACTUAL:
	case LINEAR:
		light_ctl_srv_user_data.transition = &lightness_transition;
		break;
	case CTL:
		light_ctl_srv_user_data.transition = &lightness_transition;
		gen_level_srv_s0_user_data.transition = &lightness_transition;
		break;
	case LEVEL_TEMP:
	case CTL_TEMP:
		gen_level_srv_s0_user_data.transition = &temp_transition;
		light_ctl_srv_user_data.transition = &temp_transition;
		break;
	default:
		break;
	}
}

static void tt_values_calculator(struct transition *transition)
{
	u8_t steps_multiplier, resolution;

	resolution = (transition->tt >> 6);
	steps_multiplier = (transition->tt & 0x3F);

	switch (resolution) {
	case 0:	/* 100ms */
		transition->total_duration = steps_multiplier * 100;
		break;
	case 1:	/* 1 second */
		transition->total_duration = steps_multiplier * 1000;
		break;
	case 2:	/* 10 seconds */
		transition->total_duration = steps_multiplier * 10000;
		break;
	case 3:	/* 10 minutes */
		transition->total_duration = steps_multiplier * 600000;
		break;
	}

	transition->counter = ((float) transition->total_duration / 100);

	if (transition->counter > DEVICE_SPECIFIC_RESOLUTION) {
		transition->counter = DEVICE_SPECIFIC_RESOLUTION;
	}

	ptr_counter = &transition->counter;
}

void onoff_tt_values(struct generic_onoff_state *state, u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(ONOFF);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	calculate_lightness_target_values(ONOFF);

	light_lightness_srv_user_data.tt_delta_actual =
		((float) (light_lightness_srv_user_data.actual -
			  light_lightness_srv_user_data.target_actual) /
		 state->transition->counter);
}

void level_tt_values(struct generic_level_state *state, u8_t tt, u8_t delay)
{
	if (state == &gen_level_srv_root_user_data) {
		bound_states_transition_type_reassignment(LEVEL);
	} else if (state == &gen_level_srv_s0_user_data) {
		bound_states_transition_type_reassignment(LEVEL_TEMP);
	}
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	if (state == &gen_level_srv_root_user_data) {
		calculate_lightness_target_values(LEVEL);
	} else if (state == &gen_level_srv_s0_user_data) {
		calculate_temp_target_values(LEVEL_TEMP);
	}

	state->tt_delta = ((float) (state->level - state->target_level) /
			   state->transition->counter);
}

void light_lightness_actual_tt_values(struct light_lightness_state *state,
				      u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(ACTUAL);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	calculate_lightness_target_values(ACTUAL);

	state->tt_delta_actual =
		((float) (state->actual - state->target_actual) /
		 state->transition->counter);
}

void light_lightness_linear_tt_values(struct light_lightness_state *state,
				      u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(LINEAR);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	calculate_lightness_target_values(LINEAR);

	state->tt_delta_linear =
		((float) (state->linear - state->target_linear) /
		 state->transition->counter);
}

void light_ctl_tt_values(struct light_ctl_state *state, u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(CTL);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	calculate_lightness_target_values(CTL);

	state->tt_delta_lightness =
		((float) (state->lightness - state->target_lightness) /
		 state->transition->counter);

	state->tt_delta_temp =
		((float) (state->temp - state->target_temp) /
		 state->transition->counter);

	state->tt_delta_duv =
		((float) (state->delta_uv - state->target_delta_uv) /
		 state->transition->counter);
}

void light_ctl_temp_tt_values(struct light_ctl_state *state,
			      u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(CTL_TEMP);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	calculate_temp_target_values(CTL_TEMP);

	state->tt_delta_temp = ((float) (state->temp - state->target_temp) /
				state->transition->counter);

	state->tt_delta_duv =
		((float) (state->delta_uv - state->target_delta_uv) /
		 state->transition->counter);
}

/* Timers related handlers & threads (Start) */
static void onoff_work_handler(struct k_work *work)
{
	struct generic_onoff_state *state = &gen_onoff_srv_root_user_data;

	if (state->transition->just_started) {<commit_msg>samples: mesh: nrf52: removed bugs in Target values calculation<commit_after>/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
 *
 * Copyright (c) 2018 <NAME>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <gpio.h>

#include "common.h"
#include "ble_mesh.h"
#include "device_composition.h"
#include "state_binding.h"
#include "transition.h"

u8_t transition_type, default_tt;
u32_t *ptr_counter;
struct k_timer *ptr_timer = &dummy_timer;

struct transition lightness_transition, temp_transition;

/* Function to calculate Remaining Time (Start) */

void calculate_rt(struct transition *transition)
{
	u8_t steps, resolution;
	s32_t duration_remainder;
	s64_t now;

	if (transition->just_started) {
		transition->rt = transition->tt;
	} else {
		now = k_uptime_get();
		duration_remainder = transition->total_duration -
				     (now - transition->start_timestamp);

		if (duration_remainder > 620000) {
			/* > 620 seconds -> resolution = 0b11 [10 minutes] */
			resolution = 0x03;
			steps = duration_remainder / 600000;
		} else if (duration_remainder > 62000) {
			/* > 62 seconds -> resolution = 0b10 [10 seconds] */
			resolution = 0x02;
			steps = duration_remainder / 10000;
		} else if (duration_remainder > 6200) {
			/* > 6.2 seconds -> resolution = 0b01 [1 seconds] */
			resolution = 0x01;
			steps = duration_remainder / 1000;
		} else if (duration_remainder > 0) {
			/* <= 6.2 seconds -> resolution = 0b00 [100 ms] */
			resolution = 0x00;
			steps = duration_remainder / 100;
		} else {
			resolution = 0x00;
			steps = 0x00;
		}

		transition->rt = (resolution << 6) | steps;
	}
}

/* Function to calculate Remaining Time (End) */

static void bound_states_transition_type_reassignment(u8_t type)
{
	switch (type) {
	case ONOFF:
	case LEVEL:
	case ACTUAL:
	case LINEAR:
		light_ctl_srv_user_data.transition = &lightness_transition;
		break;
	case CTL:
		light_ctl_srv_user_data.transition = &lightness_transition;
		gen_level_srv_s0_user_data.transition = &lightness_transition;
		break;
	case LEVEL_TEMP:
	case CTL_TEMP:
		gen_level_srv_s0_user_data.transition = &temp_transition;
		light_ctl_srv_user_data.transition = &temp_transition;
		break;
	default:
		break;
	}
}

static void tt_values_calculator(struct transition *transition)
{
	u8_t steps_multiplier, resolution;

	resolution = (transition->tt >> 6);
	steps_multiplier = (transition->tt & 0x3F);

	switch (resolution) {
	case 0:	/* 100ms */
		transition->total_duration = steps_multiplier * 100;
		break;
	case 1:	/* 1 second */
		transition->total_duration = steps_multiplier * 1000;
		break;
	case 2:	/* 10 seconds */
		transition->total_duration = steps_multiplier * 10000;
		break;
	case 3:	/* 10 minutes */
		transition->total_duration = steps_multiplier * 600000;
		break;
	}

	transition->counter = ((float) transition->total_duration / 100);

	if (transition->counter > DEVICE_SPECIFIC_RESOLUTION) {
		transition->counter = DEVICE_SPECIFIC_RESOLUTION;
	}

	ptr_counter = &transition->counter;
}

void onoff_tt_values(struct generic_onoff_state *state, u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(ONOFF);
	calculate_lightness_target_values(ONOFF);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	light_lightness_srv_user_data.tt_delta_actual =
		((float) (light_lightness_srv_user_data.actual -
			  light_lightness_srv_user_data.target_actual) /
		 state->transition->counter);
}

void level_tt_values(struct generic_level_state *state, u8_t tt, u8_t delay)
{
	if (state == &gen_level_srv_root_user_data) {
		bound_states_transition_type_reassignment(LEVEL);
		calculate_lightness_target_values(LEVEL);
	} else if (state == &gen_level_srv_s0_user_data) {
		bound_states_transition_type_reassignment(LEVEL_TEMP);
		calculate_temp_target_values(LEVEL_TEMP);
	}
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	state->tt_delta = ((float) (state->level - state->target_level) /
			   state->transition->counter);
}

void light_lightness_actual_tt_values(struct light_lightness_state *state,
				      u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(ACTUAL);
	calculate_lightness_target_values(ACTUAL);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	state->tt_delta_actual =
		((float) (state->actual - state->target_actual) /
		 state->transition->counter);
}

void light_lightness_linear_tt_values(struct light_lightness_state *state,
				      u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(LINEAR);
	calculate_lightness_target_values(LINEAR);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	state->tt_delta_linear =
		((float) (state->linear - state->target_linear) /
		 state->transition->counter);
}

void light_ctl_tt_values(struct light_ctl_state *state, u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(CTL);
	calculate_lightness_target_values(CTL);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	state->tt_delta_lightness =
		((float) (state->lightness - state->target_lightness) /
		 state->transition->counter);

	state->tt_delta_temp =
		((float) (state->temp - state->target_temp) /
		 state->transition->counter);

	state->tt_delta_duv =
		((float) (state->delta_uv - state->target_delta_uv) /
		 state->transition->counter);
}

void light_ctl_temp_tt_values(struct light_ctl_state *state,
			      u8_t tt, u8_t delay)
{
	bound_states_transition_type_reassignment(CTL_TEMP);
	calculate_temp_target_values(CTL_TEMP);
	state->transition->tt = tt;
	state->transition->delay = delay;

	if (tt != 0) {
		tt_values_calculator(state->transition);
	} else {
		return;
	}

	state->transition->quo_tt = state->transition->total_duration /
					state->transition->counter;

	state->tt_delta_temp = ((float) (state->temp - state->target_temp) /
				state->transition->counter);

	state->tt_delta_duv =
		((float) (state->delta_uv - state->target_delta_uv) /
		 state->transition->counter);
}

/* Timers related handlers & threads (Start) */
static void onoff_work_handler(struct k_work *work)
{
	struct generic_onoff_state *state = &gen_onoff_srv_root_user_data;

	if (state->transition->just_started) {
{'fraction_non_alphanumeric': 0.12535072951739618, 'fraction_numerical': 0.021955667789001123, 'mean_word_length': 4.6107831562377015, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 289, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '19504', 'n_tokens_mistral': 5537, 'n_tokens_neox': 4919, 'n_words': 903}