d4d5bb6d
henyxia
Bus working
|
1
2
3
4
5
6
7
8
|
#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>
|
c72d79dd
henyxia
Semaphores contro...
|
9
10
|
#include <sys/ipc.h>
#include <sys/sem.h>
|
d4d5bb6d
henyxia
Bus working
|
11
12
13
|
#include "printx.h"
#include "ui.h"
#include "nfc.h"
|
ac0ae6b0
henyxia
HVC can now use t...
|
14
|
#include "hvc.h"
|
8dca7aeb
henyxia
Automatic Pumping...
|
15
16
|
#include "heat.h"
#include "pump.h"
|
d4d5bb6d
henyxia
Bus working
|
17
|
|
c72d79dd
henyxia
Semaphores contro...
|
18
19
20
21
22
|
#define CMD_MAX 70
#define KEY 1100
#define SEM_NUMBER 2
#define SEM_INPUT 0
#define SEM_OUTPUT 1
|
d4d5bb6d
henyxia
Bus working
|
23
24
25
26
|
int bus;
bool busFree = true;
bool busStop = false;
|
c72d79dd
henyxia
Semaphores contro...
|
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
91
92
93
94
95
96
97
98
|
int mySem;
int get_sem(int i)
{
ushort semarr[30];
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}arg;
arg.array = semarr;
semctl(mySem, i, GETALL, arg);
return semarr[i];
}
void show_sem(int i)
{
int val;
val = get_sem(i);
printf("semaphore[%d]=%d\n", i, val);
}
void create_sem(int N)
{
printx(DEBUG, BUS, "create %d semaphores\n", N);
mySem = semget(KEY, N, 0666 | IPC_CREAT);
if(mySem < 0)
printx(ERROR, BUS, "Unable to create the semaphore\n");
}
void init_sem(int N)
{
int j;
int retval;
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}arg;
arg.val = 1;
for (j=0; j<N; j++)
{
retval = semctl(mySem, j, SETVAL, arg);
if(retval < 0)
printx(ERROR, BUS, "Unable to initialize the semaphore\n");
}
}
void PV(int i, int act)
{
struct sembuf op;
int retval;
op.sem_num = i;
op.sem_op = act; //1=V, -1=P
op.sem_flg = 0; //will wait
retval = semop(mySem, &op, 1);
if(retval != 0)
printx(ERROR, BUS, "Error semop will do %d\n", act);
}
void P(int i)
{
PV(i, -1);
}
void V(int i)
{
PV(i, 1);
|
8dca7aeb
henyxia
Automatic Pumping...
|
99
100
|
}
|
d4d5bb6d
henyxia
Bus working
|
101
102
103
104
105
106
107
108
109
110
111
112
113
|
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...
|
114
|
stopHVC();
|
d4d5bb6d
henyxia
Bus working
|
115
116
117
|
stopBus();
stopUI();
}
|
ac0ae6b0
henyxia
HVC can now use t...
|
118
119
|
else if(strcmp(buffer, "setpumpon") == 0)
{
|
2bc2194a
henyxia
Revert "Fixed som...
|
120
|
printx(INFO, BUS, "Setting PUMP ON");
|
ac0ae6b0
henyxia
HVC can now use t...
|
121
122
123
124
|
setPumpWantedState(true);
}
else if(strcmp(buffer, "setpumpoff") == 0)
{
|
2bc2194a
henyxia
Revert "Fixed som...
|
125
|
printx(INFO, BUS, "Setting PUMP OFF");
|
ac0ae6b0
henyxia
HVC can now use t...
|
126
127
128
129
|
setPumpWantedState(false);
}
else if(strcmp(buffer, "setheaton") == 0)
{
|
2bc2194a
henyxia
Revert "Fixed som...
|
130
|
printx(INFO, BUS, "Setting HEAT ON");
|
ac0ae6b0
henyxia
HVC can now use t...
|
131
132
133
134
|
setHeatWantedState(true);
}
else if(strcmp(buffer, "setheatoff") == 0)
{
|
2bc2194a
henyxia
Revert "Fixed som...
|
135
|
printx(INFO, BUS, "Setting HEAT OFF");
|
ac0ae6b0
henyxia
HVC can now use t...
|
136
137
|
setHeatWantedState(false);
}
|
8dca7aeb
henyxia
Automatic Pumping...
|
138
139
140
141
142
143
|
else if(strcmp(buffer, "setheaton2s") == 0)
{
printx(INFO, BUS, "Setting HEAT ON for 2 secs");
setHeatTimer(2);
setHeatWantedState(true);
}
|
5dd66ab0
henyxia
Added timer optio...
|
144
145
|
else if(strcmp(buffer, "setheaton5s") == 0)
{
|
2bc2194a
henyxia
Revert "Fixed som...
|
146
|
printx(INFO, BUS, "Setting HEAT ON for 5 secs");
|
5dd66ab0
henyxia
Added timer optio...
|
147
148
149
|
setHeatTimer(5);
setHeatWantedState(true);
}
|
c72d79dd
henyxia
Semaphores contro...
|
150
|
else if(strcmp(buffer, "setheaton10s") == 0)
|
c21a2c16
henyxia
Re added timestam...
|
151
152
153
154
155
|
{
printx(INFO, BUS, "Setting HEAT ON for 10 secs");
setHeatTimer(10);
setHeatWantedState(true);
}
|
8dca7aeb
henyxia
Automatic Pumping...
|
156
157
158
159
160
161
|
else if(strcmp(buffer, "setheaton15s") == 0)
{
printx(INFO, BUS, "Setting HEAT ON for 15 secs");
setHeatTimer(15);
setHeatWantedState(true);
}
|
5dd66ab0
henyxia
Added timer optio...
|
162
163
|
else if(strcmp(buffer, "setpumpon5s") == 0)
{
|
2bc2194a
henyxia
Revert "Fixed som...
|
164
|
printx(INFO, BUS, "Setting PUMP ON for 5 secs");
|
5dd66ab0
henyxia
Added timer optio...
|
165
166
167
|
setPumpTimer(5);
setPumpWantedState(true);
}
|
8dca7aeb
henyxia
Automatic Pumping...
|
168
169
170
171
172
173
174
175
176
177
178
|
else if(strcmp(buffer, "setpumpon10s") == 0)
{
printx(INFO, BUS, "Setting PUMP ON for 10 secs");
setPumpTimer(10);
setPumpWantedState(true);
}
else if(strcmp(buffer, "autoheat") == 0)
{
printx(INFO, BUS, "Processing Auto Heat to 90°C");
processHeat(NULL);
}
|
bf0cbb04
Pierre Letousey
Automatic regulation
|
179
180
181
182
183
184
|
else if(strcmp(buffer, "stopautoheat") == 0)
{
printx(INFO, BUS, "Auto Heat has been stopped");
stopAutoHeat();
}
|
ac0ae6b0
henyxia
HVC can now use t...
|
185
186
|
//printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
|
d4d5bb6d
henyxia
Bus working
|
187
188
189
190
191
192
193
194
195
|
}
void* processBus(void* we)
{
bus = open("bus.log", O_RDWR);
char buffer[CMD_MAX];
printx(DEBUG, BUS, "Waiting for events\n");
while(!busStop)
{
|
c72d79dd
henyxia
Semaphores contro...
|
196
|
P(SEM_OUTPUT);
|
d4d5bb6d
henyxia
Bus working
|
197
198
199
|
printx(DEBUG, BUS, "Event receved !\n");
busFree = false;
lseek(bus, 0, SEEK_SET);
|
2bc2194a
henyxia
Revert "Fixed som...
|
200
|
printx(DEBUG, BUS, "Data read %d\n", read(bus, buffer, CMD_MAX));
|
d4d5bb6d
henyxia
Bus working
|
201
202
203
204
205
|
ftruncate(bus, 0);
sync();
processCmd(buffer);
busFree = true;
printx(DEBUG, BUS, buffer);
|
c72d79dd
henyxia
Semaphores contro...
|
206
|
V(SEM_INPUT);
|
e4e1a578
henyxia
Ugly rev but work...
|
207
|
usleep(2);
|
d4d5bb6d
henyxia
Bus working
|
208
209
210
211
212
213
|
}
return NULL;
}
bool initBus()
{
|
c72d79dd
henyxia
Semaphores contro...
|
214
215
216
217
|
create_sem(SEM_NUMBER);
printx(ERROR, BUS, "Oh dear, something went wrong with %s !\n", strerror(errno));
init_sem(SEM_INPUT);
init_sem(SEM_OUTPUT);
|
d4d5bb6d
henyxia
Bus working
|
218
219
220
221
222
223
224
|
bus = creat("bus.log", 0666);
if(bus == -1)
{
printf("Unable to open the bus file\n");
return false;
}
|
d4d5bb6d
henyxia
Bus working
|
225
226
227
228
229
|
return true;
}
void sendToBus(char* cmd)
{
|
c72d79dd
henyxia
Semaphores contro...
|
230
231
|
printx(DEBUG, BUS, "Control semaphore %d in %d out %d\n", mySem, get_sem(SEM_INPUT), get_sem(SEM_OUTPUT));
P(SEM_INPUT);
|
d4d5bb6d
henyxia
Bus working
|
232
233
234
235
|
ftruncate(bus, 0);
ftruncate(bus, CMD_MAX);
lseek(bus, 0, SEEK_SET);
dprintf(bus, "%s\n", cmd);
|
c72d79dd
henyxia
Semaphores contro...
|
236
|
V(SEM_OUTPUT);
|
d4d5bb6d
henyxia
Bus working
|
237
|
}
|