bring keyfob app back (#1794)

* bring keyfob app back

* format

* cleanup

* format

* remove committed line from original list
This commit is contained in:
sommermoregentraum
2024-01-21 18:49:17 +08:00
committed by GitHub
parent ce1084abc7
commit 3314001205
6 changed files with 100 additions and 6 deletions

View File

@@ -54,6 +54,11 @@ set(EXTCPPSRC
external/spainter/ui_spectrum_painter.cpp
external/spainter/ui_spectrum_painter_text.cpp
external/spainter/ui_spectrum_painter_image.cpp
#keyfob
external/keyfob/main.cpp
external/keyfob/ui_keyfob.cpp
external/keyfob/ui_keyfob.hpp
)
set(EXTAPPLIST
@@ -70,4 +75,5 @@ set(EXTAPPLIST
jammer
gpssim
spainter
keyfob
)

View File

@@ -30,6 +30,7 @@ MEMORY
ram_external_app_jammer(rwx) : org = 0xEEF30000, len = 32k
ram_external_app_gpssim(rwx) : org = 0xEEF40000, len = 32k
ram_external_app_spainter(rwx) : org = 0xEEF50000, len = 32k
ram_external_app_keyfob(rwx) : org = 0xEEF60000, len = 32k
}
SECTIONS
@@ -116,4 +117,10 @@ SECTIONS
*(*ui*external_app*spainter*);
} > ram_external_app_spainter
.external_app_keyfob : ALIGN(4) SUBALIGN(4)
{
KEEP(*(.external_app.app_keyfob.application_information));
*(*ui*external_app*keyfob*);
} > ram_external_app_keyfob
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2023 Bernd Herzog
*
* 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.hpp"
#include "ui_keyfob.hpp"
#include "ui_navigation.hpp"
#include "external_app.hpp"
namespace ui::external_app::keyfob {
void initialize_app(ui::NavigationView& nav) {
nav.push<KeyfobView>();
}
} // namespace ui::external_app::keyfob
extern "C" {
__attribute__((section(".external_app.app_keyfob.application_information"), used)) application_information_t _application_information_keyfob = {
/*.memory_location = */ (uint8_t*)0x00000000,
/*.externalAppEntry = */ ui::external_app::keyfob::initialize_app,
/*.header_version = */ CURRENT_HEADER_VERSION,
/*.app_version = */ VERSION_MD5,
/*.app_name = */ "Keyfob",
/*.bitmap_data = */ {
0x30,
0x00,
0x30,
0x00,
0x30,
0x00,
0x30,
0x00,
0x30,
0x00,
0x30,
0x00,
0xFC,
0x00,
0xCE,
0x01,
0x86,
0x01,
0xFE,
0x01,
0x86,
0x31,
0x86,
0x49,
0xCE,
0x87,
0xFC,
0x84,
0xFC,
0x4B,
0x78,
0x30,
},
/*.icon_color = */ ui::Color::orange().v,
/*.menu_location = */ app_location_t::TX,
/*.m4_app_tag = portapack::spi_flash::image_tag_keyfob */ {'P', 'O', 'O', 'K'},
/*.m4_app_offset = */ 0x00000000, // will be filled at compile time
};
}

View File

@@ -0,0 +1,242 @@
/*
* 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::external_app::keyfob {
uint8_t KeyfobView::subaru_get_checksum() {
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;
}
bool KeyfobView::subaru_is_valid() {
if (frame[0] != 0x55)
return false;
if (subaru_get_checksum() != (frame[9] & 0x0F))
return false;
return true;
}
uint16_t KeyfobView::subaru_get_code() {
// 77777777 88888888 9999
return (frame[7] << 12) | (frame[8] << 4) | (frame[9] >> 4);
}
void KeyfobView::subaru_set_code(const uint16_t code) {
frame[7] = (code >> 12) & 0xFF;
frame[8] = (code >> 4) & 0xFF;
frame[9] &= 0x0F;
frame[9] |= (code & 0x0F) << 4;
}
int32_t KeyfobView::subaru_get_command() {
uint32_t command_a = frame[5] & 0x0F;
uint32_t command_b = frame[6] & 0x0F;
if (command_a != command_b)
return -1;
return command_a;
}
void KeyfobView::subaru_set_command(const uint32_t command) {
frame[5] &= 0xF0;
frame[5] |= command;
frame[6] &= 0xF0;
frame[6] |= command;
}
void KeyfobView::generate_payload(size_t& bitstream_length) {
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);
}
}
size_t KeyfobView::generate_frame() {
size_t bitstream_length = 0;
uint64_t payload;
// Symfield word to frame
payload = field_payload_a.to_integer();
for (size_t i = 0; i < 5; i++) {
frame[4 - i] = payload & 0xFF;
payload >>= 8;
}
payload = field_payload_b.to_integer();
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;
}
void KeyfobView::focus() {
options_make.focus();
}
KeyfobView::~KeyfobView() {
transmitter_model.disable();
baseband::shutdown();
}
void KeyfobView::update_progress(const uint32_t progress) {
text_status.set("Repeat #" + to_string_dec_uint(progress));
}
void KeyfobView::on_tx_progress(const uint32_t progress, const bool done) {
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);
}
}
void KeyfobView::on_make_change(size_t index) {
(void)index;
}
// DEBUG
void KeyfobView::update_symfields() {
for (size_t i = 0; i < 5; i++) {
field_payload_a.set_offset(i << 1, frame[i] >> 4);
field_payload_a.set_offset((i << 1) + 1, frame[i] & 0x0F);
}
for (size_t i = 0; i < 5; i++) {
field_payload_b.set_offset(i << 1, frame[5 + i] >> 4);
field_payload_b.set_offset((i << 1) + 1, frame[5 + i] & 0x0F);
}
}
void KeyfobView::on_command_change(uint32_t value) {
subaru_set_command(value);
update_symfields();
}
void KeyfobView::start_tx() {
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
);
}
KeyfobView::KeyfobView(
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]() {
auto new_view = nav.push<FrequencyKeypadView>(transmitter_model.target_frequency());
new_view->on_changed = [this](rf::Frequency f) {
transmitter_model.set_target_frequency(f);
};
};
tx_view.on_start = [this]() {
start_tx();
tx_view.set_transmitting(true);
};
tx_view.on_stop = [this]() {
tx_view.set_transmitting(false);
};
}
} /* namespace ui::external_app::keyfob */

View File

@@ -0,0 +1,129 @@
/*
* 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.hpp"
#include "ui_transmitter.hpp"
#include "transmitter_model.hpp"
#include "app_settings.hpp"
#include "radio_state.hpp"
#include "encoders.hpp"
using namespace encoders;
namespace ui::external_app::keyfob {
class KeyfobView : public View {
public:
KeyfobView(NavigationView& nav);
~KeyfobView();
void focus() override;
std::string title() const override { return "Key fob TX"; };
private:
NavigationView& nav_;
TxRadioState radio_state_{
433920000 /* frequency */,
1750000 /* bandwidth */,
OOK_SAMPLERATE /* sampling rate */
};
app_settings::SettingsManager settings_{
"tx_keyfob", app_settings::Mode::TX};
// 1013210ns / bit
static constexpr uint32_t subaru_samples_per_bit = (OOK_SAMPLERATE * 0.00101321);
static constexpr uint32_t repeats = 4;
uint8_t frame[10]{};
void generate_payload(size_t& bitstream_length);
size_t generate_frame();
void start_tx();
void on_tx_progress(const uint32_t progress, const bool done);
void update_progress(const uint32_t progress);
void on_make_change(size_t index);
void on_command_change(uint32_t value);
// DEBUG
void update_symfields();
uint8_t subaru_get_checksum();
bool subaru_is_valid();
uint16_t subaru_get_code();
void subaru_set_code(const uint16_t code);
int32_t subaru_get_command();
void subaru_set_command(const uint32_t command);
Labels labels{
{{5 * 8, 1 * 16}, "Make:", Color::light_grey()},
{{2 * 8, 2 * 16}, "Command:", Color::light_grey()},
{{2 * 8, 4 * 16}, "Payload: #####", Color::light_grey()},
{{2 * 8, 7 * 16}, "Checksum is fixed just", Color::light_grey()},
{{2 * 8, 8 * 16}, "before transmission.", Color::light_grey()},
};
OptionsField options_make{
{10 * 8, 1 * 16},
8,
{{"Subaru", 0}}};
OptionsField options_command{
{10 * 8, 2 * 16},
6,
{{"Lock", 1},
{"Unlock", 2},
{"Trunk", 11},
{"Panic", 10}}};
SymField field_payload_a{
{2 * 8, 5 * 16},
10,
SymField::Type::Hex};
SymField field_payload_b{
{13 * 8, 5 * 16},
10,
SymField::Type::Hex};
Text text_status{
{2 * 8, 13 * 16, 128, 16},
"Ready"};
ProgressBar progressbar{
{2 * 8, 13 * 16 + 20, 208, 16}};
TransmitterView tx_view{
16 * 16,
0,
15,
true};
MessageHandlerRegistration message_handler_tx_progress{
Message::ID::TXProgress,
[this](const Message* const p) {
const auto message = *reinterpret_cast<const TXProgressMessage*>(p);
this->on_tx_progress(message.progress, message.done);
}};
};
} /* namespace ui::external_app::keyfob */