suunto_common.c (4284B)
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 23 #include <string.h> // memcpy, memcmp 24 #include <assert.h> // assert 25 26 #include "suunto_common.h" 27 #include "ringbuffer.h" 28 #include "array.h" 29 30 #define RB_PROFILE_DISTANCE(a,b,l) ringbuffer_distance (a, b, DC_RINGBUFFER_EMPTY, l->rb_profile_begin, l->rb_profile_end) 31 #define RB_PROFILE_PEEK(a,l) ringbuffer_decrement (a, l->peek, l->rb_profile_begin, l->rb_profile_end) 32 33 void 34 suunto_common_device_init (suunto_common_device_t *device) 35 { 36 assert (device != NULL); 37 38 // Set the default values. 39 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 40 } 41 42 43 dc_status_t 44 suunto_common_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 45 { 46 suunto_common_device_t *device = (suunto_common_device_t *) abstract; 47 48 assert (device != NULL); 49 50 if (size && size != sizeof (device->fingerprint)) 51 return DC_STATUS_INVALIDARGS; 52 53 if (size) 54 memcpy (device->fingerprint, data, sizeof (device->fingerprint)); 55 else 56 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 57 58 return DC_STATUS_SUCCESS; 59 } 60 61 62 dc_status_t 63 suunto_common_extract_dives (suunto_common_device_t *device, const suunto_common_layout_t *layout, const unsigned char data[], dc_dive_callback_t callback, void *userdata) 64 { 65 assert (layout != NULL); 66 67 unsigned int eop; 68 if (layout->eop) { 69 // Get the end-of-profile pointer directly from the header. 70 eop = array_uint16_be (data + layout->eop); 71 } else { 72 // Get the end-of-profile pointer by searching for the 73 // end-of-profile marker in the profile ringbuffer. 74 eop = layout->rb_profile_begin; 75 while (eop < layout->rb_profile_end) { 76 if (data[eop] == 0x82) 77 break; 78 eop++; 79 } 80 } 81 82 // Validate the end-of-profile pointer. 83 if (eop < layout->rb_profile_begin || 84 eop >= layout->rb_profile_end || 85 data[eop] != 0x82) 86 { 87 return DC_STATUS_DATAFORMAT; 88 } 89 90 // Memory buffer for the profile ringbuffer. 91 unsigned int length = layout->rb_profile_end - layout->rb_profile_begin; 92 unsigned char *buffer = (unsigned char *) malloc (length); 93 if (buffer == NULL) 94 return DC_STATUS_NOMEMORY; 95 96 unsigned int current = eop; 97 unsigned int previous = eop; 98 for (unsigned int i = 0; i < length; ++i) { 99 // Move backwards through the ringbuffer. 100 if (current == layout->rb_profile_begin) 101 current = layout->rb_profile_end; 102 current--; 103 104 // Check for an end of profile marker. 105 if (data[current] == 0x82) 106 break; 107 108 // Check for an end of dive marker (of the next dive), 109 // to find the start of the current dive. 110 unsigned int idx = RB_PROFILE_PEEK (current, layout); 111 if (data[idx] == 0x80) { 112 unsigned int len = RB_PROFILE_DISTANCE (current, previous, layout); 113 if (current + len > layout->rb_profile_end) { 114 unsigned int a = layout->rb_profile_end - current; 115 unsigned int b = (current + len) - layout->rb_profile_end; 116 memcpy (buffer + 0, data + current, a); 117 memcpy (buffer + a, data + layout->rb_profile_begin, b); 118 } else { 119 memcpy (buffer, data + current, len); 120 } 121 122 if (device && memcmp (buffer + layout->fp_offset, device->fingerprint, sizeof (device->fingerprint)) == 0) { 123 free (buffer); 124 return DC_STATUS_SUCCESS; 125 } 126 127 if (callback && !callback (buffer, len, buffer + layout->fp_offset, sizeof (device->fingerprint), userdata)) { 128 free (buffer); 129 return DC_STATUS_SUCCESS; 130 } 131 132 previous = current; 133 } 134 } 135 136 free (buffer); 137 138 if (data[current] != 0x82) 139 return DC_STATUS_DATAFORMAT; 140 141 return DC_STATUS_SUCCESS; 142 }