nrf24batch.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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[20] = "";
  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 ? 0b0010 : 0b0001)<<4) | 0b0111); // 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", rw_type, send_status, 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. NRF_INITED = false;
  613. while(stream_read_line(file_stream, str)) {
  614. char *p = (char*)furi_string_get_cstr(str);
  615. if(*p <= ' ') continue;
  616. //char* delim_eq = strchr(p, '=');
  617. char* delim_col = strchr(p, ':');
  618. if(delim_col == NULL) { // Constant found - no ':'
  619. if(Constants == NULL) {
  620. Constants = furi_string_alloc_set(str);
  621. } else furi_string_cat(Constants, str);
  622. } else {
  623. str_rtrim(p);
  624. if(strncmp(p, SettingsFld_Rate, sizeof(SettingsFld_Rate)-1) == 0) {
  625. NRF_rate = atoi(p + sizeof(SettingsFld_Rate));
  626. } else if(strncmp(p, SettingsFld_Info, sizeof(SettingsFld_Info)-1) == 0) {
  627. strncpy(Info, p + sizeof(SettingsFld_Info), sizeof(Info)-1);
  628. } else if(strncmp(p, SettingsFld_Ch, sizeof(SettingsFld_Ch)-1) == 0) {
  629. NRF_channel = atoi(p + sizeof(SettingsFld_Ch));
  630. } else if(strncmp(p, SettingsFld_Address, sizeof(SettingsFld_Address)-1) == 0) {
  631. p += sizeof(SettingsFld_Address);
  632. addr_len = ConvertHexToArray(p, addr, 5);
  633. } else if(strncmp(p, SettingsFld_CRC, sizeof(SettingsFld_CRC)-1) == 0) {
  634. NRF_CRC = atoi(p + sizeof(SettingsFld_CRC));
  635. } else if(strncmp(p, SettingsFld_DPL, sizeof(SettingsFld_DPL)-1) == 0) {
  636. NRF_DPL = atoi(p + sizeof(SettingsFld_DPL));
  637. } else if(strncmp(p, SettingsFld_Resend, sizeof(SettingsFld_Resend)-1) == 0) {
  638. NRF_resend = atoi(p + sizeof(SettingsFld_Resend));
  639. } else if(strncmp(p, SettingsFld_Delay, sizeof(SettingsFld_Delay)-1) == 0) {
  640. delay_between_pkt = atoi(p + sizeof(SettingsFld_Delay));
  641. } else if(strncmp(p, SettingsFld_Payload, sizeof(SettingsFld_Payload)-1) == 0) {
  642. p += sizeof(SettingsFld_Payload);
  643. payload_fields = 0;
  644. payload_size = 0;
  645. do {
  646. uint8_t b = atoi(p);
  647. if(b < 1 || b > 4) {
  648. FURI_LOG_D(TAG, "Wrong payload format (%d)", b);
  649. err = 3;
  650. break;
  651. }
  652. payload_struct[payload_fields++] = b;
  653. payload_size += b;
  654. if(payload_fields == sizeof(payload_struct) - 1) break;
  655. if((p = strchr(p, ',')) == NULL) break;
  656. p++;
  657. } while(1);
  658. FURI_LOG_D(TAG, "Payload fields %d: %d,%d,%d", payload_fields, payload_struct[0], payload_struct[1], payload_struct[2]);
  659. } else if(strncmp(p, SettingsFld_ReadDefault, sizeof(SettingsFld_ReadDefault)-1) == 0) {
  660. ReadDefault = furi_string_alloc_set_str(p + sizeof(SettingsFld_ReadDefault));
  661. } else if(strncmp(p, SettingsFld_WriteStart, sizeof(SettingsFld_WriteStart)-1) == 0) {
  662. WriteStart = furi_string_alloc_set_str(p + sizeof(SettingsFld_WriteStart));
  663. } else if(strncmp(p, SettingsFld_WriteDefault, sizeof(SettingsFld_WriteDefault)-1) == 0) {
  664. WriteDefault = furi_string_alloc_set_str(p + sizeof(SettingsFld_WriteDefault));
  665. } else if(strncmp(p, SettingsFld_Read, sizeof(SettingsFld_Read)-1) == 0) {
  666. p += sizeof(SettingsFld_Read);
  667. if(Read_cmd == NULL) Read_cmd = malloc(sizeof(Read_cmd));
  668. else {
  669. Read_cmd = realloc(Read_cmd, sizeof(Read_cmd) * (Read_cmd_Total + 1));
  670. }
  671. if(Read_cmd == NULL) {
  672. FURI_LOG_D(TAG, "Memory low, err 4");
  673. err = 4;
  674. break;
  675. }
  676. Read_cmd[Read_cmd_Total++] = furi_string_alloc_set_str(p);
  677. } else if(strncmp(p, SettingsFld_ReadBatch, sizeof(SettingsFld_ReadBatch)-1) == 0) {
  678. p += sizeof(SettingsFld_ReadBatch);
  679. if(ReadBatch_cmd == NULL) ReadBatch_cmd = malloc(sizeof(ReadBatch_cmd));
  680. else {
  681. ReadBatch_cmd = realloc(ReadBatch_cmd, sizeof(ReadBatch_cmd) * (ReadBatch_cmd_Total + 1));
  682. }
  683. if(ReadBatch_cmd == NULL) {
  684. FURI_LOG_D(TAG, "Memory low, err 5");
  685. err = 5;
  686. break;
  687. }
  688. ReadBatch_cmd[ReadBatch_cmd_Total++] = furi_string_alloc_set_str(p);
  689. } else if(strncmp(p, SettingsFld_WriteBatch, sizeof(SettingsFld_WriteBatch)-1) == 0) {
  690. p += sizeof(SettingsFld_WriteBatch);
  691. if(WriteBatch_cmd == NULL) WriteBatch_cmd = malloc(sizeof(WriteBatch_cmd));
  692. else {
  693. WriteBatch_cmd = realloc(WriteBatch_cmd, sizeof(WriteBatch_cmd) * (WriteBatch_cmd_Total + 1));
  694. }
  695. if(WriteBatch_cmd == NULL) {
  696. FURI_LOG_D(TAG, "Memory low, err 6");
  697. err = 6;
  698. break;
  699. }
  700. WriteBatch_cmd[WriteBatch_cmd_Total++] = furi_string_alloc_set_str(p);
  701. }
  702. }
  703. }
  704. furi_string_free(str);
  705. return err;
  706. }
  707. static void save_batch(void)
  708. {
  709. // to do...
  710. }
  711. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  712. furi_assert(event_queue);
  713. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  714. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  715. }
  716. static void render_callback(Canvas* const canvas, void* ctx) {
  717. const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
  718. if(plugin_state == NULL) return;
  719. //canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen
  720. if(what_doing == 0) {
  721. canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
  722. snprintf(screen_buf, sizeof(screen_buf), "Open: %s", file_name);
  723. canvas_draw_str(canvas, 10, 10, screen_buf);
  724. if(addr_len) {
  725. canvas_draw_str(canvas, 10, 22, Info);
  726. strcpy(screen_buf, "Address: ");
  727. add_to_str_hex_bytes(screen_buf, addr, addr_len);
  728. canvas_draw_str(canvas, 10, 32, screen_buf);
  729. snprintf(screen_buf, sizeof(screen_buf), "Ch: %d, Rate: %d", NRF_channel, NRF_rate);
  730. canvas_draw_str(canvas, 10, 42, screen_buf);
  731. snprintf(screen_buf, sizeof(screen_buf), "RB: %d, R: %d, WB: %d", ReadBatch_cmd_Total, Read_cmd_Total, WriteBatch_cmd_Total);
  732. canvas_draw_str(canvas, 10, 52, screen_buf);
  733. //canvas_draw_str(canvas, 10, 60, screen_buf);
  734. } else {
  735. snprintf(screen_buf, sizeof(screen_buf), "ver. %s (vad7)", VERSION);
  736. canvas_draw_str(canvas, 10, 60, screen_buf);
  737. }
  738. canvas_draw_str(canvas, 0, 10, ">");
  739. } else if(what_doing == 1){
  740. canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
  741. char delim;
  742. uint16_t max_i;
  743. FuriString ***fsa;
  744. if(rw_type == rwt_read_batch) {
  745. canvas_draw_str(canvas, 0, 7, "Read Batch:");
  746. max_i = ReadBatch_cmd_Total;
  747. fsa = &ReadBatch_cmd;
  748. delim = ':';
  749. } else if(rw_type == rwt_read_cmd) {
  750. canvas_draw_str(canvas, 0, 7, "Read Command:");
  751. max_i = Read_cmd_Total;
  752. fsa = &Read_cmd;
  753. delim = '=';
  754. } else { // rwt_write_batch
  755. if(ask_question) {
  756. snprintf(screen_buf, sizeof(screen_buf), "RUN WRITE BATCH? %s", ask_question_answer ? "YES" : "NO");
  757. } else {
  758. strcpy(screen_buf, "Write Batch:");
  759. }
  760. canvas_draw_str(canvas, 0, 7, screen_buf);
  761. max_i = WriteBatch_cmd_Total;
  762. fsa = &WriteBatch_cmd;
  763. delim = ':';
  764. }
  765. if(NRF_ERROR) canvas_draw_str(canvas, 70, 7, "nRF24 ERROR!");
  766. uint16_t page = view_cmd[rw_type] & ~7;
  767. for(uint8_t i = 0; i < 8 && page + i < max_i; i++) {
  768. uint16_t y = 14 + i * 7;
  769. char *p = (char*) furi_string_get_cstr((*fsa)[page + i]);
  770. char *end = strchr(p, delim);
  771. if(end) {
  772. if(*(end - 2) == '*') end -= 2; // *n - var size set
  773. uint16_t len = MIN((end - p), 30);
  774. strncpy(screen_buf, p, len);
  775. screen_buf[len] = '\0';
  776. canvas_draw_str(canvas, 5, y, screen_buf);
  777. }
  778. if((view_cmd[rw_type] & 7) == i) {
  779. canvas_draw_str(canvas, 0, y, ">");
  780. canvas_draw_str(canvas, -1, y, ">");
  781. }
  782. }
  783. } else { // what_doing == 2
  784. if(rw_type == rwt_read_cmd) { // Read command
  785. canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
  786. strcpy(screen_buf, "Read cmd:");
  787. if(NRF_ERROR) strcat(screen_buf, "nRF24 ERROR!");
  788. else if(ERR) {
  789. snprintf(screen_buf + strlen(screen_buf), 16, " Error %d", ERR);
  790. canvas_draw_str(canvas, 0, 60, ERR_STR);
  791. } else if(send_status == sst_sending) strcat(screen_buf, " sending");
  792. else if(send_status == sst_receiving) strcat(screen_buf, " receiving");
  793. else if(send_status == sst_error) strcat(screen_buf, " NO ACK!");
  794. else if(send_status == sst_timeout) strcat(screen_buf, " TIMEOUT!");
  795. else if(send_status == sst_ok) strcat(screen_buf, " Ok");
  796. canvas_draw_str(canvas, 0, 10, screen_buf);
  797. if(Log_Total) {
  798. char *p = (char*)furi_string_get_cstr(Log[Log_Total - 1]);
  799. strncpy(screen_buf, p + MIN(view_x, strlen(p)), 30);
  800. canvas_draw_str(canvas, 0, 15 + 10, screen_buf);
  801. }
  802. } else { // if(rw_type == rwt_read_batch || rw_type == rwt_write_batch)
  803. canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
  804. if(ask_question && rw_type == rwt_read_batch) {
  805. snprintf(screen_buf, sizeof(screen_buf), "SAVE AS WRITE BATCH? %s", ask_question_answer ? "YES" : "NO");
  806. } else {
  807. strcpy(screen_buf, rw_type == rwt_read_batch ? "Read Batch:" : "Write Batch:");
  808. if(NRF_ERROR) strcat(screen_buf, "nRF24 ERROR!");
  809. else if(ERR) snprintf(screen_buf + strlen(screen_buf), 16, " Error %d", ERR);
  810. else if(send_status == sst_error) strcat(screen_buf, " NO ACK!");
  811. else if(send_status == sst_timeout) strcat(screen_buf, " TIMEOUT!");
  812. else if(send_status == sst_ok && ((rw_type == rwt_read_batch && (uint32_t)ReadBatch_cmd_curr == 0xFFFFFFFF)
  813. || (rw_type == rwt_write_batch && (uint32_t)WriteBatch_cmd_curr == 0xFFFFFFFF)))
  814. strcat(screen_buf, " Ok");
  815. else strcat(screen_buf, " working");
  816. }
  817. canvas_draw_str(canvas, 0, 7, screen_buf);
  818. if(Log_Total) {
  819. uint16_t page = view_Batch & ~7;
  820. for(uint8_t i = 0; i < 8 && page + i < Log_Total; i++) {
  821. uint16_t y = 14 + i * 7;
  822. screen_buf[sizeof(screen_buf) - 1] = '\0';
  823. char *p = (char*)furi_string_get_cstr(Log[page + i]);
  824. strncpy(screen_buf, p + MIN(view_x, strlen(p)), 30);
  825. if(ERR && page + i == Log_Total - 1) strcat(screen_buf, ERR_STR);
  826. canvas_draw_str(canvas, 5, y, screen_buf);
  827. if((view_Batch & 7) == i) {
  828. canvas_draw_str(canvas, 0, y, ">");
  829. canvas_draw_str(canvas, -1, y, ">");
  830. }
  831. }
  832. }
  833. }
  834. }
  835. release_mutex((ValueMutex*)ctx, plugin_state);
  836. }
  837. void work_timer_callback(void* ctx)
  838. {
  839. UNUSED(ctx);
  840. if(what_doing == 2) {
  841. if(rw_type == rwt_write_batch) {
  842. if(send_status == sst_ok) {
  843. if((uint32_t)WriteBatch_cmd_curr != 0xFFFFFFFF && ERR == 0 && furi_get_tick() - NRF_time >= delay_between_pkt) {
  844. Run_WriteBatch_cmd(NULL);
  845. }
  846. }
  847. } else if(send_status == sst_sending) { // sending
  848. if(!NRF_last_packet_send_st) { // No ACK on last attempt
  849. if(furi_get_tick() - NRF_time > delay_between_pkt) {
  850. if(NRF_repeat++ < NRF_resend) nrf24_resend_packet(); else send_status = sst_error; // error NO_ACK
  851. }
  852. }
  853. } else if(send_status == sst_receiving) { // receiving
  854. for(uint8_t i = 0; i < 3; i++) {
  855. bool new = nrf24_read_newpacket();
  856. if(new) {
  857. if(send_status != sst_receiving) break;
  858. } else if(furi_get_tick() - NRF_time > NRF_READ_TIMEOUT) {
  859. if(NRF_repeat++ < NRF_resend) {
  860. send_status = sst_sending;
  861. nrf24_resend_packet();
  862. } else {
  863. FURI_LOG_D(TAG, "TIMEOUT: %lu", furi_get_tick() - NRF_time);
  864. send_status = sst_timeout;
  865. }
  866. break;
  867. }
  868. }
  869. } else if(send_status == sst_ok) {
  870. if(rw_type == rwt_read_batch) {
  871. if((uint32_t)ReadBatch_cmd_curr != 0xFFFFFFFF && ERR == 0 && furi_get_tick() - NRF_time >= delay_between_pkt) {
  872. Run_ReadBatch_cmd(NULL);
  873. }
  874. }
  875. }
  876. }
  877. }
  878. int32_t nrf24batch_app(void* p) {
  879. UNUSED(p);
  880. APP = malloc(sizeof(nRF24Batch));
  881. APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  882. APP->plugin_state = malloc(sizeof(PluginState));
  883. ValueMutex state_mutex;
  884. if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) {
  885. furi_message_queue_free(APP->event_queue);
  886. FURI_LOG_E(TAG, "cannot create mutex");
  887. free(APP->plugin_state);
  888. return 255;
  889. }
  890. nrf24_init();
  891. // Set system callbacks
  892. APP->view_port = view_port_alloc();
  893. view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex);
  894. view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue);
  895. // Open GUI and register view_port
  896. APP->gui = furi_record_open(RECORD_GUI);
  897. gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen);
  898. APP->notification = furi_record_open(RECORD_NOTIFICATION);
  899. APP->storage = furi_record_open(RECORD_STORAGE);
  900. storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER);
  901. file_stream = file_stream_alloc(APP->storage);
  902. FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, NULL);
  903. furi_timer_start(work_timer, WORK_PERIOD);
  904. PluginEvent event;
  905. for(bool processing = true; processing;) {
  906. FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 200);
  907. PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
  908. static FuriLogLevel FuriLogLevel = FuriLogLevelDefault;
  909. if(furi_log_get_level() != FuriLogLevel) {
  910. FuriLogLevel = furi_log_get_level();
  911. if(FuriLogLevel == FuriLogLevelDebug) furi_hal_uart_set_br(FuriHalUartIdUSART1, 460800);
  912. }
  913. if(event_status == FuriStatusOk) {
  914. // press events
  915. if(event.type == EventTypeKey) {
  916. switch(event.input.key) {
  917. case InputKeyUp:
  918. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  919. if(!ask_question) {
  920. if(what_doing == 0) {
  921. } else if(what_doing == 1) {
  922. if(view_cmd[rw_type]) view_cmd[rw_type]--;
  923. } else if(what_doing == 2) {
  924. if(view_Batch) view_Batch--;
  925. }
  926. }
  927. }
  928. break;
  929. case InputKeyDown:
  930. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  931. if(!ask_question) {
  932. if(what_doing == 0) {
  933. what_doing = 1;
  934. } else if(what_doing == 1) {
  935. 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]++;
  936. } else if(what_doing == 2) {
  937. if(view_Batch < Log_Total - 1) view_Batch++;
  938. }
  939. }
  940. }
  941. break;
  942. case InputKeyLeft:
  943. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  944. if(ask_question) {
  945. ask_question_answer ^= 1;
  946. } else if(what_doing == 0) {
  947. } else if(what_doing == 1) {
  948. if(--rw_type > rwt_write_batch) rw_type = rwt_write_batch;
  949. } else if(what_doing == 2) {
  950. if(view_x) view_x--;
  951. }
  952. }
  953. break;
  954. case InputKeyRight:
  955. if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
  956. if(ask_question) {
  957. ask_question_answer ^= 1;
  958. } else if(what_doing == 0) {
  959. what_doing = 1;
  960. } else if(what_doing == 1) {
  961. if(++rw_type > rwt_write_batch) rw_type = rwt_read_batch;
  962. } else if(what_doing == 2) {
  963. view_x++;
  964. }
  965. }
  966. break;
  967. case InputKeyOk:
  968. if(event.input.type == InputTypeShort) {
  969. if(ask_question) {
  970. ask_question = 0;
  971. if(what_doing == 2 && rw_type == rwt_read_batch) {
  972. if(ask_question_answer) save_batch();
  973. } else if(what_doing == 1 && rw_type == rwt_write_batch) {
  974. if(ask_question_answer) {
  975. ERR = 0;
  976. Run_WriteBatch_cmd(WriteBatch_cmd[view_cmd[rwt_write_batch]]);
  977. view_Batch = 0;
  978. what_doing = 2;
  979. }
  980. }
  981. } else if(what_doing == 0) {
  982. file_stream_close(file_stream);
  983. if(select_settings_file()) {
  984. uint8_t err = load_settings_file();
  985. if(err) snprintf(file_name, sizeof(file_name), "LOAD ERROR #%d", err);
  986. }
  987. } else if(what_doing == 1) {
  988. if(rw_type == rwt_read_batch) {
  989. if(ReadBatch_cmd_Total) {
  990. ERR = 0;
  991. Run_ReadBatch_cmd(ReadBatch_cmd[view_cmd[rwt_read_batch]]);
  992. view_Batch = 0;
  993. what_doing = 2;
  994. }
  995. } else if(rw_type == rwt_read_cmd) {
  996. if(Read_cmd_Total) {
  997. ERR = 0;
  998. free_Log();
  999. Run_Read_cmd(Read_cmd[view_cmd[rwt_read_cmd]]);
  1000. what_doing = 2;
  1001. }
  1002. } else if(rw_type == rwt_write_batch) {
  1003. if(WriteBatch_cmd_Total) {
  1004. ask_question = 1;
  1005. ask_question_answer = 0;
  1006. }
  1007. }
  1008. } else if(what_doing == 2) {
  1009. if(rw_type == rwt_read_batch && Log_Total) {
  1010. ask_question = 1;
  1011. ask_question_answer = 0;
  1012. }
  1013. }
  1014. // } else if(event.input.type == InputTypeLong) {
  1015. // if(what_doing == 0) {
  1016. // } else if(what_doing == 1 || what_doing == 2) {
  1017. // }
  1018. }
  1019. break;
  1020. case InputKeyBack:
  1021. if(event.input.type == InputTypeLong) processing = false;
  1022. else if(event.input.type == InputTypeShort) {
  1023. if(what_doing) what_doing--;
  1024. if(what_doing == 0) rw_type = rwt_read_batch;
  1025. if(what_doing <= 1) view_x = 0;
  1026. ERR = 0;
  1027. send_status = sst_none;
  1028. }
  1029. break;
  1030. default:
  1031. break;
  1032. }
  1033. }
  1034. }
  1035. view_port_update(APP->view_port);
  1036. release_mutex(&state_mutex, plugin_state);
  1037. }
  1038. nrf24_set_idle(nrf24_HANDLE);
  1039. nrf24_deinit();
  1040. view_port_enabled_set(APP->view_port, false);
  1041. gui_remove_view_port(APP->gui, APP->view_port);
  1042. furi_record_close(RECORD_GUI);
  1043. furi_record_close(RECORD_NOTIFICATION);
  1044. furi_record_close(RECORD_STORAGE);
  1045. if(file_stream) {
  1046. file_stream_close(file_stream);
  1047. stream_free(file_stream);
  1048. }
  1049. view_port_free(APP->view_port);
  1050. furi_message_queue_free(APP->event_queue);
  1051. free_store();
  1052. furi_timer_stop(work_timer);
  1053. furi_timer_free(work_timer);
  1054. free(APP->plugin_state);
  1055. free(APP);
  1056. return 0;
  1057. }