1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include "net.h"
- #include "iec104.h"
- #include "iec10x.h"
- static err_t bms_test_process(int fd, void *data, int len)
- {
- INT8U response[2] = {0xBB, 0xAA};
- if ((len == 2) && (((INT8U *)data)[0] == 0xAA) && (((INT8U *)data)[1] == 0xBB))
- {
- send(fd, (void *)response, sizeof(response), 0);
- }
- return 1;
- }
- static err_t bms_net_process(int fd, void *data, int len)
- {
- Iec10x_Scheduled(fd);
- Iec104_StateMachine();
- // INT8U response[2] = {0xBB, 0xAA};
- // if ((len == 2) && (((INT8U *)data)[0] == 0xAA) && (((INT8U *)data)[1] == 0xBB))
- // {
- // send(fd, (void *)response, sizeof(response), 0);
- // }
- return 1;
- }
- void net_task(void)
- {
- int StaCount = 0;
- INT8U buf[50];
- INT32S ret = 0;
- INT32S sockfd = -1, newfd = -1;
- INT32U len = 0;
- struct sockaddr_in svr_addr, clt_addr;
- INT16U Iec104_RecvLen;
- int err;
- int staid = StaCount++;
- svr_addr.sin_family = AF_INET;
- svr_addr.sin_port = htons(TCP_PORT);
- svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
- while (1)
- {
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- {
- continue;
- }
- ret = bind(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
- if (ret < 0)
- {
- lwip_close(sockfd);
- sockfd = -1;
- continue;
- }
- ret = listen(sockfd, 1);
- if (ret < 0)
- {
- lwip_close(sockfd);
- continue;
- }
- len = sizeof(clt_addr);
- newfd = accept(sockfd, NULL, NULL);
- while (-1 != newfd)
- {
- ret = recv(newfd, buf, sizeof(buf) - 1, 0);
- if (ret <= 0)
- {
- lwip_close(newfd);
- newfd = -1;
- break;
- }
- bms_net_process(newfd, buf, ret);
- Iec104_RecvLen = read(newfd, buf, 50);
- if (Iec104_RecvLen <= 0 || Iec104_RecvLen > 1500)
- {
- printf("the other side has been closed (%d).\n", Iec104_RecvLen);
- continue;
- }
- LOG("#####################received \n");
- DumpHEX(buf, Iec104_RecvLen);
- Iex104_Receive(buf, Iec104_RecvLen);
- }
- lwip_close(sockfd);
- sockfd = -1;
- // OSTimeDly(10);
- }
- }
|