nrf24batch.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. //
  2. // Written by vad7, 10.01.2023. vad7@yahoo.com
  3. //
  4. #include "nrf24batch.h"
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <gui/gui.h>
  8. #include <dialogs/dialogs.h>
  9. #include <input/input.h>
  10. #include <stdlib.h>
  11. #include <dolphin/dolphin.h>
  12. #include <nrf24.h>
  13. #include <u8g2.h>
  14. #define TAG "nrf24batch"
  15. #define VERSION "1.0"
  16. #define MAX_CHANNEL 125
  17. #define SCAN_APP_PATH_FOLDER "/ext/nrf24batch"
  18. #define LOG_FILENAME "log"
  19. #define LOG_FILEEXT ".txt"
  20. #define NRF_READ_TIMEOUT 300UL // ms
  21. #define WORK_PERIOD 2 // ms, frequency of individual cmds
  22. const char SettingsFld_Info[] = "Info:";
  23. const char SettingsFld_Ch[] = "Ch:";
  24. const char SettingsFld_Rate[] = "Rate:";
  25. const char SettingsFld_DPL[] = "DPL:";
  26. const char SettingsFld_CRC[] = "CRC:";
  27. const char SettingsFld_Address[] = "Address:";
  28. const char SettingsFld_Resend[] = "Resend:";
  29. const char SettingsFld_Delay[] = "Delay_ms:";
  30. const char SettingsFld_WriteStart[] = "Write start:";
  31. const char SettingsFld_Payload[] = "Payload struct:";
  32. const char SettingsFld_ReadDefault[] = "R default:";
  33. const char SettingsFld_WriteDefault[] = "W default:";
  34. const char SettingsFld_Read[] = "R:";
  35. const char SettingsFld_Write[] = "W:";
  36. const char SettingsFld_ReadBatch[] = "RBatch:";
  37. const char SettingsFld_WriteBatch[] = "WBatch:";
  38. const char AskQuestion_Save[] = "SAVE BATCH?";
  39. #define Settings_i 'i'
  40. #define Settings_n 'n'
  41. #define VAR_EMPTY ((int32_t)0x80000000)
  42. nRF24Batch* APP;
  43. uint8_t what_doing = 0; // 0 - setup, 1 - cmd list, 2 - view send cmd
  44. enum {
  45. rwt_read_batch = 0,
  46. rwt_read_cmd,
  47. rwt_write_batch
  48. };
  49. uint8_t rw_type = rwt_read_batch; // What to do: rwt_*
  50. enum {
  51. sst_none = 0,
  52. sst_sending,
  53. sst_receiving,
  54. sst_ok,
  55. sst_error,
  56. sst_timeout
  57. };
  58. uint8_t send_status = sst_none;// sst_*
  59. bool cmd_array = false;
  60. uint8_t cmd_array_idx;
  61. uint8_t cmd_array_cnt = 0;
  62. uint8_t save_settings = 0;
  63. uint16_t view_cmd[3] = {0, 0, 0}; // ReadBatch, Read, WriteBatch
  64. uint8_t view_x = 0;
  65. char Info[32] = "";
  66. char screen_buf[64];
  67. char file_name[32];
  68. char ERR_STR[32];
  69. uint8_t ERR = 0;
  70. uint8_t NRF_rate; // 0 - 250Kbps, 1 - 1Mbps, 2 - 2Mbps
  71. uint8_t NRF_channel;// 0..125
  72. uint8_t NRF_DPL; // 1 - Dynamic Payload Length
  73. uint8_t NRF_CRC; // 1 - No, 1 - CRC 1byte, 2 - CRC 2byte
  74. uint8_t NRF_Payload;// Payload len in bytes or Minimum payload in sniff mode, 0..32
  75. uint8_t NRF_AA_OFF; // Disable Auto Acknowledgement
  76. bool NRF_ERROR = 0;
  77. bool NRF_INITED = false;
  78. uint8_t NRF_last_packet_send_st = 0;
  79. uint8_t NRF_resend = 1; // number of transaction attempts
  80. uint8_t NRF_repeat = 0; // count number of repeated requests (until < NRF_resend)
  81. uint32_t NRF_time;
  82. uint8_t addr[5]; // nRF24 address, MSB first
  83. uint8_t addr_len; // 2..5
  84. uint8_t payload[32];
  85. uint8_t payload_receive[32];
  86. uint8_t payload_struct[32]; // sizeof(1..4) in bytes of each field, example: 2,1,1
  87. uint8_t payload_fields = 0;
  88. uint8_t payload_size = 0; // bytes
  89. FuriString *ReadDefault = NULL;
  90. FuriString *WriteDefault = NULL;
  91. FuriString *WriteStart = NULL;
  92. uint32_t delay_between_pkt = 5;// ms
  93. FuriString *Constants = NULL; // text of STR=x
  94. FuriString **Read_cmd = NULL; // Names of read cmd
  95. uint16_t Read_cmd_Total = 0;
  96. FuriString **Log = NULL; // Strings: var=n
  97. uint16_t Log_Total = 0;
  98. uint8_t ask_question = 0; // 1 - Ask now
  99. uint8_t ask_question_answer = 0; // 0 - no, 1 - yes
  100. FuriString **ReadBatch_cmd = NULL; // Names of read batch cmd
  101. uint16_t ReadBatch_cmd_Total = 0;
  102. char *ReadBatch_cmd_curr = NULL; // =0xFFFFFFFF - finish
  103. uint16_t view_Batch = 0; // view pos
  104. FuriString **WriteBatch_cmd = NULL; // Names of write batch cmd
  105. uint16_t WriteBatch_cmd_Total = 0;
  106. char *WriteBatch_cmd_curr = NULL; // =0xFFFFFFFF - finish
  107. uint16_t view_WriteBatch = 0; // view pos
  108. Stream* file_stream = NULL;
  109. //#define MIN(a, b) ((a<b)?a:b)
  110. static uint8_t GetHexVal(char hex) {
  111. return (uint8_t)hex - ((uint8_t)hex < 58 ? 48 : ((uint8_t)hex < 97 ? 55 : 87));
  112. }
  113. // Return num bytes in array
  114. static uint8_t ConvertHexToArray(char * hex, uint8_t * array, uint8_t maxlen) {
  115. uint8_t len = 0;
  116. while(maxlen) {
  117. uint8_t ch = *hex++;
  118. if(ch < ' ') break;
  119. if(ch < '0') continue;
  120. *array++ = (GetHexVal(ch) << 4) + GetHexVal(*hex++);
  121. len++;
  122. maxlen--;
  123. }
  124. return len;
  125. }
  126. int32_t str_to_int(char *p)
  127. {
  128. if(*(p+1) == 'x') { // hex
  129. return strtol(p + 2, NULL, 16);
  130. } else return strtol(p, NULL, 10);
  131. }
  132. void str_rtrim(char *p)
  133. {
  134. char *delim_col = strchr(p, '\r');
  135. if(delim_col) *delim_col = '\0';
  136. else {
  137. delim_col = strchr(p, '\n');
  138. if(delim_col) *delim_col = '\0';
  139. }
  140. }
  141. static void add_to_str_hex_bytes(char *out, uint8_t *arr, int bytes)
  142. {
  143. if(bytes <= 0) return;
  144. out += strlen(out);
  145. do {
  146. snprintf(out, 3, "%02X", *arr++);
  147. out += 2;
  148. } while(--bytes);
  149. }
  150. void free_Log()
  151. {
  152. if(Log_Total) {
  153. for(uint16_t i = 0; i < Log_Total; i++) furi_string_free(Log[i]);
  154. Log_Total = 0;
  155. }
  156. if(Log) {
  157. free(Log);
  158. Log = NULL;
  159. }
  160. }
  161. void free_store(void)
  162. {
  163. if(Constants) {
  164. furi_string_free(Constants);
  165. Constants = NULL;
  166. }
  167. if(ReadDefault) {
  168. furi_string_free(ReadDefault);
  169. ReadDefault = NULL;
  170. }
  171. if(WriteDefault) {
  172. furi_string_free(WriteDefault);
  173. WriteDefault = NULL;
  174. }
  175. if(WriteStart) {
  176. furi_string_free(WriteStart);
  177. WriteDefault = NULL;
  178. }
  179. if(Read_cmd_Total) {
  180. for(uint16_t i = 0; i < Read_cmd_Total; i++) furi_string_free(Read_cmd[i]);
  181. Read_cmd_Total = 0;
  182. }
  183. if(Read_cmd) {
  184. free(Read_cmd);
  185. Read_cmd = NULL;
  186. }
  187. if(ReadBatch_cmd_Total) {
  188. for(uint16_t i = 0; i < ReadBatch_cmd_Total; i++) furi_string_free(ReadBatch_cmd[i]);
  189. ReadBatch_cmd_Total = 0;
  190. }
  191. if(ReadBatch_cmd) {
  192. free(ReadBatch_cmd);
  193. ReadBatch_cmd = NULL;
  194. }
  195. if(WriteBatch_cmd_Total) {
  196. for(uint16_t i = 0; i < WriteBatch_cmd_Total; i++) furi_string_free(WriteBatch_cmd[i]);
  197. WriteBatch_cmd_Total = 0;
  198. }
  199. if(WriteBatch_cmd) {
  200. free(WriteBatch_cmd);
  201. WriteBatch_cmd = NULL;
  202. }
  203. free_Log();
  204. }
  205. static bool select_settings_file() {
  206. DialogsApp* dialogs = furi_record_open("dialogs");
  207. bool result = false;
  208. FuriString* path;
  209. path = furi_string_alloc();
  210. furi_string_set(path, SCAN_APP_PATH_FOLDER);
  211. DialogsFileBrowserOptions browser_options;
  212. dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL);
  213. browser_options.hide_ext = false;
  214. bool ret = dialog_file_browser_show(dialogs, path, path, &browser_options);
  215. furi_record_close("dialogs");
  216. if(ret) {
  217. if(!file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  218. FURI_LOG_D(TAG, "Cannot open file \"%s\"", furi_string_get_cstr(path));
  219. file_stream_close(file_stream);
  220. } else {
  221. FURI_LOG_D(TAG, "Open file \"%s\"", furi_string_get_cstr(path));
  222. strncpy(file_name, furi_string_get_cstr(path) + sizeof(SCAN_APP_PATH_FOLDER), sizeof(file_name));
  223. result = true;
  224. }
  225. }
  226. furi_string_free(path);
  227. return result;
  228. }
  229. static void prepare_nrf24(void)
  230. {
  231. if(!NRF_INITED) {
  232. nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, NRF_channel);
  233. nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, (NRF_rate == 0 ? 0b00100000 : NRF_rate == 1 ? 0 : 0b00001000) | 0b111); // +TX high power
  234. nrf24_write_reg(nrf24_HANDLE, REG_CONFIG, 0x70 | ((NRF_CRC == 1 ? 0b1000 : NRF_CRC == 2 ? 0b1100 : 0))); // Mask all interrupts
  235. nrf24_write_reg(nrf24_HANDLE, REG_SETUP_RETR, ((NRF_rate == 0 ? 0b0100 : 0b0010)<<4) | 0b1111); // Automatic Retransmission, ARD, ARC
  236. nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x01); // Auto acknowledgement
  237. nrf24_write_reg(nrf24_HANDLE, REG_FEATURE, NRF24_EN_DYN_ACK | (NRF_DPL ? 4 : 0)); // Enables the W_TX_PAYLOAD_NOACK command, Disable Payload with ACK, set Dynamic Payload
  238. nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? 0x3F : 0); // Enable dynamic payload reg
  239. nrf24_write_reg(nrf24_HANDLE, RX_PW_P0, payload_size);
  240. nrf24_set_maclen(nrf24_HANDLE, addr_len);
  241. nrf24_set_mac(REG_RX_ADDR_P0, addr, addr_len);
  242. uint8_t tmp[5] = { 0 };
  243. nrf24_read_reg(nrf24_HANDLE, REG_RX_ADDR_P0, tmp, addr_len);
  244. for(uint8_t i = 0; i < addr_len / 2; i++) {
  245. uint8_t tb = tmp[i];
  246. tmp[i] = tmp[addr_len - i - 1];
  247. tmp[addr_len - i - 1] = tb;
  248. }
  249. NRF_ERROR = memcmp(addr, tmp, addr_len) != 0;
  250. nrf24_set_mac(REG_TX_ADDR, addr, addr_len);
  251. nrf24_write_reg(nrf24_HANDLE, REG_EN_RXADDR, 1);
  252. //nrf24_set_idle(nrf24_HANDLE);
  253. NRF_INITED = true;
  254. }
  255. nrf24_flush_tx(nrf24_HANDLE);
  256. nrf24_flush_rx(nrf24_HANDLE);
  257. nrf24_write_reg(nrf24_HANDLE, REG_STATUS, MAX_RT | RX_DR | TX_DS);
  258. furi_hal_gpio_write(nrf24_CE_PIN, false);
  259. }
  260. // true - ok
  261. uint8_t nrf24_send_packet()
  262. {
  263. if(furi_log_get_level() == FuriLogLevelDebug) {
  264. char buf[65]; buf[0] = 0; add_to_str_hex_bytes(buf, payload, payload_size); FURI_LOG_D(TAG, "SEND: %s", buf);
  265. }
  266. //nrf24_flush_tx(nrf24_HANDLE);
  267. //nrf24_write_reg(nrf24_HANDLE, REG_STATUS, RX_DR | TX_DS | MAX_RT);
  268. NRF_last_packet_send_st = nrf24_txpacket(nrf24_HANDLE, payload, payload_size, true); // ACK
  269. if(NRF_last_packet_send_st) {
  270. if((rw_type == rwt_read_cmd || rw_type == rwt_read_batch) && send_status == sst_sending) { // Read
  271. nrf24_set_rx_mode(nrf24_HANDLE);
  272. send_status = sst_receiving; // receiving
  273. }
  274. } else notification_message(APP->notification, &sequence_blink_red_100);
  275. NRF_time = furi_get_tick();
  276. FURI_LOG_D(TAG, "Send packet: %d%s", NRF_last_packet_send_st, send_status == sst_receiving ? ", Receiving" : "");
  277. return NRF_last_packet_send_st;
  278. }
  279. uint8_t nrf24_resend_packet()
  280. {
  281. if(Log_Total) {
  282. FuriString *str = Log[Log_Total - 1];
  283. char *p = strstr(furi_string_get_cstr(str), ": ");
  284. if(p) furi_string_left(str, p - furi_string_get_cstr(str) + 2);
  285. }
  286. return nrf24_send_packet();
  287. }
  288. // true - new packet
  289. bool nrf24_read_newpacket() {
  290. bool found = false;
  291. uint8_t packetsize;
  292. uint8_t st = nrf24_rxpacket(nrf24_HANDLE, payload_receive, &packetsize, NRF_DPL ? 0 : payload_size);
  293. if(st & RX_DR) {
  294. NRF_time = furi_get_tick();
  295. if(furi_log_get_level() == FuriLogLevelDebug) {
  296. char buf[65]; buf[0] = 0; add_to_str_hex_bytes(buf, payload_receive, packetsize); FURI_LOG_D(TAG, "READ(%X): %s", st, buf);
  297. }
  298. if(Log_Total) {
  299. FuriString *str = Log[Log_Total - 1];
  300. uint8_t size = 1;
  301. char *p = strchr((char*)furi_string_get_cstr(str), '*');
  302. if(p) {
  303. p++;
  304. if(*p == '=') size = 0; // string
  305. else {
  306. size = *p - '0';
  307. if(size > 4) size = 0;
  308. }
  309. }
  310. int32_t var;
  311. if(size <= 1) var = *payload_receive;
  312. else if(size == 2) var = *(int16_t*)payload_receive;
  313. else if(size == 3) var = (*(uint32_t*)payload_receive) & 0xFFFFFF;
  314. else var = *(int32_t*)payload_receive;
  315. //FURI_LOG_D(TAG, "VAR(%d): %ld", size, var);
  316. if(size == 0) furi_string_cat_printf(str, "%c", (char)var);
  317. else {
  318. if(var >= 0 && var <= 9) furi_string_cat_printf(str, "%ld", var);
  319. else {
  320. char hex[9];
  321. snprintf(hex, sizeof(hex), "%lX", var);
  322. furi_string_cat_printf(str, "%ld (%s)", var, hex + (var < 0 ? 8 - size * 2 : 0));
  323. }
  324. }
  325. if(cmd_array) {
  326. if(--cmd_array_cnt) {
  327. furi_string_cat_str(str, ",");
  328. payload[cmd_array_idx]++;
  329. NRF_repeat = 0;
  330. send_status = sst_sending;
  331. nrf24_send_packet();
  332. } else send_status = sst_ok;
  333. } else {
  334. if(size == 0) { // string, until '\0'
  335. if(var == 0) send_status = sst_ok;
  336. } else send_status = sst_ok;
  337. }
  338. }
  339. //notification_message(APP->notification, &sequence_blink_white_100);
  340. found = true;
  341. }
  342. return found;
  343. }
  344. // Search in constatnt pull (Const1=n; Const2=n;...)
  345. // -32768 - not found
  346. int32_t subs_constant(char *p, uint8_t len)
  347. {
  348. char *c = (char*)furi_string_get_cstr(Constants);
  349. while((c = strchr(c, *p))) {
  350. if(strncmp(c, p, len) != 0) {
  351. c++;
  352. continue;
  353. }
  354. if(c == (char*)furi_string_get_cstr(Constants) || *(c-1) == ';' || *(c-1) <= ' ') {
  355. c += len;
  356. if(*c == '=') {
  357. c++;
  358. return str_to_int(c);
  359. }
  360. } else c += len;
  361. }
  362. return VAR_EMPTY;
  363. }
  364. // fill payload with default = p
  365. // if var_n = VAR_EMPTY - skip filling var_*
  366. bool fill_payload(char *p, uint8_t *idx_i, int32_t var_n)
  367. {
  368. if(idx_i) *idx_i = 255;
  369. uint8_t fld = 0; // field #
  370. uint8_t idx = 0; // byte index
  371. do {
  372. int32_t b = 0;
  373. char *end = strchr(p, ',');
  374. if(*p >= '0' && *p <= '9') { // Number found
  375. b = str_to_int(p);
  376. } else if(*p == 'i' && *(p + 1) == ':') { // 'i:' array index
  377. b = str_to_int(p + 2);
  378. if(idx_i) *idx_i = idx;
  379. } else if(*p == 'n' && *(p + 1) < '0') { // var_n
  380. if(var_n != VAR_EMPTY) b = var_n;
  381. } else if(*p >= 'A') { // constant found
  382. b = subs_constant(p, end ? (uint8_t)(end - p) : strlen(p));
  383. if(b == VAR_EMPTY) {
  384. ERR = 1;
  385. strcpy(ERR_STR, "No ");
  386. strcat(ERR_STR, p);
  387. FURI_LOG_D(TAG, "Constant not found: %s", p);
  388. return false;
  389. }
  390. } else if(end == p) {
  391. idx += payload_struct[fld];
  392. } else {
  393. ERR = 2;
  394. strcpy(ERR_STR, "char: ");
  395. uint8_t l = strlen(ERR_STR);
  396. ERR_STR[l] = *p;
  397. ERR_STR[l+1] = '\0';
  398. FURI_LOG_D(TAG, "Wrong format char(%c)", *p);
  399. return false;
  400. }
  401. if(end != p) {
  402. payload[idx++] = b;
  403. if(payload_struct[fld] > 1) payload[idx++] = b >> 8;
  404. if(payload_struct[fld] > 2) payload[idx++] = b >> 16;
  405. if(payload_struct[fld] > 3) payload[idx++] = b >> 24;
  406. }
  407. if(++fld == payload_fields || idx >= sizeof(payload) || end == NULL) break;
  408. p = end + 1;
  409. } while(1);
  410. return true;
  411. }
  412. // Cmd: "name=payload"
  413. bool Run_Read_cmd(FuriString *cmd)
  414. {
  415. char *p = (char*)furi_string_get_cstr(cmd);
  416. p = strchr(p, '=');
  417. if(p == NULL) return false;
  418. if(Log == NULL) Log = malloc(sizeof(Log));
  419. else Log = realloc(Log, sizeof(Log) * (Log_Total + 1));
  420. if(Log == NULL) {
  421. ERR = 3;
  422. strcpy(ERR_STR, "Memory low");
  423. FURI_LOG_D(TAG, ERR_STR);
  424. return false;
  425. }
  426. FuriString *fs = furi_string_alloc();
  427. furi_string_set_strn(fs, (char*)furi_string_get_cstr(cmd), p - (char*)furi_string_get_cstr(cmd));
  428. furi_string_cat_str(fs, ": ");
  429. Log[Log_Total++] = fs;
  430. p++;
  431. memset(payload, 0, sizeof(payload));
  432. if(ReadDefault && !fill_payload((char*)furi_string_get_cstr(ReadDefault), NULL, VAR_EMPTY)) return false;
  433. if(!fill_payload(p, &cmd_array_idx, VAR_EMPTY)) return false;
  434. memset(payload_receive, 0, sizeof(payload_receive));
  435. cmd_array = false;
  436. if(*(p - 2) == ']' && cmd_array_idx != 255) { // array
  437. p = strchr(furi_string_get_cstr(cmd), '[');
  438. if(p) {
  439. cmd_array_cnt = str_to_int(p + 1);
  440. if(cmd_array_cnt > 1) cmd_array = true; // array
  441. }
  442. }
  443. prepare_nrf24();
  444. if(NRF_ERROR) return false;
  445. what_doing = 2;
  446. NRF_repeat = 0;
  447. send_status = sst_sending; // Read - sending
  448. nrf24_send_packet();
  449. return true;
  450. }
  451. // run commands one by one, true - command running
  452. bool Run_ReadBatch_cmd(FuriString *cmd)
  453. {
  454. char *p;
  455. if(cmd) {
  456. p = strchr((char*)furi_string_get_cstr(cmd), ':');
  457. if(p == NULL) return false;
  458. p += 2;
  459. ReadBatch_cmd_curr = NULL;
  460. free_Log();
  461. } else {
  462. if(ReadBatch_cmd_curr) p = ReadBatch_cmd_curr; else return false;
  463. }
  464. char *end = strchr(p, ';');
  465. uint8_t len;
  466. if(end) len = end - p;
  467. else {
  468. str_rtrim(p);
  469. len = strlen(p);
  470. }
  471. for(uint16_t i = 0; i < Read_cmd_Total; i++) {
  472. FuriString *fs = Read_cmd[i];
  473. if(strncmp((char*)furi_string_get_cstr(fs), p, len) == 0) {
  474. char c = *((char*)furi_string_get_cstr(fs) + len);
  475. if(c != '=' && c != '*' && c != '[') continue;
  476. if(end) ReadBatch_cmd_curr = end + 1; else ReadBatch_cmd_curr = (char*)0xFFFFFFFF;
  477. Run_Read_cmd(fs);
  478. return true;
  479. }
  480. }
  481. ERR = 4;
  482. strcpy(ERR_STR, "Not found");
  483. FURI_LOG_D(TAG, "CMD %s: %s", ERR_STR, p == NULL ? "" : p);
  484. return false;
  485. }
  486. bool Run_WriteBatch_cmd(FuriString *cmd)
  487. {
  488. char *p;
  489. send_status = sst_none;
  490. prepare_nrf24();
  491. if(NRF_ERROR) return false;
  492. if(cmd) {
  493. p = strchr((char*)furi_string_get_cstr(cmd), ':');
  494. if(p == NULL) return false;
  495. p += 2;
  496. WriteBatch_cmd_curr = NULL;
  497. free_Log();
  498. if(WriteStart) {
  499. if(!fill_payload((char*)furi_string_get_cstr(WriteStart), NULL, VAR_EMPTY)) return false;
  500. send_status = sst_sending;
  501. uint8_t i = 0;
  502. do {
  503. if(nrf24_send_packet()) break;
  504. furi_delay_ms(delay_between_pkt);
  505. } while(i++ < NRF_resend);
  506. if(i >= NRF_resend && i) { // not ok
  507. send_status = sst_error;
  508. return false;
  509. }
  510. }
  511. } else {
  512. if(WriteBatch_cmd_curr) p = WriteBatch_cmd_curr; else return false;
  513. }
  514. char *end = strchr(p, ';');
  515. uint8_t len;
  516. if(end) {
  517. len = end - p;
  518. WriteBatch_cmd_curr = end + 1;
  519. } else {
  520. str_rtrim(p);
  521. len = strlen(p);
  522. WriteBatch_cmd_curr = (char*)0xFFFFFFFF;
  523. }
  524. FuriString *fs = furi_string_alloc();
  525. if(Log == NULL) Log = malloc(sizeof(Log));
  526. else Log = realloc(Log, sizeof(Log) * (Log_Total + 1));
  527. if(Log == NULL) {
  528. ERR = 3;
  529. strcpy(ERR_STR, "Memory low");
  530. FURI_LOG_D(TAG, ERR_STR);
  531. return false;
  532. }
  533. furi_string_set_strn(fs, p, len);
  534. Log[Log_Total++] = fs;
  535. char *arr = NULL;
  536. cmd_array = false;
  537. int32_t new = 0;
  538. for(uint8_t i = 0; i < len; i++) {
  539. if(p[i] == '=') {
  540. len = i;
  541. char *p2 = p + i + 1;
  542. if(*p2 == '{') {
  543. arr = ++p2; // array
  544. cmd_array = true;
  545. }
  546. new = str_to_int(p2);
  547. break;
  548. }
  549. }
  550. FURI_LOG_D(TAG, "WriteBatch: =%d, (%d)%s", (int)new, len, p);
  551. FuriString* str = furi_string_alloc();
  552. stream_rewind(file_stream);
  553. while(stream_read_line(file_stream, str)) {
  554. char *w = (char*)furi_string_get_cstr(str);
  555. if(strncmp(w, SettingsFld_Write, sizeof(SettingsFld_Write)-1) != 0) continue;
  556. w += sizeof(SettingsFld_Write);
  557. char* delim_col = strchr(w, '=');
  558. if(delim_col == NULL || len != delim_col - w) continue;
  559. if(strncmp(p, w, len) != 0) continue;
  560. delim_col++;
  561. str_rtrim(delim_col);
  562. cmd_array_cnt = 255;
  563. do {
  564. memset(payload, 0, sizeof(payload));
  565. if(WriteDefault && !fill_payload((char*)furi_string_get_cstr(WriteDefault), NULL, new)) return false;
  566. if(!fill_payload(delim_col, &cmd_array_idx, VAR_EMPTY)) return false;
  567. if(cmd_array && cmd_array_idx != 255) {
  568. if(cmd_array_cnt != 255) payload[cmd_array_idx] = cmd_array_cnt;
  569. } else cmd_array = false;
  570. send_status = sst_sending;
  571. NRF_repeat = 0;
  572. uint8_t i = 0;
  573. do {
  574. if(nrf24_send_packet()) break;
  575. furi_delay_ms(delay_between_pkt);
  576. } while(i++ < NRF_resend);
  577. if(i < NRF_resend || i == 0) { // ok
  578. if(cmd_array) { // array
  579. for(; arr != NULL;) {
  580. if(*arr == ',') break;
  581. if(*arr == '}' || *arr < ' ') arr = NULL; else arr++;
  582. }
  583. if(arr == NULL) {
  584. send_status = sst_ok;
  585. break;
  586. }
  587. arr++;
  588. new = str_to_int(arr);
  589. cmd_array_cnt = payload[cmd_array_idx] + 1;
  590. continue;
  591. } else send_status = sst_ok;
  592. }
  593. break;
  594. } while(1);
  595. if(send_status != sst_ok) {
  596. send_status = sst_error;
  597. furi_string_cat_str(fs, "!");
  598. }
  599. return send_status == sst_ok;
  600. }
  601. ERR = 7;
  602. strcpy(ERR_STR, "NOT FOUND!");
  603. send_status = sst_error;
  604. return false;
  605. }
  606. // Return 0 - success, otherwise an error
  607. static uint8_t load_settings_file() {
  608. uint8_t err = 0;
  609. FURI_LOG_D(TAG, "Loading settings file");
  610. FuriString* str = furi_string_alloc();
  611. free_store();
  612. Info[0] = '\0';
  613. NRF_INITED = false;
  614. while(stream_read_line(file_stream, str)) {
  615. char *p = (char*)furi_string_get_cstr(str);
  616. if(*p <= ' ') continue;
  617. //char* delim_eq = strchr(p, '=');
  618. char* delim_col = strchr(p, ':');
  619. if(delim_col == NULL) { // Constant found - no ':'
  620. if(Constants == NULL) {
  621. Constants = furi_string_alloc_set(str);
  622. } else furi_string_cat(Constants, str);
  623. } else {
  624. str_rtrim(p);
  625. if(strncmp(p, SettingsFld_Rate, sizeof(SettingsFld_Rate)-1) == 0) {
  626. NRF_rate = atoi(p + sizeof(SettingsFld_Rate));
  627. } else if(strncmp(p, SettingsFld_Info, sizeof(SettingsFld_Info)-1) == 0) {
  628. strncpy(Info, p + sizeof(SettingsFld_Info), sizeof(Info)-1);
  629. } else if(strncmp(p, SettingsFld_Ch, sizeof(SettingsFld_Ch)-1) == 0) {
  630. NRF_channel = atoi(p + sizeof(SettingsFld_Ch));
  631. } else if(strncmp(p, SettingsFld_Address, sizeof(SettingsFld_Address)-1) == 0) {
  632. p += sizeof(SettingsFld_Address);
  633. addr_len = ConvertHexToArray(p, addr, 5);
  634. } else if(strncmp(p, SettingsFld_CRC, sizeof(SettingsFld_CRC)-1) == 0) {
  635. NRF_CRC = atoi(p + sizeof(SettingsFld_CRC));
  636. } else if(strncmp(p, SettingsFld_DPL, sizeof(SettingsFld_DPL)-1) == 0) {
  637. NRF_DPL = atoi(p + sizeof(SettingsFld_DPL));
  638. } else if(strncmp(p, SettingsFld_Resend, sizeof(SettingsFld_Resend)-1) == 0) {
  639. NRF_resend = atoi(p + sizeof(SettingsFld_Resend));
  640. } else if(strncmp(p, SettingsFld_Delay, sizeof(SettingsFld_Delay)-1) == 0) {
  641. delay_between_pkt = atoi(p + sizeof(SettingsFld_Delay));
  642. } else if(strncmp(p, SettingsFld_Payload, sizeof(SettingsFld_Payload)-1) == 0) {
  643. p += sizeof(SettingsFld_Payload);
  644. payload_fields = 0;
  645. payload_size = 0;
  646. do {
  647. uint8_t b = atoi(p);
  648. if(b < 1 || b > 4) {
  649. FURI_LOG_D(TAG, "Wrong payload format (%d)", b);
  650. err = 3;
  651. break;
  652. }
  653. payload_struct[payload_fields++] = b;
  654. payload_size += b;
  655. if(payload_fields == sizeof(payload_struct) - 1) break;
  656. if((p = strchr(p, ',')) == NULL) break;
  657. p++;
  658. } while(1);
  659. FURI_LOG_D(TAG, "Payload fields %d: %d,%d,%d", payload_fields, payload_struct[0], payload_struct[1], payload_struct[2]);
  660. } else if(strncmp(p, SettingsFld_ReadDefault, sizeof(SettingsFld_ReadDefault)-1) == 0) {
  661. ReadDefault = furi_string_alloc_set_str(p + sizeof(SettingsFld_ReadDefault));
  662. } else if(strncmp(p, SettingsFld_WriteStart, sizeof(SettingsFld_WriteStart)-1) == 0) {
  663. WriteStart = furi_string_alloc_set_str(p + sizeof(SettingsFld_WriteStart));
  664. } else if(strncmp(p, SettingsFld_WriteDefault, sizeof(SettingsFld_WriteDefault)-1) == 0) {
  665. WriteDefault = furi_string_alloc_set_str(p + sizeof(SettingsFld_WriteDefault));
  666. } else if(strncmp(p, SettingsFld_Read, sizeof(SettingsFld_Read)-1) == 0) {
  667. p += sizeof(SettingsFld_Read);
  668. if(Read_cmd == NULL) Read_cmd = malloc(sizeof(Read_cmd));
  669. else {
  670. Read_cmd = realloc(Read_cmd, sizeof(Read_cmd) * (Read_cmd_Total + 1));
  671. }
  672. if(Read_cmd == NULL) {
  673. FURI_LOG_D(TAG, "Memory low, err 4");
  674. err = 4;
  675. break;
  676. }
  677. Read_cmd[Read_cmd_Total++] = furi_string_alloc_set_str(p);
  678. } else if(strncmp(p, SettingsFld_ReadBatch, sizeof(SettingsFld_ReadBatch)-1) == 0) {
  679. p += sizeof(SettingsFld_ReadBatch);
  680. if(ReadBatch_cmd == NULL) ReadBatch_cmd = malloc(sizeof(ReadBatch_cmd));
  681. else {
  682. ReadBatch_cmd = realloc(ReadBatch_cmd, sizeof(ReadBatch_cmd) * (ReadBatch_cmd_Total + 1));
  683. }
  684. if(ReadBatch_cmd == NULL) {
  685. FURI_LOG_D(TAG, "Memory low, err 5");
  686. err = 5;
  687. break;
  688. }
  689. ReadBatch_cmd[ReadBatch_cmd_Total++] = furi_string_alloc_set_str(p);
  690. } else if(strncmp(p, SettingsFld_WriteBatch, sizeof(SettingsFld_WriteBatch)-1) == 0) {
  691. p += sizeof(SettingsFld_WriteBatch);
  692. if(WriteBatch_cmd == NULL) WriteBatch_cmd = malloc(sizeof(WriteBatch_cmd));
  693. else {
  694. WriteBatch_cmd = realloc(WriteBatch_cmd, sizeof(WriteBatch_cmd) * (WriteBatch_cmd_Total + 1));
  695. }
  696. if(WriteBatch_cmd == NULL) {
  697. FURI_LOG_D(TAG, "Memory low, err 6");
  698. err = 6;
  699. break;
  700. }
  701. WriteBatch_cmd[WriteBatch_cmd_Total++] = furi_string_alloc_set_str(p);
  702. }
  703. }
  704. }
  705. furi_string_free(str);
  706. return err;
  707. }
  708. static void save_batch(void)
  709. {
  710. // to do...
  711. }
  712. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  713. furi_assert(event_queue);
  714. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  715. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  716. }
  717. static void render_callback(Canvas* const canvas, void* ctx) {
  718. const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
  719. if(plugin_state == NULL) return;
  720. //canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen
  721. if(what_doing == 0) {
  722. canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
  723. snprintf(screen_buf, sizeof(screen_buf), "Open: %s", file_name);
  724. canvas_draw_str(canvas, 10, 10, screen_buf);
  725. if(addr_len) {
  726. canvas_draw_str(canvas, 10, 22, Info);
  727. strcpy(screen_buf, "Address: ");
  728. add_to_str_hex_bytes(screen_buf, addr, addr_len);
  729. canvas_draw_str(canvas, 10, 32, screen_buf);
  730. snprintf(screen_buf, sizeof(screen_buf), "Ch: %d, Rate: %d", NRF_channel, NRF_rate);
  731. canvas_draw_str(canvas, 10, 42, screen_buf);
  732. snprintf(screen_buf, sizeof(screen_buf), "RB: %d, R: %d, WB: %d", ReadBatch_cmd_Total, Read_cmd_Total, WriteBatch_cmd_Total);
  733. canvas_draw_str(canvas, 10, 52, screen_buf);
  734. //canvas_draw_str(canvas, 10, 60, screen_buf);
  735. } else {
  736. snprintf(screen_buf, sizeof(screen_buf), "ver. %s (vad7)", VERSION);
  737. canvas_draw_str(canvas, 10, 60, screen_buf);
  738. }
  739. canvas_draw_str(canvas, 0, 10, ">");
  740. } else if(what_doing == 1){
  741. canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
  742. char delim;
  743. uint16_t max_i;
  744. FuriString ***fsa;
  745. if(rw_type == rwt_read_batch) {
  746. canvas_draw_str(canvas, 0, 7, "Read Batch:");
  747. max_i = ReadBatch_cmd_Total;
  748. fsa = &ReadBatch_cmd;
  749. delim = ':';
  750. } else if(rw_type == rwt_read_cmd) {
  751. canvas_draw_str(canvas, 0, 7, "Read Command:");
  752. max_i = Read_cmd_Total;
  753. fsa = &Read_cmd;
  754. delim = '=';
  755. } else { // rwt_write_batch
  756. if(ask_question) {
  757. snprintf(screen_buf, sizeof(screen_buf), "RUN WRITE BATCH? %s", ask_question_answer ? "YES" : "NO");
  758. } else {
  759. strcpy(screen_buf, "Write Batch:");
  760. }
  761. canvas_draw_str(canvas, 0, 7, screen_buf);
  762. max_i = WriteBatch_cmd_Total;
  763. fsa = &WriteBatch_cmd;
  764. delim = ':';
  765. }
  766. if(NRF_ERROR) canvas_draw_str(canvas, 70, 7, "nRF24 ERROR!");
  767. uint16_t page = view_cmd[rw_type] & ~7;
  768. for(uint8_t i = 0; i < 8 && page + i < max_i; i++) {
  769. uint16_t y = 14 + i * 7;
  770. char *p = (char*) furi_string_get_cstr((*fsa)[page + i]);
  771. char *end = strchr(p, delim);
  772. if(end) {
  773. if(*(end - 2) == '*') end -= 2; // *n - var size set
  774. uint16_t len = MIN((end - p), 30);
  775. strncpy(screen_buf, p, len);
  776. screen_buf[len] = '\0';
  777. canvas_draw_str(canvas, 5, y, screen_buf);
  778. }
  779. if((view_cmd[rw_type] & 7) == i) {
  780. canvas_draw_str(canvas, 0, y, ">");
  781. canvas_draw_str(canvas, -1, y, ">");
  782. }
  783. }
  784. } else { // what_doing == 2
  785. if(rw_type == rwt_read_cmd) { // Read command
  786. canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
  787. strcpy(screen_buf, "Read cmd:");
  788. if(NRF_ERROR) strcat(screen_buf, "nRF24 ERROR!");
  789. else if(ERR) {
  790. snprintf(screen_buf + strlen(screen_buf), 16, " Error %d", ERR);
  791. canvas_draw_str(canvas, 0, 60, ERR_STR);
  792. } else if(send_status == sst_sending) strcat(screen_buf, " sending");
  793. else if(send_status == sst_receiving) strcat(screen_buf, " receiving");
  794. else if(send_status == sst_error) strcat(screen_buf, " NO ACK!");
  795. else if(send_status == sst_timeout) strcat(screen_buf, " TIMEOUT!");
  796. else if(send_status == sst_ok) strcat(screen_buf, " Ok");
  797. canvas_draw_str(canvas, 0, 10, screen_buf);
  798. if(Log_Total) {
  799. char *p = (char*)furi_string_get_cstr(Log[Log_Total - 1]);
  800. strncpy(screen_buf, p + MIN(view_x, strlen(p)), 30);
  801. canvas_draw_str(canvas, 0, 15 + 10, screen_buf);
  802. }
  803. } else { // if(rw_type == rwt_read_batch || rw_type == rwt_write_batch)
  804. canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
  805. if(ask_question && rw_type == rwt_read_batch) {
  806. snprintf(screen_buf, sizeof(screen_buf), "SAVE AS WRITE BATCH? %s", ask_question_answer ? "YES" : "NO");
  807. } else {
  808. strcpy(screen_buf, rw_type == rwt_read_batch ? "Read Batch:" : "Write Batch:");
  809. if(NRF_ERROR) strcat(screen_buf, "nRF24 ERROR!");
  810. else if(ERR) snprintf(screen_buf + strlen(screen_buf), 16, " Error %d", ERR);
  811. else if(send_status == sst_error) strcat(screen_buf, " NO ACK!");
  812. else if(send_status == sst_timeout) strcat(screen_buf, " TIMEOUT!");
  813. else if(send_status == sst_ok && ((rw_type == rwt_read_batch && (uint32_t)ReadBatch_cmd_curr == 0xFFFFFFFF)
  814. || (rw_type == rwt_write_batch && (uint32_t)WriteBatch_cmd_curr == 0xFFFFFFFF)))
  815. strcat(screen_buf, " Ok");
  816. else strcat(screen_buf, " working");
  817. }
  818. canvas_draw_str(canvas, 0, 7, screen_buf);
  819. if(Log_Total) {
  820. uint16_t page = view_Batch & ~7;
  821. for(uint8_t i = 0; i < 8 && page + i < Log_Total; i++) {
  822. uint16_t y = 14 + i * 7;
  823. screen_buf[sizeof(screen_buf) - 1] = '\0';
  824. char *p = (char*)furi_string_get_cstr(Log[page + i]);
  825. strncpy(screen_buf, p + MIN(view_x, strlen(p)), 30);
  826. if(ERR && page + i == Log_Total - 1) strcat(screen_buf, ERR_STR);
  827. canvas_draw_str(canvas, 5, y, screen_buf);
  828. if((view_Batch & 7) == i) {
  829. canvas_draw_str(canvas, 0, y, ">");
  830. canvas_draw_str(canvas, -1, y, ">");
  831. }
  832. }
  833. }
  834. }
  835. }
  836. release_mutex((ValueMutex*)ctx, plugin_state);
  837. }
  838. void work_timer_callback(void* ctx)
  839. {
  840. UNUSED(ctx);
  841. if(what_doing == 2) {
  842. if(rw_type == rwt_write_batch) {
  843. if(send_status == sst_ok) {
  844. if((uint32_t)WriteBatch_cmd_curr != 0xFFFFFFFF && ERR == 0 && furi_get_tick() - NRF_time >= delay_between_pkt) {
  845. Run_WriteBatch_cmd(NULL);
  846. }
  847. }
  848. } else if(send_status == sst_sending) { // sending
  849. if(!NRF_last_packet_send_st) { // No ACK on last attempt
  850. if(furi_get_tick() - NRF_time > delay_between_pkt) {
  851. if(NRF_repeat++ < NRF_resend) nrf24_resend_packet(); else send_status = sst_error; // error NO_ACK
  852. }
  853. }
  854. } else if(send_status == sst_receiving) { // receiving
  855. for(uint8_t i = 0; i < 3; i++) {
  856. bool new = nrf24_read_newpacket();
  857. if(new) {
  858. if(send_status != sst_receiving) break;
  859. } else if(furi_get_tick() - NRF_time > NRF_READ_TIMEOUT) {
  860. if(NRF_repeat++ < NRF_resend) {
  861. send_status = sst_sending;
  862. nrf24_resend_packet();
  863. } else {
  864. FURI_LOG_D(TAG, "TIMEOUT: %lu", furi_get_tick() - NRF_time);
  865. send_status = sst_timeout;
  866. }
  867. break;
  868. }
  869. }
  870. } else if(send_status == sst_ok) {
  871. if(rw_type == rwt_read_batch) {
  872. if((uint32_t)ReadBatch_cmd_curr != 0xFFFFFFFF && ERR == 0 && furi_get_tick() - NRF_time >= delay_between_pkt) {
  873. Run_ReadBatch_cmd(NULL);
  874. }
  875. }
  876. }
  877. }
  878. }
  879. int32_t nrf24batch_app(void* p) {
  880. UNUSED(p);
  881. APP = malloc(sizeof(nRF24Batch));
  882. APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  883. APP->plugin_state = malloc(sizeof(PluginState));
  884. ValueMutex state_mutex;
  885. if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) {
  886. furi_message_queue_free(APP->event_queue);
  887. FURI_LOG_E(TAG, "cannot create mutex");
  888. free(APP->plugin_state);
  889. return 255;
  890. }
  891. nrf24_init();
  892. // Set system callbacks
  893. APP->view_port = view_port_alloc();
  894. view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex);
  895. view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue);
  896. // Open GUI and register view_port
  897. APP->gui = furi_record_open(RECORD_GUI);
  898. gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen);
  899. APP->notification = furi_record_open(RECORD_NOTIFICATION);
  900. APP->storage = furi_record_open(RECORD_STORAGE);
  901. storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER);
  902. file_stream = file_stream_alloc(APP->storage);
  903. FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, NULL);
  904. furi_timer_start(work_timer, WORK_PERIOD);
  905. PluginEvent event;
  906. for(bool processing = true; processing;) {
  907. FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 200);
  908. PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
  909. static FuriLogLevel FuriLogLevel = FuriLogLevelDefault;
  910. if(furi_log_get_level() != FuriLogLevel) {
  911. FuriLogLevel = furi_log_get_level();
  912. if(FuriLogLevel == FuriLogLevelDebug) furi_hal_uart_set_br(FuriHalUartIdUSART1, 1843200);
  913. }
  914. if(event_status == FuriStatusOk) {
  915. // press events
  916. if(event.type == EventTypeKey) {
  917. switch(event.input.key) {
  918. case InputKeyUp:
  919. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  920. if(!ask_question) {
  921. if(what_doing == 0) {
  922. } else if(what_doing == 1) {
  923. if(view_cmd[rw_type]) view_cmd[rw_type]--;
  924. } else if(what_doing == 2) {
  925. if(view_Batch) view_Batch--;
  926. }
  927. }
  928. }
  929. break;
  930. case InputKeyDown:
  931. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  932. if(!ask_question) {
  933. if(what_doing == 0) {
  934. what_doing = 1;
  935. } else if(what_doing == 1) {
  936. if(view_cmd[rw_type] + 1 < (rw_type == rwt_read_batch ? ReadBatch_cmd_Total : rw_type == rwt_read_cmd ? Read_cmd_Total : WriteBatch_cmd_Total)) view_cmd[rw_type]++;
  937. } else if(what_doing == 2) {
  938. if(view_Batch < Log_Total - 1) view_Batch++;
  939. }
  940. }
  941. }
  942. break;
  943. case InputKeyLeft:
  944. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  945. if(ask_question) {
  946. ask_question_answer ^= 1;
  947. } else if(what_doing == 0) {
  948. } else if(what_doing == 1) {
  949. if(--rw_type > rwt_write_batch) rw_type = rwt_write_batch;
  950. } else if(what_doing == 2) {
  951. if(view_x) view_x--;
  952. }
  953. }
  954. break;
  955. case InputKeyRight:
  956. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  957. if(ask_question) {
  958. ask_question_answer ^= 1;
  959. } else if(what_doing == 0) {
  960. what_doing = 1;
  961. } else if(what_doing == 1) {
  962. if(++rw_type > rwt_write_batch) rw_type = rwt_read_batch;
  963. } else if(what_doing == 2) {
  964. view_x++;
  965. }
  966. }
  967. break;
  968. case InputKeyOk:
  969. if(event.input.type == InputTypeShort) {
  970. if(ask_question) {
  971. ask_question = 0;
  972. if(what_doing == 2 && rw_type == rwt_read_batch) {
  973. if(ask_question_answer) save_batch();
  974. } else if(what_doing == 1 && rw_type == rwt_write_batch) {
  975. if(ask_question_answer) {
  976. ERR = 0;
  977. Run_WriteBatch_cmd(WriteBatch_cmd[view_cmd[rwt_write_batch]]);
  978. view_Batch = 0;
  979. what_doing = 2;
  980. }
  981. }
  982. } else if(what_doing == 0) {
  983. file_stream_close(file_stream);
  984. if(select_settings_file()) {
  985. uint8_t err = load_settings_file();
  986. if(err) snprintf(file_name, sizeof(file_name), "LOAD ERROR #%d", err);
  987. }
  988. } else if(what_doing == 1) {
  989. if(rw_type == rwt_read_batch) {
  990. if(ReadBatch_cmd_Total) {
  991. ERR = 0;
  992. Run_ReadBatch_cmd(ReadBatch_cmd[view_cmd[rwt_read_batch]]);
  993. view_Batch = 0;
  994. what_doing = 2;
  995. }
  996. } else if(rw_type == rwt_read_cmd) {
  997. if(Read_cmd_Total) {
  998. ERR = 0;
  999. free_Log();
  1000. Run_Read_cmd(Read_cmd[view_cmd[rwt_read_cmd]]);
  1001. what_doing = 2;
  1002. }
  1003. } else if(rw_type == rwt_write_batch) {
  1004. if(WriteBatch_cmd_Total) {
  1005. ask_question = 1;
  1006. ask_question_answer = 0;
  1007. }
  1008. }
  1009. } else if(what_doing == 2) {
  1010. if(rw_type == rwt_read_batch && Log_Total) {
  1011. ask_question = 1;
  1012. ask_question_answer = 0;
  1013. }
  1014. }
  1015. // } else if(event.input.type == InputTypeLong) {
  1016. // if(what_doing == 0) {
  1017. // } else if(what_doing == 1 || what_doing == 2) {
  1018. // }
  1019. }
  1020. break;
  1021. case InputKeyBack:
  1022. if(event.input.type == InputTypeLong) processing = false;
  1023. else if(event.input.type == InputTypeShort) {
  1024. if(what_doing) what_doing--;
  1025. if(what_doing == 0) rw_type = rwt_read_batch;
  1026. if(what_doing <= 1) view_x = 0;
  1027. ERR = 0;
  1028. send_status = sst_none;
  1029. }
  1030. break;
  1031. default:
  1032. break;
  1033. }
  1034. }
  1035. }
  1036. view_port_update(APP->view_port);
  1037. release_mutex(&state_mutex, plugin_state);
  1038. }
  1039. nrf24_set_idle(nrf24_HANDLE);
  1040. nrf24_deinit();
  1041. view_port_enabled_set(APP->view_port, false);
  1042. gui_remove_view_port(APP->gui, APP->view_port);
  1043. furi_record_close(RECORD_GUI);
  1044. furi_record_close(RECORD_NOTIFICATION);
  1045. furi_record_close(RECORD_STORAGE);
  1046. if(file_stream) {
  1047. file_stream_close(file_stream);
  1048. stream_free(file_stream);
  1049. }
  1050. view_port_free(APP->view_port);
  1051. furi_message_queue_free(APP->event_queue);
  1052. free_store();
  1053. furi_timer_stop(work_timer);
  1054. furi_timer_free(work_timer);
  1055. free(APP->plugin_state);
  1056. free(APP);
  1057. return 0;
  1058. }