tizen 2.4 release
[framework/location/libslp-lbs-plugin-replay.git] / xps-plugin / src / geoclue_xps_plugin_test.c
1 /*
2  * gps-manager replay plugin
3  *
4  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7  *          Genie Kim <daejins.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <errno.h>
27 #include <sys/time.h>
28
29 #include <geoclue_plugin_intf.h>
30
31 #include "geoclue_plugin_debug.h"
32
33
34 int geoclue_plugin_test_load(void);
35 int geoclue_plugin_test_unload(void);
36 int geoclue_plugin_test_location(unsigned long period, LocationCallback cb, void *arg, void **handle);
37 int geoclue_plugin_test_cancel(void *handle, CancelCallback cb, void *arg);
38 void geoclue_plugin_test_get_offline_token(const unsigned char *key, unsigned int keyLengh, OfflineTokenCallback cb, void *arg);
39 int geoclue_plugin_test_offline_location(const unsigned char *key,
40                 unsigned int keyLength,
41                 const unsigned char *token,
42                 unsigned int tokenSize,
43                 LocationCallback cb,
44                 void *arg);
45
46 static const geoclue_plugin_interface g_geoclue_plugin_interface_test_interface = {
47         geoclue_plugin_test_load,
48         geoclue_plugin_test_unload,
49         geoclue_plugin_test_location,
50         geoclue_plugin_test_cancel,
51         geoclue_plugin_test_get_offline_token,
52         geoclue_plugin_test_offline_location
53 };
54
55 typedef struct {
56         int index;                                              /* used for handle */
57         plugin_LocationInfo *location;
58         unsigned long period;
59         LocationCallback location_cb;   /* save from location */
60         void *arg;                                              /* save from location */
61 } GeoclueXpsPluginTest;
62
63 static GeoclueXpsPluginTest *xps_plugint_test = NULL;
64
65 static gboolean update_fake_position(gpointer data)
66 {
67         GeoclueXpsPluginTest *xps_plugin = data;
68
69         if (xps_plugin) {
70                 if (xps_plugin->location) {
71                         if (xps_plugin->location->latitude < 90) {
72                                 xps_plugin->location->latitude++;
73                         } else {
74                                 xps_plugin->location->latitude = 0;
75                         }
76                         if (xps_plugin->location->longitude < 180) {
77                                 xps_plugin->location->longitude++;
78                         } else {
79                                 xps_plugin->location->longitude = 0;
80                         }
81                         if (xps_plugin->location->age < 10000) {
82                                 xps_plugin->location->age++;
83                         } else {
84                                 xps_plugin->location->age = 0;
85                         }
86                         if (xps_plugin->location->altitude < 5000) {
87                                 xps_plugin->location->altitude++;
88                         } else {
89                                 xps_plugin->location->altitude = 0;
90                         }
91                         if (xps_plugin->location->bearing < 90) {
92                                 xps_plugin->location->bearing++;
93                         } else {
94                                 xps_plugin->location->bearing = 0;
95                         }
96                         if (xps_plugin->location->hpe < 100) {
97                                 xps_plugin->location->hpe++;
98                         } else {
99                                 xps_plugin->location->hpe = 0;
100                         }
101                         if (xps_plugin->location->speed < 250) {
102                                 xps_plugin->location->speed++;
103                         } else {
104                                 xps_plugin->location->speed = 0;
105                         }
106                 }
107
108                 /* called intervals */
109                 if (xps_plugin->location_cb) {
110                         xps_plugin->location_cb(xps_plugin->arg, xps_plugin->location, NULL);
111                 }
112         }
113
114         return TRUE;
115 }
116
117 int geoclue_plugin_test_load(void)
118 {
119         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_load called");
120
121
122         /* create plugin_LocationInfo *location */
123         if (NULL == xps_plugint_test) {
124                 xps_plugint_test = g_new0(GeoclueXpsPluginTest, 1);
125                 if (NULL == xps_plugint_test) {
126                         LOG_PLUGIN(LOG_ERROR, "[ERROR] GeoclueXpsPluginTest create fail");
127                         return FALSE;
128                 } else {
129                         xps_plugint_test->index = 0;
130                         xps_plugint_test->period = 5000;        /* 5s */
131                         xps_plugint_test->location = g_new0(plugin_LocationInfo, 1);
132                         if (NULL == xps_plugint_test->location) {
133                                 LOG_PLUGIN(LOG_ERROR, "[ERROR] plugin_LocationInfo create fail");
134                                 g_free(xps_plugint_test);
135                                 return FALSE;
136                         }
137                         xps_plugint_test->location->latitude = 10;
138                         xps_plugint_test->location->longitude = 10;
139                         xps_plugint_test->location->hpe = 10;
140                         xps_plugint_test->location->altitude = 10;
141                         xps_plugint_test->location->age = 10;
142                         xps_plugint_test->location->speed = 10;
143                         xps_plugint_test->location->bearing = 10;
144                 }
145         }
146
147         /* create the timer */
148         /*g_timeout_add (xps_plugint_test->period, update_fake_position, xps_plugint_test); */
149         g_timeout_add(5000, update_fake_position, xps_plugint_test);
150
151         return TRUE;
152 }
153 int geoclue_plugin_test_unload(void)
154 {
155         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_unload called");
156
157         /* free plugin_LocationInfo *location */
158         if (xps_plugint_test) {
159                 if (xps_plugint_test->location) {
160                         g_free(xps_plugint_test->location);
161                         xps_plugint_test->location = NULL;
162                 }
163                 g_free(xps_plugint_test);
164                 xps_plugint_test = NULL;
165         }
166
167         /* kill the timer */
168         return TRUE;
169 }
170 int geoclue_plugin_test_location(unsigned long period, LocationCallback cb, void *arg, void **handle)
171 {
172         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_location called");
173
174         /* update the plugin_LocationInfo *location in the timer */
175
176         /* update handle */
177         if (xps_plugint_test) {
178                 LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_location: before set handle");
179                 xps_plugint_test->index++;
180                 gchar *tmp = g_strdup_printf("%d", xps_plugint_test->index);
181                 *handle = (void *)tmp;
182                 LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_location: after set handle, set[%s], handle[%s]", tmp, *handle);
183
184                 /* call LocationCallback */
185                 if (cb) {
186                         cb(arg, xps_plugint_test->location, NULL);
187                         xps_plugint_test->location_cb = cb;
188                         xps_plugint_test->arg = arg;
189                 }
190
191                 LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_location after call callback");
192         }
193
194         return TRUE;            /* to test online */
195         /*return FALSE;         // to test the offline */
196 }
197 int geoclue_plugin_test_cancel(void *handle, CancelCallback cb, void *arg)
198 {
199         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_cancel called");
200         /* check handle */
201         if (handle) {
202                 LOG_PLUGIN(LOG_DEBUG, "canel handle %s", handle);
203                 g_free(handle);
204                 handle = NULL;
205         }
206
207         /* call CancelCallback */
208         if (cb) {
209                 cb(arg);
210         }
211         return TRUE;
212 }
213
214 void geoclue_plugin_test_get_offline_token(const unsigned char *key,
215                 unsigned int keyLengh,
216                 OfflineTokenCallback cb,
217                 void *arg)
218 {
219         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_get_offline_token called");
220
221         unsigned char *key_copied = NULL;
222         if (key && keyLengh > 0) {
223                 key_copied = g_memdup(key, keyLengh);
224                 key_copied[keyLengh - 1] = '\0';
225                 if (key_copied) {
226                         LOG_PLUGIN(LOG_DEBUG, "key_copied [%s]", key_copied);
227
228                         /* call OfflineTokenCallback */
229                         if (cb) {
230                                 char *token = g_strdup("samsung_token");
231                                 LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_get_offline_token: before callback");
232                                 cb(arg, token, strlen(token));
233                                 LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_get_offline_token: after callback");
234                         }
235                 } else {
236                         LOG_PLUGIN(LOG_ERROR, "[ERROR] key copy fail");
237                 }
238         } else {
239                 LOG_PLUGIN(LOG_ERROR, "[ERROR] key or keyLengh parameter error");
240         }
241
242 }
243 int geoclue_plugin_test_offline_location(const unsigned char *key,
244                 unsigned int keyLength,
245                 const unsigned char *token,
246                 unsigned int tokenSize,
247                 LocationCallback cb,
248                 void *arg)
249 {
250         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_offline_location called");
251
252         if (cb) {
253                 if (xps_plugint_test) {
254                         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_offline_location: before callback");
255                         cb(arg, xps_plugint_test->location, NULL);
256                         LOG_PLUGIN(LOG_DEBUG, "geoclue_plugin_test_offline_location: before callback");
257                         xps_plugint_test->location_cb = cb;
258                         xps_plugint_test->arg = arg;
259                 }
260         }
261         return TRUE;
262 }
263
264 EXPORT_API const geoclue_plugin_interface *get_geoclue_plugin_interface()
265 {
266         LOG_PLUGIN(LOG_DEBUG, "get_geoclue_plugin_interface called");
267         return &g_geoclue_plugin_interface_test_interface;
268 }