diff --git a/software/monitor/monitor/ClientUDP.cs b/software/monitor/monitor/ClientUDP.cs new file mode 100644 index 0000000..2ba7ae1 --- /dev/null +++ b/software/monitor/monitor/ClientUDP.cs @@ -0,0 +1,69 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace monitor +{ + public static class ClientUDP + { + private const int listenPort = 11000; + private static byte[] ImageBuffer = null; + + private static UdpClient listener = null; + private static IPEndPoint groupEP = null; + + public static void UDPOpen(int port) + { + listener = new UdpClient(port); + groupEP = new IPEndPoint(IPAddress.Any, port); + } + + public static void UDPClose() + { + listener.Close(); + } + + public static byte[] GetImage() + { + bool done = false; + + try + { + while (!done) + { + Console.WriteLine("Waiting for broadcast"); + byte[] bytes = listener.Receive(ref groupEP); + + Console.WriteLine("Received broadcast from {0} :\n {1}\n", + groupEP.ToString(), + Encoding.ASCII.GetString(bytes, 0, bytes.Length)); + + if (bytes[0]=='I') { + // Nouvelle trame recu + ImageBuffer = bytes; + } + else if (bytes[bytes.Length-1]=='D') + { + Array.Resize(ref ImageBuffer, ImageBuffer.Length + bytes.Length); // resize currrent buffer + + System.Buffer.BlockCopy(ImageBuffer, 0, bytes, ImageBuffer.Length-bytes.Length, bytes.Length); + done = true; + } + else{ + Array.Resize(ref ImageBuffer, ImageBuffer.Length + bytes.Length); // resize currrent buffer + + System.Buffer.BlockCopy(ImageBuffer, 0, bytes, ImageBuffer.Length-bytes.Length, bytes.Length); + } + } + + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + + return ImageBuffer; + } + } +} diff --git a/software/monitor/monitor/monitor.csproj b/software/monitor/monitor/monitor.csproj index 934b3be..fb7ff39 100644 --- a/software/monitor/monitor/monitor.csproj +++ b/software/monitor/monitor/monitor.csproj @@ -70,6 +70,7 @@ + \ No newline at end of file diff --git a/software/raspberry/superviseur-robot/lib/src/server.cpp b/software/raspberry/superviseur-robot/lib/src/server.cpp index bb5bcdf..6b9b78d 100644 --- a/software/raspberry/superviseur-robot/lib/src/server.cpp +++ b/software/raspberry/superviseur-robot/lib/src/server.cpp @@ -19,15 +19,22 @@ #include #include #include +#include #define NB_CONNECTION_MAX 1 int socketFD = -1; int clientID = -1; +int socketUDP= -1; +struct sockaddr_in UDPcliaddr; +int UDPcliaddrlen = -1; +char *UDPBuffer=0; + int openServer(int port) { struct sockaddr_in server; - + struct sockaddr_in serverUDP; + socketFD = socket(AF_INET, SOCK_STREAM, 0); if (socketFD < 0) { perror("Can not create socket"); @@ -45,13 +52,33 @@ int openServer(int port) { listen(socketFD, NB_CONNECTION_MAX); + /* Open UDP connection */ + socketUDP = socket(AF_INET, SOCK_DGRAM, 0); + if (socketUDP < 0) { + perror("Can not create UDP socket"); + exit(-1); + } + + serverUDP.sin_addr.s_addr = INADDR_ANY; + serverUDP.sin_family = AF_INET; + serverUDP.sin_port = htons(port+1); + + memset(&UDPcliaddr, 0, sizeof(UDPcliaddr)); + + if (bind(socketUDP, (struct sockaddr *) &serverUDP, sizeof (serverUDP)) < 0) { + perror("Can not bind UDP socket"); + exit(-1); + } + return socketFD; } int closeServer() { close(socketFD); - + close(socketUDP); + socketFD = -1; + socketUDP = -1; return 0; } @@ -80,6 +107,40 @@ int sendDataToServerForClient(int client, char *data, int length) { else return 0; } +void waitUDPClientAddr(void) +{ + char buffer[10]; + recvfrom(socketUDP, (char *)buffer, 10, + MSG_WAITALL, ( struct sockaddr *) &UDPcliaddr, &UDPcliaddrlen); +} + +int sendImage(char *data, int length) { + if (clientID >= 0) + { + UDPBuffer= (char*)malloc(length+12); + + UDPBuffer[0]='I'; + UDPBuffer[1]='N'; + UDPBuffer[2]='S'; + UDPBuffer[3]='A'; + UDPBuffer[4]='B'; + UDPBuffer[5]='G'; + + UDPBuffer[length+6]='A'; + UDPBuffer[length+7]='S'; + UDPBuffer[length+8]='N'; + UDPBuffer[length+9]='I'; + UDPBuffer[length+10]='E'; + UDPBuffer[length+11]='D'; + + memcpy((void*)(UDPBuffer+6),(const void *)data, length); + + return sendto(socketUDP, data, length, + MSG_CONFIRM, (const struct sockaddr *) &UDPcliaddr, UDPcliaddrlen); + } + else return 0; +} + int receiveDataFromServer(char *data, int size) { return receiveDataFromServerFromClient(clientID, data, size); } diff --git a/software/raspberry/testeur/testeur/nbproject/Makefile-Debug.mk b/software/raspberry/testeur/testeur/nbproject/Makefile-Debug.mk index 65daf72..5fcde42 100644 --- a/software/raspberry/testeur/testeur/nbproject/Makefile-Debug.mk +++ b/software/raspberry/testeur/testeur/nbproject/Makefile-Debug.mk @@ -69,27 +69,27 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testeur: ${OBJECTFILES} ${OBJECTDIR}/_ext/e4d40e25/image.o: ../../superviseur-robot/lib/src/image.cpp ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${RM} "$@.d" - $(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/image.o ../../superviseur-robot/lib/src/image.cpp + $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/image.o ../../superviseur-robot/lib/src/image.cpp ${OBJECTDIR}/_ext/e4d40e25/message.o: ../../superviseur-robot/lib/src/message.cpp ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${RM} "$@.d" - $(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/message.o ../../superviseur-robot/lib/src/message.cpp + $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/message.o ../../superviseur-robot/lib/src/message.cpp ${OBJECTDIR}/_ext/e4d40e25/robot.o: ../../superviseur-robot/lib/src/robot.cpp ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${RM} "$@.d" - $(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/robot.o ../../superviseur-robot/lib/src/robot.cpp + $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/robot.o ../../superviseur-robot/lib/src/robot.cpp ${OBJECTDIR}/_ext/e4d40e25/server.o: ../../superviseur-robot/lib/src/server.cpp ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${RM} "$@.d" - $(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/server.o ../../superviseur-robot/lib/src/server.cpp + $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/server.o ../../superviseur-robot/lib/src/server.cpp ${OBJECTDIR}/main.o: main.cpp ${MKDIR} -p ${OBJECTDIR} ${RM} "$@.d" - $(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp + $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp # Subprojects .build-subprojects: diff --git a/software/raspberry/testeur/testeur/nbproject/private/private.xml b/software/raspberry/testeur/testeur/nbproject/private/private.xml index 5985810..4532088 100644 --- a/software/raspberry/testeur/testeur/nbproject/private/private.xml +++ b/software/raspberry/testeur/testeur/nbproject/private/private.xml @@ -7,12 +7,9 @@ - file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/image.h - file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp - file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp - file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/robot.cpp - file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/testeur/testeur/main.cpp - file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/robot.h + file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp + file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp + file:/home/dimercur/Documents/Travail/dumber/software/raspberry/testeur/testeur/main.cpp