thermferm/devices.c

changeset 162
6fc9e3f7962f
parent 161
493e39bb0a08
child 163
3d813570a5e3
equal deleted inserted replaced
161:493e39bb0a08 162:6fc9e3f7962f
35 * Auto detect hotplugged or known to be present devices 35 * Auto detect hotplugged or known to be present devices
36 */ 36 */
37 int devices_detect(void) 37 int devices_detect(void)
38 { 38 {
39 struct dirent *de; 39 struct dirent *de;
40 // FILE *fp;
41 DIR *fd; 40 DIR *fd;
42 devices_list *device, *ndev; 41 devices_list *device, *ndev;
43 int found, subdevices, i, rc = 0; 42 int found, subdevices, i, rc = 0;
44 char family[3]; 43 char buf[40];
45 uuid_t uu; 44 uuid_t uu;
45 #ifdef HAVE_WIRINGPI_H
46 int pin;
47 #endif
46 48
47 /* 49 /*
48 * Scan for 1-wire devices 50 * Scan for 1-wire devices
49 */ 51 */
50 if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { 52 if ((fd = opendir((char *)"/sys/bus/w1/devices"))) {
57 break; 59 break;
58 } 60 }
59 } 61 }
60 62
61 if (found == FALSE) { 63 if (found == FALSE) {
62 strncpy(family, de->d_name, 2); 64 strncpy(buf, de->d_name, 2);
63 family[2] = '\0'; 65 buf[2] = '\0';
64 subdevices = 1; 66 subdevices = 1;
65 if (strcmp(family, (char *)"29") == 0) 67 if (strcmp(buf, (char *)"29") == 0)
66 subdevices = 8; 68 subdevices = 8;
67 if (strcmp(family, (char *)"3a") == 0) 69 if (strcmp(buf, (char *)"3a") == 0)
68 subdevices = 2; 70 subdevices = 2;
69 for (i = 0; i < subdevices; i++) { 71 for (i = 0; i < subdevices; i++) {
70 ndev = (devices_list *)malloc(sizeof(devices_list)); 72 ndev = (devices_list *)malloc(sizeof(devices_list));
71 ndev->next = NULL; 73 ndev->next = NULL;
72 ndev->version = 1; 74 ndev->version = 1;
73 ndev->uuid = malloc(37); 75 ndev->uuid = malloc(37);
74 uuid_generate(uu); 76 uuid_generate(uu);
75 uuid_unparse(uu, ndev->uuid); 77 uuid_unparse(uu, ndev->uuid);
76 ndev->type = DEVTYPE_W1; 78 ndev->type = DEVTYPE_W1;
77 ndev->direction = DEVDIR_UNDEF; 79 ndev->direction = DEVDIR_UNDEF;
78 if (strcmp(family, (char *)"10") == 0) { 80 if (strcmp(buf, (char *)"10") == 0) {
79 ndev->direction = DEVDIR_IN_ANALOG; 81 ndev->direction = DEVDIR_IN_ANALOG;
80 ndev->description = xstrcpy((char *)"18S20,Digital thermometer"); 82 ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer");
81 } else if (strcmp(family, (char *)"28") == 0) { 83 } else if (strcmp(buf, (char *)"28") == 0) {
82 ndev->direction = DEVDIR_IN_ANALOG; 84 ndev->direction = DEVDIR_IN_ANALOG;
83 ndev->description = xstrcpy((char *)"18B20,Digital thermometer"); 85 ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
84 } else if (strcmp(family, (char *)"29") == 0) { 86 } else if (strcmp(buf, (char *)"29") == 0) {
85 ndev->description = xstrcpy((char *)"2408,8 Channel addressable switch/LCD"); 87 ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD");
86 } else if (strcmp(family, (char *)"3a") == 0) { 88 } else if (strcmp(buf, (char *)"3a") == 0) {
87 ndev->description = xstrcpy((char *)"2413,Dual channel addressable switch"); 89 ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
88 } else if (strcmp(family, (char *)"w1") == 0) { 90 } else if (strcmp(buf, (char *)"w1") == 0) {
89 ndev->description = xstrcpy((char *)"Master System device"); 91 ndev->description = xstrcpy((char *)"Master System device");
90 } else { 92 } else {
91 ndev->description = xstrcpy((char *)"Unknown device family "); 93 ndev->description = xstrcpy((char *)"Unknown device family ");
92 ndev->description = xstrcat(ndev->description, family); 94 ndev->description = xstrcat(ndev->description, buf);
93 } 95 }
94 ndev->value = ndev->inuse = 0; 96 ndev->value = ndev->inuse = 0;
95 ndev->present = DEVPRESENT_YES; 97 ndev->present = DEVPRESENT_YES;
96 ndev->address = xstrcpy(de->d_name); 98 ndev->address = xstrcpy(de->d_name);
97 ndev->subdevice = i; 99 ndev->subdevice = i;
113 } 115 }
114 } 116 }
115 } 117 }
116 } 118 }
117 } 119 }
120
121 #ifdef HAVE_WIRINGPI_H
122 if (piBoardRev() == 2) {
123 /*
124 * Support rev B boards only
125 */
126 found = FALSE;
127 for (device = Config.devices; device; device = device->next) {
128 if (device->type == DEVTYPE_GPIO) {
129 found = TRUE;
130 break;
131 }
132 }
133
134 if (found == FALSE) {
135 /*
136 * There were no GPIO devices found.
137 */
138 subdevices = 12;
139 pin = 0;
140 for (i = 0; i < subdevices; i++) {
141 if (i == 8)
142 pin = 17;
143
144 ndev = (devices_list *)malloc(sizeof(devices_list));
145 ndev->next = NULL;
146 ndev->version = 1;
147 ndev->uuid = malloc(37);
148 uuid_generate(uu);
149 uuid_unparse(uu, ndev->uuid);
150 ndev->type = DEVTYPE_GPIO;
151 ndev->value = digitalRead(pin);
152 ndev->present = DEVPRESENT_YES;
153 snprintf(buf, 39, "GPIO%d", i);
154 ndev->address = xstrcpy(buf);
155 snprintf(buf, 39, "Raspberry GPIO %d", i);
156 ndev->description = xstrcpy(buf);
157 ndev->subdevice = i;
158 ndev->gpiopin = pin;
159 ndev->timestamp = time(NULL);
160 if (i == 7) {
161 ndev->direction = DEVDIR_INTERN;
162 ndev->inuse = 1;
163 ndev->comment = xstrcpy((char *)"1-Wire bus");
164 } else {
165 ndev->direction = DEVDIR_IN_BIN;
166 ndev->inuse = 0;
167 ndev->comment = xstrcpy((char *)"Raspberry GPIO");
168 }
169 pin++;
170
171 if (Config.devices == NULL) {
172 Config.devices = ndev;
173 } else {
174 for (device = Config.devices; device; device = device->next) {
175 if (device->next == NULL) {
176 device->next = ndev;
177 break;
178 }
179 }
180 }
181 rc++;
182 }
183 }
184 }
185 #endif
118 186
119 return rc; 187 return rc;
120 } 188 }
121 189
122 190

mercurial