Sending lap time to cloud
[apps/native/gear-racing-car.git] / src / cloud / lap_info.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 <stdbool.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <glib.h>
21 #include <string.h>
22 #include <regex.h>
23 #include <arpa/inet.h>
24 #include <ctype.h>
25 #include "log.h"
26 #include "cloud/lap_info.h"
27
28 #define MAX_CAR_ID_LENGTH 256
29
30 struct lap_info
31 {
32         char *user_name;
33         char *car_id;
34         long lap_time;
35 };
36
37 lap_info_t *lap_info_create(void)
38 {
39         struct lap_info *lap_info = g_new0(struct lap_info, 1);
40         retvm_if(!lap_info, NULL, "Could not allocate memory!");
41
42         return lap_info;
43 }
44
45 lap_info_t *lap_info_copy(const lap_info_t *lap_info)
46 {
47         retv_if(!lap_info, NULL);
48
49         struct lap_info *lap_info_cpy = g_new0(struct lap_info, 1);
50         lap_info_cpy->user_name = strndup(lap_info->user_name, MAX_CAR_ID_LENGTH);
51         lap_info_cpy->car_id = strndup(lap_info->car_id, MAX_CAR_ID_LENGTH);
52         lap_info_cpy->lap_time = lap_info->lap_time;
53
54         return lap_info_cpy;
55 }
56
57 void lap_info_destroy(lap_info_t *lap_info)
58 {
59         ret_if(!lap_info);
60
61         free(lap_info->user_name);
62         free(lap_info->car_id);
63         g_free(lap_info);
64 }
65
66 const char *lap_info_get_car_id(lap_info_t *lap_info)
67 {
68         retv_if(!lap_info, NULL);
69         return lap_info->car_id;
70 }
71
72 int lap_info_set_car_id(lap_info_t *lap_info, const char *car_id)
73 {
74         retv_if(!lap_info, -1);
75         retv_if(car_id == NULL, -1);
76         retv_if(strlen(car_id) >= MAX_CAR_ID_LENGTH, -1);
77
78         if (!lap_info->car_id)
79                 lap_info->car_id = (char *)g_malloc(MAX_CAR_ID_LENGTH * sizeof(char));
80
81         snprintf(lap_info->car_id, MAX_CAR_ID_LENGTH, "%s", car_id);
82         return 0;
83 }
84
85 const char *lap_info_get_user_name(lap_info_t *lap_info)
86 {
87         retv_if(!lap_info, NULL);
88         return lap_info->user_name;
89 }
90
91 int lap_info_set_user_name(lap_info_t *lap_info, const char *user_name)
92 {
93         retv_if(!lap_info, -1);
94         retv_if(user_name == NULL, -1);
95         retv_if(strlen(user_name) >= MAX_CAR_ID_LENGTH, -1);
96
97         if (!lap_info->user_name)
98                 lap_info->user_name = (char *)g_malloc(MAX_CAR_ID_LENGTH * sizeof(char));
99
100         snprintf(lap_info->user_name, MAX_CAR_ID_LENGTH, "%s", user_name);
101         return 0;
102 }
103
104 long lap_info_get_lap_time(lap_info_t *lap_info)
105 {
106         retv_if(!lap_info, 0);
107         return lap_info->lap_time;
108 }
109
110 int lap_info_set_lap_time(lap_info_t *lap_info, long lap_time)
111 {
112         retv_if(!lap_info, -1);
113         lap_info->lap_time = lap_time;
114         return 0;
115 }