source: svn/trunk/newcon3bcm2_21bu/dta/src/app/ch_map.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 26.2 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2006, 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:
15 *
16 * Revision History:
17 *
18 * $brcm_Log:  $
19 *
20 ***************************************************************************/
21
22#include "bstd.h"
23#include "ministd.h"
24#include "bsettop_smessage.h"
25#include "ch_map.h"
26#include "ts_psi.h"
27#include "ts_psi.h"
28#include "ts_pat.h"
29#include "ts_pmt.h"
30#include "psip_ett.h"
31#include "si.h"
32#include "si_nit.h"
33#include "si_ntt.h"
34#include "si_stt.h"
35#include "si_svct.h"
36
37BDBG_MODULE(ch_map);            /* Register software module with debug interface */
38
39#define CH_MAP_MUTEX_TIMEOUT    500
40
41/*
42Summary:
43        Initialize channel map mgmt internals (structure may already be initialized).
44        Initialize mutex if it is 0.
45*/
46void ch_map_init(ch_map_t *p_ch_map,b_mutex_t *p_mutex)
47{
48        BDBG_ASSERT(p_ch_map);
49        BDBG_ASSERT(p_mutex);
50        p_ch_map->p_mutex = p_mutex;
51}
52
53/*
54Summary:
55        reset channel map mgmt internals.
56*/
57void ch_map_reset(ch_map_t *p_ch_map)
58{
59        BDBG_ASSERT(p_ch_map);
60        BDBG_ASSERT(p_ch_map->p_mutex);
61        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
62        {
63                p_ch_map->mms_map.num_mms = 0;
64                p_ch_map->freq_map.num_freq = 0;
65                p_ch_map->st_map.num_st = 0;
66                p_ch_map->vch_map.num_vch = 0;
67                bos_release_mutex(p_ch_map->p_mutex);
68        } else
69        {
70                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
71        }
72}
73
74/*
75Summary:
76        copy channel map.
77*/
78void ch_map_copy(ch_map_t *p_ch_map_to,ch_map_t *p_ch_map_from,unsigned int flags)
79{
80        BDBG_ASSERT(p_ch_map_to);
81        BDBG_ASSERT(p_ch_map_from);
82        BDBG_ASSERT(p_ch_map_to->p_mutex);
83        BDBG_ASSERT(p_ch_map_from->p_mutex);
84        if (bos_acquire_mutex(p_ch_map_to->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
85        {
86                if (bos_acquire_mutex(p_ch_map_from->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
87                {
88                        p_ch_map_to->cur_ch = p_ch_map_from->cur_ch;            /* current channel */
89
90                        if (flags & 0x8)
91                                memcpy(&p_ch_map_to->mms_map,&p_ch_map_from->mms_map,sizeof(mms_map_t));
92                        if (flags & 0x1)
93                                memcpy(&p_ch_map_to->freq_map,&p_ch_map_from->freq_map,sizeof(freq_map_t));
94                        if (flags & 0x4)
95                                memcpy(&p_ch_map_to->st_map,&p_ch_map_from->st_map,sizeof(st_map_t));
96                        if (flags & 0x2)
97                                memcpy(&p_ch_map_to->vch_map,&p_ch_map_from->vch_map,sizeof(vch_map_t));
98                        bos_release_mutex(p_ch_map_from->p_mutex);
99                } else
100                {
101                        BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
102                }
103                bos_release_mutex(p_ch_map_to->p_mutex);
104        } else
105        {
106                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
107        }
108}
109/*
110Summary:
111        compare mms maps.
112*/
113int ch_map_cmp_mms(mms_map_t    *p_mms_map_to, mms_map_t *p_mms_map_from)
114{
115        mms_t   *p_mms_from;
116        mms_t   *p_mms_to;
117        int i;
118
119        if (p_mms_map_to->num_mms != p_mms_map_from->num_mms)
120        {
121                return -1;
122        }
123
124        for (i = 0; i < p_mms_map_to->num_mms; ++i)
125        {
126                p_mms_from =  &(p_mms_map_from->mms[i]);
127                p_mms_to = &(p_mms_map_to->mms[i]);
128
129                if (p_mms_from->code_rate != p_mms_to->code_rate)
130                        return -1;
131
132                if (p_mms_from->idx != p_mms_to->idx)
133                        return -1;
134
135                if (p_mms_from->modulation != p_mms_to->modulation)
136                        return -1;
137
138                if (p_mms_from->symbol_rate != p_mms_to->symbol_rate)
139                        return -1;
140        }
141
142        return 0;
143}
144/*
145Summary:
146        compare freq maps.
147*/
148int ch_map_cmp_freq(freq_map_t  *p_freq_map_to, freq_map_t *p_freq_map_from)
149{
150        freq_t  *p_from;
151        freq_t  *p_to;
152        int i;
153
154        if (p_freq_map_to->num_freq != p_freq_map_from->num_freq)
155        {
156                return -1;
157        }
158
159        for (i = 0; i < p_freq_map_to->num_freq; ++i)
160        {
161                p_from =  &(p_freq_map_from->freq[i]);
162                p_to = &(p_freq_map_to->freq[i]);
163
164                if (p_from->freq_khz != p_to->freq_khz)
165                        return -1;
166
167                if (p_from->idx != p_to->idx)
168                        return -1;
169        }
170
171        return 0;
172}
173/*
174Summary:
175        compare st entry to entries in to map.
176*/
177int ch_map_cmp_st_entry(st_map_t        *p_map_to, st_t *p_from)
178{
179        st_t    *p_to;
180        int i;
181
182        for (i = 0; i < p_map_to->num_st; ++i)
183        {
184                p_to = &(p_map_to->st[i]);
185
186                if ((p_from->source_id == p_to->source_id) && (strcmp(p_from->name,p_to->name) == 0))
187                        return 0;
188        }
189
190        return -1;
191}
192/*
193Summary:
194        compare st maps.
195*/
196int ch_map_cmp_st(st_map_t      *p_map_to, st_map_t *p_map_from)
197{
198        st_t    *p_from;
199        int i;
200
201        if (p_map_to->num_st != p_map_from->num_st)
202        {
203                return -1;
204        }
205
206        for (i = 0; i < p_map_from->num_st; ++i)
207        {
208                p_from =  &(p_map_from->st[i]);
209
210                if (ch_map_cmp_st_entry(p_map_to,p_from) != 0)
211                        return -1;
212        }
213
214        return 0;
215}
216/*
217Summary:
218        compare vch maps.
219*/
220int ch_map_cmp_vch(vch_map_t    *p_map_to, vch_map_t *p_map_from)
221{
222        vch_t   *p_from;
223        vch_t   *p_to;
224        int i;
225
226        if (p_map_to->num_vch != p_map_from->num_vch)
227        {
228                return -1;
229        }
230
231        for (i = 0; i < p_map_to->num_vch; ++i)
232        {
233                p_from =  &(p_map_from->vch[i]);
234                p_to = &(p_map_to->vch[i]);
235               
236                if (p_from->ch_num != p_to->ch_num)
237                        return -1;
238                if (p_from->source_id != p_to->source_id)
239                        return -1;
240                if (p_from->program_num != p_to->program_num)
241                        return -1;
242                if (p_from->freq_idx != p_to->freq_idx)
243                        return -1;
244                if (p_from->mms_idx != p_to->mms_idx)
245                        return -1;
246                if (p_from->vchflags != p_to->vchflags)
247                        return -1;
248        }
249
250        return 0;
251}
252
253int ch_map_cmp_vch_with_pid(vch_map_t *p_map_to, vch_map_t *p_map_from)
254{
255        int i;
256        vch_t   *p_from;
257        vch_t   *p_to;
258
259        for (i=0; i<p_map_to->num_vch; ++i)
260        {
261                p_from = &(p_map_from->vch[i]);
262                p_to = &(p_map_to->vch[i]);
263                if ((p_from->video_pid != 0) && (p_to->video_pid != p_from->video_pid)) {
264                        return -1;
265                }
266                if ((p_from->audio_pid[0] != 0) && (p_to->audio_pid[0] != p_from->audio_pid[0])) {
267                        return -1;
268                }
269        }
270        return 0;
271}
272/*
273Summary:
274        check channel map for changes.
275*/
276unsigned int ch_map_cmp(ch_map_t *p_ch_map_to,ch_map_t *p_ch_map_from)
277{
278        int cmp_result = 0;
279        BDBG_ASSERT(p_ch_map_to);
280        BDBG_ASSERT(p_ch_map_from);
281        BDBG_ASSERT(p_ch_map_to->p_mutex);
282        BDBG_ASSERT(p_ch_map_from->p_mutex);
283        if (bos_acquire_mutex(p_ch_map_to->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
284        {
285                if (bos_acquire_mutex(p_ch_map_from->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
286                {
287                        if (p_ch_map_to->mms_map.num_mms != p_ch_map_from->mms_map.num_mms)
288                                cmp_result |= 0x8;
289                        if (p_ch_map_to->freq_map.num_freq != p_ch_map_from->freq_map.num_freq)
290                                cmp_result |= 0x1;
291                        if (p_ch_map_to->st_map.num_st != p_ch_map_from->st_map.num_st)
292                                cmp_result |= 0x4;
293                        if (p_ch_map_to->vch_map.num_vch != p_ch_map_from->vch_map.num_vch)
294                                cmp_result |= 0x2;
295                        if (ch_map_cmp_mms(&p_ch_map_to->mms_map,&p_ch_map_from->mms_map) != 0)
296                                cmp_result |= 0x8;
297                        if (ch_map_cmp_freq(&p_ch_map_to->freq_map,&p_ch_map_from->freq_map) != 0)
298                                cmp_result |= 0x1;
299                        if (ch_map_cmp_st(&p_ch_map_to->st_map,&p_ch_map_from->st_map) != 0)
300                                cmp_result |= 0x4;
301                        if (ch_map_cmp_vch(&p_ch_map_to->vch_map,&p_ch_map_from->vch_map) != 0)
302                                cmp_result |= 0x2;
303                        bos_release_mutex(p_ch_map_from->p_mutex);
304                } else
305                {
306                        BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
307                }
308                bos_release_mutex(p_ch_map_to->p_mutex);
309        } else
310        {
311                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
312        }
313        return cmp_result;
314}
315/*
316Summary:
317        Check for matching entry.
318*/
319static bool ch_map_check_mms(ch_map_t *p_ch_map, mms_t *p_mms)
320{
321        int i;
322        for (i = 0; i < p_ch_map->mms_map.num_mms; ++i)
323        {
324                if (p_ch_map->mms_map.mms[i].idx == p_mms->idx)
325                        return true;
326        }
327        BDBG_MSG(("%s:%d no entry exists\n", __FUNCTION__,__LINE__));
328        return false;
329}
330
331/*
332Summary:
333        Add an mms entry.
334*/
335void ch_map_add_mms(ch_map_t *p_ch_map, mms_t *p_mms)
336{
337        BDBG_ASSERT(p_ch_map);
338        BDBG_ASSERT(p_ch_map->p_mutex);
339        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
340        {
341                if (p_ch_map->mms_map.num_mms == MAX_FREQ_MAP)
342                {
343                        bos_release_mutex(p_ch_map->p_mutex);
344                        BDBG_WRN(("%s:%d ENTRY LIMIT %d of %d\n", __FUNCTION__,__LINE__,p_ch_map->mms_map.num_mms,MAX_FREQ_MAP));
345                        return;
346                }
347                if (!ch_map_check_mms(p_ch_map,p_mms))
348                {
349                        p_ch_map->mms_map.mms[p_ch_map->mms_map.num_mms] = *p_mms;
350                        p_ch_map->mms_map.num_mms++;
351                }
352                bos_release_mutex(p_ch_map->p_mutex);
353                BDBG_MSG(("%s:%d num_mms = %d\n", __FUNCTION__,__LINE__,p_ch_map->mms_map.num_mms));
354        } else
355        {
356                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
357        }
358}
359/*
360Summary:
361        Get mms for frequency idx.
362*/
363bool ch_map_get_mms(ch_map_t *p_ch_map, 
364                                        unsigned char mms_idx,
365                                        mms_t *p_mms
366                                        )
367{
368        int idx;
369        bool result = false;
370        BDBG_ASSERT(p_ch_map);
371        BDBG_ASSERT(p_ch_map->p_mutex);
372        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
373        {
374                for (idx = 0; idx < p_ch_map->mms_map.num_mms; ++idx)
375                {
376                        if (mms_idx == p_ch_map->mms_map.mms[idx].idx)
377                        {
378                                *p_mms = p_ch_map->mms_map.mms[idx];
379                                result = true;
380                                break;
381                        }
382                }
383       
384                bos_release_mutex(p_ch_map->p_mutex);
385        }
386        else
387                return false;
388        return result;
389}
390
391/*
392Summary:
393        Check for matching entry.
394*/
395static bool ch_map_check_freq(ch_map_t *p_ch_map, freq_t *p_freq)
396{
397        int i;
398        for (i = 0; i < p_ch_map->freq_map.num_freq; ++i)
399        {
400                if (p_ch_map->freq_map.freq[i].idx == p_freq->idx)
401                        return true;
402        }
403        BDBG_MSG(("%s:%d no entry exists\n", __FUNCTION__,__LINE__));
404        return false;
405}
406
407
408/*
409Summary:
410        Add a frequency entry.
411*/
412void ch_map_add_freq(ch_map_t *p_ch_map, freq_t *p_freq)
413{
414        BDBG_ASSERT(p_ch_map);
415        BDBG_ASSERT(p_ch_map->p_mutex);
416        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
417        {
418                if (p_ch_map->freq_map.num_freq == MAX_FREQ_MAP)
419                {
420                        bos_release_mutex(p_ch_map->p_mutex);
421                        BDBG_WRN(("%s:%d ENTRY LIMIT %d of %d\n", __FUNCTION__,__LINE__,p_ch_map->freq_map.num_freq,MAX_FREQ_MAP));
422                        return;
423                }
424                if (!ch_map_check_freq(p_ch_map,p_freq))
425                {
426                        p_ch_map->freq_map.freq[p_ch_map->freq_map.num_freq] = *p_freq;
427                        p_ch_map->freq_map.num_freq++;
428                }
429                bos_release_mutex(p_ch_map->p_mutex);
430                BDBG_MSG(("%s:%d num_freq = %d\n", __FUNCTION__,__LINE__,p_ch_map->freq_map.num_freq));
431        } else
432        {
433                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
434        }
435}
436/*
437Summary:
438        Check for matching entry.
439*/
440static bool ch_map_check_vch(ch_map_t *p_ch_map, vch_t *p_vch)
441{
442        int i;
443        for (i = 0; i < p_ch_map->vch_map.num_vch; ++i)
444        {
445                if (p_ch_map->vch_map.vch[i].ch_num == p_vch->ch_num)
446                        return true;
447        }
448        BDBG_MSG(("%s:%d no entry exists(%d,%d)\n", __FUNCTION__,__LINE__,p_vch->ch_num,p_vch->program_num));
449        return false;
450}
451/*
452Summary:
453        Add a virtual channel entry.
454*/
455void ch_map_add_vch(ch_map_t *p_ch_map, vch_t *p_vch)
456{
457        BDBG_ASSERT(p_ch_map);
458        BDBG_ASSERT(p_ch_map->p_mutex);
459        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
460        {
461                int idx,idx2, found_slot;
462                found_slot = 0;
463
464                if (p_ch_map->vch_map.num_vch == MAX_VCH)
465                {
466                        bos_release_mutex(p_ch_map->p_mutex);
467                        BDBG_WRN(("%s:%d ENTRY LIMIT, %d of %d\n", __FUNCTION__,__LINE__,p_ch_map->vch_map.num_vch,MAX_VCH));
468                        return;
469                }
470
471                if (ch_map_check_vch(p_ch_map,p_vch))
472                {
473                        bos_release_mutex(p_ch_map->p_mutex);
474                        BDBG_MSG(("%s:%d entry exists(%d,%d)\n", __FUNCTION__,__LINE__,p_vch->ch_num,p_vch->program_num));
475                        return;
476                }
477                for (idx = 0; idx < p_ch_map->vch_map.num_vch; idx++)
478                {
479                        if (p_vch->ch_num < p_ch_map->vch_map.vch[idx].ch_num)
480                        {
481                                for (idx2 = p_ch_map->vch_map.num_vch - 1; idx2 >= idx; idx2--)
482                                {
483                                        BDBG_MSG(("move %d from %d to %d\n",p_ch_map->vch_map.vch[idx2].ch_num, idx2, idx2 + 1));
484                                        memcpy(&p_ch_map->vch_map.vch[idx2 + 1],&p_ch_map->vch_map.vch[idx2], sizeof(vch_t));
485                                }
486
487                                memcpy(&p_ch_map->vch_map.vch[idx],p_vch, sizeof(vch_t));
488                                BDBG_MSG(("Add %d at %d\n",p_vch->ch_num, idx));
489                                found_slot = 1;
490                                break;
491                        }
492                }
493                if (!found_slot)
494                {
495                        memcpy(&p_ch_map->vch_map.vch[p_ch_map->vch_map.num_vch],p_vch, sizeof(vch_t));
496                        BDBG_MSG(("Append %d at %d\n",p_vch->ch_num, p_ch_map->vch_map.num_vch));
497                }
498                p_ch_map->vch_map.num_vch++;
499                bos_release_mutex(p_ch_map->p_mutex);
500        } else
501        {
502                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
503        }
504}
505
506/*
507Summary:
508        Check for matching entry.
509*/
510static bool ch_map_check_st(ch_map_t *p_ch_map, st_t *p_st)
511{
512        int i;
513        for (i = 0; i < p_ch_map->st_map.num_st; ++i)
514        {
515                if (p_ch_map->st_map.st[i].source_id == p_st->source_id)
516                        return true;
517        }
518        BDBG_MSG(("%s:%d no entry exists\n", __FUNCTION__,__LINE__));
519        return false;
520}
521
522/*
523Summary:
524        Add a source text entry.
525*/
526void ch_map_add_st(ch_map_t *p_ch_map, st_t     *p_st)
527{
528        BDBG_ASSERT(p_ch_map);
529        BDBG_ASSERT(p_ch_map->p_mutex);
530        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
531        {
532                if (p_ch_map->st_map.num_st == MAX_VCH)
533                {
534                        bos_release_mutex(p_ch_map->p_mutex);
535                        BDBG_WRN(("%s:%d ENTRY LIMIT\n", __FUNCTION__,__LINE__));
536                        return;
537                }
538
539                if (!ch_map_check_st(p_ch_map,p_st))
540                {
541                        p_ch_map->st_map.st[p_ch_map->st_map.num_st] = *p_st;
542                        p_ch_map->st_map.num_st++;
543                }
544                bos_release_mutex(p_ch_map->p_mutex);
545        } else
546        {
547                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
548        }
549}
550
551#ifdef CONFIG_DVB
552/*
553 * Summary:
554 * Add source text entry if it's new entry, or update the existing entry
555 */
556void ch_map_add_update_st(ch_map_t *p_ch_map, st_t *p_st)
557{
558        int i;
559
560        BDBG_ASSERT(p_ch_map);
561        BDBG_ASSERT(p_ch_map->p_mutex);
562        if (bos_acquire_mutex(p_ch_map->p_mutex, CH_MAP_MUTEX_TIMEOUT) == b_ok)
563        {
564                for (i=0; i<p_ch_map->st_map.num_st; ++i) {
565                        if (p_ch_map->st_map.st[i].source_id == p_st->source_id) {
566                                break;
567                        }
568                }
569               
570                if (i == p_ch_map->st_map.num_st)
571                {
572                        if (p_ch_map->st_map.num_st == MAX_VCH) {
573                                bos_release_mutex(p_ch_map->p_mutex);
574                                BDBG_WRN(("%s:%d ENTRY LIMIT", __FUNCTION__, __LINE__));
575                                return;
576                        }
577                        p_ch_map->st_map.st[p_ch_map->st_map.num_st] = *p_st;
578                        p_ch_map->st_map.num_st++;
579                }
580                else {
581                        p_ch_map->st_map.st[i] = *p_st; 
582                }
583                bos_release_mutex(p_ch_map->p_mutex);
584        } else 
585        {
586                BDBG_WRN(("%s:%d mutex timeout", __FUNCTION__,__LINE__));
587        }
588}
589
590bool ch_map_is_new_vch(ch_map_t *p_ch_map, vch_t *p_vch)
591{
592        int i;
593        BDBG_ASSERT(p_ch_map);
594        BDBG_ASSERT(p_ch_map->p_mutex);
595        if (bos_acquire_mutex(p_ch_map->p_mutex, CH_MAP_MUTEX_TIMEOUT) == b_ok)
596        {
597                for (i = 0; i < p_ch_map->vch_map.num_vch; ++i)
598                {
599                        if (p_ch_map->vch_map.vch[i].program_num == p_vch->program_num) {
600                                bos_release_mutex(p_ch_map->p_mutex);   
601                                return false;
602                        }
603                }
604                bos_release_mutex(p_ch_map->p_mutex);
605                return true;
606        }
607        else {
608                BDBG_WRN(("%s:%d mutex timeout", __FUNCTION__,__LINE__));
609        }
610
611        return true;
612}       
613#endif
614       
615/*
616Summary:
617        Get number of map entries.
618*/
619void ch_map_get_counts(ch_map_t *p_ch_map, 
620                                           unsigned short *num_vch, 
621                                           unsigned short *num_st,
622                                           unsigned char *num_freq
623                                           )
624{
625        BDBG_ASSERT(p_ch_map);
626        BDBG_ASSERT(p_ch_map->p_mutex);
627        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
628        {
629                *num_vch = p_ch_map->vch_map.num_vch;
630                *num_st = p_ch_map->st_map.num_st;
631                *num_freq = p_ch_map->freq_map.num_freq;
632                bos_release_mutex(p_ch_map->p_mutex);
633        } else
634        {
635                *num_vch = 0;
636                *num_st = 0;
637                *num_freq = 0;
638                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
639        }
640}
641
642/*
643Summary:
644        Get number of map entries.
645*/
646void ch_map_get_vch_num(ch_map_t *p_ch_map, 
647                                           unsigned short *num_vch
648                                           )
649{
650        BDBG_ASSERT(p_ch_map);
651        BDBG_ASSERT(p_ch_map->p_mutex);
652        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
653        {
654                *num_vch = p_ch_map->vch_map.num_vch;
655                bos_release_mutex(p_ch_map->p_mutex);
656        } else
657        {
658                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
659        }
660}
661/*
662Summary:
663        Find the matching freqency info for the given the vch reference.
664*/
665bool ch_map_find_freq(ch_map_t *p_ch_map, vch_t *p_vch, freq_t *p_freq)
666{
667        int idx;
668        for (idx = 0; idx < p_ch_map->freq_map.num_freq; ++idx)
669        {
670                if (p_vch->freq_idx == p_ch_map->freq_map.freq[idx].idx)
671                {
672                        *p_freq = p_ch_map->freq_map.freq[idx];
673                        return true;
674                }
675        }
676        BDBG_MSG(("%s:%d frequncy %d not found\n", __FUNCTION__,__LINE__,p_vch->freq_idx));
677        return false;
678}
679/*
680Summary:
681        Find the matching source text for the given the vch reference.
682*/
683static bool ch_map_find_st(ch_map_t *p_ch_map, vch_t *p_vch, st_t       *p_st)
684{
685        int idx;
686        for (idx = 0; idx < p_ch_map->st_map.num_st; ++idx)
687        {
688                if (p_vch->source_id == p_ch_map->st_map.st[idx].source_id)
689                {
690                        *p_st = p_ch_map->st_map.st[idx];
691                        return true;
692                }
693        }
694        return false;
695}
696
697/*
698Summary:
699        Get current channel information.
700*/
701bool ch_map_get_current(ch_map_t *p_ch_map, vch_t *p_vch, st_t  *p_st, freq_t *p_freq)
702{
703        bool result = false;
704        BDBG_ASSERT(p_ch_map);
705        BDBG_ASSERT(p_ch_map->p_mutex);
706        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
707        {
708                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
709                {
710                        bos_release_mutex(p_ch_map->p_mutex);
711                        BDBG_MSG(("%s:%d channel map empty(%d,%d)\n", __FUNCTION__,__LINE__,p_ch_map->freq_map.num_freq,p_ch_map->vch_map.num_vch));
712                        return result;
713                }
714
715                *p_vch = p_ch_map->vch_map.vch[p_ch_map->cur_ch];
716                if (ch_map_find_freq(p_ch_map,p_vch,p_freq))
717                {
718                        result = true;
719                }
720
721                if (!ch_map_find_st(p_ch_map,p_vch,p_st))
722                {
723                        p_st->name[0] = '\0';
724                        p_st->source_id = 0;
725                }
726                bos_release_mutex(p_ch_map->p_mutex);
727        } else
728        {
729                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
730        }
731        return result;
732}
733
734/*
735Summary:
736        Update current channel information.
737*/
738bool ch_map_vch_update(ch_map_t *p_ch_map, vch_t *p_vch)
739{
740        bool result = false;
741        BDBG_ASSERT(p_ch_map);
742        BDBG_ASSERT(p_ch_map->p_mutex);
743        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
744        {
745                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
746                {
747                        bos_release_mutex(p_ch_map->p_mutex);
748                        BDBG_MSG(("%s:%d channel map empty(%d,%d)\n", __FUNCTION__,__LINE__,p_ch_map->freq_map.num_freq,p_ch_map->vch_map.num_vch));
749                        return result;
750                }
751
752                /* only update if this vch matches current vch */
753                if ((p_ch_map->vch_map.vch[p_ch_map->cur_ch].freq_idx != p_vch->freq_idx) ||
754                        (p_ch_map->vch_map.vch[p_ch_map->cur_ch].source_id != p_vch->source_id) ||
755                        (p_ch_map->vch_map.vch[p_ch_map->cur_ch].ch_num != p_vch->ch_num) ||
756                        (p_ch_map->vch_map.vch[p_ch_map->cur_ch].program_num != p_vch->program_num))
757                {
758                        bos_release_mutex(p_ch_map->p_mutex);
759                        BDBG_WRN(("%s:%d current vch changed, abort update(%d,0x%04x,%d,%d) != (%d,0x%04x,%d,%d)\n",
760                                           __FUNCTION__,__LINE__,
761                                          p_ch_map->vch_map.vch[p_ch_map->cur_ch].freq_idx,
762                                          p_ch_map->vch_map.vch[p_ch_map->cur_ch].source_id,
763                                          p_ch_map->vch_map.vch[p_ch_map->cur_ch].ch_num,
764                                          p_ch_map->vch_map.vch[p_ch_map->cur_ch].program_num,
765                                          p_vch->freq_idx,p_vch->source_id,p_vch->ch_num,p_vch->program_num
766                                          ));
767                        return result;
768                }
769                p_ch_map->vch_map.vch[p_ch_map->cur_ch] = *p_vch;
770                result = true;
771                bos_release_mutex(p_ch_map->p_mutex);
772        } 
773        else
774        {
775                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
776        }
777        return result;
778}
779
780/*
781Summary:
782        Set next channel information.
783*/
784bool ch_map_set_next(ch_map_t *p_ch_map)
785{
786        bool result = false;
787        int idx = 0;
788        BDBG_ASSERT(p_ch_map);
789        BDBG_ASSERT(p_ch_map->p_mutex);
790        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
791        {
792                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
793                {
794                        bos_release_mutex(p_ch_map->p_mutex);
795                        return result;
796                }
797
798                do
799                {
800                        p_ch_map->cur_ch++;
801
802                        if (p_ch_map->cur_ch >= p_ch_map->vch_map.num_vch)
803                                p_ch_map->cur_ch = 0;
804
805            result = ((p_ch_map->vch_map.vch[p_ch_map->cur_ch].vchflags == 0) && 
806                                          !(p_ch_map->vch_map.vch[p_ch_map->cur_ch].hd_flag&0x5)) ? true : false;
807                } while (!result && (idx++ < p_ch_map->vch_map.num_vch));
808               
809                bos_release_mutex(p_ch_map->p_mutex);
810        } else
811        {
812                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
813        }
814        return result;
815}
816
817
818/*
819Summary:
820        Set prev channel information.
821*/
822bool ch_map_set_prev(ch_map_t *p_ch_map)
823{
824        bool result = false;
825        int idx = 0;
826        BDBG_ASSERT(p_ch_map);
827        BDBG_ASSERT(p_ch_map->p_mutex);
828        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
829        {
830                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
831                {
832                        bos_release_mutex(p_ch_map->p_mutex);
833                        return result;
834                }
835
836                do
837                {
838                        if (p_ch_map->cur_ch == 0)
839                                p_ch_map->cur_ch = p_ch_map->vch_map.num_vch - 1;
840                        else
841                                p_ch_map->cur_ch--;
842            result = ((p_ch_map->vch_map.vch[p_ch_map->cur_ch].vchflags == 0) && 
843                                          !(p_ch_map->vch_map.vch[p_ch_map->cur_ch].hd_flag&0x5)) ? true : false;
844                } while (!result && (idx++ < p_ch_map->vch_map.num_vch));
845
846                bos_release_mutex(p_ch_map->p_mutex);
847        } else
848        {
849                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
850        }
851        return result;
852}
853
854/*Summary:
855        determine if a given channel is visible (not hidden) in the channel map.
856*/
857bool ch_map_is_channel_visible(ch_map_t *p_ch_map,
858                                   unsigned short ch_num)
859{
860        bool result = false;
861        int idx;
862        BDBG_ASSERT(p_ch_map);
863        BDBG_ASSERT(p_ch_map->p_mutex);
864
865        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
866        {
867                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
868                {
869                        bos_release_mutex(p_ch_map->p_mutex);
870                        return result;
871                }
872
873                for (idx = 0; idx < p_ch_map->vch_map.num_vch; ++idx)
874                {
875                        if (p_ch_map->vch_map.vch[idx].ch_num == ch_num)
876                        {
877                if (p_ch_map->vch_map.vch[idx].vchflags == 0 && !(p_ch_map->vch_map.vch[idx].hd_flag&0x5))
878                                {
879                                 result = true;
880                            }
881                            bos_release_mutex(p_ch_map->p_mutex);
882                            return result;//we are done looking, get out
883                        }
884                }
885
886                bos_release_mutex(p_ch_map->p_mutex);
887        } else
888        {
889                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
890        }
891        return result;
892}
893
894
895/*
896Summary:
897        Set channel information by number.
898*/
899bool ch_map_set_ch(ch_map_t *p_ch_map,
900                                   unsigned short ch_num)
901{
902        bool result = false;
903        int idx;
904        BDBG_ASSERT(p_ch_map);
905        BDBG_ASSERT(p_ch_map->p_mutex);
906
907        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
908        {
909                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
910                {
911                        bos_release_mutex(p_ch_map->p_mutex);
912                        return result;
913                }
914
915                for (idx = 0; idx < p_ch_map->vch_map.num_vch; ++idx)
916                {
917                        if (p_ch_map->vch_map.vch[idx].ch_num == ch_num)
918                        {
919                                p_ch_map->cur_ch = idx;
920                                result = true;
921                                break;
922                        }
923                }
924
925                bos_release_mutex(p_ch_map->p_mutex);
926        } else
927        {
928                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
929        }
930        return result;
931}
932
933/*
934Summary:
935        Set current channel given source ID.
936*/
937bool ch_map_set(ch_map_t *p_ch_map,unsigned short source_id)
938{
939        bool result = false;
940        int idx;
941        BDBG_ASSERT(p_ch_map);
942        BDBG_ASSERT(p_ch_map->p_mutex);
943
944        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
945        {
946                if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
947                {
948                        bos_release_mutex(p_ch_map->p_mutex);
949                        return result;
950                }
951
952                for (idx = 0; idx < p_ch_map->vch_map.num_vch; ++idx)
953                {
954                        if (p_ch_map->vch_map.vch[idx].source_id == source_id)
955                        {
956                                p_ch_map->cur_ch = idx;
957                                result = true;
958                                break;
959                        }
960                }
961
962                bos_release_mutex(p_ch_map->p_mutex);
963        } else
964        {
965                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
966        }
967        return result;
968}
969
970/*
971Summary:
972        try to find a source ID from the frequency vector and program number (for EAS major/minor ch#).
973*/
974bool ch_map_find_soure_id(ch_map_t *p_ch_map,
975         unsigned char freq_vec,
976         unsigned short prog_num,
977         unsigned short *p_source_id
978         )
979{
980        bool result = false;
981        int idx;
982        BDBG_ASSERT(p_ch_map);
983        BDBG_ASSERT(p_ch_map->p_mutex);
984
985        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
986        {
987        if ((p_ch_map->freq_map.num_freq <= 0) || (p_ch_map->vch_map.num_vch <= 0))
988        {
989                        bos_release_mutex(p_ch_map->p_mutex);
990                return result;
991        }
992
993        for (idx = 0; idx < p_ch_map->vch_map.num_vch; ++idx)
994        {
995                if ((p_ch_map->vch_map.vch[idx].freq_idx == freq_vec) && (p_ch_map->vch_map.vch[idx].program_num == prog_num))
996                {
997                        *p_source_id = p_ch_map->vch_map.vch[idx].source_id;
998                        result = true;
999                        break;
1000                        }
1001                }
1002
1003                bos_release_mutex(p_ch_map->p_mutex);
1004        } else
1005        {
1006                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
1007        }
1008        return result;
1009}
1010/*
1011Summary:
1012Output details to console.
1013 */
1014void ch_map_print_vch(vch_t *p_vch)
1015{
1016        int i;
1017        printf("\t\t{ %d,0x%04x,%d,%d,%d,0x%02x,%d,%d,0x%04x,0x%04x,0x%04x,0x%02x,0x%04x,{",
1018                p_vch->ch_num, 
1019                p_vch->source_id,
1020                p_vch->program_num, 
1021                p_vch->freq_idx,
1022                p_vch->mms_idx,
1023                p_vch->vchflags,
1024                p_vch->num_audio,
1025                p_vch->cur_audio,
1026                p_vch->pcr_pid,
1027                p_vch->scte127_pid,
1028                p_vch->video_pid,
1029                p_vch->video_type,
1030                p_vch->ca_pid);
1031
1032        for (i = 0; i < MAX_AUDIO_PIDS; i++)
1033        {
1034                if (i >= p_vch->num_audio)
1035                        printf("0x0000,");
1036                else
1037                        printf("0x%04x,",p_vch->audio_pid[i]);
1038        }
1039        printf("},{");
1040
1041        for (i = 0; i < MAX_AUDIO_PIDS; i++)
1042        {
1043                if (i >= p_vch->num_audio)
1044                        printf("0x00,");
1045                else
1046                        printf("0x%02x,",p_vch->audio_type[i]);
1047        }
1048        printf("},{");
1049
1050        for (i = 0; i < MAX_AUDIO_PIDS; i++)
1051        {
1052                if (i >= p_vch->num_audio)
1053                        printf("\' \',\' \',\' \',");
1054                else
1055                   printf("%c,%c,%c,",p_vch->audio_lang[i][0], p_vch->audio_lang[i][1],p_vch->audio_lang[i][2]);
1056        }
1057        printf("} },\n");
1058}
1059/*
1060Summary:
1061Output details to console.
1062 */
1063void ch_output(ch_map_t *p_ch_map)
1064{
1065        int idx;
1066        BDBG_ASSERT(p_ch_map);
1067        BDBG_ASSERT(p_ch_map->p_mutex);
1068
1069        if (bos_acquire_mutex(p_ch_map->p_mutex,CH_MAP_MUTEX_TIMEOUT) == b_ok)
1070        {
1071                printf("const ch_map_t s_ch_map = \n{\n");
1072                printf("\t0x%08x,\n",0);
1073                printf("\t%d,\n",p_ch_map->cur_ch);
1074                printf("\t{ %d,\n",p_ch_map->mms_map.num_mms);
1075
1076                for (idx = 0; idx < p_ch_map->mms_map.num_mms; ++idx)
1077                {
1078                        printf("\t\t%c{ %d,%d,%d,%d }%c,\n",(idx == 0) ? '{' : ' ',
1079                                        p_ch_map->mms_map.mms[idx].idx, p_ch_map->mms_map.mms[idx].code_rate,p_ch_map->mms_map.mms[idx].modulation,p_ch_map->mms_map.mms[idx].symbol_rate,
1080                                        (idx == (p_ch_map->mms_map.num_mms - 1)) ? '}' : ' ');
1081                }
1082                printf("\t},\n");
1083
1084                printf("\t{ %d,\n",p_ch_map->freq_map.num_freq);
1085
1086                for (idx = 0; idx < p_ch_map->freq_map.num_freq; ++idx)
1087                {
1088                        printf("\t\t%c{ %d,%d }%c,\n",(idx == 0) ? '{' : ' ',
1089                                        p_ch_map->freq_map.freq[idx].idx, p_ch_map->freq_map.freq[idx].freq_khz,
1090                                        (idx == (p_ch_map->freq_map.num_freq - 1)) ? '}' : ' ');
1091                }
1092                printf("\t},\n");
1093
1094                printf("\t{ %d,\n",p_ch_map->st_map.num_st);
1095
1096                for (idx = 0; idx < p_ch_map->st_map.num_st; ++idx)
1097                {
1098                        printf("\t\t%c{ 0x%04x,\"%s\" }%c,\n",(idx == 0) ? '{' : ' ',
1099                                        p_ch_map->st_map.st[idx].source_id, p_ch_map->st_map.st[idx].name,
1100                                        (idx == (p_ch_map->st_map.num_st - 1)) ? '}' : ' ');
1101                }
1102                printf("\t},\n");
1103
1104                printf("\t{ %d,\n",p_ch_map->vch_map.num_vch);
1105                printf("\t\t{");
1106
1107                for (idx = 0; idx < p_ch_map->vch_map.num_vch; ++idx)
1108                {
1109                        bos_sleep(50);
1110                        ch_map_print_vch(&(p_ch_map->vch_map.vch[idx]));
1111                }
1112                printf("\n\t\t}\n");
1113                printf("\t},\n");
1114
1115                printf("};\n");
1116
1117                bos_release_mutex(p_ch_map->p_mutex);
1118        } else
1119        {
1120                BDBG_WRN(("%s:%d mutex timeout\n", __FUNCTION__,__LINE__));
1121        }
1122        return;
1123}
1124
1125
Note: See TracBrowser for help on using the repository browser.