📄 pklib.c
字号:
printf ("pkLoopbackTest: Error: failed to open %s\n", loopName); return (ERROR); } /* set baud and mode, flush the buffers */ (void) ioctl (loopFd, FIOBAUDRATE, baud); (void) ioctl (loopFd, FIOSETOPTIONS, OPT_RAW); (void) ioctl (loopFd, FIOFLUSH, 0); /* verify loopback operation (one character not counted in <numSent>) */ inBuf = ~letterL; /* inBuf differs from outBuf */ (void) write (loopFd, &letterL, 1); /* send a char */ taskDelay (sysClkRateGet() / 4); /* allow time for loopback */ (void) ioctl (loopFd, FIONREAD, (int) (&mule)); /* any character? */ if (mule == 1) (void) read (loopFd, &inBuf, 1); /* read it */ if (inBuf != letterL) /* mismatch? */ { printf ("pkLoopbackTest: Error: loopback connection failed\n"); (void) close (loopFd); return (ERROR); } /* declare argument to be passed to protocol hook - the channel # */ (void) ioctl (loopFd, FIOPROTOARG, channel); /* clear input counters for this channel (in case we've run before) */ if (mTest == 0) { /* attach hook for receiving chars */ (void) ioctl (loopFd, FIOPROTOHOOK, (int) pkSeqLoopHook); pkSeqLoopCount [channel][0] = 0; pkSeqLoopCount [channel][1] = 0; } else { /* attach hook for receiving chars */ (void) ioctl (loopFd, FIOPROTOHOOK, (int) pkMultiLoopHook); for (mule = 0; mule < PK_LOOP_TASK_NUM; mule ++) pkMultiLoopCount [channel][mule] = 0; } loopState [channel] = 0; } else loopFd = pkLoopFd [channel]; /* initialize output buffer */ if (mTest == 0) { for (mule = 0; mule < PK_LOOP_OUT_LEN - 1; mule ++) outBuf [mule] = 0x00 + (mule % PK_LOOP_PAT_LEN); /* 'A' ... 'Z' */ outBuf [mule] = '0' + (channel % 10); /* '0' ... '9' */ } else { for (mule = 0; mule < PK_LOOP_OUT_LEN; mule ++) outBuf [mule] = 'A' + call; /* 'A' ... 'Z' */ /* start another loopback task for multiple loopback test */ if (call < (PK_LOOP_TASK_NUM - 1)) taskSpawn (NULL, 100, VX_FP_TASK, 20000, pkLoopbackTest, 1, (int) channel, (int) baud, (int) numToSend, (int) NULL, (int) NULL, (int) NULL, (int) NULL, (int) NULL, (int) NULL); } /* write the characters out */ while ((numSent < numToSend) || (numToSend == 0)) { /* check abort flag */ if (pkLoopbackAbort) break; /* write the output buffer */ if (numToSend) mule = MIN (PK_LOOP_OUT_LEN, numToSend - numSent);/* # bytes? */ else mule = PK_LOOP_OUT_LEN; (void) write (loopFd, outBuf, mule); numSent += mule; } /* wait for i/o to complete */ if (mTest == 1) pkLoopTask [channel] --; if (call == 0) { do { taskDelay (sysClkRateGet() / 4); (void) ioctl (loopFd, FIONWRITE, (int) (&mule)); /* wait for out */ } while (mule != 0); taskDelay (sysClkRateGet()); /* the first multiple task waits for all other multiple tasks to end */ if (mTest == 1) while (pkLoopTask [channel] != 0) taskDelay (sysClkRateGet()); (void) ioctl (loopFd, FIOPROTOHOOK, 0); (void) close (loopFd); } return (OK); }/********************************************************************************* pkSeqLoopShow - display results of sequential loopback test** This routine displays the data generated from running the sequential serial* loopback test (pkLoopbackTest(0,X,X,X)). The output consists of a count* of characters written to serial port <channel> during the test. Included is* a count of errors encountered (an error was recorded any time a character* was received out of order).** RETURNS: OK or ERROR.*/STATUS pkSeqLoopShow ( int channel /* serial channel */ ) { if ((channel == CONSOLE_TTY) || (channel < 0) || (channel >= NUM_TTY)) { printf ("pkSeqLoopShow: Error: invalid parameter\n"); return (ERROR); } printf ("characters = %d\n", pkSeqLoopCount [channel][0]); printf ("errors = %d\n", pkSeqLoopCount [channel][1]); printf ("OK\n"); return (OK); }/********************************************************************************* pkMultiLoopShow - display results of multiple loopback test** This routine displays the data generated from running the multiple serial* loopback test (pkLoopbackTest(1,X,X,X)). The output consists of a list* of tasks and the number of characters each task has written to the* serial port specified by <channel>.** RETURNS: OK or ERROR.*/STATUS pkMultiLoopShow ( int channel /* serial channel */ ) { int mule; /* task counter */ if ((channel == CONSOLE_TTY) || (channel < 0) || (channel >= NUM_TTY)) { printf ("pkMultiLoopShow: Error: invalid parameter\n"); return (ERROR); } else { for (mule = 0; mule < PK_LOOP_TASK_NUM; mule ++) { printf ("task = %d ", mule + 1); printf ("count = %d\n", pkMultiLoopCount [channel][mule]); } printf ("OK\n"); } return (OK); }/********************************************************************************* pkUdpEchod - echo daemon for UDP packets** This routine is an echo daemon for UDP/IP networking packets. Packets* up to 4096 bytes are read in from port 7 (the standard UDP echo port) and* then are written back to port 7.** The function pkUdpInit() is called to create a socket and bind it.** RETURNS: does not return unless error (error number given)*/int pkUdpEchod (void) { int cnt; /* size of packet received */ int fromLen; /* size of host sock structure */ fromLen = sizeof(from); if (!flagInitUdp) { pkUdpInit(); } /* loop, reading from socket, and echoing */ while (1) { cnt = recvfrom (skt1, (char *) buf, sizeof (buf), 0, (struct sockaddr *) &from, &fromLen); if (cnt > 0) sendto (skt1, (char *) buf, cnt, 0, (struct sockaddr *) &from, sizeof (from)); } }/********************************************************************************* pkUdpInit - initialize socket for UDP test** This routine opens a socket and binds it to port 7.** RETURNS: N/A*/void pkUdpInit (void) { int sktBufSiz; /* size of largest packet */ int fromLen; /* size of host sock structure */ bzero ((char *) &sin, sizeof (sin)); /* clr sock addrs */ bzero ((char *) &from, sizeof (from)); skt1 = socket (AF_INET, SOCK_DGRAM, 0); /* open socket */ if (skt1 < 0) { logMsg ("pkUdpEchod: socket error\n",0,0,0,0,0,0); exit (1); } sin.sin_family = AF_INET; /* fill in our sock addr */ sin.sin_port = htons (IPPORT_ECHO); /* the ECHO port */ /* bind our sock addr */ if (bind (skt1, (struct sockaddr *) &sin, sizeof (sin)) < 0) { logMsg ("pkUdpEchod: bind error\n",0,0,0,0,0,0); close (skt1); exit (2); } fromLen = sizeof (from); /* get size of sock addr */ sktBufSiz = sizeof (buf); /* increase size of socket buffers */ if (setsockopt (skt1, SOL_SOCKET, SO_SNDBUF, (char *) &sktBufSiz, sizeof (sktBufSiz)) < 0) { close (skt1); logMsg ("pkUdpEchod: setsockopt error\n",0,0,0,0,0,0); exit (3); } if (setsockopt (skt1, SOL_SOCKET, SO_RCVBUF, (char *) &sktBufSiz, sizeof (sktBufSiz)) < 0) { close (skt1); logMsg ("pkUdpEchod: setsockopt error\n",0,0,0,0,0,0); exit (4); } flagInitUdp = TRUE; }#ifdef INCLUDE_TIMESTAMP/********************************************************************************* pkTimestamp - tests sysTimestamp() and sysTimestampLock()** This routine calls the function sysTimestamp() or sysTimestampLock()(after* locking interrupts) depending on the boolean parameter locked.** RETURNS: tick counter value*/UINT32 pkTimestamp ( BOOL locked ) { int lockKey; UINT32 tickRet; if (!locked) return (sysTimestampLock ()); lockKey = intLock (); tickRet = sysTimestamp (); intUnlock (lockKey); return (tickRet); }/********************************************************************************* pkTimestampRoll - returns a counter value or the current tick** This routine reads the location pointed to by tickCounter if non null, or* returns the current system clock tick.** RETURNS: counter value or clock tick*/UINT32 pkTimestampRoll ( UINT *tickCounter ) { if (tickCounter == 0) return (tickGet()); else return (*tickCounter); }/********************************************************************************* pkTimestampTestShort - short-term test of timestamp timer** This routine performs a short-term test of the timestamp timer. It reads* values with a delay in-between. The test is repeated up to 16 times. If* the counter is always increasing the test is successful.** RETURNS: OK if success, ERROR otherwise.*/STATUS pkTimestampTestShort ( UINT *tickCounter, BOOL locked ) { UINT countVal [2]; UINT32 tickVal [2]; STATUS retValue = OK; int ix; volatile int iy; /* short term test: make sure timestamp is incrementing */ for (ix = 0; (ix < 0x10) && (retValue == OK); ix++) { countVal [0] = pkTimestampRoll (tickCounter); tickVal [0] = pkTimestamp (locked); for (iy = 0; iy < 1000; iy++) ; tickVal [1] = pkTimestamp (locked); countVal [1] = pkTimestampRoll (tickCounter); if ((tickVal [0] >= tickVal [1]) && (countVal [0] >= countVal [1])) retValue = ERROR; } return (retValue); }/********************************************************************************* pkTimestampTestInit - initialize timestamp test** This routine creates a semahore and a watchdog timer for the timestamp* test. The ids are stored in two global variables and a flag is set if* the two are created successfuly.** RETURNS: OK if success, ERROR otherwise.*/STATUS pkTimestampTestInit() { tsTmoSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY); tsTmoWd = wdCreate (); if ((tsTmoSem == NULL) || (tsTmoWd == NULL)) { tsInit = FALSE; return ERROR; } else { tsInit = TRUE; return OK; } }/********************************************************************************* pkTimestampTestLong - long-term timestamp test** This routine performs a long-term timestamp test.** RETURNS: OK, or ERROR.*/void pkTimestampTestLong ( UINT *tickCounter, int timeout, BOOL locked, int nslices ) { UINT * pCountVal = malloc (nslices * 2 * sizeof (UINT)); UINT32 * pTickVal = malloc (nslices * 2 * sizeof (UINT32)); UINT * pCountPtr = pCountVal; UINT32 * pTickPtr = pTickVal; UINT32 calcTicks; volatile UINT32 tsBucket; float errorVal; int second; int delay; int ix; STATUS retValue = OK; second = timeout / nslices; delay = second * sysClkRateGet(); calcTicks = second * sysTimestampFreq(); if (tsInit == FALSE) { if (pkTimestampTestInit() == ERROR) { printf("FAIL long-term timestamp test\n"); goto pkTimestampTestLongExit; } } printf ("\nStarting long-term timestamp test.\n"); printf ("This test will run for approximately %.1f minute(s).\n\n", (float) timeout/60); for (ix = 0; ix < nslices; ix++) { if (wdStart (tsTmoWd, delay, semGive, (int) tsTmoSem) == ERROR) { printf("FAIL long-term timestamp test\n"); goto pkTimestampTestLongExit; } *pCountPtr++ = pkTimestampRoll (tickCounter); *pTickPtr++ = pkTimestamp (locked);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -