1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <errno.h>
5 #include <string.h>
6 
7 #include <owcapi.h>
8 
9 const char DEFAULT_ARGV[] = "--fake 28 --fake 10";
10 
11 const char DEFAULT_PATH[] = "/";
12 
13 /* Takes a path and filename and prints the 1-wire value */
14 /* makes sure the bridging "/" in the path is correct */
15 /* watches for total length and free allocated space */
GetValue(const char * path,const char * name)16 void GetValue(const char *path, const char *name)
17 {
18 	char fullpath[PATH_MAX + 1];
19 	int length_left = PATH_MAX;
20 
21 	char *get_buffer;
22 	ssize_t get_return;
23 	size_t get_length;
24 
25 	/* Check arguments and lengths */
26 	if (path == NULL || strlen(path) == 0) {
27 		path = "/";
28 	}
29 	if (name == NULL) {
30 		name = "";
31 	}
32 
33 	fullpath[PATH_MAX] = '\0';	// make sure final 0 byte
34 
35 	strncpy(fullpath, path, length_left);
36 	length_left -= strlen(fullpath);
37 
38 	if (length_left > 0) {
39 		if (fullpath[PATH_MAX - length_left - 1] != '/') {
40 			strcat(fullpath, "/");
41 			--length_left;
42 		}
43 	}
44 	if (name[0] == '/') {
45 		++name;
46 	}
47 
48 	strncpy(&fullpath[PATH_MAX - length_left], name, length_left);
49 
50 	get_return = OW_get(fullpath, &get_buffer, &get_length);
51 	if (get_return < 0) {
52 		printf("ERROR (%d) reading %s (%s)\n", errno, fullpath, strerror(errno));
53 	} else {
54 		printf("%s has value %s (%d bytes)\n", fullpath, get_buffer, (int) get_length);
55 		free(get_buffer);
56 	}
57 }
58 
main(int argc,char ** argv)59 int main(int argc, char **argv)
60 {
61 	const char *path = DEFAULT_PATH;
62 
63 	ssize_t dir_return;
64 	char *dir_buffer = NULL;
65 	char *dir_buffer_copy = NULL;
66 	size_t dir_length;
67 	char **d_buffer;
68 	char *dir_member;
69 
70 	/* ------------- INITIALIZATIOB ------------------ */
71 	/* steal first argument and treat it as a path (if not beginning with '-') */
72 	if ((argc > 1) && (argv[1][0] != '-')) {
73 		path = argv[1];
74 		argv = &argv[1];
75 		argc--;
76 	}
77 
78 	if (argc < 2) {
79 		printf("Starting with extra parameter \"%s\" as default\n", DEFAULT_ARGV);
80 
81 		if (OW_init(DEFAULT_ARGV) < 0) {
82 			printf("OW_init error = %d (%s)\n", errno, strerror(errno));
83 			goto cleanup;
84 		}
85 	} else {
86 		if (OW_init_args(argc, argv) < 0) {
87 			printf("OW_init error = %d (%s)\n", errno, strerror(errno));
88 			goto cleanup;
89 		}
90 	}
91 
92 	/* ------------- DIRECTORY PATH ------------------ */
93 	dir_return = OW_get(path, &dir_buffer, &dir_length);
94 	if (dir_return < 0) {
95 		printf("DIRECTORY ERROR (%d) reading %s (%s)\n", errno, path, strerror(errno));
96 		goto cleanup;
97 	} else {
98 		printf("Directory %s has members %s (%d bytes)\n", path, dir_buffer, (int) dir_length);
99 	}
100 
101 	printf("\n");
102 
103 	/* ------------- GO THROUGH DIR ------------------ */
104 	d_buffer = &dir_buffer;
105 	dir_buffer_copy = dir_buffer;
106 	while ((dir_member = strsep(d_buffer, ",")) != NULL) {
107 		switch (dir_member[0]) {
108 		case '1':
109 			if (dir_member[1] == '0') {
110 				// DS18S20 (family code 10)
111 				GetValue(dir_member, "temperature");
112 			}
113 			//fall through
114 		case '0':
115 		case '2':
116 		case '3':
117 		case '4':
118 		case '5':
119 		case '6':
120 		case '7':
121 		case '8':
122 		case '9':
123 		case 'A':
124 		case 'B':
125 		case 'C':
126 		case 'D':
127 		case 'E':
128 		case 'F':
129 			{
130 				GetValue(dir_member, "type");
131 			}
132 			break;
133 		default:
134 			break;
135 		}
136 	}
137 
138 	/* ------------- STATIC PATHS   ------------------ */
139 	GetValue("system/process", "pid");
140 	GetValue("badPath", "badName");
141 
142   cleanup:
143 	/* ------------- DONE -- CLEANUP ----------------- */
144 	OW_finish();
145 
146 	if (dir_buffer_copy) {
147 		free(dir_buffer_copy);
148 		dir_buffer_copy = NULL;
149 	}
150 
151 	return 0;
152 }
153