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