static int gen_onoff_set()

in apps/blemesh_models_example_2/src/device_composition.c [270:339]


static int gen_onoff_set(struct bt_mesh_model *model,
			  struct bt_mesh_msg_ctx *ctx,
			  struct os_mbuf *buf)
{
	uint8_t tid, onoff, tt, delay;
	int64_t now;
	int rc;
	struct generic_onoff_state *state = model->user_data;

	onoff = net_buf_simple_pull_u8(buf);
	tid = net_buf_simple_pull_u8(buf);

	if (onoff > STATE_ON) {
		return 0;
	}

	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))) {
		rc = gen_onoff_get(model, ctx, buf);
		return rc;
	}

	switch (buf->om_len) {
	case 0x00:	/* No optional fields are available */
		tt = default_tt;
		delay = 0;
		break;
	case 0x02:	/* Optional fields are available */
		tt = net_buf_simple_pull_u8(buf);
		if ((tt & 0x3F) == 0x3F) {
			return 0;
		}

		delay = net_buf_simple_pull_u8(buf);
		break;
	default:
		return 0;
	}

	*ptr_counter = 0;
	os_callout_stop(ptr_timer);

	state->last_tid = tid;
	state->last_src_addr = ctx->addr;
	state->last_dst_addr = ctx->recv_dst;
	state->last_msg_timestamp = now;
	state->target_onoff = onoff;

	if (state->target_onoff != state->onoff) {
		onoff_tt_values(state, tt, delay);
	} else {
		gen_onoff_get(model, ctx, buf);
		rc = gen_onoff_publish(model);
		return rc;
	}

	/* For Instantaneous Transition */
	if (state->transition->counter == 0) {
		state->onoff = state->target_onoff;
	}

	state->transition->just_started = true;
	gen_onoff_get(model, ctx, buf);
	rc = gen_onoff_publish(model);
	onoff_handler(state);
    return rc;
}