Thu, 31 Jul 2014 17:44:50 +0200
Added device configuration
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 | // FILE *fp; | |
41 | DIR *fd; | |
42 | devices_list *device, *ndev; | |
43 | int found, subdevices, i, rc = 0; | |
44 | char *family; | |
45 | uuid_t uu; | |
46 | ||
47 | /* | |
48 | * Scan for 1-wire devices | |
49 | */ | |
50 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { | |
51 | while ((de = readdir(fd))) { | |
52 | if (de->d_name[0] != '.') { | |
53 | found = FALSE; | |
54 | for (device = Config.devices; device; device = device->next) { | |
55 | if (strcmp(device->address ,de->d_name) == 0) { | |
56 | found = TRUE; | |
57 | break; | |
58 | } | |
59 | } | |
60 | ||
61 | if (found == FALSE) { | |
62 | family = malloc(3); | |
63 | strncpy(family, de->d_name, 2); | |
64 | subdevices = 1; | |
65 | if (strcmp(family, (char *)"29") == 0) | |
66 | subdevices = 8; | |
67 | if (strcmp(family, (char *)"3a") == 0) | |
68 | subdevices = 2; | |
69 | for (i = 0; i < subdevices; i++) { | |
70 | ndev = (devices_list *)malloc(sizeof(devices_list)); | |
71 | ndev->next = NULL; | |
72 | ndev->version = 1; | |
73 | ndev->uuid = malloc(37); | |
74 | uuid_generate(uu); | |
75 | uuid_unparse(uu, ndev->uuid); | |
76 | ndev->type = DEVTYPE_W1; | |
77 | ndev->direction = DEVDIR_UNDEF; | |
78 | if (strcmp(family, (char *)"10") == 0) { | |
79 | ndev->direction = DEVDIR_IN_ANALOG; | |
80 | ndev->description = xstrcpy((char *)"18S20,Digital thermometer"); | |
81 | } else if (strcmp(family, (char *)"28") == 0) { | |
82 | ndev->direction = DEVDIR_IN_ANALOG; | |
83 | ndev->description = xstrcpy((char *)"18B20,Digital thermometer"); | |
84 | } else if (strcmp(family, (char *)"29") == 0) { | |
85 | ndev->description = xstrcpy((char *)"2408,8 Channel addressable switch/LCD"); | |
86 | } else if (strcmp(family, (char *)"3a") == 0) { | |
87 | ndev->description = xstrcpy((char *)"2413,Dual channel addressable switch"); | |
88 | } else if (strcmp(family, (char *)"w1") == 0) { | |
89 | ndev->description = xstrcpy((char *)"Master System device"); | |
90 | } else { | |
91 | ndev->description = xstrcpy((char *)"Unknown device family "); | |
92 | ndev->description = xstrcat(ndev->description, family); | |
93 | } | |
94 | ndev->value = ndev->inuse = 0; | |
95 | ndev->present = DEVPRESENT_YES; | |
96 | ndev->address = xstrcpy(de->d_name); | |
97 | ndev->subdevice = i; | |
98 | ndev->gpiopin = -1; | |
99 | ndev->comment = xstrcpy((char *)"Auto detected device"); | |
100 | ndev->timestamp = time(NULL); | |
101 | ||
102 | if (Config.devices == NULL) { | |
103 | Config.devices = ndev; | |
104 | } else { | |
105 | for (device = Config.devices; device; device = device->next) { | |
106 | if (device->next == NULL) | |
107 | device->next = ndev; | |
108 | break; | |
109 | } | |
110 | } | |
111 | rc++; | |
112 | } | |
113 | } | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | return rc; | |
119 | } | |
120 | ||
121 | ||
122 | ||
123 | #ifdef HAVE_WIRINGPI_H | |
124 | PI_THREAD (my_devices_loop) | |
125 | #else | |
126 | void *my_devices_loop(void *threadid) | |
127 | #endif | |
128 | { | |
129 | devices_list *device; | |
130 | // char line[60], *p = NULL; | |
131 | // FILE *fp; | |
132 | // int temp, rc, deviation; | |
133 | ||
134 | syslog(LOG_NOTICE, "Thread my_devices_loop started"); | |
135 | if (debug) | |
136 | fprintf(stdout, "Thread my_devices_loop started\n"); | |
137 | ||
138 | /* | |
139 | * Loop forever until the external shutdown variable is set. | |
140 | */ | |
141 | for (;;) { | |
142 | ||
143 | /* | |
144 | * Here send our 1-wire sensors values | |
145 | */ | |
146 | for (device = Config.devices; device; device = device->next) { | |
147 | ||
148 | if (my_shutdown) | |
149 | break; | |
150 | ||
151 | /* | |
152 | * Build path to the on-wire sensor | |
153 | */ | |
154 | // device = xstrcpy((char *)"/sys/bus/w1/devices/"); | |
155 | // device = xstrcat(device, tmp1->master); | |
156 | // device = xstrcat(device, (char *)"/"); | |
157 | // device = xstrcat(device, tmp1->name); | |
158 | // device = xstrcat(device, (char *)"/w1_slave"); | |
159 | ||
160 | /* | |
161 | * Read sensor data | |
162 | */ | |
163 | // if ((fp = fopen(device, "r"))) { | |
164 | /* | |
165 | * The output looks like: | |
166 | * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES | |
167 | * 72 01 4b 46 7f ff 0e 10 57 t=23125 | |
168 | */ | |
169 | // fgets(line, 50, fp); | |
170 | // line[strlen(line)-1] = '\0'; | |
171 | // if ((line[36] == 'Y') && (line[37] == 'E')) { | |
172 | /* | |
173 | * CRC is Ok, continue | |
174 | */ | |
175 | // fgets(line, 50, fp); | |
176 | // line[strlen(line)-1] = '\0'; | |
177 | // strtok(line, (char *)"="); | |
178 | // p = strtok(NULL, (char *)"="); | |
179 | // rc = sscanf(p, "%d", &temp); | |
180 | // if ((rc == 1) && (tmp1->lastval != temp)) { | |
181 | /* | |
182 | * It is possible to have read errors or extreme values. | |
183 | * This can happen with bad connections so we compare the | |
184 | * value with the previous one. If the difference is too | |
185 | * much, we don't send that value. That also means that if | |
186 | * the next value is ok again, it will be marked invalid too. | |
187 | * Maximum error is 20 degrees for now. | |
188 | */ | |
189 | // deviation = 20000; | |
190 | // if ( (tmp1->lastval == 0) || | |
191 | // (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) { | |
192 | /* | |
193 | * Temperature is changed and valid, set flag. | |
194 | */ | |
195 | // tmp1->update = TRUE; | |
196 | // } else { | |
197 | // syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp); | |
198 | // if (debug) { | |
199 | // fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp); | |
200 | // } | |
201 | // } | |
202 | // tmp1->lastval = temp; | |
203 | // } | |
204 | // } else { | |
205 | // syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name); | |
206 | // } | |
207 | // fclose(fp); | |
208 | // tmp1->present = 1; | |
209 | // } else { | |
210 | // tmp1->present = 0; | |
211 | // if (debug) | |
212 | // printf("sensor %s is missing\n", tmp1->name); | |
213 | // } | |
214 | ||
215 | // free(device); | |
216 | // device = NULL; | |
217 | } | |
218 | usleep(10000); | |
219 | } | |
220 | ||
221 | syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); | |
222 | if (debug) | |
223 | fprintf(stdout, "Thread my_devices_loop stopped\n"); | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
228 | ||
229 |