mirror of
https://github.com/D4C1-Labs/Flipper-ARF.git
synced 2026-03-29 16:20:01 +00:00
Compare commits
7 Commits
dev-bfdf60
...
dev-57dafb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57dafbc76d | ||
|
|
e116abaa9b | ||
|
|
fd9564e301 | ||
|
|
de133ebe09 | ||
|
|
fc03342591 | ||
|
|
75a5334a9b | ||
|
|
696041410b |
72
.github/ISSUE_TEMPLATE/01_bug_report.yml
vendored
72
.github/ISSUE_TEMPLATE/01_bug_report.yml
vendored
@@ -1,45 +1,77 @@
|
||||
name: Bug report
|
||||
description: File a bug reports regarding the firmware.
|
||||
name: Bug Report
|
||||
description: Report a bug in Flipper-ARF firmware.
|
||||
labels: ["bug"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for taking the time to fill out an issue, this template is meant for any issues related to the Flipper Zero unleashed firmware.
|
||||
Thanks for reporting a bug in Flipper-ARF. Please fill in as much detail as possible.
|
||||
- type: input
|
||||
id: firmware-version
|
||||
attributes:
|
||||
label: Firmware version
|
||||
description: "ARF version or git commit hash."
|
||||
placeholder: "e.g. ARF 0.1.2 or commit abc1234"
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: hardware
|
||||
attributes:
|
||||
label: Hardware setup
|
||||
description: "Which hardware configuration are you using?"
|
||||
options:
|
||||
- Flipper Zero (stock)
|
||||
- Flipper Zero (modded antenna)
|
||||
- Flipper Zero + external CC1101
|
||||
- Other (describe below)
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: protocol
|
||||
attributes:
|
||||
label: Protocol affected
|
||||
description: "Which protocol is affected, if applicable?"
|
||||
placeholder: "e.g. Kia V3/V4, PSA GROUP, Keeloq, Fiat Mystery"
|
||||
- type: input
|
||||
id: frequency
|
||||
attributes:
|
||||
label: Frequency & modulation
|
||||
description: "RF frequency and modulation used, if relevant."
|
||||
placeholder: "e.g. 433.92 MHz AM"
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Describe the bug.
|
||||
description: "A clear and concise description of what the bug is."
|
||||
label: Bug description
|
||||
description: "A clear and concise description of the bug."
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: repro
|
||||
attributes:
|
||||
label: Reproduction
|
||||
label: Steps to reproduce
|
||||
description: "How can this bug be reproduced?"
|
||||
placeholder: |
|
||||
1. Switch on...
|
||||
2. Press button '....'
|
||||
3. Wait for the moon phase
|
||||
4. It burns
|
||||
1. Open SubGhz app
|
||||
2. Load saved .sub file
|
||||
3. Press Send
|
||||
4. Observe error / unexpected behavior
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: target
|
||||
- type: textarea
|
||||
id: expected
|
||||
attributes:
|
||||
label: Target
|
||||
description: Specify the target
|
||||
# Target seems to be largely ignored by outside sources.
|
||||
label: Expected vs actual behavior
|
||||
description: "What did you expect to happen, and what actually happened?"
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: logs
|
||||
attributes:
|
||||
label: Logs
|
||||
description: Attach your debug logs here
|
||||
label: Logs / screenshots
|
||||
description: "Attach debug logs (via serial CLI) or screenshots if available."
|
||||
render: Text
|
||||
# Avoid rendering as Markdown here.
|
||||
- type: textarea
|
||||
id: anything-else
|
||||
attributes:
|
||||
label: Anything else?
|
||||
description: Let us know if you have anything else to share.
|
||||
label: Additional context
|
||||
description: "Any other information that might help (vehicle model, .sub file contents, etc.)."
|
||||
|
||||
20
.github/ISSUE_TEMPLATE/02_enhancements.yml
vendored
20
.github/ISSUE_TEMPLATE/02_enhancements.yml
vendored
@@ -1,20 +0,0 @@
|
||||
name: Enhancements
|
||||
description: Suggest improvements for any existing functionality within the firmware.
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for taking the time to fill out an issue. This template is meant for feature requests and improvements to already existing functionality.
|
||||
- type: textarea
|
||||
id: proposal
|
||||
attributes:
|
||||
label: "Describe the enhancement you're suggesting."
|
||||
description: |
|
||||
Feel free to describe in as much detail as you wish.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: anything-else
|
||||
attributes:
|
||||
label: Anything else?
|
||||
description: Let us know if you have anything else to share.
|
||||
45
.github/ISSUE_TEMPLATE/03_feature_request.yml
vendored
45
.github/ISSUE_TEMPLATE/03_feature_request.yml
vendored
@@ -1,23 +1,46 @@
|
||||
name: Feature Request
|
||||
description: For feature requests regarding the firmware.
|
||||
description: Suggest a new feature or improvement for Flipper-ARF.
|
||||
labels: ["feature request"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thank you for taking the time to fill out an issue, this template is meant for any feature suggestions.
|
||||
- type: textarea
|
||||
id: proposal
|
||||
Thanks for suggesting a feature for Flipper-ARF. Please describe your idea in detail.
|
||||
- type: dropdown
|
||||
id: category
|
||||
attributes:
|
||||
label: "Description of the feature you're suggesting."
|
||||
description: |
|
||||
Please describe your feature request in as many details as possible.
|
||||
- Describe what it should do.
|
||||
- Note whetever it is to extend existing functionality or introduce new functionality.
|
||||
label: Category
|
||||
description: "What area does this feature fall under?"
|
||||
options:
|
||||
- New protocol
|
||||
- Protocol improvement
|
||||
- UI / UX
|
||||
- Build system / tooling
|
||||
- Other
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: manufacturer
|
||||
attributes:
|
||||
label: Manufacturer / protocol
|
||||
description: "Which manufacturer or protocol is this related to, if applicable?"
|
||||
placeholder: "e.g. Toyota, Renault, Keeloq"
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Description
|
||||
description: "Describe the feature you're suggesting."
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: use-case
|
||||
attributes:
|
||||
label: Use case
|
||||
description: "Why is this needed? What problem does it solve?"
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: anything-else
|
||||
attributes:
|
||||
label: Anything else?
|
||||
description: Let us know if you have anything else to share.
|
||||
label: Additional context
|
||||
description: "Any references, datasheets, links, or examples that support this request."
|
||||
|
||||
111
.github/ISSUE_TEMPLATE/04_protocol_submission.yml
vendored
Normal file
111
.github/ISSUE_TEMPLATE/04_protocol_submission.yml
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
name: Protocol / Algorithm Submission
|
||||
description: Submit a new protocol decoder, encoder, or cipher implementation.
|
||||
labels: ["protocol", "contribution"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Use this template to submit a new protocol implementation or algorithm for inclusion in Flipper-ARF.
|
||||
Include as much technical detail as possible — timing, frame structure, cipher type, and test captures.
|
||||
- type: input
|
||||
id: protocol-name
|
||||
attributes:
|
||||
label: Protocol name
|
||||
description: "Name for the protocol (as it should appear in the firmware)."
|
||||
placeholder: "e.g. Renault V2, Opel Corsa, Nissan V0"
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: manufacturer
|
||||
attributes:
|
||||
label: Manufacturer / vehicle
|
||||
description: "Which manufacturer or vehicles use this protocol?"
|
||||
placeholder: "e.g. Renault Clio 2010-2018, Opel/Vauxhall Corsa D"
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: frequency
|
||||
attributes:
|
||||
label: Frequency & modulation
|
||||
description: "RF frequency and modulation type."
|
||||
placeholder: "e.g. 433.92 MHz FM (FSK)"
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: encoding
|
||||
attributes:
|
||||
label: Encoding
|
||||
description: "How are bits encoded in the RF signal?"
|
||||
options:
|
||||
- PWM (Pulse Width Modulation)
|
||||
- Manchester
|
||||
- Differential Manchester
|
||||
- OOK raw
|
||||
- Other (describe in frame structure)
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: timing
|
||||
attributes:
|
||||
label: Timing parameters
|
||||
description: "Provide timing values for the protocol."
|
||||
placeholder: |
|
||||
te_short: 400 us
|
||||
te_long: 800 us
|
||||
te_delta: 150 us
|
||||
Preamble: 16 pairs of alternating short pulses
|
||||
Sync: 1200 us HIGH
|
||||
Gap: 10000 us between bursts
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: frame-structure
|
||||
attributes:
|
||||
label: Frame structure
|
||||
description: "Describe the bit layout — field positions, sizes, fixed vs rolling parts."
|
||||
placeholder: |
|
||||
Total bits: 68
|
||||
Bits 0-31: Encrypted (KeeLoq)
|
||||
Bits 32-59: Serial (28 bits)
|
||||
Bits 60-63: Button code (4 bits)
|
||||
Bits 64-67: CRC (4 bits, XOR of nibbles)
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: cipher
|
||||
attributes:
|
||||
label: Cipher / rolling code type
|
||||
description: "What cipher or rolling code scheme does this protocol use?"
|
||||
options:
|
||||
- None (static code)
|
||||
- KeeLoq
|
||||
- AES
|
||||
- TEA / XTEA
|
||||
- Hitag2
|
||||
- Custom / proprietary
|
||||
- Unknown (needs analysis)
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: status
|
||||
attributes:
|
||||
label: Implementation status
|
||||
description: "How far along is the implementation?"
|
||||
options:
|
||||
- Concept only (analysis / documentation)
|
||||
- Decoder working
|
||||
- Encoder working
|
||||
- Both decoder and encoder working
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: captures
|
||||
attributes:
|
||||
label: Test captures
|
||||
description: "Paste .sub file contents or raw pulse data for validation. Attach files if too large."
|
||||
render: Text
|
||||
- type: textarea
|
||||
id: references
|
||||
attributes:
|
||||
label: References
|
||||
description: "Links to datasheets, research papers, FCC filings, or related projects."
|
||||
99
.github/ISSUE_TEMPLATE/05_key_recording_submission.yml
vendored
Normal file
99
.github/ISSUE_TEMPLATE/05_key_recording_submission.yml
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
name: Key Recording Submission
|
||||
description: Contribute captured keyfob recordings for protocol analysis.
|
||||
labels: ["recording", "data"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Use this template to submit captured keyfob recordings (.sub files or raw data).
|
||||
These recordings help with protocol reverse engineering, decoder validation, and cipher analysis.
|
||||
|
||||
**Tips for useful captures:**
|
||||
- Record 10+ sequential presses per button without long gaps
|
||||
- Note the exact button pressed for each capture
|
||||
- If possible, capture from multiple buttons on the same fob
|
||||
- Include the vehicle make, model, and year
|
||||
- type: input
|
||||
id: vehicle
|
||||
attributes:
|
||||
label: Vehicle / device
|
||||
description: "Make, model, year, and any relevant trim info."
|
||||
placeholder: "e.g. 2015 Fiat Panda 1.2 Pop"
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: protocol
|
||||
attributes:
|
||||
label: Protocol (if known)
|
||||
description: "Which protocol was detected, or select Unknown if not yet identified."
|
||||
options:
|
||||
- Unknown / new protocol
|
||||
- VAG GROUP
|
||||
- Cayenne
|
||||
- PSA GROUP
|
||||
- Ford V0
|
||||
- Fiat SpA
|
||||
- Fiat Mystery
|
||||
- Subaru
|
||||
- Siemens (Mazda)
|
||||
- Kia V0
|
||||
- Kia V1
|
||||
- Kia V2
|
||||
- Kia V3/V4
|
||||
- Kia V5
|
||||
- Kia V6
|
||||
- Suzuki
|
||||
- Mitsubishi V0
|
||||
- Keeloq
|
||||
- Other (specify below)
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: frequency
|
||||
attributes:
|
||||
label: Frequency & modulation used
|
||||
description: "The frequency and modulation setting used during capture."
|
||||
placeholder: "e.g. 433.92 MHz AM650"
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: buttons
|
||||
attributes:
|
||||
label: Button / function
|
||||
description: "Which buttons were recorded and what they do."
|
||||
placeholder: "e.g. Lock (Btn A), Unlock (Btn B), Trunk (Btn C)"
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: num-captures
|
||||
attributes:
|
||||
label: Number of captures
|
||||
description: "How many presses were recorded per button?"
|
||||
placeholder: "e.g. 10 sequential presses per button"
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: capture-method
|
||||
attributes:
|
||||
label: Capture method
|
||||
description: "How were the signals captured?"
|
||||
options:
|
||||
- SubGhz Read RAW
|
||||
- SubGhz decoded (saved .sub)
|
||||
- External SDR (HackRF, RTL-SDR, etc.)
|
||||
- Other
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: capture-data
|
||||
attributes:
|
||||
label: Capture data
|
||||
description: "Paste .sub file contents here, or attach files. For multiple files, use separate code blocks labeled by button."
|
||||
render: Text
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: notes
|
||||
attributes:
|
||||
label: Notes
|
||||
description: "Any observations — counter gaps, time between captures, battery changes, multiple fobs, etc."
|
||||
7
.github/ISSUE_TEMPLATE/config.yml
vendored
7
.github/ISSUE_TEMPLATE/config.yml
vendored
@@ -1,8 +1 @@
|
||||
blank_issues_enabled: true
|
||||
contact_links:
|
||||
- name: Telegram
|
||||
url: https://t.me/flipperzero_unofficial
|
||||
about: Unofficial Telegram chat
|
||||
- name: Discord
|
||||
url: https://discord.unleashedflip.com
|
||||
about: Unofficial Discord Community
|
||||
|
||||
28
.github/pull_request_template.md
vendored
28
.github/pull_request_template.md
vendored
@@ -1,13 +1,25 @@
|
||||
# What's new
|
||||
## Summary
|
||||
|
||||
- [ Describe changes here ]
|
||||
<!-- What changed and why? Keep it concise. -->
|
||||
|
||||
# Verification
|
||||
## Protocol(s) affected
|
||||
|
||||
- [ Describe how to verify changes ]
|
||||
<!-- Which protocol(s) does this PR touch? e.g. Kia V3/V4, PSA GROUP, none -->
|
||||
|
||||
# Checklist (For Reviewer)
|
||||
## Type of change
|
||||
|
||||
- [ ] PR has description of feature/bug
|
||||
- [ ] Description contains actions to verify feature/bugfix
|
||||
- [ ] I've built this code, uploaded it to the device and verified feature/bugfix
|
||||
- [ ] Bug fix
|
||||
- [ ] New protocol
|
||||
- [ ] Protocol improvement (encoder/decoder/display)
|
||||
- [ ] Build system / infrastructure
|
||||
- [ ] Other
|
||||
|
||||
## Testing
|
||||
|
||||
<!-- How was this verified? Include hardware used, captures tested, etc. -->
|
||||
|
||||
## Checklist
|
||||
|
||||
- [ ] Built with `./fbt COMPACT=1 DEBUG=0 updater_package` (no errors)
|
||||
- [ ] Flashed and tested on Flipper Zero
|
||||
- [ ] No regressions in other protocols
|
||||
|
||||
51
README.md
51
README.md
@@ -123,14 +123,14 @@ Flipper-ARF aims to achieve:
|
||||
- [x] D-Pad mapping (Lock / Unlock / Boot / Trunk) during emulation
|
||||
- [x] VAG MFKey support and updated Keeloq codes
|
||||
- [x] PSA XTEA brute force for saved → emulation workflow
|
||||
- [x] Brute force of counter in saved → emulation scene for smoother keyfob emulation
|
||||
- [x] Brute force of counter in saved → can be accellerated trough the companion app via bluetooth
|
||||
- [x] RollJam app (Internal CC1101 for RX & TX captured signal; External CC1101 for jamming) — requires more real-world testing
|
||||
|
||||
---
|
||||
|
||||
## To Do / Planned Features
|
||||
|
||||
- [ ] Keeloq Key Manager inside firmware
|
||||
- [X] Keeloq Key Manager inside firmware
|
||||
- [ ] Add Scher Khan & Starline protocols
|
||||
- [ ] Fix and reintegrate RollJam app (future updates)
|
||||
- [ ] Expand and refine Subaru, Kia, PSA, and other manufacturer protocols
|
||||
@@ -178,6 +178,53 @@ Contributions are welcome if they:
|
||||

|
||||
---
|
||||
|
||||
## Citations & References
|
||||
|
||||
The following academic publications have been invaluable to the development and understanding of the protocols implemented in this firmware.
|
||||
|
||||
### Automotive RKE Security
|
||||
|
||||
- **Lock It and Still Lose It — On the (In)Security of Automotive Remote Keyless Entry Systems**
|
||||
Flavio D. Garcia, David Oswald, Timo Kasper, Pierre Pavlidès
|
||||
*USENIX Security 2016*
|
||||
https://www.usenix.org/system/files/conference/usenixsecurity16/sec16_paper_garcia.pdf
|
||||
|
||||
- **Clonable Key Fobs: Analyzing and Breaking RKE Protocols**
|
||||
Roberto Gesteira-Miñarro, Gregorio López, Rafael Palacios
|
||||
*International Journal of Information Security, Springer, May 2025, 24(3)*
|
||||
DOI: [10.1007/s10207-025-01063-7](https://doi.org/10.1007/s10207-025-01063-7)
|
||||
|
||||
- **The Role of Cryptographic Techniques in Remote Keyless Entry (RKE) Systems**
|
||||
Jananga Chiran — Sri Lanka Institute of Information Technology
|
||||
*November 2023*
|
||||
DOI: [10.5281/zenodo.14677864](https://doi.org/10.5281/zenodo.14677864)
|
||||
|
||||
### Immobiliser & Transponder Systems
|
||||
|
||||
- **Dismantling DST80-based Immobiliser Systems**
|
||||
Lennert Wouters, Jan Van den Herrewegen, Flavio D. Garcia, David Oswald, Benedikt Gierlichs, Bart Preneel
|
||||
*IACR Transactions on Cryptographic Hardware and Embedded Systems (TCHES), 2020, Vol. 2*
|
||||
DOI: [10.13154/tches.v2020.i2.99-127](https://doi.org/10.13154/tches.v2020.i2.99-127)
|
||||
|
||||
### RFID & Protocol Analysis Tooling
|
||||
|
||||
- **A Toolbox for RFID Protocol Analysis**
|
||||
Flavio D. Garcia
|
||||
*IEEE International Conference on RFID, 2012*
|
||||
DOI: [10.1109/rfid.2012.19](https://doi.org/10.1109/rfid.2012.19)
|
||||
|
||||
### Relay & Replay Attacks
|
||||
|
||||
- **Implementing and Testing RollJam on Software-Defined Radios**
|
||||
*Università di Bologna (UNIBO), CRIS*
|
||||
https://cris.unibo.it/handle/11585/999874
|
||||
|
||||
- **Enhanced Vehicular Roll-Jam Attack Using a Known Noise Source**
|
||||
*Inaugural International Symposium on Vehicle Security & Privacy, January 2023*
|
||||
DOI: [10.14722/vehiclesec.2023.23037](https://doi.org/10.14722/vehiclesec.2023.23037)
|
||||
|
||||
---
|
||||
|
||||
# Disclaimer
|
||||
|
||||
This project is provided solely for **educational, research, and interoperability purposes**.
|
||||
|
||||
@@ -1,27 +1,13 @@
|
||||
#include "kia_v0.h"
|
||||
|
||||
#include "../blocks/const.h"
|
||||
#include "../blocks/decoder.h"
|
||||
#include "../blocks/encoder.h"
|
||||
#include "../blocks/generic.h"
|
||||
#include "../blocks/math.h"
|
||||
#include "../blocks/custom_btn_i.h"
|
||||
|
||||
static const char* kia_v0_btn_name(uint8_t btn) {
|
||||
if(btn == 0x01) return "Lock";
|
||||
if(btn == 0x02) return "Unlock";
|
||||
if(btn == 0x03) return "Boot";
|
||||
return "?";
|
||||
}
|
||||
|
||||
static uint8_t kia_v0_get_btn_code() {
|
||||
uint8_t custom_btn = subghz_custom_btn_get();
|
||||
uint8_t original_btn = subghz_custom_btn_get_original();
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_OK) return original_btn;
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_UP) return 0x01; // Lock
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_DOWN) return 0x02; // Unlock
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_LEFT) return 0x03; // Boot/Trunk
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_RIGHT) return 0x03; // Boot/Trunk
|
||||
return original_btn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define TAG "KiaProtocolV0"
|
||||
#define TAG "SubGhzProtocolKiaV0"
|
||||
|
||||
static const SubGhzBlockConst subghz_protocol_kia_const = {
|
||||
.te_short = 250,
|
||||
@@ -30,25 +16,20 @@ static const SubGhzBlockConst subghz_protocol_kia_const = {
|
||||
.min_count_bit_for_found = 61,
|
||||
};
|
||||
|
||||
// Multi-burst configuration
|
||||
#define KIA_TOTAL_BURSTS 2
|
||||
#define KIA_INTER_BURST_GAP_US 25000
|
||||
|
||||
struct SubGhzProtocolDecoderKIA {
|
||||
SubGhzProtocolDecoderBase base;
|
||||
|
||||
SubGhzBlockDecoder decoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
|
||||
uint16_t header_count;
|
||||
};
|
||||
|
||||
struct SubGhzProtocolEncoderKIA {
|
||||
SubGhzProtocolEncoderBase base;
|
||||
|
||||
SubGhzProtocolBlockEncoder encoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
|
||||
uint32_t serial;
|
||||
uint8_t button;
|
||||
uint16_t counter;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
@@ -58,19 +39,13 @@ typedef enum {
|
||||
KIADecoderStepCheckDuration,
|
||||
} KIADecoderStep;
|
||||
|
||||
// Forward declarations for encoder
|
||||
void* subghz_protocol_encoder_kia_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_kia_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_kia_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_kia_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_kia_yield(void* context);
|
||||
|
||||
const SubGhzProtocolDecoder subghz_protocol_kia_decoder = {
|
||||
.alloc = subghz_protocol_decoder_kia_alloc,
|
||||
.free = subghz_protocol_decoder_kia_free,
|
||||
|
||||
.feed = subghz_protocol_decoder_kia_feed,
|
||||
.reset = subghz_protocol_decoder_kia_reset,
|
||||
|
||||
.get_hash_data = subghz_protocol_decoder_kia_get_hash_data,
|
||||
.serialize = subghz_protocol_decoder_kia_serialize,
|
||||
.deserialize = subghz_protocol_decoder_kia_deserialize,
|
||||
@@ -80,16 +55,17 @@ const SubGhzProtocolDecoder subghz_protocol_kia_decoder = {
|
||||
const SubGhzProtocolEncoder subghz_protocol_kia_encoder = {
|
||||
.alloc = subghz_protocol_encoder_kia_alloc,
|
||||
.free = subghz_protocol_encoder_kia_free,
|
||||
|
||||
.deserialize = subghz_protocol_encoder_kia_deserialize,
|
||||
.stop = subghz_protocol_encoder_kia_stop,
|
||||
.yield = subghz_protocol_encoder_kia_yield,
|
||||
};
|
||||
|
||||
const SubGhzProtocol subghz_protocol_kia_v0 = {
|
||||
.name = KIA_PROTOCOL_V0_NAME,
|
||||
.name = SUBGHZ_PROTOCOL_KIA_V0_NAME,
|
||||
.type = SubGhzProtocolTypeDynamic,
|
||||
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable |
|
||||
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
|
||||
|
||||
.decoder = &subghz_protocol_kia_decoder,
|
||||
.encoder = &subghz_protocol_kia_encoder,
|
||||
};
|
||||
@@ -101,7 +77,7 @@ const SubGhzProtocol subghz_protocol_kia_v0 = {
|
||||
* MSB-first processing
|
||||
*/
|
||||
static uint8_t kia_crc8(uint8_t* data, size_t len) {
|
||||
uint8_t crc = 0;
|
||||
uint8_t crc = 0x00;
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
crc ^= data[i];
|
||||
for(size_t j = 0; j < 8; j++) {
|
||||
@@ -136,14 +112,6 @@ static uint8_t kia_calculate_crc(uint64_t data) {
|
||||
static bool kia_verify_crc(uint64_t data) {
|
||||
uint8_t received_crc = data & 0xFF;
|
||||
uint8_t calculated_crc = kia_calculate_crc(data);
|
||||
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"CRC Check - Received: 0x%02X, Calculated: 0x%02X, Match: %s",
|
||||
received_crc,
|
||||
calculated_crc,
|
||||
(received_crc == calculated_crc) ? "YES" : "NO");
|
||||
|
||||
return (received_crc == calculated_crc);
|
||||
}
|
||||
|
||||
@@ -155,386 +123,250 @@ void* subghz_protocol_encoder_kia_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderKIA* instance = malloc(sizeof(SubGhzProtocolEncoderKIA));
|
||||
instance->base.protocol = &subghz_protocol_kia_v0;
|
||||
instance->serial = 0;
|
||||
instance->button = 0;
|
||||
instance->counter = 0;
|
||||
|
||||
instance->encoder.size_upload = (32 + 2 + 118 + 1) * KIA_TOTAL_BURSTS + (KIA_TOTAL_BURSTS - 1);
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
instance->encoder.size_upload = 848;
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
instance->encoder.repeat =
|
||||
10; // High repeat count for continuous transmission while button is held
|
||||
instance->encoder.front = 0;
|
||||
instance->encoder.repeat = 1;
|
||||
instance->encoder.is_running = false;
|
||||
|
||||
FURI_LOG_I(TAG, "Encoder allocated at %p", instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_kia_free(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
if(instance->encoder.upload) {
|
||||
free(instance->encoder.upload);
|
||||
}
|
||||
free(instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild the 61-bit data packet with current button/counter values and recalculate CRC
|
||||
*/
|
||||
static void subghz_protocol_encoder_kia_update_data(SubGhzProtocolEncoderKIA* instance) {
|
||||
uint64_t data = 0;
|
||||
|
||||
// Bits 56-59: Preserve from original capture
|
||||
data |= (instance->generic.data & 0x0F00000000000000ULL);
|
||||
|
||||
// Bits 40-55: Counter (16 bits)
|
||||
data |= ((uint64_t)(instance->counter & 0xFFFF) << 40);
|
||||
|
||||
// Bits 12-39: Serial (28 bits)
|
||||
data |= ((uint64_t)(instance->serial & 0x0FFFFFFF) << 12);
|
||||
|
||||
// Bits 8-11: Button (4 bits)
|
||||
data |= ((uint64_t)(instance->button & 0x0F) << 8);
|
||||
|
||||
// Bits 0-7: Calculate and set CRC
|
||||
uint8_t crc = kia_calculate_crc(data);
|
||||
data |= crc;
|
||||
|
||||
instance->generic.data = data;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Data updated - Serial: 0x%07lX, Btn: 0x%X, Cnt: 0x%04X, CRC: 0x%02X",
|
||||
instance->serial,
|
||||
instance->button,
|
||||
instance->counter,
|
||||
crc);
|
||||
FURI_LOG_I(TAG, "Full data: 0x%016llX", instance->generic.data);
|
||||
}
|
||||
|
||||
static void subghz_protocol_encoder_kia_get_upload(SubGhzProtocolEncoderKIA* instance) {
|
||||
furi_check(instance);
|
||||
size_t index = 0;
|
||||
|
||||
for(uint8_t burst = 0; burst < KIA_TOTAL_BURSTS; burst++) {
|
||||
if(burst > 0) {
|
||||
instance->encoder.upload[index++] = level_duration_make(false, KIA_INTER_BURST_GAP_US);
|
||||
}
|
||||
|
||||
for(int i = 0; i < 32; i++) {
|
||||
bool is_high = (i % 2) == 0;
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(is_high, subghz_protocol_kia_const.te_short);
|
||||
}
|
||||
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, subghz_protocol_kia_const.te_long);
|
||||
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, subghz_protocol_kia_const.te_long);
|
||||
|
||||
for(uint8_t bit_num = 0; bit_num < 59; bit_num++) {
|
||||
uint64_t bit_mask = 1ULL << (58 - bit_num);
|
||||
uint8_t current_bit = (instance->generic.data & bit_mask) ? 1 : 0;
|
||||
|
||||
uint32_t duration = current_bit ? subghz_protocol_kia_const.te_long :
|
||||
subghz_protocol_kia_const.te_short;
|
||||
|
||||
instance->encoder.upload[index++] = level_duration_make(true, duration);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, duration);
|
||||
}
|
||||
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, subghz_protocol_kia_const.te_long * 2);
|
||||
}
|
||||
|
||||
instance->encoder.size_upload = index;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Upload built: %d bursts, size_upload=%zu, data_count_bit=%u, data=0x%016llX",
|
||||
KIA_TOTAL_BURSTS,
|
||||
instance->encoder.size_upload,
|
||||
instance->generic.data_count_bit,
|
||||
instance->generic.data);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
|
||||
flipper_format_rewind(flipper_format);
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
instance->encoder.repeat = 10;
|
||||
|
||||
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
|
||||
do {
|
||||
// Read protocol name and validate
|
||||
FuriString* temp_str = furi_string_alloc();
|
||||
if(!flipper_format_read_string(flipper_format, "Protocol", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Missing Protocol");
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Protocol: %s", furi_string_get_cstr(temp_str));
|
||||
|
||||
if(!furi_string_equal(temp_str, instance->base.protocol->name)) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Wrong protocol %s != %s",
|
||||
furi_string_get_cstr(temp_str),
|
||||
instance->base.protocol->name);
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
furi_string_free(temp_str);
|
||||
|
||||
// Read bit count
|
||||
uint32_t bit_count_temp;
|
||||
if(!flipper_format_read_uint32(flipper_format, "Bit", &bit_count_temp, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing Bit");
|
||||
break;
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Bit count read: %lu", bit_count_temp);
|
||||
|
||||
// Always use 61 bits for Kia V0
|
||||
instance->generic.data_count_bit = 61;
|
||||
|
||||
// Read key data
|
||||
temp_str = furi_string_alloc();
|
||||
if(!flipper_format_read_string(flipper_format, "Key", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Missing Key");
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
const char* key_str = furi_string_get_cstr(temp_str);
|
||||
FURI_LOG_I(TAG, "Key string: %s", key_str);
|
||||
|
||||
// Manual hex parsing
|
||||
uint64_t key = 0;
|
||||
size_t str_len = strlen(key_str);
|
||||
|
||||
size_t hex_pos = 0;
|
||||
for(size_t i = 0; i < str_len && hex_pos < 16; i++) {
|
||||
char c = key_str[i];
|
||||
if(c == ' ') continue;
|
||||
|
||||
uint8_t nibble;
|
||||
if(c >= '0' && c <= '9') {
|
||||
nibble = c - '0';
|
||||
} else if(c >= 'A' && c <= 'F') {
|
||||
nibble = c - 'A' + 10;
|
||||
} else if(c >= 'a' && c <= 'f') {
|
||||
nibble = c - 'a' + 10;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Invalid hex character: %c", c);
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
key = (key << 4) | nibble;
|
||||
hex_pos++;
|
||||
}
|
||||
|
||||
furi_string_free(temp_str);
|
||||
|
||||
if(hex_pos != 16) {
|
||||
FURI_LOG_E(TAG, "Invalid key length: %zu nibbles (expected 16)", hex_pos);
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data = key;
|
||||
FURI_LOG_I(TAG, "Parsed key: 0x%016llX", instance->generic.data);
|
||||
|
||||
if(instance->generic.data == 0) {
|
||||
FURI_LOG_E(TAG, "Key is zero after parsing!");
|
||||
break;
|
||||
}
|
||||
|
||||
// Verify CRC of the captured data
|
||||
if(!kia_verify_crc(key)) {
|
||||
FURI_LOG_W(TAG, "CRC mismatch in captured data - signal may be corrupted");
|
||||
}
|
||||
|
||||
// Read or extract serial
|
||||
if(!flipper_format_read_uint32(flipper_format, "Serial", &instance->serial, 1)) {
|
||||
instance->serial = (uint32_t)((key >> 12) & 0x0FFFFFFF);
|
||||
FURI_LOG_I(TAG, "Extracted serial: 0x%08lX", instance->serial);
|
||||
} else {
|
||||
FURI_LOG_I(TAG, "Read serial: 0x%08lX", instance->serial);
|
||||
}
|
||||
|
||||
// Read or extract button
|
||||
uint32_t btn_temp;
|
||||
if(flipper_format_read_uint32(flipper_format, "Btn", &btn_temp, 1)) {
|
||||
instance->button = (uint8_t)btn_temp;
|
||||
} else {
|
||||
instance->button = (key >> 8) & 0x0F;
|
||||
}
|
||||
subghz_custom_btn_set_original(instance->button);
|
||||
subghz_custom_btn_set_max(4);
|
||||
instance->button = kia_v0_get_btn_code();
|
||||
|
||||
// Read or extract counter
|
||||
uint32_t cnt_temp;
|
||||
if(flipper_format_read_uint32(flipper_format, "Cnt", &cnt_temp, 1)) {
|
||||
instance->counter = (uint16_t)cnt_temp;
|
||||
FURI_LOG_I(TAG, "Read counter: 0x%04X", instance->counter);
|
||||
} else {
|
||||
instance->counter = (key >> 40) & 0xFFFF;
|
||||
FURI_LOG_I(TAG, "Extracted counter: 0x%04X", instance->counter);
|
||||
}
|
||||
|
||||
// Rebuild data with CRC recalculation
|
||||
subghz_protocol_encoder_kia_update_data(instance);
|
||||
|
||||
if(!flipper_format_read_uint32(
|
||||
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1)) {
|
||||
instance->encoder.repeat = 10;
|
||||
FURI_LOG_D(
|
||||
TAG, "Repeat not found in file, using default 10 for continuous transmission");
|
||||
}
|
||||
|
||||
subghz_protocol_encoder_kia_get_upload(instance);
|
||||
|
||||
instance->encoder.is_running = true;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
if(instance->generic.data == 0) {
|
||||
FURI_LOG_E(TAG, "Warning: data is 0!");
|
||||
}
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Encoder initialized - will send %d bursts, repeat=%u, front=%zu",
|
||||
KIA_TOTAL_BURSTS,
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.front);
|
||||
FURI_LOG_I(TAG, "Final data to transmit: 0x%016llX", instance->generic.data);
|
||||
res = SubGhzProtocolStatusOk;
|
||||
} while(false);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_kia_stop(void* context) {
|
||||
if(!context) return;
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
}
|
||||
|
||||
LevelDuration subghz_protocol_encoder_kia_yield(void* context) {
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
|
||||
if(!instance || !instance->encoder.upload || instance->encoder.repeat == 0 ||
|
||||
!instance->encoder.is_running) {
|
||||
if(instance) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Encoder yield stopped: repeat=%u, is_running=%d, upload=%p",
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.is_running,
|
||||
instance->encoder.upload);
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
return level_duration_reset();
|
||||
}
|
||||
|
||||
if(instance->encoder.front >= instance->encoder.size_upload) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Encoder front out of bounds: %zu >= %zu",
|
||||
instance->encoder.front,
|
||||
instance->encoder.size_upload);
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
return level_duration_reset();
|
||||
}
|
||||
|
||||
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
|
||||
|
||||
if(instance->encoder.front < 5 || instance->encoder.front == 0) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Encoder yield[%zu]: repeat=%u, size=%zu, level=%d, duration=%lu",
|
||||
instance->encoder.front,
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.size_upload,
|
||||
level_duration_get_level(ret),
|
||||
level_duration_get_duration(ret));
|
||||
}
|
||||
|
||||
if(++instance->encoder.front == instance->encoder.size_upload) {
|
||||
instance->encoder.repeat--;
|
||||
instance->encoder.front = 0;
|
||||
FURI_LOG_I(
|
||||
TAG, "Encoder completed one cycle, remaining repeat=%u", instance->encoder.repeat);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set button value and recalculate CRC
|
||||
* Analysis of received data
|
||||
* @param instance Pointer to a SubGhzBlockGeneric* instance
|
||||
*/
|
||||
static void subghz_protocol_kia_check_remote_controller(SubGhzBlockGeneric* instance);
|
||||
|
||||
/**
|
||||
* Generating an upload from data.
|
||||
* @param instance Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @return true On success
|
||||
*/
|
||||
static bool subghz_protocol_encoder_kia_get_upload(SubGhzProtocolEncoderKIA* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
// Save original button
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
size_t index = 0;
|
||||
size_t size_upload = (instance->generic.data_count_bit * 2 + 32) * 2 + 540;
|
||||
if(size_upload > instance->encoder.size_upload) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Size upload exceeds allocated encoder buffer. %i",
|
||||
instance->generic.data_count_bit);
|
||||
return false;
|
||||
} else {
|
||||
instance->encoder.size_upload = size_upload;
|
||||
}
|
||||
|
||||
// Counter increment logic
|
||||
if(instance->generic.cnt < 0xFFFF) {
|
||||
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xFFFF) {
|
||||
instance->generic.cnt = 0;
|
||||
} else {
|
||||
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
|
||||
}
|
||||
} else if(instance->generic.cnt >= 0xFFFF) {
|
||||
instance->generic.cnt = 0;
|
||||
}
|
||||
|
||||
// Get button (custom or original)
|
||||
// This allows button changing with directional keys in SubGhz app
|
||||
uint8_t btn = subghz_custom_btn_get() == SUBGHZ_CUSTOM_BTN_OK ?
|
||||
subghz_custom_btn_get_original() :
|
||||
subghz_custom_btn_get();
|
||||
|
||||
// Update the generic button value for potential button changes
|
||||
instance->generic.btn = btn;
|
||||
|
||||
// Build data packet
|
||||
uint64_t data = 0;
|
||||
|
||||
// Bits 56-59: Fixed preamble (0x0F)
|
||||
data |= ((uint64_t)(0x0F) << 56);
|
||||
|
||||
// Bits 40-55: Counter (16 bits)
|
||||
data |= ((uint64_t)(instance->generic.cnt & 0xFFFF) << 40);
|
||||
|
||||
// Bits 12-39: Serial (28 bits)
|
||||
data |= ((uint64_t)(instance->generic.serial & 0x0FFFFFFF) << 12);
|
||||
|
||||
// Bits 8-11: Button (4 bits)
|
||||
data |= ((uint64_t)(btn & 0x0F) << 8);
|
||||
|
||||
// Bits 0-7: CRC
|
||||
uint8_t crc = kia_calculate_crc(data);
|
||||
data |= crc;
|
||||
|
||||
instance->generic.data = data;
|
||||
|
||||
// Send header (270 pulses of te_short)
|
||||
for(uint16_t i = 270; i > 0; i--) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_const.te_short);
|
||||
}
|
||||
|
||||
// Send 2 data bursts
|
||||
for(uint8_t h = 2; h > 0; h--) {
|
||||
// Send sync bits (15 pulses of te_short)
|
||||
for(uint8_t i = 15; i > 0; i--) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_const.te_short);
|
||||
}
|
||||
|
||||
// Send data bits (PWM encoding)
|
||||
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
|
||||
if(bit_read(instance->generic.data, i - 1)) {
|
||||
// Send bit 1: long pulse
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_const.te_long);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_const.te_long);
|
||||
} else {
|
||||
// Send bit 0: short pulse
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_const.te_short);
|
||||
}
|
||||
}
|
||||
|
||||
// Send stop bit (3x te_long)
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_const.te_long * 3);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_const.te_long * 3);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
do {
|
||||
ret = subghz_block_generic_deserialize_check_count_bit(
|
||||
&instance->generic,
|
||||
flipper_format,
|
||||
subghz_protocol_kia_const.min_count_bit_for_found);
|
||||
|
||||
if(ret != SubGhzProtocolStatusOk) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Extract serial, button, counter from data
|
||||
subghz_protocol_kia_check_remote_controller(&instance->generic);
|
||||
|
||||
// Verify CRC
|
||||
if(!kia_verify_crc(instance->generic.data)) {
|
||||
FURI_LOG_W(TAG, "CRC mismatch in loaded file");
|
||||
ret = SubGhzProtocolStatusErrorParserOthers;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!subghz_protocol_encoder_kia_get_upload(instance)) {
|
||||
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
|
||||
break;
|
||||
}
|
||||
|
||||
// Update the Key in the file with the new counter/button/CRC
|
||||
if(!flipper_format_rewind(flipper_format)) {
|
||||
FURI_LOG_E(TAG, "Rewind error");
|
||||
ret = SubGhzProtocolStatusErrorParserOthers;
|
||||
break;
|
||||
}
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
for(size_t i = 0; i < sizeof(uint64_t); i++) {
|
||||
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF;
|
||||
}
|
||||
if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(TAG, "Unable to update Key");
|
||||
ret = SubGhzProtocolStatusErrorParserKey;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->encoder.is_running = true;
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ENCODER HELPER FUNCTIONS
|
||||
// ============================================================================
|
||||
|
||||
void subghz_protocol_encoder_kia_set_button(void* context, uint8_t button) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
instance->button = button & 0x0F;
|
||||
subghz_protocol_encoder_kia_update_data(instance);
|
||||
subghz_protocol_encoder_kia_get_upload(instance);
|
||||
FURI_LOG_I(TAG, "Button set to 0x%X, upload rebuilt with new CRC", instance->button);
|
||||
instance->generic.btn = button & 0x0F;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set counter value and recalculate CRC
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_set_counter(void* context, uint16_t counter) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
instance->counter = counter;
|
||||
subghz_protocol_encoder_kia_update_data(instance);
|
||||
subghz_protocol_encoder_kia_get_upload(instance);
|
||||
FURI_LOG_I(TAG, "Counter set to 0x%04X, upload rebuilt with new CRC", instance->counter);
|
||||
instance->generic.cnt = counter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment counter and recalculate CRC
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_increment_counter(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
instance->counter++;
|
||||
subghz_protocol_encoder_kia_update_data(instance);
|
||||
subghz_protocol_encoder_kia_get_upload(instance);
|
||||
FURI_LOG_I(
|
||||
TAG, "Counter incremented to 0x%04X, upload rebuilt with new CRC", instance->counter);
|
||||
if(instance->generic.cnt < 0xFFFF) {
|
||||
instance->generic.cnt++;
|
||||
} else {
|
||||
instance->generic.cnt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current counter value
|
||||
*/
|
||||
uint16_t subghz_protocol_encoder_kia_get_counter(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
return instance->counter;
|
||||
return instance->generic.cnt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current button value
|
||||
*/
|
||||
uint8_t subghz_protocol_encoder_kia_get_button(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKIA* instance = context;
|
||||
return instance->button;
|
||||
return instance->generic.btn;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -546,23 +378,24 @@ void* subghz_protocol_decoder_kia_alloc(SubGhzEnvironment* environment) {
|
||||
SubGhzProtocolDecoderKIA* instance = malloc(sizeof(SubGhzProtocolDecoderKIA));
|
||||
instance->base.protocol = &subghz_protocol_kia_v0;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_kia_free(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_kia_reset(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
instance->decoder.parser_step = KIADecoderStepReset;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
|
||||
switch(instance->decoder.parser_step) {
|
||||
@@ -590,6 +423,7 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
subghz_protocol_kia_const.te_delta) &&
|
||||
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
|
||||
subghz_protocol_kia_const.te_delta)) {
|
||||
// Found header
|
||||
instance->header_count++;
|
||||
break;
|
||||
} else if(
|
||||
@@ -597,13 +431,12 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
subghz_protocol_kia_const.te_delta) &&
|
||||
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
|
||||
subghz_protocol_kia_const.te_delta)) {
|
||||
// Found start bit
|
||||
if(instance->header_count > 15) {
|
||||
instance->decoder.parser_step = KIADecoderStepSaveDuration;
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 1;
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
|
||||
FURI_LOG_I(
|
||||
TAG, "Starting data decode after %u header pulses", instance->header_count);
|
||||
} else {
|
||||
instance->decoder.parser_step = KIADecoderStepReset;
|
||||
}
|
||||
@@ -616,29 +449,21 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
if(level) {
|
||||
if(duration >=
|
||||
(subghz_protocol_kia_const.te_long + subghz_protocol_kia_const.te_delta * 2UL)) {
|
||||
// End of transmission detected
|
||||
// Found stop bit
|
||||
instance->decoder.parser_step = KIADecoderStepReset;
|
||||
|
||||
if(instance->decoder.decode_count_bit ==
|
||||
subghz_protocol_kia_const.min_count_bit_for_found) {
|
||||
instance->generic.data = instance->decoder.decode_data;
|
||||
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
|
||||
|
||||
// Verify CRC before accepting the packet
|
||||
if(kia_verify_crc(instance->generic.data)) {
|
||||
FURI_LOG_I(TAG, "Valid signal received with correct CRC");
|
||||
} else {
|
||||
FURI_LOG_W(TAG, "Signal received but CRC mismatch!");
|
||||
}
|
||||
|
||||
if(instance->base.callback)
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
} else {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Incomplete signal: only %u bits",
|
||||
instance->decoder.decode_count_bit);
|
||||
FURI_LOG_W(TAG, "CRC verification failed, packet rejected");
|
||||
}
|
||||
}
|
||||
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
break;
|
||||
@@ -646,6 +471,7 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
instance->decoder.te_last = duration;
|
||||
instance->decoder.parser_step = KIADecoderStepCheckDuration;
|
||||
}
|
||||
|
||||
} else {
|
||||
instance->decoder.parser_step = KIADecoderStepReset;
|
||||
}
|
||||
@@ -667,12 +493,6 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
|
||||
instance->decoder.parser_step = KIADecoderStepSaveDuration;
|
||||
} else {
|
||||
FURI_LOG_W(
|
||||
TAG,
|
||||
"Timing mismatch at bit %u. Last: %lu, Current: %lu",
|
||||
instance->decoder.decode_count_bit,
|
||||
instance->decoder.te_last,
|
||||
duration);
|
||||
instance->decoder.parser_step = KIADecoderStepReset;
|
||||
}
|
||||
} else {
|
||||
@@ -682,14 +502,33 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analysis of received data
|
||||
* @param instance Pointer to a SubGhzBlockGeneric* instance
|
||||
*/
|
||||
static void subghz_protocol_kia_check_remote_controller(SubGhzBlockGeneric* instance) {
|
||||
/*
|
||||
* 0x0F 0112 43B04EC 1 7D
|
||||
* 0x0F 0113 43B04EC 1 DF
|
||||
* 0x0F 0114 43B04EC 1 30
|
||||
* 0x0F 0115 43B04EC 2 13
|
||||
* 0x0F 0116 43B04EC 3 F5
|
||||
* CNT Serial K CRC8 Kia
|
||||
*/
|
||||
|
||||
instance->serial = (uint32_t)((instance->data >> 12) & 0x0FFFFFFF);
|
||||
instance->btn = (instance->data >> 8) & 0x0F;
|
||||
instance->cnt = (instance->data >> 40) & 0xFFFF;
|
||||
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(instance->btn);
|
||||
}
|
||||
|
||||
subghz_custom_btn_set_max(4);
|
||||
}
|
||||
|
||||
uint8_t subghz_protocol_decoder_kia_get_hash_data(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(
|
||||
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
|
||||
@@ -699,42 +538,37 @@ SubGhzProtocolStatus subghz_protocol_decoder_kia_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
|
||||
subghz_protocol_kia_check_remote_controller(&instance->generic);
|
||||
|
||||
instance->generic.data_count_bit = 61;
|
||||
SubGhzProtocolStatus ret =
|
||||
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
SubGhzProtocolStatus ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
flipper_format_write_uint32(flipper_format, "Serial", &instance->generic.serial, 1);
|
||||
uint32_t temp = instance->generic.btn;
|
||||
flipper_format_write_uint32(flipper_format, "Btn", &temp, 1);
|
||||
flipper_format_write_uint32(flipper_format, "Cnt", &instance->generic.cnt, 1);
|
||||
uint32_t crc_temp = instance->generic.data & 0xFF;
|
||||
flipper_format_write_uint32(flipper_format, "CRC", &crc_temp, 1);
|
||||
if(instance->generic.data_count_bit < subghz_protocol_kia_const.min_count_bit_for_found) {
|
||||
ret = SubGhzProtocolStatusErrorParserBitCount;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
return subghz_block_generic_deserialize_check_count_bit(
|
||||
&instance->generic, flipper_format, subghz_protocol_kia_const.min_count_bit_for_found);
|
||||
static const char* subghz_protocol_kia_get_name_button(uint8_t btn) {
|
||||
const char* name_btn[5] = {"Unknown", "Lock", "Unlock", "Trunk", "Horn"};
|
||||
return name_btn[btn < 5 ? btn : 0];
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_kia_get_string(void* context, FuriString* output) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKIA* instance = context;
|
||||
|
||||
subghz_protocol_kia_check_remote_controller(&instance->generic);
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
uint32_t code_found_hi = instance->generic.data >> 32;
|
||||
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
|
||||
|
||||
@@ -746,15 +580,18 @@ void subghz_protocol_decoder_kia_get_string(void* context, FuriString* output) {
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%08lX%08lX\r\n"
|
||||
"Sn:%07lX Btn:[%s] Cnt:%04lX\r\n"
|
||||
"CRC:%02X %s\r\n",
|
||||
"Sn:%07lX Cnt:%04lX\r\n"
|
||||
"Btn:%02X:[%s]\r\n"
|
||||
"CRC:%02X %s",
|
||||
instance->generic.protocol_name,
|
||||
instance->generic.data_count_bit,
|
||||
code_found_hi,
|
||||
code_found_lo,
|
||||
instance->generic.serial,
|
||||
kia_v0_btn_name(kia_v0_get_btn_code()),
|
||||
instance->generic.cnt,
|
||||
instance->generic.btn,
|
||||
subghz_protocol_kia_get_name_button(instance->generic.btn),
|
||||
received_crc,
|
||||
crc_valid ? "(OK)" : "(FAIL)");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "kia_generic.h"
|
||||
#include "base.h"
|
||||
|
||||
|
||||
#define KIA_PROTOCOL_V0_NAME "KIA/HYU V0"
|
||||
#define SUBGHZ_PROTOCOL_KIA_V0_NAME "KIA/HYU V0"
|
||||
|
||||
typedef struct SubGhzProtocolDecoderKIA SubGhzProtocolDecoderKIA;
|
||||
typedef struct SubGhzProtocolEncoderKIA SubGhzProtocolEncoderKIA;
|
||||
@@ -12,23 +11,133 @@ extern const SubGhzProtocolDecoder subghz_protocol_kia_decoder;
|
||||
extern const SubGhzProtocolEncoder subghz_protocol_kia_encoder;
|
||||
extern const SubGhzProtocol subghz_protocol_kia_v0;
|
||||
|
||||
// Decoder functions
|
||||
/**
|
||||
* Allocate SubGhzProtocolEncoderKIA.
|
||||
* @param environment Pointer to a SubGhzEnvironment instance
|
||||
* @return SubGhzProtocolEncoderKIA* pointer to a SubGhzProtocolEncoderKIA instance
|
||||
*/
|
||||
void* subghz_protocol_encoder_kia_alloc(SubGhzEnvironment* environment);
|
||||
|
||||
/**
|
||||
* Free SubGhzProtocolEncoderKIA.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_free(void* context);
|
||||
|
||||
/**
|
||||
* Deserialize and generating an upload to send.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @return status
|
||||
*/
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_kia_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
|
||||
/**
|
||||
* Forced transmission stop.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_stop(void* context);
|
||||
|
||||
/**
|
||||
* Getting the level and duration of the upload to be loaded into DMA.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @return LevelDuration
|
||||
*/
|
||||
LevelDuration subghz_protocol_encoder_kia_yield(void* context);
|
||||
|
||||
/**
|
||||
* Set button value for encoding.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @param button Button value (0-4)
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_set_button(void* context, uint8_t button);
|
||||
|
||||
/**
|
||||
* Set counter value for encoding.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @param counter Counter value (0-65535)
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_set_counter(void* context, uint16_t counter);
|
||||
|
||||
/**
|
||||
* Increment counter by 1.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
*/
|
||||
void subghz_protocol_encoder_kia_increment_counter(void* context);
|
||||
|
||||
/**
|
||||
* Get current counter value.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @return Current counter value
|
||||
*/
|
||||
uint16_t subghz_protocol_encoder_kia_get_counter(void* context);
|
||||
|
||||
/**
|
||||
* Get current button value.
|
||||
* @param context Pointer to a SubGhzProtocolEncoderKIA instance
|
||||
* @return Current button value
|
||||
*/
|
||||
uint8_t subghz_protocol_encoder_kia_get_button(void* context);
|
||||
|
||||
/**
|
||||
* Allocate SubGhzProtocolDecoderKIA.
|
||||
* @param environment Pointer to a SubGhzEnvironment instance
|
||||
* @return SubGhzProtocolDecoderKIA* pointer to a SubGhzProtocolDecoderKIA instance
|
||||
*/
|
||||
void* subghz_protocol_decoder_kia_alloc(SubGhzEnvironment* environment);
|
||||
|
||||
/**
|
||||
* Free SubGhzProtocolDecoderKIA.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
*/
|
||||
void subghz_protocol_decoder_kia_free(void* context);
|
||||
|
||||
/**
|
||||
* Reset decoder SubGhzProtocolDecoderKIA.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
*/
|
||||
void subghz_protocol_decoder_kia_reset(void* context);
|
||||
|
||||
/**
|
||||
* Parse a raw sequence of levels and durations received from the air.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
* @param level Signal level true-high false-low
|
||||
* @param duration Duration of this level in, us
|
||||
*/
|
||||
void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t duration);
|
||||
|
||||
/**
|
||||
* Getting the hash sum of the last randomly received parcel.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
* @return hash Hash sum (uint8_t)
|
||||
*/
|
||||
uint8_t subghz_protocol_decoder_kia_get_hash_data(void* context);
|
||||
|
||||
/**
|
||||
* Serialize data SubGhzProtocolDecoderKIA.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
|
||||
* @return status
|
||||
*/
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
|
||||
/**
|
||||
* Deserialize data SubGhzProtocolDecoderKIA.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @return status
|
||||
*/
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_kia_get_string(void* context, FuriString* output);
|
||||
|
||||
// Encoder helper functions
|
||||
void subghz_protocol_encoder_kia_set_button(void* context, uint8_t button);
|
||||
void subghz_protocol_encoder_kia_set_counter(void* context, uint16_t counter);
|
||||
void subghz_protocol_encoder_kia_increment_counter(void* context);
|
||||
uint16_t subghz_protocol_encoder_kia_get_counter(void* context);
|
||||
uint8_t subghz_protocol_encoder_kia_get_button(void* context);
|
||||
/**
|
||||
* Getting a textual representation of the received data.
|
||||
* @param context Pointer to a SubGhzProtocolDecoderKIA instance
|
||||
* @param output Resulting text
|
||||
*/
|
||||
void subghz_protocol_decoder_kia_get_string(void* context, FuriString* output);
|
||||
|
||||
@@ -1,57 +1,39 @@
|
||||
#include "kia_v1.h"
|
||||
|
||||
#include "../blocks/const.h"
|
||||
#include "../blocks/decoder.h"
|
||||
#include "../blocks/encoder.h"
|
||||
#include "../blocks/generic.h"
|
||||
#include "../blocks/math.h"
|
||||
#include "../blocks/custom_btn_i.h"
|
||||
|
||||
static uint8_t kia_v1_get_btn_code() {
|
||||
uint8_t custom_btn = subghz_custom_btn_get();
|
||||
uint8_t original_btn = subghz_custom_btn_get_original();
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_OK) return original_btn;
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_UP) return 0x01; // Lock
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_DOWN) return 0x02; // Unlock
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_LEFT) return 0x03; // Boot/Trunk
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_RIGHT) return 0x03; // Boot/Trunk
|
||||
return original_btn;
|
||||
}
|
||||
|
||||
static const char* kia_v1_btn_name(uint8_t btn) {
|
||||
if(btn == 0x01) return "Lock";
|
||||
if(btn == 0x02) return "Unlock";
|
||||
if(btn == 0x03) return "Boot";
|
||||
return "??";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#include <lib/toolbox/manchester_decoder.h>
|
||||
|
||||
#define TAG "KiaV1"
|
||||
#define TAG "SubGhzProtocolKiaV1"
|
||||
|
||||
// Costanti esattamente come ProtoP irate
|
||||
#define KIA_V1_TOTAL_BURSTS 3
|
||||
#define KIA_V1_INTER_BURST_GAP_US 25000
|
||||
#define KIA_V1_HEADER_PULSES 90
|
||||
|
||||
static const SubGhzBlockConst kia_protocol_v1_const = {
|
||||
static const SubGhzBlockConst subghz_protocol_kia_v1_const = {
|
||||
.te_short = 800,
|
||||
.te_long = 1600,
|
||||
.te_delta = 200,
|
||||
.min_count_bit_for_found = 57,
|
||||
.min_count_bit_for_found = 57, // Come ProtoP irate
|
||||
};
|
||||
|
||||
struct SubGhzProtocolDecoderKiaV1 {
|
||||
SubGhzProtocolDecoderBase base;
|
||||
|
||||
SubGhzBlockDecoder decoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
|
||||
uint16_t header_count;
|
||||
ManchesterState manchester_saved_state;
|
||||
ManchesterState manchester_saved_state; // Come ProtoP irate
|
||||
uint8_t crc;
|
||||
bool crc_check;
|
||||
};
|
||||
|
||||
struct SubGhzProtocolEncoderKiaV1 {
|
||||
SubGhzProtocolEncoderBase base;
|
||||
|
||||
SubGhzProtocolBlockEncoder encoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
};
|
||||
@@ -59,57 +41,53 @@ struct SubGhzProtocolEncoderKiaV1 {
|
||||
typedef enum {
|
||||
KiaV1DecoderStepReset = 0,
|
||||
KiaV1DecoderStepCheckPreamble,
|
||||
KiaV1DecoderStepDecodeData,
|
||||
KiaV1DecoderStepDecodeData, // Come ProtoP irate
|
||||
} KiaV1DecoderStep;
|
||||
|
||||
const SubGhzProtocolDecoder kia_protocol_v1_decoder = {
|
||||
.alloc = kia_protocol_decoder_v1_alloc,
|
||||
.free = kia_protocol_decoder_v1_free,
|
||||
|
||||
.feed = kia_protocol_decoder_v1_feed,
|
||||
.reset = kia_protocol_decoder_v1_reset,
|
||||
|
||||
.get_hash_data = kia_protocol_decoder_v1_get_hash_data,
|
||||
.serialize = kia_protocol_decoder_v1_serialize,
|
||||
.deserialize = kia_protocol_decoder_v1_deserialize,
|
||||
.get_string = kia_protocol_decoder_v1_get_string,
|
||||
const SubGhzProtocolDecoder subghz_protocol_kia_v1_decoder = {
|
||||
.alloc = subghz_protocol_decoder_kia_v1_alloc,
|
||||
.free = subghz_protocol_decoder_kia_v1_free,
|
||||
.feed = subghz_protocol_decoder_kia_v1_feed,
|
||||
.reset = subghz_protocol_decoder_kia_v1_reset,
|
||||
.get_hash_data = subghz_protocol_decoder_kia_v1_get_hash_data,
|
||||
.serialize = subghz_protocol_decoder_kia_v1_serialize,
|
||||
.deserialize = subghz_protocol_decoder_kia_v1_deserialize,
|
||||
.get_string = subghz_protocol_decoder_kia_v1_get_string,
|
||||
};
|
||||
|
||||
const SubGhzProtocolEncoder kia_protocol_v1_encoder = {
|
||||
.alloc = kia_protocol_encoder_v1_alloc,
|
||||
.free = kia_protocol_encoder_v1_free,
|
||||
|
||||
.deserialize = kia_protocol_encoder_v1_deserialize,
|
||||
.stop = kia_protocol_encoder_v1_stop,
|
||||
.yield = kia_protocol_encoder_v1_yield,
|
||||
const SubGhzProtocolEncoder subghz_protocol_kia_v1_encoder = {
|
||||
.alloc = subghz_protocol_encoder_kia_v1_alloc,
|
||||
.free = subghz_protocol_encoder_kia_v1_free,
|
||||
.deserialize = subghz_protocol_encoder_kia_v1_deserialize,
|
||||
.stop = subghz_protocol_encoder_kia_v1_stop,
|
||||
.yield = subghz_protocol_encoder_kia_v1_yield,
|
||||
};
|
||||
|
||||
const SubGhzProtocol subghz_protocol_kia_v1 = {
|
||||
.name = KIA_PROTOCOL_V1_NAME,
|
||||
.name = SUBGHZ_PROTOCOL_KIA_V1_NAME,
|
||||
.type = SubGhzProtocolTypeDynamic,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM |
|
||||
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
|
||||
SubGhzProtocolFlag_Send,
|
||||
|
||||
.decoder = &kia_protocol_v1_decoder,
|
||||
.encoder = &kia_protocol_v1_encoder,
|
||||
.decoder = &subghz_protocol_kia_v1_decoder,
|
||||
.encoder = &subghz_protocol_kia_v1_encoder,
|
||||
};
|
||||
|
||||
static void kia_v1_check_remote_controller(SubGhzProtocolDecoderKiaV1* instance);
|
||||
|
||||
static uint8_t kia_v1_crc4(const uint8_t* bytes, int count, uint8_t offset) {
|
||||
uint8_t crc = 0;
|
||||
|
||||
for(int i = 0; i < count; i++) {
|
||||
uint8_t b = bytes[i];
|
||||
crc ^= ((b & 0x0F) ^ (b >> 4));
|
||||
}
|
||||
|
||||
crc = (crc + offset) & 0x0F;
|
||||
return crc;
|
||||
}
|
||||
|
||||
static void kia_v1_check_remote_controller(SubGhzProtocolDecoderKiaV1* instance) {
|
||||
|
||||
|
||||
static void subghz_protocol_kia_v1_check_remote_controller(SubGhzProtocolDecoderKiaV1* instance) {
|
||||
// Estrazione campi esattamente come ProtoP irate
|
||||
instance->generic.serial = instance->generic.data >> 24;
|
||||
instance->generic.btn = (instance->generic.data >> 16) & 0xFF;
|
||||
instance->generic.cnt = ((instance->generic.data >> 4) & 0xF) << 8 |
|
||||
@@ -137,9 +115,27 @@ static void kia_v1_check_remote_controller(SubGhzProtocolDecoderKiaV1* instance)
|
||||
|
||||
instance->crc = cnt_high << 4 | crc;
|
||||
instance->crc_check = (crc == (instance->generic.data & 0xF));
|
||||
|
||||
// Imposta bottoni custom
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
}
|
||||
|
||||
void* kia_protocol_encoder_v1_alloc(SubGhzEnvironment* environment) {
|
||||
static const char* subghz_protocol_kia_v1_get_name_button(uint8_t btn) {
|
||||
switch(btn) {
|
||||
case 0x1: return "Lock";
|
||||
case 0x2: return "Unlock";
|
||||
case 0x3: return "Trunk";
|
||||
case 0x4: return "Panic";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void* subghz_protocol_encoder_kia_v1_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderKiaV1* instance = malloc(sizeof(SubGhzProtocolEncoderKiaV1));
|
||||
|
||||
@@ -151,20 +147,46 @@ void* kia_protocol_encoder_v1_alloc(SubGhzEnvironment* environment) {
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v1_free(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_encoder_kia_v1_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
free(instance->encoder.upload);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
static void kia_protocol_encoder_v1_get_upload(SubGhzProtocolEncoderKiaV1* instance) {
|
||||
furi_check(instance);
|
||||
void subghz_protocol_encoder_kia_v1_stop(void* context) {
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
|
||||
LevelDuration subghz_protocol_encoder_kia_v1_yield(void* context) {
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
|
||||
instance->encoder.is_running = false;
|
||||
return level_duration_reset();
|
||||
}
|
||||
|
||||
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
|
||||
|
||||
if(++instance->encoder.front == instance->encoder.size_upload) {
|
||||
instance->encoder.repeat--;
|
||||
instance->encoder.front = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ENCODER GET_UPLOAD
|
||||
static void subghz_protocol_encoder_kia_v1_get_upload(SubGhzProtocolEncoderKiaV1* instance) {
|
||||
furi_assert(instance);
|
||||
size_t index = 0;
|
||||
|
||||
// Calcolo CRC come ProtoP irate
|
||||
uint8_t cnt_high = (instance->generic.cnt >> 8) & 0xF;
|
||||
uint8_t char_data[7];
|
||||
char_data[0] = (instance->generic.serial >> 24) & 0xFF;
|
||||
@@ -185,37 +207,47 @@ static void kia_protocol_encoder_v1_get_upload(SubGhzProtocolEncoderKiaV1* insta
|
||||
crc = kia_v1_crc4(char_data, 6, 1);
|
||||
}
|
||||
|
||||
// Costruisci data esattamente come ProtoP irate
|
||||
instance->generic.data = (uint64_t)instance->generic.serial << 24 |
|
||||
instance->generic.btn << 16 | (instance->generic.cnt & 0xFF) << 8 |
|
||||
((instance->generic.cnt >> 8) & 0xF) << 4 | crc;
|
||||
(uint64_t)instance->generic.btn << 16 |
|
||||
(uint64_t)(instance->generic.cnt & 0xFF) << 8 |
|
||||
(uint64_t)((instance->generic.cnt >> 8) & 0xF) << 4 |
|
||||
crc;
|
||||
|
||||
// 3 burst come ProtoP irate
|
||||
for(uint8_t burst = 0; burst < KIA_V1_TOTAL_BURSTS; burst++) {
|
||||
// Gap tra burst
|
||||
if(burst > 0) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, KIA_V1_INTER_BURST_GAP_US);
|
||||
}
|
||||
|
||||
// 90 header pulses: LOW-HIGH con te_long
|
||||
for(int i = 0; i < KIA_V1_HEADER_PULSES; i++) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v1_const.te_long);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v1_const.te_long);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)kia_protocol_v1_const.te_long);
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_v1_const.te_long);
|
||||
}
|
||||
|
||||
// SHORT_LOW prima dei dati
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v1_const.te_short);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v1_const.te_short);
|
||||
|
||||
// Manchester encoding dei dati
|
||||
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
|
||||
if(bit_read(instance->generic.data, i - 2)) {
|
||||
// Bit 1: HIGH-LOW
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)kia_protocol_v1_const.te_short);
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_v1_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v1_const.te_short);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v1_const.te_short);
|
||||
} else {
|
||||
// Bit 0: LOW-HIGH
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v1_const.te_short);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v1_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)kia_protocol_v1_const.te_short);
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_v1_const.te_short);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,233 +255,87 @@ static void kia_protocol_encoder_v1_get_upload(SubGhzProtocolEncoderKiaV1* insta
|
||||
instance->encoder.size_upload = index;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Upload built: %d bursts, size_upload=%zu, data_count_bit=%u, data=0x%016llX",
|
||||
KIA_V1_TOTAL_BURSTS,
|
||||
instance->encoder.size_upload,
|
||||
instance->generic.data_count_bit,
|
||||
instance->generic.data);
|
||||
FURI_LOG_I(TAG, "Upload built: size=%zu, data=0x%014llX", index, instance->generic.data);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_encoder_v1_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_kia_v1_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
flipper_format_rewind(flipper_format);
|
||||
|
||||
do {
|
||||
FuriString* temp_str = furi_string_alloc();
|
||||
if(!flipper_format_read_string(flipper_format, "Protocol", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Missing Protocol");
|
||||
furi_string_free(temp_str);
|
||||
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
if(ret != SubGhzProtocolStatusOk) {
|
||||
FURI_LOG_E(TAG, "Deserialize failed");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!furi_string_equal(temp_str, instance->base.protocol->name)) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Wrong protocol %s != %s",
|
||||
furi_string_get_cstr(temp_str),
|
||||
instance->base.protocol->name);
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
furi_string_free(temp_str);
|
||||
// Imposta data_count_bit
|
||||
instance->generic.data_count_bit = subghz_protocol_kia_v1_const.min_count_bit_for_found;
|
||||
|
||||
uint32_t bit_count_temp;
|
||||
if(!flipper_format_read_uint32(flipper_format, "Bit", &bit_count_temp, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing Bit");
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data_count_bit = kia_protocol_v1_const.min_count_bit_for_found;
|
||||
|
||||
temp_str = furi_string_alloc();
|
||||
if(!flipper_format_read_string(flipper_format, "Key", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Missing Key");
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
const char* key_str = furi_string_get_cstr(temp_str);
|
||||
uint64_t key = 0;
|
||||
size_t str_len = strlen(key_str);
|
||||
size_t hex_pos = 0;
|
||||
for(size_t i = 0; i < str_len && hex_pos < 16; i++) {
|
||||
char c = key_str[i];
|
||||
if(c == ' ') continue;
|
||||
|
||||
uint8_t nibble;
|
||||
if(c >= '0' && c <= '9') {
|
||||
nibble = c - '0';
|
||||
} else if(c >= 'A' && c <= 'F') {
|
||||
nibble = c - 'A' + 10;
|
||||
} else if(c >= 'a' && c <= 'f') {
|
||||
nibble = c - 'a' + 10;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Invalid hex character: %c", c);
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
key = (key << 4) | nibble;
|
||||
hex_pos++;
|
||||
}
|
||||
|
||||
furi_string_free(temp_str);
|
||||
|
||||
if(hex_pos != 16) {
|
||||
FURI_LOG_E(TAG, "Invalid key length: %zu nibbles (expected 16)", hex_pos);
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data = key;
|
||||
FURI_LOG_I(TAG, "Parsed key: 0x%016llX", instance->generic.data);
|
||||
|
||||
if(instance->generic.data == 0) {
|
||||
FURI_LOG_E(TAG, "Key is zero after parsing!");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_format_read_uint32(flipper_format, "Serial", &instance->generic.serial, 1)) {
|
||||
// Estrai serial, btn, cnt dalla data (come ProtoP irate)
|
||||
instance->generic.serial = instance->generic.data >> 24;
|
||||
FURI_LOG_I(TAG, "Extracted serial: 0x%08lX", instance->generic.serial);
|
||||
} else {
|
||||
FURI_LOG_I(TAG, "Read serial: 0x%08lX", instance->generic.serial);
|
||||
}
|
||||
|
||||
uint32_t btn_temp;
|
||||
if(flipper_format_read_uint32(flipper_format, "Btn", &btn_temp, 1)) {
|
||||
instance->generic.btn = (uint8_t)btn_temp;
|
||||
} else {
|
||||
instance->generic.btn = (instance->generic.data >> 16) & 0xFF;
|
||||
}
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
instance->generic.btn = kia_v1_get_btn_code();
|
||||
|
||||
uint32_t cnt_temp;
|
||||
if(flipper_format_read_uint32(flipper_format, "Cnt", &cnt_temp, 1)) {
|
||||
instance->generic.cnt = (uint16_t)cnt_temp;
|
||||
FURI_LOG_I(TAG, "Read counter: 0x%03lX", (unsigned long)instance->generic.cnt);
|
||||
} else {
|
||||
instance->generic.cnt = ((instance->generic.data >> 4) & 0xF) << 8 |
|
||||
((instance->generic.data >> 8) & 0xFF);
|
||||
FURI_LOG_I(TAG, "Extracted counter: 0x%03lX", (unsigned long)instance->generic.cnt);
|
||||
|
||||
FURI_LOG_I(TAG, "Deserialized: data=%014llX, serial=%08lX, btn=%02X, cnt=%03lX",
|
||||
instance->generic.data, instance->generic.serial,
|
||||
instance->generic.btn, instance->generic.cnt);
|
||||
|
||||
// Imposta bottone originale per custom buttons
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
// Incrementa counter
|
||||
if(instance->generic.cnt < 0xFFF) {
|
||||
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
|
||||
if(instance->generic.cnt > 0xFFF) {
|
||||
instance->generic.cnt = 0;
|
||||
}
|
||||
} else {
|
||||
instance->generic.cnt = 0;
|
||||
}
|
||||
|
||||
if(!flipper_format_read_uint32(
|
||||
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1)) {
|
||||
instance->encoder.repeat = 10;
|
||||
FURI_LOG_D(
|
||||
TAG, "Repeat not found in file, using default 10 for continuous transmission");
|
||||
// Gestione bottoni custom
|
||||
uint8_t btn = subghz_custom_btn_get();
|
||||
if(btn != SUBGHZ_CUSTOM_BTN_OK) {
|
||||
instance->generic.btn = btn;
|
||||
}
|
||||
|
||||
kia_protocol_encoder_v1_get_upload(instance);
|
||||
// Costruisci upload
|
||||
subghz_protocol_encoder_kia_v1_get_upload(instance);
|
||||
|
||||
// Aggiorna file con nuova key
|
||||
if(!flipper_format_rewind(flipper_format)) {
|
||||
FURI_LOG_E(TAG, "Rewind error");
|
||||
ret = SubGhzProtocolStatusErrorParserOthers;
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
for(size_t i = 0; i < sizeof(uint64_t); i++) {
|
||||
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> (i * 8)) & 0xFF;
|
||||
}
|
||||
if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(TAG, "Unable to update Key");
|
||||
ret = SubGhzProtocolStatusErrorParserKey;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->encoder.is_running = true;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Encoder deserialized: repeat=%u, size_upload=%zu, is_running=%d, front=%zu",
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.size_upload,
|
||||
instance->encoder.is_running,
|
||||
instance->encoder.front);
|
||||
|
||||
ret = SubGhzProtocolStatusOk;
|
||||
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v1_stop(void* context) {
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
|
||||
LevelDuration kia_protocol_encoder_v1_yield(void* context) {
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Encoder yield stopped: repeat=%u, is_running=%d",
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.is_running);
|
||||
instance->encoder.is_running = false;
|
||||
return level_duration_reset();
|
||||
}
|
||||
|
||||
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
|
||||
|
||||
if(instance->encoder.front < 5 || instance->encoder.front == 0) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Encoder yield[%zu]: repeat=%u, size=%zu, level=%d, duration=%lu",
|
||||
instance->encoder.front,
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.size_upload,
|
||||
level_duration_get_level(ret),
|
||||
level_duration_get_duration(ret));
|
||||
}
|
||||
|
||||
if(++instance->encoder.front == instance->encoder.size_upload) {
|
||||
instance->encoder.repeat--;
|
||||
instance->encoder.front = 0;
|
||||
FURI_LOG_I(
|
||||
TAG, "Encoder completed one cycle, remaining repeat=%u", instance->encoder.repeat);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v1_set_button(void* context, uint8_t button) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
instance->generic.btn = button & 0xFF;
|
||||
kia_protocol_encoder_v1_get_upload(instance);
|
||||
FURI_LOG_I(TAG, "Button set to 0x%02X, upload rebuilt with new CRC", instance->generic.btn);
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v1_set_counter(void* context, uint16_t counter) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
instance->generic.cnt = counter & 0xFFF;
|
||||
kia_protocol_encoder_v1_get_upload(instance);
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Counter set to 0x%03X, upload rebuilt with new CRC",
|
||||
(uint16_t)instance->generic.cnt);
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v1_increment_counter(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
instance->generic.cnt = (instance->generic.cnt + 1) & 0xFFF;
|
||||
kia_protocol_encoder_v1_get_upload(instance);
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Counter incremented to 0x%03X, upload rebuilt with new CRC",
|
||||
(uint16_t)instance->generic.cnt);
|
||||
}
|
||||
|
||||
uint16_t kia_protocol_encoder_v1_get_counter(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
return instance->generic.cnt;
|
||||
}
|
||||
|
||||
uint8_t kia_protocol_encoder_v1_get_button(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKiaV1* instance = context;
|
||||
return instance->generic.btn;
|
||||
}
|
||||
|
||||
void* kia_protocol_decoder_v1_alloc(SubGhzEnvironment* environment) {
|
||||
void* subghz_protocol_decoder_kia_v1_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolDecoderKiaV1* instance = malloc(sizeof(SubGhzProtocolDecoderKiaV1));
|
||||
instance->base.protocol = &subghz_protocol_kia_v1;
|
||||
@@ -457,28 +343,29 @@ void* kia_protocol_decoder_v1_alloc(SubGhzEnvironment* environment) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v1_free(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_decoder_kia_v1_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v1_reset(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_decoder_kia_v1_reset(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
instance->decoder.parser_step = KiaV1DecoderStepReset;
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_check(context);
|
||||
// FEED
|
||||
void subghz_protocol_decoder_kia_v1_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
|
||||
ManchesterEvent event = ManchesterEventReset;
|
||||
|
||||
switch(instance->decoder.parser_step) {
|
||||
case KiaV1DecoderStepReset:
|
||||
if((level) && (DURATION_DIFF(duration, kia_protocol_v1_const.te_long) <
|
||||
kia_protocol_v1_const.te_delta)) {
|
||||
if((level) && (DURATION_DIFF(duration, subghz_protocol_kia_v1_const.te_long) <
|
||||
subghz_protocol_kia_v1_const.te_delta)) {
|
||||
instance->decoder.parser_step = KiaV1DecoderStepCheckPreamble;
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count = 0;
|
||||
@@ -494,10 +381,10 @@ void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration)
|
||||
|
||||
case KiaV1DecoderStepCheckPreamble:
|
||||
if(!level) {
|
||||
if((DURATION_DIFF(duration, kia_protocol_v1_const.te_long) <
|
||||
kia_protocol_v1_const.te_delta) &&
|
||||
(DURATION_DIFF(instance->decoder.te_last, kia_protocol_v1_const.te_long) <
|
||||
kia_protocol_v1_const.te_delta)) {
|
||||
if((DURATION_DIFF(duration, subghz_protocol_kia_v1_const.te_long) <
|
||||
subghz_protocol_kia_v1_const.te_delta) &&
|
||||
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_v1_const.te_long) <
|
||||
subghz_protocol_kia_v1_const.te_delta)) {
|
||||
instance->header_count++;
|
||||
instance->decoder.te_last = duration;
|
||||
} else {
|
||||
@@ -506,10 +393,10 @@ void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration)
|
||||
}
|
||||
if(instance->header_count > 70) {
|
||||
if((!level) &&
|
||||
(DURATION_DIFF(duration, kia_protocol_v1_const.te_short) <
|
||||
kia_protocol_v1_const.te_delta) &&
|
||||
(DURATION_DIFF(instance->decoder.te_last, kia_protocol_v1_const.te_long) <
|
||||
kia_protocol_v1_const.te_delta)) {
|
||||
(DURATION_DIFF(duration, subghz_protocol_kia_v1_const.te_short) <
|
||||
subghz_protocol_kia_v1_const.te_delta) &&
|
||||
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_v1_const.te_long) <
|
||||
subghz_protocol_kia_v1_const.te_delta)) {
|
||||
instance->decoder.decode_count_bit = 1;
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
|
||||
instance->header_count = 0;
|
||||
@@ -519,12 +406,18 @@ void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration)
|
||||
break;
|
||||
|
||||
case KiaV1DecoderStepDecodeData:
|
||||
if((DURATION_DIFF(duration, kia_protocol_v1_const.te_short) <
|
||||
kia_protocol_v1_const.te_delta)) {
|
||||
if((DURATION_DIFF(duration, subghz_protocol_kia_v1_const.te_short) <
|
||||
subghz_protocol_kia_v1_const.te_delta)) {
|
||||
event = level ? ManchesterEventShortLow : ManchesterEventShortHigh;
|
||||
} else if((DURATION_DIFF(duration, kia_protocol_v1_const.te_long) <
|
||||
kia_protocol_v1_const.te_delta)) {
|
||||
} else if((DURATION_DIFF(duration, subghz_protocol_kia_v1_const.te_long) <
|
||||
subghz_protocol_kia_v1_const.te_delta)) {
|
||||
event = level ? ManchesterEventLongLow : ManchesterEventLongHigh;
|
||||
} else {
|
||||
// Durata non valida - reset completo
|
||||
instance->decoder.parser_step = KiaV1DecoderStepReset;
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if(event != ManchesterEventReset) {
|
||||
@@ -537,7 +430,8 @@ void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration)
|
||||
}
|
||||
}
|
||||
|
||||
if(instance->decoder.decode_count_bit == kia_protocol_v1_const.min_count_bit_for_found) {
|
||||
if(instance->decoder.decode_count_bit ==
|
||||
subghz_protocol_kia_v1_const.min_count_bit_for_found) {
|
||||
instance->generic.data = instance->decoder.decode_data;
|
||||
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
|
||||
if(instance->base.callback)
|
||||
@@ -551,52 +445,35 @@ void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration)
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t kia_protocol_decoder_v1_get_hash_data(void* context) {
|
||||
furi_check(context);
|
||||
uint8_t subghz_protocol_decoder_kia_v1_get_hash_data(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(
|
||||
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus kia_protocol_decoder_v1_serialize(
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v1_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
|
||||
kia_v1_check_remote_controller(instance);
|
||||
|
||||
SubGhzProtocolStatus ret =
|
||||
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
flipper_format_write_uint32(flipper_format, "Serial", &instance->generic.serial, 1);
|
||||
uint32_t temp = instance->generic.btn;
|
||||
flipper_format_write_uint32(flipper_format, "Btn", &temp, 1);
|
||||
temp = instance->generic.cnt;
|
||||
flipper_format_write_uint32(flipper_format, "Cnt", &temp, 1);
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_decoder_v1_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v1_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
flipper_format_rewind(flipper_format);
|
||||
return subghz_block_generic_deserialize_check_count_bit(
|
||||
&instance->generic, flipper_format, kia_protocol_v1_const.min_count_bit_for_found);
|
||||
&instance->generic, flipper_format, subghz_protocol_kia_v1_const.min_count_bit_for_found);
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v1_get_string(void* context, FuriString* output) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_decoder_kia_v1_get_string(void* context, FuriString* output) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV1* instance = context;
|
||||
|
||||
kia_v1_check_remote_controller(instance);
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
subghz_protocol_kia_v1_check_remote_controller(instance);
|
||||
|
||||
uint32_t code_found_hi = instance->generic.data >> 32;
|
||||
uint32_t code_found_lo = instance->generic.data & 0xFFFFFFFF;
|
||||
|
||||
@@ -604,17 +481,17 @@ void kia_protocol_decoder_v1_get_string(void* context, FuriString* output) {
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%06lX%08lX\r\n"
|
||||
"Serial:%08lX\r\n"
|
||||
"Cnt:%03lX CRC:%01X %s\r\n"
|
||||
"Btn:%02X:%s\r\n",
|
||||
"Sn:%08lX Cnt:%03lX\r\n"
|
||||
"Btn:%02X:[%s]\r\n"
|
||||
"CRC:%02X %s\r\n",
|
||||
instance->generic.protocol_name,
|
||||
instance->generic.data_count_bit,
|
||||
code_found_hi,
|
||||
code_found_lo,
|
||||
instance->generic.serial,
|
||||
instance->generic.cnt,
|
||||
instance->crc,
|
||||
instance->crc_check ? "OK" : "WRONG",
|
||||
instance->generic.btn,
|
||||
kia_v1_btn_name(kia_v1_get_btn_code()));
|
||||
subghz_protocol_kia_v1_get_name_button(instance->generic.btn),
|
||||
instance->crc,
|
||||
instance->crc_check ? "(OK)" : "(FAIL)");
|
||||
}
|
||||
|
||||
@@ -1,42 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include "kia_generic.h"
|
||||
#include "base.h"
|
||||
#include "../blocks/math.h"
|
||||
|
||||
|
||||
#define KIA_PROTOCOL_V1_NAME "KIA/HYU V1"
|
||||
#define SUBGHZ_PROTOCOL_KIA_V1_NAME "KIA/HYU V1"
|
||||
|
||||
typedef struct SubGhzProtocolDecoderKiaV1 SubGhzProtocolDecoderKiaV1;
|
||||
typedef struct SubGhzProtocolEncoderKiaV1 SubGhzProtocolEncoderKiaV1;
|
||||
|
||||
extern const SubGhzProtocolDecoder kia_protocol_v1_decoder;
|
||||
extern const SubGhzProtocolEncoder kia_protocol_v1_encoder;
|
||||
extern const SubGhzProtocol subghz_protocol_kia_v1;
|
||||
|
||||
// Decoder functions
|
||||
void* kia_protocol_decoder_v1_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_decoder_v1_free(void* context);
|
||||
void kia_protocol_decoder_v1_reset(void* context);
|
||||
void kia_protocol_decoder_v1_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t kia_protocol_decoder_v1_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus kia_protocol_decoder_v1_serialize(
|
||||
void* subghz_protocol_decoder_kia_v1_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_decoder_kia_v1_free(void* context);
|
||||
void subghz_protocol_decoder_kia_v1_reset(void* context);
|
||||
void subghz_protocol_decoder_kia_v1_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t subghz_protocol_decoder_kia_v1_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v1_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_decoder_v1_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_decoder_v1_get_string(void* context, FuriString* output);
|
||||
subghz_protocol_decoder_kia_v1_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_kia_v1_get_string(void* context, FuriString* output);
|
||||
|
||||
// Encoder functions
|
||||
void* kia_protocol_encoder_v1_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_encoder_v1_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_encoder_v1_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_encoder_v1_stop(void* context);
|
||||
LevelDuration kia_protocol_encoder_v1_yield(void* context);
|
||||
void* subghz_protocol_encoder_kia_v1_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_kia_v1_free(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_kia_v1_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_kia_v1_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_kia_v1_yield(void* context);
|
||||
|
||||
// Encoder helper functions for UI
|
||||
void kia_protocol_encoder_v1_set_button(void* context, uint8_t button);
|
||||
void kia_protocol_encoder_v1_set_counter(void* context, uint16_t counter);
|
||||
void kia_protocol_encoder_v1_increment_counter(void* context);
|
||||
uint16_t kia_protocol_encoder_v1_get_counter(void* context);
|
||||
uint8_t kia_protocol_encoder_v1_get_button(void* context);
|
||||
void subghz_protocol_encoder_kia_v1_set_button(void* context, uint8_t button);
|
||||
void subghz_protocol_encoder_kia_v1_set_counter(void* context, uint16_t counter);
|
||||
void subghz_protocol_encoder_kia_v1_increment_counter(void* context);
|
||||
uint16_t subghz_protocol_encoder_kia_v1_get_counter(void* context);
|
||||
uint8_t subghz_protocol_encoder_kia_v1_get_button(void* context);
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
#include "kia_v2.h"
|
||||
|
||||
#include "../blocks/const.h"
|
||||
#include "../blocks/decoder.h"
|
||||
#include "../blocks/encoder.h"
|
||||
#include "../blocks/generic.h"
|
||||
#include "../blocks/math.h"
|
||||
#include "../blocks/custom_btn_i.h"
|
||||
|
||||
static uint8_t kia_v2_get_btn_code() {
|
||||
uint8_t custom_btn = subghz_custom_btn_get();
|
||||
uint8_t original_btn = subghz_custom_btn_get_original();
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_OK) return original_btn;
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_UP) return 0x01; // Lock
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_DOWN) return 0x02; // Unlock
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_LEFT) return 0x03; // Boot/Trunk
|
||||
if(custom_btn == SUBGHZ_CUSTOM_BTN_RIGHT) return 0x03; // Boot/Trunk
|
||||
return original_btn;
|
||||
}
|
||||
|
||||
|
||||
#include <lib/toolbox/manchester_decoder.h>
|
||||
#include <lib/toolbox/manchester_encoder.h>
|
||||
#include <furi.h>
|
||||
#include <furi_hal_subghz.h>
|
||||
|
||||
#define TAG "KiaV2"
|
||||
#define TAG "SubGhzProtocolKiaV2"
|
||||
|
||||
#define KIA_V2_HEADER_PAIRS 252
|
||||
#define KIA_V2_TOTAL_BURSTS 2
|
||||
|
||||
|
||||
static const SubGhzBlockConst kia_protocol_v2_const = {
|
||||
static const SubGhzBlockConst subghz_protocol_kia_v2_const = {
|
||||
.te_short = 500,
|
||||
.te_long = 1000,
|
||||
.te_delta = 150,
|
||||
@@ -34,7 +27,6 @@ struct SubGhzProtocolDecoderKiaV2 {
|
||||
SubGhzBlockDecoder decoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
uint16_t header_count;
|
||||
|
||||
ManchesterState manchester_state;
|
||||
};
|
||||
|
||||
@@ -50,40 +42,38 @@ typedef enum {
|
||||
KiaV2DecoderStepCollectRawBits,
|
||||
} KiaV2DecoderStep;
|
||||
|
||||
const SubGhzProtocolDecoder kia_protocol_v2_decoder = {
|
||||
.alloc = kia_protocol_decoder_v2_alloc,
|
||||
.free = kia_protocol_decoder_v2_free,
|
||||
.feed = kia_protocol_decoder_v2_feed,
|
||||
.reset = kia_protocol_decoder_v2_reset,
|
||||
.get_hash_data = kia_protocol_decoder_v2_get_hash_data,
|
||||
.serialize = kia_protocol_decoder_v2_serialize,
|
||||
.deserialize = kia_protocol_decoder_v2_deserialize,
|
||||
.get_string = kia_protocol_decoder_v2_get_string,
|
||||
const SubGhzProtocolDecoder subghz_protocol_kia_v2_decoder = {
|
||||
.alloc = subghz_protocol_decoder_kia_v2_alloc,
|
||||
.free = subghz_protocol_decoder_kia_v2_free,
|
||||
.feed = subghz_protocol_decoder_kia_v2_feed,
|
||||
.reset = subghz_protocol_decoder_kia_v2_reset,
|
||||
.get_hash_data = subghz_protocol_decoder_kia_v2_get_hash_data,
|
||||
.serialize = subghz_protocol_decoder_kia_v2_serialize,
|
||||
.deserialize = subghz_protocol_decoder_kia_v2_deserialize,
|
||||
.get_string = subghz_protocol_decoder_kia_v2_get_string,
|
||||
};
|
||||
|
||||
const SubGhzProtocolEncoder kia_protocol_v2_encoder = {
|
||||
.alloc = kia_protocol_encoder_v2_alloc,
|
||||
.free = kia_protocol_encoder_v2_free,
|
||||
.deserialize = kia_protocol_encoder_v2_deserialize,
|
||||
.stop = kia_protocol_encoder_v2_stop,
|
||||
.yield = kia_protocol_encoder_v2_yield,
|
||||
const SubGhzProtocolEncoder subghz_protocol_kia_v2_encoder = {
|
||||
.alloc = subghz_protocol_encoder_kia_v2_alloc,
|
||||
.free = subghz_protocol_encoder_kia_v2_free,
|
||||
.deserialize = subghz_protocol_encoder_kia_v2_deserialize,
|
||||
.stop = subghz_protocol_encoder_kia_v2_stop,
|
||||
.yield = subghz_protocol_encoder_kia_v2_yield,
|
||||
};
|
||||
|
||||
const SubGhzProtocol subghz_protocol_kia_v2 = {
|
||||
.name = KIA_PROTOCOL_V2_NAME,
|
||||
.name = SUBGHZ_PROTOCOL_KIA_V2_NAME,
|
||||
.type = SubGhzProtocolTypeDynamic,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_433 | SubGhzProtocolFlag_FM |
|
||||
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
|
||||
SubGhzProtocolFlag_Send,
|
||||
.decoder = &kia_protocol_v2_decoder,
|
||||
.encoder = &kia_protocol_v2_encoder,
|
||||
.decoder = &subghz_protocol_kia_v2_decoder,
|
||||
.encoder = &subghz_protocol_kia_v2_encoder,
|
||||
};
|
||||
|
||||
static uint8_t kia_v2_calculate_crc(uint64_t data) {
|
||||
// Remove the CRC nibble (last 4 bits) to get the actual data
|
||||
uint64_t data_without_crc = data >> 4;
|
||||
|
||||
// Extract 6 bytes from the data
|
||||
uint8_t bytes[6];
|
||||
bytes[0] = (uint8_t)(data_without_crc);
|
||||
bytes[1] = (uint8_t)(data_without_crc >> 8);
|
||||
@@ -100,8 +90,21 @@ static uint8_t kia_v2_calculate_crc(uint64_t data) {
|
||||
return (crc + 1) & 0x0F;
|
||||
}
|
||||
|
||||
static void kia_protocol_encoder_v2_get_upload(SubGhzProtocolEncoderKiaV2* instance) {
|
||||
furi_check(instance);
|
||||
static void subghz_protocol_kia_v2_check_remote_controller(SubGhzProtocolDecoderKiaV2* instance) {
|
||||
instance->generic.serial = (uint32_t)((instance->generic.data >> 20) & 0xFFFFFFFF);
|
||||
instance->generic.btn = (uint8_t)((instance->generic.data >> 16) & 0x0F);
|
||||
|
||||
uint16_t raw_count = (uint16_t)((instance->generic.data >> 4) & 0xFFF);
|
||||
instance->generic.cnt = ((raw_count >> 4) | (raw_count << 8)) & 0xFFF;
|
||||
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
}
|
||||
|
||||
static void subghz_protocol_encoder_kia_v2_get_upload(SubGhzProtocolEncoderKiaV2* instance) {
|
||||
furi_assert(instance);
|
||||
size_t index = 0;
|
||||
|
||||
uint8_t crc = kia_v2_calculate_crc(instance->generic.data);
|
||||
@@ -110,44 +113,36 @@ static void kia_protocol_encoder_v2_get_upload(SubGhzProtocolEncoderKiaV2* insta
|
||||
for(uint8_t burst = 0; burst < KIA_V2_TOTAL_BURSTS; burst++) {
|
||||
for(int i = 0; i < KIA_V2_HEADER_PAIRS; i++) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v2_const.te_long);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v2_const.te_long);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)kia_protocol_v2_const.te_long);
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_v2_const.te_long);
|
||||
}
|
||||
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v2_const.te_short);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v2_const.te_short);
|
||||
|
||||
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
|
||||
bool bit = bit_read(instance->generic.data, i - 2);
|
||||
|
||||
if(bit) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)kia_protocol_v2_const.te_short);
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_v2_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v2_const.te_short);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v2_const.te_short);
|
||||
} else {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)kia_protocol_v2_const.te_short);
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_kia_v2_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)kia_protocol_v2_const.te_short);
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_kia_v2_const.te_short);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
instance->encoder.size_upload = index;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Upload built: %d bursts, size_upload=%zu, data_count_bit=%u, data=0x%016llX",
|
||||
KIA_V2_TOTAL_BURSTS,
|
||||
instance->encoder.size_upload,
|
||||
instance->generic.data_count_bit,
|
||||
instance->generic.data);
|
||||
}
|
||||
|
||||
void* kia_protocol_encoder_v2_alloc(SubGhzEnvironment* environment) {
|
||||
void* subghz_protocol_encoder_kia_v2_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderKiaV2* instance = malloc(sizeof(SubGhzProtocolEncoderKiaV2));
|
||||
|
||||
@@ -163,214 +158,115 @@ void* kia_protocol_encoder_v2_alloc(SubGhzEnvironment* environment) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v2_free(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_encoder_kia_v2_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKiaV2* instance = context;
|
||||
free(instance->encoder.upload);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_encoder_v2_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderKiaV2* instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
flipper_format_rewind(flipper_format);
|
||||
|
||||
do {
|
||||
FuriString* temp_str = furi_string_alloc();
|
||||
if(!flipper_format_read_string(flipper_format, "Protocol", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Missing Protocol");
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!furi_string_equal(temp_str, instance->base.protocol->name)) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Wrong protocol %s != %s",
|
||||
furi_string_get_cstr(temp_str),
|
||||
instance->base.protocol->name);
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
furi_string_free(temp_str);
|
||||
|
||||
uint32_t bit_count_temp;
|
||||
if(!flipper_format_read_uint32(flipper_format, "Bit", &bit_count_temp, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing Bit");
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data_count_bit = kia_protocol_v2_const.min_count_bit_for_found;
|
||||
|
||||
temp_str = furi_string_alloc();
|
||||
if(!flipper_format_read_string(flipper_format, "Key", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Missing Key");
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
const char* key_str = furi_string_get_cstr(temp_str);
|
||||
uint64_t key = 0;
|
||||
size_t str_len = strlen(key_str);
|
||||
size_t hex_pos = 0;
|
||||
for(size_t i = 0; i < str_len && hex_pos < 16; i++) {
|
||||
char c = key_str[i];
|
||||
if(c == ' ') continue;
|
||||
|
||||
uint8_t nibble;
|
||||
if(c >= '0' && c <= '9') {
|
||||
nibble = c - '0';
|
||||
} else if(c >= 'A' && c <= 'F') {
|
||||
nibble = c - 'A' + 10;
|
||||
} else if(c >= 'a' && c <= 'f') {
|
||||
nibble = c - 'a' + 10;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Invalid hex character: %c", c);
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
|
||||
key = (key << 4) | nibble;
|
||||
hex_pos++;
|
||||
}
|
||||
|
||||
furi_string_free(temp_str);
|
||||
|
||||
if(hex_pos != 16) {
|
||||
FURI_LOG_E(TAG, "Invalid key length: %zu nibbles (expected 16)", hex_pos);
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data = key;
|
||||
FURI_LOG_I(TAG, "Parsed key: 0x%016llX", instance->generic.data);
|
||||
|
||||
if(instance->generic.data == 0) {
|
||||
FURI_LOG_E(TAG, "Key is zero after parsing!");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_format_read_uint32(flipper_format, "Serial", &instance->generic.serial, 1)) {
|
||||
instance->generic.serial = (uint32_t)((instance->generic.data >> 20) & 0xFFFFFFFF);
|
||||
FURI_LOG_I(TAG, "Extracted serial: 0x%08lX", instance->generic.serial);
|
||||
} else {
|
||||
FURI_LOG_I(TAG, "Read serial: 0x%08lX", instance->generic.serial);
|
||||
}
|
||||
|
||||
uint32_t btn_temp;
|
||||
if(flipper_format_read_uint32(flipper_format, "Btn", &btn_temp, 1)) {
|
||||
instance->generic.btn = (uint8_t)btn_temp;
|
||||
} else {
|
||||
instance->generic.btn = (uint8_t)((instance->generic.data >> 16) & 0x0F);
|
||||
}
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
instance->generic.btn = kia_v2_get_btn_code();
|
||||
|
||||
uint32_t cnt_temp;
|
||||
if(flipper_format_read_uint32(flipper_format, "Cnt", &cnt_temp, 1)) {
|
||||
instance->generic.cnt = (uint16_t)cnt_temp;
|
||||
FURI_LOG_I(TAG, "Read counter: 0x%03lX", (unsigned long)instance->generic.cnt);
|
||||
} else {
|
||||
uint16_t raw_count = (uint16_t)((instance->generic.data >> 4) & 0xFFF);
|
||||
instance->generic.cnt = ((raw_count >> 4) | (raw_count << 8)) & 0xFFF;
|
||||
FURI_LOG_I(TAG, "Extracted counter: 0x%03lX", (unsigned long)instance->generic.cnt);
|
||||
}
|
||||
|
||||
uint64_t new_data = 0;
|
||||
|
||||
new_data |= 1ULL << 52;
|
||||
|
||||
new_data |= ((uint64_t)instance->generic.serial << 20) & 0xFFFFFFFFF00000ULL;
|
||||
|
||||
uint32_t uVar6 = ((uint32_t)(instance->generic.cnt & 0xFF) << 8) |
|
||||
((uint32_t)(instance->generic.btn & 0x0F) << 16) |
|
||||
((uint32_t)(instance->generic.cnt >> 4) & 0xF0);
|
||||
|
||||
new_data |= (uint64_t)uVar6;
|
||||
|
||||
instance->generic.data = new_data;
|
||||
instance->generic.data_count_bit = 53;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Encoder reconstruct: serial=0x%08lX, btn=0x%X, cnt=0x%03lX, uVar6=0x%05lX, data=0x%016llX",
|
||||
(unsigned long)instance->generic.serial,
|
||||
(unsigned int)instance->generic.btn,
|
||||
(unsigned long)instance->generic.cnt,
|
||||
(unsigned long)uVar6,
|
||||
(unsigned long long)instance->generic.data);
|
||||
|
||||
if(!flipper_format_read_uint32(
|
||||
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1)) {
|
||||
instance->encoder.repeat = 10;
|
||||
FURI_LOG_D(TAG, "Repeat not found in file, using default 10");
|
||||
}
|
||||
|
||||
kia_protocol_encoder_v2_get_upload(instance);
|
||||
|
||||
instance->encoder.is_running = true;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Encoder deserialized: repeat=%u, size_upload=%zu, is_running=%d, front=%zu",
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.size_upload,
|
||||
instance->encoder.is_running,
|
||||
instance->encoder.front);
|
||||
|
||||
ret = SubGhzProtocolStatusOk;
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void kia_protocol_encoder_v2_stop(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_encoder_kia_v2_stop(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKiaV2* instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
|
||||
LevelDuration kia_protocol_encoder_v2_yield(void* context) {
|
||||
furi_check(context);
|
||||
LevelDuration subghz_protocol_encoder_kia_v2_yield(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKiaV2* instance = context;
|
||||
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Encoder yield stopped: repeat=%u, is_running=%d",
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.is_running);
|
||||
instance->encoder.is_running = false;
|
||||
return level_duration_reset();
|
||||
}
|
||||
|
||||
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
|
||||
|
||||
if(instance->encoder.front < 5 || instance->encoder.front == 0) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Encoder yield[%zu]: repeat=%u, size=%zu, level=%d, duration=%lu",
|
||||
instance->encoder.front,
|
||||
instance->encoder.repeat,
|
||||
instance->encoder.size_upload,
|
||||
level_duration_get_level(ret),
|
||||
level_duration_get_duration(ret));
|
||||
}
|
||||
|
||||
if(++instance->encoder.front == instance->encoder.size_upload) {
|
||||
instance->encoder.repeat--;
|
||||
instance->encoder.front = 0;
|
||||
FURI_LOG_I(
|
||||
TAG, "Encoder completed one cycle, remaining repeat=%u", instance->encoder.repeat);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* kia_protocol_decoder_v2_alloc(SubGhzEnvironment* environment) {
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_kia_v2_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderKiaV2* instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
do {
|
||||
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
if(ret != SubGhzProtocolStatusOk) {
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data_count_bit = subghz_protocol_kia_v2_const.min_count_bit_for_found;
|
||||
|
||||
instance->generic.serial = (uint32_t)((instance->generic.data >> 20) & 0xFFFFFFFF);
|
||||
instance->generic.btn = (uint8_t)((instance->generic.data >> 16) & 0x0F);
|
||||
|
||||
uint16_t raw_count = (uint16_t)((instance->generic.data >> 4) & 0xFFF);
|
||||
instance->generic.cnt = ((raw_count >> 4) | (raw_count << 8)) & 0xFFF;
|
||||
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
if(instance->generic.cnt < 0xFFF) {
|
||||
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
|
||||
if(instance->generic.cnt > 0xFFF) {
|
||||
instance->generic.cnt = 0;
|
||||
}
|
||||
} else {
|
||||
instance->generic.cnt = 0;
|
||||
}
|
||||
|
||||
uint8_t btn = subghz_custom_btn_get();
|
||||
if(btn != SUBGHZ_CUSTOM_BTN_OK) {
|
||||
instance->generic.btn = btn;
|
||||
}
|
||||
|
||||
uint64_t bit52 = instance->generic.data & (1ULL << 52);
|
||||
|
||||
uint64_t new_data = 0;
|
||||
new_data |= bit52;
|
||||
new_data |= ((uint64_t)instance->generic.serial << 20) & 0x000FFFFFFFF00000ULL;
|
||||
|
||||
uint32_t uVar6 = ((uint32_t)(instance->generic.cnt & 0xFF) << 8) |
|
||||
((uint32_t)(instance->generic.btn & 0x0F) << 16) |
|
||||
((uint32_t)(instance->generic.cnt >> 4) & 0xF0);
|
||||
new_data |= (uint64_t)uVar6;
|
||||
|
||||
instance->generic.data = new_data;
|
||||
instance->generic.data_count_bit = 53;
|
||||
|
||||
subghz_protocol_encoder_kia_v2_get_upload(instance);
|
||||
|
||||
if(!flipper_format_rewind(flipper_format)) {
|
||||
ret = SubGhzProtocolStatusErrorParserOthers;
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
for(size_t i = 0; i < sizeof(uint64_t); i++) {
|
||||
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> (i * 8)) & 0xFF;
|
||||
}
|
||||
if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
ret = SubGhzProtocolStatusErrorParserKey;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->encoder.is_running = true;
|
||||
ret = SubGhzProtocolStatusOk;
|
||||
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* subghz_protocol_decoder_kia_v2_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolDecoderKiaV2* instance = malloc(sizeof(SubGhzProtocolDecoderKiaV2));
|
||||
instance->base.protocol = &subghz_protocol_kia_v2;
|
||||
@@ -378,14 +274,14 @@ void* kia_protocol_decoder_v2_alloc(SubGhzEnvironment* environment) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v2_free(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_decoder_kia_v2_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v2_reset(void* context) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_decoder_kia_v2_reset(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
instance->decoder.parser_step = KiaV2DecoderStepReset;
|
||||
instance->header_count = 0;
|
||||
@@ -394,35 +290,28 @@ void kia_protocol_decoder_v2_reset(void* context) {
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_check(context);
|
||||
void subghz_protocol_decoder_kia_v2_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
|
||||
switch(instance->decoder.parser_step) {
|
||||
case KiaV2DecoderStepReset:
|
||||
if((level) && (DURATION_DIFF(duration, kia_protocol_v2_const.te_long) <
|
||||
kia_protocol_v2_const.te_delta)) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_long) < subghz_protocol_kia_v2_const.te_delta) {
|
||||
instance->decoder.parser_step = KiaV2DecoderStepCheckPreamble;
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count = 0;
|
||||
manchester_advance(
|
||||
instance->manchester_state,
|
||||
ManchesterEventReset,
|
||||
&instance->manchester_state,
|
||||
NULL);
|
||||
manchester_advance(instance->manchester_state, ManchesterEventReset,
|
||||
&instance->manchester_state, NULL);
|
||||
}
|
||||
break;
|
||||
|
||||
case KiaV2DecoderStepCheckPreamble:
|
||||
if(level) // HIGH pulse
|
||||
{
|
||||
if(DURATION_DIFF(duration, kia_protocol_v2_const.te_long) <
|
||||
kia_protocol_v2_const.te_delta) {
|
||||
if(level) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_long) <
|
||||
subghz_protocol_kia_v2_const.te_delta) {
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count++;
|
||||
} else if(
|
||||
DURATION_DIFF(duration, kia_protocol_v2_const.te_short) <
|
||||
kia_protocol_v2_const.te_delta) {
|
||||
} else if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_short) <
|
||||
subghz_protocol_kia_v2_const.te_delta) {
|
||||
if(instance->header_count >= 100) {
|
||||
instance->header_count = 0;
|
||||
instance->decoder.decode_data = 0;
|
||||
@@ -436,13 +325,12 @@ void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration)
|
||||
instance->decoder.parser_step = KiaV2DecoderStepReset;
|
||||
}
|
||||
} else {
|
||||
if(DURATION_DIFF(duration, kia_protocol_v2_const.te_long) <
|
||||
kia_protocol_v2_const.te_delta) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_long) <
|
||||
subghz_protocol_kia_v2_const.te_delta) {
|
||||
instance->header_count++;
|
||||
instance->decoder.te_last = duration;
|
||||
} else if(
|
||||
DURATION_DIFF(duration, kia_protocol_v2_const.te_short) <
|
||||
kia_protocol_v2_const.te_delta) {
|
||||
} else if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_short) <
|
||||
subghz_protocol_kia_v2_const.te_delta) {
|
||||
instance->decoder.te_last = duration;
|
||||
} else {
|
||||
instance->decoder.parser_step = KiaV2DecoderStepReset;
|
||||
@@ -453,12 +341,11 @@ void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration)
|
||||
case KiaV2DecoderStepCollectRawBits: {
|
||||
ManchesterEvent event;
|
||||
|
||||
if(DURATION_DIFF(duration, kia_protocol_v2_const.te_short) <
|
||||
kia_protocol_v2_const.te_delta) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_short) <
|
||||
subghz_protocol_kia_v2_const.te_delta) {
|
||||
event = level ? ManchesterEventShortLow : ManchesterEventShortHigh;
|
||||
} else if(
|
||||
DURATION_DIFF(duration, kia_protocol_v2_const.te_long) <
|
||||
kia_protocol_v2_const.te_delta) {
|
||||
} else if(DURATION_DIFF(duration, subghz_protocol_kia_v2_const.te_long) <
|
||||
subghz_protocol_kia_v2_const.te_delta) {
|
||||
event = level ? ManchesterEventLongLow : ManchesterEventLongHigh;
|
||||
} else {
|
||||
instance->decoder.parser_step = KiaV2DecoderStepReset;
|
||||
@@ -466,8 +353,8 @@ void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration)
|
||||
}
|
||||
|
||||
bool data_bit;
|
||||
if(manchester_advance(
|
||||
instance->manchester_state, event, &instance->manchester_state, &data_bit)) {
|
||||
if(manchester_advance(instance->manchester_state, event,
|
||||
&instance->manchester_state, &data_bit)) {
|
||||
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data_bit;
|
||||
instance->decoder.decode_count_bit++;
|
||||
|
||||
@@ -488,6 +375,7 @@ void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration)
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
instance->header_count = 0;
|
||||
instance->decoder.parser_step = KiaV2DecoderStepReset;
|
||||
manchester_advance(instance->manchester_state, ManchesterEventReset, &instance->manchester_state, NULL);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -495,67 +383,66 @@ void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration)
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t kia_protocol_decoder_v2_get_hash_data(void* context) {
|
||||
furi_check(context);
|
||||
uint8_t subghz_protocol_decoder_kia_v2_get_hash_data(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(
|
||||
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
|
||||
|
||||
uint32_t hash = instance->generic.serial;
|
||||
hash ^= (instance->generic.btn << 24);
|
||||
hash ^= (instance->generic.cnt << 12);
|
||||
|
||||
return (uint8_t)(hash ^ (hash >> 8) ^ (hash >> 16) ^ (hash >> 24));
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus kia_protocol_decoder_v2_serialize(
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v2_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
|
||||
SubGhzProtocolStatus ret =
|
||||
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
uint32_t crc = instance->generic.data & 0x0F;
|
||||
flipper_format_write_uint32(flipper_format, "CRC", &crc, 1);
|
||||
flipper_format_write_uint32(flipper_format, "Serial", &instance->generic.serial, 1);
|
||||
uint32_t temp = instance->generic.btn;
|
||||
flipper_format_write_uint32(flipper_format, "Btn", &temp, 1);
|
||||
flipper_format_write_uint32(flipper_format, "Cnt", &instance->generic.cnt, 1);
|
||||
uint32_t raw_count = (uint32_t)((instance->generic.data >> 4) & 0xFFF);
|
||||
flipper_format_write_uint32(flipper_format, "RawCnt", &raw_count, 1);
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_decoder_v2_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v2_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
return subghz_block_generic_deserialize_check_count_bit(
|
||||
&instance->generic, flipper_format, kia_protocol_v2_const.min_count_bit_for_found);
|
||||
&instance->generic, flipper_format, subghz_protocol_kia_v2_const.min_count_bit_for_found);
|
||||
}
|
||||
|
||||
void kia_protocol_decoder_v2_get_string(void* context, FuriString* output) {
|
||||
furi_check(context);
|
||||
static const char* subghz_protocol_kia_v2_get_name_button(uint8_t btn) {
|
||||
switch(btn) {
|
||||
case 0x1: return "Lock";
|
||||
case 0x2: return "Unlock";
|
||||
case 0x3: return "Trunk";
|
||||
case 0x4: return "Panic";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_kia_v2_get_string(void* context, FuriString* output) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderKiaV2* instance = context;
|
||||
subghz_custom_btn_set_original(instance->generic.btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
uint8_t crc = instance->generic.data & 0x0F;
|
||||
|
||||
bool crc_valid = crc == kia_v2_calculate_crc(instance->generic.data);
|
||||
subghz_protocol_kia_v2_check_remote_controller(instance);
|
||||
uint8_t crc_received = instance->generic.data & 0x0F;
|
||||
uint8_t crc_calculated = kia_v2_calculate_crc(instance->generic.data);
|
||||
bool crc_ok = (crc_received == crc_calculated);
|
||||
|
||||
furi_string_cat_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%013llX\r\n"
|
||||
"Sn:%08lX Btn:%X\r\n"
|
||||
"Cnt:%03lX CRC:%X - %s\r\n",
|
||||
"Sn:%08lX Cnt:%03lX\r\n"
|
||||
"Btn:%02X:[%s]\r\n"
|
||||
"CRC:%X %s",
|
||||
instance->generic.protocol_name,
|
||||
instance->generic.data_count_bit,
|
||||
instance->generic.data,
|
||||
instance->generic.serial,
|
||||
kia_v2_get_btn_code(),
|
||||
instance->generic.cnt,
|
||||
crc,
|
||||
crc_valid ? "OK" : "BAD");
|
||||
instance->generic.btn,
|
||||
subghz_protocol_kia_v2_get_name_button(instance->generic.btn),
|
||||
crc_received,
|
||||
crc_ok ? "(OK)" : "(FAIL)");
|
||||
}
|
||||
|
||||
@@ -1,34 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "kia_generic.h"
|
||||
#include "base.h"
|
||||
#include "../blocks/math.h"
|
||||
#include <lib/toolbox/manchester_decoder.h>
|
||||
|
||||
|
||||
#define KIA_PROTOCOL_V2_NAME "KIA/HYU V2"
|
||||
#define SUBGHZ_PROTOCOL_KIA_V2_NAME "KIA/HYU V2"
|
||||
|
||||
typedef struct SubGhzProtocolDecoderKiaV2 SubGhzProtocolDecoderKiaV2;
|
||||
typedef struct SubGhzProtocolEncoderKiaV2 SubGhzProtocolEncoderKiaV2;
|
||||
|
||||
extern const SubGhzProtocolDecoder kia_protocol_v2_decoder;
|
||||
extern const SubGhzProtocolEncoder kia_protocol_v2_encoder;
|
||||
extern const SubGhzProtocol subghz_protocol_kia_v2;
|
||||
|
||||
void* kia_protocol_decoder_v2_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_decoder_v2_free(void* context);
|
||||
void kia_protocol_decoder_v2_reset(void* context);
|
||||
void kia_protocol_decoder_v2_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t kia_protocol_decoder_v2_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus kia_protocol_decoder_v2_serialize(
|
||||
void* subghz_protocol_decoder_kia_v2_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_decoder_kia_v2_free(void* context);
|
||||
void subghz_protocol_decoder_kia_v2_reset(void* context);
|
||||
void subghz_protocol_decoder_kia_v2_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t subghz_protocol_decoder_kia_v2_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v2_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_decoder_v2_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_decoder_v2_get_string(void* context, FuriString* output);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v2_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_kia_v2_get_string(void* context, FuriString* output);
|
||||
|
||||
void* kia_protocol_encoder_v2_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_encoder_v2_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_encoder_v2_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_encoder_v2_stop(void* context);
|
||||
LevelDuration kia_protocol_encoder_v2_yield(void* context);
|
||||
void* subghz_protocol_encoder_kia_v2_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_kia_v2_free(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_kia_v2_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_kia_v2_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_kia_v2_yield(void* context);
|
||||
|
||||
@@ -424,6 +424,11 @@ SubGhzProtocolStatus
|
||||
|
||||
furi_string_free(temp_str);
|
||||
|
||||
if(hex_pos != 16) {
|
||||
FURI_LOG_E(TAG, "Invalid key length: %zu nibbles", hex_pos);
|
||||
break;
|
||||
}
|
||||
|
||||
instance->generic.data = key;
|
||||
|
||||
flipper_format_rewind(flipper_format);
|
||||
|
||||
Reference in New Issue
Block a user