testharn.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:23k
- /*******************************************************************************
- * Copyright(c) Philips Consumer Electronics B.V. 2001
- * The attached material and the information contained therein is proprietary
- * to Philips and is issued only under strict confidentiality arrangements.
- * It shall not be used, reproduced, copied in whole or in part, adapted,
- * modified, or disseminated without a written license of Philips.
- * It must be returned to Philips upon its first request.
- *
- * Project: SA-MP ( Super Audio Media Player )
- * File %name: testharn.c %
- * %version: 12 %
- * %date_modified: Tue Nov 12 15:04:35 2002 %
- * %derived_by: klerxm %
- * Creation date: Tue Feb 26 18:03:49 2002
- * First author: kokh
- *
- * Advanced Systems and Applications Lab - Eindhoven
- *
- * Continuus %full_filespec: testharn.c~12:csrc:4 %
- *
- * Description: Test harness
- *
- * Change history:
- *
- * Rev Date Who Comments
- * ---- ---------- -------- -----------------------------------------------------
- * 1 2002-02-16 kokh Initial version (taken over from DVDv2)
- * 2-5 2002-03-07 kokh Several improvements
- * 6 2002-05-23 klerxm Added tasks for sending and receiving strings over UART
- * 7 2002-10-09 potm Made reception more robust with regard to line endings
- * 8 2002-10-14 potm Added definition of MAX_MESSAGE_SIZE
- * 9 2002-11-07 klerxm See CRPR443: Added mutual exclusion semaphore to thCommSend
- * See CRPR448: Decreased maximum message size and added
- * additional error checking in thCommRecvTask
- * Alo keep calling UART_Write until entire string has been
- * sent in thCommSendTask
- * 10 2002-11-11 klerxm See CRPR448: Increased mailbox size and added retry
- * mechanism in thCommSend for OS_SendMsg
- * 11 2002-11-12 klerxm See CRPR448: Use different sizes for send and receive
- * buffers and always check buffer overflow
- * Changed prefix of local functions and variables to comply
- * to naming convention
- * 12 2003-04-02 klerxm Additional error check on received command string
- *
- *******************************************************************************/
- #define COMP_PREFIX "TESTH"
- /*******************************************************************************
- * Include Files
- *******************************************************************************/
- #include "config.h"
- #if 1 //def TEST_HARNASS///////////////////////////////////////////////////////////////////////////////////////////////
- #include "Config.h" // Global Configuration - do not remove!
- #include "cpuv186tv186t.h"
- #include "Kernelker_api.h"
- #include "Cpuv186tserio.h"
- #include <embedded.h>
- #include "playcoresampsamp_gen.h"
- #if 0 //jerry todo: check later
- #include <glue_dbg.h>
- #include <glue_osa.h>
- #else
- #include "includesysdefs.h"
- #endif
- #include "playcorenav_sacdglue_test.h"
- #include "playcorenav_sacdfatal.h"
- #if 0 //jerry todo: check later
- #include <sdm.h>
- #endif
- #include "playcoretestharnass_samptestharn.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdarg.h>
- /*******************************************************************************
- * Macro Definitions
- *******************************************************************************/
- #define TH_MBOX_SIZE 20
- #define TH_RCV_BUFFER_SIZE 100 //change to 100 from 256 to save memory
- #define TH_SND_BUFFER_SIZE 512
- /*******************************************************************************
- * Data Definitions
- *******************************************************************************/
- //jerry cai add CONST attribute.
- #ifdef TEST_HARNASS
- extern CONST THGConnection THGConnections[];
- #endif
- //jerry cai, print nothing at the moment. Otherwise it can affect the communication between host and target.
- #define DBG_Dprintf(x, y, z)
- //jerry cai disable it for saving memory #define TH_USE_MSG_BUFFER
- #ifdef TH_USE_MSG_BUFFER
- #define TH_MAX_MESSAGES 128
- #define TH_CHAR_BUFFER_SIZE 8192
- static char *thCurrentGrp = NULL;
- static char *thCurrentCmd = NULL;
- static SAMP(UInt32) thCmdSem = 0;
- static int thMsgStart[TH_MAX_MESSAGES];
- static int thMsgFill = 0;
- static char thCharBuffer[TH_CHAR_BUFFER_SIZE];
- static int thCharFill = 0;
- #endif
- static int thInitCounter = 0;
- #if 0 //jerry cai
- static char thCommBuffer[TH_MBOX_SIZE][TH_SND_BUFFER_SIZE];
- #endif
- static unsigned long thCommIndex = 0;
- static SAMP(UInt32) thCommSemId;
- static SAMP(UInt32) thCommMboxId;
- static SAMP(UInt32) thCommSendTaskId;
- #if 0 //jerry cai
- static SAMP(UInt32) thCommRecvTaskId;
- #endif
- /*******************************************************************************
- * Function prototype
- *******************************************************************************/
- static int thCheckMsg(char *msg);
- static int thDecode(char *msg, char **group, char **cmd, char **options);
- static void thMakeParamStr(char *parstring, int length, char *formatstr, va_list argptr);
- static void thGroupSwitch(char *group, char *cmd, char *options);
- #ifdef TH_USE_MSG_BUFFER
- static void thFlushMessageBuffer(void);
- static void thPutMessageInBuffer(char *msg);
- #endif
- void __interrupt SER1_ISR(void);
- /*******************************************************************************
- * Local Functions
- *******************************************************************************/
- /*******************************************************************************
- * Name : thCommRecvTask
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- #ifdef TEST_HARNASS
- #if 1
- extern BYTE SER1_BUFF[100];
- extern BYTE ser1_buff_counter;
- extern BYTE ser1_message_ready;
- #endif
- BYTE get_ser1_buff_counter()
- {
- return ser1_buff_counter;
- }
- #if 1 // jerry cai
- void thCommRecvTask(void)
- #else
- static void thCommRecvTask(void)
- #endif
- {
- SAMP(Bool) end, error;
- SAMP(UInt16) read;
- char buffer[TH_RCV_BUFFER_SIZE], *bufferptr;
- #if 0 //jerry cai
- char group[10];
- char cmd[TH_RCV_BUFFER_SIZE];
- char options[TH_RCV_BUFFER_SIZE];
- #else
- char *group, *cmd, *options;
- #endif
- // SAMP(TH_Init)();
- while (1)
- {
- /* read a whole line into buffer */
- // memset(buffer, 0, TH_RCV_BUFFER_SIZE);
- // bufferptr = buffer;
- end = FALSE;
- error = FALSE;
- while(ser1_message_ready == 0)usleep(2000UL);
- ser1_message_ready = 0;
- if (!error)
- {
- /* end string */
- // *bufferptr = ' ';
- /* decode string */
- if( thDecode(SER1_BUFF, &group, &cmd, &options) )
- {
- SAMP(UInt32) err = SAMP(ERR_OK);
- /* call group switch function */
- thGroupSwitch(group, cmd, options);
- // SAMP(TH_Send)(group, cmd, "%u", err);
- }
- else
- {
- DBG_Dprintf( COMP_PREFIX, DBG_MASK_ERROR, "Invalid message received" );
- }
- }
- else
- {
- DBG_Dprintf( COMP_PREFIX, DBG_MASK_ERROR, "Received message exceeds maximum size" );
- }
- }
- }
- #endif
- #if 0 //jerry cai remove send task. Messages will be sent to host directly by the other tasks.
- /*******************************************************************************
- * Name : thCommSendTask
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- static void thCommSendTask(void)
- {
- unsigned long index;
- char *msg;
- SAMP(UInt16) written, length;
- size_t size;
- while (1)
- {
- /* wait for the queue */
- if ( SAMP(OS_RecvMsg)(thCommMboxId, OS_FOREVER, &index, sizeof(unsigned long), &size) == SAMP(ERR_OK) )
- {
- msg = thCommBuffer[index];
- written = 0;
- do
- {
- msg += written;
- length = strlen(msg);
- SAMP(UART_Write)(msg, length, 500, &written);
- }
- while( written != length );
- }
- }
- }
- #endif
- /*******************************************************************************
- * Name : thCommSend
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- static void thCommSend(char* msg)
- {
- ASSERT(strlen(msg) < TH_SND_BUFFER_SIZE);
- #if 0 //jerry cai
- GUARDED( SAMP(OS_SemP)( thCommSemId, OS_SAFE4EVER ) );
- /* copy string to buffer */
- strncpy(thCommBuffer[thCommIndex], msg, TH_SND_BUFFER_SIZE-1);
- thCommBuffer[thCommIndex][TH_SND_BUFFER_SIZE-1] = ' ';
- /* send index of string to task */
- while( SAMP(OS_SendMsg)(thCommMboxId, &thCommIndex, sizeof(unsigned long)) != SAMP(ERR_OK) )
- {
- /* mailbox full, wait 10ms and try again */
- ASSERT( SAMP(OS_SemP)( thCommSemId, 1 ) == SAMP(ERR_FAILED) );
- }
- /* update index */
- thCommIndex = (thCommIndex+1)%TH_MBOX_SIZE;
- GUARDED( SAMP(OS_SemV)( thCommSemId ) );
- #else
- printf(msg);
- #endif
- }
- static long strtolong(char *str)
- {
- int i = 0;
- long l = 0;
- char sign = 1;
- int unit = 10;
- while (*str == ' ') str++;
- if(*str == '-')
- {
- str++;
- sign = -1;
- }
- else
- if(*str == '+')
- {
- str++;
- sign = 1;
- }
- if('0' == str[0] && 'x' == str[1])
- {
- str += 2;
- unit = 16;
- }
- while('0'<=str[i] && str[i] <= '9' || (16 == unit && 'a'<=str[i] && str[i] <= 'f'))
- {
- l = l * unit;
- if(16 == unit && 'a'<=str[i])
- l = l + (str[i] - 'a');
- else
- l = l + (str[i] - '0');
- i++;
- }
- if(-1 == sign)
- l = -l;
- return l;
- }
- static unsigned long strtoulong(char *str)
- {
- int i = 0;
- unsigned long l = 0;
- int unit = 10;
- while (*str == ' ') str++;
- if('0' == str[0] && 'x' == str[1])
- {
- str += 2;
- unit = 16;
- }
- while('0'<=str[i] && str[i] <= '9' || (16 == unit && 'a'<=str[i] && str[i] <= 'f'))
- {
- l = l * unit;
- if(16 == unit && 'a'<=str[i])
- l = l + (str[i] - 'a') + 10;
- else
- l = l + (str[i] - '0');
- i++;
- }
- return l;
- }
- /*******************************************************************************
- * Local Test Harness Functions
- *******************************************************************************/
- static void thltoa(char *str, long l)
- {
- int count;
- char tempstr[12];
- char *ptr;
- if ((unsigned long)l == 2147483648UL)
- {
- strcpy(str, "-2147483648");
- return;
- }
- if (l < 0)
- {
- strcpy(str, "-");
- l = -l;
- }
- else
- {
- *str = ' ';
- }
- for (count = 0; count <= 10; count++)
- {
- tempstr[10 - count] = (char ) (l % 10) + '0';
- l /= 10;
- }
- tempstr[11] = 0;
- ptr = tempstr;
- while (*ptr == '0') ptr++;
- strcat(str, ptr);
- if (*str == ' ') strcpy(str, "0");
- }
- static void thutoa(char *str, unsigned long l)
- {
- int count;
- char tempstr[12];
- char *ptr;
- for (count = 0; count <= 10; count++)
- {
- tempstr[10 - count] = (char) (l % 10) + '0';
- l /= 10;
- }
- tempstr[11] = 0;
- ptr = tempstr;
- while (*ptr == '0') ptr++;
- strcpy(str, ptr);
- if (*str == ' ') strcpy(str, "0");
- }
- static void thMakeParamStr(char *parstring, int length, char *formatstr, va_list argptr)
- {
- char *formatptr;
- unsigned long unsarg;
- long longarg;
- char *stringarg;
- char tempstr[15];
- /* intialise parstring */
- strcpy(parstring, "");
-
- /* set formatptr and parptr */
- formatptr = formatstr;
- while (*formatptr && length>2)
- {
- /* skip % sign */
- formatptr++;
-
- if (*formatptr != 's')
- {
- switch(*formatptr)
- {
- case 'd':
- case 'l':
- longarg = va_arg(argptr, long);
- thltoa(tempstr, longarg);
- strncat(parstring, tempstr, length-2);
- strcat(parstring, "," );
- length -= strlen(tempstr)+1;
- break;
- case 'u':
- unsarg = va_arg(argptr, unsigned long);
- thutoa(tempstr, unsarg);
- strncat(parstring, tempstr, length-2);
- strcat(parstring, "," );
- length -= strlen(tempstr)+1;
- break;
- default:
- break;
- }
- }
- else
- {
- stringarg = va_arg(argptr, char*);
- strncat(parstring, stringarg, length-2);
- strcat(parstring, ",");
- length -= strlen(stringarg)+1;
- }
- formatptr++;
- }
- parstring[strlen(parstring) - 1] = ' ';
- }
- int thCheckMsg(char *msg)
- {
- char *pch = msg;
- if (*pch != '[') return 0;
- while ((*pch != ']') && (*pch != 0))
- pch++;
- if (*pch != ']') return 0;
- pch++;
- if (*pch != 'r') return 0;
- pch++;
- if (*pch != 0) return 0;
- return 1;
- }
- static int thDecode(char *msg, char **group, char **cmd, char **options)
- {
- char *pch, *find;
- int nr;
- while(*msg == 0)
- msg++;
-
- if ( thCheckMsg(msg) )
- {
- pch = msg+1;
- find = strchr(pch, ',');
- nr = find-pch;
- if ( nr > 5 )
- {
- nr = 5;
- }
- #if 0 //jerry cai
- strncpy(group, pch, nr);
- #else
- *group = pch;
- #endif
- pch[nr] = ' ';
- pch = find+1;
- find = strchr(pch, ',');
- if (find == NULL)
- {
- find = strchr(pch, ']');
- }
- #if 0 //jerry cai
- strncpy(cmd, pch, find-pch);
- #else
- *cmd = pch;
- #endif
- pch[find-pch] = ' ';
- pch = find+1;
- find = strchr(pch, ']');
- if (find)
- {
- #if 0 //jerry cai
- strncpy(options, pch, find-pch);
- #else
- *options = pch;
- #endif
- pch[find-pch] = ' ';
- }
- else
- {
- #if 0 //jerry cai
- strcpy(options, "");
- #else
- *options = pch - 1;
- #endif
- }
- return 1;
- }
- return 0;
- }
- #ifdef TH_USE_MSG_BUFFER
- static void thFlushMessageBuffer(void)
- {
- int i;
- SAMP(OS_SemP)(thCmdSem, OS_FOREVER);
- for (i=0; i<thMsgFill; i++)
- {
- thCommSend(&thCharBuffer[thMsgStart[i]]);
- }
- thMsgFill = 0;
- thCharFill = 0;
- SAMP(OS_SemV)(thCmdSem);
- }
- static void thPutMessageInBuffer(char *msg)
- {
- int spaceAvail = 1;
- int length = strlen(msg);
- if (thMsgFill == TH_MAX_MESSAGES)
- {
- spaceAvail = 0;
- }
- if (thCharFill + length >= TH_CHAR_BUFFER_SIZE)
- {
- spaceAvail = 0;
- }
- if (spaceAvail)
- {
- SAMP(OS_SemP)(thCmdSem, OS_FOREVER);
- thMsgStart[thMsgFill] = thCharFill;
- thMsgFill ++;
- strcpy(&thCharBuffer[thCharFill], msg);
- thCharFill += length + 1;
- SAMP(OS_SemV)(thCmdSem);
- }
- else
- {
- DBG_Dprintf( COMP_PREFIX, DBG_MASK_WARNING, "Out of buffer space for messages" );
- thFlushMessageBuffer();
- thCommSend(msg);
- }
- }
- #endif
- #ifdef TEST_HARNASS
- static void thGroupSwitch(char *group, char *cmd, char *options)
- {
- int count;
- long grp, grp2;
- char newgroup[6];
- #ifdef TH_USE_MSG_BUFFER
- SAMP(OS_SemP)(thCmdSem, OS_FOREVER);
- thCurrentGrp = group;
- thCurrentCmd = cmd;
- SAMP(OS_SemV)(thCmdSem);
- #endif
- strcpy(newgroup, group);
- if (strlen(group) < 5)
- {
- for (count = strlen(newgroup); count < 5; count++)
- {
- newgroup[count] = '_';
- }
- newgroup[5] = 0;
- }
- #if 0
- for (count = 0; count < 5; count++)
- {
- toupper(newgroup[count]);
- }
- #endif
- grp = (long) GROUP((long) newgroup[0], (long) newgroup[1], (long) newgroup[2], (long) newgroup[3],
- (long) newgroup[4]);
- count = 0;
- do
- {
- grp2 = THGConnections[count].group;
- if (grp2 == grp)
- {
- THGConnections[count].func(cmd, options);
- count = -1;
- }
- if (grp2 == 0)
- {
- count = -1;
- }
- count++;
- }
- while (count > 0);
- #ifdef TH_USE_MSG_BUFFER
- SAMP(OS_SemP)(thCmdSem, OS_FOREVER);
- thCurrentGrp = NULL;
- thCurrentCmd = NULL;
- SAMP(OS_SemV)(thCmdSem);
- thFlushMessageBuffer();
- #endif
- }
- #endif
- /*******************************************************************************
- * Exported Functions
- *******************************************************************************/
- /*******************************************************************************
- * Name : TH_GetParam
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- void TH_GetParam(char *parstring, char *formatstr, ...)
- {
- va_list marker;
- char *formatptr, *parptr, *tempptr;
- char tempstr[TH_RCV_BUFFER_SIZE];
- void *ptr;
- char *endptr;
- unsigned long *unsarg;
- long *longarg;
- char *stringarg;
- /* start parameter list after formatstr */
- va_start( marker, formatstr );
- /* set formatptr and parptr */
- formatptr = formatstr;
- parptr = parstring;
- /* while there are strings in the format string */
- while (*formatptr && parptr)
- {
- /* skip % sign */
- formatptr++;
- /* find next argument */
- if (*parptr == ',') parptr++;
- ptr = va_arg( marker, void *);
- if (*formatptr != 's')
- {
- tempptr = strchr(parptr, ',');
- if (tempptr)
- {
- strncpy(tempstr, parptr, tempptr - parptr);
- tempstr[tempptr - parptr] = 0;
- }
- else
- {
- //jerry cai, bugbug??? why copy string to NULL pointer?
- strcpy(tempstr, parptr);
- }
- parptr = tempptr;
- switch (*formatptr)
- {
- case 'd':
- case 'l':
- longarg = (long*) ptr;
- *longarg = strtolong(tempstr);
- break;
- case 'u':
- unsarg = (unsigned long*) ptr;
- *unsarg = strtoulong(tempstr);
- break;
- }
- }
- else
- {
- stringarg = (char *) ptr;
- tempptr = strchr(parptr, ',');
- if (tempptr)
- {
- strncpy(stringarg, parptr, tempptr - parptr);
- stringarg[tempptr - parptr] = 0;
- }
- else
- {
- strcpy(stringarg, parptr);
- }
- parptr = tempptr;
- }
- formatptr++;
- }
- va_end( marker );
- }
- /*******************************************************************************
- * Name : TH_Send
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- SAMP(ErrCode) SAMP(TH_Send)(char *group, char *cmd, char *format, ...)
- {
- char msg[TH_SND_BUFFER_SIZE];
- va_list marker;
- /* start parameter list after formatstr */
- va_start(marker, format);
- strcpy(msg, "[");
- strcat(msg, group);
- strcat(msg, ",");
- strcat(msg, cmd);
- strcat(msg, ",");
- thMakeParamStr(&msg[strlen(msg)], TH_SND_BUFFER_SIZE-strlen(msg)-1, format, marker);
- va_end(marker);
- strcat(msg, "]n"); //jerry cai todo change back to r for Philips.was r.
- #ifdef TH_USE_MSG_BUFFER
- SAMP(OS_SemP)(thCmdSem, OS_FOREVER);
- if ((thCurrentGrp != NULL) && (thCurrentCmd != NULL))
- {
- if ((strcmp(thCurrentGrp, group) == 0) &&
- (strcmp(thCurrentCmd, cmd) == 0))
- {
- SAMP(OS_SemV)(thCmdSem);
- thCommSend(msg);
- }
- else
- {
- SAMP(OS_SemV)(thCmdSem);
- thPutMessageInBuffer(msg);
- }
- }
- else
- {
- SAMP(OS_SemV)(thCmdSem);
- thCommSend(msg);
- }
- #else
- thCommSend(msg);
- #endif
- return SAMP(ERR_OK);
- }
- /*******************************************************************************
- * Name : TH_Trace
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- SAMP(ErrCode) SAMP(TH_Trace)(char *msg)
- {
- SAMP(TH_Send)("TSTH", "TRACE", "%s", msg);
- return SAMP(ERR_OK);
- }
- /*******************************************************************************
- * Name : TH_Init
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- SAMP(ErrCode) SAMP(TH_Init)(void)
- {
- SAMP(ErrCode) lErr = SAMP(ERR_OK);
- #if 0 //jerry cai , THG_RCV task is combined with debug task, so the semaphore, mailbox and task are not needed here.
- if( thInitCounter == 0 )
- {
- #ifdef TH_USE_MSG_BUFFER
- GUARDED( SAMP(OS_SemCreate)(1, &thCmdSem) );
- #endif
- GUARDED( SAMP(UART_Init)() );
- GUARDED( SAMP(OS_SemCreate)( 1, &thCommSemId ) );
- GUARDED( SAMP(OS_MboxCreate)( TH_MBOX_SIZE, &thCommMboxId ) );
- #if 0 //jerry cai
- GUARDED( SAMP(OS_TaskCreate)( SDM_PRIORITY_THG_SND, SDM_STACKSIZE_THG_SND, thCommSendTask, &thCommSendTaskId ) );
- #endif
- GUARDED( SAMP(OS_TaskCreate)( SDM_PRIORITY_THG_RCV, SDM_STACKSIZE_THG_RCV, thCommRecvTask, &thCommRecvTaskId ) );
- SAMP(TH_Trace)("SA-MP TestHarness Initialised...");
- }
- thInitCounter++;
- ASSERT( thInitCounter>0 );
- #else
- // sig_sem(SEM_READY_FOR_CL);
- #endif
- return lErr;
- }
- /*******************************************************************************
- * Name : TH_Term
- * Purpose : -
- * Input : -
- * Output : -
- * Returns : -
- * Description: See purpose.
- *******************************************************************************/
- SAMP(ErrCode) SAMP(TH_Term)(void)
- {
- SAMP(ErrCode) lErr = SAMP(ERR_OK);
- #if 0 //jerry cai , THG_RCV task is combined with debug task, so the semaphore, mailbox and task are not needed here.
- ASSERT( thInitCounter>0 );
- thInitCounter--;
- if( thInitCounter == 0 )
- {
- GUARDED( SAMP(OS_TaskDelete)( thCommRecvTaskId ) );
- #if 0 //jerry cai
- GUARDED( SAMP(OS_TaskDelete)( thCommSendTaskId ) );
- #endif
- GUARDED( SAMP(OS_MboxDelete)( thCommMboxId ) );
- GUARDED( SAMP(OS_SemDelete)( thCommSemId ) );
- GUARDED( SAMP(UART_Term)() );
- #ifdef TH_USE_MSG_BUFFER
- GUARDED( SAMP(OS_SemDelete)( thCmdSem ) );
- #endif
- }
- #endif
- return lErr;
- }
- #endif ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*******************************************************************************
- * End of File testharn.c
- *******************************************************************************/