1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
24 // Branding Policy.
25 //---------------------------------------------------------------------------
26 //
27 // thermodl.c - This utility uses to download the results of the
28 // current mission of a DS1921 Thermochron iButton.
29 //
30 // Version: 2.00
31 //
32 // History:
33 // 1.03 -> 2.00 Reorganization of Public Domain Kit
34 // Y2K update, display all histogram bins, debug
35 // dump. Supports multiple thermochons.
36 //
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "ownet.h"
41 #include "thermo21.h"
42
43 // defines
44 #define MAXDEVICES 20
45
46 // external function prototypes
47 extern int DownloadThermo(int,uchar *,ThermoStateType *, FILE *);
48 extern void DebugToString(MissionStatus *, TempAlarmEvents *, Histogram *, Log *, char *);
49 extern void MissionStatusToString(MissionStatus *,int,char *);
50 extern void InterpretStatus(MissionStatus *);
51 extern void InterpretHistogram(Histogram *);
52 extern void HistogramToString(Histogram *,int,char *);
53 extern void InterpretAlarms(TempAlarmEvents *,MissionStatus *);
54 extern void AlarmsToString(TempAlarmEvents *,char *);
55 extern void InterpretLog(Log *, MissionStatus *);
56 extern void LogToString(Log *,int,char *);
57 extern SMALLINT FindDevices(int,uchar FamilySN[][8],SMALLINT,SMALLINT);
58 extern SMALLINT owAcquire(int,char *);
59 extern void owRelease(int);
60 extern void ExitProg(char *,int);
61
62 // local function prototypes
63 void PrintResults(ThermoStateType *,FILE *,int);
64
65 //----------------------------------------------------------------------
66 // This is the Main routine for thermodl.
67 //
main(int argc,char ** argv)68 int main(int argc, char **argv)
69 {
70 int Fahrenheit=FALSE,filenum,num,i,j;
71 FILE *fp;
72 ThermoStateType ThermoState;
73 uchar ThermoSN[MAXDEVICES][8]; //the serial numbers for the devices
74 int portnum=0;
75
76 // check arguments to see if request instruction with '?' or too many
77 if ((argc < 2) || (argc > 4) || ((argc > 1) && (argv[1][0] == '?' || argv[1][1] == '?')))
78 ExitProg("\nusage: thermodl 1wire_net_name <output_filename> </Fahrenheit>\n"
79 " - Thermochron download on the 1-Wire Net port\n"
80 " - 1wire_net_port required port name\n"
81 " example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" \n"
82 " (Linux DS2480),\"1\" (Win32 TMEX)\n"
83 " - <output_filename> optional output filename\n"
84 " - </Fahrenheit> optional Fahrenheit mode (default Celsius)\n"
85 " - version 2.00\n",1);
86
87 // attempt to acquire the 1-Wire Net
88 if (!owAcquire(portnum,argv[1]))
89 {
90 OWERROR_DUMP(stdout);
91 exit(1);
92 }
93
94 // success
95 printf("Port opened: %s\n",argv[1]);
96
97 //----------------------------------------
98 // Introduction
99 printf("\n/----------------------------------------------\n");
100 printf(" Find and download DS1921 Thermochron iButton(s)\n"
101 " Version 2.00\n\n");
102
103 // check arguments for temperature conversion and filename
104 Fahrenheit = FALSE;
105 filenum = 0;
106 if (argc >= 3)
107 {
108 if (argv[2][0] != '/')
109 filenum = 2;
110 else if ((argv[2][1] == 'F') || (argv[2][1] == 'f'))
111 Fahrenheit = TRUE;
112
113 if (argc == 4)
114 {
115 if (argv[3][0] != '/')
116 filenum = 3;
117 else if ((argv[3][1] == 'F') || (argv[3][1] == 'f'))
118 Fahrenheit = TRUE;
119 }
120 }
121
122 // open the output file
123 fp = NULL;
124 if (filenum > 0)
125 {
126 fp = fopen(argv[filenum],"w+");
127 if(fp == NULL)
128 {
129 printf("ERROR, Could not open output file!\n");
130 exit(1);
131 }
132 else
133 printf("File '%s' opened to write mission results.\n",
134 argv[filenum]);
135 }
136
137 // get list of Thermochron's
138 num = FindDevices(portnum, &ThermoSN[0],THERMO_FAM, MAXDEVICES);
139
140 // check if not present or more then 1 present
141 if (num == 0)
142 ExitProg("Thermochron not present on 1-Wire\n",1);
143
144 // loop to download each Thermochron
145 for (i = 0; i < num; i++)
146 {
147 // set the serial number portion in the thermo state
148 printf("\nDownloading: ");
149 for (j = 7; j >= 0; j--)
150 {
151 ThermoState.MissStat.serial_num[j] = ThermoSN[i][j];
152 printf("%02X",ThermoSN[i][j]);
153 }
154 printf("\n");
155
156 // download the Thermochron found
157 if (DownloadThermo(portnum,&ThermoSN[i][0],&ThermoState,stdout))
158 {
159 // interpret the results of the download
160 InterpretStatus(&ThermoState.MissStat);
161 InterpretAlarms(&ThermoState.AlarmData, &ThermoState.MissStat);
162 InterpretHistogram(&ThermoState.HistData);
163 InterpretLog(&ThermoState.LogData, &ThermoState.MissStat);
164
165 // print the output
166 PrintResults(&ThermoState,fp,Fahrenheit);
167 }
168 else
169 {
170 fprintf(fp,"\nError downloading device: ");
171 for (j = 0; j < 8; j++)
172 fprintf(fp,"%02X",ThermoSN[i][j]);
173 fprintf(fp,"\n");
174 }
175 }
176
177 // close opened file
178 if (fp != NULL)
179 {
180 printf("File '%s' closed.\n",
181 argv[filenum]);
182 fclose(fp);
183 }
184
185 // release the 1-Wire Net
186 owRelease(portnum);
187 printf("Closing port %s.\n", argv[1]);
188 ExitProg("End program normally\n",0);
189
190 return 0;
191 }
192
193 //--------------------------------------------------------------------------
194 // Prints the mission data optionaly to a file or standard out
195 //
PrintResults(ThermoStateType * ThermoState,FILE * fp,int ConvertToF)196 void PrintResults(ThermoStateType *ThermoState, FILE *fp, int ConvertToF)
197 {
198 char *str;
199
200 // check if need to use standard out
201 if (fp == NULL)
202 fp = stdout;
203
204 // get big block to use as a buffer
205 str = malloc(80000);
206 if (str == NULL)
207 {
208 printf("Insufficient memory available to print!\n");
209 return;
210 }
211
212 // mission status
213 MissionStatusToString(&ThermoState->MissStat, ConvertToF, &str[0]);
214 fprintf(fp,"\n%s\n",str);
215
216 // alarm events
217 AlarmsToString(&ThermoState->AlarmData, &str[0]);
218 fprintf(fp,"%s\n",str);
219
220 // histogram
221 HistogramToString(&ThermoState->HistData, ConvertToF, &str[0]);
222 fprintf(fp,"%s\n",str);
223
224 // log data
225 LogToString(&ThermoState->LogData, ConvertToF, &str[0]);
226 fprintf(fp,"%s\n",str);
227
228 // debug raw data
229 DebugToString(&ThermoState->MissStat, &ThermoState->AlarmData,
230 &ThermoState->HistData, &ThermoState->LogData, &str[0]);
231 fprintf(fp,"%s\n",str);
232
233 // free the memory block used
234 free(str);
235 }
236
237