upload tizen1.0 source
[framework/location/libslp-location.git] / tests / location-api-test-util.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.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 <glib.h>
23 #include <json-glib.h>
24 #include "location.h"
25
26 JsonParser *parser;
27 JsonNode *root;
28
29 static int _get_polygon_position_count(int polygon_index)
30 {
31         if (parser == NULL || root == NULL) return 0;
32
33         JsonObject* polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
34         if (polygon_obj == NULL) return 0;
35
36         JsonArray * positions = json_object_get_array_member (polygon_obj, "positions");
37         if (positions == NULL) return 0;
38
39         return json_array_get_length(positions);
40 }
41
42 static LocationPosition* _get_position_from_polygon(int polygon_index, int pos_index)
43 {
44         double latitude = 0.0;
45         double longitude = 0.0;
46         LocationPosition *position = NULL;
47         if (parser == NULL || root == NULL) return NULL;
48
49         JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
50         JsonArray * pos_array = json_object_get_array_member(polygon_obj, "positions");
51         JsonObject* pos = json_array_get_object_element(pos_array, pos_index);
52
53         latitude = json_object_get_double_member(pos, "latitude");
54         longitude = json_object_get_double_member(pos, "longitude");
55
56         if (latitude == 0.0 || longitude == 0.0) return NULL;
57
58         position = location_position_new(0, latitude, longitude, 0.0, LOCATION_STATUS_2D_FIX);
59
60         return position;
61 }
62
63 static LocationPosition* _get_marker_position_from_polygon(int polygon_index, int pos_index)
64 {
65         double latitude = 0.0;
66         double longitude = 0.0;
67         LocationPosition *position = NULL;
68         if (parser == NULL || root == NULL) return NULL;
69
70         JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
71         JsonArray * pos_array = json_object_get_array_member(polygon_obj, "marker_position");
72         JsonObject* pos = json_array_get_object_element(pos_array, pos_index);
73
74         latitude = json_object_get_double_member(pos, "latitude");
75         longitude = json_object_get_double_member(pos, "longitude");
76
77         if (latitude == 0.0 || longitude == 0.0) return NULL;
78
79         position = location_position_new(0, latitude, longitude, 0.0, LOCATION_STATUS_2D_FIX);
80
81         return position;
82 }
83
84 static void _free_position_list(gpointer data)
85 {
86         if (data == NULL) return;
87
88         LocationPosition *position = (LocationPosition*) data;
89
90         location_position_free(position);
91 }
92
93
94 LocationBoundary* json_util_get_polygon_boundary(int polygon_index)
95 {
96         if (parser == NULL || root == NULL) {
97                 g_printf("invalid param parser[%d], root[%d]\n", parser, root);
98                 return NULL;
99         }
100         GList* position_list = NULL;
101         LocationBoundary *boundary = NULL;
102         int index = 0;
103         int pos_count = _get_polygon_position_count(polygon_index);
104         if (pos_count == 0) return NULL;
105
106         for(index = 0; index < pos_count; index++) {
107                 position_list = g_list_append(position_list, _get_position_from_polygon(polygon_index, index));
108         }
109
110         boundary = location_boundary_new_for_polygon(position_list);
111
112         g_list_free_full(position_list, (GDestroyNotify)_free_position_list);
113
114         return boundary;
115 }
116
117
118 /* Polygon boundary */
119 int json_util_get_polygon_count(void)
120 {
121         JsonArray * array = json_node_get_array(root);
122
123         return json_array_get_length(array);
124 }
125
126 char *json_util_get_polygon_name(int polygon_index)
127 {
128         char *name = NULL;
129         JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
130
131         name = (char *)json_object_get_string_member(polygon_obj, "name");
132         if (name == NULL)  return NULL;
133
134         return g_strdup(name);
135 }
136
137 /* Test Marker */
138 char * json_util_get_marker_name(int polygon_index, int pos_index)
139 {
140         char *result = NULL;
141         if (parser == NULL || root == NULL) return NULL;
142
143         JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
144         JsonArray * pos_array = json_object_get_array_member(polygon_obj, "marker_position");
145         JsonObject* pos = json_array_get_object_element(pos_array, pos_index);
146
147         result = (char *)json_object_get_string_member(pos, "where");
148         if (result == NULL) return NULL;
149
150         return g_strdup(result);
151 }
152
153 int json_util_get_marker_position_count(int polygon_index)
154 {
155         if (parser == NULL || root == NULL) return 0;
156
157         JsonObject* polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
158         JsonArray * marker_position = json_object_get_array_member (polygon_obj, "marker_position");
159
160         return json_array_get_length(marker_position);
161 }
162
163 LocationPosition *json_util_get_marker_position(int polygon_index, int marker_index)
164 {
165         if (parser == NULL || root == NULL) return NULL;
166
167         LocationPosition* position = NULL;
168         position = _get_marker_position_from_polygon(polygon_index, marker_index);
169
170         return position;
171 }
172
173 char* json_util_result_zone_test(int polygon_index, int marker_index)
174 {
175         if (parser == NULL || root == NULL) return NULL;
176
177         char *result = NULL;
178         JsonObject *polygon_obj = json_array_get_object_element(json_node_get_array(root), polygon_index);
179         JsonArray * pos_array = json_object_get_array_member(polygon_obj, "marker_position");
180         JsonObject* pos = json_array_get_object_element(pos_array, marker_index);
181
182         result = (char *)json_object_get_string_member(pos, "result");
183         if (result == NULL) return NULL;
184
185         return g_strdup(result);
186 }
187
188 void json_util_init(const char * file_name)
189 {
190         g_print("Enter init_json_parser");
191         GError *error;
192         gboolean ret = FALSE;
193         if (parser != NULL) return;
194
195         parser = json_parser_new();
196
197         error = NULL;
198         ret = json_parser_load_from_file(parser, file_name, &error);
199         if (ret == FALSE) {
200                 g_print("Unable to parser[%s]:[%s]\n", file_name, error->message);
201                 return ;
202         }
203
204         root = json_parser_get_root(parser);
205 }