1/******************************************************************************
2 *
3 *  Copyright (C) 1999-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/******************************************************************************
20 * Construct a buffer that contains multiple Type-Length-Value contents
21 * that is used by the HAL in a CORE_SET_CONFIG NCI command.
22 ******************************************************************************/
23
24#define LOG_TAG "NfcNciHal"
25#include "OverrideLog.h"
26#include "StartupConfig.h"
27
28
29const UINT8 StartupConfig::mMaxLength = 255;
30
31
32/*******************************************************************************
33**
34** Function:        initialize
35**
36** Description:     Initialize all member variables.
37**
38** Returns:         None
39**
40*******************************************************************************/
41StartupConfig::StartupConfig ()
42{
43    //set first byte to 0, which is length of payload
44    mBuffer.append ((uint8_string::size_type) 1, (uint8_string::value_type) 0);
45}
46
47
48/*******************************************************************************
49**
50** Function:        initialize
51**
52** Description:     Reset all member variables.
53**
54** Returns:         None
55**
56*******************************************************************************/
57void StartupConfig::initialize ()
58{
59    mBuffer.clear ();
60    //set first byte to 0, which is length of payload
61    mBuffer.append ((uint8_string::size_type) 1, (uint8_string::value_type) 0);
62}
63
64
65/*******************************************************************************
66**
67** Function:        getInternalBuffer
68**
69** Description:     Get the pointer to buffer that contains multiple
70**                  Type-Length-Value contents.
71**
72** Returns:         Pointer to buffer.
73**
74*******************************************************************************/
75const UINT8* StartupConfig::getInternalBuffer ()
76{
77    return mBuffer.data ();
78}
79
80
81/*******************************************************************************
82**
83** Function:        append
84**
85** Description:     Append new config data to internal buffer.
86**                  newContent: buffer containing new content; newContent[0] is
87**                          payload length; newContent[1..end] is payload.
88**
89** Returns:         True if ok.
90**
91*******************************************************************************/
92bool StartupConfig::append (const UINT8* newContent)
93{
94    static const char fn [] = "StartupConfig::append";
95    if ((newContent[0]+mBuffer.size()) > mMaxLength)
96    {
97        ALOGE ("%s: exceed max length", fn);
98        return false;
99    }
100    ALOGD ("%s: try append %u bytes", fn, (uint8_string::size_type) (newContent[0]));
101    //append new payload into private buffer
102    mBuffer.append (newContent+1, (uint8_string::size_type) (newContent[0]));
103    //increase size counter of payload in private buffer
104    mBuffer[0] = mBuffer[0] + newContent[0];
105    ALOGD ("%s: new size %u bytes", fn, mBuffer[0]);
106    return true;
107};
108
109
110/*******************************************************************************
111**
112** Function:        append
113**
114** Description:     Append new config data to internal buffer.
115**                  newContent: buffer containing new content; newContent[0] is
116**                          payload length; newContent[1..end] is payload.
117**                  newContentLen: total length of newContent.
118**
119** Returns:         True if ok.
120**
121*******************************************************************************/
122bool StartupConfig::append (const UINT8* newContent, UINT8 newContentLen)
123{
124    static const char fn [] = "StartupConfig::append";
125    if ((newContent[0]+1) != newContentLen)
126    {
127        ALOGE ("%s: invalid length at newContent[0]", fn);
128        return false;
129    }
130    return append (newContent);
131};
132
133
134/*******************************************************************************
135**
136** Function:        disableSecureElement
137**
138** Description:     Adjust a TLV to disable secure element(s).  The TLV's type is 0xC2.
139**                  bitmask: 0xC0 = do not detect any secure element.
140**                           0x40 = do not detect secure element in slot 0.
141**                           0x80 = do not detect secure element in slot 1.
142**
143** Returns:         True if ok.
144**
145*******************************************************************************/
146bool StartupConfig::disableSecureElement (UINT8 bitmask)
147{
148    const UINT8 maxLen = mBuffer[0];
149    UINT8 index = 1, tlvType = 0, tlvLen = 0;
150    bool found0xC2 = false;
151
152    while (true)
153    {
154        if (index > maxLen)
155            break;
156        tlvType = mBuffer [index];
157        index++;
158        tlvLen = mBuffer [index];
159        index++;
160        if (tlvType == 0xC2) //this TLV controls secure elements
161        {
162            index++; //index of second byte in TLV's value
163            mBuffer [index] = mBuffer [index] | bitmask; //turn on certain bits
164            found0xC2 = true;
165        }
166        else
167            index += tlvLen;
168    }
169
170    if (found0xC2 == false)
171    {
172        UINT8 tlv [] = {0x04, 0xC2, 0x02, 0x61, 0x00};
173        tlv [4] = tlv [4] | bitmask;
174        found0xC2 = append (tlv);
175    }
176    return found0xC2;
177}
178