1 /*
2  * Copyright (c) 2020 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACELITE_FEATURE_ABILITY_MODULE_H
17 #define OHOS_ACELITE_FEATURE_ABILITY_MODULE_H
18 
19 #include "acelite_config.h"
20 #include "non_copyable.h"
21 
22 #if (defined(FEATURE_FEATURE_ABILITY_MODULE) && (FEATURE_FEATURE_ABILITY_MODULE == 1))
23 #include "ability_kit.h"
24 #include "presets/preset_module.h"
25 namespace OHOS {
26 namespace ACELite {
27 static constexpr char FEATURE_ABILITY[] = "FeatureAbility";
28 struct FailCallbackParams : public MemoryHeap {
29     void *data;
30     uint16_t dataLength;
31     uint16_t errorCode;
FailCallbackParamsFailCallbackParams32     FailCallbackParams() : data(nullptr), dataLength(0), errorCode(0) {}
33     ACE_DISALLOW_COPY_AND_MOVE(FailCallbackParams);
34 };
35 class FeatureAbilityModule final : public PresetModule {
36 public:
37     ACE_DISALLOW_COPY_AND_MOVE(FeatureAbilityModule);
38     /**
39      * @fn FeatureAbilityModule::FeatureAbilityModule()
40      *
41      * @brief Constructor.
42      */
FeatureAbilityModule()43     FeatureAbilityModule() : PresetModule(FEATURE_ABILITY) {}
44 
45     /**
46      * @fn FeatureAbilityModule::~FeatureAbilityModule()
47      *
48      * @brief Constructor.
49      */
50     ~FeatureAbilityModule() = default;
51 
52     void Init() override;
53 
54     static void Release();
55 
56 private:
57     /**
58      * @fn FeatureAbilityModule::SubscribeMessage
59      *
60      * @brief subscribe message which comes from other device
61      * @param func function object
62      * @param context the context of function execution
63      * @param args the list of arguments
64      * @param length the length of arguments list
65      */
66     static jerry_value_t SubscribeMessage(const jerry_value_t func,
67                                           const jerry_value_t context,
68                                           const jerry_value_t args[],
69                                           const jerry_length_t length);
70 
71     /**
72      * @fn FeatureAbilityModule::UnsubscribeMessage
73      *
74      * @brief unsubscribe message which comes from other device
75      * @param func function object
76      * @param context the context of function execution
77      * @param args the list of arguments
78      * @param length the length of arguments list
79      */
80     static jerry_value_t UnsubscribeMessage(const jerry_value_t func,
81                                             const jerry_value_t context,
82                                             const jerry_value_t args[],
83                                             const jerry_length_t length);
84 
85     /**
86      * @fn FeatureAbilityModule::SendMsgToPeer
87      *
88      * @brief send message to phone app
89      * @param func function object
90      * @param context the context of function execution
91      * @param args the list of arguments
92      * @param length the length of arguments list
93      */
94     static jerry_value_t SendMsgToPeer(const jerry_value_t func,
95                                        const jerry_value_t context,
96                                        const jerry_value_t args[],
97                                        const jerry_length_t length);
98 
99     static void SyncFailCallback(jerry_value_t &failCb,
100                                  const jerry_value_t &context,
101                                  const char * const data,
102                                  uint16_t errorCode);
103 
104     static void SyncSendMsgCallback(const jerry_value_t &arg,
105                                     const jerry_value_t &context,
106                                     const char *errMsg,
107                                     const uint16_t errCode,
108                                     bool success);
109 
110     /**
111      * @fn FeatureAbilityModule::Detect
112      *
113      * @brief ping to phone app
114      * @param func function object
115      * @param context the context of function execution
116      * @param args the list of arguments
117      * @param length the length of arguments list
118      */
119     static jerry_value_t Detect(const jerry_value_t func,
120                                      const jerry_value_t context,
121                                      const jerry_value_t args[],
122                                      const jerry_length_t length);
123 
124     static int32_t MessageSuccessCallback(const void *data);
125 
126     static void CopySuccessMessage(const FeatureAbilityDataInfo *origin, FeatureAbilityDataInfo *&target);
127 
128     static int32_t MessageFailCallback(const void *data, uint16_t dataLength, uint16_t errorCode);
129 
130     static void AsyncSuccessCallback(void *data);
131 
132     static void AsyncFailCallback(void *data);
133 
134     static void ReleaseJsValues();
135 
136     static void ReleaseJsValue(jerry_value_t &value);
137 
138     static void ReleaseDetectJsValue();
139 
140     static void ReleaseSendMsgJsValue();
141 
142     /**
143      * the execute context of callback
144      */
145     static jerry_value_t callbackContext_;
146 
147     /**
148      * success callback
149      */
150     static jerry_value_t successCallback_;
151 
152     /**
153      * fail callback
154      */
155     static jerry_value_t failCallback_;
156 
157     /**
158      * register callback whether of not
159      */
160     static bool registed;
161 
162     /**
163      * the execute context of callback for sendMsg
164      */
165     static jerry_value_t sendMsgCallbackContext_;
166 
167     /**
168      * success callback for sendMsg
169      */
170     static jerry_value_t sendMsgSuccessCallback_;
171 
172     /**
173      * fail callback for sendMsg
174      */
175     static jerry_value_t sendMsgFailCallback_;
176 
177     /**
178      * the execute context of callback for detect
179      */
180     static jerry_value_t detectCallbackContext_;
181 
182     /**
183      * success callback for detect
184      */
185     static jerry_value_t detectSuccessCallback_;
186 
187     /**
188      * fail callback for detect
189      */
190     static jerry_value_t detectFailCallback_;
191 };
192 } // namespace ACELite
193 } // namespace OHOS
194 #endif
195 namespace OHOS {
196 namespace ACELite {
197 class FeaAbilityModule final {
198 public:
199     ACE_DISALLOW_COPY_AND_MOVE(FeaAbilityModule);
200     FeaAbilityModule() = default;
201     ~FeaAbilityModule() = default;
Load()202     static void Load()
203     {
204 #if (defined(FEATURE_FEATURE_ABILITY_MODULE) && (FEATURE_FEATURE_ABILITY_MODULE == 1))
205         FeatureAbilityModule featureAbilityModule;
206         featureAbilityModule.Init();
207 #endif
208     }
Release()209     static void Release()
210     {
211 #if (defined(FEATURE_FEATURE_ABILITY_MODULE) && (FEATURE_FEATURE_ABILITY_MODULE == 1))
212     FeatureAbilityModule::Release();
213 #endif
214     }
215 };
216 } // namespace ACELite
217 } // namespace OHOS
218 #endif // OHOS_ACELITE_FEATURE_ABILITY_MODULE_H
219