<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Jan 13, 2015 at 8:06 AM, Jef Driesen <span dir="ltr"><<a href="mailto:jef@libdivecomputer.org" target="_blank">jef@libdivecomputer.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">John,<br>
<br>
I finally had some time again for another look at the cochran backend. This time I concentrated mainly on the data from your Commander. The main issue there is that the profile ringbuffer is completely full. The oldest logbook entries are pointing to profile data that has already been overwritten with newer dives. Because this is not handled properly, those older dives end up with completely bogus profile data.<br>
<br>
There are 347 logbook entries, and the end-of-profile (eop) pointer contains the value 0x0009a20d. If I dump the pre, begin and end pointers again, I get this:<br>
<br>
 346 00098290 00098478 00099d5a   488 1203<br>
 345 00097d5f 00097d5f 00097ddf     0 1201<br>
 344 0009661a 000966d3 000978ae   185 1201<br>
 ...<br>
 225 0009b16e 0009b16e 0009cbce     0 1201<br>
 224 00099e22 00099e6f 0009acbd    77 1201 <- Contains the eop address.<br>
 223 00096e85 00098109 00099971  4740 1201<br>
 ...<br>
  78 0009b79a 0009c2a1 0009d247  2823 1205<br>
  77 00099eb5 00099edf 0009b2e9    42 1201 <- Yet another time here.<br>
  76 00098588 000985c4 00099a04    60 1201<br>
 ...<br>
   1 00021928 00021a00 00023420   216 1201<br>
   0 00020000 000201fd 00021477   509 1201<br>
<br>
As you can see dive #224 contains the eop address, which means part of the profile belongs to dive #346. Thus all older dives no longer have valid profile data.<br>
<br>
The easiest solution to fix this is to process dives from the oldest to the newest one (which you already do), and sum the profile length. Once the total length exceeds the size of the ringbuffer (e.g. the maximum amount of profile data), all older dives will no longer have profile data. For those dives you only have the logbook entry.<br>
<br>
I have attached a patch with a proof of concept implementation. It's far from complete, but I think you'll get the idea. The patch also illustrate how you can nicely concentrate all the main logic for downloading the dives into the foreach function, instead of scattered over many functions. The main idea is that you try to separate the download and parsing logic as much as possible.<br>
<br>
(More comments inline.)<span class=""><br></span></blockquote><div><br></div><div>Is the application going to expect to see dives from oldest to newest?<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="">
<br>
On 2014-12-22 21:58, John Van Ostrand wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="">
On Mon, Dec 22, 2014 at 10:38 AM, Jef Driesen <<a href="mailto:jef@libdivecomputer.org" target="_blank">jef@libdivecomputer.org</a>><br>
</span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
A memory dump should be independent from whatever logic we use for<span class=""><br>
locating the dives. If you simply read everything from address 0 to the<br>
end, then you we can keep using those old memory dumps. If a device has<br>
lots of memory, then yes, it may take a while to download, but that's fine.<br>
Downloading a memory dump is not the normal use-case. It's intended for<br>
troubleshooting, and then you do want all the data. (For reference,<br>
downloading a full memory on a Suunto Vyper takes 10-15 minutes, and that's<br>
only 8KB of data!)<br>
<br>
If we really don't know the total amount of memory, then we'll have to<br>
make an assumption. If that assumptions turns out to be wrong, we can<br>
always adjust it. But at least we always have all data up to a certain<br>
address.<br>
<br>
</span></blockquote><span class="">
<br>
I'll have to commit to knowing the end of memory then. Right now I've<br>
avoided stating the end explicitly because I'm not sure where it is. If all<br>
Cochran DCs return 0xFF when past memory I can be a little liberal and not<br>
crash.<br>
</span></blockquote>
<br>
Rather than assuming a value that might be too large, I would start with a relative small value and increase whenever there is evidence that there is indeed more memory. Since the last memory area contains the profile ringbuffer, this evidence will most likely be a ringbuffer pointer that points past our assumed end. That's something that's very easy to check. <br></blockquote><div><br></div><div>Certainly it's easier if we know where memory starts and ends and I may be close to figuring that out. I've programmed this with a few ideals in mind. The first is to not dump a dive just because we aren't absolutely sure of the data. I argue this because I assume most end users of libdivecomputer (e.g. subsurface users) are not relying on the data to validate diving algorithms, they want to see a graphical representation of their dive. If they are like me they want to see most of their dive even if the last few samples are wrong. This explains why I'd like to see an attempt to retrieve corrupt dives and dives that wrap the sample buffer. In my experience it shows useful data. I'll get to my other ideals later.<br><br>The code is and was using the logic you describe above initially because I had limited experience with Cochran DCs. I use logic like that in cochran_get_sample_parms() where I interate through the dives to establish a low/high range of sample data. I round down or up as appropriate to 16k boundaries. 16K representing 91 min of dive samples on an EMC or 136 min on a Commander. I figured this was a reasonable number to capture most recreational dives that wrapped. Rounding both values is an attempt to capture samples for dives that have wrapped 
the DC buffer. On a new computer the first log entry should point to the start of memory however consider a DC where the log book has wrapped. The log entry with the lowest start memory location will probably not point to the start of sample memory. The start of sample memory will likely have sample data from the last dive that wrapped. The same is true for the end of sample memory. In other words the log book is not guaranteed to have a dive that points to the exact start or end of memory. We could ignore any dive with wrapping sample data, as you recommend but I think there's a good case for doing everything to present a dive to the end user.<br><br> The rounded sample-end-pointer was also used in 
cochran_guess_sample_end_address() prior to commit 
e4608460f246565a260e821f7869de668f79caa8. Guessing was done when the 
dive end wasn't recorded properly. The sample-end-pointer was also used to reassemble sample data for dives that wrapped the sample buffer. I should revisit this code to make sure it still works as intended.<br><br>It wasn't until recent changes to the dump function that I decided I would commit to a memory end value. You expressed concern about guessing the end of samples, specifically where they wrapped and so I removed guessing entirely and went with hard coded values. I also based this decision on what Cochran's dealer said and what I've seen with the DCs I've tested. I've mentioned before that the DC can be configured to use less memory, dealers have the ability to restrict and grant memory usage. I'm told that the dealer's software does this and as such I don't know if Cochran charges the dealer or if this is pure profit for the dealer. Keeping that in mind I haven't seen a Cochran with restricted memory, partly perhaps because I have only seen one where  sample memory was exhausted and possibly because dealers (maybe this one in particular) just grant all the memory or customers order it enabled. Basically I became much more comfortable hard coding the sample-end-pointer based on my experience.<br><br></div><div>I can understand the point of view that we should fail when we are unsure of data and then expect the end user to file a bug report. This would likely get earlier reports of errors but in the interim many new users might be turned away when the software fails to work for them.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
If we start with a value that is too large, and there is a device where the profile data crosses the ringbuffer end, then we will bogus profile data. I'll illustrate with an example. Assume for a momement that the real profile ringbuffer is in the range 0x200-0x400, and we (incorrectly) assume the end is at 0x800. If there happens to be a dive that crosses the ringbuffer end, for example from 0x380 to 0x280, then we'll read a first part from 0x380 to 0x800 and a second part from 0x200 to 0x280.<br>
<br>
200  280  380  400            800<br>
 |xxxxx    xxxxx|              |    <- Correct<br>
 |yyyyy    yyyyy|yyyyyyyyyyyyyy|    <- Wrong<br>
<br>
As you can see that first part contains an extra 0x400 bytes that shouldn't be there. We can't easily detect this kind of errors, other than the user noticing the resulting profile is completely bogus.<br>
<br>
If we had estimated the amount of memory too small, for example at 0x300, then we would be able to tell immediately that the 0x380 pointer is outside the allowed range. Then we know we have to adjust the range.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Since the other pieces of data (id, misc and config) have a fixed size,<br>
the structure for a good memory dump could be as simple as concatenating<br>
all pieces:<br>
<br>
id       67 bytes<br>
misc     1500 bytes<br>
config   2x512 (emc) or 4x512 (commander) bytes<br>
memory   variable length, memory starting at address<br>
<br>
An alternative is to only include the memory part, and take care of the<br>
other three separately, by means of the vendor event. This is how it's done<br>
in the several other backends (e.g. oceanic and reefnet).<br>
<br>
</blockquote>
<br>
Considering I'm one of two users of this I'd like to have the extra data.<br>
And considering the person doing the dump knows the model it should be<br>
apparent how to parse the data so I don't need to make a generic format.<br>
</blockquote>
<br></span>
I'm not sure I understand your concerns here. Of course we want that extra data. To locate the dives, we need at a minimum the id and config0 packets. And depending on what we discover, we might need the others too at some point. I did only mention that instead of pre-pending the id, misc and config blocks to the memory dump, we could deliver them separately by means of the vendor event. That's how it's done in those other backends.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Do you think we should consider the pre-dive event as part of the dive<br>
data? Based on your description, these pre-dive events do not look really<br>
interesting from a dive logbook point of view. I have no idea how the<br>
parser should deal with it, other than just ignoring them.<br>
<br>
</blockquote>
<br>
If the data were readily available I'd be interested in some of the data,<br>
like altitude and temp changes while travelling. However I've looked at<br>
some of the raw pre-dive data and I can't parse the event data. All that<br>
said I think this data is a passing interest.<br>
<br>
I took my cue from Analyst. It doesn't show the pre-dive events on dive<br>
profiles nor does it show the off-gassing data. It has an option to display<br>
"inter-dive" events and does so similar to a dive. I've not been interested<br>
in the data, except to find out what it was. I've been thinking of<br>
libdivecomputer in the context of Subsurface which has no mechanism to show<br>
inter-dive events or data. I think libdivecomputer would need some other<br>
mechanism to present this data and if only the Cochran does this it may not<br>
make sense to spend the time.<br>
</blockquote>
<br></span>
That's also my impression.<br>
<br>
But remember that even if libdivecomputer itself doesn't use this information nor provides an api to parse it, there might be someone out there who is interested in the raw data. It's perfectly possible to build a cochran specific application that use libdivecomputer only for downloading the data, and does the parsing on its own. Probably not a very likely scenario, but not impossible. Btw, that's also the reason why libdivecomputer always provides access to the raw data!<br>
<br>
Note that if we now strip this info, and change our minds later one, then it might be too late. Changing the data format will break backwards compatibility.</blockquote><div><br></div><div class="h5">I agree it would be nice to provide this data via API. If we decide to provide this data via the existing API it's going to mess up a lot of existing applications. The inter-dive events may not be so bad, since there are usually only a few events, keep in mind some dives have a lot. Also I don't know how to parse data from these events.<br><br>The off-gassing data, if sent out through the samples_foreach function are going to be incorporated into the aplications' dive profiles and appear as flat trailing lines in the profile. Temperature might be the only interesting sample and barely so.<br><br></div><div class="h5">Already other interesting data from the Cochran data stream is not provided through the API. Data like deepest stop time and tissue compartment loads are not accessible. To make off-gassing samples meaningful we should also present the in-dive TC loads and then we should indicate to the application that this is inter-dive data.<br><br></div><div class="h5">
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
A pointer of FFFFFFFF means the end of dive was never logged. I had a<br>
problem with my DC where it would reboot not logging the end of dive. The<br>
code tries to salvage the dive logs.<br>
<br>
</blockquote>
<br>
Can you recover some of the samples? Based on what I've seen so far (see<br>
my long table) it makes no sense to try to recover the samples, because it<br>
looks like at least part of the data you are recovering are the pre-dive<br>
events of another dive. That certainly doesn't look right.<br>
<br>
Look for example at dive #79:<br>
<br>
78 0014b8f3 0014b94a 0014ef9d    87 1800<br>
79 0014f6a5 0014f70d ffffffff   104 1800<br>
80 0014f6a5 0015043c 00151442  3479 1373862<br>
<br>
Your code to guess the end pointer will replace ffffffff with 0015043c,<br>
which is the start of the next dive (#80). So for dive #79 you take the<br>
range 0014f70d to 0015043c as the sample data. But that memory range is<br>
also the last part of the pre-dive events for dive #80. This overlap<br>
doesn't make any sense to me.<br>
<br>
</blockquote>
<br>
You can see several of those starting about there. The battery was low and<br>
the DC was starting to exhibit the crash problem so I was intentionally<br>
invoking it about that time so I could be confident in describing the<br>
problem.<br>
<br>
Take a look at<br>
<br>
 95 001627c7 0016447b ffffffff 7348 1451976<br>
 96 001627c7 0016463c 001661b3 7797 1451976<br>
<br>
That 7348 byte of samples on dive 95 was a 40 minute dive in Tobermory,<br>
Canada followed by another about that length. Both imported mostly<br>
correctly. I'd rather have that flawed dive profile than nothing.<br>
<br>
I'm not sure what dive 79 was, but I figured the user would rather see<br>
corrupt dive imported than none so s/he could make a choice.<br>
</blockquote>
<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div></div>
If you look at dive #96, then there is 7797 bytes of pre-dive events in the range 001627c7-0016463c. But 7348 of those bytes are also the pre-dive events of dive #96 (range 001627c7-0016447b). That's what puzzles me. How can the pre-dive events of two dives overlap? That simply doesn't make any sense to me.<br>
<br>
If you then guess the end address of dive #95 to be the start address of dive #96, then the samples of dive #95 end up as somewhere in the pre-dive data of dive #96. That's just weird.<br>
<br>
<br>
The only explanation that I can come up with is that the dive computer started writing dive #95. Then it failed before the dive was finished (e.g. battery empty, reboot or whatever) and the end pointer didn't get written correctly. Hence the ffffffff value. But the end-of-profile pointer is probably only updated after the dive is finished. Hence the pre-dive pointer of the next dive retains the same value. I suspect that the current write position is somehow preserved and thus the next dive will start from there again. But this is of course just guessing.<br>
<br></blockquote><div> </div><div>That's what I think too. given that flash can be slow and have wears with writes I expect it's logical to eliminate unnecessary writes. Based on what I've seen the Cochran code writes the first half of the log when the dive starts (e.g. deeper than 6ft). There are two end-points to a dive. The first is when the diver ascends to 3ft. I think at this point Cochran updates the sample-end-pointer in config0. Cochran then continues logging until the configured "Post dive surface interval minutes" (10 - 20 minutes) elapses. This is why you see 1800 bytes (10 min * 60 seconds * 3 samples/sec) between dive-end-pointer and the pre-dive-pointer. At any time prior to this elapsing a submersion to below 6 ft continues the dive. I think this is why these post-dive samples exist. After the elapsed interval, the end-dive log is written and now the pre-dive-pointer is known and can be updated in config0.<br><br></div><div>So although the pre-dive pointer is bogus on the dive after a corrupt dive, the dive sample data of the corrupt dive is intact but determining the end has been difficult. Now that I've thought through the Cochran logic, I should go over my logic to see if I can better determine the end of a corrupt dive.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I understand why you're trying to salvage those corrupt dives (in the sense that having something is better than nothing at all), but I worry that this will complicate the code or produce bogus data (which is even worse). When I look at some of those salvaged dives, that seems to be the case already. They end abrupt, which is probably normal. But there appears to be some amount of "random" data (e.g. spikes in the profile) too. And that's not good. Maybe this is just a bug in the parsing that we can fix? I simply don't know at this point.<br></blockquote><div><br></div><div>The code ins't too complicated because of it. A corrupt dive is known because of the FFFFFFFF dive-end-pointer. Knowing that we loop, exiting on a memory boundary, when we see a sample that doesn't make sense (0xFF), when we see normal end-dive events or normal start-dive events. If we don't find end-dive events and we stop because of start-dive events we may have processed some inter-dive events as sample data and I think that's why we see the spikes at the end. It's a few lines of code in the parse function plus the guess function and a few if-thens. Since corrupt dives don't have end-dive log data the code parses the samples for depth, temp and duration, etc.<br><br></div><div>Now that I've walked through the cochran logic and why it's writing sample pointers at certain times I might be able to do better guessing at end-dives.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
What does the Cochran application do with those corrupt dives? Does it show them? With or without a profile?<br></blockquote><div><br></div><div>Cochran refuses to display a corrupt dive. You also mention that the Commander shows some erratic dives, although in the context of reclaimed and overwritten sample data. There were a lot of Commander dives that looked corrupt in Analyst. I was happy that libdivecomputer/subsurface mirrored those corrupt dive profiles because it suggested the sample data was corrupt or that we were at least as capable as Analyst.<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I've been holding off asking Cochran again for help. They previously<br>
refused citing concern that open source code would reveal too much about<br>
their algorithm. Now that the code exists I wonder if they would be<br>
willing<br>
to help by telling me how to determine model and memory configuration, or<br>
if they will demand that I stop.. It's also the same reason I've been<br>
trying to respect their privacy by not reading anything but my data.<br>
<br>
</blockquote>
<br>
That's their usual argument. The truth is that I've never encountered a<br>
device where you can download anything worth protecting (e.g. firmware<br>
code, decompression algorithm). All you can access is the memory containing<br>
the dives.<br>
</blockquote>
<br></span><span class="">
Have you had any success in changing their minds? Do you have any good<br>
examples of DC manufacturers that have been open that I could use as<br>
examples when talking to Cochran next?<br>
</span></blockquote>
<br>
My experience so far is that either they are open minded (HW, Reefnet, Atomics, Shearwater, etc) or not. Convincing them appears to be rather difficult :-(<span class=""><font color="#888888"><br>
<br></font></span></blockquote><div><br></div></div><br clear="all"></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div>John Van Ostrand<br></div><div>At large on sabbatical<br></div><br></div></div>
</div></div>