1 /******************************************************************************
2  *
3  *  Copyright 2020 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_IMPL_H_
20 #define _WEAVER_IMPL_H_
21 
22 #include <mutex>
23 #include <weaver_interface.h>
24 #include <weaver_parser.h>
25 #include <weaver_transport.h>
26 
27 class WeaverImpl : public WeaverInterface {
28 public:
29   /**
30    * \brief Function to initialize Weaver Interface
31    *
32    * \retval This function return Weaver_STATUS_OK (0) in case of success
33    *         In case of failure returns other Status_Weaver.
34    */
35   Status_Weaver Init() override;
36 
37   /**
38    * \brief Function to read slot information
39    * \param[out]   slotInfo - slot information values read out
40    *
41    * \retval This function return Weaver_STATUS_OK (0) in case of success
42    *         In case of failure returns other Status_Weaver errorcodes.
43    */
44   Status_Weaver GetSlots(SlotInfo &slotInfo) override;
45 
46   /**
47    * \brief Function to read value of specific key & slotId
48    * \param[in]    slotId -       input slotId which's information to be read
49    * \param[in]    key -          input key which's information to be read
50    * \param[out]   readRespInfo - read information values to be read out
51    *
52    * \retval This function return Weaver_STATUS_OK (0) in case of success
53    *         In case of failure returns other Status_Weaver errorcodes.
54    */
55   Status_Weaver Read(uint32_t slotId, const std::vector<uint8_t> &key,
56                      ReadRespInfo &readRespInfo) override;
57 
58   /**
59    * \brief Function to write value to specific key & slotId
60    * \param[in]    slotId -       input slotId where value to be write
61    * \param[in]    key -          input key where value to be write
62    * \param[in]   value -        input value which will be written
63    *
64    * \retval This function return Weaver_STATUS_OK (0) in case of success
65    *         In case of failure returns other Status_Weaver.
66    */
67   Status_Weaver Write(uint32_t slotId, const std::vector<uint8_t> &key,
68                       const std::vector<uint8_t> &value) override;
69 
70   /**
71    * \brief Function to de-initialize Weaver Interface
72    *
73    * \retval This function return Weaver_STATUS_OK (0) in case of success
74    *         In case of failure returns other Status_Weaver.
75    */
76   Status_Weaver DeInit() override;
77 
78   /**
79    * \brief static function to get the singleton instance of WeaverImpl class
80    *
81    * \retval instance of WeaverImpl.
82    */
83   static WeaverImpl *getInstance();
84 
85 private:
86   /* Transport interface to be use for communication */
87   WeaverTransport *mTransport;
88   /* Parser interface to frame weaver commands and parse response*/
89   WeaverParser *mParser;
90   /* Internal close api for transport close */
91   bool close();
92   /* Private constructor to make class singleton*/
93   WeaverImpl() = default;
94   /* Private destructor to make class singleton*/
95   ~WeaverImpl() = default;
96   /* Private copy constructor to make class singleton*/
97   WeaverImpl(const WeaverImpl &) = delete;
98   /* Private operator overload to make class singleton*/
99   WeaverImpl &operator=(const WeaverImpl &) = delete;
100 
101   /* Private self instance for singleton purpose*/
102   static WeaverImpl *s_instance;
103 
104   /* Private once flag (c++11) for singleton purpose.
105    * once_flag should pass to multiple calls of
106    * std::call_once allows those calls to coordinate with each other
107    * such a way only one will actually run to completion.
108    */
109   static std::once_flag s_instanceFlag;
110 
111   /* Private function to create the instance of self class
112    * Same will be used for std::call_once
113    */
114   static void createInstance();
115 };
116 
117 #endif /* _WEAVER_IMPL_H_ */
118