suunto_eon.c (7522B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2008 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <string.h> // memcmp, memcpy 23 #include <stdlib.h> // malloc, free 24 25 #include "suunto_eon.h" 26 #include "suunto_common.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "checksum.h" 30 #include "array.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), &suunto_eon_device_vtable) 33 34 #define SZ_MEMORY 0x900 35 36 typedef struct suunto_eon_device_t { 37 suunto_common_device_t base; 38 dc_iostream_t *iostream; 39 } suunto_eon_device_t; 40 41 static dc_status_t suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 42 static dc_status_t suunto_eon_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 43 44 static const dc_device_vtable_t suunto_eon_device_vtable = { 45 sizeof(suunto_eon_device_t), 46 DC_FAMILY_SUUNTO_EON, 47 suunto_common_device_set_fingerprint, /* set_fingerprint */ 48 NULL, /* read */ 49 NULL, /* write */ 50 suunto_eon_device_dump, /* dump */ 51 suunto_eon_device_foreach, /* foreach */ 52 NULL, /* timesync */ 53 NULL /* close */ 54 }; 55 56 static const suunto_common_layout_t suunto_eon_layout = { 57 0, /* eop */ 58 0x100, /* rb_profile_begin */ 59 SZ_MEMORY, /* rb_profile_end */ 60 6, /* fp_offset */ 61 3 /* peek */ 62 }; 63 64 65 dc_status_t 66 suunto_eon_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 67 { 68 dc_status_t status = DC_STATUS_SUCCESS; 69 suunto_eon_device_t *device = NULL; 70 71 if (out == NULL) 72 return DC_STATUS_INVALIDARGS; 73 74 // Allocate memory. 75 device = (suunto_eon_device_t *) dc_device_allocate (context, &suunto_eon_device_vtable); 76 if (device == NULL) { 77 ERROR (context, "Failed to allocate memory."); 78 return DC_STATUS_NOMEMORY; 79 } 80 81 // Initialize the base class. 82 suunto_common_device_init (&device->base); 83 84 // Set the default values. 85 device->iostream = iostream; 86 87 // Set the serial communication protocol (1200 8N2). 88 status = dc_iostream_configure (device->iostream, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE); 89 if (status != DC_STATUS_SUCCESS) { 90 ERROR (context, "Failed to set the terminal attributes."); 91 goto error_free; 92 } 93 94 // Set the timeout for receiving data (1000ms). 95 status = dc_iostream_set_timeout (device->iostream, 1000); 96 if (status != DC_STATUS_SUCCESS) { 97 ERROR (context, "Failed to set the timeout."); 98 goto error_free; 99 } 100 101 // Clear the RTS line. 102 status = dc_iostream_set_rts (device->iostream, 0); 103 if (status != DC_STATUS_SUCCESS) { 104 ERROR (context, "Failed to set the DTR/RTS line."); 105 goto error_free; 106 } 107 108 *out = (dc_device_t*) device; 109 110 return DC_STATUS_SUCCESS; 111 112 error_free: 113 dc_device_deallocate ((dc_device_t *) device); 114 return status; 115 } 116 117 118 static dc_status_t 119 suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 120 { 121 dc_status_t status = DC_STATUS_SUCCESS; 122 suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; 123 124 // Pre-allocate the required amount of memory. 125 if (!dc_buffer_reserve (buffer, SZ_MEMORY)) { 126 ERROR (abstract->context, "Insufficient buffer space available."); 127 return DC_STATUS_NOMEMORY; 128 } 129 130 // Enable progress notifications. 131 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 132 progress.maximum = SZ_MEMORY + 1; 133 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 134 135 // Send the command. 136 unsigned char command[1] = {'P'}; 137 status = dc_iostream_write (device->iostream, command, sizeof (command), NULL); 138 if (status != DC_STATUS_SUCCESS) { 139 ERROR (abstract->context, "Failed to send the command."); 140 return status; 141 } 142 143 // Receive the answer. 144 unsigned int nbytes = 0; 145 unsigned char answer[SZ_MEMORY + 1] = {0}; 146 while (nbytes < sizeof(answer)) { 147 // Set the minimum packet size. 148 unsigned int len = 64; 149 150 // Increase the packet size if more data is immediately available. 151 size_t available = 0; 152 status = dc_iostream_get_available (device->iostream, &available); 153 if (status == DC_STATUS_SUCCESS && available > len) 154 len = available; 155 156 // Limit the packet size to the total size. 157 if (nbytes + len > sizeof(answer)) 158 len = sizeof(answer) - nbytes; 159 160 // Read the packet. 161 status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL); 162 if (status != DC_STATUS_SUCCESS) { 163 ERROR (abstract->context, "Failed to receive the answer."); 164 return status; 165 } 166 167 // Update and emit a progress event. 168 progress.current += len; 169 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 170 171 nbytes += len; 172 } 173 174 // Verify the checksum of the package. 175 unsigned char crc = answer[sizeof (answer) - 1]; 176 unsigned char ccrc = checksum_add_uint8 (answer, sizeof (answer) - 1, 0x00); 177 if (crc != ccrc) { 178 ERROR (abstract->context, "Unexpected answer checksum."); 179 return DC_STATUS_PROTOCOL; 180 } 181 182 dc_buffer_append (buffer, answer, SZ_MEMORY); 183 184 // Emit a device info event. 185 dc_event_devinfo_t devinfo; 186 devinfo.model = 0; 187 devinfo.firmware = 0; 188 devinfo.serial = array_convert_bcd2dec (answer + 244, 3); 189 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 190 191 return DC_STATUS_SUCCESS; 192 } 193 194 195 static dc_status_t 196 suunto_eon_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 197 { 198 suunto_common_device_t *device = (suunto_common_device_t *) abstract; 199 200 dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); 201 if (buffer == NULL) 202 return DC_STATUS_NOMEMORY; 203 204 dc_status_t rc = suunto_eon_device_dump (abstract, buffer); 205 if (rc != DC_STATUS_SUCCESS) { 206 dc_buffer_free (buffer); 207 return rc; 208 } 209 210 rc = suunto_common_extract_dives (device, &suunto_eon_layout, dc_buffer_get_data (buffer), 211 callback, userdata); 212 213 dc_buffer_free (buffer); 214 215 return rc; 216 } 217 218 219 dc_status_t 220 suunto_eon_device_write_name (dc_device_t *abstract, unsigned char data[], unsigned int size) 221 { 222 dc_status_t status = DC_STATUS_SUCCESS; 223 suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; 224 225 if (!ISINSTANCE (abstract)) 226 return DC_STATUS_INVALIDARGS; 227 228 if (size > 20) 229 return DC_STATUS_INVALIDARGS; 230 231 // Send the command. 232 unsigned char command[21] = {'N'}; 233 memcpy (command + 1, data, size); 234 status = dc_iostream_write (device->iostream, command, sizeof (command), NULL); 235 if (status != DC_STATUS_SUCCESS) { 236 ERROR (abstract->context, "Failed to send the command."); 237 return status; 238 } 239 240 return DC_STATUS_SUCCESS; 241 } 242 243 244 dc_status_t 245 suunto_eon_device_write_interval (dc_device_t *abstract, unsigned char interval) 246 { 247 dc_status_t status = DC_STATUS_SUCCESS; 248 suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; 249 250 if (!ISINSTANCE (abstract)) 251 return DC_STATUS_INVALIDARGS; 252 253 // Send the command. 254 unsigned char command[2] = {'T', interval}; 255 status = dc_iostream_write (device->iostream, command, sizeof (command), NULL); 256 if (status != DC_STATUS_SUCCESS) { 257 ERROR (abstract->context, "Failed to send the command."); 258 return status; 259 } 260 261 return DC_STATUS_SUCCESS; 262 }