source: svn/trunk/zasc/app/dst_eroum_interface.cpp @ 76

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

1.phkim

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