unleashed-firmware/lib/one_wire/ibutton/ibutton_key.c
SG bdba15b366
[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
2022-03-29 16:01:56 +03:00

121 lines
No EOL
2.9 KiB
C

#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);
}