source: svn/trunk/zasc/app_c/DST_ChannelTune.c @ 6

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

1.phkim

  1. revision copy newcon3sk r27
File size: 34.9 KB
Line 
1#include "DST_ChannelTune.h"
2#include "DST_HostInterface.h"
3#include "DST_GlobalVariables.h"
4#include "DST_WindowEvent.h"
5#include "DST_WindowType.h"
6#include "DST_WinManagerTask.h" //ui
7#include "DST_DataStrings.h"
8#include "DST_SignalMonitor.h"
9#include "DST_UserInterface.h"
10#include "DST_FontEngine.h"
11#include "DST_CCTask.h"
12#include "DST_WinManager.h"
13#include "DST_ISDBT_ChannelTask.h"
14#include "dst_eroum_interface.h"
15#include "DST_DB_Engine.h"
16#include "DST_DB.h"
17#include "DST_EEPROM.h"
18#include "DST_GlobalVariables.h"
19#include "DST_Updates.h"
20#include "DST_MemoryDB.h"
21
22void DST_USBUpdateInit(void);
23
24void DST_DB_Del();
25char *DST_GetModelName(void);
26
27static void DST_CT_CallBack(DS_U8 Cmd, DS_U32 p1, DS_U32 p2, DS_U32 p3, DS_U32 p4, DS_U32 p5, DS_U32 p6);
28
29// °øÀå ÃʱâÈ­ ÈÄ ÀçÆÃ ÈÄ Ã¹¹øÂ°·Î ºÒ¸®´Â ÇÔ¼ö
30// EEPROM°ú ä³Î¸ÊÀ» ¸ðµÎ Áö¿î´Ù.
31void DST_Factory_Reset()
32{
33        DST_DB_Del();
34        DST_EEPROM_FactoryInit();
35}
36
37void DST_Tune(DS_U8 RF, DS_U16 program_number)
38{
39        if (program_number == 0)
40        {
41//              CDB db;
42//              db.Query("update signal set signal_strength=99 where signal_strength=100");
43//              db.Query("delete from signal where rf=%d", RF);
44//              db.Query("insert into signal values(%d, %d, %d, %d, %d)", RF, 100, 0, 0, 1);
45                //Minor = 1;
46        }
47        JST_Open(DST_CT_CallBack);
48        JST_Tune(RF, DHL_MODULATION_8VSB, program_number);
49}
50
51// ä³Î Æ©´×À» ÇÑ´Ù.
52void DST_Scan(DS_U8 RF)
53{
54        JST_Open(DST_CT_CallBack);
55        JST_Scan(RF);
56}
57       
58#if 0
59____AV_STATE___()
60#endif
61static DS_U8  g_AV_RF = 0;
62static DS_U16 g_AV_program_number = 0;
63static DS_U16 g_AV_source_id = 0;
64
65// ÄݹéÀ¸·Î ¿Ã¶ó¿Â AVÀÇ ÁøÇà »óȲ
66void DST_GetAVState(DS_U8 *RF, DS_U16 *program_number, DS_U16 *source_id)
67{
68        if (RF) *RF = g_AV_RF;
69        if (program_number) *program_number = g_AV_program_number;
70        if (source_id) *source_id = g_AV_source_id;
71}
72
73static DS_U32 g_AV_PCR = 0;
74static DS_U32 g_AV_VideoType = 0;
75static DS_U32  g_AV_AudioType = 0;
76static DS_U32 g_AV_VideoPID = 0;
77static DS_U32  g_AV_AudioPID = 0;
78
79void DST_SetAVInfo(DS_U32 PCR, DS_U32 vPid, DS_U32 aPid, DS_U32 vType, DS_U32 aType)
80{
81        g_AV_PCR = PCR;
82        g_AV_VideoPID = vPid;
83        g_AV_AudioPID = aPid;
84        g_AV_VideoType = vType;
85        g_AV_AudioType = aType;
86
87}
88
89// ÄݹéÀ¸·Î ¿Ã¶ó¿Â AVÀÇ ÁøÇà »óȲ
90void DST_GetAVInfo(DS_U32 *PCR, DS_U32 *vPid, DS_U32 *aPid, DS_U32 *vType, DS_U32 *aType)
91{
92        if (PCR) *PCR = g_AV_PCR;
93        if (vType) *vPid = g_AV_VideoPID;
94        if (aType) *aPid = g_AV_AudioPID;
95        if (vType) *vType = g_AV_VideoType;
96        if (aType) *aType = g_AV_AudioType;
97}
98
99
100static DS_U16 DST_g_VideoWidthTV = 0;
101static DS_U16 DST_g_VideoHeightTV = 0;
102static DS_U16 DST_g_RefreshRate = 0;
103static bool DST_g_bInterlace = false;
104static bool DST_g_Wide = false;
105// ºñµð¿À Å©±â¸¦ ¹ÝȯÇÑ´Ù.
106// ¸®ÅϰªÀº interlace À¯¹«´Ù.
107void DST_GetVideoSize(DS_U16* width, DS_U16* height, DS_U16* fps, bool* bInterlace, bool* bWide)
108{
109#if 1
110        *width = DST_g_VideoWidthTV;
111        *height = DST_g_VideoHeightTV;
112        *fps = DST_g_RefreshRate;
113        *bInterlace = DST_g_bInterlace;
114        *bWide = DST_g_Wide;
115#else
116//      //Å×½ºÆ®¿ë:: ºñµð¿À ¼Ò½º°¡ 4:3À϶§
117//      *width = 720;
118//      *height = 480;
119//      *fps = DST_g_RefreshRate;
120//      *bInterlace = DST_g_bInterlace;
121//      *bWide = false;
122        //Å×½ºÆ®¿ë:: ºñµð¿À ¼Ò½º°¡ 16:9À϶§
123        *width = 1920;
124        *height = 1080;
125        *fps = DST_g_RefreshRate;
126        *bInterlace = DST_g_bInterlace; 
127        *bWide = true;
128
129#endif
130}
131
132void DST_ResetVideoSize()
133{
134        DST_g_VideoWidthTV = 0;
135        DST_g_VideoHeightTV = 0;
136        DST_g_RefreshRate = 0;
137        DST_g_bInterlace = false;
138        DST_g_Wide = false;
139}
140
141#if 0
142____TIME___()
143#endif
144
145#if 0
146____STT Set___()
147#endif
148static DS_U32 g_systemTime = 0;
149static DS_U8 g_gpsUtcOffset = 0;
150static DS_U32 g_refreshSystemTime = 0;  // system time °»½Å ½Ã°£
151
152void DST_SetTimeBySTT(DS_U32 RF, DS_U32 a_systemTime, DS_U8 a_offset, DS_U32 a_time)
153{
154//      DST_Printf("RF = %d a_systemTime = %d a_offset = %d a_time=%d\n", RF, a_systemTime, a_offset, a_time);
155        g_systemTime = a_systemTime;
156        g_gpsUtcOffset = a_offset;
157        DST_g_TimeOffset[RF]=g_gpsUtcOffset;
158        g_refreshSystemTime = a_time;
159}
160
161DS_U32 DST_GetCurrentUTCTime()
162{
163        DS_U32 systemTime = 0;
164        if(g_systemTime)
165        {
166                if (g_refreshSystemTime > DST_OS_GetTickCount()) g_refreshSystemTime = DST_OS_GetTickCount();
167                systemTime = g_systemTime + (DST_OS_GetTickCount() - g_refreshSystemTime) /  DST_OS_GetTicksPerSecond();
168        }
169        return systemTime;
170}
171
172DS_U32 DST_GetCurrentLocalTime()
173{
174        if(g_systemTime == 0) return 0;
175
176        return DST_GetCurrentUTCTime() + g_gpsUtcOffset + 9 * 3600;
177}
178
179DS_U32 DST_UTCTimeToLocalTime(DS_U32 a_systemTime)
180{
181        if(a_systemTime == 0) return 0;
182
183        DS_U32 localTime = a_systemTime + g_gpsUtcOffset + 9 * 3600;
184        DST_Printf("system time -> local time : %ld -> %ld\n", a_systemTime, localTime);
185       
186        return localTime;
187}
188
189DS_U16* DST_GetTimeString(DS_U32 a_time, TIME_MODE a_mode, bool bApplyTimeZone, int a_offset)
190{
191        DS_U32 year, month, date, day;
192        DS_U32 hour, min, sec;
193        DS_U32 utc = a_time;
194        DS_U32 daytab[2][13] = {
195                {365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
196                {366, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
197        };
198        int acc_day;
199        int i, leap;
200
201        utc += a_offset;
202        /* timezone ¼³Á¤¿¡ µû¸¥ º¸Á¤À» ÇÑ´Ù. */
203        if(bApplyTimeZone)
204                utc += (9 * 3600);
205       
206        /* 1980/1/1 ºÎÅÍ UTCÀÇ ±âÁØÀÎ 1980/1/6 ±îÁöÀÇ ½Ã°£À» ´õÇÑ ´ÙÀ½
207            1980³â ºÎÅÍ °è»êÇØ ³ª°¡¸é ÆíÇÏ´Ù.
208        */
209        utc += 432000;   /* 5Àϰ£ÀÇ ÃÊ  */
210        acc_day = -5;
211        year = 1980;
212        month = 1;
213        date = 1;
214        day = 2; /* Tuesday: 1980³â 1¿ù 1ÀÏÀº È­¿äÀÏ */
215        hour = 0;
216        min = 0;
217        sec = 0;
218        leap = 1;       /* 1980³âÀº 4ÀÇ ¹è¼öÀ̹ǷΠÀ±³â.. */
219
220        /* year¸¦ °è»ê */
221        while (utc >= (daytab[leap][0]*86400))
222        {
223                utc -= daytab[leap][0]*86400;
224                acc_day += daytab[leap][0];
225                year ++;
226                leap = ((year%4==0)&&(year%100!=0))||(year%400==0);
227        }
228
229        /* month¸¦ °è»ê */
230        i=1;
231        while (utc >= (daytab[leap][i]*86400))
232        {
233                utc -= daytab[leap][i]*86400;
234                acc_day += daytab[leap][i];
235                month ++;
236                i++;
237        }
238
239        date += (utc/86400);
240        acc_day += (utc/86400);
241        utc = utc%86400;
242       
243        day = (acc_day + 7) % 7;
244       
245        hour = (utc/3600);
246        utc = utc%3600;
247       
248        min = (utc/60);         
249        sec = utc%60;
250
251        static DS_U16 timeStr[100];
252        memset(timeStr, 0, 100);
253        char tmpStr[20] = {0, };
254        switch(a_mode)
255        {
256                case DATE_NO_YEAR_TIME_NO_SEC_DETAIL:
257                {
258                        sprintf(tmpStr, "%d", (int)month);
259                        str2uni(timeStr, tmpStr);
260                        strcat16(timeStr, pz_Month[OSD_Lang]);
261                        memset(tmpStr, 0, strlen(tmpStr));
262                        sprintf(tmpStr, " %d", (int)date);
263                        str2uni(&timeStr[strlen16(timeStr)], tmpStr);
264                        strcat16(timeStr, pz_Day[OSD_Lang]);           
265                        strcat16_8(timeStr, (char*)"(");
266                        DS_U16* dayStr = DST_GetDayOfWeakString(day);
267                        strcat16(timeStr, dayStr);
268                        strcat16_8(timeStr, (char*)") ");
269                        strcat16(timeStr,  (hour>12)?pz_PM[OSD_Lang]:pz_AM[OSD_Lang]);
270                        sprintf(tmpStr, " %02d:%02d", (hour>12)?(int)hour-12:(int)hour, (int)min);
271                        strcat16_8(timeStr, tmpStr);
272                        break;
273                }       
274                case DATE_NO_YEAR_TIME_NO_SEC:
275                {
276                        sprintf(tmpStr, "%d/%d(", (int)month, (int)date);
277                        str2uni(timeStr, tmpStr);
278                        DS_U16* dayStr = DST_GetDayOfWeakString(day);
279                        strcat16(timeStr, dayStr);
280                        strcat16_8(timeStr, (char*)") ");
281                        strcat16(timeStr,  (hour>12)?pz_PM[OSD_Lang]:pz_AM[OSD_Lang]);
282                        sprintf(tmpStr, " %02d:%02d", (hour>12)?(int)hour-12:(int)hour, (int)min);
283
284                        strcat16_8(timeStr, tmpStr);
285                        break;
286                }
287                case DATE_TIME_NO_SEC:
288                {
289                        sprintf(tmpStr, "%d.%d.%d(", (int)year, (int)month, (int)date);
290                        str2uni(timeStr, tmpStr);
291                        DS_U16* dayStr = DST_GetDayOfWeakString(day);
292                        strcat16(timeStr, dayStr);
293                        strcat16_8(timeStr, (char*)") ");
294                        strcat16(timeStr,  (hour>12)?pz_PM[OSD_Lang]:pz_AM[OSD_Lang]);
295                        sprintf(tmpStr, " %02d:%02d", (hour>12)?(int)hour-12:(int)hour, (int)min);
296
297                        strcat16_8(timeStr, tmpStr);
298                        break;
299                }
300                case DATE_TIME:
301                {
302                        sprintf(tmpStr, "%d.%d.%d(", (int)year, (int)month, (int)date);
303                        str2uni(timeStr, tmpStr);
304                        DS_U16* dayStr = DST_GetDayOfWeakString(day);
305                        strcat16(timeStr, dayStr);
306                        strcat16_8(timeStr, (char*)") ");
307                        sprintf(tmpStr, "%02d:%02d:%02d", (int)hour, (int)min, (int)sec);
308                        strcat16_8(timeStr, tmpStr);
309                        break;
310                }
311                case DATE_ONLY:
312                {
313                        sprintf(tmpStr, "%d.%d.%d(", (int)year, (int)month, (int)date);
314                        str2uni(timeStr, tmpStr);
315                        DS_U16* dayStr = DST_GetDayOfWeakString(day);
316                        strcat16(timeStr, dayStr);
317                        sprintf(tmpStr, ")");
318                        strcat16_8(timeStr, tmpStr);
319                        break;
320                }
321                case TIME_NO_SEC:
322                {
323                        strcat16(timeStr,  (hour>12)?pz_PM[OSD_Lang]:pz_AM[OSD_Lang]);
324                        sprintf(tmpStr, "%02d:%02d", (hour>12)?(int)hour-12:(int)hour, (int)min);
325
326                        str2uni(&timeStr[strlen16(timeStr)], tmpStr);
327                        break;
328                }
329                case TIME_NO_SEC_NO_AMPM:
330                {
331                        sprintf(tmpStr, "%02d:%02d", (hour>12)?(int)hour-12:(int)hour, (int)min);
332                        str2uni(timeStr, tmpStr);
333                        break;
334                }
335                case TIME_ONLY:
336                {
337                        sprintf(tmpStr, "%02d:%02d:%02d", (int)hour, (int)min, (int)sec);
338                        str2uni(timeStr, tmpStr);
339                        break;
340                }
341                case AMPM:
342                {                       
343                        strcat16(timeStr,  (hour>12)?pz_PM[OSD_Lang]:pz_AM[OSD_Lang]);
344                        break;
345                }
346        }
347
348        return timeStr;
349}
350
351// ¾Æ·¡ ºê¶óÁúÇà ½Ã°£ °ü·Ã ÇÔ¼ö »èÁ¦ ¿¹Á¤
352typedef struct 
353{
354        DS_U16 date;
355        DS_U32 time;
356        int offset;
357        DS_U32 tick;
358} DATETIME;
359
360static DATETIME g_time[256];
361
362// MJD½Ã°£ º¯È¯
363//static void DST_MJD(int mjd, int *year, int *month, int *day, int *wday)
364//{
365//      int y = int((mjd-15078.2f)/365.25f);
366//      int m = int((mjd-14956.1f - int(y*365.25f)) / 30.6001f);
367//      int k = (m==14 || m==15) ? 1 : 0;
368//      *day = mjd-14956-int(y*365.25f)-int(m*30.6001f);
369//      *year = 1900 + y + k;
370//      *month = m - 1 - k * 12;
371//      *wday = ((mjd+3) % 7); // ÀÏ¿äÀÏÀÌ 0 ¿ù¿äÀÏÀÌ 1
372//}
373
374int DST_GetTimeOffset()
375{
376        DS_U8 RF = 0;
377        DST_GetAVState(&RF, 0, 0);
378        DATETIME time = g_time[0];
379        if (g_time[RF].tick) time = g_time[RF];
380        return time.offset;
381}
382
383// ÇöÀç ½Ã°£À» °¡Á®¿Â´Ù.
384//bool DST_GetTime(STime_t *t)
385//{
386//      DS_U8 RF = 0;
387//      DST_GetAVState(&RF);
388//      DATETIME time = g_time[0];
389//      if (g_time[RF].tick) time = g_time[RF];
390//      if (time.date == 0 || time.tick == 0) return false;
391//      int sec = time.time + ((DST_OS_GetTickCount() - time.tick) / DST_OS_GetTicksPerSecond()) + time.offset * (60*60);
392//      int day = time.date;
393//      if (sec >= (24*60*60)) // ƽ Ä«¿îÆ®·Î º¸Á¤ÇÑ °ªÀÌ ¹ã 12½Ã¸¦ ³Ñ¾î°¡¸é º¸Á¤ÇÑ´Ù.
394//      {
395//               sec = sec - (24*60*60);
396//               day = day + 1;
397//      }
398//      if (sec < 0) // OFFSETÀ» Àû¿ëÇϸé À½¼ö°¡ µÉ ¼ö ÀÖ´Ù
399//      {
400//               sec = sec + (24*60*60);
401//               day = day - 1;
402//      }
403//      DST_MJD(day, &t->year, &t->month, &t->day, &t->wday);
404//      t->hour = sec / 3600;
405//      t->min  = (sec % 3600) / 60;
406//      t->sec  = sec % 60;
407//      return true;
408//}
409
410#if 0
411____NEW_EPG_DB___()
412#endif
413
414#if EPG_SUPPORT
415
416static void q_sort(DS_U32 numbers[], DS_U32 left, DS_U32 right)
417{
418        static int count = 0;
419        DS_U32 pivot, l_hold, r_hold;
420        count++;
421        if(count > 1000) DST_Printf("\n\n\n\n\n\n ERROR : sort count exceed 1000 count %d \n\n\n\n\n\n",count);
422       
423        l_hold = left;
424        r_hold = right;
425        pivot = numbers[left];
426        while (left < right)
427        {
428                while ((numbers[right] >= pivot) && (left < right))
429                  right--;
430                if (left != right)
431                {
432                  numbers[left] = numbers[right];
433                  left++;
434                }
435                while ((numbers[left] <= pivot) && (left < right))
436                  left++;
437                if (left != right)
438                {
439                  numbers[right] = numbers[left];
440                  right--;
441                }
442        }
443        numbers[left] = pivot;
444        pivot = left;
445        left = l_hold;
446        right = r_hold;
447        if (left < pivot)
448        q_sort(numbers, left, pivot-1);
449        if (right > pivot)
450        q_sort(numbers, pivot+1, right);
451
452        count--;
453}
454
455EPG_DB* DST_EPG_DB_Get(int *nCount, int limit)
456{
457//      *nCount = 0;
458//      return 0;
459#if 1
460        //DST_Printf("DST_EPG_DB_Get\n");
461        // ÇöÀç ¹æ¼ÛÁßÀÎ Á¤º¸¸¦ °¡Á®¿Â´Ù
462        int count = 0;
463        DS_U8  RF = 0;
464        DS_U16 sID = 0;
465        DST_GetAVState(&RF, 0, &sID);
466        EPG_DB *tmp_epg_db =0;
467        EPG_DB *epg_db =0;
468        if (sID == 0) 
469        {
470                *nCount = count;
471                return 0;       
472        }
473
474        DS_U32 curTime = DST_GetCurrentUTCTime();
475        int max_start_time = -1;
476        int tmp_count = 0;
477        int i = 0;
478        DS_U32 eventTime = 0;
479        bool bFound = false;
480//      T();
481        DS_U32 tmp_startTime = 0;
482        DS_U32 tmp_endTime = tmp_startTime + 0;
483
484        DS_U32 *_startTime_list = (DS_U32*)DST_OS_Calloc(DB_EIT_SUB_MAX, sizeof(DS_U32));
485       
486        for (i = 0; i < DB_EIT_SUB_MAX; i++)
487        {
488                if(db_eit_sub[i].rf != RF) continue;
489                if(db_eit_sub[i].source_id != sID) continue;
490                if(db_eit_sub[i].start_time <= curTime)
491                {
492                        _startTime_list[tmp_count] = db_eit_sub[i].start_time;
493                        tmp_count ++;
494                }
495               
496        }
497        if(tmp_count < 1)
498        {
499                *nCount = count;
500                return 0;
501        }       
502
503        q_sort(_startTime_list, 0 ,tmp_count-1);
504
505        for (i = 0; i < DB_EIT_SUB_MAX; i++)
506        {
507                if(db_eit_sub[i].start_time == _startTime_list[tmp_count-1])
508                {
509                        eventTime = db_eit_sub[i].start_time;
510                        tmp_startTime = db_eit_sub[i].start_time;
511                        tmp_endTime = tmp_startTime + db_eit_sub[i].length_in_seconds;
512                        if(tmp_startTime <= curTime && curTime < tmp_endTime)
513                        {
514                                bFound = true;
515                                break;
516                        }                       
517                }
518        }
519
520        DST_OS_Free(&_startTime_list);
521       
522        int nRealCount = 0;
523        int pos_list[DB_EIT_SUB_MAX];
524        memset(pos_list, 0 , sizeof(pos_list));
525        if(bFound)
526        {
527                for (i = 0; i < DB_EIT_SUB_MAX; i++)
528                {
529                        if(db_eit_sub[i].rf != RF) continue;
530                        if(db_eit_sub[i].source_id != sID) continue;
531                        if(db_eit_sub[i].start_time >= eventTime)
532                        {
533                                pos_list[nRealCount] = i; 
534                                nRealCount ++;
535                        }
536                       
537                }
538               
539                if(nRealCount < 1)
540                {
541                        *nCount = count;
542                        return 0;
543                }
544                tmp_epg_db = (EPG_DB*)DST_OS_Calloc(nRealCount, sizeof(EPG_DB));
545                int j=0;
546               
547                DS_U32 *tmp_startTime_list = (DS_U32*)DST_OS_Calloc(nRealCount, sizeof(DS_U32));
548                int x = 0;
549                bool bSkip = false;
550                count = 0;
551                for( i=0 ; i<nRealCount ; i++)
552                {
553                         bSkip = false;
554                        for(x = 0; x < nRealCount; x++)
555                        {
556                                if(tmp_epg_db[x].event_id == db_eit_sub[pos_list[i]].event_id && tmp_epg_db[x].source_id== db_eit_sub[pos_list[i]].source_id && tmp_epg_db[x].start_time== db_eit_sub[pos_list[i]].start_time) 
557                                {
558                                        bSkip = true;
559                                        break;
560                                }
561                        }
562                        if(!bSkip)
563                        {
564                                tmp_epg_db[count].source_id = db_eit_sub[pos_list[i]].source_id;
565                                tmp_epg_db[count].event_id =  db_eit_sub[pos_list[i]].event_id;
566                                strcpy((char*)tmp_epg_db[count].title,  db_eit_sub[pos_list[i]].title); 
567                                tmp_epg_db[count].start_time=  db_eit_sub[pos_list[i]].start_time;
568                                tmp_epg_db[count].duration=  db_eit_sub[pos_list[i]].length_in_seconds;
569                                for( j = 0; j< DB_ETT_MAX; j++)
570                                {
571                                        if(db_ett[j].crc == 0) continue;
572                                        if(db_ett[j].rf != RF) continue;
573                                        if(db_ett[j].source_id != tmp_epg_db[count].source_id) continue;
574                                        if(db_ett[j].event_id != tmp_epg_db[count].event_id) continue;
575                                        tmp_epg_db[count].text = (char*)DST_OS_Calloc(strlen(db_ett[j].title)+1, 1);
576                                        strcpy(tmp_epg_db[count].text, db_ett[j].title);
577                                        break;
578                                }
579                                if(tmp_epg_db[count].text == 0)
580                                {
581                                        tmp_epg_db[count].text = (char*)DST_OS_Calloc(1, 1);
582                                }
583                                tmp_startTime_list[count] = tmp_epg_db[count].start_time;
584                                count++;
585                        }
586                }
587//              DST_Printf("nRealCount=%d,count=%d\n",nRealCount,count);
588//              for(i = 0; i < count; i++)
589//              {
590//                      DST_Printf("source_id=%d , event_id=%d , title=%s , text=%s\n",tmp_epg_db[i].source_id,tmp_epg_db[i].event_id,tmp_epg_db[i].title,tmp_epg_db[i].text);
591//              }
592
593                DS_U32 *startTime_list = (DS_U32*)DST_OS_Calloc(count, sizeof(DS_U32));
594                memcpy(startTime_list, tmp_startTime_list, count*sizeof(DS_U32));       
595
596                epg_db = (EPG_DB*)DST_OS_Calloc(count, sizeof(EPG_DB));
597                if(count > 0) q_sort(startTime_list, 0, count-1);
598
599                i=0;
600                for(i=0;i<count;i++)
601                {
602                        j=0;
603                        for(j=0;j<count;j++)
604                        {                       
605                                if(tmp_epg_db[j].start_time == startTime_list[i])
606                                {
607                                        epg_db[i].source_id = tmp_epg_db[j].source_id;
608                                        epg_db[i].event_id = tmp_epg_db[j].event_id;
609                                        strcpy(epg_db[i].title, tmp_epg_db[j].title);
610                                        epg_db[i].start_time = tmp_epg_db[j].start_time;
611                                        epg_db[i].duration = tmp_epg_db[j].duration;
612                                        epg_db[i].text = (char*)DST_OS_Calloc(strlen(tmp_epg_db[j].text)+1, 1);
613                                        strcpy(epg_db[i].text, tmp_epg_db[j].text);
614                                        break;
615                                }
616                        }
617
618                }
619//              for(i = 0; i < count; i++)
620//              {
621//                      DST_Printf("source_id=%d , event_id=%d , title=%s , text=%s\n",epg_db[i].source_id,epg_db[i].event_id,epg_db[i].title,epg_db[i].text);
622//              }               
623                DST_EPG_DB_Free(tmp_epg_db, nRealCount);
624                DST_OS_Free(&tmp_startTime_list);
625                DST_OS_Free(&startTime_list);
626                nRealCount = count;
627
628
629        }
630        *nCount = nRealCount;
631        return epg_db;
632#endif
633}
634// EPG µðºñ ¸Þ¸ð¸®¸¦ ÇØÁ¦ÇÑ´Ù.
635void DST_EPG_DB_Free(EPG_DB* db, int nCount)
636{
637        int i;
638        for ( i = 0; i < nCount; i++)
639        {
640                if (db[i].text) DST_OS_Free(&db[i].text);       
641        }
642        DST_OS_Free(&db);
643}
644
645#endif
646
647#if 0
648____SIGNAL MONITOR___()
649#endif
650
651
652static DHL_AUDIO_MODE g_AudioMode = DHL_AUDIO_UNKNOWN;
653DHL_AUDIO_MODE DST_AUD_GetMode()
654{
655        return g_AudioMode;
656}
657
658int DST_GetSignalStrength()
659{
660        DS_U8 RF = 0;
661        DST_GetAVState(&RF, 0, 0);
662        return DST_g_SignalStrength;
663}
664
665
666static bool g_Lock  = true;
667static bool g_Video = true;
668static bool g_Audio = true;
669
670bool  DST_IsAVBlock()
671{
672        return (g_Lock == false || g_Video == false);
673}
674
675bool DST_IsWeakSignal()
676{
677        return (g_Lock == false);
678}
679
680bool DST_IsAudioOnly()
681{
682        return (g_Lock == true && g_Video == false && g_Audio == true);
683}
684
685bool DST_IsNoProgram()
686{
687        return (g_Lock == true && g_Video == false && g_Audio == false);
688}
689
690static int TUNE_SCAN_STATE = CT_STOPPED;
691bool DST_IsTuneMode()
692{
693        return (TUNE_SCAN_STATE == CT_TUNE_START);
694}
695
696
697
698
699typedef struct 
700{
701        DS_U16 date; // day
702        DS_U32 time; // sec
703} DATETIME2;
704bool DST_IsBetweenTime(DATETIME2 compare_time, DATETIME2 conrol_start_time, DATETIME2 conrol_end_time)
705{       
706        if(compare_time.date == 0 || conrol_start_time.date == 0 || conrol_end_time.date == 0) return false;
707        if(conrol_start_time.time <= compare_time.time && compare_time.time < conrol_end_time.time)
708        {
709                return true;
710        }
711        else
712        {
713                if(conrol_start_time.date<= compare_time.date && compare_time.date < conrol_end_time.date) return true;
714                else return false;
715        }
716       
717}
718
719
720
721#if 0
722____RF_UPDATE___()
723#endif
724
725
726static char* g_RFUpdate_data = 0;
727static int g_RFUpdate_data_length = 0;
728
729//static int DST_CheckCRC(DS_U32 crc_value, char * data, int size)
730//{
731//      DS_U32 ulCrc = DST_crc32((DS_U8 *)data, size);
732//
733//      if (ulCrc != crc_value)
734//      {
735//              DST_Printf("Total image crc error!!!\n");
736//              DST_Printf("\nFilelength:0x%08x \n", size);
737//              DST_Printf("Computed crc32:0x%08x, correct crc32:0x%08x\n",(int)ulCrc, (int)crc_value);
738//              return CRC_READ_TOTAL_FAIL;
739//      }
740//
741//      DST_Printf("Total image crc ok...\n");
742//      return UPDATE_OK;       
743//}
744
745int write2Flash(int partition, int partition_size, int data_size, int offset, char * data, int category)
746{
747#ifdef DSTAR
748                char filename[64];
749                sprintf(filename, "%s", category == UPDATE_BOOTLOADER ? "bootloader.bin" : "image.bin" );
750                FILE *fp = fopen(filename,"wb+");
751#endif
752
753        int ret = 0;
754        int old_pos = -1;
755
756        SWinEventMsg event;
757        event.cmd = WM_RF_UPDATE;
758        int i;
759        for ( i = 0; i < data_size; i+=4096)
760        {
761                int pos = i * 100 / data_size;
762                int length = (i+4096 > data_size) ? data_size-i : 4096;
763#ifdef DSTAR
764                fwrite(&g_RFUpdate_data[i+offset], 1, length, fp);
765#endif
766                if (i < partition_size)
767                {
768                        /*ret = */DHL_Flash_Write(partition+i, length, (DS_U8*)&data[offset+i]);
769                }
770
771                if (ret != 0) return ret;
772                       
773                if (pos != old_pos)
774                {
775                        old_pos = pos;
776                        event.data32[0] = FLASH_WRITING;
777                        event.data32[1] = pos;
778                        event.data32[2] = category;
779                        static DS_U32 tick = 0;
780                        // 0.5Ãʸ¶´Ù ±×¸®µµ·Ï
781                        if (tick > DST_OS_GetTickCount()) tick = DST_OS_GetTickCount();
782                        if((DST_OS_GetTickCount() - tick >= DST_OS_GetTicksPerSecond()/2) || (pos >= 100)) 
783                        {
784                                tick = DST_OS_GetTickCount();
785                                DST_SendWindowEvent(event);
786                        }
787                        DST_OS_Delay(10);
788                }
789        }
790#ifdef DSTAR
791        fclose(fp);
792#endif 
793        return ret;
794
795}
796
797void sendFinishMessage(int status1, int status2, int category)
798{
799        SWinEventMsg event;
800        event.cmd = WM_RF_UPDATE;
801        event.data32[0] = status1;
802        event.data32[1] = status2;
803        event.data32[2] = category;
804        DST_SendWindowEvent(event);
805       
806        g_RFUpdate_data_length = 0;
807        DST_CT_CallBack(CT_FLASH_WRITE_COMPLETE,0,0,0,0,0,0);   
808}
809
810static void tFlashWrite()
811{
812        int err_ret = 0;
813        int buf_offset = 0;
814        DS_U8 nBank = DHL_INFO_GetBankNumber();
815       
816        // 4KB¾¿ ¾´´Ù.
817       
818        //Çì´õ º¹»ç
819        BootHeader bootHeader;
820//      ApplicationHeader applicationHeader;
821        memset(&bootHeader, 0 , sizeof(BootHeader));
822        memcpy(&bootHeader, &g_RFUpdate_data[FLASH_BOOT_SIZE], sizeof(BootHeader));
823        DST_Printf("\n");
824        DST_Printf("bootHeader.boot_order : %ld (0x%16X)\n",bootHeader.boot_order,bootHeader.boot_order);
825        DST_Printf("bootHeader.chip_id    : %ld (0x%16X)\n",bootHeader.chip_id,bootHeader.chip_id);
826        DST_Printf("bootHeader.dummy[1]   : %ld (0x%16X)\n",bootHeader.dummy[0],bootHeader.dummy[0]);
827        DST_Printf("bootHeader.dummy[1]   : %ld (0x%16X)\n",bootHeader.dummy[1],bootHeader.dummy[1]);
828
829        //Bootloader Flash Writing
830        err_ret = write2Flash(FLASH_BOOT_POS, FLASH_BOOT_SIZE, FLASH_BOOT_SIZE, buf_offset, g_RFUpdate_data, UPDATE_BOOTLOADER);
831        if(err_ret != 0)
832        {
833                sendFinishMessage(FLASH_WRITING_FAIL, WRITE_FLASH_FAIL, UPDATE_BOOTLOADER);
834                DST_OS_SelfDeleteTask();
835                return;         
836        }
837        buf_offset = FLASH_BOOT_SIZE;
838        //Application Flash Writing
839        DS_U32 nFlashBaseAddress = nBank ? FLASH_BANK0_POS : FLASH_BANK1_POS;
840        err_ret = write2Flash(nFlashBaseAddress, FLASH_BANK_SIZE, g_RFUpdate_data_length - FLASH_BOOT_SIZE, buf_offset, g_RFUpdate_data, nBank ? UPDATE_APPLICATION0 : UPDATE_APPLICATION1);
841        if(err_ret != 0)
842        {
843                sendFinishMessage(FLASH_WRITING_FAIL, WRITE_FLASH_FAIL, nBank ? UPDATE_APPLICATION0 : UPDATE_APPLICATION1);
844                DST_OS_SelfDeleteTask();
845                return;
846        }       
847
848        DHL_INFO_SetBankNumber(nBank ? 0 : 1);
849       
850        nBank = DHL_INFO_GetBankNumber();
851
852        nFlashBaseAddress = nBank ? FLASH_BANK0_POS : FLASH_BANK1_POS;
853        err_ret = write2Flash(nFlashBaseAddress, FLASH_BANK_SIZE, g_RFUpdate_data_length - FLASH_BOOT_SIZE, buf_offset, g_RFUpdate_data, nBank ? UPDATE_APPLICATION0 : UPDATE_APPLICATION1);
854        if(err_ret != 0)
855        {
856                sendFinishMessage(FLASH_WRITING_FAIL, WRITE_FLASH_FAIL, nBank ? UPDATE_APPLICATION0 : UPDATE_APPLICATION1);
857                DST_OS_SelfDeleteTask();
858                return;
859        }       
860
861        DHL_INFO_SetBankNumber(nBank ? 0 : 1);
862
863
864        DST_OS_Delay(10);
865        sendFinishMessage(FLASH_WRITING_COMPLETE,UPDATE_FINISHED,UPDATE_OK);
866        DST_OS_SelfDeleteTask();
867}
868
869static void DST_RFUpdateWrite(char* p, int length)
870{
871        if (g_RFUpdate_data) return;
872        g_RFUpdate_data = p;
873        g_RFUpdate_data_length = length;
874        /*int taskID = */DST_OS_SpawnTask((void (*)(void *)) tFlashWrite, (char*)"tFlashWrite", APP_TASK_PRIO_RF_UPDATE_WRITE, WIN_MGR_TASK_STACKSIZE, 0);
875}
876
877static int DST_g_PowerLevel = 0;
878static int DST_g_SNR = 0;
879static bool DST_g_vAlive = false;
880static bool DST_g_aAlive = false;
881
882void DST_GetAVAliveInfo(bool *vAlive, bool *aAlive)
883{
884        if(vAlive) *vAlive = DST_g_vAlive;
885        if(aAlive) *aAlive = DST_g_aAlive;
886}
887
888void DST_GetSignalInfo(bool *bLock, int *ss, int *powerlevel, int *snr)
889{
890        DS_U8 RF = 0;
891        DST_GetAVState(&RF, 0, 0);
892        if (bLock) *bLock = DST_g_Lock;
893        if (ss) *ss = DST_g_SignalStrength;
894        if (powerlevel) *powerlevel = DST_g_PowerLevel;
895        if (snr) *snr = DST_g_SNR;
896}
897
898// class CMEMFILEÀ» C·Î º¯È¯
899static int m_nTotalSize = 0; // ¸Þ¸ð¸® ÆÄÀÏÀÇ Àüü ±æÀÌ
900static int m_nBlockSize = 0; // ºí·°´ç »çÀÌÁî
901static char* m_data = 0; // ÀúÀåµÇ´Â µ¥ÀÌÅÍ
902static int m_nMaxBlockNumber = 0; // °¡ÀåÅ« ºí·° ¹øÈ£. ºí·°¹øÈ£´Â 0¿¡¼­ ½ÃÀÛÇÑ´Ù.
903static int* m_BlockList =  0; // ÀÌ¹Ì ¹ÞÀº µ¥ÀÌÅÍ´Â ¹ö¸®±âÀ§ÇØ ÀÌÀü¿¡ ¹ÞÀº ºí·° ¹øÈ£¸¦ ÀúÀåÇÑ´Ù.
904static int m_nReceiveCount = 0; // ¹ÞÀº À¯È¿ÇÑ µ¥ÀÌÅÍ °¹¼ö
905
906static void CMemFile_New(int nTotalSize, int nBlockSize)
907{
908        m_nTotalSize = nTotalSize;
909        m_nBlockSize = nBlockSize;
910        DST_OS_Free(&m_data);
911        m_data = (char*)DST_OS_Malloc(nTotalSize);
912        m_nMaxBlockNumber = (nTotalSize-1) / nBlockSize;
913        DST_OS_Free(&m_BlockList);
914        m_BlockList = (int*)DST_OS_Malloc((m_nMaxBlockNumber+1)* sizeof(int));
915        m_nReceiveCount = 0;
916}
917
918static void CMemFile_SetBlock(int nBlockNumber, char* data, int nDataLength)
919{
920        int i;
921        for ( i = 0; i < m_nReceiveCount; i++) if (m_BlockList[i] == nBlockNumber) return; // ÀÌ¹Ì ¹ÞÀº µ¥ÀÌÅÍ¸é ¹«½Ã
922        if (nBlockNumber > m_nMaxBlockNumber) return; // À߸øµÈ ºí·°¹øÈ£´Â ¹ö¸°´Ù.
923        //printf("m_nBlockSize = %d nBlockNumber = %d\n", m_nBlockSize , nBlockNumber);
924        memcpy(m_data + m_nBlockSize * nBlockNumber, data, nDataLength);
925        m_BlockList[m_nReceiveCount++] = nBlockNumber; // ºí·° ¹øÈ£¸¦ ÀúÀåÇÏ¿© ´Ù½Ã ¹Þ´Â°ÍÀ» ¹æÁö
926}
927
928static void CMemFile_GetProgress(int *RecieveCount, int *TotalCount)
929{
930        *RecieveCount = m_nReceiveCount; 
931        *TotalCount = m_nMaxBlockNumber+1;
932}
933
934static char* CMemFile_GetData()
935{
936        return m_data;
937}
938
939static int CMemFile_GetLength()
940{
941        return m_nTotalSize;
942}
943
944static void CMemFile_Delete()
945{
946        DST_OS_Free(&m_data);
947        DST_OS_Free(&m_BlockList);
948}
949       
950static void DST_CT_CallBack(DS_U8 Cmd, DS_U32 p1, DS_U32 p2, DS_U32 p3, DS_U32 p4, DS_U32 p5, DS_U32 p6)
951{
952        if (Cmd == CT_RECEIVE_STT)
953        {
954                //DST_SendWindowEventWithOnlyCmd(WM_RECEIVE_STT);
955                DST_SetTimeBySTT(p1, p2, p3, p4);
956        }
957       
958        switch (Cmd)
959        {
960                case CT_SIGNAL:
961                case CT_SIGNAL_INFO:
962                case CT_AV_START:
963                case CT_OTC_RECEIVE_DDB:
964                case CT_RECEIVE_RF_UPDATE:
965                case CT_VIDEO_RESOLUTION:
966                case CT_AV_INFO:
967                case CT_AUDIO_MODE:
968                        break;
969                       
970                default:
971                        {
972                                SWinEventMsg event;
973                                event.cmd = WM_CT_MSG;
974                                event.data32[0] = Cmd;
975                                event.data32[1] = p1;
976                                event.data32[2] = p2;
977                                event.data32[3] = p3;
978                                event.data32[4] = p4;   
979                                event.data32[5] = p5;           
980                                event.data32[6] = p6;
981                                DST_SendWindowEvent(event);
982                        }
983                        break;
984        }
985//      if (Cmd != CT_SIGNAL_INFO  && Cmd != CT_AV_START)
986//      {
987////            DST_Printf(">>Cmd = %s(%d) %x %x %x %x\n", CT_GetMsgName(Cmd), Cmd, (int)p1, (int)p2, (int)p3, (int)p4);
988//      }
989        static DS_U32 DST_g_WeakSignalStartTime = 0;
990        SWinEventMsg event;
991        memset(&event, 0, sizeof(SWinEventMsg));
992
993        switch (Cmd)
994        {
995                case CT_RECEIVE_TVCT:
996                        if (p2 == 0) break;
997                        {
998                                // VCT¿¡¼­ Æ©´×ÇÏ·Á´ø ä³ÎÀÌ »ç¶óÁø °æ¿ì
999                                //      µ¿ÀÏ RFÀÇ Ã¹¹øÂ° ä³Î·Î Æ©´×
1000                                DBLock(true);
1001                                DS_U16 program_number = 0;
1002                                int i;
1003                                for (i = 0; i < DB_CHANNEL_DB_MAX; i++)
1004                                {
1005                                        if (db_channel_db[i].rf != p1) continue;
1006                                        program_number = db_channel_db[i].program_number;
1007                                        break;
1008                                }
1009                                DBLock(false);
1010                                if (program_number > 0) DST_UI_ChannelTune(p1, program_number);
1011                        }                       
1012                        break;
1013                       
1014                case CT_AV_INFO:
1015                        //(CT_AV_INFO, RF, PcrPid, VideoPid, AudioPid, VideoType, AudioType)
1016                        if(g_AV_PCR != p2 || g_AV_VideoPID != p3 || g_AV_AudioPID != p4 || g_AV_VideoType != p5 || g_AV_AudioType != p6)
1017                        {
1018                                DST_SetAVInfo(p2, p3, p4, p5, p6);
1019                                DST_SendWindowEventWithOnlyCmd(WM_SOURCE_FORMAT_DONE);                 
1020                        }
1021                        break;
1022
1023                case CT_SIGNAL_INFO:
1024                        //(CT_SIGNAL_INFO, RF, bLock, ss, ber_a, ber_b)
1025                        DST_g_Lock = p2;
1026                        DST_g_SignalStrength = p3;
1027                        DST_g_PowerLevel = p4;
1028                        DST_g_SNR = p5;
1029                        {
1030                                bool bWeakSignal = false;
1031                                if (DST_g_Lock == false) bWeakSignal = true;
1032//                              if (DST_g_Demod != (DHL_MODULATION_MODE)DHL_MODULATION_8VSB && DST_g_SNR <= 25) bWeakSignal = true;
1033//                              if (DST_g_Demod == (DHL_MODULATION_MODE)DHL_MODULATION_8VSB && DST_g_SNR <= 14) bWeakSignal = true;
1034                                static int old_rf = 0;
1035                                if (old_rf != (int)p1) // Á֯ļö°¡ º¯°æµÈ °æ¿ì´Â Weak Signal ¾Æ´Ñ °É·Î °£ÁÖ
1036                                {
1037                                        bWeakSignal = false;
1038                                        old_rf = p1;
1039                                }
1040                                if (bWeakSignal == true)
1041                                {
1042                                        if (DST_g_WeakSignalStartTime == 0) DST_g_WeakSignalStartTime = DST_OS_GetTickCount();
1043                                }       
1044                                else
1045                                {
1046                                        DST_g_WeakSignalStartTime = 0;
1047                                }
1048                                if (DST_g_WeakSignalStartTime > DST_OS_GetTickCount()) DST_g_WeakSignalStartTime = DST_OS_GetTickCount();
1049                               
1050                                if (DST_g_WeakSignalMuteOff == false)
1051                                {
1052                                        CT_WeakSignalMute(bWeakSignal && (DST_OS_GetTickCount() - DST_g_WeakSignalStartTime) > 10*DST_OS_GetTicksPerSecond()); // 10ÃÊÀÌ»ó Weak Signal
1053                                }
1054                        }
1055
1056                        break;
1057
1058                case  CT_SIGNAL:
1059                        {
1060                                //(CT_SIGNAL, RF, sourceID, VideoPid ? DHL_VID_Alive() : 0, AudioPid ? DHL_AUD_Alive() : 0)
1061                                static DS_U32 nLastSendCommand = 0;
1062                                static DS_U32 nLastSendTime = 0;
1063                                static DS_U32 LastSourceID = 0;
1064                                static DS_U32 LastRF = 0;
1065                                static DS_U32 old_nCmd = SM_GOOD_SIGNAL;
1066                                DS_U32 nCmd = SM_GOOD_SIGNAL;
1067                                if (DST_g_LastTuneTryTime > DST_OS_GetTickCount()) DST_g_LastTuneTryTime = DST_OS_GetTickCount();
1068                                if ((nCmd == SM_GOOD_SIGNAL)&& DST_OS_GetTickCount() - DST_g_LastTuneTryTime < WAITING_PERIOD && DST_g_VideoHeightTV == 0)
1069                                {
1070                                        nCmd = SM_WAIT_SIGNAL;
1071                                }                               
1072                                if (DST_g_WeakSignalStartTime > DST_OS_GetTickCount()) DST_g_WeakSignalStartTime = DST_OS_GetTickCount();
1073                                if (nCmd == SM_GOOD_SIGNAL && DST_g_WeakSignalStartTime > 0 && DST_OS_GetTickCount() - DST_g_WeakSignalStartTime > DST_OS_GetTicksPerSecond())
1074                                {
1075                                        nCmd = SM_WEAK_SIGNAL;
1076                                }
1077                                if ((nCmd == SM_GOOD_SIGNAL )&& p3 == 0 && p4 == 0 && DST_OS_GetTickCount() - DST_g_LastTuneTryTime > WAITING_PERIOD)
1078                                {
1079                                        nCmd = SM_NO_PROGRAM;
1080                                }
1081                                if ((nCmd == SM_GOOD_SIGNAL || nCmd == SM_WAIT_SIGNAL )&& p3 == 0 && p4 != 0)
1082                                {
1083                                        nCmd = SM_AUDIO_ONLY;
1084                                }
1085                                if (nLastSendTime >  DST_OS_GetTickCount()) nLastSendTime = DST_OS_GetTickCount();
1086//                              DST_Printf("LastSourceID:%d, p2:%d++++++++++++++++++++++++++++++++++++++\n",LastSourceID,p2);
1087                                if(LastRF != p1 || LastSourceID != p2 ) 
1088                                {
1089                                        nCmd = SM_WAIT_SIGNAL;
1090                                        LastRF = p1;
1091                                        LastSourceID = p2;
1092                                }
1093
1094                                if (DST_g_BadSignalStartTime > DST_OS_GetTickCount())   DST_g_BadSignalStartTime = DST_OS_GetTickCount();
1095                                if(nCmd == SM_WEAK_SIGNAL || nCmd == SM_NO_PROGRAM)
1096                                {
1097                                        if(old_nCmd != nCmd)
1098                                        {
1099                                                if(!(old_nCmd == SM_WEAK_SIGNAL || old_nCmd == SM_NO_PROGRAM))
1100                                                {
1101                                                        DST_g_BadSignalStartTime = DST_OS_GetTickCount();
1102                                                }
1103                                                old_nCmd = nCmd;
1104                                        }
1105                                }
1106                                else
1107                                {
1108                                        DST_g_BadSignalStartTime = 0;
1109                                }
1110                               
1111                                if (nCmd != nLastSendCommand || DST_OS_GetTickCount() - nLastSendTime > DST_OS_GetTicksPerSecond())
1112                                {
1113                                        DST_g_vAlive = p3?true:false;
1114                                        DST_g_aAlive = p4?true:false;
1115                                        DST_g_SignalState = nCmd;
1116                                        event.cmd = WM_SIGNAL_STATE;
1117                                        event.data32[0] = nCmd;
1118                                        DST_SendWindowEvent(event);     
1119                                        nLastSendCommand = nCmd;
1120                                        nLastSendTime = DST_OS_GetTickCount();
1121                                       
1122//                                      DST_g_OldSignalState = event.data32[0];
1123//                                      if (DST_g_SignalState == SM_NO_PROGRAM) DST_g_OldSignalState = SM_NO_PROGRAM;
1124//                                      DST_Printf("\n\n\nDST_g_SignalState:%d\n\n\n",DST_g_SignalState);
1125                                }
1126                        }               
1127                        break;
1128                case CT_AUDIO_MODE:
1129                        g_AudioMode = (DHL_AUDIO_MODE)p1;
1130                        break;
1131                case CT_VIDEO_RESOLUTION:
1132                        DST_g_VideoWidthTV = (p1 < 2000 && p1 < 2000) ? p1 : 0;
1133                        DST_g_VideoHeightTV = (p2 < 2000 && p2 < 2000) ? p2 : 0;
1134                        DST_g_RefreshRate = p3;
1135                        DST_g_bInterlace = p4 ? true: false;
1136                        switch (p5)
1137                        {
1138                                case DHL_SOURCE_16x9:
1139                                        DST_g_Wide = true;
1140                                        break;
1141                                case DHL_SOURCE_4x3:
1142                                        DST_g_Wide = false;
1143                                        break;
1144                                default:
1145                                        if (DST_g_VideoWidthTV == 0 || DST_g_VideoHeightTV == 0) break;
1146                                        DST_g_Wide = (((DST_g_VideoWidthTV * 100) / DST_g_VideoHeightTV) > 155) ? true : false;
1147                                        break;
1148                        }
1149                        // DST_SendWindowEventWithOnlyCmd(WM_SOURCE_FORMAT_DONE);
1150                        DST_AspectRatio();
1151                        break;
1152
1153                case CT_SCAN_LOCK_WAIT:
1154                        DST_g_SignalStrength = p3;
1155                        break;
1156                       
1157                case CT_AV_START:
1158                        if(g_AV_RF != p1 || g_AV_program_number != p2)
1159                        {
1160                                static bool bFirst = true;
1161                                DST_CloseWin(WIN_ERROR);
1162                                if(DST_g_bFactoryMode == false)
1163                                {
1164                                        if (DST_GetWin(WIN_CHANNEL_BANNER) == false)
1165                                                DST_CreateWin(WIN_CHANNEL_BANNER, WIN_NULL, BANNER_NORMAL, 3, 0, 0);
1166                                }
1167                               
1168                                bFirst = false;
1169                        }
1170                        break;
1171                case CT_SCAN_START:
1172                        TUNE_SCAN_STATE = CT_SCAN_START;
1173                        break;
1174                case CT_TUNE_START:
1175                        TUNE_SCAN_STATE = CT_TUNE_START;
1176                        if(g_AV_RF != p1 || g_AV_program_number != p2)
1177                        {
1178                                DST_ResetVideoSize();
1179                                DST_CloseWin(WIN_ERROR);
1180                        }
1181                        g_AV_RF = p1;
1182                        g_AV_program_number = p2;
1183                        g_AV_source_id = p3;
1184                        //DST_Printf("\n\n\n");
1185                        //DST_Printf("CT_TUNE_START %d %d %d\n", g_AV_RF, g_AV_program_number, g_AV_source_id);
1186                        //DST_Printf("\n\n\n");
1187                        break;
1188                case CT_STOPPED:
1189                        TUNE_SCAN_STATE = CT_STOPPED;
1190                        g_AV_RF = 0;
1191                        g_AV_program_number = 0;
1192                        g_AV_source_id = 0;
1193                        break;
1194                case CT_CVT_START:
1195                        g_AV_RF = 0;
1196                        g_AV_program_number = 0;
1197                        g_AV_source_id = 0;
1198                        break;
1199                       
1200                case CT_OTC_START:
1201                        DST_g_bOTCMode = true;
1202//                      DST_SendWindowProgressEvent(WM_OTC_DOWNLOADING_STARTED);
1203                        break;
1204        }
1205       
1206        // OTC °ü·Ã ó¸®
1207//      static CMemFile *mf = 0;
1208        static bool bOTC_Receive_Complete = true;
1209        switch (Cmd)
1210        {
1211                case CT_OTC_RECEIVE_DII:
1212//                      if (mf) delete mf;
1213//                      mf = new CMemFile(p1, p2);
1214                        CMemFile_New(p1, p2);
1215                        bOTC_Receive_Complete = false; 
1216                        break;
1217                       
1218                case CT_OTC_RECEIVE_DDB:
1219                        {
1220                                if (bOTC_Receive_Complete) break;
1221                                CMemFile_SetBlock(p1, (char*)p2, p3);
1222                                int nReceiveCount = 0;
1223                                int nMaxBlockNumber = 0;
1224                                CMemFile_GetProgress(&nReceiveCount, &nMaxBlockNumber);
1225                                SWinEventMsg event;
1226                                memset(&event, 0, sizeof(SWinEventMsg));
1227                                event.cmd = WM_RF_UPDATE;
1228                                if (nReceiveCount == nMaxBlockNumber)
1229                                {
1230                                        bOTC_Receive_Complete = true;
1231                                        event.data32[0] = DATA_RECEIVING_COMPLETE; // ¼ö½Å ¿Ï·á
1232                                        DST_SendWindowEvent(event);
1233                                        JST_Stop();
1234                                        DST_CreateWin(WIN_UPDATE, WIN_NULL, 0,0,0,0);
1235                                        DST_RFUpdateWrite(CMemFile_GetData(), CMemFile_GetLength());
1236                                }
1237                                else
1238                                {
1239                                        static int old_pos = -1;
1240                                        int pos =  (nReceiveCount - 1) * 100 / nMaxBlockNumber;
1241                                        event.data32[0] = DATA_RECEIVING; // ¼ö½ÅÁß
1242                                        event.data32[1] = pos;
1243                                        if (old_pos != pos) 
1244                                        {
1245                                                DST_SendWindowEvent(event);
1246                                                old_pos = pos;
1247                                        }
1248                                }
1249                        }
1250                        break;
1251                case CT_FLASH_WRITE_COMPLETE:
1252                        CMemFile_Delete();
1253                        T();DST_Printf("CT_FLASH_WRITE_COMPLETE\n");
1254                        bOTC_Receive_Complete = true;
1255                        break;
1256
1257                case CT_RF_UPDATE_START:
1258                        bOTC_Receive_Complete = false; 
1259                        break;
1260                       
1261                case CT_RECEIVE_RF_UPDATE:
1262                        {
1263                                if (bOTC_Receive_Complete == true) break;
1264                                char *p = (char*)p2;
1265                                char *strModelName = &p[3+5]; //Á¦Ç°¸ðµ¨¸í
1266                                char *strVersionName = &p[13+5]; // S/W ¹öÀü
1267                                char *strFileSize = &p[21+5]; // Àüü ÆÄÀÏ ±æÀÌ
1268//                              char *strPosition = &p[29]; // µ¥ÀÌÅÍÀÇ ½ÃÀÛ À§Ä¡
1269                                char *strLength = &p[37+5]; // µ¥ÀÌÅÍÀÇ ±æÀÌ
1270                                char *strMaxBlockNumber = &p[45+5]; // ¸¶Áö¸· ºí·° ¹øÈ£
1271                                char *strBlockNumber = &p[53+5]; // ¸î¹øÂ° ºí·°ÀÎÁö 0ºÎÅÍ ½ÃÀÛ
1272                                char *pSrc = &p[61+5];
1273                               
1274                                int nFileSize = dst_atoi(strFileSize);
1275//                              int nPosition = dst_atoi(strPosition);
1276                                int nLength = dst_atoi(strLength);
1277                                int nMaxBlockNumber = dst_atoi(strMaxBlockNumber);
1278                                int nBlockNumber = dst_atoi(strBlockNumber);
1279                       
1280                                // Å×½ºÆ® ºñ±³ Á¶°ÇÀÓ È®ÀÎ ÈÄ ¼öÁ¤ ÇÊ¿ä
1281                                static char* pData = 0;
1282                                static int*   pLength = 0;
1283                               
1284                                SWinEventMsg event;
1285                                event.cmd = WM_RF_UPDATE;
1286                                memcpy(&event.data32[2], strVersionName, 8);
1287                                       
1288                                if (strcmp(strModelName, DST_GetModelName()) != 0)
1289                                {
1290                                        T();
1291                                        DST_Printf("%s %s\n", strModelName, DST_GetModelName());
1292                                        bOTC_Receive_Complete = true;
1293                                        JST_Stop();
1294                                        if (pData) DST_OS_Free(&pData);
1295                                        if (pLength) DST_OS_Free(&pLength);
1296                                        event.data32[0] = 100; // ´Ù¸¥ ¸ðµ¨ÀÓ
1297                                        DST_SendWindowEvent(event);
1298                                        break;
1299                                }
1300//                              if (strcmp(strVersionName, DST_GetAppShortVersion()) == 0)
1301//                              {
1302//                                      T();
1303//                                      bOTC_Receive_Complete = true;
1304//                                      JST_Stop();
1305//                                      if (pData) DST_OS_Free(&pData);
1306//                                      if (pLength) DST_OS_Free(&pLength);
1307//                                      event.data32[0] = 101; // °°Àº ¹öÀüÀÓ
1308//                                      DST_SendWindowEvent(event);
1309//                                      break;
1310//                              }
1311                               
1312                                if (CMemFile_GetLength() == 0) // óÀ½ ¹Þ´Â °æ¿ì
1313                                {
1314                                        bOTC_Receive_Complete = false;
1315                                        CMemFile_New(nFileSize, nLength);
1316                                }
1317                                CMemFile_SetBlock(nBlockNumber, pSrc, nLength);
1318                               
1319                                int nReceiveCount = 0;
1320                                nMaxBlockNumber = 0;
1321                                CMemFile_GetProgress(&nReceiveCount, &nMaxBlockNumber);
1322                                //SWinEventMsg event;
1323                                memset(&event, 0, sizeof(SWinEventMsg));
1324                                event.cmd = WM_RF_UPDATE;
1325                                if (nReceiveCount == nMaxBlockNumber)
1326                                {
1327                                        bOTC_Receive_Complete = true;
1328                                        event.data32[0] = DATA_RECEIVING_COMPLETE; // ¼ö½Å ¿Ï·á
1329                                        DST_SendWindowEvent(event);
1330                                        JST_Stop();
1331                                        DST_RFUpdateWrite(CMemFile_GetData(), CMemFile_GetLength());
1332                                }
1333                                else
1334                                {
1335                                        static int old_pos = -1;
1336                                        int pos =  (nReceiveCount - 1) * 100 / nMaxBlockNumber;
1337                                        event.data32[0] = DATA_RECEIVING; // ¼ö½ÅÁß
1338                                        event.data32[1] = pos;
1339                                        if (old_pos != pos) 
1340                                        {
1341                                                DST_SendWindowEvent(event);
1342                                                old_pos = pos;
1343                                        }
1344                                }
1345                        }
1346                        break;
1347        }
1348}
Note: See TracBrowser for help on using the repository browser.