| The Android Open Source Project | e9df6ba | 2012-12-13 14:55:37 -0800 | [diff] [blame^] | 1 | #include "OverrideLog.h" | 
|  | 2 | #include "spdhelper.h" | 
|  | 3 | #include "config.h" | 
|  | 4 |  | 
|  | 5 | void SpdHelper::setPatchAsBad() | 
|  | 6 | { | 
|  | 7 | getInstance().setPatchAsBadImpl(); | 
|  | 8 | } | 
|  | 9 |  | 
|  | 10 | void SpdHelper::incErrorCount() | 
|  | 11 | { | 
|  | 12 | getInstance().incErrorCountImpl(); | 
|  | 13 | } | 
|  | 14 |  | 
|  | 15 | bool SpdHelper::isPatchBad(UINT8* prm, UINT32 len) | 
|  | 16 | { | 
|  | 17 | return getInstance().isPatchBadImpl(prm, len); | 
|  | 18 | } | 
|  | 19 |  | 
|  | 20 | bool SpdHelper::isSpdDebug() | 
|  | 21 | { | 
|  | 22 | bool b = getInstance().isSpdDebugImpl(); | 
|  | 23 | ALOGD("%s SpdDebug is %s", __func__, (b ? "TRUE" : "FALSE")); | 
|  | 24 | return b; | 
|  | 25 | } | 
|  | 26 |  | 
|  | 27 | void SpdHelper::incErrorCountImpl() | 
|  | 28 | { | 
|  | 29 | if (++mErrorCount >= mMaxErrorCount) | 
|  | 30 | { | 
|  | 31 | setPatchAsBadImpl(); | 
|  | 32 | } | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | void SpdHelper::setPatchAsBadImpl() | 
|  | 36 | { | 
|  | 37 | mIsPatchBad = true; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | inline const char * toHex(UINT8 b) | 
|  | 41 | { | 
|  | 42 | static char hex[] = "0123456789ABCDEF"; | 
|  | 43 | static char c[3]; | 
|  | 44 | c[0] = hex[((b >> 4) & 0x0F)]; | 
|  | 45 | c[1] = hex[((b >> 0) & 0x0F)]; | 
|  | 46 | c[2] = '\0'; | 
|  | 47 | return &c[0]; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | bool SpdHelper::isPatchBadImpl(UINT8* prm, UINT32 len) | 
|  | 51 | { | 
|  | 52 | string strNew; | 
|  | 53 |  | 
|  | 54 | // Get the patch ID from the prm data. | 
|  | 55 | for (int i = 0; i < 8 && i < len; ++i) | 
|  | 56 | strNew.append(toHex(*prm++)); | 
|  | 57 |  | 
|  | 58 | // If it is not the same patch as before, then reset things. | 
|  | 59 | if ( strNew != mPatchId ) | 
|  | 60 | { | 
|  | 61 | mPatchId = strNew; | 
|  | 62 | mErrorCount = 0; | 
|  | 63 | mIsPatchBad = false; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | // Otherwise the 'mIsPatchBad' will tell if its bad or not. | 
|  | 67 | ALOGD("%s '%s' (%d) is %sa known bad patch file", __func__, mPatchId.c_str(), mErrorCount, (mIsPatchBad ? "" : "not ")); | 
|  | 68 |  | 
|  | 69 | return mIsPatchBad; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | SpdHelper& SpdHelper::getInstance() | 
|  | 73 | { | 
|  | 74 | static SpdHelper* theInstance = NULL; | 
|  | 75 | if (theInstance == NULL) | 
|  | 76 | theInstance= new SpdHelper; | 
|  | 77 | return *theInstance; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | SpdHelper::SpdHelper() | 
|  | 81 | { | 
|  | 82 | mErrorCount = 0; | 
|  | 83 | mPatchId.erase(); | 
|  | 84 | if(!GetNumValue((char*)NAME_SPD_MAXRETRYCOUNT, &mMaxErrorCount, sizeof(mMaxErrorCount))) | 
|  | 85 | mMaxErrorCount = DEFAULT_SPD_MAXRETRYCOUNT; | 
|  | 86 | if (!GetNumValue((char*)NAME_SPD_DEBUG, &mSpdDebug, sizeof(mSpdDebug))) | 
|  | 87 | mSpdDebug = false; | 
|  | 88 | } |