|
| 1 | +#include "../new_common.h" |
| 2 | +#include "../new_cfg.h" |
| 3 | +#include "../logging/logging.h" |
| 4 | +#include "../obk_config.h" |
| 5 | +#include "drv_mdns.h" |
| 6 | + |
| 7 | +#if (PLATFORM_BK7231N || PLATFORM_BK7231T) && ENABLE_DRIVER_MDNS |
| 8 | + |
| 9 | +#include "lwip/apps/mdns_opts.h" |
| 10 | + |
| 11 | +#if LWIP_MDNS_RESPONDER |
| 12 | + |
| 13 | +#include "lwip/apps/mdns.h" |
| 14 | +#include "lwip/err.h" |
| 15 | +#include "lwip/init.h" |
| 16 | +#include "lwip/netif.h" |
| 17 | +#include "net.h" |
| 18 | +#include "tcpip.h" |
| 19 | + |
| 20 | +#ifndef LOCK_TCPIP_CORE |
| 21 | +#define LOCK_TCPIP_CORE() |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef UNLOCK_TCPIP_CORE |
| 25 | +#define UNLOCK_TCPIP_CORE() |
| 26 | +#endif |
| 27 | + |
| 28 | +extern int Main_IsConnectedToWiFi(); |
| 29 | +extern int DRV_MDNS_Active; |
| 30 | + |
| 31 | +static int g_mdnsInitDone = 0; |
| 32 | +static int g_mdnsNetifAdded = 0; |
| 33 | +static s8_t g_mdnsServiceSlot = -1; |
| 34 | + |
| 35 | +#if (LWIP_VERSION_MAJOR > 2) || (LWIP_VERSION_MAJOR == 2 && LWIP_VERSION_MINOR >= 1) |
| 36 | +#define DRV_MDNS_HAS_RESTART_API 1 |
| 37 | +#else |
| 38 | +#define DRV_MDNS_HAS_RESTART_API 0 |
| 39 | +#endif |
| 40 | + |
| 41 | +static struct netif *DRV_MDNS_GetStaNetif(void) { |
| 42 | + return (struct netif *)net_get_sta_handle(); |
| 43 | +} |
| 44 | + |
| 45 | +static void DRV_MDNS_StartOrRestart(void) { |
| 46 | + struct netif *netif; |
| 47 | + const char *hostName; |
| 48 | + err_t err; |
| 49 | + |
| 50 | + netif = DRV_MDNS_GetStaNetif(); |
| 51 | + if (netif == 0) { |
| 52 | + addLogAdv(LOG_ERROR, LOG_FEATURE_HTTP, "DRV_MDNS: no STA netif handle"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + hostName = CFG_GetShortDeviceName(); |
| 57 | + if (hostName == 0 || *hostName == 0) { |
| 58 | + hostName = "openbeken"; |
| 59 | + } |
| 60 | + |
| 61 | + LOCK_TCPIP_CORE(); |
| 62 | + |
| 63 | + if (!g_mdnsInitDone) { |
| 64 | + mdns_resp_init(); |
| 65 | + g_mdnsInitDone = 1; |
| 66 | + } |
| 67 | + |
| 68 | + if (!g_mdnsNetifAdded) { |
| 69 | + err = mdns_resp_add_netif(netif, hostName, 120); |
| 70 | + if (err == ERR_OK) { |
| 71 | + g_mdnsNetifAdded = 1; |
| 72 | + g_mdnsServiceSlot = mdns_resp_add_service(netif, hostName, "_http", DNSSD_PROTO_TCP, 80, 120, 0, 0); |
| 73 | + if (g_mdnsServiceSlot < 0) { |
| 74 | + addLogAdv(LOG_ERROR, LOG_FEATURE_HTTP, "DRV_MDNS: mdns_resp_add_service failed"); |
| 75 | + } |
| 76 | +#if DRV_MDNS_HAS_RESTART_API |
| 77 | + mdns_resp_announce(netif); |
| 78 | +#else |
| 79 | + mdns_resp_netif_settings_changed(netif); |
| 80 | +#endif |
| 81 | + addLogAdv(LOG_INFO, LOG_FEATURE_HTTP, "DRV_MDNS: responder started (%s.local)", hostName); |
| 82 | + } else { |
| 83 | + addLogAdv(LOG_ERROR, LOG_FEATURE_HTTP, "DRV_MDNS: mdns_resp_add_netif failed %d", (int)err); |
| 84 | + } |
| 85 | + } else { |
| 86 | +#if DRV_MDNS_HAS_RESTART_API |
| 87 | + mdns_resp_restart(netif); |
| 88 | + mdns_resp_announce(netif); |
| 89 | +#else |
| 90 | + mdns_resp_netif_settings_changed(netif); |
| 91 | +#endif |
| 92 | + addLogAdv(LOG_INFO, LOG_FEATURE_HTTP, "DRV_MDNS: responder restarted"); |
| 93 | + } |
| 94 | + |
| 95 | + UNLOCK_TCPIP_CORE(); |
| 96 | +} |
| 97 | + |
| 98 | +void DRV_MDNS_Init(void) { |
| 99 | + DRV_MDNS_Active = 1; |
| 100 | + |
| 101 | + if (!Main_IsConnectedToWiFi()) { |
| 102 | + addLogAdv(LOG_INFO, LOG_FEATURE_HTTP, "DRV_MDNS_Init - no wifi, await connection"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + DRV_MDNS_StartOrRestart(); |
| 107 | +} |
| 108 | + |
| 109 | +void DRV_MDNS_RunEverySecond(void) { |
| 110 | +} |
| 111 | + |
| 112 | +void DRV_MDNS_RunQuickTick(void) { |
| 113 | +} |
| 114 | + |
| 115 | +void DRV_MDNS_Shutdown(void) { |
| 116 | + struct netif *netif; |
| 117 | + |
| 118 | + addLogAdv(LOG_INFO, LOG_FEATURE_HTTP, "DRV_MDNS_Shutdown"); |
| 119 | + |
| 120 | + if (g_mdnsNetifAdded) { |
| 121 | + netif = DRV_MDNS_GetStaNetif(); |
| 122 | + if (netif != 0) { |
| 123 | + LOCK_TCPIP_CORE(); |
| 124 | + mdns_resp_remove_netif(netif); |
| 125 | + UNLOCK_TCPIP_CORE(); |
| 126 | + } |
| 127 | + g_mdnsNetifAdded = 0; |
| 128 | + g_mdnsServiceSlot = -1; |
| 129 | + } |
| 130 | + |
| 131 | + DRV_MDNS_Active = 0; |
| 132 | +} |
| 133 | + |
| 134 | +#else |
| 135 | + |
| 136 | +extern int DRV_MDNS_Active; |
| 137 | + |
| 138 | +void DRV_MDNS_Init(void) { |
| 139 | + addLogAdv(LOG_INFO, LOG_FEATURE_HTTP, "DRV_MDNS unavailable in this SDK build"); |
| 140 | + DRV_MDNS_Active = 0; |
| 141 | +} |
| 142 | + |
| 143 | +void DRV_MDNS_RunEverySecond(void) { |
| 144 | +} |
| 145 | + |
| 146 | +void DRV_MDNS_RunQuickTick(void) { |
| 147 | +} |
| 148 | + |
| 149 | +void DRV_MDNS_Shutdown(void) { |
| 150 | + DRV_MDNS_Active = 0; |
| 151 | +} |
| 152 | + |
| 153 | +#endif |
| 154 | + |
| 155 | +#else |
| 156 | + |
| 157 | +extern int DRV_MDNS_Active; |
| 158 | + |
| 159 | +void DRV_MDNS_Init(void) { |
| 160 | + DRV_MDNS_Active = 0; |
| 161 | +} |
| 162 | + |
| 163 | +void DRV_MDNS_RunEverySecond(void) { |
| 164 | +} |
| 165 | + |
| 166 | +void DRV_MDNS_RunQuickTick(void) { |
| 167 | +} |
| 168 | + |
| 169 | +void DRV_MDNS_Shutdown(void) { |
| 170 | + DRV_MDNS_Active = 0; |
| 171 | +} |
| 172 | + |
| 173 | +#endif |
0 commit comments