[FL-2393][FL-2381] iButton, OneWire: move to plain C (#1068)

* iButton: getting started on the worker concept
* Hal delay: added global instructions_per_us variable
* iButton: one wire slave
* iButton: ibutton key setter
* iButton: one wire host, use ibutton_hal
* iButton\RFID: common pulse decoder concept
* iButton: cyfral decoder
* iButton: worker thread concept
* iButton: metakom decoder
* iButton: write key through worker
* iButton: worker mode holder
* iButton: worker improvements
* iButton: Cyfral encoder
* iButton: Metakom encoder
* lib: pulse protocol helpers
* iButton: Metakom decoder
* iButton: Cyfral decoder
* iButton worker: separate modes
* iButton: libs documentation
* HAL: iButton gpio modes
* iButton worker: rename modes file
* iButton worker, hal: move to LL
* iButton CLI: worker for reading and emulation commands
* iButton HAL: correct init and emulation sequence
* iButton cli: moved to plain C
* iButton: move to worker, small step to plain C
* Libs, one wire: move to plain C
* Libs: added forgotten files to compilation
* iButton writer: get rid of manual disable/enable irq
This commit is contained in:
SG
2022-03-29 23:01:56 +10:00
committed by GitHub
parent d15a9500c6
commit bdba15b366
112 changed files with 4444 additions and 3231 deletions

View File

@@ -0,0 +1,126 @@
#include "encoder_cyfral.h"
#include <furi_hal.h>
#define CYFRAL_DATA_SIZE sizeof(uint16_t)
#define CYFRAL_PERIOD (125 * instructions_per_us)
#define CYFRAL_0_LOW (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_0_HI (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_LOW (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_HI (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_SET_DATA(level, len) \
*polarity = level; \
*length = len;
struct EncoderCyfral {
uint32_t data;
uint32_t index;
};
EncoderCyfral* encoder_cyfral_alloc() {
EncoderCyfral* cyfral = malloc(sizeof(EncoderCyfral));
encoder_cyfral_reset(cyfral);
return cyfral;
}
void encoder_cyfral_free(EncoderCyfral* cyfral) {
free(cyfral);
}
void encoder_cyfral_reset(EncoderCyfral* cyfral) {
cyfral->data = 0;
cyfral->index = 0;
}
uint32_t cyfral_encoder_encode(const uint16_t data) {
uint32_t value = 0;
for(int8_t i = 0; i <= 7; i++) {
switch((data >> (i * 2)) & 0b00000011) {
case 0b11:
value = value << 4;
value += 0b00000111;
break;
case 0b10:
value = value << 4;
value += 0b00001011;
break;
case 0b01:
value = value << 4;
value += 0b00001101;
break;
case 0b00:
value = value << 4;
value += 0b00001110;
break;
default:
break;
}
}
return value;
}
void encoder_cyfral_set_data(EncoderCyfral* cyfral, const uint8_t* data, size_t data_size) {
furi_assert(cyfral);
furi_check(data_size >= CYFRAL_DATA_SIZE);
uint16_t intermediate;
memcpy(&intermediate, data, CYFRAL_DATA_SIZE);
cyfral->data = cyfral_encoder_encode(intermediate);
}
void encoder_cyfral_get_pulse(EncoderCyfral* cyfral, bool* polarity, uint32_t* length) {
if(cyfral->index < 8) {
// start word (0b0001)
switch(cyfral->index) {
case 0:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 1:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 2:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 3:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 4:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 5:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 6:
CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
break;
case 7:
CYFRAL_SET_DATA(true, CYFRAL_1_HI);
break;
}
} else {
// data
uint8_t data_start_index = cyfral->index - 8;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = ((cyfral->data >> bit_index) & 1);
if(!clock_polarity) {
if(bit_value) {
CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
} else {
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
}
} else {
if(bit_value) {
CYFRAL_SET_DATA(true, CYFRAL_1_HI);
} else {
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
}
}
}
cyfral->index++;
if(cyfral->index >= (9 * 4 * 2)) {
cyfral->index = 0;
}
}

View File

@@ -0,0 +1,54 @@
/**
* @file encoder_cyfral.h
*
* Cyfral pulse format encoder
*/
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct EncoderCyfral EncoderCyfral;
/**
* Allocate Cyfral encoder
* @return EncoderCyfral*
*/
EncoderCyfral* encoder_cyfral_alloc();
/**
* Deallocate Cyfral encoder
* @param cyfral
*/
void encoder_cyfral_free(EncoderCyfral* cyfral);
/**
* Reset Cyfral encoder
* @param cyfral
*/
void encoder_cyfral_reset(EncoderCyfral* cyfral);
/**
* Set data to be encoded to Cyfral pulse format, 2 bytes
* @param cyfral
* @param data
* @param data_size
*/
void encoder_cyfral_set_data(EncoderCyfral* cyfral, const uint8_t* data, size_t data_size);
/**
* Pop pulse from Cyfral encoder
* @param cyfral
* @param polarity
* @param length
*/
void encoder_cyfral_get_pulse(EncoderCyfral* cyfral, bool* polarity, uint32_t* length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,93 @@
#include "encoder_metakom.h"
#include <furi_hal.h>
#define METAKOM_DATA_SIZE sizeof(uint32_t)
#define METAKOM_PERIOD (125 * instructions_per_us)
#define METAKOM_0_LOW (METAKOM_PERIOD * 0.33f)
#define METAKOM_0_HI (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_LOW (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_HI (METAKOM_PERIOD * 0.33f)
#define METAKOM_SET_DATA(level, len) \
*polarity = !level; \
*length = len;
struct EncoderMetakom {
uint32_t data;
uint32_t index;
};
EncoderMetakom* encoder_metakom_alloc() {
EncoderMetakom* metakom = malloc(sizeof(EncoderMetakom));
encoder_metakom_reset(metakom);
return metakom;
}
void encoder_metakom_free(EncoderMetakom* metakom) {
free(metakom);
}
void encoder_metakom_reset(EncoderMetakom* metakom) {
metakom->data = 0;
metakom->index = 0;
}
void encoder_metakom_set_data(EncoderMetakom* metakom, const uint8_t* data, size_t data_size) {
furi_assert(metakom);
furi_check(data_size >= METAKOM_DATA_SIZE);
memcpy(&metakom->data, data, METAKOM_DATA_SIZE);
}
void encoder_metakom_get_pulse(EncoderMetakom* metakom, bool* polarity, uint32_t* length) {
if(metakom->index == 0) {
// sync bit
METAKOM_SET_DATA(true, METAKOM_PERIOD);
} else if(metakom->index >= 1 && metakom->index <= 6) {
// start word (0b010)
switch(metakom->index) {
case 1:
METAKOM_SET_DATA(false, METAKOM_0_LOW);
break;
case 2:
METAKOM_SET_DATA(true, METAKOM_0_HI);
break;
case 3:
METAKOM_SET_DATA(false, METAKOM_1_LOW);
break;
case 4:
METAKOM_SET_DATA(true, METAKOM_1_HI);
break;
case 5:
METAKOM_SET_DATA(false, METAKOM_0_LOW);
break;
case 6:
METAKOM_SET_DATA(true, METAKOM_0_HI);
break;
}
} else {
// data
uint8_t data_start_index = metakom->index - 7;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = (metakom->data >> (32 - 1 - bit_index)) & 1;
if(!clock_polarity) {
if(bit_value) {
METAKOM_SET_DATA(false, METAKOM_1_LOW);
} else {
METAKOM_SET_DATA(false, METAKOM_0_LOW);
}
} else {
if(bit_value) {
METAKOM_SET_DATA(true, METAKOM_1_HI);
} else {
METAKOM_SET_DATA(true, METAKOM_0_HI);
}
}
}
metakom->index++;
if(metakom->index >= (1 + 3 * 2 + 32 * 2)) {
metakom->index = 0;
}
}

View File

@@ -0,0 +1,54 @@
/**
* @file encoder_metakom.h
*
* Metakom pulse format encoder
*/
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct EncoderMetakom EncoderMetakom;
/**
* Allocate Metakom encoder
* @return EncoderMetakom*
*/
EncoderMetakom* encoder_metakom_alloc();
/**
* Deallocate Metakom encoder
* @param metakom
*/
void encoder_metakom_free(EncoderMetakom* metakom);
/**
* Reset Metakom encoder
* @param metakom
*/
void encoder_metakom_reset(EncoderMetakom* metakom);
/**
* Set data to be encoded to Metakom pulse format, 4 bytes
* @param metakom
* @param data
* @param data_size
*/
void encoder_metakom_set_data(EncoderMetakom* metakom, const uint8_t* data, size_t data_size);
/**
* Pop pulse from Metakom encoder
* @param cyfral
* @param polarity
* @param length
*/
void encoder_metakom_get_pulse(EncoderMetakom* metakom, bool* polarity, uint32_t* length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,121 @@
#include <furi.h>
#include <one_wire/maxim_crc.h>
#include "ibutton_key.h"
struct iButtonKey {
uint8_t data[IBUTTON_KEY_DATA_SIZE];
char name[IBUTTON_KEY_NAME_SIZE];
iButtonKeyType type;
};
iButtonKey* ibutton_key_alloc() {
iButtonKey* key = malloc(sizeof(iButtonKey));
memset(key, 0, sizeof(iButtonKey));
return key;
}
void ibutton_key_free(iButtonKey* key) {
free(key);
}
void ibutton_key_set(iButtonKey* to, const iButtonKey* from) {
memcpy(to, from, sizeof(iButtonKey));
}
void ibutton_key_set_data(iButtonKey* key, uint8_t* data, uint8_t data_count) {
furi_check(data_count > 0);
furi_check(data_count <= IBUTTON_KEY_DATA_SIZE);
memset(key->data, 0, IBUTTON_KEY_DATA_SIZE);
memcpy(key->data, data, data_count);
}
void ibutton_key_clear_data(iButtonKey* key) {
memset(key->data, 0, IBUTTON_KEY_DATA_SIZE);
}
const uint8_t* ibutton_key_get_data_p(iButtonKey* key) {
return key->data;
}
uint8_t ibutton_key_get_data_size(iButtonKey* key) {
return ibutton_key_get_size_by_type(key->type);
}
void ibutton_key_set_name(iButtonKey* key, const char* name) {
strlcpy(key->name, name, IBUTTON_KEY_NAME_SIZE);
}
const char* ibutton_key_get_name_p(iButtonKey* key) {
return key->name;
}
void ibutton_key_set_type(iButtonKey* key, iButtonKeyType key_type) {
key->type = key_type;
}
iButtonKeyType ibutton_key_get_type(iButtonKey* key) {
return key->type;
}
const char* ibutton_key_get_string_by_type(iButtonKeyType key_type) {
switch(key_type) {
case iButtonKeyCyfral:
return "Cyfral";
break;
case iButtonKeyMetakom:
return "Metakom";
break;
case iButtonKeyDS1990:
return "Dallas";
break;
default:
furi_crash("Invalid iButton type");
return "";
break;
}
}
bool ibutton_key_get_type_by_string(const char* type_string, iButtonKeyType* key_type) {
if(strcmp(type_string, ibutton_key_get_string_by_type(iButtonKeyCyfral)) == 0) {
*key_type = iButtonKeyCyfral;
} else if(strcmp(type_string, ibutton_key_get_string_by_type(iButtonKeyMetakom)) == 0) {
*key_type = iButtonKeyMetakom;
} else if(strcmp(type_string, ibutton_key_get_string_by_type(iButtonKeyDS1990)) == 0) {
*key_type = iButtonKeyDS1990;
} else {
return false;
}
return true;
}
uint8_t ibutton_key_get_size_by_type(iButtonKeyType key_type) {
uint8_t size = 0;
switch(key_type) {
case iButtonKeyCyfral:
size = 2;
break;
case iButtonKeyMetakom:
size = 4;
break;
case iButtonKeyDS1990:
size = 8;
break;
}
return size;
}
uint8_t ibutton_key_get_max_size() {
return IBUTTON_KEY_DATA_SIZE;
}
bool ibutton_key_dallas_crc_is_valid(iButtonKey* key) {
return (maxim_crc8(key->data, 8, MAXIM_CRC8_INIT) == 0);
}
bool ibutton_key_dallas_is_1990_key(iButtonKey* key) {
return (key->data[0] == 0x01);
}

View File

@@ -0,0 +1,145 @@
/**
* @file ibutton_key.h
*
* iButton key data holder
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define IBUTTON_KEY_DATA_SIZE 8
#define IBUTTON_KEY_NAME_SIZE 22
typedef enum {
iButtonKeyDS1990,
iButtonKeyCyfral,
iButtonKeyMetakom,
} iButtonKeyType;
typedef struct iButtonKey iButtonKey;
/**
* Allocate key
* @return iButtonKey*
*/
iButtonKey* ibutton_key_alloc();
/**
* Free key
* @param key
*/
void ibutton_key_free(iButtonKey* key);
/**
* Copy key
* @param to
* @param from
*/
void ibutton_key_set(iButtonKey* to, const iButtonKey* from);
/**
* Set key data
* @param key
* @param data
* @param data_count
*/
void ibutton_key_set_data(iButtonKey* key, uint8_t* data, uint8_t data_count);
/**
* Clear key data
* @param key
*/
void ibutton_key_clear_data(iButtonKey* key);
/**
* Get pointer to key data
* @param key
* @return const uint8_t*
*/
const uint8_t* ibutton_key_get_data_p(iButtonKey* key);
/**
* Get key data size
* @param key
* @return uint8_t
*/
uint8_t ibutton_key_get_data_size(iButtonKey* key);
/**
* Set key name
* @param key
* @param name
*/
void ibutton_key_set_name(iButtonKey* key, const char* name);
/**
* Get pointer to key name
* @param key
* @return const char*
*/
const char* ibutton_key_get_name_p(iButtonKey* key);
/**
* Set key type
* @param key
* @param key_type
*/
void ibutton_key_set_type(iButtonKey* key, iButtonKeyType key_type);
/**
* Get key type
* @param key
* @return iButtonKeyType
*/
iButtonKeyType ibutton_key_get_type(iButtonKey* key);
/**
* Get type string from key type
* @param key_type
* @return const char*
*/
const char* ibutton_key_get_string_by_type(iButtonKeyType key_type);
/**
* Get key type from string
* @param type_string
* @param key_type
* @return bool
*/
bool ibutton_key_get_type_by_string(const char* type_string, iButtonKeyType* key_type);
/**
* Get key data size from type
* @param key_type
* @return uint8_t
*/
uint8_t ibutton_key_get_size_by_type(iButtonKeyType key_type);
/**
* Get max key size
* @return uint8_t
*/
uint8_t ibutton_key_get_max_size();
/**
* Check if CRC for onewire key is valid
* @param key
* @return true
* @return false
*/
bool ibutton_key_dallas_crc_is_valid(iButtonKey* key);
/**
* Check if onewire key is a DS1990 key
* @param key
* @return true
* @return false
*/
bool ibutton_key_dallas_is_1990_key(iButtonKey* key);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,28 @@
/**
* @file ibutton_key_command.h
*
* List of misc commands for Dallas and blanks
*/
#pragma once
#define RW1990_1_CMD_WRITE_RECORD_FLAG 0xD1
#define RW1990_1_CMD_READ_RECORD_FLAG 0xB5
#define RW1990_1_CMD_WRITE_ROM 0xD5
#define RW1990_2_CMD_WRITE_RECORD_FLAG 0x1D
#define RW1990_2_CMD_READ_RECORD_FLAG 0x1E
#define RW1990_2_CMD_WRITE_ROM 0xD5
#define TM2004_CMD_READ_STATUS 0xAA
#define TM2004_CMD_READ_MEMORY 0xF0
#define TM2004_CMD_WRITE_ROM 0x3C
#define TM2004_CMD_FINALIZATION 0x35
#define TM2004_ANSWER_READ_MEMORY 0xF5
#define TM01_CMD_WRITE_RECORD_FLAG 0xC1
#define TM01_CMD_WRITE_ROM 0xC5
#define TM01_CMD_SWITCH_TO_CYFRAL 0xCA
#define TM01_CMD_SWITCH_TO_METAKOM 0xCB
#define DS1990_CMD_READ_ROM 0x33

View File

@@ -0,0 +1,197 @@
#include <furi.h>
#include <furi_hal.h>
#include <atomic.h>
#include "ibutton_worker_i.h"
typedef enum {
iButtonMessageEnd,
iButtonMessageStop,
iButtonMessageRead,
iButtonMessageWrite,
iButtonMessageEmulate,
} iButtonMessageType;
typedef struct {
iButtonMessageType type;
union {
iButtonKey* key;
} data;
} iButtonMessage;
static int32_t ibutton_worker_thread(void* thread_context);
iButtonWorker* ibutton_worker_alloc() {
iButtonWorker* worker = malloc(sizeof(iButtonWorker));
worker->key_p = NULL;
worker->key_data = malloc(ibutton_key_get_max_size());
worker->host = onewire_host_alloc();
worker->slave = onewire_slave_alloc();
worker->writer = ibutton_writer_alloc(worker->host);
worker->device = onewire_device_alloc(0, 0, 0, 0, 0, 0, 0, 0);
worker->pulse_decoder = pulse_decoder_alloc();
worker->protocol_cyfral = protocol_cyfral_alloc();
worker->protocol_metakom = protocol_metakom_alloc();
worker->messages = osMessageQueueNew(1, sizeof(iButtonMessage), NULL);
worker->mode_index = iButtonWorkerIdle;
worker->last_dwt_value = 0;
worker->read_cb = NULL;
worker->write_cb = NULL;
worker->emulate_cb = NULL;
worker->cb_ctx = NULL;
worker->encoder_cyfral = encoder_cyfral_alloc();
worker->encoder_metakom = encoder_metakom_alloc();
worker->thread = furi_thread_alloc();
furi_thread_set_name(worker->thread, "ibutton_worker");
furi_thread_set_callback(worker->thread, ibutton_worker_thread);
furi_thread_set_context(worker->thread, worker);
furi_thread_set_stack_size(worker->thread, 2048);
pulse_decoder_add_protocol(
worker->pulse_decoder,
protocol_cyfral_get_protocol(worker->protocol_cyfral),
PulseProtocolCyfral);
pulse_decoder_add_protocol(
worker->pulse_decoder,
protocol_metakom_get_protocol(worker->protocol_metakom),
PulseProtocolMetakom);
return worker;
}
void ibutton_worker_read_set_callback(
iButtonWorker* worker,
iButtonWorkerReadCallback callback,
void* context) {
furi_check(worker->mode_index == iButtonWorkerIdle);
worker->read_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_write_set_callback(
iButtonWorker* worker,
iButtonWorkerWriteCallback callback,
void* context) {
furi_check(worker->mode_index == iButtonWorkerIdle);
worker->write_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_emulate_set_callback(
iButtonWorker* worker,
iButtonWorkerEmulateCallback callback,
void* context) {
furi_check(worker->mode_index == iButtonWorkerIdle);
worker->emulate_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_read_start(iButtonWorker* worker, iButtonKey* key) {
iButtonMessage message = {.type = iButtonMessageRead, .data.key = key};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_write_start(iButtonWorker* worker, iButtonKey* key) {
iButtonMessage message = {.type = iButtonMessageWrite, .data.key = key};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_emulate_start(iButtonWorker* worker, iButtonKey* key) {
iButtonMessage message = {.type = iButtonMessageEmulate, .data.key = key};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_stop(iButtonWorker* worker) {
iButtonMessage message = {.type = iButtonMessageStop};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_free(iButtonWorker* worker) {
pulse_decoder_free(worker->pulse_decoder);
protocol_metakom_free(worker->protocol_metakom);
protocol_cyfral_free(worker->protocol_cyfral);
ibutton_writer_free(worker->writer);
onewire_slave_free(worker->slave);
onewire_host_free(worker->host);
onewire_device_free(worker->device);
encoder_cyfral_free(worker->encoder_cyfral);
encoder_metakom_free(worker->encoder_metakom);
osMessageQueueDelete(worker->messages);
furi_thread_free(worker->thread);
free(worker->key_data);
free(worker);
}
void ibutton_worker_start_thread(iButtonWorker* worker) {
furi_thread_start(worker->thread);
}
void ibutton_worker_stop_thread(iButtonWorker* worker) {
iButtonMessage message = {.type = iButtonMessageEnd};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
furi_thread_join(worker->thread);
}
void ibutton_worker_switch_mode(iButtonWorker* worker, iButtonWorkerMode mode) {
ibutton_worker_modes[worker->mode_index].stop(worker);
worker->mode_index = mode;
ibutton_worker_modes[worker->mode_index].start(worker);
}
void ibutton_worker_set_key_p(iButtonWorker* worker, iButtonKey* key) {
worker->key_p = key;
}
static int32_t ibutton_worker_thread(void* thread_context) {
iButtonWorker* worker = thread_context;
bool running = true;
iButtonMessage message;
osStatus_t status;
ibutton_worker_modes[worker->mode_index].start(worker);
while(running) {
status = osMessageQueueGet(
worker->messages, &message, NULL, ibutton_worker_modes[worker->mode_index].quant);
if(status == osOK) {
switch(message.type) {
case iButtonMessageEnd:
ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
ibutton_worker_set_key_p(worker, NULL);
running = false;
break;
case iButtonMessageStop:
ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
ibutton_worker_set_key_p(worker, NULL);
break;
case iButtonMessageRead:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerRead);
break;
case iButtonMessageWrite:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerWrite);
break;
case iButtonMessageEmulate:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerEmulate);
break;
}
} else if(status == osErrorTimeout) {
ibutton_worker_modes[worker->mode_index].tick(worker);
} else {
furi_crash("iButton worker error");
}
}
ibutton_worker_modes[worker->mode_index].stop(worker);
return 0;
}

View File

@@ -0,0 +1,113 @@
/**
* @file ibutton_worker.h
*
* iButton worker
*/
#pragma once
#include "ibutton_key.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
iButtonWorkerWriteOK,
iButtonWorkerWriteSameKey,
iButtonWorkerWriteNoDetect,
iButtonWorkerWriteCannotWrite,
} iButtonWorkerWriteResult;
typedef void (*iButtonWorkerReadCallback)(void* context);
typedef void (*iButtonWorkerWriteCallback)(void* context, iButtonWorkerWriteResult result);
typedef void (*iButtonWorkerEmulateCallback)(void* context, bool emulated);
typedef struct iButtonWorker iButtonWorker;
/**
* Allocate ibutton worker
* @return iButtonWorker*
*/
iButtonWorker* ibutton_worker_alloc();
/**
* Free ibutton worker
* @param worker
*/
void ibutton_worker_free(iButtonWorker* worker);
/**
* Start ibutton worker thread
* @param worker
*/
void ibutton_worker_start_thread(iButtonWorker* worker);
/**
* Stop ibutton worker thread
* @param worker
*/
void ibutton_worker_stop_thread(iButtonWorker* worker);
/**
* Set "read success" callback
* @param worker
* @param callback
* @param context
*/
void ibutton_worker_read_set_callback(
iButtonWorker* worker,
iButtonWorkerReadCallback callback,
void* context);
/**
* Start read mode
* @param worker
* @param key
*/
void ibutton_worker_read_start(iButtonWorker* worker, iButtonKey* key);
/**
* Set "write event" callback
* @param worker
* @param callback
* @param context
*/
void ibutton_worker_write_set_callback(
iButtonWorker* worker,
iButtonWorkerWriteCallback callback,
void* context);
/**
* Start write mode
* @param worker
* @param key
*/
void ibutton_worker_write_start(iButtonWorker* worker, iButtonKey* key);
/**
* Set "emulate success" callback
* @param worker
* @param callback
* @param context
*/
void ibutton_worker_emulate_set_callback(
iButtonWorker* worker,
iButtonWorkerEmulateCallback callback,
void* context);
/**
* Start emulate mode
* @param worker
* @param key
*/
void ibutton_worker_emulate_start(iButtonWorker* worker, iButtonKey* key);
/**
* Stop all modes
* @param worker
*/
void ibutton_worker_stop(iButtonWorker* worker);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,79 @@
/**
* @file ibutton_worker_i.h
*
* iButton worker, internal definitions
*/
#pragma once
#include "ibutton_worker.h"
#include "ibutton_writer.h"
#include "../one_wire_host.h"
#include "../one_wire_slave.h"
#include "../one_wire_device.h"
#include "../pulse_protocols/pulse_decoder.h"
#include "pulse_protocols/protocol_cyfral.h"
#include "pulse_protocols/protocol_metakom.h"
#include "encoder/encoder_cyfral.h"
#include "encoder/encoder_metakom.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
PulseProtocolCyfral,
PulseProtocolMetakom,
} PulseProtocols;
typedef struct {
const uint32_t quant;
const void (*start)(iButtonWorker* worker);
const void (*tick)(iButtonWorker* worker);
const void (*stop)(iButtonWorker* worker);
} iButtonWorkerModeType;
typedef enum {
iButtonWorkerIdle = 0,
iButtonWorkerRead = 1,
iButtonWorkerWrite = 2,
iButtonWorkerEmulate = 3,
} iButtonWorkerMode;
typedef enum {
iButtonEmulateModeCyfral,
iButtonEmulateModeMetakom,
} iButtonEmulateMode;
struct iButtonWorker {
iButtonKey* key_p;
uint8_t* key_data;
OneWireHost* host;
OneWireSlave* slave;
OneWireDevice* device;
iButtonWriter* writer;
iButtonWorkerMode mode_index;
osMessageQueueId_t messages;
FuriThread* thread;
PulseDecoder* pulse_decoder;
ProtocolCyfral* protocol_cyfral;
ProtocolMetakom* protocol_metakom;
uint32_t last_dwt_value;
iButtonWorkerReadCallback read_cb;
iButtonWorkerWriteCallback write_cb;
iButtonWorkerEmulateCallback emulate_cb;
void* cb_ctx;
EncoderCyfral* encoder_cyfral;
EncoderMetakom* encoder_metakom;
iButtonEmulateMode emulate_mode;
};
extern const iButtonWorkerModeType ibutton_worker_modes[];
void ibutton_worker_switch_mode(iButtonWorker* worker, iButtonWorkerMode mode);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,330 @@
#include <furi.h>
#include <furi_hal.h>
#include "ibutton_worker_i.h"
#include "ibutton_key_command.h"
void ibutton_worker_mode_idle_start(iButtonWorker* worker);
void ibutton_worker_mode_idle_tick(iButtonWorker* worker);
void ibutton_worker_mode_idle_stop(iButtonWorker* worker);
void ibutton_worker_mode_emulate_start(iButtonWorker* worker);
void ibutton_worker_mode_emulate_tick(iButtonWorker* worker);
void ibutton_worker_mode_emulate_stop(iButtonWorker* worker);
void ibutton_worker_mode_read_start(iButtonWorker* worker);
void ibutton_worker_mode_read_tick(iButtonWorker* worker);
void ibutton_worker_mode_read_stop(iButtonWorker* worker);
void ibutton_worker_mode_write_start(iButtonWorker* worker);
void ibutton_worker_mode_write_tick(iButtonWorker* worker);
void ibutton_worker_mode_write_stop(iButtonWorker* worker);
const iButtonWorkerModeType ibutton_worker_modes[] = {
{
.quant = osWaitForever,
.start = ibutton_worker_mode_idle_start,
.tick = ibutton_worker_mode_idle_tick,
.stop = ibutton_worker_mode_idle_stop,
},
{
.quant = 100,
.start = ibutton_worker_mode_read_start,
.tick = ibutton_worker_mode_read_tick,
.stop = ibutton_worker_mode_read_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_write_start,
.tick = ibutton_worker_mode_write_tick,
.stop = ibutton_worker_mode_write_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_emulate_start,
.tick = ibutton_worker_mode_emulate_tick,
.stop = ibutton_worker_mode_emulate_stop,
},
};
/*********************** IDLE ***********************/
void ibutton_worker_mode_idle_start(iButtonWorker* worker) {
}
void ibutton_worker_mode_idle_tick(iButtonWorker* worker) {
}
void ibutton_worker_mode_idle_stop(iButtonWorker* worker) {
}
/*********************** READ ***********************/
void ibutton_worker_comparator_callback(bool level, void* context) {
iButtonWorker* worker = context;
uint32_t current_dwt_value = DWT->CYCCNT;
pulse_decoder_process_pulse(
worker->pulse_decoder, level, current_dwt_value - worker->last_dwt_value);
worker->last_dwt_value = current_dwt_value;
}
bool ibutton_worker_read_comparator(iButtonWorker* worker) {
bool result = false;
pulse_decoder_reset(worker->pulse_decoder);
furi_hal_rfid_pins_reset();
// pulldown pull pin, we sense the signal through the analog part of the RFID schematic
furi_hal_rfid_pin_pull_pulldown();
furi_hal_rfid_comp_set_callback(ibutton_worker_comparator_callback, worker);
worker->last_dwt_value = DWT->CYCCNT;
furi_hal_rfid_comp_start();
// TODO: rework with thread events, "pulse_decoder_get_decoded_index_with_timeout"
delay(100);
int32_t decoded_index = pulse_decoder_get_decoded_index(worker->pulse_decoder);
if(decoded_index >= 0) {
pulse_decoder_get_data(
worker->pulse_decoder, decoded_index, worker->key_data, ibutton_key_get_max_size());
}
switch(decoded_index) {
case PulseProtocolCyfral:
furi_check(worker->key_p != NULL);
ibutton_key_set_type(worker->key_p, iButtonKeyCyfral);
ibutton_key_set_data(worker->key_p, worker->key_data, ibutton_key_get_max_size());
result = true;
break;
case PulseProtocolMetakom:
furi_check(worker->key_p != NULL);
ibutton_key_set_type(worker->key_p, iButtonKeyMetakom);
ibutton_key_set_data(worker->key_p, worker->key_data, ibutton_key_get_max_size());
result = true;
break;
break;
default:
break;
}
furi_hal_rfid_comp_stop();
furi_hal_rfid_comp_set_callback(NULL, NULL);
furi_hal_rfid_pins_reset();
return result;
}
bool ibutton_worker_read_dallas(iButtonWorker* worker) {
bool result = false;
onewire_host_start(worker->host);
delay(100);
FURI_CRITICAL_ENTER();
if(onewire_host_search(worker->host, worker->key_data, NORMAL_SEARCH)) {
onewire_host_reset_search(worker->host);
// key found, verify
if(onewire_host_reset(worker->host)) {
onewire_host_write(worker->host, DS1990_CMD_READ_ROM);
bool key_valid = true;
for(uint8_t i = 0; i < ibutton_key_get_max_size(); i++) {
if(onewire_host_read(worker->host) != worker->key_data[i]) {
key_valid = false;
break;
}
}
if(key_valid) {
result = true;
furi_check(worker->key_p != NULL);
ibutton_key_set_type(worker->key_p, iButtonKeyDS1990);
ibutton_key_set_data(worker->key_p, worker->key_data, ibutton_key_get_max_size());
}
}
} else {
onewire_host_reset_search(worker->host);
}
onewire_host_stop(worker->host);
FURI_CRITICAL_EXIT();
return result;
}
void ibutton_worker_mode_read_start(iButtonWorker* worker) {
furi_hal_power_enable_otg();
}
void ibutton_worker_mode_read_tick(iButtonWorker* worker) {
bool valid = false;
if(ibutton_worker_read_dallas(worker)) {
valid = true;
} else if(ibutton_worker_read_comparator(worker)) {
valid = true;
}
if(valid) {
if(worker->read_cb != NULL) {
worker->read_cb(worker->cb_ctx);
}
ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
}
}
void ibutton_worker_mode_read_stop(iButtonWorker* worker) {
furi_hal_power_disable_otg();
}
/*********************** EMULATE ***********************/
static void onewire_slave_callback(void* context) {
furi_assert(context);
iButtonWorker* worker = context;
if(worker->emulate_cb != NULL) {
worker->emulate_cb(worker->cb_ctx, true);
}
}
void ibutton_worker_emulate_dallas_start(iButtonWorker* worker) {
uint8_t* device_id = onewire_device_get_id_p(worker->device);
const uint8_t* key_id = ibutton_key_get_data_p(worker->key_p);
const uint8_t key_size = ibutton_key_get_max_size();
memcpy(device_id, key_id, key_size);
onewire_slave_attach(worker->slave, worker->device);
onewire_slave_start(worker->slave);
onewire_slave_set_result_callback(worker->slave, onewire_slave_callback, worker);
}
void ibutton_worker_emulate_dallas_stop(iButtonWorker* worker) {
onewire_slave_stop(worker->slave);
onewire_slave_detach(worker->slave);
}
void ibutton_worker_emulate_timer_cb(void* context) {
furi_assert(context);
iButtonWorker* worker = context;
bool polarity;
uint32_t length;
switch(worker->emulate_mode) {
case iButtonEmulateModeCyfral:
encoder_cyfral_get_pulse(worker->encoder_cyfral, &polarity, &length);
break;
case iButtonEmulateModeMetakom:
encoder_metakom_get_pulse(worker->encoder_metakom, &polarity, &length);
break;
}
furi_hal_ibutton_emulate_set_next(length);
if(polarity) {
furi_hal_ibutton_pin_high();
} else {
furi_hal_ibutton_pin_low();
}
}
void ibutton_worker_emulate_timer_start(iButtonWorker* worker) {
furi_assert(worker->key_p);
const uint8_t* key_id = ibutton_key_get_data_p(worker->key_p);
const uint8_t key_size = ibutton_key_get_max_size();
switch(ibutton_key_get_type(worker->key_p)) {
case iButtonKeyDS1990:
return;
break;
case iButtonKeyCyfral:
worker->emulate_mode = iButtonEmulateModeCyfral;
encoder_cyfral_reset(worker->encoder_cyfral);
encoder_cyfral_set_data(worker->encoder_cyfral, key_id, key_size);
break;
case iButtonKeyMetakom:
worker->emulate_mode = iButtonEmulateModeMetakom;
encoder_metakom_reset(worker->encoder_metakom);
encoder_metakom_set_data(worker->encoder_metakom, key_id, key_size);
break;
}
furi_hal_ibutton_start_drive();
furi_hal_ibutton_emulate_start(0, ibutton_worker_emulate_timer_cb, worker);
}
void ibutton_worker_emulate_timer_stop(iButtonWorker* worker) {
furi_hal_ibutton_emulate_stop();
}
void ibutton_worker_mode_emulate_start(iButtonWorker* worker) {
furi_assert(worker->key_p);
furi_hal_rfid_pins_reset();
furi_hal_rfid_pin_pull_pulldown();
switch(ibutton_key_get_type(worker->key_p)) {
case iButtonKeyDS1990:
ibutton_worker_emulate_dallas_start(worker);
break;
case iButtonKeyCyfral:
case iButtonKeyMetakom:
ibutton_worker_emulate_timer_start(worker);
break;
}
}
void ibutton_worker_mode_emulate_tick(iButtonWorker* worker) {
}
void ibutton_worker_mode_emulate_stop(iButtonWorker* worker) {
furi_assert(worker->key_p);
furi_hal_rfid_pins_reset();
switch(ibutton_key_get_type(worker->key_p)) {
case iButtonKeyDS1990:
ibutton_worker_emulate_dallas_stop(worker);
break;
case iButtonKeyCyfral:
case iButtonKeyMetakom:
ibutton_worker_emulate_timer_stop(worker);
break;
}
}
/*********************** WRITE ***********************/
void ibutton_worker_mode_write_start(iButtonWorker* worker) {
furi_hal_power_enable_otg();
onewire_host_start(worker->host);
}
void ibutton_worker_mode_write_tick(iButtonWorker* worker) {
furi_check(worker->key_p != NULL);
iButtonWriterResult writer_result = ibutton_writer_write(worker->writer, worker->key_p);
iButtonWorkerWriteResult result;
switch(writer_result) {
case iButtonWriterOK:
result = iButtonWorkerWriteOK;
break;
case iButtonWriterSameKey:
result = iButtonWorkerWriteSameKey;
break;
case iButtonWriterNoDetect:
result = iButtonWorkerWriteNoDetect;
break;
case iButtonWriterCannotWrite:
result = iButtonWorkerWriteCannotWrite;
break;
default:
result = iButtonWorkerWriteNoDetect;
break;
}
if(worker->write_cb != NULL) {
worker->write_cb(worker->cb_ctx, result);
}
}
void ibutton_worker_mode_write_stop(iButtonWorker* worker) {
furi_hal_power_disable_otg();
onewire_host_stop(worker->host);
}

View File

@@ -0,0 +1,298 @@
#include <furi.h>
#include <furi_hal.h>
#include "ibutton_writer.h"
#include "ibutton_key_command.h"
/*********************** PRIVATE ***********************/
struct iButtonWriter {
OneWireHost* host;
};
static void writer_write_one_bit(iButtonWriter* writer, bool value, uint32_t delay) {
onewire_host_write_bit(writer->host, value);
delay_us(delay);
}
static void writer_write_byte_ds1990(iButtonWriter* writer, uint8_t data) {
for(uint8_t n_bit = 0; n_bit < 8; n_bit++) {
onewire_host_write_bit(writer->host, data & 1);
delay_us(5000);
data = data >> 1;
}
}
static bool writer_compare_key_ds1990(iButtonWriter* writer, iButtonKey* key) {
bool result = false;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
bool presence = onewire_host_reset(writer->host);
if(presence) {
onewire_host_write(writer->host, DS1990_CMD_READ_ROM);
result = true;
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
if(ibutton_key_get_data_p(key)[i] != onewire_host_read(writer->host)) {
result = false;
break;
}
}
}
FURI_CRITICAL_EXIT();
}
return result;
}
static bool writer_write_TM2004(iButtonWriter* writer, iButtonKey* key) {
uint8_t answer;
bool result = true;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
// write rom, addr is 0x0000
onewire_host_reset(writer->host);
onewire_host_write(writer->host, TM2004_CMD_WRITE_ROM);
onewire_host_write(writer->host, 0x00);
onewire_host_write(writer->host, 0x00);
// write key
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
// write key byte
onewire_host_write(writer->host, ibutton_key_get_data_p(key)[i]);
answer = onewire_host_read(writer->host);
// TODO: check answer CRC
// pulse indicating that data is correct
delay_us(600);
writer_write_one_bit(writer, 1, 50000);
// read writed key byte
answer = onewire_host_read(writer->host);
// check that writed and readed are same
if(ibutton_key_get_data_p(key)[i] != answer) {
result = false;
break;
}
}
if(!writer_compare_key_ds1990(writer, key)) {
result = false;
}
onewire_host_reset(writer->host);
FURI_CRITICAL_EXIT();
} else {
result = false;
}
return result;
}
static bool writer_write_1990_1(iButtonWriter* writer, iButtonKey* key) {
bool result = false;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
// unlock
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_1_CMD_WRITE_RECORD_FLAG);
delay_us(10);
writer_write_one_bit(writer, 0, 5000);
// write key
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_1_CMD_WRITE_ROM);
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
// inverted key for RW1990.1
writer_write_byte_ds1990(writer, ~ibutton_key_get_data_p(key)[i]);
delay_us(30000);
}
// lock
onewire_host_write(writer->host, RW1990_1_CMD_WRITE_RECORD_FLAG);
writer_write_one_bit(writer, 1, 10000);
FURI_CRITICAL_EXIT();
if(writer_compare_key_ds1990(writer, key)) {
result = true;
}
}
return result;
}
static bool writer_write_1990_2(iButtonWriter* writer, iButtonKey* key) {
bool result = false;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
// unlock
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_2_CMD_WRITE_RECORD_FLAG);
delay_us(10);
writer_write_one_bit(writer, 1, 5000);
// write key
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_2_CMD_WRITE_ROM);
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
writer_write_byte_ds1990(writer, ibutton_key_get_data_p(key)[i]);
delay_us(30000);
}
// lock
onewire_host_write(writer->host, RW1990_2_CMD_WRITE_RECORD_FLAG);
writer_write_one_bit(writer, 0, 10000);
FURI_CRITICAL_EXIT();
if(writer_compare_key_ds1990(writer, key)) {
result = true;
}
}
return result;
}
/*
// TODO: adapt and test
static bool writer_write_TM01(
iButtonWriter* writer,
iButtonKey type,
const uint8_t* key,
uint8_t key_length) {
bool result = true;
{
// TODO test and encoding
FURI_CRITICAL_ENTER();
// unlock
onewire_host_reset(writer->host);
onewire_host_write(writer->host, TM01::CMD_WRITE_RECORD_FLAG);
onewire_write_one_bit(1, 10000);
// write key
onewire_host_reset(writer->host);
onewire_host_write(writer->host, TM01::CMD_WRITE_ROM);
// TODO: key types
//if(type == KEY_METAKOM || type == KEY_CYFRAL) {
//} else {
for(uint8_t i = 0; i < key->get_type_data_size(); i++) {
write_byte_ds1990(key->get_data()[i]);
delay_us(10000);
}
//}
// lock
onewire_host_write(writer->host, TM01::CMD_WRITE_RECORD_FLAG);
onewire_write_one_bit(0, 10000);
FURI_CRITICAL_EXIT();
}
if(!compare_key_ds1990(key)) {
result = false;
}
{
FURI_CRITICAL_ENTER();
if(key->get_key_type() == iButtonKeyType::KeyMetakom ||
key->get_key_type() == iButtonKeyType::KeyCyfral) {
onewire_host_reset(writer->host);
if(key->get_key_type() == iButtonKeyType::KeyCyfral)
onewire_host_write(writer->host, TM01::CMD_SWITCH_TO_CYFRAL);
else
onewire_host_write(writer->host, TM01::CMD_SWITCH_TO_METAKOM);
onewire_write_one_bit(1);
}
FURI_CRITICAL_EXIT();
}
return result;
}
*/
static iButtonWriterResult writer_write_DS1990(iButtonWriter* writer, iButtonKey* key) {
iButtonWriterResult result = iButtonWriterNoDetect;
bool same_key = writer_compare_key_ds1990(writer, key);
if(!same_key) {
// currently we can write:
// RW1990_1, TM08v2, TM08vi-2 by write_1990_1()
// RW1990_2 by write_1990_2()
// RW2004, RW2004, TM2004 with EEPROM by write_TM2004();
bool write_result = true;
do {
if(writer_write_1990_1(writer, key)) break;
if(writer_write_1990_2(writer, key)) break;
if(writer_write_TM2004(writer, key)) break;
write_result = false;
} while(false);
if(write_result) {
result = iButtonWriterOK;
} else {
result = iButtonWriterCannotWrite;
}
} else {
result = iButtonWriterSameKey;
}
return result;
}
/*********************** PUBLIC ***********************/
iButtonWriter* ibutton_writer_alloc(OneWireHost* host) {
iButtonWriter* writer = malloc(sizeof(iButtonWriter));
writer->host = host;
return writer;
}
void ibutton_writer_free(iButtonWriter* writer) {
free(writer);
}
iButtonWriterResult ibutton_writer_write(iButtonWriter* writer, iButtonKey* key) {
iButtonWriterResult result = iButtonWriterNoDetect;
osKernelLock();
bool blank_present = onewire_host_reset(writer->host);
osKernelUnlock();
if(blank_present) {
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
result = writer_write_DS1990(writer, key);
default:
break;
}
}
return result;
}
void ibutton_writer_start(iButtonWriter* writer) {
furi_hal_power_enable_otg();
onewire_host_start(writer->host);
}
void ibutton_writer_stop(iButtonWriter* writer) {
onewire_host_stop(writer->host);
furi_hal_power_disable_otg();
}

View File

@@ -0,0 +1,60 @@
/**
* @file ibutton_writer.h
*
* iButton blanks writer
*/
#pragma once
#include <furi_hal_gpio.h>
#include "ibutton_key.h"
#include "../one_wire_host.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
iButtonWriterOK,
iButtonWriterSameKey,
iButtonWriterNoDetect,
iButtonWriterCannotWrite,
} iButtonWriterResult;
typedef struct iButtonWriter iButtonWriter;
/**
* Allocate writer
* @param host
* @return iButtonWriter*
*/
iButtonWriter* ibutton_writer_alloc(OneWireHost* host);
/**
* Deallocate writer
* @param writer
*/
void ibutton_writer_free(iButtonWriter* writer);
/**
* Write key to blank
* @param writer
* @param key
* @return iButtonWriterResult
*/
iButtonWriterResult ibutton_writer_write(iButtonWriter* writer, iButtonKey* key);
/**
* Start writing. Must be called before write attempt
* @param writer
*/
void ibutton_writer_start(iButtonWriter* writer);
/**
* Stop writing
* @param writer
*/
void ibutton_writer_stop(iButtonWriter* writer);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,256 @@
#include "protocol_cyfral.h"
#include <stdlib.h>
#include <string.h>
#include <furi/check.h>
#include <furi_hal_delay.h>
#define CYFRAL_DATA_SIZE 2
#define CYFRAL_MAX_PERIOD_US 230
typedef enum {
CYFRAL_BIT_WAIT_FRONT_HIGH,
CYFRAL_BIT_WAIT_FRONT_LOW,
} CyfralBitState;
typedef enum {
CYFRAL_WAIT_START_NIBBLE,
CYFRAL_READ_NIBBLE,
CYFRAL_READ_STOP_NIBBLE,
} CyfralState;
struct ProtocolCyfral {
PulseProtocol* protocol;
CyfralState state;
CyfralBitState bit_state;
// ready flag, key is read and valid
// TODO: atomic access
bool ready;
// key data storage
uint16_t key_data;
// high + low period time
uint32_t period_time;
// temporary nibble storage
uint8_t nibble;
// data valid flag
// MUST be checked only in READ_STOP_NIBBLE state
bool data_valid;
// nibble index, we expect 8 nibbles
uint8_t index;
// bit index in nibble, 4 bit per nibble
uint8_t bit_index;
// max period, 230us x clock per us
uint32_t max_period;
};
static void cyfral_pulse(void* context, bool polarity, uint32_t length);
static void cyfral_reset(void* context);
static void cyfral_get_data(void* context, uint8_t* data, size_t length);
static bool cyfral_decoded(void* context);
ProtocolCyfral* protocol_cyfral_alloc() {
ProtocolCyfral* cyfral = malloc(sizeof(ProtocolCyfral));
cyfral_reset(cyfral);
cyfral->protocol = pulse_protocol_alloc();
pulse_protocol_set_context(cyfral->protocol, cyfral);
pulse_protocol_set_pulse_cb(cyfral->protocol, cyfral_pulse);
pulse_protocol_set_reset_cb(cyfral->protocol, cyfral_reset);
pulse_protocol_set_get_data_cb(cyfral->protocol, cyfral_get_data);
pulse_protocol_set_decoded_cb(cyfral->protocol, cyfral_decoded);
return cyfral;
}
void protocol_cyfral_free(ProtocolCyfral* cyfral) {
furi_assert(cyfral);
pulse_protocol_free(cyfral->protocol);
free(cyfral);
}
PulseProtocol* protocol_cyfral_get_protocol(ProtocolCyfral* cyfral) {
furi_assert(cyfral);
return cyfral->protocol;
}
static void cyfral_get_data(void* context, uint8_t* data, size_t length) {
furi_assert(context);
furi_check(length >= CYFRAL_DATA_SIZE);
ProtocolCyfral* cyfral = context;
memcpy(data, &cyfral->key_data, CYFRAL_DATA_SIZE);
}
static bool cyfral_decoded(void* context) {
furi_assert(context);
ProtocolCyfral* cyfral = context;
bool decoded = cyfral->ready;
return decoded;
}
static void cyfral_reset(void* context) {
furi_assert(context);
ProtocolCyfral* cyfral = context;
cyfral->state = CYFRAL_WAIT_START_NIBBLE;
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_LOW;
cyfral->period_time = 0;
cyfral->bit_index = 0;
cyfral->ready = false;
cyfral->index = 0;
cyfral->key_data = 0;
cyfral->nibble = 0;
cyfral->data_valid = true;
cyfral->max_period = CYFRAL_MAX_PERIOD_US * instructions_per_us;
}
static bool cyfral_process_bit(
ProtocolCyfral* cyfral,
bool polarity,
uint32_t length,
bool* bit_ready,
bool* bit_value) {
bool result = true;
*bit_ready = false;
// bit start from low
switch(cyfral->bit_state) {
case CYFRAL_BIT_WAIT_FRONT_LOW:
if(polarity == true) {
cyfral->period_time += length;
*bit_ready = true;
if(cyfral->period_time <= cyfral->max_period) {
if((cyfral->period_time / 2) > length) {
*bit_value = false;
} else {
*bit_value = true;
}
} else {
result = false;
}
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_HIGH;
} else {
result = false;
}
break;
case CYFRAL_BIT_WAIT_FRONT_HIGH:
if(polarity == false) {
cyfral->period_time = length;
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_LOW;
} else {
result = false;
}
break;
}
return result;
}
static void cyfral_pulse(void* context, bool polarity, uint32_t length) {
furi_assert(context);
ProtocolCyfral* cyfral = context;
bool bit_ready;
bool bit_value;
if(cyfral->ready) return;
switch(cyfral->state) {
case CYFRAL_WAIT_START_NIBBLE:
// wait for start word
if(cyfral_process_bit(cyfral, polarity, length, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = ((cyfral->nibble << 1) | bit_value) & 0x0F;
if(cyfral->nibble == 0b0001) {
cyfral->nibble = 0;
cyfral->state = CYFRAL_READ_NIBBLE;
}
}
} else {
cyfral_reset(cyfral);
}
break;
case CYFRAL_READ_NIBBLE:
// read nibbles
if(cyfral_process_bit(cyfral, polarity, length, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = (cyfral->nibble << 1) | bit_value;
cyfral->bit_index++;
//convert every nibble to 2-bit index
if(cyfral->bit_index == 4) {
switch(cyfral->nibble) {
case 0b1110:
cyfral->key_data = (cyfral->key_data << 2) | 0b11;
break;
case 0b1101:
cyfral->key_data = (cyfral->key_data << 2) | 0b10;
break;
case 0b1011:
cyfral->key_data = (cyfral->key_data << 2) | 0b01;
break;
case 0b0111:
cyfral->key_data = (cyfral->key_data << 2) | 0b00;
break;
default:
cyfral->data_valid = false;
break;
}
cyfral->nibble = 0;
cyfral->bit_index = 0;
cyfral->index++;
}
// succefully read 8 nibbles
if(cyfral->index == 8) {
cyfral->state = CYFRAL_READ_STOP_NIBBLE;
}
}
} else {
cyfral_reset(cyfral);
}
break;
case CYFRAL_READ_STOP_NIBBLE:
// read stop nibble
if(cyfral_process_bit(cyfral, polarity, length, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = ((cyfral->nibble << 1) | bit_value) & 0x0F;
cyfral->bit_index++;
switch(cyfral->bit_index) {
case 0:
case 1:
case 2:
case 3:
break;
case 4:
if(cyfral->nibble == 0b0001) {
// validate data
if(cyfral->data_valid) {
cyfral->ready = true;
} else {
cyfral_reset(cyfral);
}
} else {
cyfral_reset(cyfral);
}
break;
default:
cyfral_reset(cyfral);
break;
}
}
} else {
cyfral_reset(cyfral);
}
break;
}
}

View File

@@ -0,0 +1,38 @@
/**
* @file protocol_cyfral.h
*
* Cyfral pulse format decoder
*/
#pragma once
#include <stdint.h>
#include "../../pulse_protocols/pulse_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ProtocolCyfral ProtocolCyfral;
/**
* Allocate decoder
* @return ProtocolCyfral*
*/
ProtocolCyfral* protocol_cyfral_alloc();
/**
* Deallocate decoder
* @param cyfral
*/
void protocol_cyfral_free(ProtocolCyfral* cyfral);
/**
* Get protocol interface
* @param cyfral
* @return PulseProtocol*
*/
PulseProtocol* protocol_cyfral_get_protocol(ProtocolCyfral* cyfral);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,262 @@
#include "protocol_metakom.h"
#include <stdlib.h>
#include <string.h>
#include <furi/check.h>
#include <furi_hal_delay.h>
#define METAKOM_DATA_SIZE 4
#define METAKOM_PERIOD_SAMPLE_COUNT 10
typedef enum {
METAKOM_WAIT_PERIOD_SYNC,
METAKOM_WAIT_START_BIT,
METAKOM_WAIT_START_WORD,
METAKOM_READ_WORD,
METAKOM_READ_STOP_WORD,
} MetakomState;
typedef enum {
METAKOM_BIT_WAIT_FRONT_HIGH,
METAKOM_BIT_WAIT_FRONT_LOW,
} MetakomBitState;
struct ProtocolMetakom {
PulseProtocol* protocol;
// high + low period time
uint32_t period_time;
uint32_t low_time_storage;
uint8_t period_sample_index;
uint32_t period_sample_data[METAKOM_PERIOD_SAMPLE_COUNT];
// ready flag
// TODO: atomic access
bool ready;
uint8_t tmp_data;
uint8_t tmp_counter;
uint32_t key_data;
uint8_t key_data_index;
MetakomBitState bit_state;
MetakomState state;
};
static void metakom_pulse(void* context, bool polarity, uint32_t length);
static void metakom_reset(void* context);
static void metakom_get_data(void* context, uint8_t* data, size_t length);
static bool metakom_decoded(void* context);
ProtocolMetakom* protocol_metakom_alloc() {
ProtocolMetakom* metakom = malloc(sizeof(ProtocolMetakom));
metakom_reset(metakom);
metakom->protocol = pulse_protocol_alloc();
pulse_protocol_set_context(metakom->protocol, metakom);
pulse_protocol_set_pulse_cb(metakom->protocol, metakom_pulse);
pulse_protocol_set_reset_cb(metakom->protocol, metakom_reset);
pulse_protocol_set_get_data_cb(metakom->protocol, metakom_get_data);
pulse_protocol_set_decoded_cb(metakom->protocol, metakom_decoded);
return metakom;
}
void protocol_metakom_free(ProtocolMetakom* metakom) {
furi_assert(metakom);
pulse_protocol_free(metakom->protocol);
free(metakom);
}
PulseProtocol* protocol_metakom_get_protocol(ProtocolMetakom* metakom) {
furi_assert(metakom);
return metakom->protocol;
}
static void metakom_get_data(void* context, uint8_t* data, size_t length) {
furi_assert(context);
furi_check(length >= METAKOM_DATA_SIZE);
ProtocolMetakom* metakom = context;
memcpy(data, &metakom->key_data, METAKOM_DATA_SIZE);
}
static bool metakom_decoded(void* context) {
furi_assert(context);
ProtocolMetakom* metakom = context;
bool decoded = metakom->ready;
return decoded;
}
static void metakom_reset(void* context) {
furi_assert(context);
ProtocolMetakom* metakom = context;
metakom->ready = false;
metakom->period_sample_index = 0;
metakom->period_time = 0;
metakom->tmp_counter = 0;
metakom->tmp_data = 0;
for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
metakom->period_sample_data[i] = 0;
};
metakom->state = METAKOM_WAIT_PERIOD_SYNC;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
metakom->key_data = 0;
metakom->key_data_index = 0;
metakom->low_time_storage = 0;
}
static bool metakom_parity_check(uint8_t data) {
uint8_t ones_count = 0;
bool result;
for(uint8_t i = 0; i < 8; i++) {
if((data >> i) & 0b00000001) {
ones_count++;
}
}
result = (ones_count % 2 == 0);
return result;
}
static bool metakom_process_bit(
ProtocolMetakom* metakom,
bool polarity,
uint32_t time,
uint32_t* high_time,
uint32_t* low_time) {
bool result = false;
switch(metakom->bit_state) {
case METAKOM_BIT_WAIT_FRONT_LOW:
if(polarity == false) {
*low_time = metakom->low_time_storage;
*high_time = time;
result = true;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_HIGH;
}
break;
case METAKOM_BIT_WAIT_FRONT_HIGH:
if(polarity == true) {
metakom->low_time_storage = time;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
}
break;
}
return result;
}
static void metakom_pulse(void* context, bool polarity, uint32_t time) {
furi_assert(context);
ProtocolMetakom* metakom = context;
if(metakom->ready) return;
uint32_t high_time = 0;
uint32_t low_time = 0;
switch(metakom->state) {
case METAKOM_WAIT_PERIOD_SYNC:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
metakom->period_sample_data[metakom->period_sample_index] = high_time + low_time;
metakom->period_sample_index++;
if(metakom->period_sample_index == METAKOM_PERIOD_SAMPLE_COUNT) {
for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
metakom->period_time += metakom->period_sample_data[i];
};
metakom->period_time /= METAKOM_PERIOD_SAMPLE_COUNT;
metakom->state = METAKOM_WAIT_START_BIT;
}
}
break;
case METAKOM_WAIT_START_BIT:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
metakom->tmp_counter++;
if(high_time > metakom->period_time) {
metakom->tmp_counter = 0;
metakom->state = METAKOM_WAIT_START_WORD;
}
if(metakom->tmp_counter > 40) {
metakom_reset(metakom);
}
}
break;
case METAKOM_WAIT_START_WORD:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 3) {
if(metakom->tmp_data == 0b010) {
metakom->tmp_counter = 0;
metakom->tmp_data = 0;
metakom->state = METAKOM_READ_WORD;
} else {
metakom_reset(metakom);
}
}
}
break;
case METAKOM_READ_WORD:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 8) {
if(metakom_parity_check(metakom->tmp_data)) {
metakom->key_data = (metakom->key_data << 8) | metakom->tmp_data;
metakom->key_data_index++;
metakom->tmp_data = 0;
metakom->tmp_counter = 0;
if(metakom->key_data_index == 4) {
// check for stop bit
if(high_time > metakom->period_time) {
metakom->state = METAKOM_READ_STOP_WORD;
} else {
metakom_reset(metakom);
}
}
} else {
metakom_reset(metakom);
}
}
}
break;
case METAKOM_READ_STOP_WORD:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 3) {
if(metakom->tmp_data == 0b010) {
metakom->ready = true;
} else {
metakom_reset(metakom);
}
}
}
break;
}
}

View File

@@ -0,0 +1,38 @@
/**
* @file protocol_metakom.h
*
* Metakom pulse format decoder
*/
#pragma once
#include <stdint.h>
#include "../../pulse_protocols/pulse_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ProtocolMetakom ProtocolMetakom;
/**
* Allocate decoder
* @return ProtocolMetakom*
*/
ProtocolMetakom* protocol_metakom_alloc();
/**
* Free decoder
* @param metakom
*/
void protocol_metakom_free(ProtocolMetakom* metakom);
/**
* Get protocol interface
* @param metakom
* @return PulseProtocol*
*/
PulseProtocol* protocol_metakom_get_protocol(ProtocolMetakom* metakom);
#ifdef __cplusplus
}
#endif