1 | #include <sys/types.h>
|
---|
2 | #include <sys/stat.h>
|
---|
3 | #include <sys/time.h>
|
---|
4 | #include <fcntl.h>
|
---|
5 | #include <termios.h>
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <time.h>
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <string.h>
|
---|
10 | #include <unistd.h>
|
---|
11 | #include <sys/select.h>
|
---|
12 |
|
---|
13 | #define bzero(b,len) (memset((b), '\0' ,(len)), (void) 0)
|
---|
14 |
|
---|
15 |
|
---|
16 | /*
|
---|
17 | Included for Evidence server
|
---|
18 | */
|
---|
19 | #define SERVER_NAME "ARDUINO" // Name to use in DIM
|
---|
20 | #include "Evidence.h"
|
---|
21 | #define NUM_VAL 10
|
---|
22 |
|
---|
23 | /* baudrate settings are defined in <asm/termbits.h>, which is
|
---|
24 | included by <termios.h> */
|
---|
25 | #define BAUDRATE B9600
|
---|
26 | /* change this definition for the correct port */
|
---|
27 | #define MODEMDEVICE "/dev/myArduino"
|
---|
28 | #define _POSIX_SOURCE 1 /* POSIX compliant source */
|
---|
29 |
|
---|
30 | #define FALSE 0
|
---|
31 | #define TRUE 1
|
---|
32 |
|
---|
33 | #define myPath "/ct3data/SlowData/" // will be used later on
|
---|
34 | //#define myPath "" // for testing purposes
|
---|
35 |
|
---|
36 |
|
---|
37 | FILE * openOutfile(char *path);
|
---|
38 | static int poll_stdin_time(int microsekunden) ;
|
---|
39 |
|
---|
40 | volatile int STOP=FALSE;
|
---|
41 |
|
---|
42 | int main(int argc, char *argv[])
|
---|
43 | {
|
---|
44 | int fd, res;
|
---|
45 | struct termios oldtio,newtio;
|
---|
46 | char buf[255];
|
---|
47 |
|
---|
48 | /*
|
---|
49 | Start Evidence server and create services
|
---|
50 | */
|
---|
51 | EvidenceServer Srv(SERVER_NAME);
|
---|
52 |
|
---|
53 | float Val[NUM_VAL];
|
---|
54 | char Name[100];
|
---|
55 | DimService *Service[NUM_VAL];
|
---|
56 |
|
---|
57 | for (int i=0; i<NUM_VAL; i++) {
|
---|
58 | Val[i] = -99;
|
---|
59 | snprintf(Name, sizeof(Name), SERVER_NAME"/VAL%.2d", i);
|
---|
60 | Service[i] = new DimService (Name, Val[i]);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*
|
---|
64 | Open modem device for reading and writing and not as controlling tty
|
---|
65 | because we don't want to get killed if linenoise sends CTRL-C.
|
---|
66 | */
|
---|
67 | fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
|
---|
68 | if (fd <0) {perror(MODEMDEVICE); exit(-1); }
|
---|
69 |
|
---|
70 | tcgetattr(fd,&oldtio); /* save current serial port settings */
|
---|
71 | bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
|
---|
72 |
|
---|
73 | /*
|
---|
74 | BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
|
---|
75 | CRTSCTS : output hardware flow control (only used if the cable has
|
---|
76 | all necessary lines. See sect. 7 of Serial-HOWTO)
|
---|
77 | CS8 : 8n1 (8bit,no parity,1 stopbit)
|
---|
78 | CLOCAL : local connection, no modem contol
|
---|
79 | CREAD : enable receiving characters
|
---|
80 | */
|
---|
81 | newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
|
---|
82 |
|
---|
83 | // CRTSCTS | maybe not needed //DN 090907
|
---|
84 |
|
---|
85 | /*
|
---|
86 | IGNPAR : ignore bytes with parity errors
|
---|
87 | ICRNL : map CR to NL (otherwise a CR input on the other computer
|
---|
88 | will not terminate input)
|
---|
89 | otherwise make device raw (no other input processing)
|
---|
90 | */
|
---|
91 | newtio.c_iflag = IGNPAR ;
|
---|
92 |
|
---|
93 | // | ICRNL // i guess this is not needed here. DN 090907
|
---|
94 |
|
---|
95 | /*
|
---|
96 | Raw output.
|
---|
97 | */
|
---|
98 | newtio.c_oflag = 0;
|
---|
99 |
|
---|
100 | /*
|
---|
101 | ICANON : enable canonical input
|
---|
102 | disable all echo functionality, and don't send signals to calling program
|
---|
103 | */
|
---|
104 | newtio.c_lflag = ICANON;
|
---|
105 |
|
---|
106 | /*
|
---|
107 | initialize all control characters
|
---|
108 | default values can be found in /usr/include/termios.h, and are given
|
---|
109 | in the comments, but we don't need them here
|
---|
110 | */
|
---|
111 | newtio.c_cc[VINTR] = 0; /* Ctrl-c */
|
---|
112 | newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
|
---|
113 | newtio.c_cc[VERASE] = 0; /* del */
|
---|
114 | newtio.c_cc[VKILL] = 0; /* @ */
|
---|
115 | newtio.c_cc[VEOF] = 4; /* Ctrl-d */
|
---|
116 | newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
|
---|
117 | newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
|
---|
118 | newtio.c_cc[VSWTC] = 0; /* '\0' */
|
---|
119 | newtio.c_cc[VSTART] = 0; /* Ctrl-q */
|
---|
120 | newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
|
---|
121 | newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
|
---|
122 | newtio.c_cc[VEOL] = 0; /* '\0' */
|
---|
123 | newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
|
---|
124 | newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
|
---|
125 | newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
|
---|
126 | newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
|
---|
127 | newtio.c_cc[VEOL2] = 0; /* '\0' */
|
---|
128 |
|
---|
129 | /*
|
---|
130 | now clean the modem line and activate the settings for the port
|
---|
131 | */
|
---|
132 | tcflush(fd, TCIFLUSH);
|
---|
133 | tcsetattr(fd,TCSANOW,&newtio);
|
---|
134 |
|
---|
135 | /*
|
---|
136 | terminal settings done, now handle input
|
---|
137 | In this example, inputting a 'z' at the beginning of a line will
|
---|
138 | exit the program.
|
---|
139 | */
|
---|
140 |
|
---|
141 | FILE *outfile;
|
---|
142 |
|
---|
143 |
|
---|
144 | char outstring[400];
|
---|
145 | //char c;
|
---|
146 | char Temp[200], *Token;
|
---|
147 |
|
---|
148 | while (STOP==FALSE) { /* loop until we have a terminating condition */
|
---|
149 | /* read blocks program execution until a line terminating character is
|
---|
150 | input, even if more than 255 chars are input. If the number
|
---|
151 | of characters read is smaller than the number of chars available,
|
---|
152 | subsequent reads will return the remaining chars. res will be set
|
---|
153 | to the actual number of characters actually read */
|
---|
154 | res = read(fd,buf,255);
|
---|
155 | buf[res]=0; /* set end of string, so we can printf */
|
---|
156 |
|
---|
157 |
|
---|
158 | if (strncmp (buf,"[Temp,",6) != 0) continue;
|
---|
159 |
|
---|
160 | strncpy(Temp, buf, sizeof(Temp));
|
---|
161 | Token = strtok(Temp, ", ");
|
---|
162 | for (int i=0; i<NUM_VAL; i++) {
|
---|
163 | Token = strtok(NULL, ", ");
|
---|
164 | if (Token != NULL) Val[i] = atof(Token);
|
---|
165 | Service[i]->updateService();
|
---|
166 | }
|
---|
167 |
|
---|
168 | outfile = openOutfile(myPath);
|
---|
169 | if (outfile == NULL) {return -1;}
|
---|
170 |
|
---|
171 | time_t rawtime;
|
---|
172 | struct tm * timeinfo;
|
---|
173 |
|
---|
174 | time ( &rawtime );
|
---|
175 | timeinfo = localtime ( &rawtime );
|
---|
176 |
|
---|
177 | struct timeval timeofday;
|
---|
178 | gettimeofday(&timeofday, NULL);
|
---|
179 |
|
---|
180 |
|
---|
181 | sprintf(outstring, "DALLAS Sensorvalues %04d %02d %02d %02d %02d %02d %03d %d %s",
|
---|
182 | (*timeinfo).tm_year+1900,
|
---|
183 | (*timeinfo).tm_mon+1,
|
---|
184 | (*timeinfo).tm_mday,
|
---|
185 | (*timeinfo).tm_hour,
|
---|
186 | (*timeinfo).tm_min,
|
---|
187 | (*timeinfo).tm_sec,
|
---|
188 | (int) timeofday.tv_usec/1000,
|
---|
189 | (int)rawtime,
|
---|
190 | buf);
|
---|
191 |
|
---|
192 | printf("%s", outstring);
|
---|
193 |
|
---|
194 | fprintf(outfile, "%s", outstring);
|
---|
195 | fflush(outfile);
|
---|
196 | fclose(outfile);
|
---|
197 |
|
---|
198 | if (poll_stdin_time(1500000)==1) {
|
---|
199 | STOP=TRUE;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | } //end while(STOP==FALSE)
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 | /* restore the old port settings */
|
---|
208 | tcsetattr(fd,TCSANOW,&oldtio);
|
---|
209 |
|
---|
210 | /*
|
---|
211 | Delete Evidence services
|
---|
212 | */
|
---|
213 | for (int i=0; i<NUM_VAL; i++) {
|
---|
214 | delete Service[i];
|
---|
215 | }
|
---|
216 |
|
---|
217 | return 0;
|
---|
218 | } //end main();
|
---|
219 |
|
---|
220 | // OPens or Creates an Outfile named
|
---|
221 | // "CLIM_YYYYMMDD.slow"
|
---|
222 | // this file should contain all enviromentals from noon to noon.
|
---|
223 | // The File is created if it doesn't exist.
|
---|
224 | // all data is appended, nothing will be deleted or overwritten.
|
---|
225 |
|
---|
226 | FILE * openOutfile(char *path)
|
---|
227 | {
|
---|
228 | FILE *outfile;
|
---|
229 |
|
---|
230 | time_t rawtime;
|
---|
231 | struct tm * timeinfo;
|
---|
232 |
|
---|
233 | time ( &rawtime );
|
---|
234 | timeinfo = gmtime ( &rawtime );
|
---|
235 |
|
---|
236 | // check what day it is today :-)
|
---|
237 | if ((*timeinfo).tm_hour >12){ //then it is already tommorow
|
---|
238 | rawtime += 86400;
|
---|
239 | timeinfo = gmtime ( &rawtime );
|
---|
240 | }
|
---|
241 | //generate filename
|
---|
242 | char *outfilename;
|
---|
243 | outfilename =(char*) calloc( strlen(path)+25 , sizeof(char));
|
---|
244 | if (outfilename ==NULL)
|
---|
245 | {
|
---|
246 | perror("could not allocate space for outputfilename");
|
---|
247 | return NULL;
|
---|
248 | }
|
---|
249 | sprintf(outfilename,"%sCLIM_%04d%02d%02d.slow",
|
---|
250 | path, // from user
|
---|
251 | (*timeinfo).tm_year+1900,
|
---|
252 | (*timeinfo).tm_mon+1,
|
---|
253 | (*timeinfo).tm_mday );
|
---|
254 |
|
---|
255 | // there is no need to check if file is already existing.
|
---|
256 | // if not fopen(name,"a") will create it.
|
---|
257 | // if it exists
|
---|
258 | // it will only be opened for appnendign
|
---|
259 | outfile = fopen(outfilename,"a");
|
---|
260 |
|
---|
261 | if (outfile==NULL){
|
---|
262 | perror("could not open outputfile");
|
---|
263 | return NULL;}
|
---|
264 |
|
---|
265 | return outfile;
|
---|
266 | }
|
---|
267 |
|
---|
268 | static int poll_stdin_time(int microsekunden) {
|
---|
269 | struct timeval timeout;
|
---|
270 | fd_set read_fds;
|
---|
271 | int c;
|
---|
272 | int stdin_status;
|
---|
273 |
|
---|
274 | FD_ZERO(&read_fds);
|
---|
275 | FD_SET(STDIN_FILENO, &read_fds);
|
---|
276 | timeout.tv_sec = 0;
|
---|
277 | timeout.tv_usec =microsekunden;
|
---|
278 | stdin_status = select(STDIN_FILENO+1, &read_fds, NULL, NULL, &timeout);
|
---|
279 | if (stdin_status == 1 ) {
|
---|
280 | c=getchar();
|
---|
281 | fflush(stdin);
|
---|
282 | if (c=='q') {
|
---|
283 | return 1;
|
---|
284 | }
|
---|
285 | } else if (stdin_status ==0) {
|
---|
286 | return 0;
|
---|
287 | } else {
|
---|
288 | perror("select()");
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 | return -1;
|
---|
294 |
|
---|
295 |
|
---|
296 | }
|
---|