<commit_before>from __future__ import print_function
from past.builtins import xrange
from builtins import input, bytes
import six

import os
import sys
import cmd
import time
import serial
import select
import struct
import threading
import math
import pickle
import binascii

from cancat import iso_tp

baud = 4000000


# command constants (used to identify messages between
# python client and the CanCat transceiver
CMD_LOG                     = 0x2f
CMD_LOG_HEX                 = 0x2e

CMD_CAN_RECV                = 0x30
CMD_PING_RESPONSE           = 0x31
CMD_CHANGE_BAUD_RESULT      = 0x32
CMD_CAN_BAUD_RESULT         = 0x33
CMD_CAN_SEND_RESULT         = 0x34
CMD_ISO_RECV                = 0x35
CMD_SET_FILT_MASK           = 0x36
CMD_CAN_MODE_RESULT         = 0x37
CMD_CAN_SEND_ISOTP_RESULT   = 0x38
CMD_CAN_RECV_ISOTP_RESULT   = 0x39
CMD_CAN_SENDRECV_ISOTP_RESULT = 0x3A
CMD_SET_FILT_MASK_RESULT    = 0x3B
CMD_PRINT_CAN_REGS          = 0x3C

CMD_PING                    = 0x41
CMD_CHANGE_BAUD             = 0x42
CMD_CAN_BAUD                = 0x43
CMD_CAN_SEND                = 0x44
CMD_CAN_MODE                = 0x45
CMD_CAN_MODE_SNIFF_CAN0     = 0x00 # Start sniffing on can 0
CMD_CAN_MODE_SNIFF_CAN1     = 0x01 # Start sniffing on can 1
CMD_CAN_MODE_CITM           = 0x02 # Start CITM between can1 and can2
CMD_CAN_SEND_ISOTP          = 0x46
CMD_CAN_RECV_ISOTP          = 0x47
CMD_CAN_SENDRECV_ISOTP      = 0x48


CAN_RESP_OK                 = (0)
CAN_RESP_FAILINIT           = (1)
CAN_RESP_FAILTX             = (2)
CAN_RESP_MSGAVAIL           = (3)
CAN_RESP_NOMSG              = (4)
CAN_RESP_CTRLERROR          = (5)
CAN_RESP_GETTXBFTIMEOUT     = (6)
CAN_RESP_SENDMSGTIMEOUT     = (7)
CAN_RESP_FAIL               = (0xff)

CAN_RESPS = { v: k for k,v in globals().items() if k.startswith('CAN_RESP_') }

# constants for setting baudrate for the CAN bus
CAN_AUTOBPS  = 0
CAN_5KBPS    = 1
CAN_10KBPS   = 2
CAN_20KBPS   = 3
CAN_25KBPS   = 4
CAN_31K25BPS = 5
CAN_33KBPS   = 6
CAN_40KBPS   = 7
CAN_50KBPS   = 8
CAN_80KBPS   = 9
CAN_83K3BPS  = 10
CAN_95KBPS   = 11
CAN_100KBPS  = 12
CAN_125KBPS  = 13
CAN_200KBPS  = 14
CAN_250KBPS  = 15
CAN_500KBPS  = 16
CAN_666KBPS  = 17
CAN_1000KBPS = 18

# Flag to indicate STD (11-bit) or EXT (29-bit) messages
ARBID_11BIT = 0
ARBID_29BIT = 1

# state constants for the Receiver thread
RXTX_DISCONN    = -1
RXTX_SYNC       = 0
RXTX_GO         = 1

# constants for CANreplay mode
TIMING_FAST         = 0
TIMING_REAL         = 1
TIMING_INTERACTIVE  = 2
TIMING_SEARCH       = 3

# constants for VIEW settings:
VIEW_ASCII =        1<<0
VIEW_COMPARE =      1<<1
VIEW_BOOKMARKS =    1<<2
VIEW_TS_DELTA =     1<<3
VIEW_ENDSUM =       1<<4
VIEW_ALL = VIEW_ASCII | VIEW_COMPARE | VIEW_BOOKMARKS | VIEW_TS_DELTA | VIEW_ENDSUM

# message id's and metadata (soon to be moved into modules)
GM_messages = {
        }

Ford_messages = {
        }

Chrysler_messages = {
        }

Toyota_messages = {
        }

Honda_messages = {
        }

VW_messages = {
        }

Nissan_messages = {
        }

Mitsubishi_messages = {
        }

Hyundai_messages = {
        }

Kia_messages = {
        }

Suzuki_messages = {
        }

Harley_messages = {
        }

# helper functions for printing log messages from the CanCat Transceiver
def handleLogToScreen(message, canbuf):
    print('LOG: %s' % repr(message))

def handleLogHexToScreen(message, canbuf):
    num = struct.unpack("<L", message)
    print('LOG: %x' % num)

def handleCanMsgsDuringSniff(message, canbuf, arbids=None):
    ts = time.time()
    idx = canbuf._submitMessage(CMD_CAN_RECV, (ts, message))
    arbid, data = canbuf._splitCanMsg(message)

    if arbids:
        if arbid in arbids:
            print(reprCanMsg(idx, ts, arbid, data))
    else:
        print(reprCanMsg(idx, ts, arbid, data))

default_cmdhandlers = {
        CMD_LOG : handleLogToScreen,
        CMD_LOG_HEX: handleLogHexToScreen,
        }

def loadCanBuffer(filename):
    return pickle.load(open(filename))

def keystop(delay=0):
    if os.name == 'posix':
        return len(select.select([sys.stdin],[],[],delay)[0])
    else:
        return msvcrt.kbhit()

class SPECIAL_CASE(object):
    pass
DONT_PRINT_THIS_MESSAGE = SPECIAL_CASE

class CanInterface(object):
    _msg_source_idx = CMD_CAN_RECV
    def __init__(self, port=None, baud=baud, verbose=False, cmdhandlers=None, comment='', load_filename=None, orig_iface=None, max_msgs=None):
        '''
        CAN Analysis Workspace
        This can be subclassed by vendor to allow more vendor-specific code
        based on the way each vendor uses the varios Buses
        '''
        if orig_iface != None:
            self._consumeInterface(orig_iface)
            return<commit_msg>msvcrt bugfix<commit_after>from __future__ import print_function
from past.builtins import xrange
from builtins import input, bytes
import six

import os
import sys
import cmd
import time
import serial
import select
import struct
import threading
import math
import pickle
import binascii

from cancat import iso_tp

baud = 4000000


# command constants (used to identify messages between
# python client and the CanCat transceiver
CMD_LOG                     = 0x2f
CMD_LOG_HEX                 = 0x2e

CMD_CAN_RECV                = 0x30
CMD_PING_RESPONSE           = 0x31
CMD_CHANGE_BAUD_RESULT      = 0x32
CMD_CAN_BAUD_RESULT         = 0x33
CMD_CAN_SEND_RESULT         = 0x34
CMD_ISO_RECV                = 0x35
CMD_SET_FILT_MASK           = 0x36
CMD_CAN_MODE_RESULT         = 0x37
CMD_CAN_SEND_ISOTP_RESULT   = 0x38
CMD_CAN_RECV_ISOTP_RESULT   = 0x39
CMD_CAN_SENDRECV_ISOTP_RESULT = 0x3A
CMD_SET_FILT_MASK_RESULT    = 0x3B
CMD_PRINT_CAN_REGS          = 0x3C

CMD_PING                    = 0x41
CMD_CHANGE_BAUD             = 0x42
CMD_CAN_BAUD                = 0x43
CMD_CAN_SEND                = 0x44
CMD_CAN_MODE                = 0x45
CMD_CAN_MODE_SNIFF_CAN0     = 0x00 # Start sniffing on can 0
CMD_CAN_MODE_SNIFF_CAN1     = 0x01 # Start sniffing on can 1
CMD_CAN_MODE_CITM           = 0x02 # Start CITM between can1 and can2
CMD_CAN_SEND_ISOTP          = 0x46
CMD_CAN_RECV_ISOTP          = 0x47
CMD_CAN_SENDRECV_ISOTP      = 0x48


CAN_RESP_OK                 = (0)
CAN_RESP_FAILINIT           = (1)
CAN_RESP_FAILTX             = (2)
CAN_RESP_MSGAVAIL           = (3)
CAN_RESP_NOMSG              = (4)
CAN_RESP_CTRLERROR          = (5)
CAN_RESP_GETTXBFTIMEOUT     = (6)
CAN_RESP_SENDMSGTIMEOUT     = (7)
CAN_RESP_FAIL               = (0xff)

CAN_RESPS = { v: k for k,v in globals().items() if k.startswith('CAN_RESP_') }

# constants for setting baudrate for the CAN bus
CAN_AUTOBPS  = 0
CAN_5KBPS    = 1
CAN_10KBPS   = 2
CAN_20KBPS   = 3
CAN_25KBPS   = 4
CAN_31K25BPS = 5
CAN_33KBPS   = 6
CAN_40KBPS   = 7
CAN_50KBPS   = 8
CAN_80KBPS   = 9
CAN_83K3BPS  = 10
CAN_95KBPS   = 11
CAN_100KBPS  = 12
CAN_125KBPS  = 13
CAN_200KBPS  = 14
CAN_250KBPS  = 15
CAN_500KBPS  = 16
CAN_666KBPS  = 17
CAN_1000KBPS = 18

# Flag to indicate STD (11-bit) or EXT (29-bit) messages
ARBID_11BIT = 0
ARBID_29BIT = 1

# state constants for the Receiver thread
RXTX_DISCONN    = -1
RXTX_SYNC       = 0
RXTX_GO         = 1

# constants for CANreplay mode
TIMING_FAST         = 0
TIMING_REAL         = 1
TIMING_INTERACTIVE  = 2
TIMING_SEARCH       = 3

# constants for VIEW settings:
VIEW_ASCII =        1<<0
VIEW_COMPARE =      1<<1
VIEW_BOOKMARKS =    1<<2
VIEW_TS_DELTA =     1<<3
VIEW_ENDSUM =       1<<4
VIEW_ALL = VIEW_ASCII | VIEW_COMPARE | VIEW_BOOKMARKS | VIEW_TS_DELTA | VIEW_ENDSUM

# message id's and metadata (soon to be moved into modules)
GM_messages = {
        }

Ford_messages = {
        }

Chrysler_messages = {
        }

Toyota_messages = {
        }

Honda_messages = {
        }

VW_messages = {
        }

Nissan_messages = {
        }

Mitsubishi_messages = {
        }

Hyundai_messages = {
        }

Kia_messages = {
        }

Suzuki_messages = {
        }

Harley_messages = {
        }

# helper functions for printing log messages from the CanCat Transceiver
def handleLogToScreen(message, canbuf):
    print('LOG: %s' % repr(message))

def handleLogHexToScreen(message, canbuf):
    num = struct.unpack("<L", message)
    print('LOG: %x' % num)

def handleCanMsgsDuringSniff(message, canbuf, arbids=None):
    ts = time.time()
    idx = canbuf._submitMessage(CMD_CAN_RECV, (ts, message))
    arbid, data = canbuf._splitCanMsg(message)

    if arbids:
        if arbid in arbids:
            print(reprCanMsg(idx, ts, arbid, data))
    else:
        print(reprCanMsg(idx, ts, arbid, data))

default_cmdhandlers = {
        CMD_LOG : handleLogToScreen,
        CMD_LOG_HEX: handleLogHexToScreen,
        }

def loadCanBuffer(filename):
    return pickle.load(open(filename))

def keystop(delay=0):
    if os.name == 'posix':
        return len(select.select([sys.stdin],[],[],delay)[0])
    else:
        import msvcrt
        return msvcrt.kbhit()

class SPECIAL_CASE(object):
    pass
DONT_PRINT_THIS_MESSAGE = SPECIAL_CASE

class CanInterface(object):
    _msg_source_idx = CMD_CAN_RECV
    def __init__(self, port=None, baud=baud, verbose=False, cmdhandlers=None, comment='', load_filename=None, orig_iface=None, max_msgs=None):
        '''
        CAN Analysis Workspace
        This can be subclassed by vendor to allow more vendor-specific code
        based on the way each vendor uses the varios Buses
        '''
        if orig_iface != None:
            self._consumeInterface(orig_iface)
            return
{'fraction_non_alphanumeric': 0.07251908396946564, 'fraction_numerical': 0.04198473282442748, 'mean_word_length': 2.5758150113722516, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1023629', 'n_tokens_mistral': 3778, 'n_tokens_neox': 3459, 'n_words': 794}