#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define OG_SHIFTLIGHT 1
#define OG_FULLBEAM 2
#define OG_HANDBRAKE 4
#define OG_PITSPEED 8
#define OG_TC 16
#define OG_HEADLIGHTS 32
#define OG_SIGNAL_L 64
#define OG_SIGNAL_R 128
#define OG_REDLINE 256
#define OG_OILWARN 512
#define OG_1 1024
#define OG_2 2048
#define OG_3 4096
#define OG_4 8192
#define OG_KM 16384
#define OG_BAR 32768
#define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
#define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
#define MYPORT 55555 // the port users will be connecting to
typedef struct
{
unsigned int Time; // time in milliseconds (to check order)
char Car [4]; // Car name
unsigned short Flags; // OG_FLAGS (see below)
unsigned char Gear; // Reverse:0, Neutral:1, First:2...
unsigned char SpareB;
float Speed; // M/S
float RPM; // RPM
float Turbo; // BAR
float EngTemp; // C
float Fuel; // 0 to 1
float OilPress; // BAR
float Spare1;
float Spare2;
float Spare3;
float Throttle; // 0 to 1
float Brake; // 0 to 1
float Clutch; // 0 to 1
char Display1[16]; // Usually Fuel
char Display2[16]; // Usually Settings
int ID;
} __attribute__ ((packed)) OutGaugePacket;
float floatSwap (float value)
{
//char *value = (char*) &fl;
char buffer[ 4 ];
buffer[ 0 ] = ((char*)&value)[ 3 ];
buffer[ 1 ] = ((char*)&value)[ 2 ];
buffer[ 2 ] = ((char*)&value)[ 1 ];
buffer[ 3 ] = ((char*)&value)[ 0 ];
return *( (float *) &buffer );
}
int main(void)
{
int sockfd; // let's make a file descriptor
OutGaugePacket og_packet; // let's create an packet
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
memset(&(og_packet),'\0',96); // zero out the struct
//let's create the socket
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
// setting up our contact information here
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
//binding our socket to a port and address
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
for (;;){
// receiving the goodies
if ((numbytes=recvfrom(sockfd, (void *)&og_packet, sizeof(OutGaugePacket) , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("\ngot packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n\n", numbytes);
printf("Contents of packet:\n");
printf("Time = %u \t\tmemory address is %#X\n", NTOHL(og_packet.Time), &og_packet.Time);
printf("Car = %s \t\t\tmemory address is %#X\n", &og_packet.Car, &og_packet.Car);
printf("Gear = %d \t\t\tmemory address is %#X\n", og_packet.Gear -1, &og_packet.Gear);
printf("SpareB = %d \t\tmemory address is %#X\n", og_packet.SpareB, &og_packet.Gear);
printf("Speed = %.f \t\tmemory address is %#X\n", (floatSwap(og_packet.Speed)) * 3.6, &og_packet.Speed);
printf("RPM = %.f \t\tmemory address is %#X\n", floatSwap(og_packet.RPM), &og_packet.RPM);
printf("Turbo = %f \tmemory address is %#X\n", floatSwap(og_packet.Turbo), &og_packet.Turbo);
printf("EngTemp = %.f \t\tmemory address is %#X\n", floatSwap(og_packet.EngTemp), &og_packet.EngTemp);
printf("Fuel = %f \tmemory address is %#X\n", floatSwap(og_packet.Fuel), &og_packet.Fuel);
printf("OilPress = %.2f \tmemory address is %#X\n", floatSwap(og_packet.OilPress), &og_packet.OilPress);
printf("Spare1 = %.2f \tmemory address is %#X\n", floatSwap(og_packet.Spare1), &og_packet.Spare1);
printf("Spare2 = %.2f \tmemory address is %#X\n", floatSwap(og_packet.Spare2), &og_packet.Spare2);
printf("Spare3 = %.2f \tmemory address is %#X\n", floatSwap(og_packet.Spare3), &og_packet.Spare3);
printf("Throttle = %.f%% \tmemory address is %#X\n", (floatSwap(og_packet.Throttle) * 100), &og_packet.Throttle);
printf("Brake = %.f%% \t\tmemory address is %#X\n", (floatSwap(og_packet.Brake)* 100), &og_packet.Brake);
printf("Clutch = %.f%% \t\tmemory address is %#X\n", (floatSwap(og_packet.Clutch)* 100), &og_packet.Clutch);
printf("Display1 = %s\t\t\t%#X\n", &og_packet.Display1, &og_packet.Display1);
printf("Display2 = %s\t\t\t%#X\n", &og_packet.Display2, &og_packet.Display2);
printf("ID = %u \t\t\tmemory address is %#X\n", NTOHL(og_packet.ID), &og_packet.ID);
}
close(sockfd);
return 0;
}
got packet from 192.168.1.2
packet is 96 bytes long
Contents of packet:
Time = 167580 memory address is 0XBFFFF8B4
Car = RB4 memory address is 0XBFFFF8B8
Gear = 3 memory address is 0XBFFFF8BE
SpareB = 0 memory address is 0XBFFFF8BE
Speed = 96 memory address is 0XBFFFF8C0
RPM = 7090 memory address is 0XBFFFF8C4
Turbo = 0.082909 memory address is 0XBFFFF8C8
EngTemp = 0 memory address is 0XBFFFF8CC
Fuel = 0.069851 memory address is 0XBFFFF8D0
OilPress = 0.00 memory address is 0XBFFFF8D4
Spare1 = 0.00 memory address is 0XBFFFF8D8
Spare2 = 0.00 memory address is 0XBFFFF8DC
Spare3 = 0.00 memory address is 0XBFFFF8E0
Throttle = 80% memory address is 0XBFFFF8E4
Brake = 0% memory address is 0XBFFFF8E8
Clutch = 20% memory address is 0XBFFFF8EC
Display1 = Fuel 5.2 0XBFFFF8F0
Display2 = Brake Bal F 73% 0XBFFFF900
ID = 1 memory address is 0XBFFFF910
[Session started at 2007-04-24 02:01:01 +0200.]
got packet from 192.168.1.2
packet is 96 bytes long
Time = 23690, memory address is BFFFF910
Car = FXO, memory address is BFFFF914
Speed = 133.660039, memory address is BFFFF91C
RPM = 6800.274902, memory address is BFFFF920
ID of connection = 1
sizeof struct = 96
OutGuage has exited with status 0.
float floatSwap(char *value)
{
char buffer[ 4 ];
buffer[ 0 ] = value[ 3 ];
buffer[ 1 ] = value[ 2 ];
buffer[ 2 ] = value[ 1 ];
buffer[ 3 ] = value[ 0 ];
return *( (float *) &buffer );
}
printf("RPM = %f, memory address is %X\n", floatSwap(&og_packet.RPM), &og_packet.RPM);
warning: passing argument 1 of 'floatSwap' from incompatible pointer type
[Session started at 2007-04-24 01:51:33 +0200.]
got packet from 192.168.1.2
packet is 96 bytes long
Time = 21180, memory address is BFFFF880
Car = FXO, memory address is BFFFF884
Speed = 0.000000, memory address is A
RPM = 7941.076172, memory address is BFFFF890
ID of connection = 1
sizeof struct = 96
RPM = 2432237568.000000, memory address is BFFFF890
OutGuageFoundation has exited with status 0.
[Session started at 2007-04-24 01:11:59 +0200.]
got packet from 192.168.1.2
packet is 96 bytes long
Time = 255830, memory address is BFFFF790
Car = RB4, memory address is BFFFF794
Speed = 0.000000, memory address is A
RPM = -419242517033593602048.000000, memory address is BFFFF7A0
ID of connection = 1
sizeof struct = 96
RPM = 128.000000, memory address is BFFFF7A0
RPM = 128.000000, memory address is BFFFF7A0
RPM = 128.000000, memory address is BFFFF7A0
OutGuage has exited with status 0.
[Session started at 2007-04-24 01:12:04 +0200.]
got packet from 192.168.1.2
packet is 96 bytes long
Time = 261400, memory address is BFFFF790
Car = RB4, memory address is BFFFF794
Speed = nan, memory address is A
RPM = -16382559176468975824404480.000000, memory address is BFFFF7A0
ID of connection = 1
sizeof struct = 96
RPM = 128.000000, memory address is BFFFF7A0
RPM = 128.000000, memory address is BFFFF7A0
RPM = 128.000000, memory address is BFFFF7A0
OutGuage has exited with status 0.
// 4-byte number
int INT_little_endian_TO_big_endian(int i)
{
return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
}
[Session started at 2007-04-24 00:31:52 +0200.]
got packet from 192.168.1.2
packet is 96 bytes long
Time = 2929786880, memory address is BFFFF950
Car = RB4, memory address is BFFFF954
Speed = 1587048.125000, memory address is BFFFF95C
RPM = -29579291864432017246519296.000000, memory address is BFFFF960
ID of connection = 16777216
sizeof struct = 96
RPM = 2147483648.000000, memory address is BFFFF960
RPM = 2147483648.000000, memory address is BFFFF960
OutGuage has exited with status 0.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define OG_SHIFTLIGHT 1
#define OG_FULLBEAM 2
#define OG_HANDBRAKE 4
#define OG_PITSPEED 8
#define OG_TC 16
#define OG_HEADLIGHTS 32
#define OG_SIGNAL_L 64
#define OG_SIGNAL_R 128
#define OG_REDLINE 256
#define OG_OILWARN 512
#define OG_1 1024
#define OG_2 2048
#define OG_3 4096
#define OG_4 8192
#define OG_KM 16384
#define OG_BAR 32768
#define MYPORT 55555 // the port users will be connecting to
#define MAXBUFLEN 92
typedef struct
{
unsigned int Time; // time in milliseconds (to check order)
char Car [4]; // Car name
unsigned short Flags; // OG_FLAGS (see below)
unsigned char Gear; // Reverse:0, Neutral:1, First:2...
unsigned char SpareB;
float Speed; // M/S
float RPM; // RPM
float Turbo; // BAR
float EngTemp; // C
float Fuel; // 0 to 1
float OilPress; // BAR
float Spare1;
float Spare2;
float Spare3;
float Throttle; // 0 to 1
float Brake; // 0 to 1
float Clutch; // 0 to 1
char Display1[16]; // Usually Fuel
char Display2[16]; // Usually Settings
int ID;
} __attribute__ ((packed)) OutGaugePacket;
int main(void)
{
int sockfd;
OutGaugePacket og_packet;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
//memset(&(og_packet),'\0',95); // zero out the struct
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if ((numbytes=recvfrom(sockfd, &og_packet, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("\n\ngot packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n\n", numbytes);
printf("Time = %u, memory address is %X\n", og_packet.Time &og_packet.Time);
printf("Car = %s, memory address is %X\n", &og_packet.Car, &og_packet.Car);
printf("Speed = %f, memory address is %X\n", og_packet.Speed, &og_packet.Speed);
printf("RPM = %f, memory address is %X\n", og_packet.RPM, &og_packet.RPM);
printf("ID of connection = %d", og_packet.ID);
printf("\nsizeof struct = %d", sizeof(og_packet));
close(sockfd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define OG_SHIFTLIGHT 1
#define OG_FULLBEAM 2
#define OG_HANDBRAKE 4
#define OG_PITSPEED 8
#define OG_TC 16
#define OG_HEADLIGHTS 32
#define OG_SIGNAL_L 64
#define OG_SIGNAL_R 128
#define OG_REDLINE 256
#define OG_OILWARN 512
#define OG_1 1024
#define OG_2 2048
#define OG_3 4096
#define OG_4 8192
#define OG_KM 16384
#define OG_BAR 32768
#define MYPORT 55555 // the port users will be connecting to
#define MAXBUFLEN 92
typedef struct
{
unsigned int Time; // time in milliseconds (to check order)
char Car [4]; // Car name
unsigned short Flags; // OG_FLAGS (see below)
unsigned char Gear; // Reverse:0, Neutral:1, First:2...
unsigned char SpareB;
float Speed; // M/S
float RPM; // RPM
float Turbo; // BAR
float EngTemp; // C
float Fuel; // 0 to 1
float OilPress; // BAR
float Spare1;
float Spare2;
float Spare3;
float Throttle; // 0 to 1
float Brake; // 0 to 1
float Clutch; // 0 to 1
char Display1[16]; // Usually Fuel
char Display2[16]; // Usually Settings
}OutGauge;
int main(void)
{
int sockfd;
OutGauge new;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
memset(&(new),'\0',95); // zero out the struct
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if ((numbytes=recvfrom(sockfd, &new, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
new.Speed = htonl(new.Speed);
printf("\n\ngot packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n\n", numbytes);
printf("Time = %u, memory address is %X\n", new.Time &new.Time);
printf("Car = %s, memory address is %X\n", &new.Car, &new.Car);
printf("Speed = %f, memory address is %X\n", new.Speed, &new.Speed);
printf("RPM = %f, memory address is %X\n", new.RPM, &new.RPM);
close(sockfd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define OG_SHIFTLIGHT 1
#define OG_FULLBEAM 2
#define OG_HANDBRAKE 4
#define OG_PITSPEED 8
#define OG_TC 16
#define OG_HEADLIGHTS 32
#define OG_SIGNAL_L 64
#define OG_SIGNAL_R 128
#define OG_REDLINE 256
#define OG_OILWARN 512
#define OG_1 1024
#define OG_2 2048
#define OG_3 4096
#define OG_4 8192
#define OG_KM 16384
#define OG_BAR 32768
#define MYPORT 55555 // the port users will be connecting to
#define MAXBUFLEN 96
typedef struct {
unsigned Time;
char Car[4];
char Flags[2];
char Gear;
char SpareB;
float Speed;
float RPM;
float Turbo; // BAR
float EngTemp; // C
float Fuel; // 0 to 1
float OilPress; // BAR
float Spare1;
float Spare2;
float Spare3;
float Throttle; // 0 to 1
float Brake; // 0 to 1
float Clutch; // 0 to 1
char Display1[16]; // Usually Fuel
char Display2[16]; // Usually Settings
} OutGauge;
int main(void)
{
int sockfd;
OutGauge new;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
memset(&(new),'\0',96); // zero out the struct
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if ((numbytes=recvfrom(sockfd, &new, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n\n", numbytes);
printf("Time = %u\n", new.Time);
printf("Car = %s\n", &new.Car);
printf("Speed = %f\n", new.Speed);
printf("RPM = %f\n", new.RPM);
close(sockfd);
return 0;
}