1 /*
2  * Copyright 2024 NXP
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 <vector>
18 
19 using namespace std;
20 
21 typedef void(reader_poll_info_callback_t)(uint16_t data_len, uint8_t* p_data);
22 
23 /**
24  * @brief This class handles the parsing of Lx notifications and
25  * send reader poll info notification's. It identifis A, B and F
26  * Modulation event's and RF ON & OFF event's, all the other
27  * notifications it considers it as Unknown event's
28  *
29  */
30 class ReaderPollConfigParser {
31  private:
32   reader_poll_info_callback_t* callback = nullptr;
33   uint8_t lastKnownGain = 0x00;
34   uint8_t lastKnownModEvent = 0x00;
35 
36   /*****************************************************************************
37    *
38    * Function         getWellKnownModEventData
39    *
40    * Description      Frames Well known type reader poll info notification
41    *
42    * Parameters       event - Event type A, B & F
43    *                  timeStamp - time stamp of the event
44    *                  gain - RSSI value
45    *                  data - data contains REQ, WUP and AFI
46    *
47    * Returns          Returns Well known type reader poll info notification
48    *
49    ****************************************************************************/
50   vector<uint8_t> getWellKnownModEventData(uint8_t event,
51                                            vector<uint8_t> timeStamp,
52                                            uint8_t gain, vector<uint8_t> data);
53 
54   /*****************************************************************************
55    *
56    * Function         getRFEventData
57    *
58    * Description      Frames Well known type reader poll info notification
59    *
60    * Parameters       timeStamp - time stamp of the event
61    *                  gain - RSSI value
62    *                  rfState - 0x00 for RF OFF, 0x01 for RF ON
63    *
64    * Returns          Returns RF State reader poll info notification
65    *
66    ****************************************************************************/
67   vector<uint8_t> getRFEventData(vector<uint8_t> timeStamp, uint8_t gain,
68                                  bool rfState);
69 
70   /*****************************************************************************
71    *
72    * Function         getUnknownEvent
73    *
74    * Description      Frames Unknown event type reader poll info notification
75    *
76    * Parameters       data - Data bytes of Unknown event
77    *                  timeStamp - time stamp of the event
78    *                  gain - RSSI value
79    *
80    * Returns          Returns Unknown type reader poll info notification
81    *
82    ***************************************************************************/
83   vector<uint8_t> getUnknownEvent(vector<uint8_t> data,
84                                   vector<uint8_t> timeStamp, uint8_t gain);
85 
86   /*****************************************************************************
87    *
88    * Function         parseCmaEvent
89    *
90    * Description      This function parses the unknown frames
91    *
92    * Parameters       p_event - Data bytes of type Unknown event
93    *
94    * Returns          Filters Type-B/Type-F data frames
95    *                  and converts other frame to  unknown frame
96    *
97    ***************************************************************************/
98   vector<uint8_t> parseCmaEvent(vector<uint8_t> p_event);
99 
100   /*****************************************************************************
101    *
102    * Function         getEvent
103    *
104    * Description      It identifies the type of event and gets the reader poll
105    *                  info
106    *                  notification
107    *
108    * Parameters       p_event - Vector Lx Notification
109    *                  cmaEventType - CMA event type
110    *
111    * Returns          This function return reader poll info notification
112    *
113    ****************************************************************************/
114   vector<uint8_t> getEvent(vector<uint8_t> p_event, uint8_t cmaEventType);
115 
116   /*****************************************************************************
117    *
118    * Function         notifyPollingLoopInfoEvent
119    *
120    * Description      It sends polling info notification to upper layer
121    *
122    * Parameters       p_data - Polling loop info notification
123    *
124    * Returns          void
125    *
126    ****************************************************************************/
127   void notifyPollingLoopInfoEvent(vector<uint8_t> p_data);
128 
129 #if (NXP_UNIT_TEST == TRUE)
130   /*
131     Friend class is used to test private function's of ReaderPollConfigParser
132   */
133   friend class ReaderPollConfigParserTest;
134 #endif
135 
136  public:
137   bool readExtraBytesForUnknownEvent = false;
138   uint8_t extraByteLength = 0;
139   uint8_t notificationType = 0;
140   vector<uint8_t> unknownEventTimeStamp;
141   vector<uint8_t> extraBytes = vector<uint8_t>();
142   /*****************************************************************************
143    *
144    * Function         parseAndSendReaderPollInfo
145    *
146    * Description      Function to parse Lx Notification & Send Reader Poll info
147    *                  notification
148    *
149    * Parameters       p_ntf - Lx Notification
150    *                  p_len - Notification length
151    *
152    * Returns          This function return true in case of success
153    *                  In case of failure returns false.
154    *
155    ****************************************************************************/
156   bool parseAndSendReaderPollInfo(uint8_t* p_ntf, uint16_t p_len);
157 
158   /*****************************************************************************
159    *
160    * Function         parseAndSendReaderPollInfo
161    *
162    * Description      Function to check it is Lx Notification or not
163    *
164    * Parameters       p_ntf - Lx Notification
165    *                  p_len - Notification length
166    *
167    * Returns          This function return true if it is Lx otherwise false
168    *
169    ****************************************************************************/
170   bool isLxNotification(uint8_t* p_ntf, uint16_t p_len);
171 
172   /*****************************************************************************
173    *
174    * Function         setReaderPollCallBack
175    *
176    * Description      Function to set the callback, It will be used to notify
177    *                  each reader polling data notifications
178    *
179    * Parameters       callback - nfc data callback object
180    *
181    * Returns          void
182    *
183    ****************************************************************************/
184   void setReaderPollCallBack(reader_poll_info_callback_t* callback);
185 
186   /*****************************************************************************
187    *
188    * Function         resetExtraBytesInfo
189    *
190    * Description      Function to reset the extra bytes info of UnknownEvent
191    *
192    * Parameters       None
193    *
194    * Returns          void
195    *
196    ****************************************************************************/
197   void resetExtraBytesInfo();
198 
199   /*****************************************************************************
200    *
201    * Function         setNotificationType
202    *
203    * Description      Function to select the Notification type for Observe mode
204    *                  By default all type of notification enabled if not set
205    *
206    * Parameters       None
207    *
208    * Returns          void
209    *
210    ****************************************************************************/
211   void setNotificationType(uint8_t notificationType);
212 
213   /*****************************************************************************
214    *
215    * Function         getTimestampInMicroSeconds
216    *
217    * Description      Function to convert Timestamp in microseconds and gives it
218    *                  in Big endian format
219    *
220    * Parameters       rawFrame
221    *
222    * Returns          vector<uint8_t>
223    *
224    ****************************************************************************/
225   vector<uint8_t> getTimestampInMicroSeconds(vector<uint8_t> rawFrame);
226 };
227