1 /*
2 * Copyright (c) 2023 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 #include "common/native_common.h"
16 #include "napi/native_api.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <ctime>
20 #include <js_native_api_types.h>
21 #include <memory.h>
22 #include <sys/timerfd.h>
23 #include <sys/timex.h>
24 #include <unistd.h>
25 #include <utime.h>
26 #include <securec.h>
27
28 #define PARAM_1 1
29 #define MPARAM_1 (-1)
30 #define PARAM_0 0
31
32 extern "C" int __adjtimex_time64(struct timex *);
AdjTimex_time64_One(napi_env env,napi_callback_info info)33 static napi_value AdjTimex_time64_One(napi_env env, napi_callback_info info)
34 {
35 struct timex tx;
36 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
37 tx.offset = ADJ_OFFSET;
38 tx.tick = ADJ_TICK;
39 tx.maxerror = ADJ_MAXERROR;
40 tx.esterror = ADJ_ESTERROR;
41 tx.constant = ADJ_TIMECONST;
42 tx.freq = ADJ_FREQUENCY;
43 tx.status = ADJ_STATUS;
44 tx.precision = PARAM_1;
45 tx.tolerance = PARAM_1;
46 tx.ppsfreq = PARAM_1;
47 tx.jitter = PARAM_1;
48 tx.stabil = PARAM_1;
49 tx.jitcnt = PARAM_1;
50 tx.calcnt = PARAM_1;
51 tx.errcnt = PARAM_1;
52 tx.stbcnt = PARAM_1;
53 int resultValue = __adjtimex_time64(&tx);
54 NAPI_ASSERT(env, resultValue != MPARAM_1, "AdjTimex_time64_One failed");
55 napi_value result = nullptr;
56 napi_create_int32(env, resultValue == TIME_BAD, &result);
57 return result;
58 }
59
AdjTimex_time64_Two(napi_env env,napi_callback_info info)60 static napi_value AdjTimex_time64_Two(napi_env env, napi_callback_info info)
61 {
62 errno = PARAM_0;
63 struct timex tx;
64 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
65 int resultValue = __adjtimex_time64(&tx);
66 NAPI_ASSERT(env, resultValue != MPARAM_1, "AdjTimex_time64_Two failed");
67 napi_value result = nullptr;
68 napi_create_int32(env, resultValue == TIME_BAD, &result);
69 return result;
70 }
71
72 extern "C" int adjtimex(struct timex *);
AdjTimex_One(napi_env env,napi_callback_info info)73 static napi_value AdjTimex_One(napi_env env, napi_callback_info info)
74 {
75 struct timex tx;
76 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
77 tx.offset = ADJ_OFFSET;
78 tx.tick = ADJ_TICK;
79 tx.maxerror = ADJ_MAXERROR;
80 tx.esterror = ADJ_ESTERROR;
81 tx.constant = ADJ_TIMECONST;
82 tx.freq = ADJ_FREQUENCY;
83 tx.status = ADJ_STATUS;
84 tx.precision = PARAM_1;
85 tx.tolerance = PARAM_1;
86 tx.ppsfreq = PARAM_1;
87 tx.jitter = PARAM_1;
88 tx.stabil = PARAM_1;
89 tx.jitcnt = PARAM_1;
90 tx.calcnt = PARAM_1;
91 tx.errcnt = PARAM_1;
92 tx.stbcnt = PARAM_1;
93 int resultValue = adjtimex(&tx);
94 NAPI_ASSERT(env, resultValue != MPARAM_1, "AdjTimex_One failed");
95 napi_value result = nullptr;
96 napi_create_int32(env, resultValue == TIME_BAD, &result);
97 return result;
98 }
99
AdjTimex_Two(napi_env env,napi_callback_info info)100 static napi_value AdjTimex_Two(napi_env env, napi_callback_info info)
101 {
102 errno = PARAM_0;
103 struct timex tx;
104 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
105 int resultValue = adjtimex(&tx);
106 napi_value result = nullptr;
107 napi_create_int32(env, resultValue == TIME_BAD, &result);
108 return result;
109 }
110
111 extern "C" int __clock_adjtime64(clockid_t, struct timex *);
Clock_adjTime64_One(napi_env env,napi_callback_info info)112 static napi_value Clock_adjTime64_One(napi_env env, napi_callback_info info)
113 {
114 struct timex tx;
115 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
116 int returnValue = __clock_adjtime64(CLOCK_REALTIME, &tx);
117 napi_value result = nullptr;
118 napi_create_int32(env, returnValue == TIME_BAD, &result);
119 return result;
120 }
121
Clock_adjTime64_Two(napi_env env,napi_callback_info info)122 static napi_value Clock_adjTime64_Two(napi_env env, napi_callback_info info)
123 {
124 struct timex tx = {ADJ_OFFSET_SS_READ};
125 int returnValue = __clock_adjtime64(CLOCK_MONOTONIC, &tx);
126 napi_value result = nullptr;
127 napi_create_int32(env, returnValue == MPARAM_1, &result);
128 return result;
129 }
130
Clock_adjTime64_Three(napi_env env,napi_callback_info info)131 static napi_value Clock_adjTime64_Three(napi_env env, napi_callback_info info)
132 {
133 struct timex tx;
134 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
135 int returnValue = __clock_adjtime64(CLOCK_PROCESS_CPUTIME_ID, &tx);
136 napi_value result = nullptr;
137 napi_create_int32(env, returnValue == MPARAM_1, &result);
138 return result;
139 }
140
Clock_adjTime64_Four(napi_env env,napi_callback_info info)141 static napi_value Clock_adjTime64_Four(napi_env env, napi_callback_info info)
142 {
143 struct timex tx;
144 memset_s(&tx, sizeof(tx), PARAM_0, sizeof(tx));
145 int returnValue = __clock_adjtime64(CLOCK_THREAD_CPUTIME_ID, &tx);
146 napi_value result = nullptr;
147 napi_create_int32(env, returnValue == MPARAM_1, &result);
148 return result;
149 }
150
151 EXTERN_C_START
Init(napi_env env,napi_value exports)152 static napi_value Init(napi_env env, napi_value exports)
153 {
154 napi_property_descriptor desc[] = {
155 {"AdjTimex_time64_One", nullptr, AdjTimex_time64_One, nullptr, nullptr, nullptr, napi_default, nullptr},
156 {"AdjTimex_time64_Two", nullptr, AdjTimex_time64_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
157 {"AdjTimex_One", nullptr, AdjTimex_One, nullptr, nullptr, nullptr, napi_default, nullptr},
158 {"AdjTimex_Two", nullptr, AdjTimex_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
159 {"Clock_adjTime64_One", nullptr, Clock_adjTime64_One, nullptr, nullptr, nullptr, napi_default, nullptr},
160 {"Clock_adjTime64_Two", nullptr, Clock_adjTime64_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
161 {"Clock_adjTime64_Three", nullptr, Clock_adjTime64_Three, nullptr, nullptr, nullptr, napi_default, nullptr},
162 {"Clock_adjTime64_Four", nullptr, Clock_adjTime64_Four, nullptr, nullptr, nullptr, napi_default, nullptr},
163 };
164 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
165 return exports;
166 }
167 EXTERN_C_END
168
169 static napi_module demoModule = {
170 .nm_version = 1,
171 .nm_flags = 0,
172 .nm_filename = nullptr,
173 .nm_register_func = Init,
174 .nm_modname = "libtimexndk1",
175 .nm_priv = ((void *)0),
176 .reserved = {0},
177 };
178
RegisterModule(void)179 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }