summaryrefslogtreecommitdiffstats
path: root/K2LABI/ABI/ABIFilter.cpp
blob: 806e06e834383437ac6eb461540656387274ecc5 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "ABIFilter.h"

CABIFilter::CABIFilter()
{
    for(int index = 0; index < 256; index++)
    {
        m_filters[index] = NULL;
    }
}

//-----------------------------------------------------------------------------

CABIFilter::~CABIFilter()
{
    for(int index = 0; index < 256; index++)
    {
        if(m_filters[index] != NULL)
        {
            delete m_filters[index];
            m_filters[index] = NULL;
        }
    }
}

//-----------------------------------------------------------------------------

void CABIFilter::AddIntervalFilter(Bus bus, unsigned short handle, unsigned int min, unsigned int max)
{
    int index = (int) bus;
    if (m_filters[index] == NULL)
    {
        m_filters[index] = new CIdFilter();
    }
    m_filters[index]->AddIntervalFilter(handle, min, max);
}

//-----------------------------------------------------------------------------

void CABIFilter::AddMaskedFilter(Bus bus, unsigned short handle, unsigned int id, unsigned int mask)
{
    int index = (int)bus;
    if (m_filters[index] == NULL)
    {
        m_filters[index] = new CIdFilter();
    }
    m_filters[index]->AddMaskedFilter(handle, id, mask);
}

//-----------------------------------------------------------------------------

void CABIFilter::RemoveFilter(unsigned short handle)
{
    for (int index = 0; index < 256; index++)
    {
        if (m_filters[index] != NULL)
        {
            m_filters[index]->RemoveFilter(handle);
        }
    }
}

//-----------------------------------------------------------------------------

bool CABIFilter::Match(Bus bus, unsigned int id)
{
    int index = (int)bus;
    CIdFilter* filter = m_filters[index];
    if (filter == NULL)
    {
        //if no filters are added all messages will be delivered.
        return true;
    }
    return filter->Match(id);
}

//-----------------------------------------------------------------------------

IntervalIdFilterEntry::IntervalIdFilterEntry(unsigned short handle, unsigned int min, unsigned int max) :
m_handle(handle),
m_min(min),
m_max(max)
{
}

unsigned short IntervalIdFilterEntry::Handle()
{
    return m_handle;
}

//-----------------------------------------------------------------------------

bool IntervalIdFilterEntry::Match(unsigned int id)
{
    return ((m_min <= id) && (id <= m_max));
}

//-----------------------------------------------------------------------------

MaskedIdFilterEntry::MaskedIdFilterEntry(unsigned short handle, unsigned int id, unsigned int mask) :
    m_handle(handle),
    m_id(id),
    m_mask(mask)
{
}

//-----------------------------------------------------------------------------

unsigned short MaskedIdFilterEntry::Handle()
{
    return m_handle;
}

//-----------------------------------------------------------------------------

bool MaskedIdFilterEntry::Match(unsigned int id)
{
    return ((id & m_mask) == (m_id & m_mask));
}

//-----------------------------------------------------------------------------

CIdFilter::CIdFilter() :
m_filters()
{
    ::InitializeCriticalSection(&m_criticalSection);
}

//-----------------------------------------------------------------------------

CIdFilter::~CIdFilter()
{
    ::DeleteCriticalSection(&m_criticalSection);
}

//-----------------------------------------------------------------------------

void CIdFilter::AddIntervalFilter(unsigned short handle, unsigned int min, unsigned int max)
{
    IntervalIdFilterEntry* filter = new IntervalIdFilterEntry(handle, min, max);
    m_filters.push_back(filter);
}

//-----------------------------------------------------------------------------

void CIdFilter::AddMaskedFilter(unsigned short handle, unsigned int id, unsigned int mask)
{
    MaskedIdFilterEntry* filter = new MaskedIdFilterEntry(handle, id, mask);
    m_filters.push_back(filter);
}

//-----------------------------------------------------------------------------

void CIdFilter::RemoveFilter(unsigned short handle)
{
    ::EnterCriticalSection(&m_criticalSection);
    for (unsigned int index = 0; index < m_filters.size(); index++)
    {
        IFilterEntry* filter = m_filters[index];
        if (filter->Handle() == handle)
        {
            m_filters.erase (m_filters.begin() + index);
            delete filter;
            break;
        }
    }
    ::LeaveCriticalSection(&m_criticalSection);
}

//-----------------------------------------------------------------------------

bool CIdFilter::Match(unsigned int id)
{
    //TODO: PORT this unix ("for each" does not exist)
#if 0
    ::EnterCriticalSection(&m_criticalSection);
    bool ret = false;
    for each (IFilterEntry* filterEntry in m_filters)
    {
        if (filterEntry->Match(id))
        {
            ret = true;
            break;
        }
    }
    ::LeaveCriticalSection(&m_criticalSection);
    return ret;
#else
    return true;
#endif
}