1 /******************************************************************************
2  *
3  *  Copyright (C) 2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "NfcNciHal"
20 #include "OverrideLog.h"
21 #include "spdhelper.h"
22 #include "config.h"
23 
setPatchAsBad()24 void SpdHelper::setPatchAsBad()
25 {
26     getInstance().setPatchAsBadImpl();
27 }
28 
incErrorCount()29 void SpdHelper::incErrorCount()
30 {
31     getInstance().incErrorCountImpl();
32 }
33 
isPatchBad(UINT8 * prm,UINT32 len)34 bool SpdHelper::isPatchBad(UINT8* prm, UINT32 len)
35 {
36     return getInstance().isPatchBadImpl(prm, len);
37 }
38 
isSpdDebug()39 bool SpdHelper::isSpdDebug()
40 {
41     bool b = getInstance().isSpdDebugImpl();
42     ALOGD("%s SpdDebug is %s", __func__, (b ? "TRUE" : "FALSE"));
43     return b;
44 }
45 
incErrorCountImpl()46 void SpdHelper::incErrorCountImpl()
47 {
48     if (++mErrorCount >= mMaxErrorCount)
49     {
50         setPatchAsBadImpl();
51     }
52 }
53 
setPatchAsBadImpl()54 void SpdHelper::setPatchAsBadImpl()
55 {
56     mIsPatchBad = true;
57 }
58 
toHex(UINT8 b)59 inline const char * toHex(UINT8 b)
60 {
61     static char hex[] = "0123456789ABCDEF";
62     static char c[3];
63     c[0] = hex[((b >> 4) & 0x0F)];
64     c[1] = hex[((b >> 0) & 0x0F)];
65     c[2] = '\0';
66     return &c[0];
67 }
68 
isPatchBadImpl(UINT8 * prm,UINT32 len)69 bool SpdHelper::isPatchBadImpl(UINT8* prm, UINT32 len)
70 {
71     string strNew;
72 
73     // Get the patch ID from the prm data.
74     for (int i = 0; i < 8 && i < len; ++i)
75         strNew.append(toHex(*prm++));
76 
77     // If it is not the same patch as before, then reset things.
78     if ( strNew != mPatchId )
79     {
80         mPatchId = strNew;
81         mErrorCount = 0;
82         mIsPatchBad = false;
83     }
84 
85     // Otherwise the 'mIsPatchBad' will tell if its bad or not.
86     ALOGD("%s '%s' (%d) is %sa known bad patch file", __func__, mPatchId.c_str(), mErrorCount, (mIsPatchBad ? "" : "not "));
87 
88     return mIsPatchBad;
89 }
90 
getInstance()91 SpdHelper& SpdHelper::getInstance()
92 {
93     static SpdHelper* theInstance = NULL;
94     if (theInstance == NULL)
95         theInstance= new SpdHelper;
96     return *theInstance;
97 }
98 
SpdHelper()99 SpdHelper::SpdHelper()
100 {
101     mErrorCount = 0;
102     mPatchId.erase();
103     if(!GetNumValue((char*)NAME_SPD_MAXRETRYCOUNT, &mMaxErrorCount, sizeof(mMaxErrorCount)))
104         mMaxErrorCount = DEFAULT_SPD_MAXRETRYCOUNT;
105     mIsPatchBad = false;
106     if (!GetNumValue((char*)NAME_SPD_DEBUG, &mSpdDebug, sizeof(mSpdDebug)))
107         mSpdDebug = false;
108 }
109