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