forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier_mocks.c
More file actions
102 lines (81 loc) · 2.35 KB
/
notifier_mocks.c
File metadata and controls
102 lines (81 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
//
// Author: Artur Kloniecki <arturx.kloniecki@linux.intel.com>
#include <stdint.h>
#include <stdlib.h>
#include <rtos/alloc.h>
#include <sof/lib/notifier.h>
#include <sof/list.h>
#include <sof/common.h>
struct callback_handle {
void *receiver;
const void *caller;
void (*cb)(void *arg, enum notify_id, void *data);
struct list_item list;
};
static struct notify *_notify;
struct notify **arch_notify_get(void)
{
int i;
if (!_notify) {
_notify = malloc(sizeof(*_notify));
for (i = 0; i < NOTIFIER_ID_COUNT; i++)
list_init(&_notify->list[i]);
}
return &_notify;
}
void notifier_event(const void *caller, enum notify_id type, uint32_t core_mask,
void *data, uint32_t data_size)
{
struct notify *notify = *arch_notify_get();
struct list_item *wlist;
struct list_item *tlist;
struct callback_handle *handle;
list_for_item_safe(wlist, tlist, ¬ify->list[type]) {
handle = container_of(wlist, struct callback_handle, list);
if (!caller || !handle->caller || handle->caller == caller)
handle->cb(handle->receiver, type, data);
}
}
int notifier_register(void *receiver, void *caller, enum notify_id type,
void (*cb)(void *arg, enum notify_id type, void *data), uint32_t flags)
{
struct notify *notify = *arch_notify_get();
struct callback_handle *handle;
if (type >= NOTIFIER_ID_COUNT)
return -EINVAL;
handle = rzalloc(0, 0, 0, sizeof(struct callback_handle));
if (!handle)
return -ENOMEM;
list_init(&handle->list);
handle->receiver = receiver;
handle->caller = caller;
handle->cb = cb;
list_item_append(&handle->list, ¬ify->list[type]);
return 0;
}
void notifier_unregister(void *receiver, void *caller, enum notify_id type)
{
struct notify *notify = *arch_notify_get();
struct list_item *wlist;
struct list_item *tlist;
struct callback_handle *handle;
if (type >= NOTIFIER_ID_COUNT)
return;
list_for_item_safe(wlist, tlist, ¬ify->list[type]) {
handle = container_of(wlist, struct callback_handle, list);
if ((!receiver || handle->receiver == receiver) &&
(!caller || handle->caller == caller)) {
list_item_del(&handle->list);
free(handle);
}
}
}
void notifier_unregister_all(void *receiver, void *caller)
{
int i;
for (i = 0; i < NOTIFIER_ID_COUNT; i++)
notifier_unregister(receiver, caller, i);
}