Blame view

bus.c 1.44 KB
d4d5bb6d   henyxia   Bus working
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
  #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"
  
  #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();
  		//stopHVC();
  		stopBus();
  		stopUI();
  	}
  	//else
  		//printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
  }
  
  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;
  }