This commit is contained in:
pvvx 2017-06-21 03:00:20 +03:00
parent 34d3652711
commit 39f77eb92b
1844 changed files with 899433 additions and 7 deletions

View file

@ -0,0 +1,81 @@
#include "FreeRTOS.h"
#include "task.h"
#include <platform/platform_stdlib.h>
#include <lwip/sockets.h>
#include <lwip_netconf.h>
#include <lwip/netif.h>
static void example_bcast_thread(void *param)
{
int socket = -1;
int broadcast = 1;
struct sockaddr_in bindAddr;
uint16_t port = 49152;
unsigned char packet[1024];
// Create socket
if((socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("ERROR: socket failed\n");
goto err;
}
// Set broadcast socket option
if(setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0){
printf("ERROR: setsockopt failed\n");
goto err;
}
// Set the bind address
memset(&bindAddr, 0, sizeof(bindAddr));
bindAddr.sin_family = AF_INET;
bindAddr.sin_port = htons(port);
bindAddr.sin_addr.s_addr = INADDR_ANY;
if(bind(socket, (struct sockaddr *) &bindAddr, sizeof(bindAddr)) < 0){
printf("ERROR: bind failed\n");
goto err;
}
while(1) {
// Receive broadcast
int packetLen;
struct sockaddr from;
struct sockaddr_in *from_sin = (struct sockaddr_in*) &from;
socklen_t fromLen = sizeof(from);
if((packetLen = recvfrom(socket, &packet, sizeof(packet), 0, &from, &fromLen)) >= 0) {
uint8_t *ip = (uint8_t *) &from_sin->sin_addr.s_addr;
uint16_t from_port = ntohs(from_sin->sin_port);
printf("recvfrom - %d bytes from %d.%d.%d.%d:%d\n", packetLen, ip[0], ip[1], ip[2], ip[3], from_port);
}
// Send broadcast
if(packetLen > 0) {
int sendLen;
struct sockaddr to;
struct sockaddr_in *to_sin = (struct sockaddr_in*) &to;
to_sin->sin_family = AF_INET;
to_sin->sin_port = htons(port);
to_sin->sin_addr.s_addr = INADDR_ANY;
if((sendLen = sendto(socket, packet, packetLen, 0, &to, sizeof(struct sockaddr))) < 0)
printf("ERROR: sendto broadcast\n");
else
printf("sendto - %d bytes to broadcast:%d\n", sendLen, port);
}
}
err:
printf("ERROR: broadcast example failed\n");
close(socket);
vTaskDelete(NULL);
return;
}
void example_bcast(void)
{
if(xTaskCreate(example_bcast_thread, ((const char*)"example_bcast_thread"), 2048, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("\n\r%s xTaskCreate(init_thread) failed", __FUNCTION__);
}

View file

@ -0,0 +1,6 @@
#ifndef EXAMPLE_BCAST_H
#define EXAMPLE_BCAST_H
void example_bcast(void);
#endif /* EXAMPLE_BCAST_H */

View file

@ -0,0 +1,28 @@
LWIP BROADCAST EXAMPLE
Description:
Listen broadcast message on port 49152.
Send packet with the content of received packet to broadcast address.
Configuration:
[lwipopts.h]
#define LWIP_UDP 1
[platform_opts.h]
#define CONFIG_EXAMPLE_BCAST 1
Execution:
Can make automatical Wi-Fi connection when booting by using wlan fast connect example.
A broadcast example thread will be started automatically when booting.
Test:
1. Prepare a NB and connect to the same AP Ameba connected.
2. NB: iperf -c 192.168.1.255 -t 60 -i 1 -p 49152 -u
3. The recv/send messages should be printed out on Ameba console.
4. Use sniffer to make sure the packets send from Ameba are broadcast messages.
Note:
If you encounter some message like:
ERROR: sendto broadcast
[Driver]: skb_unavailable=1 in last 2 seconds
It means that the skb buffer is not enough for the massive UDP packets to be sent.
If you want to prevent the error you can add some delay time between sending packets or enlarge the skb buffer configuration.