mirror of
https://github.com/cwyark/sdk-ameba-v4.0b_without_nda_gcc.git
synced 2025-07-31 20:31:07 +00:00
Initial commit
This commit is contained in:
commit
6f665866d6
3358 changed files with 1106791 additions and 0 deletions
|
|
@ -0,0 +1,56 @@
|
|||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include <platform/platform_stdlib.h>
|
||||
#include <lwip/sockets.h>
|
||||
|
||||
#define SERVER_IP "192.168.13.1"
|
||||
#define SERVER_PORT 80
|
||||
|
||||
// Need to define ERRNO in lwipopts.h
|
||||
int errno = 0;
|
||||
|
||||
static void example_nonblock_connect_thread(void *param)
|
||||
{
|
||||
int server_fd = -1;
|
||||
struct sockaddr_in server_addr;
|
||||
|
||||
// Delay to wait for IP by DHCP
|
||||
vTaskDelay(10000);
|
||||
printf("\nExample: Non-blocking socket connect\n");
|
||||
|
||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL, 0) | O_NONBLOCK);
|
||||
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
|
||||
server_addr.sin_port = htons(SERVER_PORT);
|
||||
connect(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
|
||||
|
||||
if(errno == EINPROGRESS) {
|
||||
fd_set wfds;
|
||||
struct timeval time_out;
|
||||
|
||||
time_out.tv_sec = 3; // Set select timeout of 3 seconds
|
||||
time_out.tv_usec = 0;
|
||||
FD_ZERO(&wfds) ;
|
||||
FD_SET(server_fd, &wfds); // Only set server fd
|
||||
|
||||
// Use select to wait for non-blocking connect
|
||||
if(select(server_fd + 1, NULL, &wfds, NULL, &time_out) == 1)
|
||||
printf("Server connection successful\n");
|
||||
else
|
||||
printf("Server connection failed\n");
|
||||
}
|
||||
else {
|
||||
printf("ERROR: connect\n");
|
||||
}
|
||||
|
||||
close(server_fd);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void example_nonblock_connect(void)
|
||||
{
|
||||
if(xTaskCreate(example_nonblock_connect_thread, ((const char*)"example_nonblock_connect_thread"), 1024, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
|
||||
printf("\n\r%s xTaskCreate(init_thread) failed", __FUNCTION__);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef EXAMPLE_NONBLOCK_CONNECT_H
|
||||
#define EXAMPLE_NONBLOCK_CONNECT_H
|
||||
|
||||
void example_nonblock_connect(void);
|
||||
|
||||
#endif /* EXAMPLE_NONBLOCK_CONNECT_H */
|
||||
16
component/common/example/nonblock_connect/readme.txt
Normal file
16
component/common/example/nonblock_connect/readme.txt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
LWIP SOCKET NONBLOCKING CONNECT EXAMPLE
|
||||
|
||||
Description:
|
||||
TCP nonblocking connect with use of select() for connection timeout handling.
|
||||
|
||||
Configuration:
|
||||
Modify SERVER_IP and SERVER_PORT in example_nonblock_connect.c for server connection
|
||||
|
||||
[platform_opts.h]
|
||||
#define CONFIG_EXAMPLE_NONBLOCK_CONNECT 1
|
||||
[lwipopts.h]
|
||||
#define ERRNO
|
||||
|
||||
Execution:
|
||||
Can make automatical Wi-Fi connection when booting by using wlan fast connect example.
|
||||
A socket nonblocking connect example thread will be started automatically when booting.
|
||||
Loading…
Add table
Add a link
Reference in a new issue