Works now with Windows Visual Studio C++ too
[owTools.git] / windows / owTools / owerr.cpp
diff --git a/windows/owTools/owerr.cpp b/windows/owTools/owerr.cpp
new file mode 100644 (file)
index 0000000..76c5176
--- /dev/null
@@ -0,0 +1,348 @@
+//---------------------------------------------------------------------------\r
+// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining a\r
+// copy of this software and associated documentation files (the "Software"),\r
+// to deal in the Software without restriction, including without limitation\r
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
+// and/or sell copies of the Software, and to permit persons to whom the\r
+// Software is furnished to do so, subject to the following conditions:\r
+//\r
+// The above copyright notice and this permission notice shall be included\r
+// in all copies or substantial portions of the Software.\r
+//\r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
+// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES\r
+// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
+// OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
+// Except as contained in this notice, the name of Dallas Semiconductor\r
+// shall not be used except as stated in the Dallas Semiconductor\r
+// Branding Policy.\r
+//---------------------------------------------------------------------------\r
+//\r
+// owerr.c - Library functions for error handling with 1-Wire library\r
+//\r
+// Version: 1.00\r
+//\r
+\r
+#include <string.h>\r
+#ifndef _WIN32_WCE\r
+#include <stdio.h>\r
+#endif\r
+#ifdef _WIN64\r
+#include <stdio.h>\r
+#endif\r
+#include "ownet.h"\r
+\r
+#ifndef SIZE_OWERROR_STACK\r
+   #ifdef SMALL_MEMORY_TARGET\r
+      //for small memory, only hole 1 error\r
+      #define SIZE_OWERROR_STACK 1\r
+   #else\r
+      #define SIZE_OWERROR_STACK 10\r
+   #endif\r
+#endif\r
+\r
+//---------------------------------------------------------------------------\r
+// Variables\r
+//---------------------------------------------------------------------------\r
+\r
+// Error Struct for holding error information.\r
+// In DEBUG, this will also hold the line number and filename.\r
+typedef struct\r
+{\r
+   int owErrorNum;\r
+#ifdef DEBUG\r
+   int lineno;\r
+   char *filename;\r
+#endif\r
+} owErrorStruct;\r
+\r
+// Ring-buffer used for stack.\r
+// In case of overflow, deepest error is over-written.\r
+static owErrorStruct owErrorStack[SIZE_OWERROR_STACK];\r
+\r
+// Stack pointer to top-most error.\r
+static int owErrorPointer = 0;\r
+\r
+\r
+//---------------------------------------------------------------------------\r
+// Functions Definitions\r
+//---------------------------------------------------------------------------\r
+int owGetErrorNum(void);\r
+void owClearError(void);\r
+int owHasErrors(void);\r
+#ifdef DEBUG\r
+   void owRaiseError(int,int,char*);\r
+#else\r
+   void owRaiseError(int);\r
+#endif\r
+#ifndef SMALL_MEMORY_TARGET\r
+   void owPrintErrorMsg(FILE *);\r
+   void owPrintErrorMsgStd();\r
+   char *owGetErrorMsg(int);\r
+#endif\r
+\r
+\r
+//--------------------------------------------------------------------------\r
+// The 'owGetErroNum' returns the error code of the top-most error on the\r
+// error stack.  NOTE: This function has the side effect of popping the\r
+// current error off the stack.  All successive calls to 'owGetErrorNum'\r
+// will further clear the error stack.\r
+//\r
+// For list of error codes, see 'ownet.h'\r
+//\r
+// Returns:   int :  The error code of the top-most error on the stack\r
+//\r
+int owGetErrorNum(void)\r
+{\r
+   int i = owErrorStack[ owErrorPointer ].owErrorNum;\r
+   owErrorStack[ owErrorPointer ].owErrorNum = 0;\r
+   if(!owErrorPointer)\r
+      owErrorPointer = SIZE_OWERROR_STACK - 1;\r
+   else\r
+      owErrorPointer = (owErrorPointer - 1);\r
+   return i;\r
+}\r
+\r
+//--------------------------------------------------------------------------\r
+// The 'owClearError' clears all the errors.\r
+//\r
+void owClearError(void)\r
+{\r
+   owErrorStack[ owErrorPointer ].owErrorNum = 0;\r
+}\r
+\r
+//--------------------------------------------------------------------------\r
+// The 'owHasErrors' is a boolean test function which tests whether or not\r
+// a valid error is waiting on the stack.\r
+//\r
+// Returns:   TRUE (1) : When there are errors on the stack.\r
+//            FALSE (0): When stack's errors are set to 0, or NO_ERROR_SET.\r
+//\r
+int owHasErrors(void)\r
+{\r
+   if(owErrorStack[ owErrorPointer ].owErrorNum)\r
+      return 1; //TRUE\r
+   else\r
+      return 0; //FALSE\r
+}\r
+\r
+#ifdef DEBUG\r
+   //--------------------------------------------------------------------------\r
+   // The 'owRaiseError' is the method for raising an error onto the error\r
+   // stack.\r
+   //\r
+   // Arguments:  int err - the error code you wish to raise.\r
+   //             int lineno - DEBUG only - the line number where it was raised\r
+   //             char* filename - DEBUG only - the file name where it occured.\r
+   //\r
+   void owRaiseError(int err, int lineno, char* filename)\r
+   {\r
+      owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;\r
+      owErrorStack[ owErrorPointer ].owErrorNum = err;\r
+      owErrorStack[ owErrorPointer ].lineno = lineno;\r
+      owErrorStack[ owErrorPointer ].filename = filename;\r
+   }\r
+#else\r
+   //--------------------------------------------------------------------------\r
+   // The 'owRaiseError' is the method for raising an error onto the error\r
+   // stack.\r
+   //\r
+   // Arguments:  int err - the error code you wish to raise.\r
+   //\r
+   void owRaiseError(int err)\r
+   {\r
+      owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;\r
+      owErrorStack[ owErrorPointer ].owErrorNum = err;\r
+   }\r
+#endif\r
+\r
+\r
+// SMALL_MEMORY_TARGET - embedded microcontrollers, where these\r
+// messaging functions might not make any sense.\r
+#ifndef SMALL_MEMORY_TARGET\r
+   //Array of meaningful error messages to associate with codes.\r
+   //Not used on targets with low memory (i.e. PIC).\r
+   static char *owErrorMsg[125] =\r
+   {\r
+   /*000*/ "No Error Was Set",\r
+   /*001*/ "No Devices found on 1-Wire Network",\r
+   /*002*/ "1-Wire Net Reset Failed",\r
+   /*003*/ "Search ROM Error: Couldn't locate next device on 1-Wire",\r
+   /*004*/ "Access Failed: Could not select device",\r
+   /*005*/ "DS2480B Adapter Not Detected",\r
+   /*006*/ "DS2480B: Wrong Baud",\r
+   /*007*/ "DS2480B: Bad Response",\r
+   /*008*/ "Open COM Failed",\r
+   /*009*/ "Write COM Failed",\r
+   /*010*/ "Read COM Failed",\r
+   /*011*/ "Data Block Too Large",\r
+   /*012*/ "Block Transfer failed",\r
+   /*013*/ "Program Pulse Failed",\r
+   /*014*/ "Program Byte Failed",\r
+   /*015*/ "Write Byte Failed",\r
+   /*016*/ "Read Byte Failed",\r
+   /*017*/ "Write Verify Failed",\r
+   /*018*/ "Read Verify Failed",\r
+   /*019*/ "Write Scratchpad Failed",\r
+   /*020*/ "Copy Scratchpad Failed",\r
+   /*021*/ "Incorrect CRC Length",\r
+   /*022*/ "CRC Failed",\r
+   /*023*/ "Failed to acquire a necessary system resource",\r
+   /*024*/ "Failed to initialize system resource",\r
+   /*025*/ "Data too long to fit on specified device.",\r
+   /*026*/ "Read exceeds memory bank end.",\r
+   /*027*/ "Write exceeds memory bank end.",\r
+   /*028*/ "Device select failed",\r
+   /*029*/ "Read Scratch Pad verify failed.",\r
+   /*030*/ "Copy scratchpad complete not found",\r
+   /*031*/ "Erase scratchpad complete not found",\r
+   /*032*/ "Address read back from scrachpad was incorrect",\r
+   /*033*/ "Read page with extra-info not supported by this memory bank",\r
+   /*034*/ "Read page packet with extra-info not supported by this memory bank",\r
+   /*035*/ "Length of packet requested exceeds page size",\r
+   /*036*/ "Invalid length in packet",\r
+   /*037*/ "Program pulse required but not available",\r
+   /*038*/ "Trying to access a read-only memory bank",\r
+   /*039*/ "Current bank is not general purpose memory",\r
+   /*040*/ "Read back from write compare is incorrect, page may be locked",\r
+   /*041*/ "Invalid page number for this memory bank",\r
+   /*042*/ "Read page with CRC not supported by this memory bank",\r
+   /*043*/ "Read page with CRC and extra-info not supported by this memory bank",\r
+   /*044*/ "Read back from write incorrect, could not lock page",\r
+   /*045*/ "Read back from write incorrect, could not lock redirect byte",\r
+   /*046*/ "The read of the status was not completed.",\r
+   /*047*/ "Page redirection not supported by this memory bank",\r
+   /*048*/ "Lock Page redirection not supported by this memory bank",\r
+   /*049*/ "Read back byte on EPROM programming did not match.",\r
+   /*050*/ "Can not write to a page that is locked.",\r
+   /*051*/ "Can not lock a redirected page that has already been locked.",\r
+   /*052*/ "Trying to redirect a locked redirected page.",\r
+   /*053*/ "Trying to lock a page that is already locked.",\r
+   /*054*/ "Trying to write to a memory bank that is write protected.",\r
+   /*055*/ "Error due to not matching MAC.",\r
+   /*056*/ "Memory Bank is write protected.",\r
+   /*057*/ "Secret is write protected, can not Load First Secret.",\r
+   /*058*/ "Error in Reading Scratchpad after Computing Next Secret.",\r
+   /*059*/ "Load Error from Loading First Secret.",\r
+   /*060*/ "Power delivery required but not available",\r
+   /*061*/ "Not a valid file name.",\r
+   /*062*/ "Unable to Create a Directory in this part.",\r
+   /*063*/ "That file already exists.",\r
+   /*064*/ "The directory is not empty.",\r
+   /*065*/ "The wrong type of part for this operation.",\r
+   /*066*/ "The max len for this file is too small.",\r
+   /*067*/ "This is not a write once bank.",\r
+   /*068*/ "The file can not be found.",\r
+   /*069*/ "There is not enough space available.",\r
+   /*070*/ "There is not a page to match that bit in the bitmap.",\r
+   /*071*/ "There are no jobs for EPROM parts.",\r
+   /*072*/ "Function not supported to modify attributes.",\r
+   /*073*/ "Handle is not in use.",\r
+   /*074*/ "Tring to read a write only file.",\r
+   /*075*/ "There is no handle available for use.",\r
+   /*076*/ "The directory provided is an invalid directory.",\r
+   /*077*/ "Handle does not exist.",\r
+   /*078*/ "Serial Number did not match with current job.",\r
+   /*079*/ "Can not program EPROM because a non-EPROM part on the network.",\r
+   /*080*/ "Write protect redirection byte is set.",\r
+   /*081*/ "There is an inappropriate directory length.",\r
+   /*082*/ "The file has already been terminated.",\r
+   /*083*/ "Failed to read memory page of iButton part.",\r
+   /*084*/ "Failed to match scratchpad of iButton part.",\r
+   /*085*/ "Failed to erase scratchpad of iButton part.",\r
+   /*086*/ "Failed to read scratchpad of iButton part.",\r
+   /*087*/ "Failed to execute SHA function on SHA iButton.",\r
+   /*088*/ "SHA iButton did not return a status completion byte.",\r
+   /*089*/ "Write data page failed.",\r
+   /*090*/ "Copy secret into secret memory pages failed.",\r
+   /*091*/ "Bind unique secret to iButton failed.",\r
+   /*092*/ "Could not install secret into user token.",\r
+   /*093*/ "Transaction Incomplete: signature did not match.",\r
+   /*094*/ "Transaction Incomplete: could not sign service data.",\r
+   /*095*/ "User token did not provide a valid authentication response.",\r
+   /*096*/ "Failed to answer a challenge on the user token.",\r
+   /*097*/ "Failed to create a challenge on the coprocessor.",\r
+   /*098*/ "Transaction Incomplete: service data was not valid.",\r
+   /*099*/ "Transaction Incomplete: service data was not updated.",\r
+   /*100*/ "Unrecoverable, catastrophic service failure occured.",\r
+   /*101*/ "Load First Secret from scratchpad data failed.",\r
+   /*102*/ "Failed to match signature of user's service data.",\r
+   /*103*/ "Subkey out of range for the DS1991.",\r
+   /*104*/ "Block ID out of range for the DS1991",\r
+   /*105*/ "Password is enabled",\r
+   /*106*/ "Password is invalid",\r
+   /*107*/ "This memory bank has no read only password",\r
+   /*108*/ "This memory bank has no read/write password",\r
+   /*109*/ "1-Wire is shorted",\r
+   /*110*/ "Error communicating with 1-Wire adapter",\r
+   /*111*/ "CopyScratchpad failed: Ending Offset must go to end of page",\r
+   /*112*/ "WriteScratchpad failed: Ending Offset must go to end of page",\r
+   /*113*/ "Mission can not be stopped while one is not in progress",\r
+   /*114*/ "Error stopping the mission",\r
+   /*115*/ "Port number is outside (0,MAX_PORTNUM) interval",\r
+   /*116*/ "Level of the 1-Wire was not changed",\r
+   /*117*/ "Both the Read Only and Read Write Passwords must be set",\r
+   /*118*/ "Failure to change latch state."\r
+   /*119*/ "Could not open usb port through libusb",\r
+   /*120*/ "Libusb DS2490 port already opened",\r
+   /*121*/ "Failed to set libusb configuration",\r
+   /*122*/ "Failed to claim libusb interface",\r
+   /*123*/ "Failed to set libusb altinterface",\r
+   /*124*/ "No adapter found at this port number"\r
+   };\r
+\r
+   char *owGetErrorMsg(int err)\r
+   {\r
+      return owErrorMsg[err];\r
+   }\r
+\r
+#ifndef __C51__\r
+   //--------------------------------------------------------------------------\r
+   // The 'owPrintErrorMsg' is the method for printing an error from the stack.\r
+   // The destination for the print is specified by the argument, fileno, which\r
+   // can be stderr, stdout, or a log file.  In non-debug mode, the output is\r
+   // of the form:\r
+   // Error num: Error msg\r
+   //\r
+   // In debug-mode, the output is of the form:\r
+   // Error num: filename line#: Error msg\r
+   //\r
+   // NOTE: This function has the side-effect of popping the error off the stack.\r
+   //\r
+   // Arguments:  FILE*: the destination for printing.\r
+   //\r
+   void owPrintErrorMsg(FILE *filenum)\r
+   {\r
+   #ifdef DEBUG\r
+      int l = owErrorStack[ owErrorPointer ].lineno;\r
+      char *f = owErrorStack[ owErrorPointer ].filename;\r
+      int err = owGetErrorNum();\r
+      fprintf(filenum,"Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);\r
+   #else\r
+      int err = owGetErrorNum();\r
+      fprintf(filenum,"Error %d: %s\r\n",err,owErrorMsg[err]);\r
+   #endif\r
+   }\r
+#endif //__C51__\r
+\r
+   // Same as above, except uses default printf output\r
+   void owPrintErrorMsgStd()\r
+   {\r
+   #ifdef DEBUG\r
+      int l = owErrorStack[ owErrorPointer ].lineno;\r
+      char *f = owErrorStack[ owErrorPointer ].filename;\r
+      int err = owGetErrorNum();\r
+      printf("Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);\r
+   #else\r
+      int err = owGetErrorNum();\r
+      printf("Error %d: %s\r\n",err,owErrorMsg[err]);\r
+   #endif\r
+   }\r
+#endif\r
+\r