xref: /haiku/src/kits/bluetooth/UI/PincodeWindow.cpp (revision dd419e59dcdc1dacec64aec3433288111445d7c8)
1 /*
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.
4  */
5 
6 
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <malloc.h>
10 
11 #include <String.h>
12 #include <Message.h>
13 #include <Application.h>
14 
15 #include <Button.h>
16 #include <GroupLayoutBuilder.h>
17 #include <InterfaceDefs.h>
18 #include <SpaceLayoutItem.h>
19 #include <StringView.h>
20 #include <TextControl.h>
21 
22 #include <bluetooth/RemoteDevice.h>
23 #include <bluetooth/LocalDevice.h>
24 #include <bluetooth/bdaddrUtils.h>
25 #include <bluetooth/bluetooth_error.h>
26 
27 #include <bluetooth/HCI/btHCI_command.h>
28 #include <bluetooth/HCI/btHCI_event.h>
29 
30 #include <PincodeWindow.h>
31 #include <bluetoothserver_p.h>
32 #include <CommandManager.h>
33 
34 
35 #define H_SEPARATION  15
36 #define V_SEPARATION  10
37 #define BD_ADDR_LABEL "BD_ADDR: "
38 
39 
40 static const uint32 skMessageAcceptButton = 'acCp';
41 static const uint32 skMessageCancelButton = 'mVch';
42 
43 
44 namespace Bluetooth {
45 
46 
47 #if 0
48 #pragma mark -
49 #endif
50 
51 
52 PincodeWindow::PincodeWindow(bdaddr_t address, hci_id hid)
53 	: BWindow(BRect(800, 200, 900, 300), "Pincode request",
54 		B_FLOATING_WINDOW,
55 		B_WILL_ACCEPT_FIRST_CLICK | B_NOT_RESIZABLE|  B_NOT_ZOOMABLE
56 		| B_AUTO_UPDATE_SIZE_LIMITS,
57 		B_ALL_WORKSPACES), fBdaddr(address), fHid(hid)
58 {
59 	InitUI();
60 
61 	// TODO: Get more info about device" ote name/features/encry/auth... etc
62 	SetBDaddr(bdaddrUtils::ToString(fBdaddr));
63 
64 }
65 
66 
67 PincodeWindow::PincodeWindow(RemoteDevice* rDevice)
68 	: BWindow(BRect(800, 200, 900, 300), "Pincode request",
69 		B_FLOATING_WINDOW,
70 		B_WILL_ACCEPT_FIRST_CLICK | B_NOT_ZOOMABLE | B_NOT_RESIZABLE
71 		| B_AUTO_UPDATE_SIZE_LIMITS,
72 		B_ALL_WORKSPACES)
73 {
74 	// TODO: Get more info about device" ote name/features/encry/auth... etc
75 	SetBDaddr(bdaddrUtils::ToString(rDevice->GetBluetoothAddress()));
76 	fHid = (rDevice->GetLocalDeviceOwner())->ID();
77 }
78 
79 
80 void
81 PincodeWindow::InitUI()
82 {
83 	SetLayout(new BGroupLayout(B_HORIZONTAL));
84 
85 	fMessage = new BStringView("Pincode", "Please enter the pincode ...");
86 	fMessage->SetFont(be_bold_font);
87 
88 	fRemoteInfo = new BStringView("bdaddr","BD_ADDR: ");
89 
90 	// TODO: Pincode cannot be more than 16 bytes
91 	fPincodeText = new BTextControl("pincode TextControl", "PIN code:",
92 		"5555", NULL);
93 
94 	fAcceptButton = new BButton("fAcceptButton", "Pair",
95 		new BMessage(skMessageAcceptButton));
96 
97 	fCancelButton = new BButton("fCancelButton", "Cancel",
98 		new BMessage(skMessageCancelButton));
99 
100 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 10)
101 				.Add(fMessage)
102 				.Add(fRemoteInfo)
103 				.Add(fPincodeText)
104 				.Add(BGroupLayoutBuilder(B_HORIZONTAL, 10)
105 					.AddGlue()
106 					.Add(fCancelButton)
107 					.Add(fAcceptButton)
108 					.SetInsets(5, 5, 5, 5)
109 				)
110 			.SetInsets(15, 15, 15, 15)
111 	);
112 }
113 
114 
115 void
116 PincodeWindow::MessageReceived(BMessage *msg)
117 {
118 //	status_t err = B_OK;
119 
120 	switch(msg->what)
121 	{
122 		case skMessageAcceptButton:
123 		{
124 			BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
125 			BMessage reply;
126 			size_t size;
127 			int8 bt_status = BT_ERROR;
128 
129 			void* command = buildPinCodeRequestReply(fBdaddr,
130 				strlen(fPincodeText->Text()),
131 				(char*)fPincodeText->Text(), &size);
132 
133 			if (command == NULL) {
134 				break;
135 			}
136 
137 			request.AddInt32("hci_id", fHid);
138 			request.AddData("raw command", B_ANY_TYPE, command, size);
139 			request.AddInt16("eventExpected",  HCI_EVENT_CMD_COMPLETE);
140 			request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL,
141 				OCF_PIN_CODE_REPLY));
142 
143 			// we reside in the server
144 			if (be_app_messenger.SendMessage(&request, &reply) == B_OK) {
145 				if (reply.FindInt8("status", &bt_status ) == B_OK ) {
146 					PostMessage(B_QUIT_REQUESTED);
147 				}
148 				// TODO: something failed here
149 			}
150 			break;
151 		}
152 
153 		case skMessageCancelButton:
154 		{
155 			BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
156 			BMessage reply;
157 			size_t size;
158 			int8 bt_status = BT_ERROR;
159 
160 			void* command = buildPinCodeRequestNegativeReply(fBdaddr, &size);
161 
162 			if (command == NULL) {
163 				break;
164 			}
165 
166 			request.AddInt32("hci_id", fHid);
167 			request.AddData("raw command", B_ANY_TYPE, command, size);
168 			request.AddInt16("eventExpected",  HCI_EVENT_CMD_COMPLETE);
169 			request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL,
170 				OCF_PIN_CODE_NEG_REPLY));
171 
172 			if (be_app_messenger.SendMessage(&request, &reply) == B_OK) {
173 				if (reply.FindInt8("status", &bt_status ) == B_OK ) {
174 					PostMessage(B_QUIT_REQUESTED);
175 				}
176 				// TODO: something failed here
177 			}
178 			break;
179 		}
180 
181 		default:
182 			BWindow::MessageReceived(msg);
183 			break;
184 	}
185 }
186 
187 
188 bool PincodeWindow::QuitRequested()
189 {
190 	return BWindow::QuitRequested();
191 }
192 
193 
194 void PincodeWindow::SetBDaddr(BString address)
195 {
196 	BString label;
197 
198 	label << BD_ADDR_LABEL << address;
199 	printf("++ %s\n",label.String());
200 	fRemoteInfo->SetText(label.String());
201 	fRemoteInfo->ResizeToPreferred();
202 	//Invalidate();
203 
204 }
205 
206 
207 } /* end namespace Bluetooth */
208