1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Flora License, Version 1.1 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cloud/cloud_communication.h"
#include <glib.h>
#include <wifi-manager.h>
#include <stdlib.h>
#include "cloud/car_info.h"
#include "cloud/cloud_request.h"
#include "cloud/cloud_lap_request.h"
#include "cloud/lap_info.h"
#include "log.h"
#include "config.h"
#include "net-util.h"
#include "resource/resource_led.h"
#define CONFIG_LED_STATE_KEY_READY "ready"
#define CONFIG_DEFAULT_LED_3BIT_READY 1, 0, 1
#define CONFIG_DEFAULT_LED_24BIT_READY 215, 15, 96
#define CONFIG_LED_STATE_KEY_FAIL "fail"
#define CONFIG_DEFAULT_LED_3BIT_FAIL 1, 0, 0
#define CONFIG_DEFAULT_LED_24BIT_FAIL 255, 0, 0
typedef struct communication_data_ {
gboolean is_initialized;
gboolean is_running;
gboolean is_connected;
car_info_t *car_info;
guint source_id;
} communication_data_t;
static communication_data_t _communication;
static void post_response_cb(request_result_e result, void *user_data);
static gboolean post_timer_cb(gpointer data);
static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_addr, void *user_data);
static int set_car_id();
static int set_car_ip();
static int set_car_name();
static int set_ap_mac();
static int set_ap_ssid();
int cloud_communication_init()
{
retvm_if(_communication.is_initialized, -1, "Cloud communication is already initialized");
_communication.car_info = car_info_create();
set_car_id();
set_car_ip();
set_car_name();
set_ap_mac();
set_ap_ssid();
net_util_set_wifi_connection_changed_cb(wifi_changed_cb, NULL);
_communication.is_connected = car_info_get_ap_mac(_communication.car_info) && car_info_get_ap_ssid(_communication.car_info);
_communication.is_initialized = true;
return 0;
}
void cloud_communication_start(int interval)
{
retm_if(!_communication.is_initialized, "Cloud communication is not initialized");
retm_if(_communication.is_running, "Cloud communication is already running");
_communication.source_id = g_timeout_add_seconds(interval, post_timer_cb, _communication.car_info);
_communication.is_running = TRUE;
}
void cloud_communication_stop()
{
retm_if(!_communication.is_initialized, "Cloud communication is not initialized");
retm_if(!_communication.is_running, "Cloud communication is already stopped");
g_source_remove(_communication.source_id);
_communication.is_running = FALSE;
}
void cloud_communication_fini()
{
retm_if(!_communication.is_initialized, "Cloud communication is already finalized");
cloud_communication_stop();
car_info_destroy(_communication.car_info);
}
void cloud_communication_post_lap(const long laptime, const char *driver_name)
{
retm_if(!_communication.is_connected, "Car is not connected to network");
lap_info_t *lap = lap_info_create();
lap_info_set_car_id(lap, car_info_get_car_id(_communication.car_info));
lap_info_set_user_name(lap, driver_name);
lap_info_set_lap_time(lap, laptime);
_D("POST lap");
cloud_lap_request_api_racing_post(lap, (cloud_request_lap_post_finish_cb)post_response_cb, NULL);
lap_info_destroy(lap);
}
static void post_response_cb(request_result_e result, void *user_data)
{
if (result == SUCCESS) {
_I("POST SUCCESS");
resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_READY,
CONFIG_DEFAULT_LED_3BIT_READY,
CONFIG_DEFAULT_LED_24BIT_READY,
LED_COLOR_GREEN);
}
else {
_I("POST FAILURE");
resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_FAIL,
CONFIG_DEFAULT_LED_3BIT_FAIL,
CONFIG_DEFAULT_LED_24BIT_FAIL,
LED_COLOR_RED);
}
}
static gboolean post_timer_cb(gpointer data)
{
FUNCTION_START;
retv_if(!data, FALSE);
retvm_if(!_communication.is_connected, TRUE, "Car is not connected to network");
car_info_t *car = (car_info_t *)data;
cloud_request_api_racing_post(car, post_response_cb, NULL);
FUNCTION_END;
return TRUE;
}
static int set_car_id()
{
char *id = NULL;
int ret = 0;
ret = config_get_string("Car", "Id", &id);
if (ret != 0) {
_E("Getting car ID from config failed!");
return -1;
}
car_info_set_car_id(_communication.car_info, id);
g_free(id);
return 0;
}
static int set_car_ip()
{
char *ip;
int ret = net_util_get_ip_addr(&ip);
if (ret != 0) {
return -1;
}
car_info_set_car_ip(_communication.car_info, ip);
g_free(ip);
return 0;
}
static int set_car_name()
{
char *name;
int ret = 0;
ret = config_get_string("Car", "Name", &name);
if (ret != 0) {
_E("Getting car name from config failed!");
return -1;
}
car_info_set_car_name(_communication.car_info, name);
g_free(name);
return 0;
}
static int set_ap_mac()
{
char *mac;
int ret = net_util_get_ap_mac(&mac);
if (ret != 0) {
return -1;
}
car_info_set_car_ap_mac(_communication.car_info, mac);
g_free(mac);
return 0;
}
static int set_ap_ssid()
{
char *ssid;
int ret = net_util_get_ap_ssid(&ssid);
if (ret != 0) {
return -1;
}
car_info_set_ap_ssid(_communication.car_info, ssid);
g_free(ssid);
return 0;
}
static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_addr, void *user_data)
{
car_info_set_car_ap_mac(_communication.car_info, ap_mac);
car_info_set_ap_ssid(_communication.car_info, ap_ssid);
car_info_set_car_ip(_communication.car_info, ip_addr);
_communication.is_connected = ap_mac && ap_ssid;
if (_communication.is_connected) {
resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_READY,
CONFIG_DEFAULT_LED_3BIT_READY,
CONFIG_DEFAULT_LED_24BIT_READY,
LED_COLOR_GREEN);
} else {
resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_FAIL,
CONFIG_DEFAULT_LED_3BIT_FAIL,
CONFIG_DEFAULT_LED_24BIT_FAIL,
LED_COLOR_RED);
}
}
|