Oh!!! Here I am re-attaching the 10KB program...
manjunath
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <string.h>
#include <sys/timeb.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/signal.h>
#include <math.h>
#define PROBABILITY 0.01
#define THRESHOLD 0.5
#define NUM_OF_NODES 3
/*
* Random Number Generator
*/
/* Random number generator ran1 from Computers in Physics */
/* Volume 6 No. 5, 1992, 522-524, Press and Teukolsky */
/* To generate real random numbers 0.0-1.0 */
/* Should be seeded with a negative integer */
#define IA 16807
#define IM 2147483647
#define IQ 127773
#define IR 2836
#define NTAB 32
#define EPS (1.2E-07)
#define MAX(a,b) (a>b)?a:b
#define MIN(a,b) (a<b)?a:b
double randReal(idum)
int *idum;
{
int j,k;
static int iv[NTAB],iy=0;
void nrerror();
static double NDIV = 1.0/(1.0+(IM-1.0)/NTAB);
static double RNMX = (1.0-EPS);
static double AM = (1.0/IM);
if ((*idum <= 0) || (iy == 0)) {
*idum = MAX(-*idum,*idum);
for(j=NTAB+7;j>=0;j--) {
k = *idum/IQ;
*idum = IA*(*idum-k*IQ)-IR*k;
if(*idum < 0) *idum += IM;
if(j < NTAB) iv[j] = *idum;
}
iy = iv[0];
}
k = *idum/IQ;
*idum = IA*(*idum-k*IQ)-IR*k;
if(*idum<0) *idum += IM;
j = iy*NDIV;
iy = iv[j];
iv[j] = *idum;
return MIN(AM*iy,RNMX);
}
#undef IA
#undef IM
#undef IQ
#undef IR
#undef NTAB
#undef EPS
#undef MAX
#undef MIN
time_t syncsecs;
short syncmsecs;
void *playSound();
void *randomSound();
typedef struct TOS_Msg {
uint16_t addr;
uint8_t type;
uint8_t group;
uint8_t length;
uint8_t data[29];
}TOS_Msg;
typedef TOS_Msg * TOS_MsgPtr;
/*
* Time stamped pkt
*/
typedef struct localTOS_Msg {
uint16_t addr;
uint8_t type;
uint8_t group;
uint8_t length;
uint8_t data[29];
}localTOS_Msg;
typedef struct DemoMsg {
uint16_t source;
uint16_t number;
char color;
} DemoMsg;
typedef DemoMsg * DemoMsgPtr;
/**** Read Input Line **************/
// this is some old code I copied from another program to
// read a line of input from the keyboard
int getline( char s[], int lim ) {
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c;
if (c == '\n') { s[i]=c; ++i; };
s[i] = '\0';
return i;
}
/**** Write TOS_Msg to Socket *******/
// this code is modified from another program to write all bytes
// of a TOS_Msg to the TCP socket
void putStream( int socket, TOS_MsgPtr p ) {
int i, r;
uint8_t n;
char * s;
extern int errno;
n = p->length + sizeof(TOS_Msg) - 29;
s = (char *) p;
// SerialForwarder expects the first byte to be a count
// of the number of bytes in the TOS_Msg.
r = write(socket, &n, 1);
// printf("sent %x\n",n);
if (r < 0) { perror("putStream error"); exit(errno); }
for (i = 0; i < n; i++) {
r = write(socket, &s[i], 1);
// printf("sent %x\n",*(s+i));
if (r < 0) { perror("putStream error"); exit(errno); }
}
}
/**** Read Stream from Socket ******/
void getStream( int socket, TOS_MsgPtr p ) {
int i, r;
char * s;
uint8_t n;
extern int errno;
s = (char *) p;
// first byte read should be the count of the number
// of bytes in the received TOS_Msg
r = read(socket, &n, 1);
if (r < 0) { perror("getStream error"); exit(errno); }
if (r == 0) { printf("connection broken!\n"); exit(0); }
// printf("got message for %d bytes\n",m);
// now read the data
if ( n > sizeof(TOS_Msg) ) {
printf("wierd message for %d bytes returned\n",n);
exit(1);
}
for (i = 0; i < n; i++) {
r = read(socket, &s[i], 1);
if (r < 0) { perror("getStream error"); exit(errno); }
if (r == 0) { printf("connection broken!\n"); exit(0); }
}
return;
}
/**** Exchange SF Protocol Number ******/
// wierd protocol code needed by SerialForwarder -- totally
// undocumented, I had to read the code of SFProtocol.java
void exchange( int socket ) {
int i, r;
char s[2];
extern int errno;
for (i = 0; i < 2; i++) {
r = read(socket, &s[i], 1);
if (r < 0) { perror("getExchange error"); exit(errno); }
if (r == 0) { printf("connection broken!\n"); exit(0); }
}
if ( !(s[0] == 'T' & s[1] == '!') ) {
printf("SFProtocol exchange failed\n"); exit(0);
}
s[1] = ' ';
for (i = 0; i < 2; i++) {
r = write(socket, &s[i], 1);
if (r < 0) { perror("putExchange error"); exit(errno); }
}
}
int firstTime=0;
int soundFlag=0;
int sampleIndex, samples[10], startSoundFlag, decisionFlag=0, randomFlag;
double globalTime;
double randomTime;
int seed=0;
double uniformDistVal=0.0;
double mu=0.0;
double LR[2000];
int seqNumOnBS=0;
/*
* Algorithm
*/
int algorithm()
{
double partOne, partTwo, denominator;
partOne = ( (1.0-mu)*(1-PROBABILITY) ) / (mu + (PROBABILITY*(1-mu)) );
partTwo = LR[samples[0]] * LR[samples[1]] * LR[samples[2]];
denominator = 1 + (partOne*partTwo);
mu = 1/denominator;
//printf("MU = %e\n", mu);
if(mu > THRESHOLD)
return 1;
else {
return 0;
}
}
void readLR()
{
FILE *fp;
float val;
int i=0;
fp = fopen("LR.txt", "r");
while( (fscanf(fp, "%f", &val)) != EOF) {
LR[i] = val;
//printf("%e\n", LR[i]);
i = i + 1;
}
fclose(fp);
}
int main() {
int i,r;
int ret;
int sock;
extern int errno;
extern int h_errno;
struct sockaddr_in toServer;
struct hostent * h;
struct pollfd waitor;
TOS_Msg buffer;
DemoMsgPtr dp;
char message[256];
struct timeval tv;
struct timezone tz;
struct timeb tb;
localTOS_Msg *samplePkt;
int *systemTimePtr;
short sample, *srcAddrPtr;
pthread_t thread, randomThread;
pthread_attr_t attr, randomAttr;
readLR();
// open socket for port 9001 on localhost
bzero((char *) &toServer, sizeof(toServer));
toServer.sin_family = AF_INET;
toServer.sin_port = htons(9001);
h = gethostbyname("localhost");
if (h == NULL) { perror("gethostbyname error"); exit(h_errno); }
bcopy(h->h_addr, (char *) &toServer.sin_addr, h->h_length);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { perror("unable to create socket"); exit(errno); }
i = 1;
r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof (i));
if (r < 0) { perror("setsockopt error");
close(sock); exit(errno); }
r = connect(sock, (struct sockaddr *) &toServer, sizeof(toServer));
if (r < 0) { perror("connect error"); exit(errno); }
// use the "poll()" function so that we don't get stuck
// trying to read from a socket that has no data
// use "man poll" to read about this neat system function
waitor.fd = sock;
waitor.events = POLLIN;
// do the silly "version exchange" that SFProtocol.java needs
exchange(sock);
while (1) {
/***** Loop to read any response(s) ********************/
for (;;) {
// wait for 250 milliseconds for a message to return
poll(&waitor,1,250);
if (!(waitor.revents & POLLIN)) break; // no message!
getStream(sock, &buffer);
/*
* Beacon Message
*/
if(buffer.type == 100) {
if(firstTime == 0) {
printf("Synchronization Time\n");
globalTime = 0.0;
firstTime = 1;
seed = rand();
seed = -1 * seed;
uniformDistVal = randReal(&seed);
randomTime = ceil( (log(1.0 - uniformDistVal))/
(log(1-PROBABILITY)) );
randomTime = (int)randomTime % 10;
//printf("Random Time = %e\n", randomTime);
randomTime = 2.0 * randomTime;
if(randomTime == 0.0) {
randomTime = 2.0;
}
//printf("%e, %d, %d\n",
// globalTime, startSoundFlag,
decisionFlag);
pthread_attr_init(&randomAttr);
pthread_create(&randomThread, &randomAttr,
randomSound, NULL);
}
}
else {
if(buffer.type == 101) {
samplePkt = (localTOS_Msg
*)malloc(sizeof(localTOS_Msg));
memcpy((void *)samplePkt, (void *)&buffer,
sizeof(TOS_Msg));
systemTimePtr = (int *)((char *)samplePkt->data
+ 2);
srcAddrPtr = (short *)((char *)samplePkt->data
+ 6);
sample = (samplePkt->data[0] + (256 *
samplePkt->data[1]));
samples[sampleIndex] = sample;
sampleIndex++;
//printf("%d\n", sample);
if(seqNumOnBS != *srcAddrPtr) {
printf("Packet Loss...SeqAtBS = %d,
Packet has %d\n",
seqNumOnBS, *srcAddrPtr);
samples[0] = sample;
sampleIndex = 1;
seqNumOnBS++;
}
if(sampleIndex == NUM_OF_NODES) {
//if(decisionFlag == 0)
decisionFlag = algorithm();
seqNumOnBS++;
sampleIndex = 0;
//printf("DECISION = %d\n",
decisionFlag);
}
//printf("%d\n", *srcAddrPtr);
//printf("sample = %d\n", sample);
free(samplePkt);
}
}
}
}
}
void *playSound()
{
void *ptr;
ptr = (void *)malloc(4);
system("play sine.wav");
pthread_exit(ptr);
}
void *randomSound()
{
pthread_t LocalThread;
pthread_attr_t LocalAttr;
for(;;) {
usleep(500000);
globalTime = globalTime + 0.5;
randomFlag = 0;
if(startSoundFlag) {
randomTime = randomTime - 1.0;
randomFlag = 1;
if(randomTime == 0.0) {
system("killall -9 play");
system("killall -9 sox");
seed = rand();
seed = -1 * seed;
uniformDistVal = randReal(&seed);
randomTime = ceil( (log(1.0 - uniformDistVal))
/(log(1-PROBABILITY)) );
randomTime = (int)randomTime % 10;
//printf("Random Time = %e\n", randomTime);
randomTime = 2.0 * randomTime;
if(randomTime == 0.0)
randomTime = 2.0;
startSoundFlag = 0;
/*
* Go back to Initial Status
*/
printf("REBOOT\n");
system("java net.tinyos.tools.BcastInject
led_on");
decisionFlag = 0;
mu = 0.0;
}
}
if(!startSoundFlag) {
if(randomFlag != 1)
randomTime = randomTime - 1.0;
if(randomTime == 0.0) {
startSoundFlag = 1;
randomTime = 2.0 * 10.0;
pthread_attr_init(&LocalAttr);
pthread_create(&LocalThread, &LocalAttr,
playSound, NULL);
}
}
printf("%e, %d, %d\n",
globalTime, startSoundFlag, decisionFlag);
}
}
_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help