luat_netdrv.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "luat_base.h"
  2. #include "luat_netdrv.h"
  3. #include "luat_network_adapter.h"
  4. #ifdef __LUATOS__
  5. // #include "luat_netdrv_ch390h.h"
  6. // #include "luat_netdrv_uart.h"
  7. #endif
  8. #include "luat_mem.h"
  9. #ifdef LUAT_USE_AIRLINK
  10. #include "luat_airlink.h"
  11. #endif
  12. #define LUAT_LOG_TAG "netdrv"
  13. #include "luat_log.h"
  14. static luat_netdrv_t* drvs[NW_ADAPTER_QTY];
  15. luat_netdrv_t* luat_netdrv_ch390h_setup(luat_netdrv_conf_t *conf);
  16. luat_netdrv_t* luat_netdrv_uart_setup(luat_netdrv_conf_t *conf);
  17. luat_netdrv_t* luat_netdrv_whale_setup(luat_netdrv_conf_t *conf);
  18. luat_netdrv_t* luat_netdrv_setup(luat_netdrv_conf_t *conf) {
  19. if (conf->id < 0 || conf->id >= NW_ADAPTER_QTY) {
  20. return NULL;
  21. }
  22. if (drvs[conf->id] == NULL) {
  23. // 注册新的设备?
  24. #ifdef __LUATOS__
  25. if (conf->impl == 1) { // CH390H
  26. drvs[conf->id] = luat_netdrv_ch390h_setup(conf);
  27. return drvs[conf->id];
  28. }
  29. if (conf->impl == 16) { // UART
  30. drvs[conf->id] = luat_netdrv_uart_setup(conf);
  31. return drvs[conf->id];
  32. }
  33. if (conf->impl == 64) { // WHALE
  34. drvs[conf->id] = luat_netdrv_whale_setup(conf);
  35. return drvs[conf->id];
  36. }
  37. #endif
  38. }
  39. else {
  40. if (drvs[conf->id]->boot) {
  41. //LLOGD("启动网络设备 %p", drvs[conf->id]);
  42. drvs[conf->id]->boot(drvs[conf->id], NULL);
  43. return drvs[conf->id];
  44. }
  45. }
  46. LLOGW("无效的注册id或类型 id=%d, impl=%d", conf->id, conf->impl);
  47. return NULL;
  48. }
  49. int luat_netdrv_dhcp(int32_t id, int32_t enable) {
  50. if (id < 0 || id >= NW_ADAPTER_QTY) {
  51. return -1;
  52. }
  53. if (drvs[id] == NULL) {
  54. return -1;
  55. }
  56. if (drvs[id]->dhcp == NULL) {
  57. LLOGW("该netdrv不支持设置dhcp开关");
  58. return -1;
  59. }
  60. return drvs[id]->dhcp(drvs[id], drvs[id]->userdata, enable);
  61. }
  62. int luat_netdrv_ready(int32_t id) {
  63. if (drvs[id] == NULL) {
  64. return -1;
  65. }
  66. return drvs[id]->ready(drvs[id], drvs[id]->userdata);
  67. }
  68. int luat_netdrv_register(int32_t id, luat_netdrv_t* drv) {
  69. if (drvs[id] != NULL) {
  70. return -1;
  71. }
  72. drvs[id] = drv;
  73. return 0;
  74. }
  75. int luat_netdrv_mac(int32_t id, const char* new, char* old) {
  76. if (id < 0 || id >= NW_ADAPTER_QTY) {
  77. return -1;
  78. }
  79. if (drvs[id] == NULL || drvs[id]->netif == NULL) {
  80. return -1;
  81. }
  82. memcpy(old, drvs[id]->netif->hwaddr, 6);
  83. if (new) {
  84. memcpy(drvs[id]->netif->hwaddr, new, 6);
  85. }
  86. return 0;
  87. }
  88. void luat_netdrv_stat_inc(luat_netdrv_statics_item_t* stat, size_t len) {
  89. stat->bytes += len;
  90. stat->counter ++;
  91. }
  92. luat_netdrv_t* luat_netdrv_get(int id) {
  93. if (id < 0 || id >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
  94. return NULL;
  95. }
  96. return drvs[id];
  97. }
  98. static char tmpbuff[1024];
  99. void luat_netdrv_print_pkg(const char* tag, uint8_t* buff, size_t len) {
  100. if (len > 511) {
  101. len = 511;
  102. }
  103. memset(tmpbuff, 0, 1024);
  104. for (size_t i = 0; i < len; i++)
  105. {
  106. sprintf(tmpbuff + i * 2, "%02X", buff[i]);
  107. }
  108. LLOGD("%s %s", tag, tmpbuff);
  109. }
  110. #define NAPT_CHKSUM_16BIT_LEN sizeof(uint16_t)
  111. uint32_t alg_hdr_16bitsum(const uint16_t *buff, uint16_t len)
  112. {
  113. uint32_t sum = 0;
  114. uint16_t *pos = (uint16_t *)buff;
  115. uint16_t remainder_size = len;
  116. while (remainder_size > 1)
  117. {
  118. sum += *pos ++;
  119. remainder_size -= NAPT_CHKSUM_16BIT_LEN;
  120. }
  121. if (remainder_size > 0)
  122. {
  123. sum += *(uint8_t*)pos;
  124. }
  125. return sum;
  126. }
  127. uint16_t alg_iphdr_chksum(const uint16_t *buff, uint16_t len)
  128. {
  129. uint32_t sum = alg_hdr_16bitsum(buff, len);
  130. sum = (sum >> 16) + (sum & 0xFFFF);
  131. sum += (sum >> 16);
  132. return (uint16_t)(~sum);
  133. }
  134. uint16_t alg_tcpudphdr_chksum(uint32_t src_addr, uint32_t dst_addr, uint8_t proto, const uint16_t *buff, uint16_t len)
  135. {
  136. uint32_t sum = 0;
  137. sum += (src_addr & 0xffffUL);
  138. sum += ((src_addr >> 16) & 0xffffUL);
  139. sum += (dst_addr & 0xffffUL);
  140. sum += ((dst_addr >> 16) & 0xffffUL);
  141. sum += (uint32_t)htons((uint16_t)proto);
  142. sum += (uint32_t)htons(len);
  143. sum += alg_hdr_16bitsum(buff, len);
  144. sum = (sum >> 16) + (sum & 0xFFFF);
  145. sum += (sum >> 16);
  146. return (uint16_t)(~sum);
  147. }
  148. void luat_netdrv_netif_input(void* args) {
  149. netdrv_pkg_msg_t* ptr = (netdrv_pkg_msg_t*)args;
  150. struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, ptr->len, PBUF_RAM);
  151. if (p == NULL) {
  152. LLOGD("分配pbuf失败!!! %d", ptr->len);
  153. luat_heap_free(ptr);
  154. return;
  155. }
  156. pbuf_take(p, ptr->buff, ptr->len);
  157. // luat_airlink_hexdump("收到IP数据,注入到netif", ptr->buff, ptr->len);
  158. int ret = ptr->netif->input(p, ptr->netif);
  159. if (ret) {
  160. LLOGW("netif->input ret %d", ret);
  161. pbuf_free(p);
  162. }
  163. luat_heap_free(ptr);
  164. }
  165. int luat_netdrv_netif_input_proxy(struct netif * netif, uint8_t* buff, uint16_t len) {
  166. netdrv_pkg_msg_t* ptr = luat_heap_malloc(sizeof(netdrv_pkg_msg_t) + len);
  167. if (ptr == NULL) {
  168. LLOGE("收到rx数据,但内存已满, 无法处理只能抛弃 %d", len - 4);
  169. return 1; // 需要处理下一个包
  170. }
  171. memcpy(ptr->buff, buff, len);
  172. ptr->netif = netif;
  173. ptr->len = len;
  174. int ret = tcpip_callback(luat_netdrv_netif_input, ptr);
  175. if (ret) {
  176. luat_heap_free(ptr);
  177. LLOGE("tcpip_callback 返回错误!!! ret %d", ret);
  178. return 1;
  179. }
  180. return 0;
  181. }