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