Code sync from tizen_2.4
[platform/core/telephony/libtcore.git] / src / communicator.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 "server.h"
30 #include "communicator.h"
31
32 struct tcore_communicator_type {
33         char *name;
34         struct tcore_communitor_operations *ops;
35
36         void *user_data;
37
38         TcorePlugin *parent_plugin;
39 };
40
41
42 Communicator *tcore_communicator_new(TcorePlugin *plugin,
43         const char *name, struct tcore_communitor_operations *ops)
44 {
45         Communicator *comm;
46
47         comm = g_try_malloc0(sizeof(struct tcore_communicator_type));
48         if (!comm)
49                 return NULL;
50
51         if (name)
52                 comm->name = g_strdup(name);
53
54         comm->parent_plugin = plugin;
55         comm->ops = ops;
56
57         tcore_server_add_communicator(tcore_plugin_ref_server(plugin), comm);
58
59         return comm;
60 }
61
62 void tcore_communicator_free(Communicator *comm)
63 {
64         if (!comm)
65                 return;
66
67         if (comm->name)
68                 g_free(comm->name);
69
70         g_free(comm);
71 }
72
73 TcorePlugin *tcore_communicator_ref_plugin(Communicator *comm)
74 {
75         if (!comm)
76                 return NULL;
77
78         return comm->parent_plugin;
79 }
80
81 const char *tcore_communicator_ref_name(Communicator *comm)
82 {
83         if (!comm)
84                 return NULL;
85
86         return (const char *)comm->name;
87 }
88
89 TReturn tcore_communicator_link_user_data(Communicator *comm, void *data)
90 {
91         if (!comm)
92                 return TCORE_RETURN_EINVAL;
93
94         comm->user_data = data;
95
96         return TCORE_RETURN_SUCCESS;
97 }
98
99 void *tcore_communicator_ref_user_data(Communicator *comm)
100 {
101         if (!comm)
102                 return NULL;
103
104         return comm->user_data;
105 }
106
107 TReturn tcore_communicator_dispatch_request(Communicator *comm, UserRequest *ur)
108 {
109         Server *s;
110
111         if (!comm || !ur)
112                 return TCORE_RETURN_EINVAL;
113
114         s = tcore_plugin_ref_server(comm->parent_plugin);
115
116         return tcore_server_dispatch_request(s, ur);
117 }
118
119 TReturn tcore_communicator_send_response(Communicator *comm, UserRequest *ur,
120                 enum tcore_response_command command,
121                 unsigned int data_len, const void *data)
122 {
123         if (!comm || !comm->ops || !comm->ops->send_response)
124                 return TCORE_RETURN_EINVAL;
125
126         dbg("ur = 0x%x", (unsigned int)ur);
127
128         return comm->ops->send_response(comm, ur, command, data_len, data);
129 }
130
131 TReturn tcore_communicator_send_notification(Communicator *comm,
132                 CoreObject *source, enum tcore_notification_command command,
133                 unsigned int data_len, const void *data)
134 {
135         if (!comm || !comm->ops || !comm->ops->send_notification)
136                 return TCORE_RETURN_EINVAL;
137
138         return comm->ops->send_notification(comm, source, command, data_len, data);
139 }