1# -*- coding: utf-8 -*-
2# Copyright 2019 Oticon A/S
3# SPDX-License-Identifier: Apache-2.0
4
5import struct;
6from enum import IntEnum;
7
8opCodes = { 0x0401: 'Inquire',
9            0x0406: 'Disconnect',
10            0x041D: 'Read Remote Version Information',
11            0x0C01: 'Set Event Mask',
12            0x0C03: 'Reset',
13            0x0C2D: 'Read Transmit Power Level',
14            0x0C31: 'Set Controller To Host Flow Control',
15            0x0C33: 'Host Buffer Size',
16            0x0C35: 'Host Number Of Completed Packets',
17            0x0C63: 'Set Event Mask Page 2',
18            0x0C6C: 'Read LE Host Support',
19            0x0C6D: 'Write LE Host Support',
20            0x0C7B: 'Read Authenticated Payload Timeout',
21            0x0C7C: 'Write Authenticated Payload Timeout',
22            0x1001: 'Read Local Version Information',
23            0x1002: 'Read Local Supported Commands',
24            0x1003: 'Read Local Supported Features',
25            0x1005: 'Read Buffer Size',
26            0x1009: 'Read BD_ADDR',
27            0x1405: 'Read RSSI',
28            0x2001: 'LE Set Event Mask',
29            0x2002: 'LE Read Buffer Size',
30            0x2003: 'LE Read Local Supported Features',
31            0x2005: 'LE Set Random Address',
32            0x2006: 'LE Set Advertising Parameters',
33            0x2007: 'LE Read Advertising Channel TX Power',
34            0x2008: 'LE Set Advertising Data',
35            0x2009: 'LE Set Scan Response Data',
36            0x200A: 'LE Set Advertising Enable',
37            0x200B: 'LE Set Scan Parameters',
38            0x200C: 'LE Set Scan Enable',
39            0x200D: 'LE Create Connection',
40            0x200E: 'LE Create Connection Cancel',
41            0x200F: 'LE Read White List Size',
42            0x2010: 'LE Clear White List',
43            0x2011: 'LE Add Device To White List',
44            0x2012: 'LE Remove Device From White List',
45            0x2013: 'LE Connection Update',
46            0x2014: 'LE Set Host Channel Classification',
47            0x2015: 'LE Read Channel Map',
48            0x2016: 'LE Read Remote Features',
49            0x2017: 'LE Encrypt',
50            0x2018: 'LE Rand',
51            0x2019: 'LE Start Encryption',
52            0x201A: 'LE Long Term Key Request Reply',
53            0x201B: 'LE Long Term Key Request Negative Reply',
54            0x201C: 'LE Read Supported States',
55            0x201D: 'LE Receiver Test',
56            0x201E: 'LE Transmitter Test',
57            0x201F: 'LE Test End',
58            0x2020: 'LE Remote Connection Parameter Request Reply',
59            0x2021: 'LE Remote Connection Parameter Request Negative Reply',
60            0x2022: 'LE Set Data Length',
61            0x2023: 'LE Read Suggested Default Data Length',
62            0x2024: 'LE Write Suggested Default Data Length',
63            0x2027: 'LE Add Device to Resolving List',
64            0x2028: 'LE Remove Device From Resolving List',
65            0x2029: 'LE Clear Resolving List',
66            0x202A: 'LE Read Resolving List Size',
67            0x202B: 'LE Read Peer Resolvable Address',
68            0x202C: 'LE Read Local Resolvable Address',
69            0x202D: 'LE Set Address Resolution Enable',
70            0x202E: 'LE Set Resolvable Private Address Timeout',
71            0x202F: 'LE Read Maximum Data Length',
72            0x2030: 'LE Read PHY',
73            0x2031: 'LE Set Default PHY',
74            0x2032: 'LE Set PHY',
75            0x2033: 'LE Enhanced Receiver Test',
76            0x2034: 'LE Enhanced Transmitter Test',
77            0x2036: 'LE Set Extended Advertising Parameters',
78            0x2037: 'LE Set Extended Advertising Data',
79            0x2038: 'LE Set Extended Scan Response Data',
80            0x2039: 'LE Set Extended Advertising Enable',
81            0x203A: 'LE Read Maximum Advertising Data Length',
82            0x203B: 'LE Read Number of Supported Advertising Sets',
83            0x203C: 'LE Remove Advertising Set',
84            0x203D: 'LE Clear Advertising Sets',
85            0x203E: 'LE Set Periodic Advertising Parameters',
86            0x203F: 'LE Set Periodic Advertising Data',
87            0x2040: 'LE Set Periodic Advertising Enable',
88            0x2041: 'LE Set Extended Scan Parameters',
89            0x2042: 'LE Set Extended Scan Enable',
90            0x2043: 'LE Extended Create Connection',
91            0x2044: 'LE Periodic Advertising Create Sync',
92            0x2045: 'LE Periodic Advertising Create Sync Cancel',
93            0x2046: 'LE Periodic Advertising Terminate Sync',
94            0x2047: 'LE Add Device To Periodic Advertiser List',
95            0x2048: 'LE Remove Device From Periodic Advertiser List',
96            0x2049: 'LE Clear Periodic Advertiser List',
97            0x204A: 'LE Read Periodic Advertiser List Size',
98            0x204B: 'LE Read Transmit Power',
99            0x204C: 'LE Read RF Path Compensation',
100            0x204D: 'LE Write RF Path Compensation',
101            0x204E: 'LE Set Privacy Mode',
102            0xFC06: 'Write BD_ADDR' };
103
104def formatAddress(address, addressType=None):
105    result = '';
106    for part in reversed(address):
107        if ( len(result) ):
108            result += ':';
109        result += '%02X' % part;
110    if ( not addressType is None ):
111        result += '(R)' if ( (addressType & 1) == 1 ) else '(P)';
112    return result;
113
114def formatChannelMap(channelMap):
115    value = 0;
116    for part in reversed(channelMap):
117        value <<= 8;
118        value += part;
119    return '%010X' % value;
120
121def formatArray(intArray):
122    result = '';
123    for part in intArray:
124        if ( len(result) ):
125            result += ' ';
126        result += '%02X' % part;
127    return result;
128
129def formatPattern(pattern):
130    PatternTexts = [ 'Pseudo-Random bit sequence 9', 'Pattern of alternating bits ‘11110000’', 'Pattern of alternating bits ‘10101010’', 'Pseudo-Random bit sequence 15',
131                     'Pattern of All ‘1’ bits', 'Pattern of All ‘0’ bits', 'Pattern of alternating bits ‘00001111’', 'Pattern of alternating bits ‘0101’' ];
132    return PatternTexts[pattern] if ( pattern in range(len(PatternTexts)) ) else 'Unknown pattern!';
133
134def showCommands(commands, trace):
135    CommandTexts = [
136        [ 'Inquiry', 'Inquiry Cancel', 'Periodic Inquiry Mode', 'Exit Periodic Inquiry Mode', 'Create Connection', 'Disconnect', 'Add SCO Connection (deprecated)', 'Create Connection Cancel' ],
137        [ 'Accept Connection Request', 'Reject Connection Request', 'Link Key Request Reply', 'Link Key Request Negative Reply', 'PIN Code Request Reply', 'PIN Code Request Negative Reply', 'Change Connection Packet Type', 'Authentication Requested' ],
138        [ 'Set Connection Encryption', 'Change Connection Link Key', 'Master Link Key', 'Remote Name Request', 'Remote Name Request Cancel', 'Read Remote Supported Features', 'Read Remote Extended Features', 'Read Remote Version Information' ],
139        [ 'Read Clock Offset', 'Read LMP Handle', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use' ],
140        [ 'Reserved for Future Use', 'Hold Mode', 'Sniff Mode', 'Exit Sniff Mode', 'Reserved for Future Use', 'Reserved for Future Use', 'QoS Setup', 'Role Discovery' ],
141        [ 'Switch Role', 'Read Link Policy Settings', 'Write Link Policy Settings', 'Read Default Link Policy Settings', 'Write Default Link Policy Settings', 'Flow Specification', 'Set Event Mask', 'Reset' ],
142        [ 'Set Event Filter', 'Flush', 'Read PIN Type', 'Write PIN Type', 'Create New Unit Key', 'Read Stored Link Key', 'Write Stored Link Key', 'Delete Stored Link Key' ],
143        [ 'Write Local Name', 'Read Local Name', 'Read Connection Accept Timeout', 'Write Connection Accept Timeout', 'Read Page Timeout', 'Write Page Timeout', 'Read Scan Enable', 'Write Scan Enable' ],
144        [ 'Read Page Scan Activity', 'Write Page Scan Activity', 'Read Inquiry Scan Activity', 'Write Inquiry Scan Activity', 'Read Authentication Enable', 'Write Authentication Enable', 'Read Encryption Mode (deprecated)', 'Write Encryption Mode (deprecated)' ],
145        [ 'Read Class Of Device', 'Write Class Of Device', 'Read Voice Setting', 'Write Voice Setting', 'Read Automatic Flush Timeout', 'Write Automatic Flush Timeout', 'Read Num Broadcast Retransmissions', 'Write Num Broadcast Retransmissions' ],
146        [ 'Read Hold Mode Activity', 'Write Hold Mode Activity', 'Read Transmit Power Level', 'Read Synchronous Flow Control Enable', 'Write Synchronous Flow Control Enable', 'Set Controller To Host Flow Control', 'Host Buffer Size', 'Host Number Of Completed Packets' ],
147        [ 'Read Link Supervision Timeout', 'Write Link Supervision Timeout', 'Read Number of Supported IAC', 'Read Current IAC LAP', 'Write Current IAC LAP', 'Read Page Scan Mode Period (deprecated)', 'Write Page Scan Mode Period (deprecated)', 'Read Page Scan Mode (deprecated)' ],
148        [ 'Write Page Scan Mode (deprecated)', 'Set AFH Host Channel Classification', 'Reserved for Future Use', 'Reserved for Future Use', 'Read Inquiry Scan Type', 'Write Inquiry Scan Type', 'Read Inquiry Mode', 'Write Inquiry Mode' ],
149        [ 'Read Page Scan Type', 'Write Page Scan Type', 'Read AFH Channel Assessment Mode', 'Write AFH Channel Assessment Mode', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use' ],
150        [ 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Read Local Version Information', 'Reserved for Future Use', 'Read Local Supported Features', 'Read Local Extended Features', 'Read Buffer Size' ],
151        [ 'Read Country Code [Deprecated]', 'Read BD ADDR', 'Read Failed Contact Counter', 'Reset Failed Contact Counter', 'Read Link Quality', 'Read RSSI', 'Read AFH Channel Map', 'Read Clock' ],
152        [ 'Read Loopback Mode', 'Write Loopback Mode', 'Enable Device Under Test Mode', 'Setup Synchronous Connection Request', 'Accept Synchronous Connection Request', 'Reject Synchronous Connection Request', 'Reserved for Future Use', 'Reserved for Future Use' ],
153        [ 'Read Extended Inquiry Response', 'Write Extended Inquiry Response', 'Refresh Encryption Key', 'Reserved for Future Use', 'Sniff Subrating', 'Read Simple Pairing Mode', 'Write Simple Pairing Mode', 'Read Local OOB Data' ],
154        [ 'Read Inquiry Response Transmit Power Level', 'Write Inquiry Transmit Power Level', 'Read Default Erroneous Data Reporting', 'Write Default Erroneous Data Reporting', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'IO Capability Request Reply' ],
155        [ 'User Confirmation Request Reply', 'User Confirmation Request Negative Reply', 'User Passkey Request Reply', 'User Passkey Request Negative Reply', 'Remote OOB Data Request Reply', 'Write Simple Pairing Debug Mode', 'Enhanced Flush', 'Remote OOB Data Request Negative Reply' ],
156        [ 'Reserved for Future Use', 'Reserved for Future Use', 'Send Keypress Notification', 'IO Capability Request Negative Reply', 'Read Encryption Key Size', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use' ],
157        [ 'Create Physical Link', 'Accept Physical Link', 'Disconnect Physical Link', 'Create Logical Link', 'Accept Logical Link', 'Disconnect Logical Link', 'Logical Link Cancel', 'Flow Spec Modify' ],
158        [ 'Read Logical Link Accept Timeout', 'Write Logical Link Accept Timeout', 'Set Event Mask Page 2', 'Read Location Data', 'Write Location Data', 'Read Local AMP Info', 'Read Local AMP_ASSOC', 'Write Remote AMP_ASSOC' ],
159        [ 'Read Flow Control Mode', 'Write Flow Control Mode', 'Read Data Block Size', 'Reserved for Future Use', 'Reserved for Future Use', 'Enable AMP Receiver Reports', 'AMP Test End', 'AMP Test' ],
160        [ 'Read Enhanced Transmit Power Level', 'Reserved for Future Use', 'Read Best Effort Flush Timeout', 'Write Best Effort Flush Timeout', 'Short Range Mode', 'Read LE Host Support', 'Write LE Host Support', 'Reserved for Future Use' ],
161        [ 'LE Set Event Mask', 'LE Read Buffer Size', 'LE Read Local Supported Features', 'Reserved for Future Use', 'LE Set Random Address', 'LE Set Advertising Parameters', 'LE Read Advertising Channel TX Power', 'LE Set Advertising Data' ],
162        [ 'LE Set Scan Response Data', 'LE Set Advertising Enable', 'LE Set Scan Parameters', 'LE Set Scan Enable', 'LE Create Connection', 'LE Create Connection Cancel', 'LE Read White List Size', 'LE Clear White List' ],
163        [ 'LE Add Device To White List', 'LE Remove Device From White List', 'LE Connection Update', 'LE Set Host Channel Classification', 'LE Read Channel Map', 'LE Read Remote Features', 'LE Encrypt', 'LE Rand' ],
164        [ 'LE Start Encryption', 'LE Long Term Key Request Reply', 'LE Long Term Key Request Negative Reply', 'LE Read Supported States', 'LE Receiver Test', 'LE Transmitter Test', 'LE Test End', 'Reserved for Future Use' ],
165        [ 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Enhanced Setup Synchronous Connection', 'Enhanced Accept Synchronous Connection', 'Read Local Supported Codecs', 'Set MWS Channel Parameters', 'Set External Frame Configuration' ],
166        [ 'Set MWS Signaling', 'Set MWS Transport Layer', 'Set MWS Scan Frequency Table', 'Get MWS Transport Layer Configuration', 'Set MWS PATTERN Configuration', 'Set Triggered Clock Capture', 'Truncated Page', 'Truncated Page Cancel' ],
167        [ 'Set Connectionless Slave Broadcast', 'Set Connectionless Slave Broadcast Receive', 'Start Synchronization Train', 'Receive Synchronization Train', 'Set Reserved LT_ADDR', 'Delete Reserved LT_ADDR', 'Set Connectionless Slave Broadcast Data', 'Read Synchronization Train Parameters' ],
168        [ 'Write Synchronization Train Parameters', 'Remote OOB Extended Data Request Reply', 'Read Secure Connections Host Support', 'Write Secure Connections Host Support', 'Read Authenticated Payload Timeout', 'Write Authenticated Payload Timeout', 'Read Local OOB Extended Data', 'Write Secure Connections Test Mode' ],
169        [ 'Read Extended Page Timeout', 'Write Extended Page Timeout', 'Read Extended Inquiry Length', 'Write Extended Inquiry Length', 'LE Remote Connection Parameter Request Reply', 'LE Remote Connection Parameter Request Negative Reply', 'LE Set Data Length', 'LE Read Suggested Default Data Length' ],
170        [ 'LE Write Suggested Default Data Length', 'LE Read Local P-256 Public Key', 'LE Generate DH Key', 'LE Add Device To Resolving List', 'LE Remove Device From Resolving List', 'LE Clear Resolving List', 'LE Read Resolving List Size', 'LE Read Peer Resolvable Address' ],
171        [ 'LE Read Local Resolvable Address', 'LE Set Address Resolution Enable', 'LE Set Resolvable Private Address Timeout', 'LE Read Maximum Data Length', 'LE Read PHY Command', 'LE Set Default PHY Command', 'LE Set PHY Command', 'LE Enhanced Receiver Test Command' ],
172        [ 'LE Enhanced Transmitter Test Command', 'LE Set Advertising Set Random Address Command', 'LE Set Extended Advertising Parameters Command', 'LE Set Extended Advertising Data Command', 'LE Set Extended Scan Response Data Command', 'LE Set Extended Advertising Enable Command', 'LE Read Maximum Advertising Data Length Command', 'LE Read Number of Supported Advertising Sets Command' ],
173        [ 'LE Remove Advertising Set Command', 'LE Clear Advertising Sets Command', 'LE Set Periodic Advertising Parameters Command', 'LE Set Periodic Advertising Data Command', 'LE Set Periodic Advertising Enable Command', 'LE Set Extended Scan Parameters Command', 'LE Set Extended Scan Enable Command', 'LE Extended Create Connection Command' ],
174        [ 'LE Periodic Advertising Create Sync Command', 'LE Periodic Advertising Create Sync Cancel Command', 'LE Periodic Advertising Terminate Sync Command', 'LE Add Device To Periodic Advertiser List Command', 'LE Remove Device From Periodic Advertiser List Command', 'LE Clear Periodic Advertiser List Command', 'LE Read Periodic Advertiser List Size Command', 'LE Read Transmit Power Command' ],
175        [ 'LE Read RF Path Compensation Command', 'LE Write RF Path Compensation Command', 'LE Set Privacy Mode', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use' ]
176    ];
177
178    for n in range(len(CommandTexts)):
179        fmt = '%2i %s';
180        for i in range(8):
181            txt = 'SUPPORTED' if ( commands[n] & (1<<i) ) else 'NOT SUPPORTED';
182            if ( i ):
183               trace.trace(8, fmt % CommandTexts[n][i] + ' ' + txt);
184            else:
185               trace.trace(8, fmt % (n, CommandTexts[n][i] + ' ' + txt));
186            fmt = '   %s';
187
188def showFeatures(features, trace):
189    FeatureTexts = [
190        [ '3 slot packets', '5 slot packets', 'Encryption', 'Slot offset', 'Timing accuracy', 'Role switch', 'Hold mode', 'Sniff mode' ],
191        [ 'Reserved for Future Use', 'Power control requests', 'Channel quality driven data rate (CQDDR)', 'SCO link', 'HV2 packets', 'HV3 packets', 'µ-law log synchronous data', 'A-law log synchronous data' ],
192        [ 'CVSD synchronous data', 'Paging parameter negotiation', 'Power control', 'Transparent synchronous data', 'Flow control lag (least significant bit)', 'Flow control lag (middle bit)', 'Flow control lag (most significant bit)', 'Broadcast Encryption' ],
193        [ 'Reserved for Future Use', 'Enhanced Data Rate ACL 2 Mb/s mode', 'Enhanced Data Rate ACL 3 Mb/s mode', 'Enhanced inquiry scan', 'Interlaced inquiry scan', 'Interlaced page scan', 'RSSI with inquiry results', 'Extended SCO link (EV3 packets)' ],
194        [ 'EV4 packets', 'EV5 packets', 'Reserved for Future Use', 'AFH capable slave', 'AFH classification slave', 'BR/EDR Not Supported', 'LE Supported (Controller)', '3-slot Enhanced Data Rate ACL packets' ],
195        [ '5-slot Enhanced Data Rate ACL packets', 'Sniff subrating', 'Pause encryption', 'AFH capable master', 'AFH classification master', 'Enhanced Data Rate eSCO 2 Mb/s mode', 'Enhanced Data Rate eSCO 3 Mb/s mode', '3-slot Enhanced Data Rate eSCO packets' ],
196        [ 'Extended Inquiry Response', 'Simultaneous LE and BR/EDR to Same Device Capable (Controller)', 'Reserved for Future Use', 'Secure Simple Pairing', 'Encapsulated PDU', 'Erroneous Data Reporting', 'Non-flushable Packet Boundary Flag', 'Reserved for Future Use' ],
197        [ 'Link Supervision Timeout Changed Event', 'Inquiry TX Power Level', 'Enhanced Power Control', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use' ]
198    ];
199
200    for n in range(len(FeatureTexts)):
201        fmt = '%2i %s';
202        for i in range(8):
203            txt = 'SUPPORTED' if ( features[n] & (1<<i) ) else 'NOT SUPPORTED';
204            if ( i ):
205                trace.trace(8, fmt % FeatureTexts[n][i] + ' ' + txt);
206            else:
207                trace.trace(8, fmt % (n, FeatureTexts[n][i] + ' ' + txt));
208            fmt = '   %s';
209
210def showLEFeatures(features, trace):
211    LEFeatureTexts = [
212        [ 'LE Encryption', 'Connection Parameters Request Procedure', 'Extended Reject Indication', 'Slave-initiated Features Exchange', 'LE Ping', 'LE Data Packet Length Extension', 'LL Privacy', 'Extended Scanner Filter Policies' ],
213        [ 'LE 2M PHY', 'Stable Modulation Index - Transmitter', 'Stable Modulation Index - Receiver', 'LE Coded PHY', 'LE Extended Advertising', 'LE Periodic Advertising', 'Channel Selection Algorithm #2', 'LE Power Class 1' ],
214        [ 'Minimum Number of Used Channels Procedure', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use', 'Reserved for Future Use' ],
215    ];
216
217    for n in range(len(LEFeatureTexts)):
218        fmt = '%2i %s';
219        for i in range(8):
220            txt = 'SUPPORTED' if ( features[n] & (1<<i) ) else 'NOT SUPPORTED';
221            if ( i ):
222                trace.trace(8, fmt % LEFeatureTexts[n][i] + ' ' + txt);
223            else:
224                trace.trace(8, fmt % (n, LEFeatureTexts[n][i] + ' ' + txt));
225            fmt = '   %s';
226
227def showLEStates(states, trace):
228    LEStateTexts = [ 'Scannable Advertising State', 'Connectable Advertising State', 'Non-connectable Advertising State', 'High Duty Cycle Directed Advertising State', 'Low Duty Cycle Directed Advertising State',
229                     'Active Scanning State', 'Passive Scanning State', 'Initiating State', 'Connection State (Master Role)', 'Connection State (Slave Role)' ];
230    LEStateMap   = [ [   4,   1,   2,   8,  64,  32, 128, 512 ],
231                     [  68,  65,  66,  72,  36,  33,  34,  40 ],
232                     [ 132, 129, 260, 257, 516, 513, 192, 160 ],
233                     [ 320, 288, 576, 544, 384,  16,  80,  48 ],
234                     [ 130, 136, 144, 258, 264, 272, 514, 520 ],
235                     [ 528, 640,   0,   0,   0,   0,   0,   0 ] ];
236
237    for n in range(len(LEStateMap)):
238        fmt = '%2i %s';
239        for i in range(8):
240            if ( LEStateMap[n][i] ):
241                txt = '';
242                for j in range(len(LEStateTexts)):
243                    if ( LEStateMap[n][i] & (1<<j) ):
244                        if ( len(txt) ):
245                             txt += ' | '
246                        txt += LEStateTexts[j];
247                txt += ' SUPPORTED' if ( states[n] & (1<<i) ) else ' NOT SUPPORTED';
248                if ( i ):
249                    trace.trace(8, fmt % txt);
250                else:
251                    trace.trace(8, fmt % (n, txt));
252            fmt = '   %s';
253
254def showEventMask(eventMask, trace):
255    """
256        0x0000000000000000 ~ No events specified.
257    """
258    EventTexts = [ 'Inquiry Complete Event', 'Inquiry Result Event', 'Connection Complete Event', 'Connection Request Event',
259                   'Disconnection Complete Event', 'Authentication Complete Event', 'Remote Name Request Complete Event', 'Encryption Change Event',
260                   'Change Connection Link Key Complete Event', 'Master Link Key Complete Event', 'Read Remote Supported Features Complete Event', 'Read Remote Version Information Complete Event',
261                   'QoS Setup Complete Event', 'Reserved', 'Reserved', 'Hardware Error Event',
262                   'Flush Occurred Event', 'Role Change Event', 'Reserved', 'Mode Change Event',
263                   'Return Link Keys Event', 'PIN Code Request Event', 'Link Key Request Event', 'Link Key Notification Event',
264                   'Loopback Command Event', 'Data Buffer Overflow Event', 'Max Slots Change Event', 'Read Clock Offset Complete Event',
265                   'Connection Packet Type Changed Event', 'QoS Violation Event', 'Page Scan Mode Change Event [deprecated]', 'Page Scan Repetition Mode Change Event',
266                   'Flow Specification Complete Event', 'Inquiry Result with RSSI Event', 'Read Remote Extended Features Complete Event', 'Reserved',
267                   'Reserved', 'Reserved', 'Reserved', 'Reserved',
268                   'Reserved', 'Reserved', 'Reserved', 'Synchronous Connection Complete Event',
269                   'Synchronous Connection Changed Event', 'Sniff Subrating Event', 'Extended Inquiry Result Event', 'Encryption Key Refresh Complete Event',
270                   'IO Capability Request Event', 'IO Capability Request Reply Event', 'User Confirmation Request Event', 'User Passkey Request Event',
271                   'Remote OOB Data Request Event','Simple Pairing Complete Event', 'Reserved', 'Link Supervision Timeout Changed Event',
272                   'Enhanced Flush Complete Event', 'Reserved', 'User Passkey Notification Event', 'Keypress Notification Event',
273                   'Remote Host Supported Features Notification Event', 'LE Meta-Event' ];
274    """
275        0x00001FFFFFFFFFFF ~ Default.
276        0xC000000000000000 ~ Reserved for future use."
277    """
278    value = 0;
279    for part in reversed(eventMask):
280        value <<= 8;
281        value += part;
282    if ( value & 0x3FFFFFFFFFFFFFFF ):
283        if ( value == 0x1FFFFFFFFFFF ):
284            txt = 'Default';
285        else:
286            txt = '';
287            for n in range(len(EventTexts)):
288                if ( len(txt) ):
289                    txt += ' | ';
290                if ( value & (1<<n) ):
291                    txt += LEEventTexts[n];
292    else:
293        txt = 'No events specified';
294    trace.trace(8, txt);
295
296def showLEEventMask(eventMask, trace):
297    """
298        0x0000000000000000 ~ No LE events specified.
299    """
300    LEEventTexts = [ 'LE Connection Complete Event', 'LE Advertising Report Event', 'LE Connection Update Complete Event', 'LE Read Remote Used Features Complete Event',
301                     'LE Long Term Key Request Event', 'LE Remote Connection Parameter Request Event' ];
302    """
303        0x000000000000001F ~ Default.
304        0xFFFFFFFFFFFFFFC0 ~ Reserved for future use.
305    """
306    value = 0;
307    for part in reversed(eventMask):
308        value <<= 8;
309        value += part;
310    if ( value & 0x1f ):
311        if ( value == 0x1f ):
312            txt = 'Default';
313        else:
314            txt = '';
315            for n in range(len(LEEventTexts)):
316                if ( len(txt) ):
317                    txt += ' | ';
318                if ( value & (1<<n) ):
319                    txt += LEEventTexts[n];
320    else:
321        txt = 'No LE events specified';
322    trace.trace(8, txt);
323
324def toNumber(intArray):
325    value = 0;
326    for part in reversed(intArray):
327        value <<= 8;
328        value += part;
329    return value;
330
331def toArray(number, size):
332    array = [0 for _ in range(size)];
333
334    for i in range(size):
335        array[i] = number & 0xFF;
336        number >>= 8;
337    return array;
338