1 /*
2  * Copyright (C) 2022 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 "NfcStatsUtil.h"
18 
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21 #include <log/log.h>
22 #include <statslog_nfc.h>
23 
24 #include "nfc_api.h"
25 
26 using android::base::StringPrintf;
27 
28 /*******************************************************************************
29 **
30 ** Function:        logNfcTagType
31 **
32 ** Description:     determine Nfc tag type from given protocol and log
33 ** accordingly
34 **                  protocol: tag protocol
35 **                  discoveryMode: tag discovery mode
36 **
37 ** Returns:         None
38 **
39 *******************************************************************************/
logNfcTagType(int protocol,int discoveryMode)40 void NfcStatsUtil::logNfcTagType(int protocol, int discoveryMode) {
41   static const char fn[] = "NfcStatsUtil::logNfcTagType";
42   LOG(DEBUG) << StringPrintf("%s: protocol %d, mode %d", fn, protocol,
43                              discoveryMode);
44   int tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_UNKNOWN;
45   if (protocol == NFC_PROTOCOL_T1T) {
46     tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_TYPE_1;
47   } else if (protocol == NFC_PROTOCOL_T2T) {
48     tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_TYPE_2;
49   } else if (protocol == NFC_PROTOCOL_T3T) {
50     tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_TYPE_3;
51   } else if (protocol == NFC_PROTOCOL_MIFARE) {
52     tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_MIFARE_CLASSIC;
53   } else if (protocol == NFC_PROTOCOL_ISO_DEP) {
54     if ((discoveryMode == NFC_DISCOVERY_TYPE_POLL_A) ||
55         (discoveryMode == NFC_DISCOVERY_TYPE_LISTEN_A)) {
56       tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_TYPE_4A;
57     } else if ((discoveryMode == NFC_DISCOVERY_TYPE_POLL_B) ||
58                (discoveryMode == NFC_DISCOVERY_TYPE_POLL_B_PRIME) ||
59                (discoveryMode == NFC_DISCOVERY_TYPE_LISTEN_B) ||
60                (discoveryMode == NFC_DISCOVERY_TYPE_LISTEN_B_PRIME)) {
61       tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_TYPE_4B;
62     }
63   } else if (protocol == NFC_PROTOCOL_T5T) {
64     tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_TYPE_5;
65   } else if (protocol == NFC_PROTOCOL_KOVIO) {
66     tagType = nfc::stats::NFC_TAG_TYPE_OCCURRED__TYPE__TAG_KOVIO_BARCODE;
67   }
68   writeNfcStatsTagTypeOccurred(tagType);
69 }
70 
71 /*******************************************************************************
72 **
73 ** Function:        writeNfcStatsTagTypeOccurred
74 **
75 ** Description:     stats_write TagTypeOccurred atom with provided type
76 **                  tagType: NfcTagType defined in
77 ** frameworks/proto_logging/stats/enums/nfc/enums.proto
78 **
79 ** Returns:         None
80 **
81 *******************************************************************************/
writeNfcStatsTagTypeOccurred(int tagType)82 void NfcStatsUtil::writeNfcStatsTagTypeOccurred(int tagType) {
83   static const char fn[] = "NfcStatsUtil::writeNfcStatsTagTypeOccurred";
84   LOG(DEBUG) << StringPrintf("%s: %d", fn, tagType);
85 
86   nfc::stats::stats_write(nfc::stats::NFC_TAG_TYPE_OCCURRED, tagType);
87 }
88