Advertisement
nene1234

arp one device work

Jul 3rd, 2025
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <lilka.h>
  2. #include "ipapp.h"
  3. #include "servicemanager.h"
  4. #include "services/network.h"
  5.  
  6. extern "C" {
  7. #include "lwip/etharp.h"
  8. #include "lwip/netif.h"
  9. #include "lwip/ip_addr.h"
  10. }
  11.  
  12. IPApp::IPApp() : App("IP Address") {}
  13.  
  14. void IPApp::run() {
  15.     struct netif* netif = netif_list; // Зазвичай перший — ваш WiFi STA
  16.  
  17.     while (true) {
  18.         String ipAddress = WiFi.localIP().toString();
  19.         String macStr = WiFi.macAddress();
  20.         String info = "IP: " + ipAddress + "\nMAC: " + macStr;
  21.  
  22.         // Надсилаємо ARP-запит до цільового IP
  23.         ip4_addr_t target_ip;
  24.         ip4addr_aton("192.168.0.105", &target_ip); // IP пристрою, якому надсилаєте ARP
  25.         etharp_request(netif, &target_ip); // Відправка ARP-запиту
  26.  
  27.         // Чекаємо ~500 мс для отримання відповіді
  28.         vTaskDelay(500 / portTICK_PERIOD_MS);
  29.  
  30.         // Перевіряємо ARP-кеш
  31.         struct eth_addr *eth_ret;
  32.         struct netif *netif_ret;
  33.         const ip4_addr_t *ip_ret;
  34.         String targetMacStr = "not found";
  35.         if (etharp_find_addr(netif, (ip4_addr_t*)&target_ip, &eth_ret, &ip_ret) >= 0) {
  36.             char macbuf[18];
  37.             snprintf(macbuf, sizeof(macbuf), "%02X:%02X:%02X:%02X:%02X:%02X",
  38.                 eth_ret->addr[0], eth_ret->addr[1], eth_ret->addr[2],
  39.                 eth_ret->addr[3], eth_ret->addr[4], eth_ret->addr[5]);
  40.             targetMacStr = String(macbuf);
  41.         } else {
  42.             targetMacStr = "not found (no ARP reply)";
  43.         }
  44.         info += "\n\n192.168.0.105 - MAC: " + targetMacStr;
  45.  
  46.         // Використовуємо готовий UI-компонент
  47.         lilka::Alert alert("Мережа", info);
  48.         alert.draw(canvas);
  49.         queueDraw();
  50.  
  51.         // Очікуємо, поки користувач не натисне кнопку для виходу
  52.         while (!alert.isFinished()) {
  53.             alert.update();
  54.             vTaskDelay(LILKA_UI_UPDATE_DELAY_MS / portTICK_PERIOD_MS);
  55.             lilka::State state = lilka::controller.getState();
  56.             if (state.b.pressed) {
  57.                 return;
  58.             }
  59.         }
  60.         // Після закриття alert — вихід з циклу (або можна зробити повторну перевірку)
  61.         return;
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. #ifndef IPAPP_H
  68. #define IPAPP_H
  69.  
  70. #include <lilka.h>
  71. #include "app.h"
  72. #include "services/network.h"
  73.  
  74. class IPApp : public App {
  75. public:
  76.     IPApp();
  77. private:
  78.     void run() override;
  79. };
  80.  
  81. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement