nrf24batch.c 42 KB

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