NetworkInterface.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /***********************************************************************************************************************
  2. * Copyright (C) 2021 Arkmicro Corporation. All rights reserved.
  3. ***********************************************************************************************************************/
  4. /***********************************************************************************************************************
  5. * File Name : NetworkInterface.c
  6. * Device(s) : RTL8189FTV
  7. * Description : Interfaces FreeRTOS TCP/IP stack to RX Ethernet driver.
  8. ***********************************************************************************************************************/
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. /* FreeRTOS includes. */
  14. #include "FreeRTOS.h"
  15. #include "task.h"
  16. #include "FreeRTOS_IP.h"
  17. #include "FreeRTOS_IP_Private.h"
  18. /*#include "FreeRTOS_DNS.h" */
  19. #include "NetworkBufferManagement.h"
  20. #include "NetworkInterface.h"
  21. #include "FreeRTOS_ARP.h"
  22. #include "iot_wifi.h"
  23. #include "board.h"
  24. //#define USE_AP 0
  25. //#define DUMP_NETWORK_DATA
  26. //#undef DUMP_NETWORK_DATA
  27. #ifdef WIFI_SUPPORT
  28. #if CONFIG_USB_DEVICE_CDC_NCM || !USE_LWIP
  29. #error "Do not choose USE_USB_DEVICE when use WIFI_SUPPORT"
  30. #endif
  31. #endif
  32. typedef enum
  33. {
  34. eMACInit, /* Must initialise MAC. */
  35. eMACPass, /* Initialisation was successful. */
  36. eMACFailed, /* Initialisation failed. */
  37. } eMAC_INIT_STATUS_TYPE;
  38. static eMAC_INIT_STATUS_TYPE xMacInitStatus = eMACInit;
  39. extern void cmd_test(const char* temp_uart_buf);
  40. static int InitializeNetwork( void );
  41. int g_ncm_register(const char *name);
  42. /***********************************************************************************************************************
  43. * Function Name: xNetworkInterfaceInitialise ()
  44. * Description : Initialization of Ethernet driver.
  45. * Arguments : none
  46. * Return Value : pdPASS, pdFAIL
  47. **********************************************************************************************************************/
  48. BaseType_t xNetworkInterfaceInitialise( void )
  49. {
  50. BaseType_t xReturn;
  51. if( xMacInitStatus == eMACInit )
  52. {
  53. //rltk_wlan_set_netif_info(0, NULL, "00:0c:29:5d:2e:05");
  54. /*
  55. * Perform the hardware specific network initialization here using the Ethernet driver library to initialize the
  56. * Ethernet hardware, initialize DMA descriptors, and perform a PHY auto-negotiation to obtain a network link.
  57. *
  58. * InitialiseNetwork() uses Ethernet peripheral driver library function, and returns 0 if the initialization fails.
  59. */
  60. if( InitializeNetwork() == pdFALSE )
  61. {
  62. xMacInitStatus = eMACFailed;
  63. }
  64. else
  65. {
  66. /* Indicate that the MAC initialisation succeeded. */
  67. xMacInitStatus = eMACPass;
  68. }
  69. FreeRTOS_printf( ( "InitializeNetwork returns %s\n", ( xMacInitStatus == eMACPass ) ? "OK" : " Fail" ) );
  70. }
  71. if( xMacInitStatus == eMACPass )
  72. {
  73. xReturn = pdPASS;
  74. }
  75. else
  76. {
  77. xReturn = pdFAIL;
  78. }
  79. FreeRTOS_printf( ( "xNetworkInterfaceInitialise returns %d\n", xReturn ) );
  80. return xReturn;
  81. } /* End of function xNetworkInterfaceInitialise() */
  82. /***********************************************************************************************************************
  83. * Function Name: xNetworkInterfaceOutput ()
  84. * Description : Simple network output interface.
  85. * Arguments : pxDescriptor, xReleaseAfterSend
  86. * Return Value : pdTRUE, pdFALSE
  87. **********************************************************************************************************************/
  88. #if CONFIG_USB_DEVICE_CDC_NCM
  89. void gether_send(NetworkBufferDescriptor_t * const pxDescriptor);
  90. #endif
  91. BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
  92. BaseType_t xReleaseAfterSend )
  93. {
  94. BaseType_t xReturn = pdFALSE;
  95. #ifdef DUMP_NETWORK_DATA
  96. if (1) {
  97. int i;
  98. char* tmp = (char *)pxDescriptor->pucEthernetBuffer;
  99. printf("\r\nsend len:%d--> ", pxDescriptor->xDataLength);
  100. for (i = 0; i < pxDescriptor->xDataLength; i++) {
  101. printf("%02x ", tmp[i]);
  102. }printf("send end\r\n");
  103. }
  104. #endif
  105. #ifdef WIFI_SUPPORT
  106. xReturn = (BaseType_t)wifi_send_network_data((const char *)pxDescriptor->pucEthernetBuffer, (int)pxDescriptor->xDataLength);
  107. if (-1 == xReturn) {
  108. goto exit;
  109. }
  110. #endif
  111. xReturn = pdTRUE;
  112. #if CONFIG_USB_DEVICE_CDC_NCM
  113. NetworkBufferDescriptor_t *pxDescriptor_eth = pxDescriptor;
  114. if (!xReleaseAfterSend) {
  115. pxDescriptor_eth = pxDuplicateNetworkBufferWithDescriptor(pxDescriptor, pxDescriptor->xDataLength);
  116. xReleaseAfterSend = pdFALSE;
  117. } else {
  118. xReleaseAfterSend = pdFALSE;//release at usb net driver
  119. }
  120. //print_hex(pxDescriptor->pucEthernetBuffer, pxDescriptor->xDataLength, NULL);
  121. gether_send(pxDescriptor_eth);
  122. #else
  123. #ifdef WIFI_SUPPORT
  124. exit:
  125. #endif
  126. #endif
  127. if( xReleaseAfterSend != pdFALSE )
  128. {
  129. /* It is assumed SendData() copies the data out of the FreeRTOS+TCP Ethernet
  130. * buffer. The Ethernet buffer is therefore no longer needed, and must be
  131. * freed for re-use. */
  132. vReleaseNetworkBufferAndDescriptor( pxDescriptor );
  133. }
  134. return xReturn;
  135. } /* End of function xNetworkInterfaceOutput() */
  136. /***********************************************************************************************************************
  137. * Function Name: vNetworkInterfaceAllocateRAMToBuffers ()
  138. * Description : .
  139. * Arguments : pxNetworkBuffers
  140. * Return Value : none
  141. **********************************************************************************************************************/
  142. void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
  143. {
  144. } /* End of function vNetworkInterfaceAllocateRAMToBuffers() */
  145. /***********************************************************************************************************************
  146. * Function Name: InitializeNetwork ()
  147. * Description :
  148. * Arguments : none
  149. * Return Value : pdTRUE, pdFALSE
  150. **********************************************************************************************************************/
  151. //#include "wifi_structures.h"
  152. #if 0
  153. static rtw_result_t ark_rtw_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
  154. {
  155. char bssid[32] = {0};
  156. char *ptr = (char *)malloced_scan_result->ap_details.BSSID.octet;
  157. sprintf(bssid, "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
  158. printf("\r\nSSID:%s Bssid:%s Signal strength:%d DB\r\n", malloced_scan_result->ap_details.SSID.val, bssid,
  159. malloced_scan_result->ap_details.signal_strength);
  160. if (malloced_scan_result->scan_complete != 0) {
  161. printf("scan complete!\r\n");
  162. scan_comp_flag = 1;
  163. }
  164. return 0;
  165. }
  166. #endif
  167. static int InitializeNetwork( void )
  168. {
  169. BaseType_t return_code = pdTRUE;
  170. #if CONFIG_USB_DEVICE_CDC_NCM
  171. g_ncm_register("ncm");
  172. #endif
  173. return return_code;
  174. } /* End of function InitializeNetwork() */
  175. /***********************************************************************************************************************
  176. * End of file "NetworkInterface.c"
  177. **********************************************************************************************************************/
  178. static UBaseType_t ulNextRand;
  179. BaseType_t xTotalSuccess = 0;
  180. UBaseType_t uxRand( void )
  181. {
  182. const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
  183. ulNextRand = portGET_RUN_TIME_COUNTER_VALUE();
  184. ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
  185. return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );
  186. }
  187. extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
  188. uint16_t usSourcePort,
  189. uint32_t ulDestinationAddress,
  190. uint16_t usDestinationPort )
  191. {
  192. ( void ) ulSourceAddress;
  193. ( void ) usSourcePort;
  194. ( void ) ulDestinationAddress;
  195. ( void ) usDestinationPort;
  196. return uxRand();
  197. }
  198. BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber )
  199. {
  200. *( pulNumber ) = uxRand();
  201. return pdTRUE;
  202. }
  203. #if 0
  204. void vApplicationPingReplyHook( ePingReplyStatus_t eStatus,
  205. uint16_t usIdentifier )
  206. {
  207. //if( eStatus == eSuccess )
  208. {
  209. FreeRTOS_printf( ( "Ping response received. ID: %d\r\n", usIdentifier ) );
  210. printf("Ping response received. ID: %d\r\n", usIdentifier);
  211. /* Increment successful ping replies. */
  212. xTotalSuccess++;
  213. }
  214. }
  215. #endif