e548ca3cb576647ed73f8088c43be8766d1fc2ad
[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 <unistd.h>
21 #include <glib.h>
22 #include <service_app.h>
23 #include "log.h"
24 #include "dc_motor.h"
25
26 typedef struct app_data_s {
27         guint timer_id;
28 } app_data;
29
30 static void service_app_control(app_control_h app_control, void *data)
31 {
32         return;
33 }
34
35 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
36 {
37         return;
38 }
39
40 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
41 {
42         return;
43 }
44
45 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
46 {
47         return;
48 }
49
50 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
51 {
52         return;
53 }
54
55 static inline int __get_r_val(int val)
56 {
57         if (val > 0)
58                 return 0 - val;
59         else
60                 return ABS(val);
61 }
62
63 static gboolean __control_dcmotor_cb(gpointer user_data)
64 {
65         static int value = -2000;
66         int value2 = 0;
67
68         value = __get_r_val(value);
69
70         dc_motor_speed_set(DC_MOTOR_ID_L, 0);
71         dc_motor_speed_set(DC_MOTOR_ID_R, 0);
72
73         sleep(1);
74
75         dc_motor_speed_set(DC_MOTOR_ID_L, value);
76         dc_motor_speed_set(DC_MOTOR_ID_R, value);
77
78         sleep(5);
79         if (value > 0)
80                 value2 = value + 1000;
81         else
82                 value2 = value - 1000;
83
84         dc_motor_speed_set(DC_MOTOR_ID_L, value2);
85         dc_motor_speed_set(DC_MOTOR_ID_R, value2);
86
87         return G_SOURCE_CONTINUE;
88 }
89
90 static bool service_app_create(void *data)
91 {
92         app_data *ad = data;
93         int ret = 0;
94         ret = dc_motor_init();
95         if (ret) {
96                 _E("failed init motor, terminating this application");
97                 service_app_exit();
98         }
99
100         ad->timer_id = g_timeout_add_seconds(10, __control_dcmotor_cb, ad);
101
102         return true;
103 }
104
105 static void service_app_terminate(void *data)
106 {
107         app_data *ad = data;
108
109         if (ad->timer_id) {
110                 g_source_remove(ad->timer_id);
111                 ad->timer_id = 0;
112         }
113
114         dc_motor_fini();
115         log_file_close();
116
117         _D("Bye ~");
118
119         return;
120 }
121
122 int main(int argc, char* argv[])
123 {
124         app_data *ad = NULL;
125         int ret = 0;
126         service_app_lifecycle_callback_s event_callback;
127         app_event_handler_h handlers[5] = {NULL, };
128
129         log_type_set(LOG_TYPE_DLOG);
130
131         ad = calloc(1, sizeof(app_data));
132         retv_if(!ad, -1);
133
134         event_callback.create = service_app_create;
135         event_callback.terminate = service_app_terminate;
136         event_callback.app_control = service_app_control;
137
138         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
139                 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
140         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
141                 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
142         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
143                 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
144         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
145                 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
146
147         ret = service_app_main(argc, argv, &event_callback, ad);
148         if (ret)
149                 _E("failed to start app");
150
151         return 0;
152 }
153