nrf24batch.c 44 KB

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