mirror of
https://github.com/D4C1-Labs/Flipper-ARF.git
synced 2026-03-29 19:49:59 +00:00
Compare commits
21 Commits
dev-2d8f35
...
dev-03897a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03897a406e | ||
|
|
09a7668fe7 | ||
|
|
76fbf79bff | ||
|
|
bafe135a56 | ||
|
|
77b58feb92 | ||
|
|
57dafbc76d | ||
|
|
e116abaa9b | ||
|
|
fd9564e301 | ||
|
|
de133ebe09 | ||
|
|
fc03342591 | ||
|
|
bfdf60944f | ||
|
|
0290f601a0 | ||
|
|
2e5648f3f4 | ||
|
|
cffd268950 | ||
|
|
ddb85d034f | ||
|
|
55f770328c | ||
|
|
75a5334a9b | ||
|
|
696041410b | ||
|
|
72d3992092 | ||
|
|
c1d145c9cc | ||
|
|
6507bed882 |
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
|
||||
|
||||
@@ -1,128 +1,94 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
# Flipper-ARF Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, religion, or sexual identity
|
||||
and orientation.
|
||||
We as members, contributors, and maintainers of Flipper-ARF pledge to make participation in this project a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming, and responsible research community.
|
||||
|
||||
## Our Standards
|
||||
## Ethical Research Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
Flipper-ARF is an automotive security research project. All contributions, discussions, and use of this project must adhere to the following ethical standards:
|
||||
|
||||
* Demonstrating empathy and kindness toward other people
|
||||
1. **Lawful purpose only.** All work must be for lawful, educational, or explicitly authorized security research purposes. Contributors must comply with all applicable local, national, and international laws.
|
||||
|
||||
2. **No unauthorized access.** Do not use this firmware or any knowledge gained from it to access vehicles, devices, or systems without explicit authorization from the owner.
|
||||
|
||||
3. **Responsible disclosure.** If your research reveals a vulnerability in a manufacturer's system, follow responsible disclosure practices — notify the manufacturer and allow reasonable time for remediation before any public disclosure.
|
||||
|
||||
4. **Key material handling.** Do not share manufacturer-specific cryptographic keys, seeds, or proprietary algorithms outside the scope of this project's research goals. Key material included in the project is for protocol interoperability research only.
|
||||
|
||||
5. **Authorized captures only.** Signal captures and key recordings submitted to the project should come from researcher-owned vehicles or devices, or be obtained with explicit written permission from the owner.
|
||||
|
||||
6. **No enabling of criminal activity.** We do not condone/support/endorse vehicle theft, unauthorized entry, tracking, surveillance, or any other criminal activity.
|
||||
|
||||
7. **Radio frequency compliance.** Comply with radio frequency regulations in your jurisdiction. Transmission testing should be conducted in controlled environments or within legally permitted parameters.
|
||||
|
||||
## Community Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment:
|
||||
|
||||
* Sharing well-documented protocol analysis and research findings
|
||||
* Providing detailed capture data with proper context (vehicle, method, conditions)
|
||||
* Being respectful of differing opinions, viewpoints, and experiences
|
||||
* Giving and gracefully accepting constructive feedback
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
* Focusing on what is best not just for us as individuals, but for the
|
||||
overall community
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
Examples of unacceptable behavior:
|
||||
|
||||
* The use of sexualized language or imagery, and sexual attention or
|
||||
advances of any kind
|
||||
* Sharing techniques specifically intended to facilitate vehicle theft or unauthorized access
|
||||
* The use of sexualized language or imagery, and sexual attention or advances of any kind
|
||||
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or email
|
||||
address, without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
* Publishing others' private information without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional or research setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
Project maintainers are responsible for clarifying and enforcing these standards and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, harmful, or in violation of the ethical research standards above.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
Maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official e-mail address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
This Code of Conduct applies within all project spaces, including the repository, issue tracker, pull requests, and any associated communication channels. It also applies when an individual is representing the project in public spaces.
|
||||
|
||||
## Enforcement
|
||||
## Reporting
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
hello@flipperdevices.com.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
Instances of abusive, harassing, unethical, or otherwise unacceptable behavior may be reported by opening a confidential issue on the project's GitHub repository or by contacting the maintainers directly through GitHub.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
All complaints will be reviewed and investigated promptly and fairly. All maintainers are obligated to respect the privacy and security of the reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
Maintainers will follow these guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
**Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
**Consequence**: A private, written warning providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series
|
||||
of actions.
|
||||
**Impact**: A violation through a single incident or series of actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or
|
||||
permanent ban.
|
||||
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. Violating these terms may lead to a temporary or permanent ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
**Impact**: A serious violation of community or ethical research standards, including sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
**Consequence**: A temporary ban from any sort of interaction or public communication with the project for a specified period of time. Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
**Impact**: Demonstrating a pattern of violation of community or ethical standards, including sustained inappropriate behavior, harassment, or using the project to enable criminal activity.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within
|
||||
the community.
|
||||
**Consequence**: A permanent ban from any sort of public interaction within the project.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.0, available at
|
||||
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
||||
This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 2.0, with additional ethical research guidelines specific to the Flipper-ARF project.
|
||||
|
||||
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
||||
enforcement ladder](https://github.com/mozilla/diversity).
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
https://www.contributor-covenant.org/faq. Translations are available at
|
||||
https://www.contributor-covenant.org/translations.
|
||||
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
||||
|
||||
114
README.md
114
README.md
@@ -10,6 +10,22 @@ This project may incorporate, adapt, or build upon **other open-source projects*
|
||||
|
||||
---
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Showcase](#showcase)
|
||||
- [Supported Systems](#supported-systems)
|
||||
- [How to Build](#how-to-build)
|
||||
- [Project Scope](#project-scope)
|
||||
- [Implemented Protocols](#implemented-protocols)
|
||||
- [To Do / Planned Features](#to-do--planned-features)
|
||||
- [Design Philosophy](#design-philosophy)
|
||||
- [Research Direction](#research-direction)
|
||||
- [Contribution Policy](#contribution-policy)
|
||||
- [Citations & References](#citations--references)
|
||||
- [Disclaimer](#disclaimer)
|
||||
|
||||
---
|
||||
|
||||
## Showcase
|
||||
|
||||
| | |
|
||||
@@ -34,7 +50,7 @@ This project may incorporate, adapt, or build upon **other open-source projects*
|
||||
| PSA (Peugeot/Citroën/DS) | PSA GROUP | 433 MHz | AM/FM | Yes | Yes |
|
||||
| Ford | Ford V0 | 315/433 MHz | AM | Yes | Yes |
|
||||
| Fiat | Fiat SpA | 433 MHz | AM | Yes | Yes |
|
||||
| Fiat | Fiat Mystery | 433 MHz | AM | No | Yes |
|
||||
| Fiat | Fiat Marelli | 433 MHz | AM | No | Yes |
|
||||
| Subaru | Subaru | 433 MHz | AM | Yes | Yes |
|
||||
| Mazda | Siemens (5WK49365D) | 315/433 MHz | FM | Yes | Yes |
|
||||
| Kia/Hyundai | Kia V0 | 433 MHz | FM | Yes | Yes |
|
||||
@@ -123,14 +139,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] RollJam app (Internal CC1101 for RX & TX captured signal; External CC1101 for jamming) — requires more real-world testing
|
||||
- [x] Brute force of counter in saved → can be accellerated trough the companion app via bluetooth
|
||||
- [X] Keeloq Key Manager inside firmware
|
||||
- [x] RollJam app (Internal CC1101 for RX & TX captured signal; External CC1101 for jamming) — requires more real-world testing (no available yet)
|
||||
|
||||
---
|
||||
|
||||
## To Do / Planned Features
|
||||
|
||||
- [ ] 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
|
||||
@@ -175,7 +191,95 @@ Contributions are welcome if they:
|
||||
> Non-automotive features are considered out-of-scope for now.
|
||||
|
||||
### This code is a mess!
|
||||

|
||||

|
||||
---
|
||||
|
||||
## 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)
|
||||
|
||||
- **SoK: Stealing Cars Since Remote Keyless Entry Introduction and How to Defend From It**
|
||||
Tommaso Bianchi, Alessandro Brighente, Mauro Conti, Edoardo Pavan — University of Padova / Delft University of Technology
|
||||
*arXiv, 2025*
|
||||
https://arxiv.org/pdf/2505.02713
|
||||
|
||||
- **Security of Automotive Systems**
|
||||
Lennert Wouters, Benedikt Gierlichs, Bart Preneel
|
||||
*Wiley, February 2025*
|
||||
DOI: [10.1002/9781394351930.ch11](https://doi.org/10.1002/9781394351930.ch11)
|
||||
|
||||
### KeeLoq Cryptanalysis
|
||||
|
||||
- **Cryptanalysis of the KeeLoq Block Cipher**
|
||||
Andrey Bogdanov
|
||||
*Cryptology ePrint Archive, Paper 2007/055*
|
||||
https://eprint.iacr.org/2007/055
|
||||
|
||||
- **On the Power of Power Analysis in the Real World: A Complete Break of the KeeLoq Code Hopping Scheme**
|
||||
Thomas Eisenbarth, Timo Kasper, Amir Moradi, Christof Paar, Mahmoud Salmasizadeh, Mohammad T. Manzuri Shalmani
|
||||
*CRYPTO 2008*
|
||||
https://www.iacr.org/archive/crypto2008/51570204/51570204.pdf
|
||||
|
||||
- **A Practical Attack on KeeLoq**
|
||||
Sebastiaan Indesteege, Nathan Keller, Orr Dunkelman, Eli Biham, Bart Preneel
|
||||
*EUROCRYPT 2008*
|
||||
https://www.iacr.org/archive/eurocrypt2008/49650001/49650001.pdf
|
||||
|
||||
- **Breaking KeeLoq in a Flash: On Extracting Keys at Lightning Speed**
|
||||
*Springer*
|
||||
DOI: [10.1007/978-3-642-02384-2_25](https://doi.org/10.1007/978-3-642-02384-2_25)
|
||||
|
||||
### 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)
|
||||
|
||||
- **RollBack: A New Time-Agnostic Replay Attack Against the Automotive Remote Keyless Entry Systems**
|
||||
Levente Csikor, Hoon Wei Lim, Jun Wen Wong, Soundarya Ramesh, Rohini Poolat Parameswarath, Mun Choon Chan
|
||||
*ACM*
|
||||
DOI: [10.1145/3627827](https://doi.org/10.1145/3627827)
|
||||
|
||||
- **Relay Attacks on Passive Keyless Entry and Start Systems in Modern Cars**
|
||||
Aurelien Francillon, Boris Danev, Srdjan Capkun
|
||||
*NDSS 2011*
|
||||
https://www.ndss-symposium.org/ndss2011/relay-attacks-on-passive-keyless-entry-and-start-systems-in-modern-cars/
|
||||
|
||||
---
|
||||
|
||||
# Disclaimer
|
||||
|
||||
@@ -4,18 +4,26 @@
|
||||
|
||||
#define TAG "FiatMystery"
|
||||
|
||||
// Fiat Panda "pandarella e tonino":
|
||||
// te_short ~260us, te_long ~520us (Manchester)
|
||||
// Suspected Magneti Marelli BSI keyfob protocol
|
||||
// Found on: Fiat Panda (and possibly other Fiat/Lancia/Alfa ~2003-2012)
|
||||
//
|
||||
// RF: 433.92 MHz, Manchester encoding
|
||||
// te_short ~260us, te_long ~520us
|
||||
// Preamble: ~191 short-short pairs (alternating 260us HIGH/LOW)
|
||||
// Gap: ~3126us LOW
|
||||
// Sync: ~2065us HIGH
|
||||
// Data: 86 Manchester bits
|
||||
// Retransmissions: 7-8 per press
|
||||
// Data: 88 Manchester bits (often decoded as 104 with 16-bit 0xFFFF preamble residue)
|
||||
// Retransmissions: 7-10 per press
|
||||
//
|
||||
// Frame layout (after stripping 16-bit 0xFFFF preamble):
|
||||
// Bytes 0-3: Fixed ID / Serial (32 bits)
|
||||
// Byte 4: Button (upper nibble) | Type (lower nibble, always 0x2)
|
||||
// Bytes 5-10: Rolling/encrypted code (48 bits)
|
||||
#define FIAT_MYSTERY_PREAMBLE_MIN 200 // Min preamble pulses (100 pairs)
|
||||
#define FIAT_MYSTERY_GAP_MIN 2500 // Gap detection threshold (us)
|
||||
#define FIAT_MYSTERY_SYNC_MIN 1500 // Sync pulse minimum (us)
|
||||
#define FIAT_MYSTERY_SYNC_MAX 2600 // Sync pulse maximum (us)
|
||||
#define FIAT_MYSTERY_MAX_DATA_BITS 96 // Max data bits to collect
|
||||
#define FIAT_MYSTERY_MAX_DATA_BITS 104 // Max data bits to collect (13 bytes)
|
||||
|
||||
static const SubGhzBlockConst subghz_protocol_fiat_mystery_const = {
|
||||
.te_short = 260,
|
||||
@@ -31,7 +39,7 @@ struct SubGhzProtocolDecoderFiatMystery {
|
||||
ManchesterState manchester_state;
|
||||
uint8_t decoder_state;
|
||||
uint16_t preamble_count;
|
||||
uint8_t raw_data[12]; // Up to 96 bits (12 bytes)
|
||||
uint8_t raw_data[13]; // Up to 104 bits (13 bytes)
|
||||
uint8_t bit_count;
|
||||
uint32_t extra_data; // Bits beyond first 64, right-aligned
|
||||
uint32_t te_last;
|
||||
@@ -292,13 +300,20 @@ void subghz_protocol_decoder_fiat_mystery_feed(void* context, bool level, uint32
|
||||
if(frame_complete) {
|
||||
instance->generic.data_count_bit = instance->bit_count;
|
||||
|
||||
// Frame layout: bytes 0-1 are 0xFFFF preamble residue
|
||||
// Bytes 2-5: Fixed ID (serial)
|
||||
// Byte 6: Button (upper nibble) | subtype (lower nibble)
|
||||
// Bytes 7-12: Rolling/encrypted code (48 bits)
|
||||
instance->generic.serial =
|
||||
((uint32_t)instance->raw_data[4] << 24) |
|
||||
((uint32_t)instance->raw_data[5] << 16) |
|
||||
((uint32_t)instance->raw_data[6] << 8) |
|
||||
((uint32_t)instance->raw_data[7]);
|
||||
instance->generic.cnt = (uint32_t)(instance->generic.data >> 32);
|
||||
instance->generic.btn = 0;
|
||||
((uint32_t)instance->raw_data[2] << 24) |
|
||||
((uint32_t)instance->raw_data[3] << 16) |
|
||||
((uint32_t)instance->raw_data[4] << 8) |
|
||||
((uint32_t)instance->raw_data[5]);
|
||||
instance->generic.btn = (instance->raw_data[6] >> 4) & 0xF;
|
||||
instance->generic.cnt =
|
||||
((uint32_t)instance->raw_data[7] << 16) |
|
||||
((uint32_t)instance->raw_data[8] << 8) |
|
||||
((uint32_t)instance->raw_data[9]);
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
@@ -385,22 +400,41 @@ SubGhzProtocolStatus subghz_protocol_decoder_fiat_mystery_deserialize(
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const char* fiat_mystery_button_name(uint8_t btn) {
|
||||
switch(btn) {
|
||||
case 0x2:
|
||||
return "Btn A";
|
||||
case 0x4:
|
||||
return "Btn B";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_fiat_mystery_get_string(void* context, FuriString* output) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderFiatMystery* instance = context;
|
||||
|
||||
uint8_t total_bytes = (instance->bit_count + 7) / 8;
|
||||
if(total_bytes > 12) total_bytes = 12;
|
||||
if(total_bytes > 13) total_bytes = 13;
|
||||
|
||||
furi_string_cat_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%08lX%08lX\r\n"
|
||||
"Sn:%08lX Btn:%s(0x%X)\r\n"
|
||||
"Roll:%02X%02X%02X%02X%02X%02X\r\n"
|
||||
"Data:",
|
||||
instance->generic.protocol_name,
|
||||
instance->bit_count,
|
||||
(uint32_t)(instance->generic.data >> 32),
|
||||
(uint32_t)(instance->generic.data & 0xFFFFFFFF));
|
||||
instance->generic.serial,
|
||||
fiat_mystery_button_name(instance->generic.btn),
|
||||
instance->generic.btn,
|
||||
instance->raw_data[7],
|
||||
instance->raw_data[8],
|
||||
instance->raw_data[9],
|
||||
(total_bytes > 10) ? instance->raw_data[10] : 0,
|
||||
(total_bytes > 11) ? instance->raw_data[11] : 0,
|
||||
(total_bytes > 12) ? instance->raw_data[12] : 0);
|
||||
|
||||
for(uint8_t i = 0; i < total_bytes; i++) {
|
||||
furi_string_cat_printf(output, "%02X", instance->raw_data[i]);
|
||||
|
||||
@@ -1,20 +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 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,
|
||||
@@ -23,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 {
|
||||
@@ -51,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,
|
||||
@@ -73,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,
|
||||
};
|
||||
@@ -94,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++) {
|
||||
@@ -119,7 +102,7 @@ static uint8_t kia_calculate_crc(uint64_t data) {
|
||||
crc_data[3] = (data >> 24) & 0xFF;
|
||||
crc_data[4] = (data >> 16) & 0xFF;
|
||||
crc_data[5] = (data >> 8) & 0xFF;
|
||||
|
||||
|
||||
return kia_crc8(crc_data, 6);
|
||||
}
|
||||
|
||||
@@ -129,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);
|
||||
}
|
||||
|
||||
@@ -148,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->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;
|
||||
|
||||
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;
|
||||
}
|
||||
if(subghz_custom_btn_get_original() == 0)
|
||||
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);
|
||||
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);
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
|
||||
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;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -539,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) {
|
||||
@@ -567,7 +407,7 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
instance->header_count = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case KIADecoderStepCheckPreambula:
|
||||
if(level) {
|
||||
if((DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
|
||||
@@ -583,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(
|
||||
@@ -590,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;
|
||||
}
|
||||
@@ -604,34 +444,26 @@ void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t durati
|
||||
instance->decoder.parser_step = KIADecoderStepReset;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case KIADecoderStepSaveDuration:
|
||||
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");
|
||||
if(instance->base.callback)
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
} else {
|
||||
FURI_LOG_W(TAG, "Signal received but CRC mismatch!");
|
||||
FURI_LOG_W(TAG, "CRC verification failed, packet rejected");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
break;
|
||||
@@ -639,11 +471,12 @@ 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;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case KIADecoderStepCheckDuration:
|
||||
if(!level) {
|
||||
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
|
||||
@@ -660,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 {
|
||||
@@ -675,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);
|
||||
@@ -692,46 +538,40 @@ SubGhzProtocolStatus subghz_protocol_decoder_kia_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_check(context);
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
furi_assert(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);
|
||||
|
||||
SubGhzProtocolStatus ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
if(instance->generic.data_count_bit < subghz_protocol_kia_const.min_count_bit_for_found) {
|
||||
ret = SubGhzProtocolStatusErrorParserBitCount;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
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);
|
||||
if(subghz_custom_btn_get_original() == 0)
|
||||
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;
|
||||
|
||||
|
||||
uint8_t received_crc = instance->generic.data & 0xFF;
|
||||
uint8_t calculated_crc = kia_calculate_crc(instance->generic.data);
|
||||
bool crc_valid = (received_crc == calculated_crc);
|
||||
@@ -740,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:%X 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_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 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"
|
||||
|
||||
#define KIA_V1_TOTAL_BURSTS 3
|
||||
// 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
|
||||
#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,60 +41,56 @@ 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 |
|
||||
.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 |
|
||||
instance->generic.cnt = ((instance->generic.data >> 4) & 0xF) << 8 |
|
||||
((instance->generic.data >> 8) & 0xFF);
|
||||
|
||||
uint8_t cnt_high = (instance->generic.cnt >> 8) & 0xF;
|
||||
@@ -123,7 +101,7 @@ static void kia_v1_check_remote_controller(SubGhzProtocolDecoderKiaV1* instance)
|
||||
char_data[3] = instance->generic.serial & 0xFF;
|
||||
char_data[4] = instance->generic.btn;
|
||||
char_data[5] = instance->generic.cnt & 0xFF;
|
||||
|
||||
|
||||
uint8_t crc;
|
||||
if(cnt_high == 0) {
|
||||
uint8_t offset = (instance->generic.cnt >= 0x098) ? instance->generic.btn : 1;
|
||||
@@ -137,34 +115,78 @@ 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));
|
||||
|
||||
|
||||
instance->base.protocol = &subghz_protocol_kia_v1;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
|
||||
|
||||
instance->encoder.repeat = 10;
|
||||
instance->encoder.size_upload = 1200;
|
||||
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;
|
||||
@@ -173,7 +195,7 @@ static void kia_protocol_encoder_v1_get_upload(SubGhzProtocolEncoderKiaV1* insta
|
||||
char_data[3] = instance->generic.serial & 0xFF;
|
||||
char_data[4] = instance->generic.btn;
|
||||
char_data[5] = instance->generic.cnt & 0xFF;
|
||||
|
||||
|
||||
uint8_t crc;
|
||||
if(cnt_high == 0) {
|
||||
uint8_t offset = (instance->generic.cnt >= 0x098) ? instance->generic.btn : 1;
|
||||
@@ -185,271 +207,135 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Imposta data_count_bit
|
||||
instance->generic.data_count_bit = subghz_protocol_kia_v1_const.min_count_bit_for_found;
|
||||
|
||||
// Estrai serial, btn, cnt dalla data (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 |
|
||||
((instance->generic.data >> 8) & 0xFF);
|
||||
|
||||
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);
|
||||
}
|
||||
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_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)) {
|
||||
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);
|
||||
|
||||
// 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 = ((instance->generic.data >> 4) & 0xF) << 8 |
|
||||
((instance->generic.data >> 8) & 0xFF);
|
||||
FURI_LOG_I(TAG, "Extracted counter: 0x%03lX", (unsigned long)instance->generic.cnt);
|
||||
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 ret;
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
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 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_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,278 +90,183 @@ 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);
|
||||
size_t index = 0;
|
||||
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);
|
||||
instance->generic.data = (instance->generic.data & ~0x0FULL) | crc;
|
||||
|
||||
|
||||
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));
|
||||
|
||||
|
||||
instance->base.protocol = &subghz_protocol_kia_v2;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
|
||||
|
||||
instance->encoder.repeat = 10;
|
||||
instance->encoder.size_upload = 1300;
|
||||
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_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);
|
||||
}
|
||||
if(subghz_custom_btn_get_original() == 0)
|
||||
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;
|
||||
@@ -379,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;
|
||||
@@ -395,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;
|
||||
@@ -437,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,13 +340,12 @@ 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;
|
||||
@@ -467,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++;
|
||||
|
||||
@@ -489,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;
|
||||
@@ -496,68 +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 ret;
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
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;
|
||||
if(subghz_custom_btn_get_original() == 0)
|
||||
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 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);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,37 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "kia_generic.h"
|
||||
#include "base.h"
|
||||
#include "../blocks/math.h"
|
||||
|
||||
#define SUBGHZ_PROTOCOL_KIA_V3_V4_NAME "KIA/HYU V3/V4"
|
||||
|
||||
#define KIA_PROTOCOL_V3_V4_NAME "Kia V3/V4"
|
||||
typedef struct SubGhzProtocolDecoderKiaV3V4 SubGhzProtocolDecoderKiaV3V4;
|
||||
typedef struct SubGhzProtocolEncoderKiaV3V4 SubGhzProtocolEncoderKiaV3V4;
|
||||
|
||||
extern const SubGhzProtocol subghz_protocol_kia_v3_v4;
|
||||
|
||||
// Decoder functions
|
||||
void* kia_protocol_decoder_v3_v4_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_decoder_v3_v4_free(void* context);
|
||||
void kia_protocol_decoder_v3_v4_reset(void* context);
|
||||
void kia_protocol_decoder_v3_v4_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t kia_protocol_decoder_v3_v4_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus kia_protocol_decoder_v3_v4_serialize(
|
||||
void* subghz_protocol_decoder_kia_v3_v4_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_decoder_kia_v3_v4_free(void* context);
|
||||
void subghz_protocol_decoder_kia_v3_v4_reset(void* context);
|
||||
void subghz_protocol_decoder_kia_v3_v4_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t subghz_protocol_decoder_kia_v3_v4_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v3_v4_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_decoder_v3_v4_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_decoder_v3_v4_get_string(void* context, FuriString* output);
|
||||
subghz_protocol_decoder_kia_v3_v4_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_kia_v3_v4_get_string(void* context, FuriString* output);
|
||||
|
||||
// Encoder functions
|
||||
void* kia_protocol_encoder_v3_v4_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_encoder_v3_v4_free(void* context);
|
||||
void* subghz_protocol_encoder_kia_v3_v4_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_kia_v3_v4_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_encoder_v3_v4_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_encoder_v3_v4_stop(void* context);
|
||||
LevelDuration kia_protocol_encoder_v3_v4_yield(void* context);
|
||||
|
||||
// Encoder helper functions for UI
|
||||
void kia_protocol_encoder_v3_v4_set_button(void* context, uint8_t button);
|
||||
void kia_protocol_encoder_v3_v4_set_counter(void* context, uint16_t counter);
|
||||
void kia_protocol_encoder_v3_v4_increment_counter(void* context);
|
||||
uint16_t kia_protocol_encoder_v3_v4_get_counter(void* context);
|
||||
uint8_t kia_protocol_encoder_v3_v4_get_button(void* context);
|
||||
subghz_protocol_encoder_kia_v3_v4_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_kia_v3_v4_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_kia_v3_v4_yield(void* context);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,36 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "kia_generic.h"
|
||||
#include <lib/toolbox/manchester_decoder.h>
|
||||
#include "base.h"
|
||||
#include "../blocks/math.h"
|
||||
|
||||
|
||||
#define KIA_PROTOCOL_V5_NAME "Kia V5"
|
||||
#define SUBGHZ_PROTOCOL_KIA_V5_NAME "KIA/HYU V5"
|
||||
|
||||
typedef struct SubGhzProtocolDecoderKiaV5 SubGhzProtocolDecoderKiaV5;
|
||||
typedef struct SubGhzProtocolEncoderKiaV5 SubGhzProtocolEncoderKiaV5;
|
||||
|
||||
extern const SubGhzProtocolDecoder kia_protocol_v5_decoder;
|
||||
extern const SubGhzProtocolEncoder kia_protocol_v5_encoder;
|
||||
extern const SubGhzProtocol subghz_protocol_kia_v5;
|
||||
|
||||
// Decoder functions
|
||||
void* kia_protocol_decoder_v5_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_decoder_v5_free(void* context);
|
||||
void kia_protocol_decoder_v5_reset(void* context);
|
||||
void kia_protocol_decoder_v5_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t kia_protocol_decoder_v5_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus kia_protocol_decoder_v5_serialize(
|
||||
void* subghz_protocol_decoder_kia_v5_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_decoder_kia_v5_free(void* context);
|
||||
void subghz_protocol_decoder_kia_v5_reset(void* context);
|
||||
void subghz_protocol_decoder_kia_v5_feed(void* context, bool level, uint32_t duration);
|
||||
uint8_t subghz_protocol_decoder_kia_v5_get_hash_data(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_kia_v5_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_decoder_v5_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_decoder_v5_get_string(void* context, FuriString* output);
|
||||
subghz_protocol_decoder_kia_v5_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_kia_v5_get_string(void* context, FuriString* output);
|
||||
|
||||
// Encoder functions
|
||||
void* kia_protocol_encoder_v5_alloc(SubGhzEnvironment* environment);
|
||||
void kia_protocol_encoder_v5_free(void* context);
|
||||
void* subghz_protocol_encoder_kia_v5_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_kia_v5_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
kia_protocol_encoder_v5_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void kia_protocol_encoder_v5_stop(void* context);
|
||||
LevelDuration kia_protocol_encoder_v5_yield(void* context);
|
||||
subghz_protocol_encoder_kia_v5_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_kia_v5_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_kia_v5_yield(void* context);
|
||||
void subghz_protocol_encoder_kia_v5_set_button(void* context, uint8_t button);
|
||||
void subghz_protocol_encoder_kia_v5_set_counter(void* context, uint16_t counter);
|
||||
void subghz_protocol_encoder_kia_v5_increment_counter(void* context);
|
||||
uint16_t subghz_protocol_encoder_kia_v5_get_counter(void* context);
|
||||
uint8_t subghz_protocol_encoder_kia_v5_get_button(void* context);
|
||||
|
||||
@@ -42,7 +42,7 @@ const SubGhzProtocol* const subghz_protocol_registry_items[] = {
|
||||
&subghz_protocol_kia_v0, &subghz_protocol_kia_v1,
|
||||
&subghz_protocol_kia_v2, &subghz_protocol_kia_v3_v4,
|
||||
&subghz_protocol_kia_v5, &subghz_protocol_kia_v6,
|
||||
&suzuki_protocol, &subghz_protocol_mitsubishi_v0,
|
||||
&subghz_protocol_suzuki, &subghz_protocol_mitsubishi_v0,
|
||||
};
|
||||
|
||||
const SubGhzProtocolRegistry subghz_protocol_registry = {
|
||||
|
||||
@@ -1,60 +1,46 @@
|
||||
#include "subaru.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 subaru_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 "SubaruProtocol"
|
||||
|
||||
#define TAG "SubGhzProtocolSubaru"
|
||||
|
||||
static const SubGhzBlockConst subghz_protocol_subaru_const = {
|
||||
.te_short = 800,
|
||||
.te_long = 1600,
|
||||
.te_delta = 200,
|
||||
.te_delta = 250,
|
||||
.min_count_bit_for_found = 64,
|
||||
};
|
||||
|
||||
#define SUBARU_PREAMBLE_PAIRS 80
|
||||
#define SUBARU_GAP_US 2800
|
||||
#define SUBARU_SYNC_US 2800
|
||||
#define SUBARU_TOTAL_BURSTS 3
|
||||
#define SUBARU_INTER_BURST_GAP 25000
|
||||
|
||||
typedef struct SubGhzProtocolDecoderSubaru {
|
||||
struct SubGhzProtocolDecoderSubaru {
|
||||
SubGhzProtocolDecoderBase base;
|
||||
SubGhzBlockDecoder decoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
|
||||
|
||||
uint16_t header_count;
|
||||
uint16_t bit_count;
|
||||
uint8_t data[8];
|
||||
|
||||
|
||||
uint64_t key;
|
||||
uint32_t serial;
|
||||
uint8_t btn;
|
||||
uint16_t cnt;
|
||||
} SubGhzProtocolDecoderSubaru;
|
||||
uint8_t button;
|
||||
uint16_t count;
|
||||
};
|
||||
|
||||
typedef struct SubGhzProtocolEncoderSubaru {
|
||||
struct SubGhzProtocolEncoderSubaru {
|
||||
SubGhzProtocolEncoderBase base;
|
||||
SubGhzProtocolBlockEncoder encoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
|
||||
|
||||
uint64_t key;
|
||||
uint32_t serial;
|
||||
uint8_t btn;
|
||||
uint16_t cnt;
|
||||
} SubGhzProtocolEncoderSubaru;
|
||||
uint8_t button;
|
||||
uint16_t count;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
SubaruDecoderStepReset = 0,
|
||||
@@ -65,6 +51,12 @@ typedef enum {
|
||||
SubaruDecoderStepCheckDuration,
|
||||
} SubaruDecoderStep;
|
||||
|
||||
static void subaru_decode_count(const uint8_t* KB, uint16_t* count);
|
||||
static void subaru_encode_count(uint8_t* KB, uint16_t count);
|
||||
static void subaru_add_bit(SubGhzProtocolDecoderSubaru* instance, bool bit);
|
||||
static bool subaru_process_data(SubGhzProtocolDecoderSubaru* instance);
|
||||
static void subghz_protocol_encoder_subaru_get_upload(SubGhzProtocolEncoderSubaru* instance);
|
||||
|
||||
const SubGhzProtocolDecoder subghz_protocol_subaru_decoder = {
|
||||
.alloc = subghz_protocol_decoder_subaru_alloc,
|
||||
.free = subghz_protocol_decoder_subaru_free,
|
||||
@@ -85,19 +77,49 @@ const SubGhzProtocolEncoder subghz_protocol_subaru_encoder = {
|
||||
};
|
||||
|
||||
const SubGhzProtocol subghz_protocol_subaru = {
|
||||
.name = SUBARU_PROTOCOL_NAME,
|
||||
.name = SUBGHZ_PROTOCOL_SUBARU_NAME,
|
||||
.type = SubGhzProtocolTypeDynamic,
|
||||
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
|
||||
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM |
|
||||
SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
|
||||
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
|
||||
.decoder = &subghz_protocol_subaru_decoder,
|
||||
.encoder = &subghz_protocol_subaru_encoder,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// DECODER HELPER FUNCTIONS
|
||||
// ============================================================================
|
||||
static uint8_t subaru_get_button_code(uint8_t custom_btn) {
|
||||
switch(custom_btn) {
|
||||
case 1: return 0x01;
|
||||
case 2: return 0x02;
|
||||
case 3: return 0x03;
|
||||
case 4: return 0x04;
|
||||
case 5: return 0x08;
|
||||
default: return 0x01;
|
||||
}
|
||||
}
|
||||
|
||||
static void subaru_decode_count(const uint8_t* KB, uint16_t* cnt) {
|
||||
static uint8_t subaru_btn_to_custom(uint8_t btn_code) {
|
||||
switch(btn_code) {
|
||||
case 0x01: return 1;
|
||||
case 0x02: return 2;
|
||||
case 0x03: return 3;
|
||||
case 0x04: return 4;
|
||||
case 0x08: return 5;
|
||||
default: return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static const char* subaru_get_button_name(uint8_t btn) {
|
||||
switch(btn) {
|
||||
case 0x01: return "Lock";
|
||||
case 0x02: return "Unlock";
|
||||
case 0x03: return "Trunk";
|
||||
case 0x04: return "Panic";
|
||||
case 0x08: return "0x08";
|
||||
default: return "??";
|
||||
}
|
||||
}
|
||||
|
||||
static void subaru_decode_count(const uint8_t* KB, uint16_t* count) {
|
||||
uint8_t lo = 0;
|
||||
if((KB[4] & 0x40) == 0) lo |= 0x01;
|
||||
if((KB[4] & 0x80) == 0) lo |= 0x02;
|
||||
@@ -107,19 +129,19 @@ static void subaru_decode_count(const uint8_t* KB, uint16_t* cnt) {
|
||||
if((KB[6] & 0x02) == 0) lo |= 0x20;
|
||||
if((KB[5] & 0x40) == 0) lo |= 0x40;
|
||||
if((KB[5] & 0x80) == 0) lo |= 0x80;
|
||||
|
||||
|
||||
uint8_t REG_SH1 = (KB[7] << 4) & 0xF0;
|
||||
if(KB[5] & 0x04) REG_SH1 |= 0x04;
|
||||
if(KB[5] & 0x08) REG_SH1 |= 0x08;
|
||||
if(KB[6] & 0x80) REG_SH1 |= 0x02;
|
||||
if(KB[6] & 0x40) REG_SH1 |= 0x01;
|
||||
|
||||
|
||||
uint8_t REG_SH2 = ((KB[6] << 2) & 0xF0) | ((KB[7] >> 4) & 0x0F);
|
||||
|
||||
|
||||
uint8_t SER0 = KB[3];
|
||||
uint8_t SER1 = KB[1];
|
||||
uint8_t SER2 = KB[2];
|
||||
|
||||
|
||||
uint8_t total_rot = 4 + lo;
|
||||
for(uint8_t i = 0; i < total_rot; ++i) {
|
||||
uint8_t t_bit = (SER0 >> 7) & 1;
|
||||
@@ -127,10 +149,10 @@ static void subaru_decode_count(const uint8_t* KB, uint16_t* cnt) {
|
||||
SER1 = ((SER1 << 1) & 0xFE) | ((SER2 >> 7) & 1);
|
||||
SER2 = ((SER2 << 1) & 0xFE) | t_bit;
|
||||
}
|
||||
|
||||
|
||||
uint8_t T1 = SER1 ^ REG_SH1;
|
||||
uint8_t T2 = SER2 ^ REG_SH2;
|
||||
|
||||
|
||||
uint8_t hi = 0;
|
||||
if((T1 & 0x10) == 0) hi |= 0x04;
|
||||
if((T1 & 0x20) == 0) hi |= 0x08;
|
||||
@@ -140,8 +162,67 @@ static void subaru_decode_count(const uint8_t* KB, uint16_t* cnt) {
|
||||
if((T1 & 0x02) == 0) hi |= 0x80;
|
||||
if((T2 & 0x08) == 0) hi |= 0x20;
|
||||
if((T2 & 0x04) == 0) hi |= 0x10;
|
||||
|
||||
*count = ((hi << 8) | lo) & 0xFFFF;
|
||||
}
|
||||
|
||||
*cnt = ((hi << 8) | lo) & 0xFFFF;
|
||||
static void subaru_encode_count(uint8_t* KB, uint16_t count) {
|
||||
uint8_t lo = count & 0xFF;
|
||||
uint8_t hi = (count >> 8) & 0xFF;
|
||||
|
||||
KB[4] &= ~0xC0;
|
||||
KB[5] &= ~0xC3;
|
||||
KB[6] &= ~0x03;
|
||||
|
||||
if((lo & 0x01) == 0) KB[4] |= 0x40;
|
||||
if((lo & 0x02) == 0) KB[4] |= 0x80;
|
||||
if((lo & 0x04) == 0) KB[5] |= 0x01;
|
||||
if((lo & 0x08) == 0) KB[5] |= 0x02;
|
||||
if((lo & 0x10) == 0) KB[6] |= 0x01;
|
||||
if((lo & 0x20) == 0) KB[6] |= 0x02;
|
||||
if((lo & 0x40) == 0) KB[5] |= 0x40;
|
||||
if((lo & 0x80) == 0) KB[5] |= 0x80;
|
||||
|
||||
uint8_t SER0 = KB[3];
|
||||
uint8_t SER1 = KB[1];
|
||||
uint8_t SER2 = KB[2];
|
||||
|
||||
uint8_t total_rot = 4 + lo;
|
||||
for(uint8_t i = 0; i < total_rot; ++i) {
|
||||
uint8_t t_bit = (SER0 >> 7) & 1;
|
||||
SER0 = ((SER0 << 1) & 0xFE) | ((SER1 >> 7) & 1);
|
||||
SER1 = ((SER1 << 1) & 0xFE) | ((SER2 >> 7) & 1);
|
||||
SER2 = ((SER2 << 1) & 0xFE) | t_bit;
|
||||
}
|
||||
|
||||
uint8_t T1 = 0xFF;
|
||||
uint8_t T2 = 0xFF;
|
||||
|
||||
if(hi & 0x04) T1 &= ~0x10;
|
||||
if(hi & 0x08) T1 &= ~0x20;
|
||||
if(hi & 0x02) T2 &= ~0x80;
|
||||
if(hi & 0x01) T2 &= ~0x40;
|
||||
if(hi & 0x40) T1 &= ~0x01;
|
||||
if(hi & 0x80) T1 &= ~0x02;
|
||||
if(hi & 0x20) T2 &= ~0x08;
|
||||
if(hi & 0x10) T2 &= ~0x04;
|
||||
|
||||
uint8_t new_REG_SH1 = T1 ^ SER1;
|
||||
uint8_t new_REG_SH2 = T2 ^ SER2;
|
||||
|
||||
KB[5] &= ~0x0C;
|
||||
KB[6] &= ~0xC0;
|
||||
|
||||
KB[7] = (KB[7] & 0xF0) | ((new_REG_SH1 >> 4) & 0x0F);
|
||||
|
||||
if(new_REG_SH1 & 0x04) KB[5] |= 0x04;
|
||||
if(new_REG_SH1 & 0x08) KB[5] |= 0x08;
|
||||
if(new_REG_SH1 & 0x02) KB[6] |= 0x80;
|
||||
if(new_REG_SH1 & 0x01) KB[6] |= 0x40;
|
||||
|
||||
KB[6] = (KB[6] & 0xC3) | ((new_REG_SH2 >> 2) & 0x3C);
|
||||
|
||||
KB[7] = (KB[7] & 0x0F) | ((new_REG_SH2 << 4) & 0xF0);
|
||||
}
|
||||
|
||||
static void subaru_add_bit(SubGhzProtocolDecoderSubaru* instance, bool bit) {
|
||||
@@ -161,230 +242,24 @@ static bool subaru_process_data(SubGhzProtocolDecoderSubaru* instance) {
|
||||
if(instance->bit_count < 64) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
uint8_t* b = instance->data;
|
||||
|
||||
instance->key = ((uint64_t)b[0] << 56) | ((uint64_t)b[1] << 48) | ((uint64_t)b[2] << 40) |
|
||||
((uint64_t)b[3] << 32) | ((uint64_t)b[4] << 24) | ((uint64_t)b[5] << 16) |
|
||||
((uint64_t)b[6] << 8) | ((uint64_t)b[7]);
|
||||
|
||||
|
||||
instance->key = ((uint64_t)b[0] << 56) | ((uint64_t)b[1] << 48) |
|
||||
((uint64_t)b[2] << 40) | ((uint64_t)b[3] << 32) |
|
||||
((uint64_t)b[4] << 24) | ((uint64_t)b[5] << 16) |
|
||||
((uint64_t)b[6] << 8) | ((uint64_t)b[7]);
|
||||
|
||||
instance->serial = ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3];
|
||||
instance->btn = b[0] & 0x0F;
|
||||
subaru_decode_count(b, &instance->cnt);
|
||||
|
||||
instance->button = b[0] & 0x0F;
|
||||
subaru_decode_count(b, &instance->count);
|
||||
|
||||
instance->decoder.decode_data = instance->key;
|
||||
instance->decoder.decode_count_bit = 64;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ENCODER IMPLEMENTATION
|
||||
// ============================================================================
|
||||
|
||||
void* subghz_protocol_encoder_subaru_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderSubaru* instance = malloc(sizeof(SubGhzProtocolEncoderSubaru));
|
||||
|
||||
instance->base.protocol = &subghz_protocol_subaru;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
|
||||
instance->encoder.repeat = 10;
|
||||
instance->encoder.size_upload = 1024;
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
instance->key = 0;
|
||||
instance->serial = 0;
|
||||
instance->btn = 0;
|
||||
instance->cnt = 0;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_subaru_free(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
if(instance->encoder.upload) {
|
||||
free(instance->encoder.upload);
|
||||
}
|
||||
free(instance);
|
||||
}
|
||||
|
||||
static void subghz_protocol_encoder_subaru_get_upload(SubGhzProtocolEncoderSubaru* instance) {
|
||||
furi_check(instance);
|
||||
size_t index = 0;
|
||||
|
||||
uint32_t te_short = subghz_protocol_subaru_const.te_short;
|
||||
uint32_t te_long = subghz_protocol_subaru_const.te_long;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Building upload: key=0x%016llX, serial=0x%06lX, btn=0x%X, cnt=0x%04X",
|
||||
instance->key,
|
||||
instance->serial,
|
||||
subaru_get_btn_code(),
|
||||
instance->cnt);
|
||||
|
||||
for(uint8_t burst = 0; burst < SUBARU_TOTAL_BURSTS; burst++) {
|
||||
if(burst > 0) {
|
||||
instance->encoder.upload[index++] = level_duration_make(false, SUBARU_INTER_BURST_GAP);
|
||||
}
|
||||
|
||||
// Preamble: Long HIGH/LOW pairs
|
||||
for(int i = 0; i < SUBARU_PREAMBLE_PAIRS; i++) {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_long);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_long);
|
||||
}
|
||||
|
||||
// Replace last preamble LOW with gap (to avoid consecutive LOWs combining)
|
||||
instance->encoder.upload[index - 1] = level_duration_make(false, SUBARU_GAP_US);
|
||||
|
||||
// Sync: Long HIGH
|
||||
instance->encoder.upload[index++] = level_duration_make(true, SUBARU_SYNC_US);
|
||||
|
||||
// Sync end: Long LOW
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_long);
|
||||
|
||||
// Data: 64 bits, PWM encoding
|
||||
// Short HIGH = 1, Long HIGH = 0
|
||||
for(int bit = 63; bit >= 0; bit--) {
|
||||
if((instance->key >> bit) & 1) {
|
||||
// Bit 1: Short HIGH
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_short);
|
||||
} else {
|
||||
// Bit 0: Long HIGH
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_long);
|
||||
}
|
||||
// LOW separator
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_short);
|
||||
}
|
||||
|
||||
// End marker: extended LOW
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_long * 2);
|
||||
}
|
||||
|
||||
instance->encoder.size_upload = index;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
FURI_LOG_I(TAG, "Upload built: %zu elements", instance->encoder.size_upload);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_subaru_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
instance->encoder.repeat = 10;
|
||||
|
||||
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", furi_string_get_cstr(temp_str));
|
||||
furi_string_free(temp_str);
|
||||
break;
|
||||
}
|
||||
furi_string_free(temp_str);
|
||||
|
||||
uint32_t bit_count_temp = 0;
|
||||
if(!flipper_format_read_uint32(flipper_format, "Bit", &bit_count_temp, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing Bit");
|
||||
break;
|
||||
}
|
||||
|
||||
// Read key in standard hex format (e.g. "A0 3B F8 68 54 53 62 00")
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
if(!flipper_format_read_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(TAG, "Missing Key");
|
||||
break;
|
||||
}
|
||||
instance->key = 0;
|
||||
for(int i = 0; i < 8; i++) {
|
||||
instance->key = (instance->key << 8) | key_data[i];
|
||||
}
|
||||
FURI_LOG_I(TAG, "Parsed key: 0x%016llX", instance->key);
|
||||
|
||||
if(!flipper_format_read_uint32(flipper_format, "Serial", &instance->serial, 1)) {
|
||||
instance->serial = (uint32_t)((instance->key >> 8) & 0xFFFFFF);
|
||||
}
|
||||
|
||||
uint32_t btn_temp = 0;
|
||||
if(flipper_format_read_uint32(flipper_format, "Btn", &btn_temp, 1)) {
|
||||
instance->btn = (uint8_t)btn_temp;
|
||||
} else {
|
||||
instance->btn = (uint8_t)(instance->key >> 56) & 0x0F;
|
||||
}
|
||||
if(subghz_custom_btn_get_original() == 0)
|
||||
subghz_custom_btn_set_original(instance->btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
instance->btn = subaru_get_btn_code();
|
||||
|
||||
uint32_t cnt_temp = 0;
|
||||
if(flipper_format_read_uint32(flipper_format, "Cnt", &cnt_temp, 1)) {
|
||||
instance->cnt = (uint16_t)cnt_temp;
|
||||
} else {
|
||||
instance->cnt = 0;
|
||||
}
|
||||
|
||||
if(!flipper_format_read_uint32(
|
||||
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1)) {
|
||||
instance->encoder.repeat = 10;
|
||||
}
|
||||
|
||||
subghz_protocol_encoder_subaru_get_upload(instance);
|
||||
instance->encoder.is_running = true;
|
||||
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"Encoder ready: key=0x%016llX, serial=0x%06lX, btn=0x%X",
|
||||
instance->key,
|
||||
instance->serial,
|
||||
instance->btn);
|
||||
|
||||
ret = SubGhzProtocolStatusOk;
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_subaru_stop(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
|
||||
LevelDuration subghz_protocol_encoder_subaru_yield(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
|
||||
if(!instance->encoder.is_running || instance->encoder.repeat == 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// DECODER IMPLEMENTATION
|
||||
// ============================================================================
|
||||
|
||||
void* subghz_protocol_decoder_subaru_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolDecoderSubaru* instance = malloc(sizeof(SubGhzProtocolDecoderSubaru));
|
||||
@@ -394,13 +269,13 @@ void* subghz_protocol_decoder_subaru_alloc(SubGhzEnvironment* environment) {
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_subaru_free(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_subaru_reset(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
instance->decoder.parser_step = SubaruDecoderStepReset;
|
||||
instance->decoder.te_last = 0;
|
||||
@@ -410,25 +285,21 @@ void subghz_protocol_decoder_subaru_reset(void* context) {
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
|
||||
uint32_t te_short = subghz_protocol_subaru_const.te_short;
|
||||
uint32_t te_long = subghz_protocol_subaru_const.te_long;
|
||||
uint32_t te_delta = subghz_protocol_subaru_const.te_delta;
|
||||
|
||||
|
||||
switch(instance->decoder.parser_step) {
|
||||
case SubaruDecoderStepReset:
|
||||
if(level && (DURATION_DIFF(duration, te_long) < te_delta)) {
|
||||
if(level && DURATION_DIFF(duration, subghz_protocol_subaru_const.te_long) < subghz_protocol_subaru_const.te_delta) {
|
||||
instance->decoder.parser_step = SubaruDecoderStepCheckPreamble;
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case SubaruDecoderStepCheckPreamble:
|
||||
if(!level) {
|
||||
if(DURATION_DIFF(duration, te_long) < te_delta) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_subaru_const.te_long) < subghz_protocol_subaru_const.te_delta) {
|
||||
instance->header_count++;
|
||||
} else if(duration > 2000 && duration < 3500) {
|
||||
if(instance->header_count > 20) {
|
||||
@@ -440,7 +311,7 @@ void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t dur
|
||||
instance->decoder.parser_step = SubaruDecoderStepReset;
|
||||
}
|
||||
} else {
|
||||
if(DURATION_DIFF(duration, te_long) < te_delta) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_subaru_const.te_long) < subghz_protocol_subaru_const.te_delta) {
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count++;
|
||||
} else {
|
||||
@@ -448,7 +319,7 @@ void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t dur
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case SubaruDecoderStepFoundGap:
|
||||
if(level && duration > 2000 && duration < 3500) {
|
||||
instance->decoder.parser_step = SubaruDecoderStepFoundSync;
|
||||
@@ -456,9 +327,9 @@ void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t dur
|
||||
instance->decoder.parser_step = SubaruDecoderStepReset;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case SubaruDecoderStepFoundSync:
|
||||
if(!level && (DURATION_DIFF(duration, te_long) < te_delta)) {
|
||||
if(!level && DURATION_DIFF(duration, subghz_protocol_subaru_const.te_long) < subghz_protocol_subaru_const.te_delta) {
|
||||
instance->decoder.parser_step = SubaruDecoderStepSaveDuration;
|
||||
instance->bit_count = 0;
|
||||
memset(instance->data, 0, sizeof(instance->data));
|
||||
@@ -466,29 +337,32 @@ void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t dur
|
||||
instance->decoder.parser_step = SubaruDecoderStepReset;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case SubaruDecoderStepSaveDuration:
|
||||
if(level) {
|
||||
if(DURATION_DIFF(duration, te_short) < te_delta) {
|
||||
// Short HIGH = bit 1
|
||||
if(DURATION_DIFF(duration, subghz_protocol_subaru_const.te_short) < subghz_protocol_subaru_const.te_delta) {
|
||||
subaru_add_bit(instance, true);
|
||||
instance->decoder.te_last = duration;
|
||||
instance->decoder.parser_step = SubaruDecoderStepCheckDuration;
|
||||
} else if(DURATION_DIFF(duration, te_long) < te_delta) {
|
||||
// Long HIGH = bit 0
|
||||
} else if(DURATION_DIFF(duration, subghz_protocol_subaru_const.te_long) < subghz_protocol_subaru_const.te_delta) {
|
||||
subaru_add_bit(instance, false);
|
||||
instance->decoder.te_last = duration;
|
||||
instance->decoder.parser_step = SubaruDecoderStepCheckDuration;
|
||||
} else if(duration > 3000) {
|
||||
// End of transmission
|
||||
if(instance->bit_count >= 64) {
|
||||
if(subaru_process_data(instance)) {
|
||||
instance->generic.data = instance->key;
|
||||
instance->generic.data_count_bit = 64;
|
||||
instance->generic.serial = instance->serial;
|
||||
instance->generic.btn = instance->btn;
|
||||
instance->generic.cnt = instance->cnt;
|
||||
|
||||
instance->generic.btn = instance->button;
|
||||
instance->generic.cnt = instance->count;
|
||||
|
||||
uint8_t custom_btn = subaru_btn_to_custom(instance->button);
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(custom_btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(5);
|
||||
|
||||
if(instance->base.callback) {
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
}
|
||||
@@ -502,22 +376,27 @@ void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t dur
|
||||
instance->decoder.parser_step = SubaruDecoderStepReset;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case SubaruDecoderStepCheckDuration:
|
||||
if(!level) {
|
||||
if((DURATION_DIFF(duration, te_short) < te_delta) ||
|
||||
(DURATION_DIFF(duration, te_long) < te_delta)) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_subaru_const.te_short) < subghz_protocol_subaru_const.te_delta ||
|
||||
DURATION_DIFF(duration, subghz_protocol_subaru_const.te_long) < subghz_protocol_subaru_const.te_delta) {
|
||||
instance->decoder.parser_step = SubaruDecoderStepSaveDuration;
|
||||
} else if(duration > 3000) {
|
||||
// Gap - end of packet
|
||||
if(instance->bit_count >= 64) {
|
||||
if(subaru_process_data(instance)) {
|
||||
instance->generic.data = instance->key;
|
||||
instance->generic.data_count_bit = 64;
|
||||
instance->generic.serial = instance->serial;
|
||||
instance->generic.btn = instance->btn;
|
||||
instance->generic.cnt = instance->cnt;
|
||||
|
||||
instance->generic.btn = instance->button;
|
||||
instance->generic.cnt = instance->count;
|
||||
|
||||
uint8_t custom_btn = subaru_btn_to_custom(instance->button);
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(custom_btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(5);
|
||||
|
||||
if(instance->base.callback) {
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
}
|
||||
@@ -535,7 +414,7 @@ void subghz_protocol_decoder_subaru_feed(void* context, bool level, uint32_t dur
|
||||
}
|
||||
|
||||
uint8_t subghz_protocol_decoder_subaru_get_hash_data(void* context) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(
|
||||
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
|
||||
@@ -545,76 +424,230 @@ SubGhzProtocolStatus subghz_protocol_decoder_subaru_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
|
||||
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
instance->generic.data = instance->key;
|
||||
instance->generic.data_count_bit = 64;
|
||||
|
||||
SubGhzProtocolStatus ret =
|
||||
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_subaru_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
|
||||
SubGhzProtocolStatus ret = subghz_block_generic_deserialize_check_count_bit(
|
||||
&instance->generic, flipper_format, subghz_protocol_subaru_const.min_count_bit_for_found);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
uint32_t temp = instance->serial;
|
||||
flipper_format_write_uint32(flipper_format, "Serial", &temp, 1);
|
||||
temp = instance->btn;
|
||||
flipper_format_write_uint32(flipper_format, "Btn", &temp, 1);
|
||||
temp = instance->cnt;
|
||||
flipper_format_write_uint32(flipper_format, "Cnt", &temp, 1);
|
||||
instance->key = instance->generic.data;
|
||||
|
||||
uint8_t b[8];
|
||||
for(int i = 0; i < 8; i++) {
|
||||
b[i] = (uint8_t)(instance->key >> (56 - i * 8));
|
||||
}
|
||||
|
||||
instance->serial = ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3];
|
||||
instance->button = b[0] & 0x0F;
|
||||
subaru_decode_count(b, &instance->count);
|
||||
|
||||
instance->generic.serial = instance->serial;
|
||||
instance->generic.btn = instance->button;
|
||||
instance->generic.cnt = instance->count;
|
||||
|
||||
uint8_t custom_btn = subaru_btn_to_custom(instance->button);
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(custom_btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(5);
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_subaru_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
|
||||
SubGhzProtocolStatus ret = subghz_block_generic_deserialize_check_count_bit(
|
||||
&instance->generic, flipper_format, subghz_protocol_subaru_const.min_count_bit_for_found);
|
||||
if(ret != SubGhzProtocolStatusOk) return ret;
|
||||
|
||||
instance->key = instance->generic.data;
|
||||
|
||||
flipper_format_rewind(flipper_format);
|
||||
flipper_format_read_uint32(flipper_format, "Serial", &instance->serial, 1);
|
||||
|
||||
uint32_t btn_temp = 0;
|
||||
flipper_format_read_uint32(flipper_format, "Btn", &btn_temp, 1);
|
||||
instance->btn = (uint8_t)btn_temp;
|
||||
|
||||
uint32_t cnt_temp = 0;
|
||||
flipper_format_read_uint32(flipper_format, "Cnt", &cnt_temp, 1);
|
||||
instance->cnt = (uint16_t)cnt_temp;
|
||||
|
||||
instance->generic.serial = instance->serial;
|
||||
instance->generic.btn = instance->btn;
|
||||
instance->generic.cnt = instance->cnt;
|
||||
|
||||
return SubGhzProtocolStatusOk;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_subaru_get_string(void* context, FuriString* output) {
|
||||
furi_check(context);
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSubaru* instance = context;
|
||||
if(subghz_custom_btn_get_original() == 0)
|
||||
subghz_custom_btn_set_original(instance->btn);
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
|
||||
uint32_t key_hi = (uint32_t)(instance->key >> 32);
|
||||
uint32_t key_lo = (uint32_t)(instance->key & 0xFFFFFFFF);
|
||||
|
||||
|
||||
furi_string_cat_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%08lX%08lX\r\n"
|
||||
"Sn:%06lX Btn:%X Cnt:%04X\r\n",
|
||||
"Sn:%06lX Cnt:%04X\r\n"
|
||||
"Btn:%X [%s]",
|
||||
instance->generic.protocol_name,
|
||||
instance->generic.data_count_bit,
|
||||
key_hi,
|
||||
key_lo,
|
||||
instance->serial,
|
||||
subaru_get_btn_code(),
|
||||
instance->cnt);
|
||||
instance->count,
|
||||
instance->button,
|
||||
subaru_get_button_name(instance->button));
|
||||
}
|
||||
|
||||
void* subghz_protocol_encoder_subaru_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderSubaru* instance = malloc(sizeof(SubGhzProtocolEncoderSubaru));
|
||||
|
||||
instance->base.protocol = &subghz_protocol_subaru;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
|
||||
instance->encoder.repeat = 10;
|
||||
instance->encoder.size_upload = 2048;
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
instance->encoder.is_running = false;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_subaru_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
free(instance->encoder.upload);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_subaru_stop(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
|
||||
LevelDuration subghz_protocol_encoder_subaru_yield(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSubaru* 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;
|
||||
}
|
||||
|
||||
static void subghz_protocol_encoder_subaru_get_upload(SubGhzProtocolEncoderSubaru* instance) {
|
||||
furi_assert(instance);
|
||||
size_t index = 0;
|
||||
|
||||
uint32_t te_short = subghz_protocol_subaru_const.te_short;
|
||||
uint32_t te_long = subghz_protocol_subaru_const.te_long;
|
||||
uint32_t gap_duration = 2500;
|
||||
uint32_t sync_duration = 2500;
|
||||
|
||||
for(int i = 0; i < 20; i++) {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_long);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_long);
|
||||
}
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_long);
|
||||
|
||||
instance->encoder.upload[index++] = level_duration_make(false, gap_duration);
|
||||
|
||||
instance->encoder.upload[index++] = level_duration_make(true, sync_duration);
|
||||
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_long);
|
||||
|
||||
for(int i = 63; i >= 0; i--) {
|
||||
bool bit = (instance->key >> i) & 1;
|
||||
if(bit) {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_short);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_short);
|
||||
} else {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_long);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, te_short);
|
||||
}
|
||||
}
|
||||
|
||||
instance->encoder.upload[index++] = level_duration_make(true, te_long);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, gap_duration * 2);
|
||||
|
||||
instance->encoder.size_upload = index;
|
||||
instance->encoder.front = 0;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_subaru_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSubaru* instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
do {
|
||||
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
if(ret != SubGhzProtocolStatusOk) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint64_t original_key = instance->generic.data;
|
||||
|
||||
uint8_t b[8];
|
||||
for(int i = 0; i < 8; i++) {
|
||||
b[i] = (uint8_t)(original_key >> (56 - i * 8));
|
||||
}
|
||||
|
||||
instance->serial = ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3];
|
||||
instance->button = b[0] & 0x0F;
|
||||
subaru_decode_count(b, &instance->count);
|
||||
|
||||
uint8_t original_custom_btn = subaru_btn_to_custom(instance->button);
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
subghz_custom_btn_set_original(original_custom_btn);
|
||||
}
|
||||
subghz_custom_btn_set_max(5);
|
||||
|
||||
uint8_t selected_custom_btn;
|
||||
if(subghz_custom_btn_get() == SUBGHZ_CUSTOM_BTN_OK) {
|
||||
selected_custom_btn = subghz_custom_btn_get_original();
|
||||
} else {
|
||||
selected_custom_btn = subghz_custom_btn_get();
|
||||
}
|
||||
|
||||
uint8_t new_button = subaru_get_button_code(selected_custom_btn);
|
||||
instance->button = new_button;
|
||||
|
||||
uint32_t mult = furi_hal_subghz_get_rolling_counter_mult();
|
||||
instance->count = (instance->count + mult) & 0xFFFF;
|
||||
|
||||
b[0] = (b[0] & 0xF0) | (instance->button & 0x0F);
|
||||
|
||||
subaru_encode_count(b, instance->count);
|
||||
|
||||
instance->key = 0;
|
||||
for(int i = 0; i < 8; i++) {
|
||||
instance->key = (instance->key << 8) | b[i];
|
||||
}
|
||||
|
||||
instance->generic.data = instance->key;
|
||||
instance->generic.serial = instance->serial;
|
||||
instance->generic.btn = instance->button;
|
||||
instance->generic.cnt = instance->count;
|
||||
|
||||
subghz_protocol_encoder_subaru_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->key >> (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;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi.h>
|
||||
#include <lib/subghz/protocols/base.h>
|
||||
#include <lib/subghz/types.h>
|
||||
#include <lib/subghz/blocks/const.h>
|
||||
#include <lib/subghz/blocks/decoder.h>
|
||||
#include <lib/subghz/blocks/encoder.h>
|
||||
#include <lib/subghz/blocks/generic.h>
|
||||
#include <lib/subghz/blocks/math.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
#include "base.h"
|
||||
#include "../blocks/math.h"
|
||||
|
||||
#define SUBGHZ_PROTOCOL_SUBARU_NAME "SUBARU"
|
||||
|
||||
#define SUBARU_PROTOCOL_NAME "Subaru"
|
||||
typedef struct SubGhzProtocolDecoderSubaru SubGhzProtocolDecoderSubaru;
|
||||
typedef struct SubGhzProtocolEncoderSubaru SubGhzProtocolEncoderSubaru;
|
||||
|
||||
extern const SubGhzProtocol subghz_protocol_subaru;
|
||||
|
||||
@@ -25,14 +20,14 @@ SubGhzProtocolStatus subghz_protocol_decoder_subaru_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_subaru_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_subaru_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_subaru_get_string(void* context, FuriString* output);
|
||||
|
||||
// Encoder functions
|
||||
void* subghz_protocol_encoder_subaru_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_subaru_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_subaru_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_subaru_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_subaru_yield(void* context);
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_subaru_deserialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#include "suzuki.h"
|
||||
|
||||
#define TAG "SuzukiProtocol"
|
||||
#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"
|
||||
|
||||
// ============================================================================
|
||||
// PROTOCOL CONSTANTS
|
||||
// ============================================================================
|
||||
#define TAG "SuzukiProtocol"
|
||||
|
||||
static const SubGhzBlockConst subghz_protocol_suzuki_const = {
|
||||
.te_short = 250,
|
||||
@@ -13,68 +16,88 @@ static const SubGhzBlockConst subghz_protocol_suzuki_const = {
|
||||
.min_count_bit_for_found = 64,
|
||||
};
|
||||
|
||||
#define SUZUKI_PREAMBLE_COUNT 350
|
||||
#define SUZUKI_GAP_TIME 2000
|
||||
#define SUZUKI_GAP_DELTA 399
|
||||
#define SUZUKI_GAP_TIME 2000
|
||||
#define SUZUKI_GAP_DELTA 399
|
||||
#define SUZUKI_MIN_PREAMBLE_COUNT 200
|
||||
#define SUZUKI_ENCODER_PREAMBLE_COUNT 300
|
||||
|
||||
// ============================================================================
|
||||
// DECODER STRUCT
|
||||
// ============================================================================
|
||||
|
||||
typedef struct SubGhzProtocolDecoderSuzuki {
|
||||
typedef struct SubGhzProtocolDecoderSuzuki
|
||||
{
|
||||
SubGhzProtocolDecoderBase base;
|
||||
SubGhzBlockDecoder decoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
uint16_t header_count;
|
||||
} SubGhzProtocolDecoderSuzuki;
|
||||
|
||||
// ============================================================================
|
||||
// ENCODER STRUCT
|
||||
// ============================================================================
|
||||
|
||||
typedef struct SubGhzProtocolEncoderSuzuki {
|
||||
typedef struct SubGhzProtocolEncoderSuzuki
|
||||
{
|
||||
SubGhzProtocolEncoderBase base;
|
||||
SubGhzProtocolBlockEncoder encoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
} SubGhzProtocolEncoderSuzuki;
|
||||
|
||||
// ============================================================================
|
||||
// DECODER STATE MACHINE
|
||||
// ============================================================================
|
||||
|
||||
typedef enum {
|
||||
typedef enum
|
||||
{
|
||||
SuzukiDecoderStepReset = 0,
|
||||
SuzukiDecoderStepCountPreamble = 1,
|
||||
SuzukiDecoderStepDecodeData = 2,
|
||||
} SuzukiDecoderStep;
|
||||
|
||||
// ============================================================================
|
||||
// HELPER FUNCTIONS
|
||||
// ============================================================================
|
||||
|
||||
static void suzuki_add_bit(SubGhzProtocolDecoderSuzuki* instance, uint8_t bit) {
|
||||
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | bit;
|
||||
instance->decoder.decode_count_bit++;
|
||||
}
|
||||
|
||||
static const char* suzuki_get_button_name(uint8_t btn) {
|
||||
switch(btn) {
|
||||
case 1:
|
||||
return "Panic";
|
||||
case 2:
|
||||
return "Boot";
|
||||
case 3:
|
||||
return "Lock";
|
||||
case 4:
|
||||
return "Unlock";
|
||||
default:
|
||||
return "Unknown";
|
||||
static uint8_t suzuki_crc8(uint8_t* data, size_t len) {
|
||||
uint8_t crc = 0x00;
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
crc ^= data[i];
|
||||
for(size_t j = 0; j < 8; j++) {
|
||||
if((crc & 0x80) != 0)
|
||||
crc = (uint8_t)((crc << 1) ^ 0x7F);
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
static uint8_t suzuki_calculate_crc(uint64_t data) {
|
||||
uint8_t crc_data[6];
|
||||
crc_data[0] = (data >> 52) & 0xFF;
|
||||
crc_data[1] = (data >> 44) & 0xFF;
|
||||
crc_data[2] = (data >> 36) & 0xFF;
|
||||
crc_data[3] = (data >> 28) & 0xFF;
|
||||
crc_data[4] = (data >> 20) & 0xFF;
|
||||
crc_data[5] = (data >> 12) & 0xFF;
|
||||
return suzuki_crc8(crc_data, 6);
|
||||
}
|
||||
|
||||
static uint8_t suzuki_custom_to_btn(uint8_t custom) {
|
||||
switch(custom) {
|
||||
case 1: return 3;
|
||||
case 2: return 4;
|
||||
case 3: return 2;
|
||||
case 4: return 1;
|
||||
default: return 4;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// PROTOCOL DEFINITION
|
||||
// ============================================================================
|
||||
static uint8_t suzuki_btn_to_custom(uint8_t btn) {
|
||||
switch(btn) {
|
||||
case 1: return 4;
|
||||
case 2: return 3;
|
||||
case 3: return 1;
|
||||
case 4: return 2;
|
||||
default: return 2;
|
||||
}
|
||||
}
|
||||
|
||||
static bool suzuki_verify_crc(uint64_t data) {
|
||||
uint8_t received_crc = (data >> 4) & 0xFF;
|
||||
uint8_t calculated_crc = suzuki_calculate_crc(data);
|
||||
return (received_crc == calculated_crc);
|
||||
}
|
||||
|
||||
const SubGhzProtocolDecoder subghz_protocol_suzuki_decoder = {
|
||||
.alloc = subghz_protocol_decoder_suzuki_alloc,
|
||||
@@ -95,55 +118,49 @@ const SubGhzProtocolEncoder subghz_protocol_suzuki_encoder = {
|
||||
.yield = subghz_protocol_encoder_suzuki_yield,
|
||||
};
|
||||
|
||||
const SubGhzProtocol suzuki_protocol = {
|
||||
.name = SUZUKI_PROTOCOL_NAME,
|
||||
const SubGhzProtocol subghz_protocol_suzuki = {
|
||||
.name = SUBGHZ_PROTOCOL_SUZUKI_NAME,
|
||||
.type = SubGhzProtocolTypeDynamic,
|
||||
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
|
||||
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM |
|
||||
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
|
||||
.decoder = &subghz_protocol_suzuki_decoder,
|
||||
.encoder = &subghz_protocol_suzuki_encoder,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// DECODER IMPLEMENTATION
|
||||
// ============================================================================
|
||||
|
||||
void* subghz_protocol_decoder_suzuki_alloc(SubGhzEnvironment* environment) {
|
||||
void *subghz_protocol_decoder_suzuki_alloc(SubGhzEnvironment *environment)
|
||||
{
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolDecoderSuzuki* instance = malloc(sizeof(SubGhzProtocolDecoderSuzuki));
|
||||
instance->base.protocol = &suzuki_protocol;
|
||||
SubGhzProtocolDecoderSuzuki *instance = malloc(sizeof(SubGhzProtocolDecoderSuzuki));
|
||||
instance->base.protocol = &subghz_protocol_suzuki;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_suzuki_free(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
void subghz_protocol_decoder_suzuki_free(void *context)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_suzuki_reset(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
void subghz_protocol_decoder_suzuki_reset(void *context)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
instance->decoder.parser_step = SuzukiDecoderStepReset;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_suzuki_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
void subghz_protocol_decoder_suzuki_feed(void *context, bool level, uint32_t duration)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
|
||||
switch(instance->decoder.parser_step) {
|
||||
switch (instance->decoder.parser_step)
|
||||
{
|
||||
case SuzukiDecoderStepReset:
|
||||
// Wait for HIGH pulse (~250µs) to start preamble
|
||||
if(!level) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(DURATION_DIFF(duration, subghz_protocol_suzuki_const.te_short) >
|
||||
subghz_protocol_suzuki_const.te_delta) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!level) return;
|
||||
if (DURATION_DIFF(duration, subghz_protocol_suzuki_const.te_short) > subghz_protocol_suzuki_const.te_delta) return;
|
||||
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
instance->decoder.parser_step = SuzukiDecoderStepCountPreamble;
|
||||
@@ -151,78 +168,86 @@ void subghz_protocol_decoder_suzuki_feed(void* context, bool level, uint32_t dur
|
||||
break;
|
||||
|
||||
case SuzukiDecoderStepCountPreamble:
|
||||
if(level) {
|
||||
// HIGH pulse
|
||||
if(instance->header_count >= 300) {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_suzuki_const.te_long) <=
|
||||
subghz_protocol_suzuki_const.te_delta) {
|
||||
if (level)
|
||||
{
|
||||
if (instance->header_count >= SUZUKI_MIN_PREAMBLE_COUNT)
|
||||
{
|
||||
if (DURATION_DIFF(duration, subghz_protocol_suzuki_const.te_long) <= subghz_protocol_suzuki_const.te_delta)
|
||||
{
|
||||
instance->decoder.parser_step = SuzukiDecoderStepDecodeData;
|
||||
suzuki_add_bit(instance, 1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(DURATION_DIFF(duration, subghz_protocol_suzuki_const.te_short) <=
|
||||
subghz_protocol_suzuki_const.te_delta) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DURATION_DIFF(duration, subghz_protocol_suzuki_const.te_short) <= subghz_protocol_suzuki_const.te_delta)
|
||||
{
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count++;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
instance->decoder.parser_step = SuzukiDecoderStepReset;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SuzukiDecoderStepDecodeData:
|
||||
if(level) {
|
||||
// HIGH pulse - determines bit value
|
||||
if(duration < subghz_protocol_suzuki_const.te_long) {
|
||||
if (level)
|
||||
{
|
||||
if (duration < subghz_protocol_suzuki_const.te_long)
|
||||
{
|
||||
uint32_t diff_long = 500 - duration;
|
||||
if(diff_long > 99) {
|
||||
uint32_t diff_short;
|
||||
if(duration < 250) {
|
||||
diff_short = 250 - duration;
|
||||
} else {
|
||||
diff_short = duration - 250;
|
||||
}
|
||||
|
||||
if(diff_short <= 99) {
|
||||
suzuki_add_bit(instance, 0);
|
||||
}
|
||||
} else {
|
||||
if (diff_long > 99)
|
||||
{
|
||||
uint32_t diff_short = (duration < 250) ? (250 - duration) : (duration - 250);
|
||||
if (diff_short <= 99) suzuki_add_bit(instance, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
suzuki_add_bit(instance, 1);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t diff_long = duration - 500;
|
||||
if(diff_long <= 99) {
|
||||
suzuki_add_bit(instance, 1);
|
||||
}
|
||||
if (diff_long <= 99) suzuki_add_bit(instance, 1);
|
||||
}
|
||||
} else {
|
||||
// LOW pulse - check for gap (end of transmission)
|
||||
uint32_t diff_gap;
|
||||
if(duration < SUZUKI_GAP_TIME) {
|
||||
diff_gap = SUZUKI_GAP_TIME - duration;
|
||||
} else {
|
||||
diff_gap = duration - SUZUKI_GAP_TIME;
|
||||
}
|
||||
|
||||
if(diff_gap <= SUZUKI_GAP_DELTA) {
|
||||
if(instance->decoder.decode_count_bit == 64) {
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t diff_gap = (duration < SUZUKI_GAP_TIME) ? (SUZUKI_GAP_TIME - duration) : (duration - SUZUKI_GAP_TIME);
|
||||
|
||||
if (diff_gap <= SUZUKI_GAP_DELTA)
|
||||
{
|
||||
if (instance->decoder.decode_count_bit == 64)
|
||||
{
|
||||
instance->generic.data = instance->decoder.decode_data;
|
||||
instance->generic.data_count_bit = 64;
|
||||
|
||||
uint64_t data = instance->generic.data;
|
||||
uint32_t data_high = (uint32_t)(data >> 32);
|
||||
uint32_t data_low = (uint32_t)data;
|
||||
if (suzuki_verify_crc(instance->generic.data))
|
||||
{
|
||||
uint64_t data = instance->generic.data;
|
||||
uint32_t data_high = (uint32_t)(data >> 32);
|
||||
uint32_t data_low = (uint32_t)data;
|
||||
|
||||
instance->generic.serial = ((data_high & 0xFFF) << 16) | (data_low >> 16);
|
||||
instance->generic.btn = (data_low >> 12) & 0xF;
|
||||
instance->generic.cnt = (data_high << 4) >> 16;
|
||||
|
||||
instance->generic.serial = ((data_high & 0xFFF) << 16) | (data_low >> 16);
|
||||
instance->generic.btn = (data_low >> 12) & 0xF;
|
||||
instance->generic.cnt = (data_high << 4) >> 16;
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
uint8_t custom = suzuki_btn_to_custom(instance->generic.btn);
|
||||
subghz_custom_btn_set_original(custom);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
if(instance->base.callback) {
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
if (instance->base.callback)
|
||||
{
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
instance->decoder.parser_step = SuzukiDecoderStepReset;
|
||||
@@ -232,186 +257,238 @@ void subghz_protocol_decoder_suzuki_feed(void* context, bool level, uint32_t dur
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t subghz_protocol_decoder_suzuki_get_hash_data(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(
|
||||
&instance->decoder, (instance->generic.data_count_bit / 8) + 1);
|
||||
uint8_t subghz_protocol_decoder_suzuki_get_hash_data(void *context)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(&instance->decoder, (instance->generic.data_count_bit / 8) + 1);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_suzuki_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
ret = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
// Save CRC
|
||||
uint32_t crc = (instance->generic.data >> 4) & 0xFF;
|
||||
flipper_format_write_uint32(flipper_format, "CRC", &crc, 1);
|
||||
|
||||
// Save decoded fields
|
||||
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);
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_suzuki_serialize(void *context, FlipperFormat *flipper_format, SubGhzRadioPreset *preset)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
|
||||
uint32_t temp_serial = instance->generic.serial;
|
||||
uint32_t temp_cnt = instance->generic.cnt;
|
||||
uint32_t temp_btn = instance->generic.btn;
|
||||
|
||||
instance->generic.serial = 0;
|
||||
instance->generic.cnt = 0;
|
||||
instance->generic.btn = 0;
|
||||
|
||||
SubGhzProtocolStatus ret = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
|
||||
instance->generic.serial = temp_serial;
|
||||
instance->generic.cnt = temp_cnt;
|
||||
instance->generic.btn = temp_btn;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_suzuki_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
return subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_suzuki_deserialize(void *context, FlipperFormat *flipper_format)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
|
||||
SubGhzProtocolStatus ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
|
||||
if(ret == SubGhzProtocolStatusOk) {
|
||||
uint64_t data = instance->generic.data;
|
||||
instance->generic.cnt = (uint32_t)((data >> 44) & 0xFFFFF);
|
||||
instance->generic.serial = (uint32_t)((data >> 16) & 0x0FFFFFFF);
|
||||
instance->generic.btn = (uint8_t)((data >> 12) & 0xF);
|
||||
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
uint8_t custom = suzuki_btn_to_custom(instance->generic.btn);
|
||||
subghz_custom_btn_set_original(custom);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_suzuki_get_string(void* context, FuriString* output) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolDecoderSuzuki* instance = context;
|
||||
static const char *suzuki_get_button_name(uint8_t btn)
|
||||
{
|
||||
switch (btn)
|
||||
{
|
||||
case 1: return "Panic";
|
||||
case 2: return "Trunk";
|
||||
case 3: return "Lock";
|
||||
case 4: return "Unlock";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_suzuki_get_string(void *context, FuriString *output) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSuzuki *instance = context;
|
||||
|
||||
uint64_t data = instance->generic.data;
|
||||
uint32_t key_high = (data >> 32) & 0xFFFFFFFF;
|
||||
uint32_t key_low = data & 0xFFFFFFFF;
|
||||
uint8_t crc = (data >> 4) & 0xFF;
|
||||
|
||||
uint8_t received_crc = (data >> 4) & 0xFF;
|
||||
|
||||
uint8_t calculated_crc = suzuki_calculate_crc(data);
|
||||
bool crc_valid = (received_crc == calculated_crc);
|
||||
|
||||
furi_string_cat_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:%08lX%08lX\r\n"
|
||||
"Sn:%07lX Btn:%X %s\r\n"
|
||||
"Cnt:%04lX CRC:%02X\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,
|
||||
key_high,
|
||||
key_low,
|
||||
instance->generic.serial,
|
||||
instance->generic.cnt,
|
||||
instance->generic.btn,
|
||||
suzuki_get_button_name(instance->generic.btn),
|
||||
instance->generic.cnt,
|
||||
crc);
|
||||
received_crc,
|
||||
crc_valid ? "(OK)" : "(FAIL)");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ENCODER IMPLEMENTATION
|
||||
// ============================================================================
|
||||
|
||||
void* subghz_protocol_encoder_suzuki_alloc(SubGhzEnvironment* environment) {
|
||||
void *subghz_protocol_encoder_suzuki_alloc(SubGhzEnvironment *environment)
|
||||
{
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderSuzuki* instance = malloc(sizeof(SubGhzProtocolEncoderSuzuki));
|
||||
instance->base.protocol = &suzuki_protocol;
|
||||
SubGhzProtocolEncoderSuzuki *instance = malloc(sizeof(SubGhzProtocolEncoderSuzuki));
|
||||
instance->base.protocol = &subghz_protocol_suzuki;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
instance->encoder.upload = NULL;
|
||||
instance->encoder.is_running = false;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_suzuki_free(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSuzuki* instance = context;
|
||||
void subghz_protocol_encoder_suzuki_free(void *context)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSuzuki *instance = context;
|
||||
if(instance->encoder.upload) {
|
||||
free(instance->encoder.upload);
|
||||
}
|
||||
free(instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the upload buffer for transmission
|
||||
* Signal format: 350 preamble pairs (SHORT HIGH/SHORT LOW) + 64 data bits + gap
|
||||
* Data encoding: SHORT HIGH = 0, LONG HIGH = 1
|
||||
*/
|
||||
static void subghz_protocol_encoder_suzuki_get_upload(SubGhzProtocolEncoderSuzuki* instance) {
|
||||
furi_check(instance);
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
// Free old upload if exists
|
||||
if(instance->encoder.upload) {
|
||||
free(instance->encoder.upload);
|
||||
}
|
||||
|
||||
// Allocate: preamble pairs + data bits (each has HIGH + LOW) + end gap
|
||||
instance->encoder.size_upload = (SUZUKI_PREAMBLE_COUNT * 2) + (64 * 2) + 1;
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
|
||||
// Preamble: SHORT HIGH / SHORT LOW pairs
|
||||
for(size_t i = 0; i < SUZUKI_PREAMBLE_COUNT; i++) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, subghz_protocol_suzuki_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, subghz_protocol_suzuki_const.te_short);
|
||||
}
|
||||
|
||||
// Data: 64 bits, MSB first
|
||||
// SHORT HIGH (~250µs) = 0, LONG HIGH (~500µs) = 1
|
||||
for(int bit = 63; bit >= 0; bit--) {
|
||||
if((instance->generic.data >> bit) & 1) {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, subghz_protocol_suzuki_const.te_long);
|
||||
} else {
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, subghz_protocol_suzuki_const.te_short);
|
||||
}
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, subghz_protocol_suzuki_const.te_short);
|
||||
}
|
||||
|
||||
// End gap
|
||||
instance->encoder.upload[index++] = level_duration_make(false, SUZUKI_GAP_TIME);
|
||||
|
||||
instance->encoder.size_upload = index;
|
||||
instance->encoder.front = 0;
|
||||
}
|
||||
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_suzuki_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSuzuki* instance = context;
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_suzuki_deserialize(void *context, FlipperFormat *flipper_format)
|
||||
{
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSuzuki *instance = context;
|
||||
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
|
||||
|
||||
do {
|
||||
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
if(ret != SubGhzProtocolStatusOk) {
|
||||
if (ret != SubGhzProtocolStatusOk) break;
|
||||
|
||||
uint64_t data = instance->generic.data;
|
||||
|
||||
instance->generic.cnt = (uint32_t)((data >> 44) & 0xFFFFF);
|
||||
instance->generic.serial = (uint32_t)((data >> 16) & 0x0FFFFFFF);
|
||||
instance->generic.btn = (uint8_t)((data >> 12) & 0xF);
|
||||
|
||||
if(subghz_custom_btn_get_original() == 0) {
|
||||
uint8_t custom = suzuki_btn_to_custom(instance->generic.btn);
|
||||
subghz_custom_btn_set_original(custom);
|
||||
}
|
||||
subghz_custom_btn_set_max(4);
|
||||
|
||||
uint32_t mult = furi_hal_subghz_get_rolling_counter_mult();
|
||||
instance->generic.cnt = (instance->generic.cnt + mult) & 0xFFFFF;
|
||||
|
||||
uint8_t selected = subghz_custom_btn_get() == SUBGHZ_CUSTOM_BTN_OK ?
|
||||
subghz_custom_btn_get_original() :
|
||||
subghz_custom_btn_get();
|
||||
uint8_t btn = suzuki_custom_to_btn(selected);
|
||||
instance->generic.btn = btn;
|
||||
|
||||
uint64_t new_data = 0;
|
||||
new_data |= ((uint64_t)(instance->generic.cnt & 0xFFFFF) << 44);
|
||||
new_data |= ((uint64_t)(instance->generic.serial & 0x0FFFFFFF) << 16);
|
||||
new_data |= ((uint64_t)(instance->generic.btn & 0xF) << 12);
|
||||
|
||||
uint8_t crc = suzuki_calculate_crc(new_data);
|
||||
new_data |= ((uint64_t)crc << 4);
|
||||
|
||||
instance->generic.data = new_data;
|
||||
|
||||
if(!flipper_format_rewind(flipper_format)) {
|
||||
ret = SubGhzProtocolStatusErrorParserOthers;
|
||||
break;
|
||||
}
|
||||
|
||||
subghz_protocol_encoder_suzuki_get_upload(instance);
|
||||
uint8_t key_data[8];
|
||||
for(size_t i = 0; i < 8; i++) {
|
||||
key_data[i] = (instance->generic.data >> (56 - i * 8)) & 0xFF;
|
||||
}
|
||||
|
||||
if(!flipper_format_update_hex(flipper_format, "Key", key_data, 8)) {
|
||||
ret = SubGhzProtocolStatusErrorParserKey;
|
||||
break;
|
||||
}
|
||||
|
||||
size_t preamble_count = SUZUKI_ENCODER_PREAMBLE_COUNT;
|
||||
size_t bit_count = 64;
|
||||
instance->encoder.size_upload = (preamble_count * 2) + (bit_count * 2) + 1;
|
||||
|
||||
if(instance->encoder.upload) {
|
||||
free(instance->encoder.upload);
|
||||
}
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
for (size_t i = 0; i < preamble_count; i++) {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, subghz_protocol_suzuki_const.te_short);
|
||||
instance->encoder.upload[index++] = level_duration_make(false, subghz_protocol_suzuki_const.te_short);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bit_count; i++) {
|
||||
uint8_t bit = (instance->generic.data >> (63 - i)) & 1;
|
||||
|
||||
if (bit) {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, subghz_protocol_suzuki_const.te_long);
|
||||
} else {
|
||||
instance->encoder.upload[index++] = level_duration_make(true, subghz_protocol_suzuki_const.te_short);
|
||||
}
|
||||
instance->encoder.upload[index++] = level_duration_make(false, subghz_protocol_suzuki_const.te_short);
|
||||
}
|
||||
|
||||
instance->encoder.upload[index++] = level_duration_make(false, SUZUKI_GAP_TIME);
|
||||
|
||||
instance->encoder.is_running = true;
|
||||
instance->encoder.repeat = 10;
|
||||
instance->encoder.repeat = 5;
|
||||
instance->encoder.front = 0;
|
||||
|
||||
ret = SubGhzProtocolStatusOk;
|
||||
} while(false);
|
||||
|
||||
} while (false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_suzuki_stop(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSuzuki* instance = context;
|
||||
void subghz_protocol_encoder_suzuki_stop(void *context)
|
||||
{
|
||||
SubGhzProtocolEncoderSuzuki *instance = context;
|
||||
instance->encoder.is_running = false;
|
||||
}
|
||||
|
||||
LevelDuration subghz_protocol_encoder_suzuki_yield(void* context) {
|
||||
furi_check(context);
|
||||
SubGhzProtocolEncoderSuzuki* instance = context;
|
||||
LevelDuration subghz_protocol_encoder_suzuki_yield(void *context)
|
||||
{
|
||||
SubGhzProtocolEncoderSuzuki *instance = context;
|
||||
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
|
||||
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) {
|
||||
if (++instance->encoder.front == instance->encoder.size_upload)
|
||||
{
|
||||
instance->encoder.repeat--;
|
||||
instance->encoder.front = 0;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi.h>
|
||||
#include <lib/subghz/protocols/base.h>
|
||||
#include <lib/subghz/types.h>
|
||||
#include <lib/subghz/blocks/const.h>
|
||||
#include <lib/subghz/blocks/decoder.h>
|
||||
#include <lib/subghz/blocks/encoder.h>
|
||||
#include <lib/subghz/blocks/generic.h>
|
||||
#include <lib/subghz/blocks/math.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
#include "base.h"
|
||||
#include "../blocks/math.h"
|
||||
|
||||
#define SUZUKI_PROTOCOL_NAME "Suzuki"
|
||||
#define SUBGHZ_PROTOCOL_SUZUKI_NAME "SUZUKI"
|
||||
|
||||
extern const SubGhzProtocol suzuki_protocol;
|
||||
typedef struct SubGhzProtocolDecoderSuzuki SubGhzProtocolDecoderSuzuki;
|
||||
typedef struct SubGhzProtocolEncoderSuzuki SubGhzProtocolEncoderSuzuki;
|
||||
|
||||
extern const SubGhzProtocol subghz_protocol_suzuki;
|
||||
|
||||
// Decoder functions
|
||||
void* subghz_protocol_decoder_suzuki_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_decoder_suzuki_free(void* context);
|
||||
void subghz_protocol_decoder_suzuki_reset(void* context);
|
||||
@@ -24,14 +19,11 @@ SubGhzProtocolStatus subghz_protocol_decoder_suzuki_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_decoder_suzuki_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
SubGhzProtocolStatus subghz_protocol_decoder_suzuki_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_decoder_suzuki_get_string(void* context, FuriString* output);
|
||||
|
||||
// Encoder functions
|
||||
void* subghz_protocol_encoder_suzuki_alloc(SubGhzEnvironment* environment);
|
||||
void subghz_protocol_encoder_suzuki_free(void* context);
|
||||
SubGhzProtocolStatus
|
||||
subghz_protocol_encoder_suzuki_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
SubGhzProtocolStatus subghz_protocol_encoder_suzuki_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
void subghz_protocol_encoder_suzuki_stop(void* context);
|
||||
LevelDuration subghz_protocol_encoder_suzuki_yield(void* context);
|
||||
|
||||
Reference in New Issue
Block a user