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