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