suunto_solution.c (9499B)
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 <stdlib.h> // malloc, free 23 24 #include <libdivecomputer/units.h> 25 26 #include "suunto_solution.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "ringbuffer.h" 30 #include "array.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), &suunto_solution_device_vtable) 33 34 #define SZ_MEMORY 256 35 36 #define RB_PROFILE_BEGIN 0x020 37 #define RB_PROFILE_END 0x100 38 39 typedef struct suunto_solution_device_t { 40 dc_device_t base; 41 dc_iostream_t *iostream; 42 } suunto_solution_device_t; 43 44 static dc_status_t suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 45 static dc_status_t suunto_solution_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 46 47 static const dc_device_vtable_t suunto_solution_device_vtable = { 48 sizeof(suunto_solution_device_t), 49 DC_FAMILY_SUUNTO_SOLUTION, 50 NULL, /* set_fingerprint */ 51 NULL, /* read */ 52 NULL, /* write */ 53 suunto_solution_device_dump, /* dump */ 54 suunto_solution_device_foreach, /* foreach */ 55 NULL, /* timesync */ 56 NULL /* close */ 57 }; 58 59 static dc_status_t 60 suunto_solution_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); 61 62 dc_status_t 63 suunto_solution_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 64 { 65 dc_status_t status = DC_STATUS_SUCCESS; 66 suunto_solution_device_t *device = NULL; 67 68 if (out == NULL) 69 return DC_STATUS_INVALIDARGS; 70 71 // Allocate memory. 72 device = (suunto_solution_device_t *) dc_device_allocate (context, &suunto_solution_device_vtable); 73 if (device == NULL) { 74 ERROR (context, "Failed to allocate memory."); 75 return DC_STATUS_NOMEMORY; 76 } 77 78 // Set the default values. 79 device->iostream = iostream; 80 81 // Set the serial communication protocol (1200 8N2). 82 status = dc_iostream_configure (device->iostream, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_TWO, DC_FLOWCONTROL_NONE); 83 if (status != DC_STATUS_SUCCESS) { 84 ERROR (context, "Failed to set the terminal attributes."); 85 goto error_free; 86 } 87 88 // Set the timeout for receiving data (1000ms). 89 status = dc_iostream_set_timeout (device->iostream, 1000); 90 if (status != DC_STATUS_SUCCESS) { 91 ERROR (context, "Failed to set the timeout."); 92 goto error_free; 93 } 94 95 // Clear the RTS line. 96 status = dc_iostream_set_rts (device->iostream, 0); 97 if (status != DC_STATUS_SUCCESS) { 98 ERROR (context, "Failed to set the DTR/RTS line."); 99 goto error_free; 100 } 101 102 *out = (dc_device_t*) device; 103 104 return DC_STATUS_SUCCESS; 105 106 error_free: 107 dc_device_deallocate ((dc_device_t *) device); 108 return status; 109 } 110 111 112 static dc_status_t 113 suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 114 { 115 dc_status_t status = DC_STATUS_SUCCESS; 116 suunto_solution_device_t *device = (suunto_solution_device_t*) abstract; 117 118 // Allocate the required amount of memory. 119 if (!dc_buffer_resize (buffer, SZ_MEMORY)) { 120 ERROR (abstract->context, "Insufficient buffer space available."); 121 return DC_STATUS_NOMEMORY; 122 } 123 124 unsigned char *data = dc_buffer_get_data (buffer); 125 126 // Enable progress notifications. 127 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 128 progress.maximum = SZ_MEMORY - 1 + 2; 129 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 130 131 unsigned char command[3] = {0}; 132 unsigned char answer[3] = {0}; 133 134 // Assert DTR 135 status = dc_iostream_set_dtr(device->iostream, 1); 136 if (status != DC_STATUS_SUCCESS) { 137 ERROR (abstract->context, "Failed to set the DTR line."); 138 return status; 139 } 140 141 // Send: 0xFF 142 command[0] = 0xFF; 143 dc_iostream_write (device->iostream, command, 1, NULL); 144 145 // Receive: 0x3F 146 status = dc_iostream_read (device->iostream, answer, 1, NULL); 147 if (status != DC_STATUS_SUCCESS) 148 return status; 149 if (answer[0] != 0x3F) 150 WARNING (abstract->context, "Unexpected answer byte."); 151 152 // Send: 0x4D, 0x01, 0x01 153 command[0] = 0x4D; 154 command[1] = 0x01; 155 command[2] = 0x01; 156 dc_iostream_write (device->iostream, command, 3, NULL); 157 158 // Update and emit a progress event. 159 progress.current += 1; 160 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 161 162 data[0] = 0x00; 163 for (unsigned int i = 1; i < SZ_MEMORY; ++i) { 164 // Receive: 0x01, i, data[i] 165 status = dc_iostream_read (device->iostream, answer, 3, NULL); 166 if (status != DC_STATUS_SUCCESS) 167 return status; 168 if (answer[0] != 0x01 || answer[1] != i) 169 WARNING (abstract->context, "Unexpected answer byte."); 170 171 // Send: i 172 command[0] = i; 173 dc_iostream_write (device->iostream, command, 1, NULL); 174 175 // Receive: data[i] 176 status = dc_iostream_read (device->iostream, data + i, 1, NULL); 177 if (status != DC_STATUS_SUCCESS) 178 return status; 179 if (data[i] != answer[2]) 180 WARNING (abstract->context, "Unexpected answer byte."); 181 182 // Send: 0x0D 183 command[0] = 0x0D; 184 dc_iostream_write (device->iostream, command, 1, NULL); 185 186 // Update and emit a progress event. 187 progress.current += 1; 188 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 189 } 190 191 // Receive: 0x02, 0x00, 0x80 192 status = dc_iostream_read (device->iostream, answer, 3, NULL); 193 if (status != DC_STATUS_SUCCESS) 194 return status; 195 if (answer[0] != 0x02 || answer[1] != 0x00 || answer[2] != 0x80) 196 WARNING (abstract->context, "Unexpected answer byte."); 197 198 // Send: 0x80 199 command[0] = 0x80; 200 dc_iostream_write (device->iostream, command, 1, NULL); 201 202 // Receive: 0x80 203 status = dc_iostream_read (device->iostream, answer, 1, NULL); 204 if (status != DC_STATUS_SUCCESS) 205 return status; 206 if (answer[0] != 0x80) 207 WARNING (abstract->context, "Unexpected answer byte."); 208 209 // Send: 0x20 210 command[0] = 0x20; 211 dc_iostream_write (device->iostream, command, 1, NULL); 212 213 // Receive: 0x3F 214 status = dc_iostream_read (device->iostream, answer, 1, NULL); 215 if (status != DC_STATUS_SUCCESS) 216 return status; 217 if (answer[0] != 0x3F) 218 WARNING (abstract->context, "Unexpected answer byte."); 219 220 // Update and emit a progress event. 221 progress.current += 1; 222 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 223 224 // Emit a device info event. 225 dc_event_devinfo_t devinfo; 226 devinfo.model = 0; 227 devinfo.firmware = 0; 228 devinfo.serial = array_convert_bcd2dec (data + 0x1D, 3); 229 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 230 231 return DC_STATUS_SUCCESS; 232 } 233 234 235 static dc_status_t 236 suunto_solution_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 237 { 238 dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); 239 if (buffer == NULL) 240 return DC_STATUS_NOMEMORY; 241 242 dc_status_t rc = suunto_solution_device_dump (abstract, buffer); 243 if (rc != DC_STATUS_SUCCESS) { 244 dc_buffer_free (buffer); 245 return rc; 246 } 247 248 rc = suunto_solution_extract_dives (abstract, 249 dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); 250 251 dc_buffer_free (buffer); 252 253 return rc; 254 } 255 256 257 static dc_status_t 258 suunto_solution_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) 259 { 260 if (abstract && !ISINSTANCE (abstract)) 261 return DC_STATUS_INVALIDARGS; 262 263 if (size < SZ_MEMORY) 264 return DC_STATUS_DATAFORMAT; 265 266 unsigned char buffer[RB_PROFILE_END - RB_PROFILE_BEGIN] = {0}; 267 268 // Get the end of the profile ring buffer. 269 unsigned int eop = data[0x18]; 270 if (eop < RB_PROFILE_BEGIN || 271 eop >= RB_PROFILE_END || 272 data[eop] != 0x82) 273 { 274 return DC_STATUS_DATAFORMAT; 275 } 276 277 // The profile data is stored backwards in the ringbuffer. To locate 278 // the most recent dive, we start from the end of profile marker and 279 // traverse the ringbuffer in the opposite direction (forwards). 280 // Since the profile data is now processed in the "wrong" direction, 281 // it needs to be reversed again. 282 unsigned int previous = eop; 283 unsigned int current = eop; 284 for (unsigned int i = 0; i < RB_PROFILE_END - RB_PROFILE_BEGIN; ++i) { 285 // Move forwards through the ringbuffer. 286 current++; 287 if (current == RB_PROFILE_END) 288 current = RB_PROFILE_BEGIN; 289 290 // Check for an end of profile marker. 291 if (data[current] == 0x82) 292 break; 293 294 // Store the current byte into the buffer. By starting at the 295 // end of the buffer, the data is automatically reversed. 296 unsigned int idx = RB_PROFILE_END - RB_PROFILE_BEGIN - i - 1; 297 buffer[idx] = data[current]; 298 299 // Check for an end of dive marker (of the next dive), 300 // to find the start of the current dive. 301 unsigned int peek = ringbuffer_increment (current, 2, RB_PROFILE_BEGIN, RB_PROFILE_END); 302 if (data[peek] == 0x80) { 303 unsigned int len = ringbuffer_distance (previous, current, DC_RINGBUFFER_EMPTY, RB_PROFILE_BEGIN, RB_PROFILE_END); 304 305 if (callback && !callback (buffer + idx, len, NULL, 0, userdata)) 306 return DC_STATUS_SUCCESS; 307 308 previous = current; 309 } 310 } 311 312 if (data[current] != 0x82) 313 return DC_STATUS_DATAFORMAT; 314 315 return DC_STATUS_SUCCESS; 316 }