xref: /aosp_15_r20/system/nfc/src/adaptation/debug_lmrt.cc (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
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 #include "include/debug_lmrt.h"
17 
18 #include <android-base/logging.h>
19 #include <android-base/stringprintf.h>
20 
21 using android::base::StringPrintf;
22 
23 /* The payload of each RF_SET_LISTEN_MODE_ROUTING_CMD when commit routing */
24 lmrt_payload_t lmrt_payloads;
25 
26 /* The committed routing table stored in tlv form  */
27 std::vector<uint8_t> committed_lmrt_tlvs(0);
28 
29 /*******************************************************************************
30 **
31 ** Function         debug_lmrt_init
32 **
33 ** Description      initialize the lmrt_payloads
34 **
35 ** Returns          None
36 **
37 *******************************************************************************/
debug_lmrt_init(void)38 void debug_lmrt_init(void) {
39   std::vector<uint8_t> empty_more(0);
40   std::vector<uint8_t> empty_entry_count(0);
41   std::vector<std::vector<uint8_t>> empty_tlvs(0);
42 
43   lmrt_payloads.more.swap(empty_more);
44   lmrt_payloads.entry_count.swap(empty_entry_count);
45   lmrt_payloads.tlvs.swap(empty_tlvs);
46 }
47 
48 /*******************************************************************************
49 **
50 ** Function         lmrt_log
51 **
52 ** Description      print the listen mode routing configuration for debug use
53 **
54 ** Returns          None
55 **
56 *******************************************************************************/
lmrt_log(void)57 void lmrt_log(void) {
58   if (!WOULD_LOG(VERBOSE)) return;
59 
60   static const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
61                                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
62 
63   for (int i = 0; i < lmrt_payloads.more.size(); i++) {
64     std::string tlvs_str;
65     for (uint8_t byte : lmrt_payloads.tlvs[i]) {
66       tlvs_str.push_back(hexmap[byte >> 4]);
67       tlvs_str.push_back(hexmap[byte & 0x0F]);
68     }
69 
70     LOG(VERBOSE) << StringPrintf("lmrt_log: Packet %d/%d, %d more packet", i + 1,
71                                (int)lmrt_payloads.more.size(),
72                                lmrt_payloads.more[i]);
73     LOG(VERBOSE) << StringPrintf("lmrt_log: %d entries in this packet",
74                                lmrt_payloads.entry_count[i]);
75 
76     LOG(VERBOSE) << StringPrintf("lmrt_log: tlv: %s", tlvs_str.c_str());
77   }
78 }
79 
80 /*******************************************************************************
81 **
82 ** Function         lmrt_capture
83 **
84 ** Description      record the last RF_SET_LISTEN_MODE_ROUTING_CMD
85 **
86 ** Returns          None
87 **
88 *******************************************************************************/
lmrt_capture(uint8_t * buf,uint8_t buf_size)89 void lmrt_capture(uint8_t* buf, uint8_t buf_size) {
90   if (buf == nullptr || buf_size < 5) return;
91 
92   if (lmrt_payloads.more.size() > 0) {
93     if (lmrt_payloads.more.back() == 0) {
94       /* if the MORE setting of the last lmrt command is 0x00,
95        * that means the data in lmrt_payloads are obsolete, empty it */
96       debug_lmrt_init();
97     }
98   }
99 
100   /* push_back the last lmrt command to lmrt_payloads */
101   lmrt_payloads.more.push_back(buf[3]);
102   lmrt_payloads.entry_count.push_back(buf[4]);
103   if (buf_size == 5) {
104     lmrt_payloads.tlvs.push_back(std::vector<uint8_t>(0));
105   } else {
106     lmrt_payloads.tlvs.push_back(std::vector<uint8_t>(buf + 5, buf + buf_size));
107   }
108 }
109 
110 /*******************************************************************************
111 **
112 ** Function         lmrt_update
113 **
114 ** Description      Update the committed tlvs to committed_lmrt_tlvs
115 **
116 ** Returns          None
117 **
118 *******************************************************************************/
lmrt_update(void)119 void lmrt_update(void) {
120   lmrt_log();
121   std::vector<uint8_t> temp(0);
122 
123   /* combine all tlvs in lmrt_payloads.tlvs */
124   for (auto tlv : lmrt_payloads.tlvs) {
125     temp.insert(temp.end(), tlv.begin(), tlv.end());
126   }
127 
128   committed_lmrt_tlvs.swap(temp);
129 }
130 
131 /*******************************************************************************
132 **
133 ** Function         lmrt_get_max_size
134 **
135 ** Description      This function is used to get the max size of the routing
136 **                  table from cache
137 **
138 ** Returns          Max Routing Table Size
139 **
140 *******************************************************************************/
lmrt_get_max_size(void)141 int lmrt_get_max_size(void) { return nfc_cb.max_ce_table; }
142 
143 /*******************************************************************************
144 **
145 ** Function         lmrt_get_tlvs
146 **
147 ** Description      This function is used to get the committed listen mode
148 **                  routing configuration command
149 **
150 ** Returns          The committed listen mode routing configuration command
151 **
152 *******************************************************************************/
lmrt_get_tlvs()153 std::vector<uint8_t>* lmrt_get_tlvs() { return &committed_lmrt_tlvs; }
154