eth_view_process.c 14 KB

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