BluetoothUSB.Mod 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. MODULE BluetoothUSB; (** AUTHOR "staubesv"; PURPOSE "HCI USB Transport Layer"; *)
  2. (**
  3. *
  4. * History:
  5. *
  6. * 01.12.2005 Cleanup (staubesv)
  7. *)
  8. IMPORT KernelLog, Streams, Plugins, Bluetooth, UsbBluetooth, Usb, Usbdi;
  9. TYPE
  10. UsbTransportLayer* = OBJECT(Bluetooth.TransportLayer)
  11. VAR
  12. driver : UsbBluetooth.BluetoothDriver;
  13. TraceReceive*, TraceSend*: BOOLEAN;
  14. event : Bluetooth.EventPacket; (* for EventHandler, must be global *)
  15. eventExpectedParams : LONGINT; (* expected length of parameters in bytes *)
  16. eventParamOffset : LONGINT;
  17. acl: Bluetooth.ACLPacket;
  18. aclExpectedParams : LONGINT; (* expected length of parameters in bytes *)
  19. aclParamOffset : LONGINT;
  20. PROCEDURE &Init*(name: ARRAY OF CHAR; sender: Streams.Sender; receiver: Streams.Receiver);
  21. VAR plugin : Plugins.Plugin;
  22. BEGIN
  23. Init^(name,NIL,NIL);
  24. TraceSend := FALSE; TraceReceive := FALSE;
  25. plugin := Usb.usbDrivers.Get(name);
  26. IF plugin = NIL THEN
  27. KernelLog.String("UsbBluetooth: "); KernelLog.String(name); KernelLog.String(" no found."); KernelLog.Ln;
  28. ELSE
  29. driver:=plugin(UsbBluetooth.BluetoothDriver);
  30. driver.SetEventHandler(EventHandler);
  31. driver.SetAclHandler(ReadACL);
  32. END;
  33. END Init;
  34. PROCEDURE Init2*(name: ARRAY OF CHAR): BOOLEAN;
  35. VAR plugin : Plugins.Plugin;
  36. BEGIN
  37. (* provisorisch *)
  38. TraceSend:=TRUE; TraceReceive:=TRUE;
  39. plugin := Usb.usbDrivers.Get(name);
  40. IF plugin=NIL THEN
  41. KernelLog.String("UsbBluetooth: "); KernelLog.String(name); KernelLog.String(" no found."); KernelLog.Ln;
  42. RETURN FALSE;
  43. END;
  44. driver := plugin(UsbBluetooth.BluetoothDriver);
  45. driver.SetEventHandler(EventHandler);
  46. driver.SetAclHandler(ReadACL);
  47. RETURN TRUE;
  48. END Init2;
  49. PROCEDURE Close*;
  50. END Close;
  51. (** receives HCI event packet fragments (each fragment 16 bytes) and enters them into the event queue *)
  52. PROCEDURE EventHandler(packet : Usbdi.Buffer; actLen : LONGINT);
  53. VAR
  54. i : LONGINT;
  55. eventQueue : Bluetooth.Queue;
  56. PROCEDURE DeliverPacket;
  57. BEGIN
  58. ASSERT((event#NIL));
  59. IF TraceReceive THEN KernelLog.String("UsbBluetooth: EventHandler: Packet added to event queue.");
  60. KernelLog.Ln; KernelLog.Ln;
  61. END;
  62. eventQueue := sink[Bluetooth.Event];
  63. eventQueue.Add(event);
  64. eventParamOffset:=0; eventExpectedParams:=0;
  65. event:=NIL;
  66. END DeliverPacket;
  67. BEGIN
  68. IF TraceReceive THEN
  69. KernelLog.String("UsbBluetooth: ") ; KernelLog.String(driver.name);
  70. KernelLog.String(": EventHandler: Incoming: "); KernelLog.Int(actLen, 0); KernelLog.String(" Byte(s): ");
  71. KernelLog.Ln;
  72. END;
  73. IF event=NIL THEN (* should be the beginning of a new event packet *)
  74. IF TraceReceive THEN
  75. KernelLog.String("New packet: "); ShowEvent(ORD(packet[0]));
  76. KernelLog.String(", "); KernelLog.Int(ORD(packet[1]), 0); KernelLog.String(" Byte(s) params: ");
  77. FOR i := 0 TO actLen-1 DO KernelLog.Hex(ORD(packet[i]), -2); KernelLog.Char(" ") END;
  78. (* if the packet contains status information, display it as text ... *)
  79. i := ORD(packet[0]);
  80. IF (i = 01H) OR (i=03H) OR ((i >= 05H) & (i<=0DH)) OR (i=0FH) OR (i=12H) OR (i=14H) OR (i=1CH) OR (i=1DH) THEN
  81. KernelLog.String("Status : "); ShowErrorCode(ORD(packet[2]));
  82. END;
  83. KernelLog.Ln;
  84. END;
  85. NEW(event);
  86. event.code:=packet[0];
  87. event.paramLen := ORD(packet[1]);
  88. ASSERT(event.paramLen < Bluetooth.MaxEventParamLen);
  89. IF event.paramLen>14 THEN (* there will be more fragments of this event packet *)
  90. eventExpectedParams := event.paramLen-14 ;
  91. ASSERT(actLen=16);
  92. FOR i:=0 TO 13 DO event.params[i] := packet[2+i]; END;
  93. eventParamOffset := 16;
  94. ELSE (* cool! parameters fit into this packet *)
  95. ASSERT(actLen=2+event.paramLen);
  96. FOR i:=0 TO event.paramLen-1 DO event.params[i]:=packet[2+i]; END;
  97. DeliverPacket;
  98. END;
  99. ELSE (* next fragment of packet *)
  100. IF TraceReceive THEN
  101. KernelLog.String("Fragment: "); FOR i := 0 TO LEN(packet)-1 DO KernelLog.Hex(ORD(packet[i]), -2); KernelLog.Char(" ") END;KernelLog.Ln;
  102. END;
  103. IF eventExpectedParams <= 16 THEN (* fits in this packet *)
  104. ASSERT(actLen=eventExpectedParams);
  105. FOR i:=0 TO eventExpectedParams-1 DO event.params[eventParamOffset+i]:=packet[i];END;
  106. DeliverPacket;
  107. ELSE (* there will be at least on more packet *)
  108. ASSERT(actLen=16);
  109. eventExpectedParams:=eventExpectedParams-16;
  110. eventParamOffset:=eventParamOffset+16;
  111. FOR i:=0 TO 15 DO event.params[eventParamOffset+i]:=packet[i]; END;
  112. END;
  113. END;
  114. END EventHandler;
  115. PROCEDURE ReadACL(packet : Usbdi.Buffer; actLen : LONGINT);
  116. VAR
  117. queue: Bluetooth.Queue;
  118. i: LONGINT;
  119. PROCEDURE DeliverPacket;
  120. BEGIN
  121. ASSERT(acl#NIL);
  122. queue := sink[Bluetooth.ACL];
  123. queue.Add(acl);
  124. aclParamOffset:=0; aclExpectedParams:=0;
  125. acl:=NIL;
  126. END DeliverPacket;
  127. BEGIN
  128. IF TraceReceive THEN KernelLog.String("UsbBluetooth: Device "); KernelLog.String(name); KernelLog.String(" receives ACL: "); END;
  129. IF acl=NIL THEN (* should be the beginning of a new acl packet *)
  130. NEW(acl);
  131. i := ORD(packet[0]) + ORD(packet[1])*100H;
  132. acl.handle := i MOD 1000H;
  133. acl.PB := (i DIV 1000H) MOD 4;
  134. acl.BC := (i DIV 4000H) MOD 4;
  135. acl.len := ORD(packet[2]) + ORD(packet[3])*100H;
  136. ASSERT(acl.len <= Bluetooth.MaxACLDataLen);
  137. IF TraceReceive THEN
  138. KernelLog.String("New Packet: "); KernelLog.Int(acl.len, 0); KernelLog.String(" Byte(s): ");
  139. FOR i:=0 TO actLen-1 DO KernelLog.Hex(ORD(packet[i]),-2); KernelLog.Char(" "); END;
  140. KernelLog.Ln;
  141. END;
  142. IF acl.len>60 THEN (* there will be more fragments of this ACL packet *)
  143. aclExpectedParams := acl.len-60 ;
  144. ASSERT(actLen=64);
  145. FOR i:=0 TO 59 DO acl.data[i] := packet[4+i]; END;
  146. aclParamOffset := 64;
  147. ELSE (* cool. parameters fit into this packet *)
  148. ASSERT(actLen=4+acl.len);
  149. FOR i:=0 TO acl.len-1 DO acl.data[i]:=packet[4+i]; END;
  150. DeliverPacket;
  151. END;
  152. ELSE (* next fragment of packet *)
  153. IF TraceReceive THEN
  154. KernelLog.String("Fragment: "); FOR i:=0 TO actLen-1 DO KernelLog.Hex(ORD(packet[i]),-2); KernelLog.Char(" "); END;
  155. KernelLog.Ln;
  156. END;
  157. IF aclExpectedParams <= 64 THEN (* fits in this packet *)
  158. ASSERT(actLen=aclExpectedParams);
  159. FOR i:=0 TO aclExpectedParams-1 DO acl.data[aclParamOffset+i]:=packet[i];END;
  160. DeliverPacket;
  161. ELSE (* there will be at least on more packet *)
  162. ASSERT(actLen=64);
  163. FOR i:=0 TO 63 DO acl.data[aclParamOffset+i]:=packet[i]; END;
  164. aclExpectedParams:=aclExpectedParams-64;
  165. aclParamOffset:=aclParamOffset+64;
  166. END;
  167. END;
  168. END ReadACL;
  169. PROCEDURE Send*(type: LONGINT; VAR data: ARRAY OF CHAR; ofs, len: LONGINT; VAR res: WORD);
  170. VAR i: LONGINT;
  171. BEGIN {EXCLUSIVE}
  172. IF TraceSend THEN
  173. KernelLog.Ln;
  174. KernelLog.String("UsbBluetooth: Send: "); KernelLog.String(name); KernelLog.String(": ");
  175. KernelLog.Hex(type, -2); KernelLog.Char(" ");
  176. FOR i := 0 TO len-1 DO KernelLog.Hex(ORD(data[ofs+i]), -2); KernelLog.Char(" ") END;
  177. KernelLog.Ln;
  178. END;
  179. CASE type OF
  180. | Bluetooth.Command: driver.SendCommand(data, ofs, len, res);
  181. | Bluetooth.ACL: driver.SendACL(data, ofs, len, res);
  182. (* Bluetooth.Event cannot be send to the host controller; Bluetooth.SCO would require isochronous USB transfers, which
  183. are not yet implemented *)
  184. ELSE
  185. IF TraceSend THEN KernelLog.String("wrong packet type"); KernelLog.Ln; END;
  186. res := Bluetooth.ErrInvalidParameters;
  187. END;
  188. END Send;
  189. PROCEDURE Send1H*(type: LONGINT; VAR hdr: ARRAY OF CHAR; hdrlen: LONGINT; VAR data: ARRAY OF CHAR; ofs, len: LONGINT; VAR res: WORD);
  190. VAR i: LONGINT; buffer : POINTER TO ARRAY OF CHAR; bufferLen : LONGINT;
  191. BEGIN
  192. IF TraceSend THEN
  193. KernelLog.Ln;
  194. KernelLog.String("UsbBluetooth: "); KernelLog.String(name); KernelLog.String(": Send1H:");
  195. KernelLog.String(" HdrLen: "); KernelLog.Int(hdrlen, 0); KernelLog.String(" DataLen: "); KernelLog.Int(len, 0);
  196. KernelLog.String(" DataOfs: "); KernelLog.Int(ofs, 0); KernelLog.String(" Type: "); KernelLog.Hex(type, -2);
  197. KernelLog.String(" Hdr: "); FOR i := 0 TO hdrlen-1 DO KernelLog.Hex(ORD(hdr[i]), -2); KernelLog.Char(" ") END;
  198. KernelLog.String(" Data: "); FOR i := 0 TO len-1 DO KernelLog.Hex(ORD(data[ofs+i]), -2); KernelLog.Char(" ") END;
  199. KernelLog.Ln
  200. END;
  201. bufferLen := hdrlen + len;
  202. NEW(buffer,bufferLen);
  203. FOR i:=0 TO hdrlen-1 DO buffer[i]:=hdr[i]; END;
  204. FOR i:=0 TO len-1 DO buffer[i+hdrlen]:=data[ofs+i]; END;
  205. CASE type OF
  206. | Bluetooth.Command: driver.SendCommand(buffer^, 0, bufferLen, res);
  207. | Bluetooth.ACL: driver.SendACL(buffer^, 0, bufferLen, res);
  208. (* Bluetooth.Event cannot be send to the host controller; Bluetooth.SCO would require isochronous USB transfers, which
  209. are not yet implemented *)
  210. ELSE
  211. IF TraceSend THEN KernelLog.String("wrong packet type"); KernelLog.Ln; END;
  212. res:=Bluetooth.ErrInvalidParameters;
  213. END;
  214. END Send1H;
  215. PROCEDURE Send2H*(type: LONGINT; VAR hdr1: ARRAY OF CHAR; hdr1len: LONGINT;
  216. VAR hdr2: ARRAY OF CHAR; hdr2len: LONGINT; VAR data: ARRAY OF CHAR; ofs, len: LONGINT; VAR res: WORD);
  217. VAR
  218. i: LONGINT;
  219. buffer : POINTER TO ARRAY OF CHAR;
  220. bufferLen : LONGINT;
  221. BEGIN
  222. IF TraceSend THEN
  223. KernelLog.String("UsbBluetooth: Send2H: "); KernelLog.String(name); KernelLog.String(": ");
  224. KernelLog.Hex(type, -2); KernelLog.Char(" ");
  225. FOR i := 0 TO hdr1len-1 DO KernelLog.Hex(ORD(hdr1[i]), -2); KernelLog.Char(" ") END;
  226. FOR i := 0 TO hdr2len-1 DO KernelLog.Hex(ORD(hdr2[i]), -2); KernelLog.Char(" ") END;
  227. FOR i := 0 TO len-1 DO KernelLog.Hex(ORD(data[ofs+i]), -2); KernelLog.Char(" ") END;
  228. KernelLog.Ln
  229. END;
  230. bufferLen:=hdr1len+hdr2len+len;
  231. NEW(buffer,bufferLen);
  232. FOR i:=0 TO hdr1len-1 DO buffer[i]:=hdr1[i]; END;
  233. FOR i:=0 TO hdr2len-1 DO buffer[hdr1len+i]:=hdr2[i]; END;
  234. FOR i:=0 TO len-1 DO buffer[i+hdr1len+hdr2len]:=data[ofs+i]; END;
  235. CASE type OF
  236. | Bluetooth.Command: driver.SendCommand(buffer^, 0, bufferLen, res);
  237. | Bluetooth.ACL: driver.SendACL(buffer^, 0, bufferLen, res);
  238. (* Bluetooth.Event cannot be send to the host controller; Bluetooth.SCO would require isochronous USB transfers, which
  239. are not yet implemented *)
  240. ELSE
  241. IF TraceSend THEN KernelLog.String("wrong packet type"); KernelLog.Ln; END;
  242. res := Bluetooth.ErrInvalidParameters;
  243. END;
  244. END Send2H;
  245. END UsbTransportLayer;
  246. PROCEDURE ShowEvent(event : LONGINT);
  247. BEGIN
  248. CASE event OF
  249. 01H: KernelLog.String("Inquiry Compete");
  250. |02H: KernelLog.String("Inquiry Result");
  251. |03H: KernelLog.String("Connection Complete");
  252. |04H: KernelLog.String("Connection Request");
  253. |05H: KernelLog.String("Disconnection Complete");
  254. |06H: KernelLog.String("Authentication Complete");
  255. |07H: KernelLog.String("Remote Name Request Complete");
  256. |08H: KernelLog.String("Encryption Change");
  257. |09H: KernelLog.String("Change Connection Link Key Complete");
  258. |0AH: KernelLog.String("Master Link Key Complete");
  259. |0BH: KernelLog.String("Read Remote Supported Features Complete");
  260. |0CH: KernelLog.String("Read Remote Version Information Complete");
  261. |0DH: KernelLog.String("QoS Setup Complete");
  262. |0EH: KernelLog.String("Command Complete");
  263. |0FH: KernelLog.String("Command Status");
  264. |10H: KernelLog.String("Hardware Error");
  265. |11H: KernelLog.String("Flush Occured");
  266. |12H: KernelLog.String("Role Change");
  267. |13H: KernelLog.String("Number Of Completed Packets");
  268. |14H: KernelLog.String("Mode Change");
  269. |15H: KernelLog.String("Return Link Keys");
  270. |16H: KernelLog.String("PIN Code Request");
  271. |17H: KernelLog.String("Link Key Request");
  272. |18H: KernelLog.String("Link Key Notification");
  273. |19H: KernelLog.String("Loopback Command");
  274. |1AH: KernelLog.String("Data Buffer Overflow");
  275. |1BH: KernelLog.String("Max Slots Change");
  276. |1CH: KernelLog.String("Read Clock Offset Complete");
  277. |1DH: KernelLog.String("Connection Packet Type Changed");
  278. |1EH: KernelLog.String("QoS Violation");
  279. |1FH: KernelLog.String("Page Scan Mode Change");
  280. |20H: KernelLog.String("Page Scan Repetition Mode Change");
  281. |0FEH: KernelLog.String("Bluetooth Logo Testing");
  282. |0FFH: KernelLog.String("Vendor-specific");
  283. ELSE
  284. KernelLog.String("Unkown");
  285. END;
  286. END ShowEvent;
  287. PROCEDURE ShowErrorCode(errorcode : LONGINT);
  288. BEGIN
  289. CASE errorcode OF
  290. 00H: KernelLog.String("OK");
  291. | 01H: KernelLog.String("Unknown HCI Command");
  292. | 02H: KernelLog.String("No Connection");
  293. | 03H: KernelLog.String("Hardware Failure");
  294. | 04H: KernelLog.String("Page Timeout");
  295. | 05H: KernelLog.String("Authentication Failure");
  296. | 06H: KernelLog.String("Key Missing");
  297. | 07H: KernelLog.String("Memory Full");
  298. | 08H: KernelLog.String("Connection Timeout");
  299. | 09H: KernelLog.String("Max Number Of Connections");
  300. | 0AH: KernelLog.String("Max Number Of SCO Connection To A Device");
  301. | 0BH: KernelLog.String("ACL Connection Already Exists");
  302. | 0CH: KernelLog.String("Command Disallowed");
  303. | 0DH: KernelLog.String("Host Rejected due to limited resources");
  304. | 0EH: KernelLog.String("Host Rejected due to security reasons");
  305. | 0FH: KernelLog.String("Host Rejected (Remote Device is personal device)");
  306. | 10H: KernelLog.String("Host Timeout");
  307. | 11H: KernelLog.String("Unsupported Feature or Parameter Value");
  308. | 12H: KernelLog.String("Invalid HCI Command Parameters");
  309. | 13H: KernelLog.String("Other End Terminated Connection (User ended connection)");
  310. | 14H: KernelLog.String("Other End Terminated Connection (Low Resources)");
  311. | 15H: KernelLog.String("Other End Terminated Connection (About to Power Off)");
  312. | 16H: KernelLog.String("Connection Terminated by Local Host");
  313. | 17H: KernelLog.String("Repeated Attempts");
  314. | 18H: KernelLog.String("Pairing Not Allowd");
  315. | 19H: KernelLog.String("Unknown LMP PDU");
  316. | 1AH: KernelLog.String("Unsupported Remote Feature");
  317. | 1BH: KernelLog.String("SCO Offset Rejected");
  318. | 1CH: KernelLog.String("SCO Interval Rejected");
  319. | 1DH: KernelLog.String("SCO Airmode Rejected");
  320. | 1EH: KernelLog.String("Invalid LMP Parameters");
  321. | 1FH: KernelLog.String("Unspecified Error");
  322. | 20H: KernelLog.String("Unsupported LMP Parameter Value");
  323. | 21H: KernelLog.String("Role Change Not Allowed");
  324. | 22H: KernelLog.String("LMP Response Timeout");
  325. | 23H: KernelLog.String("LMP Error Transaction Collision");
  326. | 24H: KernelLog.String("LMP PDU Not Allowed");
  327. | 25H: KernelLog.String("Encryption Mode Not Acceptable");
  328. | 26H: KernelLog.String("Unit Key Used");
  329. | 27H: KernelLog.String("QoS is Not Supported");
  330. | 28H: KernelLog.String("Instant Passed");
  331. | 29H: KernelLog.String("Pairing with Unit Key Not Supported");
  332. | 2AH..0FFH: KernelLog.String("Reserved for Future Use");
  333. ELSE
  334. KernelLog.String("Unknown");
  335. END;
  336. END ShowErrorCode;
  337. END BluetoothUSB.