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_TRANSPORT_IMPL_H_
20 #define _WEAVER_TRANSPORT_IMPL_H_
21 
22 #include <mutex>
23 #include <weaver_transport.h>
24 
25 class WeaverTransportImpl : public WeaverTransport {
26 public:
27   /**
28    * \brief Function to initialize Weaver Transport Interface
29    *
30    * \param[in]    aid -  applet id to be set to transport interface
31    *
32    * \retval This function return true in case of success
33    *         In case of failure returns false.
34    */
35   bool Init(std::vector<std::vector<uint8_t>> aid) override;
36 
37   /**
38    * \brief Function to open applet connection
39    *
40    * \param[in]    data -         command for open applet
41    * \param[out]   resp -         response from applet
42    *
43    * \retval This function return true in case of success
44    *         In case of failure returns false.
45    */
46   bool OpenApplet(std::vector<uint8_t> data,
47                   std::vector<uint8_t> &resp) override;
48 
49   /**
50    * \brief Function to close applet connection
51    *
52    * \retval This function return true in case of success
53    *         In case of failure returns false.
54    */
55   bool CloseApplet() override;
56 
57   /**
58    * \brief Function to send commands to applet
59    *
60    * \param[in]    data -         command to be send to applet
61    * \param[out]   resp -         response from applet
62    *
63    * \retval This function return true in case of success
64    *         In case of failure returns false.
65    */
66   bool Send(std::vector<uint8_t> data, std::vector<uint8_t> &resp) override;
67 
68   /**
69    * \brief Function to de-initialize Weaver Transport Interface
70    *
71    * \retval This function return true in case of success
72    *         In case of failure returns false.
73    */
74   bool DeInit() override;
75 
76   /**
77    * \brief static function to get the singleton instance of WeaverTransportImpl
78    * class
79    *
80    * \retval instance of WeaverTransportImpl.
81    */
82   static WeaverTransportImpl *getInstance();
83 
84 private:
85   /* Private constructor to make class singleton*/
86   WeaverTransportImpl() = default;
87   /* Private destructor to make class singleton*/
88   ~WeaverTransportImpl() = default;
89   /* Private copy constructor to make class singleton*/
90   WeaverTransportImpl(const WeaverTransportImpl &) = delete;
91   /* Private operator overload to make class singleton*/
92   WeaverTransportImpl &operator=(const WeaverTransportImpl &) = delete;
93   /* Private api to detect if device boot completed or not*/
94   bool isDeviceBootCompleted();
95 
96   /* Private self instance for singleton purpose*/
97   static WeaverTransportImpl *s_instance;
98   /* Private once flag (c++11) for singleton purpose.
99    * once_flag should pass to multiple calls of
100    * std::call_once allows those calls to coordinate with each other
101    * such a way only one will actually run to completion.
102    */
103   static std::once_flag s_instanceFlag;
104   /* Private function to create the instance of self class
105    * Same will be used for std::call_once
106    */
107   static void createInstance();
108   /* Private wrapper function to send apdu.
109    * It will try with alternate aids if sending is failed.
110    */
111   bool sendInternal(std::vector<uint8_t> data, std::vector<uint8_t> &resp);
112 };
113 
114 #endif /* _WEAVER_TRANSPORT_IMPL_H_ */
115