【问题标题】:Getting DHCP options using ESP32使用 ESP32 获取 DHCP 选项
【发布时间】:2022-06-20 00:19:04
【问题描述】:

我正在处理试图通过 Wifi 和以太网连接到 dhcp 服务器的 ESP32 模块 (ESP-IDF)。关键是,我正在尝试获取 DHCP 选项,但我失败了……

我尝试使用 LWIP 和 ESP-NETIF。 (我还要猜XXXX和YYYY是什么)

// Create an esp_netif pointer to store current interface
  esp_netif_t* ifscan = esp_netif_next(NULL);

  // Stores the unique interface descriptor, such as "PP1", etc
  char ifdesc[7];
  ifdesc[6] = 0;  // Ensure null terminated string
  uint32_t value = 0;

  while (ifscan != NULL)
  {
      esp_netif_get_netif_impl_name(ifscan, ifdesc);
      Serial.printf("IF NAME: %s\n", ifdesc);

      esp_err_t code = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, ESP_NETIF_DOMAIN_NAME_SERVER, XXXX, YYYY);
      Serial.printf("RES: %s - OPTION: %s\n", esp_err_to_name(code), XXXXX);
   


      // Get the next interface
      ifscan = esp_netif_next(ifscan);
  }
      Serial.printf("Done listing network interfaces");

有没有人有源代码示例显示连接后如何获取选项?

提前谢谢你。

【问题讨论】:

    标签: esp32 dhcp lwip esp-idf


    【解决方案1】:

    代码中只实现了几个 DHCP 选项。
    检查source code (github)esp_netif_dhcpc_option
    ESP_NETIF_DOMAIN_NAME_SERVER e.q.不是。

    对于ESP_NETIF_OP_GET,仅实现了ESP_NETIF_IP_REQUEST_RETRY_TIMEESP_NETIF_VENDOR_SPECIFIC_INFO
    否则你会得到ESP_ERR_ESP_NETIF_INVALID_PARAMS (20481 0x5001)。

    esp_err_t esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val,
                                     uint32_t opt_len)
    {
    ...
        if (opt_op == ESP_NETIF_OP_GET) {
            if (esp_netif->dhcpc_status == ESP_NETIF_DHCP_STOPPED) {
                return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED;
            }
            switch (opt_id) {
                case ESP_NETIF_IP_REQUEST_RETRY_TIME:
                    if (opt_len == sizeof(dhcp->tries)) {
                        *(uint8_t *)opt_val = dhcp->tries;
                    }
                    break;
    #if ESP_DHCP && !ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER
                case ESP_NETIF_VENDOR_SPECIFIC_INFO:
                    return dhcp_get_vendor_specific_information(opt_len, opt_val);
    #endif
                default:
                    return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
            }
    ...
    }
    

    我尝试在我的 ESP32 上使用您的代码和选项 224

      Serial.println("Option >");
      esp_err_t result = 0;
      uint32_t value = 0;
      esp_netif_t * ifscan = esp_netif_next(NULL);
      char ifname[10];
      result = esp_netif_get_netif_impl_name(ifscan, ifname);
      Serial.print("esp_netif_get_netif_impl_name: ");
      Serial.println(result);
      result = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, (esp_netif_dhcp_option_id_t)224, &value, 4);
      Serial.print("esp_netif_dhcpc_option: ");
      Serial.println(result);
      Serial.print("esp_netif_dhcpc_option value: ");
      Serial.println(value);
      Serial.println("Option <");
    

    结果:

    Option >
    esp_netif_get_netif_impl_name: 0
    esp_netif_dhcpc_option: 20481
    esp_netif_dhcpc_option value: 0
    Option <
    

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多