espressif / esp-homekit-sdk

544 stars 98 forks source link

How to include esp_hap_database.h file in app_main.c #80

Closed twlawrenceko closed 2 years ago

twlawrenceko commented 2 years ago

Hello,

As HAP uses port 80, we would like to redirect port 80 to port 8080 once the user hits port 80 (i.e. 192.168.1.1 --> 192.168.1.1:8080). However, the uri handler is in HAP and the httpd_handle_t is defined in esp_hap_database.h. In order to create a redirect, a httpd_register_uri_handler(hap_priv.server, &port_redirect_post_uri) command is needed. However, even with #include <esp_hap_database.h> added on top of app_main.c it says fatal error: esp_hap_database.h: No such file or directory.

Please assist on how to configure CMakeLists file or others to be possible to get hap_priv.server.

Below is a chain of function steps:

  1. hap_httpd_start() inside hap_start().
  2. hap_platform_httpd_start(&hap_priv.server) called inside hap_httpd_start().
  3. hap_platform_httpd_start(httpd_handle_t *handle) uses &hap_priv.server that is pointed into it.

Thank you

shahpiyushv commented 2 years ago

@twlawrenceko , what exactly is the purpose of what you are doing? If you want to use port 8080 instead of 80 for HAP, you might as well change it using CONFIG_HAP_HTTP_SERVER_PORT

twlawrenceko commented 2 years ago

@twlawrenceko , what exactly is the purpose of what you are doing? If you want to use port 8080 instead of 80 for HAP, you might as well change it using CONFIG_HAP_HTTP_SERVER_PORT

I am not familiar with HAP protocols, would there be any issue or trouble if we use port 8080 for HAP instead of port 80? A http webpage is hosted but for the ease of use, we would like to enter the hostname and redirect to port 8080 automatically.

shahpiyushv commented 2 years ago

@twlawrenceko , HAP can work on any port, just that it should match with what is announced in the HAP mDNS service. CONFIG_HAP_HTTP_SERVER_PORT takes care of that.

shahpiyushv commented 2 years ago

@twlawrenceko , just to add, even if you want to use the hap_priv.server pointer to add any new URIs, instead of accessing the internal DB, you can use the hap_platform_httpd_get_handle() API, an example of which can be found here.

twlawrenceko commented 2 years ago

@shahpiyushv , Thank you for the detailed info. This is an extended question regarding to this: The mdns_hostname_set() does not seem to when HAP is running. Is there any method to fix this?

Update: mDNS is initialized in HAP at hap_mdns_init(), with "MyHost.local" it works, but is it possible to change the mDNS name to something else (i.e. "esp-home.local")

shahpiyushv commented 2 years ago

@twlawrenceko do you mean to say that if you change the hostname using mdns_hostname_set(), it does not take effect? Please use it after hap_start() and see if it works.

twlawrenceko commented 2 years ago

@twlawrenceko do you mean to say that if you change the hostname using mdns_hostname_set(), it does not take effect? Please use it after hap_start() and see if it works.

Thank you for the notice, after hap_start() it works.

twlawrenceko commented 1 year ago

Hello @shahpiyushv ,

It seems to be related issue to this one which HAP is conflicting with the httpd_server connection.

We have the follow error when visiting REST server with HAP running at the same time:

W (50673) httpd: httpd_accept_conn: error in accept (23)
W (50673) httpd: httpd_server: error accepting new connection

Before merging HAP, we have enhanced the reliability in WS and REST server (based on restful_server and wss_server), CONFIG_LWIP_SO_LINGER=y is also set in menuconfig along with httpd.close_fn modifications . And to merge with HAP gracefully with REST server, we have changed CONFIG_HAP_HTTP_SERVER_PORT=8080 and CONFIG_HAP_HTTP_MAX_OPEN_SOCKETS=7 in menuconfig for HAP to avoid port conflicts.

How can we fix the random error in accept (23) error in this case along with HAP enabled?