net_104.c 56 KB

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