1  /******************************************************************************
2   *
3   *  Copyright 2020, 2022-2023 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  
19  #ifndef _WEAVER_PARSER_IMPL_H_
20  #define _WEAVER_PARSER_IMPL_H_
21  
22  #include <mutex>
23  #include <weaver_parser.h>
24  
25  
26  class WeaverParserImpl : public WeaverParser {
27  public:
28    static const uint8_t  sThrottleGetDataP1 = 0xDF;
29    /**
30     * \brief Function to Frame weaver applet request command for getSlots
31     *
32     * \param[out]    request - framed getslots command as vector
33     *
34     * \retval This function return true in case of success
35     *         In case of failure returns false.
36     */
37    bool FrameGetSlotCmd(std::vector<uint8_t> &request);
38  
39    /**
40     * \brief Function to Frame weaver applet request command for open
41     *
42     * \param[out]    request - framed open command as vector
43     *
44     * \retval This function return true in case of success
45     *         In case of failure returns false.
46     */
47    bool FrameOpenCmd(std::vector<uint8_t> &request);
48  
49    /**
50     * \brief Function to Frame weaver applet request command for read
51     *
52     * \param[in]     slotId  - input slotId to be used in read request.
53     * \param[in]     key     - input key to be used in read request.
54     * \param[out]    request - framed read command as vector
55     *
56     * \retval This function return true in case of success
57     *         In case of failure returns false.
58     */
59    bool FrameReadCmd(uint32_t slotId, const std::vector<uint8_t> &key,
60                      std::vector<uint8_t> &request);
61  
62    /**
63     * \brief Function to Frame weaver applet request command for write
64     *
65     * \param[in]     slotId  - input slotId to be used in write request.
66     * \param[in]     key     - input key to be used in write request.
67     * \param[in]     value   - input value to be used in write request.
68     * \param[out]    request - framed write command as vector
69     *
70     * \retval This function return true in case of success
71     *         In case of failure returns false.
72     */
73    bool FrameWriteCmd(uint32_t slotId, const std::vector<uint8_t> &key,
74                       const std::vector<uint8_t> &value,
75                       std::vector<uint8_t> &request);
76  
77    /**
78     * \brief Function to Frame weaver applet request command for get data
79     *
80     * \param[in]     p1      - p1 value for get Data command.
81     * \param[in]     p2      - p2 value for get Data command.
82     * \param[out]    request - framed get data command as vector
83     *
84     * \retval This function return true in case of success
85     *         In case of failure returns false.
86     */
87    bool FrameGetDataCmd(uint8_t p1, uint8_t p2,
88                       std::vector<uint8_t> &request);
89  
90    /**
91     * \brief Function to Parse getSlots response
92     *
93     * \param[in]     response  - response from applet.
94     * \param[out]    slotInfo  - parsed slots Information read out from applet
95     * response.
96     *
97     * \retval This function return true in case of success
98     *         In case of failure returns false.
99     */
100    Status_Weaver ParseSlotInfo(std::vector<uint8_t> response,
101                                SlotInfo &slotInfo);
102  
103    /**
104     * \brief Function to Parse read response
105     *
106     * \param[in]     response  - response from applet.
107     * \param[out]    readInfo  - parsed read Information read out from applet
108     * response.
109     *
110     * \retval This function return true in case of success
111     *         In case of failure returns false.
112     */
113    Status_Weaver ParseReadInfo(std::vector<uint8_t> response,
114                                ReadRespInfo &readInfo);
115  
116    /**
117     * \brief Function to Parse get data response
118     *
119     * \param[in]     response  - response from applet.
120     * \param[out]    readInfo  - parsed Get data Information read out from applet
121     * response.
122     *
123     * \retval This function return true in case of success
124     *         In case of failure returns false.
125     */
126    Status_Weaver ParseGetDataInfo(std::vector<uint8_t> response,
127                                  GetDataRespInfo &getDataInfo);
128  
129    /**
130     * \brief Function to check if response from applet is Success or not
131     *
132     * \param[in]     response  - response from applet.
133     *
134     * \retval This function return true if response code from applet is success
135     *         and false in other cases.
136     */
137    bool isSuccess(std::vector<uint8_t> response);
138  
139    /**
140     * \brief Function to get Weaver Applet ID
141     *
142     * \param[out]    aid  - applet ids of the weaver applet.
143     *
144     * \retval This function return true in case of success
145     *         In case of failure returns false.
146     */
147    bool getAppletId(std::vector<std::vector<uint8_t>> &aid);
148  
149    /**
150     * \brief static function to get the singleton instance of WeaverParserImpl
151     * class
152     *
153     * \retval instance of WeaverParserImpl.
154     */
155    static WeaverParserImpl *getInstance();
156  
157  private:
158    /* Internal error codes for Parser Implementation */
159    enum APP_ERR_CODE {
160      APP_SUCCESS,
161      APP_FAILED,
162      APP_INVALID_LEN,
163      APP_INVALID_SLOT,
164      APP_INVALID_P1_P2,
165      APP_UNKNOWN_ERR,
166    };
167  
168    /**
169     * \brief Private internal Function to check the response status code
170     *
171     * \param[in]    response  - response from  weaver applet.
172     *
173     * \retval This function return errorcode from APP_ERR_CODE type
174     */
175    APP_ERR_CODE checkStatus(std::vector<uint8_t> response);
176    /* Private constructor to make class singleton*/
177    WeaverParserImpl() = default;
178    /* Private destructor to make class singleton*/
179    ~WeaverParserImpl() = default;
180    /* Private copy constructor to make class singleton*/
181    WeaverParserImpl(const WeaverParserImpl &) = delete;
182    /* Private operator overload to make class singleton*/
183    WeaverParserImpl &operator=(const WeaverParserImpl &) = delete;
184  
185    /* Private self instance for singleton purpose*/
186    static WeaverParserImpl *s_instance;
187    /* Private once flag (c++11) for singleton purpose.
188     * once_flag should pass to multiple calls of
189     * std::call_once allows those calls to coordinate with each other
190     * such a way only one will actually run to completion.
191     */
192    static std::once_flag s_instanceFlag;
193    /* Private function to create the instance of self class
194     * Same will be used for std::call_once
195     */
196    static void createInstance();
197  };
198  
199  #endif /* _WEAVER_PARSER_IMPL_H_ */
200