Blame view

bus.c 2.35 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
  	else if(strcmp(buffer, "setpumpon") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
38
  		printx(INFO, BUS, "Setting PUMP ON");
ac0ae6b0   henyxia   HVC can now use t...
39
40
41
42
  		setPumpWantedState(true);
  	}
  	else if(strcmp(buffer, "setpumpoff") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
43
  		printx(INFO, BUS, "Setting PUMP OFF");
ac0ae6b0   henyxia   HVC can now use t...
44
45
46
47
  		setPumpWantedState(false);
  	}
  	else if(strcmp(buffer, "setheaton") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
48
  		printx(INFO, BUS, "Setting HEAT ON");
ac0ae6b0   henyxia   HVC can now use t...
49
50
51
52
  		setHeatWantedState(true);
  	}
  	else if(strcmp(buffer, "setheatoff") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
53
  		printx(INFO, BUS, "Setting HEAT OFF");
ac0ae6b0   henyxia   HVC can now use t...
54
55
  		setHeatWantedState(false);
  	}
5dd66ab0   henyxia   Added timer optio...
56
57
  	else if(strcmp(buffer, "setheaton5s") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
58
  		printx(INFO, BUS, "Setting HEAT ON for 5 secs");
5dd66ab0   henyxia   Added timer optio...
59
60
61
  		setHeatTimer(5);
  		setHeatWantedState(true);
  	}
c21a2c16   henyxia   Re added timestam...
62
63
64
65
66
67
  	else if(strcmp(buffer, "setheaton35s") == 0)
  	{
  		printx(INFO, BUS, "Setting HEAT ON for 10 secs");
  		setHeatTimer(10);
  		setHeatWantedState(true);
  	}
5dd66ab0   henyxia   Added timer optio...
68
69
  	else if(strcmp(buffer, "setpumpon5s") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
70
  		printx(INFO, BUS, "Setting PUMP ON for 5 secs");
5dd66ab0   henyxia   Added timer optio...
71
72
73
  		setPumpTimer(5);
  		setPumpWantedState(true);
  	}
ac0ae6b0   henyxia   HVC can now use t...
74
75
  
  	//printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
d4d5bb6d   henyxia   Bus working
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  }
  
  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);
2bc2194a   henyxia   Revert "Fixed som...
90
  		printx(DEBUG, BUS, "Data read %d\n", read(bus, buffer, CMD_MAX));
d4d5bb6d   henyxia   Bus working
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
116
117
118
119
120
121
  		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;
  }