]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/utils/can-proxy.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[lincan.git] / lincan / utils / can-proxy.c
index ab9bee864418116653a60d6796d9cddbc2f003ac..d9dbcddf75744db94416d8fdc8caea8009486102 100644 (file)
@@ -35,8 +35,6 @@ will forward packets from CAN to the most recent IP/UDP address.
 #define DEFAULT_CAN_CLIENT_PORT 3008
 #define MAX_MSG_SIZE 4000
 
-typedef struct canmsg_t canmsg_t;
-
 extern int SendCanMsg(canmsg_t * msg);
 extern int ReceiveCanMsg(canmsg_t * msg);
 extern int ReadInput(int fd);
@@ -89,10 +87,7 @@ int fdError;
 int fdIn;
 FILE * fLog;
 long iBusFlags;
-int bBusLock=0;
 time_t tsStarted;
-long tmWhenLocked;
-long tmDurationLocked;
 long tmLastVerified=0;
 long tmNow;  // current time, global, will be updated in many places
 long tmLastSentReceived=0;
@@ -102,6 +97,7 @@ double time0;
 double SleepUntil=0;
 
 int quiet = 0;
+int use_select = 0;
 
 
 long tmGet() {
@@ -155,7 +151,7 @@ int WriteNet(int fd, void * msg, int size) {
 int ReadNet(int fd, void * msg, int size) {
        char recvbuf[MAX_MSG_SIZE];
        int ret;
-       int recvaddrsize = sizeof(recvaddr);
+       unsigned int recvaddrsize = sizeof(recvaddr);
 
        // setting recvaddr is required for non-connected sockets
        memcpy(&recvaddr, &fromaddr, sizeof(recvaddr));
@@ -168,7 +164,17 @@ int ReadNet(int fd, void * msg, int size) {
        return ret;
 }
 
-
+void show_usage(void) {
+       printf("can-proxy options:\n"
+       "-i     : intercative (send CAN packets typed by user)\n"
+       "-o file.log : log all traffic to a file\n"
+       "-p port : send CAN packets arriving at UDP port\n"
+       "-c     : use select() for CAN (driver can-0.7.1-pi3.4 or newer)\n"
+       "-q     : quiet\n"
+       "-h     : this help\n"
+       "UDP arriving at specified port causes can-proxy to forward all CAN\n"
+       "traffic back to the sender, establishing bi-directional communication.\n");
+}
 
 /**
        handling command line options, calling functions, main program loop 
@@ -189,13 +195,12 @@ int main(int argc, char * argv[]) {
        fLog = NULL;
        
        iBusFlags = 0;
-       tmWhenLocked = tmDurationLocked = 0;
        time0 = gettime();
        tsStarted = tvNow.tv_sec;
        tvSelectTimeout.tv_sec = 0;
        tvSelectTimeout.tv_usec = 500000; // wake up every 0.5 s even without data
 
-       while((opt=getopt(argc, argv, "io:p:c:l"))>0) {
+       while((opt=getopt(argc, argv, "io:p:ch"))>0) {
                switch(opt) {
                case 'i': // interactive or stdin
                        fdIn = 0;
@@ -208,20 +213,21 @@ int main(int argc, char * argv[]) {
                        break;
                case 'c':
                        fdCanDev = open(optarg,O_RDWR/*|O_NONBLOCK - select supported*/);
-                       break;
-               case 'l': // lock the bus during dispense
-                       bBusLock = 1;
+                       use_select++;
                        break;
                case 'q':
                        quiet ++;
                        break;
+               case 'h':
+                       show_usage();
+                       break;
                default:
                        break;
                }
        }
 
        if(!quiet)
-               fprintf(stderr, "can-proxy v0.7.1-pi3.3 (C) 2002 BFAD GmbH http://www.getembedded.de/ (GPL) \n");
+               fprintf(stderr, "can-proxy v0.7.1-pi3.5 (C) 2002 BFAD GmbH http://www.getembedded.de/ (GPL) \n");
 
        
        if(fdCanDev<0)
@@ -288,7 +294,8 @@ int main(int argc, char * argv[]) {
                if(fdIn>=0 && FD_ISSET(fdIn, &readsel)) {
                        ReadInput(fdIn);
                }
-//             usleep(20000); //can driver does select(), no need for delay
+               if(!use_select)
+                       usleep(20000);
        }
        return 0;       
 }
@@ -369,7 +376,6 @@ int ReceiveCanMsg(canmsg_t * msg) {
 
        msg->flags = 0;
        msg->cob = 0;
-       msg->timestamp = 0;
        ret = read(fdCanDev,msg, CAN_MSG_SIZE);
        if(ret == CAN_MSG_SIZE) {       
                LogMsg("RECV", fLog, msg);
@@ -498,14 +504,6 @@ int ReadCommand(char *buf) {
                        sscanf(ptr,"%i", &usSleep);
                SleepUntil = gettime()-time0 + usSleep/1000000.0;
                break;
-       case 'l':
-               strtok(buf, " \t");
-               printf("bus locking is %s", (bBusLock ? "ON" : "OFF"));
-               ptr = strtok(NULL, " \t");
-               if(ptr)
-                       sscanf(ptr,"%i", &bBusLock);
-               printf(" switched to %s\n", (bBusLock ? "ON" : "OFF"));
-               break;
        case 'w':
                if(BuildCanMsg(buf, &msg) == 0) {
                        SendCanMsg(&msg);