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
|
/*
* 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 "model/model_cloud_connection.h"
#include <glib.h>
#include "gear-racing-controller.h"
#include "cloud/cloud_request.h"
#include "net-util.h"
#include "log.h"
typedef struct _s_cloud_model_connection {
t_model_cloud_connection_update_cb controller_update_cb;
t_model_cloud_connection_update_cb reconnect_cb;
char player_name[PLAYER_NAME_MAX_LEN + 1];
car_info_t **cars;
int cars_size;
gboolean is_initialized;
} s_model_cloud_connection;
s_model_cloud_connection model_cloud_connection = { 0, };
static void _data_received(request_result_e result, car_info_t **car_info, int size, void *user_data);
static void _save_cars(car_info_t **car_info, int size);
static void _release_cars();
void model_cloud_connection_init(void)
{
if(model_cloud_connection.is_initialized) {
_E("Model cloud connection has already been initialized!");
return;
}
model_cloud_connection.is_initialized = true;
}
void model_cloud_connection_subscribe_event(t_model_cloud_connection_update_cb model_update_cb)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
model_cloud_connection.controller_update_cb = model_update_cb;
}
void model_cloud_connection_model_state_change(void)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
static s_model_cloud_connection_cb_data model_data = {
.type = MODEL_TYPE_END,
};
model_cloud_connection.controller_update_cb(&model_data);
}
void model_cloud_connection_unsubscirbe_event(void)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
model_cloud_connection.controller_update_cb = NULL;
model_cloud_connection.reconnect_cb = NULL;
}
bool model_cloud_connection_player_name_set(const char *player_name)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return false;
}
strncpy(model_cloud_connection.player_name, player_name, PLAYER_NAME_MAX_LEN);
//In case of failure, return 0
return true;
}
void model_cloud_connection_subscribe_reconnect(t_model_cloud_connection_update_cb model_update_cb)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
model_cloud_connection.reconnect_cb = model_update_cb;
}
void model_cloud_connection_try_reconnect(void)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
static s_model_cloud_connection_cb_data model_data = {
.type = MODEL_TYPE_END,
};
//TODO: Reconnect processing
if (model_cloud_connection.reconnect_cb) {
model_cloud_connection.reconnect_cb(&model_data);
}
}
void model_cloud_connection_start_get_names(t_model_cloud_connection_names_cb callback)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
char *mac;
int ret = net_util_get_ap_mac(&mac);
if(ret != 0) {
_E("Couldn't receive Access point's MAC");
callback(NULL, -1);
return;
}
_D("Access point's MAC: %s", mac);
cloud_request_api_racing_get(mac, _data_received, callback);
free(mac);
}
car_info_t *model_cloud_connection_get_car_by_id(int id)
{
if(!model_cloud_connection.cars) {
_E("No cars stored!");
return NULL;
}
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return NULL;
}
if(id >= model_cloud_connection.cars_size || id < 0) {
return NULL;
}
return model_cloud_connection.cars[id];
}
void model_cloud_connection_shutdown(void)
{
if(!model_cloud_connection.is_initialized) {
_E("Model cloud connection hasn't been initialized");
return;
}
_release_cars();
model_cloud_connection_unsubscirbe_event();
model_cloud_connection.is_initialized = false;
}
static void _data_received(request_result_e result, car_info_t **car_info, int size, void *user_data)
{
t_model_cloud_connection_names_cb callback = (t_model_cloud_connection_names_cb) user_data;
if(result != SUCCESS) {
callback(NULL, -1);
return;
}
_release_cars();
_D("Received %d cars", size);
_save_cars(car_info, size);
if(callback) {
callback(model_cloud_connection.cars, model_cloud_connection.cars_size);
}
}
static void _save_cars(car_info_t **car_info, int size)
{
model_cloud_connection.cars = (car_info_t**) malloc(sizeof(car_info_t*)*size);
for(int i = 0; i < size; ++i) {
model_cloud_connection.cars[i] = car_info_copy(car_info[i]);
}
model_cloud_connection.cars_size = size;
}
static void _release_cars()
{
if(model_cloud_connection.cars) {
for(int i = 0; i < model_cloud_connection.cars_size; ++i) {
car_info_destroy(model_cloud_connection.cars[i]);
}
free(model_cloud_connection.cars);
model_cloud_connection.cars = NULL;
model_cloud_connection.cars_size = 0;
}
}
|