Branch data Line data Source code
1 : : /* 2 : : * Copyright (c) 2019 Peter Bigot Consulting, LLC 3 : : * Copyright (c) 2020 Nordic Semiconductor ASA 4 : : * 5 : : * SPDX-License-Identifier: Apache-2.0 6 : : */ 7 : : 8 : : #include <kernel.h> 9 : : #include <sys/notify.h> 10 : : 11 : 1 : int sys_notify_validate(struct sys_notify *notify) 12 : : { 13 : 1 : int rv = 0; 14 : : 15 [ - + ]: 1 : if (notify == NULL) { 16 : 0 : return -EINVAL; 17 : : } 18 : : 19 : : /* Validate configuration based on mode */ 20 [ + - - ]: 1 : switch (sys_notify_get_method(notify)) { 21 : 1 : case SYS_NOTIFY_METHOD_SPINWAIT: 22 : 1 : break; 23 : 0 : case SYS_NOTIFY_METHOD_CALLBACK: 24 [ # # ]: 0 : if (notify->method.callback == NULL) { 25 : 0 : rv = -EINVAL; 26 : : } 27 : 0 : break; 28 : : #ifdef CONFIG_POLL 29 : : case SYS_NOTIFY_METHOD_SIGNAL: 30 : : if (notify->method.signal == NULL) { 31 : : rv = -EINVAL; 32 : : } 33 : : break; 34 : : #endif /* CONFIG_POLL */ 35 : 0 : default: 36 : 0 : rv = -EINVAL; 37 : 0 : break; 38 : : } 39 : : 40 : : /* Clear the result here instead of in all callers. */ 41 [ + - ]: 1 : if (rv == 0) { 42 : 1 : notify->result = 0; 43 : : } 44 : : 45 : 1 : return rv; 46 : : } 47 : : 48 : 1 : sys_notify_generic_callback sys_notify_finalize(struct sys_notify *notify, 49 : : int res) 50 : : { 51 : 1 : struct k_poll_signal *sig = NULL; 52 : 1 : sys_notify_generic_callback rv = NULL; 53 : 1 : uint32_t method = sys_notify_get_method(notify); 54 : : 55 : : /* Store the result and capture secondary notification 56 : : * information. 57 : : */ 58 : 1 : notify->result = res; 59 [ + - - - ]: 1 : switch (method) { 60 : 1 : case SYS_NOTIFY_METHOD_SPINWAIT: 61 : 1 : break; 62 : 0 : case SYS_NOTIFY_METHOD_CALLBACK: 63 : 0 : rv = notify->method.callback; 64 : 0 : break; 65 : 0 : case SYS_NOTIFY_METHOD_SIGNAL: 66 : 0 : sig = notify->method.signal; 67 : 0 : break; 68 : 0 : default: 69 : 0 : __ASSERT_NO_MSG(false); 70 : : } 71 : : 72 : : /* Mark completion by clearing the flags field to the 73 : : * completed state, releasing any spin-waiters, then complete 74 : : * secondary notification. 75 : : */ 76 : 1 : compiler_barrier(); 77 : 1 : notify->flags = SYS_NOTIFY_METHOD_COMPLETED; 78 : : 79 : : if (IS_ENABLED(CONFIG_POLL) && (sig != NULL)) { 80 : : k_poll_signal_raise(sig, res); 81 : : } 82 : : 83 : 1 : return rv; 84 : : }