Snapshot 7ddd630e136a035ba463c427285c5c3e9f199ee0
Change-Id: If08c7b905da667dd9e5110231e4592842b634006
diff --git a/halimpl/bcm2079x/adaptation/spdhelper.cpp b/halimpl/bcm2079x/adaptation/spdhelper.cpp
new file mode 100644
index 0000000..00e9567
--- /dev/null
+++ b/halimpl/bcm2079x/adaptation/spdhelper.cpp
@@ -0,0 +1,88 @@
+#include "OverrideLog.h"
+#include "spdhelper.h"
+#include "config.h"
+
+void SpdHelper::setPatchAsBad()
+{
+ getInstance().setPatchAsBadImpl();
+}
+
+void SpdHelper::incErrorCount()
+{
+ getInstance().incErrorCountImpl();
+}
+
+bool SpdHelper::isPatchBad(UINT8* prm, UINT32 len)
+{
+ return getInstance().isPatchBadImpl(prm, len);
+}
+
+bool SpdHelper::isSpdDebug()
+{
+ bool b = getInstance().isSpdDebugImpl();
+ ALOGD("%s SpdDebug is %s", __func__, (b ? "TRUE" : "FALSE"));
+ return b;
+}
+
+void SpdHelper::incErrorCountImpl()
+{
+ if (++mErrorCount >= mMaxErrorCount)
+ {
+ setPatchAsBadImpl();
+ }
+}
+
+void SpdHelper::setPatchAsBadImpl()
+{
+ mIsPatchBad = true;
+}
+
+inline const char * toHex(UINT8 b)
+{
+ static char hex[] = "0123456789ABCDEF";
+ static char c[3];
+ c[0] = hex[((b >> 4) & 0x0F)];
+ c[1] = hex[((b >> 0) & 0x0F)];
+ c[2] = '\0';
+ return &c[0];
+}
+
+bool SpdHelper::isPatchBadImpl(UINT8* prm, UINT32 len)
+{
+ string strNew;
+
+ // Get the patch ID from the prm data.
+ for (int i = 0; i < 8 && i < len; ++i)
+ strNew.append(toHex(*prm++));
+
+ // If it is not the same patch as before, then reset things.
+ if ( strNew != mPatchId )
+ {
+ mPatchId = strNew;
+ mErrorCount = 0;
+ mIsPatchBad = false;
+ }
+
+ // Otherwise the 'mIsPatchBad' will tell if its bad or not.
+ ALOGD("%s '%s' (%d) is %sa known bad patch file", __func__, mPatchId.c_str(), mErrorCount, (mIsPatchBad ? "" : "not "));
+
+ return mIsPatchBad;
+}
+
+SpdHelper& SpdHelper::getInstance()
+{
+ static SpdHelper* theInstance = NULL;
+ if (theInstance == NULL)
+ theInstance= new SpdHelper;
+ return *theInstance;
+}
+
+SpdHelper::SpdHelper()
+{
+ mErrorCount = 0;
+ mPatchId.erase();
+ if(!GetNumValue((char*)NAME_SPD_MAXRETRYCOUNT, &mMaxErrorCount, sizeof(mMaxErrorCount)))
+ mMaxErrorCount = DEFAULT_SPD_MAXRETRYCOUNT;
+ if (!GetNumValue((char*)NAME_SPD_DEBUG, &mSpdDebug, sizeof(mSpdDebug)))
+ mSpdDebug = false;
+}