1 /**
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "include/debug_lmrt.h"
18
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21
22 using android::base::StringPrintf;
23
24 /* The payload of each RF_SET_LISTEN_MODE_ROUTING_CMD when commit routing */
25 lmrt_payload_t lmrt_payloads;
26
27 /* The committed routing table stored in tlv form */
28 std::vector<uint8_t> committed_lmrt_tlvs(0);
29
30 /*******************************************************************************
31 **
32 ** Function debug_lmrt_init
33 **
34 ** Description initialize the lmrt_payloads
35 **
36 ** Returns None
37 **
38 *******************************************************************************/
debug_lmrt_init(void)39 void debug_lmrt_init(void) {
40 std::vector<uint8_t> empty_more(0);
41 std::vector<uint8_t> empty_entry_count(0);
42 std::vector<std::vector<uint8_t>> empty_tlvs(0);
43
44 lmrt_payloads.more.swap(empty_more);
45 lmrt_payloads.entry_count.swap(empty_entry_count);
46 lmrt_payloads.tlvs.swap(empty_tlvs);
47 }
48
49 /*******************************************************************************
50 **
51 ** Function lmrt_log
52 **
53 ** Description print the listen mode routing configuration for debug use
54 **
55 ** Returns None
56 **
57 *******************************************************************************/
lmrt_log(void)58 void lmrt_log(void) {
59 if (!WOULD_LOG(VERBOSE)) return;
60
61 static const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
62 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
63
64 for (int i = 0; i < lmrt_payloads.more.size(); i++) {
65 std::string tlvs_str;
66 for (uint8_t byte : lmrt_payloads.tlvs[i]) {
67 tlvs_str.push_back(hexmap[byte >> 4]);
68 tlvs_str.push_back(hexmap[byte & 0x0F]);
69 }
70
71 LOG(VERBOSE) << StringPrintf("lmrt_log: Packet %d/%d, %d more packet", i + 1,
72 (int)lmrt_payloads.more.size(),
73 lmrt_payloads.more[i]);
74 LOG(VERBOSE) << StringPrintf("lmrt_log: %d entries in this packet",
75 lmrt_payloads.entry_count[i]);
76
77 LOG(VERBOSE) << StringPrintf("lmrt_log: tlv: %s", tlvs_str.c_str());
78 }
79 }
80
81 /*******************************************************************************
82 **
83 ** Function lmrt_capture
84 **
85 ** Description record the last RF_SET_LISTEN_MODE_ROUTING_CMD
86 **
87 ** Returns None
88 **
89 *******************************************************************************/
lmrt_capture(uint8_t * buf,uint8_t buf_size)90 void lmrt_capture(uint8_t* buf, uint8_t buf_size) {
91 if (buf == nullptr || buf_size < 5) return;
92
93 if (lmrt_payloads.more.size() > 0) {
94 if (lmrt_payloads.more.back() == 0) {
95 /* if the MORE setting of the last lmrt command is 0x00,
96 * that means the data in lmrt_payloads are obsolete, empty it */
97 debug_lmrt_init();
98 }
99 }
100
101 /* push_back the last lmrt command to lmrt_payloads */
102 lmrt_payloads.more.push_back(buf[3]);
103 lmrt_payloads.entry_count.push_back(buf[4]);
104 if (buf_size == 5) {
105 lmrt_payloads.tlvs.push_back(std::vector<uint8_t>(0));
106 } else {
107 lmrt_payloads.tlvs.push_back(std::vector<uint8_t>(buf + 5, buf + buf_size));
108 }
109 }
110
111 /*******************************************************************************
112 **
113 ** Function lmrt_update
114 **
115 ** Description Update the committed tlvs to committed_lmrt_tlvs
116 **
117 ** Returns None
118 **
119 *******************************************************************************/
lmrt_update(void)120 void lmrt_update(void) {
121 lmrt_log();
122 std::vector<uint8_t> temp(0);
123
124 /* combine all tlvs in lmrt_payloads.tlvs */
125 for (auto tlv : lmrt_payloads.tlvs) {
126 temp.insert(temp.end(), tlv.begin(), tlv.end());
127 }
128
129 committed_lmrt_tlvs.swap(temp);
130 }
131
132 /*******************************************************************************
133 **
134 ** Function lmrt_get_max_size
135 **
136 ** Description This function is used to get the max size of the routing
137 ** table from cache
138 **
139 ** Returns Max Routing Table Size
140 **
141 *******************************************************************************/
lmrt_get_max_size(void)142 int lmrt_get_max_size(void) { return nfc_cb.max_ce_table; }
143
144 /*******************************************************************************
145 **
146 ** Function lmrt_get_tlvs
147 **
148 ** Description This function is used to get the committed listen mode
149 ** routing configuration command
150 **
151 ** Returns The committed listen mode routing configuration command
152 **
153 *******************************************************************************/
lmrt_get_tlvs()154 std::vector<uint8_t>* lmrt_get_tlvs() { return &committed_lmrt_tlvs; }
155