source: svn/newcon3bcm2_21bu/nexus/app/bsettop_smessage_nexus.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 12.1 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2008, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: $
11 * $brcm_Revision: $
12 * $brcm_Date: $
13 *
14 * Module Description: message filtering module
15 *
16 ***************************************************************************/
17
18
19#include "bapp_task.h"
20#include "bapp_util.h"
21#include "bsettop_smessage.h"
22#include "nexus_pid_channel.h"
23#include "nexus_parser_band.h"
24#include "nexus_message.h"
25#include "nexus_memory.h"
26
27BDBG_MODULE(smsg);
28
29#define MSG_VERBOSE 0
30#if MSG_VERBOSE & 1
31        #define BDBG_MSG_1(x) BDBG_MSG(x)
32#else
33        #define BDBG_MSG_1(x) BDBG_NOP()
34#endif
35
36#define MAX_PIDS 24
37#define MAX_FILTERS 24
38
39#define START_PID_CHANNEL 8
40#define B_INVALID_PID 0xFFFF
41#define B_INVALID_PID_CHANNEL SMESSAGE_INVALID_CHANNEL
42#define B_INVALID_BAND ((int)(-1))
43#define FILTER_SIZE 16
44#define TS_PACKET_SIZE 188
45#define SM_MAGIC 0xBADBABE0
46#define SM_BUFFER_SIZE (188 * 512)
47#define SM_WAIT (-1)
48#define SM_POLL_INTERVAL 50
49#define MAX_CAP 4
50#define FILTER_CAP 0
51
52enum sm_task_state_t
53{
54        STS_DONE,
55        STS_RUN
56};
57
58struct smessage_stream
59{
60        BLST_S_ENTRY(smessage_stream) next;
61        struct smessage_pid * sm_pid;
62        smessage_callback callback;
63        smessage_callback overflow;
64        void * context;
65        void *priv;
66        uint16_t pid;
67        NEXUS_PidChannelHandle pidChannel;
68        NEXUS_MessageHandle msg;
69    NEXUS_MessageStartSettings startSettings;
70    void * buffer;
71    size_t buffer_size;
72        smessage_format format;
73};
74
75BLST_S_HEAD(smessage_stream_list_t, smessage_stream);
76
77
78struct smessage_state
79{
80        uint32_t magic;
81        int band;
82
83        bapp_task_t task;
84        bapp_task_mutex_t lock;
85        enum sm_task_state_t task_state;
86
87        struct smessage_stream_list_t free_filter;
88        struct smessage_stream_list_t started_filter;
89        struct smessage_stream sm[MAX_FILTERS];
90};
91
92static struct smessage_state sm_st;
93
94#define SM_STACK_SIZE 0x400
95#define SM_PRIORITY 16
96static uint32_t sm_stack[SM_STACK_SIZE];
97static char * sm_task_name = "msg";
98
99static void bsettop_smessage_parser_task(void * param);
100
101
102/*
103  Summary:
104  Initialize the message system.
105 */
106bresult smessage_init(void *data)
107{
108        int i;
109        bapp_task_params t_param;
110        bapp_result_t bres = eBAPP_RESULT_OK;
111
112        BAPP_UNUSED(data);
113        BDBG_MSG_1(("%s", __PRETTY_FUNCTION__));
114
115        memset(&sm_st, 0, sizeof(struct smessage_stream));
116
117        BLST_S_INIT(&sm_st.free_filter);
118        BLST_S_INIT(&sm_st.started_filter);
119
120        for (i = 0; i < MAX_FILTERS; i ++)
121        {
122                BLST_S_INSERT_HEAD(&sm_st.free_filter, &sm_st.sm[i], next);
123        }
124
125        bres = bapp_task_create_mutex(&sm_st.lock);
126        if (eBAPP_RESULT_OK != bres)
127        {
128                goto ExitFunc;
129        }
130        sm_st.task_state = STS_RUN;
131        t_param.priority = SM_PRIORITY;
132        t_param.stack_size = SM_STACK_SIZE;
133        t_param.stack = sm_stack;
134        t_param.name = sm_task_name;
135        bres = bapp_task_start_task(&sm_st.task, &t_param, bsettop_smessage_parser_task, NULL);
136        sm_st.magic = SM_MAGIC;
137        ExitFunc:
138        return bres;
139}
140
141/*
142  Summary:
143  Cleanup the messages system
144 */
145bresult smessage_uninit(void)
146{
147        sm_st.magic = 0;
148        return eBAPP_RESULT_OK;
149}
150
151/*
152  Summary:
153  Open a message stream for a particular format of data
154 */
155smessage_stream_t smessage_open(smessage_format format)
156{
157        smessage_stream_t st;
158        bapp_result_t result = eBAPP_RESULT_FAILURE;
159
160        st = NULL;
161
162        switch (format)
163        {
164        case smessage_format_ts:
165                break;
166
167        case smessage_format_tsc:
168        case smessage_format_psi:
169                result = bapp_task_acquire_mutex(sm_st.lock, SM_WAIT);
170                if (eBAPP_RESULT_OK != result)
171                {
172                        BDBG_ERR(("%s:%d",__FILE__, __LINE__));
173                        break;
174                }
175               
176                st = BLST_S_FIRST(&sm_st.free_filter);
177                if (NULL != st)
178                {
179                        BLST_S_REMOVE_HEAD(&sm_st.free_filter, next);
180                        st->format = format;
181                        bapp_task_release_mutex(sm_st.lock);
182                } else
183                {
184                        BDBG_ERR(("%s:%d st - NULL",__FILE__, __LINE__));
185                        bapp_task_release_mutex(sm_st.lock);
186                }
187        default:
188                break;
189        }
190        return st;
191}
192/*
193  Summary:
194  Close a message stream
195 */
196void smessage_close(smessage_stream_t stream)
197{
198        bapp_result_t result = eBAPP_RESULT_FAILURE;
199        result = smessage_stop(stream);
200        if (eBAPP_RESULT_OK != result)
201        {
202                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
203        }
204        BLST_S_INSERT_HEAD(&sm_st.free_filter, stream, next);
205}
206
207/*
208  Summary:
209  Initialize parameters structure
210*/
211void smessage_stream_params_init( smessage_stream_params_t *params, smessage_stream_t stream)
212{
213        int i;
214
215        BAPP_UNUSED(stream);
216        if (NULL != params)
217        {
218                memset(params, 0, sizeof(smessage_stream_params_t));
219                params->band = B_INVALID_BAND;
220                params->pid = B_INVALID_PID;
221                params->pid_channel = B_INVALID_PID_CHANNEL;
222                for (i = 0; i < FILTER_SIZE; i++)
223                {
224                        params->filter.mask[i] = 0xFF;
225                        params->filter.excl[i] = 0xFF;
226                }
227        }
228}
229
230/*
231  Summary:
232  Capture message according to the parameters
233 */
234bresult smessage_start(const smessage_stream_params_t * params, smessage_stream_t stream)
235{
236        bapp_result_t result = eBAPP_RESULT_FAILURE;
237    NEXUS_ParserBandSettings parserBandSettings;
238    NEXUS_MessageSettings settings;
239        NEXUS_MemoryAllocationSettings memSettings;
240    NEXUS_PidChannelSettings pidChannelSettings;
241
242        BDBG_ASSERT(NULL != stream);
243        BDBG_ASSERT(NULL != params);
244        BDBG_ASSERT(params->buffer);
245
246        if (((stream->format != smessage_format_psi) && (stream->format != smessage_format_tsc) ) || (B_INVALID_BAND == params->band) || 
247                        (NULL != stream->msg) || (NULL != stream->pidChannel) || (0 == params->buffer_size))
248        {
249                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
250                BDBG_ERR(("stream->msg = 0x%08x",stream->msg));
251                BDBG_ERR(("stream->pidChannel = 0x%08x",stream->pidChannel));
252                BDBG_ERR(("params->pid = 0x%08x",params->pid));
253                BDBG_ERR(("stream->format = %d",stream->format));
254                BDBG_ERR(("params->band = %d",params->band));
255                BDBG_ERR(("params->buffer_size = %d",params->buffer_size));
256                goto ExitFunc;
257        }
258
259        NEXUS_Memory_GetDefaultAllocationSettings(&memSettings);
260
261        result = bapp_task_acquire_mutex(sm_st.lock, SM_WAIT);
262        if (eBAPP_RESULT_OK != result)
263        {
264                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
265                goto ExitFunc;
266        }
267
268        NEXUS_ParserBand_GetSettings((NEXUS_ParserBand)params->band, &parserBandSettings);
269        parserBandSettings.sourceType = NEXUS_ParserBandSourceType_eInputBand;
270        NEXUS_ParserBand_SetSettings((NEXUS_ParserBand)params->band, &parserBandSettings);
271
272        stream->pid = params->pid;
273
274    NEXUS_PidChannel_GetDefaultSettings(&pidChannelSettings);
275    stream->pidChannel = NEXUS_PidChannel_Open((NEXUS_ParserBand)params->band, params->pid, &pidChannelSettings);
276        if (stream->pidChannel == NULL)
277        {
278                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
279                goto ExitUnlock;
280        }
281
282    NEXUS_Message_GetDefaultSettings(&settings);
283    settings.maxContiguousMessageSize = 4096;
284    settings.bufferSize = 0; /* don't have Message alloc the buffer. recommended for maximum flexibility. */
285
286    stream->msg = NEXUS_Message_Open(&settings);
287    NEXUS_Message_GetDefaultStartSettings(stream->msg, &stream->startSettings);
288
289        memSettings.alignment = 4;
290    if (NEXUS_Memory_Allocate(params->buffer_size,&memSettings,&stream->startSettings.buffer) != BERR_SUCCESS)
291        {
292                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
293                NEXUS_Message_Close(stream->msg);
294                stream->msg = NULL;
295                goto ExitUnlock;
296        }
297    stream->buffer = params->buffer;
298
299    stream->startSettings.bufferSize = params->buffer_size;
300        switch (stream->format)
301        {
302        case smessage_format_tsc:
303                stream->startSettings.format = NEXUS_MessageFormat_eTs;
304                break;
305        case smessage_format_psi:
306        default:
307                stream->startSettings.format = NEXUS_MessageFormat_ePsi;
308                break;
309        }
310        stream->startSettings.psfCrcDisabled = true; /* disable CRC check on short packets */
311    stream->buffer_size = params->buffer_size;
312       
313    /* use the default filter for any data */
314
315        memcpy(stream->startSettings.filter.coefficient, params->filter.coef, FILTER_SIZE);
316        memcpy(stream->startSettings.filter.mask, params->filter.mask, FILTER_SIZE);
317        memcpy(stream->startSettings.filter.exclusion, params->filter.excl, FILTER_SIZE);
318        stream->startSettings.pidChannel = stream->pidChannel;
319   
320        if (NEXUS_Message_Start(stream->msg, &stream->startSettings) != NEXUS_SUCCESS)
321        {
322                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
323                NEXUS_Message_Close(stream->msg);
324                stream->msg = NULL;
325                NEXUS_Memory_Free(stream->startSettings.buffer);
326                stream->startSettings.buffer = NULL;
327                goto ExitUnlock;
328        }
329
330        stream->callback = params->data_ready_callback;
331        stream->overflow = params->overflow;
332        stream->context = params->callback_context;
333        stream->priv = params->priv;
334        BLST_S_INSERT_HEAD(&sm_st.started_filter, stream, next);
335
336        result = eBAPP_RESULT_OK;
337
338ExitUnlock:
339        if (eBAPP_RESULT_OK != bapp_task_release_mutex(sm_st.lock))
340        {
341                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
342        }
343ExitFunc:
344        return result;
345}
346
347bresult smessage_stop(smessage_stream_t stream)
348{
349        bapp_result_t result = eBAPP_RESULT_FAILURE;
350        BDBG_ASSERT(NULL != stream);
351        result = bapp_task_acquire_mutex(sm_st.lock, SM_WAIT);
352        if (eBAPP_RESULT_OK != result)
353        {
354                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
355                goto ExitFunc;
356        }
357
358        if (stream->msg == NULL) /* Already closed */
359        {
360                if (eBAPP_RESULT_OK != bapp_task_release_mutex(sm_st.lock))
361                {
362                        BDBG_ERR(("%s:%d",__FILE__, __LINE__));
363                }
364                return eBAPP_RESULT_OK;
365        }
366
367        if (stream->pidChannel != NULL) /* Already closed */
368        {
369                NEXUS_PidChannel_Close(stream->pidChannel);
370                stream->pidChannel = NULL;
371        }
372
373    if (stream->msg)
374        {
375                NEXUS_Message_Stop(stream->msg);
376                NEXUS_Message_Close(stream->msg);
377                stream->msg = NULL;
378        }
379    if (stream->startSettings.buffer)
380    {
381                NEXUS_Memory_Free(stream->startSettings.buffer);
382                stream->startSettings.buffer = NULL;
383        }
384
385        BLST_S_REMOVE(&sm_st.started_filter, stream, smessage_stream, next);
386        result = eBAPP_RESULT_OK;
387
388        if (eBAPP_RESULT_OK != bapp_task_release_mutex(sm_st.lock))
389        {
390                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
391        }
392
393ExitFunc:
394        return result;
395}
396
397bresult smessage_get_buffer(smessage_stream_t stream, const void ** pbuffer, size_t * plength)
398{
399        bapp_result_t result;
400        result = bapp_task_acquire_mutex(sm_st.lock, SM_WAIT);
401        if (eBAPP_RESULT_OK != result)
402        {
403                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
404                goto ExitFunc;
405        }
406
407        if (NEXUS_Message_GetBuffer(stream->msg, (const void **)pbuffer, plength) != NEXUS_SUCCESS)
408        {
409                result = eBAPP_RESULT_FAILURE;
410        }
411
412
413        if (eBAPP_RESULT_OK != bapp_task_release_mutex(sm_st.lock))
414        {
415                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
416        }
417ExitFunc:
418        return result;
419}
420
421bresult smessage_read_complete(smessage_stream_t stream, size_t consumed)
422{
423        bapp_result_t result;
424        result = bapp_task_acquire_mutex(sm_st.lock, SM_WAIT);
425        if (eBAPP_RESULT_OK != result)
426        {
427                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
428                goto ExitFunc;
429        }
430
431        NEXUS_Message_ReadComplete(stream->msg, consumed);
432
433        if (eBAPP_RESULT_OK != bapp_task_release_mutex(sm_st.lock))
434        {
435                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
436        }
437ExitFunc:
438        return result;
439}
440
441static void DBG_HEXDUMP(unsigned char* ptr, unsigned long len)
442{
443        unsigned long i;
444        for (i=0; i<len; i++ )
445        {
446                if ( i % 16 == 0 )
447                        printf ("\n");
448                printf ("0x%02x, ", ptr[i]);
449        }
450        printf("\n\n");
451}
452
453void bsettop_smessage_parser_task(void * param)
454{
455        smessage_stream_t stream;
456        void * buffer;
457        size_t size;
458       
459        BAPP_UNUSED(param);
460
461        while (1)
462        {
463                if (bapp_task_acquire_mutex(sm_st.lock, SM_WAIT) != eBAPP_RESULT_OK)
464                        continue;
465               
466                for(stream = BLST_S_FIRST(&sm_st.started_filter); stream ; stream = BLST_S_NEXT(stream, next)) 
467                {
468                        if (!stream->callback || !stream->buffer)
469                                continue;
470
471                        size = 0;
472                        if (NEXUS_Message_GetBuffer(stream->msg, (const void **)&buffer, &size) == NEXUS_SUCCESS)
473                        {
474                                if (size <= 0)
475                                        continue;
476/*
477                                BDBG_ERR(("%s:%d(0x%08x,%d)",__FILE__, __LINE__,buffer,size));
478                                DBG_HEXDUMP(buffer,size);
479*/
480                                bapp_util_memcpy(stream->buffer,buffer,size); /* use the app-allocated buffer. */
481
482                                stream->buffer = stream->callback(stream->context,size);
483
484                                NEXUS_Message_ReadComplete(stream->msg, size);
485
486                                if (stream->buffer == NULL)
487                                {
488                                        bapp_task_release_mutex(sm_st.lock);
489                                        smessage_stop(stream);
490                                        if (bapp_task_acquire_mutex(sm_st.lock, SM_WAIT) != eBAPP_RESULT_OK)
491                                        {
492                                                BDBG_ERR(("%s:%d",__FILE__, __LINE__));
493                                                break;
494                                        }
495                                }
496                        }
497                }
498
499                bapp_task_release_mutex(sm_st.lock);
500
501                bapp_task_sleep(SM_POLL_INTERVAL);
502        }
503}
504
505
Note: See TracBrowser for help on using the repository browser.