eth_view_process.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #include "eth_view_process.h"
  2. #include "eth_worker.h"
  3. #include "eth_worker_i.h"
  4. #include "finik_eth_icons.h"
  5. #include <furi_hal.h>
  6. #include <gui/gui.h>
  7. #include <gui/canvas.h>
  8. #include <string.h>
  9. #include "u8g2.h"
  10. #define TAG "EthView"
  11. EthViewProcess* ethernet_view_process_malloc(EthWorkerProcess type) {
  12. EthViewProcess* evp = malloc(sizeof(EthViewProcess));
  13. evp->type = type;
  14. evp->autofill = 1;
  15. evp->carriage = 0;
  16. evp->position = 0;
  17. evp->x = 27;
  18. evp->y = 6;
  19. evp->strings_cnt = 10;
  20. if(type == EthWorkerProcessInit) {
  21. evp->y += 22;
  22. EthViewDrawInit* init = malloc(sizeof(EthViewDrawInit));
  23. memset(init, 0, sizeof(EthViewDrawInit));
  24. init->mac[0] = 0x10;
  25. init->mac[1] = 0x08;
  26. init->mac[2] = 0xDC;
  27. init->mac[3] = 0x47;
  28. init->mac[4] = 0x47;
  29. init->mac[5] = 0x54;
  30. evp->draw_struct = init;
  31. } else if(type == EthWorkerProcessStatic) {
  32. evp->y += 22;
  33. EthViewDrawStatic* stat = malloc(sizeof(EthViewDrawStatic));
  34. memset(stat, 0, sizeof(EthViewDrawStatic));
  35. stat->ip[0] = 192;
  36. stat->ip[1] = 168;
  37. stat->ip[2] = 0;
  38. stat->ip[3] = 101;
  39. stat->mask[0] = 255;
  40. stat->mask[1] = 255;
  41. stat->mask[2] = 255;
  42. stat->mask[3] = 0;
  43. stat->gateway[0] = 192;
  44. stat->gateway[1] = 168;
  45. stat->gateway[2] = 0;
  46. stat->gateway[3] = 1;
  47. stat->dns[0] = 192;
  48. stat->dns[1] = 168;
  49. stat->dns[2] = 0;
  50. stat->dns[3] = 1;
  51. evp->draw_struct = stat;
  52. evp->strings_cnt = 20;
  53. } else if(type == EthWorkerProcessDHCP) {
  54. evp->strings_cnt = 20;
  55. } else if(type == EthWorkerProcessPing) {
  56. evp->y += 11;
  57. EthViewDrawPing* ping = malloc(sizeof(EthViewDrawPing));
  58. memset(ping, 0, sizeof(EthViewDrawPing));
  59. ping->ip[0] = 8;
  60. ping->ip[1] = 8;
  61. ping->ip[2] = 8;
  62. ping->ip[3] = 8;
  63. evp->draw_struct = ping;
  64. evp->strings_cnt = 20;
  65. }
  66. evp->fifo = malloc(sizeof(EthViewProcessLine) * evp->strings_cnt);
  67. return evp;
  68. }
  69. void ethernet_view_process_free(EthViewProcess* evp) {
  70. free(evp->fifo);
  71. if(evp->type == EthWorkerProcessInit || evp->type == EthWorkerProcessStatic ||
  72. evp->type == EthWorkerProcessPing) {
  73. free(evp->draw_struct);
  74. }
  75. free(evp);
  76. }
  77. static void draw_hex_digit(Canvas* canvas, uint8_t x, uint8_t y, uint8_t digit) {
  78. char digit_str[] = "0";
  79. if(digit < 0xA) {
  80. digit_str[0] += digit;
  81. } else if(digit < 0x10) {
  82. digit_str[0] = 'A';
  83. digit_str[0] += digit - 0xA;
  84. } else {
  85. return;
  86. }
  87. canvas_draw_str(canvas, x, y, digit_str);
  88. }
  89. static void draw_dec_number(Canvas* canvas, uint8_t x, uint8_t y, uint8_t num) {
  90. char num_str[] = "0";
  91. {
  92. num_str[0] = '0' + num % 10;
  93. canvas_draw_str(canvas, x + 6 + 6, y, num_str);
  94. }
  95. if(num >= 10) {
  96. num_str[0] = '0' + num / 10 - (num / 100) * 10;
  97. canvas_draw_str(canvas, x + 6, y, num_str);
  98. }
  99. if(num >= 100) {
  100. num_str[0] = '0' + num / 100;
  101. canvas_draw_str(canvas, x, y, num_str);
  102. }
  103. }
  104. static void draw_static_mode(Canvas* canvas, uint8_t mode) {
  105. const uint8_t s1 = 13;
  106. const uint8_t s2 = 31;
  107. const uint8_t s3 = 19;
  108. const uint8_t s4 = 21;
  109. const uint8_t s = 35;
  110. const uint8_t h = 7;
  111. const uint8_t y = 10;
  112. const uint8_t y1 = 15;
  113. if(mode == EthViewDrawStaticModeIp) {
  114. canvas_invert_color(canvas);
  115. canvas_draw_box(canvas, s, y, s1, h);
  116. canvas_invert_color(canvas);
  117. canvas_draw_str(canvas, 38, y1, "ip");
  118. }
  119. if(mode == EthViewDrawStaticModeMask) {
  120. canvas_invert_color(canvas);
  121. canvas_draw_box(canvas, s + s1, y, s2, h);
  122. canvas_invert_color(canvas);
  123. canvas_draw_str(canvas, 53, y1, "mask");
  124. }
  125. if(mode == EthViewDrawStaticModeGateway) {
  126. canvas_invert_color(canvas);
  127. canvas_draw_box(canvas, s + s1 + s2, y, s3, h);
  128. canvas_invert_color(canvas);
  129. canvas_draw_str(canvas, 82, y1, "gw");
  130. }
  131. if(mode == EthViewDrawStaticModeDNS) {
  132. canvas_invert_color(canvas);
  133. canvas_draw_box(canvas, s + s1 + s2 + s3, y, s4, h);
  134. canvas_invert_color(canvas);
  135. canvas_draw_str(canvas, 102, y1, "dns");
  136. }
  137. }
  138. static uint8_t* draw_static_get_current_adress(EthViewDrawStatic* evds) {
  139. furi_assert(evds);
  140. if(evds->current_mode == EthViewDrawStaticModeIp) return evds->ip;
  141. if(evds->current_mode == EthViewDrawStaticModeMask) return evds->mask;
  142. if(evds->current_mode == EthViewDrawStaticModeGateway) return evds->gateway;
  143. if(evds->current_mode == EthViewDrawStaticModeDNS) return evds->dns;
  144. return evds->ip;
  145. }
  146. void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas) {
  147. furi_assert(canvas);
  148. furi_assert(process);
  149. canvas_set_font(canvas, FontSecondary);
  150. const uint8_t x = process->x;
  151. const uint8_t y = process->y;
  152. const uint8_t str_height = 11;
  153. const uint8_t str_count = (64 - y) / str_height;
  154. uint8_t carriage = process->carriage;
  155. uint8_t position = process->position;
  156. if(process->autofill) {
  157. position = (carriage + process->strings_cnt - str_count) % process->strings_cnt;
  158. process->position = position;
  159. }
  160. for(uint8_t i = 0; i < str_count; ++i) {
  161. uint8_t y1 = y + (i + 1) * str_height;
  162. canvas_draw_str(canvas, x, y1, process->fifo[(position + i) % process->strings_cnt].data);
  163. }
  164. if(process->type == EthWorkerProcessInit) {
  165. uint8_t editing = process->editing;
  166. canvas_draw_icon(canvas, 27, 10, &I_init_100x19px);
  167. uint8_t octet = ((EthViewDrawInit*)process->draw_struct)->current_octet;
  168. uint8_t* mac = ((EthViewDrawInit*)process->draw_struct)->mac;
  169. for(uint8_t i = 0; i < 6; ++i) {
  170. uint8_t x1 = 29 + i * 17;
  171. uint8_t x2 = x1 + 6;
  172. draw_hex_digit(canvas, x1, 25, (mac[i] & 0x0F));
  173. draw_hex_digit(canvas, x2, 25, (mac[i] & 0xF0) >> 4);
  174. if(editing && (octet / 2 == i)) {
  175. uint8_t x = octet & 1 ? x2 : x1;
  176. canvas_draw_line(canvas, x, 26, x + 4, 26);
  177. canvas_draw_line(canvas, x, 27, x + 4, 27);
  178. }
  179. }
  180. } else if(process->type == EthWorkerProcessStatic) {
  181. canvas_draw_frame(canvas, 31, 18, 21, 13);
  182. canvas_draw_frame(canvas, 55, 18, 21, 13);
  183. canvas_draw_frame(canvas, 79, 18, 21, 13);
  184. canvas_draw_frame(canvas, 103, 18, 21, 13);
  185. canvas_draw_box(canvas, 29, 10, 97, 7);
  186. uint8_t mode = ((EthViewDrawStatic*)process->draw_struct)->current_mode;
  187. uint8_t current_digit = ((EthViewDrawStatic*)process->draw_struct)->current_digit;
  188. uint8_t* adress = draw_static_get_current_adress((EthViewDrawStatic*)process->draw_struct);
  189. uint8_t editing = ((EthViewDrawStatic*)process->draw_struct)->editing;
  190. for(uint8_t i = 0; i < 4; ++i) {
  191. if(i == mode && process->editing) {
  192. draw_static_mode(canvas, mode);
  193. } else {
  194. canvas_invert_color(canvas);
  195. draw_static_mode(canvas, i);
  196. canvas_invert_color(canvas);
  197. }
  198. }
  199. for(uint8_t i = 0; i < 4; ++i) {
  200. uint8_t x = 33 + i * 24;
  201. draw_dec_number(canvas, x, 27, adress[i]);
  202. if(editing && (current_digit / 3 == i)) {
  203. uint8_t x1 = x + 6 * (current_digit % 3);
  204. canvas_draw_line(canvas, x1, 28, x1 + 4, 28);
  205. canvas_draw_line(canvas, x1, 29, x1 + 4, 29);
  206. }
  207. }
  208. } else if(process->type == EthWorkerProcessPing) {
  209. canvas_draw_box(canvas, 26, 8, 5, 13);
  210. canvas_draw_box(canvas, 52, 8, 3, 13);
  211. canvas_draw_box(canvas, 76, 8, 3, 13);
  212. canvas_draw_box(canvas, 100, 8, 3, 13);
  213. canvas_draw_box(canvas, 124, 8, 4, 13);
  214. canvas_draw_frame(canvas, 31, 8, 21, 13);
  215. canvas_draw_frame(canvas, 55, 8, 21, 13);
  216. canvas_draw_frame(canvas, 79, 8, 21, 13);
  217. canvas_draw_frame(canvas, 103, 8, 21, 13);
  218. uint8_t current_digit = ((EthViewDrawPing*)process->draw_struct)->current_digit;
  219. uint8_t* adress = ((EthViewDrawPing*)process->draw_struct)->ip;
  220. for(uint8_t i = 0; i < 4; ++i) {
  221. uint8_t x = 33 + i * 24;
  222. draw_dec_number(canvas, x, 17, adress[i]);
  223. if(process->editing && (current_digit / 3 == i)) {
  224. uint8_t x1 = x + 6 * (current_digit % 3);
  225. canvas_draw_line(canvas, x1, 18, x1 + 4, 18);
  226. canvas_draw_line(canvas, x1, 19, x1 + 4, 19);
  227. }
  228. }
  229. }
  230. }
  231. static void mac_change_hex_digit(uint8_t* mac, uint8_t octet, int8_t diff) {
  232. uint8_t digit = (octet & 1) ? (mac[octet / 2] >> 4) : (mac[octet / 2]);
  233. digit = (digit + diff) & 0xF;
  234. mac[octet / 2] = (mac[octet / 2] & ((octet & 1) ? 0x0F : 0xF0)) |
  235. (digit << ((octet & 1) ? 4 : 0));
  236. }
  237. static void adress_change_dec_digit(uint8_t* ip, uint8_t digit, int8_t diff) {
  238. {
  239. uint8_t k = 0;
  240. k = (digit % 3 == 0) ? 100 : k;
  241. k = (digit % 3 == 1) ? 10 : k;
  242. k = (digit % 3 == 2) ? 1 : k;
  243. diff *= k;
  244. }
  245. {
  246. int16_t ip1 = ip[digit / 3];
  247. if(diff > 0 && ((0x100 - ip1) > diff)) ip1 += diff;
  248. if(diff < 0 && (ip1 + diff >= 0)) ip1 += diff;
  249. ip[digit / 3] = ip1;
  250. }
  251. }
  252. void ethernet_view_process_keyevent(EthViewProcess* process, InputKey key) {
  253. furi_assert(process);
  254. if(process->type == EthWorkerProcessInit) {
  255. uint8_t octet = ((EthViewDrawInit*)process->draw_struct)->current_octet;
  256. uint8_t* mac = ((EthViewDrawInit*)process->draw_struct)->mac;
  257. if(key == InputKeyLeft) {
  258. if(octet > 0) {
  259. octet -= 1;
  260. } else {
  261. process->editing = 0;
  262. }
  263. } else if(key == InputKeyRight) {
  264. if(octet < 11) octet += 1;
  265. } else if(key == InputKeyUp) {
  266. mac_change_hex_digit(mac, octet, 1);
  267. } else if(key == InputKeyDown) {
  268. mac_change_hex_digit(mac, octet, -1);
  269. } else if(key == InputKeyOk || key == InputKeyBack) {
  270. process->editing = 0;
  271. }
  272. ((EthViewDrawInit*)process->draw_struct)->current_octet = octet;
  273. } else if(process->type == EthWorkerProcessDHCP) {
  274. process->editing = 0;
  275. if(key == InputKeyUp || key == InputKeyDown) {
  276. ethernet_view_process_move(process, (key == InputKeyDown) ? 1 : -1);
  277. }
  278. } else if(process->type == EthWorkerProcessStatic) {
  279. uint8_t digit = ((EthViewDrawStatic*)process->draw_struct)->current_digit;
  280. uint8_t mode = ((EthViewDrawStatic*)process->draw_struct)->current_mode;
  281. uint8_t* adress = draw_static_get_current_adress((EthViewDrawStatic*)process->draw_struct);
  282. uint8_t editing = ((EthViewDrawStatic*)process->draw_struct)->editing;
  283. if(editing) {
  284. if(key == InputKeyLeft) {
  285. if(digit > 0) {
  286. digit -= 1;
  287. } else {
  288. ((EthViewDrawStatic*)process->draw_struct)->editing = 0;
  289. }
  290. } else if(key == InputKeyRight) {
  291. if(digit < 11) digit += 1;
  292. } else if(key == InputKeyUp) {
  293. adress_change_dec_digit(adress, digit, 1);
  294. } else if(key == InputKeyDown) {
  295. adress_change_dec_digit(adress, digit, -1);
  296. } else if(key == InputKeyOk || key == InputKeyBack) {
  297. ((EthViewDrawStatic*)process->draw_struct)->editing = 0;
  298. }
  299. } else {
  300. if(key == InputKeyLeft) {
  301. if(mode > 0) {
  302. mode -= 1;
  303. } else {
  304. process->editing = 0;
  305. }
  306. } else if(key == InputKeyRight) {
  307. if(mode < 3) {
  308. mode += 1;
  309. }
  310. } else if(key == InputKeyUp || key == InputKeyDown) {
  311. ethernet_view_process_move(process, (key == InputKeyDown) ? 1 : -1);
  312. } else if(key == InputKeyOk) {
  313. ((EthViewDrawStatic*)process->draw_struct)->editing = 1;
  314. } else if(key == InputKeyBack) {
  315. mode = 0;
  316. process->editing = 0;
  317. }
  318. }
  319. ((EthViewDrawStatic*)process->draw_struct)->current_mode = mode;
  320. ((EthViewDrawStatic*)process->draw_struct)->current_digit = digit;
  321. } else if(process->type == EthWorkerProcessPing) {
  322. uint8_t digit = ((EthViewDrawPing*)process->draw_struct)->current_digit;
  323. uint8_t* adress = ((EthViewDrawPing*)process->draw_struct)->ip;
  324. if(key == InputKeyLeft) {
  325. if(digit > 0) {
  326. digit -= 1;
  327. } else {
  328. process->editing = 0;
  329. }
  330. } else if(key == InputKeyRight) {
  331. if(digit < 11) digit += 1;
  332. } else if(key == InputKeyUp) {
  333. adress_change_dec_digit(adress, digit, 1);
  334. } else if(key == InputKeyDown) {
  335. adress_change_dec_digit(adress, digit, -1);
  336. } else if(key == InputKeyOk || key == InputKeyBack) {
  337. process->editing = 0;
  338. }
  339. ((EthViewDrawPing*)process->draw_struct)->current_digit = digit;
  340. } else {
  341. if(key == InputKeyBack || key == InputKeyLeft) {
  342. process->editing = 0;
  343. }
  344. }
  345. }
  346. void ethernet_view_process_move(EthViewProcess* process, int8_t shift) {
  347. furi_assert(process);
  348. uint8_t position = process->position;
  349. if(shift <= -process->strings_cnt) {
  350. position = 0;
  351. } else if(shift >= process->strings_cnt) {
  352. position = process->carriage - 1;
  353. } else {
  354. position = (position + (process->strings_cnt + shift)) % process->strings_cnt;
  355. }
  356. process->position = position;
  357. process->autofill = !shift;
  358. }
  359. void ethernet_view_process_autofill(EthViewProcess* process, uint8_t state) {
  360. furi_assert(process);
  361. process->autofill = state;
  362. }
  363. static uint16_t get_string_with_width(const char* str, uint16_t width) {
  364. u8g2_t canvas_memory;
  365. Canvas* canvas = (Canvas*)&canvas_memory; // grazniy hack
  366. canvas_set_font(canvas, FontSecondary);
  367. uint8_t end = 0;
  368. char copy[SCREEN_SYMBOLS_WIDTH + 1] = {0};
  369. for(;;) {
  370. if(str[end] == '\0') {
  371. break;
  372. }
  373. if(end == SCREEN_SYMBOLS_WIDTH) {
  374. break;
  375. }
  376. copy[end] = str[end];
  377. if(canvas_string_width(canvas, copy) > width) {
  378. end -= 1;
  379. break;
  380. }
  381. end += 1;
  382. }
  383. return end;
  384. }
  385. void evp_printf(EthViewProcess* process, const char* format, ...) {
  386. va_list args;
  387. va_start(args, format);
  388. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  389. va_end(args);
  390. ethernet_view_process_print(process, furi_string_get_cstr(fstring));
  391. furi_string_free(fstring);
  392. }
  393. void ethernet_view_process_print(EthViewProcess* process, const char* str) {
  394. furi_assert(process);
  395. uint16_t max_width = 126 - process->x;
  396. uint16_t ptr = 0;
  397. uint16_t len = strlen(str);
  398. while(ptr < len) {
  399. uint16_t start = ptr;
  400. ptr += get_string_with_width(str + ptr, max_width);
  401. uint8_t carriage = process->carriage;
  402. uint8_t carriage1 = (carriage + 1) % process->strings_cnt;
  403. uint8_t carriage2 = (carriage + 2) % process->strings_cnt;
  404. FURI_LOG_I(TAG, "print %d %d %d %d %d", max_width, len, start, carriage, carriage1);
  405. memset(process->fifo[carriage].data, 0, SCREEN_SYMBOLS_WIDTH);
  406. memset(process->fifo[carriage1].data, 0, SCREEN_SYMBOLS_WIDTH);
  407. memset(process->fifo[carriage2].data, 0, SCREEN_SYMBOLS_WIDTH);
  408. memcpy(process->fifo[carriage].data, str + start, ptr - start);
  409. process->carriage = carriage1;
  410. }
  411. }