2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
17 #include "lap_counter/lap_counter.h"
25 #define MIN_LAP_TIME 5
26 #define MAX_NAME_LENGTH 256
28 typedef struct lap_counter_data {
29 const char *user_name;
30 struct timespec last_timestamp;
33 static lap_counter_data_t s_info = {0, };
35 void lap_counter_init()
37 char buf[MAX_NAME_LENGTH];
38 snprintf(buf, MAX_NAME_LENGTH, "Default: %s %s", __DATE__, __TIME__); //
40 s_info.user_name = strdup(buf);
43 void lap_counter_set_user_name(const char *user_name)
45 free((void*)s_info.user_name);
46 s_info.user_name = strdup(user_name);
47 if(!s_info.user_name) {
50 _D("User name set to %s", s_info.user_name);
53 const char *lap_counter_get_user_name(void)
55 return s_info.user_name;
58 void _print_time(const char *title, struct timespec *ts)
61 struct tm *_tm = localtime(&(ts->tv_sec));
62 strftime(&buf[0], PATH_MAX, "%H:%M:%S", _tm);
64 snprintf(&buf[strlen(buf)], PATH_MAX, ".%03ld", ts->tv_nsec / (long)1e6);
66 _D ("%s %s", title, buf);
69 static inline struct timespec _calculate_lap_time(struct timespec *prev, struct timespec *now)
73 lap.tv_sec = now->tv_sec - prev->tv_sec;
74 lap.tv_nsec = now->tv_nsec - prev->tv_nsec;
77 _D("----------------------------------------------");
78 _print_time("PREV:\t", prev);
79 _print_time("NOW:\t", now);
81 if (lap.tv_sec < MIN_LAP_TIME) {
89 _print_time("LAP:\t", &lap);
91 if (lap.tv_nsec < 0) {
93 lap.tv_nsec = 1e9 + lap.tv_nsec;
95 _print_time("LAP_MOD:\t", &lap);
97 _D("----------------------------------------------");
102 void lap_counter_get_lap_time()
104 struct timespec timestamp;
105 int ret = clock_gettime(CLOCK_MONOTONIC, ×tamp);
106 retv_error_message(ret != 0, ret);
108 if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) {
109 _calculate_lap_time(&s_info.last_timestamp, ×tamp);
114 s_info.last_timestamp.tv_sec = timestamp.tv_sec;
115 s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
118 void lap_counter_shutdown()
120 free(s_info.user_name);