• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Unionman Technology 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 #include <cstdio>
16 #include <cstring>
17 #include "iremote_proxy.h"
18 #include "iremote_object.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "system_ability_definition.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "hilog/log.h"
26 
27 #define ZLOGE(LOG_LABEL, fmt, args...)                                                                                 \
28     (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
29 #define ZLOGW(LOG_LABEL, fmt, args...)                                                                                 \
30     (void)OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
31 #define ZLOGI(LOG_LABEL, fmt, args...)                                                                                 \
32     (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
33 #define ZLOGD(LOG_LABEL, fmt, args...)                                                                                 \
34     (void)OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args)
35 
36 using namespace OHOS;
37 // 定义消息码
38 const int ABILITY_LENET = 6;
39 static HiviewDFX::HiLogLabel LABEL = {LOG_APP, 0x0002, "ShellServer"};
40 class IShellAbility : public IRemoteBroker {
41 public:
42     // DECLARE_INTERFACE_DESCRIPTOR是必需的,入参需使用std::u16string;
43     DECLARE_INTERFACE_DESCRIPTOR(u"shell.Ability");
44     virtual std::string lenet(const std::string &dummy) = 0;
45 };
46 
47 class ShellAbilityProxy : public IRemoteProxy<IShellAbility> {
48 public:
49     explicit ShellAbilityProxy(const sptr<IRemoteObject> &impl);
50     std::string lenet(const std::string &dummy) override;
51 
52 private:
53     static inline BrokerDelegator<ShellAbilityProxy> delegator_; // 方便后续使用iface_cast宏
54 };
55 
ShellAbilityProxy(const sptr<IRemoteObject> & impl)56 ShellAbilityProxy::ShellAbilityProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IShellAbility>(impl)
57 {
58 }
59 
lenet(const std::string & dummy)60 std::string ShellAbilityProxy::lenet(const std::string &dummy)
61 {
62     MessageOption option;
63     MessageParcel dataParcel;
64     MessageParcel replyParcel;
65     dataParcel.WriteString(dummy);
66     int error = Remote()->SendRequest(ABILITY_LENET, dataParcel, replyParcel, option);
67     std::string result = (error == ERR_NONE) ? replyParcel.ReadString() : "-1";
68 
69     return result;
70 }
71 
lenet(const std::string data)72 static std::string lenet(const std::string data)
73 {
74     int serverId = 1602;
75     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76     if (samgr == nullptr) {
77         ZLOGW(LABEL, "erro samgr");
78         return "erro samgr";
79     } else {
80         ZLOGW(LABEL, "samgr  got");
81     }
82     sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(ServerId);
83     if (remoteObject != nullptr) {
84         ZLOGW(LABEL, "Got Shell Service object");
85     } else {
86         ZLOGW(LABEL, "Got Shell Service object error");
87         return "Got Shell Service object error";
88     }
89     sptr<IShellAbility> ShellAbility = iface_cast<IShellAbility>(remoteObject); // 使用iface_cast宏转换成具体类型
90 
91     std::string result = ShellAbility->lenet(data.c_str());
92     ZLOGW(LABEL, "client ipc shell serevr %{pubilc}s", result.c_str());
93     return result;
94 }
95 
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99 
100 struct LedAddOnData {
101     napi_async_work asyncWork = nullptr; // 异步工作项
102     napi_deferred deferred = nullptr;    // 用于Promise的resolve、reject处理
103     napi_ref callback = nullptr;         // 回调函数
104     char *args[1] = {nullptr};
105     char *arr;    // 2个输入参数
106     char *result; // 业务逻辑处理结果(返回值)
107 };
108 // 业务逻辑处理函数。
completeCBForPromise(napi_env env,napi_status status,void * data)109 static void completeCBForPromise(napi_env env, napi_status status, void *data)
110 {
111     LedAddOnData *addOnData = (LedAddOnData *)data;
112     napi_value result = nullptr;
113     napi_create_string_utf8(env, addOnData->result, strlen(addOnData->result), &result);
114     napi_resolve_deferred(env, addOnData->deferred, result);
115     free(addOnData->args[0]);
116     free(addOnData->result);
117     // 删除napi_ref对象
118     if (addOnData->callback != nullptr) {
119         napi_delete_reference(env, addOnData->callback);
120     }
121     // 删除异步工作项
122     napi_delete_async_work(env, addOnData->asyncWork);
123     delete addOnData;
124 }
125 // 业务逻辑处理函数,由worker线程池调度执行。
ShellExecuteCB(napi_env env,void * data)126 static void ShellExecuteCB(napi_env env, void *data)
127 {
128     LedAddOnData *addOnData = (LedAddOnData *)data;
129     std::string result = lenet(addOnData->args[0]);
130     addOnData->result = strdup(result.c_str()); // 使用 strdup 分配并复制字符串
131 }
lenetWithPromise(napi_env env,napi_callback_info info)132 static napi_value lenetWithPromise(napi_env env, napi_callback_info info)
133 {
134     // 期望从 ArkTS 侧获取的参数的数量,这里只接受一个参数
135     size_t argc = 1;
136     napi_value args[1] = {nullptr};
137     // 从 info 中获取从 ArkTS 侧传递过来的参数
138     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
139     // 将获取的 ArkTS 参数转换为 native 信息,这里将其转换为字符串类型
140     ZLOGW(LABEL, "lenetWithPromise got");
141     // 创建promise
142     napi_value promise = nullptr;
143     napi_deferred deferred = nullptr;
144     NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
145 
146     // 异步工作项上下文用户数据,传递到异步工作项的execute、complete之间传递数据
147     auto addonData = new LedAddOnData {
148         .asyncWork = nullptr,
149         .deferred = deferred,
150     };
151 
152     // 将被收到的参数传入
153     size_t strSize = 0;
154     napi_get_value_string_utf8(env, args[0], nullptr, 0, &strSize);
155     char *strValue = new char[strSize + 1];
156     napi_status status1;
157     size_t strSize1 = 0;
158     status1 = napi_get_value_string_utf8(env, args[0], strValue, strSize + 1, &strSize1);
159     addonData->args[0] = (char *)malloc(strlen(strValue) + 1);
160     addonData->args[0] = strdup(strValue); // 使用 strdup 分配并复制字符串
161     // 创建async work,创建成功后通过最后一个参数(addonData->asyncWork)返回asyncwork的handle
162     napi_value resourceName = nullptr;
163     napi_create_string_utf8(env, "ShellExecuteCB", NAPI_AUTO_LENGTH, &resourceName);
164     ZLOGW(LABEL, "lenetWithPromise working");
165     napi_create_async_work(env, nullptr, resourceName, ShellExecuteCB, completeCBForPromise, (void *)addonData,
166                            &addonData->asyncWork);
167     // 将刚创建的async work加到队列,由底层去调度执行
168     napi_queue_async_work(env, addonData->asyncWork);
169     // 返回promise
170     return promise;
171 }
172 /* 以上为实现异步计算接下来10s内温度,可设置为传参类型 */
173 
174 /*
175  * 注册接口
176  */
registerdeviceinfoApis(napi_env env,napi_value exports)177 static napi_value registerdeviceinfoApis(napi_env env, napi_value exports)
178 {
179     napi_property_descriptor desc[] = {
180         DECLARE_NAPI_FUNCTION("lenetWithPromise", lenetWithPromise),
181     };
182     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
183     return exports;
184 }
185 /*
186  * 模块定义
187  */
188 static napi_module writeShellnapiModule = {
189     .nm_version = 1,
190     .nm_flags = 0,
191     .nm_filename = nullptr,
192     .nm_register_func = registerdeviceinfoApis,
193     .nm_modname = "lenetnapi", // 模块名
194     .nm_priv = ((void *)0),
195     .reserved = {0},
196 };
197 #ifdef __cplusplus
198 }
199 #endif
200 /*
201  * 注册模块
202  */
RegisterModule(void)203 extern "C" __attribute__((constructor)) void RegisterModule(void)
204 {
205     napi_module_register(&writeShellnapiModule); // 接口注册函数
206 }
207