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