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