2121
2222#include <string.h> // memcpy, memcmp
2323#include <stdlib.h> // malloc, free
24- #include <assert.h> // assert
2524
2625#include "mares_iconhd.h"
2726#include "context-private.h"
@@ -178,19 +177,21 @@ mares_iconhd_get_model (mares_iconhd_device_t *device)
178177
179178static dc_status_t
180179mares_iconhd_packet (mares_iconhd_device_t * device ,
181- const unsigned char command [], unsigned int csize ,
180+ unsigned char cmd ,
181+ const unsigned char data [], unsigned int size ,
182182 unsigned char answer [], unsigned int asize )
183183{
184184 dc_status_t status = DC_STATUS_SUCCESS ;
185185 dc_device_t * abstract = (dc_device_t * ) device ;
186186
187- assert (csize >= 2 );
188-
189187 if (device_is_cancelled (abstract ))
190188 return DC_STATUS_CANCELLED ;
191189
192190 // Send the command header to the dive computer.
193- status = dc_iostream_write (device -> iostream , command , 2 , NULL );
191+ const unsigned char command [2 ] = {
192+ cmd , cmd ^ XOR ,
193+ };
194+ status = dc_iostream_write (device -> iostream , command , sizeof (command ), NULL );
194195 if (status != DC_STATUS_SUCCESS ) {
195196 ERROR (abstract -> context , "Failed to send the command." );
196197 return status ;
@@ -211,8 +212,8 @@ mares_iconhd_packet (mares_iconhd_device_t *device,
211212 }
212213
213214 // Send the command payload to the dive computer.
214- if (csize > 2 ) {
215- status = dc_iostream_write (device -> iostream , command + 2 , csize - 2 , NULL );
215+ if (size ) {
216+ status = dc_iostream_write (device -> iostream , data , size , NULL );
216217 if (status != DC_STATUS_SUCCESS ) {
217218 ERROR (abstract -> context , "Failed to send the command." );
218219 return status ;
@@ -244,11 +245,11 @@ mares_iconhd_packet (mares_iconhd_device_t *device,
244245}
245246
246247static dc_status_t
247- mares_iconhd_transfer (mares_iconhd_device_t * device , const unsigned char command [], unsigned int csize , unsigned char answer [], unsigned int asize )
248+ mares_iconhd_transfer (mares_iconhd_device_t * device , unsigned char cmd , const unsigned char data [], unsigned int size , unsigned char answer [], unsigned int asize )
248249{
249250 unsigned int nretries = 0 ;
250251 dc_status_t rc = DC_STATUS_SUCCESS ;
251- while ((rc = mares_iconhd_packet (device , command , csize , answer , asize )) != DC_STATUS_SUCCESS ) {
252+ while ((rc = mares_iconhd_packet (device , cmd , data , size , answer , asize )) != DC_STATUS_SUCCESS ) {
252253 // Automatically discard a corrupted packet,
253254 // and request a new one.
254255 if (rc != DC_STATUS_PROTOCOL && rc != DC_STATUS_TIMEOUT )
@@ -283,23 +284,21 @@ mares_iconhd_read_object(mares_iconhd_device_t *device, dc_event_progress_t *pro
283284
284285 // Transfer the init packet.
285286 unsigned char rsp_init [16 ];
286- unsigned char cmd_init [18 ] = {
287- CMD_OBJ_INIT ,
288- CMD_OBJ_INIT ^ XOR ,
287+ unsigned char cmd_init [16 ] = {
289288 0x40 ,
290289 (index >> 0 ) & 0xFF ,
291290 (index >> 8 ) & 0xFF ,
292291 subindex & 0xFF
293292 };
294293 memset (cmd_init + 6 , 0x00 , sizeof (cmd_init ) - 6 );
295- status = mares_iconhd_transfer (device , cmd_init , sizeof (cmd_init ), rsp_init , sizeof (rsp_init ));
294+ status = mares_iconhd_transfer (device , CMD_OBJ_INIT , cmd_init , sizeof (cmd_init ), rsp_init , sizeof (rsp_init ));
296295 if (status != DC_STATUS_SUCCESS ) {
297296 ERROR (abstract -> context , "Failed to transfer the init packet." );
298297 return status ;
299298 }
300299
301300 // Verify the packet header.
302- if (memcmp (cmd_init + 3 , rsp_init + 1 , 3 ) != 0 ) {
301+ if (memcmp (cmd_init + 1 , rsp_init + 1 , 3 ) != 0 ) {
303302 ERROR (abstract -> context , "Unexpected packet header." );
304303 return DC_STATUS_PROTOCOL ;
305304 }
@@ -347,8 +346,7 @@ mares_iconhd_read_object(mares_iconhd_device_t *device, dc_event_progress_t *pro
347346
348347 // Transfer the segment packet.
349348 unsigned char rsp_segment [1 + 504 ];
350- unsigned char cmd_segment [] = {cmd , cmd ^ XOR };
351- status = mares_iconhd_transfer (device , cmd_segment , sizeof (cmd_segment ), rsp_segment , len + 1 );
349+ status = mares_iconhd_transfer (device , cmd , NULL , 0 , rsp_segment , len + 1 );
352350 if (status != DC_STATUS_SUCCESS ) {
353351 ERROR (abstract -> context , "Failed to transfer the segment packet." );
354352 return status ;
@@ -447,8 +445,7 @@ mares_iconhd_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_
447445 dc_iostream_purge (device -> iostream , DC_DIRECTION_ALL );
448446
449447 // Send the version command.
450- unsigned char command [] = {CMD_VERSION , CMD_VERSION ^ XOR };
451- status = mares_iconhd_transfer (device , command , sizeof (command ),
448+ status = mares_iconhd_transfer (device , CMD_VERSION , NULL , 0 ,
452449 device -> version , sizeof (device -> version ));
453450 if (status != DC_STATUS_SUCCESS ) {
454451 goto error_free_iostream ;
@@ -460,9 +457,8 @@ mares_iconhd_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_
460457 // Read the size of the flash memory.
461458 unsigned int memsize = 0 ;
462459 if (device -> model == QUAD ) {
463- unsigned char cmd_flash [] = {CMD_FLASHSIZE , CMD_FLASHSIZE ^ XOR };
464460 unsigned char rsp_flash [4 ] = {0 };
465- status = mares_iconhd_transfer (device , cmd_flash , sizeof ( cmd_flash ) , rsp_flash , sizeof (rsp_flash ));
461+ status = mares_iconhd_transfer (device , CMD_FLASHSIZE , NULL , 0 , rsp_flash , sizeof (rsp_flash ));
466462 if (status != DC_STATUS_SUCCESS ) {
467463 WARNING (context , "Failed to read the flash memory size." );
468464 } else {
@@ -575,7 +571,7 @@ mares_iconhd_device_read (dc_device_t *abstract, unsigned int address, unsigned
575571 len = device -> packetsize ;
576572
577573 // Read the packet.
578- unsigned char command [] = {CMD_READ , CMD_READ ^ XOR ,
574+ unsigned char command [] = {
579575 (address ) & 0xFF ,
580576 (address >> 8 ) & 0xFF ,
581577 (address >> 16 ) & 0xFF ,
@@ -584,7 +580,7 @@ mares_iconhd_device_read (dc_device_t *abstract, unsigned int address, unsigned
584580 (len >> 8 ) & 0xFF ,
585581 (len >> 16 ) & 0xFF ,
586582 (len >> 24 ) & 0xFF };
587- rc = mares_iconhd_transfer (device , command , sizeof (command ), data , len );
583+ rc = mares_iconhd_transfer (device , CMD_READ , command , sizeof (command ), data , len );
588584 if (rc != DC_STATUS_SUCCESS )
589585 return rc ;
590586
0 commit comments