1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8
9 #define LOG_LEVEL LOG_LEVEL_DBG
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(handler_demo);
12
13 #if !defined(CONFIG_MCUMGR_GRP_EXAMPLE_APP) && !defined(CONFIG_MCUMGR_GRP_EXAMPLE_MODULE)
14 #error Building this application with neither CONFIG_MCUMGR_GRP_EXAMPLE_APP or \
15 CONFIG_MCUMGR_GRP_EXAMPLE_MODULE enabled is not valid
16 #endif
17
18 #ifdef CONFIG_MCUMGR_GRP_EXAMPLE_OTHER_HOOK
19 #include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
20 #include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
21 #include <example_mgmt.h>
22 #include <example_mgmt_callbacks.h>
23
24 static struct mgmt_callback test_callback;
25 static bool last_run;
26
test_function(uint32_t event,enum mgmt_cb_return prev_status,int32_t * rc,uint16_t * group,bool * abort_more,void * data,size_t data_size)27 enum mgmt_cb_return test_function(uint32_t event, enum mgmt_cb_return prev_status, int32_t *rc,
28 uint16_t *group, bool *abort_more, void *data, size_t data_size)
29 {
30 if (event == MGMT_EVT_OP_EXAMPLE_OTHER) {
31 last_run = !last_run;
32
33 if (last_run) {
34 /* Return a dummy error for a demo */
35 *group = MGMT_GROUP_ID_EXAMPLE;
36 *rc = EXAMPLE_MGMT_ERR_REJECTED_BY_HOOK;
37
38 LOG_INF("Received hook, rejecting!");
39 return MGMT_CB_ERROR_ERR;
40 }
41
42 LOG_INF("Received hook, allowing");
43 } else {
44 LOG_ERR("Received unknown event: %d", event);
45 }
46
47 /* Return OK status code to continue with acceptance to underlying handler */
48 return MGMT_CB_OK;
49 }
50 #endif
51
main(void)52 int main(void)
53 {
54 #ifdef CONFIG_MCUMGR_GRP_EXAMPLE_OTHER_HOOK
55 /* Register for the example hook */
56 test_callback.callback = test_function;
57 test_callback.event_id = MGMT_EVT_OP_EXAMPLE_OTHER;
58 mgmt_callback_register(&test_callback);
59 #endif
60
61 return 0;
62 }
63