查看: 2095|回复: 0

[经验] 中文版TI的TCP/IP协议栈--NDK

[复制链接]

该用户从未签到

发表于 2021-3-6 20:45:19 | 显示全部楼层 |阅读模式
分享到:
这是之前用TI的DM642做视频编码器用到的网络协议栈,源码TI官网上有的下载。维基网上也有关于NDK的一些技术文档,都是英文的,看了费劲。
看这个之前我对TCP/IP协议几乎不了解,拿到这个就开始看英文文档,天昏地暗的,边看边整理些东西,没基础真的痛苦,硬着头皮看吧。下面都是我边看边整理的,怕丢了,放到这,以后还有用。

       一、NDK中创建任务的方法:
       1、用标准的DSP/BIOS API
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">struct TSK_Attrs ta;</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta = TSK_ATTRS;</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.priority = OS_TASKPRINORM;</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.stack = 0;</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.stacksize = stacksize;</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.stackseg = 0;</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.environ = 0;</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.name = "TaskName";</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">ta.exitflag = 0;</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">hMyTask = TSK_create( (Fxn)entrypoint, &ta, arg1, arg2, arg3 );</span>
复制代码

      2、用NDK的任务抽象API
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">hMyTask = TaskCreate( entrypoint, "TaskName", OS_TASKPRINORM, stacksize, arg1, arg2, arg3 );</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">In both cases, hMyTask is a handle to a DSP/BIOS TSK task thread.</span>
复制代码


      二、内存分配
       应用程序在分配内存时最好使用标准的malloc()/free()函数,或者使用DSP/BIOS来分配。

       三、NDK初始化和配置
       1、必须包含NETCTRL.LIB,NETCTRL模块是协议栈初始化、配置和事件调度的核心。
       2、由DSP/BIOS创建的线程是程序的入口点,并且最终成为NETCTRL调度线程。这个控制线程直到协议栈关闭才返回给调用者。
       3、在调用其他任何协议栈API之前必须先调用NC_SystemOpen()函数。它初始化协议栈及其所需内存环境。它的两个参数Priority和OpMode分别决定调度任务的优先级和调度器何时开始执行。
       Priority包括NC_PRIORITY_LOW 和 NC_PRIORITY_HIGH两种,
       OpMode包括NC_OPMODE_POLLING 和 NC_OPMODE_INTERRUPT两种,大部分情况使用interrupt模式,而polling模式会持续运行,当使用polling模式时,优先级必须设为低(NC_PRIORITY_LOW)。
       4、使用实例:
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// THIS IS THE FIRST THING DONE IN AN APPLICATION!!</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT );</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( rc )</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">printf("NC_SystemOpen Failed (%d)/n",rc);</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">for(;;);</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码



       5、系统配置,包括以下参数:
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">· Network Hostname</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">· IP Address and Subnet Mask</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">· IP Address of Default Routes</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">· Services to be Executed (DHCP, DNS, HTTP, etc.)</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">· IP Address of name servers</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">· Stack Properties (IP routing, socket buffer size, ARP timeouts, etc.)</span>
复制代码



       系统配置开始时调用CfgNew()来创建配置句柄。
       配置好之后调用NC_NetStart()函数,该函数有4个参数,配置句柄,指向开始回调函数的指针,指向结束函数的指针,指向IP地址事件的函数。开始和结束函数都只被调用一次。开始函数在初始化结束准备执行网络应用程序时调用,结束函数在系统完全关闭时调用,意味着协议栈将不能执行网路应用。IP地址事件函数能够多次被调用。
       NC_NetStart()到系统关闭才返回一个关闭代码。
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Boot the system using our configuration</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// We keep booting until the function returns 0. This allows</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// us to have a "reboot" command.</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">do</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">rc = NC_NetStart( hCfg, NetworkStart, NetworkStop, NetworkIPAddr );</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">} while( rc > 0 );</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">As an example of a network start callback, the NetworkStart() function below opens a user SMTP server</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">application by calling an open function to create the main application thread.</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// NetworkStart</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  16. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// This function is called after the configuration has booted</span>
  17. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  18. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static SMTP_Handle hSMTP;</span>
  19. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static void NetworkStart( )</span>
  20. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  21. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Create an SMTP server</span>
  22. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">task hSMTP = SMTP_open( );</span>
  23. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  24. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  25. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// NetworkStop</span>
  26. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  27. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// This function is called when the network is shutting down</span>
  28. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  29. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static void NetworkStop()</span>
  30. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  31. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Close our SMTP server task</span>
  32. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">SMTP_close( hSMTP );</span>
  33. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码



       NetworkIPAddr()函数通常用来同步网络任务,该网络任务需要在执行前设置一个本地IP地址。
void NetIPCb( IPN IPAddr, uint IfIndex, uint fAdd );
    IPAddr            增加或者移除的IP地址
    IfIndex           外设接口获取或者移除IP地址的标识
    fAdd              增加一个IP地址时设为1,移除IP地址时设为0
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// NetworkIPAddr</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// This function is called whenever an IP address binding is</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// added or removed from the system.</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd )</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">IPN IPTmp;</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( fAdd )</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  printf("Network Added: ");</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">else</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  printf("Network Removed: ");</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Print a message</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">IPTmp = ntohl( IPAddr );</span>
  16. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">printf("If-%d:%d.%d.%d.%d/n", IfIdx,</span>
  17. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">          (UINT8)(IPTmp>>24) & 0xFF,</span>
  18. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">          (UINT8)(IPTmp>>16) & 0xFF,</span>
  19. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">          (UINT8)(IPTmp>>8) & 0xFF,</span>
  20. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">          (UINT8) IPTmp & 0xFF );</span>
  21. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码


      6、关闭协议栈的方法:
       ①手动关闭,NC_NetStop(1)重启网络栈,NC_NetStop(0)关闭网络栈。
       ②当检测到致命错误时关闭,NC_NetStop(-1)。
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// We do not want the stack to abort on any error禁止错误引起的关闭</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  uint rc = DBG_NONE;</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGABORTLEVEL,</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">               CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );</span>
复制代码



       7、追踪服务状态
       当使用NETTOOLS库时,NETTOOLS状态回调函数被引入,这个回调函数追踪被配置使能的服务的状态。状态回调函数有两级,第一个回调由NETTOOLS服务生成,当服务状态改变时它调用配置服务提供者。然后配置服务提供者增加它自己的状态到报告中,并且调用应用程序回调函数。当应用程序增加服务到系统配置中时,一个指向应用程序回调的指针被提供。
void StatusCallback( uint Item, uint Status, uint Code, HANDLE hCfgEntry )
Item         Item value of entry changed被更改的入口的项目值
Status       New status新状态
Code         Report code (if any)报告代码
hCfgEntry    Non-Referenced HANDLE to entry with status change不引用

       实例:
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Service Status Reports</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" };  //不能改变,在netcfg.h中定义</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static char *ReportStr[] = { "","Running","Updated","Complete","Fault" };  //不能改变,在nettools.h中定义</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static char *StatusStr[] = { "Disabled", "Waiting", "IPTerm", "Failed", "Enabled" }</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static void ServiceReport( uint Item, uint Status, uint Report, HANDLE h )</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">printf( "Service Status: %-9s: %-9s: %-9s: %03d/n",</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  TaskName[Item-1], StatusStr[Status],</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  ReportStr[Report/256], Report&0xFF );</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码



       以上函数打印的最后一个值是答应通过Report传递的低8位的值,这个值是固定的,大部分情况下这个值不需要。通常,如果服务成功,它报告Complete,失败,他报告Fault。对于那些不会结束的服务(例如,当IP分配启动时,DHCP客户端会持续运行),Report的高位字节意味着Running,而服务特定的低字节必须被用来指定当前状态。
      
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> For example, the status codes returned in the 8 least significant bits of Report when using the DHCP client service are:</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">DHCPCODE_IPADD           Client has added an IP address</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">DHCPCODE_IPREMOVE        IP address removed and CFG erased</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">DHCPCODE_IPRENEW         IP renewed, DHCP config space reset</span>
复制代码


       大部分情况下不必去核对这些状态报告代码,除非以下情况:
       当使用DHCP客户端来配置协议栈,DHCP客户端控制CFGTAG_SYSINFO标签空间的前256个入口。这些入口与这256个DHCP操作标签通信。应用程序可以检查DHCPCODE_IPADD或者DHCPCODE_IPRENEW返回代码以便它能够读或者改变通过DHCP客户端获得的信息。
       8、不使用DHCP client时,手动配置DNS的IP地址方法如下:
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">IPN IPTmp;</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Manually add the DNS server "128.114.12.2"</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">IPTmp = inet_addr("128.114.12.2");</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 );</span>
复制代码


      如果以上代码被加到使用DHCP的应用程序中,当DHCP执行状态更新时这个入口将会被清除。
       9、使用DHCP client时,手动配置DNS的IP地址方法如下:必须在DHCP配置完成以后再手动增加DNS服务。
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Service Status Reports</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" };</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static char *ReportStr[] = { "","Running","Updated","Complete","Fault" };</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static char *StatusStr[] = { "Disabled","Waiting","IPTerm", "Failed","Enabled" };</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static void ServiceReport( uint Item, uint Status, uint Report, HANDLE h )</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">printf( "Service Status: %-9s: %-9s: %-9s: %03d/n",</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  TaskName[Item-1], StatusStr[Status],</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  ReportStr[Report/256], Report&0xFF );</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Example of adding to the DHCP configuration space</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// When using the DHCP client, the client has full control over access</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// to the first 256 entries in the CFGTAG_SYSINFO space. Here, we want</span>
  16. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// to manually add a DNS server to the configuration, but we can only</span>
  17. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// do it once DHCP has finished its programming.</span>
  18. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  19. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( Item == CFGITEM_SERVICE_DHCPCLIENT &&</span>
  20. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  Status == CIS_SRV_STATUS_ENABLED &&</span>
  21. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  (Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPADD) ||</span>
  22. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPRENEW)) )</span>
  23. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  24. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  IPN IPTmp;</span>
  25. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  // Manually add the DNS server when specified. If the address</span>
  26. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  // string reads "0.0.0.0", IPTmp will be set to zero.</span>
  27. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  IPTmp = inet_addr(DNSServer);</span>

  28. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  if( IPTmp )</span>
  29. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   CfgAddEntry( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER,</span>
  30. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">     0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 );</span>
  31. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  32. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码

四、操作系统配置结构体和NDK配置结构体
       以上两个结构体的值可以直接赋值,但是有两个原因说明增加这个参数给系统配置是有用的:
       第一,它为所有的网络配置提供了固定的API。
       第二,如果使用了配置加载和保存功能,这些配置参数都被保存除了系统配置的其余部分。
       以下代码可以改变答应输出的调试信息的级别,例如,不打印出警告信息,而可以打印出调试信息:
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// We do not want to see debug messages less than WARNINGS</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">rc = DBG_WARN;</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL,</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );</span>
复制代码


      五、存储和加载配置
       1、配置设置好后,存储在非易失性存储器中。
  1. int CfgSave(HANDLE hCfg, int *pSize, UINT8 *pData);
复制代码


      返回值:正确返回被写的字节数,size错误返回0,操作错误返回小于1。
       描述:该函数将由hCfg指定的配置内容存储到pData指定的内存块。
       数据缓冲区的大小最初由pSize指定,如果这个指针指向的size值为0(pSize本身不能为NULL指针),这个函数不会试图存储配置,相反地,会计算需要的大小并且将这个值写到由pSize指定的位置。事实上,在任何时候pSize处的值都比存储配置所需的值要小,函数返回0值并且pSize处的值被用来设置存储数据所需的大小。参数pData指向接收配置信息的数据缓冲区。
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">int SaveConfig( HANDLE hCfg )</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">UINT8 *pBuf;</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">int size;</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// Get the required size to save the configuration</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">CfgSave( hCfg, &size, 0 );   //计算存储所需的大小并存储到pSize</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( size && (pBuf = malloc(size) ) )</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  CfgSave( hCfg, &size, pBuf );</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  MyMemorySave( pBuf, size );  //假设这个函数是将线性缓冲区存储到非易失性存储器</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  Free( pBuf );</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  return(1);</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">return(0);</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码


      2、加载配置
       实例如下:假设两个函数
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">MyMemorySize()返回线性buffer中的配置的存储大小</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">MyMemoryLoad()从flash中加载线性buffer</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">int NetworkTest()</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">int rc;</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">HANDLE hCfg;</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">UINT8 *pBuf;</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">Int size;</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 在应用程序中,这绝对是第一个必须被完成的!</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT );</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( rc )</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  printf("NC_SystemOpen Failed (%d)/n",rc);</span>
  16. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  for(;;);</span>
  17. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  18. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  19. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 首先加载装有配置信息的线性存储块</span>
  20. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  21. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 分配一个buffer用来装载配置信息</span>
  22. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">size = MyMemorySize();</span>
  23. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( !size )</span>
  24. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  goto main_exit;</span>
  25. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">pBuf = malloc( size );</span>
  26. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( !pBuf )</span>
  27. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  goto main_exit;</span>
  28. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 将配置信息从flash装载到buffer中</span>
  29. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">MyMemoryLoad( pBuf, size );</span>
  30. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  31. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 创建新配置并且加载配置信息</span>
  32. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  33. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 创建一个新配置</span>
  34. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">hCfg = CfgNew();</span>
  35. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( !hCfg )</span>
  36. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  37. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  printf("Unable to create configuration/n");</span>
  38. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  free( pBuf );</span>
  39. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  goto main_exit;</span>
  40. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  41. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 加载配置信息(然后我们可以释放buffer)</span>
  42. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">CfgLoad( hCfg, size, pBuf );</span>
  43. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">Free( pBuf );</span>
  44. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  45. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 用这个配置来启动这个系统</span>
  46. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  47. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// We keep booting until the function returns less than 1. This allows</span>
  48. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// us to have a "reboot" command.</span>
  49. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">//</span>
  50. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">do</span>
  51. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  52. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  rc = NC_NetStart( hCfg, NetworkStart, NetworkStop, NetworkIPAddr );</span>
  53. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">} while( rc > 0 );</span>
  54. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 删除配置</span>
  55. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">CfgFree( hCfg );</span>
  56. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">// 关闭操作系统</span>
  57. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">    main_exit:</span>
  58. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">NC_SystemmClose();</span>
  59. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">return(0);</span>
  60. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码



       六、ping NDK目标系统,以下代码例子配置IP重组最大的尺寸为65500个字节。
uint tmp = 65500;
CfgAddEntry(hCfg, CFGTAG_IP, CFGITEM_IP_IPREASMMAXSIZE,
  CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8*) &tmp, 0);

      七、发送和接收UDP数据包超过最大传输单元尺寸的方法:
       1、NDK配置操作:
CFGITEM_IP_SOCKUDPRXLIMIT
CFGITEM_IP_IPREASMMAXSIZE
       2、socket操作:
SO_SNDBUF
SO_RCVBUF

       3、操作系统适配层定义:
MMALLOC_MAXSIZE
MMALLOC_MAXSIZE


       例如:为了配置发送和接收的UDP数据包能达到65500字节的大小,一下代码必须被执行
       1、uint tmp = 65500;
  
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> CfgAddEntry(hCfg, CFGTAG_IP, CFGITEM_IP_IPREASMMAXSIZE,</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8*) &tmp, 0);</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   CfgAddEntry(hCfg, CFGTAG_IP, CFGITEM_IP_SOCKUDPRXLIMIT,</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8*) &tmp, 0);</span>
复制代码



       2、在"pbm.c"文件中修改MMALLOC_MAXSIZE参数,在"mem.c"文件中修改RAW_PAGE_SIZE参数,并且重新建立OSAL库。
       3、uint tmp = 65500;
  
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"=""> setsockopt(s, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(int) );</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">   setsockopt(s, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(int) );</span>
复制代码

      八、UDP数据报有效载荷时间戳
       NDK允许应用程序更新UDP数据包的有效载荷。常用的方法是更新数据包的时间戳信息。这样,发送端和接收端能更精确地调整依赖于改变系统特有的运行时间的传递延时。
       1、在传输端:
       在每个socket上,通过使用"setsockopt"函数,应用程序可以注册一个唤起函数。
       将数据包插入驱动的传输队列之前,协议栈调用这个唤起函数。
       在头部,唤起函数要更新UDP校验和信息。
       以下代码示意了怎样控制它:
void myTxTimestampFxn(UINT8 *pIpHdr) {
     ...
         }
setsockopt(s, SOL_SOCKET, SO_TXTIMESTAMP, (void*) myTxTimestampFxn, sizeof(void*));


       2、在接收端:
       在每个接口基础上,通过使用"EtherConfig"函数,应用程序可以注册一个唤起函数。EtherConfig函数在"netctrl.c"文件中的NC_NetStart()函数中设置。
这个唤起函数仅仅在处理包之前协议栈调度器调用。
       在头部,唤起函数要更新UDP校验和信息。
       以下代码示意了怎样控制它:
void myRcvTimestampFxn(UINT8 *pIpHdr) {
     ...
          }
EtherConfig( hEther, 1518, 14, 0, 6, 12, 4, myRcvTimestampFxn);

      九、调试信息
       包括DBG_INFO,DBG_WARN,DBG_ERROR。使用这些等级有两个目的:
       1,决定调试信息是否会被打印。
       2,决定调试信息是否会引起NDK关闭。
       DBG_ERROR这一层的信息会引起栈的关闭。可以通过系统配置和使用操作系统配置结构来调整这个行为。

       十、存储器出错
       当诊断NDK调试信息时,字存储器出错会频繁发生。这是因为对于缓冲设备很容易造成存储器出错。包含在NDK中的大部分示例程序都是用全L2缓冲模式。在这种模式下,任何对CPU内部存储边界的读写访问都会引起缓冲区出错,从而引起存储器出错。因为内部存储(L2)边界从地址0x00000000开始,当使用全缓冲时,一个空指针会导致问题。
       当L2使用cache+RAM模式时,对于地址0x00000000的读写不会引起缓冲错误。

       十一、程序死锁
       大部分程序死锁都是由于任务堆栈空间不足引起的。例如,当我们写一个HTTP CGI函数时,CGI函数的任务线程可能总共只需要5000字节的任务堆栈空间。因此,使用过大的堆栈是不被推荐的。
       一般来说,不使用下面的源码:
  1. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">myTask()</span>
  2. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  3. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">char TempBuffer[2000];</span>
  4. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">myFun( TempBuffer );</span>
  5. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  6. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">而是这样使用:</span>
  7. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">myTask()</span>
  8. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  9. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">char *pTempBuf;</span>
  10. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">pTempBuf = MEM_alloc( 0, 2000, 0);</span>
  11. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">if( pTempBuf != MEM_ILLEGAL )</span>
  12. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</span>
  13. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  myFun( pTempBuf );</span>
  14. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">  MEM_free( pTempBuf, 2000 );</span>
  15. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
  16. <span style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">}</span>
复制代码



       如果调用一个内存分配函数速度太快,可以考虑使用外部buffer。这仅仅是个例子,几乎不要事先考虑就能排除所有可能的堆栈溢出情况,并且消除可能的程序死锁。

       十二、内存管理报告
mmAlloc()和mmFree():分配/释放小的内存块
mmBulkAlloc()和mmBulkFree():分配/释放较大(不受限制的,通常在3000bytes以上)内存块
48:48 ( 75%)   18:96 ( 56%)   8:128 ( 33%)   28:256 ( 77%)   1:512 ( 16%)   0:1536   0:3072
(21504/46080 mmAlloc: 61347036/0/61346947, mmBulk: 25/0/17)
18:96 ( 56%):内存管理器的页的大小是3072 bytes,至多被分成18块*96字节,使用了一页的56%。
mmAlloc: 61347036/0/61346947  :调用了mmAlloc()函数61347036次,失败了0次,调用mmFree()函数61346947次。在任何时候,调用mmAlloc()的次数 + 失败的次数 = 调用mmFree()的次数 + 应该分配而未分配的次数。假如在最终的报告中有 mmAlloc:n1/n2/n3,n1+n2应该等于n3,如果不等,就有内存泄露。


       十三、NC_NetStart()函数流程
NC_NetStart()
{
设备初始化;
创建配置启动线程;
网络调度器(NetScheduler(););
关闭配置;
关闭设备;
}


回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /1 下一条



手机版|小黑屋|与非网

GMT+8, 2024-4-25 09:45 , Processed in 0.112875 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.