Merge from master
[platform/core/appfw/message-port.git] / src / message-port.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        message-port.cpp
20  * @brief       This is the implementation file for the MessagePort.
21  */
22
23 #include <stdlib.h>
24
25 #include "message-port.h"
26 #include "message-port-log.h"
27
28 #include "MessagePortProxy.h"
29
30 int
31 messageport_register_local_port(const char* local_port, messageport_message_cb callback)
32 {
33         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
34         if (pProxy != NULL)
35         {
36                 return pProxy->RegisterMessagePort(local_port, false, callback);
37         }
38
39         return MESSAGEPORT_ERROR_IO_ERROR;
40 }
41
42 int
43 messageport_register_trusted_local_port(const char* local_port, messageport_message_cb callback)
44 {
45         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
46         if (pProxy != NULL)
47         {
48                 return pProxy->RegisterMessagePort(local_port, true, callback);
49         }
50
51         return MESSAGEPORT_ERROR_IO_ERROR;
52 }
53
54 int
55 messageport_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
56 {
57         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
58         if (pProxy != NULL)
59         {
60                 return pProxy->CheckRemotePort(remote_app_id, remote_port, false, exist);
61         }
62
63         return MESSAGEPORT_ERROR_IO_ERROR;
64 }
65
66 int
67 messageport_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
68 {
69         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
70         if (pProxy != NULL)
71         {
72                 return pProxy->CheckRemotePort(remote_app_id, remote_port, true, exist);
73         }
74
75         return MESSAGEPORT_ERROR_IO_ERROR;
76 }
77
78 int
79 messageport_send_message(const char* remote_app_id, const char* remote_port, bundle* message)
80 {
81         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
82         if (pProxy != NULL)
83         {
84                 return pProxy->SendMessage(remote_app_id, remote_port, false, message);
85         }
86
87         return MESSAGEPORT_ERROR_IO_ERROR;
88 }
89
90 int
91 messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message)
92 {
93         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
94         if (pProxy != NULL)
95         {
96                 return pProxy->SendMessage(remote_app_id, remote_port, true, message);
97         }
98
99         return MESSAGEPORT_ERROR_IO_ERROR;
100 }
101
102 int
103 messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* message)
104 {
105         int ret = 0;
106         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
107         if (pProxy != NULL)
108         {
109                 char* pName = pProxy->GetLocalPortNameN(id);
110                 bool trusted = false;
111                 pProxy->CheckTrustedLocalPort(id, &trusted);
112
113                 ret = pProxy->SendMessage(pName, trusted, remote_app_id, remote_port, false, message);
114
115                 free(pName);
116
117                 return ret;
118         }
119
120         return MESSAGEPORT_ERROR_IO_ERROR;
121 }
122
123 int
124 messageport_send_bidirectional_trusted_message(int id, const char* remote_app_id, const char* remote_port, bundle* message)
125 {
126         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
127         if (pProxy != NULL)
128         {
129                 bool trusted = false;
130                 int ret = pProxy->CheckTrustedLocalPort(id, &trusted);
131                 if (ret < 0)
132                 {
133                         return ret;
134                 }
135
136                 char* pName = pProxy->GetLocalPortNameN(id);
137                 if (pName == NULL)
138                 {
139                         return MESSAGEPORT_ERROR_INVALID_PARAMETER;
140                 }
141
142                 ret = pProxy->SendMessage(pName, trusted, remote_app_id, remote_port, true, message);
143
144                 free(pName);
145
146                 return ret;
147         }
148
149         return MESSAGEPORT_ERROR_IO_ERROR;
150 }
151
152 int
153 messageport_get_local_port_name(int id, char **name)
154 {
155         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
156         if (pProxy != NULL)
157         {
158                 char* pName = pProxy->GetLocalPortNameN(id);
159                 if (pName == NULL)
160                 {
161                         return MESSAGEPORT_ERROR_INVALID_PARAMETER;
162                 }
163                 else
164                 {
165                         *name = pName;
166                         return 0;
167                 }
168         }
169
170         return MESSAGEPORT_ERROR_IO_ERROR;
171 }
172
173 int
174 messageport_check_trusted_local_port(int id, bool *trusted)
175 {
176         MessagePortProxy* pProxy = MessagePortProxy::GetProxy();
177         if (pProxy != NULL)
178         {
179                 return pProxy->CheckTrustedLocalPort(id, trusted);
180         }
181
182         return MESSAGEPORT_ERROR_IO_ERROR;
183 }
184