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