dfbd2991e46de1dd338f89f9821edbfbbe011039
[apps/native/gear-racing-car.git] / src / lap_counter / lap_counter.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
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 #include "lap_counter/lap_counter.h"
18 #include <log.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <time.h>
24
25 #define MIN_LAP_TIME 5
26 #define MAX_NAME_LENGTH 256
27
28 typedef struct lap_counter_data {
29         const char *user_name;
30         struct timespec last_timestamp;
31 } lap_counter_data_t;
32
33 static lap_counter_data_t s_info = {0, };
34
35 void lap_counter_init()
36 {
37         char buf[MAX_NAME_LENGTH];
38         snprintf(buf, MAX_NAME_LENGTH, "Default: %s %s", __DATE__, __TIME__); //
39
40         s_info.user_name = strdup(buf);
41 }
42
43 void lap_counter_set_user_name(const char *user_name)
44 {
45         free((void*)s_info.user_name);
46         s_info.user_name = strdup(user_name);
47         if(!s_info.user_name) {
48                 return;
49         }
50         _D("User name set to %s", s_info.user_name);
51 }
52
53 const char *lap_counter_get_user_name(void)
54 {
55         return s_info.user_name;
56 }
57
58 void _print_time(const char *title, struct timespec *ts)
59 {
60         char buf[PATH_MAX];
61         struct tm *_tm = localtime(&(ts->tv_sec));
62         strftime(&buf[0], PATH_MAX, "%H:%M:%S", _tm);
63
64         snprintf(&buf[strlen(buf)], PATH_MAX, ".%03ld", ts->tv_nsec / (long)1e6);
65
66         _D ("%s %s", title, buf);
67 }
68
69 static inline struct timespec _calculate_lap_time(struct timespec *prev, struct timespec *now)
70 {
71         struct timespec lap;
72
73         lap.tv_sec = now->tv_sec - prev->tv_sec;
74         lap.tv_nsec = now->tv_nsec - prev->tv_nsec;
75
76
77         _D("----------------------------------------------");
78         _print_time("PREV:\t", prev);
79         _print_time("NOW:\t", now);
80
81         if (lap.tv_sec < MIN_LAP_TIME) {
82                 lap.tv_sec = 0;
83                 lap.tv_nsec = 0;
84                 _D ("TOO SHORT LAP");
85                 return lap;
86         }
87
88
89         _print_time("LAP:\t", &lap);
90
91         if (lap.tv_nsec < 0) {
92                 lap.tv_sec--;
93                 lap.tv_nsec = 1e9 + lap.tv_nsec;
94
95                 _print_time("LAP_MOD:\t", &lap);
96         }
97         _D("----------------------------------------------");
98
99         return lap;
100 }
101
102 void lap_counter_get_lap_time()
103 {
104         struct timespec timestamp;
105         int ret = clock_gettime(CLOCK_MONOTONIC, &timestamp);
106         retv_error_message(ret != 0, ret);
107
108         if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) {
109                 _calculate_lap_time(&s_info.last_timestamp, &timestamp);
110         } else {
111                 _D("Initial lap");
112         }
113
114         s_info.last_timestamp.tv_sec = timestamp.tv_sec;
115         s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
116 }
117
118 void lap_counter_shutdown()
119 {
120         free(s_info.user_name);
121 }