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