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