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.
 
 
 
 
 
 

60 lines
1.4 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
  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):
  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. #print('mp:', repr(pb.pkt))
  20. else:
  21. obj = (c_uint8 * len(s))(*s)
  22. pb.pkt = obj
  23. pb.pktlen = len(s)
  24. pb._make_pktbuf_ref = (obj, s)
  25. return pb
  26. process_msgfunc_t = CFUNCTYPE(None, PktBuf, POINTER(PktBuf))
  27. _lib = CDLL('liblora_test.dylib')
  28. _lib._strobe_state_size.restype = c_size_t
  29. _lib._strobe_state_size.argtypes = ()
  30. _strobe_state_u64_cnt = (_lib._strobe_state_size() + 7) // 8
  31. class CommsState(Structure):
  32. _fields_ = [
  33. # The alignment of these may be off
  34. ('cs_state', c_uint64 * _strobe_state_u64_cnt),
  35. ('cs_start', c_uint64 * _strobe_state_u64_cnt),
  36. ('cs_procmsg', process_msgfunc_t),
  37. ]
  38. for func, ret, args in [
  39. ('comms_init', None, (POINTER(CommsState), process_msgfunc_t)),
  40. ('comms_process', None, (POINTER(CommsState), PktBuf, POINTER(PktBuf))),
  41. ('strobe_seed_prng', None, (POINTER(c_uint8), c_ssize_t)),
  42. ]:
  43. f = getattr(_lib, func)
  44. f.restype = ret
  45. f.argtypes = args
  46. locals()[func] = f