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