1 /******************************************************************************
2  *
3  *  Copyright 2018 NXP
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 #pragma once
19 
20 #include <time.h>
21 #include <string>
22 #include <vector>
23 
24 /*Time spent in Active mode per count provided by NFCC*/
25 static const uint32_t ACTIVE_TIME_PER_TIMER_COUNT_IN_MILLISEC = 20;
26 /*Types of  Power states supported by NFCC */
27 typedef struct NfccPowerStateInfo {
28   /* state name: Active/Standby */
29   std::string name;
30   /* Time spent in msec at this  power state since boot */
31   uint64_t residencyInMsecSinceBoot;
32   /* Total number of times Nfcc entered this state */
33   uint64_t totalTransitions;
34 } NfccPowerStateInfo_t;
35 
36 /*Class to track the time spent in Standby mode by NFCC*/
37 class NfccPowerTracker {
38  public:
39   static NfccPowerTracker& getInstance();
40 
41   /*******************************************************************************
42   **
43   ** Function         Initialize
44   **
45   ** Description      get all prerequisite information from NFCC needed for
46   **                  Power tracker calculations.
47   **
48   ** Returns          void
49   **
50   *******************************************************************************/
51   void Initialize();
52 
53   /*******************************************************************************
54   **
55   ** Function         ProcessCmd
56   **
57   ** Description      Parse the commands going to NFCC,
58   **                  get the time at which power relevant commands are sent
59   **                  (ex:Screen state/OMAPI session)is sent and
60   **                  log/cache the timestamp to file.
61   **
62   ** Returns          void
63   **
64   *******************************************************************************/
65   void ProcessCmd(uint8_t*, uint16_t len);
66 
67   /*******************************************************************************
68   **
69   ** Function         ProcessNtf
70   **
71   ** Description      Parse the Notifications coming from NFCC,
72   **                  get the time at which power relevant notifications are
73   **                  received (ex:RF ON-OFF/ACTIVATE-DEACTIVATE NTF/
74   **                  PROP_PWR_TRACKINFO). Calculate error in standby time by
75   **                  comparing the expectated value from NFC HAL and received
76   **                  value from NFCC. Update power state duration info
77   **                  to file.
78   **
79   ** Returns          void
80   **
81   *******************************************************************************/
82   void ProcessNtf(uint8_t* cmd, uint16_t len);
83 
84   /*******************************************************************************
85   **
86   ** Function         Pause
87   **
88   ** Description      Pause Power state Information Tracking,Tracking will
89   **                  resume once next power tracker notification is recieved as
90   **                  part of ProcessNtf.
91   **
92   ** Returns          void
93   **
94   *******************************************************************************/
95   void Pause();
96 
97   /*******************************************************************************
98   **
99   ** Function         Reset
100   **
101   ** Description      Stop power tracker information processing and delete
102   **                  power track log file.
103   **
104   ** Returns          void
105   **
106   *******************************************************************************/
107   void Reset();
108 
109  private:
110   NfccPowerTracker();
111   ~NfccPowerTracker();
112 
113   /*******************************************************************************
114   **
115   ** Function         UpdatePowerStateLog
116   **
117   ** Description      update the powerstate related information in log file
118   **
119   ** Returns          void
120   **
121   *******************************************************************************/
122   void UpdatePowerStateLog(NfccPowerStateInfo_t standbyTime,
123                            NfccPowerStateInfo_t activeTime);
124 
125   /*******************************************************************************
126    **
127    ** Function         ReadPowerStateLog
128    **
129    ** Description      Retrieve powerstate related information from log file.
130    **
131    ** Returns          true if read successful, false otherwise.
132    **
133    *******************************************************************************/
134   bool ReadPowerStateLog();
135 
136   /*******************************************************************************
137    **
138    ** Function         ProcessPowerTrackNtf
139    **
140    ** Description      Process Power Tracker notification.
141    **
142    ** Returns          void
143    **
144    *******************************************************************************/
145   void ProcessPowerTrackNtf(uint8_t* rsp, uint16_t rsp_len);
146 
147   /*******************************************************************************
148   **
149   ** Function         TimeDiff
150   **
151   ** Description      Computes time difference in milliseconds.
152   **
153   ** Returns          Time difference in milliseconds
154   **
155   *******************************************************************************/
156   uint64_t TimeDiff(timespec start, timespec end);
157   /*******************************************************************************
158   **
159   ** Function         TryLockFile
160   **
161   ** Description      Lock PowerTracker log file. Any application trying to read
162   **                  from PowerTracker log file shall acquire lock before
163   **                  reading to avoid inconsistent data.
164   **
165   ** Returns          true if locking was successful
166   **                  false if there was a failure to lock file.
167   *******************************************************************************/
168   bool TryLockFile(FILE* fp);
169   /*******************************************************************************
170   **
171   ** Function         UnlockFile
172   **
173   ** Description      Unlock previously locked PowerTracker log file.
174   **
175   ** Returns          void
176   *******************************************************************************/
177   void UnlockFile(FILE* fp);
178   struct timespec mLastScreenOffTimeStamp = {0, 0},
179                   mLastScreenOnTimeStamp = {0, 0};
180   /*Used to calculate time NFCC is active during Card emulation/P2P/Reader
181    * modes*/
182   struct timespec mActiveTimeStart = {0, 0}, mActiveTimeEnd = {0, 0};
183 
184   bool mIsLastUpdateScreenOn;
185   bool mIsFirstPwrTrkNtfRecvd;
186 
187   uint64_t mActiveDurationFromLastScreenUpdate = 0;
188   NfccPowerStateInfo_t mActiveInfo, mStandbyInfo, mErrorInStandbyInfo;
189 
190   /*Last powertracker processing aborted due to NFC HAL Service abort*/
191   bool mLastPowerTrackAborted = false;
192   /* Time spent in standby mode in one discovery loop containing poll */
193   uint32_t mStandbyTimePerDiscLoopInMillisec;
194   const std::string STR_ACTIVE = "Active: ", STR_STANDBY = "StandBy: ";
195 };
196