diff --git a/makefile b/makefile index 33d3a8e..0a661f8 100755 --- a/makefile +++ b/makefile @@ -8,8 +8,7 @@ SRC=src # with debug: #CFLAGS:= $(CFLAGS) -O3 -Wall -g -CFLAGS:= $(CFLAGS) -O3 -Wall - +CFLAGS:= $(CFLAGS) -std=c11 -O3 -Wall -D_POSIX_C_SOURCE all: objdir imgcomp objdir: diff --git a/src/compare.c b/src/compare.c index 8749abf..d3a4d0c 100755 --- a/src/compare.c +++ b/src/compare.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "imgcomp.h" #include "config.h" diff --git a/src/compare_util.c b/src/compare_util.c index b8c274d..1a2bcd0 100755 --- a/src/compare_util.c +++ b/src/compare_util.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "imgcomp.h" #include "config.h" diff --git a/src/config.c b/src/config.c index 182b6d4..96dc2e7 100755 --- a/src/config.c +++ b/src/config.c @@ -8,6 +8,7 @@ #include // to declare isupper(), tolower() #include #include +#include #include "imgcomp.h" #include "config.h" diff --git a/src/send_udp.c b/src/send_udp.c index 95f4358..d8f3ab7 100755 --- a/src/send_udp.c +++ b/src/send_udp.c @@ -1,190 +1,182 @@ -//---------------------------------------------------------------------------------- -// Module for sending UDP notifcations to stepper program, -// for aming heater, fan, or cap shooter at kids and squirrels. -//---------------------------------------------------------------------------------- -#include -#include -#include "imgcomp.h" - -#ifdef _WIN32 - #define WIN32_LEAN_AND_MEAN // To keep windows.h bloat down. - #ifdef OLD_WINSOCK - #include - #else - #include - #endif - #include -#else - #include - #include - #include - #include - #include - #include - #include - #include - typedef unsigned char BYTE; - typedef unsigned char UCHAR; - typedef unsigned short USHORT; - typedef unsigned long ULONG; - typedef int SOCKET; - typedef int BOOL; - #define FALSE 0 - #define TRUE 1 - #define SOCKET_ERROR -1 - #define INVALID_SOCKET -1 - typedef fd_set FD_SET; - typedef struct timeval TIMEVAL; -#endif - - - -#define MAGIC_ID 0xf581 // Magic value to identify pings from this program. -#define MAGIC_PORTNUM 7777 // Magic port number for UDP pings (this is the port Joe picked for his app) -#define UDP_MAGIC 0x46c1 - -//------------------------------------------------------------------------------------- -// Structure for a test UDP packet. -typedef struct { - int Ident; - int Level; - int xpos; - int ypos; - int IsAdjust; - int Motion; -}Udp_t; - -//------------------------------------------------------------------------------------- - -#define STATUS_FAILED 0xFFFF - -struct sockaddr_in dest; -static SOCKET sockUDP; - -//-------------------------------------------------------------------------- -// Form and send a UDP packet -//-------------------------------------------------------------------------- -void SendUDP(int x, int y, int level, int motion) -{ - int wrote; - int datasize; - Udp_t Buf; - - if (!dest.sin_port){ - fprintf(stderr, "UDP not initialized\n"); - return; - } - - memset(&Buf, 0, sizeof(Buf)); - - Buf.Ident = UDP_MAGIC; - Buf.Level = level; - Buf.Motion = motion; - Buf.xpos = x; - Buf.ypos = y; - Buf.IsAdjust = 0; - - datasize = sizeof(Udp_t); - - wrote = sendto(sockUDP,(char *)&Buf, datasize, 0,(struct sockaddr*)&dest, sizeof(struct sockaddr_in)); - - if (wrote == SOCKET_ERROR){ - perror("UDP sendto failed"); - - #ifndef _WIN32 - printf("On Linux, This eventually happens if the destination port is unreacable and Uping is not root\n"); - #endif - exit(-1); - } - if (wrote < datasize ) { - fprintf(stdout,"Wrote %d bytes of %d\n",wrote, datasize); - } - - //printf("Sent UDP packet, x=%d\n",x); -} - -//-------------------------------------------------------------------------- -// Main -//-------------------------------------------------------------------------- -int InitUDP(char * HostName) -{ - #ifdef _WIN32 - WSADATA wsaData; - #endif - - struct hostent * hp; - unsigned int addr=0; - unsigned short PortNum = MAGIC_PORTNUM; // Use a different port, leave this for listener - - //------------------------------------------------------------------- - // Resolve the remote address - printf("Init UDP to %s",HostName); - - memset(&dest,0,sizeof(struct sockaddr_in)); - - if (HostName){ - hp = gethostbyname(HostName); - - if (hp == NULL){ - // If address is not a host name, assume its a x.x.x.x format address. - addr = inet_addr(HostName); - if (addr == INADDR_NONE) { - // Not a number format address either. Give up. - fprintf(stderr,"Unable to resolve %s\n",HostName); - exit(-1); - } - dest.sin_addr.s_addr = addr; - dest.sin_family = AF_INET; - - }else{ - // Resolved to a host name. - memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length); - dest.sin_family = hp->h_addrtype; - } - }else{ - // No host name specified. Use loopback address by default. - printf("No host name specified. Using 'loopback' as target\n"); - addr = inet_addr("127.0.0.1"); - dest.sin_addr.s_addr = addr; - dest.sin_family = AF_INET; - } - - dest.sin_port = htons(PortNum); - - //------------------------------------------------------------------- - // Open socket for reception of regular UDP packets. - { - struct sockaddr_in local; - - local.sin_family = AF_INET; - local.sin_addr.s_addr = INADDR_ANY; - - local.sin_port = htons(PortNum+2); - sockUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - - if (sockUDP == INVALID_SOCKET){ - perror("couldn't create UDP socket"); - exit(-1); - } - - if (bind(sockUDP,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR) { - #ifdef _WIN32 - int err; - err = WSAGetLastError(); - if (err != WSAEADDRINUSE){ - perror("bind() failed with error"); - WSACleanup(); - exit(-1); - }else{ - printf("Listen port already in use\n"); - } - #else - perror("bind failed"); - exit(-1); - #endif - } - } - - return 0; -} - +//---------------------------------------------------------------------------------- +// Module for sending UDP notifcations to stepper program, +// for aming heater, fan, or cap shooter at kids and squirrels. +//---------------------------------------------------------------------------------- +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "imgcomp.h" +typedef unsigned char BYTE; +typedef unsigned char UCHAR; +typedef unsigned short USHORT; +typedef unsigned long ULONG; +typedef int SOCKET; +typedef int BOOL; +#define FALSE 0 +#define TRUE 1 +#define SOCKET_ERROR -1 +#define INVALID_SOCKET -1 +typedef fd_set FD_SET; +typedef struct timeval TIMEVAL; +#define h_addr h_addr_list[0] + + +#define MAGIC_ID 0xf581 // Magic value to identify pings from this program. +#define MAGIC_PORTNUM 7777 // Magic port number for UDP pings (this is the port Joe picked for his app) +#define UDP_MAGIC 0x46c1 + +//------------------------------------------------------------------------------------- +// Structure for a test UDP packet. +typedef struct { + int Ident; + int Level; + int xpos; + int ypos; + int IsAdjust; + int Motion; +}Udp_t; + +//------------------------------------------------------------------------------------- + +#define STATUS_FAILED 0xFFFF + +struct sockaddr_in dest; +static SOCKET sockUDP; + +//-------------------------------------------------------------------------- +// Form and send a UDP packet +//-------------------------------------------------------------------------- +void SendUDP(int x, int y, int level, int motion) +{ + int wrote; + int datasize; + Udp_t Buf; + + if (!dest.sin_port){ + fprintf(stderr, "UDP not initialized\n"); + return; + } + + memset(&Buf, 0, sizeof(Buf)); + + Buf.Ident = UDP_MAGIC; + Buf.Level = level; + Buf.Motion = motion; + Buf.xpos = x; + Buf.ypos = y; + Buf.IsAdjust = 0; + + datasize = sizeof(Udp_t); + + wrote = sendto(sockUDP,(char *)&Buf, datasize, 0,(struct sockaddr*)&dest, sizeof(struct sockaddr_in)); + + if (wrote == SOCKET_ERROR){ + perror("UDP sendto failed"); + + #ifndef _WIN32 + printf("On Linux, This eventually happens if the destination port is unreacable and Uping is not root\n"); + #endif + exit(-1); + } + if (wrote < datasize ) { + fprintf(stdout,"Wrote %d bytes of %d\n",wrote, datasize); + } + + //printf("Sent UDP packet, x=%d\n",x); +} + +//-------------------------------------------------------------------------- +// Main +//-------------------------------------------------------------------------- +int InitUDP(char * HostName) +{ + #ifdef _WIN32 + WSADATA wsaData; + #endif + + struct hostent * hp; + unsigned int addr=0; + unsigned short PortNum = MAGIC_PORTNUM; // Use a different port, leave this for listener + + //------------------------------------------------------------------- + // Resolve the remote address + printf("Init UDP to %s",HostName); + + memset(&dest,0,sizeof(struct sockaddr_in)); + + if (HostName){ + hp = gethostbyname(HostName); + + if (hp == NULL){ + // If address is not a host name, assume its a x.x.x.x format address. + addr = inet_addr(HostName); + if (addr == INADDR_NONE) { + // Not a number format address either. Give up. + fprintf(stderr,"Unable to resolve %s\n",HostName); + exit(-1); + } + dest.sin_addr.s_addr = addr; + dest.sin_family = AF_INET; + + }else{ + // Resolved to a host name. + memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length); + dest.sin_family = hp->h_addrtype; + } + }else{ + // No host name specified. Use loopback address by default. + printf("No host name specified. Using 'loopback' as target\n"); + addr = inet_addr("127.0.0.1"); + dest.sin_addr.s_addr = addr; + dest.sin_family = AF_INET; + } + + dest.sin_port = htons(PortNum); + + //------------------------------------------------------------------- + // Open socket for reception of regular UDP packets. + { + struct sockaddr_in local; + + local.sin_family = AF_INET; + local.sin_addr.s_addr = INADDR_ANY; + + local.sin_port = htons(PortNum+2); + sockUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (sockUDP == INVALID_SOCKET){ + perror("couldn't create UDP socket"); + exit(-1); + } + + if (bind(sockUDP,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR) { + #ifdef _WIN32 + int err; + err = WSAGetLastError(); + if (err != WSAEADDRINUSE){ + perror("bind() failed with error"); + WSACleanup(); + exit(-1); + }else{ + printf("Listen port already in use\n"); + } + #else + perror("bind failed"); + exit(-1); + #endif + } + } + + return 0; +} +