diff options
Diffstat (limited to 'GEOLOC/app')
| -rw-r--r-- | GEOLOC/app/Makefile | 18 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/Makefile | 22 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/app.c | 83 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/app.h | 17 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/records.c | 51 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/records.h | 17 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/Makefile | 15 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/clientBC.c | 45 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/clientTCP.c | 46 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/com.c | 44 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/com.h | 40 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/gwframe.h | 16 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/serverBC.c | 51 | ||||
| -rw-r--r-- | GEOLOC/app/anchor/socket/serverTCP.c | 61 | ||||
| -rw-r--r-- | GEOLOC/app/frame.c | 22 | ||||
| -rw-r--r-- | GEOLOC/app/frame.h | 21 | ||||
| -rw-r--r-- | GEOLOC/app/mobile/app.c | 84 | ||||
| -rw-r--r-- | GEOLOC/app/mobile/app.h | 16 |
18 files changed, 669 insertions, 0 deletions
diff --git a/GEOLOC/app/Makefile b/GEOLOC/app/Makefile new file mode 100644 index 0000000..4f23251 --- /dev/null +++ b/GEOLOC/app/Makefile @@ -0,0 +1,18 @@ + +all: app.o + +app.o: +ifeq ($(TARGET),ANCHOR) + $(MAKE) -C ./anchor + cp anchor/app.o ./ +else + $(CC) -c ./mobile/app.c -o ./app.o +endif + $(CC) -c frame.c -o frame.o + ld -r app.o frame.o -o app_tmp.o + mv app_tmp.o $@ + +.PHONY: clean + +clean: + -rm ./app.o ./frame.o diff --git a/GEOLOC/app/anchor/Makefile b/GEOLOC/app/anchor/Makefile new file mode 100644 index 0000000..91280a1 --- /dev/null +++ b/GEOLOC/app/anchor/Makefile @@ -0,0 +1,22 @@ + +all: app.o + +app.o: socket.o app_tmp.o records.o + ld -r $^ -o $@ + -rm app_tmp.o + +socket.o: + $(MAKE) -C ./socket + cp socket/socket.o ./ + +records.o:records.c + $(CC) -c $^ -o $@ + +app_tmp.o:app.c + $(CC) -c $^ -o $@ + +.PHONY: clean + +clean: + $(MAKE) clean -C ./socket + -rm ./*.o diff --git a/GEOLOC/app/anchor/app.c b/GEOLOC/app/anchor/app.c new file mode 100644 index 0000000..4ff2c6e --- /dev/null +++ b/GEOLOC/app/anchor/app.c @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <sys/stat.h> +#include <stdlib.h> +#include <sys/types.h> +#include <string.h> +#include "app.h" +#include "../frame.h" +#include <pthread.h> +#include "../../lib/gps.h" +#include "socket/com.h" +#include "records.h" + +//extern struct NMEA_GGA NmeaGgaFrame; +pthread_mutex_t mutex_NmeaGgaFrame; // Mutex for thread that use MASTER_IP + +void runApp(Config config){ + // Hello msg + printf("\n|Starting gateway application|\n\n"); + + // Start GW communication + startGWCom(); + + // Ensure we are in standby mode and apply configuration + config.mode=MODE_STDBY; + applyMode(config); + applyConfig(config); + + config.mode=MODE_RX; + applyMode(config); + + pthread_t frameHandlerThread; + + printf("Wait for packet...\n"); + while(1){ + if(digitalRead(0x7)==1){ + // Build parameters + Frame frame=pullFrame(); + int rssi=fetchRSSI(); + void *param; + param=malloc(sizeof(Frame)+sizeof(int)); + *((Frame *)param)=frame; + *((int *)(param+sizeof(Frame)))=rssi; + // Run thread + pthread_create(&frameHandlerThread, NULL, handleMessage, param); + config.mode=MODE_STDBY; + applyMode(config); + config.mode=MODE_RX; + applyMode(config); + } + } + pthread_join(frameHandlerThread,NULL); + +} + +void *handleMessage(void *args){ + // Fetch parameters + Frame frame=*((Frame *)args); + int rssi=*((int *)(args+sizeof(Frame))); + + // Print informations + printf("Packet receive !!!\n\n"); + printf("Frame info :\nId : %d\nStamp : %d\nData 1-6 : %d %d %d %d %d %d\nRSSI : %d\n\n", + frame.id, frame.stamp, + frame.data[0],frame.data[1],frame.data[2],frame.data[3],frame.data[4],frame.data[5], + rssi); + + GWFrame masterFrame; + masterFrame.slaveID=ANCHOR_ID; + + pthread_mutex_lock(& mutex_NmeaGgaFrame); + masterFrame.ggaFrame=getNMEA_GGAFrame(); + pthread_mutex_unlock(& mutex_NmeaGgaFrame); + + masterFrame.frame=frame; + masterFrame.rssi=rssi; + + if(!IS_MASTER){ + sendDataToMaster(masterFrame); + } + else{ + saveFrame(masterFrame); + } +} diff --git a/GEOLOC/app/anchor/app.h b/GEOLOC/app/anchor/app.h new file mode 100644 index 0000000..06d569b --- /dev/null +++ b/GEOLOC/app/anchor/app.h @@ -0,0 +1,17 @@ +#ifndef app_h +#define app_h + +#include "../frame.h" +#include "../../lib/config.h" + +/** + * Run the application + */ +void runApp(Config config); + +/** + * Used to handle received frame from mobile + */ +void *handleMessage(void *args); + +#endif diff --git a/GEOLOC/app/anchor/records.c b/GEOLOC/app/anchor/records.c new file mode 100644 index 0000000..db14206 --- /dev/null +++ b/GEOLOC/app/anchor/records.c @@ -0,0 +1,51 @@ + +#include "records.h" +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> + +void saveFrame(GWFrame frame){ + // Create records dir if not exists + mkdir(RECORDS_DIR,0777); + +// // Build string + char idStr[50]; + sprintf(idStr,"/anchor_%d", frame.slaveID); + char filePath[200]; + filePath[0]='\0'; + strcat(filePath, RECORDS_DIR); + strcat(filePath, idStr); + +// // Save in file + FILE *file; + short writeHeader=0; + pthread_mutex_lock (& mutex_file); + if(access( filePath, F_OK ) == -1){ + writeHeader=1; + } + file=fopen(filePath,"a+"); + if(file!=NULL){ + if(writeHeader){ + fprintf(file,"GWID,GPSSTATE,LatDeg,LatMin,LatSec,LatDir,LonDeg,LonMin,LonSec,LonDir,turtleID,rssi\n"); + } + fprintf(file,"%d,%d,%d,%d,%f,%d,%d,%d,%f,%d,%d,%d\n", + frame.slaveID, + frame.ggaFrame.state, + frame.ggaFrame.latDeg, + frame.ggaFrame.latMin, + frame.ggaFrame.latSec, + frame.ggaFrame.latDir, + frame.ggaFrame.lonDeg, + frame.ggaFrame.lonMin, + frame.ggaFrame.lonSec, + frame.ggaFrame.lonDir, + frame.frame.id, + frame.rssi); + fclose(file); + } + else{ + printf("Failed to open file %s.\n",filePath); + } + pthread_mutex_unlock (& mutex_file); +} diff --git a/GEOLOC/app/anchor/records.h b/GEOLOC/app/anchor/records.h new file mode 100644 index 0000000..db17b8a --- /dev/null +++ b/GEOLOC/app/anchor/records.h @@ -0,0 +1,17 @@ +#ifndef records_h +#define records_h + +#include <pthread.h> +#include "socket/gwframe.h" + +#define RECORDS_DIR "./records" + +// Mutex for thread writing in same file +pthread_mutex_t mutex_file; + +/** + * Save GWFrame into file + */ +void saveFrame(GWFrame frame); + +#endif diff --git a/GEOLOC/app/anchor/socket/Makefile b/GEOLOC/app/anchor/socket/Makefile new file mode 100644 index 0000000..84632b2 --- /dev/null +++ b/GEOLOC/app/anchor/socket/Makefile @@ -0,0 +1,15 @@ + +all:socket.o + +socket.o: clientTCP.c serverTCP.c clientBC.c serverBC.c com.c + $(CC) -c clientTCP.c + $(CC) -c serverTCP.c + $(CC) -c clientBC.c + $(CC) -c serverBC.c + $(CC) -c com.c + ld -r ./*.o -o $@ + +.PHONY: clean + +clean: + -rm ./*.o diff --git a/GEOLOC/app/anchor/socket/clientBC.c b/GEOLOC/app/anchor/socket/clientBC.c new file mode 100644 index 0000000..8230a6d --- /dev/null +++ b/GEOLOC/app/anchor/socket/clientBC.c @@ -0,0 +1,45 @@ +#include<stdio.h> +#include<string.h> //strlen +#include<sys/socket.h> +#include <unistd.h> +#include<arpa/inet.h> //inet_addr +#include "com.h" + + + + +void rcvIPFromMaster() +{ + int socketID; + struct sockaddr_in SockAddr; + int allowBC=1; + + socketID=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + setsockopt(socketID,SOL_SOCKET,SO_BROADCAST,(void*) &allowBC,sizeof(allowBC)); + + SockAddr.sin_family=AF_INET; + SockAddr.sin_port=htons(DATA_PORT); + SockAddr.sin_addr.s_addr=htonl(INADDR_BROADCAST); + + + int binded=bind(socketID,(struct sockaddr *) &SockAddr,sizeof(SockAddr)); + if(binded<0){ + puts("Failed to bind socket for receiving IP from master."); + close(socketID); + } + else{ + listen(socketID,3); + socklen_t src_addr_len=sizeof(SockAddr); + char buffer[MASTER_IP_SIZE]; + memset(buffer, 0x0,MASTER_IP_SIZE); + recvfrom(socketID,buffer,sizeof(buffer),0,(struct sockaddr*)&SockAddr,&src_addr_len); + close(socketID); + puts(buffer); + + pthread_mutex_lock(&mutex_master_ip); + memset(MASTER_IP, 0x0,MASTER_IP_SIZE); + memcpy(MASTER_IP, buffer,MASTER_IP_SIZE); + pthread_mutex_unlock(&mutex_master_ip); + } + +} diff --git a/GEOLOC/app/anchor/socket/clientTCP.c b/GEOLOC/app/anchor/socket/clientTCP.c new file mode 100644 index 0000000..621da24 --- /dev/null +++ b/GEOLOC/app/anchor/socket/clientTCP.c @@ -0,0 +1,46 @@ +#include<stdio.h> +#include<string.h> //strlen +#include <stdlib.h> +#include<sys/socket.h> +#include <unistd.h> +#include<arpa/inet.h> //inet_addr +#include "com.h" + +void sendDataToMaster(GWFrame frame){ + + int socket_desc; + struct sockaddr_in server; + socket_desc=socket(AF_INET, SOCK_STREAM,0); + char message[1000]="GET / HTTP/1.1\r\n\r\n"; + char serverReply[2000]; + + if(socket_desc==-1){ + puts("Failed to create socket."); + } + else{ + // Configure server + pthread_mutex_lock(&mutex_master_ip); + server.sin_addr.s_addr = inet_addr(MASTER_IP); + pthread_mutex_unlock(&mutex_master_ip); + server.sin_family = AF_INET; + server.sin_port = htons(DATA_PORT); + + if(connect(socket_desc,(struct sockaddr *)&server, sizeof(server))<0){ + puts("Failed to connect to server"); + } + else{ + puts("Socket connected"); + if(send(socket_desc, &frame, sizeof(GWFrame),0)<0){ + puts("Failed to send message."); + } + puts("Data send !"); + if(recv(socket_desc, serverReply,2000,0)<0){ + puts("Timeout"); + } + puts(serverReply); + + } + + close(socket_desc); + } +} diff --git a/GEOLOC/app/anchor/socket/com.c b/GEOLOC/app/anchor/socket/com.c new file mode 100644 index 0000000..f0fa1d2 --- /dev/null +++ b/GEOLOC/app/anchor/socket/com.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <string.h> +#include <pthread.h> +#include "com.h" +#include "../records.h" + +static void *rcvIPFromMasterThread(void *args){ + while(1){ + rcvIPFromMaster(); + puts("Master IP receive !"); + //delay(IP_SEND_INTERVAL/2); // Add delay to free cpu resources + } +} + +static void *sendIPToSlaveThread(void *args){ + while(1){ + puts("Send Master IP to slave..."); + sendIPToSlave(); + delay(IP_SEND_INTERVAL); // Wait five minutes + } +} + + +static void *rcvDataFromSlaveThread(void *args){ + while(1){ + GWFrame frame; + frame=rcvDataFromSlave(); + puts("Frame receive from a slave !"); + saveFrame(frame); + } +} + +void startGWCom(){ + +#if IS_MASTER != 0 + pthread_t sendIPThread, rcvDataThread; + pthread_create(&sendIPThread, NULL, sendIPToSlaveThread, NULL); + pthread_create(&rcvDataThread, NULL, rcvDataFromSlaveThread, NULL); +#else + pthread_t rcvIPThread; + pthread_create(&rcvIPThread, NULL, rcvIPFromMasterThread, NULL); +#endif + +} diff --git a/GEOLOC/app/anchor/socket/com.h b/GEOLOC/app/anchor/socket/com.h new file mode 100644 index 0000000..18539e5 --- /dev/null +++ b/GEOLOC/app/anchor/socket/com.h @@ -0,0 +1,40 @@ +#ifndef com_h +#define com_h + +#include "gwframe.h" + +#define IP_SEND_INTERVAL 300000 +#define DATA_PORT 8888 +#define IFACE "wlan0" +#define MASTER_IP_SIZE 100 // Buffer size for IP address char + +char MASTER_IP[MASTER_IP_SIZE]; // Master IP (global variable) +pthread_mutex_t mutex_master_ip; // Mutex for thread that use MASTER_IP + +/** + * Init GW communication + */ +void startGWCom(); + +/** + * Send GW frame to master GW + */ +void sendDataToMaster(GWFrame frame); + +/** + * Start master GW slave frame receiver server + */ +GWFrame rcvDataFromSlave(); + +/** + * Start slave ip reveiver server + */ +void rcvIPFromMaster(); + +/** + * Send master ip to slaves (broadcast UDP) + */ +void sendIPToSlave(); + + +#endif diff --git a/GEOLOC/app/anchor/socket/gwframe.h b/GEOLOC/app/anchor/socket/gwframe.h new file mode 100644 index 0000000..b454d8c --- /dev/null +++ b/GEOLOC/app/anchor/socket/gwframe.h @@ -0,0 +1,16 @@ +#ifndef gwframe_h +#define gwframe_h + +#include "../../frame.h" +#include "../../../lib/gps.h" + +typedef struct GWFrame GWFrame; +struct GWFrame { + short slaveID; + struct NMEA_GGA ggaFrame; + int rssi; + Frame frame; +}; + + +#endif diff --git a/GEOLOC/app/anchor/socket/serverBC.c b/GEOLOC/app/anchor/socket/serverBC.c new file mode 100644 index 0000000..2e2da88 --- /dev/null +++ b/GEOLOC/app/anchor/socket/serverBC.c @@ -0,0 +1,51 @@ +#include<stdio.h> +#include<string.h> //strlen +#include<sys/socket.h> +#include <net/if.h> +#include "com.h" +#include <unistd.h> +#include <sys/ioctl.h> +#include<arpa/inet.h> //inet_addr + + + +static char *getIp(){ + int fd; + struct ifreq ifr; + + char iface[] = IFACE; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + + //Type of address to retrieve - IPv4 IP address + ifr.ifr_addr.sa_family = AF_INET; + + //Copy the interface name in the ifreq structure + strncpy(ifr.ifr_name , iface , IFNAMSIZ-1); + + ioctl(fd, SIOCGIFADDR, &ifr); + + close(fd); + return(inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr)); +} + +void sendIPToSlave() +{ + int socketID; + struct sockaddr_in SockAddr; + int allowBC=1; + + socketID=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + setsockopt(socketID,SOL_SOCKET,SO_BROADCAST,(void*) &allowBC,sizeof(allowBC)); + + SockAddr.sin_family=AF_INET; + SockAddr.sin_port=htons(DATA_PORT); + SockAddr.sin_addr.s_addr=htonl(INADDR_BROADCAST); + + char *addr; + addr=getIp(); + sendto(socketID,addr,strlen(addr),0,(struct sockaddr*)&SockAddr, sizeof(SockAddr)); + +} + + diff --git a/GEOLOC/app/anchor/socket/serverTCP.c b/GEOLOC/app/anchor/socket/serverTCP.c new file mode 100644 index 0000000..2ed9b71 --- /dev/null +++ b/GEOLOC/app/anchor/socket/serverTCP.c @@ -0,0 +1,61 @@ + +#include<stdio.h> +#include<string.h> //strlen +#include<sys/socket.h> +#include <unistd.h> +#include<arpa/inet.h> //inet_addr +#include "com.h" +#include "./gwframe.h" +#include "../records.h" + + +int serverTCP_socket_desc=-1; + +GWFrame rcvDataFromSlave(){ + + if(serverTCP_socket_desc==-1){ + // Create socket + serverTCP_socket_desc=socket(AF_INET, SOCK_STREAM,0); + if(serverTCP_socket_desc==-1){ + puts("Failed to create socket"); + exit(1); + } + struct sockaddr_in listen_addr; + + listen_addr.sin_family = AF_INET; + listen_addr.sin_addr.s_addr = INADDR_ANY; + listen_addr.sin_port = htons(DATA_PORT); + + int binded=bind(serverTCP_socket_desc,(struct sockaddr *) &listen_addr,sizeof(listen_addr)); + if(binded<0){ + puts("Failed to bind, trying until it work..."); + while(binded<0){ + binded=bind(serverTCP_socket_desc,(struct sockaddr *) &listen_addr,sizeof(listen_addr)); + sleep(1); + } + puts("Bind succeed !"); + } + + listen(serverTCP_socket_desc,3); + } + + int client,len; + struct sockaddr_in client_addr; + client=accept(serverTCP_socket_desc, (struct sockaddr *)&client_addr, (socklen_t*)&len); + if(client<0){ + close(serverTCP_socket_desc); + serverTCP_socket_desc=-1; + puts("Acceptation failed"); + } + else{ + GWFrame frame; + recv(client, &frame, sizeof(GWFrame),0); + char msg[100]="Données bien reçus par la gateway maitre."; + write(client, msg,strlen(msg)); + close(client); + return(frame); + + } +} + + diff --git a/GEOLOC/app/frame.c b/GEOLOC/app/frame.c new file mode 100644 index 0000000..1908075 --- /dev/null +++ b/GEOLOC/app/frame.c @@ -0,0 +1,22 @@ +#include "frame.h" + + +void pushFrame(Frame frame){ + writeReg(REG_FIFO, frame.id); + writeReg(REG_FIFO, frame.stamp); + int i; + for(i=0;i<FRAME_DATA_SIZE;i++){ + writeReg(REG_FIFO,frame.data[i]); + } +} + +Frame pullFrame(){ + Frame frame; + frame.id=readReg(REG_FIFO); + frame.stamp=readReg(REG_FIFO); + int i; + for(i=0;i<FRAME_DATA_SIZE;i++){ + frame.data[i]=readReg(REG_FIFO); + } + return(frame); +} diff --git a/GEOLOC/app/frame.h b/GEOLOC/app/frame.h new file mode 100644 index 0000000..27a76cf --- /dev/null +++ b/GEOLOC/app/frame.h @@ -0,0 +1,21 @@ +#ifndef frame_h +#define frame_h + +#define FRAME_SIZE 10 +#define FRAME_DATA_SIZE 8 + +#include "../lib/types.h" +#include "../lib/dragino.h" +#include "../lib/config.h" + +typedef struct Frame Frame; +struct Frame { + byte id; + byte stamp; + byte data[FRAME_DATA_SIZE]; +}; + +void pushFrame(Frame frame); +Frame pullFrame(); + +#endif diff --git a/GEOLOC/app/mobile/app.c b/GEOLOC/app/mobile/app.c new file mode 100644 index 0000000..6887948 --- /dev/null +++ b/GEOLOC/app/mobile/app.c @@ -0,0 +1,84 @@ +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include "app.h" +#include "../frame.h" +#include <string.h> +#include "../../lib/gps.h" +#include <unistd.h> + +extern struct NMEA_GGA NmeaGgaFrame; + +void runApp(Config config){ + + // Hello msg + printf("\n|Starting mobile application|\n\n"); + + // Ensure we are in standby mode and apply configuration + config.mode=MODE_STDBY; + applyMode(config); + applyConfig(config); + + // Write frame on sx1276 + Frame frame; + frame.id=234; + frame.stamp=81; + frame.data[0]=76; + frame.data[1]=17; + frame.data[2]=16; + frame.data[3]=87; + frame.data[4]=12; + frame.data[5]=106; + pushFrame(frame); + config.mode=MODE_TX; + applyMode(config); // Send frame + + // Wait for frame to be sending + while(1){ + if(digitalRead(0x7)==1){ + printf("Packet send !!!\n\n"); + config.mode=MODE_STDBY; + applyMode(config); + saveGPSPosition(); + delay(2000); + // Write frame on sx1276 + pushFrame(frame); + config.mode=MODE_TX; + applyMode(config); // Send frame + } + } +} + +void saveGPSPosition(){ + + // Fetch GPS position + NmeaGgaFrame=getNMEA_GGAFrame(); + + // Save in file + FILE *file; + char filePath[]="gps.csv"; + short writeHeader=0; + if(access( filePath, F_OK ) == -1){ + writeHeader=1; + } + file=fopen(filePath,"a+"); + if(file!=NULL){ + if(writeHeader){ + fprintf(file,"GPSSTATE,LatDeg,LatMin,LatSec,LatDir,LonDeg,LonMin,LonSec,LonDir\n"); + } + fprintf(file,"%d,%d,%d,%f,%d,%d,%d,%f,%d\n", + NmeaGgaFrame.state, + NmeaGgaFrame.latDeg, + NmeaGgaFrame.latMin, + NmeaGgaFrame.latSec, + NmeaGgaFrame.latDir, + NmeaGgaFrame.lonDeg, + NmeaGgaFrame.lonMin, + NmeaGgaFrame.lonSec, + NmeaGgaFrame.lonDir); + fclose(file); + } + else{ + printf("Failed to open file %s.\n",filePath); + } +} diff --git a/GEOLOC/app/mobile/app.h b/GEOLOC/app/mobile/app.h new file mode 100644 index 0000000..80e661b --- /dev/null +++ b/GEOLOC/app/mobile/app.h @@ -0,0 +1,16 @@ +#ifndef app_h +#define app_h + +#include "../../lib/config.h" + +/** + * Run the application + */ +void runApp(Config config); + +/** + * Save the GPS position + */ +void saveGPSPosition(); + +#endif |
