source: svn/trunk/zasc/app_c/dst_eroum_interface.c @ 2

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

first commit

File size: 43.6 KB
Line 
1#include "dst_eroum_interface.h"
2
3#ifdef DSTAR
4
5#include "dsthallocal.h"
6#include "dsthalerror.h"
7#include "dsthalPsiProcess.h"
8#include "dsthalcap.h"
9#include "dsthalsys.h"
10#include "dsthaldev.h"
11
12#else
13
14#include "DHL_SYS_Impl.h"
15#include "DHL_SYS.h"
16#include "DHL_PSI.h"
17#include "DHL_FE.h"
18#include "DHL_UIO.h"
19#include "DHL_AVCAP.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25#include "DHL_Graphic.h"
26
27#ifdef __cplusplus
28}
29#endif
30
31#endif
32
33#include "DST_ISDBT_ChannelTask.h"
34#include "DST_CommonAPI.h"
35#include "DST_HostInterface.h"
36
37#include "DST_WinManagerTask.h"
38#include "sqlite3.h"
39#include "DST_UserInterface.h"
40
41static DHL_SignalInfoCallBack g_SignalInfoCallBack = 0;
42static DHL_VideoStartCallBack g_VideoStartCallBack = 0;
43static DHL_AudioStartCallBack g_AudioStartCallBack = 0;
44static DHL_SiCallBack g_SiCallBack = 0;
45static DHL_CCCallBack g_CCCallBack = 0;
46static DHL_IrCallBack g_IrCallBack = 0;
47
48#ifdef DSTAR
49DHL_PSI_HANDLE hPsi = 0;
50#else
51tDHL_TSD tsd = 0;
52#endif
53
54/*extern "C" */void change_file(int num, int bReadList, int bForce, int bCont);
55
56#ifdef DSTAR
57static void _DHL_SignalInfoCallBack(bool bFocus, DHL_CAP_VIDEO_INPUT vi, DS_U16 hRes, DS_U16 vRes, DS_U16 RefreshRate, bool bInterlaced, DHL_CAP_SRCASPECTRATIO Aspect)
58{
59        if (g_SignalInfoCallBack == 0) return;
60        DHL_SOURCE_ASEPCT as = DHL_SOURCE_UNKNOWN;
61        switch (Aspect)
62        {
63                case DHL_CAP_SRC_ASPECT_16_9:
64                        as = DHL_SOURCE_16x9;
65                        break;
66                case DHL_CAP_SRC_ASPECT_SQUARE:
67                case DHL_CAP_SRC_ASPECT_4_3:
68                        as = DHL_SOURCE_4x3;
69                        break;
70                default:
71                        break;
72        }
73        g_SignalInfoCallBack(hRes, vRes, RefreshRate, bInterlaced ? true : false, as);
74}
75#endif
76
77// 2012.12.04 USER DATA¿¡¼­ CC µ¥ÀÌÅÍ ÃßÃâ
78static void DHL_UserData_DTVCCPacketBlock(DS_U8 service_number, DS_U8 nSize, DS_U8 *buff)
79{
80#if 0
81        DST_Printf("%s service_number = %d nSize = %d %X\n", __func__, service_number, nSize, buff);
82        for (int i = 0; i<nSize; i++) DST_Printf("%02X ", buff[i]);
83        DST_Printf("\n");
84#endif
85        //ZASÀÇ °æ¿ì 1¹ø ¼­ºñ½º¸¸ ó¸®ÇÑ´Ù.
86        if (service_number != 1) return;
87        if (g_CCCallBack) g_CCCallBack((DS_U8*)buff, (int)nSize);
88}
89
90static void DHL_UserData_DTVCCPacket(DS_U8 nSize, DS_U8 *buff)
91{
92        static DS_U8 nSeqNumber = 0xFF;
93        static DS_U8 prev_nSize;
94        static DS_U8 prev_buff[256];
95       
96        if (nSize == prev_nSize && memcmp(buff, prev_buff, nSize) == 0) return; // ÀÌÀüµ¥ÀÌÅÍ¿Í µ¿ÀÏÇÏ¸é ¹«½Ã
97        prev_nSize = nSize;
98        memcpy(prev_buff,  buff, nSize);
99       
100        DS_U8 seq_number = (buff[0] >> 6) & 0x03;
101        // DST_Printf("seq_number = %d\n", seq_number);
102        if (nSeqNumber != 0xFF && ((nSeqNumber+1)%4) != seq_number)
103        {
104                DST_Printf("Seq Error\n");
105                if (g_CCCallBack) g_CCCallBack((DS_U8*)0, -1);
106        }
107        nSeqNumber = seq_number;       
108        if (nSize < 3) return;
109        DS_U8 block_size = buff[1] & 0x1F; // Fugure 7 in EIA-708B
110        if (block_size < 1) return; 
111        DS_U8 service_number = (buff[1]>>5) & 0x07;
112        if (service_number < 1) return;
113        if (service_number == 7) 
114        {
115                if (nSize < 3) return;
116                service_number += (buff[2] & 0x3F); // extended service_number Figure 8
117                if (block_size > nSize-2) block_size = nSize-2; // block size º¸Á¤
118                DHL_UserData_DTVCCPacketBlock(service_number, block_size, &buff[3]);
119        }
120        if (block_size > nSize-1) block_size = nSize-1; // block size º¸Á¤
121        DHL_UserData_DTVCCPacketBlock(service_number, block_size, &buff[2]);
122}
123
124static void DHL_UserData_DTVCC(bool bStart, DS_U8 cc_data_1, DS_U8 cc_data_2)
125{
126        //DST_Printf("%s %d %02X %02X\n", __func__, bStart, cc_data_1, cc_data_2);
127        static DS_U8 nPacketSize = 0;
128        static DS_U8 data[256];
129        static DS_U8 nReceiveCount = 0;
130        if (bStart)
131        {
132                nPacketSize = (cc_data_1 & 0x3F);
133                nPacketSize = (nPacketSize == 0) ? 128 : nPacketSize*2; // Figure 4
134                data[0] = cc_data_1;
135                data[1] = cc_data_2;
136                nReceiveCount = 2;
137        }
138        else
139        {
140                if (nPacketSize == 0) return;
141                if (nPacketSize > nReceiveCount) 
142                {
143                        data[nReceiveCount] = cc_data_1;
144                        nReceiveCount++;
145                }
146                if (nPacketSize > nReceiveCount) 
147                {
148                        data[nReceiveCount] = cc_data_2;
149                        nReceiveCount++;
150                }
151                if (nReceiveCount >=  nPacketSize)
152                {
153                        DHL_UserData_DTVCCPacket(nReceiveCount, data);
154                        nPacketSize = 0;
155                        nReceiveCount = 0;
156                }
157        }
158}
159
160#ifdef DSTAR
161static void DHL_UserData_ClosedCaption(DS_U16 nSize, DS_U8 *buff)
162{
163#if 0
164        DST_Printf("%s %d %X\n", __func__, nSize, buff);
165        for (int i = 0; i<nSize; i++) DST_Printf("%02X ", buff[i]);
166        DST_Printf("\n");
167#endif
168        DS_U8 cc_count = buff[0] & 0x1F;
169        if (nSize < cc_count * 3 + 2) return;
170        int i;
171        for ( i = 0; i < cc_count; i++)
172        {
173                bool cc_valid = (buff[i*3+2] & 0x04) ? true : false;
174                DS_U8 cc_type = (buff[i*3+2] & 0x03);
175                if (cc_valid == false) continue;
176                switch (cc_type) // EIA-708B #7
177                {
178                        case 0: // line21 field 1
179                                break;
180                        case 1: // line21 field 2
181                                break;
182                        case 2: // dtvcc data
183                                DHL_UserData_DTVCC(false, buff[i*3+3],  buff[i*3+4]);
184                                break;
185                        case 3: // dtvcc start
186                                DHL_UserData_DTVCC(true, buff[i*3+3],  buff[i*3+4]);
187                                break;
188                        default:
189                                break;
190                }
191        }
192}
193#endif
194
195#ifdef DSTAR
196static void DHL_UserData_Callback(DS_U16 nSize, DS_U8 *buff)
197{
198        int i;
199#if 0
200        DST_Printf("%s %d %X\n", __func__, nSize, buff);
201        for (i = 0; i<nSize; i++) DST_Printf("%02X ", (char*)buff[i]);
202        DST_Printf("\n");
203#endif
204
205#ifdef DSTAR
206                for ( i=0; i < nSize - 10; i++)
207                {
208                        // user_data_start_code = 0x000001B2 a_53b 5.2.3
209                        if (buff[i] == 0x00 && buff[i+1] == 0x00 && buff[i+2] == 0x01 && buff[i+3] == 0xB2)
210                        {
211                                // ATSC_identifier = 0x47413934
212                                // user_data_type_code = 0x03
213                                if (buff[i+4] == 0x47 && buff[i+5] == 0x41 && buff[i+6] == 0x39 && buff[i+7] == 0x34 && buff[i+8] == 0x03)
214                                {
215                                        DHL_UserData_ClosedCaption(nSize - i - 9, &buff[i+9]);
216                                }
217                        }
218                }
219#else
220        for (i=0; i < nSize - 6; i++)
221        {
222                if (buff[i+0] == 0x47 && buff[i+1] == 0x41 && buff[i+2] == 0x39 && buff[i+3] == 0x34 && buff[i+4] == 0x03)
223                {
224                        DHL_UserData_ClosedCaption(nSize - i - 5, &buff[i+5]);
225                }
226        }
227#endif
228}
229#endif
230
231#ifdef DSTAR
232static void _DHL_CCCallBack(DS_U32 nSize, void *buffer)
233{
234        DHL_UserData_Callback((DS_U16)nSize, (DS_U8*)buffer);
235}
236#endif
237
238#ifdef DSTAR
239
240static void _DHL_VideoStartCallBack()
241{
242        if (g_VideoStartCallBack == 0) return;
243        g_VideoStartCallBack();
244}
245
246#else
247
248#define USER_DATA_COUNT_MAX 8
249static int g_UserDataSize[USER_DATA_COUNT_MAX];
250static DS_U8 g_UserData[USER_DATA_COUNT_MAX][256];
251static int g_UserDataHead = 0;
252static int g_UserDataTail = 0;
253static void tUserData()
254{
255        while (1)
256        {
257                DST_OS_Delay(10); // 10ms
258                if (g_UserDataHead == g_UserDataTail) continue;
259//              DST_Printf("g_UserDataTail = %d\n", g_UserDataTail);
260                int nLen = g_UserDataSize[g_UserDataTail];
261                DS_U8 *pdata = g_UserData[g_UserDataTail];
262                int i;
263                for (i = 0; i < nLen; i+=3)
264                {
265                        switch (pdata[i])
266                        {
267                                case 0: // line21 field 1
268                                        break;
269                                case 1: // line21 field 2
270                                        break;
271                                case 2: // dtvcc data
272                                        DHL_UserData_DTVCC(false, pdata[i+1],  pdata[i+2]);
273                                        break;
274                                case 3: // dtvcc start
275                                        DHL_UserData_DTVCC(true, pdata[i+1],  pdata[i+2]);
276                                        break;
277                                default:
278                                        break;
279                        }
280                }
281                g_UserDataTail = (g_UserDataTail >= USER_DATA_COUNT_MAX-1) ? 0 : g_UserDataTail+1;
282        }
283}
284
285static void _DHL_AV_CallBack(tDHL_AVCallbackType cb_type, DS_U32 param)
286{
287        if (cb_type == eDHL_CB_VideoSeqHdr && g_SignalInfoCallBack)
288        {
289                if (param == 0) return; 
290                tDHL_VideoSeqHdr *pSeq = (tDHL_VideoSeqHdr *)param;
291                DST_Printf("-- Video Sequence Header info -- \n");
292                DST_Printf("aspect_ratio_information : %d \n", pSeq->aspect_ratio_information);
293                DST_Printf("frame_rate_code : %d \n", pSeq->frame_rate_code);
294                DST_Printf("horizontal_size : %d \n", pSeq->horizontal_size);
295                DST_Printf("vertical_size : %d \n", pSeq->vertical_size);
296                DST_Printf("progressive_sequence : %d \n", pSeq->progressive_sequence);
297
298                g_SignalInfoCallBack(pSeq->horizontal_size, pSeq->vertical_size,
299                        pSeq->frame_rate_code, pSeq->progressive_sequence ? false : true,
300                        (pSeq->aspect_ratio_information == DAR_9_16) ? DHL_SOURCE_16x9 : DHL_SOURCE_4x3);
301        }
302        if (cb_type == eDHL_CB_FirstVideoShow && g_VideoStartCallBack)
303        {
304                g_VideoStartCallBack();
305        }
306        if (cb_type == eDHL_CB_VideoUserData)
307        {
308                if (param == 0) return;
309                tDHL_UserData *pud=(tDHL_UserData *)param;
310                if (pud->pdata == 0) return;
311                if (pud->hdr.payload_size > 256) return;
312                int nLen = pud->hdr.payload_size;
313                nLen = (nLen/3)*3;
314                g_UserDataSize[g_UserDataHead] = nLen;
315                memcpy(g_UserData[g_UserDataHead], pud->pdata, nLen);
316//              DST_Printf("g_UserDataHead = %d\n", g_UserDataHead);
317                g_UserDataHead = (g_UserDataHead >= USER_DATA_COUNT_MAX-1) ? 0 : g_UserDataHead+1;
318        }
319}
320
321#endif
322
323#if 0
324____System___()
325#endif
326
327#ifdef DSTAR
328static void DSTAR_KeyCallBack(int bRemocon, DS_U32 Code, int bPressed);
329#endif
330
331// SYSTEM ÃʱâÈ­
332/*extern "C" */void dbg_print(int val);
333DHL_ERROR DHL_SYS_Initialize()
334{
335#ifdef DSTAR
336#else
337        DHL_SYS_PlatformInit(); // ÃʱâÈ­
338        int DHL_NVM_FlashGetSize(int flashId);
339        FLASH_TOTAL_SIZE =  DHL_NVM_FlashGetSize(0);
340        if (FLASH_TOTAL_SIZE < 8*1024*1024) FLASH_BANK1_POS = FLASH_BANK0_POS;
341        DST_OS_SpawnTask((void(*)(void *))tUserData, (char*)"tUserData", APP_TASK_USER_DATA, WIN_MGR_TASK_STACKSIZE,  0);
342#endif
343        return DHL_OK_;
344}
345
346typedef void (*irCallback)(unsigned int KeyCode, unsigned int repeat);
347static void IRCallback(unsigned int code, unsigned int repeat);
348extern void IR_Open(irCallback ircallback);
349
350DHL_ERROR DHL_SYS_TV_Open(
351                DHL_SignalInfoCallBack signal,
352                DHL_VideoStartCallBack video,
353                DHL_AudioStartCallBack audio,
354                DHL_SiCallBack si,
355                DHL_CCCallBack cc,
356                DHL_IrCallBack Ir)
357{
358        // ÄÝ¹é µî·Ï
359        g_SignalInfoCallBack = signal;
360        g_VideoStartCallBack = video;
361        g_AudioStartCallBack = audio;
362        g_SiCallBack = si;
363        g_CCCallBack = cc;
364        g_IrCallBack = Ir;
365        // ÃʱâÈ­ ÀÛ¾÷
366#ifdef DSTAR
367        DHL_DISP_PresetVideoOutputFormat(DHL_VIDEO_WXGA);
368        DHL_SYS_Initiate();
369        DHL_PSI_Init(&hPsi);
370        DHL_FE_Init(TUNER_FAT);
371        DHL_DTV_Init();
372        DHL_DMX_Init();
373        DHL_DMX_Start();
374        DHL_CAP_PresetAudioInput(DHL_CAP_DTV0_AUDIO);
375        DHL_DEV_InitSmartCardInterface((void*)0); // ÀÌ ¶óÀÎÀ» Á¦°ÅÇϸé DSTAR ¼¼¸¶Æ÷¾î ¿¡·¯°¡ ³­´Ù.
376        DHL_CAP_Init();
377        DHL_CAP_StartVideoEx(DHL_CAP_DTV0_VIDEO, DHL_CAP_VIDEO_NONE, DHL_PIPMODE_NONE, DHL_PIP_NO_PIP);
378        DHL_DTV_StopAudio();
379        DHL_CAP_ChangeAudioInput(DHL_CAP_DTV0_AUDIO);
380        DHL_CAP_StartAudio();
381        DHL_DTV_PresetCallback((void *) _DHL_VideoStartCallBack, DHL_CB_NOTIFY_VSTARTING, 1);
382        DHL_CAP_PresetCallback2((DHL_CAP_CBFUNC2_t)_DHL_SignalInfoCallBack);
383        DHL_DTV_PresetCallback((void *) _DHL_CCCallBack, DHL_CB_VDC_CCUDP, 0);
384        DHL_DTV_InitCC();
385        DHL_DTV_EnableCC();
386        DHL_IR_Init(DHL_IR_NEC);
387        DHL_IR_SetCbFunc(DSTAR_KeyCallBack);
388        void test_cc();
389        test_cc();
390#else
391        DHL_AV_SetCallback(eDHL_CB_VideoSeqHdr, _DHL_AV_CallBack);
392        DHL_AV_SetCallback(eDHL_CB_FirstVideoShow, _DHL_AV_CallBack);
393        DHL_AV_SetCallback(eDHL_CB_VideoUserData, _DHL_AV_CallBack);
394        // IR ŽºÅ© »ý¼º
395        static void tIRTask();
396        DST_OS_SpawnTask((void(*)(void *))tIRTask, (char*)"tIRTask", APP_TASK_PRIO_IR, WIN_MGR_TASK_STACKSIZE,  0);
397        IR_Open(IRCallback);
398#endif
399        return DHL_OK_;
400}
401
402DHL_ERROR DHL_SYS_TV_Close()
403{
404        return DHL_OK_;
405}
406
407#ifdef DSTAR
408#else
409#define PSIDataArray_t tDHL_PSI_DataArray
410#define DHL_TBL_HANDLE tDHL_PSI_ControlHandle
411#define PSIEvent tDHL_PSI_Event
412#define psiDataReceived ePSIEVENT_DATARECEIVED
413#endif
414
415typedef struct PSIMultiEventProcData_t
416{
417        int err;
418        DS_U32 hEvent;
419        DS_U32 id;
420        DS_U32 freq;
421        DS_U16 sID;
422} PSIMultiEventProcData;
423
424static void CT_MultiEventProc(PSIEvent event, DHL_TBL_HANDLE hTblHandle, DS_U32 userParam)
425{
426        if (!hTblHandle || event != psiDataReceived) return;
427        PSIDataArray_t *desc;
428        if (DHL_PSI_ReadPSIData(hTblHandle, &desc) != DHL_OK) return;
429        PSIMultiEventProcData *procData = (PSIMultiEventProcData *) userParam;
430        if (g_SiCallBack) g_SiCallBack(procData->freq, procData->id, desc->sectPtr, 0);
431        DHL_PSI_FreePSIData(desc);
432}
433
434#if 0
435____Video___()
436#endif
437
438static int prev_lock = 0; // Tuner Lock »óÅÂ
439// Video ½ÃÀÛ
440static bool g_power_display = true;
441static bool bVideoStart = false;
442DHL_ERROR DHL_VID_Start(DS_U16 VideoPID, DS_U16 PcrPID, DHL_VIDEO_TYPE type)
443{
444        if (bVideoStart == true) DHL_VID_Stop();
445        if (g_power_display == false) return DHL_FAIL_; 
446        if (prev_lock == 0) return DHL_FAIL_; 
447        bVideoStart = true;
448#ifdef DSTAR
449        DHL_DTV_StartVideoEx2(VideoPID, PcrPID, type );
450#else
451        tDHL_VideoCodingType nType = eDHL_VIDEO_TYPE_H264;
452        switch (type)
453        {
454                case DHL_VIDEO_MPEG1:
455                        nType = eDHL_VIDEO_TYPE_MPEG1;
456                        break;
457                case DHL_VIDEO_MPEG2:
458                        nType = eDHL_VIDEO_TYPE_MPEG2;
459                        break;
460                default:
461                        break;
462        }
463        T();
464        DHL_AV_VideoStart(0, VideoPID, PcrPID, nType);
465        T();
466#endif
467        return DHL_OK_;
468}
469
470// Video Á¾·á
471DHL_ERROR DHL_VID_Stop()
472{
473        if (bVideoStart == false) return DHL_OK_;
474         bVideoStart = false;
475#ifdef DSTAR
476        DHL_DTV_StopVideo();
477#else
478        T();
479        DHL_AV_VideoStop(0);
480#endif
481        return DHL_OK_;
482}
483
484// Video Mute ºñµð¿À È­¸éÀ» °ËÀº»öÀ¸·Î º¸ÀÌÁö ¾Êµµ·Ï ÇÔ. CVBS¿¡¼­µµ µ¿ÀÏ
485// bMute = false --> Video Unmute
486// bMute = true  --> Video Mute
487DHL_ERROR DHL_VID_Mute(bool bMute)
488{
489#ifndef DSTAR
490        DST_Printf("DHL_VID_Mute %d\n", bMute);
491        DHL_AV_VideoHide(0, bMute ? 1 : 0);
492#endif
493        return DHL_OK_;
494}
495
496// Video Alive µðÄÚµùµÈ ºñµð¿À°¡ ÀÖ´ÂÁö ¿©ºÎ¸¦ ¾Ë·ÁÁÖ´Â ÇÔ¼ö
497bool DHL_VID_Alive()
498{
499#ifndef DSTAR
500        tDHL_VideoStatus vs;
501        DHL_AV_VideoGetStatus(0, &vs);
502        if (!vs.bOutputExist) return false;
503#endif
504        return true;
505}
506
507// Video Aspect ¼³Á¤
508// ÀÔ·Â ASPECT(4:3 16:9) ¿¡µû¶ó °¢°¢ ³× °¡Áö ¸ðµå°¡ ÀÖÀ½
509DHL_ERROR DHL_VID_Aspect(DHL_ASPECT_RATIO ar, DS_U16 w, DS_U16 h)
510{
511#ifndef DSTAR
512        tDHL_DispARC adj = eDHL_ARC_FULLSCREEN;
513        switch (ar)
514        {
515                case DHL_ASPECT_4x3_4x3:
516                        adj = eDHL_ARC_FULLSCREEN;
517                        printf("DHL_ASPECT_4x3_4x3 eDHL_ARC_FULLSCREEN\n");
518                        break;
519                case DHL_ASPECT_16x9_4x3_WIDE:
520                        adj = eDHL_ARC_LETTERBOX;
521                        printf("DHL_ASPECT_16x9_4x3_WIDE eDHL_ARC_FULLSCREEN\n");
522                        break;
523                case DHL_ASPECT_16x9_4x3_NORMAL:
524                        adj = eDHL_ARC_FULLSCREEN;
525                        printf("DHL_ASPECT_16x9_4x3_NORMAL eDHL_ARC_SIDEBAR\n");
526                        break;
527                case DHL_ASPECT_16x9_4x3_ZOOM:
528                        adj = eDHL_ARC_ZOOM2;
529                        printf("DHL_ASPECT_16x9_4x3_ZOOM eDHL_ARC_ZOOM1\n");
530                        break;
531                default:
532                        break;
533        }
534        DHL_AV_VideoSetARC(adj, adj);
535#endif
536        return DHL_OK_;
537}
538
539#if 0
540____Audio___()
541#endif
542
543// Audio ½ÃÀÛ
544// PcrPID == 0 Àΰæ¿ì Audio Only ä³Î
545static bool bAudioStart = false;
546DHL_ERROR DHL_AUD_Start(DS_U16 AudioPid, DS_U16 PcrPID, DHL_AUDIO_TYPE type)
547{
548        DST_Printf("%s(0x%X, 0x%X, %d)\n", __func__, AudioPid, PcrPID, type);
549        if (bAudioStart == true) DHL_AUD_Stop();
550        bAudioStart = true;
551#ifndef DSTAR
552        tDHL_AudioCodingType nType = eDHL_AUDIO_TYPE_AAC_LATM;
553        switch (type)
554        {
555                case DHL_AUDIO_MPEG1:
556                        nType = eDHL_AUDIO_TYPE_MPEG_1;
557                        break;
558                case DHL_AUDIO_MPEG2:
559                        nType = eDHL_AUDIO_TYPE_MPEG_2;
560                        break;
561                case DHL_AUDIO_AAC_ADTS:
562                        nType = eDHL_AUDIO_TYPE_AAC_ADTS;
563                        break;
564                case DHL_AUDIO_AAC_LATM:
565                        nType = eDHL_AUDIO_TYPE_AAC_LATM;
566                        break;
567                case DHL_AUDIO_AC3:
568                        nType = eDHL_AUDIO_TYPE_AC3;
569                        break;
570                default:
571                        break;
572        }
573        DHL_AV_AudioStart(AudioPid, PcrPID, nType);
574#endif
575        return DHL_OK_;
576}
577
578// Audio Á¾·á
579DHL_ERROR DHL_AUD_Stop()
580{
581        if (bAudioStart == false) return DHL_OK_;
582        bAudioStart = false;
583#ifndef DSTAR
584        T();
585        DHL_AV_AudioStop();
586#endif
587        return DHL_OK_;
588}
589
590// Audio Volume, 0 is mute
591DHL_ERROR DHL_AUD_Volume(DS_U8 nVal)
592{
593        static DS_U8 prev = 200;
594        if (prev == nVal) return DHL_OK_;
595        prev = nVal;   
596//      DST_Printf("%s|%d\n", __func__, nVal);
597#ifndef DSTAR
598//      T();
599        DHL_AV_AudioSetUserVolumeRange(0,100);
600        if (nVal == 0)
601        {
602                DHL_AV_AudioSetVolume(0);
603                DHL_AV_AudioMuteControl(eDHL_AUDIO_MUTE_GPIO, TRUE);
604        }
605        else
606        {
607                DHL_AV_AudioMuteControl(eDHL_AUDIO_MUTE_GPIO, FALSE);
608                DHL_AV_AudioSetVolume(nVal);
609        }
610#endif
611        return DHL_OK_;
612}
613
614// Audio Alive µðÄÚµùµÈ ¿Àµð¿À°¡ ÀÖ´ÂÁö ¿©ºÎ¸¦ ¾Ë·ÁÁÖ´Â ÇÔ¼ö
615bool DHL_AUD_Alive()
616{
617#ifndef DSTAR
618        tDHL_AudioStatus as;
619        DHL_AV_AudioGetStatus(&as);
620        if (!as.bOutputExist) return false;
621#endif
622        return true;
623}
624
625// Play ÁßÀÎ ¿Àµð¿ÀÀÇ ¸ðµå¸¦ ¹ÝȯÇÑ´Ù.
626DHL_AUDIO_MODE DHL_AUD_GetMode()
627{
628        return DHL_AUDIO_STEREO;
629}
630
631#if 0
632____SI___()
633#endif
634
635typedef struct 
636{
637        DS_U32 nRequestID;
638        PSIMultiEventProcData *aProcData;
639        void **aPsiCtl;
640} DHL_SI_HANDLE;
641
642#ifdef DSTAR
643#else
644#define tableMode ePSIMODE_TABLE
645#define sectionMode ePSIMODE_SECTION
646#define PSIMode tDHL_PSI_Mode
647#endif
648
649static DHL_HANDLE DHL_SI_Monitor(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid, DS_U8 tableID, PSIMode psiMode, DS_U16 source_id/* = 0*/, bool bETT/* = false*/)
650{
651        DHL_SI_HANDLE *handle = (DHL_SI_HANDLE *)DST_OS_Calloc(1, sizeof(DHL_SI_HANDLE));
652        handle->nRequestID = nRequestID;
653        handle->aProcData = (PSIMultiEventProcData *)DST_OS_Calloc(1, sizeof(PSIMultiEventProcData));
654        handle->aProcData->err = noError;
655        handle->aProcData->id = nRequestID;
656        handle->aProcData->freq = RF;
657        handle->aPsiCtl = (void **)DST_OS_Calloc(1, sizeof(void *));
658#ifdef DSTAR
659        PSIMask_t *pref = 0;
660        if (source_id)
661        {
662                if (bETT == false) // EIT
663                {
664                        DD_PSI_GetEmptyPSIMask(5,&pref);
665                        pref->entries[0].value = (tableID == 0xFF) ? 0 : tableID;
666                        pref->entries[0].matchMask = (tableID == 0xFF) ? 0 : 0xFF;
667                        pref->entries[3].value = (source_id >> 8) & 0xFF;
668                        pref->entries[3].matchMask = 0xFF;
669                        pref->entries[4].value = source_id & 0xFF;
670                        pref->entries[4].matchMask = 0xFF;
671                }
672                else // ETT
673                {
674                        DD_PSI_GetEmptyPSIMask(11,&pref);
675                        pref->entries[0].value = (tableID == 0xFF) ? 0 : tableID;
676                        pref->entries[0].matchMask = (tableID == 0xFF) ? 0 : 0xFF;
677                        pref->entries[9].value = (source_id >> 8) & 0xFF;
678                        pref->entries[9].matchMask = 0xFF;
679                        pref->entries[10].value = source_id & 0xFF;
680                        pref->entries[10].matchMask = 0xFF;                     
681                }
682                T();
683        }
684        else
685        {
686                DD_PSI_GetEmptyPSIMask(1,&pref);
687                pref->entries[0].value = (tableID == 0xFF) ? 0 : tableID;
688                pref->entries[0].matchMask = (tableID == 0xFF) ? 0 : 0xFF;
689        }
690        if (pref)
691        {
692                DHL_RESULT err = DD_PSI_MonitorPSIPid(hPsi, pid, psiMode,  psiCRCChange,
693                        pref, 4*1024, 256, CT_MultiEventProc, (DS_U32)handle->aProcData, handle->aPsiCtl);
694                if (err) PSI_Free(pref);
695                if (err) DST_Printf( "\t !! %s err %d \n", __func__, err);
696        }
697#else
698        tDHL_PSI_Filter *pref = 0;
699        if (source_id)
700        {
701                if (bETT == false) // EIT
702                {
703                        DHL_PSI_AllocGenericFilter(&pref);
704                        pref->coef[0]= (tableID == 0xFF) ? 0 : tableID;
705                        pref->mask[0]  = (tableID == 0xFF) ? 0xFF : 0x00;
706                        pref->coef[3]= (source_id >> 8) & 0xFF;
707                        pref->mask[3]  = 0x00;
708                        pref->coef[4] = source_id & 0xFF;
709                        pref->mask[4]  = 0x00;
710                }
711                else // ETT
712                {
713                        DHL_PSI_AllocGenericFilter(&pref);
714                        pref->coef[0]= (tableID == 0xFF) ? 0 : tableID;
715                        pref->mask[0]  = (tableID == 0xFF) ? 0xFF : 0x00;
716                        pref->coef[9]= (source_id >> 8) & 0xFF;
717                        pref->mask[9]  = 0x00;
718                        pref->coef[10] = source_id & 0xFF;
719                        pref->mask[10]  = 0x00; 
720                }
721        }
722        else
723        {
724                DHL_PSI_AllocGenericFilter(&pref);
725                pref->coef[0] = tableID; // Table ID°¡ 0xFFÀ̸é HAL¿¡¼­ MApi_DMX_SectPatternSetÀÇ ±æÀ̸¦ 0À¸·Î ÇÔ
726                pref->mask[0] = (tableID == 0xFF) ? 0xFF : 0x00;
727                //      pref->mode[0] = 0xFF;
728        }
729        if (pref)
730        {
731                DHL_RESULT err = DHL_PSI_StartMonitor(tsd, pid, psiMode, ePSIUPDATE_CRCCHANGE,
732                        pref, 4*1024, 256, CT_MultiEventProc, (DS_U32)handle->aProcData, (tDHL_PSI_ControlHandle *)handle->aPsiCtl);
733                if (err) DST_OS_Free(&pref);
734        }
735#endif
736        return (DHL_HANDLE)handle;
737}
738
739DHL_HANDLE DHL_SI_MonitorPAT(DS_U32 RF, DS_U32 nRequestID)
740{
741        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
742        return DHL_SI_Monitor(RF, nRequestID, 0x0000, 0x00, tableMode, 0, false);
743}
744
745DHL_HANDLE DHL_SI_MonitorPMT(DS_U32 RF, DS_U32 nRequestID, DS_U16 Pid, DS_U16 source_id)
746{
747        DST_Printf("%s nRequestID = %d pid = 0x%X\n", __func__, (int)nRequestID, Pid);
748        return DHL_SI_Monitor(RF, nRequestID, Pid, 0x02, tableMode, source_id, false);
749}
750
751DHL_HANDLE DHL_SI_MonitorTVCT(DS_U32 RF, DS_U32 nRequestID)
752{
753        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
754        return DHL_SI_Monitor(RF, nRequestID, 0x1FFB, 0xC8, tableMode, 0, false);
755}
756
757#if CVCT_SUPPORT
758DHL_HANDLE DHL_SI_MonitorCVCT(DS_U32 RF, DS_U32 nRequestID)
759{
760        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
761        return 0;
762}
763#endif
764
765#if EPG_SUPPORT
766DHL_HANDLE DHL_SI_MonitorMGT(DS_U32 RF, DS_U32 nRequestID)
767{
768        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
769        return DHL_SI_Monitor(RF, nRequestID, 0x1FFB, 0xC7, tableMode, 0, false);
770}
771#endif
772
773DHL_HANDLE DHL_SI_MonitorSTT(DS_U32 RF, DS_U32 nRequestID)
774{
775        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
776        return DHL_SI_Monitor(RF, nRequestID, 0x1FFB, 0xCD, sectionMode, 0, false);
777}
778
779#if EPG_SUPPORT
780DHL_HANDLE DHL_SI_MonitorEIT(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid, DS_U16 source_id)
781{
782        DST_Printf("%s nRequestID = %d pid = 0x%X source_id =0x%X\n", __func__, (int)nRequestID, pid, source_id);
783        return DHL_SI_Monitor(RF, nRequestID, pid, 0xCB, tableMode, source_id, false);
784}
785
786DHL_HANDLE DHL_SI_MonitorETT(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid, DS_U16 source_id)
787{
788        DST_Printf("%s nRequestID = %d pid = 0x%X source_id =0x%X\n", __func__, (int)nRequestID, pid, source_id);
789        return DHL_SI_Monitor(RF, nRequestID, pid, 0xCC, tableMode, source_id, true);
790}
791#endif
792
793DHL_HANDLE DHL_SI_MonitorRFUpdate(DS_U32 RF, DS_U32 nRequestID)
794{
795        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
796        return DHL_SI_Monitor(RF, nRequestID, 0x1FF5, 0xF5, sectionMode, 0, false);
797}
798
799DHL_HANDLE DHL_SI_MonitorSCTE_CVT(DS_U32 RF, DS_U32 nRequestID)
800{
801        DST_Printf("%s nRequestID = %d\n", __func__, (int)nRequestID);
802#ifdef DSTAR
803        return DHL_SI_Monitor(RF, nRequestID, 0xDE1, 0x3C, sectionMode, 0, false);
804#else // CMB ¿¡¼­ PID ÇÒ´ç¹Þ°í Á¤È®ÇÑ °ªÀ¸·Î ¼öÁ¤ÇØ¾ß ÇÔ
805        return DHL_SI_Monitor(RF, nRequestID, 0xDF1, 0x3C, sectionMode, 0, false);
806#endif
807}
808
809DHL_HANDLE DHL_SI_MonitorSCTE_DII(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid)
810{
811        DST_Printf("%s nRequestID = %d 0x%X\n", __func__, (int)nRequestID, pid);
812        return DHL_SI_Monitor(RF, nRequestID, pid, 0x3B, tableMode, 0, false);
813}
814
815DHL_HANDLE DHL_SI_MonitorSCTE_DDB(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid)
816{
817        DST_Printf("%s nRequestID = %d 0x%X\n", __func__, (int)nRequestID, pid);
818        return DHL_SI_Monitor(RF, nRequestID, pid, 0x3C, sectionMode, 0, false);
819}
820
821DHL_ERROR DHL_SI_MonitorStop(DHL_HANDLE* handle)
822{
823        if (handle == 0 || *handle == 0) return DHL_FAIL_;
824        T();
825        DHL_SI_HANDLE *h = (DHL_SI_HANDLE *)*handle;
826        PSIMultiEventProcData *aProcData = h->aProcData;
827        void **aPsiCtl = h->aPsiCtl;
828#ifdef DSTAR
829        DHL_PSI_CancelMonitor(aPsiCtl[0]);
830#else
831        DHL_PSI_StopMonitor((tDHL_PSI_ControlHandle)aPsiCtl[0]);
832#endif
833        if (aPsiCtl) DST_OS_Free(&aPsiCtl);
834        if (aProcData) DST_OS_Free(&aProcData);
835        DST_OS_Free(&h);
836        *handle = 0;
837        return DHL_OK_;
838}
839
840#if 0
841____Tuner___()
842#endif
843
844// Æ©³Ê ½ÃÀÛ
845// Khz ´ÜÀ§ Á֯ļö
846static DS_U32 tuner_start_tick = 0;
847DHL_ERROR DHL_TUN_Start(int rf, DHL_MODULATION_MODE nMode)
848{
849        int freqkHz = DST_GetMHzFrequencybyIndex(rf) * 1000;
850        if (DST_g_bUseAirFreq)
851        {
852                if (rf == DST_GetIndexByRFNumber(4)) freqkHz = 69 * 1000;
853                if (rf == DST_GetIndexByRFNumber(18)) freqkHz = 497 * 1000;
854                if (rf == DST_GetIndexByRFNumber(48)) freqkHz = 677 * 1000;
855        }
856        DST_Printf("%s|%d|Tune Freq = %d kHz\n", __func__, __LINE__, freqkHz);
857#ifdef DSTAR
858        change_file(rf, 0, 0, 1);
859        TunerDemod demod = DEMOD_8VSB;
860        if (nMode == DHL_MODULATION_64QAM) demod = DEMOD_64QAM;
861        if (nMode == DHL_MODULATION_256QAM) demod = DEMOD_256QAM;
862        DHL_FE_Start(TUNER_FAT, freqkHz, demod, FREQ);
863#else
864        static bool bInit = false;
865        if (bInit == false)
866        {
867                bInit = true;
868                T();
869                DHL_FE_Init();
870                T();
871        }
872        tDHL_Demod demod = eDHL_DEMOD_8VSB;
873        if (nMode == DHL_MODULATION_64QAM) demod = eDHL_DEMOD_64QAM;
874        if (nMode == DHL_MODULATION_256QAM) demod = eDHL_DEMOD_256QAM;
875        T();
876        DHL_FE_Start(0, freqkHz, demod, 0);
877        T();
878#endif
879        tuner_start_tick = DST_OS_GetTickCount();
880        return DHL_OK_;
881}
882
883// Æ©³Ê Á¾·á
884DHL_ERROR DHL_TUN_Stop()
885{
886#ifdef DSTAR
887        DHL_FE_Stop(TUNER_FAT);
888#else
889        DHL_FE_Stop(0);
890#endif
891        return DHL_OK_;
892}
893
894DHL_ERROR DHL_TUNE_Info(DS_U8 RF, int *SignalStrength, bool *bLock, int *nPower, int *nSNR)
895{
896       
897#ifdef DSTAR
898        *SignalStrength = rand() % 100;
899        *bLock = true;
900        *nPower = rand() % 100;
901#else
902        int i;
903        static int prev_snr[5];
904//      static int prev_lock = 0;
905        if (tuner_start_tick > DST_OS_GetTickCount()) tuner_start_tick = DST_OS_GetTickCount();
906        if (DST_OS_GetTickCount() - tuner_start_tick < DST_OS_GetTicksPerSecond()/10) // 100ms À̳»¿¡´Â È£ÃâÇÏÁö ¸»ÀÚ
907        {
908                *bLock = false;
909                *SignalStrength = 0;
910                for (i=0; i < 5; i++) prev_snr[i] = -1;
911                prev_lock = 0;
912                return DHL_OK_;
913        }
914       
915        int lock = 0, snr = 0, power = 0;
916        DHL_RESULT dhl_fe_get_sig_info_detail(int *lock, int *snr, int* power);
917        dhl_fe_get_sig_info_detail(&lock, &snr, &power);
918        if (prev_lock != lock)
919        {
920                DST_Printf("tuner lock = %d  %d0msec\n", lock, DST_OS_GetTickCount() - tuner_start_tick);
921                prev_lock = lock;
922        }
923//      DST_Printf("power = %d %d0msec\n", power, DST_OS_GetTickCount() - tuner_start_tick);
924        if (lock == 0 || snr > 20000) snr = 0;  // ¿¡·¯°ªÀ¸·Î ¹«½ÃÇÑ´Ù.
925        *nSNR = snr;
926        for (i=0; i < 4; i++) prev_snr[i] = prev_snr[i+1];
927        prev_snr[4] = snr;
928       
929        int nValidCount = 0;
930        int nSum = 0;
931        for (i=0; i < 5; i++)
932        {
933                if (prev_snr[i] < 0) continue;
934                nValidCount++;
935                nSum += prev_snr[i];
936        }
937        snr = nSum / nValidCount;
938        // snrÀÇ °ªÀº 0~12000 »çÀÌÀÇ °ªÀÌ´Ù.
939        snr-=2000;
940        if (snr > 12000) snr = 12000;
941        if (snr < 0) snr = 0;
942        snr/=100;
943        *bLock = lock ? true : false;
944        *SignalStrength = snr; // ¿ÀÅ佺ĵ½Ã LOCKÀÌ ¾Æ´Ñ ÀÌ °ªÀÌ 0º¸´Ù Å©¸é ½ÅÈ£°¡ Àִ°ÍÀ¸·Î °£ÁÖÇÑ´Ù.
945        *nPower = power;
946        if (*SignalStrength > 100) *SignalStrength = 100;
947//      DST_Printf("tick = %d\n", DST_OS_GetTickCount() - tuner_start_tick);
948//      DST_Printf("bLock = %d SignalStrength = %d\n", *bLock, *SignalStrength);
949#endif
950        return DHL_OK_;
951}
952
953#if 0
954____GFX___()
955#endif
956
957#ifdef DSTAR
958
959#if OSD_PIXEL_BPP == 16
960#define DS_PIXEL_t DS_U16
961#define PIXEL_FORMAT 10 //PF_RGB16
962#else
963#define DS_PIXEL_t DS_U32
964#define PIXEL_FORMAT 0 //PF_RGB32
965#endif
966
967/*extern "C" */void DHL_GFX_DrawImage( DS_U32 pBufferId, int offsetX, int offsetY, int w, int h, DS_PIXEL_t *pPixels);
968/*extern "C" */void DHL_GFX_UpdateScreen(int Plane);
969static DS_U32 id = 0;
970static DS_U32 gMsgQ = 0;
971
972static void tUpdateScreen()
973{
974        DS_U32 msg = 0;
975        DS_U32 retLen;
976        while (1)
977        {
978                //T();
979                DST_OS_ReceiveMessage_NoWait(gMsgQ, (DS_U32*)&msg, sizeof(DS_U32), &retLen);
980                DHL_GFX_UpdateScreen(0); // µÎ°³ÀÇ Å½ºÅ©¿¡¼­ µ¿½Ã¿¡ È£ÃâÇÏ¸é ½Ã½ºÅÛ ¸ØÃã...
981        }
982}
983
984static void DHL_UpdateScreen()
985{
986        DS_U32 msg = 0;
987        if (gMsgQ) DST_OS_SendMessage(gMsgQ, &msg, sizeof(DS_U32));
988}
989
990void DHL_GFX_Open()
991{
992        static bool bInit = false;
993        if (bInit == false)
994        {
995                DHL_GFX_Init(0,
996                        0, 0, DST_GetScreenWidth(), DST_GetScreenHeight() + 16,
997                        DST_GetScreenWidth(), DST_GetScreenHeight() + 16, PIXEL_FORMAT, &id);
998                bInit = true;
999                gMsgQ = DST_OS_CreateMessageQueue("qUpdateScreen", 0, 100, sizeof(DS_U32));
1000                /*DS_U32 TaskID = */DST_OS_SpawnTask((void(*)(void *))tUpdateScreen, (char*)"tUpdateScreen", APP_TASK_PRIO_WIN_MGR, WIN_MGR_TASK_STACKSIZE,  0);
1001        }
1002}
1003
1004void DHL_GFX_Draw(OSD_PIXEL_T *src, int x, int y, int w, int h)
1005{
1006        DHL_GFX_DrawImage(id, x, y, w, h, src);
1007        DHL_UpdateScreen();
1008}
1009
1010void DHL_SetLED(bool bRed, bool bGreen)
1011{
1012        static bool old_bRed = false;
1013        static bool old_bGreen = false;
1014
1015        if (bRed == old_bRed && bGreen == old_bGreen) return;
1016        old_bRed = bRed;
1017        old_bGreen = bGreen;
1018        DHL_GFX_Open();
1019        OSD_PIXEL_T *src = (OSD_PIXEL_T *)DST_OS_Calloc(DST_GetScreenWidth() * 16, sizeof(OSD_PIXEL_T));
1020        OSD_PIXEL_T color = 0;
1021        if (bRed)
1022        {
1023                color = bGreen ? CONV32_16(0xFFFFFF00) : CONV32_16(0xFFFF0000);
1024        }
1025        else
1026        {
1027                color = bGreen ? CONV32_16(0xFF00FF00) : CONV32_16(0xFF000000);
1028        }
1029        int i;
1030        for ( i = 0; i < DST_GetScreenWidth() * 16; i++) src[i] = color;
1031        DHL_GFX_DrawImage(id, 0, DST_GetScreenHeight(), DST_GetScreenWidth(), 16, src);
1032        DHL_UpdateScreen();
1033        DST_OS_Free(&src);
1034}
1035
1036#else
1037
1038//extern "C" DS_U8 MsOS_Dcache_Flush(DS_U32 u32Start, DS_U32 u32Size);
1039
1040void DHL_GFX_Open()
1041{
1042        T();
1043        DHL_RESULT DHL_GrpInit();
1044        DHL_GrpInit();
1045        T();
1046}
1047
1048void DHL_GFX_Draw(OSD_PIXEL_T *src, int x, int y, int w, int h)
1049{
1050        void dhl_draw_image2(char* src, int x, int y, int w, int h);
1051        dhl_draw_image2((char*)src, x, y, w, h);
1052}
1053
1054void DHL_SetLED(bool bRed, bool bGreen)
1055{
1056        static bool bFirst = true;
1057        static bool bRedOld = false;
1058        static bool bGreenOld = false;
1059        if (bFirst == true || bRedOld != bRed || bGreenOld != bGreen)
1060        {
1061                void dhl_led_control2(DS_U32 onoff);
1062                DS_U32 value = (bGreen ? 2 : 0) + (bRed ? 1: 0);
1063                {
1064                        #include "bsettop_rfm.h"
1065                        extern brfm_t   g_sys_rfm;
1066                        buser_AOV_input_LED(g_sys_rfm, value);
1067                }       
1068                bFirst = false;
1069                bRedOld = bRed;
1070                bGreenOld =  bGreen;
1071        }
1072}
1073#endif
1074
1075char *DST_GetBoardVersion(void)
1076{
1077        char *strVersion = (char*)"R0.1";
1078#ifndef DSTAR
1079        // TO DO GPIO µîÀ» Àо H/W ¹öÀü ¹®ÀÚ¿­ ¹Ýȯ
1080#endif
1081        return strVersion;
1082}
1083
1084void DHL_SetCh34(DS_U8 nVal)
1085{
1086#ifndef DSTAR
1087        DHL_SetRFM34(nVal ? 1 : 0);
1088#endif
1089}
1090
1091#if 0
1092____IR___()
1093#endif
1094
1095static DS_U8 g_KeyPadGPIO = 0;
1096DS_U8 DHL_GetKeyPadGPIO()
1097{
1098#ifndef DSTAR
1099        // TO DO
1100        // KEY PAD GPIO¸¦ Àоî¿Â´Ù
1101        // POWER = 1 UP  =2 DN =4
1102// TODO µ¿ÀÛ È®ÀÎ ÇÊ¿ä
1103//      g_KeyPadGPIO = DHL_SYS_ReadGPIO(7) ? 1:0;
1104//      g_KeyPadGPIO += DHL_SYS_ReadGPIO(GPIO_CH_UP/*6*/) ? 2: 0;
1105//      g_KeyPadGPIO += DHL_SYS_ReadGPIO(GPIO_CH_DN/*5*/) ? 4: 0;
1106#endif
1107        return g_KeyPadGPIO;
1108}
1109
1110#ifdef DSTAR
1111// DSTAR¿¡¼­ NEC ۸¦ Èä³»³»±â À§ÇÑ Å½ºÅ© 110ms°£°ÝÀ¸·Î ¸®ÇÍŰ ó¸®ÇÑ´Ù.
1112static DS_U32 DSTAR_KEY_MsgQ = 0;
1113typedef struct 
1114{
1115        DS_U32 Code;
1116        DS_U32 bPressed;
1117        DS_U32 tick;
1118} DSTAR_KEY;
1119
1120static void tRepeatKey()
1121{
1122        DSTAR_KEY msg ={0,0,0};
1123        DS_U32 retLen = 0;
1124        DS_U32 tick = 0;
1125        while (1)
1126        {
1127//              T();
1128                DST_OS_Delay(10); // 10ms
1129                if (DSTAR_KEY_MsgQ == 0) continue;
1130                DSTAR_KEY tmp ={0,0,0};
1131                if (DST_OS_ReceiveMessage_NoWait(DSTAR_KEY_MsgQ, (DS_U32*)&tmp, sizeof(DSTAR_KEY), &retLen) == noError)
1132                {
1133                        msg = tmp;
1134                        if (msg.bPressed) 
1135                        {
1136                                if (g_IrCallBack) g_IrCallBack(msg.Code, 0, DST_OS_GetTickCount());
1137                        }
1138                        tick = DST_OS_GetTickCount();
1139                }
1140                if (msg.bPressed == 0) continue;
1141                if (tick > DST_OS_GetTickCount()) tick = DST_OS_GetTickCount();
1142                if (DST_OS_GetTickCount() - tick < DST_OS_GetTicksPerSecond()/10) continue;
1143                if (msg.bPressed) 
1144                {
1145                        if (g_IrCallBack) g_IrCallBack(msg.Code, 1, DST_OS_GetTickCount());
1146                }
1147                tick = DST_OS_GetTickCount();
1148        }
1149}
1150
1151// DSTAR Ű Äݹé
1152static void DSTAR_KeyCallBack(int bRemocon, DS_U32 Code, int bPressed)
1153{
1154        DST_Printf("|%s| code = 0x%02X, bPressed = %d\n", __func__, (int)Code, bPressed);
1155        static bool bStart = true;
1156        if (bStart == true)
1157        {
1158                bStart = false;
1159                DSTAR_KEY_MsgQ = DST_OS_CreateMessageQueue("DSTAR_KEY", 0, 100, sizeof(DSTAR_KEY));
1160                DST_OS_SpawnTask((void(*)(void *))tRepeatKey, (char*)"DSTAR_KEY", APP_TASK_PRIO_WIN_MGR, WIN_MGR_TASK_STACKSIZE,  0);
1161}
1162        // ŰÆÐµå¸¦ Èä³»³»±â À§ÇÑ Å°
1163        // Home + -
1164        if (Code == 0xCAFE0116 || Code == 0xCAFE010E || Code ==0xCAFE010D)
1165        {
1166                // DSTAR ŰÆÐµå
1167                if (bPressed == 1)
1168                {
1169                        if (Code ==0xCAFE0116) g_KeyPadGPIO = 1;// home key
1170                        if (Code ==0xCAFE010E) g_KeyPadGPIO = 2;// +
1171                        if (Code ==0xCAFE010D) g_KeyPadGPIO = 4;// -
1172                }
1173                else
1174                {
1175                        g_KeyPadGPIO = 0;
1176                }
1177        }
1178        else
1179        {
1180                DSTAR_KEY msg;
1181                msg.Code = Code;
1182                msg.bPressed = bPressed;
1183                msg.tick = DST_OS_GetTickCount();
1184                DST_OS_SendMessage(DSTAR_KEY_MsgQ, (DS_U32 *)&msg, sizeof(DSTAR_KEY));
1185        }
1186}
1187#else
1188
1189static DS_U32 g_ir_code = 0;
1190static DS_U32 g_ir_repeat = 0;
1191static void tIRTask()
1192{
1193        while (1)
1194        {
1195                DST_OS_Delay(10);
1196                if (g_ir_code == 0) continue;
1197                DS_U32 ir_code = g_ir_code;
1198                DS_U32 ir_repeat = g_ir_repeat;
1199                g_ir_code = 0;
1200                g_ir_repeat = 0;
1201                DST_g_KeyPressTime = DST_OS_GetTickCount();
1202                if (g_IrCallBack) g_IrCallBack(ir_code, ir_repeat, DST_g_KeyPressTime);
1203        }
1204}
1205static void IRCallback(unsigned int code, unsigned int repeat)
1206{
1207        int i = 0;
1208        unsigned int _code = 0;
1209        for (i = 0; i < 32; i++) // ºñÆ® ¹ÝÀü
1210        {
1211                _code |= ((code >> i) & 0x1) << (31-i);
1212        }
1213        g_ir_code = _code;
1214        g_ir_repeat = (repeat == 85) ? 0 : 1;
1215}
1216
1217#endif
1218
1219
1220#if 0
1221____OS___()
1222#endif
1223
1224DS_U32 DST_OS_GetTickCount()
1225{
1226        static DS_U32 sema4 = 0;
1227        if (sema4== 0) sema4 = DST_OS_CreateLock((char*)"tick");
1228        DST_OS_Lock(sema4);
1229        static unsigned int old_tick = 0;
1230    static unsigned int loop = 0;
1231#ifdef DSTAR
1232        static unsigned start_tick = 0;
1233        if (start_tick == 0) start_tick = OS_GetTickCount();
1234    unsigned int tick = (OS_GetTickCount() - start_tick) & 0xFFFF;
1235#else
1236    unsigned int tick = (osi_gettickcount() >> 1) & 0xFFFF;
1237#endif
1238    if (old_tick > tick) loop += 0x10000;
1239    old_tick = tick;
1240    unsigned int ret = loop  + tick;
1241    DST_OS_Unlock(sema4);
1242    return ret;
1243}
1244
1245void DST_OS_Delay(DS_U32 ms)
1246{
1247#ifdef DSTAR
1248        if (ms < 10) ms = 10;
1249        OS_Delay(ms/10);
1250#else
1251        DHL_OS_Delay(ms);
1252#endif
1253}
1254
1255DS_U32 DST_OS_GetTicksPerSecond()
1256{
1257#ifdef DSTAR
1258        return OS_GetTicksPerSecond();
1259#else
1260        return 100;
1261#endif
1262}
1263
1264void DST_OS_GetRunTime(DS_U32 *hour, DS_U32 *min, DS_U32 *sec)
1265{
1266        DS_U32 n_sec = DST_OS_GetTickCount() / DST_OS_GetTicksPerSecond();
1267        DS_U32 n_min = n_sec / 60;
1268        DS_U32 n_hour = n_min / 60;
1269        *sec = n_sec % 60;
1270        *min = n_min % 60;
1271        *hour = n_hour;
1272}
1273
1274DS_U32 DST_OS_SpawnTask (void (*func)(void* ), char *name, DS_U16 prio, DS_U16 stacksize, DS_U32 arg)
1275{
1276#ifdef DSTAR
1277        return OS_SpawnTask((void (*)(DS_U32))func, name, prio, stacksize, arg);
1278#else
1279        return DHL_OS_CreateTask(func, name, prio, stacksize, arg);
1280#endif
1281}
1282
1283void DST_OS_SelfDeleteTask()
1284{
1285#ifdef DSTAR
1286#else
1287        DHL_OS_SelfDeleteTask();
1288#endif
1289}
1290
1291DS_U32 DST_OS_ReceiveMessage_NoWait(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen)
1292{
1293#ifdef DSTAR
1294        return OS_ReceiveMessage_NoWait(qId, msgBuf, maxLen, retLen);
1295#else
1296        return DHL_OS_ReceiveMessage((DHL_OS_MSGQ_ID)qId, msgBuf, 0);
1297#endif
1298}
1299
1300DS_U32 DST_OS_ReceiveMessage_Wait(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen, DS_U32 timeOut)
1301{
1302#ifdef DSTAR
1303        if (timeOut < 10) timeOut = 10;
1304        return OS_ReceiveMessage_Wait(qId, msgBuf, maxLen, retLen, timeOut/10);
1305#else
1306        return DHL_OS_ReceiveMessage((DHL_OS_MSGQ_ID)qId, msgBuf, timeOut == 0 ? DHL_TIMEOUT_FOREVER : timeOut);
1307#endif
1308}
1309
1310DS_U32 DST_OS_SendMessage (DS_U32 qId, DS_U32 *pBuffer, DS_U32 nBytes)
1311{
1312#ifdef DSTAR
1313        return OS_SendMessage(qId, pBuffer, nBytes);
1314#else
1315        return DHL_OS_SendMessage((DHL_OS_MSGQ_ID)qId, pBuffer, nBytes);
1316#endif
1317}
1318
1319DS_U32 DST_OS_DeleteMessageQueue(DS_U32 qId)
1320{
1321#ifdef DSTAR
1322        return OS_DeleteMessageQueue(qId);
1323#else
1324        DHL_OS_DeleteMessageQueue((DHL_OS_MSGQ_ID)qId);
1325        return 0;
1326#endif
1327}
1328
1329DS_U32 DST_OS_CreateMessageQueue (const char *name, DS_U32 option, DS_U32 maxMessage, DS_U32 messageLength)
1330{
1331#ifdef DSTAR
1332        return OS_CreateMessageQueue (name, option, maxMessage, messageLength);
1333#else
1334        return (DS_U32)DHL_OS_CreateMessageQueue (name, option, maxMessage, messageLength);
1335#endif
1336}
1337
1338DS_U32 DST_OS_CreateLock(char *name)
1339{
1340#ifdef DSTAR
1341        return OS_CreateMutex(name);
1342#else
1343        return (DS_U32)DHL_OS_CreateMutexSemaphore(name);
1344#endif
1345}
1346
1347void DST_OS_Lock(DS_U32 id)
1348{
1349#ifdef DSTAR
1350        OS_TakeMutex(id);
1351#else
1352        DHL_OS_TakeSemaphore((void*)id, DHL_TIMEOUT_FOREVER);
1353#endif
1354}
1355
1356void DST_OS_Unlock(DS_U32 id)
1357{
1358#ifdef DSTAR
1359        OS_GiveMutex(id);
1360#else
1361        DHL_OS_GiveSemaphore((void*)id);
1362#endif
1363}
1364
1365#if 0
1366____MEMORY___()
1367#endif
1368
1369// LEVEL 0 Æ÷ÆÃ ·¹À̾î
1370// Ç÷§Æû¿¡ µû¶ó Æ÷ÆÃÇØ¾ß ÇÏ´Â ·¹À̾î
1371// malloc°ú free´Â ¿©±â¿¡¼­ Æ÷ÆÃÇÑ´Ù.
1372
1373// LEVEL 1 C Ç¥ÁØÇÔ¼ö ±¸Çö
1374// malloc free calloc realloc À» C ÀÎÅÍÆäÀ̽º¿Í µ¿ÀÏÇÏ°Ô ±¸Çö
1375// freetype sqlite µî Æ÷ÆÃ½Ã¿¡ È£Ãâ
1376// C Ç¥ÁØÇÔ¼ö¿Í ȣȯµÇ´Â ¸Þ¸ð¸® ÇÔ¼ö (¶óÀ̺귯¸® Æ÷ÆÃ¿ë)
1377// C Æ÷ÆÃÀ» À§ÇØ extern "C" ·Î ¼±¾ð
1378
1379void *DST_OS_MallocDirect(int size) // malloc
1380{
1381        if (size <= 0) return 0;
1382        void *p = OS_Malloc(size);
1383        memset(p, 0, size);
1384        return p;
1385}
1386
1387void *DST_OS_CallocDirect(int num, int size) // calloc
1388{
1389        return DST_OS_MallocDirect(num * size);
1390}
1391
1392void DST_OS_FreeDirect( void *ptr) // free
1393{
1394        OS_FreeDirect(ptr);
1395}
1396
1397void *DST_OS_ReallocDirect(void *ptr, int size) // realloc
1398{
1399    if (!ptr) return DST_OS_MallocDirect(size);
1400    void *nptr = DST_OS_MallocDirect(size);
1401    if (!nptr) return NULL;
1402    memcpy(nptr,ptr,size);
1403    DST_OS_FreeDirect(ptr);
1404    return nptr;
1405}
1406
1407
1408
1409// LEVEL 2 APP È£Ãâ ÇÔ¼ö ±¸Çö
1410// ¸Þ¸ð¸® ¸®½ºÆ®¸¦ »ý¼ºÇؼ­ È®ÀÎÇÒ ¼ö ÀÖ´Ù.
1411
1412#define MAX_FUNC_NAME 32 // Çã¿ëÇÒ ÃÖ´ë ÇÔ¼ö À̸§ ±æÀÌ
1413#define MAX_COUNT 500 // °ü¸®ÇÒ °¹¼ö
1414
1415typedef struct
1416{
1417        char func[MAX_FUNC_NAME+1];
1418        int nLine;
1419        void *p;
1420        int nSize;
1421        unsigned int tick;
1422} MEM_LIST;
1423
1424static MEM_LIST memlist[MAX_COUNT];
1425
1426void DST_OS_PrintMemUnit(void)
1427{
1428        int i = 0, nSum = 0, nCount = 0;
1429        DST_Printf("|------------+----------+----------------------------------+------|\n");
1430        DST_Printf("|   Address  |    Size  |             Function             | Line |\n");
1431        DST_Printf("|------------+----------+----------------------------------+------|\n");
1432        for (i = 0; i < MAX_COUNT; i++)
1433        {
1434                if (memlist[i].p == 0) continue;
1435                DST_Printf("| 0x%08X | %8d | %32s | %4d | %4d |\n", (int)memlist[i].p, memlist[i].nSize, memlist[i].func, memlist[i].nLine, (int)(DST_OS_GetTickCount() - memlist[i].tick)/100 );
1436                nSum+=memlist[i].nSize;
1437                nCount++;
1438        }
1439        DST_Printf("|------------+----------+----------------------------------+------|\n");
1440        DST_Printf("| %10d | %8d |                                         |\n", nCount, nSum);
1441        DST_Printf("|------------+----------+----------------------------------+------|\n");
1442}
1443
1444static void Add_MemUint(const char* func, int nLine, void *p, int nSize)
1445{
1446        int i = 0;
1447        for (i = 0; i < MAX_COUNT; i++)
1448        {
1449                if (memlist[i].p != 0) continue;
1450                if (strlen(func) > MAX_FUNC_NAME)
1451                {
1452                        memcpy(memlist[i].func, func, MAX_FUNC_NAME);
1453                        memlist[i].func[MAX_FUNC_NAME] = 0;
1454                }
1455                else
1456                {
1457                        strcpy(memlist[i].func, func);
1458                }
1459                memlist[i].nLine = nLine;
1460                memlist[i].p = p;
1461                memlist[i].nSize = nSize;
1462                memlist[i].tick = DST_OS_GetTickCount();
1463                break;
1464        }
1465        //Print_MemUnit();
1466}
1467
1468
1469static void Del_MemUnit(void *p, const char * func, int nLine)
1470{
1471        int i = 0;
1472        if (p == 0) return;
1473        for (i = 0; i < MAX_COUNT; i++)
1474        {
1475                if (memlist[i].p != p) continue;
1476                memlist[i].p = 0;
1477                break;
1478        }
1479        if (i >= MAX_COUNT)
1480        {
1481#if 0
1482                DST_Printf("\n\n\n\nTry to delete unallocated memory 0x%08X. %s %d\n\n\n", (int)p, func, nLine);
1483                Print_All_MemUnit();
1484                DST_OS_Delay(5);
1485#endif
1486        }
1487        //Print_MemUnit();
1488}
1489
1490void *_DST_OS_Malloc(unsigned int size, const char* func, int nLine)
1491{
1492        void *p = 0;
1493        if (size == 0) return 0;
1494        p = DST_OS_MallocDirect(size);
1495        if (p == 0)
1496        {
1497                DST_Printf("%s|%s|%d|size =%d|Out of memory\n", __func__, func, nLine, size);
1498                DST_OS_PrintMemUnit();
1499        }
1500        else
1501        {
1502                Add_MemUint(func, nLine, p, size);
1503        }
1504        return p;
1505}
1506
1507void *_DST_OS_Calloc(unsigned int count, unsigned int size, const char* func, int nLine)
1508{
1509        void *p = _DST_OS_Malloc(count * size, func, nLine);
1510        if (!p) return 0;
1511        memset(p, 0, count*size);
1512        return p;
1513}
1514
1515void _DST_OS_Free(void *where, const char* func, int nLine)
1516{
1517        if (where == 0) return;
1518        Del_MemUnit(where, func, nLine);
1519        DST_OS_FreeDirect(where);
1520        where = 0;
1521}
1522
1523#if 0
1524____POWER___()
1525#endif
1526
1527void DHL_SYS_Off()
1528{
1529#ifdef DSTAR
1530        DHL_SYS_Terminate();
1531        exit(1);
1532#else
1533        DHL_SYS_Reset(eDHL_RESET_Standby);
1534#endif
1535}
1536
1537void DHL_SYS_Reboot()
1538{
1539#ifdef DSTAR
1540        DHL_SYS_Reset();
1541        exit(1);
1542#else
1543        DHL_SYS_Reset(eDHL_RESET_Reboot);
1544#endif
1545}
1546
1547// Video Ãâ·Â On / OFF
1548void DHL_POWER_Display(bool bOn)
1549{
1550        static bool bOn_Old = true;
1551        if (bOn == bOn_Old) return;
1552        if (bVideoStart == true) return; // ºñµð¿À ½ÃÀ۽ÿ¡´Â µ¿ÀÛÇÏÁö ¾Êµµ·Ï ÇÑ´Ù
1553        bOn_Old = bOn;
1554        T();
1555        DST_Printf("%s %s\n", __func__, bOn ? "On" : "Off");
1556#ifdef DSTAR
1557#else
1558        void DHL_AV_DispStop(BOOL onoff);
1559        DHL_AV_DispStop(bOn ? 0 : 1);
1560        DHL_RESULT DHL_SetLTO(BOOL onoff);
1561        DHL_SetLTO(bOn ? 0 : 1);
1562#endif
1563        g_power_display = bOn;
1564}
1565
1566// Flash Memory¿¡¼­ Bank ¹øÈ£¸¦ Àоî¿Â´Ù.
1567// Flash ¸Þ¸ð¸®ÀÇ Ã¹¹øÂ° ¹ÙÀÌÆ®°¡ 1ÀÌ ¾Æ´Ï¸é Bank0 1À̸é Bank1ÀÌ´Ù.
1568static DS_U8 g_BankNumber = 0xFF;
1569// Return °ªÀÌ 0À̸é Bank0 1À̸é Bank1
1570bool DHL_INFO_GetBankNumber()
1571{
1572        if (g_BankNumber == 0xFF)
1573        {
1574                DS_U8* buff = DHL_Flash_Read(FLASH_BANK_SELECT_POS, 1);
1575                g_BankNumber = (buff[0]==1) ? 1 : 0;
1576                DST_OS_Free(&buff);
1577        }
1578        return g_BankNumber ==1 ? 1 : 0;
1579}
1580
1581void DHL_INFO_SetBankNumber(DS_U8 data)
1582{
1583        if (FLASH_TOTAL_SIZE < 8*1024*1024) data = 0; // 4MB FlashÀÇ Bank´Â Ç×»ó 0ÀÌ´Ù
1584        if (g_BankNumber == data) return;
1585        DHL_Flash_Write(FLASH_BANK_SELECT_POS, 1, & data);
1586        DST_Printf("%s %d\n", __func__, data);
1587        g_BankNumber = data;
1588}
1589
1590// Flash Memory¿¡¼­ LAST POWER STATE¸¦ Àоî¿Â´Ù.
1591// Flash ¸Þ¸ð¸®ÀÇ Ã¹¹øÂ° ¹ÙÀÌÆ®°¡ 0À̸é OFF »óÅ 0ÀÌ ¾Æ´Ï¸é ONÀÌ´Ù.
1592static DS_U8 g_LastPowerState = 0xFF;
1593bool DHL_INFO_GetLastPowerState()
1594{
1595        if (g_LastPowerState == 0xFF)
1596        {
1597                DS_U8* buff = DHL_Flash_Read(FLASH_LAST_POWER_POS, 1);
1598                g_LastPowerState = (buff[0]!=0) ? 1 : 0;
1599                DST_OS_Free(&buff);
1600        }
1601        return g_LastPowerState ? true : false;
1602}
1603
1604void DHL_INFO_SetLastPowerState(bool bOn)
1605{
1606        DS_U8 data = bOn ? 1 : 0;
1607        if (g_LastPowerState == data) return;
1608//      data = 1; // JUST TEST
1609        DHL_Flash_Write(FLASH_LAST_POWER_POS, 1, & data);
1610        DST_Printf("%s %d\n", __func__, data);
1611        g_LastPowerState = data;
1612}
1613
1614#if 0
1615____Flash_Memory___()
1616#endif
1617
1618static void FlashLock(bool bLock)
1619{
1620        static DS_U32 sema4 = 0;
1621        if (sema4== 0) 
1622        {
1623#ifndef DSTAR
1624                // DHL_NVM_Init(); // DHL_SYS_PlatformInit ¿¡¼­ È£ÃâÇϹǷΠÁ¦°Å
1625#endif
1626                sema4 = DST_OS_CreateLock((char*)"flash");
1627        }
1628        bLock ? DST_OS_Lock(sema4) : DST_OS_Unlock(sema4);
1629}
1630
1631DS_U32 FLASH_TOTAL_SIZE =  (8*1024*1024);
1632DS_U32 FLASH_BANK1_POS = (0x400000);
1633
1634// 4Kbyte ¼½ÅÍ ´ÜÀ§·Î Write 0~1023 ±îÁö ¼½Å͹øÈ£
1635static void _FlashWrite(DS_U16 nSector, DS_U16 nSize, DS_U8* data)
1636{
1637#ifndef DSTAR
1638        DHL_RESULT DHL_NVM_FlashWrite(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len);
1639        if (DHL_NVM_FlashWrite(0, nSector * 4096, data, nSize) != 0)
1640        {
1641                DST_Printf("|%s|Err|DHL_NVM_FlashWrite(addr:0x%X, len:%d)\n",__func__, nSector * 4096, nSize);
1642        }
1643#else
1644        if (nSector*4096 + nSize >= FLASH_TOTAL_SIZE)
1645        {
1646                T();
1647                exit(1);
1648        }
1649        FILE *fp = fopen("flash.bin" , "rb+");
1650        fseek(fp, nSector * 4096, SEEK_SET);
1651        fwrite(data, 1, nSize, fp);
1652        fclose(fp);
1653#endif
1654}
1655
1656// address´Â 4Kbyte ´ÜÀ§°ªÀ¸·Î ½ÃÀÛÇØ¾ß ÇÑ´Ù.
1657// nSize´Â °¡´ÉÇÑ ±æÀÌ·Î Á¦ÇÑÀÌ ¾ø´Ù.
1658void DHL_Flash_Write(DS_U32 address, DS_U32 nSize, DS_U8 *data)
1659{
1660        if (address%4096 != 0)
1661        {
1662                DST_Printf("%s|invalid address = %d(0x%X) nSize = %d\n", __func__, (int)address, (int)address, (int)nSize);
1663                return;
1664        }
1665        FlashLock(true);
1666        // 4Kbyte ´ÜÀ§·Î ³ª´©¾î¼­ È£ÃâÇÑ´Ù.
1667        DS_U32 i;
1668        for ( i =  0; i < nSize; i+= 4096)
1669        {
1670                DS_U32 nSize2 = 4096;
1671                if (i+nSize2 > nSize) nSize2 = nSize - i;
1672                _FlashWrite((address + i)/4096, nSize2, &data[i]);
1673        }
1674        FlashLock(false);
1675}
1676
1677// È£ÃâÇÑ ÂÊ¿¡¼­ ¸Þ¸ð¸® ÇØÁ¦ ÇÊ¿ä
1678// Ç×»ó Writeº¸´Ù ¸ÕÀú È£ÃâµÇ¹Ç·Î ÃʱâÈ­ Äڵ嵵 Æ÷ÇÔÇÑ´Ù.
1679DS_U8* DHL_Flash_Read(DS_U32 address, DS_U32 nSize)
1680{
1681        FlashLock(true);
1682        DS_U8* data = (DS_U8*)DST_OS_Malloc(nSize);
1683#ifdef DSTAR
1684        if (address + nSize >= FLASH_TOTAL_SIZE)
1685        {
1686                T();
1687                exit(1);
1688        }
1689        // ÆÄÀÏÀÌ ¾øÀ¸¸é »ý¼ºÇÑ´Ù.
1690        FILE *fp = fopen("flash.bin" , "rb");
1691        int nFileLength = 0;
1692        if (fp)
1693        {
1694                fseek(fp, 0, SEEK_END);
1695                nFileLength = ftell(fp);
1696                fclose(fp);
1697        }
1698        if (nFileLength != FLASH_TOTAL_SIZE)
1699        {
1700                fp = fopen("flash.bin", "wb");
1701                DS_U8 *tmp = (DS_U8*)DST_OS_Malloc(FLASH_TOTAL_SIZE);
1702                memset(tmp,0xFF, FLASH_TOTAL_SIZE);
1703                fwrite(tmp, 1, FLASH_TOTAL_SIZE, fp);
1704                DST_OS_Free(&tmp);
1705                fclose(fp);
1706        }
1707        fp = fopen("flash.bin" , "rb");
1708        fseek(fp, address, SEEK_SET);
1709        fread(data, 1, nSize, fp);
1710        fclose(fp);
1711#else
1712        // TO DO Flash Init DHL_SYS_PlatformInit¿¡¼­ ÀÌ¹Ì È£Ãâ
1713        // TO DO Flash Read
1714        DHL_RESULT DHL_NVM_FlashRead(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len);
1715        if (DHL_NVM_FlashRead(0, address, data, nSize) != 0)
1716        {
1717                DST_Printf("|%s|Err|DHL_NVM_FlashRead(addr:0x%x, len:0x%x)\n",__func__, (int)address, (int)nSize);
1718        }
1719#endif
1720        FlashLock(false);
1721        return data;
1722}
1723
1724
1725// ministd¿¡¼­ Áö¿øÇÏÁö ¾Ê´Â stdc ÇÔ¼ö
1726
1727int dst_atoi(char *st)
1728{
1729        bool bMinus = false;
1730                if (*st == '-' ) 
1731                {
1732                        bMinus = true;
1733                        st++;
1734                }
1735                int i = 0;
1736        while((*st >= '0' && *st <= '9'))
1737        {
1738                i = i * 10 + (*(st)) - '0';
1739                st++;
1740        }
1741        return bMinus ? -i : i;
1742}
1743
1744#ifndef DSTAR
1745void *memchr(s, c, n)
1746        const void *s;
1747        register unsigned char c;
1748        register size_t n;
1749{
1750        if (n != 0) {
1751                register const unsigned char *p = s;
1752
1753                do {
1754                        if (*p++ == c)
1755                                return ((void *)(p - 1));
1756                } while (--n != 0);
1757        }
1758        return (0);
1759}
1760#include <time.h>
1761struct tm * gmtime(register const time_t *timer)
1762{
1763        static struct tm br_time;
1764        return &br_time;
1765}
1766
1767size_t strftime(char *s, size_t maxsize,        const char *format,  const struct tm *t)
1768{
1769        s[0] = 0;
1770        return 0;
1771}
1772#endif
1773
1774
1775#ifdef DSTAR // CC FeedingÀ» À§ÇÑ Å×½ºÆ® ÇÔ¼ö
1776#include <stdlib.h>
1777#define SIZE_BUFF (1024*32)
1778static void ttest__cc()
1779{
1780        FILE *fp = fopen("/home/megakiss/stream/KBS.tp", "rb");
1781        if (fp == 0) return;
1782        while(1)
1783        {
1784                OS_Delay(OS_GetTicksPerSecond()/100);
1785                DS_U8 buff[SIZE_BUFF*2];
1786                int n = fread(buff, 1,SIZE_BUFF, fp);
1787                if (n != SIZE_BUFF)
1788                {
1789                        fclose(fp);
1790                        return;
1791                } 
1792                _DHL_CCCallBack( SIZE_BUFF, buff);
1793        }
1794}
1795
1796static void ttest_cc()
1797{
1798        while (1)
1799        {
1800                OS_Delay(OS_GetTicksPerSecond());
1801                ttest__cc();
1802        }
1803}
1804
1805void test_cc()
1806{
1807        static int taskID = 0;
1808        if (taskID) return;
1809        taskID = OS_SpawnTask((void (*)(DS_U32)) ttest_cc, (char*)"ttest_cc", APP_TASK_PRIO_WIN_MGR, WIN_MGR_TASK_STACKSIZE, 0);
1810}
1811#endif // CC FeedingÀ» À§ÇÑ Å×½ºÆ® ÇÔ¼ö
Note: See TracBrowser for help on using the repository browser.