1.工程准备
由于要使用wifi功能,直接从wlan_demo复制一份出来,然后修改。
源文件只留下 main.c 就可以了。
BUILD.gn文件
import("//device/xradio/xr806/liteos_m/config.gni")
static_library("app_mying") {
configs = []
sources = [
"main.c",
]
cflags = board_cflags
include_dirs = board_include_dirs
include_dirs += [
"//kernel/liteos_m/kernel/arch/include",
"//utils/native/lite/include",
"//foundation/communication/wifi_lite/interfaces/wifiservice",
]
}
2. XR806 SDK
仔细看下xr806工程库的结构,
xr806的xr_skylark路径下属于芯片原生驱动库!
该路径下面有各种功能参考示例,如trustzone、net、ping、json等。
因此,直接参考xr_skylark\include下的内容,来实现一些简单的功能。
3.实现的功能
0)连接WiFi;
根据自己的SSID和PSK修改,代码直接copy原来的。
1)ping服务器,进行联通性测试;
ping一下自己的服务器,根据需要进行修改。
include下面有ping/ping.h文件。很简单的一个结构体(如下所示),看情况就是给个地址,设置一下参数,然后就可以实现ping命令的功能了。
struct ping_data {
ip_addr_t sin_addr; /* server addr */
u32_t count; /* number of ping */
u32_t data_long; /* the ping packet data long */
u32_t interval; /* Wait interval seconds between sending each packet, default 1s */
u32_t timeout; /* Time to wait for a response, in seconds */
u32_t deadline; /* Specify a timeout, in seconds, ping thread will stop if timeout */
u32_t ttl; /* ttl ping only. Set the IP Time to Live. */
int run_flag; /* run flag, 0:stop 1:start */
};
s32_t ping(struct ping_data *data);
2)作为TCP客户端,连接server,发数据;
通过TCP连接服务器,发数据。
设置服务器信息
【地址】(比如:192.168.1.100)
【端口号】(比如5679)
net路径下面有lwip库,借此实现网络通信功能。
代码里,通过宏定义的方式,将lwip_xxx改成了与linux下的soket API一样的接口。
参考正常的TCP_Client程序就行了。
3)读取一下xr_skylark里的cjson版本信息。
发现有cjson库,然后就随便测试一下。
4.程序示例
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "driver/chip/hal_gpio.h"
#include "kernel/os/os.h"
#include "wifi_device.h"
#include "cjson/cJSON.h"
#include "net/ping/ping.h"
#include "net/lwip-2.1.2/lwip/sockets.h"
#include "net/lwip-2.1.2/lwip/tcp.h"
#include "net/lwip-2.1.2/lwip/inet.h"
#include "net/lwip-2.1.2/lwip/ip_addr.h"
#define WIFI_DEVICE_CONNECT_AP_SSID "ChinaNet-111"
#define WIFI_DEVICE_CONNECT_AP_PSK "111666111"
#define GPIO_OUTPUT_PORT GPIO_PORT_A
static OS_Thread_t g_main_thread;
static void gpio_output_init(void)
{
GPIO_InitParam param;
param.driving = GPIO_DRIVING_LEVEL_1;
param.mode = GPIOx_Pn_F1_OUTPUT;
param.pull = GPIO_PULL_NONE;
HAL_GPIO_Init(GPIO_OUTPUT_PORT, GPIO_PIN_21, ¶m);//led灯对应IO
}
static void gpio_output_ctl(uint8_t level)
{
HAL_GPIO_WritePin(GPIO_OUTPUT_PORT, GPIO_PIN_21, level ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
}
void wifi_connect(void)
{
const char ssid_want_connect[] = WIFI_DEVICE_CONNECT_AP_SSID;
const char psk[] = WIFI_DEVICE_CONNECT_AP_PSK;
printf("\n=========== Connect Test Start ===========\n");
if (WIFI_SUCCESS != EnableWifi()) {
printf("Error: EnableWifi fail.\n");
return;
}
printf("EnableWifi Success.\n");
if (WIFI_STA_ACTIVE == IsWifiActive())
printf("Wifi is active.\n");
OS_Sleep(1);
if (WIFI_SUCCESS != Scan()) {
printf("Error: Scan fail.\n");
return;
}
printf("Wifi Scan Success.\n");
OS_Sleep(1);
WifiScanInfo scan_results[30];
unsigned int scan_num = 30;
if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) {
printf("Error: GetScanInfoList fail.\n");
return;
}
WifiDeviceConfig config = { 0 };
int netId = 0;
int i;
for (i = 0; i < scan_num; i++) {
if (0 == strcmp(scan_results[i].ssid, ssid_want_connect)) {
memcpy(config.ssid, scan_results[i].ssid,
WIFI_MAX_SSID_LEN);
memcpy(config.bssid, scan_results[i].bssid,
WIFI_MAC_LEN);
strcpy(config.preSharedKey, psk);
config.securityType = scan_results[i].securityType;
config.wapiPskType = WIFI_PSK_TYPE_ASCII;
config.freq = scan_results[i].frequency;
break;
}
}
if (i >= scan_num) {
printf("Error: No found ssid in scan_results\n");
return;
}
printf("GetScanInfoList Success.\n");
if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) {
printf("Error: AddDeviceConfig Fail\n");
return;
}
printf("AddDeviceConfig Success.\n");
if (WIFI_SUCCESS != ConnectTo(netId)) {
printf("Error: ConnectTo Fail\n");
return;
}
printf("ConnectTo Success\n");
OS_Sleep(3);
WifiLinkedInfo get_linked_res;
if (WIFI_SUCCESS != GetLinkedInfo(&get_linked_res)) {
printf("Error: GetLinkedInfo Fail\n");
return;
}
printf("GetLinkedInfo Success.\n");
printf("ssid: %s\n", get_linked_res.ssid);
printf("bssid: ");
for (int j = 0; j < WIFI_MAC_LEN; j++) {
printf("%02X", get_linked_res.bssid[j]);
}
printf("\n");
printf("rssi: %d\n", get_linked_res.rssi);
unsigned char get_mac_res[WIFI_MAC_LEN];
if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
printf("Error: GetDeviceMacAddress Fail\n");
return;
}
printf("GetDeviceMacAddress Success.\n");
for (int j = 0; j < WIFI_MAC_LEN - 1; j++) {
printf("%02X:", get_mac_res[j]);
}
printf("%02X\n", get_mac_res[WIFI_MAC_LEN - 1]);
}
struct ping_data ping_t;
//ping命令参数设置
void ping_init()
{
ip_addr_t server_ip;
inet_aton("129.204.63.27", &server_ip);
ping_t.sin_addr = server_ip;
ping_t.count = 0xF;
ping_t.data_long = 512;
ping_t.timeout = 30;
ping_t.run_flag = 1;
}
//TCP SOCKET
int s;
void tcp_test_init()
{
//socket create!
s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//address info!
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5679);
inet_pton(AF_INET, "192.168.1.100", &server_addr.sin_addr);
//connect!
if(connect(s,(void *)&server_addr,sizeof(server_addr)) < 0) {
printf("connect tcp_server failed! \r\n");
} else {
printf("connect tcp_server successfuly! \r\n");
}
// send data
send(s, "xr806\n", 7, 0);
}
static void MainThread(void *arg)
{
gpio_output_init();
wifi_connect();
ping_init();
ping(&ping_t);
tcp_test_init();
char buf[32];
int cnt = 0;
while(1) {
sprintf(buf,"XR806:%s : %d \r\n",cJSON_Version(), cnt++);
//向服务器发送数据
send(s,buf, sizeof(buf), 0);
printf("%s:Hello XR806 \r\n",__func__);
gpio_output_ctl(1);
OS_Sleep(1);
gpio_output_ctl(0);
OS_Sleep(1);
}
}
void WifiTestMain(void)
{
printf("Wifi Test Start\r\n");
if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL,
OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
printf("[ERR] Create MainThread Failed\n");
}
}
SYS_RUN(WifiTestMain);
5.结果展示
通过网络调试工具,建立TCP服务器,接收到了xr806发来的数据。
串口打印信息
[net INF] netif (IPv4) is up
[net INF] address: 192.168.1.110
[net INF] gateway: 192.168.1.1
[net INF] netmask: 255.255.255.0
[net INF] msg <network IPv6 state>
GetLinkedInfo Success.
ssid: ChinaNet-111
bssid: 5475956E3374
rssi: 110
GetDeviceMacAddress Success.
9C:9E:49:BA:5B:01
PING 129.204.63.27 520 bytes of data.
Request timeout for icmp_seq=1
512 bytes from 129.204.63.27: icmp_seq=2 time=43 ms
512 bytes from 129.204.63.27: icmp_seq=3 time=43 ms
512 bytes from 129.204.63.27: icmp_seq=4 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=5 time=43 ms
512 bytes from 129.204.63.27: icmp_seq=6 time=45 ms
512 bytes from 129.204.63.27: icmp_seq=7 time=46 ms
512 bytes from 129.204.63.27: icmp_seq=8 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=9 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=10 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=11 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=12 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=13 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=14 time=44 ms
512 bytes from 129.204.63.27: icmp_seq=15 time=43 ms
--- 129.204.63.27 ping statistics ---
15 packets transmitted, 14 received, 6% packet loss, time 672ms
rtt min/avg/max/mdev = 43/43/46/3 ms
connect tcp_server successfuly!
MainThread:Hello XR806
MainThread:Hello XR806