def550e2b4ef53bcf529d3ef7551864ddafa316b
[framework/telephony/libtcore.git] / src / co_sap.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 "queue.h"
30 #include "user_request.h"
31 #include "co_sap.h"
32
33 struct private_object_data {
34         struct tcore_sap_operations *ops;
35 };
36
37 static TReturn _dispatcher(CoreObject *o, UserRequest *ur)
38 {
39         enum tcore_request_command command;
40         struct private_object_data *po = NULL;
41
42         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_SAP, TCORE_RETURN_EINVAL);
43
44         po = tcore_object_ref_object(o);
45         if (!po || !po->ops)
46                 return TCORE_RETURN_ENOSYS;
47
48         command = tcore_user_request_get_command(ur);
49         switch (command) {
50                 case TREQ_SAP_REQ_CONNECT:
51                         po = tcore_object_ref_object(o);
52                         if (!po->ops->connect)
53                                 return TCORE_RETURN_ENOSYS;
54
55                         return po->ops->connect(o, ur);
56                         break;
57
58                 case TREQ_SAP_REQ_DISCONNECT:
59                         if (!po->ops->disconnect)
60                                 return TCORE_RETURN_ENOSYS;
61
62                         return po->ops->disconnect(o, ur);
63                         break;
64
65                 case TREQ_SAP_REQ_STATUS:
66                         if (!po->ops->req_status)
67                                 return TCORE_RETURN_ENOSYS;
68
69                         return po->ops->req_status(o, ur);
70                         break;
71
72                 case TREQ_SAP_REQ_ATR:
73                         if (!po->ops->get_atr)
74                                 return TCORE_RETURN_ENOSYS;
75
76                         return po->ops->get_atr(o, ur);
77                         break;
78
79                 case TREQ_SAP_TRANSFER_APDU:
80                         if (!po->ops->transfer_apdu)
81                                 return TCORE_RETURN_ENOSYS;
82
83                         return po->ops->transfer_apdu(o, ur);
84                         break;
85
86                 case TREQ_SAP_SET_PROTOCOL:
87                         if (!po->ops->set_transport_protocol)
88                                 return TCORE_RETURN_ENOSYS;
89
90                         return po->ops->set_transport_protocol(o, ur);
91                         break;
92
93                 case TREQ_SAP_SET_POWER:
94                         if (!po->ops->set_power)
95                                 return TCORE_RETURN_ENOSYS;
96
97                         return po->ops->set_power(o, ur);
98                         break;
99
100                 case TREQ_SAP_REQ_CARDREADERSTATUS:
101                         if (!po->ops->get_cardreader_status)
102                                 return TCORE_RETURN_ENOSYS;
103
104                         return po->ops->get_cardreader_status(o, ur);
105                         break;
106
107                 default:
108                         break;
109         }
110         return TCORE_RETURN_SUCCESS;
111 }
112
113 static void _clone_hook(CoreObject *src, CoreObject *dest)
114 {
115         struct private_object_data *src_po = NULL;
116         struct private_object_data *dest_po = NULL;
117
118         if (!src || !dest)
119                 return;
120
121         dest_po = calloc(sizeof(struct private_object_data), 1);
122         if (!dest_po) {
123                 tcore_object_link_object(dest, NULL);
124                 return;
125         }
126
127         src_po = tcore_object_ref_object(src);
128         dest_po->ops = src_po->ops;
129
130         tcore_object_link_object(dest, dest_po);
131 }
132
133 static void _free_hook(CoreObject *o)
134 {
135         struct private_object_data *po = NULL;
136
137         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SAP);
138
139         po = tcore_object_ref_object(o);
140         if (po) {
141                 free(po);
142                 tcore_object_link_object(o, NULL);
143         }
144 }
145
146 CoreObject *tcore_sap_new(TcorePlugin *p, const char *name,
147                 struct tcore_sap_operations *ops, TcoreHal *hal)
148 {
149         CoreObject *o = NULL;
150         struct private_object_data *po = NULL;
151
152         if (!p)
153                 return NULL;
154
155         o = tcore_object_new(p, name, hal);
156         if (!o)
157                 return NULL;
158
159         po = calloc(sizeof(struct private_object_data), 1);
160         if (!po) {
161                 tcore_object_free(o);
162                 return NULL;
163         }
164
165         po->ops = ops;
166
167         tcore_object_set_type(o, CORE_OBJECT_TYPE_SAP);
168         tcore_object_link_object(o, po);
169         tcore_object_set_free_hook(o, _free_hook);
170         tcore_object_set_clone_hook(o, _clone_hook);
171         tcore_object_set_dispatcher(o, _dispatcher);
172
173         return o;
174 }
175
176 void tcore_sap_free(CoreObject *o)
177 {
178         struct private_object_data *po = NULL;
179
180         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SAP);
181
182         po = tcore_object_ref_object(o);
183         if (!po)
184                 return;
185
186         free(po);
187         tcore_object_free(o);
188 }