diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/app_gpio.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/app_gpio.c index 396db117d8116..392bdd319d282 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/app_gpio.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/app_gpio.c @@ -7,6 +7,7 @@ #include +#include "app_gpio.h" #include "publisher.h" struct device *led_device[4]; @@ -31,6 +32,7 @@ void app_gpio_init(void) GPIO_DIR_OUT | GPIO_PUD_PULL_UP); gpio_pin_write(led_device[0], DT_ALIAS_LED0_GPIOS_PIN, 1); +#ifndef ONE_LED_ONE_BUTTON_BOARD led_device[1] = device_get_binding(DT_ALIAS_LED1_GPIOS_CONTROLLER); gpio_pin_configure(led_device[1], DT_ALIAS_LED1_GPIOS_PIN, GPIO_DIR_OUT | GPIO_PUD_PULL_UP); @@ -45,7 +47,7 @@ void app_gpio_init(void) gpio_pin_configure(led_device[3], DT_ALIAS_LED3_GPIOS_PIN, GPIO_DIR_OUT | GPIO_PUD_PULL_UP); gpio_pin_write(led_device[3], DT_ALIAS_LED3_GPIOS_PIN, 1); - +#endif /* Buttons configuration & setting */ k_work_init(&button_work, publish); @@ -61,6 +63,7 @@ void app_gpio_init(void) gpio_add_callback(button_device[0], &button_cb[0]); gpio_pin_enable_callback(button_device[0], DT_ALIAS_SW0_GPIOS_PIN); +#ifndef ONE_LED_ONE_BUTTON_BOARD button_device[1] = device_get_binding(DT_ALIAS_SW1_GPIOS_CONTROLLER); gpio_pin_configure(button_device[1], DT_ALIAS_SW1_GPIOS_PIN, (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | @@ -93,4 +96,5 @@ void app_gpio_init(void) BIT(DT_ALIAS_SW3_GPIOS_PIN)); gpio_add_callback(button_device[3], &button_cb[3]); gpio_pin_enable_callback(button_device[3], DT_ALIAS_SW3_GPIOS_PIN); +#endif } diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/common.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/common.h index 118e2b57c3559..b04fc49004401 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/common.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/common.h @@ -10,5 +10,6 @@ void update_led_gpio(void); void update_light_state(void); +void update_vnd_led_gpio(void); #endif diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/main.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/main.c index 30280ac27fb83..3058de7491fa9 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/main.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/main.c @@ -32,22 +32,20 @@ static void light_default_var_init(void) ctl->light->range_max = LIGHTNESS_MAX; ctl->light->last = LIGHTNESS_MAX; ctl->light->def = LIGHTNESS_MAX; + ctl->light->target = ctl->light->def; ctl->temp->range_min = TEMP_MIN; ctl->temp->range_max = TEMP_MAX; ctl->temp->def = TEMP_MAX; + ctl->temp->target = ctl->temp->def; ctl->duv->def = DELTA_UV_DEF; - - ctl->light_temp_def = (u32_t) ((LIGHTNESS_MAX << 16) | TEMP_MAX); - ctl->light_temp_last_tgt = (u32_t) ((LIGHTNESS_MAX << 16) | TEMP_MAX); + ctl->duv->target = ctl->duv->def; } /* This function should only get call after execution of settings_load() */ static void light_default_status_init(void) { - u16_t light_def; - /* Retrieve Range of Lightness */ if (ctl->light->range) { ctl->light->range_max = (u16_t) (ctl->light->range >> 16); @@ -60,12 +58,12 @@ static void light_default_status_init(void) ctl->temp->range_min = (u16_t) ctl->temp->range; } - /* Retrieve Default Lightness Value */ - light_def = (u16_t) (ctl->light_temp_def >> 16); - ctl->light->def = constrain_lightness(light_def); + ctl->light->last = constrain_lightness(ctl->light->last); + ctl->light->def = constrain_lightness(ctl->light->def); + ctl->light->target = constrain_lightness(ctl->light->target); - /* Retrieve Default Temperature Value */ - ctl->temp->def = (u16_t) ctl->light_temp_def; + ctl->temp->def = constrain_temperature(ctl->temp->def); + ctl->temp->target = constrain_temperature(ctl->temp->target); ctl->temp->current = ctl->temp->def; ctl->duv->current = ctl->duv->def; @@ -82,18 +80,30 @@ static void light_default_status_init(void) } break; case STATE_RESTORE: - ctl->light->current = (u16_t) (ctl->light_temp_last_tgt >> 16); - ctl->temp->current = (u16_t) ctl->light_temp_last_tgt; + ctl->light->current = ctl->light->target; + ctl->temp->current = ctl->temp->target; + ctl->duv->current = ctl->duv->target; break; } - default_tt = ctl->tt; - ctl->light->target = ctl->light->current; ctl->temp->target = ctl->temp->current; ctl->duv->target = ctl->duv->current; } +void update_vnd_led_gpio(void) +{ +#ifndef ONE_LED_ONE_BUTTON_BOARD + if (vnd_user_data.current == STATE_ON) { + /* LED2 On */ + gpio_pin_write(led_device[1], DT_ALIAS_LED1_GPIOS_PIN, 0); + } else { + /* LED2 Off */ + gpio_pin_write(led_device[1], DT_ALIAS_LED1_GPIOS_PIN, 1); + } +#endif +} + void update_led_gpio(void) { u8_t power, color; @@ -112,6 +122,7 @@ void update_led_gpio(void) gpio_pin_write(led_device[0], DT_ALIAS_LED0_GPIOS_PIN, 1); } +#ifndef ONE_LED_ONE_BUTTON_BOARD if (power < 50) { /* LED3 On */ gpio_pin_write(led_device[2], DT_ALIAS_LED2_GPIOS_PIN, 0); @@ -127,6 +138,7 @@ void update_led_gpio(void) /* LED4 Off */ gpio_pin_write(led_device[3], DT_ALIAS_LED3_GPIOS_PIN, 1); } +#endif } void update_light_state(void) diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/ble_mesh.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/ble_mesh.h index 881acbca1d033..5e9d46bd2c226 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/ble_mesh.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/ble_mesh.h @@ -16,39 +16,97 @@ #include /* Model Operation Codes */ -#define BT_MESH_MODEL_OP_GEN_ONOFF_GET BT_MESH_MODEL_OP_2(0x82, 0x01) -#define BT_MESH_MODEL_OP_GEN_ONOFF_SET BT_MESH_MODEL_OP_2(0x82, 0x02) -#define BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x03) -#define BT_MESH_MODEL_OP_GEN_ONOFF_STATUS BT_MESH_MODEL_OP_2(0x82, 0x04) - -#define BT_MESH_MODEL_OP_GEN_LEVEL_GET BT_MESH_MODEL_OP_2(0x82, 0x05) -#define BT_MESH_MODEL_OP_GEN_LEVEL_SET BT_MESH_MODEL_OP_2(0x82, 0x06) -#define BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x07) -#define BT_MESH_MODEL_OP_GEN_LEVEL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x08) -#define BT_MESH_MODEL_OP_GEN_DELTA_SET BT_MESH_MODEL_OP_2(0x82, 0x09) -#define BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0A) -#define BT_MESH_MODEL_OP_GEN_MOVE_SET BT_MESH_MODEL_OP_2(0x82, 0x0B) -#define BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0C) - -#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_STATUS BT_MESH_MODEL_OP_2(0x82, 0x10) - -#define BT_MESH_MODEL_GEN_ONPOWERUP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x12) - -#define BT_MESH_MODEL_LIGHT_LIGHTNESS_STATUS BT_MESH_MODEL_OP_2(0x82, 0x4E) -#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_STATUS \ +#define BT_MESH_MODEL_OP_GEN_ONOFF_GET BT_MESH_MODEL_OP_2(0x82, 0x01) +#define BT_MESH_MODEL_OP_GEN_ONOFF_SET BT_MESH_MODEL_OP_2(0x82, 0x02) +#define BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x03) +#define BT_MESH_MODEL_OP_GEN_ONOFF_STATUS BT_MESH_MODEL_OP_2(0x82, 0x04) + +#define BT_MESH_MODEL_OP_GEN_LEVEL_GET BT_MESH_MODEL_OP_2(0x82, 0x05) +#define BT_MESH_MODEL_OP_GEN_LEVEL_SET BT_MESH_MODEL_OP_2(0x82, 0x06) +#define BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x07) +#define BT_MESH_MODEL_OP_GEN_LEVEL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x08) +#define BT_MESH_MODEL_OP_GEN_DELTA_SET BT_MESH_MODEL_OP_2(0x82, 0x09) +#define BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0A) +#define BT_MESH_MODEL_OP_GEN_MOVE_SET BT_MESH_MODEL_OP_2(0x82, 0x0B) +#define BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0C) + +#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_GET BT_MESH_MODEL_OP_2(0x82, 0x0D) +#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_SET BT_MESH_MODEL_OP_2(0x82, 0x0E) +#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_SET_UNACK \ + BT_MESH_MODEL_OP_2(0x82, 0x0F) +#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_STATUS BT_MESH_MODEL_OP_2(0x82, 0x10) + +#define BT_MESH_MODEL_GEN_ONPOWERUP_GET BT_MESH_MODEL_OP_2(0x82, 0x11) +#define BT_MESH_MODEL_GEN_ONPOWERUP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x12) +#define BT_MESH_MODEL_GEN_ONPOWERUP_SET BT_MESH_MODEL_OP_2(0x82, 0x13) +#define BT_MESH_MODEL_GEN_ONPOWERUP_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x14) + +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_GET BT_MESH_MODEL_OP_2(0x82, 0x4B) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_SET BT_MESH_MODEL_OP_2(0x82, 0x4C) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x4D) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_STATUS BT_MESH_MODEL_OP_2(0x82, 0x4E) + +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_GET \ + BT_MESH_MODEL_OP_2(0x82, 0x4F) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_SET \ + BT_MESH_MODEL_OP_2(0x82, 0x50) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_SET_UNACK \ + BT_MESH_MODEL_OP_2(0x82, 0x51) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_STATUS \ BT_MESH_MODEL_OP_2(0x82, 0x52) -#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LAST_STATUS \ + +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LAST_GET \ + BT_MESH_MODEL_OP_2(0x82, 0x53) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LAST_STATUS \ BT_MESH_MODEL_OP_2(0x82, 0x54) -#define BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_STATUS \ + +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_GET \ + BT_MESH_MODEL_OP_2(0x82, 0x55) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_STATUS \ BT_MESH_MODEL_OP_2(0x82, 0x56) -#define BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_STATUS \ + +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_GET \ + BT_MESH_MODEL_OP_2(0x82, 0x57) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_STATUS \ BT_MESH_MODEL_OP_2(0x82, 0x58) -#define BT_MESH_MODEL_LIGHT_CTL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x60) -#define BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_STATUS \ +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_SET \ + BT_MESH_MODEL_OP_2(0x82, 0x59) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK \ + BT_MESH_MODEL_OP_2(0x82, 0x5A) + +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_SET \ + BT_MESH_MODEL_OP_2(0x82, 0x5B) +#define BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_SET_UNACK \ + BT_MESH_MODEL_OP_2(0x82, 0x5C) + +#define BT_MESH_MODEL_LIGHT_CTL_GET BT_MESH_MODEL_OP_2(0x82, 0x5D) +#define BT_MESH_MODEL_LIGHT_CTL_SET BT_MESH_MODEL_OP_2(0x82, 0x5E) +#define BT_MESH_MODEL_LIGHT_CTL_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x5F) +#define BT_MESH_MODEL_LIGHT_CTL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x60) + +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_GET BT_MESH_MODEL_OP_2(0x82, 0x61) + +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_GET \ + BT_MESH_MODEL_OP_2(0x82, 0x62) +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_STATUS \ BT_MESH_MODEL_OP_2(0x82, 0x63) -#define BT_MESH_MODEL_LIGHT_CTL_TEMP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x66) -#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_STATUS BT_MESH_MODEL_OP_2(0x82, 0x68) + +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_SET BT_MESH_MODEL_OP_2(0x82, 0x64) +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x65) +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x66) + +#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_GET BT_MESH_MODEL_OP_2(0x82, 0x67) +#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_STATUS BT_MESH_MODEL_OP_2(0x82, 0x68) + +#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_SET BT_MESH_MODEL_OP_2(0x82, 0x69) +#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_SET_UNACK \ + BT_MESH_MODEL_OP_2(0x82, 0x6A) + +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_SET \ + BT_MESH_MODEL_OP_2(0x82, 0x6B) +#define BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_SET_UNACK \ + BT_MESH_MODEL_OP_2(0x82, 0x6C) void bt_ready(void); diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.c index 0b60141a748a2..58569866cba84 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.c @@ -7,13 +7,12 @@ #include -#include "app_gpio.h" -#include "storage.h" - #include "ble_mesh.h" +#include "common.h" #include "device_composition.h" #include "state_binding.h" #include "transition.h" +#include "storage.h" static struct bt_mesh_cfg_srv cfg_srv = { .relay = BT_MESH_RELAY_ENABLED, @@ -70,8 +69,6 @@ BT_MESH_MODEL_PUB_DEFINE(gen_level_srv_pub_s0, NULL, 2 + 5); BT_MESH_MODEL_PUB_DEFINE(gen_level_cli_pub_s0, NULL, 2 + 7); /* Definitions of models publication context (End) */ -/* Definitions of models user data (Start) */ - struct lightness light; struct temperature temp; struct delta_uv duv; @@ -85,6 +82,8 @@ struct light_ctl_state state = { struct light_ctl_state *const ctl = &state; +/* Definitions of models user data (Start) */ + struct vendor_state vnd_user_data; /* Definitions of models user data (End) */ @@ -99,19 +98,18 @@ static void gen_onoff_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 3 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_GEN_ONOFF_STATUS); net_buf_simple_add_u8(msg, (u8_t) get_current(ONOFF)); - if (state->light->current == state->light->target) { + if (ctl->light->current == ctl->light->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_u8(msg, (u8_t) get_target(ONOFF)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -124,7 +122,6 @@ void gen_onoff_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -133,10 +130,10 @@ void gen_onoff_publish(struct bt_mesh_model *model) bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_GEN_ONOFF_STATUS); net_buf_simple_add_u8(msg, (u8_t) get_current(ONOFF)); - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_u8(msg, (u8_t) get_target(ONOFF)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -151,7 +148,6 @@ static void gen_onoff_set_unack(struct bt_mesh_model *model, { u8_t tid, onoff, tt, delay; s64_t now; - struct light_ctl_state *state = model->user_data; onoff = net_buf_simple_pull_u8(buf); tid = net_buf_simple_pull_u8(buf); @@ -161,16 +157,16 @@ static void gen_onoff_set_unack(struct bt_mesh_model *model, } now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -185,32 +181,32 @@ static void gen_onoff_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(ONOFF, &onoff); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(ONOFF); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_onoff_publish(model); - onoff_handler(state); + onoff_handler(); } static void gen_onoff_set(struct bt_mesh_model *model, @@ -219,7 +215,6 @@ static void gen_onoff_set(struct bt_mesh_model *model, { u8_t tid, onoff, tt, delay; s64_t now; - struct light_ctl_state *state = model->user_data; onoff = net_buf_simple_pull_u8(buf); tid = net_buf_simple_pull_u8(buf); @@ -229,17 +224,17 @@ static void gen_onoff_set(struct bt_mesh_model *model, } now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { gen_onoff_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -254,19 +249,19 @@ static void gen_onoff_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(ONOFF, &onoff); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(ONOFF); } else { gen_onoff_get(model, ctx, buf); @@ -274,14 +269,14 @@ static void gen_onoff_set(struct bt_mesh_model *model, } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_onoff_get(model, ctx, buf); gen_onoff_publish(model); - onoff_handler(state); + onoff_handler(); } /* Generic OnOff Client message handlers */ @@ -304,19 +299,18 @@ static void gen_level_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_GEN_LEVEL_STATUS); - net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL)); + net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL_LIGHT)); - if (state->light->current == state->light->target) { + if (ctl->light->current == ctl->light->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); - net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL)); - net_buf_simple_add_u8(msg, state->transition->rt); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); + net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL_LIGHT)); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -329,19 +323,18 @@ void gen_level_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; } bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_GEN_LEVEL_STATUS); - net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL)); + net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL_LIGHT)); - if (state->transition->counter) { - calculate_rt(state->transition); - net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL)); - net_buf_simple_add_u8(msg, state->transition->rt); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); + net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL_LIGHT)); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -357,22 +350,21 @@ static void gen_level_set_unack(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t level; s64_t now; - struct light_ctl_state *state = model->user_data; level = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -387,32 +379,32 @@ static void gen_level_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - set_target(LEVEL, &level); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; + set_target(LEVEL_LIGHT, &level); - if (state->light->target != state->light->current) { - set_transition_values(LEVEL); + if (ctl->light->target != ctl->light->current) { + set_transition_values(LEVEL_LIGHT); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_publish(model); - level_lightness_handler(state); + level_lightness_handler(); } static void gen_level_set(struct bt_mesh_model *model, @@ -422,23 +414,22 @@ static void gen_level_set(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t level; s64_t now; - struct light_ctl_state *state = model->user_data; level = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { gen_level_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -453,34 +444,34 @@ static void gen_level_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - set_target(LEVEL, &level); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; + set_target(LEVEL_LIGHT, &level); - if (state->light->target != state->light->current) { - set_transition_values(LEVEL); + if (ctl->light->target != ctl->light->current) { + set_transition_values(LEVEL_LIGHT); } else { gen_level_get(model, ctx, buf); return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_get(model, ctx, buf); gen_level_publish(model); - level_lightness_handler(state); + level_lightness_handler(); } static void gen_delta_set_unack(struct bt_mesh_model *model, @@ -491,30 +482,29 @@ static void gen_delta_set_unack(struct bt_mesh_model *model, static s16_t last_level; s32_t target, delta; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s32_t) net_buf_simple_pull_le32(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { - if (state->light->delta == delta) { + if (ctl->light->delta == delta) { return; } target = last_level + delta; } else { - last_level = (s16_t) get_current(LEVEL); + last_level = (s16_t) get_current(LEVEL_LIGHT); target = last_level + delta; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -529,16 +519,16 @@ static void gen_delta_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; if (target < INT16_MIN) { target = INT16_MIN; @@ -546,22 +536,22 @@ static void gen_delta_set_unack(struct bt_mesh_model *model, target = INT16_MAX; } - set_target(DELTA_LEVEL, &target); + set_target(DELTA_LEVEL_LIGHT, &target); - if (state->light->target != state->light->current) { - set_transition_values(LEVEL); + if (ctl->light->target != ctl->light->current) { + set_transition_values(LEVEL_LIGHT); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_publish(model); - level_lightness_handler(state); + level_lightness_handler(); } static void gen_delta_set(struct bt_mesh_model *model, @@ -572,31 +562,30 @@ static void gen_delta_set(struct bt_mesh_model *model, static s16_t last_level; s32_t target, delta; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s32_t) net_buf_simple_pull_le32(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { - if (state->light->delta == delta) { + if (ctl->light->delta == delta) { gen_level_get(model, ctx, buf); return; } target = last_level + delta; } else { - last_level = (s16_t) get_current(LEVEL); + last_level = (s16_t) get_current(LEVEL_LIGHT); target = last_level + delta; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -611,16 +600,16 @@ static void gen_delta_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; if (target < INT16_MIN) { target = INT16_MIN; @@ -628,24 +617,24 @@ static void gen_delta_set(struct bt_mesh_model *model, target = INT16_MAX; } - set_target(DELTA_LEVEL, &target); + set_target(DELTA_LEVEL_LIGHT, &target); - if (state->light->target != state->light->current) { - set_transition_values(LEVEL); + if (ctl->light->target != ctl->light->current) { + set_transition_values(LEVEL_LIGHT); } else { gen_level_get(model, ctx, buf); return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_get(model, ctx, buf); gen_level_publish(model); - level_lightness_handler(state); + level_lightness_handler(); } static void gen_move_set_unack(struct bt_mesh_model *model, @@ -653,24 +642,24 @@ static void gen_move_set_unack(struct bt_mesh_model *model, struct net_buf_simple *buf) { u8_t tid, tt, delay; - s16_t delta, target; + s16_t delta; + u16_t target; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -685,41 +674,40 @@ static void gen_move_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = MOVE; - state->light->delta = delta; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = MOVE; + ctl->light->delta = delta; if (delta < 0) { - target = INT16_MIN; - set_target(LEVEL, &target); + target = ctl->light->range_min; } else if (delta > 0) { - target = INT16_MAX; - set_target(LEVEL, &target); + target = ctl->light->range_max; } else if (delta == 0) { - state->light->target = state->light->current; + target = ctl->light->current; } + set_target(MOVE_LIGHT, &target); - if (state->light->target != state->light->current) { - set_transition_values(MOVE_LEVEL); + if (ctl->light->target != ctl->light->current) { + set_transition_values(MOVE_LIGHT); } else { return; } - if (state->transition->counter == 0U) { + if (ctl->transition->counter == 0U) { return; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_publish(model); - level_lightness_handler(state); + level_lightness_handler(); } static void gen_move_set(struct bt_mesh_model *model, @@ -727,25 +715,25 @@ static void gen_move_set(struct bt_mesh_model *model, struct net_buf_simple *buf) { u8_t tid, tt, delay; - s16_t delta, target; + s16_t delta; + u16_t target; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { gen_level_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -760,43 +748,42 @@ static void gen_move_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = MOVE; - state->light->delta = delta; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = MOVE; + ctl->light->delta = delta; if (delta < 0) { - target = INT16_MIN; - set_target(LEVEL, &target); + target = ctl->light->range_min; } else if (delta > 0) { - target = INT16_MAX; - set_target(LEVEL, &target); + target = ctl->light->range_max; } else if (delta == 0) { - state->light->target = state->light->current; + target = ctl->light->current; } + set_target(MOVE_LIGHT, &target); - if (state->light->target != state->light->current) { - set_transition_values(MOVE_LEVEL); + if (ctl->light->target != ctl->light->current) { + set_transition_values(MOVE_LIGHT); } else { gen_level_get(model, ctx, buf); return; } - if (state->transition->counter == 0U) { + if (ctl->transition->counter == 0U) { return; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_get(model, ctx, buf); gen_level_publish(model); - level_lightness_handler(state); + level_lightness_handler(); } /* Generic Level Client message handlers */ @@ -819,10 +806,9 @@ static void gen_def_trans_time_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 1 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_GEN_DEF_TRANS_TIME_STATUS); - net_buf_simple_add_u8(msg, state->tt); + net_buf_simple_add_u8(msg, ctl->tt); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send GEN_DEF_TT_SRV Status response\n"); @@ -833,14 +819,13 @@ static void gen_def_trans_time_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; } bt_mesh_model_msg_init(msg, BT_MESH_MODEL_GEN_DEF_TRANS_TIME_STATUS); - net_buf_simple_add_u8(msg, state->tt); + net_buf_simple_add_u8(msg, ctl->tt); err = bt_mesh_model_publish(model); if (err) { @@ -848,47 +833,48 @@ static void gen_def_trans_time_publish(struct bt_mesh_model *model) } } -static bool gen_def_trans_time_setunack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static void gen_def_trans_time_set_unack(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { u8_t tt; - struct light_ctl_state *state = model->user_data; + tt = net_buf_simple_pull_u8(buf); - /* Here, Model specification is silent about tid implementation */ if ((tt & 0x3F) == 0x3F) { - return false; + return; } - if (state->tt != tt) { - state->tt = tt; - default_tt = tt; + if (ctl->tt != tt) { + ctl->tt = tt; gen_def_trans_time_publish(model); save_on_flash(GEN_DEF_TRANS_TIME_STATE); } - - return true; -} - -static void gen_def_trans_time_set_unack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) -{ - gen_def_trans_time_setunack(model, ctx, buf); } static void gen_def_trans_time_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - if (gen_def_trans_time_setunack(model, ctx, buf) == true) { - gen_def_trans_time_get(model, ctx, buf); + u8_t tt; + + tt = net_buf_simple_pull_u8(buf); + + if ((tt & 0x3F) == 0x3F) { + return; } -} + if (ctl->tt != tt) { + ctl->tt = tt; + gen_def_trans_time_get(model, ctx, buf); + gen_def_trans_time_publish(model); + save_on_flash(GEN_DEF_TRANS_TIME_STATE); + } else { + gen_def_trans_time_get(model, ctx, buf); + } +} /* Generic Default Transition Time Client message handlers */ static void gen_def_trans_time_status(struct bt_mesh_model *model, @@ -905,10 +891,9 @@ static void gen_onpowerup_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 1 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_GEN_ONPOWERUP_STATUS); - net_buf_simple_add_u8(msg, state->onpowerup); + net_buf_simple_add_u8(msg, ctl->onpowerup); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send GEN_POWER_ONOFF_SRV Status response\n"); @@ -930,14 +915,13 @@ static void gen_onpowerup_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; } bt_mesh_model_msg_init(msg, BT_MESH_MODEL_GEN_ONPOWERUP_STATUS); - net_buf_simple_add_u8(msg, state->onpowerup); + net_buf_simple_add_u8(msg, ctl->onpowerup); err = bt_mesh_model_publish(model); if (err) { @@ -945,41 +929,45 @@ static void gen_onpowerup_publish(struct bt_mesh_model *model) } } -static bool gen_onpowerup_setunack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static void gen_onpowerup_set_unack(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { u8_t onpowerup; - struct light_ctl_state *state = model->user_data; + onpowerup = net_buf_simple_pull_u8(buf); - /* Here, Model specification is silent about tid implementation */ if (onpowerup > STATE_RESTORE) { - return false; + return; } - if (state->onpowerup != onpowerup) { - state->onpowerup = onpowerup; + if (ctl->onpowerup != onpowerup) { + ctl->onpowerup = onpowerup; gen_onpowerup_publish(model); save_on_flash(GEN_ONPOWERUP_STATE); } - - return true; -} - -static void gen_onpowerup_set_unack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) -{ - gen_onpowerup_setunack(model, ctx, buf); } static void gen_onpowerup_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - if (gen_onpowerup_setunack(model, ctx, buf) == true) { + u8_t onpowerup; + + onpowerup = net_buf_simple_pull_u8(buf); + + if (onpowerup > STATE_RESTORE) { + return; + } + + if (ctl->onpowerup != onpowerup) { + ctl->onpowerup = onpowerup; + + gen_onpowerup_get(model, ctx, buf); + gen_onpowerup_publish(model); + save_on_flash(GEN_ONPOWERUP_STATE); + } else { gen_onpowerup_get(model, ctx, buf); } } @@ -1032,13 +1020,7 @@ static void vnd_set_unack(struct bt_mesh_model *model, printk("Vendor model message = %04x\n", state->current); - if (state->current == STATE_ON) { - /* LED2 On */ - gpio_pin_write(led_device[1], DT_ALIAS_LED1_GPIOS_PIN, 0); - } else { - /* LED2 Off */ - gpio_pin_write(led_device[1], DT_ALIAS_LED1_GPIOS_PIN, 1); - } + update_vnd_led_gpio(); } static void vnd_set(struct bt_mesh_model *model, @@ -1064,19 +1046,18 @@ static void light_lightness_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_STATUS); net_buf_simple_add_le16(msg, (u16_t) get_current(ACTUAL)); - if (state->light->current == state->light->target) { + if (ctl->light->current == ctl->light->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(ACTUAL)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -1089,7 +1070,6 @@ void light_lightness_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -1098,10 +1078,10 @@ void light_lightness_publish(struct bt_mesh_model *model) bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_STATUS); net_buf_simple_add_le16(msg, (u16_t) get_current(ACTUAL)); - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(ACTUAL)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -1117,22 +1097,21 @@ static void light_lightness_set_unack(struct bt_mesh_model *model, u8_t tid, tt, delay; u16_t actual; s64_t now; - struct light_ctl_state *state = model->user_data; actual = net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -1147,39 +1126,32 @@ static void light_lightness_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); - - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - - if (actual > 0 && actual < state->light->range_min) { - actual = state->light->range_min; - } else if (actual > state->light->range_max) { - actual = state->light->range_max; - } + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(ACTUAL, &actual); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(ACTUAL); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_lightness_publish(model); - light_lightness_actual_handler(state); + light_lightness_actual_handler(); } static void light_lightness_set(struct bt_mesh_model *model, @@ -1189,23 +1161,22 @@ static void light_lightness_set(struct bt_mesh_model *model, u8_t tid, tt, delay; u16_t actual; s64_t now; - struct light_ctl_state *state = model->user_data; actual = net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { light_lightness_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -1220,26 +1191,19 @@ static void light_lightness_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); - - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - - if (actual > 0 && actual < state->light->range_min) { - actual = state->light->range_min; - } else if (actual > state->light->range_max) { - actual = state->light->range_max; - } + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(ACTUAL, &actual); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(ACTUAL); } else { light_lightness_get(model, ctx, buf); @@ -1247,14 +1211,14 @@ static void light_lightness_set(struct bt_mesh_model *model, } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_lightness_get(model, ctx, buf); light_lightness_publish(model); - light_lightness_actual_handler(state); + light_lightness_actual_handler(); } static void light_lightness_linear_get(struct bt_mesh_model *model, @@ -1262,20 +1226,19 @@ static void light_lightness_linear_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_STATUS); net_buf_simple_add_le16(msg, (u16_t) get_current(LINEAR)); - if (state->light->current == state->light->target) { + if (ctl->light->current == ctl->light->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(LINEAR)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -1288,7 +1251,6 @@ void light_lightness_linear_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -1298,10 +1260,10 @@ void light_lightness_linear_publish(struct bt_mesh_model *model) BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_STATUS); net_buf_simple_add_le16(msg, (u16_t) get_current(LINEAR)); - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(LINEAR)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -1317,22 +1279,21 @@ static void light_lightness_linear_set_unack(struct bt_mesh_model *model, u8_t tid, tt, delay; u16_t linear; s64_t now; - struct light_ctl_state *state = model->user_data; linear = net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -1347,32 +1308,32 @@ static void light_lightness_linear_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(LINEAR, &linear); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(LINEAR); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_lightness_linear_publish(model); - light_lightness_linear_handler(state); + light_lightness_linear_handler(); } static void light_lightness_linear_set(struct bt_mesh_model *model, @@ -1382,23 +1343,22 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, u8_t tid, tt, delay; u16_t linear; s64_t now; - struct light_ctl_state *state = model->user_data; linear = net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { light_lightness_linear_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -1413,19 +1373,19 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(LINEAR, &linear); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(LINEAR); } else { light_lightness_linear_get(model, ctx, buf); @@ -1433,14 +1393,14 @@ static void light_lightness_linear_set(struct bt_mesh_model *model, } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_lightness_linear_get(model, ctx, buf); light_lightness_linear_publish(model); - light_lightness_linear_handler(state); + light_lightness_linear_handler(); } static void light_lightness_last_get(struct bt_mesh_model *model, @@ -1448,10 +1408,9 @@ static void light_lightness_last_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 2 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_LAST_STATUS); - net_buf_simple_add_le16(msg, state->light->last); + net_buf_simple_add_le16(msg, ctl->light->last); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send LightLightnessLast Status response\n"); @@ -1463,11 +1422,10 @@ static void light_lightness_default_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 2 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_STATUS); - net_buf_simple_add_le16(msg, state->light->def); + net_buf_simple_add_le16(msg, ctl->light->def); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send LightLightnessDef Status response\n"); @@ -1479,14 +1437,13 @@ static void light_lightness_range_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); - struct light_ctl_state *state = model->user_data; - state->light->status_code = RANGE_SUCCESSFULLY_UPDATED; + ctl->light->status_code = RANGE_SUCCESSFULLY_UPDATED; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_STATUS); - net_buf_simple_add_u8(msg, state->light->status_code); - net_buf_simple_add_le16(msg, state->light->range_min); - net_buf_simple_add_le16(msg, state->light->range_max); + net_buf_simple_add_u8(msg, ctl->light->status_code); + net_buf_simple_add_le16(msg, ctl->light->range_min); + net_buf_simple_add_le16(msg, ctl->light->range_max); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send LightLightnessRange Status response\n"); @@ -1499,7 +1456,6 @@ static void light_lightness_default_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -1507,7 +1463,7 @@ static void light_lightness_default_publish(struct bt_mesh_model *model) bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_STATUS); - net_buf_simple_add_le16(msg, state->light->def); + net_buf_simple_add_le16(msg, ctl->light->def); err = bt_mesh_model_publish(model); if (err) { @@ -1520,17 +1476,15 @@ static void light_lightness_default_set_unack(struct bt_mesh_model *model, struct net_buf_simple *buf) { u16_t lightness; - struct light_ctl_state *state = model->user_data; lightness = net_buf_simple_pull_le16(buf); + lightness = constrain_lightness(lightness); - /* Here, Model specification is silent about tid implementation */ - - if (state->light->def != lightness) { - state->light->def = constrain_lightness(lightness); + if (ctl->light->def != lightness) { + ctl->light->def = lightness; light_lightness_default_publish(model); - save_on_flash(LIGHTNESS_TEMP_DEF_STATE); + save_on_flash(DEF_STATES); } } @@ -1538,24 +1492,35 @@ static void light_lightness_default_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - light_lightness_default_set_unack(model, ctx, buf); - light_lightness_default_get(model, ctx, buf); + u16_t lightness; + + lightness = net_buf_simple_pull_le16(buf); + lightness = constrain_lightness(lightness); + + if (ctl->light->def != lightness) { + ctl->light->def = lightness; + + light_lightness_default_get(model, ctx, buf); + light_lightness_default_publish(model); + save_on_flash(DEF_STATES); + } else { + light_lightness_default_get(model, ctx, buf); + } } static void light_lightness_range_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; } bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_STATUS); - net_buf_simple_add_u8(msg, state->light->status_code); - net_buf_simple_add_le16(msg, state->light->range_min); - net_buf_simple_add_le16(msg, state->light->range_max); + net_buf_simple_add_u8(msg, ctl->light->status_code); + net_buf_simple_add_le16(msg, ctl->light->range_min); + net_buf_simple_add_le16(msg, ctl->light->range_max); err = bt_mesh_model_publish(model); if (err) { @@ -1563,56 +1528,70 @@ static void light_lightness_range_publish(struct bt_mesh_model *model) } } -static bool light_lightness_range_setunack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static void light_lightness_range_set_unack(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { u16_t min, max; - struct light_ctl_state *state = model->user_data; min = net_buf_simple_pull_le16(buf); max = net_buf_simple_pull_le16(buf); - /* Here, Model specification is silent about tid implementation */ - if (min == 0U || max == 0U) { - return false; - } else { - if (min <= max) { - state->light->status_code = RANGE_SUCCESSFULLY_UPDATED; + return; + } - if (state->light->range_min != min || - state->light->range_max != max) { + if (min <= max) { + ctl->light->status_code = RANGE_SUCCESSFULLY_UPDATED; - state->light->range_min = min; - state->light->range_max = max; + if (ctl->light->range_min != min || + ctl->light->range_max != max) { - light_lightness_range_publish(model); - save_on_flash(LIGHTNESS_RANGE); - } - } else { - /* The provided value for Range Max cannot be set */ - state->light->status_code = CANNOT_SET_RANGE_MAX; - return false; + ctl->light->range_min = min; + ctl->light->range_max = max; + + light_lightness_range_publish(model); + save_on_flash(LIGHTNESS_RANGE); } + } else { + /* The provided value for Range Max cannot be set */ + ctl->light->status_code = CANNOT_SET_RANGE_MAX; + return; } - - return true; -} - -static void light_lightness_range_set_unack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) -{ - light_lightness_range_setunack(model, ctx, buf); } static void light_lightness_range_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - if (light_lightness_range_setunack(model, ctx, buf) == true) { - light_lightness_range_get(model, ctx, buf); + u16_t min, max; + + min = net_buf_simple_pull_le16(buf); + max = net_buf_simple_pull_le16(buf); + + if (min == 0U || max == 0U) { + return; + } + + if (min <= max) { + ctl->light->status_code = RANGE_SUCCESSFULLY_UPDATED; + + if (ctl->light->range_min != min || + ctl->light->range_max != max) { + + ctl->light->range_min = min; + ctl->light->range_max = max; + + light_lightness_range_get(model, ctx, buf); + light_lightness_range_publish(model); + save_on_flash(LIGHTNESS_RANGE); + } else { + light_lightness_range_get(model, ctx, buf); + } + } else { + /* The provided value for Range Max cannot be set */ + ctl->light->status_code = CANNOT_SET_RANGE_MAX; + return; } } @@ -1677,22 +1656,21 @@ static void light_ctl_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 9 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_CTL_STATUS); - net_buf_simple_add_le16(msg, (u16_t) get_current(CTL)); + net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_LIGHT)); net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_TEMP)); - if (state->light->current == state->light->target && - state->temp->current == state->temp->target) { + if (ctl->light->current == ctl->light->target && + ctl->temp->current == ctl->temp->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); - net_buf_simple_add_le16(msg, (u16_t) get_target(CTL)); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); + net_buf_simple_add_le16(msg, (u16_t) get_target(CTL_LIGHT)); net_buf_simple_add_le16(msg, (u16_t) get_target(CTL_TEMP)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -1705,7 +1683,6 @@ void light_ctl_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -1716,14 +1693,14 @@ void light_ctl_publish(struct bt_mesh_model *model) /* Here, as per Model specification, status should be * made up of lightness & temperature values only */ - net_buf_simple_add_le16(msg, (u16_t) get_current(CTL)); + net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_LIGHT)); net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_TEMP)); - if (state->transition->counter) { - calculate_rt(state->transition); - net_buf_simple_add_le16(msg, (u16_t) get_target(CTL)); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); + net_buf_simple_add_le16(msg, (u16_t) get_target(CTL_LIGHT)); net_buf_simple_add_le16(msg, (u16_t) get_target(CTL_TEMP)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -1740,7 +1717,6 @@ static void light_ctl_set_unack(struct bt_mesh_model *model, s16_t delta_uv; u16_t lightness, temp; s64_t now; - struct light_ctl_state *state = model->user_data; lightness = net_buf_simple_pull_le16(buf); temp = net_buf_simple_pull_le16(buf); @@ -1752,16 +1728,16 @@ static void light_ctl_set_unack(struct bt_mesh_model *model, } now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -1776,45 +1752,38 @@ static void light_ctl_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); - - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - set_target(CTL, &lightness); - - if (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; - } + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; + set_target(CTL_LIGHT, &lightness); set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); - if (state->light->target != state->light->current || - state->temp->target != state->temp->current || - state->duv->target != state->duv->current) { - set_transition_values(CTL); + if (ctl->light->target != ctl->light->current || + ctl->temp->target != ctl->temp->current || + ctl->duv->target != ctl->duv->current) { + set_transition_values(CTL_LIGHT); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; - state->temp->current = state->temp->target; - state->duv->current = state->duv->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; + ctl->temp->current = ctl->temp->target; + ctl->duv->current = ctl->duv->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_ctl_publish(model); - light_ctl_handler(state); + light_ctl_handler(); } static void light_ctl_set(struct bt_mesh_model *model, @@ -1825,7 +1794,6 @@ static void light_ctl_set(struct bt_mesh_model *model, s16_t delta_uv; u16_t lightness, temp; s64_t now; - struct light_ctl_state *state = model->user_data; lightness = net_buf_simple_pull_le16(buf); temp = net_buf_simple_pull_le16(buf); @@ -1837,17 +1805,17 @@ static void light_ctl_set(struct bt_mesh_model *model, } now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { light_ctl_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -1862,47 +1830,40 @@ static void light_ctl_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); - - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - set_target(CTL, &lightness); - - if (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; - } + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; + set_target(CTL_LIGHT, &lightness); set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); - if (state->light->target != state->light->current || - state->temp->target != state->temp->current || - state->duv->target != state->duv->current) { - set_transition_values(CTL); + if (ctl->light->target != ctl->light->current || + ctl->temp->target != ctl->temp->current || + ctl->duv->target != ctl->duv->current) { + set_transition_values(CTL_LIGHT); } else { light_ctl_get(model, ctx, buf); return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->light->current = state->light->target; - state->temp->current = state->temp->target; - state->duv->current = state->duv->target; + if (ctl->transition->counter == 0U) { + ctl->light->current = ctl->light->target; + ctl->temp->current = ctl->temp->target; + ctl->duv->current = ctl->duv->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_ctl_get(model, ctx, buf); light_ctl_publish(model); - light_ctl_handler(state); + light_ctl_handler(); } static void light_ctl_temp_range_get(struct bt_mesh_model *model, @@ -1910,14 +1871,13 @@ static void light_ctl_temp_range_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); - struct light_ctl_state *state = model->user_data; - state->temp->status_code = RANGE_SUCCESSFULLY_UPDATED; + ctl->temp->status_code = RANGE_SUCCESSFULLY_UPDATED; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_STATUS); - net_buf_simple_add_u8(msg, state->temp->status_code); - net_buf_simple_add_le16(msg, state->temp->range_min); - net_buf_simple_add_le16(msg, state->temp->range_max); + net_buf_simple_add_u8(msg, ctl->temp->status_code); + net_buf_simple_add_le16(msg, ctl->temp->range_min); + net_buf_simple_add_le16(msg, ctl->temp->range_max); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send LightCTL Temp Range Status response\n"); @@ -1929,12 +1889,11 @@ static void light_ctl_default_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 6 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_CTL_DEFAULT_STATUS); - net_buf_simple_add_le16(msg, state->light->def); - net_buf_simple_add_le16(msg, state->temp->def); - net_buf_simple_add_le16(msg, state->duv->def); + net_buf_simple_add_le16(msg, ctl->light->def); + net_buf_simple_add_le16(msg, ctl->temp->def); + net_buf_simple_add_le16(msg, ctl->duv->def); if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) { printk("Unable to send LightCTL Default Status response\n"); @@ -1947,16 +1906,15 @@ static void light_ctl_default_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; } bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_CTL_DEFAULT_STATUS); - net_buf_simple_add_le16(msg, state->light->def); - net_buf_simple_add_le16(msg, state->temp->def); - net_buf_simple_add_le16(msg, state->duv->def); + net_buf_simple_add_le16(msg, ctl->light->def); + net_buf_simple_add_le16(msg, ctl->temp->def); + net_buf_simple_add_le16(msg, ctl->duv->def); err = bt_mesh_model_publish(model); if (err) { @@ -1964,55 +1922,63 @@ static void light_ctl_default_publish(struct bt_mesh_model *model) } } -static bool light_ctl_default_setunack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static void light_ctl_default_set_unack(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { u16_t lightness, temp; s16_t delta_uv; - struct light_ctl_state *state = model->user_data; lightness = net_buf_simple_pull_le16(buf); temp = net_buf_simple_pull_le16(buf); delta_uv = (s16_t) net_buf_simple_pull_le16(buf); - /* Here, Model specification is silent about tid implementation */ - if (temp < TEMP_MIN || temp > TEMP_MAX) { - return false; + return; } - if (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; - } + lightness = constrain_lightness(lightness); + temp = constrain_temperature(temp); - if (state->light->def != lightness || state->temp->def != temp || - state->duv->def != delta_uv) { - state->light->def = constrain_lightness(lightness); - state->temp->def = temp; - state->duv->def = delta_uv; + if (ctl->light->def != lightness || ctl->temp->def != temp || + ctl->duv->def != delta_uv) { + ctl->light->def = lightness; + ctl->temp->def = temp; + ctl->duv->def = delta_uv; light_ctl_default_publish(model); - save_on_flash(LIGHTNESS_TEMP_DEF_STATE); + save_on_flash(DEF_STATES); } - - return true; -} - -static void light_ctl_default_set_unack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) -{ - light_ctl_default_setunack(model, ctx, buf); } static void light_ctl_default_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - if (light_ctl_default_setunack(model, ctx, buf) == true) { + u16_t lightness, temp; + s16_t delta_uv; + + lightness = net_buf_simple_pull_le16(buf); + temp = net_buf_simple_pull_le16(buf); + delta_uv = (s16_t) net_buf_simple_pull_le16(buf); + + if (temp < TEMP_MIN || temp > TEMP_MAX) { + return; + } + + lightness = constrain_lightness(lightness); + temp = constrain_temperature(temp); + + if (ctl->light->def != lightness || ctl->temp->def != temp || + ctl->duv->def != delta_uv) { + ctl->light->def = lightness; + ctl->temp->def = temp; + ctl->duv->def = delta_uv; + + light_ctl_default_get(model, ctx, buf); + light_ctl_default_publish(model); + save_on_flash(DEF_STATES); + } else { light_ctl_default_get(model, ctx, buf); } } @@ -2021,16 +1987,15 @@ static void light_ctl_temp_range_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; } bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_STATUS); - net_buf_simple_add_u8(msg, state->temp->status_code); - net_buf_simple_add_le16(msg, state->temp->range_min); - net_buf_simple_add_le16(msg, state->temp->range_max); + net_buf_simple_add_u8(msg, ctl->temp->status_code); + net_buf_simple_add_le16(msg, ctl->temp->range_min); + net_buf_simple_add_le16(msg, ctl->temp->range_max); err = bt_mesh_model_publish(model); if (err) { @@ -2038,58 +2003,74 @@ static void light_ctl_temp_range_publish(struct bt_mesh_model *model) } } -static bool light_ctl_temp_range_setunack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) +static void light_ctl_temp_range_set_unack(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) { u16_t min, max; - struct light_ctl_state *state = model->user_data; min = net_buf_simple_pull_le16(buf); max = net_buf_simple_pull_le16(buf); - /* Here, Model specification is silent about tid implementation */ - /* This is as per 6.1.3.1 in Mesh Model Specification */ if (min < TEMP_MIN || min > TEMP_MAX || max < TEMP_MIN || max > TEMP_MAX) { - return false; + return; } if (min <= max) { - state->temp->status_code = RANGE_SUCCESSFULLY_UPDATED; + ctl->temp->status_code = RANGE_SUCCESSFULLY_UPDATED; - if (state->temp->range_min != min || - state->temp->range_max != max) { + if (ctl->temp->range_min != min || + ctl->temp->range_max != max) { - state->temp->range_min = min; - state->temp->range_max = max; + ctl->temp->range_min = min; + ctl->temp->range_max = max; light_ctl_temp_range_publish(model); save_on_flash(TEMPERATURE_RANGE); } } else { /* The provided value for Range Max cannot be set */ - state->temp->status_code = CANNOT_SET_RANGE_MAX; - return false; + ctl->temp->status_code = CANNOT_SET_RANGE_MAX; + return; } - - return true; -} - -static void light_ctl_temp_range_set_unack(struct bt_mesh_model *model, - struct bt_mesh_msg_ctx *ctx, - struct net_buf_simple *buf) -{ - light_ctl_temp_range_setunack(model, ctx, buf); } static void light_ctl_temp_range_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - if (light_ctl_temp_range_setunack(model, ctx, buf) == true) { - light_ctl_temp_range_get(model, ctx, buf); + u16_t min, max; + + min = net_buf_simple_pull_le16(buf); + max = net_buf_simple_pull_le16(buf); + + /* This is as per 6.1.3.1 in Mesh Model Specification */ + if (min < TEMP_MIN || min > TEMP_MAX || + max < TEMP_MIN || max > TEMP_MAX) { + return; + } + + if (min <= max) { + ctl->temp->status_code = RANGE_SUCCESSFULLY_UPDATED; + + if (ctl->temp->range_min != min || + ctl->temp->range_max != max) { + + ctl->temp->range_min = min; + ctl->temp->range_max = max; + + light_ctl_temp_range_get(model, ctx, buf); + light_ctl_temp_range_publish(model); + save_on_flash(TEMPERATURE_RANGE); + } else { + light_ctl_temp_range_get(model, ctx, buf); + } + } else { + /* The provided value for Range Max cannot be set */ + ctl->temp->status_code = CANNOT_SET_RANGE_MAX; + return; } } @@ -2157,22 +2138,21 @@ static void light_ctl_temp_get(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 9 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_LIGHT_CTL_TEMP_STATUS); net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_TEMP)); net_buf_simple_add_le16(msg, (s16_t) get_current(CTL_DELTA_UV)); - if (state->temp->current == state->temp->target && - state->duv->current == state->duv->target) { + if (ctl->temp->current == ctl->temp->target && + ctl->duv->current == ctl->duv->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(CTL_TEMP)); net_buf_simple_add_le16(msg, (s16_t) get_target(CTL_DELTA_UV)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -2185,7 +2165,6 @@ void light_ctl_temp_publish(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -2195,11 +2174,11 @@ void light_ctl_temp_publish(struct bt_mesh_model *model) net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_TEMP)); net_buf_simple_add_le16(msg, (s16_t) get_current(CTL_DELTA_UV)); - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(CTL_TEMP)); net_buf_simple_add_le16(msg, (s16_t) get_target(CTL_DELTA_UV)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -2216,7 +2195,6 @@ static void light_ctl_temp_set_unack(struct bt_mesh_model *model, s16_t delta_uv; u16_t temp; s64_t now; - struct light_ctl_state *state = model->user_data; temp = net_buf_simple_pull_le16(buf); delta_uv = (s16_t) net_buf_simple_pull_le16(buf); @@ -2227,16 +2205,16 @@ static void light_ctl_temp_set_unack(struct bt_mesh_model *model, } now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2251,42 +2229,35 @@ static void light_ctl_temp_set_unack(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); - - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - - if (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; - } + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); - if (state->temp->target != state->temp->current || - state->duv->target != state->duv->current) { + if (ctl->temp->target != ctl->temp->current || + ctl->duv->target != ctl->duv->current) { set_transition_values(CTL_TEMP); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->temp->current = state->temp->target; - state->duv->current = state->duv->target; + if (ctl->transition->counter == 0U) { + ctl->temp->current = ctl->temp->target; + ctl->duv->current = ctl->duv->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_ctl_temp_publish(model); - light_ctl_temp_handler(state); + light_ctl_temp_handler(); } static void light_ctl_temp_set(struct bt_mesh_model *model, @@ -2297,7 +2268,6 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, s16_t delta_uv; u16_t temp; s64_t now; - struct light_ctl_state *state = model->user_data; temp = net_buf_simple_pull_le16(buf); delta_uv = (s16_t) net_buf_simple_pull_le16(buf); @@ -2308,17 +2278,17 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, } now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { light_ctl_temp_get(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2333,28 +2303,21 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); - - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; - - if (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; - } + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); - if (state->temp->target != state->temp->current || - state->duv->target != state->duv->current) { + if (ctl->temp->target != ctl->temp->current || + ctl->duv->target != ctl->duv->current) { set_transition_values(CTL_TEMP); } else { light_ctl_temp_get(model, ctx, buf); @@ -2362,15 +2325,15 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->temp->current = state->temp->target; - state->duv->current = state->duv->target; + if (ctl->transition->counter == 0U) { + ctl->temp->current = ctl->temp->target; + ctl->duv->current = ctl->duv->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; light_ctl_temp_get(model, ctx, buf); light_ctl_temp_publish(model); - light_ctl_temp_handler(state); + light_ctl_temp_handler(); } /* Generic Level (TEMPERARTURE) Server message handlers */ @@ -2379,19 +2342,18 @@ static void gen_level_get_temp(struct bt_mesh_model *model, struct net_buf_simple *buf) { struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); - struct light_ctl_state *state = model->user_data; bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_GEN_LEVEL_STATUS); net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL_TEMP)); - if (state->temp->current == state->temp->target) { + if (ctl->temp->current == ctl->temp->target) { goto send; } - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL_TEMP)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -2404,7 +2366,6 @@ void gen_level_publish_temp(struct bt_mesh_model *model) { int err; struct net_buf_simple *msg = model->pub->msg; - struct light_ctl_state *state = model->user_data; if (model->pub->addr == BT_MESH_ADDR_UNASSIGNED) { return; @@ -2413,10 +2374,10 @@ void gen_level_publish_temp(struct bt_mesh_model *model) bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_GEN_LEVEL_STATUS); net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL_TEMP)); - if (state->transition->counter) { - calculate_rt(state->transition); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL_TEMP)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -2432,22 +2393,21 @@ static void gen_level_set_unack_temp(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t level; s64_t now; - struct light_ctl_state *state = model->user_data; level = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2462,32 +2422,32 @@ static void gen_level_set_unack_temp(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(LEVEL_TEMP, &level); - if (state->temp->target != state->temp->current) { + if (ctl->temp->target != ctl->temp->current) { set_transition_values(LEVEL_TEMP); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->temp->current = state->temp->target; + if (ctl->transition->counter == 0U) { + ctl->temp->current = ctl->temp->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_publish_temp(model); - level_temp_handler(state); + level_temp_handler(); } static void gen_level_set_temp(struct bt_mesh_model *model, @@ -2497,23 +2457,22 @@ static void gen_level_set_temp(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t level; s64_t now; - struct light_ctl_state *state = model->user_data; level = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { gen_level_get_temp(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2528,19 +2487,19 @@ static void gen_level_set_temp(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; set_target(LEVEL_TEMP, &level); - if (state->temp->target != state->temp->current) { + if (ctl->temp->target != ctl->temp->current) { set_transition_values(LEVEL_TEMP); } else { gen_level_get_temp(model, ctx, buf); @@ -2548,14 +2507,14 @@ static void gen_level_set_temp(struct bt_mesh_model *model, } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->temp->current = state->temp->target; + if (ctl->transition->counter == 0U) { + ctl->temp->current = ctl->temp->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_get_temp(model, ctx, buf); gen_level_publish_temp(model); - level_temp_handler(state); + level_temp_handler(); } static void gen_delta_set_unack_temp(struct bt_mesh_model *model, @@ -2566,18 +2525,17 @@ static void gen_delta_set_unack_temp(struct bt_mesh_model *model, static s16_t last_level; s32_t target, delta; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s32_t) net_buf_simple_pull_le32(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { - if (state->temp->delta == delta) { + if (ctl->temp->delta == delta) { return; } target = last_level + delta; @@ -2589,7 +2547,7 @@ static void gen_delta_set_unack_temp(struct bt_mesh_model *model, switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2604,16 +2562,16 @@ static void gen_delta_set_unack_temp(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; if (target < INT16_MIN) { target = INT16_MIN; @@ -2623,20 +2581,20 @@ static void gen_delta_set_unack_temp(struct bt_mesh_model *model, set_target(LEVEL_TEMP, &target); - if (state->temp->target != state->temp->current) { + if (ctl->temp->target != ctl->temp->current) { set_transition_values(LEVEL_TEMP); } else { return; } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->temp->current = state->temp->target; + if (ctl->transition->counter == 0U) { + ctl->temp->current = ctl->temp->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_publish_temp(model); - level_temp_handler(state); + level_temp_handler(); } static void gen_delta_set_temp(struct bt_mesh_model *model, @@ -2647,31 +2605,30 @@ static void gen_delta_set_temp(struct bt_mesh_model *model, static s16_t last_level; s32_t target, delta; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s32_t) net_buf_simple_pull_le32(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { - if (state->temp->delta == delta) { + if (ctl->temp->delta == delta) { gen_level_get_temp(model, ctx, buf); return; } target = last_level + delta; } else { - last_level = (s16_t) get_current(LEVEL); + last_level = (s16_t) get_current(LEVEL_TEMP); target = last_level + delta; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2686,16 +2643,16 @@ static void gen_delta_set_temp(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = NON_MOVE; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = NON_MOVE; if (target < INT16_MIN) { target = INT16_MIN; @@ -2705,7 +2662,7 @@ static void gen_delta_set_temp(struct bt_mesh_model *model, set_target(LEVEL_TEMP, &target); - if (state->temp->target != state->temp->current) { + if (ctl->temp->target != ctl->temp->current) { set_transition_values(LEVEL_TEMP); } else { gen_level_get_temp(model, ctx, buf); @@ -2713,14 +2670,14 @@ static void gen_delta_set_temp(struct bt_mesh_model *model, } /* For Instantaneous Transition */ - if (state->transition->counter == 0U) { - state->temp->current = state->temp->target; + if (ctl->transition->counter == 0U) { + ctl->temp->current = ctl->temp->target; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_get_temp(model, ctx, buf); gen_level_publish_temp(model); - level_temp_handler(state); + level_temp_handler(); } static void gen_move_set_unack_temp(struct bt_mesh_model *model, @@ -2728,24 +2685,24 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model, struct net_buf_simple *buf) { u8_t tid, tt, delay; - s16_t delta, target; + s16_t delta; + u16_t target; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2760,41 +2717,40 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = MOVE; - state->temp->delta = delta; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = MOVE; + ctl->temp->delta = delta; if (delta < 0) { - target = INT16_MIN; - set_target(LEVEL_TEMP, &target); + target = ctl->temp->range_min; } else if (delta > 0) { - target = INT16_MAX; - set_target(LEVEL_TEMP, &target); + target = ctl->temp->range_max; } else if (delta == 0) { - state->temp->target = state->temp->current; + target = ctl->temp->current; } + set_target(MOVE_TEMP, &target); - if (state->temp->target != state->temp->current) { - set_transition_values(MOVE_LEVEL_TEMP); + if (ctl->temp->target != ctl->temp->current) { + set_transition_values(MOVE_TEMP); } else { return; } - if (state->transition->counter == 0U) { + if (ctl->transition->counter == 0U) { return; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_publish_temp(model); - level_temp_handler(state); + level_temp_handler(); } static void gen_move_set_temp(struct bt_mesh_model *model, @@ -2802,25 +2758,25 @@ static void gen_move_set_temp(struct bt_mesh_model *model, struct net_buf_simple *buf) { u8_t tid, tt, delay; - s16_t delta, target; + s16_t delta; + u16_t target; s64_t now; - struct light_ctl_state *state = model->user_data; delta = (s16_t) net_buf_simple_pull_le16(buf); tid = net_buf_simple_pull_u8(buf); now = k_uptime_get(); - if (state->last_tid == tid && - state->last_src_addr == ctx->addr && - state->last_dst_addr == ctx->recv_dst && - (now - state->last_msg_timestamp <= K_SECONDS(6))) { + if (ctl->last_tid == tid && + ctl->last_src_addr == ctx->addr && + ctl->last_dst_addr == ctx->recv_dst && + (now - ctl->last_msg_timestamp <= K_SECONDS(6))) { gen_level_get_temp(model, ctx, buf); return; } switch (buf->len) { case 0x00: /* No optional fields are available */ - tt = default_tt; + tt = ctl->tt; delay = 0U; break; case 0x02: /* Optional fields are available */ @@ -2835,43 +2791,42 @@ static void gen_move_set_temp(struct bt_mesh_model *model, return; } - state->transition->counter = 0U; - k_timer_stop(&state->transition->timer); + ctl->transition->counter = 0U; + k_timer_stop(&ctl->transition->timer); - state->last_tid = tid; - state->last_src_addr = ctx->addr; - state->last_dst_addr = ctx->recv_dst; - state->last_msg_timestamp = now; - state->transition->tt = tt; - state->transition->delay = delay; - state->transition->type = MOVE; - state->temp->delta = delta; + ctl->last_tid = tid; + ctl->last_src_addr = ctx->addr; + ctl->last_dst_addr = ctx->recv_dst; + ctl->last_msg_timestamp = now; + ctl->transition->tt = tt; + ctl->transition->delay = delay; + ctl->transition->type = MOVE; + ctl->temp->delta = delta; if (delta < 0) { - target = INT16_MIN; - set_target(LEVEL_TEMP, &target); + target = ctl->temp->range_min; } else if (delta > 0) { - target = INT16_MAX; - set_target(LEVEL_TEMP, &target); + target = ctl->temp->range_max; } else if (delta == 0) { - state->temp->target = state->temp->current; + target = ctl->temp->current; } + set_target(MOVE_TEMP, &target); - if (state->temp->target != state->temp->current) { - set_transition_values(MOVE_LEVEL_TEMP); + if (ctl->temp->target != ctl->temp->current) { + set_transition_values(MOVE_TEMP); } else { gen_level_get_temp(model, ctx, buf); return; } - if (state->transition->counter == 0U) { + if (ctl->transition->counter == 0U) { return; } - state->transition->just_started = true; + ctl->transition->just_started = true; gen_level_get_temp(model, ctx, buf); gen_level_publish_temp(model); - level_temp_handler(state); + level_temp_handler(); } /* Generic Level (TEMPERATURE) Client message handlers */ @@ -3060,14 +3015,14 @@ struct bt_mesh_model root_models[] = { BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_ONOFF_SRV, gen_onoff_srv_op, &gen_onoff_srv_pub_root, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_ONOFF_CLI, gen_onoff_cli_op, &gen_onoff_cli_pub_root, NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_LEVEL_SRV, gen_level_srv_op, &gen_level_srv_pub_root, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_LEVEL_CLI, gen_level_cli_op, &gen_level_cli_pub_root, NULL), @@ -3075,7 +3030,7 @@ struct bt_mesh_model root_models[] = { BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV, gen_def_trans_time_srv_op, &gen_def_trans_time_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, gen_def_trans_time_cli_op, &gen_def_trans_time_cli_pub, @@ -3083,32 +3038,32 @@ struct bt_mesh_model root_models[] = { BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV, gen_power_onoff_srv_op, &gen_power_onoff_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV, gen_power_onoff_setup_srv_op, &gen_power_onoff_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, gen_power_onoff_cli_op, &gen_power_onoff_cli_pub, NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV, light_lightness_srv_op, &light_lightness_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV, light_lightness_setup_srv_op, &light_lightness_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, light_lightness_cli_op, &light_lightness_cli_pub, NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_CTL_SRV, light_ctl_srv_op, &light_ctl_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV, light_ctl_setup_srv_op, &light_ctl_srv_pub, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_CTL_CLI, light_ctl_cli_op, &light_ctl_cli_pub, NULL), @@ -3122,14 +3077,14 @@ struct bt_mesh_model vnd_models[] = { struct bt_mesh_model s0_models[] = { BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_LEVEL_SRV, gen_level_srv_op_temp, &gen_level_srv_pub_s0, - ctl), + NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_GEN_LEVEL_CLI, gen_level_cli_op_temp, &gen_level_cli_pub_s0, NULL), BT_MESH_MODEL(BT_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV, light_ctl_temp_srv_op, &light_ctl_srv_pub, - ctl), + NULL), }; static struct bt_mesh_elem elements[] = { diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.h index 95ad31df1f025..f25974ba9bf2b 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.h @@ -56,7 +56,6 @@ struct temperature { u16_t target; u16_t def; - u16_t last; u8_t status_code; u16_t range_min; @@ -83,9 +82,6 @@ struct light_ctl_state { u8_t onpowerup, tt; - u32_t light_temp_def; - u32_t light_temp_last_tgt; - u8_t last_tid; u16_t last_src_addr; u16_t last_dst_addr; @@ -94,6 +90,7 @@ struct light_ctl_state { struct transition *transition; }; +extern struct vendor_state vnd_user_data; extern struct light_ctl_state *const ctl; extern struct bt_mesh_model root_models[]; diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.c index cf325b8e82c88..654e956b24c0f 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.c @@ -8,7 +8,6 @@ #include #include "app_gpio.h" - #include "ble_mesh.h" #include "device_composition.h" #include "publisher.h" @@ -16,13 +15,7 @@ #define ONOFF #define GENERIC_LEVEL -#if (defined(ONOFF) || defined(ONOFF_TT)) -static u8_t tid_onoff; -#elif defined(VND_MODEL_TEST) -static u8_t tid_vnd; -#endif - -static u8_t tid_level; +static u8_t tid; static u32_t button_read(struct device *port, u32_t pin) { @@ -36,18 +29,19 @@ void publish(struct k_work *work) { int err = 0; +#ifndef ONE_LED_ONE_BUTTON_BOARD if (button_read(button_device[0], DT_ALIAS_SW0_GPIOS_PIN) == 0U) { #if defined(ONOFF) bt_mesh_model_msg_init(root_models[3].pub->msg, BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK); net_buf_simple_add_u8(root_models[3].pub->msg, 0x01); - net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++); + net_buf_simple_add_u8(root_models[3].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[3]); #elif defined(ONOFF_TT) bt_mesh_model_msg_init(root_models[3].pub->msg, BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK); net_buf_simple_add_u8(root_models[3].pub->msg, 0x01); - net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++); + net_buf_simple_add_u8(root_models[3].pub->msg, tid++); net_buf_simple_add_u8(root_models[3].pub->msg, 0x45); net_buf_simple_add_u8(root_models[3].pub->msg, 0x28); err = bt_mesh_model_publish(&root_models[3]); @@ -55,7 +49,7 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(vnd_models[0].pub->msg, BT_MESH_MODEL_OP_3(0x03, CID_ZEPHYR)); net_buf_simple_add_le16(vnd_models[0].pub->msg, 0x0001); - net_buf_simple_add_u8(vnd_models[0].pub->msg, tid_vnd++); + net_buf_simple_add_u8(vnd_models[0].pub->msg, tid++); err = bt_mesh_model_publish(&vnd_models[0]); #endif } else if (button_read(button_device[1], DT_ALIAS_SW1_GPIOS_PIN) == @@ -64,13 +58,13 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(root_models[3].pub->msg, BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK); net_buf_simple_add_u8(root_models[3].pub->msg, 0x00); - net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++); + net_buf_simple_add_u8(root_models[3].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[3]); #elif defined(ONOFF_TT) bt_mesh_model_msg_init(root_models[3].pub->msg, BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK); net_buf_simple_add_u8(root_models[3].pub->msg, 0x00); - net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++); + net_buf_simple_add_u8(root_models[3].pub->msg, tid++); net_buf_simple_add_u8(root_models[3].pub->msg, 0x45); net_buf_simple_add_u8(root_models[3].pub->msg, 0x28); err = bt_mesh_model_publish(&root_models[3]); @@ -78,7 +72,7 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(vnd_models[0].pub->msg, BT_MESH_MODEL_OP_3(0x03, CID_ZEPHYR)); net_buf_simple_add_le16(vnd_models[0].pub->msg, 0x0000); - net_buf_simple_add_u8(vnd_models[0].pub->msg, tid_vnd++); + net_buf_simple_add_u8(vnd_models[0].pub->msg, tid++); err = bt_mesh_model_publish(&vnd_models[0]); #endif } else if (button_read(button_device[2], DT_ALIAS_SW2_GPIOS_PIN) == @@ -87,7 +81,7 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(root_models[5].pub->msg, BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK); net_buf_simple_add_le16(root_models[5].pub->msg, LEVEL_S25); - net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[5].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[5]); #elif defined(ONOFF_GET) bt_mesh_model_msg_init(root_models[3].pub->msg, @@ -97,27 +91,27 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(root_models[5].pub->msg, BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK); net_buf_simple_add_le32(root_models[5].pub->msg, 100); - net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[5].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[5]); #elif defined(GENERIC_MOVE_LEVEL_TT) bt_mesh_model_msg_init(root_models[5].pub->msg, BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK); net_buf_simple_add_le16(root_models[5].pub->msg, 655); - net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[5].pub->msg, tid++); net_buf_simple_add_u8(root_models[5].pub->msg, 0x41); net_buf_simple_add_u8(root_models[5].pub->msg, 0x00); err = bt_mesh_model_publish(&root_models[5]); #elif defined(LIGHT_LIGHTNESS_TT) bt_mesh_model_msg_init(root_models[13].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x4D)); + BT_MESH_MODEL_LIGHT_LIGHTNESS_SET_UNACK); net_buf_simple_add_le16(root_models[13].pub->msg, LEVEL_U25); - net_buf_simple_add_u8(root_models[13].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[13].pub->msg, tid++); net_buf_simple_add_u8(root_models[13].pub->msg, 0x45); net_buf_simple_add_u8(root_models[13].pub->msg, 0x28); err = bt_mesh_model_publish(&root_models[13]); #elif defined(LIGHT_CTL) bt_mesh_model_msg_init(root_models[16].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x5F)); + BT_MESH_MODEL_LIGHT_CTL_SET_UNACK); /* Lightness */ net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U25); /* Temperature (value should be from 0x0320 to 0x4E20 */ @@ -125,11 +119,11 @@ void publish(struct k_work *work) net_buf_simple_add_le16(root_models[16].pub->msg, 0x0320); /* Delta UV */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000); - net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[16].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[16]); #elif defined(LIGHT_CTL_TT) bt_mesh_model_msg_init(root_models[16].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x5F)); + BT_MESH_MODEL_LIGHT_CTL_SET_UNACK); /* Lightness */ net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U25); /* Temperature (value should be from 0x0320 to 0x4E20 */ @@ -137,19 +131,19 @@ void publish(struct k_work *work) net_buf_simple_add_le16(root_models[16].pub->msg, 0x0320); /* Delta UV */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000); - net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[16].pub->msg, tid++); net_buf_simple_add_u8(root_models[16].pub->msg, 0x45); net_buf_simple_add_u8(root_models[16].pub->msg, 0x00); err = bt_mesh_model_publish(&root_models[16]); #elif defined(LIGHT_CTL_TEMP) bt_mesh_model_msg_init(root_models[16].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x65)); + BT_MESH_MODEL_LIGHT_CTL_TEMP_SET_UNACK); /* Temperature (value should be from 0x0320 to 0x4E20 */ /* This is as per 6.1.3.1 in Mesh Model Specification */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0320); /* Delta UV */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000); - net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[16].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[16]); #endif } else if (button_read(button_device[3], DT_ALIAS_SW3_GPIOS_PIN) == @@ -158,33 +152,33 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(root_models[5].pub->msg, BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK); net_buf_simple_add_le16(root_models[5].pub->msg, LEVEL_S100); - net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[5].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[5]); #elif defined(GENERIC_DELTA_LEVEL) bt_mesh_model_msg_init(root_models[5].pub->msg, BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK); net_buf_simple_add_le32(root_models[5].pub->msg, -100); - net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[5].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[5]); #elif defined(GENERIC_MOVE_LEVEL_TT) bt_mesh_model_msg_init(root_models[5].pub->msg, BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK); net_buf_simple_add_le16(root_models[5].pub->msg, -655); - net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[5].pub->msg, tid++); net_buf_simple_add_u8(root_models[5].pub->msg, 0x41); net_buf_simple_add_u8(root_models[5].pub->msg, 0x00); err = bt_mesh_model_publish(&root_models[5]); #elif defined(LIGHT_LIGHTNESS_TT) bt_mesh_model_msg_init(root_models[13].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x4D)); + BT_MESH_MODEL_LIGHT_LIGHTNESS_SET_UNACK); net_buf_simple_add_le16(root_models[13].pub->msg, LEVEL_U100); - net_buf_simple_add_u8(root_models[13].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[13].pub->msg, tid++); net_buf_simple_add_u8(root_models[13].pub->msg, 0x45); net_buf_simple_add_u8(root_models[13].pub->msg, 0x28); err = bt_mesh_model_publish(&root_models[13]); #elif defined(LIGHT_CTL) bt_mesh_model_msg_init(root_models[16].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x5F)); + BT_MESH_MODEL_LIGHT_CTL_SET_UNACK); /* Lightness */ net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U100); /* Temperature (value should be from 0x0320 to 0x4E20 */ @@ -192,11 +186,11 @@ void publish(struct k_work *work) net_buf_simple_add_le16(root_models[16].pub->msg, 0x4E20); /* Delta UV */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000); - net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[16].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[16]); #elif defined(LIGHT_CTL_TT) bt_mesh_model_msg_init(root_models[16].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x5F)); + BT_MESH_MODEL_LIGHT_CTL_SET_UNACK); /* Lightness */ net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U100); /* Temperature (value should be from 0x0320 to 0x4E20 */ @@ -204,22 +198,36 @@ void publish(struct k_work *work) net_buf_simple_add_le16(root_models[16].pub->msg, 0x4E20); /* Delta UV */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000); - net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[16].pub->msg, tid++); net_buf_simple_add_u8(root_models[16].pub->msg, 0x45); net_buf_simple_add_u8(root_models[16].pub->msg, 0x00); err = bt_mesh_model_publish(&root_models[16]); #elif defined(LIGHT_CTL_TEMP) bt_mesh_model_msg_init(root_models[16].pub->msg, - BT_MESH_MODEL_OP_2(0x82, 0x65)); + BT_MESH_MODEL_LIGHT_CTL_TEMP_SET_UNACK); /* Temperature (value should be from 0x0320 to 0x4E20 */ /* This is as per 6.1.3.1 in Mesh Model Specification */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x4E20); /* Delta UV */ net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000); - net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++); + net_buf_simple_add_u8(root_models[16].pub->msg, tid++); err = bt_mesh_model_publish(&root_models[16]); #endif } +#else + if (button_read(button_device[0], DT_ALIAS_SW0_GPIOS_PIN) == 0U) { +#if defined(ONOFF) + static u8_t state = STATE_ON; + + bt_mesh_model_msg_init(root_models[3].pub->msg, + BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK); + net_buf_simple_add_u8(root_models[3].pub->msg, + state = state ^ 0x01); + net_buf_simple_add_u8(root_models[3].pub->msg, tid++); + err = bt_mesh_model_publish(&root_models[3]); +#endif + } +#endif if (err) { printk("bt_mesh_model_publish: err: %d\n", err); diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.h index 1cb2a0a32f900..78ce47405c286 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.h @@ -21,7 +21,6 @@ #define LEVEL_U75 49152 #define LEVEL_U100 65535 -void randomize_publishers_TID(void); void publish(struct k_work *work); #endif diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.c index d67604add7f8e..e0cfcd3284a58 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.c @@ -72,11 +72,6 @@ u16_t constrain_lightness(u16_t light) return light; } -static void constrain_target_lightness(void) -{ - ctl->light->target = constrain_lightness(ctl->light->target); -} - static void constrain_target_lightness2(void) { /* This is as per Mesh Model Specification 3.3.2.2.3 */ @@ -92,6 +87,17 @@ static void constrain_target_lightness2(void) } } +u16_t constrain_temperature(u16_t temp) +{ + if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } else if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; + } + + return temp; +} + static s16_t light_ctl_temp_to_level(u16_t temp) { float tmp; @@ -125,10 +131,6 @@ u16_t level_to_light_ctl_temp(s16_t level) void set_target(u8_t type, void *dptr) { - bool set_light_ctl_delta_uv_target_value; - - set_light_ctl_delta_uv_target_value = true; - switch (type) { case ONOFF: { u8_t onoff; @@ -145,32 +147,38 @@ void set_target(u8_t type, void *dptr) } } break; - case LEVEL: + case LEVEL_LIGHT: ctl->light->target = *((s16_t *) dptr) - INT16_MIN; - constrain_target_lightness(); + ctl->light->target = constrain_lightness(ctl->light->target); break; - case DELTA_LEVEL: + case DELTA_LEVEL_LIGHT: ctl->light->target = *((s16_t *) dptr) - INT16_MIN; constrain_target_lightness2(); break; + case MOVE_LIGHT: + ctl->light->target = *((u16_t *) dptr); + break; case ACTUAL: ctl->light->target = *((u16_t *) dptr); + ctl->light->target = constrain_lightness(ctl->light->target); break; case LINEAR: ctl->light->target = linear_to_actual(*((u16_t *) dptr)); - constrain_target_lightness(); + ctl->light->target = constrain_lightness(ctl->light->target); break; - case CTL: - set_light_ctl_delta_uv_target_value = false; + case CTL_LIGHT: ctl->light->target = *((u16_t *) dptr); - constrain_target_lightness(); + ctl->light->target = constrain_lightness(ctl->light->target); break; case LEVEL_TEMP: ctl->temp->target = level_to_light_ctl_temp(*((s16_t *) dptr)); break; + case MOVE_TEMP: + ctl->temp->target = *((u16_t *) dptr); + break; case CTL_TEMP: - set_light_ctl_delta_uv_target_value = false; ctl->temp->target = *((u16_t *) dptr); + ctl->temp->target = constrain_temperature(ctl->temp->target); break; case CTL_DELTA_UV: ctl->duv->target = *((s16_t *) dptr); @@ -179,12 +187,8 @@ void set_target(u8_t type, void *dptr) return; } - if (set_light_ctl_delta_uv_target_value) { - ctl->duv->target = ctl->duv->current; - } - if (ctl->onpowerup == STATE_RESTORE) { - save_on_flash(LIGHTNESS_TEMP_LAST_TARGET_STATE); + save_on_flash(LAST_TARGET_STATES); } } @@ -201,13 +205,13 @@ int get_current(u8_t type) return STATE_OFF; } } - case LEVEL: + case LEVEL_LIGHT: return (s16_t) (ctl->light->current + INT16_MIN); case ACTUAL: return ctl->light->current; case LINEAR: return actual_to_linear(ctl->light->current); - case CTL: + case CTL_LIGHT: return ctl->light->current; case LEVEL_TEMP: return light_ctl_temp_to_level(ctl->temp->current); @@ -229,13 +233,13 @@ int get_target(u8_t type) } else { return STATE_OFF; } - case LEVEL: + case LEVEL_LIGHT: return (s16_t) (ctl->light->target + INT16_MIN); case ACTUAL: return ctl->light->target; case LINEAR: return actual_to_linear(ctl->light->target); - case CTL: + case CTL_LIGHT: return ctl->light->target; case LEVEL_TEMP: return light_ctl_temp_to_level(ctl->temp->target); diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.h index 8aa8a441feace..552014c2080f8 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.h @@ -9,23 +9,23 @@ #define _STATE_BINDING_H enum state_binding { - ONPOWERUP = 0x01, ONOFF, - LEVEL, - DELTA_LEVEL, - MOVE_LEVEL, + LEVEL_LIGHT, + DELTA_LEVEL_LIGHT, + MOVE_LIGHT, ACTUAL, LINEAR, - CTL, + CTL_LIGHT, LEVEL_TEMP, - MOVE_LEVEL_TEMP, + MOVE_TEMP, CTL_TEMP, CTL_DELTA_UV }; u16_t constrain_lightness(u16_t light); +u16_t constrain_temperature(u16_t temp); u16_t level_to_light_ctl_temp(s16_t level); void set_target(u8_t type, void *dptr); diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.c index 189f86ff013c1..b72fb76ac51bf 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.c @@ -13,8 +13,6 @@ #include "state_binding.h" #include "transition.h" -u8_t default_tt; - struct transition transition; /* Function to calculate Remaining Time (Start) */ @@ -114,14 +112,14 @@ void set_transition_values(u8_t type) switch (type) { case ONOFF: - case LEVEL: + case LEVEL_LIGHT: case ACTUAL: case LINEAR: ctl->light->delta = ((float) (ctl->light->current - ctl->light->target) / ctl->transition->counter); break; - case CTL: + case CTL_LIGHT: ctl->light->delta = ((float) (ctl->light->current - ctl->light->target) / ctl->transition->counter); @@ -182,12 +180,33 @@ static void onoff_work_handler(struct k_work *work) static void level_move_lightness_work_handler(void) { - s16_t level; + int light; + + light = 0; + + if (ctl->light->current) { + light = ctl->light->current + ctl->light->delta; + } else { + if (ctl->light->delta < 0) { + light = UINT16_MAX + ctl->light->delta; + } else if (ctl->light->delta > 0) { + light = ctl->light->delta; + } + } - level = (s16_t) get_current(LEVEL) + ctl->light->delta; + if (light > ctl->light->range_max) { + light = ctl->light->range_max; + } else if (light < ctl->light->range_min) { + light = ctl->light->range_min; + } - ctl->light->current = constrain_lightness((u16_t) (level - INT16_MIN)); + ctl->light->current = light; update_light_state(); + + if (ctl->light->target == light) { + ctl->transition->counter = 0; + k_timer_stop(&ctl->transition->timer); + } } static void level_lightness_work_handler(struct k_work *work) @@ -219,17 +238,37 @@ static void level_lightness_work_handler(struct k_work *work) update_light_state(); k_timer_stop(&ctl->transition->timer); } - } static void level_move_temp_work_handler(void) { - s16_t level; + int temp; + + temp = 0; - level = (s16_t) get_current(LEVEL_TEMP) + ctl->temp->delta; + if (ctl->temp->current) { + temp = ctl->temp->current + ctl->temp->delta; + } else { + if (ctl->temp->delta < 0) { + temp = UINT16_MAX + ctl->temp->delta; + } else if (ctl->temp->delta > 0) { + temp = ctl->temp->delta; + } + } - ctl->temp->current = level_to_light_ctl_temp(level); + if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; + } else if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } + + ctl->temp->current = temp; update_light_state(); + + if (ctl->temp->target == temp) { + ctl->transition->counter = 0; + k_timer_stop(&ctl->transition->timer); + } } static void level_temp_work_handler(struct k_work *work) @@ -287,7 +326,6 @@ static void light_lightness_actual_work_handler(struct k_work *work) update_light_state(); k_timer_stop(&ctl->transition->timer); } - } static void light_lightness_linear_work_handler(struct k_work *work) @@ -427,108 +465,105 @@ static void light_ctl_temp_tt_handler(struct k_timer *dummy) K_TIMER_DEFINE(dummy_timer, NULL, NULL); /* Messages handlers (Start) */ -void onoff_handler(struct light_ctl_state *state) +void onoff_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, onoff_tt_handler, NULL); + k_timer_init(&ctl->transition->timer, onoff_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } -void level_lightness_handler(struct light_ctl_state *state) +void level_lightness_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, + k_timer_init(&ctl->transition->timer, level_lightness_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } -void level_temp_handler(struct light_ctl_state *state) +void level_temp_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, level_temp_tt_handler, NULL); + k_timer_init(&ctl->transition->timer, level_temp_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } -void light_lightness_actual_handler(struct light_ctl_state *state) +void light_lightness_actual_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, + k_timer_init(&ctl->transition->timer, light_lightness_actual_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } -void light_lightness_linear_handler(struct light_ctl_state *state) +void light_lightness_linear_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, + k_timer_init(&ctl->transition->timer, light_lightness_linear_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } -void light_ctl_handler(struct light_ctl_state *state) +void light_ctl_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, light_ctl_tt_handler, NULL); + k_timer_init(&ctl->transition->timer, light_ctl_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } -void light_ctl_temp_handler(struct light_ctl_state *state) +void light_ctl_temp_handler(void) { - if (state->transition->counter == 0U && state->transition->delay == 0) { + if (ctl->transition->counter == 0U && ctl->transition->delay == 0) { update_light_state(); return; } - k_timer_init(&state->transition->timer, + k_timer_init(&ctl->transition->timer, light_ctl_temp_tt_handler, NULL); - k_timer_start(&state->transition->timer, - K_MSEC(state->transition->delay * 5U), - K_MSEC(state->transition->quo_tt)); + k_timer_start(&ctl->transition->timer, + K_MSEC(ctl->transition->delay * 5U), + K_MSEC(ctl->transition->quo_tt)); } /* Messages handlers (End) */ - - - diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.h index 318a7104f2de9..a26ad7a28d1c5 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.h @@ -32,8 +32,6 @@ struct transition { struct k_timer timer; }; -extern u8_t default_tt; - extern struct transition transition; extern struct k_timer dummy_timer; @@ -41,12 +39,12 @@ extern struct k_timer dummy_timer; void calculate_rt(struct transition *transition); void set_transition_values(u8_t type); -void onoff_handler(struct light_ctl_state *state); -void level_lightness_handler(struct light_ctl_state *state); -void level_temp_handler(struct light_ctl_state *state); -void light_lightness_actual_handler(struct light_ctl_state *state); -void light_lightness_linear_handler(struct light_ctl_state *state); -void light_ctl_handler(struct light_ctl_state *state); -void light_ctl_temp_handler(struct light_ctl_state *state); +void onoff_handler(void); +void level_lightness_handler(void); +void level_temp_handler(void); +void light_lightness_actual_handler(void); +void light_lightness_linear_handler(void); +void light_ctl_handler(void); +void light_ctl_temp_handler(void); #endif diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.c index ed3301c2a7c00..7a56d5d318791 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.c +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.c @@ -27,17 +27,15 @@ static void save_gen_onpowerup_state(void) settings_save_one("ps/gpo", &ctl->onpowerup, sizeof(ctl->onpowerup)); if (ctl->onpowerup == 0x02) { - save_on_flash(LIGHTNESS_TEMP_LAST_TARGET_STATE); + save_on_flash(LAST_TARGET_STATES); } } -static void save_lightness_temp_def_state(void) +static void save_def_states(void) { - ctl->light_temp_def = (u32_t) ((ctl->light->def << 16) | - ctl->temp->def); - - settings_save_one("ps/ltd", &ctl->light_temp_def, - sizeof(ctl->light_temp_def)); + settings_save_one("ps/ld", &ctl->light->def, sizeof(ctl->light->def)); + settings_save_one("ps/td", &ctl->temp->def, sizeof(ctl->temp->def)); + settings_save_one("ps/dd", &ctl->duv->def, sizeof(ctl->duv->def)); } static void save_lightness_last_state(void) @@ -47,15 +45,16 @@ static void save_lightness_last_state(void) printk("Lightness Last values have been saved !!\n"); } -static void save_lightness_temp_last_target_state(void) +static void save_last_target_states(void) { - ctl->light_temp_last_tgt = - (u32_t) ((ctl->light->target << 16) | ctl->temp->target); + settings_save_one("ps/llt", &ctl->light->target, + sizeof(ctl->light->target)); - settings_save_one("ps/ltlt", &ctl->light_temp_last_tgt, - sizeof(ctl->light_temp_last_tgt)); + settings_save_one("ps/tlt", &ctl->temp->target, + sizeof(ctl->temp->target)); - printk("Lightness & Temp. Target values have been saved !!\n"); + settings_save_one("ps/dlt", &ctl->duv->target, + sizeof(ctl->duv->target)); } static void save_lightness_range(void) @@ -87,14 +86,14 @@ static void storage_work_handler(struct k_work *work) case GEN_ONPOWERUP_STATE: save_gen_onpowerup_state(); break; - case LIGHTNESS_TEMP_DEF_STATE: - save_lightness_temp_def_state(); + case DEF_STATES: + save_def_states(); break; case LIGHTNESS_LAST_STATE: save_lightness_last_state(); break; - case LIGHTNESS_TEMP_LAST_TARGET_STATE: - save_lightness_temp_last_target_state(); + case LAST_TARGET_STATES: + save_last_target_states(); break; case LIGHTNESS_RANGE: save_lightness_range(); @@ -137,9 +136,19 @@ static int ps_set(const char *key, size_t len_rd, sizeof(ctl->onpowerup)); } - if (!strncmp(key, "ltd", key_len)) { - len = read_cb(cb_arg, &ctl->light_temp_def, - sizeof(ctl->light_temp_def)); + if (!strncmp(key, "ld", key_len)) { + len = read_cb(cb_arg, &ctl->light->def, + sizeof(ctl->light->def)); + } + + if (!strncmp(key, "td", key_len)) { + len = read_cb(cb_arg, &ctl->temp->def, + sizeof(ctl->temp->def)); + } + + if (!strncmp(key, "dd", key_len)) { + len = read_cb(cb_arg, &ctl->duv->def, + sizeof(ctl->duv->def)); } if (!strncmp(key, "ll", key_len)) { @@ -147,9 +156,19 @@ static int ps_set(const char *key, size_t len_rd, sizeof(ctl->light->last)); } - if (!strncmp(key, "ltlt", key_len)) { - len = read_cb(cb_arg, &ctl->light_temp_last_tgt, - sizeof(ctl->light_temp_last_tgt)); + if (!strncmp(key, "llt", key_len)) { + len = read_cb(cb_arg, &ctl->light->target, + sizeof(ctl->light->target)); + } + + if (!strncmp(key, "tlt", key_len)) { + len = read_cb(cb_arg, &ctl->temp->target, + sizeof(ctl->temp->target)); + } + + if (!strncmp(key, "dlt", key_len)) { + len = read_cb(cb_arg, &ctl->duv->target, + sizeof(ctl->duv->target)); } if (!strncmp(key, "lr", key_len)) { diff --git a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.h b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.h index 84d0f5a37b82a..81c68c6085bee 100644 --- a/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.h +++ b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/storage.h @@ -12,9 +12,13 @@ enum ps_variables_id { RESET_COUNTER = 0x01, GEN_DEF_TRANS_TIME_STATE, GEN_ONPOWERUP_STATE, - LIGHTNESS_TEMP_DEF_STATE, + + DEF_STATES, + LIGHTNESS_LAST_STATE, - LIGHTNESS_TEMP_LAST_TARGET_STATE, + + LAST_TARGET_STATES, + LIGHTNESS_RANGE, TEMPERATURE_RANGE };