2017-10-14 11:30:49 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2017 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_keyfob.hpp"
|
|
|
|
|
|
|
|
#include "baseband_api.hpp"
|
|
|
|
#include "string_format.hpp"
|
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
uint8_t KeyfobView::subaru_get_checksum() {
|
2023-05-18 16:16:05 -04:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 9; i++) {
|
|
|
|
checksum ^= (frame[i] & 0x0F); // 00 11 22 33 44 55 66 77 88 9-
|
|
|
|
checksum ^= ((frame[i] >> 4) & 0x0F);
|
|
|
|
}
|
|
|
|
|
|
|
|
checksum ^= ((frame[9] >> 4) & 0x0F);
|
|
|
|
checksum++;
|
|
|
|
checksum &= 0x0F;
|
|
|
|
|
|
|
|
return checksum;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyfobView::subaru_is_valid() {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (frame[0] != 0x55)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (subaru_get_checksum() != (frame[9] & 0x0F))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t KeyfobView::subaru_get_code() {
|
2023-05-18 16:16:05 -04:00
|
|
|
// 77777777 88888888 9999
|
|
|
|
return (frame[7] << 12) | (frame[8] << 4) | (frame[9] >> 4);
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::subaru_set_code(const uint16_t code) {
|
2023-05-18 16:16:05 -04:00
|
|
|
frame[7] = (code >> 12) & 0xFF;
|
|
|
|
frame[8] = (code >> 4) & 0xFF;
|
|
|
|
frame[9] &= 0x0F;
|
|
|
|
frame[9] |= (code & 0x0F) << 4;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t KeyfobView::subaru_get_command() {
|
2023-05-18 16:16:05 -04:00
|
|
|
uint32_t command_a = frame[5] & 0x0F;
|
|
|
|
uint32_t command_b = frame[6] & 0x0F;
|
|
|
|
|
|
|
|
if (command_a != command_b)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return command_a;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::subaru_set_command(const uint32_t command) {
|
2023-05-18 16:16:05 -04:00
|
|
|
frame[5] &= 0xF0;
|
|
|
|
frame[5] |= command;
|
|
|
|
frame[6] &= 0xF0;
|
|
|
|
frame[6] |= command;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::generate_payload(size_t& bitstream_length) {
|
2023-05-18 16:16:05 -04:00
|
|
|
for (size_t i = 0; i < (10 * 8); i++) {
|
|
|
|
if (frame[i >> 3] & (1 << (7 - (i & 7))))
|
|
|
|
bitstream_append(bitstream_length, 2, 0b10);
|
|
|
|
else
|
|
|
|
bitstream_append(bitstream_length, 2, 0b01);
|
|
|
|
}
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t KeyfobView::generate_frame() {
|
2023-05-18 16:16:05 -04:00
|
|
|
size_t bitstream_length = 0;
|
|
|
|
uint64_t payload;
|
|
|
|
|
|
|
|
// Symfield word to frame
|
2023-09-12 15:38:19 -04:00
|
|
|
payload = field_payload_a.to_integer();
|
2023-05-18 16:16:05 -04:00
|
|
|
for (size_t i = 0; i < 5; i++) {
|
|
|
|
frame[4 - i] = payload & 0xFF;
|
|
|
|
payload >>= 8;
|
|
|
|
}
|
2023-09-12 15:38:19 -04:00
|
|
|
payload = field_payload_b.to_integer();
|
2023-05-18 16:16:05 -04:00
|
|
|
for (size_t i = 0; i < 5; i++) {
|
|
|
|
frame[9 - i] = payload & 0xFF;
|
|
|
|
payload >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompute checksum
|
|
|
|
frame[9] = (frame[9] & 0xF0) | subaru_get_checksum();
|
|
|
|
update_symfields();
|
|
|
|
|
|
|
|
// Preamble: 128x 01
|
|
|
|
for (size_t i = 0; i < 128; i++)
|
|
|
|
bitstream_append(bitstream_length, 2, 0b01);
|
|
|
|
|
|
|
|
// Space: 4x 0
|
|
|
|
bitstream_append(bitstream_length, 4, 0b0000);
|
|
|
|
|
|
|
|
// Payload
|
|
|
|
generate_payload(bitstream_length);
|
|
|
|
|
|
|
|
// Space: 8x 0
|
|
|
|
bitstream_append(bitstream_length, 8, 0b00000000);
|
|
|
|
|
|
|
|
// Payload again
|
|
|
|
generate_payload(bitstream_length);
|
|
|
|
|
|
|
|
return bitstream_length;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::focus() {
|
2023-05-18 16:16:05 -04:00
|
|
|
options_make.focus();
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyfobView::~KeyfobView() {
|
2023-05-18 16:16:05 -04:00
|
|
|
transmitter_model.disable();
|
2023-06-17 10:54:52 -04:00
|
|
|
baseband::shutdown();
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::update_progress(const uint32_t progress) {
|
2023-05-18 16:16:05 -04:00
|
|
|
text_status.set("Repeat #" + to_string_dec_uint(progress));
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::on_tx_progress(const uint32_t progress, const bool done) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (!done) {
|
|
|
|
// Repeating...
|
|
|
|
update_progress(progress + 1);
|
|
|
|
progressbar.set_value(progress);
|
|
|
|
} else {
|
|
|
|
transmitter_model.disable();
|
|
|
|
text_status.set("Done");
|
|
|
|
progressbar.set_value(0);
|
|
|
|
tx_view.set_transmitting(false);
|
|
|
|
}
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::on_make_change(size_t index) {
|
2023-05-18 16:16:05 -04:00
|
|
|
(void)index;
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
void KeyfobView::update_symfields() {
|
2023-05-18 16:16:05 -04:00
|
|
|
for (size_t i = 0; i < 5; i++) {
|
2023-09-12 15:38:19 -04:00
|
|
|
field_payload_a.set_offset(i << 1, frame[i] >> 4);
|
|
|
|
field_payload_a.set_offset((i << 1) + 1, frame[i] & 0x0F);
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < 5; i++) {
|
2023-09-12 15:38:19 -04:00
|
|
|
field_payload_b.set_offset(i << 1, frame[5 + i] >> 4);
|
|
|
|
field_payload_b.set_offset((i << 1) + 1, frame[5 + i] & 0x0F);
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::on_command_change(uint32_t value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
subaru_set_command(value);
|
|
|
|
update_symfields();
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyfobView::start_tx() {
|
2023-05-18 16:16:05 -04:00
|
|
|
progressbar.set_max(repeats - 1);
|
|
|
|
|
|
|
|
update_progress(1);
|
|
|
|
|
|
|
|
size_t bitstream_length = generate_frame();
|
|
|
|
|
|
|
|
transmitter_model.enable();
|
|
|
|
|
|
|
|
baseband::set_ook_data(
|
|
|
|
bitstream_length,
|
|
|
|
subaru_samples_per_bit,
|
|
|
|
repeats,
|
|
|
|
200 // Pause symbols
|
|
|
|
);
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyfobView::KeyfobView(
|
2023-05-18 16:16:05 -04:00
|
|
|
NavigationView& nav)
|
|
|
|
: nav_{nav} {
|
|
|
|
baseband::run_image(portapack::spi_flash::image_tag_ook);
|
|
|
|
|
|
|
|
add_children({&labels,
|
|
|
|
&options_make,
|
|
|
|
&options_command,
|
|
|
|
&field_payload_a,
|
|
|
|
&field_payload_b,
|
|
|
|
&text_status,
|
|
|
|
&progressbar,
|
|
|
|
&tx_view});
|
|
|
|
|
|
|
|
frame[0] = 0x55;
|
|
|
|
update_symfields();
|
|
|
|
|
|
|
|
options_make.on_change = [this](size_t index, int32_t) {
|
|
|
|
on_make_change(index);
|
|
|
|
};
|
|
|
|
|
|
|
|
options_command.on_change = [this](size_t, int32_t value) {
|
|
|
|
on_command_change(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
options_make.set_selected_index(0);
|
|
|
|
|
|
|
|
tx_view.on_edit_frequency = [this, &nav]() {
|
2023-06-11 14:47:13 -04:00
|
|
|
auto new_view = nav.push<FrequencyKeypadView>(transmitter_model.target_frequency());
|
2023-05-18 16:16:05 -04:00
|
|
|
new_view->on_changed = [this](rf::Frequency f) {
|
2023-06-11 14:47:13 -04:00
|
|
|
transmitter_model.set_target_frequency(f);
|
2023-05-18 16:16:05 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
tx_view.on_start = [this]() {
|
|
|
|
start_tx();
|
|
|
|
tx_view.set_transmitting(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
tx_view.on_stop = [this]() {
|
|
|
|
tx_view.set_transmitting(false);
|
|
|
|
};
|
2017-10-14 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|