From d23e32fc4e3302470f02ab5522b7b8762bac95be Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 7 May 2013 01:05:31 +0200 Subject: [PATCH] The SPP Class now inherit from the Arduino Stream library --- _b_t_d_8cpp_source.html | 2060 ++++++++--------- _p_s3_b_t_8cpp.html | 34 +- _p_s3_b_t_8cpp_source.html | 1211 +++++----- _p_s3_b_t_8h.html | 70 +- _p_s3_b_t_8h_source.html | 225 +- _p_s3_enums_8h.html | 83 +- _p_s3_enums_8h_source.html | 280 +-- _p_s3_u_s_b_8cpp.html | 56 +- _p_s3_u_s_b_8cpp_source.html | 1016 ++++---- _p_s3_u_s_b_8h.html | 38 +- _p_s3_u_s_b_8h_source.html | 153 +- _s_p_p_8cpp_source.html | 1592 ++++++------- _s_p_p_8h_source.html | 230 +- _wii_8cpp_source.html | 46 +- _wii_8h_source.html | 408 ++-- _x_b_o_x_r_e_c_v_8cpp_source.html | 12 +- _x_b_o_x_u_s_b_8cpp_source.html | 4 +- annotated.html | 121 +- avrpins_8h.html | 2 - cdcacm_8cpp_source.html | 593 ++--- cdcacm_8h.html | 132 +- cdcacm_8h_source.html | 208 +- class_a_c_m-members.html | 17 +- class_a_c_m.html | 137 +- class_b_t_d.html | 60 +- class_bluetooth_service.html | 8 +- class_c_d_c_async_oper.html | 2 +- class_hex_dumper.html | 4 - class_keyboard_report_parser-members.html | 2 +- class_keyboard_report_parser.html | 4 +- class_p_l2303-members.html | 17 +- class_p_l2303.html | 6 + class_p_s3_b_t.html | 60 +- class_p_s3_u_s_b.html | 68 +- class_s_p_p-members.html | 44 +- class_s_p_p.html | 1204 ++-------- class_s_p_p__coll__graph.md5 | 2 +- class_s_p_p__coll__graph.png | Bin 1943 -> 3828 bytes class_s_p_p__inherit__graph.md5 | 2 +- class_s_p_p__inherit__graph.png | Bin 1943 -> 3828 bytes class_u_s_b_device_config.html | 6 +- class_w_i_i-members.html | 47 +- class_w_i_i.html | 119 +- classes.html | 68 +- functions.html | 2 +- functions_0x62.html | 41 +- functions_0x64.html | 9 +- functions_0x66.html | 2 +- functions_0x67.html | 15 +- functions_0x69.html | 14 +- functions_0x6f.html | 3 + functions_0x70.html | 67 +- functions_0x72.html | 19 +- functions_0x73.html | 26 +- functions_0x77.html | 15 +- functions_func.html | 2 +- functions_func_0x64.html | 5 +- functions_func_0x66.html | 2 +- functions_func_0x67.html | 15 +- functions_func_0x69.html | 10 +- functions_func_0x6f.html | 3 + functions_func_0x70.html | 47 +- functions_func_0x72.html | 6 +- functions_func_0x73.html | 32 +- functions_func_0x77.html | 4 +- functions_vars_0x62.html | 37 +- functions_vars_0x72.html | 5 +- functions_vars_0x77.html | 7 +- globals_0x61.html | 3 + globals_0x63.html | 6 + globals_0x6c.html | 7 +- globals_0x6d.html | 5 +- globals_0x6e.html | 23 +- globals_0x6f.html | 6 - globals_0x70.html | 4 +- globals_0x72.html | 6 + globals_0x73.html | 3 + globals_defs_0x61.html | 3 + globals_defs_0x63.html | 6 + globals_defs_0x6c.html | 7 +- globals_defs_0x6d.html | 2 +- globals_defs_0x6e.html | 3 + globals_defs_0x6f.html | 3 - globals_defs_0x70.html | 2 +- globals_defs_0x72.html | 6 + globals_defs_0x73.html | 3 + globals_func.html | 6 +- globals_vars.html | 2 - globals_vars_0x62.html | 2 - globals_vars_0x6c.html | 2 - globals_vars_0x70.html | 4 +- globals_vars_0x72.html | 2 - globals_vars_0x75.html | 2 - globals_vars_0x78.html | 2 - hexdump_8h.html | 2 - hidboot_8h_source.html | 2 +- hidusagestr_8h.html | 2 - hierarchy.html | 135 +- inherit_graph_2.map | 5 +- inherit_graph_2.md5 | 2 +- inherit_graph_2.png | Bin 4309 -> 951 bytes inherit_graph_3.map | 2 +- inherit_graph_3.md5 | 2 +- inherit_graph_3.png | Bin 951 -> 1488 bytes inherit_graph_32.map | 5 +- inherit_graph_32.md5 | 2 +- inherit_graph_32.png | Bin 1153 -> 5030 bytes inherit_graph_33.map | 2 +- inherit_graph_33.md5 | 2 +- inherit_graph_33.png | Bin 1403 -> 1153 bytes inherit_graph_34.map | 2 +- inherit_graph_34.md5 | 2 +- inherit_graph_34.png | Bin 1035 -> 1403 bytes inherit_graph_35.map | 2 +- inherit_graph_35.md5 | 2 +- inherit_graph_35.png | Bin 1044 -> 1035 bytes inherit_graph_36.map | 2 +- inherit_graph_36.md5 | 2 +- inherit_graph_36.png | Bin 1803 -> 1044 bytes inherit_graph_37.map | 2 +- inherit_graph_37.md5 | 2 +- inherit_graph_37.png | Bin 1509 -> 1803 bytes inherit_graph_38.map | 2 +- inherit_graph_38.md5 | 2 +- inherit_graph_38.png | Bin 1560 -> 1509 bytes inherit_graph_39.map | 2 +- inherit_graph_39.md5 | 2 +- inherit_graph_39.png | Bin 1394 -> 1560 bytes inherit_graph_4.map | 2 +- inherit_graph_4.md5 | 2 +- inherit_graph_4.png | Bin 1488 -> 799 bytes inherit_graph_40.map | 2 +- inherit_graph_40.md5 | 2 +- inherit_graph_40.png | Bin 1669 -> 1394 bytes inherit_graph_41.map | 2 +- inherit_graph_41.md5 | 2 +- inherit_graph_41.png | Bin 845 -> 1669 bytes inherit_graph_42.map | 2 +- inherit_graph_42.md5 | 2 +- inherit_graph_42.png | Bin 1133 -> 845 bytes inherit_graph_43.map | 16 +- inherit_graph_43.md5 | 2 +- inherit_graph_43.png | Bin 34905 -> 1133 bytes inherit_graph_44.map | 20 +- inherit_graph_44.md5 | 2 +- inherit_graph_44.png | Bin 8062 -> 34905 bytes inherit_graph_5.map | 2 +- inherit_graph_5.md5 | 2 +- inherit_graph_5.png | Bin 799 -> 1157 bytes inherit_graph_6.map | 2 +- inherit_graph_6.md5 | 2 +- inherit_graph_6.png | Bin 1157 -> 1135 bytes inherits.html | 46 +- message_8cpp.html | 72 +- message_8cpp_source.html | 66 +- message_8h.html | 74 +- message_8h_source.html | 58 +- printhex_8h.html | 2 - search/all_61.js | 3 +- search/all_62.js | 4 +- search/all_63.js | 3 + search/all_64.js | 1 - search/all_66.js | 2 +- search/all_67.js | 1 + search/all_69.js | 3 +- search/all_6c.js | 1 + search/all_6d.js | 3 +- search/all_6e.js | 3 +- search/all_6f.js | 3 +- search/all_70.js | 12 +- search/all_72.js | 6 +- search/all_73.js | 2 + search/all_77.js | 8 +- search/classes_63.js | 1 + search/defines_61.js | 3 +- search/defines_63.js | 2 + search/defines_6c.js | 1 + search/defines_6d.js | 2 +- search/defines_6e.js | 1 + search/defines_6f.js | 3 +- search/defines_70.js | 2 +- search/defines_72.js | 2 + search/defines_73.js | 1 + search/functions_61.js | 2 +- search/functions_64.js | 3 +- search/functions_66.js | 2 +- search/functions_67.js | 1 + search/functions_69.js | 3 +- search/functions_6e.js | 2 +- search/functions_6f.js | 1 + search/functions_70.js | 7 +- search/functions_72.js | 2 +- search/functions_73.js | 1 + search/functions_77.js | 2 +- search/variables_62.js | 4 +- search/variables_6d.js | 1 - search/variables_6f.js | 1 - search/variables_70.js | 2 +- search/variables_72.js | 2 +- search/variables_77.js | 6 +- struct_a_c_m___f_u_n_c___d_e_s_c_r.html | 10 +- ...l_l___m_g_m_n_t___f_u_n_c___d_e_s_c_r.html | 12 +- ...t_c_l_a_s_s___n_o_t_i_f_i_c_a_t_i_o_n.html | 14 +- struct_l_i_n_e___c_o_d_i_n_g.html | 10 +- ...l___r_i_n_g_e_r___f_u_n_c___d_e_s_c_r.html | 12 +- usb__ch9_8h.html | 2 - 206 files changed, 5530 insertions(+), 6173 deletions(-) diff --git a/_b_t_d_8cpp_source.html b/_b_t_d_8cpp_source.html index d6beedfa..9c47405d 100644 --- a/_b_t_d_8cpp_source.html +++ b/_b_t_d_8cpp_source.html @@ -243,1076 +243,1072 @@ var searchBox = new SearchBox("searchBox", "search",false,'Search');
152  else // must be a navigation controller
153  Notify(PSTR("\r\nNavigation Controller Connected"), 0x80);
154 #endif
-
155  /* Set internal bluetooth address */
+
155  /* Set internal Bluetooth address */
156  setBdaddr(my_bdaddr);
-
157  } else { // must be a Motion controller
+
157  } else { // It must be a Motion controller
158 #ifdef DEBUG
159  Notify(PSTR("\r\nMotion Controller Connected"), 0x80);
160 #endif
161  setMoveBdaddr(my_bdaddr);
162  }
-
163  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 0); // Reset configuration value
-
164  pUsb->setAddr(bAddress, 0, 0); // Reset address
-
165  Release(); // Release device
-
166  return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; // return
-
167  } else {
-
168  num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
-
169 
-
170  // check if attached device is a Bluetooth dongle and fill endpoint data structure
-
171  // first interface in the configuration must have Bluetooth assigned Class/Subclass/Protocol
-
172  // and 3 endpoints - interrupt-IN, bulk-IN, bulk-OUT,
-
173  // not necessarily in this order
-
174  for (uint8_t i = 0; i < num_of_conf; i++) {
-
175  ConfigDescParser<USB_CLASS_WIRELESS_CTRL, WI_SUBCLASS_RF, WI_PROTOCOL_BT, CP_MASK_COMPARE_ALL> confDescrParser(this);
-
176  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
-
177  if (rcode)
-
178  goto FailGetConfDescr;
-
179  if (bNumEP >= BTD_MAX_ENDPOINTS) // All endpoints extracted
-
180  break;
-
181  }
-
182 
-
183  if (bNumEP < BTD_MAX_ENDPOINTS)
-
184  goto FailUnknownDevice;
-
185 
-
186  // Assign epInfo to epinfo pointer - this time all 3 endpoins
-
187  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
-
188  if (rcode)
-
189  goto FailSetDevTblEntry;
+
163 #ifdef DEBUG
+
164  Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
+
165  for (int8_t i = 5; i > 0; i--) {
+
166  PrintHex<uint8_t > (my_bdaddr[i], 0x80);
+
167  Notify(PSTR(":"), 0x80);
+
168  }
+
169  PrintHex<uint8_t > (my_bdaddr[0], 0x80);
+
170 #endif
+
171  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 0); // Reset configuration value
+
172  pUsb->setAddr(bAddress, 0, 0); // Reset address
+
173  Release(); // Release device
+
174  return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; // return
+
175  } else {
+
176  num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
+
177 
+
178  // check if attached device is a Bluetooth dongle and fill endpoint data structure
+
179  // first interface in the configuration must have Bluetooth assigned Class/Subclass/Protocol
+
180  // and 3 endpoints - interrupt-IN, bulk-IN, bulk-OUT,
+
181  // not necessarily in this order
+
182  for (uint8_t i = 0; i < num_of_conf; i++) {
+
183  ConfigDescParser<USB_CLASS_WIRELESS_CTRL, WI_SUBCLASS_RF, WI_PROTOCOL_BT, CP_MASK_COMPARE_ALL> confDescrParser(this);
+
184  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
+
185  if (rcode)
+
186  goto FailGetConfDescr;
+
187  if (bNumEP >= BTD_MAX_ENDPOINTS) // All endpoints extracted
+
188  break;
+
189  }
190 
-
191  delay(200); // Give time for address change
-
192 
-
193  // Set Configuration Value
-
194  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bConfNum);
-
195  if (rcode)
-
196  goto FailSetConfDescr;
-
197 
-
198  hci_num_reset_loops = 100; // only loop 100 times before trying to send the hci reset command
-
199  hci_counter = 0;
-
200  hci_state = HCI_INIT_STATE;
-
201  watingForConnection = false;
-
202  bPollEnable = true;
-
203 
-
204 #ifdef DEBUG
-
205  Notify(PSTR("\r\nBluetooth Dongle Initialized"), 0x80);
-
206 #endif
-
207  }
-
208  return 0; // Successful configuration
-
209 
-
210  /* diagnostic messages */
-
211 FailGetDevDescr:
-
212  NotifyFailGetDevDescr();
-
213  goto Fail;
-
214 
-
215 FailSetDevTblEntry:
-
216  NotifyFailSetDevTblEntry();
-
217  goto Fail;
-
218 
-
219 FailGetConfDescr:
-
220  NotifyFailGetConfDescr();
+
191  if (bNumEP < BTD_MAX_ENDPOINTS)
+
192  goto FailUnknownDevice;
+
193 
+
194  // Assign epInfo to epinfo pointer - this time all 3 endpoins
+
195  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
+
196  if (rcode)
+
197  goto FailSetDevTblEntry;
+
198 
+
199  delay(200); // Give time for address change
+
200 
+
201  // Set Configuration Value
+
202  rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bConfNum);
+
203  if (rcode)
+
204  goto FailSetConfDescr;
+
205 
+
206  hci_num_reset_loops = 100; // only loop 100 times before trying to send the hci reset command
+
207  hci_counter = 0;
+
208  hci_state = HCI_INIT_STATE;
+
209  watingForConnection = false;
+
210  bPollEnable = true;
+
211 
+
212 #ifdef DEBUG
+
213  Notify(PSTR("\r\nBluetooth Dongle Initialized"), 0x80);
+
214 #endif
+
215  }
+
216  return 0; // Successful configuration
+
217 
+
218  /* diagnostic messages */
+
219 FailGetDevDescr:
+
220  NotifyFailGetDevDescr();
221  goto Fail;
222 
-
223 FailSetConfDescr:
-
224  NotifyFailSetConfDescr();
+
223 FailSetDevTblEntry:
+
224  NotifyFailSetDevTblEntry();
225  goto Fail;
226 
-
227 FailUnknownDevice:
-
228  NotifyFailUnknownDevice(VID,PID);
-
229  pUsb->setAddr(bAddress, 0, 0); // Reset address
-
230  rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
-
231 Fail:
-
232 #ifdef DEBUG
-
233  Notify(PSTR("\r\nBTD Init Failed, error code: "), 0x80);
-
234 #endif
-
235  NotifyFail(rcode);
-
236  Release();
-
237  return rcode;
-
238 }
-
239 
-
240 /* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */
-
241 void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
-
242  //ErrorMessage<uint8_t>(PSTR("Conf.Val"),conf);
-
243  //ErrorMessage<uint8_t>(PSTR("Iface Num"),iface);
-
244  //ErrorMessage<uint8_t>(PSTR("Alt.Set"),alt);
-
245 
-
246  if (alt) // wrong interface - by BT spec, no alt setting
-
247  return;
-
248 
-
249  bConfNum = conf;
-
250  uint8_t index;
-
251 
-
252  if ((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) // Interrupt In endpoint found
-
253  index = BTD_EVENT_PIPE;
-
254 
-
255  else {
-
256  if ((pep->bmAttributes & 0x02) == 2) // bulk endpoint found
-
257  index = ((pep->bEndpointAddress & 0x80) == 0x80) ? BTD_DATAIN_PIPE : BTD_DATAOUT_PIPE;
-
258  else
-
259  return;
-
260  }
-
261 
-
262  // Fill the rest of endpoint data structure
-
263  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
-
264  epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
-
265 #ifdef EXTRADEBUG
-
266  PrintEndpointDescriptor(pep);
-
267 #endif
-
268  if (pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
-
269  pollInterval = pep->bInterval;
-
270  bNumEP++;
-
271 }
-
272 
-
273 void BTD::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) {
-
274 #ifdef EXTRADEBUG
-
275  Notify(PSTR("\r\nEndpoint descriptor:"), 0x80);
-
276  Notify(PSTR("\r\nLength:\t\t"), 0x80);
-
277  PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
-
278  Notify(PSTR("\r\nType:\t\t"), 0x80);
-
279  PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
-
280  Notify(PSTR("\r\nAddress:\t"), 0x80);
-
281  PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
-
282  Notify(PSTR("\r\nAttributes:\t"), 0x80);
-
283  PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
-
284  Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
-
285  PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
-
286  Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
-
287  PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
-
288 #endif
-
289 }
-
290 
-
291 /* Performs a cleanup after failed Init() attempt */
-
292 uint8_t BTD::Release() {
-
293  for (uint8_t i = 0; i < BTD_NUMSERVICES; i++)
-
294  if (btService[i])
-
295  btService[i]->Reset(); // Reset all Bluetooth services
-
296  pUsb->GetAddressPool().FreeAddress(bAddress);
-
297  bAddress = 0;
-
298  bPollEnable = false;
-
299  bNumEP = 1; // must have to be reset to 1
-
300  return 0;
-
301 }
-
302 
-
303 uint8_t BTD::Poll() {
-
304  if (!bPollEnable)
-
305  return 0;
-
306  if (qNextPollTime <= millis()) { // Don't poll if shorter than polling interval
-
307  qNextPollTime = millis() + pollInterval; // Set new poll time
-
308  HCI_event_task(); // poll the HCI event pipe
-
309  ACL_event_task(); // start polling the ACL input pipe too, though discard data until connected
-
310  }
-
311  return 0;
-
312 }
-
313 
-
314 void BTD::HCI_event_task() {
-
315  /* check the event pipe*/
-
316  uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE; // Request more than 16 bytes anyway, the inTransfer routine will take care of this
-
317  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_EVENT_PIPE ].epAddr, &MAX_BUFFER_SIZE, hcibuf); // input on endpoint 1
-
318  if (!rcode) // Check for errors
-
319  {
-
320  switch (hcibuf[0]) //switch on event type
-
321  {
-
322  case EV_COMMAND_COMPLETE:
-
323  if (!hcibuf[5]) { // Check if command succeeded
-
324  hci_event_flag |= HCI_FLAG_CMD_COMPLETE; // set command complete flag
-
325  if ((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) { // parameters from read local version information
-
326  hci_version = hcibuf[6]; // Used to check if it supports 2.0+EDR - see http://www.bluetooth.org/Technical/AssignedNumbers/hci.htm
-
327  hci_event_flag |= HCI_FLAG_READ_VERSION;
-
328  } else if ((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) { // parameters from read local bluetooth address
-
329  for (uint8_t i = 0; i < 6; i++)
-
330  my_bdaddr[i] = hcibuf[6 + i];
-
331  hci_event_flag |= HCI_FLAG_READ_BDADDR;
-
332  }
-
333  }
-
334  break;
-
335 
-
336  case EV_COMMAND_STATUS:
-
337  if (hcibuf[2]) { // show status on serial if not OK
-
338 #ifdef DEBUG
-
339  Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
-
340  PrintHex<uint8_t > (hcibuf[2], 0x80);
-
341  Notify(PSTR(" "), 0x80);
-
342  PrintHex<uint8_t > (hcibuf[4], 0x80);
-
343  Notify(PSTR(" "), 0x80);
-
344  PrintHex<uint8_t > (hcibuf[5], 0x80);
-
345 #endif
-
346  }
-
347  break;
-
348 
-
349  case EV_INQUIRY_COMPLETE:
-
350  if (inquiry_counter >= 5) {
-
351  inquiry_counter = 0;
-
352 #ifdef DEBUG
-
353  Notify(PSTR("\r\nCouldn't find Wiimote"), 0x80);
-
354 #endif
-
355  connectToWii = false;
-
356  pairWithWii = false;
-
357  hci_state = HCI_SCANNING_STATE;
-
358  }
-
359  inquiry_counter++;
-
360  break;
-
361 
-
362  case EV_INQUIRY_RESULT:
-
363  if (hcibuf[2]) { // Check that there is more than zero responses
-
364 #ifdef EXTRADEBUG
-
365  Notify(PSTR("\r\nNumber of responses: "), 0x80);
-
366  Serial.print(hcibuf[2]);
-
367 #endif
-
368  for (uint8_t i = 0; i < hcibuf[2]; i++) {
-
369  if ((hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x04 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x25 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00) || (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x05 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00)) { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html and http://wiibrew.org/wiki/Wiimote#SDP_information
-
370  if (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08) // Check if it's the new Wiimote with motion plus inside that was detected
-
371  motionPlusInside = true;
-
372  else
-
373  motionPlusInside = false;
-
374  disc_bdaddr[0] = hcibuf[3 + 6 * i];
-
375  disc_bdaddr[1] = hcibuf[4 + 6 * i];
-
376  disc_bdaddr[2] = hcibuf[5 + 6 * i];
-
377  disc_bdaddr[3] = hcibuf[6 + 6 * i];
-
378  disc_bdaddr[4] = hcibuf[7 + 6 * i];
-
379  disc_bdaddr[5] = hcibuf[8 + 6 * i];
-
380  hci_event_flag |= HCI_FLAG_WII_FOUND;
-
381  break;
-
382  }
-
383 #ifdef EXTRADEBUG
-
384  else {
-
385  Notify(PSTR("\r\nClass of device: "), 0x80);
-
386  PrintHex<uint8_t > (hcibuf[6 + 8 * hcibuf[2] + 3 * i], 0x80);
-
387  Notify(PSTR(" "), 0x80);
-
388  PrintHex<uint8_t > (hcibuf[5 + 8 * hcibuf[2] + 3 * i], 0x80);
-
389  Notify(PSTR(" "), 0x80);
-
390  PrintHex<uint8_t > (hcibuf[4 + 8 * hcibuf[2] + 3 * i], 0x80);
-
391  }
-
392 #endif
-
393  }
-
394  }
-
395  break;
-
396 
-
397  case EV_CONNECT_COMPLETE:
-
398  hci_event_flag |= HCI_FLAG_CONNECT_EVENT;
-
399  if (!hcibuf[2]) { // check if connected OK
-
400  hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // store the handle for the ACL connection
-
401  hci_event_flag |= HCI_FLAG_CONN_COMPLETE; // set connection complete flag
-
402  }
-
403 #ifdef EXTRADEBUG
-
404  else {
-
405  Notify(PSTR("\r\nConnection Failed"), 0x80);
-
406  hci_state = HCI_CHECK_WII_SERVICE;
-
407  }
-
408 #endif
-
409  break;
-
410 
-
411  case EV_DISCONNECT_COMPLETE:
-
412  if (!hcibuf[2]) { // check if disconnected OK
-
413  hci_event_flag |= HCI_FLAG_DISCONN_COMPLETE; // set disconnect command complete flag
-
414  hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE; // clear connection complete flag
-
415  }
-
416  break;
-
417 
-
418  case EV_REMOTE_NAME_COMPLETE:
-
419  if (!hcibuf[2]) { // check if reading is OK
-
420  for (uint8_t i = 0; i < min(sizeof(remote_name),sizeof(hcibuf)-9); i++)
-
421  remote_name[i] = hcibuf[9 + i];
-
422  hci_event_flag |= HCI_FLAG_REMOTE_NAME_COMPLETE;
-
423  }
-
424  break;
-
425 
-
426  case EV_INCOMING_CONNECT:
-
427  disc_bdaddr[0] = hcibuf[2];
-
428  disc_bdaddr[1] = hcibuf[3];
-
429  disc_bdaddr[2] = hcibuf[4];
-
430  disc_bdaddr[3] = hcibuf[5];
-
431  disc_bdaddr[4] = hcibuf[6];
-
432  disc_bdaddr[5] = hcibuf[7];
-
433 #ifdef EXTRADEBUG
-
434  Notify(PSTR("\r\nClass of device: "), 0x80);
-
435  PrintHex<uint8_t > (hcibuf[10], 0x80);
-
436  Notify(PSTR(" "), 0x80);
-
437  PrintHex<uint8_t > (hcibuf[9], 0x80);
-
438  Notify(PSTR(" "), 0x80);
-
439  PrintHex<uint8_t > (hcibuf[8], 0x80);
-
440 #endif
-
441  hci_event_flag |= HCI_FLAG_INCOMING_REQUEST;
-
442  break;
-
443 
-
444  case EV_PIN_CODE_REQUEST:
-
445  if (pairWithWii) {
-
446 #ifdef DEBUG
-
447  Notify(PSTR("\r\nPairing with wiimote"), 0x80);
-
448 #endif
-
449  hci_pin_code_request_reply();
-
450  } else if (btdPin != NULL) {
-
451 #ifdef DEBUG
-
452  Notify(PSTR("\r\nBluetooth pin is set too: "), 0x80);
-
453  Serial.print(btdPin);
-
454 #endif
-
455  hci_pin_code_request_reply();
-
456  } else {
-
457 #ifdef DEBUG
-
458  Notify(PSTR("\r\nNo pin was set"), 0x80);
-
459 #endif
-
460  hci_pin_code_negative_request_reply();
-
461  }
-
462  break;
-
463 
-
464  case EV_LINK_KEY_REQUEST:
-
465 #ifdef DEBUG
-
466  Notify(PSTR("\r\nReceived Key Request"), 0x80);
-
467 #endif
-
468  hci_link_key_request_negative_reply();
-
469  break;
-
470 
-
471  case EV_AUTHENTICATION_COMPLETE:
-
472  if (pairWithWii && !connectToWii) {
-
473 #ifdef DEBUG
-
474  Notify(PSTR("\r\nPairing successful"), 0x80);
-
475 #endif
-
476  connectToWii = true; // Only send the ACL data to the Wii service
-
477  }
-
478  break;
-
479  /* We will just ignore the following events */
-
480  case EV_NUM_COMPLETE_PKT:
-
481  case EV_ROLE_CHANGED:
-
482  case EV_PAGE_SCAN_REP_MODE:
-
483  case EV_LOOPBACK_COMMAND:
-
484  case EV_DATA_BUFFER_OVERFLOW:
-
485  case EV_CHANGE_CONNECTION_LINK:
-
486  case EV_MAX_SLOTS_CHANGE:
-
487  case EV_QOS_SETUP_COMPLETE:
-
488  case EV_LINK_KEY_NOTIFICATION:
-
489  case EV_ENCRYPTION_CHANGE:
-
490  case EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE:
-
491  break;
-
492 #ifdef EXTRADEBUG
-
493  default:
-
494  if (hcibuf[0] != 0x00) {
-
495  Notify(PSTR("\r\nUnmanaged HCI Event: "), 0x80);
-
496  PrintHex<uint8_t > (hcibuf[0], 0x80);
-
497  }
-
498  break;
-
499 #endif
-
500  } // switch
-
501  }
+
227 FailGetConfDescr:
+
228  NotifyFailGetConfDescr();
+
229  goto Fail;
+
230 
+
231 FailSetConfDescr:
+
232  NotifyFailSetConfDescr();
+
233  goto Fail;
+
234 
+
235 FailUnknownDevice:
+
236  NotifyFailUnknownDevice(VID,PID);
+
237  pUsb->setAddr(bAddress, 0, 0); // Reset address
+
238  rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
+
239 Fail:
+
240 #ifdef DEBUG
+
241  Notify(PSTR("\r\nBTD Init Failed, error code: "), 0x80);
+
242 #endif
+
243  NotifyFail(rcode);
+
244  Release();
+
245  return rcode;
+
246 }
+
247 
+
248 /* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */
+
249 void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
+
250  //ErrorMessage<uint8_t>(PSTR("Conf.Val"),conf);
+
251  //ErrorMessage<uint8_t>(PSTR("Iface Num"),iface);
+
252  //ErrorMessage<uint8_t>(PSTR("Alt.Set"),alt);
+
253 
+
254  if (alt) // wrong interface - by BT spec, no alt setting
+
255  return;
+
256 
+
257  bConfNum = conf;
+
258  uint8_t index;
+
259 
+
260  if ((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) // Interrupt In endpoint found
+
261  index = BTD_EVENT_PIPE;
+
262 
+
263  else {
+
264  if ((pep->bmAttributes & 0x02) == 2) // bulk endpoint found
+
265  index = ((pep->bEndpointAddress & 0x80) == 0x80) ? BTD_DATAIN_PIPE : BTD_DATAOUT_PIPE;
+
266  else
+
267  return;
+
268  }
+
269 
+
270  // Fill the rest of endpoint data structure
+
271  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
+
272  epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
+
273 #ifdef EXTRADEBUG
+
274  PrintEndpointDescriptor(pep);
+
275 #endif
+
276  if (pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
+
277  pollInterval = pep->bInterval;
+
278  bNumEP++;
+
279 }
+
280 
+
281 void BTD::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) {
+
282 #ifdef EXTRADEBUG
+
283  Notify(PSTR("\r\nEndpoint descriptor:"), 0x80);
+
284  Notify(PSTR("\r\nLength:\t\t"), 0x80);
+
285  PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
+
286  Notify(PSTR("\r\nType:\t\t"), 0x80);
+
287  PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
+
288  Notify(PSTR("\r\nAddress:\t"), 0x80);
+
289  PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
+
290  Notify(PSTR("\r\nAttributes:\t"), 0x80);
+
291  PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
+
292  Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
+
293  PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
+
294  Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
+
295  PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
+
296 #endif
+
297 }
+
298 
+
299 /* Performs a cleanup after failed Init() attempt */
+
300 uint8_t BTD::Release() {
+
301  for (uint8_t i = 0; i < BTD_NUMSERVICES; i++) {
+
302  if (btService[i])
+
303  btService[i]->Reset(); // Reset all Bluetooth services
+
304  }
+
305 
+
306  pUsb->GetAddressPool().FreeAddress(bAddress);
+
307  bAddress = 0;
+
308  bPollEnable = false;
+
309  bNumEP = 1; // must have to be reset to 1
+
310  return 0;
+
311 }
+
312 
+
313 uint8_t BTD::Poll() {
+
314  if (!bPollEnable)
+
315  return 0;
+
316  if (qNextPollTime <= millis()) { // Don't poll if shorter than polling interval
+
317  qNextPollTime = millis() + pollInterval; // Set new poll time
+
318  HCI_event_task(); // poll the HCI event pipe
+
319  ACL_event_task(); // start polling the ACL input pipe too, though discard data until connected
+
320  }
+
321  return 0;
+
322 }
+
323 
+
324 void BTD::HCI_event_task() {
+
325  /* check the event pipe*/
+
326  uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE; // Request more than 16 bytes anyway, the inTransfer routine will take care of this
+
327  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_EVENT_PIPE ].epAddr, &MAX_BUFFER_SIZE, hcibuf); // input on endpoint 1
+
328  if (!rcode) // Check for errors
+
329  {
+
330  switch (hcibuf[0]) //switch on event type
+
331  {
+
332  case EV_COMMAND_COMPLETE:
+
333  if (!hcibuf[5]) { // Check if command succeeded
+
334  hci_event_flag |= HCI_FLAG_CMD_COMPLETE; // set command complete flag
+
335  if ((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) { // parameters from read local version information
+
336  hci_version = hcibuf[6]; // Used to check if it supports 2.0+EDR - see http://www.bluetooth.org/Technical/AssignedNumbers/hci.htm
+
337  hci_event_flag |= HCI_FLAG_READ_VERSION;
+
338  } else if ((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) { // parameters from read local bluetooth address
+
339  for (uint8_t i = 0; i < 6; i++)
+
340  my_bdaddr[i] = hcibuf[6 + i];
+
341  hci_event_flag |= HCI_FLAG_READ_BDADDR;
+
342  }
+
343  }
+
344  break;
+
345 
+
346  case EV_COMMAND_STATUS:
+
347  if (hcibuf[2]) { // show status on serial if not OK
+
348 #ifdef DEBUG
+
349  Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
+
350  PrintHex<uint8_t > (hcibuf[2], 0x80);
+
351  Notify(PSTR(" "), 0x80);
+
352  PrintHex<uint8_t > (hcibuf[4], 0x80);
+
353  Notify(PSTR(" "), 0x80);
+
354  PrintHex<uint8_t > (hcibuf[5], 0x80);
+
355 #endif
+
356  }
+
357  break;
+
358 
+
359  case EV_INQUIRY_COMPLETE:
+
360  if (inquiry_counter >= 5) {
+
361  inquiry_counter = 0;
+
362 #ifdef DEBUG
+
363  Notify(PSTR("\r\nCouldn't find Wiimote"), 0x80);
+
364 #endif
+
365  connectToWii = false;
+
366  pairWithWii = false;
+
367  hci_state = HCI_SCANNING_STATE;
+
368  }
+
369  inquiry_counter++;
+
370  break;
+
371 
+
372  case EV_INQUIRY_RESULT:
+
373  if (hcibuf[2]) { // Check that there is more than zero responses
+
374 #ifdef EXTRADEBUG
+
375  Notify(PSTR("\r\nNumber of responses: "), 0x80);
+
376  Notify(hcibuf[2], 0x80);
+
377 #endif
+
378  for (uint8_t i = 0; i < hcibuf[2]; i++) {
+
379  if ((hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x04 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x25 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00) || (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x05 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00)) { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html and http://wiibrew.org/wiki/Wiimote#SDP_information
+
380  if (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08) // Check if it's the new Wiimote with motion plus inside that was detected
+
381  motionPlusInside = true;
+
382  else
+
383  motionPlusInside = false;
+
384  disc_bdaddr[0] = hcibuf[3 + 6 * i];
+
385  disc_bdaddr[1] = hcibuf[4 + 6 * i];
+
386  disc_bdaddr[2] = hcibuf[5 + 6 * i];
+
387  disc_bdaddr[3] = hcibuf[6 + 6 * i];
+
388  disc_bdaddr[4] = hcibuf[7 + 6 * i];
+
389  disc_bdaddr[5] = hcibuf[8 + 6 * i];
+
390  hci_event_flag |= HCI_FLAG_WII_FOUND;
+
391  break;
+
392  }
+
393 #ifdef EXTRADEBUG
+
394  else {
+
395  Notify(PSTR("\r\nClass of device: "), 0x80);
+
396  PrintHex<uint8_t > (hcibuf[6 + 8 * hcibuf[2] + 3 * i], 0x80);
+
397  Notify(PSTR(" "), 0x80);
+
398  PrintHex<uint8_t > (hcibuf[5 + 8 * hcibuf[2] + 3 * i], 0x80);
+
399  Notify(PSTR(" "), 0x80);
+
400  PrintHex<uint8_t > (hcibuf[4 + 8 * hcibuf[2] + 3 * i], 0x80);
+
401  }
+
402 #endif
+
403  }
+
404  }
+
405  break;
+
406 
+
407  case EV_CONNECT_COMPLETE:
+
408  hci_event_flag |= HCI_FLAG_CONNECT_EVENT;
+
409  if (!hcibuf[2]) { // check if connected OK
+
410  hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // store the handle for the ACL connection
+
411  hci_event_flag |= HCI_FLAG_CONN_COMPLETE; // set connection complete flag
+
412  }
+
413 #ifdef EXTRADEBUG
+
414  else {
+
415  Notify(PSTR("\r\nConnection Failed"), 0x80);
+
416  hci_state = HCI_CHECK_WII_SERVICE;
+
417  }
+
418 #endif
+
419  break;
+
420 
+
421  case EV_DISCONNECT_COMPLETE:
+
422  if (!hcibuf[2]) { // check if disconnected OK
+
423  hci_event_flag |= HCI_FLAG_DISCONN_COMPLETE; // set disconnect command complete flag
+
424  hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE; // clear connection complete flag
+
425  }
+
426  break;
+
427 
+
428  case EV_REMOTE_NAME_COMPLETE:
+
429  if (!hcibuf[2]) { // check if reading is OK
+
430  for (uint8_t i = 0; i < min(sizeof(remote_name),sizeof(hcibuf)-9); i++)
+
431  remote_name[i] = hcibuf[9 + i];
+
432  hci_event_flag |= HCI_FLAG_REMOTE_NAME_COMPLETE;
+
433  }
+
434  break;
+
435 
+
436  case EV_INCOMING_CONNECT:
+
437  disc_bdaddr[0] = hcibuf[2];
+
438  disc_bdaddr[1] = hcibuf[3];
+
439  disc_bdaddr[2] = hcibuf[4];
+
440  disc_bdaddr[3] = hcibuf[5];
+
441  disc_bdaddr[4] = hcibuf[6];
+
442  disc_bdaddr[5] = hcibuf[7];
+
443 #ifdef EXTRADEBUG
+
444  Notify(PSTR("\r\nClass of device: "), 0x80);
+
445  PrintHex<uint8_t > (hcibuf[10], 0x80);
+
446  Notify(PSTR(" "), 0x80);
+
447  PrintHex<uint8_t > (hcibuf[9], 0x80);
+
448  Notify(PSTR(" "), 0x80);
+
449  PrintHex<uint8_t > (hcibuf[8], 0x80);
+
450 #endif
+
451  hci_event_flag |= HCI_FLAG_INCOMING_REQUEST;
+
452  break;
+
453 
+
454  case EV_PIN_CODE_REQUEST:
+
455  if (pairWithWii) {
+
456 #ifdef DEBUG
+
457  Notify(PSTR("\r\nPairing with wiimote"), 0x80);
+
458 #endif
+
459  hci_pin_code_request_reply();
+
460  } else if (btdPin != NULL) {
+
461 #ifdef DEBUG
+
462  Notify(PSTR("\r\nBluetooth pin is set too: "), 0x80);
+
463  NotifyStr(btdPin, 0x80);
+
464 #endif
+
465  hci_pin_code_request_reply();
+
466  } else {
+
467 #ifdef DEBUG
+
468  Notify(PSTR("\r\nNo pin was set"), 0x80);
+
469 #endif
+
470  hci_pin_code_negative_request_reply();
+
471  }
+
472  break;
+
473 
+
474  case EV_LINK_KEY_REQUEST:
+
475 #ifdef DEBUG
+
476  Notify(PSTR("\r\nReceived Key Request"), 0x80);
+
477 #endif
+
478  hci_link_key_request_negative_reply();
+
479  break;
+
480 
+
481  case EV_AUTHENTICATION_COMPLETE:
+
482  if (pairWithWii && !connectToWii) {
+
483 #ifdef DEBUG
+
484  Notify(PSTR("\r\nPairing successful"), 0x80);
+
485 #endif
+
486  connectToWii = true; // Only send the ACL data to the Wii service
+
487  }
+
488  break;
+
489  /* We will just ignore the following events */
+
490  case EV_NUM_COMPLETE_PKT:
+
491  case EV_ROLE_CHANGED:
+
492  case EV_PAGE_SCAN_REP_MODE:
+
493  case EV_LOOPBACK_COMMAND:
+
494  case EV_DATA_BUFFER_OVERFLOW:
+
495  case EV_CHANGE_CONNECTION_LINK:
+
496  case EV_MAX_SLOTS_CHANGE:
+
497  case EV_QOS_SETUP_COMPLETE:
+
498  case EV_LINK_KEY_NOTIFICATION:
+
499  case EV_ENCRYPTION_CHANGE:
+
500  case EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE:
+
501  break;
502 #ifdef EXTRADEBUG
-
503  else if (rcode != hrNAK) {
-
504  Notify(PSTR("\r\nHCI event error: "), 0x80);
-
505  PrintHex<uint8_t > (rcode, 0x80);
-
506  }
-
507 #endif
-
508  HCI_task();
-
509 }
-
510 
-
511 /* Poll Bluetooth and print result */
-
512 void BTD::HCI_task() {
-
513  switch (hci_state) {
-
514  case HCI_INIT_STATE:
-
515  hci_counter++;
-
516  if (hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events
-
517  hci_reset();
-
518  hci_state = HCI_RESET_STATE;
-
519  hci_counter = 0;
-
520  }
-
521  break;
-
522 
-
523  case HCI_RESET_STATE:
-
524  hci_counter++;
-
525  if (hci_cmd_complete) {
-
526  hci_counter = 0;
-
527 #ifdef DEBUG
-
528  Notify(PSTR("\r\nHCI Reset complete"), 0x80);
-
529 #endif
-
530  hci_state = HCI_CLASS_STATE;
-
531  hci_write_class_of_device();
-
532  } else if (hci_counter > hci_num_reset_loops) {
-
533  hci_num_reset_loops *= 10;
-
534  if (hci_num_reset_loops > 2000)
-
535  hci_num_reset_loops = 2000;
-
536 #ifdef DEBUG
-
537  Notify(PSTR("\r\nNo response to HCI Reset"), 0x80);
-
538 #endif
-
539  hci_state = HCI_INIT_STATE;
-
540  hci_counter = 0;
-
541  }
-
542  break;
-
543 
-
544  case HCI_CLASS_STATE:
-
545  if (hci_cmd_complete) {
+
503  default:
+
504  if (hcibuf[0] != 0x00) {
+
505  Notify(PSTR("\r\nUnmanaged HCI Event: "), 0x80);
+
506  PrintHex<uint8_t > (hcibuf[0], 0x80);
+
507  }
+
508  break;
+
509 #endif
+
510  } // switch
+
511  }
+
512 #ifdef EXTRADEBUG
+
513  else if (rcode != hrNAK) {
+
514  Notify(PSTR("\r\nHCI event error: "), 0x80);
+
515  PrintHex<uint8_t > (rcode, 0x80);
+
516  }
+
517 #endif
+
518  HCI_task();
+
519 }
+
520 
+
521 /* Poll Bluetooth and print result */
+
522 void BTD::HCI_task() {
+
523  switch (hci_state) {
+
524  case HCI_INIT_STATE:
+
525  hci_counter++;
+
526  if (hci_counter > hci_num_reset_loops) { // wait until we have looped x times to clear any old events
+
527  hci_reset();
+
528  hci_state = HCI_RESET_STATE;
+
529  hci_counter = 0;
+
530  }
+
531  break;
+
532 
+
533  case HCI_RESET_STATE:
+
534  hci_counter++;
+
535  if (hci_cmd_complete) {
+
536  hci_counter = 0;
+
537 #ifdef DEBUG
+
538  Notify(PSTR("\r\nHCI Reset complete"), 0x80);
+
539 #endif
+
540  hci_state = HCI_CLASS_STATE;
+
541  hci_write_class_of_device();
+
542  } else if (hci_counter > hci_num_reset_loops) {
+
543  hci_num_reset_loops *= 10;
+
544  if (hci_num_reset_loops > 2000)
+
545  hci_num_reset_loops = 2000;
546 #ifdef DEBUG
-
547  Notify(PSTR("\r\nWrite class of device"), 0x80);
+
547  Notify(PSTR("\r\nNo response to HCI Reset"), 0x80);
548 #endif
-
549  hci_state = HCI_BDADDR_STATE;
-
550  hci_read_bdaddr();
+
549  hci_state = HCI_INIT_STATE;
+
550  hci_counter = 0;
551  }
552  break;
553 
-
554  case HCI_BDADDR_STATE:
-
555  if (hci_read_bdaddr_complete) {
+
554  case HCI_CLASS_STATE:
+
555  if (hci_cmd_complete) {
556 #ifdef DEBUG
-
557  Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80);
-
558  for (int8_t i = 5; i > 0; i--) {
-
559  PrintHex<uint8_t > (my_bdaddr[i], 0x80);
-
560  Notify(PSTR(":"), 0x80);
-
561  }
-
562  PrintHex<uint8_t > (my_bdaddr[0], 0x80);
-
563 #endif
-
564  hci_read_local_version_information();
-
565  hci_state = HCI_LOCAL_VERSION_STATE;
-
566  }
-
567  break;
-
568 
-
569  case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
-
570  if (hci_read_version_complete) {
-
571  if (btdName != NULL) {
-
572  hci_set_local_name(btdName);
-
573  hci_state = HCI_SET_NAME_STATE;
-
574  } else
-
575  hci_state = HCI_CHECK_WII_SERVICE;
+
557  Notify(PSTR("\r\nWrite class of device"), 0x80);
+
558 #endif
+
559  hci_state = HCI_BDADDR_STATE;
+
560  hci_read_bdaddr();
+
561  }
+
562  break;
+
563 
+
564  case HCI_BDADDR_STATE:
+
565  if (hci_read_bdaddr_complete) {
+
566 #ifdef DEBUG
+
567  Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80);
+
568  for (int8_t i = 5; i > 0; i--) {
+
569  PrintHex<uint8_t > (my_bdaddr[i], 0x80);
+
570  Notify(PSTR(":"), 0x80);
+
571  }
+
572  PrintHex<uint8_t > (my_bdaddr[0], 0x80);
+
573 #endif
+
574  hci_read_local_version_information();
+
575  hci_state = HCI_LOCAL_VERSION_STATE;
576  }
577  break;
578 
-
579  case HCI_SET_NAME_STATE:
-
580  if (hci_cmd_complete) {
-
581 #ifdef DEBUG
-
582  Notify(PSTR("\r\nThe name is set to: "), 0x80);
-
583  Serial.print(btdName);
-
584 #endif
-
585  hci_state = HCI_CHECK_WII_SERVICE;
+
579  case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
+
580  if (hci_read_version_complete) {
+
581  if (btdName != NULL) {
+
582  hci_set_local_name(btdName);
+
583  hci_state = HCI_SET_NAME_STATE;
+
584  } else
+
585  hci_state = HCI_CHECK_WII_SERVICE;
586  }
587  break;
588 
-
589  case HCI_CHECK_WII_SERVICE:
-
590  if (pairWithWii) { // Check if it should try to connect to a wiimote
+
589  case HCI_SET_NAME_STATE:
+
590  if (hci_cmd_complete) {
591 #ifdef DEBUG
-
592  Notify(PSTR("\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press sync if you are using a Wii U Pro Controller"), 0x80);
-
593 #endif
-
594  hci_inquiry();
-
595  hci_state = HCI_INQUIRY_STATE;
-
596  } else
-
597  hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote
-
598  break;
-
599 
-
600  case HCI_INQUIRY_STATE:
-
601  if (hci_wii_found) {
-
602  hci_inquiry_cancel(); // Stop inquiry
-
603 #ifdef DEBUG
-
604  Notify(PSTR("\r\nWiimote found"), 0x80);
-
605  Notify(PSTR("\r\nNow just create the instance like so:"), 0x80);
-
606  Notify(PSTR("\r\nWII Wii(&Btd);"), 0x80);
-
607  Notify(PSTR("\r\nAnd then press any button on the Wiimote"), 0x80);
-
608 #endif
-
609  if (motionPlusInside) {
-
610  hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller
-
611  hci_state = HCI_REMOTE_NAME_STATE;
-
612  } else
-
613  hci_state = HCI_CONNECT_WII_STATE;
-
614  }
-
615  break;
-
616 
-
617  case HCI_CONNECT_WII_STATE:
-
618  if (hci_cmd_complete) {
-
619 #ifdef DEBUG
-
620  Notify(PSTR("\r\nConnecting to Wiimote"), 0x80);
-
621 #endif
-
622  hci_connect();
-
623  hci_state = HCI_CONNECTED_WII_STATE;
+
592  Notify(PSTR("\r\nThe name is set to: "), 0x80);
+
593  NotifyStr(btdName, 0x80);
+
594 #endif
+
595  hci_state = HCI_CHECK_WII_SERVICE;
+
596  }
+
597  break;
+
598 
+
599  case HCI_CHECK_WII_SERVICE:
+
600  if (pairWithWii) { // Check if it should try to connect to a wiimote
+
601 #ifdef DEBUG
+
602  Notify(PSTR("\r\nStarting inquiry\r\nPress 1 & 2 on the Wiimote\r\nOr press sync if you are using a Wii U Pro Controller"), 0x80);
+
603 #endif
+
604  hci_inquiry();
+
605  hci_state = HCI_INQUIRY_STATE;
+
606  } else
+
607  hci_state = HCI_SCANNING_STATE; // Don't try to connect to a Wiimote
+
608  break;
+
609 
+
610  case HCI_INQUIRY_STATE:
+
611  if (hci_wii_found) {
+
612  hci_inquiry_cancel(); // Stop inquiry
+
613 #ifdef DEBUG
+
614  Notify(PSTR("\r\nWiimote found"), 0x80);
+
615  Notify(PSTR("\r\nNow just create the instance like so:"), 0x80);
+
616  Notify(PSTR("\r\nWII Wii(&Btd);"), 0x80);
+
617  Notify(PSTR("\r\nAnd then press any button on the Wiimote"), 0x80);
+
618 #endif
+
619  if (motionPlusInside) {
+
620  hci_remote_name(); // We need to know the name to distinguish between a Wiimote and a Wii U Pro Controller
+
621  hci_state = HCI_REMOTE_NAME_STATE;
+
622  } else
+
623  hci_state = HCI_CONNECT_WII_STATE;
624  }
625  break;
626 
-
627  case HCI_CONNECTED_WII_STATE:
-
628  if (hci_connect_event) {
-
629  if (hci_connect_complete) {
-
630 #ifdef DEBUG
-
631  Notify(PSTR("\r\nConnected to Wiimote"), 0x80);
-
632 #endif
-
633  hci_authentication_request(); // This will start the pairing with the wiimote
-
634  hci_state = HCI_SCANNING_STATE;
-
635  } else {
-
636 #ifdef DEBUG
-
637  Notify(PSTR("\r\nTrying to connect one more time..."), 0x80);
-
638 #endif
-
639  hci_connect(); // Try to connect one more time
-
640  }
-
641  }
-
642  break;
-
643 
-
644  case HCI_SCANNING_STATE:
-
645  if (!connectToWii && !pairWithWii) {
+
627  case HCI_CONNECT_WII_STATE:
+
628  if (hci_cmd_complete) {
+
629 #ifdef DEBUG
+
630  Notify(PSTR("\r\nConnecting to Wiimote"), 0x80);
+
631 #endif
+
632  hci_connect();
+
633  hci_state = HCI_CONNECTED_WII_STATE;
+
634  }
+
635  break;
+
636 
+
637  case HCI_CONNECTED_WII_STATE:
+
638  if (hci_connect_event) {
+
639  if (hci_connect_complete) {
+
640 #ifdef DEBUG
+
641  Notify(PSTR("\r\nConnected to Wiimote"), 0x80);
+
642 #endif
+
643  hci_authentication_request(); // This will start the pairing with the wiimote
+
644  hci_state = HCI_SCANNING_STATE;
+
645  } else {
646 #ifdef DEBUG
-
647  Notify(PSTR("\r\nWait For Incoming Connection Request"), 0x80);
+
647  Notify(PSTR("\r\nTrying to connect one more time..."), 0x80);
648 #endif
-
649  hci_write_scan_enable();
-
650  watingForConnection = true;
-
651  hci_state = HCI_CONNECT_IN_STATE;
-
652  }
-
653  break;
-
654 
-
655  case HCI_CONNECT_IN_STATE:
-
656  if (hci_incoming_connect_request) {
-
657  watingForConnection = false;
-
658 #ifdef DEBUG
-
659  Notify(PSTR("\r\nIncoming Connection Request"), 0x80);
-
660 #endif
-
661  hci_remote_name();
-
662  hci_state = HCI_REMOTE_NAME_STATE;
-
663  } else if (hci_disconnect_complete)
-
664  hci_state = HCI_DISCONNECT_STATE;
-
665  break;
-
666 
-
667  case HCI_REMOTE_NAME_STATE:
-
668  if (hci_remote_name_complete) {
-
669 #ifdef DEBUG
-
670  Notify(PSTR("\r\nRemote Name: "), 0x80);
-
671  for (uint8_t i = 0; i < 30; i++) {
-
672  if (remote_name[i] == NULL)
-
673  break;
-
674  Serial.write(remote_name[i]);
-
675  }
-
676 #endif
-
677  if (strncmp((const char*)remote_name, "Nintendo", 8) == 0) {
-
678  incomingWii = true;
+
649  hci_connect(); // Try to connect one more time
+
650  }
+
651  }
+
652  break;
+
653 
+
654  case HCI_SCANNING_STATE:
+
655  if (!connectToWii && !pairWithWii) {
+
656 #ifdef DEBUG
+
657  Notify(PSTR("\r\nWait For Incoming Connection Request"), 0x80);
+
658 #endif
+
659  hci_write_scan_enable();
+
660  watingForConnection = true;
+
661  hci_state = HCI_CONNECT_IN_STATE;
+
662  }
+
663  break;
+
664 
+
665  case HCI_CONNECT_IN_STATE:
+
666  if (hci_incoming_connect_request) {
+
667  watingForConnection = false;
+
668 #ifdef DEBUG
+
669  Notify(PSTR("\r\nIncoming Connection Request"), 0x80);
+
670 #endif
+
671  hci_remote_name();
+
672  hci_state = HCI_REMOTE_NAME_STATE;
+
673  } else if (hci_disconnect_complete)
+
674  hci_state = HCI_DISCONNECT_STATE;
+
675  break;
+
676 
+
677  case HCI_REMOTE_NAME_STATE:
+
678  if (hci_remote_name_complete) {
679 #ifdef DEBUG
-
680  Notify(PSTR("\r\nWiimote is connecting"), 0x80);
-
681 #endif
-
682  if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) {
-
683 #ifdef DEBUG
-
684  Notify(PSTR(" with Motion Plus Inside"), 0x80);
-
685 #endif
-
686  motionPlusInside = true;
-
687  } else if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
-
688 #ifdef DEBUG
-
689  Notify(PSTR(" - Wii U Pro Controller"), 0x80);
-
690 #endif
-
691  motionPlusInside = true;
-
692  wiiUProController = true;
-
693  } else {
-
694  motionPlusInside = false;
-
695  wiiUProController = false;
-
696  }
-
697  }
-
698  if (pairWithWii && motionPlusInside)
-
699  hci_state = HCI_CONNECT_WII_STATE;
-
700  else {
-
701  hci_accept_connection();
-
702  hci_state = HCI_CONNECTED_STATE;
-
703  }
-
704  }
-
705  break;
-
706 
-
707  case HCI_CONNECTED_STATE:
-
708  if (hci_connect_complete) {
-
709 #ifdef DEBUG
-
710  Notify(PSTR("\r\nConnected to Device: "), 0x80);
-
711  for (int8_t i = 5; i > 0; i--) {
-
712  PrintHex<uint8_t > (disc_bdaddr[i], 0x80);
-
713  Notify(PSTR(":"), 0x80);
-
714  }
-
715  PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
-
716 #endif
-
717  // Clear these flags for a new connection
-
718  l2capConnectionClaimed = false;
-
719  sdpConnectionClaimed = false;
-
720  rfcommConnectionClaimed = false;
-
721 
-
722  hci_event_flag = 0;
-
723  hci_state = HCI_DONE_STATE;
-
724  }
-
725  break;
-
726 
-
727  case HCI_DONE_STATE:
-
728  hci_counter++;
-
729  if (hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started
-
730  hci_counter = 0;
-
731  hci_state = HCI_SCANNING_STATE;
-
732  }
-
733  break;
-
734 
-
735  case HCI_DISCONNECT_STATE:
-
736  if (hci_disconnect_complete) {
-
737 #ifdef DEBUG
-
738  Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80);
-
739 #endif
-
740  hci_event_flag = 0; // Clear all flags
-
741 
-
742  // Reset all buffers
-
743  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
-
744  hcibuf[i] = 0;
-
745  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
-
746  l2capinbuf[i] = 0;
-
747 
-
748  hci_state = HCI_SCANNING_STATE;
-
749  }
-
750  break;
-
751  default:
-
752  break;
-
753  }
-
754 }
-
755 
-
756 void BTD::ACL_event_task() {
-
757  uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE;
-
758  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_DATAIN_PIPE ].epAddr, &MAX_BUFFER_SIZE, l2capinbuf); // input on endpoint 2
-
759  if (!rcode) { // Check for errors
-
760  for (uint8_t i = 0; i < BTD_NUMSERVICES; i++)
-
761  if (btService[i])
-
762  btService[i]->ACLData(l2capinbuf);
+
680  Notify(PSTR("\r\nRemote Name: "), 0x80);
+
681  for (uint8_t i = 0; i < 30; i++) {
+
682  if (remote_name[i] == NULL)
+
683  break;
+
684  Notifyc(remote_name[i], 0x80);
+
685  }
+
686 #endif
+
687  if (strncmp((const char*)remote_name, "Nintendo", 8) == 0) {
+
688  incomingWii = true;
+
689 #ifdef DEBUG
+
690  Notify(PSTR("\r\nWiimote is connecting"), 0x80);
+
691 #endif
+
692  if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-TR", 22) == 0) {
+
693 #ifdef DEBUG
+
694  Notify(PSTR(" with Motion Plus Inside"), 0x80);
+
695 #endif
+
696  motionPlusInside = true;
+
697  } else if (strncmp((const char*)remote_name, "Nintendo RVL-CNT-01-UC", 22) == 0) {
+
698 #ifdef DEBUG
+
699  Notify(PSTR(" - Wii U Pro Controller"), 0x80);
+
700 #endif
+
701  motionPlusInside = true;
+
702  wiiUProController = true;
+
703  } else {
+
704  motionPlusInside = false;
+
705  wiiUProController = false;
+
706  }
+
707  }
+
708  if (pairWithWii && motionPlusInside)
+
709  hci_state = HCI_CONNECT_WII_STATE;
+
710  else {
+
711  hci_accept_connection();
+
712  hci_state = HCI_CONNECTED_STATE;
+
713  }
+
714  }
+
715  break;
+
716 
+
717  case HCI_CONNECTED_STATE:
+
718  if (hci_connect_complete) {
+
719 #ifdef DEBUG
+
720  Notify(PSTR("\r\nConnected to Device: "), 0x80);
+
721  for (int8_t i = 5; i > 0; i--) {
+
722  PrintHex<uint8_t > (disc_bdaddr[i], 0x80);
+
723  Notify(PSTR(":"), 0x80);
+
724  }
+
725  PrintHex<uint8_t > (disc_bdaddr[0], 0x80);
+
726 #endif
+
727  // Clear these flags for a new connection
+
728  l2capConnectionClaimed = false;
+
729  sdpConnectionClaimed = false;
+
730  rfcommConnectionClaimed = false;
+
731 
+
732  hci_event_flag = 0;
+
733  hci_state = HCI_DONE_STATE;
+
734  }
+
735  break;
+
736 
+
737  case HCI_DONE_STATE:
+
738  hci_counter++;
+
739  if (hci_counter > 1000) { // Wait until we have looped 1000 times to make sure that the L2CAP connection has been started
+
740  hci_counter = 0;
+
741  hci_state = HCI_SCANNING_STATE;
+
742  }
+
743  break;
+
744 
+
745  case HCI_DISCONNECT_STATE:
+
746  if (hci_disconnect_complete) {
+
747 #ifdef DEBUG
+
748  Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80);
+
749 #endif
+
750  hci_event_flag = 0; // Clear all flags
+
751 
+
752  // Reset all buffers
+
753  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
+
754  hcibuf[i] = 0;
+
755  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
+
756  l2capinbuf[i] = 0;
+
757 
+
758  hci_state = HCI_SCANNING_STATE;
+
759  }
+
760  break;
+
761  default:
+
762  break;
763  }
-
764 #ifdef EXTRADEBUG
-
765  else if (rcode != hrNAK) {
-
766  Notify(PSTR("\r\nACL data in error: "), 0x80);
-
767  PrintHex<uint8_t > (rcode, 0x80);
-
768  }
-
769 #endif
-
770  for (uint8_t i = 0; i < BTD_NUMSERVICES; i++)
-
771  if (btService[i])
-
772  btService[i]->Run();
-
773 }
-
774 
-
775 /************************************************************/
-
776 /* HCI Commands */
-
777 
-
778 /************************************************************/
-
779 void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
-
780  hci_event_flag &= ~HCI_FLAG_CMD_COMPLETE;
-
781  pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
-
782 }
-
783 
-
784 void BTD::hci_reset() {
-
785  hci_event_flag = 0; // Clear all the flags
-
786  hcibuf[0] = 0x03; // HCI OCF = 3
-
787  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
-
788  hcibuf[2] = 0x00;
-
789  HCI_Command(hcibuf, 3);
-
790 }
-
791 
-
792 void BTD::hci_write_scan_enable() {
-
793  hci_event_flag &= ~HCI_FLAG_INCOMING_REQUEST;
-
794  hcibuf[0] = 0x1A; // HCI OCF = 1A
-
795  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
-
796  hcibuf[2] = 0x01; // parameter length = 1
-
797  if (btdName != NULL)
-
798  hcibuf[3] = 0x03; // Inquiry Scan enabled. Page Scan enabled.
-
799  else
-
800  hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled.
-
801  HCI_Command(hcibuf, 4);
-
802 }
-
803 
-
804 void BTD::hci_write_scan_disable() {
+
764 }
+
765 
+
766 void BTD::ACL_event_task() {
+
767  uint16_t MAX_BUFFER_SIZE = BULK_MAXPKTSIZE;
+
768  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_DATAIN_PIPE ].epAddr, &MAX_BUFFER_SIZE, l2capinbuf); // input on endpoint 2
+
769  if (!rcode) { // Check for errors
+
770  for (uint8_t i = 0; i < BTD_NUMSERVICES; i++)
+
771  if (btService[i])
+
772  btService[i]->ACLData(l2capinbuf);
+
773  }
+
774 #ifdef EXTRADEBUG
+
775  else if (rcode != hrNAK) {
+
776  Notify(PSTR("\r\nACL data in error: "), 0x80);
+
777  PrintHex<uint8_t > (rcode, 0x80);
+
778  }
+
779 #endif
+
780  for (uint8_t i = 0; i < BTD_NUMSERVICES; i++)
+
781  if (btService[i])
+
782  btService[i]->Run();
+
783 }
+
784 
+
785 /************************************************************/
+
786 /* HCI Commands */
+
787 
+
788 /************************************************************/
+
789 void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
+
790  hci_event_flag &= ~HCI_FLAG_CMD_COMPLETE;
+
791  pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
+
792 }
+
793 
+
794 void BTD::hci_reset() {
+
795  hci_event_flag = 0; // Clear all the flags
+
796  hcibuf[0] = 0x03; // HCI OCF = 3
+
797  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
+
798  hcibuf[2] = 0x00;
+
799 
+
800  HCI_Command(hcibuf, 3);
+
801 }
+
802 
+
803 void BTD::hci_write_scan_enable() {
+
804  hci_event_flag &= ~HCI_FLAG_INCOMING_REQUEST;
805  hcibuf[0] = 0x1A; // HCI OCF = 1A
806  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
807  hcibuf[2] = 0x01; // parameter length = 1
-
808  hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled.
-
809  HCI_Command(hcibuf, 4);
-
810 }
-
811 
-
812 void BTD::hci_read_bdaddr() {
-
813  hcibuf[0] = 0x09; // HCI OCF = 9
-
814  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
-
815  hcibuf[2] = 0x00;
-
816  HCI_Command(hcibuf, 3);
-
817 }
-
818 
-
819 void BTD::hci_read_local_version_information() {
-
820  hcibuf[0] = 0x01; // HCI OCF = 1
-
821  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
-
822  hcibuf[2] = 0x00;
-
823  HCI_Command(hcibuf, 3);
-
824 }
-
825 
-
826 void BTD::hci_accept_connection() {
-
827  hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE;
-
828  hcibuf[0] = 0x09; // HCI OCF = 9
-
829  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
830  hcibuf[2] = 0x07; // parameter length 7
-
831  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
-
832  hcibuf[4] = disc_bdaddr[1];
-
833  hcibuf[5] = disc_bdaddr[2];
-
834  hcibuf[6] = disc_bdaddr[3];
-
835  hcibuf[7] = disc_bdaddr[4];
-
836  hcibuf[8] = disc_bdaddr[5];
-
837  hcibuf[9] = 0x00; //switch role to master
-
838 
-
839  HCI_Command(hcibuf, 10);
-
840 }
-
841 
-
842 void BTD::hci_remote_name() {
-
843  hci_event_flag &= ~HCI_FLAG_REMOTE_NAME_COMPLETE;
-
844  hcibuf[0] = 0x19; // HCI OCF = 19
-
845  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
846  hcibuf[2] = 0x0A; // parameter length = 10
-
847  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
-
848  hcibuf[4] = disc_bdaddr[1];
-
849  hcibuf[5] = disc_bdaddr[2];
-
850  hcibuf[6] = disc_bdaddr[3];
-
851  hcibuf[7] = disc_bdaddr[4];
-
852  hcibuf[8] = disc_bdaddr[5];
-
853  hcibuf[9] = 0x01; //Page Scan Repetition Mode
-
854  hcibuf[10] = 0x00; //Reserved
-
855  hcibuf[11] = 0x00; //Clock offset - low byte
-
856  hcibuf[12] = 0x00; //Clock offset - high byte
-
857 
-
858  HCI_Command(hcibuf, 13);
-
859 }
-
860 
-
861 void BTD::hci_set_local_name(const char* name) {
-
862  hcibuf[0] = 0x13; // HCI OCF = 13
-
863  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
-
864  hcibuf[2] = strlen(name) + 1; // parameter length = the length of the string + end byte
-
865  uint8_t i;
-
866  for (i = 0; i < strlen(name); i++)
-
867  hcibuf[i + 3] = name[i];
-
868  hcibuf[i + 3] = 0x00; // End of string
-
869 
-
870  HCI_Command(hcibuf, 4 + strlen(name));
-
871 }
+
808  if (btdName != NULL)
+
809  hcibuf[3] = 0x03; // Inquiry Scan enabled. Page Scan enabled.
+
810  else
+
811  hcibuf[3] = 0x02; // Inquiry Scan disabled. Page Scan enabled.
+
812 
+
813  HCI_Command(hcibuf, 4);
+
814 }
+
815 
+
816 void BTD::hci_write_scan_disable() {
+
817  hcibuf[0] = 0x1A; // HCI OCF = 1A
+
818  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
+
819  hcibuf[2] = 0x01; // parameter length = 1
+
820  hcibuf[3] = 0x00; // Inquiry Scan disabled. Page Scan disabled.
+
821 
+
822  HCI_Command(hcibuf, 4);
+
823 }
+
824 
+
825 void BTD::hci_read_bdaddr() {
+
826  hcibuf[0] = 0x09; // HCI OCF = 9
+
827  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
+
828  hcibuf[2] = 0x00;
+
829 
+
830  HCI_Command(hcibuf, 3);
+
831 }
+
832 
+
833 void BTD::hci_read_local_version_information() {
+
834  hcibuf[0] = 0x01; // HCI OCF = 1
+
835  hcibuf[1] = 0x04 << 2; // HCI OGF = 4
+
836  hcibuf[2] = 0x00;
+
837 
+
838  HCI_Command(hcibuf, 3);
+
839 }
+
840 
+
841 void BTD::hci_accept_connection() {
+
842  hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE;
+
843  hcibuf[0] = 0x09; // HCI OCF = 9
+
844  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
845  hcibuf[2] = 0x07; // parameter length 7
+
846  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
+
847  hcibuf[4] = disc_bdaddr[1];
+
848  hcibuf[5] = disc_bdaddr[2];
+
849  hcibuf[6] = disc_bdaddr[3];
+
850  hcibuf[7] = disc_bdaddr[4];
+
851  hcibuf[8] = disc_bdaddr[5];
+
852  hcibuf[9] = 0x00; //switch role to master
+
853 
+
854  HCI_Command(hcibuf, 10);
+
855 }
+
856 
+
857 void BTD::hci_remote_name() {
+
858  hci_event_flag &= ~HCI_FLAG_REMOTE_NAME_COMPLETE;
+
859  hcibuf[0] = 0x19; // HCI OCF = 19
+
860  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
861  hcibuf[2] = 0x0A; // parameter length = 10
+
862  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
+
863  hcibuf[4] = disc_bdaddr[1];
+
864  hcibuf[5] = disc_bdaddr[2];
+
865  hcibuf[6] = disc_bdaddr[3];
+
866  hcibuf[7] = disc_bdaddr[4];
+
867  hcibuf[8] = disc_bdaddr[5];
+
868  hcibuf[9] = 0x01; //Page Scan Repetition Mode
+
869  hcibuf[10] = 0x00; //Reserved
+
870  hcibuf[11] = 0x00; //Clock offset - low byte
+
871  hcibuf[12] = 0x00; //Clock offset - high byte
872 
-
873 void BTD::hci_inquiry() {
-
874  hci_event_flag &= ~HCI_FLAG_WII_FOUND;
-
875  hcibuf[0] = 0x01;
-
876  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
877  hcibuf[2] = 0x05; // Parameter Total Length = 5
-
878  hcibuf[3] = 0x33; // LAP: Genera/Unlimited Inquiry Access Code (GIAC = 0x9E8B33) - see https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
-
879  hcibuf[4] = 0x8B;
-
880  hcibuf[5] = 0x9E;
-
881  hcibuf[6] = 0x30; // Inquiry time = 61.44 sec (maximum)
-
882  hcibuf[7] = 0x0A; // 10 number of responses
-
883 
-
884  HCI_Command(hcibuf, 8);
-
885 }
-
886 
-
887 void BTD::hci_inquiry_cancel() {
-
888  hcibuf[0] = 0x02;
-
889  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
890  hcibuf[2] = 0x0; // Parameter Total Length = 0
-
891 
-
892  HCI_Command(hcibuf, 3);
-
893 }
-
894 
-
895 void BTD::hci_connect() {
-
896  hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT);
-
897  hcibuf[0] = 0x05;
-
898  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
899  hcibuf[2] = 0x0D; // parameter Total Length = 13
-
900  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
-
901  hcibuf[4] = disc_bdaddr[1];
-
902  hcibuf[5] = disc_bdaddr[2];
-
903  hcibuf[6] = disc_bdaddr[3];
-
904  hcibuf[7] = disc_bdaddr[4];
-
905  hcibuf[8] = disc_bdaddr[5];
-
906  hcibuf[9] = 0x18; // DM1 or DH1 may be used
-
907  hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used
-
908  hcibuf[11] = 0x01; // Page repetition mode R1
-
909  hcibuf[12] = 0x00; // Reserved
-
910  hcibuf[13] = 0x00; // Clock offset
-
911  hcibuf[14] = 0x00; // Invalid clock offset
-
912  hcibuf[15] = 0x00; // Do not allow role switch
-
913 
-
914  HCI_Command(hcibuf, 16);
-
915 }
-
916 
-
917 void BTD::hci_pin_code_request_reply() {
-
918  hcibuf[0] = 0x0D; // HCI OCF = 0D
-
919  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
920  hcibuf[2] = 0x17; // parameter length 23
-
921  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
-
922  hcibuf[4] = disc_bdaddr[1];
-
923  hcibuf[5] = disc_bdaddr[2];
-
924  hcibuf[6] = disc_bdaddr[3];
-
925  hcibuf[7] = disc_bdaddr[4];
-
926  hcibuf[8] = disc_bdaddr[5];
-
927  if (pairWithWii && !wiiUProController) {
-
928  hcibuf[9] = 6; // Pin length is the length of the bt address
-
929  hcibuf[10] = disc_bdaddr[0]; // The pin is the Wiimotes bt address backwards
-
930  hcibuf[11] = disc_bdaddr[1];
-
931  hcibuf[12] = disc_bdaddr[2];
-
932  hcibuf[13] = disc_bdaddr[3];
-
933  hcibuf[14] = disc_bdaddr[4];
-
934  hcibuf[15] = disc_bdaddr[5];
-
935  for (uint8_t i = 16; i < 26; i++)
-
936  hcibuf[i] = 0x00; // The rest should be 0
-
937  } else if (pairWithWii && wiiUProController) {
-
938 #ifdef DEBUG
-
939  Notify(PSTR("\r\nParing with Wii U Pro Controller"), 0x80);
-
940 #endif
-
941  hcibuf[9] = 6; // Pin length is the length of the bt address
-
942  hcibuf[10] = my_bdaddr[0]; // The pin is the Bluetooth dongles bt address backwards
-
943  hcibuf[11] = my_bdaddr[1];
-
944  hcibuf[12] = my_bdaddr[2];
-
945  hcibuf[13] = my_bdaddr[3];
-
946  hcibuf[14] = my_bdaddr[4];
-
947  hcibuf[15] = my_bdaddr[5];
-
948  for (uint8_t i = 16; i < 26; i++)
-
949  hcibuf[i] = 0x00; // The rest should be 0
-
950  } else {
-
951  hcibuf[9] = strlen(btdPin); // Length of pin
-
952  uint8_t i;
-
953  for (i = 0; i < strlen(btdPin); i++) // The maximum size of the pin is 16
-
954  hcibuf[i + 10] = btdPin[i];
-
955  for (; i < 16; i++)
-
956  hcibuf[i + 10] = 0x00; // The rest should be 0
-
957  }
-
958 
-
959  HCI_Command(hcibuf, 26);
-
960 }
-
961 
-
962 void BTD::hci_pin_code_negative_request_reply() {
-
963  hcibuf[0] = 0x0E; // HCI OCF = 0E
-
964  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
965  hcibuf[2] = 0x06; // parameter length 6
-
966  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
-
967  hcibuf[4] = disc_bdaddr[1];
-
968  hcibuf[5] = disc_bdaddr[2];
-
969  hcibuf[6] = disc_bdaddr[3];
-
970  hcibuf[7] = disc_bdaddr[4];
-
971  hcibuf[8] = disc_bdaddr[5];
-
972 
-
973  HCI_Command(hcibuf, 9);
-
974 }
-
975 
-
976 void BTD::hci_link_key_request_negative_reply() {
-
977  hcibuf[0] = 0x0C; // HCI OCF = 0C
-
978  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
979  hcibuf[2] = 0x06; // parameter length 6
-
980  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
-
981  hcibuf[4] = disc_bdaddr[1];
-
982  hcibuf[5] = disc_bdaddr[2];
-
983  hcibuf[6] = disc_bdaddr[3];
-
984  hcibuf[7] = disc_bdaddr[4];
-
985  hcibuf[8] = disc_bdaddr[5];
-
986 
-
987  HCI_Command(hcibuf, 9);
-
988 }
-
989 
-
990 void BTD::hci_authentication_request() {
-
991  hcibuf[0] = 0x11; // HCI OCF = 11
-
992  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
993  hcibuf[2] = 0x02; // parameter length = 2
-
994  hcibuf[3] = (uint8_t)(hci_handle & 0xFF); //connection handle - low byte
-
995  hcibuf[4] = (uint8_t)((hci_handle >> 8) & 0x0F); //connection handle - high byte
+
873  HCI_Command(hcibuf, 13);
+
874 }
+
875 
+
876 void BTD::hci_set_local_name(const char* name) {
+
877  hcibuf[0] = 0x13; // HCI OCF = 13
+
878  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
+
879  hcibuf[2] = strlen(name) + 1; // parameter length = the length of the string + end byte
+
880  uint8_t i;
+
881  for (i = 0; i < strlen(name); i++)
+
882  hcibuf[i + 3] = name[i];
+
883  hcibuf[i + 3] = 0x00; // End of string
+
884 
+
885  HCI_Command(hcibuf, 4 + strlen(name));
+
886 }
+
887 
+
888 void BTD::hci_inquiry() {
+
889  hci_event_flag &= ~HCI_FLAG_WII_FOUND;
+
890  hcibuf[0] = 0x01;
+
891  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
892  hcibuf[2] = 0x05; // Parameter Total Length = 5
+
893  hcibuf[3] = 0x33; // LAP: Genera/Unlimited Inquiry Access Code (GIAC = 0x9E8B33) - see https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
+
894  hcibuf[4] = 0x8B;
+
895  hcibuf[5] = 0x9E;
+
896  hcibuf[6] = 0x30; // Inquiry time = 61.44 sec (maximum)
+
897  hcibuf[7] = 0x0A; // 10 number of responses
+
898 
+
899  HCI_Command(hcibuf, 8);
+
900 }
+
901 
+
902 void BTD::hci_inquiry_cancel() {
+
903  hcibuf[0] = 0x02;
+
904  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
905  hcibuf[2] = 0x00; // Parameter Total Length = 0
+
906 
+
907  HCI_Command(hcibuf, 3);
+
908 }
+
909 
+
910 void BTD::hci_connect() {
+
911  hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT);
+
912  hcibuf[0] = 0x05;
+
913  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
914  hcibuf[2] = 0x0D; // parameter Total Length = 13
+
915  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
+
916  hcibuf[4] = disc_bdaddr[1];
+
917  hcibuf[5] = disc_bdaddr[2];
+
918  hcibuf[6] = disc_bdaddr[3];
+
919  hcibuf[7] = disc_bdaddr[4];
+
920  hcibuf[8] = disc_bdaddr[5];
+
921  hcibuf[9] = 0x18; // DM1 or DH1 may be used
+
922  hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used
+
923  hcibuf[11] = 0x01; // Page repetition mode R1
+
924  hcibuf[12] = 0x00; // Reserved
+
925  hcibuf[13] = 0x00; // Clock offset
+
926  hcibuf[14] = 0x00; // Invalid clock offset
+
927  hcibuf[15] = 0x00; // Do not allow role switch
+
928 
+
929  HCI_Command(hcibuf, 16);
+
930 }
+
931 
+
932 void BTD::hci_pin_code_request_reply() {
+
933  hcibuf[0] = 0x0D; // HCI OCF = 0D
+
934  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
935  hcibuf[2] = 0x17; // parameter length 23
+
936  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
+
937  hcibuf[4] = disc_bdaddr[1];
+
938  hcibuf[5] = disc_bdaddr[2];
+
939  hcibuf[6] = disc_bdaddr[3];
+
940  hcibuf[7] = disc_bdaddr[4];
+
941  hcibuf[8] = disc_bdaddr[5];
+
942  if (pairWithWii) {
+
943  hcibuf[9] = 6; // Pin length is the length of the Bluetooth address
+
944  if(wiiUProController) {
+
945 #ifdef DEBUG
+
946  Notify(PSTR("\r\nParing with Wii U Pro Controller"), 0x80);
+
947 #endif
+
948  for(uint8_t i = 0; i < 6; i++)
+
949  hcibuf[10 + i] = my_bdaddr[i]; // The pin is the Bluetooth dongles Bluetooth address backwards
+
950  }
+
951  else {
+
952  for(uint8_t i = 0; i < 6; i++)
+
953  hcibuf[10 + i] = disc_bdaddr[i]; // The pin is the Wiimote's Bluetooth address backwards
+
954  }
+
955  for (uint8_t i = 16; i < 26; i++)
+
956  hcibuf[i] = 0x00; // The rest should be 0
+
957  } else {
+
958  hcibuf[9] = strlen(btdPin); // Length of pin
+
959  uint8_t i;
+
960  for (i = 0; i < strlen(btdPin); i++) // The maximum size of the pin is 16
+
961  hcibuf[i + 10] = btdPin[i];
+
962  for (; i < 16; i++)
+
963  hcibuf[i + 10] = 0x00; // The rest should be 0
+
964  }
+
965 
+
966  HCI_Command(hcibuf, 26);
+
967 }
+
968 
+
969 void BTD::hci_pin_code_negative_request_reply() {
+
970  hcibuf[0] = 0x0E; // HCI OCF = 0E
+
971  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
972  hcibuf[2] = 0x06; // parameter length 6
+
973  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
+
974  hcibuf[4] = disc_bdaddr[1];
+
975  hcibuf[5] = disc_bdaddr[2];
+
976  hcibuf[6] = disc_bdaddr[3];
+
977  hcibuf[7] = disc_bdaddr[4];
+
978  hcibuf[8] = disc_bdaddr[5];
+
979 
+
980  HCI_Command(hcibuf, 9);
+
981 }
+
982 
+
983 void BTD::hci_link_key_request_negative_reply() {
+
984  hcibuf[0] = 0x0C; // HCI OCF = 0C
+
985  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
986  hcibuf[2] = 0x06; // parameter length 6
+
987  hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr
+
988  hcibuf[4] = disc_bdaddr[1];
+
989  hcibuf[5] = disc_bdaddr[2];
+
990  hcibuf[6] = disc_bdaddr[3];
+
991  hcibuf[7] = disc_bdaddr[4];
+
992  hcibuf[8] = disc_bdaddr[5];
+
993 
+
994  HCI_Command(hcibuf, 9);
+
995 }
996 
-
997  HCI_Command(hcibuf, 5);
-
998 }
-
999 
-
1000 void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
-
1001  hci_event_flag &= ~HCI_FLAG_DISCONN_COMPLETE;
-
1002  hcibuf[0] = 0x06; // HCI OCF = 6
-
1003  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
-
1004  hcibuf[2] = 0x03; // parameter length = 3
-
1005  hcibuf[3] = (uint8_t)(handle & 0xFF); //connection handle - low byte
-
1006  hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F); //connection handle - high byte
-
1007  hcibuf[5] = 0x13; // reason
-
1008 
-
1009  HCI_Command(hcibuf, 6);
-
1010 }
-
1011 
-
1012 void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
-
1013  hcibuf[0] = 0x24; // HCI OCF = 3
-
1014  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
-
1015  hcibuf[2] = 0x03; // parameter length = 3
-
1016  hcibuf[3] = 0x04; // Robot
-
1017  hcibuf[4] = 0x08; // Toy
-
1018  hcibuf[5] = 0x00;
-
1019  HCI_Command(hcibuf, 6);
-
1020 }
-
1021 /*******************************************************************
-
1022  * *
-
1023  * HCI ACL Data Packet *
-
1024  * *
-
1025  * buf[0] buf[1] buf[2] buf[3]
-
1026  * 0 4 8 11 12 16 24 31 MSB
-
1027  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
-
1028  * | HCI Handle |PB |BC | Data Total Length | HCI ACL Data Packet
-
1029  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
-
1030  *
-
1031  * buf[4] buf[5] buf[6] buf[7]
-
1032  * 0 8 16 31 MSB
-
1033  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
-
1034  * | Length | Channel ID | Basic L2CAP header
-
1035  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
-
1036  *
-
1037  * buf[8] buf[9] buf[10] buf[11]
-
1038  * 0 8 16 31 MSB
-
1039  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
-
1040  * | Code | Identifier | Length | Control frame (C-frame)
-
1041  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-. (signaling packet format)
-
1042  */
-
1043 /************************************************************/
-
1044 /* L2CAP Commands */
-
1045 
-
1046 /************************************************************/
-
1047 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
-
1048  uint8_t buf[8 + nbytes];
-
1049  buf[0] = (uint8_t)(handle & 0xff); // HCI handle with PB,BC flag
-
1050  buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
-
1051  buf[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
-
1052  buf[3] = (uint8_t)((4 + nbytes) >> 8);
-
1053  buf[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
-
1054  buf[5] = (uint8_t)(nbytes >> 8);
-
1055  buf[6] = channelLow;
-
1056  buf[7] = channelHigh;
-
1057 
-
1058  for (uint16_t i = 0; i < nbytes; i++) // L2CAP C-frame
-
1059  buf[8 + i] = data[i];
-
1060 
-
1061  uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ BTD_DATAOUT_PIPE ].epAddr, (8 + nbytes), buf);
-
1062  if (rcode) {
-
1063  delay(100); // This small delay prevents it from overflowing if it fails
-
1064 #ifdef DEBUG
-
1065  Notify(PSTR("\r\nError sending L2CAP message: 0x"), 0x80);
-
1066  PrintHex<uint8_t > (rcode, 0x80);
-
1067  Notify(PSTR(" - Channel ID: "), 0x80);
-
1068  Serial.print(channelHigh);
-
1069  Notify(PSTR(" "), 0x80);
-
1070  Serial.print(channelLow);
-
1071 #endif
-
1072  }
-
1073 }
-
1074 
-
1075 void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) {
-
1076  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
-
1077  l2capoutbuf[1] = rxid; // Identifier
-
1078  l2capoutbuf[2] = 0x04; // Length
-
1079  l2capoutbuf[3] = 0x00;
-
1080  l2capoutbuf[4] = (uint8_t)(psm & 0xff); // PSM
-
1081  l2capoutbuf[5] = (uint8_t)(psm >> 8);
-
1082  l2capoutbuf[6] = scid[0]; // Source CID
-
1083  l2capoutbuf[7] = scid[1];
-
1084 
-
1085  L2CAP_Command(handle, l2capoutbuf, 8);
-
1086 }
-
1087 
-
1088 void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) {
-
1089  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
-
1090  l2capoutbuf[1] = rxid; // Identifier
-
1091  l2capoutbuf[2] = 0x08; // Length
-
1092  l2capoutbuf[3] = 0x00;
-
1093  l2capoutbuf[4] = dcid[0]; // Destination CID
-
1094  l2capoutbuf[5] = dcid[1];
-
1095  l2capoutbuf[6] = scid[0]; // Source CID
-
1096  l2capoutbuf[7] = scid[1];
-
1097  l2capoutbuf[8] = result; // Result: Pending or Success
-
1098  l2capoutbuf[9] = 0x00;
-
1099  l2capoutbuf[10] = 0x00; // No further information
-
1100  l2capoutbuf[11] = 0x00;
-
1101 
-
1102  L2CAP_Command(handle, l2capoutbuf, 12);
-
1103 }
-
1104 
-
1105 void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
-
1106  l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
-
1107  l2capoutbuf[1] = rxid; // Identifier
-
1108  l2capoutbuf[2] = 0x08; // Length
-
1109  l2capoutbuf[3] = 0x00;
-
1110  l2capoutbuf[4] = dcid[0]; // Destination CID
-
1111  l2capoutbuf[5] = dcid[1];
-
1112  l2capoutbuf[6] = 0x00; // Flags
-
1113  l2capoutbuf[7] = 0x00;
-
1114  l2capoutbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
-
1115  l2capoutbuf[9] = 0x02; // Config Opt: length
-
1116  l2capoutbuf[10] = 0xFF; // MTU
-
1117  l2capoutbuf[11] = 0xFF;
-
1118 
-
1119  L2CAP_Command(handle, l2capoutbuf, 12);
-
1120 }
-
1121 
-
1122 void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
-
1123  l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
-
1124  l2capoutbuf[1] = rxid; // Identifier
-
1125  l2capoutbuf[2] = 0x0A; // Length
-
1126  l2capoutbuf[3] = 0x00;
-
1127  l2capoutbuf[4] = scid[0]; // Source CID
-
1128  l2capoutbuf[5] = scid[1];
-
1129  l2capoutbuf[6] = 0x00; // Flag
-
1130  l2capoutbuf[7] = 0x00;
-
1131  l2capoutbuf[8] = 0x00; // Result
-
1132  l2capoutbuf[9] = 0x00;
-
1133  l2capoutbuf[10] = 0x01; // Config
-
1134  l2capoutbuf[11] = 0x02;
-
1135  l2capoutbuf[12] = 0xA0;
-
1136  l2capoutbuf[13] = 0x02;
-
1137 
-
1138  L2CAP_Command(handle, l2capoutbuf, 14);
-
1139 }
-
1140 
-
1141 void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
-
1142  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
-
1143  l2capoutbuf[1] = rxid; // Identifier
-
1144  l2capoutbuf[2] = 0x04; // Length
-
1145  l2capoutbuf[3] = 0x00;
-
1146  l2capoutbuf[4] = dcid[0];
-
1147  l2capoutbuf[5] = dcid[1];
-
1148  l2capoutbuf[6] = scid[0];
-
1149  l2capoutbuf[7] = scid[1];
-
1150  L2CAP_Command(handle, l2capoutbuf, 8);
-
1151 }
-
1152 
-
1153 void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
-
1154  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
-
1155  l2capoutbuf[1] = rxid; // Identifier
-
1156  l2capoutbuf[2] = 0x04; // Length
-
1157  l2capoutbuf[3] = 0x00;
-
1158  l2capoutbuf[4] = dcid[0];
-
1159  l2capoutbuf[5] = dcid[1];
-
1160  l2capoutbuf[6] = scid[0];
-
1161  l2capoutbuf[7] = scid[1];
-
1162  L2CAP_Command(handle, l2capoutbuf, 8);
-
1163 }
-
1164 
-
1165 void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) {
-
1166  l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code
-
1167  l2capoutbuf[1] = rxid; // Identifier
-
1168  l2capoutbuf[2] = 0x08; // Length
-
1169  l2capoutbuf[3] = 0x00;
-
1170  l2capoutbuf[4] = infoTypeLow;
-
1171  l2capoutbuf[5] = infoTypeHigh;
-
1172  l2capoutbuf[6] = 0x00; // Result = success
-
1173  l2capoutbuf[7] = 0x00; // Result = success
-
1174  l2capoutbuf[8] = 0x00;
-
1175  l2capoutbuf[9] = 0x00;
-
1176  l2capoutbuf[10] = 0x00;
-
1177  l2capoutbuf[11] = 0x00;
-
1178  L2CAP_Command(handle, l2capoutbuf, 12);
-
1179 }
-
1180 
-
1181 /* PS3 Commands - only set Bluetooth address is implemented */
-
1182 void BTD::setBdaddr(uint8_t* BDADDR) {
-
1183  /* Set the internal bluetooth address */
-
1184  uint8_t buf[8];
-
1185  buf[0] = 0x01;
-
1186  buf[1] = 0x00;
-
1187  for (uint8_t i = 0; i < 6; i++)
-
1188  buf[i + 2] = BDADDR[5 - i]; //Copy into buffer, has to be written reversed
-
1189 
-
1190  //bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
-
1191  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
-
1192 #ifdef DEBUG
-
1193  Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
-
1194  for (int8_t i = 5; i > 0; i--) {
-
1195  PrintHex<uint8_t > (my_bdaddr[i], 0x80);
-
1196  Notify(PSTR(":"), 0x80);
-
1197  }
-
1198  PrintHex<uint8_t > (my_bdaddr[0], 0x80);
-
1199 #endif
-
1200 }
+
997 void BTD::hci_authentication_request() {
+
998  hcibuf[0] = 0x11; // HCI OCF = 11
+
999  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
1000  hcibuf[2] = 0x02; // parameter length = 2
+
1001  hcibuf[3] = (uint8_t)(hci_handle & 0xFF); //connection handle - low byte
+
1002  hcibuf[4] = (uint8_t)((hci_handle >> 8) & 0x0F); //connection handle - high byte
+
1003 
+
1004  HCI_Command(hcibuf, 5);
+
1005 }
+
1006 
+
1007 void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
+
1008  hci_event_flag &= ~HCI_FLAG_DISCONN_COMPLETE;
+
1009  hcibuf[0] = 0x06; // HCI OCF = 6
+
1010  hcibuf[1] = 0x01 << 2; // HCI OGF = 1
+
1011  hcibuf[2] = 0x03; // parameter length = 3
+
1012  hcibuf[3] = (uint8_t)(handle & 0xFF); //connection handle - low byte
+
1013  hcibuf[4] = (uint8_t)((handle >> 8) & 0x0F); //connection handle - high byte
+
1014  hcibuf[5] = 0x13; // reason
+
1015 
+
1016  HCI_Command(hcibuf, 6);
+
1017 }
+
1018 
+
1019 void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
+
1020  hcibuf[0] = 0x24; // HCI OCF = 3
+
1021  hcibuf[1] = 0x03 << 2; // HCI OGF = 3
+
1022  hcibuf[2] = 0x03; // parameter length = 3
+
1023  hcibuf[3] = 0x04; // Robot
+
1024  hcibuf[4] = 0x08; // Toy
+
1025  hcibuf[5] = 0x00;
+
1026 
+
1027  HCI_Command(hcibuf, 6);
+
1028 }
+
1029 /*******************************************************************
+
1030  * *
+
1031  * HCI ACL Data Packet *
+
1032  * *
+
1033  * buf[0] buf[1] buf[2] buf[3]
+
1034  * 0 4 8 11 12 16 24 31 MSB
+
1035  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
+
1036  * | HCI Handle |PB |BC | Data Total Length | HCI ACL Data Packet
+
1037  * .-+-+-+-+-+-+-+-|-+-+-+-|-+-|-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
+
1038  *
+
1039  * buf[4] buf[5] buf[6] buf[7]
+
1040  * 0 8 16 31 MSB
+
1041  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
+
1042  * | Length | Channel ID | Basic L2CAP header
+
1043  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
+
1044  *
+
1045  * buf[8] buf[9] buf[10] buf[11]
+
1046  * 0 8 16 31 MSB
+
1047  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-.
+
1048  * | Code | Identifier | Length | Control frame (C-frame)
+
1049  * .-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-. (signaling packet format)
+
1050  */
+
1051 /************************************************************/
+
1052 /* L2CAP Commands */
+
1053 
+
1054 /************************************************************/
+
1055 void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh) {
+
1056  uint8_t buf[8 + nbytes];
+
1057  buf[0] = (uint8_t)(handle & 0xff); // HCI handle with PB,BC flag
+
1058  buf[1] = (uint8_t)(((handle >> 8) & 0x0f) | 0x20);
+
1059  buf[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
+
1060  buf[3] = (uint8_t)((4 + nbytes) >> 8);
+
1061  buf[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
+
1062  buf[5] = (uint8_t)(nbytes >> 8);
+
1063  buf[6] = channelLow;
+
1064  buf[7] = channelHigh;
+
1065 
+
1066  for (uint16_t i = 0; i < nbytes; i++) // L2CAP C-frame
+
1067  buf[8 + i] = data[i];
+
1068 
+
1069  uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ BTD_DATAOUT_PIPE ].epAddr, (8 + nbytes), buf);
+
1070  if (rcode) {
+
1071  delay(100); // This small delay prevents it from overflowing if it fails
+
1072 #ifdef DEBUG
+
1073  Notify(PSTR("\r\nError sending L2CAP message: 0x"), 0x80);
+
1074  PrintHex<uint8_t > (rcode, 0x80);
+
1075  Notify(PSTR(" - Channel ID: "), 0x80);
+
1076  PrintHex<uint8_t > (channelHigh, 0x80);
+
1077  Notify(PSTR(" "), 0x80);
+
1078  PrintHex<uint8_t > (channelLow, 0x80);
+
1079 #endif
+
1080  }
+
1081 }
+
1082 
+
1083 void BTD::l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm) {
+
1084  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
+
1085  l2capoutbuf[1] = rxid; // Identifier
+
1086  l2capoutbuf[2] = 0x04; // Length
+
1087  l2capoutbuf[3] = 0x00;
+
1088  l2capoutbuf[4] = (uint8_t)(psm & 0xff); // PSM
+
1089  l2capoutbuf[5] = (uint8_t)(psm >> 8);
+
1090  l2capoutbuf[6] = scid[0]; // Source CID
+
1091  l2capoutbuf[7] = scid[1];
+
1092 
+
1093  L2CAP_Command(handle, l2capoutbuf, 8);
+
1094 }
+
1095 
+
1096 void BTD::l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result) {
+
1097  l2capoutbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
+
1098  l2capoutbuf[1] = rxid; // Identifier
+
1099  l2capoutbuf[2] = 0x08; // Length
+
1100  l2capoutbuf[3] = 0x00;
+
1101  l2capoutbuf[4] = dcid[0]; // Destination CID
+
1102  l2capoutbuf[5] = dcid[1];
+
1103  l2capoutbuf[6] = scid[0]; // Source CID
+
1104  l2capoutbuf[7] = scid[1];
+
1105  l2capoutbuf[8] = result; // Result: Pending or Success
+
1106  l2capoutbuf[9] = 0x00;
+
1107  l2capoutbuf[10] = 0x00; // No further information
+
1108  l2capoutbuf[11] = 0x00;
+
1109 
+
1110  L2CAP_Command(handle, l2capoutbuf, 12);
+
1111 }
+
1112 
+
1113 void BTD::l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid) {
+
1114  l2capoutbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
+
1115  l2capoutbuf[1] = rxid; // Identifier
+
1116  l2capoutbuf[2] = 0x08; // Length
+
1117  l2capoutbuf[3] = 0x00;
+
1118  l2capoutbuf[4] = dcid[0]; // Destination CID
+
1119  l2capoutbuf[5] = dcid[1];
+
1120  l2capoutbuf[6] = 0x00; // Flags
+
1121  l2capoutbuf[7] = 0x00;
+
1122  l2capoutbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
+
1123  l2capoutbuf[9] = 0x02; // Config Opt: length
+
1124  l2capoutbuf[10] = 0xFF; // MTU
+
1125  l2capoutbuf[11] = 0xFF;
+
1126 
+
1127  L2CAP_Command(handle, l2capoutbuf, 12);
+
1128 }
+
1129 
+
1130 void BTD::l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid) {
+
1131  l2capoutbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
+
1132  l2capoutbuf[1] = rxid; // Identifier
+
1133  l2capoutbuf[2] = 0x0A; // Length
+
1134  l2capoutbuf[3] = 0x00;
+
1135  l2capoutbuf[4] = scid[0]; // Source CID
+
1136  l2capoutbuf[5] = scid[1];
+
1137  l2capoutbuf[6] = 0x00; // Flag
+
1138  l2capoutbuf[7] = 0x00;
+
1139  l2capoutbuf[8] = 0x00; // Result
+
1140  l2capoutbuf[9] = 0x00;
+
1141  l2capoutbuf[10] = 0x01; // Config
+
1142  l2capoutbuf[11] = 0x02;
+
1143  l2capoutbuf[12] = 0xA0;
+
1144  l2capoutbuf[13] = 0x02;
+
1145 
+
1146  L2CAP_Command(handle, l2capoutbuf, 14);
+
1147 }
+
1148 
+
1149 void BTD::l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
+
1150  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_REQUEST; // Code
+
1151  l2capoutbuf[1] = rxid; // Identifier
+
1152  l2capoutbuf[2] = 0x04; // Length
+
1153  l2capoutbuf[3] = 0x00;
+
1154  l2capoutbuf[4] = dcid[0];
+
1155  l2capoutbuf[5] = dcid[1];
+
1156  l2capoutbuf[6] = scid[0];
+
1157  l2capoutbuf[7] = scid[1];
+
1158 
+
1159  L2CAP_Command(handle, l2capoutbuf, 8);
+
1160 }
+
1161 
+
1162 void BTD::l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid) {
+
1163  l2capoutbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
+
1164  l2capoutbuf[1] = rxid; // Identifier
+
1165  l2capoutbuf[2] = 0x04; // Length
+
1166  l2capoutbuf[3] = 0x00;
+
1167  l2capoutbuf[4] = dcid[0];
+
1168  l2capoutbuf[5] = dcid[1];
+
1169  l2capoutbuf[6] = scid[0];
+
1170  l2capoutbuf[7] = scid[1];
+
1171 
+
1172  L2CAP_Command(handle, l2capoutbuf, 8);
+
1173 }
+
1174 
+
1175 void BTD::l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh) {
+
1176  l2capoutbuf[0] = L2CAP_CMD_INFORMATION_RESPONSE; // Code
+
1177  l2capoutbuf[1] = rxid; // Identifier
+
1178  l2capoutbuf[2] = 0x08; // Length
+
1179  l2capoutbuf[3] = 0x00;
+
1180  l2capoutbuf[4] = infoTypeLow;
+
1181  l2capoutbuf[5] = infoTypeHigh;
+
1182  l2capoutbuf[6] = 0x00; // Result = success
+
1183  l2capoutbuf[7] = 0x00; // Result = success
+
1184  l2capoutbuf[8] = 0x00;
+
1185  l2capoutbuf[9] = 0x00;
+
1186  l2capoutbuf[10] = 0x00;
+
1187  l2capoutbuf[11] = 0x00;
+
1188 
+
1189  L2CAP_Command(handle, l2capoutbuf, 12);
+
1190 }
+
1191 
+
1192 /* PS3 Commands - only set Bluetooth address is implemented in this library */
+
1193 void BTD::setBdaddr(uint8_t* bdaddr) {
+
1194  /* Set the internal Bluetooth address */
+
1195  uint8_t buf[8];
+
1196  buf[0] = 0x01;
+
1197  buf[1] = 0x00;
+
1198 
+
1199  for (uint8_t i = 0; i < 6; i++)
+
1200  buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed
1201 
-
1202 void BTD::setMoveBdaddr(uint8_t* BDADDR) {
-
1203  /* Set the internal bluetooth address */
-
1204  uint8_t buf[11];
-
1205  buf[0] = 0x05;
-
1206  buf[7] = 0x10;
-
1207  buf[8] = 0x01;
-
1208  buf[9] = 0x02;
-
1209  buf[10] = 0x12;
-
1210 
-
1211  for (uint8_t i = 0; i < 6; i++)
-
1212  buf[i + 1] = BDADDR[i];
-
1213 
-
1214  //bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
-
1215  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
-
1216 #ifdef DEBUG
-
1217  Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
-
1218  for (int8_t i = 5; i > 0; i--) {
-
1219  PrintHex<uint8_t > (my_bdaddr[i], 0x80);
-
1220  Notify(PSTR(":"), 0x80);
-
1221  }
-
1222  PrintHex<uint8_t > (my_bdaddr[0], 0x80);
-
1223 #endif
-
1224 }
+
1202  // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
+
1203  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
+
1204 }
+
1205 
+
1206 void BTD::setMoveBdaddr(uint8_t* bdaddr) {
+
1207  /* Set the internal Bluetooth address */
+
1208  uint8_t buf[11];
+
1209  buf[0] = 0x05;
+
1210  buf[7] = 0x10;
+
1211  buf[8] = 0x01;
+
1212  buf[9] = 0x02;
+
1213  buf[10] = 0x12;
+
1214 
+
1215  for (uint8_t i = 0; i < 6; i++)
+
1216  buf[i + 1] = bdaddr[i];
+
1217 
+
1218  // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
+
1219  pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
+
1220 }