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/napi_helper.cpp"
16 #include "common/native_common.h"
17 #include "napi/native_api.h"
18 #include <cerrno>
19 #include <csignal>
20 #include <cstdlib>
21 #include <cstring>
22 #include <ctime>
23 #include <malloc.h>
24 #include <net/if.h>
25 #include <sched.h>
26 #include <sys/time.h>
27 #include <sys/timex.h>
28 #include <unistd.h>
29 #include <utime.h>
30 #include <utmp.h>
31 #include <uv.h>
32 
33 #define NANOSECONDS (1000000000)
34 #define TEST_FILE "/data/storage/el2/base/files/test.txt"
35 #define TEST_FILE_PATH "/data/storage/el2/base/files/"
36 #define TEST_FILE_NAME "test.txt"
37 #define PARAM_0 0
38 #define PARAM_1 1
39 #define MPARAM_1 (-1)
40 #define PARAM_3 3
41 #define PARAM_111 111
42 #define PARAM_777 777
43 #define PARAM_0666 0666
44 #define PARAM_0777 0777
45 #define MPARAM_123 (-123)
46 
47 extern "C" int __timer_gettime64(timer_t, struct itimerspec *);
48 extern "C" int __timer_settime64(timer_t, int, const struct itimerspec *__restrict, struct itimerspec *__restrict);
TimerThread(union sigval v)49 void TimerThread(union sigval v) {}
50 
Timer_getTime64_One(napi_env env, napi_callback_info info)51 static napi_value Timer_getTime64_One(napi_env env, napi_callback_info info)
52 {
53     timer_t timerid;
54     struct sigevent evp;
55     memset(&evp, PARAM_0, sizeof(struct sigevent));
56 
57     evp.sigev_value.sival_int = PARAM_111;
58     evp.sigev_notify = SIGEV_THREAD;
59     evp.sigev_notify_function = TimerThread;
60 
61     int ret = timer_create(CLOCK_REALTIME, &evp, &timerid);
62 
63     struct itimerspec it;
64     it.it_interval.tv_sec = PARAM_1;
65     it.it_interval.tv_nsec = PARAM_0;
66     it.it_value.tv_sec = PARAM_3;
67     it.it_value.tv_nsec = PARAM_0;
68     ret = __timer_settime64(timerid, PARAM_0, &it, nullptr);
69 
70     struct itimerspec tmp;
71     ret = __timer_gettime64(timerid, &tmp);
72     NAPI_ASSERT(env, tmp.it_value.tv_sec < PARAM_1 || tmp.it_value.tv_nsec < NANOSECONDS,
73                 "Timer_getTime64_One--__timer_gettime64 failed");
74 
75     napi_value result = nullptr;
76     napi_create_int32(env, ret == PARAM_0, &result);
77 
78     timer_delete(timerid);
79     return result;
80 }
81 
Timer_getTime64_Two(napi_env env, napi_callback_info info)82 static napi_value Timer_getTime64_Two(napi_env env, napi_callback_info info)
83 {
84     struct itimerspec tmp;
85     int ret = __timer_gettime64(nullptr, &tmp);
86 
87     napi_value result = nullptr;
88     napi_create_int32(env, ret != PARAM_1 && errno == EINVAL, &result);
89     return result;
90 }
91 
Timer_setTime64_One(napi_env env, napi_callback_info info)92 static napi_value Timer_setTime64_One(napi_env env, napi_callback_info info)
93 {
94     timer_t timerid;
95     struct sigevent evp;
96     memset(&evp, PARAM_0, sizeof(struct sigevent));
97     evp.sigev_value.sival_int = PARAM_111;
98     evp.sigev_notify = SIGEV_THREAD;
99     evp.sigev_notify_function = TimerThread;
100 
101     int ret = timer_create(CLOCK_REALTIME, &evp, &timerid);
102     struct itimerspec it;
103     it.it_interval.tv_sec = PARAM_1;
104     it.it_interval.tv_nsec = PARAM_0;
105     it.it_value.tv_sec = PARAM_3;
106     it.it_value.tv_nsec = PARAM_0;
107     ret = __timer_settime64(timerid, PARAM_0, &it, nullptr);
108 
109     struct itimerspec tmp;
110     ret = __timer_gettime64(timerid, &tmp);
111     NAPI_ASSERT(env, tmp.it_value.tv_sec < PARAM_1 || tmp.it_value.tv_nsec < NANOSECONDS,
112                 "Timer_setTime64_One--__timer_gettime64 failed");
113 
114     napi_value result = nullptr;
115     napi_create_int32(env, ret == PARAM_0, &result);
116 
117     timer_delete(timerid);
118     return result;
119 }
120 
Timer_setTime64_Two(napi_env env, napi_callback_info info)121 static napi_value Timer_setTime64_Two(napi_env env, napi_callback_info info)
122 {
123     struct itimerspec it;
124     it.it_interval.tv_sec = PARAM_1;
125     it.it_interval.tv_nsec = PARAM_0;
126     it.it_value.tv_sec = PARAM_3;
127     it.it_value.tv_nsec = PARAM_0;
128     int ret = __timer_settime64(nullptr, PARAM_0, &it, nullptr);
129 
130     napi_value result = nullptr;
131     napi_create_int32(env, ret != PARAM_1 && errno == EINVAL, &result);
132     return result;
133 }
134 
135 EXTERN_C_START
Init(napi_env env, napi_value exports)136 static napi_value Init(napi_env env, napi_value exports)
137 {
138     napi_property_descriptor desc[] = {
139         {"Timer_getTime64_One", nullptr, Timer_getTime64_One, nullptr, nullptr, nullptr, napi_default, nullptr},
140         {"Timer_getTime64_Two", nullptr, Timer_getTime64_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
141         {"Timer_setTime64_One", nullptr, Timer_setTime64_One, nullptr, nullptr, nullptr, napi_default, nullptr},
142         {"Timer_setTime64_Two", nullptr, Timer_setTime64_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
143     };
144     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
145     return exports;
146 }
147 EXTERN_C_END
148 
149 static napi_module demoModule = {
150     .nm_version = 1,
151     .nm_flags = 0,
152     .nm_filename = nullptr,
153     .nm_register_func = Init,
154     .nm_modname = "libtimendk1",
155     .nm_priv = ((void *)0),
156     .reserved = {0},
157 };
158 
RegisterModule(void)159 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
160