Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / utility / fw_timer.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "utility/fw_list.h"
19 #include "utility/fw_timer.h"
20 #include "utility/sync_util.h"
21
22 #ifndef EXPORT_API
23 #define EXPORT_API __attribute__ ((visibility("default")))
24 #endif
25
26 #ifndef SYNC_AGENT_LOG
27 #undef LOG_TAG
28 #define LOG_TAG "AF_TIMER"
29 #endif
30
31 #include <string.h>
32 #include <stdlib.h>
33
34 static util_timer_element_s *_timer_element_alloc(char *label);
35 static void _timer_element_free(util_timer_element_s * element);
36 static double _calculate_diff_time(util_timer_element_s * element);
37
38 static util_timer_element_s *_timer_element_alloc(char *label)
39 {
40         _INNER_FUNC_ENTER;
41
42         util_timer_element_s *element = NULL;
43         char *dest_label = NULL;
44         if (label == NULL) {
45                 goto return_part;
46         }
47
48         element = (util_timer_element_s *) calloc(1, sizeof(util_timer_element_s));
49         if (element == NULL) {
50                 _DEBUG_ERROR("fw_timer_element alloc failed because of out of memory");
51                 goto return_part;
52         }
53         dest_label = element->label;
54
55         /* set label */
56         strncpy(dest_label, label, UTIL_TIMER_MAX_LABEL_LEN);
57
58  return_part:
59         _INNER_FUNC_EXIT;
60         return element;
61 }
62
63 static void _timer_element_free(util_timer_element_s * element)
64 {
65         _INNER_FUNC_ENTER;
66
67         if (element != NULL) {
68                 free(element);
69         }
70
71         _INNER_FUNC_EXIT;
72 }
73
74 util_timer_s *util_timer_alloc()
75 {
76         _EXTERN_FUNC_ENTER;
77
78         util_timer_s *timer = (util_timer_s *) malloc(sizeof(util_timer_s));
79
80         if (timer != NULL) {
81                 util_list_init(&(timer->element_head));
82         } else {
83                 _DEBUG_ERROR("fw_timer_alloc failed because of out of memory");
84         }
85
86         _EXTERN_FUNC_EXIT;
87
88         return timer;
89 }
90
91 static double _calculate_diff_time(util_timer_element_s * element)
92 {
93         _INNER_FUNC_ENTER;
94
95         double diff_time = 0.0;
96         double sTime = element->start_time.tv_sec + (element->start_time.tv_usec / 1000000.0);
97         double fTime = element->finish_time.tv_sec + (element->finish_time.tv_usec / 1000000.0);
98         diff_time = fTime - sTime;
99
100         _INNER_FUNC_EXIT;
101
102         return diff_time;
103 }
104
105 util_timer_error_e util_timer_start(util_timer_s * timer, char *label)
106 {
107         _EXTERN_FUNC_ENTER;
108
109         util_timer_error_e err = UTIL_TIMER_OK;
110         if (timer == NULL) {
111                 err = UTIL_TIMER_INVALID_TIMER;
112                 goto return_part;
113         }
114
115         util_timer_element_s *element = _timer_element_alloc(label);
116         if (element == NULL) {
117                 err = UTIL_TIMER_OUT_OF_MEMORY;
118                 goto return_part;
119         }
120
121         /* set timer start time */
122         if (-1 == gettimeofday(&(element->start_time), NULL)) {
123                 err = UTIL_TIMER_GETTIME_ERROR;
124                 _timer_element_free(element);
125                 goto return_part;
126         }
127
128         /* add list */
129         util_list_add_node((util_list_node_s *) element, &(timer->element_head));
130
131  return_part:
132         _EXTERN_FUNC_EXIT;
133         return err;
134 }
135
136 util_timer_error_e util_timer_finish(util_timer_s * timer, char *label, double *passed_sec)
137 {
138         _EXTERN_FUNC_ENTER;
139
140         util_timer_error_e err = UTIL_TIMER_OK;
141         util_list_node_s *iter = NULL;
142         util_list_node_s *head = &(timer->element_head);
143         util_timer_element_s *element = NULL;
144
145         /* find list node */
146         UTIL_LIST_ITER(iter, head) {
147                 element = (util_timer_element_s *) iter;
148
149                 if (strncmp(label, element->label, UTIL_TIMER_MAX_LABEL_LEN) != 0) {
150                         continue;
151                 }
152
153                 if (-1 == gettimeofday(&(element->finish_time), NULL)) {
154                         err = UTIL_TIMER_GETTIME_ERROR;
155                         goto return_part;
156                 } else {
157                         /* calcuate passed_sec */
158                         if (passed_sec != NULL) {
159                                 *passed_sec = _calculate_diff_time(element);
160                         }
161
162                         break;
163                 }
164         }
165
166  return_part:
167         _EXTERN_FUNC_EXIT;
168         return err;
169 }
170
171 /* TODO */
172 util_timer_error_e util_timer_print(util_timer_s * timer)
173 {
174         _EXTERN_FUNC_ENTER;
175
176         util_timer_error_e err = UTIL_TIMER_OK;
177
178         _EXTERN_FUNC_EXIT;
179
180         return err;
181 }
182
183 /* TODO */
184 util_timer_error_e util_timer_free(util_timer_s * timer)
185 {
186         _EXTERN_FUNC_ENTER;
187
188         util_timer_error_e err = UTIL_TIMER_OK;
189
190         _EXTERN_FUNC_EXIT;
191
192         return err;
193 }