xref: /btstack/test/mesh/dump_mesh_pklg.py (revision 98e0b87678cf1ef78ba9f9322b66c49d467b3ada)
1#!/usr/bin/env python3
2# BlueKitchen GmbH (c) 2019
3
4# primitive dump for PacketLogger format
5
6# APPLE PacketLogger
7# typedef struct {
8#   uint32_t    len;
9#   uint32_t    ts_sec;
10#   uint32_t    ts_usec;
11#   uint8_t     type;   // 0xfc for note
12# }
13
14#define BLUETOOTH_DATA_TYPE_PB_ADV                                             0x29 // PB-ADV
15#define BLUETOOTH_DATA_TYPE_MESH_MESSAGE                                       0x2A // Mesh Message
16#define BLUETOOTH_DATA_TYPE_MESH_BEACON                                        0x2B // Mesh Beacon
17
18import re
19import sys
20import time
21import datetime
22import struct
23from mesh_crypto import *
24
25# state
26netkeys = {}
27appkeys = {}
28devkey = b''
29ivi = b'\x00'
30segmented_messages = {}
31
32access_messages = {
33# Foundation
340x0000: 'Config Appkey Add',
350x0001: 'Config Appkey Update',
360x0002: 'Config Composition Data Status',
370x0003: 'Config Model Publication Set',
380x0004: 'Config Health Current Status',
390x0005: 'Config Health Fault Status',
400x0006: 'Config Heartbeat Publication Status',
410x8000: 'Config Appkey Delete',
420x8001: 'Config Appkey Get',
430x8002: 'Config Appkey List',
440x8003: 'Config Appkey Status',
450x8004: 'Config Health Attention Get',
460x8005: 'Config Health Attention Set',
470x8006: 'Config Health Attention Set Unacknowledged',
480x8007: 'Config Health Attention Status',
490x8008: 'Config Composition Data Get',
500x8009: 'Config Beacon Get',
510x800a: 'Config Beacon Set',
520x800b: 'Config Beacon Status',
530x800c: 'Config Default Ttl Get',
540x800d: 'Config Default Ttl Set',
550x800e: 'Config Default Ttl Status',
560x800f: 'Config Friend Get',
570x8010: 'Config Friend Set',
580x8011: 'Config Friend Status',
590x8012: 'Config Gatt Proxy Get',
600x8013: 'Config Gatt Proxy Set',
610x8014: 'Config Gatt Proxy Status',
620x8015: 'Config Key Refresh Phase Get',
630x8016: 'Config Key Refresh Phase Set',
640x8017: 'Config Key Refresh Phase Status',
650x8018: 'Config Model Publication Get',
660x8019: 'Config Model Publication Status',
670x801a: 'Config Model Publication Virtual Address Set',
680x801b: 'Config Model Subscription Add',
690x801c: 'Config Model Subscription Delete',
700x801d: 'Config Model Subscription Delete All',
710x801e: 'Config Model Subscription Overwrite',
720x801f: 'Config Model Subscription Status',
730x8020: 'Config Model Subscription Virtual Address Add',
740x8021: 'Config Model Subscription Virtual Address Delete',
750x8022: 'Config Model Subscription Virtual Address Overwrite',
760x8023: 'Config Network Transmit Get',
770x8024: 'Config Network Transmit Set',
780x8025: 'Config Network Transmit Status',
790x8026: 'Config Relay Get',
800x8027: 'Config Relay Set',
810x8028: 'Config Relay Status',
820x8029: 'Config Sig Model Subscription Get',
830x802a: 'Config Sig Model Subscription List',
840x802b: 'Config Vendor Model Subscription Get',
850x802c: 'Config Vendor Model Subscription List',
860x802d: 'Config Low Power Node Poll Timeout Get',
870x802e: 'Config Low Power Node Poll Timeout Status',
880x802f: 'Config Health Fault Clear',
890x8030: 'Config Health Fault Clear Unacknowledged',
900x8031: 'Config Health Fault Get',
910x8032: 'Config Health Fault Test',
920x8033: 'Config Health Fault Test Unacknowledged',
930x8034: 'Config Health Period Get',
940x8035: 'Config Health Period Set',
950x8036: 'Config Health Period Set Unacknowledged',
960x8037: 'Config Health Period Status',
970x8038: 'Config Heartbeat Publication Get',
980x8039: 'Config Heartbeat Publication Set',
990x803a: 'Config Heartbeat Subscription Get',
1000x803b: 'Config Heartbeat Subscription Set',
1010x803c: 'Config Heartbeat Subscription Status',
1020x803d: 'Config Model App Bind',
1030x803e: 'Config Model App Status',
1040x803f: 'Config Model App Unbind',
1050x8040: 'Config Netkey Add',
1060x8041: 'Config Netkey Delete',
1070x8042: 'Config Netkey Get',
1080x8043: 'Config Netkey List',
1090x8044: 'Config Netkey Status',
1100x8045: 'Config Netkey Update',
1110x8046: 'Config Node Identity Get',
1120x8047: 'Config Node Identity Set',
1130x8048: 'Config Node Identity Status',
1140x8049: 'Config Node Reset',
1150x804a: 'Config Node Reset Status',
1160x804b: 'Config Sig Model App Get',
1170x804c: 'Config Sig Model App List',
1180x804d: 'Config Vendor Model App Get',
1190x804e: 'Config Vendor Model App List',
120# Generic
1210x8201: 'Generic On Off Get',
1220x8202: 'Generic On Off Set',
1230x8203: 'Generic On Off Set Unacknowledged',
1240x8204: 'Generic On Off Status',
1250x8205: 'Generic Level Get',
1260x8206: 'Generic Level Set',
1270x8207: 'Generic Level Set Unacknowledged',
1280x8208: 'Generic Level Status',
1290x8209: 'Generic Delta Set',
1300x820A: 'Generic Delta Set Unacknowledged',
1310x820B: 'Generic Move Set',
1320x820C: 'Generic Move Set Unacknowledged',
133}
134
135# quick hack to convert Access message opcode + names
136tmp_messages =[
137# 'GENERIC_ON_OFF_GET                     :0x8201',
138]
139for message in tmp_messages:
140    parts = message.split(':')
141    print ("%s: '%s'," % (parts[1].strip(),parts[0].strip().title().replace('_',' ')))
142
143# helpers
144def read_net_32_from_file(f):
145    data = f.read(4)
146    if len(data) < 4:
147        return -1
148    return struct.unpack('>I', data)[0]
149
150def as_hex(data):
151    return ''.join(["{0:02x} ".format(byte) for byte in data])
152
153def as_big_endian32(value):
154    return struct.pack('>I', value)
155
156def read_net_16(data):
157    return struct.unpack('>H', data)[0]
158
159def read_net_24(data):
160    return data[0] << 16 | struct.unpack('>H', data[1:3])[0]
161
162def read_net_32(data):
163    return struct.unpack('>I', data)[0]
164
165# log engine - simple pretty printer
166max_indent = 0
167def log_pdu(pdu, indent = 0, hide_properties = []):
168    spaces = '    ' * indent
169    print(spaces + "%-20s %s" % (pdu.type, pdu.summary))
170    if indent >= max_indent:
171        return
172    for property in pdu.properties:
173        if property.key in hide_properties:
174            continue
175        if isinstance( property.value, int):
176            print (spaces + "|%20s: 0x%x (%u)" % (property.key, property.value, property.value))
177        elif isinstance( property.value, bytes):
178            print (spaces + "|%20s: %s" % (property.key, as_hex(property.value)))
179        else:
180            print (spaces + "|%20s: %s" % (property.key, str(property.value)))
181        hide_properties.append(property.key)
182    print (spaces + '|           data: ' + as_hex(pdu.data))
183    print (spaces + '----')
184    for origin in pdu.origins:
185        log_pdu(origin, indent + 1, hide_properties)
186
187# classes
188
189class network_key(object):
190    def __init__(self, index, netkey):
191        self.index = index
192        self.netkey = netkey
193        (self.nid, self.encryption_key, self.privacy_key) = k2(netkey, b'\x00')
194
195    def __repr__(self):
196        return ("NetKey-%04x %s: NID %02x Encryption %s Privacy %s" % (self.index, self.netkey.hex(), self.nid, self.encryption_key.hex(), self.privacy_key.hex()))
197
198class application_key(object):
199    def __init__(self, index, appkey):
200        self.index = index
201        self.appkey = appkey
202        self.aid = k4(self.appkey)
203
204    def __repr__(self):
205        return ("AppKey-%04x %s: AID %02x" % (self.index, self.appkey.hex(), self.aid))
206
207class property(object):
208    def __init__(self, key, value):
209        self.key = key
210        self.value = value
211
212class layer_pdu(object):
213    def __init__(self, pdu_type, pdu_data):
214        self.summary = ''
215        self.src = None
216        self.dst = None
217        self.type = pdu_type
218        self.data = pdu_data
219        self.origins = []
220        self.properties = []
221
222    def add_property(self, key, value):
223        self.properties.append(property(key, value))
224
225class network_pdu(layer_pdu):
226    def __init__(self, pdu_data):
227        super().__init__("Network(unencrpyted)", pdu_data)
228
229        # parse pdu
230        self.ivi = (self.data[1] & 0x80) >> 7
231        self.nid = self.data[0] & 0x7f
232        self.ctl = (self.data[1] & 0x80) == 0x80
233        self.ttl = self.data[1] & 0x7f
234        self.seq = read_net_24(self.data[2:5])
235        self.src = read_net_16(self.data[5:7])
236        self.dst = read_net_16(self.data[7:9])
237        self.lower_transport = self.data[9:]
238
239        # set properties
240        self.add_property('ivi', self.ivi)
241        self.add_property('nid', self.nid)
242        self.add_property('ctl', self.ctl)
243        self.add_property('ttl', self.ttl)
244        self.add_property('seq', self.seq)
245        self.add_property('src', self.src)
246        self.add_property('dst', self.dst)
247        self.add_property('lower_transport', self.lower_transport)
248
249class lower_transport_pdu(layer_pdu):
250    def __init__(self, network_pdu):
251        super().__init__('Lower Transport', network_pdu.lower_transport)
252
253        # inherit properties
254        self.ctl = network_pdu.ctl
255        self.seq = network_pdu.seq
256        self.src = network_pdu.src
257        self.dst = network_pdu.dst
258        self.add_property('ctl', self.ctl)
259        self.add_property('seq', self.seq)
260        self.add_property('src', self.src)
261        self.add_property('dst', self.dst)
262
263        # parse pdu and set propoerties
264        self.seg = (self.data[0] & 0x80) == 0x80
265        self.add_property('seg', self.seg)
266        self.szmic = False
267        if self.ctl:
268            self.opcode = self.data[0] & 0x7f
269            self.add_property('opcode', self.opcode)
270        else:
271            self.aid = self.data[0] & 0x3f
272            self.add_property('aid', self.aid)
273            self.akf = self.data[0] & 0x40 == 0x040
274            self.add_property('akf', self.akf)
275        if self.seg:
276            if not self.ctl:
277                self.szmic = self.data[1] & 0x80 == 0x80
278                self.add_property('szmic', self.szmic)
279            temp_12 = struct.unpack('>H', self.data[1:3])[0]
280            self.seq_zero = (temp_12 >> 2) & 0x1fff
281            self.add_property('seq_zero', self.seq_zero)
282            temp_23 = struct.unpack('>H', self.data[2:4])[0]
283            self.seg_o = (temp_23 >> 5) & 0x1f
284            self.add_property('seg_o', self.seg_o)
285            self.seg_n =  temp_23 & 0x1f
286            self.add_property('seg_n', self.seg_n)
287            self.segment = self.data[4:]
288            self.add_property('segment', self.segment)
289        else:
290            self.seq_auth = self.seq
291            self.upper_transport = self.data[1:]
292            self.add_property('upper_transport', self.upper_transport)
293
294class upper_transport_pdu(layer_pdu):
295    def __init__(self, segment):
296        if segment.ctl:
297            super().__init__('Segmented Control', b'')
298        else:
299            super().__init__('Segmented Transport', b'')
300        self.ctl      = segment.ctl
301        self.src      = segment.src
302        self.dst      = segment.dst
303        self.seq      = segment.seq
304        self.akf      = segment.akf
305        self.aid      = segment.aid
306        self.szmic    = segment.szmic
307        self.seg_n    = segment.seg_n
308        self.seq_zero = segment.seq_zero
309        self.direction = segment.direction
310        # TODO handle seq_zero overrun
311        self.seq_auth = segment.seq & 0xffffe000 | segment.seq_zero
312        self.add_property('seq_auth', self.seq_auth)
313        self.missing  = (1 << (segment.seg_n+1)) - 1
314        self.data = b''
315        self.processed = False
316        self.origins = []
317        if self.ctl:
318            self.segment_len = 8
319        else:
320            self.segment_len = 12
321
322        self.add_property('src', self.src)
323        self.add_property('dst', self.dst)
324        self.add_property('aid', self.aid)
325        self.add_property('akf', self.akf)
326        self.add_property('segment_len', self.segment_len)
327        self.add_property('dir', self.direction)
328
329    def add_segment(self, network_pdu):
330        self.origins.append(network_pdu)
331        self.missing &= ~ (1 << network_pdu.seg_o)
332        if network_pdu.seg_o == self.seg_n:
333            # last segment, set len
334            self.len = (self.seg_n * self.segment_len) + len(network_pdu.segment)
335        if len(self.data) == 0 and self.complete():
336            self.reassemble()
337
338    def complete(self):
339        return self.missing == 0
340
341    def reassemble(self):
342        self.data = bytearray(self.len)
343        missing  = (1 << (self.seg_n+1)) - 1
344        for pdu in self.origins:
345            if pdu.ctl:
346                continue
347            # copy data
348            pos = pdu.seg_o * self.segment_len
349            self.data[pos:pos+len(pdu.segment)] = pdu.segment
350            # done?
351            missing &= ~ (1 << pdu.seg_o)
352            if missing == 0:
353                break
354
355class access_pdu(layer_pdu):
356    def __init__(self, lower_pdu, data):
357        super().__init__('Access', b'')
358        self.src       = lower_pdu.src
359        self.dst       = lower_pdu.dst
360        self.akf       = lower_pdu.akf
361        self.aid       = lower_pdu.aid
362        self.seq_auth  = lower_pdu.seq_auth
363        self.data      = data
364        self.direction = lower_pdu.direction
365        self.add_property('src', self.src)
366        self.add_property('dst', self.dst)
367        self.add_property('akf', self.akf)
368        self.add_property('aid', self.aid)
369        self.add_property('seq_auth', self.seq_auth)
370        self.add_property('dir', self.direction)
371
372        upper_bits = data[0] >> 6
373        if upper_bits == 0 or upper_bits == 1:
374            self.opcode = data[0]
375            self.opcode_len = 1
376        elif upper_bits == 2:
377            self.opcode = read_net_16(data[0:2])
378            self.opcode_len = 2
379        elif upper_bits == 3:
380            self.opcode = read_net_24(data[0:3])
381            self.opcode_len = 3
382        self.add_property('opcode', self.opcode)
383        self.params = data[self.opcode_len:]
384        self.add_property('params', self.params)
385        if self.opcode in access_messages:
386            self.summary = access_messages[self.opcode]
387
388def segmented_message_tag(src, direction):
389    tag = str(src) + direction
390    return tag
391
392def segmented_message_for_pdu(pdu):
393    tag = segmented_message_tag(pdu.src, pdu.direction)
394    if tag in segmented_messages:
395        seg_message = segmented_messages[tag]
396        # check seq zero
397        if pdu.seq_zero  == seg_message.seq_zero:
398            return seg_message
399    # print("new segmented message: src %04x, seq_zero %04x" % (pdu.src, pdu.seq_zero))
400    seg_message = upper_transport_pdu(pdu)
401    segmented_messages[tag] = seg_message
402    return seg_message
403
404def mesh_set_iv_index(iv_index):
405    global ivi
406    ivi = iv_index
407    print ("IV-Index: " + as_big_endian32(ivi).hex())
408
409# key management
410def mesh_add_netkey(index, netkey):
411    key = network_key(index, netkey)
412    print (key)
413    netkeys[index] = key
414
415def mesh_network_keys_for_nid(nid):
416    for (index, key) in netkeys.items():
417        if key.nid == nid:
418            yield key
419
420def mesh_set_device_key(key):
421    global devkey
422    print ("DevKey: " + key.hex())
423    devkey = key
424
425def mesh_add_application_key(index, appkey):
426    key = application_key(index, appkey)
427    print (key)
428    appkeys[index] = key
429
430def mesh_application_keys_for_aid(aid):
431    for (index, key) in appkeys.items():
432        if key.aid == aid:
433            yield key
434
435def mesh_transport_nonce(pdu, nonce_type):
436    if pdu.szmic:
437        aszmic = 0x80
438    else:
439        aszmic = 0x00
440    return bytes( [nonce_type, aszmic, pdu.seq_auth >> 16, (pdu.seq_auth >> 8) & 0xff, pdu.seq_auth & 0xff, pdu.src >> 8, pdu.src & 0xff, pdu.dst >> 8, pdu.dst & 0xff]) + as_big_endian32(ivi)
441
442def mesh_application_nonce(pdu):
443    return mesh_transport_nonce(pdu, 0x01)
444
445def mesh_device_nonce(pdu):
446    return mesh_transport_nonce(pdu, 0x02)
447
448def mesh_upper_transport_decrypt(message, data):
449    if message.szmic:
450        trans_mic_len = 8
451    else:
452        trans_mic_len = 4
453    ciphertext = data[:-trans_mic_len]
454    trans_mic  = data[-trans_mic_len:]
455    decrypted = None
456    if message.akf:
457        nonce = mesh_application_nonce(message)
458        # print( as_hex(nonce))
459        for key in mesh_application_keys_for_aid(message.aid):
460            decrypted = aes_ccm_decrypt(key.appkey, nonce, ciphertext, b'', trans_mic_len, trans_mic)
461            if decrypted != None:
462                break
463    else:
464        nonce = mesh_device_nonce(message)
465        decrypted =  aes_ccm_decrypt(devkey, nonce, ciphertext, b'', trans_mic_len, trans_mic)
466    return decrypted
467
468def mesh_process_control(control_pdu):
469    # TODO decode control message
470    # TODO add Seg Ack to sender access message origins
471    control_pdu.opcode = control_pdu.data[0]
472    control_pdu.add_property('opcode', control_pdu.opcode)
473    control_pdu.obo = (control_pdu.data[1] & 0x80) >> 7
474    control_pdu.add_property('obo', control_pdu.obo)
475    temp_12 = read_net_16(control_pdu.data[1:3])
476    control_pdu.seq_zero = (temp_12 >> 2) & 0x1fff
477    control_pdu.add_property('seq_zero', control_pdu.seq_zero)
478    control_pdu.block_ack = read_net_32(control_pdu.data[3:7])
479    control_pdu.add_property('block_ack', control_pdu.block_ack)
480
481    # try to add it to access message
482    inverse_direction = 'RX'
483    if control_pdu.direction == 'RX':
484        inverse_direcgtion = 'TX'
485    tag = segmented_message_tag(control_pdu.dst, inverse_direction)
486    if tag in segmented_messages:
487        seg_message = segmented_messages[tag]
488        seg_message.origins.append(control_pdu)
489    else:
490        log_pdu(control_pdu, 0, [])
491
492def mesh_process_access(access_pdu):
493    log_pdu(access_pdu, 0, [])
494
495def mesh_process_network_pdu_tx(network_pdu_encrypted):
496
497    # network layer - decrypt pdu
498    nid = network_pdu_encrypted.data[0] & 0x7f
499    for key in mesh_network_keys_for_nid(nid):
500        network_pdu_decrypted_data = network_decrypt(network_pdu_encrypted.data, as_big_endian32(ivi), key.encryption_key, key.privacy_key)
501        if network_pdu_decrypted_data != None:
502            break
503    if network_pdu_decrypted_data == None:
504        network_pdu_encrypted.summary = 'No encryption key found'
505        log_pdu(network_pdu_encrypted, 0, [])
506        return
507
508    # decrypted network pdu
509    network_pdu_decrypted = network_pdu(network_pdu_decrypted_data)
510    network_pdu_decrypted.direction = network_pdu_encrypted.direction
511    network_pdu_decrypted.add_property('dir', network_pdu_decrypted.direction)
512    network_pdu_decrypted.origins.append(network_pdu_encrypted)
513
514    # print("network pdu (enc)" + network_pdu_encrypted.data.hex())
515    # print("network pdu (dec)" + network_pdu_decrypted_data.hex())
516
517    # lower transport - reassemble
518    lower_transport = lower_transport_pdu(network_pdu_decrypted)
519    lower_transport.direction = network_pdu_decrypted.direction
520    lower_transport.add_property('dir', lower_transport.direction)
521    lower_transport.origins.append(network_pdu_decrypted)
522
523    if lower_transport.seg:
524        message = segmented_message_for_pdu(lower_transport)
525        message.add_segment(lower_transport)
526        if not message.complete():
527            return
528        if message.processed:
529            return
530
531        message.processed = True
532        if message.ctl:
533            mesh_process_control(message)
534        else:
535            access_payload = mesh_upper_transport_decrypt(message, message.data)
536            if access_payload == None:
537                message.summary = 'No encryption key found'
538                log_pdu(message, 0, [])
539            else:
540                access = access_pdu(message, access_payload)
541                access.direction = message.direction
542                access.origins.append(message)
543                mesh_process_access(access)
544
545    else:
546        # print("lower_transport.ctl = " + str(lower_transport.ctl))
547        if lower_transport.ctl:
548            control = layer_pdu('Unsegmented Control', lower_transport.data)
549            control.direction = lower_transport.direction
550            control.seq = lower_transport.seq
551            control.src = lower_transport.src
552            control.dst = lower_transport.dst
553            control.ctl = True
554            control.add_property('seq', lower_transport.seq)
555            control.add_property('src', lower_transport.src)
556            control.add_property('dst', lower_transport.dst)
557            control.origins.append(lower_transport)
558            mesh_process_control(control)
559        else:
560            access_payload = mesh_upper_transport_decrypt(lower_transport, lower_transport.upper_transport)
561            if access_payload == None:
562                lower_transport.summary = 'No encryption key found'
563                log_pdu(lower_transport, 0, [])
564            else:
565                access = access_pdu(lower_transport, access_payload)
566                access.add_property('seq_auth', lower_transport.seq)
567                access.direction = lower_transport.direction
568                access.origins.append(lower_transport)
569                mesh_process_access(access)
570
571
572def mesh_process_beacon_pdu(adv_pdu):
573    log_pdu(adv_pdu, 0, [])
574
575def mesh_process_adv(adv_pdu):
576    ad_len  = adv_pdu.data[0] - 1
577    ad_type = adv_pdu.data[1]
578    if ad_type == 0x2A:
579        network_pdu_encrypted = layer_pdu("Network(encrypted)", adv_data[2:2+ad_len])
580        network_pdu_encrypted.add_property('ivi', adv_data[2] >> 7)
581        network_pdu_encrypted.add_property('nid', adv_data[2] & 0x7f)
582        network_pdu_encrypted.add_property('dir', adv_pdu.direction)
583        network_pdu_encrypted.direction = adv_pdu.direction
584        network_pdu_encrypted.origins.append(adv_pdu)
585        mesh_process_network_pdu_tx(network_pdu_encrypted)
586    if ad_type == 0x2b:
587        beacon_pdu = layer_pdu("Beacon", adv_data[2:2+ad_len])
588        beacon_pdu.origins.append(adv_pdu)
589        mesh_process_beacon_pdu(beacon_pdu)
590
591
592if len(sys.argv) == 1:
593    print ('Dump Mesh PacketLogger file')
594    print ('Copyright 2019, BlueKitchen GmbH')
595    print ('')
596    print ('Usage: ' + sys.argv[0] + 'hci_dump.pklg')
597    exit(0)
598
599infile = sys.argv[1]
600
601with open (infile, 'rb') as fin:
602    pos = 0
603    while True:
604        payload_length  = read_net_32_from_file(fin)
605        if payload_length < 0:
606            break
607        ts_sec  = read_net_32_from_file(fin)
608        ts_usec = read_net_32_from_file(fin)
609        type    = ord(fin.read(1))
610        packet_len = payload_length - 9;
611        if (packet_len > 66000):
612            print ("Error parsing pklg at offset %u (%x)." % (pos, pos))
613            break
614
615        packet  = fin.read(packet_len)
616        pos     = pos + 4 + payload_length
617        # time    = "[%s.%03u] " % (datetime.datetime.fromtimestamp(ts_sec).strftime("%Y-%m-%d %H:%M:%S"), ts_usec / 1000)
618
619        if type == 0:
620            # CMD
621            if packet[0] != 0x08:
622                continue
623            if packet[1] != 0x20:
624                continue
625            adv_data = packet[4:]
626            adv_pdu = layer_pdu("ADV(TX)", adv_data)
627            adv_pdu.add_property('dir', 'TX')
628            adv_pdu.direction = 'TX'
629            mesh_process_adv(adv_pdu)
630
631        elif type == 1:
632            # EVT
633            event = packet[0]
634            if event != 0x3e:
635                continue
636            event_len = packet[1]
637            if event_len < 14:
638                continue
639            adv_data = packet[13:-1]
640            adv_pdu = layer_pdu("ADV(RX)", adv_data)
641            adv_pdu.add_property('dir', 'RX')
642            adv_pdu.direction = 'RX'
643            mesh_process_adv(adv_pdu)
644
645        elif type == 0xfc:
646            # LOG
647            log = packet.decode("utf-8")
648            parts = re.match('mesh-iv-index: (.*)', log)
649            if parts and len(parts.groups()) == 1:
650                mesh_set_iv_index(int(parts.groups()[0], 16))
651                continue
652            parts = re.match('mesh-devkey: (.*)', log)
653            if parts and len(parts.groups()) == 1:
654                mesh_set_device_key(bytes.fromhex(parts.groups()[0]))
655                continue
656            parts = re.match('mesh-appkey-(.*): (.*)', log)
657            if parts and len(parts.groups()) == 2:
658                mesh_add_application_key(int(parts.groups()[0], 16), bytes.fromhex(parts.groups()[1]))
659                continue
660            parts = re.match('mesh-netkey-(.*): (.*)', log)
661            if parts and len(parts.groups()) == 2:
662                mesh_add_netkey(int(parts.groups()[0], 16), bytes.fromhex(parts.groups()[1]))
663                continue
664
665