esp_netif_loopback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include "esp_netif_lwip_internal.h"
  15. #include "esp_netif.h"
  16. #include "esp_netif_private.h"
  17. #if CONFIG_ESP_NETIF_LOOPBACK
  18. #include "esp_log.h"
  19. //
  20. // Purpose of this module is to implement minimal loopback netif to facilitate
  21. // low level driver testing
  22. //
  23. #define ESP_NETIF_HOSTNAME_MAX_SIZE 32
  24. static const char *TAG = "esp_netif_loopback";
  25. static bool s_netif_initialized = false;
  26. static bool s_netif_started = false;
  27. static bool s_netif_up = false;
  28. /**
  29. * @brief Main esp-netif container with interface related information
  30. *
  31. *
  32. */
  33. struct esp_netif_obj {
  34. // default interface addresses
  35. uint8_t mac[NETIF_MAX_HWADDR_LEN];
  36. esp_netif_ip_info_t* ip_info;
  37. esp_netif_ip_info_t* ip_info_old;
  38. // io driver related
  39. void* driver_handle;
  40. esp_err_t (*driver_transmit)(void *h, void *buffer, size_t len);
  41. void (*driver_free_rx_buffer)(void *h, void* buffer);
  42. // misc flags, types, keys, priority
  43. esp_netif_flags_t flags;
  44. char * hostname;
  45. char * if_key;
  46. char * if_desc;
  47. int route_prio;
  48. };
  49. void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d)
  50. {
  51. memset(addr, 0, sizeof(esp_ip4_addr_t));
  52. addr->addr = esp_netif_htonl(esp_netif_ip4_makeu32(a,b,c,d));
  53. }
  54. char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen)
  55. {
  56. return NULL;
  57. }
  58. esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif)
  59. {
  60. return esp_netif->driver_handle;
  61. }
  62. esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev)
  63. {
  64. return NULL;
  65. }
  66. void* esp_netif_get_netif_impl(esp_netif_t *esp_netif)
  67. {
  68. return NULL;
  69. }
  70. esp_err_t esp_netif_init(void)
  71. {
  72. ESP_LOGI(TAG, "loopback initialization");
  73. if (s_netif_initialized) {
  74. ESP_LOGE(TAG, "esp-netif has already been initialized");
  75. return ESP_ERR_INVALID_SIZE;
  76. }
  77. s_netif_initialized = true;
  78. ESP_LOGD(TAG, "esp-netif has been successfully initialized");
  79. return ESP_OK;
  80. }
  81. esp_err_t esp_netif_deinit(void)
  82. {
  83. ESP_LOGI(TAG, "loopback initialization");
  84. if (!s_netif_initialized) {
  85. ESP_LOGE(TAG, "esp-netif has not been initialized yet");
  86. return ESP_ERR_INVALID_SIZE;
  87. }
  88. s_netif_initialized = false;
  89. ESP_LOGD(TAG, "esp-netif has been successfully deinitialized");
  90. return ESP_OK;
  91. }
  92. static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_netif_config_t *cfg)
  93. {
  94. // Basic esp_netif and lwip is a mandatory configuration and cannot be updated after esp_netif_new()
  95. if (cfg == NULL || cfg->base == NULL || cfg->stack == NULL) {
  96. return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
  97. }
  98. // Configure general esp-netif properties
  99. memcpy(esp_netif->mac, cfg->base->mac, NETIF_MAX_HWADDR_LEN);
  100. if (cfg->base->ip_info == NULL) {
  101. ip4_addr_set_zero(&esp_netif->ip_info->ip);
  102. ip4_addr_set_zero(&esp_netif->ip_info->gw);
  103. ip4_addr_set_zero(&esp_netif->ip_info->netmask);
  104. } else {
  105. memcpy(esp_netif->ip_info, cfg->base->ip_info, sizeof(esp_netif_ip_info_t));
  106. }
  107. memcpy(esp_netif->ip_info_old, esp_netif->ip_info, sizeof(esp_netif_ip_info_t));
  108. // Setup main config parameters
  109. esp_netif->flags = cfg->base->flags;
  110. if (cfg->base->if_key) {
  111. esp_netif->if_key = strdup(cfg->base->if_key);
  112. }
  113. if (cfg->base->if_desc) {
  114. esp_netif->if_desc = strdup(cfg->base->if_desc);
  115. }
  116. if (cfg->base->route_prio) {
  117. esp_netif->route_prio = cfg->base->route_prio;
  118. }
  119. // Network stack is bypassed in loopback interface
  120. // Install IO functions only if provided -- connects driver and netif
  121. // this configuration could be updated after esp_netif_new(), typically in post_attach callback
  122. if (cfg->driver) {
  123. const esp_netif_driver_ifconfig_t *esp_netif_driver_config = cfg->driver;
  124. if (esp_netif_driver_config->handle) {
  125. esp_netif->driver_handle = esp_netif_driver_config->handle;
  126. }
  127. if (esp_netif_driver_config->transmit) {
  128. esp_netif->driver_transmit = esp_netif_driver_config->transmit;
  129. }
  130. if (esp_netif_driver_config->driver_free_rx_buffer) {
  131. esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer;
  132. }
  133. }
  134. return ESP_OK;
  135. }
  136. esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config)
  137. {
  138. // mandatory configuration must be provided when creating esp_netif object
  139. if (esp_netif_config == NULL) {
  140. return NULL;
  141. }
  142. // Create parent esp-netif object
  143. esp_netif_t *esp_netif = calloc(1, sizeof(struct esp_netif_obj));
  144. if (!esp_netif) {
  145. return NULL;
  146. }
  147. // Create ip info
  148. esp_netif_ip_info_t *ip_info = calloc(1, sizeof(esp_netif_ip_info_t));
  149. if (!ip_info) {
  150. free(esp_netif);
  151. return NULL;
  152. }
  153. esp_netif->ip_info = ip_info;
  154. // creating another ip info (to store old ip)
  155. ip_info = calloc(1, sizeof(esp_netif_ip_info_t));
  156. if (!ip_info) {
  157. free(esp_netif->ip_info);
  158. free(esp_netif);
  159. return NULL;
  160. }
  161. esp_netif->ip_info_old = ip_info;
  162. esp_netif_add_to_list(esp_netif);
  163. // Configure the created object with provided configuration
  164. esp_err_t ret = esp_netif_init_configuration(esp_netif, esp_netif_config);
  165. if (ret != ESP_OK) {
  166. ESP_LOGE(TAG, "Initial configuration of esp_netif failed with %d", ret);
  167. esp_netif_destroy(esp_netif);
  168. return NULL;
  169. }
  170. return esp_netif;
  171. }
  172. void esp_netif_destroy(esp_netif_t *esp_netif)
  173. {
  174. if (esp_netif) {
  175. esp_netif_remove_from_list(esp_netif);
  176. free(esp_netif->ip_info);
  177. free(esp_netif->ip_info_old);
  178. free(esp_netif->if_key);
  179. free(esp_netif->if_desc);
  180. free(esp_netif);
  181. }
  182. }
  183. esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle)
  184. {
  185. esp_netif_driver_base_t *base_driver = driver_handle;
  186. esp_netif->driver_handle = driver_handle;
  187. if (base_driver->post_attach) {
  188. esp_err_t ret = base_driver->post_attach(esp_netif, driver_handle);
  189. if (ret != ESP_OK) {
  190. ESP_LOGE(TAG, "Post-attach callback of driver(%p) failed with %d", driver_handle, ret);
  191. return ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED;
  192. }
  193. }
  194. return ESP_OK;
  195. }
  196. esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif,
  197. const esp_netif_driver_ifconfig_t *driver_config)
  198. {
  199. if (esp_netif == NULL || driver_config == NULL) {
  200. return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
  201. }
  202. esp_netif->driver_handle = driver_config->handle;
  203. esp_netif->driver_transmit = driver_config->transmit;
  204. esp_netif->driver_free_rx_buffer = driver_config->driver_free_rx_buffer;
  205. return ESP_OK;
  206. }
  207. esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[])
  208. {
  209. return ESP_ERR_NOT_SUPPORTED;
  210. }
  211. esp_err_t esp_netif_start(esp_netif_t *esp_netif)
  212. {
  213. ESP_LOGI(TAG, "Netif started");
  214. s_netif_started = true;
  215. return ESP_OK;
  216. }
  217. esp_err_t esp_netif_stop(esp_netif_t *esp_netif)
  218. {
  219. ESP_LOGI(TAG, "Netif stopped");
  220. s_netif_started = false;
  221. return ESP_OK;
  222. }
  223. //
  224. // IO translate functions
  225. //
  226. void esp_netif_free_rx_buffer(void *h, void* buffer)
  227. {
  228. esp_netif_t *esp_netif = h;
  229. esp_netif->driver_free_rx_buffer(esp_netif->driver_handle, buffer);
  230. }
  231. esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len)
  232. {
  233. ESP_LOGV(TAG, "Transmitting data: ptr:%p, size:%d", data, len);
  234. return (esp_netif->driver_transmit)(esp_netif->driver_handle, data, len);
  235. }
  236. esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb)
  237. {
  238. ESP_LOGV(TAG, "Received data: ptr:%p, size:%d", buffer, len);
  239. esp_netif_transmit(esp_netif, buffer, len);
  240. if (eb) {
  241. esp_netif_free_rx_buffer(esp_netif, eb);
  242. }
  243. return ESP_OK;
  244. }
  245. esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif)
  246. {
  247. return ESP_ERR_NOT_SUPPORTED;
  248. }
  249. esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif)
  250. {
  251. return ESP_ERR_NOT_SUPPORTED;
  252. }
  253. esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status)
  254. {
  255. return ESP_ERR_NOT_SUPPORTED;
  256. }
  257. esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status)
  258. {
  259. return ESP_ERR_NOT_SUPPORTED;
  260. }
  261. esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif)
  262. {
  263. return ESP_ERR_NOT_SUPPORTED;
  264. }
  265. esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif)
  266. {
  267. return ESP_ERR_NOT_SUPPORTED;
  268. }
  269. esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname)
  270. {
  271. return ESP_ERR_NOT_SUPPORTED;
  272. }
  273. esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname)
  274. {
  275. return ESP_ERR_NOT_SUPPORTED;
  276. }
  277. esp_err_t esp_netif_up(esp_netif_t *esp_netif)
  278. {
  279. ESP_LOGI(TAG, "Netif going up");
  280. s_netif_up = true;
  281. return ESP_OK;
  282. }
  283. esp_err_t esp_netif_down(esp_netif_t *esp_netif)
  284. {
  285. ESP_LOGI(TAG, "Netif going down");
  286. s_netif_up = false;
  287. return ESP_OK;
  288. }
  289. bool esp_netif_is_netif_up(esp_netif_t *esp_netif)
  290. {
  291. return s_netif_up;
  292. }
  293. esp_err_t esp_netif_get_old_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info)
  294. {
  295. ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif);
  296. if (esp_netif == NULL || ip_info == NULL) {
  297. return ESP_ERR_INVALID_ARG;
  298. }
  299. memcpy(ip_info, esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t));
  300. return ESP_OK;
  301. }
  302. esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info)
  303. {
  304. ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif);
  305. if (esp_netif == NULL || ip_info == NULL) {
  306. return ESP_ERR_INVALID_ARG;
  307. }
  308. memcpy(ip_info, esp_netif->ip_info, sizeof(esp_netif_ip_info_t));
  309. return ESP_OK;
  310. }
  311. bool esp_netif_is_valid_static_ip(esp_netif_ip_info_t *ip_info)
  312. {
  313. return true;
  314. }
  315. esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info)
  316. {
  317. ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif);
  318. if (esp_netif == NULL || ip_info == NULL) {
  319. return ESP_ERR_INVALID_ARG;
  320. }
  321. memcpy(esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t));
  322. return ESP_OK;
  323. }
  324. esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns)
  325. {
  326. return ESP_ERR_NOT_SUPPORTED;
  327. }
  328. esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns)
  329. {
  330. return ESP_ERR_NOT_SUPPORTED;
  331. }
  332. esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif_sta_list_t *netif_sta_list)
  333. {
  334. return ESP_ERR_NOT_SUPPORTED;
  335. }
  336. esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif)
  337. {
  338. return ESP_ERR_NOT_SUPPORTED;
  339. }
  340. esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6)
  341. {
  342. return ESP_ERR_NOT_SUPPORTED;
  343. }
  344. esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6)
  345. {
  346. return ESP_ERR_NOT_SUPPORTED;
  347. }
  348. esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif)
  349. {
  350. return esp_netif->flags;
  351. }
  352. const char *esp_netif_get_ifkey(esp_netif_t *esp_netif)
  353. {
  354. return esp_netif->if_key;
  355. }
  356. const char *esp_netif_get_desc(esp_netif_t *esp_netif)
  357. {
  358. return esp_netif->if_desc;
  359. }
  360. uint32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type)
  361. {
  362. return 0;
  363. }
  364. esp_err_t esp_netif_dhcps_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,
  365. uint32_t opt_len)
  366. {
  367. return ESP_ERR_NOT_SUPPORTED;
  368. }
  369. 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,
  370. uint32_t opt_len)
  371. {
  372. return ESP_ERR_NOT_SUPPORTED;
  373. }
  374. int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif)
  375. {
  376. return 0;
  377. }
  378. #endif /* CONFIG_ESP_NETIF_LOOPBACK */