Blame view

bus.c 2.21 KB
d4d5bb6d   henyxia   Bus working
1
2
3
4
5
6
7
8
9
10
11
  #include <stdio.h>
  #include <stdbool.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <errno.h>
  #include <string.h>
  #include "printx.h"
  #include "ui.h"
  #include "nfc.h"
ac0ae6b0   henyxia   HVC can now use t...
12
  #include "hvc.h"
d4d5bb6d   henyxia   Bus working
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
  #define	CMD_MAX	70
  
  int		bus;
  bool	busFree = true;
  bool	busStop = false;
  
  void stopBus()
  {
  	busStop = true;
  }
  
  void processCmd(char* buffer)
  {
  	buffer[strlen(buffer)-1]='\0';
  	if(strcmp(buffer, "quit") == 0 || strcmp(buffer, "exit") == 0)
  	{
  		printx(INFO, BUS, "Exit request receved, processing ...\n");
  		stopNFC();
ac0ae6b0   henyxia   HVC can now use t...
32
  		stopHVC();
d4d5bb6d   henyxia   Bus working
33
34
35
  		stopBus();
  		stopUI();
  	}
ac0ae6b0   henyxia   HVC can now use t...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  	else if(strcmp(buffer, "setpumpon") == 0)
  	{
  		printx(INFO, BUS, "Setting PUMP ON");
  		setPumpWantedState(true);
  	}
  	else if(strcmp(buffer, "setpumpoff") == 0)
  	{
  		printx(INFO, BUS, "Setting PUMP OFF");
  		setPumpWantedState(false);
  	}
  	else if(strcmp(buffer, "setheaton") == 0)
  	{
  		printx(INFO, BUS, "Setting HEAT ON");
  		setHeatWantedState(true);
  	}
  	else if(strcmp(buffer, "setheatoff") == 0)
  	{
  		printx(INFO, BUS, "Setting HEAT OFF");
  		setHeatWantedState(false);
  	}
5dd66ab0   henyxia   Added timer optio...
56
57
58
59
60
61
62
63
64
65
66
67
  	else if(strcmp(buffer, "setheaton5s") == 0)
  	{
  		printx(INFO, BUS, "Setting HEAT ON for 5 secs");
  		setHeatTimer(5);
  		setHeatWantedState(true);
  	}
  	else if(strcmp(buffer, "setpumpon5s") == 0)
  	{
  		printx(INFO, BUS, "Setting PUMP ON for 5 secs");
  		setPumpTimer(5);
  		setPumpWantedState(true);
  	}
ac0ae6b0   henyxia   HVC can now use t...
68
69
  
  	//printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
d4d5bb6d   henyxia   Bus working
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  }
  
  void* processBus(void* we)
  {
  	bus = open("bus.log", O_RDWR);
  	char	buffer[CMD_MAX];
  	printx(DEBUG, BUS, "Waiting for events\n");
  	while(!busStop)
  	{
  		while(busFree);
  		while(!busFree);
  		printx(DEBUG, BUS, "Event receved !\n");
  		busFree = false;
  		lseek(bus, 0, SEEK_SET);
  		printx(DEBUG, BUS, "Data read %d\n", read(bus, buffer, CMD_MAX));
  		ftruncate(bus, 0);
  		sync();
  		processCmd(buffer);
  		busFree = true;
  		printx(DEBUG, BUS, buffer);
  	}
  	return NULL;
  }
  
  bool initBus()
  {
  	bus = creat("bus.log", 0666);
  	if(bus == -1)
  	{
  		printf("Unable to open the bus file\n");
  		return false;
  	}
  
  	return true;
  }
  
  void sendToBus(char* cmd)
  {
  	while(!busFree);
  	busFree = false;
  	ftruncate(bus, 0);
  	ftruncate(bus, CMD_MAX);
  	lseek(bus, 0, SEEK_SET);
  	dprintf(bus, "%s\n", cmd);
  	busFree = true;
  }