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/networking/mesh_jam_resist.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 <stdio.h>
00036 #include <netdb.h>
00037 #include <syslog.h>
00038 
00039 #include "mesh_analysis.h"
00040 #include "mesh_jam_resist.h"
00041 
00042 
00043 #define STATISTIC_SIZE  128
00044 #define SNR_ALARM       3  // sigma
00045 #define NOISE_ALARM     6  // sigma
00046 
00047 
00048 static double mean(int array[], int size) {
00049   int i, sum = 0;
00050 
00051   for (i = 0; i < size; i++)
00052     sum += array[i];
00053   return (double)sum / (double)size;
00054 }
00055 
00056 static double stddev(int array[], int size) {
00057   int i, sum = 0;
00058   double mean = mean(array, size);
00059 
00060   for (i = 0; i < size; i++)
00061     sum += pow((mean - array[i]), 2.0);
00062   return sqrt(sum / (double)size);
00063 }
00064 
00065 
00066 static int idx = 0, size = 0;
00067 static int signal[STATISTIC_SIZE], noise[STATISTIC_SIZE];
00068 
00069 
00070 void jam_analysis(struct msghdr *msg_header)
00071   int i, signal_str, noise_lvl;
00072   double snr_sigma, noise_sigma, snr[size];
00073 
00074   for (control = CMSG_FIRSTHDR(msg_header); control != NULL;
00075        control = CMSG_NXTHDR(msg_header, control)) {
00076     if (control->cmsg_level == SOL_IP && 
00077         control->cmsg_type == IP_PKTINFO) {
00078       struct in_pktinfo *packet_info = (struct in_pktinfo*)CMSG_DATA(control);
00079       signal_str = packet_info->signal;
00080       noise_lvl = packet_info->noise;
00081       break;
00082     }
00083   }
00084 
00085   for (i = 0; i < size; i++)
00086     snr[i] = (double)signal[i] / (double)noise[i];
00087 
00088   snr_sigma = stddev(snr, size);
00089   snr_mean = mean(snr, size);
00090   noise_sigma = stddev(noise, size);
00091   noise_mean = mean(noise, size);
00092 
00093   if (((double)signal_str / (double)noise_lvl <
00094        snr_mean + (SNR_ALARM * snr_sigma)) ||
00095       (double)noise_lvl > noise_mean + (NOISE_ALARM * noise_sigma)) {
00096     // FIXME: bcast alarm
00097     return;
00098   }
00099 
00100   signal[idx] = signal_str;
00101   noise[idx] = noise_lvl;
00102 
00103   if (idx < STATISTIC_SIZE)
00104     idx++;
00105   else
00106     idx = 0;
00107 
00108   if (size < STATISTIC_SIZE)
00109     size++;
00110 }


© 2007, Los Alamos National Security, LLC.