Fri, 01 Aug 2014 16:13:55 +0200
A bit of code cleanup
158 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with thermferm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include "devices.h" | |
24 | #include "thermferm.h" | |
25 | #include "xutil.h" | |
26 | ||
27 | ||
28 | extern int debug; | |
29 | extern sys_config Config; | |
30 | extern int my_shutdown; | |
31 | ||
32 | ||
33 | ||
34 | /* | |
35 | * Auto detect hotplugged or known to be present devices | |
36 | */ | |
37 | int devices_detect(void) | |
38 | { | |
39 | struct dirent *de; | |
40 | DIR *fd; | |
41 | devices_list *device, *ndev; | |
42 | int found, subdevices, i, rc = 0; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
43 | char buf[40]; |
158 | 44 | uuid_t uu; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
45 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
46 | int pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
47 | #endif |
158 | 48 | |
49 | /* | |
50 | * Scan for 1-wire devices | |
51 | */ | |
52 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { | |
53 | while ((de = readdir(fd))) { | |
54 | if (de->d_name[0] != '.') { | |
55 | found = FALSE; | |
56 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
57 | if (strcmp(device->address,de->d_name) == 0) { |
158 | 58 | found = TRUE; |
59 | break; | |
60 | } | |
61 | } | |
62 | ||
63 | if (found == FALSE) { | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
64 | strncpy(buf, de->d_name, 2); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
65 | buf[2] = '\0'; |
158 | 66 | subdevices = 1; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
67 | if (strcmp(buf, (char *)"29") == 0) |
158 | 68 | subdevices = 8; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
69 | if (strcmp(buf, (char *)"3a") == 0) |
158 | 70 | subdevices = 2; |
71 | for (i = 0; i < subdevices; i++) { | |
72 | ndev = (devices_list *)malloc(sizeof(devices_list)); | |
73 | ndev->next = NULL; | |
74 | ndev->version = 1; | |
75 | ndev->uuid = malloc(37); | |
76 | uuid_generate(uu); | |
77 | uuid_unparse(uu, ndev->uuid); | |
78 | ndev->type = DEVTYPE_W1; | |
79 | ndev->direction = DEVDIR_UNDEF; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
80 | if (strcmp(buf, (char *)"10") == 0) { |
158 | 81 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
82 | ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
83 | } else if (strcmp(buf, (char *)"28") == 0) { |
158 | 84 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
85 | ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
86 | } else if (strcmp(buf, (char *)"29") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
87 | ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
88 | } else if (strcmp(buf, (char *)"3a") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
89 | ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
90 | } else if (strcmp(buf, (char *)"w1") == 0) { |
158 | 91 | ndev->description = xstrcpy((char *)"Master System device"); |
92 | } else { | |
93 | ndev->description = xstrcpy((char *)"Unknown device family "); | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
94 | ndev->description = xstrcat(ndev->description, buf); |
158 | 95 | } |
96 | ndev->value = ndev->inuse = 0; | |
97 | ndev->present = DEVPRESENT_YES; | |
98 | ndev->address = xstrcpy(de->d_name); | |
99 | ndev->subdevice = i; | |
100 | ndev->gpiopin = -1; | |
101 | ndev->comment = xstrcpy((char *)"Auto detected device"); | |
102 | ndev->timestamp = time(NULL); | |
103 | ||
104 | if (Config.devices == NULL) { | |
105 | Config.devices = ndev; | |
106 | } else { | |
107 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
108 | if (device->next == NULL) { |
158 | 109 | device->next = ndev; |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
110 | break; |
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
111 | } |
158 | 112 | } |
113 | } | |
114 | rc++; | |
115 | } | |
116 | } | |
117 | } | |
118 | } | |
166
c31ea86fec43
Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
164
diff
changeset
|
119 | closedir(fd); |
158 | 120 | } |
121 | ||
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
122 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
123 | if (piBoardRev() == 2) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
124 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
125 | * Support rev B boards only |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
126 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
127 | found = FALSE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
128 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
129 | if (device->type == DEVTYPE_GPIO) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
130 | found = TRUE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
131 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
132 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
133 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
134 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
135 | if (found == FALSE) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
136 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
137 | * There were no GPIO devices found. |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
138 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
139 | subdevices = 12; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
140 | pin = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
141 | for (i = 0; i < subdevices; i++) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
142 | if (i == 8) |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
143 | pin = 17; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
144 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
145 | ndev = (devices_list *)malloc(sizeof(devices_list)); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
146 | ndev->next = NULL; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
147 | ndev->version = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
148 | ndev->uuid = malloc(37); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
149 | uuid_generate(uu); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
150 | uuid_unparse(uu, ndev->uuid); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
151 | ndev->type = DEVTYPE_GPIO; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
152 | ndev->value = digitalRead(pin); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
153 | ndev->present = DEVPRESENT_YES; |
163
3d813570a5e3
LIST BUS now displays the new devices table. Adjusted the web screen
Michiel Broek <mbroek@mbse.eu>
parents:
162
diff
changeset
|
154 | ndev->address = xstrcpy((char *)"GPIO"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
155 | snprintf(buf, 39, "Raspberry GPIO %d", i); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
156 | ndev->description = xstrcpy(buf); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
157 | ndev->subdevice = i; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
158 | ndev->gpiopin = pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
159 | ndev->timestamp = time(NULL); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
160 | if (i == 7) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
161 | ndev->direction = DEVDIR_INTERN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
162 | ndev->inuse = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
163 | ndev->comment = xstrcpy((char *)"1-Wire bus"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
164 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
165 | ndev->direction = DEVDIR_IN_BIN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
166 | ndev->inuse = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
167 | ndev->comment = xstrcpy((char *)"Raspberry GPIO"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
168 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
169 | pin++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
170 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
171 | if (Config.devices == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
172 | Config.devices = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
173 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
174 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
175 | if (device->next == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
176 | device->next = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
177 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
178 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
179 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
180 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
181 | rc++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
182 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
183 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
184 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
185 | #endif |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
186 | |
158 | 187 | return rc; |
188 | } | |
189 | ||
190 | ||
191 | ||
192 | #ifdef HAVE_WIRINGPI_H | |
193 | PI_THREAD (my_devices_loop) | |
194 | #else | |
195 | void *my_devices_loop(void *threadid) | |
196 | #endif | |
197 | { | |
198 | devices_list *device; | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
199 | char *addr = NULL, line[60], *p = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
200 | FILE *fp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
201 | int temp, rc; |
158 | 202 | |
203 | syslog(LOG_NOTICE, "Thread my_devices_loop started"); | |
204 | if (debug) | |
205 | fprintf(stdout, "Thread my_devices_loop started\n"); | |
206 | ||
207 | /* | |
208 | * Loop forever until the external shutdown variable is set. | |
209 | */ | |
210 | for (;;) { | |
211 | ||
212 | /* | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
213 | * Process all devices. |
158 | 214 | */ |
215 | for (device = Config.devices; device; device = device->next) { | |
216 | ||
217 | if (my_shutdown) | |
218 | break; | |
219 | ||
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
220 | switch (device->type) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
221 | case DEVTYPE_W1: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
222 | if (strncmp(device->address, (char *)"28", 2) == 0) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
223 | /* Read DS18B20 sensor */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
224 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
225 | addr = xstrcat(addr, device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
226 | addr = xstrcat(addr, (char *)"/w1_slave"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
227 | if ((fp = fopen(addr, "r"))) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
228 | if (device->present != DEVPRESENT_YES) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
229 | syslog(LOG_NOTICE, "sensor %s is back", device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
230 | device->present = DEVPRESENT_YES; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
231 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
232 | /* |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
233 | * The output looks like: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
234 | * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
235 | * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
236 | */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
237 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
238 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
239 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
240 | /* CRC is Ok, continue */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
241 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
242 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
243 | strtok(line, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
244 | p = strtok(NULL, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
245 | rc = sscanf(p, "%d", &temp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
246 | if ((rc == 1) && (device->value != temp)) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
247 | device->value = temp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
248 | device->timestamp = time(NULL); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
249 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
250 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
251 | syslog(LOG_NOTICE, "sensor %s CRC error", device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
252 | device->present = DEVPRESENT_ERROR; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
253 | } |
166
c31ea86fec43
Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
164
diff
changeset
|
254 | fclose(fp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
255 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
256 | if (device->present != DEVPRESENT_NO) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
257 | syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
258 | device->present = DEVPRESENT_NO; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
259 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
260 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
261 | free(addr); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
262 | addr = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
263 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
264 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
265 | default: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
266 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
267 | } |
158 | 268 | } |
269 | usleep(10000); | |
270 | } | |
271 | ||
272 | syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); | |
273 | if (debug) | |
274 | fprintf(stdout, "Thread my_devices_loop stopped\n"); | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
279 | ||
280 |