|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Swift Navigation Inc. |
| 3 | + * Contact: Adel Mamin <[email protected]> |
| 4 | + * |
| 5 | + * This source is subject to the license found in the file 'LICENSE' which must |
| 6 | + * be be distributed together with this source. All other rights reserved. |
| 7 | + * |
| 8 | + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 9 | + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED |
| 10 | + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + @file |
| 15 | +
|
| 16 | + This utility helps to verify the integrity of samples data from Piksi v3 HW. |
| 17 | + The regular Piksi v3 samples data format in one byte is: |
| 18 | + RF4 RF3 RF2 RF1 |
| 19 | + 00 00 00 00 |
| 20 | +
|
| 21 | + RF1 - GPS L1 |
| 22 | + RF2 - GLONASS L1 |
| 23 | + RF3 - GLONASS L2 |
| 24 | + RF4 - GPS L2 |
| 25 | +
|
| 26 | + This utility is expecting RF3 and RF2 to have 4 bits counter, |
| 27 | + which is continuosly incremented with modulo SAMPLE_COUNTER_MODULO. |
| 28 | + RF2 is expected to have two least significant bits |
| 29 | + and RF3 - two most significant bits. |
| 30 | + Therefore, the bit indexes are: |
| 31 | + RF3 RF2 |
| 32 | + 5&4 3&2 |
| 33 | +*/ |
| 34 | + |
| 35 | +#ifndef LIBSWIFTNAV_COUNTER_CHECKER_H |
| 36 | +#define LIBSWIFTNAV_COUNTER_CHECKER_H |
| 37 | + |
| 38 | +#include <stdint.h> |
| 39 | +#include <stddef.h> |
| 40 | +#include <libswiftnav/common.h> |
| 41 | + |
| 42 | +/** How many bytes we read from samples stream at a time. */ |
| 43 | +#define SAMPLE_CHUNK_SIZE (1024 * 1024) /* [bytes] */ |
| 44 | + |
| 45 | +/** The counter embedded into the sample data stream is expected |
| 46 | + to have this modulo. */ |
| 47 | +#define SAMPLE_COUNTER_MODULO 13 |
| 48 | + |
| 49 | +/** The counter bit size. */ |
| 50 | +#define SAMPLE_COUNTER_BITS 4 /* [bits] */ |
| 51 | + |
| 52 | +/** Teh counter bit offset within a byte. */ |
| 53 | +#define SAMPLE_COUNTER_OFFSET 2 /* [bits] */ |
| 54 | + |
| 55 | +/** How many bytes we read from samples file at a time. */ |
| 56 | +#define COUNTER_CHECKER_CHUNK_SIZE (1024 * 1024) /* [bytes] */ |
| 57 | + |
| 58 | +/** Get \e num of bits at \e offset in \e data */ |
| 59 | +#define GET_BITS(data, offset, num) \ |
| 60 | + ( ((data) >> (offset)) & ((1 << (num)) - 1) ) |
| 61 | + |
| 62 | +/** Get \e num of bits at \e offset in \e data */ |
| 63 | +#define SET_BITS(data, offset, num, bits) \ |
| 64 | + ( ( ~( ((1 << (num)) - 1) << (offset) ) & (data) ) | \ |
| 65 | + ( ( (bits) & ((1 << (num)) - 1) ) << (offset) ) ) |
| 66 | + |
| 67 | +/** Get GPS counter |
| 68 | + * \param data Data byte |
| 69 | + * \return The counter value |
| 70 | + */ |
| 71 | +uint8_t get_gps_counter(uint8_t data); |
| 72 | + |
| 73 | +/** Get Glonass counter |
| 74 | + * \param data Data byte |
| 75 | + * \return The counter value |
| 76 | + */ |
| 77 | +uint8_t get_glo_counter(uint8_t data); |
| 78 | + |
| 79 | +uint8_t set_gps_counter(uint8_t data, uint8_t counter); |
| 80 | +uint8_t set_glo_counter(uint8_t data, uint8_t counter); |
| 81 | + |
| 82 | +/** A data mismatch descriptor. */ |
| 83 | +struct mismatch { |
| 84 | + size_t offset; /**! Data offset [bytes]. */ |
| 85 | + uint8_t data; /**! Data. */ |
| 86 | + uint8_t expected_counter; /**! The expected counter value. */ |
| 87 | + uint8_t actual_counter; /**! The actual counter value. */ |
| 88 | +}; |
| 89 | + |
| 90 | +/** Mismatch array data */ |
| 91 | +struct mismatch_data { |
| 92 | + /** The sample data counter mismatch incidents are stored here. */ |
| 93 | + struct mismatch data[COUNTER_CHECKER_CHUNK_SIZE]; |
| 94 | + /** How many valid entries there are in \e mismatch array. */ |
| 95 | + size_t counter; |
| 96 | +}; |
| 97 | + |
| 98 | +struct callbacks { |
| 99 | + size_t (*read)(void *ptr, size_t size, void *f); |
| 100 | + void (*rewind)(void *f); |
| 101 | + uint8_t (*get_counter)(uint8_t data); |
| 102 | + uint8_t (*set_counter)(uint8_t data, uint8_t counter); |
| 103 | +}; |
| 104 | + |
| 105 | +typedef size_t (*stream_read_t)(void *ptr, size_t size, void *stream); |
| 106 | +typedef void (*stream_rewind_t)(void *stream); |
| 107 | + |
| 108 | +uint8_t get_rf32_counter(uint8_t data); |
| 109 | +uint8_t set_rf32_counter(uint8_t data, uint8_t counter); |
| 110 | +uint8_t get_rf41_counter(uint8_t data); |
| 111 | +uint8_t set_rf41_counter(uint8_t data, uint8_t counter); |
| 112 | + |
| 113 | +void report_mismatch(const struct mismatch_data *mismatch); |
| 114 | + |
| 115 | +void counter_checker_init(void); |
| 116 | + |
| 117 | +const struct mismatch_data *counter_checker_run(struct callbacks *cbs, |
| 118 | + void *stream, |
| 119 | + size_t data_size); |
| 120 | + |
| 121 | +#endif /* LIBSWIFTNAV_COUNTER_CHECKER_H */ |
0 commit comments