summaryrefslogtreecommitdiffstats
path: root/halimpl/bcm2079x/adaptation/spdhelper.cpp
blob: b93675369e57f13a1a83f11b55b7ab2823ad51bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/******************************************************************************
 *
 *  Copyright (C) 2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "NfcNciHal"
#include "spdhelper.h"
#include "_OverrideLog.h"
#include "config.h"

void SpdHelper::setPatchAsBad() { getInstance().setPatchAsBadImpl(); }

void SpdHelper::incErrorCount() { getInstance().incErrorCountImpl(); }

bool SpdHelper::isPatchBad(uint8_t* prm, uint32_t 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_t 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_t* prm, uint32_t len) {
  string strNew;

  // Get the patch ID from the prm data.
  for (uint32_t 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;
  mIsPatchBad = false;
  if (!GetNumValue((char*)NAME_SPD_DEBUG, &mSpdDebug, sizeof(mSpdDebug)))
    mSpdDebug = false;
}