#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define BROADCAST
#define LOCAL_PORT 6647
#define REMOTE_PORT 6646
#ifdef BROADCAST
//#define MAXDATASIZE 1472
#define MAXDATASIZE 1320
#else
#define MAXDATASIZE 8192
#endif // BROADCAST
int main()
{
int iter;
int sockfd;
int numbytes;
int nOptVal;
int nOptLen = sizeof(int);
struct sockaddr_in local;
struct sockaddr_in remote;
char buf[MAXDATASIZE];
for(iter = 0; iter < 10; iter++) {
memset(&remote, 56, sizeof(remote));
// setup IP address of WiShield
remote.sin_family = AF_INET;
remote.sin_port = htons(REMOTE_PORT);
#ifdef BROADCAST
inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr);
#else
inet_pton(AF_INET, "192.168.1.248", &remote.sin_addr);
#endif // BROADCAST
// setup socket and connect
if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("socket");
exit(0);
}
#ifdef BROADCAST
//set broadcast socket option
nOptVal = 1;
if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &nOptVal, nOptLen)) {
perror("setsockopt");
close(sockfd);
exit(0);
}
#endif // BROADCAST
//set the packet size
nOptVal = 65535;
if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &nOptVal, nOptLen)) {
perror("setsockopt");
close(sockfd);
exit(0);
}
// bind to a specific port on the PC
local.sin_family = AF_INET;
local.sin_port = htons(LOCAL_PORT);
local.sin_addr.s_addr = INADDR_ANY;
if ( bind(sockfd, (struct sockaddr *)&local, sizeof(local)) == -1) {
perror("bind");
exit(0);
}
// send "Hello" to WiShield
if ((numbytes = sendto(sockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&remote, sizeof(remote))) == -1) {
perror("sendto");
exit(1);
}
printf("iter: %d, total: %d, sent %d to port %d\n", iter, MAXDATASIZE, numbytes, REMOTE_PORT);
close(sockfd);
sleep(20);
}
return 0;
}
case ZG_INTR_ST_RD_CTRL_REG:
{
U16 rx_byte_cnt = (0x0000 | (hdr[1] << 8) | hdr[2]) & 0x0fff; // only 12bits matter (0-4095)
zg_buf[0] = ZG_CMD_RD_FIFO;
spi_transfer(zg_buf, rx_byte_cnt + 1, 1); // copy ZG2100 buffer contents into zg_buf
hdr[0] = ZG_CMD_RD_FIFO_DONE; // Tell zg2100 we're done reading from it's buffer
spi_transfer(hdr, 1, 1);
intr_valid = 1;
intr_state = 0;
break;
}
case ZG_INTR_ST_RD_CTRL_REG:
{
U16 rx_byte_cnt = (0x0000 | (hdr[1] << 8) | hdr[2]) & 0x0fff; // only 12bits matter (0-4095)
if ( rx_byte_cnt < (U16)UIP_BUFSIZE ){ // Check if our buffer is large enough for packet
zg_buf[0] = ZG_CMD_RD_FIFO;
spi_transfer(zg_buf, rx_byte_cnt + 1, 1); // copy ZG2100 buffer contents into zg_buf (uip_buf)
hdr[0] = ZG_CMD_RD_FIFO_DONE; // Tell ZG2100 we're done reading from it's buffer
spi_transfer(hdr, 1, 1);
intr_valid = 1;
intr_state = 0;
break;
}else{ // Too Big, ignore it and continue
intr_valid = 0;
intr_state = 0;
break;
}
}
Users browsing this forum: Yahoo [Bot] and 1 guest