summaryrefslogtreecommitdiff
path: root/Acceleration/library/icp_telephony/tdm_infrastructure_message_handler/IxPiuMhSolicitedCbMgr.c
blob: c8e8fb165eecc5eaec0deda3d43bb3159c149f8d (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
 * @file IxPiuMhSolicitedCbMgr.c
 *
 * @author Intel Corporation
 * @date 18 Jan 2002
 *
 * @description Contents are the implementation of the private API for
 * the Solicited Callback Manager module.
 *
 *
 * @par
 * This file is provided under a dual BSD/GPLv2 license.  When using or 
 *   redistributing this file, you may do so under either license.
 * 
 *   GPL LICENSE SUMMARY
 * 
 *   Copyright(c) 2007,2008,2009 Intel Corporation. All rights reserved.
 * 
 *   This program is free software; you can redistribute it and/or modify 
 *   it under the terms of version 2 of the GNU General Public License as
 *   published by the Free Software Foundation.
 * 
 *   This program is distributed in the hope that it will be useful, but 
 *   WITHOUT ANY WARRANTY; without even the implied warranty of 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 *   General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License 
 *   along with this program; if not, write to the Free Software 
 *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *   The full GNU General Public License is included in this distribution 
 *   in the file called LICENSE.GPL.
 * 
 *   Contact Information:
 *   Intel Corporation
 * 
 *   BSD LICENSE 
 * 
 *   Copyright(c) 2007,2008,2009 Intel Corporation. All rights reserved.
 *   All rights reserved.
 * 
 *   Redistribution and use in source and binary forms, with or without 
 *   modification, are permitted provided that the following conditions 
 *   are met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in 
 *       the documentation and/or other materials provided with the 
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its 
 *       contributors may be used to endorse or promote products derived 
 *       from this software without specific prior written permission.
 * 
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * 
 *
*/
#ifndef IXPIUMHCONFIG_P_H
#    define IXPIUMHSOLICITEDCBMGR_C
#else
#    error "Error, IxPiuMhConfig_p.h should not be included before this defn."
#endif

/*
 * Put the system defined include files required.
 */


/*
 * Put the user defined include files required.
 */

#include "IxOsal.h"

#include "IxPiuMhMacros_p.h"
#include "IxPiuMhSolicitedCbMgr_p.h"
#include "IxPiuMhConfig_p.h"
/*
 * #defines and macros used in this file.
 */

/*
 * Typedefs whose scope is limited to this file.
 */

/**
 * @struct IxPiuMhSolicitedCallbackListEntry
 *
 * @brief This structure is used to store the information associated with
 * an entry in the callback list.  This consists of the ID of the send
 * message (which indicates the ID of the corresponding response message)
 * and the callback function pointer itself.
 *
 */
typedef struct IxPiuMhSolicitedCallbackListEntry
{
    /** message ID */
    IxPiuMhMessageId messageId;

    /** callback function pointer */
    IxPiuMhCallback callback;

    /** pointer to next entry in the list */
    struct IxPiuMhSolicitedCallbackListEntry *next;
} IxPiuMhSolicitedCallbackListEntry;

/**
 * @struct IxPiuMhSolicitedCallbackList
 *
 * @brief This structure is used to maintain the list of response
 * callbacks.  The number of entries in this list will be variable, and
 * they will be stored in a linked list fashion for ease of addition and
 * removal.  The entries themselves are statically allocated, and are
 * organised into a "free" list and a "callback" list.  Adding an entry
 * means taking an entry from the "free" list and adding it to the
 * "callback" list.  Removing an entry means removing it from the
 * "callback" list and returning it to the "free" list.
 */

typedef struct
{
    /** pointer to the head of the free list */
    IxPiuMhSolicitedCallbackListEntry *freeHead;

    /** pointer to the head of the callback list */
    IxPiuMhSolicitedCallbackListEntry *callbackHead;

    /** pointer to the tail of the callback list */
    IxPiuMhSolicitedCallbackListEntry *callbackTail;

    /** array of entries - the first entry is used as a dummy entry to */
    /* avoid the scenario of having an empty list, hence '+ 1' */
    IxPiuMhSolicitedCallbackListEntry entries[IX_PIUMH_MAX_CALLBACKS + 1];
} IxPiuMhSolicitedCallbackList;

/**
 * @struct IxPiuMhSolicitedCbMgrStats
 *
 * @brief This structure is used to maintain statistics for the Solicited
 * Callback Manager module.
 */

typedef struct
{
    UINT32 saves;     /**< callback list saves */
    UINT32 retrieves; /**< callback list retrieves */
} IxPiuMhSolicitedCbMgrStats;

/*
 * Variable declarations global to this file only.  Externs are followed by
 * static variables.
 */

PRIVATE IxPiuMhSolicitedCallbackList
ixPiuMhSolicitedCbMgrCallbackLists[IX_PIUMH_NUM_PIUS];

PRIVATE IxPiuMhSolicitedCbMgrStats
ixPiuMhSolicitedCbMgrStats[IX_PIUMH_NUM_PIUS];

/*
 * Extern function prototypes.
 */

/*
 * Static function prototypes.
 */

/*
 * Function definition: ixPiuMhSolicitedCbMgrInitialize
 */

void ixPiuMhSolicitedCbMgrInitialize (void)
{
    IxPiuMhPiuId piuId;
    UINT32 localIndex;
    IxPiuMhSolicitedCallbackList *list = NULL;

    IX_PIUMH_TRACE0 (IX_PIUMH_FN_ENTRY_EXIT, "Entering "
                     "ixPiuMhSolicitedCbMgrInitialize\n");

    /* for each PIU ... */
    for (piuId = 0; piuId < IX_PIUMH_NUM_PIUS; piuId++)
    {
        /* initialise a pointer to the list for convenience */
        list = &ixPiuMhSolicitedCbMgrCallbackLists[piuId];

        /* for each entry in the list, after the dummy entry ... */
        for (localIndex = 1; localIndex <= IX_PIUMH_MAX_CALLBACKS; localIndex++)
        {
            /* initialise the entry */
            list->entries[localIndex].messageId = 0x00;
            list->entries[localIndex].callback = NULL;

            /* if this entry is before the last entry */
            if (localIndex < IX_PIUMH_MAX_CALLBACKS)
            {
                /* chain this entry to the following entry */
                list->entries[localIndex].next =
                    &(list->entries[localIndex + 1]);
            }
            else /* this entry is the last entry */
            {
                /* the last entry isn't chained to anything */
                list->entries[localIndex].next = NULL;
            }
        }

        /* set the free list pointer to point to the first real entry */
        /* (all real entries begin chained together on the free list) */
        list->freeHead = &(list->entries[1]);

        /* set the callback list pointers to point to the dummy entry */
        /* (the callback list is initially empty) */
        list->callbackHead = &(list->entries[0]);
        list->callbackTail = &(list->entries[0]);
    }

    IX_PIUMH_TRACE0 (IX_PIUMH_FN_ENTRY_EXIT, "Exiting "
                     "ixPiuMhSolicitedCbMgrInitialize\n");
}

/*
 * Function definition: ixPiuMhSolicitedCbMgrUninitialize
 */
void ixPiuMhSolicitedCbMgrUninitialize (void)
{
    IxPiuMhPiuId                 piuId;
    UINT32                       localIndex;
    IxPiuMhSolicitedCallbackList *list;

    IX_PIUMH_TRACE0 (IX_PIUMH_FN_ENTRY_EXIT, "Entering "
                     "ixPiuMhSolicitedCbMgrUninitialize\n");

    /* for each PIU ... */
    for (piuId = 0; piuId < IX_PIUMH_NUM_PIUS; piuId++)
    {
        /* initialise a pointer to the list for convenience */
        list = &ixPiuMhSolicitedCbMgrCallbackLists[piuId];

        /* for each entry in the list, after the dummy entry ... */
        for (localIndex = 1; localIndex <= IX_PIUMH_MAX_CALLBACKS; localIndex++)
        {
            /* initialise the entry */
            list->entries[localIndex].messageId = 0x00;
            list->entries[localIndex].callback = NULL;

            /* if this entry is before the last entry */
            if (IX_PIUMH_MAX_CALLBACKS > localIndex)
            {
                /* chain this entry to the following entry */
                list->entries[localIndex].next =
                    &(list->entries[localIndex + 1]);
            }
            else /* this entry is the last entry */
            {
                /* the last entry isn't chained to anything */
                list->entries[localIndex].next = NULL;
            }
        }
        /* set the free list pointer to point to the first real entry */
        /* (all real entries begin chained together on the free list) */
        list->freeHead = &(list->entries[1]);

        /* set the callback list pointers to point to the dummy entry */
        /* (the callback list is initially empty) */
        list->callbackHead = &(list->entries[0]);
        list->callbackTail = &(list->entries[0]);
    }

    IX_PIUMH_TRACE0 (IX_PIUMH_FN_ENTRY_EXIT, "Exiting "
                     "ixPiuMhSolicitedCbMgrUninitialize\n");

}

/*
 * Function definition: ixPiuMhSolicitedCbMgrCallbackSave
 */

IX_STATUS ixPiuMhSolicitedCbMgrCallbackSave (
    IxPiuMhPiuId piuId,
    IxPiuMhMessageId solicitedMessageId,
    IxPiuMhCallback solicitedCallback)
{
    IxPiuMhSolicitedCallbackList *list = NULL;
    IxPiuMhSolicitedCallbackListEntry *callbackEntry = NULL;

    IX_PIUMH_TRACE0 (IX_PIUMH_FN_ENTRY_EXIT, "Entering "
                     "ixPiuMhSolicitedCbMgrCallbackSave\n");

    /* initialise a pointer to the list for convenience */
    list = &ixPiuMhSolicitedCbMgrCallbackLists[piuId];

    /* check to see if there are any entries in the free list */
    if (list->freeHead == NULL)
    {
        IX_PIUMH_ERROR_REPORT ("Solicited callback list is full\n");
        return IX_FAIL;
    }

    /* there is an entry in the free list we can use */

    /* update statistical info */
    ixPiuMhSolicitedCbMgrStats[piuId].saves++;

    /* remove a callback entry from the start of the free list */
    callbackEntry = list->freeHead;
    list->freeHead = callbackEntry->next;

    /* fill in the callback entry with the new data */
    callbackEntry->messageId = solicitedMessageId;
    callbackEntry->callback = solicitedCallback;

    /* the new callback entry will be added to the tail of the callback */
    /* list, so it isn't chained to anything */
    callbackEntry->next = NULL;

    /* chain new callback entry to the last entry of the callback list */
    list->callbackTail->next = callbackEntry;
    list->callbackTail = callbackEntry;

    IX_PIUMH_TRACE0 (IX_PIUMH_FN_ENTRY_EXIT, "Exiting "
                     "ixPiuMhSolicitedCbMgrCallbackSave\n");

    return IX_SUCCESS;
}

/*
 * Function definition: ixPiuMhSolicitedCbMgrCallbackRetrieve
 */

void ixPiuMhSolicitedCbMgrCallbackRetrieve (
    IxPiuMhPiuId piuId,
    IxPiuMhMessageId solicitedMessageId,
    IxPiuMhCallback *solicitedCallback)
{
    IxPiuMhSolicitedCallbackList *list = NULL;
    IxPiuMhSolicitedCallbackListEntry *callbackEntry = NULL;
    IxPiuMhSolicitedCallbackListEntry *previousEntry = NULL;

    /* initialise a pointer to the list for convenience */
    list = &ixPiuMhSolicitedCbMgrCallbackLists[piuId];

    /* initialise the callback entry to the first entry of the callback */
    /* list - we must skip over the dummy entry, which is the previous */
    if (list != NULL)
    {
        callbackEntry = list->callbackHead->next;
        previousEntry = list->callbackHead;
    }

    /* traverse the callback list looking for an entry with a matching */
    /* message ID.  note we also save the previous entry's pointer to */
    /* allow us to unchain the matching entry from the callback list */
    while ((callbackEntry != NULL) &&
           (callbackEntry->messageId != solicitedMessageId))
    {
        previousEntry = callbackEntry;
        callbackEntry = callbackEntry->next;
    }

    /* if we didn't find a matching callback entry */
    if (callbackEntry == NULL)
    {
        /* return a NULL callback in the outgoing parameter */
        *solicitedCallback = NULL;
    }
    else /* we found a matching callback entry */
    {
        /* update statistical info */
        ixPiuMhSolicitedCbMgrStats[piuId].retrieves++;

        /* return the callback in the outgoing parameter */
        *solicitedCallback = callbackEntry->callback;

        /* unchain callback entry by chaining previous entry to next */
        previousEntry->next = callbackEntry->next;

        /* if the callback entry is at the tail of the list */
        if (list->callbackTail == callbackEntry)
        {
            /* update the tail of the callback list */
            list->callbackTail = previousEntry;
        }

        /* re-initialise the callback entry */
        callbackEntry->messageId = 0x00;
        callbackEntry->callback = NULL;

        /* add the callback entry to the start of the free list */
        callbackEntry->next = list->freeHead;
        list->freeHead = callbackEntry;
    }
}

/*
 * Function definition: ixPiuMhSolicitedCbMgrShow
 */

void ixPiuMhSolicitedCbMgrShow (
    IxPiuMhPiuId piuId)
{
    /* show the solicited callback list save counter */
    IX_PIUMH_SHOW ("Solicited callback list saves",
                   ixPiuMhSolicitedCbMgrStats[piuId].saves);

    /* show the solicited callback list retrieve counter */
    IX_PIUMH_SHOW ("Solicited callback list retrieves",
                   ixPiuMhSolicitedCbMgrStats[piuId].retrieves);
}

/*
 * Function definition: ixPiuMhSolicitedCbMgrShowReset
 */

void ixPiuMhSolicitedCbMgrShowReset (
    IxPiuMhPiuId piuId)
{
    /* reset the solicited callback list save counter */
    ixPiuMhSolicitedCbMgrStats[piuId].saves = 0;

    /* reset the solicited callback list retrieve counter */
    ixPiuMhSolicitedCbMgrStats[piuId].retrieves = 0;
}