2 * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
12 #include <Application.h>
15 #include <GroupLayoutBuilder.h>
16 #include <InterfaceDefs.h>
17 #include <SpaceLayoutItem.h>
18 #include <StringView.h>
19 #include <TextControl.h>
21 #include <bluetooth/RemoteDevice.h>
22 #include <bluetooth/LocalDevice.h>
23 #include <bluetooth/bdaddrUtils.h>
24 #include <bluetooth/bluetooth_error.h>
26 #include <bluetooth/HCI/btHCI_command.h>
27 #include <bluetooth/HCI/btHCI_event.h>
29 #include <PincodeWindow.h>
30 #include <bluetoothserver_p.h>
31 #include <CommandManager.h>
34 #define H_SEPARATION 15
35 #define V_SEPARATION 10
36 #define BD_ADDR_LABEL "BD_ADDR: "
38 static const uint32 skMessageAcceptButton
= 'acCp';
39 static const uint32 skMessageCancelButton
= 'mVch';
49 PincodeWindow::PincodeWindow(bdaddr_t address
, hci_id hid
)
50 : BWindow(BRect(800, 200, 900, 300), "Pincode request",
52 B_WILL_ACCEPT_FIRST_CLICK
| B_NOT_RESIZABLE
| B_NOT_ZOOMABLE
53 | B_AUTO_UPDATE_SIZE_LIMITS
,
54 B_ALL_WORKSPACES
), fBdaddr(address
), fHid(hid
)
58 // TODO: Get more info about device" ote name/features/encry/auth... etc
59 SetBDaddr(bdaddrUtils::ToString(fBdaddr
));
64 PincodeWindow::PincodeWindow(RemoteDevice
* rDevice
)
65 : BWindow(BRect(800, 200, 900, 300), "Pincode request",
67 B_WILL_ACCEPT_FIRST_CLICK
| B_NOT_ZOOMABLE
| B_NOT_RESIZABLE
68 | B_AUTO_UPDATE_SIZE_LIMITS
,
71 // TODO: Get more info about device" ote name/features/encry/auth... etc
72 SetBDaddr(bdaddrUtils::ToString(rDevice
->GetBluetoothAddress()));
73 fHid
= (rDevice
->GetLocalDeviceOwner())->ID();
78 PincodeWindow::InitUI()
80 SetLayout(new BGroupLayout(B_HORIZONTAL
));
82 fMessage
= new BStringView("Pincode", "Please enter the pincode ...");
83 fMessage
->SetFont(be_bold_font
);
85 fRemoteInfo
= new BStringView("bdaddr","BD_ADDR: ");
87 // TODO: Pincode cannot be more than 16 bytes
88 fPincodeText
= new BTextControl("pincode TextControl", "PIN code:", "5555", NULL
);
90 fAcceptButton
= new BButton("fAcceptButton", "Pair",
91 new BMessage(skMessageAcceptButton
));
93 fCancelButton
= new BButton("fCancelButton", "Cancel",
94 new BMessage(skMessageCancelButton
));
96 AddChild(BGroupLayoutBuilder(B_VERTICAL
, 10)
100 .Add(BGroupLayoutBuilder(B_HORIZONTAL
, 10)
104 .SetInsets(5, 5, 5, 5)
106 .SetInsets(15, 15, 15, 15)
112 PincodeWindow::MessageReceived(BMessage
*msg
)
114 // status_t err = B_OK;
118 case skMessageAcceptButton
:
120 BMessage
request(BT_MSG_HANDLE_SIMPLE_REQUEST
);
123 int8 bt_status
= BT_ERROR
;
125 void* command
= buildPinCodeRequestReply(fBdaddr
,
126 strlen(fPincodeText
->Text()),
127 (char*)fPincodeText
->Text(), &size
);
129 if (command
== NULL
) {
133 request
.AddInt32("hci_id", fHid
);
134 request
.AddData("raw command", B_ANY_TYPE
, command
, size
);
135 request
.AddInt16("eventExpected", HCI_EVENT_CMD_COMPLETE
);
136 request
.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL
,
137 OCF_PIN_CODE_REPLY
));
139 // we reside in the server
140 if (be_app_messenger
.SendMessage(&request
, &reply
) == B_OK
) {
141 if (reply
.FindInt8("status", &bt_status
) == B_OK
) {
142 PostMessage(B_QUIT_REQUESTED
);
144 // TODO: something failed here
149 case skMessageCancelButton
:
151 BMessage
request(BT_MSG_HANDLE_SIMPLE_REQUEST
);
154 int8 bt_status
= BT_ERROR
;
156 void* command
= buildPinCodeRequestNegativeReply(fBdaddr
, &size
);
158 if (command
== NULL
) {
162 request
.AddInt32("hci_id", fHid
);
163 request
.AddData("raw command", B_ANY_TYPE
, command
, size
);
164 request
.AddInt16("eventExpected", HCI_EVENT_CMD_COMPLETE
);
165 request
.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL
,
166 OCF_PIN_CODE_NEG_REPLY
));
168 if (be_app_messenger
.SendMessage(&request
, &reply
) == B_OK
) {
169 if (reply
.FindInt8("status", &bt_status
) == B_OK
) {
170 PostMessage(B_QUIT_REQUESTED
);
172 // TODO: something failed here
178 BWindow::MessageReceived(msg
);
184 bool PincodeWindow::QuitRequested()
186 return BWindow::QuitRequested();
190 void PincodeWindow::SetBDaddr(BString address
)
194 label
<< BD_ADDR_LABEL
<< address
;
195 printf("++ %s\n",label
.String());
196 fRemoteInfo
->SetText(label
.String());
197 fRemoteInfo
->ResizeToPreferred();