00001 /*!@file BeoSub/test-JAUS.C test the JAUS AUV control protocol */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 00033 #include "Util/log.H" 00034 #include <signal.h> 00035 #include <stdlib.h> 00036 #include <sys/types.h> 00037 #include <unistd.h> 00038 #include <sys/socket.h> 00039 #include <netinet/in.h> 00040 #include <unistd.h> 00041 #include <netdb.h> 00042 #include <fcntl.h> 00043 #include <arpa/inet.h> 00044 #include <stdlib.h> 00045 #include <ctype.h> 00046 00047 // JAUS command & control port 00048 #define JAUSCC_PORT 3794 00049 00050 // Max UDP packet length 00051 #define BUFLEN 9500 00052 00053 //! Trivial tester for JAUS remote commands over UDP 00054 /*! This just turns the motors on/off in response to commands received 00055 over UDP in the JAUS protocol. */ 00056 int main(const int argc, const char **argv) 00057 { 00058 // ########## block signals HUP and PIPE: 00059 sigset_t sset; sigemptyset(&sset); 00060 sigaddset(&sset, SIGHUP); sigaddset(&sset, SIGPIPE); 00061 int s = sigprocmask(SIG_BLOCK, &sset, NULL); 00062 if (s != 0) PLERROR("Sigprocmask failed"); 00063 00064 while(1) 00065 { 00066 // find out our port: 00067 struct servent *sv = getservbyname("jauscc", "udp"); int po; 00068 if (sv == NULL) 00069 { 00070 LERROR("jauscc/udp not configured in /etc/services"); 00071 po = JAUSCC_PORT; LERROR("Using default port %d", po); 00072 } 00073 else 00074 po = ntohs(sv->s_port); 00075 LINFO("Start. Listening to port %d", po); 00076 00077 // setup server: 00078 int sock = socket(AF_INET, SOCK_DGRAM, 0); 00079 if (sock == -1) 00080 { PLERROR("Cannot create server socket"); sleep(30); continue; } 00081 00082 struct sockaddr_in addr; 00083 addr.sin_family = AF_INET; 00084 addr.sin_addr.s_addr = htonl(INADDR_ANY); // accept from any host 00085 addr.sin_port = htons(po); 00086 if (bind(sock, (struct sockaddr*)(&addr), sizeof(addr)) == -1) 00087 { 00088 PLERROR("Cannot bind server socket"); close(sock); 00089 sleep(30); continue; 00090 } 00091 00092 char buf[BUFLEN]; bool running = true; 00093 while(running) 00094 { 00095 // receive a message: 00096 struct sockaddr_in fromaddr; unsigned int fromlen = sizeof(fromaddr); 00097 int ret = recvfrom(sock, buf, BUFLEN, 0, 00098 (struct sockaddr *)(&fromaddr), &fromlen); 00099 LINFO("Received message %d bytes from %s:%d", ret, 00100 inet_ntoa(fromaddr.sin_addr), ntohs(fromaddr.sin_port)); 00101 00102 // Check message format: 00103 if (strncmp(buf, "JAUS01.0", 8)) 00104 { LERROR("Missing JAUS01.0 header -- SKIPPING"); continue; } 00105 00106 // and so on... 00107 00108 00109 00110 00111 } 00112 // hum, we got messed-up... 00113 close(sock); 00114 LERROR("Cooling off for 10 seconds..."); sleep(10); 00115 } 00116 exit(1); 00117 }