Blame view

bus.c 3.82 KB
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"
d4d5bb6d   henyxia   Bus working
15
  
c72d79dd   henyxia   Semaphores contro...
16
17
18
19
20
  #define	CMD_MAX		70
  #define KEY			1100
  #define	SEM_NUMBER	2
  #define	SEM_INPUT	0
  #define	SEM_OUTPUT	1
d4d5bb6d   henyxia   Bus working
21
22
23
24
  
  int		bus;
  bool	busFree = true;
  bool	busStop = false;
c72d79dd   henyxia   Semaphores contro...
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
91
92
93
94
95
96
97
  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);
  } 
d4d5bb6d   henyxia   Bus working
98
99
100
101
102
103
104
105
106
107
108
109
110
  
  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...
111
  		stopHVC();
d4d5bb6d   henyxia   Bus working
112
113
114
  		stopBus();
  		stopUI();
  	}
ac0ae6b0   henyxia   HVC can now use t...
115
116
  	else if(strcmp(buffer, "setpumpon") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
117
  		printx(INFO, BUS, "Setting PUMP ON");
ac0ae6b0   henyxia   HVC can now use t...
118
119
120
121
  		setPumpWantedState(true);
  	}
  	else if(strcmp(buffer, "setpumpoff") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
122
  		printx(INFO, BUS, "Setting PUMP OFF");
ac0ae6b0   henyxia   HVC can now use t...
123
124
125
126
  		setPumpWantedState(false);
  	}
  	else if(strcmp(buffer, "setheaton") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
127
  		printx(INFO, BUS, "Setting HEAT ON");
ac0ae6b0   henyxia   HVC can now use t...
128
129
130
131
  		setHeatWantedState(true);
  	}
  	else if(strcmp(buffer, "setheatoff") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
132
  		printx(INFO, BUS, "Setting HEAT OFF");
ac0ae6b0   henyxia   HVC can now use t...
133
134
  		setHeatWantedState(false);
  	}
5dd66ab0   henyxia   Added timer optio...
135
136
  	else if(strcmp(buffer, "setheaton5s") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
137
  		printx(INFO, BUS, "Setting HEAT ON for 5 secs");
5dd66ab0   henyxia   Added timer optio...
138
139
140
  		setHeatTimer(5);
  		setHeatWantedState(true);
  	}
c72d79dd   henyxia   Semaphores contro...
141
  	else if(strcmp(buffer, "setheaton10s") == 0)
c21a2c16   henyxia   Re added timestam...
142
143
144
145
146
  	{
  		printx(INFO, BUS, "Setting HEAT ON for 10 secs");
  		setHeatTimer(10);
  		setHeatWantedState(true);
  	}
5dd66ab0   henyxia   Added timer optio...
147
148
  	else if(strcmp(buffer, "setpumpon5s") == 0)
  	{
2bc2194a   henyxia   Revert "Fixed som...
149
  		printx(INFO, BUS, "Setting PUMP ON for 5 secs");
5dd66ab0   henyxia   Added timer optio...
150
151
152
  		setPumpTimer(5);
  		setPumpWantedState(true);
  	}
ac0ae6b0   henyxia   HVC can now use t...
153
154
  
  	//printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
d4d5bb6d   henyxia   Bus working
155
156
157
158
159
160
161
162
163
  }
  
  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...
164
165
166
  		P(SEM_OUTPUT);
  		//while(busFree);
  		//while(!busFree);
d4d5bb6d   henyxia   Bus working
167
168
169
  		printx(DEBUG, BUS, "Event receved !\n");
  		busFree = false;
  		lseek(bus, 0, SEEK_SET);
2bc2194a   henyxia   Revert "Fixed som...
170
  		printx(DEBUG, BUS, "Data read %d\n", read(bus, buffer, CMD_MAX));
d4d5bb6d   henyxia   Bus working
171
172
173
174
175
  		ftruncate(bus, 0);
  		sync();
  		processCmd(buffer);
  		busFree = true;
  		printx(DEBUG, BUS, buffer);
c72d79dd   henyxia   Semaphores contro...
176
  		V(SEM_INPUT);
d4d5bb6d   henyxia   Bus working
177
178
179
180
181
182
  	}
  	return NULL;
  }
  
  bool initBus()
  {
c72d79dd   henyxia   Semaphores contro...
183
184
185
186
  	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
187
188
189
190
191
192
193
  	bus = creat("bus.log", 0666);
  	if(bus == -1)
  	{
  		printf("Unable to open the bus file\n");
  		return false;
  	}
  
c72d79dd   henyxia   Semaphores contro...
194
195
  	//P(SEM_INPUT);
  
d4d5bb6d   henyxia   Bus working
196
197
198
199
200
  	return true;
  }
  
  void sendToBus(char* cmd)
  {
c72d79dd   henyxia   Semaphores contro...
201
202
  	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
203
204
205
206
  	ftruncate(bus, 0);
  	ftruncate(bus, CMD_MAX);
  	lseek(bus, 0, SEEK_SET);
  	dprintf(bus, "%s\n", cmd);
c72d79dd   henyxia   Semaphores contro...
207
  	V(SEM_OUTPUT);
d4d5bb6d   henyxia   Bus working
208
  }