-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdaemon_i2c.c
89 lines (61 loc) · 1.81 KB
/
daemon_i2c.c
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
//#include "libs/jsmn/jsmn.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "daemon.h"
#include "i2cslave.h"
#include "commands.h"
#include "utils.h"
int main(int argc, char *argv[]) {
load_orders(orders);
struct daemon daemon;
daemon.curr_status = ACTIVE;
char tx_buffer[TX_BUF_SIZE];
int mode;
int i2c_fd = i2c_init(&mode, argc, argv);
if (argc < 2) {
printf("Please provide ip address as first parameter\n");
exit(EXIT_FAILURE);
}
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(i2c_fd, &rfds);
printf("Sending configure message to master \n");
FILE *place_file = fopen(PLACE_FILE_PATH, "r");
fseek(place_file, 0, SEEK_END);
long fsize = ftell(place_file);
fseek(place_file, 0, SEEK_SET);
char *place_file_str = malloc(fsize + 1);
fread(place_file_str, fsize, 1, place_file);
fclose(place_file);
place_file_str[fsize] = '\0';
char conf_msg[12];
sprintf(conf_msg, "configure;%c", place_file_str[0]);
struct timeval timeval;
timeval.tv_sec = 1;
timeval.tv_usec = 0;
int fd_modified_count = 0;
while (daemon.curr_status != STOPPED) {
switch (daemon.curr_status) {
case ACTIVE:
fd_modified_count = select(i2c_fd + 1, &rfds, NULL, NULL, &timeval);
CHKERR(fd_modified_count);
if (FD_ISSET(i2c_fd, &rfds)) {
printf("received data over i2c\n");
i2c_handle(i2c_fd, tx_buffer, mode, &rfds);
}
break;
default:
close(i2c_fd);
break;
}
//sleep(1);
}
}