From d6240900d598e4294b5efc3770e5c4635f55be09 Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Tue, 26 Nov 2019 22:58:10 +0530 Subject: [PATCH 01/11] samples: mesh: removed redundant code, blank lines & comments Removed redundant code, unnecessary blank lines & comments. Signed-off-by: Vikrant More --- .../src/mesh/device_composition.c | 10 ---- .../src/mesh/device_composition.h | 1 - .../src/mesh/publisher.c | 49 ++++++++----------- .../src/mesh/publisher.h | 1 - .../src/mesh/state_binding.c | 12 +---- .../src/mesh/state_binding.h | 1 - .../src/mesh/transition.c | 2 - 7 files changed, 22 insertions(+), 54 deletions(-) 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..5f9ee00b98892 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 @@ -855,7 +855,6 @@ static bool gen_def_trans_time_setunack(struct bt_mesh_model *model, 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; @@ -952,7 +951,6 @@ static bool gen_onpowerup_setunack(struct bt_mesh_model *model, 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; @@ -1524,8 +1522,6 @@ static void light_lightness_default_set_unack(struct bt_mesh_model *model, lightness = net_buf_simple_pull_le16(buf); - /* Here, Model specification is silent about tid implementation */ - if (state->light->def != lightness) { state->light->def = constrain_lightness(lightness); @@ -1573,8 +1569,6 @@ static bool light_lightness_range_setunack(struct bt_mesh_model *model, 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 { @@ -1976,8 +1970,6 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, 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; } @@ -2048,8 +2040,6 @@ static bool light_ctl_temp_range_setunack(struct bt_mesh_model *model, 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) { 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..d9d613c8e10ae 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; 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..e68facda7519b 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) { @@ -41,13 +34,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, 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 +48,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 +57,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 +71,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 +80,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,13 +90,13 @@ 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]); @@ -111,7 +104,7 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(root_models[13].pub->msg, BT_MESH_MODEL_OP_2(0x82, 0x4D)); 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]); @@ -125,7 +118,7 @@ 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, @@ -137,7 +130,7 @@ 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]); @@ -149,7 +142,7 @@ 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]); #endif } else if (button_read(button_device[3], DT_ALIAS_SW3_GPIOS_PIN) == @@ -158,19 +151,19 @@ 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]); @@ -178,7 +171,7 @@ void publish(struct k_work *work) bt_mesh_model_msg_init(root_models[13].pub->msg, BT_MESH_MODEL_OP_2(0x82, 0x4D)); 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]); @@ -192,7 +185,7 @@ 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, @@ -204,7 +197,7 @@ 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]); @@ -216,7 +209,7 @@ 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]); #endif } 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..d8f7b6afa04df 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 @@ -125,10 +125,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; @@ -160,8 +156,7 @@ void set_target(u8_t type, void *dptr) ctl->light->target = linear_to_actual(*((u16_t *) dptr)); constrain_target_lightness(); break; - case CTL: - set_light_ctl_delta_uv_target_value = false; + case CTL_LIGHT: ctl->light->target = *((u16_t *) dptr); constrain_target_lightness(); break; @@ -169,7 +164,6 @@ void set_target(u8_t type, void *dptr) ctl->temp->target = level_to_light_ctl_temp(*((s16_t *) dptr)); break; case CTL_TEMP: - set_light_ctl_delta_uv_target_value = false; ctl->temp->target = *((u16_t *) dptr); break; case CTL_DELTA_UV: @@ -179,10 +173,6 @@ 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); } 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..bcbc54d997c63 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,7 +9,6 @@ #define _STATE_BINDING_H enum state_binding { - ONPOWERUP = 0x01, ONOFF, LEVEL, DELTA_LEVEL, 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..cc2e7d83f5557 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 @@ -219,7 +219,6 @@ 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) @@ -287,7 +286,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) From c79080eacdab8b191a53179a33d81e6dbad333db Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Tue, 26 Nov 2019 23:18:47 +0530 Subject: [PATCH 02/11] samples: mesh: nrf52: implementation based on single pointer Implement thing based on single struct light_ctl_state pointer in entire Application. Signed-off-by: Vikrant More --- .../src/mesh/device_composition.c | 1171 ++++++++--------- .../src/mesh/transition.c | 87 +- .../src/mesh/transition.h | 14 +- 3 files changed, 607 insertions(+), 665 deletions(-) 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 5f9ee00b98892..46ed04b515ae8 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 @@ -70,8 +70,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 +83,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 +99,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 +123,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 +131,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 +149,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,10 +158,10 @@ 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; } @@ -185,32 +182,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 +216,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,10 +225,10 @@ 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; } @@ -254,19 +250,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 +270,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 +300,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)); - 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, (s16_t) get_target(LEVEL)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } send: @@ -329,7 +324,6 @@ 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; @@ -338,10 +332,10 @@ void gen_level_publish(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)); - 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)); - net_buf_simple_add_u8(msg, state->transition->rt); + net_buf_simple_add_u8(msg, ctl->transition->rt); } err = bt_mesh_model_publish(model); @@ -357,16 +351,15 @@ 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; } @@ -387,32 +380,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; + 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, &level); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(LEVEL); } 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,16 +415,15 @@ 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; } @@ -453,19 +445,19 @@ 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; + 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, &level); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(LEVEL); } else { gen_level_get(model, ctx, buf); @@ -473,14 +465,14 @@ static void gen_level_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_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,18 +483,17 @@ 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; @@ -529,16 +520,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; @@ -548,20 +539,20 @@ static void gen_delta_set_unack(struct bt_mesh_model *model, set_target(DELTA_LEVEL, &target); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(LEVEL); } 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,18 +563,17 @@ 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; } @@ -611,16 +601,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; @@ -630,7 +620,7 @@ static void gen_delta_set(struct bt_mesh_model *model, set_target(DELTA_LEVEL, &target); - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(LEVEL); } else { gen_level_get(model, ctx, buf); @@ -638,14 +628,14 @@ static void gen_delta_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_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, @@ -655,16 +645,15 @@ static void gen_move_set_unack(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t delta, 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; } @@ -685,17 +674,17 @@ 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; @@ -704,22 +693,22 @@ static void gen_move_set_unack(struct bt_mesh_model *model, target = INT16_MAX; set_target(LEVEL, &target); } else if (delta == 0) { - state->light->target = state->light->current; + ctl->light->target = ctl->light->current; } - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(MOVE_LEVEL); } 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, @@ -729,16 +718,15 @@ static void gen_move_set(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t delta, 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; } @@ -760,17 +748,17 @@ 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; @@ -779,24 +767,24 @@ static void gen_move_set(struct bt_mesh_model *model, target = INT16_MAX; set_target(LEVEL, &target); } else if (delta == 0) { - state->light->target = state->light->current; + ctl->light->target = ctl->light->current; } - if (state->light->target != state->light->current) { + if (ctl->light->target != ctl->light->current) { set_transition_values(MOVE_LEVEL); } 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 +807,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 +820,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) { @@ -853,15 +839,14 @@ static bool gen_def_trans_time_setunack(struct bt_mesh_model *model, struct net_buf_simple *buf) { u8_t tt; - struct light_ctl_state *state = model->user_data; tt = net_buf_simple_pull_u8(buf); if ((tt & 0x3F) == 0x3F) { return false; } - if (state->tt != tt) { - state->tt = tt; + if (ctl->tt != tt) { + ctl->tt = tt; default_tt = tt; gen_def_trans_time_publish(model); @@ -904,10 +889,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"); @@ -929,14 +913,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) { @@ -949,15 +932,14 @@ static bool gen_onpowerup_setunack(struct bt_mesh_model *model, struct net_buf_simple *buf) { u8_t onpowerup; - struct light_ctl_state *state = model->user_data; onpowerup = net_buf_simple_pull_u8(buf); if (onpowerup > STATE_RESTORE) { return false; } - if (state->onpowerup != onpowerup) { - state->onpowerup = onpowerup; + if (ctl->onpowerup != onpowerup) { + ctl->onpowerup = onpowerup; gen_onpowerup_publish(model); save_on_flash(GEN_ONPOWERUP_STATE); @@ -1062,19 +1044,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: @@ -1087,7 +1068,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; @@ -1096,10 +1076,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); @@ -1115,16 +1095,15 @@ 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; } @@ -1145,39 +1124,39 @@ static void light_lightness_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 (actual > 0 && actual < state->light->range_min) { - actual = state->light->range_min; - } else if (actual > state->light->range_max) { - actual = state->light->range_max; + if (actual > 0 && actual < ctl->light->range_min) { + actual = ctl->light->range_min; + } else if (actual > ctl->light->range_max) { + actual = ctl->light->range_max; } 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, @@ -1187,16 +1166,15 @@ 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; } @@ -1218,26 +1196,26 @@ static void light_lightness_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 (actual > 0 && actual < state->light->range_min) { - actual = state->light->range_min; - } else if (actual > state->light->range_max) { - actual = state->light->range_max; + if (actual > 0 && actual < ctl->light->range_min) { + actual = ctl->light->range_min; + } else if (actual > ctl->light->range_max) { + actual = ctl->light->range_max; } 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); @@ -1245,14 +1223,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, @@ -1260,20 +1238,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: @@ -1286,7 +1263,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; @@ -1296,10 +1272,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); @@ -1315,16 +1291,15 @@ 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; } @@ -1345,32 +1320,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, @@ -1380,16 +1355,15 @@ 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; } @@ -1411,19 +1385,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); @@ -1431,14 +1405,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, @@ -1446,10 +1420,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"); @@ -1461,11 +1434,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"); @@ -1477,14 +1449,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"); @@ -1497,7 +1468,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; @@ -1505,7 +1475,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) { @@ -1518,12 +1488,11 @@ 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); - if (state->light->def != lightness) { - state->light->def = constrain_lightness(lightness); + if (ctl->light->def != lightness) { + ctl->light->def = constrain_lightness(lightness); light_lightness_default_publish(model); save_on_flash(LIGHTNESS_TEMP_DEF_STATE); @@ -1542,16 +1511,15 @@ 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) { @@ -1564,7 +1532,6 @@ static bool light_lightness_range_setunack(struct bt_mesh_model *model, 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); @@ -1573,20 +1540,20 @@ static bool light_lightness_range_setunack(struct bt_mesh_model *model, return false; } else { if (min <= max) { - state->light->status_code = RANGE_SUCCESSFULLY_UPDATED; + ctl->light->status_code = RANGE_SUCCESSFULLY_UPDATED; - if (state->light->range_min != min || - state->light->range_max != max) { + if (ctl->light->range_min != min || + ctl->light->range_max != max) { - state->light->range_min = min; - state->light->range_max = max; + 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; + ctl->light->status_code = CANNOT_SET_RANGE_MAX; return false; } } @@ -1671,22 +1638,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_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); + if (ctl->transition->counter) { + calculate_rt(ctl->transition); net_buf_simple_add_le16(msg, (u16_t) get_target(CTL)); 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: @@ -1699,7 +1665,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; @@ -1713,11 +1678,11 @@ void light_ctl_publish(struct bt_mesh_model *model) net_buf_simple_add_le16(msg, (u16_t) get_current(CTL)); net_buf_simple_add_le16(msg, (u16_t) get_current(CTL_TEMP)); - 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)); 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); @@ -1734,7 +1699,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); @@ -1746,10 +1710,10 @@ 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; } @@ -1770,45 +1734,45 @@ static void light_ctl_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(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; + if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } else if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; } 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) { + if (ctl->light->target != ctl->light->current || + ctl->temp->target != ctl->temp->current || + ctl->duv->target != ctl->duv->current) { set_transition_values(CTL); } 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, @@ -1819,7 +1783,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); @@ -1831,10 +1794,10 @@ 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; } @@ -1856,30 +1819,30 @@ static void light_ctl_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(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; + if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } else if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; } 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) { + if (ctl->light->target != ctl->light->current || + ctl->temp->target != ctl->temp->current || + ctl->duv->target != ctl->duv->current) { set_transition_values(CTL); } else { light_ctl_get(model, ctx, buf); @@ -1887,16 +1850,16 @@ static void light_ctl_set(struct bt_mesh_model *model, } /* 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, @@ -1904,14 +1867,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"); @@ -1923,12 +1885,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"); @@ -1941,16 +1902,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,7 +1924,6 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, { 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); @@ -1974,17 +1933,17 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, return false; } - if (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; + if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } else if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; } - 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 = constrain_lightness(lightness); + ctl->temp->def = temp; + ctl->duv->def = delta_uv; light_ctl_default_publish(model); save_on_flash(LIGHTNESS_TEMP_DEF_STATE); @@ -2013,16 +1972,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) { @@ -2035,7 +1993,6 @@ static bool light_ctl_temp_range_setunack(struct bt_mesh_model *model, 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); @@ -2047,20 +2004,20 @@ static bool light_ctl_temp_range_setunack(struct bt_mesh_model *model, } 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; + ctl->temp->status_code = CANNOT_SET_RANGE_MAX; return false; } @@ -2147,22 +2104,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: @@ -2175,7 +2131,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; @@ -2185,11 +2140,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); @@ -2206,7 +2161,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); @@ -2217,10 +2171,10 @@ 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; } @@ -2241,42 +2195,42 @@ static void light_ctl_temp_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 (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; + if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } else if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; } 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, @@ -2287,7 +2241,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); @@ -2298,10 +2251,10 @@ 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; } @@ -2323,28 +2276,28 @@ static void light_ctl_temp_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 (temp < state->temp->range_min) { - temp = state->temp->range_min; - } else if (temp > state->temp->range_max) { - temp = state->temp->range_max; + if (temp < ctl->temp->range_min) { + temp = ctl->temp->range_min; + } else if (temp > ctl->temp->range_max) { + temp = ctl->temp->range_max; } 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); @@ -2352,15 +2305,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 */ @@ -2369,19 +2322,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: @@ -2394,7 +2346,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; @@ -2403,10 +2354,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); @@ -2422,16 +2373,15 @@ 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; } @@ -2452,32 +2402,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, @@ -2487,16 +2437,15 @@ 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; } @@ -2518,19 +2467,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); @@ -2538,14 +2487,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, @@ -2556,18 +2505,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; @@ -2594,16 +2542,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; @@ -2613,20 +2561,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, @@ -2637,18 +2585,17 @@ 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; } @@ -2676,16 +2623,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; @@ -2695,7 +2642,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); @@ -2703,14 +2650,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, @@ -2720,16 +2667,15 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t delta, 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; } @@ -2750,17 +2696,17 @@ 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; @@ -2769,22 +2715,22 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model, target = INT16_MAX; set_target(LEVEL_TEMP, &target); } else if (delta == 0) { - state->temp->target = state->temp->current; + ctl->temp->target = ctl->temp->current; } - if (state->temp->target != state->temp->current) { + if (ctl->temp->target != ctl->temp->current) { set_transition_values(MOVE_LEVEL_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, @@ -2794,16 +2740,15 @@ static void gen_move_set_temp(struct bt_mesh_model *model, u8_t tid, tt, delay; s16_t delta, 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; } @@ -2825,17 +2770,17 @@ 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; @@ -2844,24 +2789,24 @@ static void gen_move_set_temp(struct bt_mesh_model *model, target = INT16_MAX; set_target(LEVEL_TEMP, &target); } else if (delta == 0) { - state->temp->target = state->temp->current; + ctl->temp->target = ctl->temp->current; } - if (state->temp->target != state->temp->current) { + if (ctl->temp->target != ctl->temp->current) { set_transition_values(MOVE_LEVEL_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 */ @@ -3050,14 +2995,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), @@ -3065,7 +3010,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, @@ -3073,32 +3018,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), @@ -3112,14 +3057,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/transition.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.c index cc2e7d83f5557..0e2577bcda5e8 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 @@ -425,108 +425,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..1eb719e903600 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 @@ -41,12 +41,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 From cfb36bdd93908a5f96de3eb93e0b9666d594270b Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Tue, 26 Nov 2019 23:26:59 +0530 Subject: [PATCH 03/11] samples: mesh: nrf52: rename enum attributes Rename attributes in enum state_binding. Signed-off-by: Vikrant More --- .../src/mesh/device_composition.c | 62 +++++++++---------- .../src/mesh/state_binding.c | 12 ++-- .../src/mesh/state_binding.h | 10 +-- .../src/mesh/transition.c | 6 +- 4 files changed, 45 insertions(+), 45 deletions(-) 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 46ed04b515ae8..fb5b460f1ba9e 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 @@ -302,7 +302,7 @@ static void gen_level_get(struct bt_mesh_model *model, struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 5 + 4); 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 (ctl->light->current == ctl->light->target) { goto send; @@ -310,7 +310,7 @@ static void gen_level_get(struct bt_mesh_model *model, if (ctl->transition->counter) { calculate_rt(ctl->transition); - net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL)); + net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL_LIGHT)); net_buf_simple_add_u8(msg, ctl->transition->rt); } @@ -330,11 +330,11 @@ void gen_level_publish(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)); + net_buf_simple_add_le16(msg, (s16_t) get_current(LEVEL_LIGHT)); if (ctl->transition->counter) { calculate_rt(ctl->transition); - net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL)); + net_buf_simple_add_le16(msg, (s16_t) get_target(LEVEL_LIGHT)); net_buf_simple_add_u8(msg, ctl->transition->rt); } @@ -390,10 +390,10 @@ static void gen_level_set_unack(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - set_target(LEVEL, &level); + set_target(LEVEL_LIGHT, &level); if (ctl->light->target != ctl->light->current) { - set_transition_values(LEVEL); + set_transition_values(LEVEL_LIGHT); } else { return; } @@ -455,10 +455,10 @@ static void gen_level_set(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - set_target(LEVEL, &level); + set_target(LEVEL_LIGHT, &level); if (ctl->light->target != ctl->light->current) { - set_transition_values(LEVEL); + set_transition_values(LEVEL_LIGHT); } else { gen_level_get(model, ctx, buf); return; @@ -499,7 +499,7 @@ static void gen_delta_set_unack(struct bt_mesh_model *model, target = last_level + delta; } else { - last_level = (s16_t) get_current(LEVEL); + last_level = (s16_t) get_current(LEVEL_LIGHT); target = last_level + delta; } @@ -537,10 +537,10 @@ 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 (ctl->light->target != ctl->light->current) { - set_transition_values(LEVEL); + set_transition_values(LEVEL_LIGHT); } else { return; } @@ -580,7 +580,7 @@ static void gen_delta_set(struct bt_mesh_model *model, target = last_level + delta; } else { - last_level = (s16_t) get_current(LEVEL); + last_level = (s16_t) get_current(LEVEL_LIGHT); target = last_level + delta; } @@ -618,10 +618,10 @@ 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 (ctl->light->target != ctl->light->current) { - set_transition_values(LEVEL); + set_transition_values(LEVEL_LIGHT); } else { gen_level_get(model, ctx, buf); return; @@ -688,16 +688,16 @@ static void gen_move_set_unack(struct bt_mesh_model *model, if (delta < 0) { target = INT16_MIN; - set_target(LEVEL, &target); + set_target(LEVEL_LIGHT, &target); } else if (delta > 0) { target = INT16_MAX; - set_target(LEVEL, &target); + set_target(LEVEL_LIGHT, &target); } else if (delta == 0) { ctl->light->target = ctl->light->current; } if (ctl->light->target != ctl->light->current) { - set_transition_values(MOVE_LEVEL); + set_transition_values(MOVE_LIGHT); } else { return; } @@ -762,16 +762,16 @@ static void gen_move_set(struct bt_mesh_model *model, if (delta < 0) { target = INT16_MIN; - set_target(LEVEL, &target); + set_target(LEVEL_LIGHT, &target); } else if (delta > 0) { target = INT16_MAX; - set_target(LEVEL, &target); + set_target(LEVEL_LIGHT, &target); } else if (delta == 0) { ctl->light->target = ctl->light->current; } if (ctl->light->target != ctl->light->current) { - set_transition_values(MOVE_LEVEL); + set_transition_values(MOVE_LIGHT); } else { gen_level_get(model, ctx, buf); return; @@ -1640,7 +1640,7 @@ static void light_ctl_get(struct bt_mesh_model *model, struct net_buf_simple *msg = NET_BUF_SIMPLE(2 + 9 + 4); 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 (ctl->light->current == ctl->light->target && @@ -1650,7 +1650,7 @@ static void light_ctl_get(struct bt_mesh_model *model, if (ctl->transition->counter) { calculate_rt(ctl->transition); - net_buf_simple_add_le16(msg, (u16_t) get_target(CTL)); + 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, ctl->transition->rt); } @@ -1675,12 +1675,12 @@ 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 (ctl->transition->counter) { calculate_rt(ctl->transition); - net_buf_simple_add_le16(msg, (u16_t) get_target(CTL)); + 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, ctl->transition->rt); } @@ -1744,7 +1744,7 @@ static void light_ctl_set_unack(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - set_target(CTL, &lightness); + set_target(CTL_LIGHT, &lightness); if (temp < ctl->temp->range_min) { temp = ctl->temp->range_min; @@ -1758,7 +1758,7 @@ static void light_ctl_set_unack(struct bt_mesh_model *model, if (ctl->light->target != ctl->light->current || ctl->temp->target != ctl->temp->current || ctl->duv->target != ctl->duv->current) { - set_transition_values(CTL); + set_transition_values(CTL_LIGHT); } else { return; } @@ -1829,7 +1829,7 @@ static void light_ctl_set(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - set_target(CTL, &lightness); + set_target(CTL_LIGHT, &lightness); if (temp < ctl->temp->range_min) { temp = ctl->temp->range_min; @@ -1843,7 +1843,7 @@ static void light_ctl_set(struct bt_mesh_model *model, if (ctl->light->target != ctl->light->current || ctl->temp->target != ctl->temp->current || ctl->duv->target != ctl->duv->current) { - set_transition_values(CTL); + set_transition_values(CTL_LIGHT); } else { light_ctl_get(model, ctx, buf); return; @@ -2602,7 +2602,7 @@ static void gen_delta_set_temp(struct bt_mesh_model *model, target = last_level + delta; } else { - last_level = (s16_t) get_current(LEVEL); + last_level = (s16_t) get_current(LEVEL_TEMP); target = last_level + delta; } @@ -2719,7 +2719,7 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model, } if (ctl->temp->target != ctl->temp->current) { - set_transition_values(MOVE_LEVEL_TEMP); + set_transition_values(MOVE_TEMP); } else { return; } @@ -2793,7 +2793,7 @@ static void gen_move_set_temp(struct bt_mesh_model *model, } if (ctl->temp->target != ctl->temp->current) { - set_transition_values(MOVE_LEVEL_TEMP); + set_transition_values(MOVE_TEMP); } else { gen_level_get_temp(model, ctx, buf); return; 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 d8f7b6afa04df..d945bc8cff954 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 @@ -141,11 +141,11 @@ 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(); break; - case DELTA_LEVEL: + case DELTA_LEVEL_LIGHT: ctl->light->target = *((s16_t *) dptr) - INT16_MIN; constrain_target_lightness2(); break; @@ -191,13 +191,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); @@ -219,13 +219,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 bcbc54d997c63..a792c97848bf0 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 @@ -10,15 +10,15 @@ enum state_binding { 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 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 0e2577bcda5e8..ad183e1c9bd4a 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 @@ -114,14 +114,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); @@ -184,7 +184,7 @@ static void level_move_lightness_work_handler(void) { s16_t level; - level = (s16_t) get_current(LEVEL) + ctl->light->delta; + level = (s16_t) get_current(LEVEL_LIGHT) + ctl->light->delta; ctl->light->current = constrain_lightness((u16_t) (level - INT16_MIN)); update_light_state(); From b4b4ed415614dcf0045715f58b0b07d01305201f Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Wed, 27 Nov 2019 00:38:59 +0530 Subject: [PATCH 04/11] samples: mesh: nrf52: separately save default & last target values Separately saved default & last target values of lightness, temperature & delta_uv on flash (using settings layer). Signed-off-by: Vikrant More --- .../onoff_level_lighting_vnd_app/src/main.c | 22 +++---- .../src/mesh/device_composition.c | 4 +- .../src/mesh/device_composition.h | 3 - .../src/mesh/state_binding.c | 2 +- .../src/storage.c | 65 ++++++++++++------- .../src/storage.h | 8 ++- 6 files changed, 60 insertions(+), 44 deletions(-) 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..b3f4d7ef5a1c4 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,9 @@ 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); - - /* Retrieve Default Temperature Value */ - ctl->temp->def = (u16_t) ctl->light_temp_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); ctl->temp->current = ctl->temp->def; ctl->duv->current = ctl->duv->def; @@ -82,8 +77,9 @@ 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; } 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 fb5b460f1ba9e..6fe7a8d7a0521 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 @@ -1495,7 +1495,7 @@ static void light_lightness_default_set_unack(struct bt_mesh_model *model, ctl->light->def = constrain_lightness(lightness); light_lightness_default_publish(model); - save_on_flash(LIGHTNESS_TEMP_DEF_STATE); + save_on_flash(DEF_STATES); } } @@ -1946,7 +1946,7 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, ctl->duv->def = delta_uv; light_ctl_default_publish(model); - save_on_flash(LIGHTNESS_TEMP_DEF_STATE); + save_on_flash(DEF_STATES); } return true; 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 d9d613c8e10ae..777b1ba64c837 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 @@ -82,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; 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 d945bc8cff954..9289d227d0634 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 @@ -174,7 +174,7 @@ void set_target(u8_t type, void *dptr) } if (ctl->onpowerup == STATE_RESTORE) { - save_on_flash(LIGHTNESS_TEMP_LAST_TARGET_STATE); + save_on_flash(LAST_TARGET_STATES); } } 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 }; From 16c789c7adf76f96ef642a549df2e06b63748d0f Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Wed, 27 Nov 2019 23:27:10 +0530 Subject: [PATCH 05/11] samples: mesh: nrf52: used constrain functions whereever possible Added support of constrain_temperature() function. Used constrain_lightness() & constrain_temperature() whereever possible. Signed-off-by: Vikrant More --- .../onoff_level_lighting_vnd_app/src/main.c | 3 ++ .../src/mesh/device_composition.c | 54 ++----------------- .../src/mesh/state_binding.c | 24 ++++++--- .../src/mesh/state_binding.h | 1 + 4 files changed, 25 insertions(+), 57 deletions(-) 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 b3f4d7ef5a1c4..332a8df8bdbf9 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 @@ -62,6 +62,9 @@ static void light_default_status_init(void) ctl->light->def = constrain_lightness(ctl->light->def); ctl->light->target = constrain_lightness(ctl->light->target); + 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; 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 6fe7a8d7a0521..0872dae773936 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 @@ -1134,13 +1134,6 @@ static void light_lightness_set_unack(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - - if (actual > 0 && actual < ctl->light->range_min) { - actual = ctl->light->range_min; - } else if (actual > ctl->light->range_max) { - actual = ctl->light->range_max; - } - set_target(ACTUAL, &actual); if (ctl->light->target != ctl->light->current) { @@ -1206,13 +1199,6 @@ static void light_lightness_set(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - - if (actual > 0 && actual < ctl->light->range_min) { - actual = ctl->light->range_min; - } else if (actual > ctl->light->range_max) { - actual = ctl->light->range_max; - } - set_target(ACTUAL, &actual); if (ctl->light->target != ctl->light->current) { @@ -1490,9 +1476,10 @@ static void light_lightness_default_set_unack(struct bt_mesh_model *model, u16_t lightness; lightness = net_buf_simple_pull_le16(buf); + lightness = constrain_lightness(lightness); if (ctl->light->def != lightness) { - ctl->light->def = constrain_lightness(lightness); + ctl->light->def = lightness; light_lightness_default_publish(model); save_on_flash(DEF_STATES); @@ -1745,13 +1732,6 @@ static void light_ctl_set_unack(struct bt_mesh_model *model, ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; set_target(CTL_LIGHT, &lightness); - - if (temp < ctl->temp->range_min) { - temp = ctl->temp->range_min; - } else if (temp > ctl->temp->range_max) { - temp = ctl->temp->range_max; - } - set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); @@ -1830,13 +1810,6 @@ static void light_ctl_set(struct bt_mesh_model *model, ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; set_target(CTL_LIGHT, &lightness); - - if (temp < ctl->temp->range_min) { - temp = ctl->temp->range_min; - } else if (temp > ctl->temp->range_max) { - temp = ctl->temp->range_max; - } - set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); @@ -1933,15 +1906,12 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, return false; } - if (temp < ctl->temp->range_min) { - temp = ctl->temp->range_min; - } else if (temp > ctl->temp->range_max) { - temp = ctl->temp->range_max; - } + 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 = constrain_lightness(lightness); + ctl->light->def = lightness; ctl->temp->def = temp; ctl->duv->def = delta_uv; @@ -2205,13 +2175,6 @@ static void light_ctl_temp_set_unack(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - - if (temp < ctl->temp->range_min) { - temp = ctl->temp->range_min; - } else if (temp > ctl->temp->range_max) { - temp = ctl->temp->range_max; - } - set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); @@ -2286,13 +2249,6 @@ static void light_ctl_temp_set(struct bt_mesh_model *model, ctl->transition->tt = tt; ctl->transition->delay = delay; ctl->transition->type = NON_MOVE; - - if (temp < ctl->temp->range_min) { - temp = ctl->temp->range_min; - } else if (temp > ctl->temp->range_max) { - temp = ctl->temp->range_max; - } - set_target(CTL_TEMP, &temp); set_target(CTL_DELTA_UV, &delta_uv); 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 9289d227d0634..861b347a6354c 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; @@ -143,7 +149,7 @@ void set_target(u8_t type, void *dptr) break; 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_LIGHT: ctl->light->target = *((s16_t *) dptr) - INT16_MIN; @@ -151,20 +157,22 @@ void set_target(u8_t type, void *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_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 CTL_TEMP: 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); 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 a792c97848bf0..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 @@ -25,6 +25,7 @@ enum state_binding { }; 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); From 8f0e1b60e1f3522116b0fafb1d4727c4a8e46dfd Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Thu, 28 Nov 2019 00:31:36 +0530 Subject: [PATCH 06/11] samples: mesh: nrf52: added separate update_vnd_led_gpio() Added sepate function update_vnd_led_gpio(). Signed-off-by: Vikrant More --- .../mesh/onoff_level_lighting_vnd_app/src/common.h | 1 + .../mesh/onoff_level_lighting_vnd_app/src/main.c | 11 +++++++++++ .../src/mesh/device_composition.c | 13 +++---------- .../src/mesh/device_composition.h | 1 + 4 files changed, 16 insertions(+), 10 deletions(-) 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 332a8df8bdbf9..8c03b636b72e5 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 @@ -93,6 +93,17 @@ static void light_default_status_init(void) ctl->duv->target = ctl->duv->current; } +void update_vnd_led_gpio(void) +{ + 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); + } +} + void update_led_gpio(void) { u8_t power, color; 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 0872dae773936..e97fe99f7bc16 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, @@ -1012,13 +1011,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, 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 777b1ba64c837..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 @@ -90,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[]; From 0f359d2f12ab684483db8be4dd4ed6418e0e0b2b Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Thu, 28 Nov 2019 03:14:21 +0530 Subject: [PATCH 07/11] samples: mesh: nrf52: corrected sequence of get & publish messages Corrected sequence of execution of get & publish messages. Signed-off-by: Vikrant More --- .../src/mesh/device_composition.c | 255 +++++++++++------- 1 file changed, 164 insertions(+), 91 deletions(-) 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 e97fe99f7bc16..d6eaafa101605 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 @@ -833,15 +833,16 @@ 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; + tt = net_buf_simple_pull_u8(buf); if ((tt & 0x3F) == 0x3F) { - return false; + return; } if (ctl->tt != tt) { @@ -851,27 +852,31 @@ static bool gen_def_trans_time_setunack(struct bt_mesh_model *model, 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; + default_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, @@ -926,15 +931,16 @@ 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; + onpowerup = net_buf_simple_pull_u8(buf); if (onpowerup > STATE_RESTORE) { - return false; + return; } if (ctl->onpowerup != onpowerup) { @@ -943,22 +949,27 @@ static bool gen_onpowerup_setunack(struct bt_mesh_model *model, 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); } } @@ -1483,8 +1494,20 @@ 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) @@ -1507,9 +1530,9 @@ 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; @@ -1517,43 +1540,60 @@ static bool light_lightness_range_setunack(struct bt_mesh_model *model, max = net_buf_simple_pull_le16(buf); if (min == 0U || max == 0U) { - return false; - } else { - if (min <= max) { - ctl->light->status_code = RANGE_SUCCESSFULLY_UPDATED; + return; + } - if (ctl->light->range_min != min || - ctl->light->range_max != max) { + if (min <= max) { + ctl->light->status_code = RANGE_SUCCESSFULLY_UPDATED; - ctl->light->range_min = min; - ctl->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 */ - ctl->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; } } @@ -1884,9 +1924,9 @@ 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; @@ -1896,7 +1936,7 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, delta_uv = (s16_t) net_buf_simple_pull_le16(buf); if (temp < TEMP_MIN || temp > TEMP_MAX) { - return false; + return; } lightness = constrain_lightness(lightness); @@ -1911,22 +1951,36 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model, light_ctl_default_publish(model); 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); } } @@ -1951,9 +2005,9 @@ 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; @@ -1963,7 +2017,7 @@ static bool light_ctl_temp_range_setunack(struct bt_mesh_model *model, /* 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) { @@ -1981,25 +2035,44 @@ static bool light_ctl_temp_range_setunack(struct bt_mesh_model *model, } else { /* The provided value for Range Max cannot be set */ ctl->temp->status_code = CANNOT_SET_RANGE_MAX; - return false; + 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; } } From 9456a6342e4d414203b55291d267506eaaf91376 Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Thu, 28 Nov 2019 03:28:17 +0530 Subject: [PATCH 08/11] samples: mesh: nrf52: removed redundant global variable Removed global variable 'default_tt' & code depend on it which is redundant as per latest implementation. Signed-off-by: Vikrant More --- .../onoff_level_lighting_vnd_app/src/main.c | 2 - .../src/mesh/device_composition.c | 46 +++++++++---------- .../src/mesh/transition.c | 2 - .../src/mesh/transition.h | 2 - 4 files changed, 22 insertions(+), 30 deletions(-) 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 8c03b636b72e5..9ffe8fcadb092 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 @@ -86,8 +86,6 @@ static void light_default_status_init(void) break; } - default_tt = ctl->tt; - ctl->light->target = ctl->light->current; ctl->temp->target = ctl->temp->current; ctl->duv->target = ctl->duv->current; 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 d6eaafa101605..66cc5e343551f 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 @@ -166,7 +166,7 @@ static void gen_onoff_set_unack(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 */ @@ -234,7 +234,7 @@ static void gen_onoff_set(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 */ @@ -364,7 +364,7 @@ static void gen_level_set_unack(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 */ @@ -429,7 +429,7 @@ static void gen_level_set(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 */ @@ -504,7 +504,7 @@ static void gen_delta_set_unack(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 */ @@ -585,7 +585,7 @@ static void gen_delta_set(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 */ @@ -658,7 +658,7 @@ static void gen_move_set_unack(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 */ @@ -732,7 +732,7 @@ static void gen_move_set(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 */ @@ -847,7 +847,6 @@ static void gen_def_trans_time_set_unack(struct bt_mesh_model *model, if (ctl->tt != tt) { ctl->tt = tt; - default_tt = tt; gen_def_trans_time_publish(model); save_on_flash(GEN_DEF_TRANS_TIME_STATE); @@ -868,7 +867,6 @@ static void gen_def_trans_time_set(struct bt_mesh_model *model, if (ctl->tt != tt) { ctl->tt = tt; - default_tt = tt; gen_def_trans_time_get(model, ctx, buf); gen_def_trans_time_publish(model); @@ -1113,7 +1111,7 @@ static void light_lightness_set_unack(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 */ @@ -1178,7 +1176,7 @@ static void light_lightness_set(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 */ @@ -1295,7 +1293,7 @@ static void light_lightness_linear_set_unack(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 */ @@ -1360,7 +1358,7 @@ static void light_lightness_linear_set(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 */ @@ -1739,7 +1737,7 @@ static void light_ctl_set_unack(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 */ @@ -1817,7 +1815,7 @@ static void light_ctl_set(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 */ @@ -2216,7 +2214,7 @@ static void light_ctl_temp_set_unack(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 */ @@ -2290,7 +2288,7 @@ static void light_ctl_temp_set(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 */ @@ -2409,7 +2407,7 @@ static void gen_level_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 */ @@ -2474,7 +2472,7 @@ static void gen_level_set_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 */ @@ -2549,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 */ @@ -2630,7 +2628,7 @@ static void gen_delta_set_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 */ @@ -2703,7 +2701,7 @@ static void gen_move_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 */ @@ -2777,7 +2775,7 @@ static void gen_move_set_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 */ 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 ad183e1c9bd4a..717f073b9633d 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) */ 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 1eb719e903600..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; From f8e8b87b9ac5786f86106f3a544f508c74d0c022 Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Fri, 29 Nov 2019 01:04:50 +0530 Subject: [PATCH 09/11] samples: mesh: nrf52: added some more macros support Added some more macros support. Signed-off-by: Vikrant More --- .../src/mesh/ble_mesh.h | 112 +++++++++++++----- .../src/mesh/publisher.c | 16 +-- 2 files changed, 93 insertions(+), 35 deletions(-) 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/publisher.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.c index e68facda7519b..2cc1c7a3fcd94 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 @@ -102,7 +102,7 @@ void publish(struct k_work *work) 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++); net_buf_simple_add_u8(root_models[13].pub->msg, 0x45); @@ -110,7 +110,7 @@ void publish(struct k_work *work) 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 */ @@ -122,7 +122,7 @@ void publish(struct k_work *work) 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 */ @@ -136,7 +136,7 @@ void publish(struct k_work *work) 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); @@ -169,7 +169,7 @@ void publish(struct k_work *work) 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++); net_buf_simple_add_u8(root_models[13].pub->msg, 0x45); @@ -177,7 +177,7 @@ void publish(struct k_work *work) 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 */ @@ -189,7 +189,7 @@ void publish(struct k_work *work) 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 */ @@ -203,7 +203,7 @@ void publish(struct k_work *work) 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); From 8453ab09c27451ddada918fa3fd32f95721eeba9 Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Sat, 30 Nov 2019 22:19:27 +0530 Subject: [PATCH 10/11] samples: mesh: nrf52: minor improvement in MOVE msg handlers Changes in MOVE message handler are as per Mesh Model Specification which says: "Upon receiving a Generic Move Set message, the Generic Level Server shall respond with a Generic Level Status message. The target Generic Level state is the upper limit of the Generic Level state when the transition speed is positive, or the lower limit of the Generic Level state when the transition speed is negative." Signed-off-by: Vikrant More --- .../src/mesh/device_composition.c | 48 ++++++++--------- .../src/mesh/state_binding.c | 6 +++ .../src/mesh/transition.c | 54 ++++++++++++++++--- 3 files changed, 78 insertions(+), 30 deletions(-) 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 66cc5e343551f..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 @@ -642,7 +642,8 @@ 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; delta = (s16_t) net_buf_simple_pull_le16(buf); @@ -686,14 +687,13 @@ static void gen_move_set_unack(struct bt_mesh_model *model, ctl->light->delta = delta; if (delta < 0) { - target = INT16_MIN; - set_target(LEVEL_LIGHT, &target); + target = ctl->light->range_min; } else if (delta > 0) { - target = INT16_MAX; - set_target(LEVEL_LIGHT, &target); + target = ctl->light->range_max; } else if (delta == 0) { - ctl->light->target = ctl->light->current; + target = ctl->light->current; } + set_target(MOVE_LIGHT, &target); if (ctl->light->target != ctl->light->current) { set_transition_values(MOVE_LIGHT); @@ -715,7 +715,8 @@ 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; delta = (s16_t) net_buf_simple_pull_le16(buf); @@ -760,14 +761,13 @@ static void gen_move_set(struct bt_mesh_model *model, ctl->light->delta = delta; if (delta < 0) { - target = INT16_MIN; - set_target(LEVEL_LIGHT, &target); + target = ctl->light->range_min; } else if (delta > 0) { - target = INT16_MAX; - set_target(LEVEL_LIGHT, &target); + target = ctl->light->range_max; } else if (delta == 0) { - ctl->light->target = ctl->light->current; + target = ctl->light->current; } + set_target(MOVE_LIGHT, &target); if (ctl->light->target != ctl->light->current) { set_transition_values(MOVE_LIGHT); @@ -2685,7 +2685,8 @@ 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; delta = (s16_t) net_buf_simple_pull_le16(buf); @@ -2729,14 +2730,13 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model, 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) { - ctl->temp->target = ctl->temp->current; + target = ctl->temp->current; } + set_target(MOVE_TEMP, &target); if (ctl->temp->target != ctl->temp->current) { set_transition_values(MOVE_TEMP); @@ -2758,7 +2758,8 @@ 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; delta = (s16_t) net_buf_simple_pull_le16(buf); @@ -2803,14 +2804,13 @@ static void gen_move_set_temp(struct bt_mesh_model *model, 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) { - ctl->temp->target = ctl->temp->current; + target = ctl->temp->current; } + set_target(MOVE_TEMP, &target); if (ctl->temp->target != ctl->temp->current) { set_transition_values(MOVE_TEMP); 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 861b347a6354c..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 @@ -155,6 +155,9 @@ void set_target(u8_t type, void *dptr) 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); @@ -170,6 +173,9 @@ void set_target(u8_t type, void *dptr) 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: ctl->temp->target = *((u16_t *) dptr); ctl->temp->target = constrain_temperature(ctl->temp->target); 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 717f073b9633d..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 @@ -180,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; - level = (s16_t) get_current(LEVEL_LIGHT) + ctl->light->delta; + light = 0; - ctl->light->current = constrain_lightness((u16_t) (level - INT16_MIN)); + 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; + } + } + + 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 = 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) @@ -221,12 +242,33 @@ static void level_lightness_work_handler(struct k_work *work) static void level_move_temp_work_handler(void) { - s16_t level; + int temp; - level = (s16_t) get_current(LEVEL_TEMP) + ctl->temp->delta; + temp = 0; - ctl->temp->current = level_to_light_ctl_temp(level); + 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; + } + } + + 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) From d89f0546e0e99abeec17d05c369e169d48a9b673 Mon Sep 17 00:00:00 2001 From: Vikrant More Date: Sun, 1 Dec 2019 00:18:50 +0530 Subject: [PATCH 11/11] samples: mesh: nrf52: added some preprocessor directive Added some preprocessor directive so that code get compile for some more nRF52 boards which has only one LED & one button. Signed-off-by: Vikrant More --- .../onoff_level_lighting_vnd_app/src/app_gpio.c | 6 +++++- .../mesh/onoff_level_lighting_vnd_app/src/main.c | 4 ++++ .../src/mesh/publisher.c | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) 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/main.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/main.c index 9ffe8fcadb092..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 @@ -93,6 +93,7 @@ static void light_default_status_init(void) 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); @@ -100,6 +101,7 @@ void update_vnd_led_gpio(void) /* LED2 Off */ gpio_pin_write(led_device[1], DT_ALIAS_LED1_GPIOS_PIN, 1); } +#endif } void update_led_gpio(void) @@ -120,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); @@ -135,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/publisher.c b/samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/publisher.c index 2cc1c7a3fcd94..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 @@ -29,6 +29,7 @@ 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, @@ -213,6 +214,20 @@ void publish(struct k_work *work) 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);