[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/scp-mac/tools csmaDebugTab.h, NONE, 1.1 lplDeubgTab.h, NONE, 1.1 scpDebugTab.h, NONE, 1.1 snoop.c, NONE, 1.1 timerDebugTab.h, NONE, 1.1 uartByte.c, NONE, 1.1 uartDebugParser.c, NONE, 1.1 uartDebugServer.c, NONE, 1.1
Wei Ye
weiyeisi at users.sourceforge.net
Tue May 6 10:30:59 PDT 2008
- Previous message: [Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/scp-mac/apps/TimerTest Makefile, NONE, 1.1 README, NONE, 1.1 TimerTest.nc, NONE, 1.1 TimerTestM.nc, NONE, 1.1 config.h, NONE, 1.1
- Next message: [Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/scp-mac/tos/platform/avrmote CounterAsyncC.nc, NONE, 1.1 CounterAsyncM.nc, NONE, 1.1 LocalTimeC.nc, NONE, 1.1 LocalTimeM.nc, NONE, 1.1 TimerC.nc, NONE, 1.1 TimerM.nc, NONE, 1.1 timerEvents.h, NONE, 1.1
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
Update of /cvsroot/tinyos/tinyos-1.x/contrib/scp-mac/tools
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv9805/scp-mac/tools
Added Files:
csmaDebugTab.h lplDeubgTab.h scpDebugTab.h snoop.c
timerDebugTab.h uartByte.c uartDebugParser.c uartDebugServer.c
Log Message:
scp-mac implementation from USC/ISI
--- NEW FILE: csmaDebugTab.h ---
/*
* Copyright (C) 2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* this file is the messages to be printed out for debugging
* This table of states and events is used by uartDebugServer.c
* and uartDebugParser.c
*/
#ifndef STATE_EVENT
#define STATE_EVENT
char *stateEvent[46] = {
/* CSMA states */
"state:SLEEP",
"state:IDLE",
"state:PRE_TX",
"state:CARR_SENSE",
"state:TX_PKT",
"state:BACKOFF",
"state:RECEIVE",
"state:WAIT_CTS",
"state:WAIT_DATA",
"state:WAIT_ACK",
"state:undefined",
/* pkt transmission events */
"event:TX_MSG_ACCEPTED",
"event:TX_MSG_REJECTED_ERROR",
"event:TX_MSG_REJECTED_BUFFERED",
"event:TX_MSG_DONE",
"event:TX_BCAST_DONE",
"event:TX_RTS_DONE",
"event:TX_CTS_DONE",
"event:TX_UCAST_DONE",
"event:TX_ACK_DONE",
/* pkt reception events */
"event:RX_MSG_ERROR",
"event:RX_BCAST_DONE",
"event:RX_RTS_DONE",
"event:RX_CTS_DONE",
"event:RX_UCAST_DONE",
"event:RX_ACK_DONE",
"event:RX_UNKNOWN_PKT",
"event:RX_RTS_OTHERS",
"event:RX_CTS_OTHERS",
"event:RX_UCAST_OTHERS",
"event:RX_ACK_OTHERS",
/* timer events */
"event:TIMER_STARTED_NAV",
"event:TIMER_UPDATED_NAV",
"event:TIMER_STARTED_NEIGHB_NAV",
"event:TIMER_UPDATED_NEIGHB_NAV",
"event:TIMER_FIRE_NAV",
"event:TIMER_FIRE_NEIGHB_NAV",
"event:TIMER_FIRE_BACKOFF",
/* carrier sense events */
"event:CHANNEL_BUSY_DETECTED",
"event:CHANNEL_IDLE_DETECTED",
"event:START_SYMBOL_DETECTED",
"event:START_SYMBOL_SENT",
/* other events */
"event:TRYTOSEND_SUCCESS",
"event:TRYTOSEND_FAILURE",
"event:UPPER_LAYER_RADIO_ON",
"event:UPPER_LAYER_RADIO_OFF",
};
#endif
--- NEW FILE: lplDeubgTab.h ---
/*
* Copyright (C) 2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* this file is the messages to be printed out for debugging
* This table of states and events is used by uartDebugServer.c
* and uartDebugParser.c
*/
#ifndef STATE_EVENT
#define STATE_EVENT
char *stateEvent[29] = {
/* states */
"state:IDLE",
"state:POLL_CHANNEL",
"state:undefined",
"state:undefined",
"state:undefined",
/* events */
"event:POLL_TIMER_FIRED",
"event:START_CARRIER_SENSE",
"event:POLL_CHANNEL_FAIL_NOT_SLEEP",
"event:POLL_CHANNEL_FAIL_NOT_ENABLED",
"event:POLL_CHANNEL_FAIL_VIRTUAL_CS",
"event:POLL_CHANNEL_FAIL_WAKEUP_RADIO",
"event:POLL_CHANNEL_ENABLED",
"event:POLL_CHANNEL_DISABLED",
"event:VIRTUAL_CS_IDLE",
"event:VIRTUAL_CS_BUSY",
"event:TRY_TO_SLEEP_FAILED",
"event:CHANNEL_IDLE_DETECTED",
"event:CHANNEL_BUSY_DETECTED",
"event:WAIT_TIMER_STARTED",
"event:WAIT_TIMER_FIRED",
"event:RESEND_REQUESTED",
"event:TX_REQUEST_ACCEPTED",
"event:TX_REQUEST_REJECTED_MSG_ERROR",
"event:TX_REQUEST_REJECTED_NO_BUFFER",
"event:TX_MSG_DONE",
"event:CSMA_RADIO_DONE",
"event:START_SYMBOL_DETECTED",
"event:RX_MSG_DONE",
"event:SET_RADIO_SLEEP"
};
#endif
--- NEW FILE: scpDebugTab.h ---
/*
* Copyright (C) 2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* this file is the messages to be printed out for debugging
* This table of states and events is used by uartDebugServer.c
* and uartDebugParser.c
*/
#ifndef STATE_EVENT
#define STATE_EVENT
char *stateEvent[27] = {
/* states */
"state:IDLE",
"state:TX_TONE",
"state:TX_PKT",
"state:undefined",
"state:undefined",
/* events */
"event:VIRTUAL_CS_IDLE",
"event:VIRTUAL_CS_BUSY",
"event:CHANNEL_IDLE_DETECTED",
"event:CHANNEL_BUSY_DETECTED",
"event:TX_TIMER_FIRED",
"event:SYNC_TIMER_FIRED",
"event:ADAPTIVE_TIMER_FIRED",
"event:TX_REQUEST_ACCEPTED",
"event:TX_REQUEST_REJECTED_MSG_ERROR",
"event:TX_REQUEST_REJECTED_NO_BUFFER",
"event:TX_TONE_DONE",
"event:START_SYMBOL_SENT",
"event:TX_MSG_DONE",
"event:CSMA_RADIO_DONE",
"event:START_SYMBOL_DETECTED",
"event:RX_MSG_DONE",
"event:RADIO_IDLE_DONE",
"event:RADIO_IDLE_WAIT",
"event:RADIO_IDLE_FAILED",
"event:CARRIER_SENSE_STARTED",
"event:ADAPTIVE_TX_PKT",
"event:ADAPTIVE_TIMER_STARTED"
"event:NO_PKT_TO_SEND"
};
#endif
--- NEW FILE: snoop.c ---
/*
* Copyright (C) 2003-2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye, Eric Osterweil
*
* This program captures all packets sent by snooper, and display them
* To be used with the snooper at apps/snooper/SNOOPER.c
* The first byte is packet length
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <strings.h>
#include <errno.h>
#include <inttypes.h>
#define MAX_LENGTH 250
#define BAUDRATE_MICA B19200 // baudrate for Mica
#define BAUDRATE_MICA2 B57600 // baudrate for Mica2
#define BAUDRATE_MICAZ B115200 // baudrate for MicaZ
//#define SERIAL_DEVICE "/dev/ttyS%c" //the port to use.
#define DEFAULT_DEVICE "/dev/ttyS0" // default serial device
int input_stream;
char input_buffer[MAX_LENGTH];
long startTime;
struct timeval timeStamp;
unsigned char pktLen = 0; // packet length
void print_usage(void);
void print_packet( int p_iLineLen,
int p_iEnableCrc,
uint16_t p_uiCrc );
uint16_t read_packet(void);
int setSerial( char *p_szPlatform,
struct termios *p_pNewtio );
int16_t update_crc(char data, int16_t crc);
#define NUM_PLATFORMS 3
// don't change the order of platforms
static char *g_szPlatforms[NUM_PLATFORMS] = { "mica", "mica2", "micaz" };
int platformId = NUM_PLATFORMS;
int iEnableCrc = 1; // CRC is enabled by default
int main( int argc, char *argv[ ] )
{
int iRet = 0;
int iLineLen = 80;
int iError = 0;
int iTimeout = 1;
fd_set tFdSet;
struct timeval tTimeVal;
int iArg = 0;
char szOpts[ ] = { "p:d:t:l:c:h" };
// char *szDev = "0";
char *szPlatform = NULL;
// char *szSerial = ( char * ) malloc( sizeof( char ) * strlen( SERIAL_DEVICE ) );
char *szSerial = NULL;
char *crcCheck = NULL;
struct termios newtio;
uint16_t uiCrc = 0x00;
char *crcOptions[2] = { "crc", "nocrc" }; // don't change order
while ( EOF != ( iArg = getopt( argc, argv, szOpts ) ) )
{
switch ( iArg )
{
case 'p':
szPlatform = optarg;
break;
case 'd':
szSerial = optarg;
break;
case 't':
iTimeout = atoi( optarg );
break;
case 'l':
iLineLen = atoi( optarg );
break;
case 'c':
crcCheck = optarg;
break;
case 'h':
print_usage();
return 1;
default:
print_usage();
return 1;
}
}
// check platform
if ( NULL == szPlatform ) {
printf("No platform specified!\n");
print_usage();
return 1;
} else {
// validate specified platform
int i;
for (i = 0; i < NUM_PLATFORMS; i++) {
if (strcmp(szPlatform, g_szPlatforms[i]) == 0 ) { // found platform
platformId = i;
break;
}
}
if (platformId < NUM_PLATFORMS) { // valid platform
printf("Platform: %s\n", szPlatform);
} else {
printf("Invalid platform!\n");
print_usage();
return 1;
}
}
// check serial device
if ( szSerial == NULL ) {
// will use default device
szSerial = DEFAULT_DEVICE;
printf("Serial device: default %s\n", szSerial);
printf(" To change device, see 'snoop -h'.\n");
} else {
printf("Serial device: %s\n", szSerial);
}
// check if CRC check is required
if (crcCheck != NULL) { // user specified CRC option
if (strcmp(crcCheck, crcOptions[0]) == 0) { // CRC enabled
iEnableCrc = 1;
} else if (strcmp(crcCheck, crcOptions[1]) == 0) { // CRC disabled
iEnableCrc = 0;
} else {
printf("Invalid CRC option!\n");
print_usage();
return 1;
}
}
if (iEnableCrc == 1) {
if (platformId == 2) { // micaz
iEnableCrc = 0;
printf("CRC: disabled\n");
printf(" Can't check CRC for micaz, as its radio changes CRC bytes.\n");
} else {
printf("CRC: enabled\n");
}
} else {
printf("CRC: disabled\n");
}
bzero( &newtio, sizeof( newtio ) );
/* open input_stream for read/write */
if ( -1 == ( input_stream = open( szSerial, O_RDWR|O_NOCTTY ) ) ) {
printf( "Input_stream open %s failed!\n",
szSerial );
printf(" Possible reasons: \n");
printf(" 1. The device name is wrong.\n");
printf(" 2. The device does not exist.\n");
printf(" 2. You have no permission to open the device.\n" );
return 1;
}
if ( 0 != setSerial( szPlatform, &newtio ) ) {
fprintf( stderr, "Unable to initialize device.\n" );
return 1;
}
else
{
struct timeval sTime;
time_t timep;
tcflush(input_stream, TCIFLUSH);
tcsetattr(input_stream, TCSANOW, &newtio);
//printf("input_stream opens ok\n");
// record starting time
timep = time(NULL);
printf("Snooper starts at %s\n", ctime(&timep));
gettimeofday(&sTime, NULL); // starting time for time stamping
startTime = sTime.tv_sec;
while( 1 )
{
FD_ZERO( &tFdSet );
FD_SET( input_stream, &tFdSet );
tTimeVal.tv_sec = iTimeout;
tTimeVal.tv_usec = 0;
iError = select( input_stream + 1,
&tFdSet,
NULL,
NULL,
&tTimeVal );
if ( iError > 0
&& FD_ISSET( input_stream, &tFdSet ) )
{
uiCrc = 0x00;
uiCrc = read_packet( );
print_packet( iLineLen,
iEnableCrc,
uiCrc );
}
}
}
return 0;
}
void print_usage( )
{
//usage...
printf( "Usage: snoop -p < mica | mica2 | micaz > \n" );
printf( " [ -d <serial_device_name> ]\n" );
printf( " [ -t <select_timeout> ]\n" );
printf( " [ -l <wrap-arround_line_length> ]\n" );
printf( " [ -c < crc | nocrc > ]\n" );
printf( " [ -h ]\n" );
}
uint16_t read_packet()
{
int i, count = 0;
int j = 0;
int16_t uiCrc = 0;
bzero(input_buffer, MAX_LENGTH);
//search through to find 0x7e signifing the start of a packet
while(input_buffer[0] != (char)0x7e){
while(1 != read(input_stream, input_buffer, 1)){};
}
// have start symbol now, get time stamp
gettimeofday(&timeStamp, NULL);
// read in rest of packet
input_buffer[0] = 0;
while(1 != read(input_stream, input_buffer, 1)){
}
pktLen = input_buffer[0];
if (pktLen == 0 || pktLen > MAX_LENGTH) return;
count = 1;
while(count < pktLen) {
count += read(input_stream, input_buffer + count, pktLen - count);
}
for ( j = 0; j < count - 2; j++ )
{
uiCrc = update_crc( input_buffer[ j ],
uiCrc );
}
return uiCrc;
}
void print_packet( int p_iLineLen,
int p_iEnableCrc,
uint16_t p_uiCrc )
{
//now print out the packet
int i;
char *szFormat = " %02X";
int iLen = 3;
uint16_t uiCrc = 0;
long totalTime;
uint8_t hours, minutes, seconds;
uint16_t milisec;
// show time stamp relative to when the snooper starts
totalTime = timeStamp.tv_sec - startTime; // total seconds
seconds = (uint8_t)(totalTime % 60);
totalTime = (totalTime - seconds) / 60; // total minutes
minutes = (uint8_t)(totalTime % 60);
totalTime = (totalTime - minutes) / 60; // total hours
hours = (uint8_t)(totalTime);
milisec = (uint16_t)(timeStamp.tv_usec/1000);
printf("At %02d:%02d:%02d.%03d",
hours, minutes, seconds, milisec);
if ( pktLen == 0
|| pktLen > MAX_LENGTH )
{
printf(" LENGTH ERROR\n");
}
else
{
if ( 1 == p_iEnableCrc )
{
memcpy( &uiCrc,
&input_buffer[ pktLen - 2 ],
sizeof( uint16_t ) );
if ( p_uiCrc != uiCrc )
{
printf( " CRC ERROR");
}
}
for(i = 0; i < pktLen; i ++){
if ( 0 == ( i % ( p_iLineLen / iLen ) ) )
{
printf("\n");
}
printf( szFormat,
input_buffer[i] & 0xFF );
}
printf( "\n" );
}
fflush( stdout );
}
int setSerial( char *p_szPlatform,
struct termios *p_pNewtio )
{
int iRet = 0;
if ( NULL == p_szPlatform )
{
fprintf( stderr,
"Unable to use NULL platform.\n" );
iRet = -1;
}
else if ( 0 == strcmp( p_szPlatform,
g_szPlatforms[ 0 ] ) ) // mica
{
p_pNewtio->c_cflag = CS8 | CLOCAL | CREAD;
cfsetispeed(p_pNewtio, BAUDRATE_MICA);
cfsetospeed(p_pNewtio, BAUDRATE_MICA);
}
else if ( 0 == strcmp( p_szPlatform,
g_szPlatforms[ 1 ] ) ) // mica2
{
p_pNewtio->c_cflag = CS8 | CLOCAL | CREAD;
cfsetispeed(p_pNewtio, BAUDRATE_MICA2);
cfsetospeed(p_pNewtio, BAUDRATE_MICA2);
}
else if ( 0 == strcmp( p_szPlatform,
g_szPlatforms[ 2 ] ) ) // micaz
{
p_pNewtio->c_cflag = CS8 | CLOCAL | CREAD;
cfsetispeed(p_pNewtio, BAUDRATE_MICAZ);
cfsetospeed(p_pNewtio, BAUDRATE_MICAZ);
iEnableCrc = 0; // disable CRC check, as CC2420 changes the CRC bytes
}
else
{
perror("Unknown platform!\n");
iRet = -1;
}
printf("Input baud rate changed to %d\n", (int) cfgetispeed(p_pNewtio));
printf("Output baud rate changed to %d\n", (int) cfgetispeed(p_pNewtio));
if ( 0 == iRet )
{
p_pNewtio->c_iflag = IGNPAR;
/* Raw output_file */
p_pNewtio->c_oflag = 0;
}
return iRet;
}
int16_t update_crc(char data, int16_t crc)
{
char i;
int16_t tmp;
tmp = (int16_t)(data);
crc = crc ^ (tmp << 8);
for (i = 0; i < 8; i++) {
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021; // << is done before ^
else
crc = crc << 1;
}
return crc;
}
--- NEW FILE: timerDebugTab.h ---
/*
* Copyright (C) 2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* this file is the messages to be printed out for debugging
* This table of states and events is used by uartDebugServer.c
* and uartDebugParser.c
*/
#ifndef STATE_EVENT
#define STATE_EVENT
char *stateEvent[12] = {
/* states */
"state:undefined",
"state:undefined",
"state:undefined",
"state:undefined",
"state:undefined",
/* events */
"event:NEW_TIMER_STARTED",
"event:ONE_TIMER_STOPPED",
"event:ALL_TIMERS_STOPPED",
"event:ONE_TIMER_CHANGED_REMAINING_TIME",
"event:ONE_ASYNC_TIMER_FIRED",
"event:TIMER_FIRED_TASK_QUEUE_FULL",
"event:ONE_TASK_TIMER_FIRED"
};
#endif
--- NEW FILE: uartByte.c ---
/*
* Copyright (C) 2003-2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* This program read debugging bytes from UART, and display it on screen.
* On the mote side, it should define UART_DEBUG_ENABLE and include uartDebug.h.
* If a server's IP address is provided when running uartByte, the program
* will forward bytes to the server, instead of displaying them on screen.
*
* If running a server, it will collect the debugging bytes from each node
* through the UDP socket, and save it into a log file. The program is
* uartDebugServer.c. It is useful to debug coherent events on several nodes.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#define BAUDRATE_MICA B19200 // baudrate for Mica
#define BAUDRATE_MICA2 B57600 // baudrate for Mica2
#define SERIAL_DEVICE "/dev/ttyS0" //the port to use.
//#define SERIAL_DEVICE "/dev/ttyUSB0" //the port to use.
// for socket
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SERVER_PORT 5322
#define BUF_LEN 40
int input_stream;
char input_buffer[BUF_LEN];
long startTime; // starting time in second
// for sending result via a UDP socket
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
//char *serverName = "128.9.160.147"; // scadds ip address
//char *serverName = "127.0.0.1"; // local ip address
char *serverName = NULL;
char *platName = NULL;
void print_usage(void);
void open_input(void);
void setup_socket(void);
void read_forward(void);
int main(int argc, char ** argv) {
if (argc == 1 || argc > 3) {
print_usage();
printf("Error: invalid number of parameters!\n");
exit(1);
} else if (argc == 3) {
serverName = argv[2]; // get UDP server's IP address
}
platName = argv[1]; // get mote platform's name
open_input();
if (serverName != NULL) setup_socket();
while(1){
read_forward();
}
}
void print_usage(){
//usage...
printf("Usage: uartByte platform [IP_addr_of_server]\n");
printf(" This program reads in data from ");
printf(SERIAL_DEVICE);
printf(" and display it on screen.\n");
printf("Parameters:\n");
printf(" platform: mica or mica2.\n");
printf(" IP_address_of_server: if specified, will forward bytes instead of displaying.\n");
}
void open_input(){
char *platform[2] = {"mica", "mica2"};
time_t timep;
struct timeval sTime;
/* open input_stream for read/write */
struct termios newtio;
input_stream = open(SERIAL_DEVICE, O_RDWR|O_NOCTTY);
if (input_stream == -1) {
printf("Input_stream open failed!\n");
printf("Make sure the user has permission to open device.\n");
exit(1);
}
/* Serial port setting */
bzero(&newtio, sizeof(newtio));
if (strcmp(platName, platform[0]) == 0) {
newtio.c_cflag = BAUDRATE_MICA | CS8 | CLOCAL | CREAD;
} else if (strcmp(platName, platform[1]) == 0) {
newtio.c_cflag = BAUDRATE_MICA2 | CS8 | CLOCAL | CREAD;
} else {
print_usage();
printf("Error: Unknown platform!\n");
exit(1);
}
newtio.c_iflag = IGNPAR;
/* Raw output_file */
newtio.c_oflag = 0;
tcflush(input_stream, TCIFLUSH);
tcsetattr(input_stream, TCSANOW, &newtio);
printf("input_stream opens ok\n");
// record starting time
timep = time(NULL);
printf("\nTesting starts at %s\n", ctime(&timep));
gettimeofday(&sTime, NULL); // record starting time
startTime = sTime.tv_sec;
}
void setup_socket()
{
if ((he=gethostbyname(serverName)) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SERVER_PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
}
void read_forward(){
int count, i, numbytes;
struct timeval timeStamp;
long totalTime;
uint8_t hours, minutes, seconds;
uint16_t milisec;
bzero(input_buffer, BUF_LEN);
do {
count = read(input_stream, input_buffer, BUF_LEN);
} while (count == 0);
// read in some bytes, forward them now
if (serverName == NULL) {
gettimeofday(&timeStamp, NULL);
totalTime = timeStamp.tv_sec - startTime; // total seconds
seconds = (uint8_t)(totalTime % 60);
totalTime = (totalTime - seconds) / 60; // total minutes
minutes = (uint8_t)(totalTime % 60);
totalTime = (totalTime - minutes) / 60; // total hours
hours = (uint8_t)(totalTime);
milisec = (uint16_t)(timeStamp.tv_usec/1000);
printf("At %02d:%02d:%02d.%03d\n",
hours, minutes, seconds, milisec);
for(i = 0; i < count; i ++){
//printf(" %d\n", input_buffer[i] & 0xff);
printf(" %u\n", input_buffer[i] & 0xff);
//printf(" %x\n", input_buffer[i] & 0xff);
}
printf("\n");
} else {
// send result to server
if ((numbytes = sendto(sockfd, input_buffer, count, 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
}
}
--- NEW FILE: uartDebugParser.c ---
/*
* Copyright (C) 2003-2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* This program parses an log file generated by uartDebugServer.c
* The same table of states and events that used by uartDebugServer should be
* used here too. If no state-event table is included, will simply display
* the raw bytes provided by each node.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <inttypes.h>
// if debug another component, comment out following line
// or include a corresponding debug table, which should be the same
// as in uartDebugServer.c
#include "uartDebugTab.h"
#define MAXBUFLEN 80
int main(int argc, char ** argv)
{
unsigned int numbytes, i, msgNo;
char buf[MAXBUFLEN];
unsigned int nodeId, hours, minutes, seconds, milisec;
FILE *logFile;
char timeStamp;
if (argc != 2) {
printf("Usage: uartDebugParser logFile\n");
exit(1);
}
if ((logFile = fopen(argv[1], "r")) == NULL) {
printf("Error: can't open log file.\n");
exit(1);
}
fscanf(logFile, "%s\n", buf);
if (strcmp(buf, "time-stamped") == 0) {
timeStamp = 1;
printf("time-stamped\n");
} else if (strcmp(buf, "no_timestamp") == 0) {
timeStamp = 0;
printf("no timestamp\n");
} else {
printf("Invalid log format!\n");
exit(1);
}
fgets(buf, MAXBUFLEN, logFile);
printf("%s\n", buf);
while (1) {
if (timeStamp) {
if (EOF == fscanf(logFile, "%d %d %d %d %d",
&nodeId, &hours, &minutes, &seconds, &milisec)) {
break;
}
printf("Node %d at %02d:%02d:%02d.%03d\n",
nodeId, hours, minutes, seconds, milisec);
} else {
if (EOF == fscanf(logFile, "%d", &nodeId)) {
break;
}
printf("Node %d:\n", nodeId);
}
fscanf(logFile, "%d", &numbytes);
// print out debugging info
for(i = 0; i < numbytes; i++){
fscanf(logFile, "%d", &msgNo);
#ifdef STATE_EVENT
if (msgNo >= sizeof(stateEvent)) exit(1);
printf(" %s\n", stateEvent[msgNo]);
#else
printf(" %d\n", msgNo);
#endif
}
printf("\n");
}
fclose(logFile);
return 0;
}
--- NEW FILE: uartDebugServer.c ---
/*
* Copyright (C) 2003-2005 the University of Southern California.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition to releasing this program under the LGPL, the authors are
* willing to dual-license it under other terms. You may contact the authors
* of this project by writing to Wei Ye, USC/ISI, 4676 Admirality Way, Suite
* 1001, Marina del Rey, CA 90292, USA.
*/
/*
* Author: Wei Ye
*
* This program receives debugging bytes from each node via a UDP socket,
* and save them into a log file that can be parsed later by uartDebugParser.c.
* If a state-event table is included, it will display debugging message
* on the screen. Otherwise it will only display raw data.
*
* To use this program, you need to connect each mote to a PC, and run
* uartByte.c on the PC. On the mote side, you need to define UART_DEBUG_ENABLE
* and include uartDebug.h in the component you want to debug.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <time.h>
// if debug another component, comment out following line
// or include a corresponding debug table
#include "uartDebugTab.h"
#define MYPORT 5322 // the port users will be connecting to
#define MAXBUFLEN 100
int main(int argc, char ** argv)
{
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
unsigned int addr_len, numbytes, i;
char buf[MAXBUFLEN];
// time stamping
long startTime; // starting time in second
time_t timep;
struct timeval sTime;
struct timeval timeStamp;
long totalTime;
uint8_t nodeId, days, hours, minutes, seconds;
uint16_t milisec;
FILE *logFile;
char* logFileName;
char saveTime;
if (argc == 2) {
saveTime = 0;
if (strncmp(argv[1], "-", 1) == 0) {
printf("Error: no log file specified.\n");
exit(1);
}
logFileName = argv[1];
} else if (argc == 3) {
if (strcmp(argv[1], "-t")) {
printf("Wrong option: %s\n", argv[1]);
exit(1);
}
saveTime = 1;
logFileName = argv[2];
} else {
printf("Usage: uartDebugServer [-t] logFile\n");
printf("Options: -t save timestamp\n");
exit(1);
}
if ((logFile = fopen(logFileName, "w")) == NULL) {
printf("Error: can't open log file %s.\n", logFileName);
exit(1);
}
// record starting time
printf("Logs are saved to %s \n", logFileName);
if (saveTime) {
printf("time-stamped\n");
fprintf(logFile, "time-stamped\n");
} else {
printf("no timestamp\n");
fprintf(logFile, "no_timestamp\n");
}
timep = time(NULL);
printf("\ntest started at %s\n", ctime(&timep));
fprintf(logFile, "test started at %s\n", ctime(&timep));
gettimeofday(&sTime, NULL); // record starting time
startTime = sTime.tv_sec;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
// recvfrom will block if no packet arrives
while (1) {
if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
// get time stamp
gettimeofday(&timeStamp, NULL);
totalTime = timeStamp.tv_sec - startTime; // total seconds
seconds = (uint8_t)(totalTime % 60);
totalTime = (totalTime - seconds) / 60; // total minutes
minutes = (uint8_t)(totalTime % 60);
totalTime = (totalTime - minutes) / 60; // total hours
//hours = (uint8_t)(totalTime % 24);
//days = (uint8_t)(totalTime - hours) / 24; // total days
hours = (uint8_t)(totalTime);
milisec = (uint16_t)(timeStamp.tv_usec/1000);
//nodeId = *((uint8_t*)(&their_addr.sin_addr.s_addr) + 3) - 40; // for ISI
nodeId = *((uint8_t*)(&their_addr.sin_addr.s_addr) + 3);
printf("Node %d at %02d:%02d:%02d.%03d\n",
nodeId, hours, minutes, seconds, milisec);
if (saveTime) {
fprintf(logFile, "%d %d %d %d %d\n",
nodeId, hours, minutes, seconds, milisec);
} else {
fprintf(logFile, "%d\n", nodeId);
}
fprintf(logFile, "%d\n", numbytes);
// print out debugging info
for(i = 0; i < numbytes; i++){
uint8_t msgNo = (uint8_t)buf[i];
#ifdef STATE_EVENT
if (msgNo >= sizeof(stateEvent)) {
printf("Invalid state or event number: %d\n", msgNo);
exit(1);
}
printf(" %s\n", stateEvent[msgNo]);
#else
printf(" %d\n", msgNo);
#endif
fprintf(logFile, "%d\n", msgNo);
}
printf("\n");
fprintf(logFile, "\n");
fflush(logFile);
}
close(sockfd);
fclose(logFile);
return 0;
}
- Previous message: [Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/scp-mac/apps/TimerTest Makefile, NONE, 1.1 README, NONE, 1.1 TimerTest.nc, NONE, 1.1 TimerTestM.nc, NONE, 1.1 config.h, NONE, 1.1
- Next message: [Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/scp-mac/tos/platform/avrmote CounterAsyncC.nc, NONE, 1.1 CounterAsyncM.nc, NONE, 1.1 LocalTimeC.nc, NONE, 1.1 LocalTimeM.nc, NONE, 1.1 TimerC.nc, NONE, 1.1 TimerM.nc, NONE, 1.1 timerEvents.h, NONE, 1.1
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the Tinyos-contrib-commits
mailing list