RTL8720CF的WiFi蓝牙芯片,移植socket通信客户端代码。
#include "FreeRTOS.h"#include "task.h"#include "diag.h"#include "main.h"#include "gpio_api.h"#include "example_entry.h"#include "pwmout_api.h"#include "stdio.h"extern void console_init(void);/***************************************************************************************************************/#include "sockets.h"#include "netdb.h"#define COMPILE_TIME __DATE__","__TIME__#define LOG_MSG "\r\n\r\n######################################\r\n\r\n"\
"this is a socket test!\r\n"\"SOCKET CLIENT\r\n"\"compile time:"COMPILE_TIME"\r\n"\"\r\n######################################\r\n\r\n"#define SOCKET_HOST "192.xxx.xxx.xxx"// 填自己的#define SOCKET_PORT 5051/***************************************************************************************************************/#define DEBUG_INFO 2 #if(DEBUG_INFO == 1)#define LOG(...) printf(__VA_ARGS__)#define AT_LOG(...) printf(__VA_ARGS__)#define ERR_LOG(fmt,...) printf("ERR:%s:In Fun '%s',Line[%d] #"fmt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__)#elif(DEBUG_INFO == 2)#define LOG(...) #define AT_LOG(...) printf(__VA_ARGS__)#define ERR_LOG(fmt,...) printf("ERR:%s:In Fun '%s',Line[%d] #"fmt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__)#elif(DEBUG_INFO == 3)#define LOG(...) #define AT_LOG(...) #define ERR_LOG(fmt,...) printf("ERR:%s:In Fun '%s',Line[%d] #"fmt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__)#else#define LOG(...) #define AT_LOG(...)#define ERR_LOG(fmt,...)#endif#define GPIO_LED_PIN PA_19#define GPIO_PUSHBT_PIN PA_18#define PWM_5 PA_17#define PWM_PERIOD 20000void example_led(void);void example_led_thread(void);/* PWM */pwmout_t pwm_led;void my_pwm_init(void);void example_pwm(void);void example_pwm_thread(void);void test_socket_creat(void);void test_socket_thread(void);//任务优先级#define START_TASK_PRIO 1//任务堆栈大小 #define START_STK_SIZE 128 //任务句柄TaskHandle_t StartTask_Handler;//任务函数void start_task(void *pvParameters);// socket 通信处理char test_socket_handle(void);/*** @brief Main program.* @param None* @retval None*/int main(void){/* Initialize log uart and at command service */console_init();/* pre-processor of application example */pre_example_entry();/* wlan intialization */wlan_network();/* Execute application example */example_entry();/* user test */xTaskCreate((TaskFunction_t )start_task, //任务函数(const char* )"start_task", //任务名称(uint16_t )START_STK_SIZE, //任务堆栈大小(void* )NULL, //传递给任务函数的参数(UBaseType_t )START_TASK_PRIO, //任务优先级(TaskHandle_t* )&StartTask_Handler); //任务句柄 /* Enable Schedule, Start Kernel */vTaskStartScheduler();/* Should NEVER reach here */return 0;}//开始任务任务函数void start_task(void *pvParameters){taskENTER_CRITICAL(); //进入临界区//example_led();example_pwm();AT_LOG("\r\n######################test_socket_creat######################\r\n");test_socket_creat();vTaskDelete(StartTask_Handler); //删除开始任务taskEXIT_CRITICAL(); //退出临界区}void example_led(void){if(xTaskCreate(example_led_thread, ((const char*)"example_led_thread"),1024, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)printf("\n\r%s xTaskCreate failed\n", __FUNCTION__);}void example_led_thread(void){AT_LOG("\r\n GPIO DEMO \r\n");gpio_t gpio_led;gpio_t gpio_btn;// Init LED control pingpio_init(&gpio_led, GPIO_LED_PIN);gpio_dir(&gpio_led, PIN_OUTPUT); // Direction: Outputgpio_mode(&gpio_led, PullNone); // No pull// Initial Push Button pingpio_init(&gpio_btn, GPIO_PUSHBT_PIN);gpio_dir(&gpio_btn, PIN_INPUT); // Direction: Inputgpio_mode(&gpio_btn, PullUp); // Pull-High// my_pwm_init();// pwmout_pulsewidth_us(&pwm_led, PWM_PERIOD/2);while (1) {// turn off LEDgpio_write(&gpio_led, 0);vTaskDelay(2900);// turn on LEDgpio_write(&gpio_led, 1);vTaskDelay(10); }}void my_pwm_init(){pwmout_init(&pwm_led, PWM_5); // obj,pinpwmout_period_us(&pwm_led, PWM_PERIOD);// pwmout_pulsewidth_us(&pwm_led, pwms);}void example_pwm(void){if(xTaskCreate(example_pwm_thread, ((const char*)"pwm_thread"), 1024, NULL, tskIDLE_PRIORITY + 2, NULL) != pdPASS)printf("\n\r%s xTaskCreate failed\n", __FUNCTION__);}void example_pwm_thread(void){static int pwms = 0;my_pwm_init();pwmout_pulsewidth_us(&pwm_led, PWM_PERIOD);while(1){if(pwms >= PWM_PERIOD){pwms = 0;LOG("pwms =%d \r\n",pwms);}elsepwms += PWM_PERIOD/200;if(pwms%1000)LOG("\n\rpwms = %d \r\n",pwms);pwmout_pulsewidth_us(&pwm_led, pwms);vTaskDelay(10); }}void test_socket_creat(void){if(xTaskCreate(test_socket_thread, ((const char*)"test_socket_thread"),1024, NULL, tskIDLE_PRIORITY + 2, NULL) != pdPASS)printf("\n\r%s xTaskCreate failed\n", __FUNCTION__);}extern void fATPN(void *arg);void test_socket_thread(void){vTaskDelay(2000);AT_LOG("start connect wifi\r\n");fATPN("iQOO,123456789");AT_LOG("\r\nend connect wifi\r\n");if(test_socket_handle() != 0) {ERR_LOG("test_socket_handle\r\n");}vTaskDelete(NULL); //删除任务}char test_socket_handle(void){char exit_num = 0;// 退出编码// 第1步:创建客户端socketint sockfd = socket(AF_INET,SOCK_STREAM,0);// 第2步:向服务器发起请求struct hostent* h = gethostbyname(SOCKET_HOST);if(h == 0){exit_num = 1;ERR_LOG("gethostbyname ERR:%d\r\n",exit_num);goto exit;}struct sockaddr_in servaddr;memset(&servaddr,0,sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_port = htons(SOCKET_PORT); // 指定端口memcpy(&servaddr.sin_addr,h->h_addr,h->h_length);if(connect(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)) != 0) // 向服务器发起连接请求{exit_num = 2;ERR_LOG("connect sockfd ERR:%d\r\n",exit_num);goto exit;}char buffer[256];char i = 0;AT_LOG("**********************************start socket test\r\n");sprintf(buffer,"connect sockfd sucess msg%d\r\n",i);if(send(sockfd,buffer,strlen(buffer),0)<=0){exit_num = 3;ERR_LOG("connect sockfd ERR:%d\r\n",exit_num);goto exit;} // 第3步:与服务端天下,发送一个报文后等待回复,然后发送下一个报文for(;;){memset(buffer,0,sizeof(buffer));// 接受服务器的消息,阻塞memset(buffer,0,sizeof(buffer));if( recv(sockfd,buffer,sizeof(buffer),0) <= 0 ){AT_LOG("recv error");exit_num = -1;goto exit;}else{AT_LOG("LOG recv %d:%s",i++,buffer);if(strcmp(buffer,"+++\r\n") == 0){AT_LOG("user exit socket\r\n");break;} }// sprintf(buffer,"test msg %d\r\n",i++);if(send(sockfd,buffer,strlen(buffer),0)<=0) {AT_LOG("send error\r\n");exit_num = -1;goto exit;} else{AT_LOG("LOG client send sucess:%s\r\n",buffer);}vTaskDelay(1); }if(sockfd)close(sockfd);return 0;exit:if(exit_num!= 0)ERR_LOG("%d",exit_num);if(sockfd)close(sockfd); return exit_num; }