-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathomnikgetstats.c
More file actions
90 lines (76 loc) · 2.33 KB
/
omnikgetstats.c
File metadata and controls
90 lines (76 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
** omnikgetstats.c
**
** Connects to the Omnik via TCP
** Generates and sends the 'magic' message to the Omnik
** receives the statistics
**
** Returns: None zero on error
** binary data (string) from the Omnik
**
** Author: Beach
** V1.0 may,23 2013
** V1.1 sept, 13 2013. Added optional IPP and Serial number in conf file
*/
#include <stdio.h>
#include <errno.h>
#include <arpa/inet.h>
#include <string.h>
#include "omnikstats.h"
int omnikgetstats(char *server_reply) {
int checksum = 0;
int i;
int socket_desc;
struct sockaddr_in server;
// generate the magic message
// first 4 bytes are fixed x68 x02 x40 x30
// next 8 bytes are the reversed serial number twice(hex)
// next 2 bytes are fixed x01 x00
// next byte is a checksum (2x each binary number form the serial number + 115)
// last byte is fixed x16
char magicmessage[] = {0x68, 0x02, 0x40, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x16};
for (i=0; i<4; i++) {
magicmessage[4+i] = magicmessage[8+i] = ((stats.serial_number>>(8*i))&0xff);
checksum += magicmessage[4+i];
}
checksum *= 2;
checksum += 115;
checksum &= 0xff;
magicmessage[14] = checksum;
// printf("checksum %x\n", checksum);
// for (i=0; i<16; i++) {
// printf("%x ", (unsigned char) magicmessage[i]);
// }
// printf("\n\n");
// Now create a TCP socket for communication with the Omnik
if ((socket_desc = socket(AF_INET , SOCK_STREAM , 0)) == -1) {
printf("Could not create TCP socket");
return(1);
}
server.sin_addr.s_addr = inet_addr(stats.IPnumber);
server.sin_family = AF_INET;
server.sin_port = htons(OMNIKPORT); //port 8899
//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0) {
printf("TCP connect error: %s\n", strerror(errno));
return(1);
}
puts("Connected\n");
//Send the magic message
if (send(socket_desc , magicmessage , 16 , 0) < 0) {
puts("Send failed");
return(1);
}
// printf("Data Send %s\n", (char*) magicmessage);
//Receive a reply from the server
memset(server_reply, 0, 256);
if ((i = (recv(socket_desc, server_reply , 256 , 0))) < 0) {
puts("recv failed");
return(1);
}
// printf("Reply received %d nr of Bytes\n", i);
// for (i=0; i<256; i++) {
// printf("Byte %d\t%#x\t%u\t%c\n",i,server_reply[i], server_reply[i], server_reply[i]);
// }
return(0);
}