owerr.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
//---------------------------------------------------------------------------
// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Dallas Semiconductor
// shall not be used except as stated in the Dallas Semiconductor
// Branding Policy.
//---------------------------------------------------------------------------
//
// owerr.c - Library functions for error handling with 1-Wire library
//
// Version: 1.00
//

#include <string.h>
//#include <stdio.h>
#include "ownet.h"
#include "uart.h"


#ifndef SIZE_OWERROR_STACK
   #ifdef SOCKIT_OWM_ERR_SMALL
      //for small memory, only hold 1 error
      #define SIZE_OWERROR_STACK 1
   #else
      #define SIZE_OWERROR_STACK 10
   #endif
#endif

//---------------------------------------------------------------------------
// Variables
//---------------------------------------------------------------------------

// Error Struct for holding error information.
// In DEBUG, this will also hold the line number and filename.
typedef struct
{
   int owErrorNum;
#ifdef DEBUG
   int lineno;
   char *filename;
#endif
} owErrorStruct;

// Ring-buffer used for stack.
// In case of overflow, deepest error is over-written.
static owErrorStruct owErrorStack[SIZE_OWERROR_STACK];

// Stack pointer to top-most error.
static int owErrorPointer = 0;


//---------------------------------------------------------------------------
// Functions Definitions
//---------------------------------------------------------------------------
int owGetErrorNum(void);
void owClearError(void);
int owHasErrors(void);
#ifdef DEBUG
   void owRaiseError(int,int,char*);
#else
   void owRaiseError(int);
#endif
#ifndef SOCKIT_OWM_ERR_SMALL
   void owPrintErrorMsg(FILE *);
   void owPrintErrorMsgStd();
   char *owGetErrorMsg(int);
#endif


//--------------------------------------------------------------------------
// The 'owGetErroNum' returns the error code of the top-most error on the
// error stack. NOTE: This function has the side effect of popping the
// current error off the stack. All successive calls to 'owGetErrorNum'
// will further clear the error stack.
//
// For list of error codes, see 'ownet.h'
//
// Returns: int : The error code of the top-most error on the stack
//
int owGetErrorNum(void)
{
   int i = owErrorStack[ owErrorPointer ].owErrorNum;
   owErrorStack[ owErrorPointer ].owErrorNum = 0;
   if(!owErrorPointer)
      owErrorPointer = SIZE_OWERROR_STACK - 1;
   else
      owErrorPointer = (owErrorPointer - 1);
   return i;
}

//--------------------------------------------------------------------------
// The 'owClearError' clears all the errors.
//
void owClearError(void)
{
   owErrorStack[ owErrorPointer ].owErrorNum = 0;
}

//--------------------------------------------------------------------------
// The 'owHasErrors' is a boolean test function which tests whether or not
// a valid error is waiting on the stack.
//
// Returns: TRUE (1) : When there are errors on the stack.
// FALSE (0): When stack's errors are set to 0, or NO_ERROR_SET.
//
int owHasErrors(void)
{
   if(owErrorStack[ owErrorPointer ].owErrorNum)
      return 1; //TRUE
   else
      return 0; //FALSE
}

#ifdef DEBUG
   //--------------------------------------------------------------------------
   // The 'owRaiseError' is the method for raising an error onto the error
   // stack.
   //
   // Arguments: int err - the error code you wish to raise.
   // int lineno - DEBUG only - the line number where it was raised
   // char* filename - DEBUG only - the file name where it occured.
   //
   void owRaiseError(int err, int lineno, char* filename)
   {
      owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;
      owErrorStack[ owErrorPointer ].owErrorNum = err;
      owErrorStack[ owErrorPointer ].lineno = lineno;
      owErrorStack[ owErrorPointer ].filename = filename;
   }
#else
   //--------------------------------------------------------------------------
   // The 'owRaiseError' is the method for raising an error onto the error
   // stack.
   //
   // Arguments: int err - the error code you wish to raise.
   //
   void owRaiseError(int err)
   {
      owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;
      owErrorStack[ owErrorPointer ].owErrorNum = err;
   }
#endif


// SOCKIT_OWM_ERR_SMALL - embedded microcontrollers, where these
// messaging functions might not make any sense.
#ifndef SOCKIT_OWM_ERR_SMALL
   //Array of meaningful error messages to associate with codes.
   //Not used on targets with low memory (i.e. PIC).
   static char *owErrorMsg[125] =
   {
   /*000*/ "No Error Was Set",
   /*001*/ "No Devices found on 1-Wire Network",
   /*002*/ "1-Wire Net Reset Failed",
   /*003*/ "Search ROM Error: Couldn't locate next device on 1-Wire",
   /*004*/ "Access Failed: Could not select device",
   /*005*/ "DS2480B Adapter Not Detected",
   /*006*/ "DS2480B: Wrong Baud",
   /*007*/ "DS2480B: Bad Response",
   /*008*/ "Open COM Failed",
   /*009*/ "Write COM Failed",
   /*010*/ "Read COM Failed",
   /*011*/ "Data Block Too Large",
   /*012*/ "Block Transfer failed",
   /*013*/ "Program Pulse Failed",
   /*014*/ "Program Byte Failed",
   /*015*/ "Write Byte Failed",
   /*016*/ "Read Byte Failed",
   /*017*/ "Write Verify Failed",
   /*018*/ "Read Verify Failed",
   /*019*/ "Write Scratchpad Failed",
   /*020*/ "Copy Scratchpad Failed",
   /*021*/ "Incorrect CRC Length",
   /*022*/ "CRC Failed",
   /*023*/ "Failed to acquire a necessary system resource",
   /*024*/ "Failed to initialize system resource",
   /*025*/ "Data too long to fit on specified device.",
   /*026*/ "Read exceeds memory bank end.",
   /*027*/ "Write exceeds memory bank end.",
   /*028*/ "Device select failed",
   /*029*/ "Read Scratch Pad verify failed.",
   /*030*/ "Copy scratchpad complete not found",
   /*031*/ "Erase scratchpad complete not found",
   /*032*/ "Address read back from scrachpad was incorrect",
   /*033*/ "Read page with extra-info not supported by this memory bank",
   /*034*/ "Read page packet with extra-info not supported by this memory bank",
   /*035*/ "Length of packet requested exceeds page size",
   /*036*/ "Invalid length in packet",
   /*037*/ "Program pulse required but not available",
   /*038*/ "Trying to access a read-only memory bank",
   /*039*/ "Current bank is not general purpose memory",
   /*040*/ "Read back from write compare is incorrect, page may be locked",
   /*041*/ "Invalid page number for this memory bank",
   /*042*/ "Read page with CRC not supported by this memory bank",
   /*043*/ "Read page with CRC and extra-info not supported by this memory bank",
   /*044*/ "Read back from write incorrect, could not lock page",
   /*045*/ "Read back from write incorrect, could not lock redirect byte",
   /*046*/ "The read of the status was not completed.",
   /*047*/ "Page redirection not supported by this memory bank",
   /*048*/ "Lock Page redirection not supported by this memory bank",
   /*049*/ "Read back byte on EPROM programming did not match.",
   /*050*/ "Can not write to a page that is locked.",
   /*051*/ "Can not lock a redirected page that has already been locked.",
   /*052*/ "Trying to redirect a locked redirected page.",
   /*053*/ "Trying to lock a page that is already locked.",
   /*054*/ "Trying to write to a memory bank that is write protected.",
   /*055*/ "Error due to not matching MAC.",
   /*056*/ "Memory Bank is write protected.",
   /*057*/ "Secret is write protected, can not Load First Secret.",
   /*058*/ "Error in Reading Scratchpad after Computing Next Secret.",
   /*059*/ "Load Error from Loading First Secret.",
   /*060*/ "Power delivery required but not available",
   /*061*/ "Not a valid file name.",
   /*062*/ "Unable to Create a Directory in this part.",
   /*063*/ "That file already exists.",
   /*064*/ "The directory is not empty.",
   /*065*/ "The wrong type of part for this operation.",
   /*066*/ "The max len for this file is too small.",
   /*067*/ "This is not a write once bank.",
   /*068*/ "The file can not be found.",
   /*069*/ "There is not enough space available.",
   /*070*/ "There is not a page to match that bit in the bitmap.",
   /*071*/ "There are no jobs for EPROM parts.",
   /*072*/ "Function not supported to modify attributes.",
   /*073*/ "Handle is not in use.",
   /*074*/ "Tring to read a write only file.",
   /*075*/ "There is no handle available for use.",
   /*076*/ "The directory provided is an invalid directory.",
   /*077*/ "Handle does not exist.",
   /*078*/ "Serial Number did not match with current job.",
   /*079*/ "Can not program EPROM because a non-EPROM part on the network.",
   /*080*/ "Write protect redirection byte is set.",
   /*081*/ "There is an inappropriate directory length.",
   /*082*/ "The file has already been terminated.",
   /*083*/ "Failed to read memory page of iButton part.",
   /*084*/ "Failed to match scratchpad of iButton part.",
   /*085*/ "Failed to erase scratchpad of iButton part.",
   /*086*/ "Failed to read scratchpad of iButton part.",
   /*087*/ "Failed to execute SHA function on SHA iButton.",
   /*088*/ "SHA iButton did not return a status completion byte.",
   /*089*/ "Write data page failed.",
   /*090*/ "Copy secret into secret memory pages failed.",
   /*091*/ "Bind unique secret to iButton failed.",
   /*092*/ "Could not install secret into user token.",
   /*093*/ "Transaction Incomplete: signature did not match.",
   /*094*/ "Transaction Incomplete: could not sign service data.",
   /*095*/ "User token did not provide a valid authentication response.",
   /*096*/ "Failed to answer a challenge on the user token.",
   /*097*/ "Failed to create a challenge on the coprocessor.",
   /*098*/ "Transaction Incomplete: service data was not valid.",
   /*099*/ "Transaction Incomplete: service data was not updated.",
   /*100*/ "Unrecoverable, catastrophic service failure occured.",
   /*101*/ "Load First Secret from scratchpad data failed.",
   /*102*/ "Failed to match signature of user's service data.",
   /*103*/ "Subkey out of range for the DS1991.",
   /*104*/ "Block ID out of range for the DS1991",
   /*105*/ "Password is enabled",
   /*106*/ "Password is invalid",
   /*107*/ "This memory bank has no read only password",
   /*108*/ "This memory bank has no read/write password",
   /*109*/ "1-Wire is shorted",
   /*110*/ "Error communicating with 1-Wire adapter",
   /*111*/ "CopyScratchpad failed: Ending Offset must go to end of page",
   /*112*/ "WriteScratchpad failed: Ending Offset must go to end of page",
   /*113*/ "Mission can not be stopped while one is not in progress",
   /*114*/ "Error stopping the mission",
   /*115*/ "Port number is outside (0,MAX_PORTNUM) interval",
   /*116*/ "Level of the 1-Wire was not changed",
   /*117*/ "Both the Read Only and Read Write Passwords must be set",
   /*118*/ "Failure to change latch state."
   /*119*/ "Could not open usb port through libusb",
   /*120*/ "Libusb DS2490 port already opened",
   /*121*/ "Failed to set libusb configuration",
   /*122*/ "Failed to claim libusb interface",
   /*123*/ "Failed to set libusb altinterface",
   /*124*/ "No adapter found at this port number"
   };

   char *owGetErrorMsg(int err)
   {
      return owErrorMsg[err];
   }

   //--------------------------------------------------------------------------
   // The 'owPrintErrorMsg' is the method for printing an error from the stack.
   // The destination for the print is specified by the argument, fileno, which
   // can be stderr, stdout, or a log file. In non-debug mode, the output is
   // of the form:
   // Error num: Error msg
   //
   // In debug-mode, the output is of the form:
   // Error num: filename line#: Error msg
   //
   // NOTE: This function has the side-effect of popping the error off the stack.
   //
   // Arguments: FILE*: the destination for printing.
   //
   void owPrintErrorMsg(FILE *filenum)
   {
   #ifdef DEBUG
      int l = owErrorStack[ owErrorPointer ].lineno;
      char *f = owErrorStack[ owErrorPointer ].filename;
      int err = owGetErrorNum();
      //fprintf(filenum,"Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);
   #else
      int err = owGetErrorNum();
      //fprintf(filenum,"Error %d: %s\r\n",err,owErrorMsg[err]);
   #endif
   }

   // Same as above, except uses default printf output
   void owPrintErrorMsgStd()
   {
   #ifdef DEBUG
      int l = owErrorStack[ owErrorPointer ].lineno;
      char *f = owErrorStack[ owErrorPointer ].filename;
      int err = owGetErrorNum();
      mprintf("Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);
   #else
      int err = owGetErrorNum();
      mprintf("Error %d: %s\r\n",err,owErrorMsg[err]);
   #endif
   }
#endif