N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/Vaike/linux/system-addons/drivers/raw_seismic.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
00033  */
00034 
00035 #include <unistd.h>
00036 #include <string.h>
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 #include <fcntl.h>
00042 #include <inttypes.h>
00043 
00044 
00045 #define ASF_PORT  9001
00046 
00047 static unsigned int initialized = 0;
00048 static int s_fd;
00049 
00050 // FIXME: start and watchdog asf exec; convert to miniSoap
00051 
00052 int seismic_init(void)
00053 { 
00054         struct sockaddr_in asf_addr;
00055         char buffer[6];
00056         int fd, error;
00057 
00058         fd = socket(AF_INET, SOCK_STREAM,0);
00059         if (fd < 0)
00060                 return fd;
00061 
00062         memset(&asf_addr, '\0', sizeof(asf_addr));
00063         asf_addr.sin_family = AF_INET;
00064         asf_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
00065         asf_addr.sin_port = htons(ASF_PORT);
00066   
00067         if ((error = connect(fd, (struct sockaddr*)&asf_addr, 
00068                              sizeof(asf_addr))) < 0) 
00069                 return error;
00070 
00071         /* version header */
00072         buffer[0]='T';
00073         buffer[1]=' ';
00074         buffer[2]='0';
00075         buffer[3]='0';
00076         buffer[4]='0';
00077         buffer[5]='0';
00078         error = write(fd, buffer, 6);
00079 
00080         if ((error = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK)) == -1)
00081                 return error;
00082 
00083         return fd;
00084 }
00085 
00086 
00087 int consume_seismic(void)
00088 {
00089         uint8_t buf[256];
00090         int error = 0;
00091         uint8_t len;
00092         int detect = 0;
00093         int intensity = 0;
00094 
00095         while (!initialized) {
00096                 s_fd = seismic_init();
00097                 if (s_fd > 0)
00098                         initialized++;
00099         }
00100         memset(buf, '\0', 256);
00101 
00102         if ((error = read(s_fd, &len, 1)) < 0)
00103                 return error;
00104 
00105         if ((error = read(s_fd, buf, len)) < 0)
00106                 return error;
00107 
00108         if (len == 11) {
00109                 detect = buf[5];
00110                 intensity = buf[6] + 256 * buf[7] +
00111                         256 * 256 * buf[8] + 256 * 256 * 256 * buf[9];
00112         }
00113 
00114         return intensity;
00115 }


© 2007, Los Alamos National Security, LLC.