• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include <Hilog/log.h>
17 #include <SLES/OpenSLES.h>
18 #include "napi/native_api.h"
19 
20 
21 // The required functions are implemented here, and the return values are all "native_ Value", for JS to call.
Create(napi_env env,napi_callback_info info)22 static napi_value Create(napi_env env, napi_callback_info info)
23 {
24     // Get the parameter obtained from the front end and print using API "OH_LOG_Print".
25     size_t argc = 1;
26     napi_value args[1] = { nullptr };
27     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
28     napi_valuetype valuetype;
29     napi_typeof(env, args[0], &valuetype);
30     int value0;
31     napi_get_value_int32(env, args[0], &value0);
32     int valueForCompare = 5;
33     if (value0 > valueForCompare) {
34         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "[HiLog]", "Your digit is bigger than 5");
35     } else {
36         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "[HiLog]", "Your digit is smaller than 6");
37     }
38 
39     // Call the API "slCreateEngine" and return data to the front end.
40     char successStr[] = "Engine is successfully created";
41     char failStr[] = "Failed to create engine";
42     SLObjectItf engineObject = NULL; // Declare engine interface object
43     SLresult result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
44     napi_value resultStr;
45     if (SL_RESULT_SUCCESS == result) {
46         napi_create_string_utf8(env, successStr, sizeof(successStr), &resultStr);
47     } else {
48         napi_create_string_utf8(env, failStr, sizeof(failStr), &resultStr);
49     }
50     return resultStr;
51 }
52 
53 EXTERN_C_START
Init(napi_env env,napi_value exports)54 static napi_value Init(napi_env env, napi_value exports)
55 {
56     // The first para is the method name of JS call, the third para is the method name of cpp method.
57     napi_property_descriptor desc[] = {
58         { "create", nullptr, Create, nullptr, nullptr, nullptr, napi_default, nullptr },
59     };
60     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
61     return exports;
62 }
63 EXTERN_C_END
64 
65 static napi_module
66 demoModule = {
67 .nm_version = 1,
68 .nm_flags = 0,
69 .nm_filename = nullptr,
70 .nm_register_func = Init,
71 .nm_modname = "libnative",
72 .nm_priv = ((void *)0),
73 .reserved = {
74 0 },
75 };
76 
RegisterModule(void)77 extern "C" __attribute__((constructor)) void RegisterModule(void)
78 {
79 napi_module_register(& demoModule);
80 }