amrmodem.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import struct
  3. import usb.core
  4. import usb.util
  5. class AMRModem:
  6. USB_RT_CDC_SET_AUX_LINE_STATE = ((0x10 << 8) | 0x21)
  7. USB_RT_CDC_SET_HOOK_STATE = ((0x11 << 8) | 0x21)
  8. USB_RT_CDC_SET_COMM_FEATURE = ((0x02 << 8) | 0x21)
  9. USB_RT_CDC_GET_COMM_FEATURE = ((0x03 << 8) | 0xa1)
  10. USB_RT_CDC_CLEAR_COMM_FEATURE = ((0x04 << 8) | 0x21)
  11. ON_HOOK = 0
  12. OFF_HOOK = 1
  13. CALLED_ID = 2
  14. def __init__(self):
  15. # Locate device
  16. self.dev = usb.core.find(idVendor=0x1d50, idProduct=0x6175)
  17. if self.dev is None:
  18. raise ValueError('Device not found')
  19. def _ctrl(self, rt, wValue=0, data_or_wLength=None, timeout=None):
  20. return self.dev.ctrl_transfer(
  21. rt & 0xff,
  22. rt >> 8,
  23. wValue,
  24. 4,
  25. data_or_wLength,
  26. timeout
  27. )
  28. def set_aux_line_state(self, state):
  29. self._ctrl(self.USB_RT_CDC_SET_AUX_LINE_STATE, 1 if state else 0)
  30. def set_hook_state(self, state):
  31. self._ctrl(self.USB_RT_CDC_SET_HOOK_STATE, state)
  32. def set_country(self, cc):
  33. self._ctrl(self.USB_RT_CDC_SET_COMM_FEATURE, 2, struct.pack('<H', cc))
  34. def get_country(self):
  35. return struct.unpack('<H', self._ctrl(self.USB_RT_CDC_GET_COMM_FEATURE, 2, 2))[0]
  36. def clear_country(self):
  37. self._ctrl(self.USB_RT_CDC_CLEAR_COMM_FEATURE, 2)
  38. def read_notif(self, timeout=None):
  39. return self.read(0x83, 8, timeout=timeout)