|
| 1 | +/* |
| 2 | + * This program is free software: you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation, either version 3 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <string.h> |
| 17 | +#include <time.h> |
| 18 | + |
| 19 | +#include "here_images.h" |
| 20 | +#include "private.h" |
| 21 | + |
| 22 | +static void process_packet(here_images_t *st) |
| 23 | +{ |
| 24 | + if (st->payload_len < 28) |
| 25 | + { |
| 26 | + log_warn("HERE Image frame too short"); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + int image_type = (st->buffer[0] >> 4); |
| 31 | + int seq = (st->buffer[0] & 0x0f); |
| 32 | + |
| 33 | + if ((image_type != NRSC5_HERE_IMAGE_TRAFFIC) && (image_type != NRSC5_HERE_IMAGE_WEATHER)) |
| 34 | + { |
| 35 | + log_warn("Unknown HERE Image type: %d", image_type); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + int n1 = (st->buffer[2] << 8) | st->buffer[3]; |
| 40 | + int n2 = (st->buffer[4] << 8) | st->buffer[5]; |
| 41 | + unsigned int timestamp = ((unsigned int)st->buffer[9] << 24) | (st->buffer[10] << 16) |
| 42 | + | (st->buffer[11] << 8) | st->buffer[12]; |
| 43 | + |
| 44 | + int lat1 = ((st->buffer[14] & 0x7f) << 18) | (st->buffer[15] << 10) | (st->buffer[16] << 2) | (st->buffer[17] >> 6); |
| 45 | + if (st->buffer[14] & 0x80) |
| 46 | + lat1 = -lat1; |
| 47 | + |
| 48 | + int lon1 = ((st->buffer[17] & 0x1f) << 20) | (st->buffer[18] << 12) | (st->buffer[19] << 4) | (st->buffer[20] >> 4); |
| 49 | + if (st->buffer[17] & 0x20) |
| 50 | + lon1 = -lon1; |
| 51 | + |
| 52 | + int lat2 = ((st->buffer[20] & 0x07) << 22) | (st->buffer[21] << 14) | (st->buffer[22] << 6) | (st->buffer[23] >> 2); |
| 53 | + if (st->buffer[20] & 0x08) |
| 54 | + lat2 = -lat2; |
| 55 | + |
| 56 | + int lon2 = ((st->buffer[23] & 0x01) << 24) | (st->buffer[24] << 16) | (st->buffer[25] << 8) | st->buffer[26]; |
| 57 | + if (st->buffer[23] & 0x02) |
| 58 | + lon2 = -lon2; |
| 59 | + |
| 60 | + int filename_len = st->buffer[27]; |
| 61 | + |
| 62 | + if (st->payload_len < 34 + filename_len) |
| 63 | + { |
| 64 | + log_warn("HERE Image frame too short"); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + int file_len = (st->buffer[32 + filename_len] << 8) | st->buffer[33 + filename_len]; |
| 69 | + |
| 70 | + if (st->payload_len < 34 + filename_len + file_len) |
| 71 | + { |
| 72 | + log_warn("HERE Image frame too short"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + st->buffer[28 + filename_len] = '\0'; |
| 77 | + |
| 78 | + nrsc5_report_here_image(st->radio, image_type, seq, n1, n2, timestamp, |
| 79 | + lat1 / 100000.f, lon1 / 100000.f, lat2 / 100000.f, lon2 / 100000.f, |
| 80 | + (char *)&st->buffer[28], file_len, &st->buffer[34 + filename_len]); |
| 81 | +} |
| 82 | + |
| 83 | +void here_images_push(here_images_t *st, uint16_t seq, unsigned int len, uint8_t *buf) |
| 84 | +{ |
| 85 | + if (seq != st->expected_seq) |
| 86 | + { |
| 87 | + memset(st->buffer, 0, sizeof(st->buffer)); |
| 88 | + st->payload_len = -1; |
| 89 | + st->sync_state = 0; |
| 90 | + } |
| 91 | + |
| 92 | + for (unsigned int offset = 0; offset < len; offset++) |
| 93 | + { |
| 94 | + st->sync_state <<= 8; |
| 95 | + st->sync_state |= buf[offset]; |
| 96 | + |
| 97 | + if (st->payload_len == -1) // waiting for sync |
| 98 | + { |
| 99 | + if (((st->sync_state >> 16) & 0xffffffff) == 0xfff7fff7) |
| 100 | + { |
| 101 | + st->payload_len = st->sync_state & 0xffff; |
| 102 | + st->buffer_idx = 0; |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + st->buffer[st->buffer_idx++] = buf[offset]; |
| 108 | + if (st->buffer_idx == (st->payload_len + 2)) |
| 109 | + { |
| 110 | + process_packet(st); |
| 111 | + st->payload_len = -1; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + st->expected_seq = (seq + 1) & 0xffff; |
| 117 | +} |
| 118 | + |
| 119 | +void here_images_reset(here_images_t *st) |
| 120 | +{ |
| 121 | + st->expected_seq = -1; |
| 122 | +} |
| 123 | + |
| 124 | +void here_images_init(here_images_t *st, nrsc5_t *radio) |
| 125 | +{ |
| 126 | + st->radio = radio; |
| 127 | +} |
0 commit comments