xref: /haiku/headers/private/bluetooth/CommandManager.h (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2  * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3  * Copyright 2008 Mika Lindqvist
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #ifndef _COMMAND_MANAGER_H
7 #define _COMMAND_MANAGER_H
8 
9 #include <malloc.h>
10 #include <string.h>
11 
12 #include <bluetooth/bluetooth.h>
13 #include <bluetooth/bluetooth_error.h>
14 #include <bluetooth/HCI/btHCI_command.h>
15 #include <bluetooth/HCI/btHCI_event.h>
16 
17 #include <Message.h>
18 #include <Messenger.h>
19 
20 #include <bluetoothserver_p.h>
21 
22 #define typed_command(type) type, sizeof(type)
23 
24 template <typename Type = void, int paramSize = 0,
25 	int HeaderSize = HCI_COMMAND_HDR_SIZE>
26 class BluetoothCommand {
27 
28 public:
29 	BluetoothCommand(uint8 ogf, uint8 ocf)
30 	{
31 		fHeader = (struct hci_command_header*) fBuffer;
32 
33 		if (paramSize != 0)
34 			fContent = (Type*)(fHeader + 1);
35 		else
36 			// avoid pointing outside in case of not having parameters
37 			fContent = (Type*)fHeader;
38 
39 		fHeader->opcode = B_HOST_TO_LENDIAN_INT16(PACK_OPCODE(ogf, ocf));
40 		fHeader->clen = paramSize;
41 	}
42 
43 	Type*
44 	operator->() const
45 	{
46  		return fContent;
47 	}
48 
49 	void*
50 	Data() const
51 	{
52 		return (void*)fBuffer;
53 	}
54 
55 	size_t Size() const
56 	{
57 		return HeaderSize + paramSize;
58 	}
59 
60 private:
61 	char fBuffer[paramSize + HeaderSize];
62 	Type* fContent;
63 	struct hci_command_header* fHeader;
64 };
65 
66 
67 status_t
68 NonParameterCommandRequest(uint8 ofg, uint8 ocf, int32* result, hci_id hId,
69 	BMessenger* messenger);
70 
71 template<typename PARAMETERCONTAINER, typename PARAMETERTYPE>
72 status_t
73 SingleParameterCommandRequest(uint8 ofg, uint8 ocf, PARAMETERTYPE parameter,
74 	int32* result, hci_id hId, BMessenger* messenger)
75 {
76 	int8 bt_status = BT_ERROR;
77 
78 	BluetoothCommand<typed_command(PARAMETERCONTAINER)>
79 		simpleCommand(ofg, ocf);
80 
81 	simpleCommand->param = parameter;
82 
83 	BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
84 	BMessage reply;
85 
86 	simpleCommand->param = parameter;
87 
88 	request.AddInt32("hci_id", hId);
89 	request.AddData("raw command", B_ANY_TYPE, simpleCommand.Data(),
90 		simpleCommand.Size());
91 	request.AddInt16("eventExpected",  HCI_EVENT_CMD_COMPLETE);
92 	request.AddInt16("opcodeExpected", PACK_OPCODE(ofg, ocf));
93 
94 	if (messenger->SendMessage(&request, &reply) == B_OK) {
95 		reply.FindInt8("status", &bt_status);
96 		if (result != NULL)
97 			reply.FindInt32("result", result);
98 	}
99 
100 	return bt_status;
101 }
102 
103 
104 /* CONTROL BASEBAND */
105 void* buildReset(size_t* outsize);
106 void* buildReadLocalName(size_t* outsize);
107 void* buildWriteScan(uint8 scanmode, size_t* outsize);
108 void* buildReadClassOfDevice(size_t* outsize);
109 
110 /* LINK CONTROL */
111 void* buildRemoteNameRequest(bdaddr_t bdaddr, uint8 pscan_rep_mode,
112 	uint16 clock_offset, size_t* outsize);
113 void* buildInquiry(uint32 lap, uint8 length, uint8 num_rsp, size_t* outsize);
114 void* buildInquiryCancel(size_t* outsize);
115 void* buildPinCodeRequestReply(bdaddr_t bdaddr, uint8 length, char pincode[16],
116 	size_t* outsize);
117 void* buildPinCodeRequestNegativeReply(bdaddr_t bdaddr, size_t* outsize);
118 void* buildAcceptConnectionRequest(bdaddr_t bdaddr, uint8 role,
119 	size_t* outsize);
120 void* buildRejectConnectionRequest(bdaddr_t bdaddr, size_t* outsize);
121 
122 /* OGF_INFORMATIONAL_PARAM */
123 void* buildReadLocalVersionInformation(size_t* outsize);
124 void* buildReadBufferSize(size_t* outsize);
125 void* buildReadBdAddr(size_t* outsize);
126 
127 #endif
128