示例#1
0
def ecc_encode(msg, pad = False):

    """ encode message 'msg' which must be of length MAX_LENGTH:
          
          1. pad msg if 'pad' is True
    
          2. Map each 4bit word of 'msg' to one of the base vectors.

          3. Transpose and repeat message twice.

        The encoding attempts to map nicely to current physical low
        power devices which generally support packet handling and
        where packet lengths are generally limited to 255 bytes (TI:
        CC1101 and CC1120 etc, Freescale RF MCU, HopeRF RFM69W,
        Silicon Labs RF MCU etc). 

    """

    if pad:
        msg = pad(msg)

    length = MAX_LENGTH
    assert len(msg) == length

    div = length / 8

    enc_msg_high = []
    enc_msg_low = []
    for c in msg:
        enc_msg_high.append(BASE_VECTORS[c >> 4])
        enc_msg_low.append(BASE_VECTORS[c % 16])

    ## transpose
    tr_msg_high = bit_transpose(enc_msg_high)
    tr_msg_low = bit_transpose(enc_msg_low)

    ## duplicate
    out = []
    for i in range(8 * 2):
        _from = i * div
        _to = (i+1) * div
        out.extend(tr_msg_high[_from:_to])
        out.extend(tr_msg_high[_from:_to])
        out.extend(tr_msg_low[_from:_to])
        out.extend(tr_msg_low[_from:_to])
    
    return s_to_a(out)
示例#2
0
def ecc_encode(msg, pad=False):
    """ encode message 'msg' which must be of length MAX_LENGTH:
          
          1. pad msg if 'pad' is True
    
          2. Map each 4bit word of 'msg' to one of the base vectors.

          3. Transpose and repeat message twice.

        The encoding attempts to map nicely to current physical low
        power devices which generally support packet handling and
        where packet lengths are generally limited to 255 bytes (TI:
        CC1101 and CC1120 etc, Freescale RF MCU, HopeRF RFM69W,
        Silicon Labs RF MCU etc). 

    """

    if pad:
        msg = pad(msg)

    length = MAX_LENGTH
    assert len(msg) == length

    div = length / 8

    enc_msg_high = []
    enc_msg_low = []
    for c in msg:
        enc_msg_high.append(BASE_VECTORS[c >> 4])
        enc_msg_low.append(BASE_VECTORS[c % 16])

    ## transpose
    tr_msg_high = bit_transpose(enc_msg_high)
    tr_msg_low = bit_transpose(enc_msg_low)

    ## duplicate
    out = []
    for i in range(8 * 2):
        _from = i * div
        _to = (i + 1) * div
        out.extend(tr_msg_high[_from:_to])
        out.extend(tr_msg_high[_from:_to])
        out.extend(tr_msg_low[_from:_to])
        out.extend(tr_msg_low[_from:_to])

    return s_to_a(out)
示例#3
0
def test():
    import random

    msg = pad(s_to_a('Hello QuietCasting'))
    assert msg[0] == 18
    assert len(msg) == MAX_LENGTH

    tr_msg = bit_transpose(msg)
    assert reverse_bit_transpose(tr_msg) == msg

    print 'Encoding ...'
    enc = ecc_encode(msg)
    print enc

    print 'Corrupting message (random) ...'
    rec = []
    for c in enc:
        e = 0
        for i in range(8):
            r = random.randint(0, 999)
            if r < 5:
                e |= 1
            e <<= 1
        rec.append(c ^ e)

    total = 0
    for (i, j) in zip(rec, enc):
        if i != j:
            total += 1
    print total, 'corrupted characters detected ...'
    print rec

    print 'Decoding ...'
    dec = ecc_decode(rec)
    print dec
    if dec[1] == msg:
        print 'Decoded succesfully!'
    else:
        print 'Errors detected'
        print dec
        print msg

    print 'Corrupting message (burst) ...'
    rec = array.array('B', [c ^ 255 for c in enc[:24]]) + enc[24:]

    total = 0
    for (i, j) in zip(rec, enc):
        if i != j:
            total += 1
    print total, 'corrupted characters detected ...'
    print rec

    print 'Decoding ...'
    dec = ecc_decode(rec)
    print dec
    if dec[1] == msg:
        print 'Decoded succesfully!'
    else:
        print 'Errors detected'
        print dec
        print msg
示例#4
0
def test():
    import random

    msg = pad(s_to_a('Hello QuietCasting'))
    assert msg[0] == 18
    assert len(msg) == MAX_LENGTH

    tr_msg = bit_transpose(msg)
    assert reverse_bit_transpose(tr_msg) == msg

    print 'Encoding ...'
    enc = ecc_encode(msg)
    print enc

    print 'Corrupting message (random) ...'
    rec = []
    for c in enc:
        e = 0
        for i in range(8): 
            r = random.randint(0,999)
            if r < 5:
                e |= 1
            e <<= 1
        rec.append(c ^ e)
    
    total = 0
    for (i,j) in zip(rec, enc):
        if i!=j:
            total += 1
    print total, 'corrupted characters detected ...'
    print rec

    print 'Decoding ...'
    dec = ecc_decode(rec)
    print dec
    if dec[1] == msg:
        print 'Decoded succesfully!'
    else:
        print 'Errors detected'
        print dec
        print msg



    print 'Corrupting message (burst) ...'
    rec = array.array('B', [c^255 for c in enc[:24]]) + enc[24:]
    
    total = 0
    for (i,j) in zip(rec, enc):
        if i!=j:
            total += 1
    print total, 'corrupted characters detected ...'
    print rec

    print 'Decoding ...'
    dec = ecc_decode(rec)
    print dec
    if dec[1] == msg:
        print 'Decoded succesfully!'
    else:
        print 'Errors detected'
        print dec
        print msg