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.
 
 
 
 
 
 

66 lines
1.6 KiB

  1. from ctypes import Structure, POINTER, CFUNCTYPE, pointer
  2. from ctypes import c_uint8, c_uint16, c_ssize_t, c_size_t, c_uint64, c_int
  3. from ctypes import CDLL
  4. class PktBuf(Structure):
  5. _fields_ = [
  6. ('pkt', POINTER(c_uint8)),
  7. ('pktlen', c_uint16),
  8. ]
  9. def _from(self):
  10. return bytes(self.pkt[:self.pktlen])
  11. def __repr__(self): #pragma: no cover
  12. return 'PktBuf(pkt=%s, pktlen=%s)' % (repr(self._from()),
  13. self.pktlen)
  14. def make_pktbuf(s):
  15. pb = PktBuf()
  16. if isinstance(s, bytearray):
  17. obj = s
  18. pb.pkt = pointer(c_uint8.from_buffer(s))
  19. else:
  20. obj = (c_uint8 * len(s))(*s)
  21. pb.pkt = obj
  22. pb.pktlen = len(s)
  23. pb._make_pktbuf_ref = (obj, s)
  24. return pb
  25. process_msgfunc_t = CFUNCTYPE(None, PktBuf, POINTER(PktBuf))
  26. _lib = CDLL('liblora_test.dylib')
  27. _lib._strobe_state_size.restype = c_size_t
  28. _lib._strobe_state_size.argtypes = ()
  29. _strobe_state_u64_cnt = (_lib._strobe_state_size() + 7) // 8
  30. class CommsState(Structure):
  31. _fields_ = [
  32. # The alignment of these may be off
  33. ('cs_state', c_uint64 * _strobe_state_u64_cnt),
  34. ('cs_comm_state', c_int),
  35. ('cs_start', c_uint64 * _strobe_state_u64_cnt),
  36. ('cs_procmsg', process_msgfunc_t),
  37. ('cs_prevmsg', PktBuf),
  38. ('cs_prevmsgresp', PktBuf),
  39. ('cs_prevmsgbuf', c_uint8 * 64),
  40. ('cs_prevmsgrespbuf', c_uint8 * 64),
  41. ]
  42. for func, ret, args in [
  43. ('comms_init', None, (POINTER(CommsState), process_msgfunc_t, POINTER(PktBuf))),
  44. ('comms_process', None, (POINTER(CommsState), PktBuf, POINTER(PktBuf))),
  45. ('strobe_seed_prng', None, (POINTER(c_uint8), c_ssize_t)),
  46. ]:
  47. f = getattr(_lib, func)
  48. f.restype = ret
  49. f.argtypes = args
  50. locals()[func] = f