Cleanup unused functions and modules
[apps/native/gear-racing-car.git] / src / app.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jeonghoon Park <jh1979.park@samsung.com>
5  *
6  * Licensed under the Flora License, Version 1.1 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://floralicense.org/license/
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <math.h>
23 #include <glib.h>
24 #include <service_app.h>
25 #include "log.h"
26 #include "resource.h"
27 #include "net-util.h"
28 #include "config.h"
29 #include "cloud/cloud_communication.h"
30 #include "messages/message_manager.h"
31 #include "controller_connection_manager.h"
32 #include "command.h"
33
34 #define ENABLE_MOTOR 1
35
36 #define CONFIG_GRP_CAR "Car"
37 #define CONFIG_KEY_ID "Id"
38 #define CONFIG_KEY_NAME "Name"
39 #define CLOUD_REQUESTS_FREQUENCY 15
40
41 enum {
42         DIR_STATE_S,
43         DIR_STATE_F,
44         DIR_STATE_B,
45 };
46
47 typedef struct app_data_s {
48         unsigned int f_value;
49         unsigned int r_value;
50         unsigned int dir_state;
51         guint idle_h;
52 } app_data;
53
54 static void _initialize_components(app_data *ad);
55 static void _initialize_config();
56
57 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
58 {
59         return;
60 }
61
62 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
63 {
64         return;
65 }
66
67 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
68 {
69         _E("low battery! exit app");
70         service_app_exit();
71
72         return;
73 }
74
75 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
76 {
77         return;
78 }
79
80 static inline double __map_round(double val)
81 {
82         return floor(val + 0.5);
83 }
84
85 static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
86 {
87         int rval = 0;
88         double slope = 0;
89         slope = 1.0 * (d_max - d_min) / (v_max - v_min);
90
91         rval = d_min + __map_round(slope * (val - v_min));
92
93         return rval;
94 }
95
96 static int ___map_speed_val(int speed)
97 {
98         static const int motor_max = 4095;
99         static const int motor_min = -4095;
100         static const int speed_max = 1000;
101         static const int speed_min = -1000;
102
103         return __map_range_val(motor_max, motor_min,
104                 speed_max, speed_min, speed);
105 }
106
107 static int ___map_servo_val(int servo)
108 {
109         static const int motor_max = 500;
110         static const int motor_min = 400;
111         static const int servo_max = 1000;
112         static const int servo_min = -1000;
113
114         return __map_range_val(motor_max, motor_min,
115                 servo_max, servo_min, servo);
116 }
117
118 static int __driving_motors(int servo, int speed)
119 {
120         int val_speed;
121         int val_servo;
122
123         val_servo = ___map_servo_val(servo);
124         val_speed = ___map_speed_val(speed);
125
126         _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
127                 servo, val_servo, speed, val_speed);
128 #if ENABLE_MOTOR
129         resource_set_servo_motor_value(0, val_servo);
130         resource_set_motor_driver_L298N_speed(MOTOR_ID_1, val_speed);
131         resource_set_motor_driver_L298N_speed(MOTOR_ID_2, val_speed);
132 #endif
133
134         return 0;
135 }
136
137 static void __camera(int azimuth, int elevation)
138 {
139         //TODO: Camera steering
140 }
141
142 static void __command_received_cb(command_s command) {
143         switch(command.type) {
144         case COMMAND_TYPE_DRIVE:
145                 __driving_motors(command.data.steering.direction, command.data.steering.speed);
146                 break;
147         case COMMAND_TYPE_CAMERA:
148                 __camera(command.data.camera_position.camera_azimuth, command.data.camera_position.camera_elevation);
149                 break;
150         case COMMAND_TYPE_DRIVE_AND_CAMERA:
151                 __driving_motors(command.data.steering_and_camera.direction, command.data.steering_and_camera.speed);
152                 __camera(command.data.steering_and_camera.camera_azimuth, command.data.steering_and_camera.camera_elevation);
153                 break;
154         case COMMAND_TYPE_NONE:
155                 break;
156         default:
157                 _E("Unknown command type");
158                 break;
159         }
160 }
161
162 static void _initialize_config()
163 {
164         config_init();
165
166         char *id = NULL;
167         char *name = NULL;
168         gboolean modified = false;
169         if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
170                 char *uuid = g_uuid_string_random();
171                 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
172                 g_free(uuid);
173                 modified = true;
174         }
175         if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
176                 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
177                 modified = true;
178         }
179         if (modified == true) {
180                 config_save();
181         }
182         free(id);
183         free(name);
184 }
185
186 static void _initialize_components(app_data *ad)
187 {
188         net_util_init();
189         _initialize_config();
190         cloud_communication_init();
191         message_manager_init();
192         controller_connection_manager_listen();
193 }
194
195 static bool service_app_create(void *data)
196 {
197         int ret = 0;
198         app_data *ad = data;
199
200         /*
201          * if you want to use default configuration,
202          * Do not need to call resource_set_motor_driver_L298N_configuration(),
203          *
204         */
205 #if ENABLE_MOTOR
206         ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
207         if (ret) {
208                 _E("resource_set_motor_driver_L298N_configuration()");
209                 service_app_exit();
210         }
211         ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
212         if (ret) {
213                 _E("resource_set_motor_driver_L298N_configuration()");
214                 service_app_exit();
215         }
216 #endif
217
218         _initialize_components(ad);
219         cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
220
221         controller_connection_manager_set_command_received_cb(__command_received_cb);
222
223         return true;
224 }
225
226 static void service_app_control(app_control_h app_control, void *data)
227 {
228
229 #if ENABLE_MOTOR
230         /* set speed 0, to reduce delay of initializing motor driver */
231         resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
232         resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
233         resource_set_servo_motor_value(0, 450);
234 #endif
235
236         return;
237 }
238
239 static void service_app_terminate(void *data)
240 {
241         app_data *ad = data;
242
243         if (ad->idle_h)
244                 g_source_remove(ad->idle_h);
245
246
247         controller_connection_manager_release();
248         message_manager_shutdown();
249
250         cloud_communication_stop();
251         cloud_communication_fini();
252         config_shutdown();
253         net_util_fini();
254
255         resource_close_all();
256         log_file_close();
257
258         _D("Bye ~");
259
260         return;
261 }
262
263 int main(int argc, char* argv[])
264 {
265         app_data *ad = NULL;
266         int ret = 0;
267         service_app_lifecycle_callback_s event_callback;
268         app_event_handler_h handlers[5] = {NULL, };
269
270         log_type_set(LOG_TYPE_DLOG);
271
272         ad = calloc(1, sizeof(app_data));
273         retv_if(!ad, -1);
274
275         event_callback.create = service_app_create;
276         event_callback.terminate = service_app_terminate;
277         event_callback.app_control = service_app_control;
278
279         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
280                 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
281         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
282                 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
283         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
284                 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
285         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
286                 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
287
288         ret = service_app_main(argc, argv, &event_callback, ad);
289         if (ret)
290                 _E("failed to start app");
291
292         return 0;
293 }