|
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "BluetoothSocket.h" |
|
8 |
|
9 #include "BluetoothSocketObserver.h" |
|
10 #include "BluetoothUnixSocketConnector.h" |
|
11 #include "nsThreadUtils.h" |
|
12 |
|
13 using namespace mozilla::ipc; |
|
14 USING_BLUETOOTH_NAMESPACE |
|
15 |
|
16 BluetoothSocket::BluetoothSocket(BluetoothSocketObserver* aObserver, |
|
17 BluetoothSocketType aType, |
|
18 bool aAuth, |
|
19 bool aEncrypt) |
|
20 : mObserver(aObserver) |
|
21 , mType(aType) |
|
22 , mAuth(aAuth) |
|
23 , mEncrypt(aEncrypt) |
|
24 { |
|
25 MOZ_ASSERT(aObserver); |
|
26 } |
|
27 |
|
28 bool |
|
29 BluetoothSocket::Connect(const nsACString& aDeviceAddress, int aChannel) |
|
30 { |
|
31 MOZ_ASSERT(NS_IsMainThread()); |
|
32 MOZ_ASSERT(!aDeviceAddress.IsEmpty()); |
|
33 |
|
34 nsAutoPtr<BluetoothUnixSocketConnector> c( |
|
35 new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt)); |
|
36 |
|
37 if (!ConnectSocket(c.forget(), aDeviceAddress.BeginReading())) { |
|
38 nsAutoString addr; |
|
39 GetAddress(addr); |
|
40 BT_LOGD("%s failed. Current connected device address: %s", |
|
41 __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get()); |
|
42 return false; |
|
43 } |
|
44 |
|
45 return true; |
|
46 } |
|
47 |
|
48 bool |
|
49 BluetoothSocket::Listen(int aChannel) |
|
50 { |
|
51 MOZ_ASSERT(NS_IsMainThread()); |
|
52 |
|
53 nsAutoPtr<BluetoothUnixSocketConnector> c( |
|
54 new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt)); |
|
55 |
|
56 if (!ListenSocket(c.forget())) { |
|
57 nsAutoString addr; |
|
58 GetAddress(addr); |
|
59 BT_LOGD("%s failed. Current connected device address: %s", |
|
60 __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get()); |
|
61 return false; |
|
62 } |
|
63 |
|
64 return true; |
|
65 } |
|
66 |
|
67 void |
|
68 BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) |
|
69 { |
|
70 MOZ_ASSERT(NS_IsMainThread()); |
|
71 MOZ_ASSERT(mObserver); |
|
72 mObserver->ReceiveSocketData(this, aMessage); |
|
73 } |
|
74 |
|
75 void |
|
76 BluetoothSocket::OnConnectSuccess() |
|
77 { |
|
78 MOZ_ASSERT(NS_IsMainThread()); |
|
79 MOZ_ASSERT(mObserver); |
|
80 mObserver->OnSocketConnectSuccess(this); |
|
81 } |
|
82 |
|
83 void |
|
84 BluetoothSocket::OnConnectError() |
|
85 { |
|
86 MOZ_ASSERT(NS_IsMainThread()); |
|
87 MOZ_ASSERT(mObserver); |
|
88 mObserver->OnSocketConnectError(this); |
|
89 } |
|
90 |
|
91 void |
|
92 BluetoothSocket::OnDisconnect() |
|
93 { |
|
94 MOZ_ASSERT(NS_IsMainThread()); |
|
95 MOZ_ASSERT(mObserver); |
|
96 mObserver->OnSocketDisconnect(this); |
|
97 } |
|
98 |