?? packet_test10-3.c~
字號:
#include "../layers/physical/serial_connect.h"#include "../debug/debug.h"#include "../layers/network/packet.h"#include <stdio.h> /* Standard input/output definitions */#include <string.h> /* String function definitions */#include <unistd.h> /* UNIX standard function definitions */#include <fcntl.h> /* File control definitions */#include <errno.h> /* Error number definitions */#include <termios.h> /* POSIX terminal control definitions */#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <pthread.h>/******************************************************************* * To run this program, I use argv[1] as the serial port, so you should run * this program like: * ptest /dev/tts/2 *******************************************************************/#define MICA1_BAUDRATE B19200 /* MICA1 baudrate */#define MICA2_BAUDRATE B57600 /* MICA2 baudrate */#define DEFAULT_BAUDRATE B57600 /* Default is MICA2 */char *serial_port;int sfd,flag=0;char mote = 2;int sockfd, portno;struct sockaddr_in serv_addr;struct hostent *server;/* Open serial channel to mote */ void open_serial() { struct termios tio; sfd = open(serial_port, O_RDWR|O_NOCTTY); if (sfd < 0) { fprintf(stderr,"Cannot open serial device %s\n", serial_port); exit(-1); } /* Serial port setting */ bzero(&tio, sizeof(tio)); tio.c_iflag &= ~(IGNPAR | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); tio.c_cflag &= ~(CSIZE | PARENB); if (mote == 1) tio.c_cflag |= MICA1_BAUDRATE | CS8 | CLOCAL | CREAD; else if (mote == 2) tio.c_cflag |= MICA2_BAUDRATE | CS8 | CLOCAL | CREAD; else tio.c_cflag |= DEFAULT_BAUDRATE | CS8 | CLOCAL | CREAD; /* Raw output_file */ tio.c_oflag &= ~OPOST; tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); /* Flush buffer; parameters take effect immediately */ tcflush(sfd, TCIFLUSH); tcsetattr(sfd, TCSANOW, &tio); /* Enable non-blocking IO */ fcntl(sfd, F_SETFL, O_NONBLOCK);}/* Display error messages */void error(char *msg){ perror(msg); exit(0);}/* The server thread which listen to any packet recieved by WiFi card *//* The server IP Address is assumed to be : 10.1.1.3 *//* The server port number is : 8888 */void *server_thread(void *t){ int sockfd, newsockfd, portno, clilen; struct sockaddr_in serv_addr, cli_addr; int n,i; networkPkt p,p1; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = 8888; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); while(1) { n = read(newsockfd,&p,sizeof(p)); if (n < 0) error("ERROR reading from socket"); else if(n > 0) { p1.hdr.address = 0xffff; p1.hdr.port = 0x0a; p1.hdr.group = 0x7d; p1.hdr.length = 9; p1.data[0] = p.data[0]; p1.data[1] = p.data[1]; p1.data[2] = p.data[2]; p1.data[3] = p.data[3]; p1.data[4] = p.data[4]; p1.data[5] = p.data[5]; p1.data[6] = p.data[6]; p1.data[7] = p.data[7]; p1.data[8] = p.data[8]; if (writeNetworkPkt(sfd, &p1) != SUCCESS) { } else { printf("Packet get from Stargate through WiFi as follows:\n"); printf("The message is:\n"); printPacket(DBG_USR, CRITICAL, stdout, &p1); } } } pthread_exit(NULL);}/* The client will send a packet to the server in other stargate with IP Address: 10.1.1.2 *//*void client(networkPkt *p){ int n; networkPkt p1; p1.hdr.address = 0xffff; p1.hdr.port = 0x0a; p1.hdr.group = 0x7d; p1.hdr.length = 9; p1.data[0] = p->data[0]; p1.data[1] = p->data[1]; p1.data[2] = p->data[2]; p1.data[3] = p->data[3]; p1.data[4] = p->data[4]; p1.data[5] = p->data[5]; p1.data[6] = p->data[6]; p1.data[7] = 9; p1.data[8] = p->data[8]; if(flag == 0) { flag = 1; portno = 8888; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname("10.1.1.2"); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); } n = write(sockfd,&p1,sizeof(p1)); if (n < 0) error("ERROR writing to socket");}*/int main(int argc, char *argv[]) { //int sfd; networkPkt p; frame f; int insync = 0; char temp = '\0'; int i; int rc,t=0; pthread_t threads; setupDebug(DBG_ALL, CRITICAL); serial_port=argv[1]; open_serial();// run the server thread /* rc = pthread_create(&threads, NULL, server_thread, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); }*/ while(1) // wait for any packet recieved by the serial port via the radio channel of mote { rc = pthread_create(&threads, NULL, server_thread, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } if (readNetworkPkt(sfd, &p) != SUCCESS) { printf("COULD NOT READ PACKET!!!!\n"); } else {/* if((p.data[7] != 9)&&(p.data[6] == 1)&&(p.data[2] == 4)) // if the packet is not from mote in stargate // and the packet is RREP // and the packet is recieved from the original destination { client(&p); } else if(p.data[6] == 5) // if the packet is the routing table { printf("===============================================\n"); printf("| The Routing Table is: |\n"); printf("===============================================\n"); printf("| Destination | Next Hop | Hop Count |\n"); printf("|---------------------------------------------|\n");*/ printf("From serial:\n"); printf("The message is:\n"); printPacket(DBG_USR, CRITICAL, stdout, &p);// printf("===============================================\n");// } } } closeSerialPort(sfd); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -