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

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

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

File size: 44.1 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, 8160, 0x3C, sectionMode, 0, false); // ¼­°æ¹æ¼Û
804#else // CMB ¿¡¼­ PID ÇÒ´ç¹Þ°í Á¤È®ÇÑ °ªÀ¸·Î ¼öÁ¤ÇØ¾ß ÇÔ
805        //return DHL_SI_Monitor(RF, nRequestID, 0xDF1, 0x3C, sectionMode, 0, false);
806        //return DHL_SI_Monitor(RF, nRequestID, 0x3A2, 0x3C, sectionMode, 0, false); // ±Ý°­¹æ¼Û
807        return DHL_SI_Monitor(RF, nRequestID, 8160, 0x3C, sectionMode, 0, false); // ¼­°æ¹æ¼Û
808#endif
809}
810
811DHL_HANDLE DHL_SI_MonitorSCTE_DII(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid)
812{
813        DST_Printf("%s nRequestID = %d 0x%X\n", __func__, (int)nRequestID, pid);
814        return DHL_SI_Monitor(RF, nRequestID, pid, 0x4B, tableMode, 0, false); // ¼­°æ¹æ¼ÛÀ» À§ÇØ 3B -> 4B·Î º¯°æ
815}
816
817DHL_HANDLE DHL_SI_MonitorSCTE_DDB(DS_U32 RF, DS_U32 nRequestID, DS_U16 pid)
818{
819        DST_Printf("%s nRequestID = %d 0x%X\n", __func__, (int)nRequestID, pid);
820        return DHL_SI_Monitor(RF, nRequestID, pid, 0x4C, sectionMode, 0, false); // ¼­°æ¹æ¼ÛÀ» À§ÇØ 3C -> 4C·Î º¯°æ
821}
822
823DHL_ERROR DHL_SI_MonitorStop(DHL_HANDLE* handle)
824{
825        if (handle == 0 || *handle == 0) return DHL_FAIL_;
826        T();
827        DHL_SI_HANDLE *h = (DHL_SI_HANDLE *)*handle;
828        PSIMultiEventProcData *aProcData = h->aProcData;
829        void **aPsiCtl = h->aPsiCtl;
830#ifdef DSTAR
831        DHL_PSI_CancelMonitor(aPsiCtl[0]);
832#else
833        DHL_PSI_StopMonitor((tDHL_PSI_ControlHandle)aPsiCtl[0]);
834#endif
835        if (aPsiCtl) DST_OS_Free(&aPsiCtl);
836        if (aProcData) DST_OS_Free(&aProcData);
837        DST_OS_Free(&h);
838        *handle = 0;
839        return DHL_OK_;
840}
841
842#if 0
843____Tuner___()
844#endif
845
846// Æ©³Ê ½ÃÀÛ
847// Khz ´ÜÀ§ Á֯ļö
848static DS_U32 tuner_start_tick = 0;
849DHL_ERROR DHL_TUN_Start(int rf, DHL_MODULATION_MODE nMode)
850{
851        int freqkHz = DST_GetMHzFrequencybyIndex(rf) * 1000;
852        if (DST_g_bUseAirFreq)
853        {
854                if (rf == DST_GetIndexByRFNumber(4)) freqkHz = 69 * 1000;
855                if (rf == DST_GetIndexByRFNumber(18)) freqkHz = 497 * 1000;
856                if (rf == DST_GetIndexByRFNumber(48)) freqkHz = 677 * 1000;
857        }
858        DST_Printf("%s|%d|Tune Freq = %d kHz\n", __func__, __LINE__, freqkHz);
859#ifdef DSTAR
860        change_file(rf, 0, 0, 1);
861        TunerDemod demod = DEMOD_8VSB;
862        if (nMode == DHL_MODULATION_64QAM) demod = DEMOD_64QAM;
863        if (nMode == DHL_MODULATION_256QAM) demod = DEMOD_256QAM;
864        DHL_FE_Start(TUNER_FAT, freqkHz, demod, FREQ);
865#else
866        static bool bInit = false;
867        if (bInit == false)
868        {
869                bInit = true;
870                T();
871                DHL_FE_Init();
872                T();
873        }
874        tDHL_Demod demod = eDHL_DEMOD_8VSB;
875#if 0 // ¼­°æ¹æ¼ÛÀº µ¥ÀÌÅÍ Ã¤³Îµµ 8VSBÀ̱⶧¹®¿¡ ¸ðµÎ 8VSB °íÁ¤À¸·Î ÇÔ
876        if (nMode == DHL_MODULATION_64QAM) demod = eDHL_DEMOD_64QAM;
877        if (nMode == DHL_MODULATION_256QAM) demod = eDHL_DEMOD_256QAM;
878#endif
879        T();
880        DHL_FE_Start(0, freqkHz, demod, 0);
881        T();
882#endif
883        tuner_start_tick = DST_OS_GetTickCount();
884        return DHL_OK_;
885}
886
887// Æ©³Ê Á¾·á
888DHL_ERROR DHL_TUN_Stop()
889{
890#ifdef DSTAR
891        DHL_FE_Stop(TUNER_FAT);
892#else
893        DHL_FE_Stop(0);
894#endif
895        return DHL_OK_;
896}
897
898DHL_ERROR DHL_TUNE_Info(DS_U8 RF, int *SignalStrength, bool *bLock, int *nPower, int *nSNR)
899{
900
901#ifdef DSTAR
902        *SignalStrength = rand() % 100;
903        *bLock = true;
904        *nPower = rand() % 100;
905#else
906        int i;
907        static int prev_snr[5];
908//      static int prev_lock = 0;
909        if (tuner_start_tick > DST_OS_GetTickCount()) tuner_start_tick = DST_OS_GetTickCount();
910        if (DST_OS_GetTickCount() - tuner_start_tick < DST_OS_GetTicksPerSecond()/10) // 100ms À̳»¿¡´Â È£ÃâÇÏÁö ¸»ÀÚ
911        {
912                *bLock = false;
913                *SignalStrength = 0;
914                for (i=0; i < 5; i++) prev_snr[i] = -1;
915                prev_lock = 0;
916                return DHL_OK_;
917        }
918
919        int lock = 0, snr = 0, power = 0;
920        DHL_RESULT dhl_fe_get_sig_info_detail(int *lock, int *snr, int* power);
921        dhl_fe_get_sig_info_detail(&lock, &snr, &power);
922        if (prev_lock != lock)
923        {
924                DST_Printf("tuner lock = %d  %d0msec\n", lock, DST_OS_GetTickCount() - tuner_start_tick);
925                prev_lock = lock;
926        }
927//      DST_Printf("power = %d %d0msec\n", power, DST_OS_GetTickCount() - tuner_start_tick);
928        if (lock == 0 || snr > 20000) snr = 0;  // ¿¡·¯°ªÀ¸·Î ¹«½ÃÇÑ´Ù.
929        *nSNR = snr;
930        for (i=0; i < 4; i++) prev_snr[i] = prev_snr[i+1];
931        prev_snr[4] = snr;
932
933        int nValidCount = 0;
934        int nSum = 0;
935        for (i=0; i < 5; i++)
936        {
937                if (prev_snr[i] < 0) continue;
938                nValidCount++;
939                nSum += prev_snr[i];
940        }
941        snr = nSum / nValidCount;
942        // snrÀÇ °ªÀº 0~12000 »çÀÌÀÇ °ªÀÌ´Ù.
943        snr-=2000;
944        if (snr > 12000) snr = 12000;
945        if (snr < 0) snr = 0;
946        snr/=100;
947        *bLock = lock ? true : false;
948        *SignalStrength = snr; // ¿ÀÅ佺ĵ½Ã LOCKÀÌ ¾Æ´Ñ ÀÌ °ªÀÌ 0º¸´Ù Å©¸é ½ÅÈ£°¡ Àִ°ÍÀ¸·Î °£ÁÖÇÑ´Ù.
949        *nPower = power;
950        if (*SignalStrength > 100) *SignalStrength = 100;
951//      DST_Printf("tick = %d\n", DST_OS_GetTickCount() - tuner_start_tick);
952//      DST_Printf("bLock = %d SignalStrength = %d\n", *bLock, *SignalStrength);
953#endif
954        return DHL_OK_;
955}
956
957#if 0
958____GFX___()
959#endif
960
961#ifdef DSTAR
962
963#if OSD_PIXEL_BPP == 16
964#define DS_PIXEL_t DS_U16
965#define PIXEL_FORMAT 10 //PF_RGB16
966#else
967#define DS_PIXEL_t DS_U32
968#define PIXEL_FORMAT 0 //PF_RGB32
969#endif
970
971/*extern "C" */void DHL_GFX_DrawImage( DS_U32 pBufferId, int offsetX, int offsetY, int w, int h, DS_PIXEL_t *pPixels);
972/*extern "C" */void DHL_GFX_UpdateScreen(int Plane);
973static DS_U32 id = 0;
974static DS_U32 gMsgQ = 0;
975
976static void tUpdateScreen()
977{
978        DS_U32 msg = 0;
979        DS_U32 retLen;
980        while (1)
981        {
982                //T();
983                DST_OS_ReceiveMessage_NoWait(gMsgQ, (DS_U32*)&msg, sizeof(DS_U32), &retLen);
984                DHL_GFX_UpdateScreen(0); // µÎ°³ÀÇ Å½ºÅ©¿¡¼­ µ¿½Ã¿¡ È£ÃâÇÏ¸é ½Ã½ºÅÛ ¸ØÃã...
985        }
986}
987
988static void DHL_UpdateScreen()
989{
990        DS_U32 msg = 0;
991        if (gMsgQ) DST_OS_SendMessage(gMsgQ, &msg, sizeof(DS_U32));
992}
993
994void DHL_GFX_Open()
995{
996        static bool bInit = false;
997        if (bInit == false)
998        {
999                DHL_GFX_Init(0,
1000                        0, 0, DST_GetScreenWidth(), DST_GetScreenHeight() + 16,
1001                        DST_GetScreenWidth(), DST_GetScreenHeight() + 16, PIXEL_FORMAT, &id);
1002                bInit = true;
1003                gMsgQ = DST_OS_CreateMessageQueue("qUpdateScreen", 0, 100, sizeof(DS_U32));
1004                /*DS_U32 TaskID = */DST_OS_SpawnTask((void(*)(void *))tUpdateScreen, (char*)"tUpdateScreen", APP_TASK_PRIO_WIN_MGR, WIN_MGR_TASK_STACKSIZE,  0);
1005        }
1006}
1007
1008void DHL_GFX_Draw(OSD_PIXEL_T *src, int x, int y, int w, int h)
1009{
1010        DHL_GFX_DrawImage(id, x, y, w, h, src);
1011        DHL_UpdateScreen();
1012}
1013
1014void DHL_SetLED(bool bRed, bool bGreen)
1015{
1016        static bool old_bRed = false;
1017        static bool old_bGreen = false;
1018
1019        if (bRed == old_bRed && bGreen == old_bGreen) return;
1020        old_bRed = bRed;
1021        old_bGreen = bGreen;
1022        DHL_GFX_Open();
1023        OSD_PIXEL_T *src = (OSD_PIXEL_T *)DST_OS_Calloc(DST_GetScreenWidth() * 16, sizeof(OSD_PIXEL_T));
1024        OSD_PIXEL_T color = 0;
1025        if (bRed)
1026        {
1027                color = bGreen ? CONV32_16(0xFFFFFF00) : CONV32_16(0xFFFF0000);
1028        }
1029        else
1030        {
1031                color = bGreen ? CONV32_16(0xFF00FF00) : CONV32_16(0xFF000000);
1032        }
1033        int i;
1034        for ( i = 0; i < DST_GetScreenWidth() * 16; i++) src[i] = color;
1035        DHL_GFX_DrawImage(id, 0, DST_GetScreenHeight(), DST_GetScreenWidth(), 16, src);
1036        DHL_UpdateScreen();
1037        DST_OS_Free(&src);
1038}
1039
1040#else
1041
1042//extern "C" DS_U8 MsOS_Dcache_Flush(DS_U32 u32Start, DS_U32 u32Size);
1043
1044void DHL_GFX_Open()
1045{
1046        T();
1047        DHL_RESULT DHL_GrpInit();
1048        DHL_GrpInit();
1049        T();
1050}
1051
1052void DHL_GFX_Draw(OSD_PIXEL_T *src, int x, int y, int w, int h)
1053{
1054        void dhl_draw_image2(char* src, int x, int y, int w, int h);
1055        dhl_draw_image2((char*)src, x, y, w, h);
1056}
1057
1058void DHL_SetLED(bool bRed, bool bGreen)
1059{
1060        static bool bFirst = true;
1061        static bool bRedOld = false;
1062        static bool bGreenOld = false;
1063        if (bFirst == true || bRedOld != bRed || bGreenOld != bGreen)
1064        {
1065                void dhl_led_control2(DS_U32 onoff);
1066                DS_U32 value = (bGreen ? 2 : 0) + (bRed ? 1: 0);
1067                {
1068                        #include "bsettop_rfm.h"
1069                        extern brfm_t   g_sys_rfm;
1070                        buser_AOV_input_LED(g_sys_rfm, value);
1071                }
1072                bFirst = false;
1073                bRedOld = bRed;
1074                bGreenOld =  bGreen;
1075        }
1076}
1077#endif
1078
1079char *DST_GetBoardVersion(void)
1080{
1081        char *strVersion = (char*)"R0.1";
1082#ifndef DSTAR
1083        // TO DO GPIO µîÀ» Àо H/W ¹öÀü ¹®ÀÚ¿­ ¹Ýȯ
1084#endif
1085        return strVersion;
1086}
1087
1088void DHL_SetCh34(DS_U8 nVal)
1089{
1090#ifndef DSTAR
1091        DHL_SetRFM34(nVal ? 1 : 0);
1092#endif
1093}
1094
1095#if 0
1096____IR___()
1097#endif
1098
1099static DS_U8 g_KeyPadGPIO = 0;
1100DS_U8 DHL_GetKeyPadGPIO()
1101{
1102#ifndef DSTAR
1103        // TO DO
1104        // KEY PAD GPIO¸¦ Àоî¿Â´Ù
1105        // POWER = 1 UP  =2 DN =4
1106// TODO µ¿ÀÛ È®ÀÎ ÇÊ¿ä
1107//      g_KeyPadGPIO = DHL_SYS_ReadGPIO(7) ? 1:0;
1108//      g_KeyPadGPIO += DHL_SYS_ReadGPIO(GPIO_CH_UP/*6*/) ? 2: 0;
1109//      g_KeyPadGPIO += DHL_SYS_ReadGPIO(GPIO_CH_DN/*5*/) ? 4: 0;
1110#endif
1111        return g_KeyPadGPIO;
1112}
1113
1114#ifdef DSTAR
1115// DSTAR¿¡¼­ NEC ۸¦ Èä³»³»±â À§ÇÑ Å½ºÅ© 110ms°£°ÝÀ¸·Î ¸®ÇÍŰ ó¸®ÇÑ´Ù.
1116static DS_U32 DSTAR_KEY_MsgQ = 0;
1117typedef struct
1118{
1119        DS_U32 Code;
1120        DS_U32 bPressed;
1121        DS_U32 tick;
1122} DSTAR_KEY;
1123
1124static void tRepeatKey()
1125{
1126        DSTAR_KEY msg ={0,0,0};
1127        DS_U32 retLen = 0;
1128        DS_U32 tick = 0;
1129        while (1)
1130        {
1131//              T();
1132                DST_OS_Delay(10); // 10ms
1133                if (DSTAR_KEY_MsgQ == 0) continue;
1134                DSTAR_KEY tmp ={0,0,0};
1135                if (DST_OS_ReceiveMessage_NoWait(DSTAR_KEY_MsgQ, (DS_U32*)&tmp, sizeof(DSTAR_KEY), &retLen) == noError)
1136                {
1137                        msg = tmp;
1138                        if (msg.bPressed)
1139                        {
1140                                if (g_IrCallBack) g_IrCallBack(msg.Code, 0, DST_OS_GetTickCount());
1141                        }
1142                        tick = DST_OS_GetTickCount();
1143                }
1144                if (msg.bPressed == 0) continue;
1145                if (tick > DST_OS_GetTickCount()) tick = DST_OS_GetTickCount();
1146                if (DST_OS_GetTickCount() - tick < DST_OS_GetTicksPerSecond()/10) continue;
1147                if (msg.bPressed)
1148                {
1149                        if (g_IrCallBack) g_IrCallBack(msg.Code, 1, DST_OS_GetTickCount());
1150                }
1151                tick = DST_OS_GetTickCount();
1152        }
1153}
1154
1155// DSTAR Ű Äݹé
1156static void DSTAR_KeyCallBack(int bRemocon, DS_U32 Code, int bPressed)
1157{
1158        DST_Printf("|%s| code = 0x%02X, bPressed = %d\n", __func__, (int)Code, bPressed);
1159        static bool bStart = true;
1160        if (bStart == true)
1161        {
1162                bStart = false;
1163                DSTAR_KEY_MsgQ = DST_OS_CreateMessageQueue("DSTAR_KEY", 0, 100, sizeof(DSTAR_KEY));
1164                DST_OS_SpawnTask((void(*)(void *))tRepeatKey, (char*)"DSTAR_KEY", APP_TASK_PRIO_WIN_MGR, WIN_MGR_TASK_STACKSIZE,  0);
1165}
1166        // ŰÆÐµå¸¦ Èä³»³»±â À§ÇÑ Å°
1167        // Home + -
1168        if (Code == 0xCAFE0116 || Code == 0xCAFE010E || Code ==0xCAFE010D)
1169        {
1170                // DSTAR ŰÆÐµå
1171                if (bPressed == 1)
1172                {
1173                        if (Code ==0xCAFE0116) g_KeyPadGPIO = 1;// home key
1174                        if (Code ==0xCAFE010E) g_KeyPadGPIO = 2;// +
1175                        if (Code ==0xCAFE010D) g_KeyPadGPIO = 4;// -
1176                }
1177                else
1178                {
1179                        g_KeyPadGPIO = 0;
1180                }
1181        }
1182        else
1183        {
1184                DSTAR_KEY msg;
1185                msg.Code = Code;
1186                msg.bPressed = bPressed;
1187                msg.tick = DST_OS_GetTickCount();
1188                DST_OS_SendMessage(DSTAR_KEY_MsgQ, (DS_U32 *)&msg, sizeof(DSTAR_KEY));
1189        }
1190}
1191#else
1192
1193static DS_U32 g_ir_code = 0;
1194static DS_U32 g_ir_repeat = 0;
1195static void tIRTask()
1196{
1197        while (1)
1198        {
1199                DST_OS_Delay(10);
1200                if (g_ir_code == 0) continue;
1201                DS_U32 ir_code = g_ir_code;
1202                DS_U32 ir_repeat = g_ir_repeat;
1203                g_ir_code = 0;
1204                g_ir_repeat = 0;
1205                DST_g_KeyPressTime = DST_OS_GetTickCount();
1206                if (g_IrCallBack) g_IrCallBack(ir_code, ir_repeat, DST_g_KeyPressTime);
1207        }
1208}
1209static void IRCallback(unsigned int code, unsigned int repeat)
1210{
1211        int i = 0;
1212        unsigned int _code = 0;
1213        for (i = 0; i < 32; i++) // ºñÆ® ¹ÝÀü
1214        {
1215                _code |= ((code >> i) & 0x1) << (31-i);
1216        }
1217        g_ir_code = _code;
1218        g_ir_repeat = (repeat == 85) ? 0 : 1;
1219}
1220
1221#endif
1222
1223
1224#if 0
1225____OS___()
1226#endif
1227
1228DS_U32 DST_OS_GetTickCount()
1229{
1230        static DS_U32 sema4 = 0;
1231        if (sema4== 0) sema4 = DST_OS_CreateLock((char*)"tick");
1232        DST_OS_Lock(sema4);
1233        static unsigned int old_tick = 0;
1234    static unsigned int loop = 0;
1235#ifdef DSTAR
1236        static unsigned start_tick = 0;
1237        if (start_tick == 0) start_tick = OS_GetTickCount();
1238    unsigned int tick = (OS_GetTickCount() - start_tick) & 0xFFFF;
1239#else
1240    unsigned int tick = (osi_gettickcount() >> 1) & 0xFFFF;
1241#endif
1242    if (old_tick > tick) loop += 0x10000;
1243    old_tick = tick;
1244    unsigned int ret = loop  + tick;
1245    DST_OS_Unlock(sema4);
1246    return ret;
1247}
1248
1249void DST_OS_Delay(DS_U32 ms)
1250{
1251#ifdef DSTAR
1252        if (ms < 10) ms = 10;
1253        OS_Delay(ms/10);
1254#else
1255        DHL_OS_Delay(ms);
1256#endif
1257}
1258
1259DS_U32 DST_OS_GetTicksPerSecond()
1260{
1261#ifdef DSTAR
1262        return OS_GetTicksPerSecond();
1263#else
1264        return 100;
1265#endif
1266}
1267
1268void DST_OS_GetRunTime(DS_U32 *hour, DS_U32 *min, DS_U32 *sec)
1269{
1270        DS_U32 n_sec = DST_OS_GetTickCount() / DST_OS_GetTicksPerSecond();
1271        DS_U32 n_min = n_sec / 60;
1272        DS_U32 n_hour = n_min / 60;
1273        *sec = n_sec % 60;
1274        *min = n_min % 60;
1275        *hour = n_hour;
1276}
1277
1278DS_U32 DST_OS_SpawnTask (void (*func)(void* ), char *name, DS_U16 prio, DS_U16 stacksize, DS_U32 arg)
1279{
1280#ifdef DSTAR
1281        return OS_SpawnTask((void (*)(DS_U32))func, name, prio, stacksize, arg);
1282#else
1283        return DHL_OS_CreateTask(func, name, prio, stacksize, arg);
1284#endif
1285}
1286
1287void DST_OS_SelfDeleteTask()
1288{
1289#ifdef DSTAR
1290#else
1291        DHL_OS_SelfDeleteTask();
1292#endif
1293}
1294
1295DS_U32 DST_OS_ReceiveMessage_NoWait(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen)
1296{
1297#ifdef DSTAR
1298        return OS_ReceiveMessage_NoWait(qId, msgBuf, maxLen, retLen);
1299#else
1300        return DHL_OS_ReceiveMessage((DHL_OS_MSGQ_ID)qId, msgBuf, 0);
1301#endif
1302}
1303
1304DS_U32 DST_OS_ReceiveMessage_Wait(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen, DS_U32 timeOut)
1305{
1306#ifdef DSTAR
1307        if (timeOut < 10) timeOut = 10;
1308        return OS_ReceiveMessage_Wait(qId, msgBuf, maxLen, retLen, timeOut/10);
1309#else
1310        return DHL_OS_ReceiveMessage((DHL_OS_MSGQ_ID)qId, msgBuf, timeOut == 0 ? DHL_TIMEOUT_FOREVER : timeOut);
1311#endif
1312}
1313
1314DS_U32 DST_OS_SendMessage (DS_U32 qId, DS_U32 *pBuffer, DS_U32 nBytes)
1315{
1316#ifdef DSTAR
1317        return OS_SendMessage(qId, pBuffer, nBytes);
1318#else
1319        return DHL_OS_SendMessage((DHL_OS_MSGQ_ID)qId, pBuffer, nBytes);
1320#endif
1321}
1322
1323DS_U32 DST_OS_DeleteMessageQueue(DS_U32 qId)
1324{
1325#ifdef DSTAR
1326        return OS_DeleteMessageQueue(qId);
1327#else
1328        DHL_OS_DeleteMessageQueue((DHL_OS_MSGQ_ID)qId);
1329        return 0;
1330#endif
1331}
1332
1333DS_U32 DST_OS_CreateMessageQueue (const char *name, DS_U32 option, DS_U32 maxMessage, DS_U32 messageLength)
1334{
1335#ifdef DSTAR
1336        return OS_CreateMessageQueue (name, option, maxMessage, messageLength);
1337#else
1338        return (DS_U32)DHL_OS_CreateMessageQueue (name, option, maxMessage, messageLength);
1339#endif
1340}
1341
1342DS_U32 DST_OS_CreateLock(char *name)
1343{
1344#ifdef DSTAR
1345        return OS_CreateMutex(name);
1346#else
1347        return (DS_U32)DHL_OS_CreateMutexSemaphore(name);
1348#endif
1349}
1350
1351void DST_OS_Lock(DS_U32 id)
1352{
1353#ifdef DSTAR
1354        OS_TakeMutex(id);
1355#else
1356        DHL_OS_TakeSemaphore((void*)id, DHL_TIMEOUT_FOREVER);
1357#endif
1358}
1359
1360void DST_OS_Unlock(DS_U32 id)
1361{
1362#ifdef DSTAR
1363        OS_GiveMutex(id);
1364#else
1365        DHL_OS_GiveSemaphore((void*)id);
1366#endif
1367}
1368
1369#if 0
1370____MEMORY___()
1371#endif
1372
1373// LEVEL 0 Æ÷ÆÃ ·¹À̾î
1374// Ç÷§Æû¿¡ µû¶ó Æ÷ÆÃÇØ¾ß ÇÏ´Â ·¹À̾î
1375// malloc°ú free´Â ¿©±â¿¡¼­ Æ÷ÆÃÇÑ´Ù.
1376
1377// LEVEL 1 C Ç¥ÁØÇÔ¼ö ±¸Çö
1378// malloc free calloc realloc À» C ÀÎÅÍÆäÀ̽º¿Í µ¿ÀÏÇÏ°Ô ±¸Çö
1379// freetype sqlite µî Æ÷ÆÃ½Ã¿¡ È£Ãâ
1380// C Ç¥ÁØÇÔ¼ö¿Í ȣȯµÇ´Â ¸Þ¸ð¸® ÇÔ¼ö (¶óÀ̺귯¸® Æ÷ÆÃ¿ë)
1381// C Æ÷ÆÃÀ» À§ÇØ extern "C" ·Î ¼±¾ð
1382
1383void *DST_OS_MallocDirect(int size) // malloc
1384{
1385        if (size <= 0) return 0;
1386#ifdef DSTAR
1387        void *p = malloc(size);
1388#else
1389        void *p = OS_Malloc(size);
1390#endif
1391
1392        memset(p, 0, size);
1393        return p;
1394}
1395
1396void *DST_OS_CallocDirect(int num, int size) // calloc
1397{
1398        return DST_OS_MallocDirect(num * size);
1399}
1400
1401void DST_OS_FreeDirect( void *ptr) // free
1402{
1403#ifdef DSTAR
1404        free(ptr);
1405#else
1406        OS_FreeDirect(ptr);
1407#endif
1408}
1409
1410void *DST_OS_ReallocDirect(void *ptr, int size) // realloc
1411{
1412    if (!ptr) return DST_OS_MallocDirect(size);
1413    void *nptr = DST_OS_MallocDirect(size);
1414    if (!nptr) return NULL;
1415    memcpy(nptr,ptr,size);
1416    DST_OS_FreeDirect(ptr);
1417    return nptr;
1418}
1419
1420
1421
1422// LEVEL 2 APP È£Ãâ ÇÔ¼ö ±¸Çö
1423// ¸Þ¸ð¸® ¸®½ºÆ®¸¦ »ý¼ºÇؼ­ È®ÀÎÇÒ ¼ö ÀÖ´Ù.
1424
1425#define MAX_FUNC_NAME 32 // Çã¿ëÇÒ ÃÖ´ë ÇÔ¼ö À̸§ ±æÀÌ
1426#define MAX_COUNT 500 // °ü¸®ÇÒ °¹¼ö
1427
1428typedef struct
1429{
1430        char func[MAX_FUNC_NAME+1];
1431        int nLine;
1432        void *p;
1433        int nSize;
1434        unsigned int tick;
1435} MEM_LIST;
1436
1437static MEM_LIST memlist[MAX_COUNT];
1438
1439void DST_OS_PrintMemUnit(void)
1440{
1441        int i = 0, nSum = 0, nCount = 0;
1442        DST_Printf("|------------+----------+----------------------------------+------|\n");
1443        DST_Printf("|   Address  |    Size  |             Function             | Line |\n");
1444        DST_Printf("|------------+----------+----------------------------------+------|\n");
1445        for (i = 0; i < MAX_COUNT; i++)
1446        {
1447                if (memlist[i].p == 0) continue;
1448                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 );
1449                nSum+=memlist[i].nSize;
1450                nCount++;
1451        }
1452        DST_Printf("|------------+----------+----------------------------------+------|\n");
1453        DST_Printf("| %10d | %8d |                                         |\n", nCount, nSum);
1454        DST_Printf("|------------+----------+----------------------------------+------|\n");
1455}
1456
1457static void Add_MemUint(const char* func, int nLine, void *p, int nSize)
1458{
1459        int i = 0;
1460        for (i = 0; i < MAX_COUNT; i++)
1461        {
1462                if (memlist[i].p != 0) continue;
1463                if (strlen(func) > MAX_FUNC_NAME)
1464                {
1465                        memcpy(memlist[i].func, func, MAX_FUNC_NAME);
1466                        memlist[i].func[MAX_FUNC_NAME] = 0;
1467                }
1468                else
1469                {
1470                        strcpy(memlist[i].func, func);
1471                }
1472                memlist[i].nLine = nLine;
1473                memlist[i].p = p;
1474                memlist[i].nSize = nSize;
1475                memlist[i].tick = DST_OS_GetTickCount();
1476                break;
1477        }
1478        //Print_MemUnit();
1479}
1480
1481
1482static void Del_MemUnit(void *p, const char * func, int nLine)
1483{
1484        int i = 0;
1485        if (p == 0) return;
1486        for (i = 0; i < MAX_COUNT; i++)
1487        {
1488                if (memlist[i].p != p) continue;
1489                memlist[i].p = 0;
1490                break;
1491        }
1492        if (i >= MAX_COUNT)
1493        {
1494#if 0
1495                DST_Printf("\n\n\n\nTry to delete unallocated memory 0x%08X. %s %d\n\n\n", (int)p, func, nLine);
1496                Print_All_MemUnit();
1497                DST_OS_Delay(5);
1498#endif
1499        }
1500        //Print_MemUnit();
1501}
1502
1503void *_DST_OS_Malloc(unsigned int size, const char* func, int nLine)
1504{
1505        void *p = 0;
1506        if (size == 0) return 0;
1507        p = DST_OS_MallocDirect(size);
1508        if (p == 0)
1509        {
1510                DST_Printf("%s|%s|%d|size =%d|Out of memory\n", __func__, func, nLine, size);
1511                DST_OS_PrintMemUnit();
1512        }
1513        else
1514        {
1515                Add_MemUint(func, nLine, p, size);
1516        }
1517        return p;
1518}
1519
1520void *_DST_OS_Calloc(unsigned int count, unsigned int size, const char* func, int nLine)
1521{
1522        void *p = _DST_OS_Malloc(count * size, func, nLine);
1523        if (!p) return 0;
1524        memset(p, 0, count*size);
1525        return p;
1526}
1527
1528void _DST_OS_Free(void *where, const char* func, int nLine)
1529{
1530        if (where == 0) return;
1531        Del_MemUnit(where, func, nLine);
1532        DST_OS_FreeDirect(where);
1533        where = 0;
1534}
1535
1536#if 0
1537____POWER___()
1538#endif
1539
1540void DHL_SYS_Off()
1541{
1542#ifdef DSTAR
1543        DHL_SYS_Terminate();
1544        exit(1);
1545#else
1546        int tmbslTDA182I4_SetPowerState(int, int);
1547        tmbslTDA182I4_SetPowerState(0,1); // tmPowerStandby
1548        DHL_SYS_Reset(eDHL_RESET_Standby);
1549#endif
1550}
1551
1552void DHL_SYS_Reboot()
1553{
1554#ifdef DSTAR
1555        DHL_SYS_Reset();
1556        exit(1);
1557#else
1558        DHL_SYS_Reset(eDHL_RESET_Reboot);
1559#endif
1560}
1561
1562// Video Ãâ·Â On / OFF
1563void DHL_POWER_Display(bool bOn)
1564{
1565        static bool bOn_Old = true;
1566        if (bOn == bOn_Old) return;
1567        if (bVideoStart == true) return; // ºñµð¿À ½ÃÀ۽ÿ¡´Â µ¿ÀÛÇÏÁö ¾Êµµ·Ï ÇÑ´Ù
1568        bOn_Old = bOn;
1569        T();
1570        DST_Printf("%s %s\n", __func__, bOn ? "On" : "Off");
1571#ifdef DSTAR
1572#else
1573        void DHL_AV_DispStop(BOOL onoff);
1574        DHL_AV_DispStop(bOn ? 0 : 1);
1575        DHL_RESULT DHL_SetLTO(BOOL onoff);
1576        DHL_SetLTO(bOn ? 0 : 1);
1577#endif
1578        g_power_display = bOn;
1579}
1580
1581// Flash Memory¿¡¼­ Bank ¹øÈ£¸¦ Àоî¿Â´Ù.
1582// Flash ¸Þ¸ð¸®ÀÇ Ã¹¹øÂ° ¹ÙÀÌÆ®°¡ 1ÀÌ ¾Æ´Ï¸é Bank0 1À̸é Bank1ÀÌ´Ù.
1583static DS_U8 g_BankNumber = 0xFF;
1584// Return °ªÀÌ 0À̸é Bank0 1À̸é Bank1
1585bool DHL_INFO_GetBankNumber()
1586{
1587        if (g_BankNumber == 0xFF)
1588        {
1589                DS_U8* buff = DHL_Flash_Read(FLASH_BANK_SELECT_POS, 1);
1590                g_BankNumber = (buff[0]==1) ? 1 : 0;
1591                DST_OS_Free(&buff);
1592        }
1593        return g_BankNumber ==1 ? 1 : 0;
1594}
1595
1596void DHL_INFO_SetBankNumber(DS_U8 data)
1597{
1598        if (FLASH_TOTAL_SIZE < 8*1024*1024) data = 0; // 4MB FlashÀÇ Bank´Â Ç×»ó 0ÀÌ´Ù
1599        if (g_BankNumber == data) return;
1600        DHL_Flash_Write(FLASH_BANK_SELECT_POS, 1, & data);
1601        DST_Printf("%s %d\n", __func__, data);
1602        g_BankNumber = data;
1603}
1604
1605// Flash Memory¿¡¼­ LAST POWER STATE¸¦ Àоî¿Â´Ù.
1606// Flash ¸Þ¸ð¸®ÀÇ Ã¹¹øÂ° ¹ÙÀÌÆ®°¡ 0À̸é OFF »óÅ 0ÀÌ ¾Æ´Ï¸é ONÀÌ´Ù.
1607static DS_U8 g_LastPowerState = 0xFF;
1608bool DHL_INFO_GetLastPowerState()
1609{
1610        if (g_LastPowerState == 0xFF)
1611        {
1612                DS_U8* buff = DHL_Flash_Read(FLASH_LAST_POWER_POS, 1);
1613                g_LastPowerState = (buff[0]!=0) ? 1 : 0;
1614                DST_OS_Free(&buff);
1615        }
1616        return g_LastPowerState ? true : false;
1617}
1618
1619void DHL_INFO_SetLastPowerState(bool bOn)
1620{
1621        DS_U8 data = bOn ? 1 : 0;
1622        if (g_LastPowerState == data) return;
1623//      data = 1; // JUST TEST
1624        DHL_Flash_Write(FLASH_LAST_POWER_POS, 1, & data);
1625        DST_Printf("%s %d\n", __func__, data);
1626        g_LastPowerState = data;
1627}
1628
1629#if 0
1630____Flash_Memory___()
1631#endif
1632
1633static void FlashLock(bool bLock)
1634{
1635        static DS_U32 sema4 = 0;
1636        if (sema4== 0)
1637        {
1638#ifndef DSTAR
1639                // DHL_NVM_Init(); // DHL_SYS_PlatformInit ¿¡¼­ È£ÃâÇϹǷΠÁ¦°Å
1640#endif
1641                sema4 = DST_OS_CreateLock((char*)"flash");
1642        }
1643        bLock ? DST_OS_Lock(sema4) : DST_OS_Unlock(sema4);
1644}
1645
1646DS_U32 FLASH_TOTAL_SIZE =  (8*1024*1024);
1647DS_U32 FLASH_BANK1_POS = (0x400000);
1648
1649// 4Kbyte ¼½ÅÍ ´ÜÀ§·Î Write 0~1023 ±îÁö ¼½Å͹øÈ£
1650static void _FlashWrite(DS_U16 nSector, DS_U16 nSize, DS_U8* data)
1651{
1652#ifndef DSTAR
1653        DHL_RESULT DHL_NVM_FlashWrite(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len);
1654        if (DHL_NVM_FlashWrite(0, nSector * 4096, data, nSize) != 0)
1655        {
1656                DST_Printf("|%s|Err|DHL_NVM_FlashWrite(addr:0x%X, len:%d)\n",__func__, nSector * 4096, nSize);
1657        }
1658#else
1659        if (nSector*4096 + nSize >= FLASH_TOTAL_SIZE)
1660        {
1661                T();
1662                exit(1);
1663        }
1664        FILE *fp = fopen("flash.bin" , "rb+");
1665        fseek(fp, nSector * 4096, SEEK_SET);
1666        fwrite(data, 1, nSize, fp);
1667        fclose(fp);
1668#endif
1669}
1670
1671// address´Â 4Kbyte ´ÜÀ§°ªÀ¸·Î ½ÃÀÛÇØ¾ß ÇÑ´Ù.
1672// nSize´Â °¡´ÉÇÑ ±æÀÌ·Î Á¦ÇÑÀÌ ¾ø´Ù.
1673void DHL_Flash_Write(DS_U32 address, DS_U32 nSize, DS_U8 *data)
1674{
1675        if (address%4096 != 0)
1676        {
1677                DST_Printf("%s|invalid address = %d(0x%X) nSize = %d\n", __func__, (int)address, (int)address, (int)nSize);
1678                return;
1679        }
1680        FlashLock(true);
1681        // 4Kbyte ´ÜÀ§·Î ³ª´©¾î¼­ È£ÃâÇÑ´Ù.
1682        DS_U32 i;
1683        for ( i =  0; i < nSize; i+= 4096)
1684        {
1685                DS_U32 nSize2 = 4096;
1686                if (i+nSize2 > nSize) nSize2 = nSize - i;
1687                _FlashWrite((address + i)/4096, nSize2, &data[i]);
1688        }
1689        FlashLock(false);
1690}
1691
1692// È£ÃâÇÑ ÂÊ¿¡¼­ ¸Þ¸ð¸® ÇØÁ¦ ÇÊ¿ä
1693// Ç×»ó Writeº¸´Ù ¸ÕÀú È£ÃâµÇ¹Ç·Î ÃʱâÈ­ Äڵ嵵 Æ÷ÇÔÇÑ´Ù.
1694DS_U8* DHL_Flash_Read(DS_U32 address, DS_U32 nSize)
1695{
1696        FlashLock(true);
1697        DS_U8* data = (DS_U8*)DST_OS_Malloc(nSize);
1698#ifdef DSTAR
1699        if (address + nSize >= FLASH_TOTAL_SIZE)
1700        {
1701                T();
1702                exit(1);
1703        }
1704        // ÆÄÀÏÀÌ ¾øÀ¸¸é »ý¼ºÇÑ´Ù.
1705        FILE *fp = fopen("flash.bin" , "rb");
1706        int nFileLength = 0;
1707        if (fp)
1708        {
1709                fseek(fp, 0, SEEK_END);
1710                nFileLength = ftell(fp);
1711                fclose(fp);
1712        }
1713        if (nFileLength != FLASH_TOTAL_SIZE)
1714        {
1715                fp = fopen("flash.bin", "wb");
1716                DS_U8 *tmp = (DS_U8*)DST_OS_Malloc(FLASH_TOTAL_SIZE);
1717                memset(tmp,0xFF, FLASH_TOTAL_SIZE);
1718                fwrite(tmp, 1, FLASH_TOTAL_SIZE, fp);
1719                DST_OS_Free(&tmp);
1720                fclose(fp);
1721        }
1722        fp = fopen("flash.bin" , "rb");
1723        fseek(fp, address, SEEK_SET);
1724        fread(data, 1, nSize, fp);
1725        fclose(fp);
1726#else
1727        // TO DO Flash Init DHL_SYS_PlatformInit¿¡¼­ ÀÌ¹Ì È£Ãâ
1728        // TO DO Flash Read
1729        DHL_RESULT DHL_NVM_FlashRead(int flashId, UINT32 offset, UINT8 *pBuffer, UINT32 len);
1730        if (DHL_NVM_FlashRead(0, address, data, nSize) != 0)
1731        {
1732                DST_Printf("|%s|Err|DHL_NVM_FlashRead(addr:0x%x, len:0x%x)\n",__func__, (int)address, (int)nSize);
1733        }
1734#endif
1735        FlashLock(false);
1736        return data;
1737}
1738
1739
1740// ministd¿¡¼­ Áö¿øÇÏÁö ¾Ê´Â stdc ÇÔ¼ö
1741
1742int dst_atoi(char *st)
1743{
1744        bool bMinus = false;
1745                if (*st == '-' )
1746                {
1747                        bMinus = true;
1748                        st++;
1749                }
1750                int i = 0;
1751        while((*st >= '0' && *st <= '9'))
1752        {
1753                i = i * 10 + (*(st)) - '0';
1754                st++;
1755        }
1756        return bMinus ? -i : i;
1757}
1758
1759#ifndef DSTAR
1760void *memchr(s, c, n)
1761        const void *s;
1762        register unsigned char c;
1763        register size_t n;
1764{
1765        if (n != 0) {
1766                register const unsigned char *p = s;
1767
1768                do {
1769                        if (*p++ == c)
1770                                return ((void *)(p - 1));
1771                } while (--n != 0);
1772        }
1773        return (0);
1774}
1775#include <time.h>
1776struct tm * gmtime(register const time_t *timer)
1777{
1778        static struct tm br_time;
1779        return &br_time;
1780}
1781
1782size_t strftime(char *s, size_t maxsize,        const char *format,  const struct tm *t)
1783{
1784        s[0] = 0;
1785        return 0;
1786}
1787#endif
1788
1789
1790#ifdef DSTAR // CC FeedingÀ» À§ÇÑ Å×½ºÆ® ÇÔ¼ö
1791#include <stdlib.h>
1792#define SIZE_BUFF (1024*32)
1793static void ttest__cc()
1794{
1795        FILE *fp = fopen("/home/megakiss/stream/KBS.tp", "rb");
1796        if (fp == 0) return;
1797        while(1)
1798        {
1799                OS_Delay(OS_GetTicksPerSecond()/100);
1800                DS_U8 buff[SIZE_BUFF*2];
1801                int n = fread(buff, 1,SIZE_BUFF, fp);
1802                if (n != SIZE_BUFF)
1803                {
1804                        fclose(fp);
1805                        return;
1806                }
1807                _DHL_CCCallBack( SIZE_BUFF, buff);
1808        }
1809}
1810
1811static void ttest_cc()
1812{
1813        while (1)
1814        {
1815                OS_Delay(OS_GetTicksPerSecond());
1816                ttest__cc();
1817        }
1818}
1819
1820void test_cc()
1821{
1822        static int taskID = 0;
1823        if (taskID) return;
1824        taskID = OS_SpawnTask((void (*)(DS_U32)) ttest_cc, (char*)"ttest_cc", APP_TASK_PRIO_WIN_MGR, WIN_MGR_TASK_STACKSIZE, 0);
1825}
1826#endif // CC FeedingÀ» À§ÇÑ Å×½ºÆ® ÇÔ¼ö
Note: See TracBrowser for help on using the repository browser.