|
34 | 34 |
|
35 | 35 | #define MAXRETRIES 4
|
36 | 36 |
|
| 37 | +#define START 0x55 |
| 38 | +#define ACK 0x09 |
| 39 | +#define NAK 0x30 |
| 40 | + |
| 41 | +#define ERR_INVALID_CMD 0x02 |
| 42 | +#define ERR_INVALID_LENGTH 0x03 |
| 43 | +#define ERR_INVALID_DATA 0x04 |
| 44 | +#define ERR_BATTERY_LOW 0x05 |
| 45 | +#define ERR_BUSY 0x06 |
| 46 | + |
37 | 47 | #define SZ_MAXCMD 8
|
38 | 48 | #define SZ_MAXRSP SZ_READ
|
39 | 49 |
|
@@ -106,7 +116,7 @@ seac_screen_send (seac_screen_device_t *device, unsigned short cmd, const unsign
|
106 | 116 | // Setup the data packet
|
107 | 117 | unsigned len = size + 6;
|
108 | 118 | unsigned char packet[SZ_MAXCMD + 7] = {
|
109 |
| - 0x55, |
| 119 | + START, |
110 | 120 | (len >> 8) & 0xFF,
|
111 | 121 | (len ) & 0xFF,
|
112 | 122 | (cmd >> 8) & 0xFF,
|
@@ -144,7 +154,7 @@ seac_screen_receive (seac_screen_device_t *device, unsigned short cmd, unsigned
|
144 | 154 | }
|
145 | 155 |
|
146 | 156 | // Verify the start byte.
|
147 |
| - if (packet[0] != 0x55) { |
| 157 | + if (packet[0] != START) { |
148 | 158 | ERROR (abstract->context, "Unexpected start byte (%02x).", packet[0]);
|
149 | 159 | return DC_STATUS_PROTOCOL;
|
150 | 160 | }
|
@@ -173,17 +183,32 @@ seac_screen_receive (seac_screen_device_t *device, unsigned short cmd, unsigned
|
173 | 183 |
|
174 | 184 | // Verify the command response.
|
175 | 185 | unsigned int rsp = array_uint16_be (packet + 3);
|
176 |
| - unsigned int misc = packet[1 + length - 3]; |
177 |
| - if (rsp != cmd || misc != 0x09) { |
178 |
| - ERROR (abstract->context, "Unexpected command response (%04x %02x).", rsp, misc); |
| 186 | + if (rsp != cmd) { |
| 187 | + ERROR (abstract->context, "Unexpected command response (%04x).", rsp); |
| 188 | + return DC_STATUS_PROTOCOL; |
| 189 | + } |
| 190 | + |
| 191 | + // Verify the ACK/NAK byte. |
| 192 | + unsigned int type = packet[1 + length - 3]; |
| 193 | + if (type != ACK && type != NAK) { |
| 194 | + ERROR (abstract->context, "Unexpected ACK/NAK byte (%02x).", type); |
179 | 195 | return DC_STATUS_PROTOCOL;
|
180 | 196 | }
|
181 | 197 |
|
182 |
| - if (length - 7 != size) { |
| 198 | + // Verify the length of the packet. |
| 199 | + unsigned int expected = (type == ACK ? size : 1) + 7; |
| 200 | + if (length != expected) { |
183 | 201 | ERROR (abstract->context, "Unexpected packet length (%u).", length);
|
184 | 202 | return DC_STATUS_PROTOCOL;
|
185 | 203 | }
|
186 | 204 |
|
| 205 | + // Get the error code from a NAK packet. |
| 206 | + if (type == NAK) { |
| 207 | + unsigned int errcode = packet[5]; |
| 208 | + ERROR (abstract->context, "Received NAK packet with error code %02x.", errcode); |
| 209 | + return DC_STATUS_PROTOCOL; |
| 210 | + } |
| 211 | + |
187 | 212 | memcpy (data, packet + 5, length - 7);
|
188 | 213 |
|
189 | 214 | return DC_STATUS_SUCCESS;
|
|
0 commit comments