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 
16 #include "napi/native_api.h"
17 #include <ctime>
18 #include <sys/timerfd.h>
19 #include <unistd.h>
20 
21 #define NANOSECOND (1000000000)
22 #define MICROSECONDS (1000000)
23 #define DATA_TWO (2)
24 #define NUMBER (-1)
25 #define ONE 1
26 #define TWO 2
27 #define PARAM_0 0
28 #define PARAM_1 1
29 #define TIME_L 1659177614
30 
Timerfd_create(napi_env env, napi_callback_info info)31 static napi_value Timerfd_create(napi_env env, napi_callback_info info)
32 {
33     size_t argc = 1;
34     napi_value args[1] = {nullptr};
35     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
36     int valueFirst;
37     napi_get_value_int32(env, args[0], &valueFirst);
38     int timerfd = timerfd_create(NUMBER, NUMBER);
39     napi_value result = nullptr;
40     napi_create_int32(env, timerfd, &result);
41     close(timerfd);
42     return result;
43 }
44 
Timerfd_gettime(napi_env env, napi_callback_info info)45 static napi_value Timerfd_gettime(napi_env env, napi_callback_info info)
46 {
47     size_t argc = 1;
48     napi_value args[1] = {nullptr};
49     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
50     int valueFirst;
51     napi_get_value_int32(env, args[0], &valueFirst);
52     int timeValue = timerfd_gettime(NUMBER, nullptr);
53     napi_value result = nullptr;
54     napi_create_int32(env, timeValue, &result);
55     return result;
56 }
57 
Timerfd_settime(napi_env env, napi_callback_info info)58 static napi_value Timerfd_settime(napi_env env, napi_callback_info info)
59 {
60     size_t argc = 1;
61     napi_value args[1] = {nullptr};
62     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
63     int valueFirst;
64     napi_get_value_int32(env, args[0], &valueFirst);
65     struct itimerspec its = {{PARAM_0, PARAM_0}, {DATA_TWO, PARAM_0}};
66     struct itimerspec val;
67     int fd = PARAM_0;
68     int timeValue = PARAM_0;
69     fd = timerfd_create(CLOCK_REALTIME, PARAM_0);
70     timeValue = timerfd_settime(fd, PARAM_0, &its, nullptr);
71     timeValue = usleep(MICROSECONDS);
72     timeValue = timerfd_gettime(fd, &val);
73     napi_value result = nullptr;
74     napi_create_int32(env, timeValue, &result);
75     return result;
76 }
77 
78 EXTERN_C_START
Init(napi_env env, napi_value exports)79 static napi_value Init(napi_env env, napi_value exports)
80 {
81     napi_property_descriptor desc[] = {
82         {"timerfd_create", nullptr, Timerfd_create, nullptr, nullptr, nullptr, napi_default, nullptr},
83         {"timerfd_gettime", nullptr, Timerfd_gettime, nullptr, nullptr, nullptr, napi_default, nullptr},
84         {"timerfd_settime", nullptr, Timerfd_settime, nullptr, nullptr, nullptr, napi_default, nullptr},
85     };
86     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
87     return exports;
88 }
89 EXTERN_C_END
90 
91 static napi_module demoModule = {
92     .nm_version = 1,
93     .nm_flags = 0,
94     .nm_filename = nullptr,
95     .nm_register_func = Init,
96     .nm_modname = "time",
97     .nm_priv = ((void *)0),
98     .reserved = {0},
99 };
100 
RegisterModule(void)101 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
102