413408918e482de878a74e446db9fc287bc88f62
[framework/telephony/libtcore.git] / src / co_gps.c
1 /*
2  * libtcore
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib.h>
26
27 #include "tcore.h"
28 #include "plugin.h"
29 #include "user_request.h"
30 #include "co_gps.h"
31
32 struct private_object_data {
33         struct tcore_gps_operations *ops;
34 };
35
36 static void _clone_hook(CoreObject *src, CoreObject *dest)
37 {
38         struct private_object_data *src_po = NULL;
39         struct private_object_data *dest_po = NULL;
40
41         if (!src || !dest)
42                 return;
43
44         dest_po = calloc(sizeof(struct private_object_data), 1);
45         if (!dest_po) {
46                 tcore_object_link_object(dest, NULL);
47                 return;
48         }
49
50         src_po = tcore_object_ref_object(src);
51         dest_po->ops = src_po->ops;
52
53         tcore_object_link_object(dest, dest_po);
54 }
55
56 static TReturn _dispatcher(CoreObject *o, UserRequest *ur)
57 {
58         enum tcore_request_command command;
59         struct private_object_data *po = NULL;
60
61         if (!o || !ur)
62                 return TCORE_RETURN_EINVAL;
63
64         po = tcore_object_ref_object(o);
65         if (!po || !po->ops)
66                 return TCORE_RETURN_ENOSYS;
67
68         command = tcore_user_request_get_command(ur);
69         switch (command) {
70                 case TREQ_GPS_CONFIRM_MEASURE_POS:
71                         dbg("TREQ_GPS_CONFIRM_MEASURE_POS");
72                         if (!po->ops->confirm_measure_pos)
73                                 return TCORE_RETURN_ENOSYS;
74
75                         return po->ops->confirm_measure_pos(o, ur);
76                         break;
77                 default:
78                         dbg("not supported cmd");
79                         break;
80         }
81         return TCORE_RETURN_SUCCESS;
82 }
83
84 CoreObject *tcore_gps_new(TcorePlugin *p, const char *name,
85                 struct tcore_gps_operations *ops, TcoreHal *hal)
86 {
87         CoreObject *o = NULL;
88         struct private_object_data *po = NULL;
89
90         if (!p)
91                 return NULL;
92
93         o = tcore_object_new(p, name, hal);
94         if (!o)
95                 return NULL;
96
97         po = calloc(sizeof(struct private_object_data), 1);
98         if (!po) {
99                 tcore_object_free(o);
100                 return NULL;
101         }
102
103         po->ops = ops;
104
105         tcore_object_set_type(o, CORE_OBJECT_TYPE_GPS);
106         tcore_object_link_object(o, po);
107         tcore_object_set_dispatcher(o, _dispatcher);
108         tcore_object_set_clone_hook(o, _clone_hook);
109
110         return o;
111 }
112
113 void tcore_gps_free(CoreObject *o)
114 {
115         struct private_object_data *po = NULL;
116
117         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_GPS);
118
119         po = tcore_object_ref_object(o);
120         if (!po)
121                 return;
122
123         g_free(po);
124         tcore_object_link_object(o, po);
125         tcore_object_free(o);
126 }
127