remove mmi-common.h and move those contents to mmi.h
[platform/core/uifw/mmi-framework.git] / src / mmi-ipc.c
1 /*
2 * Copyright © 2021 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "mmi-ipc.h"
25 #include "mmi-dbg.h"
26
27 #include <tzplatform_config.h>
28 #include <unistd.h>
29 #include <rpc-port.h>
30 #include <rpc-port-internal.h>
31
32 static rpc_port_proxy_mmi_h _rpc_h;
33 static const char* _stub_appid = "mmi-manager";
34 static int _connected = 0;
35 static mmi_state_e _state = MMI_STATE_NONE;
36
37 rpc_port_proxy_mmi_h
38 mmi_ipc_get_rpc_h(void)
39 {
40         return _rpc_h;
41 }
42
43 mmi_state_e mmi_ipc_get_state(void)
44 {
45         return _state;
46 }
47
48 bool
49 mmi_ipc_is_connected(void)
50 {
51         return _connected ? true : false;
52 }
53
54 const char *
55 mmi_ipc_get_stub_appid(void)
56 {
57         return _stub_appid;
58 }
59
60 static void _on_connected(rpc_port_proxy_mmi_h h, void *user_data)
61 {
62         int r;
63         _connected = 1;
64
65         LOGI("...");
66         return;
67 }
68
69 static void _on_disconnected(rpc_port_proxy_mmi_h h, void *user_data)
70 {
71         _connected = 0;
72
73         LOGI("...");
74 }
75 static void _on_rejected(rpc_port_proxy_mmi_h h, void *user_data)
76 {
77         LOGI("...");
78         _connected = 0;
79 }
80
81 int
82 mmi_ipc_init(void)
83 {
84         /* initialize handles */
85         _rpc_h = NULL;
86         int r;
87
88         rpc_port_proxy_mmi_callback_s callback = {
89                 .connected = _on_connected,
90                 .disconnected = _on_disconnected,
91                 .rejected = _on_rejected
92         };
93
94         r = rpc_port_proxy_mmi_create(_stub_appid, &callback, NULL, &_rpc_h);
95         if (r != RPC_PORT_ERROR_NONE)
96         {
97                 LOGE("Failed to create mmi proxy handle ! (error:%d)\n", r);
98                 goto err;
99         }
100
101         r = rpc_port_proxy_mmi_connect(_rpc_h);
102         if (r != RPC_PORT_ERROR_NONE)
103         {
104                 LOGE("Failed to connect to %s ! (error:%d)\n", _stub_appid, r);
105                 goto err;
106         }
107
108         return 0;
109 err:
110         if (_rpc_h)
111                 rpc_port_proxy_mmi_destroy(_rpc_h);
112
113         rpc_port_deregister_proc_info();
114
115         _rpc_h = NULL;
116         return -1;
117 }
118
119 void
120 mmi_ipc_shutdown(void)
121 {
122         if (!_rpc_h)
123                 return;
124
125         rpc_port_proxy_mmi_destroy(_rpc_h);
126         rpc_port_deregister_proc_info();
127         _rpc_h = NULL;
128
129         _state = MMI_STATE_NONE;
130         _connected = 0;
131 }