Merge "Fix the prevent issue - #38349" into tizen_2.2
[platform/framework/native/appfw.git] / src / io / FIo_DataRouter.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FIo_DataRouter.cpp
19  * @brief       This is the implementation file for %_DataRouter class.
20  */
21
22 #include <vconf.h>
23 #include <serial.h>
24
25 #include <FBaseSysLog.h>
26 #include <FIo_DataRouter.h>
27 #include <FIo_IDataRouterEventListener.h>
28
29 using namespace Tizen::Base;
30
31 namespace Tizen { namespace Io
32 {
33
34 const char* _VCONF_KEY_DATA_ROUTER_READY_EVENT = "memory/data_router/osp_serial_open";
35
36 static serial_h gSerialHandle = null;
37 static _DataRouter* gp_DataRouter = null;
38 static int __refCount = 0;
39
40 void
41 SerialStateChangedCallback(serial_error_e result, serial_state_e state, void* userData)
42 {
43         int ret = 0;
44         if (gp_DataRouter != null)
45         {
46                 if (state == SERIAL_STATE_OPENED)
47                 {
48                         gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_OPENED);
49                 }
50                 else if (state == SERIAL_STATE_CLOSED)
51                 {
52                         gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_CLOSED);
53                         ret = serial_unset_state_changed_cb(gSerialHandle);             
54                         ret = serial_unset_data_received_cb(gSerialHandle);
55                         ret = serial_destroy(gSerialHandle);
56                 }
57                 else
58                 {
59                         SysLogException(NID_IO, E_SYSTEM, "Undefined state is required.");
60                 }
61         }
62 }
63
64 bool
65 SerialDataReceivedCallback(const char* data, int length, void* userData)
66 {
67
68         if (gp_DataRouter != null)
69         {
70                 gp_DataRouter->DataReceived(data, length);
71         }
72
73         return true;
74 }
75
76 void
77 SerialStateReadyCallback(keynode_t* node, void* userData)
78 {
79         result r = E_SUCCESS;
80         int ret = 0;
81
82         if (strcmp(_VCONF_KEY_DATA_ROUTER_READY_EVENT, vconf_keynode_get_name(node)) == 0)
83         {
84                 int dataRouterStatus = 0;
85                 ret = vconf_get_int(_VCONF_KEY_DATA_ROUTER_READY_EVENT, &dataRouterStatus);
86                 SysTryCatch(NID_IO, ret == 0, r= E_SYSTEM, r, "[%s] Failed to get vconf status");
87
88                 if(dataRouterStatus == 1)
89                 {
90                         ret = serial_create(&gSerialHandle);
91                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to initialize data router module. Error:%d", ret);
92
93                         ret = serial_set_state_changed_cb(gSerialHandle, SerialStateChangedCallback, null);
94                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for state change", GetErrorMessage(r));
95
96                         ret = serial_set_data_received_cb(gSerialHandle, SerialDataReceivedCallback, null);
97                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
98
99                         ret = serial_open(gSerialHandle);
100                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
101                 }
102         }
103
104 CATCH:
105         if (r != E_SUCCESS)
106         {
107                 ret = serial_unset_state_changed_cb(gSerialHandle);             
108                 ret = serial_unset_data_received_cb(gSerialHandle);
109                 ret = serial_close(gSerialHandle);
110                 ret = serial_destroy(gSerialHandle);
111         }
112 }
113
114 _DataRouter::_DataRouter(void)
115         : __pDataRouterEventListener(null)
116         , __initialized(false)
117 {
118 }
119 _DataRouter::~_DataRouter(void)
120 {
121         int ret = 0;
122         ret = serial_close(gSerialHandle);
123         if (ret != SERIAL_ERROR_NONE)
124         {
125                 SysLogException(NID_IO, E_SYSTEM, "Failed to close handle of serial. Error:%d", ret);
126         }
127
128         ret = serial_destroy(gSerialHandle);
129         if (ret != SERIAL_ERROR_NONE)
130         {
131                 SysLogException(NID_IO, E_SYSTEM, "Failed to release handle of serial. Error:%d", ret);
132         }
133
134         gSerialHandle = null;
135 }
136
137 result
138 _DataRouter::Construct(_IDataRouterEventListener& listener)
139 {
140         int ret = 0;
141
142         __pDataRouterEventListener = &listener;
143
144         if(__initialized == false)
145         {
146                 ret = vconf_notify_key_changed(_VCONF_KEY_DATA_ROUTER_READY_EVENT, SerialStateReadyCallback, null);
147                 SysTryReturnResult(NID_IO, ret == 0, E_SYSTEM, "It is failed to register vconf event for data router ready.");
148                 __initialized = true;
149         }
150         return E_SUCCESS;
151 }
152
153 result
154 _DataRouter::Write(const char* data, int length)
155 {
156         int ret = 0;
157         SysTryReturnResult(NID_IO, gSerialHandle != null, E_SYSTEM, "DataRouter is not ready.");
158
159         ret = serial_write(gSerialHandle, data, length);
160         SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Write is failed %d", ret);
161
162         return E_SUCCESS;
163 }
164
165 void
166 _DataRouter::DataReceived(const char* data, int length)
167 {
168         if (__pDataRouterEventListener != null)
169         {
170                 __pDataRouterEventListener->OnDataRouterDataReceivedN(data, length);
171         }
172 }
173
174 void
175 _DataRouter::StateChanged(_DataRouterState state)
176 {
177         if (__pDataRouterEventListener != null)
178         {
179                 __pDataRouterEventListener->OnDataRouterStateChanged(state);
180         }
181 }
182
183 _DataRouter*
184 _DataRouter::GetInstance(void)
185 {
186         if (gp_DataRouter == null)
187         {
188                 gp_DataRouter = new (std::nothrow) _DataRouter();
189                 SysTryReturn(NID_IO, gp_DataRouter != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
190         }
191
192         __refCount++;
193
194         return gp_DataRouter;
195 }
196
197 void
198 _DataRouter::ReleaseInstance(void)
199 {
200         if(__refCount != 0)
201                 __refCount--;
202
203         if (gp_DataRouter != null && __refCount == 0)
204         {
205                 delete gp_DataRouter;
206                 gp_DataRouter = null;
207         }
208 }
209
210 } } // Tizen::Io
211