thermferm/one-wire.c

changeset 654
e981d0185485
child 655
780cc08df263
equal deleted inserted replaced
653:6c4c884be155 654:e981d0185485
1 /**
2 * @brief One-wire devices
3 *
4 * Copyright (C) 2024
5 *
6 * This is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * mbsePi-apps is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with MBSE BBS; see the file COPYING. If not, write to the Free
18 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 #include "thermferm.h"
22 #include "statetbl.h"
23 #include "one-wire.h"
24 #include "xutil.h"
25
26
27 extern sys_config Config;
28 extern int my_shutdown;
29 extern pthread_mutex_t mutexes[5];
30
31 int my_one_wire_state = 0;
32 w1_list *w1_devices = NULL;
33
34
35 static int one_wire(void);
36
37 void *my_one_wire_loop(void *threadid)
38 {
39 my_one_wire_state = 1;
40 syslog(LOG_NOTICE, "Thread my_one_wire_loop started");
41
42 /*
43 * Run the state machine
44 */
45 one_wire();
46
47 /*
48 * Remove the dynamic tables.
49 */
50
51 syslog(LOG_NOTICE, "Thread my_one_wire_loop stopped");
52 my_one_wire_state = 0;
53 return 0;
54 }
55
56
57
58
59 SM_DECL(one_wire,(char *)"one-wire")
60 SM_STATES
61 Scan,
62 Reading,
63 Missing
64 SM_NAMES
65 (char *)"Scan",
66 (char *)"Reading",
67 (char *)"Missing"
68 SM_EDECL
69
70 int found, i;
71 FILE *fp;
72 // devices_list *device, *ndev;
73 w1_list *dev_w1, *n_w1, *cur_w1 = NULL;
74 char buffer[25], w1type[10];
75 uuid_t uu;
76
77 SM_START(Scan)
78
79 SM_STATE(Scan)
80
81 if (my_shutdown) {
82 SM_SUCCESS;
83 }
84
85 /*
86 * Scan for current one-wire devices.
87 */
88 fp = fopen("/sys/devices/w1_bus_master1/w1_master_slaves", "r");
89 if (fp == NULL) {
90 syslog(LOG_NOTICE, "No w1_bus_master: %s", strerror(errno));
91 SM_ERROR;
92 }
93 while ((fgets(buffer, 25, fp))) {
94 buffer[strlen(buffer)-1] = '\0';
95 // syslog(LOG_NOTICE, "device %d %s", strlen(buffer), buffer);
96
97 strncpy(w1type, buffer, 2);
98 w1type[2] = '\0';
99
100 /*
101 * Check if device is known and already detected.
102 */
103 if ((strcmp(w1type, (char *)"3a") == 0) || (strcmp(w1type, (char *)"10") == 0) ||
104 (strcmp(w1type, (char *)"22") == 0) || (strcmp(w1type, (char *)"28") == 0) ||
105 (strcmp(w1type, (char *)"3b") == 0) || (strcmp(w1type, (char *)"42") == 0)) {
106 found = FALSE;
107 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
108 if (strcmp(dev_w1->address, buffer) == 0) {
109 found = TRUE;
110 dev_w1->timestamp = time(NULL);
111 if (dev_w1->present != DEVPRESENT_YES) {
112 syslog(LOG_NOTICE, "One-wire device %s is back", buffer);
113 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
114 dev_w1->present = DEVPRESENT_YES;
115 pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
116 }
117 break;
118 }
119 }
120 if (found == FALSE) {
121 syslog(LOG_NOTICE, "One-wire device %s add new", buffer);
122 n_w1 = (w1_list *)malloc(sizeof(w1_list));
123 n_w1->next = NULL;
124 n_w1->address = xstrcpy(buffer);
125 strncpy(n_w1->family, buffer, 2);
126 n_w1->family[2] = '\0';
127 n_w1->present = DEVPRESENT_YES;
128 n_w1->subdevices = (strcmp(w1type, (char *)"3a") == 0) ? 2:1;
129 n_w1->timestamp = time(NULL);
130
131 pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
132 if (w1_devices == NULL) {
133 w1_devices = n_w1;
134 cur_w1 = w1_devices; /* Point to first device */
135 } else {
136 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
137 if (dev_w1->next == NULL) {
138 dev_w1->next = n_w1;
139 break;
140 }
141 }
142 }
143 pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
144 }
145 } else {
146 syslog(LOG_NOTICE, "One-wire device %d %s unknown", strlen(buffer), buffer);
147 }
148 }
149 fclose(fp);
150 SM_PROCEED(Reading);
151
152 SM_STATE(Reading)
153
154 if (my_shutdown) {
155 SM_SUCCESS;
156 }
157
158 /*
159 * cur_w1 points to the next not handled device.
160 */
161 if (cur_w1 != NULL) {
162
163 syslog(LOG_NOTICE, "Reading %s", cur_w1->address);
164
165 sleep(1);
166
167 if (cur_w1->next != NULL) {
168 cur_w1 = cur_w1->next;
169 } else {
170 cur_w1 = w1_devices;
171 }
172
173 } else {
174 sleep(1);
175 }
176
177 SM_PROCEED(Missing);
178
179 SM_STATE(Missing)
180
181 if (my_shutdown) {
182 SM_SUCCESS;
183 }
184
185 sleep(1);
186 SM_PROCEED(Scan);
187
188 SM_END
189 SM_RETURN
190
191

mercurial