xref: /utopia/UTPA2-700.0.x/modules/msos/msos/common/debug_util.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ///////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2009-2010 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ///////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// @file   debug_util.c
98 /// @brief  debug register module for all other IPs
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 
107 // Common Definition
108 #include <stdio.h>
109 #include <stdlib.h>
110 #include <string.h>
111 #include "MsCommon.h"
112 #include "MsVersion.h"
113 #include "MsOS.h"
114 #include "MsTypes.h"
115 
116 // Internal Definition
117 
118 //-------------------------------------------------------------------------------------------------
119 //  Driver Compiler Options
120 //-------------------------------------------------------------------------------------------------
121 
122 //-------------------------------------------------------------------------------------------------
123 //  Local Defines
124 //-------------------------------------------------------------------------------------------------
125 #define DBG_MSG(x)      //x
126 
127 //-------------------------------------------------------------------------------------------------
128 //  Local Structures
129 //-------------------------------------------------------------------------------------------------
130 struct DBG_LIST_ENTRY
131 {
132     const char *Func_Name;
133     const char *Func_Help;
134     MS_DBG_LINK *pAryDbgLink;
135     struct DBG_LIST_ENTRY *next;
136 };
137 
138 //-------------------------------------------------------------------------------------------------
139 //  Global Variables
140 //-------------------------------------------------------------------------------------------------
141 
142 //-------------------------------------------------------------------------------------------------
143 //  Local Variables
144 //-------------------------------------------------------------------------------------------------
145 static struct DBG_LIST_ENTRY *pDbgRoot = NULL;
146 
147 //-------------------------------------------------------------------------------------------------
148 //  Debug Functions
149 //-------------------------------------------------------------------------------------------------
150 
151 //-------------------------------------------------------------------------------------------------
152 //  Local Functions
153 //-------------------------------------------------------------------------------------------------
_MsOS_Dbg_PrintMenuBanner(const char * banner)154 static void _MsOS_Dbg_PrintMenuBanner(const char *banner)
155 {
156     printf("============\n");
157     printf("  %s\n", banner);
158     printf("============\n");
159 }
160 
_MsOS_Dbg_PrintMenu(void)161 static void _MsOS_Dbg_PrintMenu(void)
162 {
163     struct DBG_LIST_ENTRY *pCurrentEntry = pDbgRoot;
164 
165     _MsOS_Dbg_PrintMenuBanner("Main");
166 
167     while(pCurrentEntry != NULL)
168     {
169         printf("%s\t:%s\n", pCurrentEntry->Func_Name, pCurrentEntry->Func_Help);
170         pCurrentEntry = pCurrentEntry->next;
171     }
172 }
173 
_MsOS_Dbg_PrintSubMenu(const char * Func_Name,MS_DBG_LINK * pAryDbgLink)174 static void _MsOS_Dbg_PrintSubMenu(const char *Func_Name, MS_DBG_LINK *pAryDbgLink)
175 {
176     // no more parameters, print sub menu banner
177     _MsOS_Dbg_PrintMenuBanner(Func_Name);
178 
179     if(pAryDbgLink != NULL)
180     {
181         while(pAryDbgLink->pCallBack != NULL)
182         {
183             printf("%s\t:%s\n", pAryDbgLink->Func_Name, pAryDbgLink->Func_Help);
184             pAryDbgLink++;
185         }
186     }
187 }
188 
_MsOS_Dbg_IsRegisterd(const char * Func_Name)189 static MS_BOOL _MsOS_Dbg_IsRegisterd(const char *Func_Name)
190 {
191     struct DBG_LIST_ENTRY *pCurrentEntry = pDbgRoot;
192 
193     if(pCurrentEntry == NULL)
194     {
195         return FALSE;
196     }
197     else
198     {
199         while(pCurrentEntry->next != NULL)
200         {
201             if(strcmp(pCurrentEntry->Func_Name, Func_Name) == 0)
202             {
203                 MS_CRITICAL_MSG(printf("[%s] %s registered\n", __func__, Func_Name));
204                 return TRUE;
205             }
206             else
207             {
208                 pCurrentEntry = pCurrentEntry->next;
209             }
210         }
211         return FALSE;
212     }
213 }
214 
_MsOS_Dbg_ExecuteCB(int argc,char * argv[])215 static MS_BOOL _MsOS_Dbg_ExecuteCB(int argc, char *argv[])
216 {
217     // for example, input is "PNL Init"
218     struct DBG_LIST_ENTRY *pCurrentEntry = pDbgRoot;
219     int i;
220 
221     for(i=0; i<argc; i++)
222     {
223         DBG_MSG(printf("argv[%d]=%s\n",i,argv[i]));
224     }
225 
226     while(pCurrentEntry != NULL)
227     {
228         DBG_MSG(printf("ExecuteCB(%s, %s)\n", argv[0], pCurrentEntry->Func_Name));
229 
230         if(strcmp(argv[0], pCurrentEntry->Func_Name) == 0)
231         {
232             // match, execute call back
233             return MsOS_Dbg_ExecuteSubCB(pCurrentEntry->Func_Name, argc-1, argv+1, pCurrentEntry->pAryDbgLink);
234         }
235         else
236         {
237             pCurrentEntry = pCurrentEntry->next;
238         }
239     }
240 
241     // not found associate command
242     return FALSE;
243 }
244 
245 //-------------------------------------------------------------------------------------------------
246 //  Global Functions
247 //-------------------------------------------------------------------------------------------------
MsOS_Dbg_ExecuteSubCB(const char * Func_Name,int argc,char * argv[],MS_DBG_LINK * pAryDbgLink)248 MS_BOOL MsOS_Dbg_ExecuteSubCB(const char *Func_Name, int argc, char *argv[], MS_DBG_LINK *pAryDbgLink)
249 {
250     if(pAryDbgLink != NULL)
251     {
252         if(argc == 0)
253         {
254             _MsOS_Dbg_PrintSubMenu(Func_Name, pAryDbgLink);
255             return TRUE;
256         }
257         else
258         {
259             while(pAryDbgLink->pCallBack != NULL)
260             {
261                 if(strcmp(argv[0], pAryDbgLink->Func_Name) == 0)
262                 {
263                     // found call back, execute it
264                     return pAryDbgLink->pCallBack(argc-1, argv+1);
265                 }
266                 else
267                 {
268                     pAryDbgLink++;
269                 }
270             }
271 
272             return FALSE;
273         }
274     }
275 
276     return FALSE;
277 }
278 
MsOS_Dbg_Regist(const char * Func_Name,const char * Func_Help,MS_DBG_LINK * pAryDbgLink)279 MS_BOOL MsOS_Dbg_Regist(const char *Func_Name, const char *Func_Help, MS_DBG_LINK *pAryDbgLink)
280 {
281     struct DBG_LIST_ENTRY *pNewEntry;
282     struct DBG_LIST_ENTRY *pCurrentEntry;
283 
284     // check if registerd before
285     if(_MsOS_Dbg_IsRegisterd(Func_Name))
286     {
287         return FALSE;
288     }
289     else
290     {
291         pNewEntry = (struct DBG_LIST_ENTRY *) malloc(sizeof(struct DBG_LIST_ENTRY));
292         if(pNewEntry == NULL)
293         {
294             MS_CRITICAL_MSG(printf("[%s] new entry failed\n", __func__));
295             return FALSE;
296         }
297         else
298         {
299             pNewEntry->Func_Name = Func_Name;
300             pNewEntry->Func_Help = Func_Help;
301             pNewEntry->pAryDbgLink = pAryDbgLink;
302             pNewEntry->next = NULL;
303         }
304 
305         if(pDbgRoot == NULL)
306         {
307             // 1st time register
308             pDbgRoot = pNewEntry;
309         }
310         else
311         {
312             // add to the end of the list
313             pCurrentEntry = pDbgRoot;
314             while(pCurrentEntry->next != NULL)
315             {
316                 pCurrentEntry = pCurrentEntry->next;
317             }
318             pCurrentEntry->next = pNewEntry;
319         }
320 
321         return TRUE;
322     }
323 }
324 
MsOS_Dbg_ParseCmd(char * Cmd,MS_U32 u32CmdLen)325 MS_BOOL MsOS_Dbg_ParseCmd(char *Cmd, MS_U32 u32CmdLen)
326 {
327     char * token;
328     const char delimiters[] = " \r";
329     int argc = 0;
330     char *argv[20];
331 
332     DBG_MSG(printf("In %s:%d\n",__func__,__LINE__));
333 
334     if((u32CmdLen == 1) && (*Cmd == 0x0D))
335     {
336         // enter only, dump main menu
337         _MsOS_Dbg_PrintMenu();
338         return TRUE;
339     }
340     else
341     {
342         // separate cmd to two part, token and remain cmd
343         token = strtok(Cmd, delimiters);
344         while ( token != NULL)
345         {
346             argv[argc] = token;
347             DBG_MSG(printf("argv[%d]=%s\n", argc, argv[argc]));
348             argc++;
349             token = strtok(NULL, delimiters);
350         }
351 
352         // check parameters
353         if((argc == 0) || (argv[0] == NULL))
354         {
355             // error
356             return FALSE;
357         }
358         else
359         {
360             // find registered func and execute it
361             return _MsOS_Dbg_ExecuteCB(argc, argv);
362         }
363     }
364 }
365 
MsOS_RegMyDbg(void)366 void MsOS_RegMyDbg(void)
367 {
368     return;
369 }
370 
371