• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "napi/native_api.h"
18 #include "napi/native_common.h"
19 #include "ddk_api.h"
20 #include "ddk_types.h"
21 #include <cstdlib>
22 #include <js_native_api_types.h>
23 #include <unistd.h>
24 #include <vector>
25 
26 #define BUFF_LENTH 10
27 #define PARAM_0 0
28 #define PORT_READ 0x01
29 #define PORT_WRITE 0x02
30 #define PORT_ILLEGAL 0x08
31 
DdkCreateAshmemOne(napi_env env,napi_callback_info info)32 static napi_value DdkCreateAshmemOne(napi_env env, napi_callback_info info)
33 {
34     DDK_Ashmem *ashmem = nullptr;
35     const uint8_t name[100] = "TestAshmem";
36     uint32_t bufferLen = BUFF_LENTH;
37     int32_t returnValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
38     napi_value result = nullptr;
39     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
40     return result;
41 }
42 
43 
DdkCreateAshmemTwo(napi_env env,napi_callback_info info)44 static napi_value DdkCreateAshmemTwo(napi_env env, napi_callback_info info)
45 {
46     DDK_Ashmem *ashmem = nullptr;
47     const uint8_t name[100] = "TestAshmem";
48     uint32_t bufferLen = PARAM_0;
49     int32_t returnValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
50     napi_value result = nullptr;
51     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
52     return result;
53 }
54 
DdkCreateAshmemThree(napi_env env,napi_callback_info info)55 static napi_value DdkCreateAshmemThree(napi_env env, napi_callback_info info)
56 {
57     DDK_Ashmem *ashmem = nullptr;
58     const uint8_t *name = nullptr;
59     uint32_t bufferLen = BUFF_LENTH;
60     int32_t returnValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
61     napi_value result = nullptr;
62     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
63     return result;
64 }
65 
DdkMapAshmemOne(napi_env env,napi_callback_info info)66 static napi_value DdkMapAshmemOne(napi_env env, napi_callback_info info)
67 {
68     DDK_Ashmem *ashmem = nullptr;
69     const uint8_t name[100] = "TestAshmem";
70     uint32_t bufferLen = BUFF_LENTH;
71     int32_t ddkCreateAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
72     NAPI_ASSERT(env, ddkCreateAshmemValue == DDK_SUCCESS, "OH_DDK_CreateAshmem failed");
73     const uint8_t ashmemMapType = PORT_READ | PORT_WRITE;
74     int32_t returnValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
75     napi_value result = nullptr;
76     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
77     return result;
78 }
79 
DdkMapAshmemTwo(napi_env env,napi_callback_info info)80 static napi_value DdkMapAshmemTwo(napi_env env, napi_callback_info info)
81 {
82     const uint8_t ashmemMapType = PORT_READ | PORT_WRITE;
83     int32_t returnValue = OH_DDK_MapAshmem(nullptr, ashmemMapType);
84     napi_value result = nullptr;
85     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
86     return result;
87 }
88 
DdkMapAshmemThree(napi_env env,napi_callback_info info)89 static napi_value DdkMapAshmemThree(napi_env env, napi_callback_info info)
90 {
91     DDK_Ashmem *ashmem = nullptr;
92     const uint8_t name[100] = "TestAshmem";
93     uint32_t bufferLen = BUFF_LENTH;
94     int32_t ddkCreateAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
95     NAPI_ASSERT(env, ddkCreateAshmemValue == DDK_SUCCESS, "OH_DDK_CreateAshmem failed");
96     const uint8_t ashmemMapType = PORT_READ | PORT_WRITE;
97     ashmem->ashmemFd = 0;
98     int32_t returnValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
99     napi_value result = nullptr;
100     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
101     return result;
102 }
103 
DdkMapAshmemFour(napi_env env,napi_callback_info info)104 static napi_value DdkMapAshmemFour(napi_env env, napi_callback_info info)
105 {
106     DDK_Ashmem *ashmem = nullptr;
107     const uint8_t name[100] = "TestAshmem";
108     uint32_t bufferLen = BUFF_LENTH;
109     int32_t ddkCreateAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
110     NAPI_ASSERT(env, ddkCreateAshmemValue == DDK_SUCCESS, "OH_DDK_CreateAshmem failed");
111     const uint8_t ashmemMapType = PORT_ILLEGAL;
112     int32_t returnValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
113     napi_value result = nullptr;
114     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
115     return result;
116 }
117 
DdkUnmapAshmemOne(napi_env env,napi_callback_info info)118 static napi_value DdkUnmapAshmemOne(napi_env env, napi_callback_info info)
119 {
120     DDK_Ashmem *ashmem = nullptr;
121     const uint8_t name[100] = "TestAshmem";
122     uint32_t bufferLen = BUFF_LENTH;
123     int32_t ddkCreateAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
124     NAPI_ASSERT(env, ddkCreateAshmemValue == DDK_SUCCESS, "OH_DDK_CreateAshmem failed");
125     const uint8_t ashmemMapType = PORT_READ | PORT_WRITE;
126     int32_t ddkMapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
127     NAPI_ASSERT(env, ddkMapAshmemValue == DDK_SUCCESS, "OH_DDK_MapAshmem failed");
128     int32_t returnValue = OH_DDK_UnmapAshmem(ashmem);
129     napi_value result = nullptr;
130     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
131     return result;
132 }
133 
DdkUnmapAshmemTwo(napi_env env,napi_callback_info info)134 static napi_value DdkUnmapAshmemTwo(napi_env env, napi_callback_info info)
135 {
136     int32_t returnValue = OH_DDK_UnmapAshmem(nullptr);
137     napi_value result = nullptr;
138     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
139     return result;
140 }
141 
DdkDestroyAshmemOne(napi_env env,napi_callback_info info)142 static napi_value DdkDestroyAshmemOne(napi_env env, napi_callback_info info)
143 {
144     DDK_Ashmem *ashmem = nullptr;
145     const uint8_t name[100] = "TestAshmem";
146     uint32_t bufferLen = BUFF_LENTH;
147     int32_t ddkCreateAshmemValue = OH_DDK_CreateAshmem(name, bufferLen, &ashmem);
148     NAPI_ASSERT(env, ddkCreateAshmemValue == DDK_SUCCESS, "OH_DDK_CreateAshmem failed");
149     const uint8_t ashmemMapType = PORT_READ | PORT_WRITE;
150     int32_t ddkMapAshmemValue = OH_DDK_MapAshmem(ashmem, ashmemMapType);
151     NAPI_ASSERT(env, ddkMapAshmemValue == DDK_SUCCESS, "OH_DDK_MapAshmem failed");
152     int32_t returnValue = OH_DDK_DestroyAshmem(ashmem);
153     napi_value result = nullptr;
154     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
155     return result;
156 }
157 
DdkDestroyAshmemTwo(napi_env env,napi_callback_info info)158 static napi_value DdkDestroyAshmemTwo(napi_env env, napi_callback_info info)
159 {
160     int32_t returnValue = OH_DDK_DestroyAshmem(nullptr);
161     napi_value result = nullptr;
162     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
163     return result;
164 }
165 
166 EXTERN_C_START
Init(napi_env env,napi_value exports)167 static napi_value Init(napi_env env, napi_value exports)
168 {
169     napi_property_descriptor desc[] = {
170         {"ddkCreateAshmemOne", nullptr, DdkCreateAshmemOne, nullptr, nullptr, nullptr, napi_default, nullptr},
171         {"ddkCreateAshmemTwo", nullptr, DdkCreateAshmemTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
172         {"ddkCreateAshmemThree", nullptr, DdkCreateAshmemThree, nullptr, nullptr, nullptr, napi_default, nullptr},
173         {"ddkMapAshmemOne", nullptr, DdkMapAshmemOne, nullptr, nullptr, nullptr, napi_default, nullptr},
174         {"ddkMapAshmemTwo", nullptr, DdkMapAshmemTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
175         {"ddkMapAshmemThree", nullptr, DdkMapAshmemThree, nullptr, nullptr, nullptr, napi_default, nullptr},
176         {"ddkMapAshmemFour", nullptr, DdkMapAshmemFour, nullptr, nullptr, nullptr, napi_default, nullptr},
177         {"ddkUnmapAshmemOne", nullptr, DdkUnmapAshmemOne, nullptr, nullptr, nullptr, napi_default, nullptr},
178         {"ddkUnmapAshmemTwo", nullptr, DdkUnmapAshmemTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
179         {"ddkDestroyAshmemOne", nullptr, DdkDestroyAshmemOne, nullptr, nullptr, nullptr, napi_default, nullptr},
180         {"ddkDestroyAshmemTwo", nullptr, DdkDestroyAshmemTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
181     };
182 
183     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
184     return exports;
185 }
186 EXTERN_C_END
187 
188 static napi_module demoModule = {
189     .nm_version = 1,
190     .nm_flags = 0,
191     .nm_filename = nullptr,
192     .nm_register_func = Init,
193     .nm_modname = "libbaseddkjstest",
194     .nm_priv = ((void *)0),
195     .reserved = { 0 },
196 };
197 
RegisterModule(void)198 extern "C" __attribute__((constructor)) void RegisterModule(void)
199 {
200     napi_module_register(&demoModule);
201 }
202