protocol_ds1971.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "protocol_ds1971.h"
  2. #include <core/core_defines.h>
  3. #include <toolbox/pretty_format.h>
  4. #include "dallas_common.h"
  5. #define DS1971_FAMILY_CODE 0x14U
  6. #define DS1971_FAMILY_NAME "DS1971"
  7. #define DS1971_EEPROM_DATA_SIZE 32U
  8. #define DS1971_SRAM_PAGE_SIZE 32U
  9. #define DS1971_COPY_SCRATCH_DELAY_US 250U
  10. #define DS1971_DATA_BYTE_COUNT 4U
  11. #define DS1971_EEPROM_DATA_KEY "Eeprom Data"
  12. #define DS1971_MEMORY_TYPE "EEPROM"
  13. #define DS1971_CMD_FINALIZATION 0xA5
  14. typedef struct {
  15. OneWireSlave* bus;
  16. DallasCommonCommandState command_state;
  17. } DS1971ProtocolState;
  18. typedef struct {
  19. DallasCommonRomData rom_data;
  20. uint8_t eeprom_data[DS1971_EEPROM_DATA_SIZE];
  21. DS1971ProtocolState state;
  22. } DS1971ProtocolData;
  23. static bool dallas_ds1971_read(OneWireHost*, void*);
  24. static bool dallas_ds1971_write_copy(OneWireHost*, iButtonProtocolData*);
  25. static void dallas_ds1971_emulate(OneWireSlave*, iButtonProtocolData*);
  26. static bool dallas_ds1971_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
  27. static bool dallas_ds1971_save(FlipperFormat*, const iButtonProtocolData*);
  28. static void dallas_ds1971_render_data(FuriString*, const iButtonProtocolData*);
  29. static void dallas_ds1971_render_brief_data(FuriString*, const iButtonProtocolData*);
  30. static void dallas_ds1971_render_error(FuriString*, const iButtonProtocolData*);
  31. static bool dallas_ds1971_is_data_valid(const iButtonProtocolData*);
  32. static void dallas_ds1971_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
  33. static void dallas_ds1971_apply_edits(iButtonProtocolData*);
  34. static bool
  35. dallas_ds1971_read_mem(OneWireHost* host, uint8_t address, uint8_t* data, size_t data_size);
  36. static bool ds1971_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size);
  37. const iButtonProtocolDallasBase ibutton_protocol_ds1971 = {
  38. .family_code = DS1971_FAMILY_CODE,
  39. .features = iButtonProtocolFeatureExtData | iButtonProtocolFeatureWriteCopy,
  40. .data_size = sizeof(DS1971ProtocolData),
  41. .manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
  42. .name = DS1971_FAMILY_NAME,
  43. .read = dallas_ds1971_read,
  44. .write_blank = NULL, // TODO: Implement writing to blank
  45. .write_copy = dallas_ds1971_write_copy,
  46. .emulate = dallas_ds1971_emulate,
  47. .save = dallas_ds1971_save,
  48. .load = dallas_ds1971_load,
  49. .render_data = dallas_ds1971_render_data,
  50. .render_brief_data = dallas_ds1971_render_brief_data,
  51. .render_error = dallas_ds1971_render_error,
  52. .is_valid = dallas_ds1971_is_data_valid,
  53. .get_editable_data = dallas_ds1971_get_editable_data,
  54. .apply_edits = dallas_ds1971_apply_edits,
  55. };
  56. bool dallas_ds1971_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
  57. DS1971ProtocolData* data = protocol_data;
  58. return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data) &&
  59. dallas_ds1971_read_mem(host, 0, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
  60. }
  61. bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_data) {
  62. DS1971ProtocolData* data = protocol_data;
  63. onewire_host_reset(host);
  64. onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
  65. // Starting writing from address 0x0000
  66. onewire_host_write(host, DALLAS_COMMON_CMD_WRITE_SCRATCH);
  67. onewire_host_write(host, 0x00);
  68. // Write data to scratchpad
  69. onewire_host_write_bytes(host, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
  70. // Read data from scratchpad and verify
  71. bool pad_valid = false;
  72. if(onewire_host_reset(host)) {
  73. pad_valid = true;
  74. onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
  75. onewire_host_write(host, DALLAS_COMMON_CMD_READ_SCRATCH);
  76. onewire_host_write(host, 0x00);
  77. for(size_t i = 0; i < DS1971_EEPROM_DATA_SIZE; ++i) {
  78. uint8_t scratch = onewire_host_read(host);
  79. if(data->eeprom_data[i] != scratch) {
  80. pad_valid = false;
  81. break;
  82. }
  83. }
  84. }
  85. // Copy scratchpad to memory and confirm
  86. if(pad_valid) {
  87. if(onewire_host_reset(host)) {
  88. onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
  89. onewire_host_write(host, DALLAS_COMMON_CMD_COPY_SCRATCH);
  90. onewire_host_write(host, DS1971_CMD_FINALIZATION);
  91. furi_delay_us(DS1971_COPY_SCRATCH_DELAY_US);
  92. }
  93. }
  94. return pad_valid;
  95. }
  96. static bool dallas_ds1971_reset_callback(bool is_short, void* context) {
  97. furi_assert(context);
  98. DS1971ProtocolData* data = context;
  99. if(!is_short) {
  100. data->state.command_state = DallasCommonCommandStateIdle;
  101. onewire_slave_set_overdrive(data->state.bus, is_short);
  102. }
  103. return !is_short;
  104. }
  105. static bool dallas_ds1971_command_callback(uint8_t command, void* context) {
  106. furi_assert(context);
  107. DS1971ProtocolData* data = context;
  108. OneWireSlave* bus = data->state.bus;
  109. switch(command) {
  110. case DALLAS_COMMON_CMD_SEARCH_ROM:
  111. if(data->state.command_state == DallasCommonCommandStateIdle) {
  112. data->state.command_state = DallasCommonCommandStateRomCmd;
  113. return dallas_common_emulate_search_rom(bus, &data->rom_data);
  114. } else if(data->state.command_state == DallasCommonCommandStateRomCmd) {
  115. data->state.command_state = DallasCommonCommandStateMemCmd;
  116. ds1971_emulate_read_mem(bus, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
  117. return false;
  118. } else {
  119. return false;
  120. }
  121. case DALLAS_COMMON_CMD_READ_ROM:
  122. if(data->state.command_state == DallasCommonCommandStateIdle) {
  123. data->state.command_state = DallasCommonCommandStateRomCmd;
  124. return dallas_common_emulate_read_rom(bus, &data->rom_data);
  125. } else {
  126. return false;
  127. }
  128. case DALLAS_COMMON_CMD_SKIP_ROM:
  129. if(data->state.command_state == DallasCommonCommandStateIdle) {
  130. data->state.command_state = DallasCommonCommandStateRomCmd;
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. default:
  136. return false;
  137. }
  138. }
  139. void dallas_ds1971_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
  140. DS1971ProtocolData* data = protocol_data;
  141. data->state.bus = bus;
  142. onewire_slave_set_reset_callback(bus, dallas_ds1971_reset_callback, protocol_data);
  143. onewire_slave_set_command_callback(bus, dallas_ds1971_command_callback, protocol_data);
  144. }
  145. bool dallas_ds1971_load(
  146. FlipperFormat* ff,
  147. uint32_t format_version,
  148. iButtonProtocolData* protocol_data) {
  149. DS1971ProtocolData* data = protocol_data;
  150. bool success = false;
  151. do {
  152. if(format_version < 2) break;
  153. if(!dallas_common_load_rom_data(ff, format_version, &data->rom_data)) break;
  154. if(!flipper_format_read_hex(
  155. ff, DS1971_EEPROM_DATA_KEY, data->eeprom_data, DS1971_EEPROM_DATA_SIZE))
  156. break;
  157. success = true;
  158. } while(false);
  159. return success;
  160. }
  161. bool dallas_ds1971_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
  162. const DS1971ProtocolData* data = protocol_data;
  163. bool success = false;
  164. do {
  165. if(!dallas_common_save_rom_data(ff, &data->rom_data)) break;
  166. if(!flipper_format_write_hex(
  167. ff, DS1971_EEPROM_DATA_KEY, data->eeprom_data, DS1971_EEPROM_DATA_SIZE))
  168. break;
  169. success = true;
  170. } while(false);
  171. return success;
  172. }
  173. void dallas_ds1971_render_data(FuriString* result, const iButtonProtocolData* protocol_data) {
  174. const DS1971ProtocolData* data = protocol_data;
  175. pretty_format_bytes_hex_canonical(
  176. result,
  177. DS1971_DATA_BYTE_COUNT,
  178. PRETTY_FORMAT_FONT_MONOSPACE,
  179. data->eeprom_data,
  180. DS1971_EEPROM_DATA_SIZE);
  181. }
  182. void dallas_ds1971_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
  183. const DS1971ProtocolData* data = protocol_data;
  184. dallas_common_render_brief_data(
  185. result, &data->rom_data, data->eeprom_data, DS1971_EEPROM_DATA_SIZE, DS1971_MEMORY_TYPE);
  186. }
  187. void dallas_ds1971_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
  188. const DS1971ProtocolData* data = protocol_data;
  189. if(!dallas_common_is_valid_crc(&data->rom_data)) {
  190. dallas_common_render_crc_error(result, &data->rom_data);
  191. }
  192. }
  193. bool dallas_ds1971_is_data_valid(const iButtonProtocolData* protocol_data) {
  194. const DS1971ProtocolData* data = protocol_data;
  195. return dallas_common_is_valid_crc(&data->rom_data);
  196. }
  197. void dallas_ds1971_get_editable_data(
  198. iButtonEditableData* editable_data,
  199. iButtonProtocolData* protocol_data) {
  200. DS1971ProtocolData* data = protocol_data;
  201. editable_data->ptr = data->rom_data.bytes;
  202. editable_data->size = sizeof(DallasCommonRomData);
  203. }
  204. void dallas_ds1971_apply_edits(iButtonProtocolData* protocol_data) {
  205. DS1971ProtocolData* data = protocol_data;
  206. dallas_common_apply_edits(&data->rom_data, DS1971_FAMILY_CODE);
  207. }
  208. bool dallas_ds1971_read_mem(OneWireHost* host, uint8_t address, uint8_t* data, size_t data_size) {
  209. onewire_host_write(host, DALLAS_COMMON_CMD_READ_MEM);
  210. onewire_host_write(host, address);
  211. onewire_host_read_bytes(host, data, (uint8_t)data_size);
  212. return true;
  213. }
  214. bool ds1971_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size) {
  215. bool success = false;
  216. do {
  217. uint8_t address;
  218. if(!onewire_slave_receive(bus, &address, sizeof(address))) break;
  219. if(address >= data_size) break;
  220. if(!onewire_slave_send(bus, data + address, data_size - address)) break;
  221. success = true;
  222. } while(false);
  223. return success;
  224. }