net_104.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: net_104.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2012-11-07
  7. 功能说明: 网络104接口
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #ifdef CPU_FUXI
  12. /*------------------------------- 头文件 --------------------------------------
  13. */
  14. #include "head.h"
  15. #include <pthread.h>
  16. #include <errno.h>
  17. #include <sys/prctl.h>
  18. /*------------------------------- 宏定义 --------------------------------------
  19. */
  20. #define NET_104_BUF_SIZE 16384
  21. /*------------------------------ 类型结构 -------------------------------------
  22. */
  23. /*------------------------------ 全局变量 -------------------------------------
  24. */
  25. // static struct task_struct * g_ts_104[IEC104_TOTAL_SOCKETS];
  26. static pthread_t g_ts_104[IEC104_TOTAL_SOCKETS] = {0x00};
  27. static SOCKET g_104_client[IEC104_TOTAL_SOCKETS];
  28. static unsigned char g_net_104_buf_frame[IEC104_TOTAL_SOCKETS][NET_RECV_MAX]; // 以太网接收缓冲区
  29. static unsigned char g_net_104_buf[IEC104_TOTAL_SOCKETS][NET_104_BUF_SIZE]; // 以太网接收缓冲区
  30. static struct rt_fifo g_net_104_fifo[IEC104_TOTAL_SOCKETS];
  31. static volatile int g_net_reinit[CFG_ETH_MAX_LOGIC]={NET_SOCKET_INIT};//网络重新初始化标志.0表示不用重新初始化,1表示需要重新初始化
  32. // static struct semaphore net_r_lock[CFG_ETH_MAX_LOGIC]; //接收锁
  33. // static struct semaphore net_s_lock[CFG_ETH_MAX_LOGIC]; //发送锁
  34. static pthread_mutex_t net_r_lock[CFG_ETH_MAX_LOGIC]; //接收锁
  35. static pthread_mutex_t net_s_lock[CFG_ETH_MAX_LOGIC]; //发送锁
  36. static int g_net_104_close_flag[CFG_ETH_MAX_LOGIC];
  37. static int g_net_104_exit_flag[CFG_ETH_MAX_LOGIC];
  38. // static struct completion g_exit_completion_net_104[CFG_ETH_MAX_LOGIC];
  39. extern u32 g_ip_net_maintain; // 104维护工具调试客户端地址
  40. /*------------------------------ 函数声明 -------------------------------------
  41. */
  42. int _net_104_thread(IEC104_DEF *pt104);
  43. //设置socket关闭标志并等待socket关闭完成
  44. void net_104_set_sock_closing(int no)
  45. {
  46. //if(no >= g_net_num) return;
  47. g_net_reinit[no] = NET_SOCKET_CLOSING; //置关闭标志
  48. if(!tRunPara.b104Client)
  49. {
  50. int count=0;
  51. //等待关闭完成
  52. while(g_net_reinit[no] != NET_SOCKET_CLOSED)
  53. {
  54. msleep(20);
  55. if(++count>150)
  56. {
  57. rt_printf("104 client socket close fail!!!\r\n");
  58. return;
  59. }
  60. }
  61. }
  62. }
  63. //设置socket重新创建标志
  64. void net_104_set_sock_initflag(int no)
  65. {
  66. // if(no >= g_net_num) return;
  67. if(g_net_reinit[no] == NET_SOCKET_CLOSED)
  68. {
  69. g_net_reinit[no] = NET_SOCKET_REINIT;
  70. }
  71. }
  72. /**************************************************************************
  73. 函数名称:Net_Init
  74. 函数版本:1.00
  75. 作者:
  76. 创建日期:2009.1.8
  77. 函数功能说明:以太网初始化
  78. 输入参数:
  79. 输出参数:
  80. 返回值:
  81. 更新信息:
  82. 更新日志1:
  83. 日期:
  84. 修改者:
  85. 修改内容:
  86. 修改原因:
  87. ***************************************************************************/
  88. void Net_Init(void)
  89. {
  90. unsigned char mac[6];
  91. unsigned char ip[4];
  92. unsigned char mask[4];
  93. unsigned char gw[4];
  94. unsigned char routenet[4];
  95. unsigned char routenetmask[4];
  96. unsigned char routegate[4];
  97. //char * acteth;
  98. int i,j;
  99. //char tmp[16];
  100. int ret = 0;
  101. int gateflag = 0;
  102. if(tRunPara.w104K == 0)
  103. {
  104. if(pRunSet->bTT_SoeResumeComm || pRunSet->bTT_SoeResumePower)
  105. {
  106. rt_printf("定值参数错误:SOE通讯续传、SOE上电续传时参数104 K值不能为0.\r\n");
  107. rt_err_set(ERR_CODE_INIT_SOFTWARE,0);
  108. }
  109. }
  110. #if 0
  111. int debug_port;
  112. acteth = env_get_info(ENV_ETHACT);
  113. if(acteth != NULL)
  114. {
  115. if(strcmp(acteth,"eth0") == 0)
  116. {
  117. debug_port = 0;
  118. }
  119. else if(strcmp(acteth,"eth1") == 0)
  120. {
  121. debug_port = 1;
  122. }
  123. else if(strcmp(acteth,"eth2") == 0)
  124. {
  125. debug_port = 2;
  126. }
  127. else
  128. {
  129. debug_port = 2;
  130. }
  131. }
  132. else
  133. {
  134. debug_port = 2;
  135. }
  136. #endif
  137. //for(i=0;i<g_net_num;i++)
  138. for(i=0;i<CFG_ETH_MAX_LOGIC;i++)
  139. {
  140. //没有改变参数,不用初始化
  141. if(!tRunPara.tNetPara[i].bInit)
  142. {
  143. //104配置参数有修改,需关闭socket重新初始化
  144. if(tRunPara.sock_104_flag)
  145. {
  146. net_104_set_sock_initflag(i);
  147. }
  148. continue;
  149. }
  150. gateflag = 1;
  151. tRunPara.tNetPara[i].bInit=false;
  152. // 设置接口MAC地址
  153. //if(rt_if_sz2mac(env_get_info(ENV_NET1MAC+i),mac)< 0)
  154. mac[0] = 'H';
  155. mac[1] = 'G';
  156. mac[2] = (BYTE)((tRunPara.tNetPara[i].wIp>>24)&0xff);
  157. mac[3] = (BYTE)((tRunPara.tNetPara[i].wIp>>16)&0xff);
  158. mac[4] = (BYTE)((tRunPara.tNetPara[i].wIp>>8)&0xff);
  159. mac[5] = (BYTE)((tRunPara.tNetPara[i].wIp>>0)&0xff);
  160. // 设置IP
  161. memset(ip,0,sizeof(ip));
  162. ip[3] = (BYTE)((tRunPara.tNetPara[i].wIp>>0)&0xff);//低
  163. ip[2] = (BYTE)((tRunPara.tNetPara[i].wIp>>8)&0xff);
  164. ip[1] = (BYTE)((tRunPara.tNetPara[i].wIp>>16)&0xff);
  165. ip[0] = (BYTE)((tRunPara.tNetPara[i].wIp>>24)&0xff);//高 192
  166. // 设置MASK
  167. memset(mask,0,sizeof(mask));
  168. mask[3] = (BYTE)((tRunPara.tNetPara[i].wMask>>0)&0xff);
  169. mask[2] = (BYTE)((tRunPara.tNetPara[i].wMask>>8)&0xff);
  170. mask[1] = (BYTE)((tRunPara.tNetPara[i].wMask>>16)&0xff);
  171. mask[0] = (BYTE)((tRunPara.tNetPara[i].wMask>>24)&0xff);
  172. //检查是否需要关闭虚拟网卡
  173. if((int)tRunPara.tNetPara[i].old_index > 0)
  174. {
  175. rt_if_down2(tRunPara.tNetPara[i].old_ethid, tRunPara.tNetPara[i].old_index);
  176. }
  177. //获取配置相同的ethid有多少个
  178. ret=get_ethcnt(i, tRunPara.tNetPara[i].ethid);
  179. //没有的话,使用物理的
  180. if(ret==0)
  181. {
  182. // 设置网络参数
  183. net_if_set((int)tRunPara.tNetPara[i].ethid,mac,ip, mask);
  184. tRunPara.tNetPara[i].index = 0;
  185. //rt_printf_time("ret = %d, ethID = %d\r\n",ret,tRunPara.tNetPara[i].ethid);
  186. }
  187. else //有的话使用虚拟的
  188. {
  189. #if (1)
  190. printf("[%s:%d]\r\n", __func__, __LINE__);
  191. net_if_set2((int)tRunPara.tNetPara[i].ethid, ret+1, 0, ip, mask);
  192. #else
  193. net_if_set2((int)tRunPara.tNetPara[i].ethid, i+1, 0, ip, mask);
  194. #endif
  195. tRunPara.tNetPara[i].index = i+1;
  196. //rt_printf_time("Xnet = %d,ip=%x, ethID = %d, ret = %d\r\n",tRunPara.tNetPara[i].index,ip[3],tRunPara.tNetPara[i].ethid,ret);
  197. }
  198. tRunPara.tNetPara[i].old_ethid= tRunPara.tNetPara[i].ethid;
  199. tRunPara.tNetPara[i].old_index= tRunPara.tNetPara[i].index;
  200. #if 0
  201. // 如果是调试串口,不初始化
  202. if(i == debug_port)
  203. //if(i==(CFG_ETH_MAX_LOGIC-1))
  204. {
  205. //设置IP环境变量
  206. if(tRunPara.tNetPara[i].changed & 0x01)
  207. {
  208. sprintf(tmp, "%lu.%lu.%lu.%lu",( tRunPara.tNetPara[i].wIp>>24)&0xff, (tRunPara.tNetPara[i].wIp>>16)&0xff, (tRunPara.tNetPara[i].wIp>>8)&0xff, (tRunPara.tNetPara[i].wIp&0xff));
  209. env_setenv("ipaddr", tmp);
  210. }
  211. //设置Gate环境变量
  212. if(tRunPara.tNetPara[i].changed & 0x02)
  213. {
  214. sprintf(tmp, "%lu.%lu.%lu.%lu", ( tRunPara.tNetPara[i].wGate>>24)&0xff, (tRunPara.tNetPara[i].wGate>>16)&0xff, (tRunPara.tNetPara[i].wGate>>8)&0xff, (tRunPara.tNetPara[i].wGate&0xff));
  215. if(tRunPara.tNetPara[i].wGate)
  216. env_setenv("gatewayip", tmp);
  217. }
  218. //设置Mask环境变量
  219. if(tRunPara.tNetPara[i].changed & 0x04)
  220. {
  221. sprintf(tmp, "%lu.%lu.%lu.%lu", ( tRunPara.tNetPara[i].wMask>>24)&0xff, (tRunPara.tNetPara[i].wMask>>16)&0xff, (tRunPara.tNetPara[i].wMask>>8)&0xff, (tRunPara.tNetPara[i].wMask&0xff));
  222. env_setenv("netmask", tmp);
  223. }
  224. //设置ethact环境变量
  225. if(tRunPara.tNetPara[i].changed & 0x40)
  226. {
  227. if(tRunPara.tNetPara[i].ethid >= 2)
  228. {
  229. sprintf(tmp, "%s", "eth2");
  230. }
  231. else
  232. {
  233. sprintf(tmp, "eth%d", (int)tRunPara.tNetPara[i].ethid);
  234. }
  235. env_setenv("ethact", tmp);
  236. }
  237. //continue;
  238. }
  239. #endif
  240. //网段
  241. routenet[0] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>24)&0xff);
  242. routenet[1] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>16)&0xff);
  243. routenet[2] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>8)&0xff);
  244. routenet[3] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>0)&0xff);
  245. //网段掩码
  246. routenetmask[0] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>24)&0xff);
  247. routenetmask[1] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>16)&0xff);
  248. routenetmask[2] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>8)&0xff);
  249. routenetmask[3] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>0)&0xff);
  250. //网关
  251. routegate[0] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>24)&0xff);
  252. routegate[1] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>16)&0xff);
  253. routegate[2] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>8)&0xff);
  254. routegate[3] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>0)&0xff);
  255. net_route_set(routenet, routenetmask, routegate);
  256. //g_net_reinit[i] = 1;//置重新初始化标志
  257. net_104_set_sock_initflag(i);
  258. net_debug_set_sock_initflag();
  259. net_maintain_set_sock_initflag();
  260. #if 0
  261. // 得到并打印新的网络参数
  262. // net_if_get(i,mac,ip,mask);
  263. rt_printf("new mac: %02x:%02x:%02x:%02x:%02x:%02x\r\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
  264. rt_printf("new ip:%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  265. rt_printf("new mask:%d.%d.%d.%d\r\n",mask[0],mask[1],mask[2],mask[3]);
  266. rt_printf("new gw:%d.%d.%d.%d\r\n",gw[0],gw[1],gw[2],gw[3]);
  267. #endif
  268. }
  269. ret = 0;
  270. //查找是否关闭物理网卡
  271. for(i=0;i<g_net_num;i++)
  272. {
  273. for(j=0;j<CFG_ETH_MAX_LOGIC;j++)
  274. {
  275. if(tRunPara.tNetPara[j].ethid == i)
  276. {
  277. ret++; //虚拟网卡使用物理网卡计数
  278. }
  279. }
  280. //没有虚拟网卡使用物理网卡
  281. if(ret==0)
  282. {
  283. rt_if_down(i);
  284. }
  285. ret = 0;
  286. }
  287. // 设置网关
  288. gw[0] = (BYTE)((tRunPara.tNetPara[0].wGate>>24)&0xff);
  289. gw[1] = (BYTE)((tRunPara.tNetPara[0].wGate>>16)&0xff);
  290. gw[2] = (BYTE)((tRunPara.tNetPara[0].wGate>>8)&0xff);
  291. gw[3] = (BYTE)((tRunPara.tNetPara[0].wGate>>0)&0xff);
  292. // 如果地址是默认的0.0.0.0,说明用户不需要网关,
  293. // 不重设网关,使uboot环境变量中的网关有效,方便调试。
  294. if(gw[0]==0 && gw[1]==0 && gw[2]==0 && gw[3]==0)
  295. {
  296. return;
  297. }
  298. if(gateflag)
  299. {
  300. net_gateway_set(gw);
  301. }
  302. //noted by sunxi: 20230105 eth0需要重新设置mac才能成功。
  303. for(i=0;i<1;i++)
  304. {
  305. mac[0] = 'H';
  306. mac[1] = 'G';
  307. mac[2] = (BYTE)((tRunPara.tNetPara[i].wIp>>24)&0xff);
  308. mac[3] = (BYTE)((tRunPara.tNetPara[i].wIp>>16)&0xff);
  309. mac[4] = (BYTE)((tRunPara.tNetPara[i].wIp>>8)&0xff);
  310. mac[5] = (BYTE)((tRunPara.tNetPara[i].wIp>>0)&0xff);
  311. rt_if_down(i);
  312. net_if_set((int)tRunPara.tNetPara[i].ethid,mac,NULL, NULL);
  313. rt_if_up(i);
  314. }
  315. return;
  316. }
  317. /*------------------------------ 外部函数 -------------------------------------
  318. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  319. */
  320. int net_104_init(void)
  321. {
  322. int net;
  323. char buf[32];
  324. // struct sched_param sp;
  325. // pid_t pid;
  326. int ret;
  327. pthread_attr_t attr;
  328. IEC104_Init();
  329. for(net=0; net<CFG_ETH_MAX_LOGIC; net++)
  330. //for(net=0; net<1; net++)
  331. {
  332. g_104_client[net] = RT_SOCKET_ERR;
  333. // init_completion(&g_exit_completion_net_104[net]);
  334. g_net_reinit[net] = 0; //初始化标志
  335. // init_MUTEX(&net_r_lock[net]);//初始化接收锁
  336. // init_MUTEX(&net_s_lock[net]);//初始化发送锁
  337. pthread_mutex_init(&net_r_lock[net], NULL);
  338. pthread_mutex_init(&net_s_lock[net], NULL);
  339. // 如果接口不正常,不初始化
  340. #if 0
  341. if(tRunPara.tNetPara[net].index == 0)
  342. {
  343. if(rt_if_ip_get(tRunPara.tNetPara[net].ethid, buf)!=0)
  344. {
  345. continue;
  346. }
  347. }
  348. else
  349. {
  350. if(rt_if_ip_get2(tRunPara.tNetPara[net].ethid, tRunPara.tNetPara[net].index, buf)!=0)
  351. {
  352. continue;
  353. }
  354. }
  355. // 运行应用104线程
  356. sprintf(buf,"net_104_%d",net);
  357. pid = kernel_thread((int (*)(void *))_net_104_thread,&g_t104[net],CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  358. g_ts_104[net] = find_task_by_vpid(pid);
  359. if(IS_ERR(g_ts_104[net]))
  360. {
  361. rt_printf("ts=%p\r\n",g_ts_104[net]);
  362. g_ts_104[net] = NULL;
  363. return -1;
  364. }
  365. // 设置调度策略和优先级
  366. sp.sched_priority = 8;
  367. sched_setscheduler(g_ts_104[net],SCHED_FIFO,&sp);
  368. // 设置进程名字
  369. set_task_comm(g_ts_104[net], buf);
  370. #endif
  371. //rt_printf("----------net = %d\r\n", net);
  372. // 运行应用104线程
  373. sprintf(buf,"net_104_%d",net);
  374. // 设置调度策略和优先级
  375. pthread_attr_init(&attr);
  376. pthread_attr_setschedpolicy(&attr, SCHED_FIFO); //调度
  377. struct sched_param s_parm;
  378. //s_parm.sched_priority = sched_get_priority_max(SCHED_FIFO);
  379. s_parm.sched_priority = 8;
  380. pthread_attr_setschedparam(&attr, &s_parm); //优先级
  381. //attr.__schedpolicy = SCHED_FIFO;
  382. //attr.__schedparam.sched_priority = 8;
  383. ret = pthread_create(&g_ts_104[net], &attr, (void *)_net_104_thread, &g_t104[net]);
  384. if(ret != 0) {
  385. printf("can not create thread _net_104_thread(net=%d): %d\r\n",net,errno);
  386. return -1;
  387. }
  388. }
  389. return 0;
  390. }
  391. int net_104_exit(void)
  392. {
  393. int net;
  394. for(net=0; net<CFG_ETH_MAX_LOGIC; net++)
  395. {
  396. if(g_ts_104[net])
  397. {
  398. //kthread_stop(g_ts_104[net]);
  399. g_net_104_exit_flag[net] = 1;
  400. // wait_for_completion(&g_exit_completion_net_104[net]);
  401. pthread_join(g_ts_104[net], NULL);
  402. g_ts_104[net] = 0;
  403. }
  404. }
  405. return 0;
  406. }
  407. int net_104_send_ext(int net,unsigned char *buf,int len)
  408. {
  409. int ret = 0;
  410. int ch = net+CFG_UART_NUM_MAX;
  411. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  412. {
  413. char sz[16];
  414. sprintf(sz,"\r\nTX[%d]:",ch);
  415. print_mem(sz,buf,len);
  416. rt_printf("\r\n");
  417. }
  418. //down(&net_s_lock[net]);
  419. if(g_104_client[net] != RT_SOCKET_ERR)
  420. {
  421. ret = rt_send(g_104_client[net],buf,len,0);
  422. }
  423. //up(&net_s_lock[net]);
  424. if(ret < 0)
  425. {
  426. rt_printf("net_104_send:ret=%d.\r\n",ret);
  427. }
  428. return ret;
  429. }
  430. int net_104_send(int net,unsigned char *buf,int len)
  431. {
  432. int ret = 0;
  433. u8 ti,cot,se;
  434. u8 *out;
  435. if(pRunSet->bTT_ESAM)
  436. {
  437. iec_GetMsgInfo(buf,len,&ti,&cot,&se,false);
  438. len = sec_encrpt_iec(ti,cot,se,buf,len,&out);
  439. if(len > 0)
  440. {
  441. ret=net_104_send_ext(net,out, len);
  442. }
  443. }
  444. else
  445. {
  446. ret=net_104_send_ext(net,buf, len);
  447. }
  448. return ret;
  449. }
  450. static int net_104_recv(int net, unsigned char *buf,int len)
  451. {
  452. int ret = 0;
  453. // down(&net_r_lock[net]);
  454. pthread_mutex_lock(&net_r_lock[net]);
  455. if(g_104_client[net] != RT_SOCKET_ERR)
  456. {
  457. ret = rt_recv (g_104_client[net], (void *)buf, NET_RECV_MAX, 0);
  458. }
  459. // up(&net_r_lock[net]);
  460. pthread_mutex_unlock(&net_r_lock[net]);
  461. return ret;
  462. }
  463. void net_104_close(int net)
  464. {
  465. // down(&net_r_lock[net]);
  466. // down(&net_s_lock[net]);
  467. pthread_mutex_lock(&net_r_lock[net]);
  468. pthread_mutex_lock(&net_s_lock[net]);
  469. if(g_104_client[net] != RT_SOCKET_ERR)
  470. {
  471. rt_socket_close (g_104_client[net]);
  472. g_104_client[net] = RT_SOCKET_ERR;
  473. }
  474. g_net_104_close_flag[net] = 0;
  475. // up(&net_s_lock[net]);
  476. // up(&net_r_lock[net]);
  477. pthread_mutex_unlock(&net_s_lock[net]);
  478. pthread_mutex_unlock(&net_r_lock[net]);
  479. }
  480. void net_104_close_flag(int net)
  481. {
  482. // down(&net_r_lock[net]);
  483. // down(&net_s_lock[net]);
  484. pthread_mutex_lock(&net_r_lock[net]);
  485. pthread_mutex_lock(&net_s_lock[net]);
  486. if(g_104_client[net] != RT_SOCKET_ERR)
  487. {
  488. g_net_104_close_flag[net] = 1;
  489. }
  490. // up(&net_s_lock[net]);
  491. // up(&net_r_lock[net]);
  492. pthread_mutex_unlock(&net_s_lock[net]);
  493. pthread_mutex_unlock(&net_r_lock[net]);
  494. }
  495. int net_104_is_connect(int net)
  496. {
  497. if((g_104_client[net] == RT_SOCKET_ERR) || g_net_104_close_flag[net])
  498. {
  499. return 0;
  500. }
  501. return 1;
  502. }
  503. /*------------------------------ 内部函数 -------------------------------------
  504. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  505. */
  506. extern int _net_104Client_thread(int net);
  507. int _net_104_thread(IEC104_DEF *pt104)
  508. {
  509. struct sockaddr_in server_addr;
  510. struct sockaddr_in client_addr;
  511. struct timeval timeout;
  512. struct linger ling;
  513. SOCKET sk_server,sk_tmp ;
  514. int net;
  515. int sin_size;
  516. int len,ret;
  517. unsigned char *ip;
  518. unsigned int ip_server;
  519. int recv_timeout,recv_timeout_set;
  520. int link_timeout=0;
  521. unsigned char buf;
  522. unsigned char tmpbuf[32];
  523. u16 w104Port;
  524. /*
  525. rt_printf("tRunPara.b104Client = %d \r\n",tRunPara.b104Client);
  526. tRunPara.b104Client = 0;
  527. if(tRunPara.b104Client) // 104采用客户端
  528. {
  529. //net = pt104->cNetIndex;
  530. net = 1;
  531. _net_104Client_thread(net);
  532. //return 0;
  533. }
  534. */
  535. #if 1
  536. NET_BEGIN:
  537. net = pt104->cNetIndex;
  538. // 服务器端开始建立socket描述符
  539. sk_server = rt_socket(AF_INET, SOCK_STREAM, 0);
  540. if (sk_server == RT_SOCKET_ERR)
  541. {
  542. RT_PRINTF_POSITION();
  543. g_ts_104[net] = 0;
  544. return -1;
  545. }
  546. // 服务器端填充sockaddr结构
  547. memset (&server_addr, 0, sizeof (struct sockaddr_in));
  548. server_addr.sin_family = AF_INET;
  549. //rt_if_ip_get(net,(unsigned char *)&ip_server);
  550. //ret=get_ethcnt(net, tRunPara.tNetPara[net].ethid);
  551. //IP在物理网卡上
  552. if( tRunPara.tNetPara[net].index == 0)
  553. {
  554. rt_if_ip_get(tRunPara.tNetPara[net].ethid, (unsigned char *)&ip_server);
  555. w104Port = tRunPara.w104Port; // 网络1、2使用可变端口,保证级联时不冲突。
  556. }
  557. else //IP在虚拟网卡上
  558. {
  559. //从虚拟网卡获取IP地址
  560. rt_if_ip_get2(tRunPara.tNetPara[net].ethid, tRunPara.tNetPara[net].index, (unsigned char *)&ip_server);
  561. w104Port = NET_104_PORT; // 调试网络使用固定的2404端口,保证维护工具可以连接。
  562. }
  563. server_addr.sin_addr.s_addr = htonl (ip_server);
  564. server_addr.sin_port = htons (w104Port);
  565. //printf("-------Server ip:%s\n",inet_ntoa(server_addr.sin_addr));
  566. // 捆绑sockfd描述符
  567. ret = rt_bind (sk_server, (struct sockaddr *)(&server_addr), sizeof (struct sockaddr));
  568. if (ret < 0)
  569. {
  570. // RT_PRINTF_POSITION();
  571. rt_socket_close (sk_server);
  572. g_ts_104[net] = 0;
  573. return -2;
  574. }
  575. // 监听sockfd描述符
  576. ret = rt_listen (sk_server, 1);
  577. if (ret < 0)
  578. {
  579. // RT_PRINTF_POSITION();
  580. rt_socket_close (sk_server);
  581. g_ts_104[net] = 0;
  582. return -3;
  583. }
  584. // 设置接收超时
  585. timeout.tv_sec = 0;
  586. timeout.tv_usec = 100000;
  587. rt_setsockopt(sk_server,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout ,sizeof(struct timeval));
  588. // 设置关闭延时
  589. ling.l_onoff = 1;
  590. ling.l_linger = 0;
  591. rt_setsockopt(sk_server,SOL_SOCKET,SO_LINGER,(char *)&ling ,sizeof(struct linger));
  592. ip = (unsigned char *)&ip_server;
  593. //rt_printf("应用104线程已启动(IP:%d.%d.%d.%d,PORT:%d)。\r\n",ip[0],ip[1],ip[2],ip[3],NET_104_PORT);
  594. #ifdef CPU_FUXI
  595. sprintf(tmpbuf,"net_104_%d",net);
  596. prctl(PR_SET_NAME, tmpbuf);
  597. #endif
  598. while(1)
  599. {
  600. // 服务器阻塞,直到客户程序建立连接
  601. sin_size = sizeof (struct sockaddr_in);
  602. sk_tmp = rt_accept (sk_server, (struct sockaddr*)(&client_addr), &sin_size);
  603. // 检查是否退出
  604. if(g_net_104_exit_flag[net])
  605. {
  606. // 关闭客户端
  607. net_104_close(net);
  608. // 关闭服务器
  609. rt_socket_close (sk_server);
  610. // complete_and_exit(&g_exit_completion_net_104[net], 0);
  611. return 0;
  612. }
  613. //是否重新初始化
  614. if(g_net_reinit[net] == NET_SOCKET_CLOSING)
  615. {
  616. // 关闭客户端
  617. net_104_close(net);
  618. // 关闭服务器
  619. rt_socket_close (sk_server);
  620. //置已经关闭标志
  621. g_net_reinit[net] = NET_SOCKET_CLOSED;
  622. //等待IP修改完成
  623. while(g_net_reinit[net] != NET_SOCKET_REINIT)
  624. {
  625. msleep(100);
  626. }
  627. //置为初始化
  628. g_net_reinit[net] = NET_SOCKET_INIT;
  629. goto NET_BEGIN;
  630. }
  631. //注意,用指针类型的socket接口,失败是返回NULL
  632. //而整数类型的socket接口,失败返回的是负数
  633. //#ifdef RT_SOCKET_FD
  634. #if 1
  635. if (sk_tmp < 0)
  636. {
  637. msleep(100);
  638. // 超时.
  639. continue;
  640. }
  641. #else
  642. if (sk_tmp == RT_SOCKET_ACCEPT_RST)
  643. {
  644. msleep(100);
  645. // 超时
  646. continue;
  647. }
  648. #endif
  649. g_104_client[net] = sk_tmp;
  650. ip = (unsigned char *)&client_addr.sin_addr.s_addr;
  651. rt_printf("104 client[net=%d,s=%d] connect(ip=%d.%d.%d.%d)!\r\n", net ,sk_tmp,ip[0],ip[1],ip[2],ip[3]);
  652. #if 0
  653. // 检查是否主站IP
  654. if(tRunPara.ip104Master1 || tRunPara.ip104Master2 || tRunPara.ip104Master3 || tRunPara.ip104Master4)
  655. {
  656. u32 network0,network1;
  657. network0 = client_addr.sin_addr.s_addr & tRunPara.tNetPara[CFG_ETH_MAX_LOGIC-1].wMask;
  658. network1 = tRunPara.tNetPara[CFG_ETH_MAX_LOGIC-1].wIp & tRunPara.tNetPara[CFG_ETH_MAX_LOGIC-1].wMask;
  659. if( (client_addr.sin_addr.s_addr != tRunPara.ip104Master1)
  660. && (client_addr.sin_addr.s_addr != tRunPara.ip104Master2)
  661. && (client_addr.sin_addr.s_addr != tRunPara.ip104Master3)
  662. && (client_addr.sin_addr.s_addr != tRunPara.ip104Master4)
  663. && (g_ip_net_maintain == 0) // 如果维护工具已连接,任意104主站可以接入。
  664. &&(network0 != network1)) // 运行维护网络的104连接进来
  665. {
  666. net_104_close(net);
  667. rt_printf("主站IP不在允许范围内!\r\n");
  668. ip = (unsigned char *)&tRunPara.ip104Master1;
  669. rt_printf("允许IP1:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  670. ip = (unsigned char *)&tRunPara.ip104Master2;
  671. rt_printf("允许IP2:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  672. ip = (unsigned char *)&tRunPara.ip104Master3;
  673. rt_printf("允许IP3:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  674. ip = (unsigned char *)&tRunPara.ip104Master4;
  675. rt_printf("允许IP4:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  676. ip = (unsigned char *)&g_ip_net_maintain;
  677. rt_printf("允许IP5:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  678. msleep(500);
  679. continue;
  680. }
  681. }
  682. #endif
  683. // 客户端已连接,处理客户端命令
  684. // 初始化接收FIFO
  685. rt_fifo_init(&g_net_104_fifo[net],g_net_104_buf[net],NET_104_BUF_SIZE);
  686. IEC104_Recv_Reset(&g_t104[net]);
  687. memcpy(g_t104[net].ip,(unsigned char *)&client_addr.sin_addr.s_addr,4);
  688. g_t104[net].LinkCountNet++;
  689. //接收帧超时初始化,超时设置为测试超时加确认超时后的2倍。
  690. recv_timeout=0;
  691. recv_timeout_set = (tRunPara.w104TestTime + tRunPara.w104AckTime)*2*10;
  692. // 循环处理接收数据
  693. while(1)
  694. {
  695. len = net_104_recv(net, g_net_104_buf_frame[net], NET_RECV_MAX);
  696. // 检查是否退出
  697. if(g_net_104_exit_flag[net])
  698. {
  699. // 关闭客户端
  700. net_104_close(net);
  701. // 关闭服务器
  702. rt_socket_close (sk_server);
  703. // complete_and_exit(&g_exit_completion_net_104[net], 0);
  704. return 0;
  705. }
  706. //是否重新初始化
  707. if(g_net_reinit[net] == NET_SOCKET_CLOSING)
  708. {
  709. // 关闭客户端
  710. net_104_close(net);
  711. // 关闭服务器
  712. rt_socket_close (sk_server);
  713. //置已经关闭标志
  714. g_net_reinit[net] = NET_SOCKET_CLOSED;
  715. //等待IP修改完成
  716. while(g_net_reinit[net] != NET_SOCKET_REINIT)
  717. {
  718. msleep(100);
  719. }
  720. //置为初始化
  721. g_net_reinit[net] = NET_SOCKET_INIT;
  722. rt_printf("net_104_thread:链路重新初始化!\r\n");
  723. goto NET_BEGIN;
  724. }
  725. if(g_net_104_close_flag[net])
  726. {
  727. break;
  728. }
  729. if((len == -EAGAIN)||(len==-1))
  730. {
  731. if( rt_get_net_linkstatus(rt_get_netcard_id_from_socket(g_104_client[net] ))==1)
  732. {
  733. link_timeout++;
  734. if(link_timeout > RT_LINK_TIMEOUT*10) //100ms超时
  735. {
  736. rt_printf("net_104_thread: link down\r\n");
  737. link_timeout = 0;
  738. break;
  739. }
  740. }
  741. else
  742. {
  743. link_timeout=0;
  744. }
  745. // 接收帧超时
  746. recv_timeout++;
  747. if(recv_timeout>recv_timeout_set)
  748. {
  749. rt_printf ("net_104_thread:接收帧超时(value=%d,set=%d)\r\n",recv_timeout,recv_timeout_set);
  750. break;
  751. }
  752. }
  753. else if((len == 0)||(len<-1))
  754. {
  755. //调用recv时,如果返回0,则表示对端关闭;返回负数,表示出错
  756. rt_printf ("net_104_thread:rt_recv(ret=%d)\r\n",len);
  757. // TODO:以下休眠语句必须屏蔽,否则net_104_close会延时调用,
  758. // 导致主循环可以在此断开的socket上继续发送数据,引起系统异常。
  759. // msleep(100);
  760. break;
  761. }
  762. else
  763. {
  764. int space;
  765. space = rt_fifo_space(&g_net_104_fifo[net]);
  766. if(space > len)
  767. {
  768. rt_fifo_put(&g_net_104_fifo[net],g_net_104_buf_frame[net],len);
  769. if(tRunPara.tNetPara[net].ethid<CFG_ETH_NUM_MAX)
  770. g_run_stu.led[CFG_UART_NUM_MAX*2+tRunPara.tNetPara[net].ethid]=1;
  771. }
  772. else
  773. {
  774. rt_printf ("net_104_thread:接收FIFO溢出(space=%d,len=%d)\r\n",space,len);
  775. }
  776. link_timeout=0;
  777. }
  778. // 接收数据
  779. // printf("g_t104[0].bSM2Data:%d,g_t104[0].bRecvOk:%d, g_link_comm.link_flag: %d, buflen: %d \n",g_t104[net].bSM2Data,g_t104[net].bRecvOk, g_link_comm.link_flag, len);
  780. while(g_t104[net].bSM2Data== false && g_t104[net].bRecvOk == false)
  781. {
  782. int j; //+++
  783. j=rt_fifo_get(&g_net_104_fifo[net],&buf,1); //+++
  784. //printf("----rt_fifo_get: %d \n",j); //+++
  785. //if(rt_fifo_get(&g_net_104_fifo[net],&buf,1))
  786. if(j)
  787. {
  788. if(g_print_comm_raw && (g_print_port & (1<<(net+CFG_UART_NUM_MAX))))
  789. {
  790. rt_printf("%02x ",buf);
  791. }
  792. if(IEC104_Recv(&g_t104[net],buf))
  793. {
  794. //printf("----IEC104_Recv recv_timeout\n"); //+++
  795. // 如果接收到完整的一帧104报文(其它数据不清定时器,防止意外连接),超时定时器清零。
  796. recv_timeout=0;
  797. }
  798. }
  799. else
  800. {
  801. break;
  802. }
  803. }
  804. }
  805. // 关闭客户端socket
  806. net_104_close(net);
  807. }
  808. #endif
  809. while(1)
  810. {
  811. rt_printf("net 104\r\n");
  812. usleep(1000*1000);
  813. }
  814. // 理论上,程序不会运行到这儿
  815. return 0;
  816. }
  817. int _net_104Client_thread(int net)
  818. {
  819. struct sockaddr_in server_addr;
  820. //struct timeval timeout;
  821. //struct linger ling;
  822. //SOCKET s;
  823. int s;
  824. int len,ret;
  825. unsigned int ip_server,link_count;
  826. int recv_timeout;
  827. unsigned char buf;
  828. unsigned char *ip;
  829. // int net = *pnet;
  830. while(1)
  831. {
  832. //rt_printf("BEGIN ---------[ %d ] [ %s ] \r\n",__LINE__,__func__);
  833. BEGIN:
  834. link_count = 0;
  835. // 检查级联IP是否设置
  836. ip_server = tRunPara.ip104Master[net];
  837. while(1)
  838. {
  839. ip_server = tRunPara.ip104Master[net];
  840. // ip_server = inet_addr("192.168.9.57");
  841. if(ip_server)
  842. {
  843. break;
  844. }
  845. // 检查是否退出
  846. if(g_net_104_exit_flag[net])
  847. {
  848. // rt_printf_time("net_104link_thread exit.\r\n");
  849. // complete_and_exit(&g_exit_completion_net_104[net], 0);
  850. return 0;
  851. }
  852. // 1秒钟检查一次
  853. msleep(1000);
  854. }
  855. ip = (unsigned char *)&ip_server;
  856. // rt_printf_time("104连接服务器开始(%d):ip=%d.%d.%d.%d!\r\n",net,ip[0],ip[1],ip[2],ip[3]);
  857. //printf("104 conect ip=%x\n",ip_server);
  858. // 创建socket
  859. //s = rt_socket(AF_INET, SOCK_STREAM, 0);
  860. s = socket(AF_INET, SOCK_STREAM, 0);
  861. if (s == RT_SOCKET_ERR)
  862. {
  863. RT_PRINTF_POSITION();
  864. return -1;
  865. }
  866. // 服务器端填充sockaddr结构
  867. memset (&server_addr, 0, sizeof (struct sockaddr_in));
  868. server_addr.sin_family = AF_INET;
  869. //server_addr.sin_addr.s_addr = htonl (ip_server);
  870. server_addr.sin_addr.s_addr = ip_server;
  871. //server_addr.sin_port = htons (tRunPara.w104Port);
  872. server_addr.sin_port = htons(2404);
  873. // 连接服务器,没有连接成功不退出
  874. while(1)
  875. {
  876. // ret = rt_connect(s,(struct sockaddr *)&server_addr,sizeof(struct sockaddr_in));
  877. ret = connect(s,(struct sockaddr *)&server_addr,sizeof(struct sockaddr_in));
  878. //printf("ret is %d\n",ret);
  879. #if 0
  880. // 检查是否退出
  881. if(g_net_104_exit_flag[net])
  882. {
  883. // 关闭客户端
  884. g_104_client[net] = RT_SOCKET_ERR;
  885. rt_socket_close (s);
  886. rt_printf_time("net_104_thread exit(rt_recv).\r\n");
  887. // complete_and_exit(&g_exit_completion_net_104[net], 0);
  888. return 0;
  889. }
  890. // 检查服务器是否变更
  891. if(ip_server != tRunPara.ip104Master[net])
  892. {
  893. // 关闭客户端
  894. g_104_client[net] = RT_SOCKET_ERR;
  895. rt_socket_close (s);
  896. ip = (unsigned char *)&tRunPara.ip104Master[net];
  897. rt_printf_time("主站服务器变更(%d):ip=%d.%d.%d.%d!\r\n",net,ip[0],ip[1],ip[2],ip[3]);
  898. goto BEGIN;
  899. }
  900. #endif
  901. if (ret == 0)
  902. {
  903. g_net_104_close_flag[net] = 0;
  904. g_link104_init_ok[net] = 0;
  905. g_104_client[net] = s;
  906. g_t104[net].bClientStart=false;
  907. link_count++;
  908. //rt_printf_time("104连接服务器成功(通道=%d,连接计数=%d):ip=%d.%d.%d.%d!\r\n",net,link_count,ip[0],ip[1],ip[2],ip[3]);
  909. printf("104 connected ok +++++++ok\n");
  910. break;
  911. }
  912. // 1秒钟连接一次
  913. msleep(1000);
  914. }
  915. #if 0
  916. // 设置接收超时
  917. timeout.tv_sec = 0;
  918. timeout.tv_usec = 100000;
  919. rt_setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout ,sizeof(struct timeval));
  920. // 设置关闭延时
  921. ling.l_onoff = 1;
  922. ling.l_linger = 0;
  923. rt_setsockopt(s,SOL_SOCKET,SO_LINGER,(char *)&ling ,sizeof(struct linger));
  924. // 初始化FIFO
  925. rt_fifo_init(&g_net_104_fifo[net],g_net_104_buf[net],NET_104_BUF_SIZE);
  926. #endif
  927. // 处理接收
  928. recv_timeout = 0;
  929. while(1)
  930. {
  931. len = rt_recv (s, g_net_104_buf_frame[net], NET_RECV_MAX, 0);
  932. printf("recv data : %s \r\n",g_net_104_buf_frame[net]);
  933. // 检查是否退出
  934. if(g_net_104_exit_flag[net])
  935. {
  936. // 关闭客户端
  937. g_104_client[net] = RT_SOCKET_ERR;
  938. rt_socket_close (s);
  939. rt_printf_time("net_104client_thread exit(rt_recv).\r\n");
  940. // complete_and_exit(&g_exit_completion_net_104[net], 0);
  941. return 0;
  942. }
  943. // 检查服务器是否变更
  944. if(ip_server != tRunPara.ip104Master[net])
  945. {
  946. // 关闭客户端
  947. g_104_client[net] = RT_SOCKET_ERR;
  948. rt_socket_close (s);
  949. rt_printf_time("104服务器变更(%d):ip=%d.%d.%d.%d!\r\n",net,ip[0],ip[1],ip[2],ip[3]);
  950. goto BEGIN;
  951. }
  952. // 应用程序主动关闭
  953. if(g_net_104_close_flag[net])
  954. {
  955. break;
  956. }
  957. if(len == -EAGAIN)
  958. {
  959. // 超时
  960. recv_timeout++;
  961. if(recv_timeout>300) // 超时30s,端口未收到数据,关闭端口
  962. {
  963. rt_printf_time("104客户端30S超时关闭(net=%d)\r\n",net);
  964. break;
  965. }
  966. }
  967. else if (len <= 0)
  968. {
  969. //调用recv时,如果返回0,则表示对端关闭;返回负数,表示出错
  970. rt_printf_time ("104客户端关闭(net=%d,ret=%d)\r\n",net,len);
  971. msleep(100);
  972. break;
  973. }
  974. else
  975. {
  976. rt_fifo_put(&g_net_104_fifo[net],g_net_104_buf_frame[net],len);
  977. recv_timeout=0; //超时计数器清零
  978. if(tRunPara.tNetPara[net].ethid<CFG_ETH_NUM_MAX)
  979. g_run_stu.led[CFG_UART_NUM_MAX*2+tRunPara.tNetPara[net].ethid]=1;
  980. }
  981. // 接收数据
  982. while(g_t104[net].bSM2Data== false && g_t104[net].bRecvOk == false)
  983. {
  984. if(rt_fifo_get(&g_net_104_fifo[net],&buf,1))
  985. {
  986. IEC104_Recv(&g_t104[net],buf);
  987. }
  988. else
  989. {
  990. break;
  991. }
  992. }
  993. // msleep(100);
  994. }
  995. // 关闭客户端socket
  996. g_104_client[net] = RT_SOCKET_ERR;
  997. rt_socket_close (s);
  998. // rt_printf_time ("net_104link_thread:rt_recv(ret=%d)\r\n",len);
  999. }
  1000. // 理论上,程序不会运行到这儿
  1001. return 0;
  1002. }
  1003. #else
  1004. /*------------------------------- 头文件 --------------------------------------
  1005. */
  1006. #include "head.h"
  1007. #include <linux/delay.h>
  1008. /*------------------------------- 宏定义 --------------------------------------
  1009. */
  1010. #define NET_104_BUF_SIZE 16384
  1011. /*------------------------------ 类型结构 -------------------------------------
  1012. */
  1013. /*------------------------------ 全局变量 -------------------------------------
  1014. */
  1015. static struct task_struct * g_ts_104[IEC104_TOTAL_SOCKETS];
  1016. static SOCKET g_104_client[IEC104_TOTAL_SOCKETS];
  1017. static unsigned char g_net_104_buf_frame[IEC104_TOTAL_SOCKETS][NET_RECV_MAX]; // 以太网接收缓冲区
  1018. static unsigned char g_net_104_buf[IEC104_TOTAL_SOCKETS][NET_104_BUF_SIZE]; // 以太网接收缓冲区
  1019. static struct rt_fifo g_net_104_fifo[IEC104_TOTAL_SOCKETS];
  1020. static volatile int g_net_reinit[CFG_ETH_MAX_LOGIC]={NET_SOCKET_INIT};//网络重新初始化标志.0表示不用重新初始化,1表示需要重新初始化
  1021. static struct semaphore net_r_lock[CFG_ETH_MAX_LOGIC]; //接收锁
  1022. static struct semaphore net_s_lock[CFG_ETH_MAX_LOGIC]; //发送锁
  1023. static int g_net_104_close_flag[CFG_ETH_MAX_LOGIC];
  1024. static int g_net_104_exit_flag[CFG_ETH_MAX_LOGIC];
  1025. static struct completion g_exit_completion_net_104[CFG_ETH_MAX_LOGIC];
  1026. extern u32 g_ip_net_maintain; // 104维护工具调试客户端地址
  1027. /*------------------------------ 函数声明 -------------------------------------
  1028. */
  1029. int _net_104_thread(IEC104_DEF *pt104);
  1030. //设置socket关闭标志并等待socket关闭完成
  1031. void net_104_set_sock_closing(int no)
  1032. {
  1033. //if(no >= g_net_num) return;
  1034. g_net_reinit[no] = NET_SOCKET_CLOSING; //置关闭标志
  1035. if(!tRunPara.b104Client)
  1036. {
  1037. int count=0;
  1038. //等待关闭完成
  1039. while(g_net_reinit[no] != NET_SOCKET_CLOSED)
  1040. {
  1041. msleep(20);
  1042. if(++count>150)
  1043. {
  1044. rt_printf("104 client socket close fail!!!\r\n");
  1045. return;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. //设置socket重新创建标志
  1051. void net_104_set_sock_initflag(int no)
  1052. {
  1053. // if(no >= g_net_num) return;
  1054. if(g_net_reinit[no] == NET_SOCKET_CLOSED)
  1055. {
  1056. g_net_reinit[no] = NET_SOCKET_REINIT;
  1057. }
  1058. }
  1059. /**************************************************************************
  1060. 函数名称:Net_Init
  1061. 函数版本:1.00
  1062. 作者:
  1063. 创建日期:2009.1.8
  1064. 函数功能说明:以太网初始化
  1065. 输入参数:
  1066. 输出参数:
  1067. 返回值:
  1068. 更新信息:
  1069. 更新日志1:
  1070. 日期:
  1071. 修改者:
  1072. 修改内容:
  1073. 修改原因:
  1074. ***************************************************************************/
  1075. void Net_Init(void)
  1076. {
  1077. unsigned char mac[6];
  1078. unsigned char ip[4];
  1079. unsigned char mask[4];
  1080. unsigned char gw[4];
  1081. unsigned char routenet[4];
  1082. unsigned char routenetmask[4];
  1083. unsigned char routegate[4];
  1084. // char * acteth;
  1085. int i,j;
  1086. char tmp[16];
  1087. int ret = 0;
  1088. int gateflag = 0;
  1089. if(tRunPara.w104K == 0)
  1090. {
  1091. if(pRunSet->bTT_SoeResumeComm || pRunSet->bTT_SoeResumePower)
  1092. {
  1093. rt_printf("定值参数错误:SOE通讯续传、SOE上电续传时参数104 K值不能为0.\r\n");
  1094. rt_err_set(ERR_CODE_INIT_SOFTWARE,0);
  1095. }
  1096. }
  1097. #if 0
  1098. int debug_port;
  1099. acteth = env_get_info(ENV_ETHACT);
  1100. if(acteth != NULL)
  1101. {
  1102. if(strcmp(acteth,"FEC0") == 0)
  1103. {
  1104. debug_port = 0;
  1105. }
  1106. else if(strcmp(acteth,"FEC1") == 0)
  1107. {
  1108. debug_port = 1;
  1109. }
  1110. else if(strcmp(acteth,"DM9000") == 0)
  1111. {
  1112. debug_port = 2;
  1113. }
  1114. else
  1115. {
  1116. debug_port = 0;
  1117. }
  1118. }
  1119. else
  1120. {
  1121. debug_port = 0;
  1122. }
  1123. #endif
  1124. // if(acteth != NULL && strcmp(acteth,"DM9000") == 0)
  1125. // {
  1126. // debug_port = 2;
  1127. // }
  1128. // else
  1129. // {
  1130. // debug_port = 0;
  1131. // }
  1132. //for(i=0;i<g_net_num;i++)
  1133. for(i=0;i<CFG_ETH_MAX_LOGIC;i++)
  1134. {
  1135. //没有改变参数,不用初始化
  1136. if(!tRunPara.tNetPara[i].bInit)
  1137. {
  1138. //104配置参数有修改,需关闭socket重新初始化
  1139. if(tRunPara.sock_104_flag)
  1140. {
  1141. net_104_set_sock_initflag(i);
  1142. }
  1143. continue;
  1144. }
  1145. gateflag = 1;
  1146. tRunPara.tNetPara[i].bInit=false;
  1147. // 设置接口MAC地址
  1148. if(rt_if_sz2mac(env_get_info(ENV_NET1MAC+i),mac)< 0)
  1149. {
  1150. mac[0] = 'H';
  1151. mac[1] = 'G';
  1152. mac[2] = (BYTE)((tRunPara.tNetPara[i].wIp>>24)&0xff);
  1153. mac[3] = (BYTE)((tRunPara.tNetPara[i].wIp>>16)&0xff);
  1154. mac[4] = (BYTE)((tRunPara.tNetPara[i].wIp>>8)&0xff);
  1155. mac[5] = (BYTE)((tRunPara.tNetPara[i].wIp>>0)&0xff);
  1156. }
  1157. // 设置IP
  1158. ip[0] = (BYTE)((tRunPara.tNetPara[i].wIp>>24)&0xff);
  1159. ip[1] = (BYTE)((tRunPara.tNetPara[i].wIp>>16)&0xff);
  1160. ip[2] = (BYTE)((tRunPara.tNetPara[i].wIp>>8)&0xff);
  1161. ip[3] = (BYTE)((tRunPara.tNetPara[i].wIp>>0)&0xff);
  1162. // 设置MASK
  1163. mask[0] = (BYTE)((tRunPara.tNetPara[i].wMask>>24)&0xff);
  1164. mask[1] = (BYTE)((tRunPara.tNetPara[i].wMask>>16)&0xff);
  1165. mask[2] = (BYTE)((tRunPara.tNetPara[i].wMask>>8)&0xff);
  1166. mask[3] = (BYTE)((tRunPara.tNetPara[i].wMask>>0)&0xff);
  1167. #if (1)
  1168. printf("[%s:%d]\r\n", __func__, __LINE__);
  1169. if(tRunPara.tNetPara[i].old_ethid != tRunPara.tNetPara[i].ethid) //网络号已改变且为物理网卡时
  1170. {
  1171. BYTE count;
  1172. if((int)tRunPara.tNetPara[i].old_index == 0)
  1173. {
  1174. rt_if_down(tRunPara.tNetPara[i].old_ethid); //此时卸载当前网卡所有P 比如eth0 eth0:2 eth0:3
  1175. //rt_printf("down eth%d\r\n",tRunPara.tNetPara[i].old_ethid);
  1176. }
  1177. else if((int)tRunPara.tNetPara[i].old_index > 0)
  1178. {
  1179. rt_if_down2(tRunPara.tNetPara[i].old_ethid, tRunPara.tNetPara[i].old_index);
  1180. // rt_printf("down eth%d:%d\r\n",tRunPara.tNetPara[i].old_ethid,tRunPara.tNetPara[i].old_index);
  1181. }
  1182. for(count=i+1; count<CFG_ETH_MAX_LOGIC; count++) //物理网卡被更新 相应的之前的物理网卡和虚拟网卡都需要重新配置
  1183. if((tRunPara.tNetPara[i].ethid==tRunPara.tNetPara[count].old_ethid)
  1184. ||(tRunPara.tNetPara[i].old_ethid==tRunPara.tNetPara[count].ethid))
  1185. {
  1186. tRunPara.tNetPara[count].bInit=true;
  1187. if((int)tRunPara.tNetPara[count].old_index > 0) //若关联的为虚拟网卡则默认不需要进行投退
  1188. {
  1189. rt_if_down2(tRunPara.tNetPara[count].old_ethid, tRunPara.tNetPara[count].old_index);
  1190. //rt_printf("down eth%d:%d\r\n",tRunPara.tNetPara[count].old_ethid,tRunPara.tNetPara[count].old_index);
  1191. tRunPara.tNetPara[count].old_index = 0;
  1192. }
  1193. else
  1194. {
  1195. rt_if_down(tRunPara.tNetPara[count].old_ethid); //此时卸载当前网卡所有P 比如eth0 eth0:2 eth0:3
  1196. //rt_printf("down eth%d\r\n",tRunPara.tNetPara[count].old_ethid);
  1197. }
  1198. }
  1199. }
  1200. else if((int)tRunPara.tNetPara[i].old_index > 0) //网络号未改变时的 虚拟网卡
  1201. {
  1202. rt_if_down2(tRunPara.tNetPara[i].old_ethid, tRunPara.tNetPara[i].old_index);
  1203. //rt_printf("down eth%d:%d\r\n",tRunPara.tNetPara[i].old_ethid,tRunPara.tNetPara[i].old_index);
  1204. }
  1205. #else
  1206. //检查是否需要关闭虚拟网卡
  1207. if((int)tRunPara.tNetPara[i].old_index > 0)
  1208. {
  1209. rt_if_down2(tRunPara.tNetPara[i].old_ethid, tRunPara.tNetPara[i].old_index);
  1210. }
  1211. #endif
  1212. //获取配置相同的ethid有多少个
  1213. ret=get_ethcnt(i, tRunPara.tNetPara[i].ethid);
  1214. //没有的话,使用物理的
  1215. if(ret==0)
  1216. {
  1217. // 设置网络参数
  1218. net_if_set((int)tRunPara.tNetPara[i].ethid,mac,ip, mask);
  1219. tRunPara.tNetPara[i].index = 0;
  1220. //rt_printf_time("ret = %d, ethID = %d\r\n",ret,tRunPara.tNetPara[i].ethid);
  1221. }
  1222. else //有的话使用虚拟的
  1223. {
  1224. #if (1)
  1225. printf("[%s:%d]\r\n", __func__, __LINE__);
  1226. net_if_set2((int)tRunPara.tNetPara[i].ethid, ret, 0, ip, mask);
  1227. tRunPara.tNetPara[i].index = ret;
  1228. #else
  1229. net_if_set2((int)tRunPara.tNetPara[i].ethid, i+1, 0, ip, mask);
  1230. tRunPara.tNetPara[i].index = i+1;
  1231. #endif
  1232. //rt_printf_time("Xnet = %d,ip=%x, ethID = %d, ret = %d\r\n",tRunPara.tNetPara[i].index,ip[3],tRunPara.tNetPara[i].ethid,ret);
  1233. }
  1234. tRunPara.tNetPara[i].old_ethid= tRunPara.tNetPara[i].ethid;
  1235. tRunPara.tNetPara[i].old_index= tRunPara.tNetPara[i].index;
  1236. // 如果是调试串口,不初始化
  1237. //if(i == debug_port)
  1238. if(i==(CFG_ETH_MAX_LOGIC-1))
  1239. {
  1240. //设置IP环境变量
  1241. if(tRunPara.tNetPara[i].changed & 0x01)
  1242. {
  1243. sprintf(tmp, "%d.%d.%d.%d",( tRunPara.tNetPara[i].wIp>>24)&0xff, (tRunPara.tNetPara[i].wIp>>16)&0xff, (tRunPara.tNetPara[i].wIp>>8)&0xff, (tRunPara.tNetPara[i].wIp&0xff));
  1244. env_setenv("ipaddr", tmp);
  1245. }
  1246. //设置Gate环境变量
  1247. if(tRunPara.tNetPara[i].changed & 0x02)
  1248. {
  1249. sprintf(tmp, "%d.%d.%d.%d", ( tRunPara.tNetPara[i].wGate>>24)&0xff, (tRunPara.tNetPara[i].wGate>>16)&0xff, (tRunPara.tNetPara[i].wGate>>8)&0xff, (tRunPara.tNetPara[i].wGate&0xff));
  1250. if(tRunPara.tNetPara[i].wGate)
  1251. env_setenv("gatewayip", tmp);
  1252. }
  1253. //设置Mask环境变量
  1254. if(tRunPara.tNetPara[i].changed & 0x04)
  1255. {
  1256. sprintf(tmp, "%d.%d.%d.%d", ( tRunPara.tNetPara[i].wMask>>24)&0xff, (tRunPara.tNetPara[i].wMask>>16)&0xff, (tRunPara.tNetPara[i].wMask>>8)&0xff, (tRunPara.tNetPara[i].wMask&0xff));
  1257. env_setenv("netmask", tmp);
  1258. }
  1259. //设置ethact环境变量
  1260. if(tRunPara.tNetPara[i].changed & 0x40)
  1261. {
  1262. if(tRunPara.tNetPara[i].ethid >= 2)
  1263. {
  1264. sprintf(tmp, "%s", "DM9000");
  1265. }
  1266. else
  1267. {
  1268. sprintf(tmp, "FEC%d", (int)tRunPara.tNetPara[i].ethid);
  1269. }
  1270. env_setenv("ethact", tmp);
  1271. }
  1272. //continue;
  1273. }
  1274. //网段
  1275. routenet[0] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>24)&0xff);
  1276. routenet[1] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>16)&0xff);
  1277. routenet[2] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>8)&0xff);
  1278. routenet[3] = (BYTE)((tRunPara.tNetPara[i].wRouteNet>>0)&0xff);
  1279. //网段掩码
  1280. routenetmask[0] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>24)&0xff);
  1281. routenetmask[1] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>16)&0xff);
  1282. routenetmask[2] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>8)&0xff);
  1283. routenetmask[3] = (BYTE)((tRunPara.tNetPara[i].wRouteNetMask>>0)&0xff);
  1284. //网关
  1285. routegate[0] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>24)&0xff);
  1286. routegate[1] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>16)&0xff);
  1287. routegate[2] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>8)&0xff);
  1288. routegate[3] = (BYTE)((tRunPara.tNetPara[i].wRouteGate>>0)&0xff);
  1289. net_route_set(routenet, routenetmask, routegate);
  1290. //g_net_reinit[i] = 1;//置重新初始化标志
  1291. net_104_set_sock_initflag(i);
  1292. net_debug_set_sock_initflag();
  1293. net_maintain_set_sock_initflag();
  1294. #if 0
  1295. // 得到并打印新的网络参数
  1296. // net_if_get(i,mac,ip,mask);
  1297. rt_printf("new mac: %02x:%02x:%02x:%02x:%02x:%02x\r\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
  1298. rt_printf("new ip:%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  1299. rt_printf("new mask:%d.%d.%d.%d\r\n",mask[0],mask[1],mask[2],mask[3]);
  1300. rt_printf("new gw:%d.%d.%d.%d\r\n",gw[0],gw[1],gw[2],gw[3]);
  1301. #endif
  1302. }
  1303. ret = 0;
  1304. //查找是否关闭物理网卡
  1305. for(i=0;i<g_net_num;i++)
  1306. {
  1307. for(j=0;j<CFG_ETH_MAX_LOGIC;j++)
  1308. {
  1309. if(tRunPara.tNetPara[j].ethid == i)
  1310. {
  1311. ret++; //虚拟网卡使用物理网卡计数
  1312. }
  1313. }
  1314. //没有虚拟网卡使用物理网卡
  1315. if(ret==0)
  1316. {
  1317. rt_if_down(i);
  1318. }
  1319. ret = 0;
  1320. }
  1321. // 设置网关
  1322. gw[0] = (BYTE)((tRunPara.tNetPara[0].wGate>>24)&0xff);
  1323. gw[1] = (BYTE)((tRunPara.tNetPara[0].wGate>>16)&0xff);
  1324. gw[2] = (BYTE)((tRunPara.tNetPara[0].wGate>>8)&0xff);
  1325. gw[3] = (BYTE)((tRunPara.tNetPara[0].wGate>>0)&0xff);
  1326. // 如果地址是默认的0.0.0.0,说明用户不需要网关,
  1327. // 不重设网关,使uboot环境变量中的网关有效,方便调试。
  1328. if(gw[0]==0 && gw[1]==0 && gw[2]==0 && gw[3]==0)
  1329. {
  1330. return;
  1331. }
  1332. if(gateflag)
  1333. {
  1334. net_gateway_set(gw);
  1335. }
  1336. return;
  1337. }
  1338. /*------------------------------ 外部函数 -------------------------------------
  1339. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  1340. */
  1341. int net_104_init(void)
  1342. {
  1343. int net;
  1344. char buf[32];
  1345. struct sched_param sp;
  1346. pid_t pid;
  1347. IEC104_Init();
  1348. for(net=0; net<CFG_ETH_MAX_LOGIC; net++)
  1349. {
  1350. g_104_client[net] = RT_SOCKET_ERR;
  1351. init_completion(&g_exit_completion_net_104[net]);
  1352. g_net_reinit[net] = 0; //初始化标志
  1353. init_MUTEX(&net_r_lock[net]);//初始化接收锁
  1354. init_MUTEX(&net_s_lock[net]);//初始化发送锁
  1355. // 如果接口不正常,不初始化
  1356. if(tRunPara.tNetPara[net].index == 0)
  1357. {
  1358. if(rt_if_ip_get(tRunPara.tNetPara[net].ethid, buf)!=0)
  1359. {
  1360. continue;
  1361. }
  1362. }
  1363. else
  1364. {
  1365. if(rt_if_ip_get2(tRunPara.tNetPara[net].ethid, tRunPara.tNetPara[net].index, buf)!=0)
  1366. {
  1367. continue;
  1368. }
  1369. }
  1370. // 运行应用104线程
  1371. sprintf(buf,"net_104_%d",net);
  1372. pid = kernel_thread((int (*)(void *))_net_104_thread,&g_t104[net],CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  1373. g_ts_104[net] = find_task_by_vpid(pid);
  1374. if(IS_ERR(g_ts_104[net]))
  1375. {
  1376. printk("ts=%p\r\n",g_ts_104[net]);
  1377. g_ts_104[net] = NULL;
  1378. return -1;
  1379. }
  1380. // 设置调度策略和优先级
  1381. sp.sched_priority = 8;
  1382. sched_setscheduler(g_ts_104[net],SCHED_FIFO,&sp);
  1383. // 设置进程名字
  1384. set_task_comm(g_ts_104[net], buf);
  1385. }
  1386. return 0;
  1387. }
  1388. int net_104_exit(void)
  1389. {
  1390. int net;
  1391. for(net=0; net<CFG_ETH_MAX_LOGIC; net++)
  1392. {
  1393. if(g_ts_104[net] != NULL)
  1394. {
  1395. //kthread_stop(g_ts_104[net]);
  1396. g_net_104_exit_flag[net] = 1;
  1397. wait_for_completion(&g_exit_completion_net_104[net]);
  1398. }
  1399. }
  1400. return 0;
  1401. }
  1402. int net_104_send_ext(int net,unsigned char *buf,int len)
  1403. {
  1404. int ret = 0;
  1405. int ch = net+CFG_UART_NUM_MAX;
  1406. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1407. {
  1408. char sz[16];
  1409. sprintf(sz,"\r\nTX[%d]:",ch);
  1410. print_mem(sz,buf,len);
  1411. rt_printf("\r\n");
  1412. }
  1413. down(&net_s_lock[net]);
  1414. if(g_104_client[net] != RT_SOCKET_ERR)
  1415. {
  1416. ret = rt_send(g_104_client[net],buf,len,0);
  1417. }
  1418. up(&net_s_lock[net]);
  1419. if(ret < 0)
  1420. {
  1421. rt_printf("net_104_send:ret=%d.\r\n",ret);
  1422. }
  1423. return ret;
  1424. }
  1425. int net_104_send(int net,unsigned char *buf,int len)
  1426. {
  1427. int ret = 0;
  1428. u8 ti,cot,se;
  1429. u8 *out;
  1430. if(pRunSet->bTT_ESAM)
  1431. {
  1432. iec_GetMsgInfo(buf,len,&ti,&cot,&se,false);
  1433. len = sec_encrpt_iec(ti,cot,se,buf,len,&out);
  1434. if(len > 0)
  1435. {
  1436. ret=net_104_send_ext(net,out, len);
  1437. }
  1438. }
  1439. else
  1440. {
  1441. ret=net_104_send_ext(net,buf, len);
  1442. }
  1443. return ret;
  1444. }
  1445. static int net_104_recv(int net, unsigned char *buf,int len)
  1446. {
  1447. int ret = 0;
  1448. down(&net_r_lock[net]);
  1449. if(g_104_client[net] != RT_SOCKET_ERR)
  1450. {
  1451. ret = rt_recv (g_104_client[net], (void *)buf, NET_RECV_MAX, MSG_DONTWAIT);
  1452. }
  1453. up(&net_r_lock[net]);
  1454. return ret;
  1455. }
  1456. void net_104_close(int net)
  1457. {
  1458. down(&net_r_lock[net]);
  1459. down(&net_s_lock[net]);
  1460. if(g_104_client[net] != RT_SOCKET_ERR)
  1461. {
  1462. rt_socket_close (g_104_client[net]);
  1463. g_104_client[net] = RT_SOCKET_ERR;
  1464. }
  1465. g_net_104_close_flag[net] = 0;
  1466. up(&net_s_lock[net]);
  1467. up(&net_r_lock[net]);
  1468. }
  1469. void net_104_close_flag(int net)
  1470. {
  1471. down(&net_r_lock[net]);
  1472. down(&net_s_lock[net]);
  1473. if(g_104_client[net] != RT_SOCKET_ERR)
  1474. {
  1475. g_net_104_close_flag[net] = 1;
  1476. }
  1477. up(&net_s_lock[net]);
  1478. up(&net_r_lock[net]);
  1479. }
  1480. int net_104_is_connect(int net)
  1481. {
  1482. if((g_104_client[net] == RT_SOCKET_ERR) || g_net_104_close_flag[net])
  1483. {
  1484. return 0;
  1485. }
  1486. return 1;
  1487. }
  1488. /*------------------------------ 内部函数 -------------------------------------
  1489. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  1490. */
  1491. extern int _net_104Client_thread(int net);
  1492. int _net_104_thread(IEC104_DEF *pt104)
  1493. {
  1494. struct sockaddr_in server_addr;
  1495. struct sockaddr_in client_addr;
  1496. struct linger ling;
  1497. SOCKET sk_server,sk_tmp ;
  1498. int net;
  1499. int sin_size;
  1500. int len,ret;
  1501. unsigned char *ip;
  1502. unsigned int ip_server;
  1503. unsigned int recv_timeout,recv_timeout_set;
  1504. unsigned int link_timeout=dTCounter;
  1505. unsigned char buf;
  1506. u16 w104Port;
  1507. if(tRunPara.b104Client) // 104采用客户端
  1508. {
  1509. net = pt104->cNetIndex;
  1510. _net_104Client_thread(net);
  1511. return 0;
  1512. }
  1513. NET_BEGIN:
  1514. net = pt104->cNetIndex;
  1515. // 服务器端开始建立socket描述符
  1516. //sk_server = rt_socket(AF_INET, SOCK_STREAM, 0);
  1517. sk_server = rt_socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);//noted by sunxi: 20221212 设为非阻塞型
  1518. if (sk_server == RT_SOCKET_ERR)
  1519. {
  1520. RT_PRINTF_POSITION();
  1521. g_ts_104[net] = NULL;
  1522. return -1;
  1523. }
  1524. // 服务器端填充sockaddr结构
  1525. memset (&server_addr, 0, sizeof (struct sockaddr_in));
  1526. server_addr.sin_family = AF_INET;
  1527. //rt_if_ip_get(net,(unsigned char *)&ip_server);
  1528. //ret=get_ethcnt(net, tRunPara.tNetPara[net].ethid);
  1529. //IP在物理网卡上
  1530. if( tRunPara.tNetPara[net].index == 0)
  1531. {
  1532. rt_if_ip_get(tRunPara.tNetPara[net].ethid, (unsigned char *)&ip_server);
  1533. w104Port = tRunPara.w104Port; // 网络1、2使用可变端口,保证级联时不冲突。
  1534. }
  1535. else //IP在虚拟网卡上
  1536. {
  1537. //从虚拟网卡获取IP地址
  1538. rt_printf("%s 获取虚拟网卡IP地址:ethid=%d,index=%d\r\n",__func__,tRunPara.tNetPara[net].ethid,tRunPara.tNetPara[net].index);
  1539. rt_if_ip_get2(tRunPara.tNetPara[net].ethid, tRunPara.tNetPara[net].index, (unsigned char *)&ip_server);
  1540. w104Port = NET_104_PORT; // 调试网络使用固定的2404端口,保证维护工具可以连接。
  1541. }
  1542. server_addr.sin_addr.s_addr = htonl (ip_server);
  1543. server_addr.sin_port = htons (w104Port);
  1544. // 捆绑sockfd描述符
  1545. ret = rt_bind (sk_server, (struct sockaddr *)(&server_addr), sizeof (struct sockaddr));
  1546. if (ret < 0)
  1547. {
  1548. RT_PRINTF_POSITION();
  1549. rt_socket_close (sk_server);
  1550. g_ts_104[net] = NULL;
  1551. return -2;
  1552. }
  1553. // 监听sockfd描述符
  1554. ret = rt_listen (sk_server, 1);
  1555. if (ret < 0)
  1556. {
  1557. RT_PRINTF_POSITION();
  1558. rt_socket_close (sk_server);
  1559. g_ts_104[net] = NULL;
  1560. return -3;
  1561. }
  1562. // 设置关闭延时
  1563. ling.l_onoff = 1;
  1564. ling.l_linger = 0;
  1565. rt_setsockopt(sk_server,SOL_SOCKET,SO_LINGER,(char *)&ling ,sizeof(struct linger));
  1566. ip = (unsigned char *)&ip_server;
  1567. //rt_printf("应用104线程已启动(IP:%d.%d.%d.%d,PORT:%d)。\r\n",ip[0],ip[1],ip[2],ip[3],NET_104_PORT);
  1568. while(1)
  1569. {
  1570. msleep(100);//noted by sunxi: 20221212 设为非阻塞型
  1571. // 服务器阻塞,直到客户程序建立连接
  1572. sin_size = sizeof (struct sockaddr_in);
  1573. sk_tmp = rt_accept (sk_server, (struct sockaddr*)(&client_addr), &sin_size);
  1574. // 检查是否退出
  1575. if(g_net_104_exit_flag[net])
  1576. {
  1577. // 关闭客户端
  1578. net_104_close(net);
  1579. // 关闭服务器
  1580. rt_socket_close (sk_server);
  1581. complete_and_exit(&g_exit_completion_net_104[net], 0);
  1582. return 0;
  1583. }
  1584. //是否重新初始化
  1585. if(g_net_reinit[net] == NET_SOCKET_CLOSING)
  1586. {
  1587. // 关闭客户端
  1588. net_104_close(net);
  1589. // 关闭服务器
  1590. rt_socket_close (sk_server);
  1591. //置已经关闭标志
  1592. g_net_reinit[net] = NET_SOCKET_CLOSED;
  1593. //等待IP修改完成
  1594. while(g_net_reinit[net] != NET_SOCKET_REINIT)
  1595. {
  1596. msleep(100);
  1597. }
  1598. //置为初始化
  1599. g_net_reinit[net] = NET_SOCKET_INIT;
  1600. goto NET_BEGIN;
  1601. }
  1602. //注意,用指针类型的socket接口,失败是返回NULL
  1603. //而整数类型的socket接口,失败返回的是负数
  1604. #ifdef RT_SOCKET_FD
  1605. if (sk_tmp < 0)
  1606. {
  1607. //msleep(100);//noted by sunxi: 20221212 设为非阻塞型
  1608. // 超时
  1609. continue;
  1610. }
  1611. #else
  1612. if (sk_tmp == RT_SOCKET_ACCEPT_RST)
  1613. {
  1614. //msleep(100);//noted by sunxi: 20221212 设为非阻塞型
  1615. // 超时
  1616. continue;
  1617. }
  1618. #endif
  1619. g_104_client[net] = sk_tmp;
  1620. ip = (unsigned char *)&client_addr.sin_addr.s_addr;
  1621. rt_printf("104 client[net=%d,s=%d] connect(ip=%d.%d.%d.%d)!\r\n", net ,sk_tmp,ip[0],ip[1],ip[2],ip[3]);
  1622. // 检查是否主站IP
  1623. if(tRunPara.ip104Master1 || tRunPara.ip104Master2 || tRunPara.ip104Master3 || tRunPara.ip104Master4)
  1624. {
  1625. u32 network0,network1;
  1626. network0 = client_addr.sin_addr.s_addr & tRunPara.tNetPara[CFG_ETH_MAX_LOGIC-1].wMask;
  1627. network1 = tRunPara.tNetPara[CFG_ETH_MAX_LOGIC-1].wIp & tRunPara.tNetPara[CFG_ETH_MAX_LOGIC-1].wMask;
  1628. if( (client_addr.sin_addr.s_addr != tRunPara.ip104Master1)
  1629. && (client_addr.sin_addr.s_addr != tRunPara.ip104Master2)
  1630. && (client_addr.sin_addr.s_addr != tRunPara.ip104Master3)
  1631. && (client_addr.sin_addr.s_addr != tRunPara.ip104Master4)
  1632. && (g_ip_net_maintain == 0) // 如果维护工具已连接,任意104主站可以接入。
  1633. &&(network0 != network1)) // 运行维护网络的104连接进来
  1634. {
  1635. net_104_close(net);
  1636. rt_printf("主站IP不在允许范围内!\r\n");
  1637. ip = (unsigned char *)&tRunPara.ip104Master1;
  1638. rt_printf("允许IP1:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  1639. ip = (unsigned char *)&tRunPara.ip104Master2;
  1640. rt_printf("允许IP2:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  1641. ip = (unsigned char *)&tRunPara.ip104Master3;
  1642. rt_printf("允许IP3:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  1643. ip = (unsigned char *)&tRunPara.ip104Master4;
  1644. rt_printf("允许IP4:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  1645. ip = (unsigned char *)&g_ip_net_maintain;
  1646. rt_printf("允许IP5:%d.%d.%d.%d)!\r\n",ip[0],ip[1],ip[2],ip[3]);
  1647. msleep(500);
  1648. continue;
  1649. }
  1650. }
  1651. // 客户端已连接,处理客户端命令
  1652. // 初始化接收FIFO
  1653. rt_fifo_init(&g_net_104_fifo[net],g_net_104_buf[net],NET_104_BUF_SIZE);
  1654. IEC104_Recv_Reset(&g_t104[net]);
  1655. memcpy(g_t104[net].ip,(unsigned char *)&client_addr.sin_addr.s_addr,4);
  1656. g_t104[net].LinkCountNet++;
  1657. //接收帧超时初始化,超时设置为测试超时加确认超时后的2倍。
  1658. recv_timeout=dTCounter;
  1659. recv_timeout_set = (tRunPara.w104TestTime + tRunPara.w104AckTime)*2*T_1s;
  1660. // 循环处理接收数据
  1661. while(1)
  1662. {
  1663. msleep(2);//noted by sunxi: 20221212 设为非阻塞型
  1664. len = net_104_recv(net, g_net_104_buf_frame[net], NET_RECV_MAX);
  1665. // 检查是否退出
  1666. if(g_net_104_exit_flag[net])
  1667. {
  1668. // 关闭客户端
  1669. net_104_close(net);
  1670. // 关闭服务器
  1671. rt_socket_close (sk_server);
  1672. complete_and_exit(&g_exit_completion_net_104[net], 0);
  1673. return 0;
  1674. }
  1675. //是否重新初始化
  1676. if(g_net_reinit[net] == NET_SOCKET_CLOSING)
  1677. {
  1678. // 关闭客户端
  1679. net_104_close(net);
  1680. // 关闭服务器
  1681. rt_socket_close (sk_server);
  1682. //置已经关闭标志
  1683. g_net_reinit[net] = NET_SOCKET_CLOSED;
  1684. //等待IP修改完成
  1685. while(g_net_reinit[net] != NET_SOCKET_REINIT)
  1686. {
  1687. msleep(100);
  1688. }
  1689. //置为初始化
  1690. g_net_reinit[net] = NET_SOCKET_INIT;
  1691. rt_printf("net_104_thread:链路重新初始化!\r\n");
  1692. goto NET_BEGIN;
  1693. }
  1694. if(g_net_104_close_flag[net])
  1695. {
  1696. break;
  1697. }
  1698. if(len == -EAGAIN)
  1699. {
  1700. if( rt_get_net_linkstatus(rt_get_netcard_id_from_socket(g_104_client[net] ))==1)
  1701. {
  1702. if((dTCounter - link_timeout)>RT_LINK_TIMEOUT*T_1s) //500ms超时
  1703. {
  1704. rt_printf("net_104_thread: link down\r\n");
  1705. link_timeout = dTCounter;
  1706. break;
  1707. }
  1708. }
  1709. else
  1710. {
  1711. link_timeout=dTCounter;
  1712. }
  1713. // 接收帧超时
  1714. if((dTCounter - recv_timeout)>recv_timeout_set)
  1715. {
  1716. rt_printf ("net_104_thread:接收帧超时(value=%d,set=%d)\r\n",recv_timeout,recv_timeout_set);
  1717. break;
  1718. }
  1719. }
  1720. else if (len <= 0)
  1721. {
  1722. //调用recv时,如果返回0,则表示对端关闭;返回负数,表示出错
  1723. rt_printf ("net_104_thread:rt_recv(ret=%d)\r\n",len);
  1724. // TODO:以下休眠语句必须屏蔽,否则net_104_close会延时调用,
  1725. // 导致主循环可以在此断开的socket上继续发送数据,引起系统异常。
  1726. // msleep(100);
  1727. break;
  1728. }
  1729. else
  1730. {
  1731. int space;
  1732. space = rt_fifo_space(&g_net_104_fifo[net]);
  1733. if(space > len)
  1734. {
  1735. rt_fifo_put(&g_net_104_fifo[net],g_net_104_buf_frame[net],len);
  1736. if(tRunPara.tNetPara[net].ethid<CFG_ETH_NUM_MAX)
  1737. g_run_stu.led[CFG_UART_NUM_MAX*2+tRunPara.tNetPara[net].ethid]=1;
  1738. }
  1739. else
  1740. {
  1741. rt_printf ("net_104_thread:接收FIFO溢出(space=%d,len=%d)\r\n",space,len);
  1742. }
  1743. link_timeout=dTCounter;
  1744. }
  1745. // 接收数据
  1746. while(g_t104[net].bSM2Data== false && g_t104[net].bRecvOk == false)
  1747. {
  1748. if(rt_fifo_get(&g_net_104_fifo[net],&buf,1))
  1749. {
  1750. if(g_print_comm_raw && (g_print_port & (1<<(net+CFG_UART_NUM_MAX))))
  1751. {
  1752. rt_printf("%02x ",buf);
  1753. }
  1754. if(IEC104_Recv(&g_t104[net],buf))
  1755. {
  1756. // 如果接收到完整的一帧104报文(其它数据不清定时器,防止意外连接),超时定时器清零。
  1757. recv_timeout=dTCounter;
  1758. }
  1759. }
  1760. else
  1761. {
  1762. break;
  1763. }
  1764. }
  1765. }
  1766. // 关闭客户端socket
  1767. net_104_close(net);
  1768. }
  1769. // 理论上,程序不会运行到这儿
  1770. return 0;
  1771. }
  1772. int _net_104Client_thread(int net)
  1773. {
  1774. struct sockaddr_in server_addr;
  1775. struct linger ling;
  1776. SOCKET s;
  1777. int len,ret;
  1778. unsigned int ip_server,link_count;
  1779. unsigned int recv_timeout;
  1780. unsigned char buf;
  1781. unsigned char *ip;
  1782. // int net = *pnet;
  1783. while(1)
  1784. {
  1785. BEGIN:
  1786. link_count = 0;
  1787. // 检查级联IP是否设置
  1788. while(1)
  1789. {
  1790. ip_server = tRunPara.ip104Master[net];
  1791. if(ip_server)
  1792. {
  1793. break;
  1794. }
  1795. // 检查是否退出
  1796. if(g_net_104_exit_flag[net])
  1797. {
  1798. // rt_printf_time("net_104link_thread exit.\r\n");
  1799. complete_and_exit(&g_exit_completion_net_104[net], 0);
  1800. return 0;
  1801. }
  1802. // 1秒钟检查一次
  1803. msleep(1000);
  1804. }
  1805. ip = (unsigned char *)&ip_server;
  1806. rt_printf_time("104连接服务器开始(%d):ip=%d.%d.%d.%d!\r\n",net,ip[0],ip[1],ip[2],ip[3]);
  1807. // 创建socket
  1808. s = rt_socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
  1809. if (s == RT_SOCKET_ERR)
  1810. {
  1811. RT_PRINTF_POSITION();
  1812. return -1;
  1813. }
  1814. // 服务器端填充sockaddr结构
  1815. memset (&server_addr, 0, sizeof (struct sockaddr_in));
  1816. server_addr.sin_family = AF_INET;
  1817. server_addr.sin_addr.s_addr = htonl (ip_server);
  1818. server_addr.sin_port = htons (tRunPara.w104Port);
  1819. // 连接服务器,没有连接成功不退出
  1820. while(1)
  1821. {
  1822. ret = rt_connect(s,(struct sockaddr *)&server_addr,sizeof(struct sockaddr_in));
  1823. // 检查是否退出
  1824. if(g_net_104_exit_flag[net])
  1825. {
  1826. // 关闭客户端
  1827. g_104_client[net] = RT_SOCKET_ERR;
  1828. rt_socket_close (s);
  1829. rt_printf_time("net_104_thread exit(rt_recv).\r\n");
  1830. complete_and_exit(&g_exit_completion_net_104[net], 0);
  1831. return 0;
  1832. }
  1833. // 检查服务器是否变更
  1834. if(ip_server != tRunPara.ip104Master[net])
  1835. {
  1836. // 关闭客户端
  1837. g_104_client[net] = RT_SOCKET_ERR;
  1838. rt_socket_close (s);
  1839. ip = (unsigned char *)&tRunPara.ip104Master[net];
  1840. rt_printf_time("主站服务器变更(%d):ip=%d.%d.%d.%d!\r\n",net,ip[0],ip[1],ip[2],ip[3]);
  1841. goto BEGIN;
  1842. }
  1843. if (ret == 0)
  1844. {
  1845. g_net_104_close_flag[net] = 0;
  1846. g_link104_init_ok[net] = 0;
  1847. g_104_client[net] = s;
  1848. g_t104[net].bClientStart=false;
  1849. link_count++;
  1850. rt_printf_time("104连接服务器成功(通道=%d,连接计数=%d):ip=%d.%d.%d.%d!\r\n",net,link_count,ip[0],ip[1],ip[2],ip[3]);
  1851. break;
  1852. }
  1853. // 1秒钟连接一次
  1854. msleep(1000);
  1855. }
  1856. // 设置关闭延时
  1857. ling.l_onoff = 1;
  1858. ling.l_linger = 0;
  1859. rt_setsockopt(s,SOL_SOCKET,SO_LINGER,(char *)&ling ,sizeof(struct linger));
  1860. // 初始化FIFO
  1861. rt_fifo_init(&g_net_104_fifo[net],g_net_104_buf[net],NET_104_BUF_SIZE);
  1862. // 处理接收
  1863. recv_timeout=dTCounter;
  1864. while(1)
  1865. {
  1866. msleep(2);//noted by sunxi: 20221212 设为非阻塞型
  1867. len = rt_recv (s, g_net_104_buf_frame[net], NET_RECV_MAX, MSG_DONTWAIT);
  1868. // 检查是否退出
  1869. if(g_net_104_exit_flag[net])
  1870. {
  1871. // 关闭客户端
  1872. g_104_client[net] = RT_SOCKET_ERR;
  1873. rt_socket_close (s);
  1874. rt_printf_time("net_104client_thread exit(rt_recv).\r\n");
  1875. complete_and_exit(&g_exit_completion_net_104[net], 0);
  1876. return 0;
  1877. }
  1878. // 检查服务器是否变更
  1879. if(ip_server != tRunPara.ip104Master[net])
  1880. {
  1881. // 关闭客户端
  1882. g_104_client[net] = RT_SOCKET_ERR;
  1883. rt_socket_close (s);
  1884. rt_printf_time("104服务器变更(%d):ip=%d.%d.%d.%d!\r\n",net,ip[0],ip[1],ip[2],ip[3]);
  1885. goto BEGIN;
  1886. }
  1887. // 应用程序主动关闭
  1888. if(g_net_104_close_flag[net])
  1889. {
  1890. break;
  1891. }
  1892. if(len == -EAGAIN)
  1893. {
  1894. // 超时
  1895. if((dTCounter - recv_timeout)>30*T_1s) // 超时30s,端口未收到数据,关闭端口
  1896. {
  1897. rt_printf_time("104客户端30S超时关闭(net=%d)\r\n",net);
  1898. break;
  1899. }
  1900. }
  1901. else if (len <= 0)
  1902. {
  1903. //调用recv时,如果返回0,则表示对端关闭;返回负数,表示出错
  1904. rt_printf_time ("104客户端关闭(net=%d,ret=%d)\r\n",net,len);
  1905. msleep(100);
  1906. break;
  1907. }
  1908. else
  1909. {
  1910. rt_fifo_put(&g_net_104_fifo[net],g_net_104_buf_frame[net],len);
  1911. recv_timeout=dTCounter; //超时计数器清零
  1912. if(tRunPara.tNetPara[net].ethid<CFG_ETH_NUM_MAX)
  1913. g_run_stu.led[CFG_UART_NUM_MAX*2+tRunPara.tNetPara[net].ethid]=1;
  1914. }
  1915. // 接收数据
  1916. while(g_t104[net].bSM2Data== false && g_t104[net].bRecvOk == false)
  1917. {
  1918. if(rt_fifo_get(&g_net_104_fifo[net],&buf,1))
  1919. {
  1920. IEC104_Recv(&g_t104[net],buf);
  1921. }
  1922. else
  1923. {
  1924. break;
  1925. }
  1926. }
  1927. }
  1928. // 关闭客户端socket
  1929. g_104_client[net] = RT_SOCKET_ERR;
  1930. rt_socket_close (s);
  1931. // rt_printf_time ("net_104link_thread:rt_recv(ret=%d)\r\n",len);
  1932. }
  1933. // 理论上,程序不会运行到这儿
  1934. return 0;
  1935. }
  1936. #endif
  1937. /*------------------------------ 测试函数 -------------------------------------
  1938. 一个实体文件必须带一个本模块的测试函数来进行单元测试,如果的确不方便在本模块中
  1939. 进行单元测试,必须在此注明实际的测试位置(例如在哪个实体文件中使用哪个测试函数).
  1940. */
  1941. /*------------------------------ 文件结束 -------------------------------------
  1942. */