Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
291 | f9daq | 1 | #include "SiTCP.h" |
2 | //------------------------------------------------------------------------------------------------------ |
||
3 | void SiTCPinit(){ |
||
4 | printf("SiTCP control --> Start.\n"); |
||
5 | WORD wVersionRequested; |
||
6 | WSADATA wsaData; |
||
7 | int err; |
||
8 | |||
9 | /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ |
||
10 | wVersionRequested = MAKEWORD(2, 2); |
||
11 | |||
12 | err = WSAStartup(wVersionRequested, &wsaData); |
||
13 | if (err != 0) { |
||
14 | /* Tell the user that we could not find a usable */ |
||
15 | /* Winsock DLL. */ |
||
16 | printf("WSAStartup failed with error: %d\n", err); |
||
17 | } |
||
18 | }; |
||
19 | //------------------------------------------------------------------------------------------------------ |
||
20 | void SiTCPclose(){ |
||
21 | WSACleanup(); |
||
22 | printf("SiTCPclose -> Done.\n"); |
||
23 | }; |
||
24 | //------------------------------------------------------------------------------------------------------ |
||
25 | int SiTCPSetIPPort(char* IpAddr, unsigned int tcp, unsigned int udp){ |
||
26 | sitcpIpAddr = IpAddr; |
||
27 | if(tcp<=0 || 65535<=tcp || udp<=0 || 65535<=udp){ |
||
28 | if(tcp<=0 || 65535<=tcp){ |
||
29 | printf("error : invalid port : tcp = %u\n", tcp); |
||
30 | } |
||
31 | if(udp<=0 || 65535<=udp){ |
||
32 | printf("error : invalid port : udp = %u\n", udp); |
||
33 | } |
||
34 | printf("exit(1);!!!\n"); |
||
35 | } |
||
36 | tcpPort = tcp; |
||
37 | udpPort = udp; |
||
38 | puts(""); |
||
39 | printf("SiTCP:IP Address = %s\n", sitcpIpAddr); |
||
40 | printf(" TCP Port = %u\n", tcpPort); |
||
41 | printf(" UDP Port = %u\n", udpPort); |
||
42 | puts(""); |
||
43 | return 1; |
||
44 | } |
||
45 | //------------------------------------------------------------------------------------------------------ |
||
46 | int SiTCPCreateTCPSock(){ |
||
47 | printf("Create socket for TCP..."); |
||
48 | tcpsock = socket(AF_INET, SOCK_STREAM, 0); |
||
49 | if(tcpsock < 0){ |
||
50 | perror("TCP socket"); |
||
51 | printf("errno = %d\n", errno); |
||
52 | printf("exit(1);!!!\n"); |
||
53 | } |
||
54 | memset(&tcpAddr, 0, sizeof(tcpAddr)); |
||
55 | tcpAddr.sin_family = AF_INET; |
||
56 | tcpAddr.sin_port = htons(tcpPort); |
||
57 | tcpAddr.sin_addr.s_addr = inet_addr(sitcpIpAddr); |
||
58 | printf(" Done\n"); |
||
59 | |||
60 | printf(" ->Trying to connect to %s ...\n", sitcpIpAddr); |
||
61 | if(connect(tcpsock, (struct sockaddr *)&tcpAddr, sizeof(tcpAddr)) < 0){ |
||
62 | if(errno != EINPROGRESS) perror("TCP connection"); |
||
63 | FD_ZERO(&rmask); |
||
64 | FD_SET(tcpsock, &rmask); |
||
65 | wmask = rmask; |
||
66 | timeout.tv_sec = 3; |
||
67 | timeout.tv_usec = 0; |
||
68 | |||
69 | int rc = select(tcpsock+1, &rmask, NULL, NULL, &timeout); |
||
70 | if(rc<0) perror("connect-select error"); |
||
71 | if(rc==0){ |
||
72 | puts("\n =====time out !=====\n "); |
||
73 | printf("exit(1);!!!\n"); |
||
74 | } |
||
75 | else{ |
||
76 | puts("\n ===connection error===\n "); |
||
77 | printf("exit(1);!!!\n"); |
||
78 | } |
||
79 | } |
||
80 | FD_ZERO(&readfds); |
||
81 | FD_SET(tcpsock, &readfds); |
||
82 | FD_SET(0, &readfds); |
||
83 | |||
84 | puts(" ->Connect success!"); |
||
85 | |||
86 | return 1; |
||
87 | } |
||
88 | //------------------------------------------------------------------------------------------------------ |
||
89 | int SiTCPCreateUDPSock(){ |
||
90 | printf("Create socket for RBCP..."); |
||
91 | udpsock = socket(AF_INET, SOCK_DGRAM, 0); |
||
92 | //if(udpsock < 0){ |
||
93 | if(udpsock == INVALID_SOCKET){ |
||
94 | //perror("UDP socket"); |
||
95 | //printf("errno = %d | udpsock = %d\n", errno, udpsock); |
||
96 | printf("errno = %d | udpsock = %d\n", WSAGetLastError(), udpsock); |
||
97 | printf("exit(1);!!!\n"); |
||
98 | } |
||
99 | memset(& SiTCPudpAddr, 0, sizeof( SiTCPudpAddr)); |
||
100 | SiTCPudpAddr.sin_family = AF_INET; |
||
101 | SiTCPudpAddr.sin_port = htons(udpPort); |
||
102 | SiTCPudpAddr.sin_addr.s_addr = inet_addr(sitcpIpAddr); |
||
103 | printf(" Done\n"); |
||
104 | return 1; |
||
105 | } |
||
106 | //------------------------------------------------------------------------------------------------------ |
||
107 | int SiTCPCloseUDPSock(){ |
||
108 | printf("Close UDP Socket..."); |
||
109 | closesocket(udpsock); |
||
110 | printf(" Done\n"); |
||
111 | return 1; |
||
112 | } |
||
113 | //------------------------------------------------------------------------------------------------------ |
||
114 | int SiTCPCloseTCPSock(){ |
||
115 | printf("Close TCP Socket..."); |
||
116 | closesocket(tcpsock); |
||
117 | printf(" Done\n"); |
||
118 | return 1; |
||
119 | } |
||
120 | //------------------------------------------------------------------------------------------------------ |
||
121 | void SiTCPRBCPskeleton(unsigned char type, unsigned char command, unsigned char id, |
||
122 | unsigned char length, unsigned int address){ |
||
123 | SiTCPsndHeader.type=type; |
||
124 | SiTCPsndHeader.command=command; |
||
125 | SiTCPsndHeader.id=id; |
||
126 | SiTCPsndHeader.length=length; |
||
127 | SiTCPsndHeader.address=htonl(address); |
||
128 | memcpy(SiTCPsndBuf, &SiTCPsndHeader, sizeof(SiTCPsndHeader)); |
||
129 | } |
||
130 | //------------------------------------------------------------------------------------------------------ |
||
131 | int SiTCPrcvRBCP_ACK(int output){ |
||
132 | //printf("hogeeeee0"); |
||
133 | fd_set setSelect; |
||
134 | int rcvdBytes; |
||
135 | unsigned char rcvdBuf[1024]; |
||
136 | int rd_data; |
||
137 | |||
138 | // puts("\nWait to receive the ACK packet..."); |
||
139 | |||
140 | int recvVal = 1; |
||
141 | |||
142 | while(recvVal){ |
||
143 | //printf("hogeeeee1"); |
||
144 | FD_ZERO(&setSelect); |
||
145 | //printf("hogeeeee2"); |
||
146 | FD_SET(udpsock, &setSelect); |
||
147 | //printf("hogeeeee3"); |
||
148 | timeout.tv_sec = 3; |
||
149 | timeout.tv_usec = 0; |
||
150 | //printf("hogeeeee4"); |
||
151 | if(select(udpsock+1, &setSelect, NULL, NULL,&timeout)==0){ |
||
152 | // time out |
||
153 | //printf("hogeeeee5"); |
||
154 | recvVal = 0; |
||
155 | puts("\n***** Timeout ! *****"); |
||
156 | //printf("hogeeeee6"); |
||
157 | return -1; |
||
158 | } |
||
159 | else { |
||
160 | // receive packet |
||
161 | //printf("hogeeeee7"); |
||
162 | if(FD_ISSET(udpsock,&setSelect)){ |
||
163 | //printf("hogeeeee8"); |
||
164 | //rcvdBytes=recvfrom(udpsock, rcvdBuf, 2048, 0, NULL, NULL); |
||
165 | rcvdBytes=recvfrom(udpsock, rcvdBuf, 1024, 0, NULL, NULL); |
||
166 | if(rcvdBytes < 0) { |
||
167 | Sleep(1000); |
||
168 | rcvdBytes=recvfrom(udpsock, rcvdBuf, 1024, 0, NULL, NULL); |
||
169 | } |
||
170 | //printf("hogeeee88"); |
||
171 | rcvdBuf[rcvdBytes]=0; |
||
172 | if(output == 1){ |
||
173 | //puts("\n***** A pacekt is received ! *****."); |
||
174 | //puts("Received data:"); |
||
175 | //printf("hogeeeee9"); |
||
176 | int j=0; |
||
177 | for(int i=0; i<rcvdBytes; i++){ |
||
178 | if(j==0) { |
||
179 | //printf("hogeeeee10"); |
||
180 | //printf("\t[%.3x]:%.2x ",i,rcvdBuf[i]); |
||
181 | j++; |
||
182 | }else if(j==3){ |
||
183 | //printf("hogeeeee11"); |
||
184 | //printf("%.2x \n",rcvdBuf[i]); |
||
185 | j=0; |
||
186 | }else{ |
||
187 | //printf("hogeeeee12"); |
||
188 | //printf("%.2x ",rcvdBuf[i]); |
||
189 | j++; |
||
190 | } |
||
191 | } |
||
192 | //puts("\n"); |
||
193 | rd_data = rcvdBuf[8]; |
||
194 | } |
||
195 | recvVal = 0; |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | // printf("%.2x",rd_data); |
||
200 | return rd_data; |
||
201 | //usleep(50); |
||
202 | } |
||
203 | //------------------------------------------------------------------------------------------------------ |
||
204 | int SiTCPGetTCPSock(){return tcpsock;} |
||
205 | int SiTCPGetUDPSock(){return udpsock;} |
||
206 | struct bcp_header SiTCPGetSiTCPsndHeader() {return SiTCPsndHeader;} |
||
207 |