Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commander core update #501

Merged
merged 3 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions docs/developer/protocol/commander_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,34 @@ Response:
Note: the `0x01` Init/Wakeup command is exceptionally not necessary before this
command.

### `0x05` - Reset Channel
### `0x0d` - Open Endpoint

Needs to be run before changing the mode on a channel if there is a chance the
channel has already been used.
Sets the mode for the channel to use.
Needs to be run **before** a read or write operation.

`0x05` - Init/Wakeup will likely need to be run first.

Command:

| Byte index | Description |
| ---------- | ----------- |
| 0x00 | 0x08 |
| 0x01 | 0x05 |
| 0x02 | 0x01 |
| 0x03 | Channel to reset |

### `0x0d` - Set Channel Mode
| 0x01 | 0x0d |
| 0x02 | Channel |
| 0x03 | New mode |

Sets the mode for the channel to use.
### `0x05` - Close Endpoint

`0x05` - Init/Wakeup will likely need to be run first.
Needs to be run **after** a read or write operation to close a previously opened endpoint.

Command:

| Byte index | Description |
| ---------- | ----------- |
| 0x00 | 0x08 |
| 0x01 | 0x0d |
| 0x02 | Channel |
| 0x03 | New mode |
| 0x01 | 0x05 |
| 0x02 | 0x01 |
| 0x03 | Channel to close |

## Mode Commands

Expand Down
18 changes: 10 additions & 8 deletions liquidctl/driver/commander_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
_CMD_WAKE = (0x01, 0x03, 0x00, 0x02)
_CMD_SLEEP = (0x01, 0x03, 0x00, 0x01)
_CMD_GET_FIRMWARE = (0x02, 0x13)
_CMD_RESET = (0x05, 0x01, 0x00)
_CMD_SET_MODE = (0x0d, 0x00)
_CMD_CLOSE_ENDPOINT = (0x05, 0x01, 0x00)
_CMD_OPEN_ENDPOINT = (0x0d, 0x00)
_CMD_READ = (0x08, 0x00)
_CMD_WRITE = (0x06, 0x00)

Expand Down Expand Up @@ -199,10 +199,9 @@ def _get_temps(self):
return temps

def _read_data(self, mode, data_type):
self._send_command(_CMD_RESET)
self._send_command(_CMD_SET_MODE, mode)
self._send_command(_CMD_OPEN_ENDPOINT, mode)
raw_data = self._send_command(_CMD_READ)

self._send_command(_CMD_CLOSE_ENDPOINT)
if tuple(raw_data[3:5]) != data_type:
raise ExpectationNotMet('device returned incorrect data type')

Expand All @@ -227,7 +226,10 @@ def _send_command(self, command, data=()):
self.device.clear_enqueued_reports()
self.device.write(buf)

buf = bytes(self.device.read(_RESPONSE_LENGTH))
res = self.device.read(_RESPONSE_LENGTH)
while res[0] != 0x00:
res = self.device.read(_RESPONSE_LENGTH)
buf = bytes(res)
assert buf[1] == command[0], 'response does not match command'
return buf

Expand All @@ -242,15 +244,15 @@ def _wake_device_context(self):
def _write_data(self, mode, data_type, data):
self._read_data(mode, data_type) # Will ensure we are writing the correct data type to avoid breakage

self._send_command(_CMD_RESET)
self._send_command(_CMD_SET_MODE, mode)
self._send_command(_CMD_OPEN_ENDPOINT, mode)

buf = bytearray(len(data) + len(data_type) + 4)
buf[0: 2] = int.to_bytes(len(data) + 2, length=2, byteorder="little", signed=False)
buf[4: 4 + len(data_type)] = data_type
buf[4 + len(data_type):] = data

self._send_command(_CMD_WRITE, buf)
self._send_command(_CMD_CLOSE_ENDPOINT)

def _fan_to_channel(self, fan):
if self._has_pump:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_commander_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __init__(self):

self._last_write = bytes()
self._modes = {}

self._awake = False

self.response_prefix = ()
Expand All @@ -52,7 +51,7 @@ def read(self, length):
if self._awake:
if self._last_write[2] == 0x08: # Get data
channel = self._last_write[3]
mode = self._modes[channel]
mode = self._modes.get(channel)
if mode[1] == 0x00:
if mode[0] == 0x17: # Get speeds
data.extend([0x06, 0x00])
Expand Down Expand Up @@ -111,19 +110,20 @@ def write(self, data):
self._last_write = data
if data[0] != 0x00 or data[1] != 0x08:
raise ValueError('Start of packets going out should be 00:08')

if data[2] == 0x0d:
channel = data[3]
if self._modes[channel] is None:
if self._modes.get(channel) is None:
self._modes[channel] = data[4:6]
else:
raise ExpectationNotMet('Previous channel was not reset')
elif data[2] == 0x05 and data[3] == 0x01:
self._modes[data[4]] = None
elif data[2] == 0x01 and data[3] == 0x03 and data[4] == 0x00:
self._awake = data[5] == 0x02
elif self._awake:
if data[2] == 0x06: # Write command
channel = data[3]
mode = self._modes[channel]
mode = self._modes.get(channel)
length = u16le_from(data[4:6])
data_type = data[8:10]
written_data = data[10:8+length]
Expand Down