source: svn/branches/remocon/zasc/app_c/dst_eroum_interface.c @ 60

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