bmc_main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "pthread.h"
  4. #include "errno.h"
  5. #include "bmc_main.h"
  6. #include "bmc_conf.h"
  7. #include "bmc_type.h"
  8. #include "sensor_thread.h"
  9. #include "power_thread.h"
  10. #include "ipmb_thread.h"
  11. #include "timer_thread.h"
  12. #include "fcntl.h"
  13. #include "./../stm32f429xx.h"
  14. #include "./../driver/GPIO/gpio.h"
  15. #include "platform.h"
  16. #include "./../driver/I2C/i2c.h"
  17. #include "i2c_api.h"
  18. #include "spi_api.h"
  19. #include "gpio_api.h"
  20. #include "./lan/udp_api/udp.h"
  21. #include "./test.h"
  22. #include <semaphore.h>
  23. #include "./lan/lanTask.h"
  24. #include <stdlib.h>
  25. #include "MsgHndlr.h"
  26. #include "sensor_sdr_init.h"
  27. #include <sys/types.h> /* See NOTES */
  28. #include <sys/socket.h>
  29. #include <string.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <unistd.h>
  34. #include <stdio.h>
  35. pthread_t gthreadIDs[MAX_THREAD]={0};
  36. int gthreadIndex = 0;
  37. BMCInfo_t g_BMCInfo;
  38. char SessionSequenceNumberCount=0;
  39. /* mutex */
  40. pthread_mutex_t sensorSdr_mutex; //when modify sdr
  41. pthread_mutex_t powerSta_mutex;
  42. sem_t sem_QUEUE;
  43. IPMIQueue_T g_IPMIIfcQueue[MAX_IPMI_IFCQ];
  44. int InitBMCSharedMem (void);
  45. int InitBmcInfo(void);
  46. #define SERVER_PORT 7897
  47. #define BACKLOG 10
  48. /*
  49. * The bmc main function.
  50. * All threads are created in here.
  51. * Author: Jimbo
  52. */
  53. int main(int argc, char **argv)
  54. {
  55. int iSocketServer;
  56. int iSocketClient;
  57. struct sockaddr_in tSocketServerAddr; //指定服务器绑定地址
  58. struct sockaddr_in tSocketClientAddr; //保存客户端地址
  59. int iRet;
  60. int iAddrLen;
  61. uint32_t recvCnt = 0;
  62. int iRecvLen;
  63. unsigned char ucRecvBuf[2000];
  64. char sendBuf[1000] = {0};
  65. int iClientNum = -1;
  66. // signal(SIGCHLD,SIG_IGN); //此函数用于处理僵尸进程
  67. iSocketServer = socket(AF_INET, SOCK_STREAM, 0);//AF_INET IPV4连接 SOCK_STREAM启动TCP连接
  68. if (-1 == iSocketServer)
  69. {
  70. printf("socket error!\n");
  71. return -1;
  72. }
  73. tSocketServerAddr.sin_family = AF_INET; //一般设置为
  74. tSocketServerAddr.sin_port = htons(SERVER_PORT); //将SERVER_PORT转化为网络字节序 host to net, short
  75. tSocketServerAddr.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY表示本机上所有IP
  76. memset(tSocketServerAddr.sin_zero, 0, 8);
  77. int opt = 1;
  78. //使用setsockopt函数可以保证端口可被重复绑定
  79. iRet = setsockopt(iSocketServer, SOL_SOCKET,SO_REUSEADDR,(const void *)&opt, sizeof(opt) );
  80. if (-1 == iRet)
  81. {
  82. printf("set sock option error!\n");
  83. close(iSocketServer);
  84. return -1;
  85. }
  86. iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr)); //绑定端口
  87. if (-1 == iRet)
  88. {
  89. printf("bind error!\n");
  90. close(iSocketServer);
  91. return -1;
  92. }
  93. iRet = listen(iSocketServer, BACKLOG); //设置监听 BACKLOG代表同时监听10路连接
  94. if (-1 == iRet)
  95. {
  96. printf("listen error!\n");
  97. return -1;
  98. }
  99. while (1)
  100. {
  101. iAddrLen = sizeof(struct sockaddr);
  102. iSocketClient = accept(iSocketServer, (struct sockaddr *)&tSocketClientAddr, &iAddrLen); //等待连接 如果建立连接
  103. if (-1 != iSocketClient)
  104. {
  105. iClientNum++;
  106. printf("Get connect from client %d : %s\n", iClientNum, inet_ntoa(tSocketClientAddr.sin_addr));
  107. recvCnt = 0;
  108. while(1)
  109. {
  110. /* 接收客户端发来的数据并显示出来 */
  111. iRecvLen = recv(iSocketClient, ucRecvBuf, 2000, 0);
  112. if (iRecvLen <= 0)
  113. {
  114. printf("Recv count %d Byte!\n", recvCnt);
  115. close(iSocketClient); //关闭连接
  116. //return -1;
  117. break;
  118. }
  119. else
  120. {
  121. if(iRecvLen != 500)
  122. printf("Recv error: %d\n",iRecvLen);
  123. recvCnt += iRecvLen;
  124. //sprintf(sendBuf, "%d\n", recvCnt);
  125. // ucRecvBuf[iRecvLen] = '\0';
  126. // printf("Get Msg From Client %d: %s\n", iClientNum, ucRecvBuf);
  127. //send(iSocketClient, sendBuf, 11, 0);//打印的同时发送一条数据给连接的客户端
  128. }
  129. }
  130. }
  131. }
  132. close(iSocketServer);
  133. return 0;
  134. /*
  135. if(sem_init(&sem_QUEUE, 0, 1) ==-1){
  136. printf("A lock for IPMI QUEUE is not created properly!!!\n");
  137. }
  138. system("ifconfig eth0 192.168.0.15");
  139. // platform_init();
  140. spi_api_read();
  141. InitBmcInfo();
  142. sensor_sdr_init();
  143. memset(g_IPMIIfcQueue,0,(sizeof(IPMIQueue_T)*MAX_IPMI_IFCQ));
  144. InitBMCSharedMem ();
  145. //create fifo
  146. {
  147. OS_CREATE_Q(OBSM_TASK_Q);
  148. OS_GET_Q(OBSM_TASK_Q,O_RDWR);
  149. OS_CREATE_Q(MSG_HNDLR_Q);
  150. OS_GET_Q(MSG_HNDLR_Q,O_RDWR);
  151. }
  152. // create task handler thread
  153. if(0 != pthread_create(&gthreadIDs[gthreadIndex++], NULL,LanTask, NULL))
  154. {
  155. printf("Error: file:%s,line:%d,create message thread error!\n"\
  156. __FILE__, __LINE__);
  157. }
  158. //create message handler thread MsgHndlr
  159. if(0 != pthread_create(&gthreadIDs[gthreadIndex++], NULL,MsgHndlr, NULL))
  160. {
  161. printf("Error: file:%s,line:%d,create message thread error!\n"\
  162. __FILE__, __LINE__);
  163. }
  164. */
  165. /*
  166. //init mutex lock
  167. if(pthread_mutex_init(&sensorSdr_mutex,NULL) != 0)
  168. {
  169. printf("Error: Create sensorSdr_mutex error!\n");
  170. }
  171. if(pthread_mutex_init(&powerSta_mutex,NULL) != 0)
  172. {
  173. printf("Error: Create powerSta_mutex error!\n");
  174. }
  175. */
  176. /*
  177. //create message handler thread
  178. if(0 != pthread_create(&gthreadIDs[gthreadIndex++], NULL, timer_task, NULL))
  179. {
  180. printf("Error: file:%s,line:%d,create timer thread error!\n"\
  181. __FILE__, __LINE__);
  182. }
  183. //create power manage thread
  184. #if(CONF_POWER_SUPPORT == 1)
  185. {
  186. if(0 != pthread_create(&gthreadIDs[gthreadIndex++], NULL, power_main, NULL))
  187. {
  188. printf("Error: file:%s,line:%d,create power thread error!\n",\
  189. __FILE__, __LINE__);
  190. }
  191. }
  192. #endif
  193. //create sensor manage thread
  194. #if(CONF_SENSOR_SUPPORT == 1)
  195. {
  196. if(0 != pthread_create(&gthreadIDs[gthreadIndex++], NULL, sensor_main, NULL))
  197. {
  198. printf("Error: file:%s,line:%d,create sensor thread error!\n",\
  199. __FILE__, __LINE__);
  200. }
  201. }
  202. #endif
  203. //create ipmb thread
  204. #if(CONF_IPMB_SUPPORT == 1)
  205. {
  206. if(0 != pthread_create(&gthreadIDs[gthreadIndex++], NULL, ipmb_main, NULL))
  207. {
  208. printf("Error: file:%s,line:%d,create ipmb thread error!\n"\
  209. __FILE__, __LINE__);
  210. }
  211. }
  212. #endif
  213. */
  214. // udp_server(argc,&(*argv));
  215. /*for(i=0x4c;i<0x4f;i++)
  216. {
  217. if(0 == I2C_Master_Transmit(&i2c_dev,0x98, NULL, 0))
  218. {
  219. printf("---> Find device %#x\n",i);
  220. }
  221. }*/
  222. /* printf("------------ I2C2 ---------------\n");
  223. i2c_dev.bus = I2C2;
  224. //printf("---> i2c_dev.bus = %#x, I2C2=%#x\n",(uint32_t)i2c_dev.bus,(uint32_t)I2C2);
  225. for(i=0x4c;i<0x4f;i++)
  226. {
  227. if(0 == I2C_Master_Transmit(&i2c_dev,i<<1, NULL, 0))
  228. {
  229. printf("---> Find device %#x\n",i);
  230. }
  231. }
  232. printf("------------ I2C3 ---------------\n");
  233. i2c_dev.bus = I2C3;
  234. //printf("---> i2c_dev.bus = %#x, I2C3=%#x\n",(uint32_t)i2c_dev.bus,(uint32_t)I2C3);
  235. for(i=0x4c;i<0x4f;i++)
  236. {
  237. if(0 == I2C_Master_Transmit(&i2c_dev,i<<1, NULL, 0))
  238. {
  239. printf("---> Find device %#x\n",i);
  240. }
  241. }
  242. */
  243. // printf("---> end test!\n");
  244. /* gpio_data_t gpin_data;
  245. while(1)
  246. {
  247. //printf("---> turn on!\n");
  248. gpin_data.port = GPIOD;
  249. gpin_data.pin = GPIO_PIN_12;
  250. gpin_data.data = 0;
  251. set_gpio_value(&gpin_data);
  252. sleep(1);
  253. //printf("---> turn off!\n");
  254. gpin_data.port = GPIOD;
  255. gpin_data.pin = GPIO_PIN_12;
  256. gpin_data.data = 1;
  257. set_gpio_value(&gpin_data);
  258. sleep(1);
  259. }*/
  260. }
  261. int InitBmcInfo(void)
  262. {
  263. //init sensorInfo
  264. int i = 0;
  265. int j = 0;
  266. for(i=0;i<MAX_SENSOR_NUM;i++) //init sdr
  267. {
  268. g_BMCInfo.sensorInfo[i].sensorSdr = getSensorSdr(i);
  269. }
  270. for(i=0;i<MAX_SENSOR_NUM;i++) //init sensor table
  271. {
  272. g_BMCInfo.sensorInfo[i].sensorDev = getSensorDev(i);
  273. }
  274. for(i=0;i<MAX_SENSOR_NUM;i++)
  275. {
  276. g_BMCInfo.SDRConfig.RecordDataOffset[i]=0;
  277. }
  278. for(i=0;i<MAX_SENSOR_NUM;i++)
  279. {
  280. if(i<2)
  281. for(j=0;j<6;j++)
  282. if(j<3)
  283. g_BMCInfo.SDRConfig.SDR[i][j] =0x80;
  284. else
  285. g_BMCInfo.SDRConfig.SDR[i][j] =0x7f;
  286. else
  287. for(j=0;j<6;j++)
  288. if(j<3)
  289. g_BMCInfo.SDRConfig.SDR[i][j] =0x00;
  290. else
  291. g_BMCInfo.SDRConfig.SDR[i][j] =0xff;
  292. }
  293. }
  294. int InitBMCSharedMem ()
  295. {
  296. BMCInfo_t* pBMCInfo = &g_BMCInfo;
  297. pBMCInfo->MsgHndlrTblSize=7;
  298. memcpy(pBMCInfo->MsgHndlrTbl,m_MsgHndlrTbl,sizeof(m_MsgHndlrTbl));
  299. Set_BMCInfo_IPMIConfig_LANIfcSupport(1,1);
  300. Set_BMCInfo_IPMIConfig_SerialIfcSupport(1,1);
  301. BMCInfo_InitChConfig();
  302. return 0;
  303. }