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