Add Camera Module
[apps/native/position-finder-server.git] / src / controller.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Flora License, Version 1.1 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://floralicense.org/license/
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <unistd.h>
23 #include <glib.h>
24 #include <Ecore.h>
25 #include <tizen.h>
26 #include <service_app.h>
27
28 #include "log.h"
29 #include "resource.h"
30 #include "connectivity.h"
31 #include "controller.h"
32 #include "controller_util.h"
33 #include "webutil.h"
34
35 #define CONNECTIVITY_KEY "opened"
36 #define SENSORING_TIME_INTERVAL 5.0f
37 #define CAMERA_TIME_INTERVAL 2
38 #define TEST_CAMERA_SAVE 0
39 #define CAMERA_ENABLED 0
40
41 typedef struct app_data_s {
42         Ecore_Timer *getter_timer;
43         connectivity_resource_s *resource_info;
44 } app_data;
45
46 static void __resource_camera_capture_completed_cb(const void *image, unsigned int size, void *user_data)
47 {
48         /* TODO */
49         const char *path = NULL;
50         const char *url = NULL;
51
52         controller_util_get_path(&path);
53
54         controller_util_get_image_address(&url);
55
56         web_util_noti_post_image_data(url, path, image, size);
57
58 #if TEST_CAMERA_SAVE
59         FILE *fp = NULL;
60         char *data_path = NULL;
61         char file[256];
62
63         data_path = app_get_data_path();
64
65         snprintf(file, sizeof(file), "%sjjoggoba.jpg", data_path);
66         free(data_path);
67         _D("File : %s", file);
68
69         fp = fopen(file, "w");
70         if (!fp) {
71                 _E("Failed to open file: %s", file);
72                 return;
73         }
74
75         if (fwrite(image, size, 1, fp) != 1) {
76                 _E("Failed to write image to file");
77                 return;
78         }
79
80         fclose(fp);
81 #endif
82 }
83
84 static Eina_Bool control_sensors_cb(void *data)
85 {
86         app_data *ad = data;
87         int value = 1;
88         int ret = 0;
89 #if CAMERA_ENABLED
90         static unsigned int count = 0;
91
92         if (count % CAMERA_TIME_INTERVAL == 0) {
93                 ret = resource_capture_camera(__resource_camera_capture_completed_cb, NULL);
94                 if (ret < 0)
95                         _E("Failed to capture camera");
96         }
97
98         count++;
99 #endif
100
101         /* This is example, get value from sensors first */
102         if (connectivity_notify_int(ad->resource_info, "Motion", value) == -1)
103                 _E("Cannot notify message");
104
105         return ECORE_CALLBACK_RENEW;
106 }
107
108 static bool service_app_create(void *data)
109 {
110         app_data *ad = data;
111         int ret = -1;
112         const char *path = NULL;
113
114         /**
115          * No modification required!!!
116          * Access only when modifying internal functions.
117          */
118         controller_init_internal_functions();
119
120         /**
121          * Create a connectivity resource and registers the resource in server.
122          */
123         connectivity_set_protocol(CONNECTIVITY_PROTOCOL_HTTP);
124
125         controller_util_get_path(&path);
126         if (path == NULL) {
127                 _E("Failed to get path");
128                 return false;
129         }
130
131         ret = connectivity_set_resource(path, "org.tizen.door", &ad->resource_info);
132         if (ret == -1) _E("Cannot broadcast resource");
133
134         /**
135          * Creates a timer to call the given function in the given period of time.
136          * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
137          */
138         ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, control_sensors_cb, ad);
139         if (!ad->getter_timer) {
140                 _E("Failed to add infrared motion getter timer");
141                 return false;
142         }
143
144         return true;
145 }
146
147 static void service_app_terminate(void *data)
148 {
149         app_data *ad = (app_data *)data;
150
151         if (ad->getter_timer)
152                 ecore_timer_del(ad->getter_timer);
153
154
155         /**
156          * Releases the resource about connectivity.
157          */
158         connectivity_unset_resource(ad->resource_info);
159
160         /**
161          * No modification required!!!
162          * Access only when modifying internal functions.
163          */
164         controller_fini_internal_functions();
165
166         free(ad);
167 }
168
169 static void service_app_control(app_control_h app_control, void *data)
170 {
171         // Todo: add your code here.
172 }
173
174 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
175 {
176         /*APP_EVENT_LANGUAGE_CHANGED*/
177 }
178
179 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
180 {
181         /*APP_EVENT_REGION_FORMAT_CHANGED*/
182 }
183
184 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
185 {
186         /*APP_EVENT_LOW_BATTERY*/
187 }
188
189 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
190 {
191         /*APP_EVENT_LOW_MEMORY*/
192 }
193
194 int main(int argc, char* argv[])
195 {
196         app_data *ad = NULL;
197         int ret = 0;
198         service_app_lifecycle_callback_s event_callback;
199         app_event_handler_h handlers[5] = {NULL, };
200
201         ad = calloc(1, sizeof(app_data));
202         retv_if(!ad, -1);
203
204         event_callback.create = service_app_create;
205         event_callback.terminate = service_app_terminate;
206         event_callback.app_control = service_app_control;
207
208         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
209         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
210         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
211         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
212
213         ret = service_app_main(argc, argv, &event_callback, ad);
214
215         return ret;
216 }