esp_netif_loopback.c 13 KB

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