Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / thermodl.c
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 // local function prototypes
47 void PrintResults(ThermoStateType *,FILE *,int);
48
49 // tini hack
50 #define ExitProg(msg,exit_code) {printf("%s\n",msg); exit(exit_code);}
51 int argc=2;
52 char *argv[]={__FILE__, "exow"};
53
54 //----------------------------------------------------------------------
55 //  This is the Main routine for thermodl.
56 //
57 int main() //short argc, char **argv)
58 {
59    int Fahrenheit=FALSE,filenum,num,i,j;
60    char return_msg[128];
61    FILE *fp;
62    ThermoStateType ThermoState;
63    uchar ThermoSN[MAXDEVICES][8]; //the serial numbers for the devices
64    int portnum=0;
65
66    // check arguments to see if request instruction with '?' or too many
67    if ((argc < 2) || (argc > 4) || ((argc > 1) && (argv[1][0] == '?' || argv[1][1] == '?')))
68        ExitProg("\nusage: thermodl 1wire_net_name <output_filename> </Fahrenheit>\n"
69               "  - Thermochron download on the 1-Wire Net port\n"
70               "  - 1wire_net_port required port name\n"
71               "    example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" \n"
72               "    (Linux DS2480),\"1\" (Win32 TMEX)\n"
73               "  - <output_filename> optional output filename\n"
74               "  - </Fahrenheit> optional Fahrenheit mode (default Celsius)\n"
75               "  - version 2.00\n",1);
76
77    // attempt to acquire the 1-Wire Net
78    if (!owAcquire(portnum,argv[1],return_msg))
79       ExitProg(return_msg,1);
80
81    // success
82    printf("%s",return_msg);
83
84    //----------------------------------------
85    // Introduction
86    printf("\n/----------------------------------------------\n");
87    printf("  Find and download DS1921 Thermochron iButton(s)\n"
88           "  Version 2.00\n\n");
89
90    // check arguments for temperature conversion and filename
91    Fahrenheit = FALSE;
92    filenum = 0;
93    if (argc >= 3)
94    {
95       if (argv[2][0] != '/')
96          filenum = 2;
97       else if ((argv[2][1] == 'F') || (argv[2][1] == 'f'))
98          Fahrenheit = TRUE;
99
100       if (argc == 4)
101       {
102          if (argv[3][0] != '/')
103             filenum = 3;
104          else if ((argv[3][1] == 'F') || (argv[3][1] == 'f'))
105             Fahrenheit = TRUE;
106       }
107    }
108
109    // open the output file
110    fp = NULL;
111    if (filenum > 0)
112    {
113      fp = fopen(argv[filenum],"w+");
114      if(fp == NULL)
115        {
116          printf("ERROR, Could not open output file!\n");
117          exit(1);
118        }
119      else
120        printf("File '%s' opened to write mission results.\n",
121               argv[filenum]);
122    }
123
124    // get list of Thermochron's
125    num = FindDevices(portnum, &ThermoSN[0],THERMO_FAM, MAXDEVICES);
126
127    // check if not present or more then 1 present
128    if (num == 0)
129       ExitProg("Thermochron not present on 1-Wire\n",1);
130
131    // loop to download each Thermochron
132    for (i = 0; i < num; i++)
133    {
134       // set the serial number portion in the thermo state
135       printf("\nDownloading: ");
136       for (j = 7; j >= 0; j--)
137       {
138          ThermoState.MissStat.serial_num[j] = ThermoSN[i][j];
139          printf("%02X",ThermoSN[i][j]);
140       }
141       printf("\n");
142       // download the Thermochron found
143       if (DownloadThermo(portnum,&ThermoSN[i][0],&ThermoState,stdout))
144       {
145         // interpret the results of the download
146         InterpretStatus(&ThermoState.MissStat);
147         InterpretAlarms(&ThermoState.AlarmData, &ThermoState.MissStat);
148         InterpretHistogram(&ThermoState.HistData);
149         InterpretLog(&ThermoState.LogData, &ThermoState.MissStat);
150
151          // print the output
152         PrintResults(&ThermoState,fp,Fahrenheit);
153       }
154       else
155       {
156          fprintf(fp,"\nError downloading device: ");
157          for (j = 0; j < 8; j++)
158             fprintf(fp,"%02X",ThermoSN[i][j]);
159          fprintf(fp,"\n");
160       }
161    }
162
163    // close opened file
164    if (fp != NULL)
165    {
166       printf("File '%s' closed.\n",
167               argv[filenum]);
168       fclose(fp);
169    }
170
171    // release the 1-Wire Net
172    //owRelease(portnum,return_msg);
173    printf("\n%s",return_msg);
174    ExitProg("End program normally\n",0);
175    return 0;
176 }
177
178 //--------------------------------------------------------------------------
179 //  Prints the mission data optionaly to a file or standard out
180 //
181 void PrintResults(ThermoStateType *ThermoState, FILE *fp, int ConvertToF)
182 {
183   // aslink only can handle 64k, so we use the second 64k bank
184    char *str=(xdata char*)0x190000;
185
186    // check if need to use standard out
187    if (fp == NULL)
188       fp = stdout;
189
190 #if 0
191    // get big block to use as a buffer
192    str = malloc(80000);
193    if (str == NULL)
194    {
195       printf("Insufficient memory available to print!\n");
196       return;
197    }
198 #endif
199
200    // mission status
201    MissionStatusToString(&ThermoState->MissStat, ConvertToF, &str[0]);
202    fprintf(fp,"\n%s\n",str);
203
204    // alarm events
205    AlarmsToString(&ThermoState->AlarmData, &str[0]);
206    fprintf(fp,"%s\n",str);
207
208    // histogram
209    HistogramToString(&ThermoState->HistData, ConvertToF, &str[0]);
210    fprintf(fp,"%s\n",str);
211
212    // log data
213    LogToString(&ThermoState->LogData, ConvertToF, &str[0]);
214    fprintf(fp,"%s\n",str);
215
216    // debug raw data
217    DebugToString(&ThermoState->MissStat, &ThermoState->AlarmData,
218       &ThermoState->HistData, &ThermoState->LogData, &str[0]);
219    fprintf(fp,"%s\n",str);
220
221 #if 0
222    // free the memory block used
223    free(str);
224 #endif
225 }
226