778a9176a10c3a199b88c74aaa810fc92c626919
[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         SysLog(NID_IO, "Enter");
44         int ret = 0;
45         if (gp_DataRouter != null)
46         {
47                 if (state == SERIAL_STATE_OPENED)
48                 {
49                         gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_OPENED);
50                 }
51                 else if (state == SERIAL_STATE_CLOSED)
52                 {
53                         gp_DataRouter->StateChanged(_DATA_ROUTER_STATE_CLOSED);
54                         ret = serial_unset_state_changed_cb(gSerialHandle);             
55                         ret = serial_unset_data_received_cb(gSerialHandle);
56                         ret = serial_destroy(gSerialHandle);
57                 }
58                 else
59                 {
60                         SysLogException(NID_IO, E_SYSTEM, "Undefined state is required.");
61                 }
62         }
63 }
64
65 bool
66 SerialDataReceivedCallback(const char* data, int length, void* userData)
67 {
68         SysLog(NID_IO, "Enter");
69         if (gp_DataRouter != null)
70         {
71                 gp_DataRouter->DataReceived(data, length);
72         }
73
74         return true;
75 }
76
77 void
78 SerialStateReadyCallback(keynode_t* node, void* userData)
79 {
80         SysLog(NID_IO, "Enter");
81         result r = E_SUCCESS;
82         int ret = 0;
83
84         if (strcmp(_VCONF_KEY_DATA_ROUTER_READY_EVENT, vconf_keynode_get_name(node)) == 0)
85         {
86                 int dataRouterStatus = 0;
87                 ret = vconf_get_int(_VCONF_KEY_DATA_ROUTER_READY_EVENT, &dataRouterStatus);
88                 SysTryCatch(NID_IO, ret == 0, r= E_SYSTEM, r, "[%s] Failed to get vconf status");
89
90                 if(dataRouterStatus == 1)
91                 {
92                         SysLog(NID_IO, "Try to initialize serial port");
93                         ret = serial_create(&gSerialHandle);
94                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to initialize data router module. Error:%d", ret);
95
96                         ret = serial_set_state_changed_cb(gSerialHandle, SerialStateChangedCallback, null);
97                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for state change", GetErrorMessage(r));
98
99                         ret = serial_set_data_received_cb(gSerialHandle, SerialDataReceivedCallback, null);
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                         ret = serial_open(gSerialHandle);
103                         SysTryCatch(NID_IO, ret == SERIAL_ERROR_NONE, r = E_SYSTEM, r, "[%s] Failed to set callback function for data receive", GetErrorMessage(r));
104                 }
105         }
106
107 CATCH:
108         if (r != E_SUCCESS)
109         {
110                 ret = serial_unset_state_changed_cb(gSerialHandle);             
111                 ret = serial_unset_data_received_cb(gSerialHandle);
112                 ret = serial_close(gSerialHandle);
113                 ret = serial_destroy(gSerialHandle);
114         }
115 }
116
117 _DataRouter::_DataRouter(void)
118         : __pDataRouterEventListener(null)
119         , __initialized(false)
120 {
121 }
122 _DataRouter::~_DataRouter(void)
123 {
124         int ret = 0;
125         ret = serial_close(gSerialHandle);
126         if (ret != SERIAL_ERROR_NONE)
127         {
128                 SysLogException(NID_IO, E_SYSTEM, "Failed to close handle of serial. Error:%d", ret);
129         }
130
131         ret = serial_destroy(gSerialHandle);
132         if (ret != SERIAL_ERROR_NONE)
133         {
134                 SysLogException(NID_IO, E_SYSTEM, "Failed to release handle of serial. Error:%d", ret);
135         }
136
137         gSerialHandle = null;
138 }
139
140 result
141 _DataRouter::Construct(_IDataRouterEventListener& listener)
142 {
143         int ret = 0;
144
145         __pDataRouterEventListener = &listener;
146
147         if(__initialized == false)
148         {
149                 ret = vconf_notify_key_changed(_VCONF_KEY_DATA_ROUTER_READY_EVENT, SerialStateReadyCallback, null);
150                 SysTryReturnResult(NID_IO, ret == 0, E_SYSTEM, "It is failed to register vconf event for data router ready.");
151                 __initialized = true;
152         }
153         return E_SUCCESS;
154 }
155
156 result
157 _DataRouter::Write(const char* data, int length)
158 {
159         int ret = 0;
160         SysTryReturnResult(NID_IO, gSerialHandle != null, E_SYSTEM, "DataRouter is not ready.");
161
162         ret = serial_write(gSerialHandle, data, length);
163         SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Write is failed %d", ret);
164
165         return E_SUCCESS;
166 }
167
168 void
169 _DataRouter::DataReceived(const char* data, int length)
170 {
171         if (__pDataRouterEventListener != null)
172         {
173                 __pDataRouterEventListener->OnDataRouterDataReceivedN(data, length);
174         }
175 }
176
177 void
178 _DataRouter::StateChanged(_DataRouterState state)
179 {
180         if (__pDataRouterEventListener != null)
181         {
182                 __pDataRouterEventListener->OnDataRouterStateChanged(state);
183         }
184 }
185
186 _DataRouter*
187 _DataRouter::GetInstance(void)
188 {
189         if (gp_DataRouter == null)
190         {
191                 gp_DataRouter = new (std::nothrow) _DataRouter();
192                 SysTryReturn(NID_IO, gp_DataRouter != null, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
193         }
194
195         __refCount++;
196
197         return gp_DataRouter;
198 }
199
200 void
201 _DataRouter::ReleaseInstance(void)
202 {
203         if(__refCount != 0)
204                 __refCount--;
205
206         if (gp_DataRouter != null && __refCount == 0)
207         {
208                 delete gp_DataRouter;
209                 gp_DataRouter = null;
210         }
211 }
212
213 } } // Tizen::Io
214