Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
1.9 KiB

  1. from encdec8b10b import EncDec8B10B
  2. a = b'Hello, this is a test of encoding.'
  3. coder = EncDec8B10B()
  4. encoded = coder.encode(EncDec8B10B.COMMA) + coder.encode(a)
  5. print('8b10b encoded:', repr(encoded))
  6. decoder = EncDec8B10B()
  7. print(repr(decoder.decode(encoded)))
  8. print('test decode:', repr(decoder.decode('')))
  9. def jm3coder(bits, lastlvl=0):
  10. '''Takes in a string of bits, and outputs a trinary level.
  11. It is guaranteed that no two outputs repeat allowing for
  12. easy clock recovery as long as the sampling rate is faster
  13. than output rate. (e.g. 30Hz (33.3ms) sampling of a 29Hz
  14. (34.5ms) signal.
  15. Note that this does not do sync, so something like an 8b10b
  16. encoder should be used on top of this, and then transmit three
  17. dummy bits at the begining as the decoder. The three will
  18. cycle through all three states help ensuring level detection
  19. An optional paramter of lastlvl can be provided, which is
  20. the previous signal level transmitted.'''
  21. r = []
  22. for i in bits:
  23. v = int(i, 2)
  24. lastlvl = (lastlvl + 1 + v) % 3
  25. r.append('%d' % lastlvl)
  26. return ''.join(r)
  27. encoded = jm3coder('000' + encoded)
  28. print('jm3coded:', repr(encoded))
  29. # make a "stretched" bit string
  30. stretched = ''.join(encoded[i:i + 50] + encoded[i:i + 50][-1] for i in range(0, len(encoded), 50))
  31. print('stretched:', repr(stretched))
  32. def jm3decoder(trits, lastlvl=0):
  33. '''Decodes a string encoded w/ jm3coder.
  34. lastlvl should/must be provided which is the last rx'd level
  35. (it must be 0, 1 or 2).
  36. '''
  37. lookup = {
  38. (0, 1): 0,
  39. (0, 2): 1,
  40. (1, 2): 0,
  41. (1, 0): 1,
  42. (2, 0): 0,
  43. (2, 1): 1,
  44. }
  45. r = []
  46. for i in trits:
  47. lvl = int(i, 3)
  48. if lvl == lastlvl:
  49. continue
  50. r.append('%d' % lookup[(lastlvl, lvl)])
  51. #r.append('%d' % ((lvl - lastlvl + 2) % 3))
  52. lastlvl = lvl
  53. return ''.join(r)
  54. decoder = EncDec8B10B()
  55. stretched = jm3decoder(stretched)
  56. print(repr(stretched))
  57. print(repr(decoder.decode(stretched)))
  58. print(repr(decoder.decode('')))
  59. print('done')