e26a97a5787030fe80d896dcf5f75a2062b4a391
[owSlave2.git] / tools_cmd / rwOW / owerr.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 // owerr.c - Library functions for error handling with 1-Wire library
28 //
29 // Version: 1.00
30 //
31
32 #include <string.h>
33 #ifndef _WIN32_WCE
34 #include <stdio.h>
35 #endif
36 #ifdef _WIN64
37 #include <stdio.h>
38 #endif
39 #include "ownet.h"
40
41 #ifndef SIZE_OWERROR_STACK
42    #ifdef SMALL_MEMORY_TARGET
43       //for small memory, only hole 1 error
44       #define SIZE_OWERROR_STACK 1
45    #else
46       #define SIZE_OWERROR_STACK 10
47    #endif
48 #endif
49
50 //---------------------------------------------------------------------------
51 // Variables
52 //---------------------------------------------------------------------------
53
54 // Error Struct for holding error information.
55 // In DEBUG, this will also hold the line number and filename.
56 typedef struct
57 {
58    int owErrorNum;
59 #ifdef DEBUG
60    int lineno;
61    char *filename;
62 #endif
63 } owErrorStruct;
64
65 // Ring-buffer used for stack.
66 // In case of overflow, deepest error is over-written.
67 static owErrorStruct owErrorStack[SIZE_OWERROR_STACK];
68
69 // Stack pointer to top-most error.
70 static int owErrorPointer = 0;
71
72
73 //---------------------------------------------------------------------------
74 // Functions Definitions
75 //---------------------------------------------------------------------------
76 int owGetErrorNum(void);
77 void owClearError(void);
78 int owHasErrors(void);
79 #ifdef DEBUG
80    void owRaiseError(int,int,char*);
81 #else
82    void owRaiseError(int);
83 #endif
84 #ifndef SMALL_MEMORY_TARGET
85    void owPrintErrorMsg(FILE *);
86    void owPrintErrorMsgStd();
87    char *owGetErrorMsg(int);
88 #endif
89
90
91 //--------------------------------------------------------------------------
92 // The 'owGetErroNum' returns the error code of the top-most error on the
93 // error stack.  NOTE: This function has the side effect of popping the
94 // current error off the stack.  All successive calls to 'owGetErrorNum'
95 // will further clear the error stack.
96 //
97 // For list of error codes, see 'ownet.h'
98 //
99 // Returns:   int :  The error code of the top-most error on the stack
100 //
101 int owGetErrorNum(void)
102 {
103    int i = owErrorStack[ owErrorPointer ].owErrorNum;
104    owErrorStack[ owErrorPointer ].owErrorNum = 0;
105    if(!owErrorPointer)
106       owErrorPointer = SIZE_OWERROR_STACK - 1;
107    else
108       owErrorPointer = (owErrorPointer - 1);
109    return i;
110 }
111
112 //--------------------------------------------------------------------------
113 // The 'owClearError' clears all the errors.
114 //
115 void owClearError(void)
116 {
117    owErrorStack[ owErrorPointer ].owErrorNum = 0;
118 }
119
120 //--------------------------------------------------------------------------
121 // The 'owHasErrors' is a boolean test function which tests whether or not
122 // a valid error is waiting on the stack.
123 //
124 // Returns:   TRUE (1) : When there are errors on the stack.
125 //            FALSE (0): When stack's errors are set to 0, or NO_ERROR_SET.
126 //
127 int owHasErrors(void)
128 {
129    if(owErrorStack[ owErrorPointer ].owErrorNum)
130       return 1; //TRUE
131    else
132       return 0; //FALSE
133 }
134
135 #ifdef DEBUG
136    //--------------------------------------------------------------------------
137    // The 'owRaiseError' is the method for raising an error onto the error
138    // stack.
139    //
140    // Arguments:  int err - the error code you wish to raise.
141    //             int lineno - DEBUG only - the line number where it was raised
142    //             char* filename - DEBUG only - the file name where it occured.
143    //
144    void owRaiseError(int err, int lineno, char* filename)
145    {
146       owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;
147       owErrorStack[ owErrorPointer ].owErrorNum = err;
148       owErrorStack[ owErrorPointer ].lineno = lineno;
149       owErrorStack[ owErrorPointer ].filename = filename;
150    }
151 #else
152    //--------------------------------------------------------------------------
153    // The 'owRaiseError' is the method for raising an error onto the error
154    // stack.
155    //
156    // Arguments:  int err - the error code you wish to raise.
157    //
158    void owRaiseError(int err)
159    {
160       owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;
161       owErrorStack[ owErrorPointer ].owErrorNum = err;
162    }
163 #endif
164
165
166 // SMALL_MEMORY_TARGET - embedded microcontrollers, where these
167 // messaging functions might not make any sense.
168 #ifndef SMALL_MEMORY_TARGET
169    //Array of meaningful error messages to associate with codes.
170    //Not used on targets with low memory (i.e. PIC).
171    static char *owErrorMsg[125] =
172    {
173    /*000*/ "No Error Was Set",
174    /*001*/ "No Devices found on 1-Wire Network",
175    /*002*/ "1-Wire Net Reset Failed",
176    /*003*/ "Search ROM Error: Couldn't locate next device on 1-Wire",
177    /*004*/ "Access Failed: Could not select device",
178    /*005*/ "DS2480B Adapter Not Detected",
179    /*006*/ "DS2480B: Wrong Baud",
180    /*007*/ "DS2480B: Bad Response",
181    /*008*/ "Open COM Failed",
182    /*009*/ "Write COM Failed",
183    /*010*/ "Read COM Failed",
184    /*011*/ "Data Block Too Large",
185    /*012*/ "Block Transfer failed",
186    /*013*/ "Program Pulse Failed",
187    /*014*/ "Program Byte Failed",
188    /*015*/ "Write Byte Failed",
189    /*016*/ "Read Byte Failed",
190    /*017*/ "Write Verify Failed",
191    /*018*/ "Read Verify Failed",
192    /*019*/ "Write Scratchpad Failed",
193    /*020*/ "Copy Scratchpad Failed",
194    /*021*/ "Incorrect CRC Length",
195    /*022*/ "CRC Failed",
196    /*023*/ "Failed to acquire a necessary system resource",
197    /*024*/ "Failed to initialize system resource",
198    /*025*/ "Data too long to fit on specified device.",
199    /*026*/ "Read exceeds memory bank end.",
200    /*027*/ "Write exceeds memory bank end.",
201    /*028*/ "Device select failed",
202    /*029*/ "Read Scratch Pad verify failed.",
203    /*030*/ "Copy scratchpad complete not found",
204    /*031*/ "Erase scratchpad complete not found",
205    /*032*/ "Address read back from scrachpad was incorrect",
206    /*033*/ "Read page with extra-info not supported by this memory bank",
207    /*034*/ "Read page packet with extra-info not supported by this memory bank",
208    /*035*/ "Length of packet requested exceeds page size",
209    /*036*/ "Invalid length in packet",
210    /*037*/ "Program pulse required but not available",
211    /*038*/ "Trying to access a read-only memory bank",
212    /*039*/ "Current bank is not general purpose memory",
213    /*040*/ "Read back from write compare is incorrect, page may be locked",
214    /*041*/ "Invalid page number for this memory bank",
215    /*042*/ "Read page with CRC not supported by this memory bank",
216    /*043*/ "Read page with CRC and extra-info not supported by this memory bank",
217    /*044*/ "Read back from write incorrect, could not lock page",
218    /*045*/ "Read back from write incorrect, could not lock redirect byte",
219    /*046*/ "The read of the status was not completed.",
220    /*047*/ "Page redirection not supported by this memory bank",
221    /*048*/ "Lock Page redirection not supported by this memory bank",
222    /*049*/ "Read back byte on EPROM programming did not match.",
223    /*050*/ "Can not write to a page that is locked.",
224    /*051*/ "Can not lock a redirected page that has already been locked.",
225    /*052*/ "Trying to redirect a locked redirected page.",
226    /*053*/ "Trying to lock a page that is already locked.",
227    /*054*/ "Trying to write to a memory bank that is write protected.",
228    /*055*/ "Error due to not matching MAC.",
229    /*056*/ "Memory Bank is write protected.",
230    /*057*/ "Secret is write protected, can not Load First Secret.",
231    /*058*/ "Error in Reading Scratchpad after Computing Next Secret.",
232    /*059*/ "Load Error from Loading First Secret.",
233    /*060*/ "Power delivery required but not available",
234    /*061*/ "Not a valid file name.",
235    /*062*/ "Unable to Create a Directory in this part.",
236    /*063*/ "That file already exists.",
237    /*064*/ "The directory is not empty.",
238    /*065*/ "The wrong type of part for this operation.",
239    /*066*/ "The max len for this file is too small.",
240    /*067*/ "This is not a write once bank.",
241    /*068*/ "The file can not be found.",
242    /*069*/ "There is not enough space available.",
243    /*070*/ "There is not a page to match that bit in the bitmap.",
244    /*071*/ "There are no jobs for EPROM parts.",
245    /*072*/ "Function not supported to modify attributes.",
246    /*073*/ "Handle is not in use.",
247    /*074*/ "Tring to read a write only file.",
248    /*075*/ "There is no handle available for use.",
249    /*076*/ "The directory provided is an invalid directory.",
250    /*077*/ "Handle does not exist.",
251    /*078*/ "Serial Number did not match with current job.",
252    /*079*/ "Can not program EPROM because a non-EPROM part on the network.",
253    /*080*/ "Write protect redirection byte is set.",
254    /*081*/ "There is an inappropriate directory length.",
255    /*082*/ "The file has already been terminated.",
256    /*083*/ "Failed to read memory page of iButton part.",
257    /*084*/ "Failed to match scratchpad of iButton part.",
258    /*085*/ "Failed to erase scratchpad of iButton part.",
259    /*086*/ "Failed to read scratchpad of iButton part.",
260    /*087*/ "Failed to execute SHA function on SHA iButton.",
261    /*088*/ "SHA iButton did not return a status completion byte.",
262    /*089*/ "Write data page failed.",
263    /*090*/ "Copy secret into secret memory pages failed.",
264    /*091*/ "Bind unique secret to iButton failed.",
265    /*092*/ "Could not install secret into user token.",
266    /*093*/ "Transaction Incomplete: signature did not match.",
267    /*094*/ "Transaction Incomplete: could not sign service data.",
268    /*095*/ "User token did not provide a valid authentication response.",
269    /*096*/ "Failed to answer a challenge on the user token.",
270    /*097*/ "Failed to create a challenge on the coprocessor.",
271    /*098*/ "Transaction Incomplete: service data was not valid.",
272    /*099*/ "Transaction Incomplete: service data was not updated.",
273    /*100*/ "Unrecoverable, catastrophic service failure occured.",
274    /*101*/ "Load First Secret from scratchpad data failed.",
275    /*102*/ "Failed to match signature of user's service data.",
276    /*103*/ "Subkey out of range for the DS1991.",
277    /*104*/ "Block ID out of range for the DS1991",
278    /*105*/ "Password is enabled",
279    /*106*/ "Password is invalid",
280    /*107*/ "This memory bank has no read only password",
281    /*108*/ "This memory bank has no read/write password",
282    /*109*/ "1-Wire is shorted",
283    /*110*/ "Error communicating with 1-Wire adapter",
284    /*111*/ "CopyScratchpad failed: Ending Offset must go to end of page",
285    /*112*/ "WriteScratchpad failed: Ending Offset must go to end of page",
286    /*113*/ "Mission can not be stopped while one is not in progress",
287    /*114*/ "Error stopping the mission",
288    /*115*/ "Port number is outside (0,MAX_PORTNUM) interval",
289    /*116*/ "Level of the 1-Wire was not changed",
290    /*117*/ "Both the Read Only and Read Write Passwords must be set",
291    /*118*/ "Failure to change latch state."
292    /*119*/ "Could not open usb port through libusb",
293    /*120*/ "Libusb DS2490 port already opened",
294    /*121*/ "Failed to set libusb configuration",
295    /*122*/ "Failed to claim libusb interface",
296    /*123*/ "Failed to set libusb altinterface",
297    /*124*/ "No adapter found at this port number"
298    };
299
300    char *owGetErrorMsg(int err)
301    {
302       return owErrorMsg[err];
303    }
304
305 #ifndef __C51__
306    //--------------------------------------------------------------------------
307    // The 'owPrintErrorMsg' is the method for printing an error from the stack.
308    // The destination for the print is specified by the argument, fileno, which
309    // can be stderr, stdout, or a log file.  In non-debug mode, the output is
310    // of the form:
311    // Error num: Error msg
312    //
313    // In debug-mode, the output is of the form:
314    // Error num: filename line#: Error msg
315    //
316    // NOTE: This function has the side-effect of popping the error off the stack.
317    //
318    // Arguments:  FILE*: the destination for printing.
319    //
320    void owPrintErrorMsg(FILE *filenum)
321    {
322    #ifdef DEBUG
323       int l = owErrorStack[ owErrorPointer ].lineno;
324       char *f = owErrorStack[ owErrorPointer ].filename;
325       int err = owGetErrorNum();
326       fprintf(filenum,"Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);
327    #else
328       int err = owGetErrorNum();
329       fprintf(filenum,"Error %d: %s\r\n",err,owErrorMsg[err]);
330    #endif
331    }
332 #endif //__C51__
333
334    // Same as above, except uses default printf output
335    void owPrintErrorMsgStd()
336    {
337    #ifdef DEBUG
338       int l = owErrorStack[ owErrorPointer ].lineno;
339       char *f = owErrorStack[ owErrorPointer ].filename;
340       int err = owGetErrorNum();
341       printf("Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);
342    #else
343       int err = owGetErrorNum();
344       printf("Error %d: %s\r\n",err,owErrorMsg[err]);
345    #endif
346    }
347 #endif
348