634773e88c07a3215d4d0b3fb46ac19e5bfec5cf
[apps/native/gear-racing-car.git] / src / cloud / cloud_communication.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 "cloud/cloud_communication.h"
18 #include <glib.h>
19 #include <wifi-manager.h>
20 #include <stdlib.h>
21 #include "cloud/car_info.h"
22 #include "cloud/cloud_request.h"
23 #include "cloud/cloud_lap_request.h"
24 #include "cloud/lap_info.h"
25 #include "log.h"
26 #include "config.h"
27 #include "net-util.h"
28
29 typedef struct communication_data_ {
30     gboolean is_initialized;
31     gboolean is_running;
32     car_info_t *car_info;
33     guint source_id;
34 } communication_data_t;
35
36 static communication_data_t _communication;
37
38 static void post_response_cb(request_result_e result, void *user_data);
39 static gboolean post_timer_cb(gpointer data);
40 static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_addr, void *user_data);
41
42 static int set_car_id();
43 static int set_car_ip();
44 static int set_car_name();
45 static int set_ap_mac();
46 static int set_ap_ssid();
47
48 int cloud_communication_init()
49 {
50     retvm_if(_communication.is_initialized, -1, "Cloud communication is already initialized");
51     _communication.car_info = car_info_create();
52
53     if (set_car_id() != 0) {
54         return -1;
55     }
56     if (set_car_ip() != 0) {
57         return -1;
58     }
59     if (set_car_name() != 0) {
60         return -1;
61     }
62     if (set_ap_mac() != 0) {
63         return -1;
64     }
65     if (set_ap_ssid() != 0) {
66         return -1;
67     }
68
69     net_util_set_wifi_connection_changed_cb(wifi_changed_cb, NULL);
70
71     _communication.is_initialized = true;
72     return 0;
73 }
74
75 void cloud_communication_start(int interval)
76 {
77     retm_if(!_communication.is_initialized, "Cloud communication is not initialized");
78     retm_if(_communication.is_running, "Cloud communication is already running");
79
80     _communication.source_id = g_timeout_add_seconds(interval, post_timer_cb, _communication.car_info);
81     _communication.is_running = TRUE;
82 }
83
84 void cloud_communication_stop()
85 {
86     retm_if(!_communication.is_initialized, "Cloud communication is not initialized");
87     retm_if(_communication.is_running, "Cloud communication is already stopped");
88
89     g_source_remove(_communication.source_id);
90     _communication.is_running = FALSE;
91 }
92
93 void cloud_communication_fini()
94 {
95     retm_if(!_communication.is_initialized, "Cloud communication is already finalized");
96
97     cloud_communication_stop();
98     car_info_destroy(_communication.car_info);
99 }
100
101
102 void cloud_communication_post_lap(const long laptime, const char *driver_name)
103 {
104         lap_info_t *lap = lap_info_create();
105
106         lap_info_set_car_id(lap, car_info_get_car_id(_communication.car_info));
107         lap_info_set_user_name(lap, driver_name);
108         lap_info_set_lap_time(lap,  laptime);
109
110         _D("POST lap");
111         cloud_lap_request_api_racing_post(lap, (cloud_request_lap_post_finish_cb)post_response_cb, NULL);
112
113         lap_info_destroy(lap);
114 }
115
116 static void post_response_cb(request_result_e result, void *user_data)
117 {
118     if (result == SUCCESS) {
119         _I("POST SUCCESS");
120     }
121     else {
122         _I("POST FAILURE");
123     }
124 }
125
126 static gboolean post_timer_cb(gpointer data)
127 {
128     retv_if(!data, FALSE);
129     car_info_t *car = (car_info_t *)data;
130     cloud_request_api_racing_post(car, post_response_cb, NULL);
131     return TRUE;
132 }
133
134 static int set_car_id()
135 {
136     char *id = NULL;
137     int ret = 0;
138     ret = config_get_string("Car", "Id", &id);
139     if (ret != 0) {
140         _E("Getting car ID from config failed!");
141         return -1;
142     }
143
144     car_info_set_car_id(_communication.car_info, id);
145     g_free(id);
146     return 0;
147 }
148
149 static int set_car_ip()
150 {
151     char *ip;
152     int ret = net_util_get_ip_addr(&ip);
153     if (ret != 0) {
154         return -1;
155     }
156     car_info_set_car_ip(_communication.car_info, ip);
157     g_free(ip);
158     return 0;
159 }
160
161 static int set_car_name()
162 {
163     char *name;
164     int ret = 0;
165
166     ret = config_get_string("Car", "Name", &name);
167     if (ret != 0) {
168         _E("Getting car name from config failed!");
169         return -1;
170     }
171     car_info_set_car_name(_communication.car_info, name);
172     g_free(name);
173     return 0;
174 }
175
176 static int set_ap_mac()
177 {
178     char *mac;
179     int ret = net_util_get_ap_mac(&mac);
180     if (ret != 0) {
181         return -1;
182     }
183     car_info_set_car_ap_mac(_communication.car_info, mac);
184     g_free(mac);
185     return 0;
186 }
187
188 static int set_ap_ssid()
189 {
190     char *ssid;
191     int ret = net_util_get_ap_ssid(&ssid);
192     if (ret != 0) {
193         return -1;
194     }
195     car_info_set_ap_ssid(_communication.car_info, ssid);
196     g_free(ssid);
197     return 0;
198 }
199
200 static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_addr, void *user_data)
201 {
202     car_info_set_car_ap_mac(_communication.car_info, ap_mac);
203     car_info_set_ap_ssid(_communication.car_info, ap_ssid);
204     car_info_set_car_ip(_communication.car_info, ip_addr);
205 }