Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / BluetoothSocketListener.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <JavaScriptCore/JavaScript.h>
18 #include <dpl/log/log.h>
19 #include <dpl/shared_ptr.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <Commons/IEvent.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <Tizen/Common/JSTizenException.h>
26 #include "BluetoothSocketListener.h"
27 #include "BluetoothMultiCallback.h"
28 #include "JSBluetoothSocket.h"
29
30 using namespace TizenApis::Commons;
31
32 namespace TizenApis {
33 namespace Tizen1_0 {
34
35 BluetoothSocketListener::~BluetoothSocketListener() 
36 {
37         LogDebug("entered");
38 }
39 BluetoothSocketListener::BluetoothSocketListener() :
40         EventBTSocketNotificationListener(ThreadEnum::NULL_THREAD)
41 {
42         LogDebug("entered");
43         
44 }
45
46 BluetoothSocketListener& BluetoothSocketListener::getInstance()
47 {
48         static BluetoothSocketListener instance;
49         return instance;
50
51 }
52
53 void BluetoothSocketListener::onAnswerReceived(const EventBTSocketNotificationPtr& event)
54 {
55         LogDebug("Enter-Socket notification");
56
57         BluetoothSocketNotifierPrivateDataPtr multiCallbacks = 
58                 DPL::DynamicPointerCast<BluetoothSocketNotifierPrivateData>(event->getPrivateData());
59
60         switch(event->getConnectionState())
61         {
62         case EventBTSocketNotification::DISCONNECTED:
63                 multiCallbacks->getOnClose()->callOnSuccess();
64                 break;                  
65         case EventBTSocketNotification::DATARECEIVED:
66         {
67                 EventBTReadDataType readData;
68                 JSBluetoothSocketPriv *priv = 
69                         static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(multiCallbacks->getObject()));
70                 IBluetoothSocketManagerPtr BluetoothSocketManager(priv->getObject());
71
72                 readData = event->getReadData();
73                 LogDebug("Size" << readData.size());
74                 BluetoothSocketManager->setReadData(readData);
75                 multiCallbacks->getOnMessage()->callOnSuccess();
76         }
77                 break;
78         case EventBTSocketNotification::SOCKETERROR:
79         default:
80                 multiCallbacks->getOnError()->callOnSuccess(JSTizenExceptionFactory::makeErrorObject(multiCallbacks->getOnError()->getContext(), 
81                                 JSTizenException::UNKNOWN_ERROR, "unknown error"));
82                 break;
83         }
84 }
85 }
86 }