summaryrefslogtreecommitdiffstats
path: root/firmware/events.c
blob: 9b810c5d0d8aa07088b83b26ba8d3d16bc6a940d (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Miika Pekkarinen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include "events.h"
#include "panic.h"

#define MAX_SYS_EVENTS 28

struct sysevent {
    unsigned short id;
    bool oneshot;
    union {
        void (*cb)(unsigned short id, void *event_data);
        void (*cb_ex)(unsigned short id, void *event_data, void *user_data);
    } handler;
    void *user_data;
};

static struct sysevent events[MAX_SYS_EVENTS];
static int invalid_userdata;

static bool do_add_event(unsigned short id, bool oneshot,
                         void *handler, void *user_data)
{
    size_t free = MAX_SYS_EVENTS;
    struct sysevent *ev;
    /* Check if the event already exists. & lowest free slot available */
    for (size_t i = MAX_SYS_EVENTS - 1; i < MAX_SYS_EVENTS; i--)
    {
        ev = &events[i];
        if (ev->handler.cb == NULL)
            free = i;

        if (ev->id == id && ev->handler.cb == handler && user_data == ev->user_data)
        {
            return false;
        }
    }

    /* is there a free slot? */
    if (free < MAX_SYS_EVENTS)
    {
        ev = &events[free];
        ev->id = id;
        ev->handler.cb = handler;
        ev->user_data = user_data;
        ev->oneshot = oneshot;

        return true;
    }

    panicf("event line full");
    return false;
}

bool add_event(unsigned short id, void (*handler)(unsigned short id, void *data))
{
    return do_add_event(id, false, handler, &invalid_userdata);
}

bool add_event_ex(unsigned short id, bool oneshot,
                  void (*handler)(unsigned short id, void *event_data, void *user_data), void *user_data)
{
    return do_add_event(id, oneshot, handler, user_data);
}

static void do_remove_event(unsigned short id, void *handler, void *user_data)
{
    for (size_t i = 0; i < MAX_SYS_EVENTS; i++)
    {
        struct sysevent *ev = &events[i];
        if (ev->id == id && ev->handler.cb == handler && user_data == ev->user_data)
        {
            ev->handler.cb = NULL;
            return;
        }
    }
}

void remove_event(unsigned short id, void (*handler)(unsigned short id, void *data))
{
    do_remove_event(id, handler, &invalid_userdata);
}

void remove_event_ex(unsigned short id,
                     void (*handler)(unsigned short id, void *event_data, void *user_data),
                     void *user_data)
{
    do_remove_event(id, handler, user_data);
}

void send_event(unsigned short id, void *data)
{
    for (size_t i = 0; i < MAX_SYS_EVENTS; i++)
    {
        struct sysevent *ev = &events[i];
        if (ev->id == id && ev->handler.cb != NULL)
        {
            if (ev->user_data != &invalid_userdata)
            {
                ev->handler.cb_ex(id, data, ev->user_data);
                if (ev->oneshot) /* only _ex events have option of oneshot */
                    ev->handler.cb = NULL;
            }
            else
                ev->handler.cb(id, data);
        }
    }
}