Unset ip_resolved_cb on vine_service_destroy
[platform/core/api/vine.git] / src / vine-timer.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17
18 #include <sys/timerfd.h>
19 #include <unistd.h>
20 #include <errno.h>
21
22 #include "vine-event-loop.h"
23 #include "vine-log.h"
24 #include "vine-timer.h"
25
26 VINE_NAMESPACE_USE;
27
28 #define TIMER_SIGNAL_NO SIGRTMIN
29 VineTimer::VineTimer()
30         : __data(NULL), __timer_fd(-1)
31 {
32         VINE_LOGD("Timer is created [%p]", this);
33 }
34
35
36 VineTimer::~VineTimer()
37 {
38         stop();
39 }
40
41 void VineTimer::start(unsigned int timeout_ms,
42         std::function<void(void *user_data)> callback, void *user_data)
43 {
44         __timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
45         if (__timer_fd == -1) {
46                 VINE_LOGE("timerfd_create fails %d", errno);
47                 return;
48         }
49
50         __timer_cb = callback;
51         __data = user_data;
52
53         struct itimerspec value;
54         value.it_interval.tv_sec = 0;
55         value.it_interval.tv_nsec = 0;
56         value.it_value.tv_sec = timeout_ms / 1000;
57         value.it_value.tv_nsec = (timeout_ms % 1000) * 1000000;
58         if (timerfd_settime(__timer_fd, 0, &value, NULL) != 0) {
59                 VINE_LOGE("timerfd_settime fails %d", errno);
60                 close(__timer_fd);
61                 return;
62         }
63
64         vine_event_loop_add_io_handler(__timer_fd, VINE_POLLIN, __timer_handler, this);
65         VINE_LOGI("Start VineTimer[%p] timer_fd[%d]", this, __timer_fd);
66 }
67
68 void VineTimer::stop()
69 {
70         VINE_LOGI("Stop VineTimer[%p] timer_fd[%d]", this, __timer_fd);
71         if (__timer_fd != -1) {
72                 vine_event_loop_del_io_handler(__timer_fd);
73                 close(__timer_fd);
74                 __timer_fd = -1;
75         }
76 }
77
78 void VineTimer::expired()
79 {
80         VINE_LOGI("VineTimer[%p] is expired. timer_fd[%d]", this, __timer_fd);
81         close(__timer_fd);
82         __timer_fd = -1;
83         __timer_cb(__data);
84 }
85
86 bool VineTimer::is_running()
87 {
88         return __timer_fd >= 0;
89 }
90
91 void VineTimer::__timer_handler(int fd, int events, void *user_data)
92 {
93         VINE_LOGD("+");
94
95         VineTimer *timer = (VineTimer *)user_data;
96         timer->expired();
97 }