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

@ -142,7 +142,6 @@
</folderInfo>
<sourceEntries>
<entry excluding="project/src/adc|adc|component/soc/realtek/8195a/misc/gcc_utility|doc|component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM4F|project/realtek_ameba1_va0_example/example_sources|build|project/realtek_ameba1_va0_example/GCC-RELEASE|component/common/network/lwip/lwip_v1.5.0.beta|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_4.c|component1|component/common/network/lwip/lwip_v1.3.2|flasher|component/os/freertos/freertos_v8.1.2/Source/portable/IAR|component/os/freertos/freertos_v9.0.0/Source/portable/MemMang/heap_1.c|component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM3_MPU|component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7|component/os/freertos/freertos_v8.1.2/Demo|component/os/freertos/freertos_v8.1.2/Source/portable/RVDS|component/soc/realtek/8195a/misc/iar_utility|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_3.c|RTL00_SDKV35a/project|tools|component/common/network/ssl/polarssl-1.3.8/ssl_self_test.c|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_2.c|project/realtek_ameba1_va0_example/disasm_to_c_boot" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="project/src/adc"/>
</sourceEntries>
</configuration>
</storageModule>
@ -390,7 +389,6 @@
<sourceEntries>
<entry excluding="project/src/adc|${ProjSDK}|adc|component/soc/realtek/8195a/misc/gcc_utility|doc|component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM4F|project/realtek_ameba1_va0_example/example_sources|build|project/realtek_ameba1_va0_example/GCC-RELEASE|component/common/network/lwip/lwip_v1.5.0.beta|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_4.c|component1|component/common/network/lwip/lwip_v1.3.2|flasher|component/os/freertos/freertos_v8.1.2/Source/portable/IAR|component/os/freertos/freertos_v9.0.0/Source/portable/MemMang/heap_1.c|component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM3_MPU|component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7|component/os/freertos/freertos_v8.1.2/Demo|component/os/freertos/freertos_v8.1.2/Source/portable/RVDS|component/soc/realtek/8195a/misc/iar_utility|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_3.c|tools|component/common/network/ssl/polarssl-1.3.8/ssl_self_test.c|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_2.c|project/realtek_ameba1_va0_example/disasm_to_c_boot|RTL00_SDKV35a" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry excluding="AutoMake/|LibAutoMake/|project/|.git/|.settings/|build/|flasher/" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="RTL00_SDKV35a"/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="project/src/adc"/>
</sourceEntries>
</configuration>
</storageModule>
@ -571,7 +569,6 @@
</folderInfo>
<sourceEntries>
<entry excluding="project/src/adc|adc|component/soc/realtek/8195a/misc/gcc_utility|doc|component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM4F|project/realtek_ameba1_va0_example/example_sources|build|project/realtek_ameba1_va0_example/GCC-RELEASE|component/common/network/lwip/lwip_v1.5.0.beta|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_4.c|component1|component/common/network/lwip/lwip_v1.3.2|flasher|component/os/freertos/freertos_v8.1.2/Source/portable/IAR|component/os/freertos/freertos_v9.0.0/Source/portable/MemMang/heap_1.c|component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM3_MPU|component/common/network/lwip/lwip_v1.4.1/port/stm32f2x7|component/os/freertos/freertos_v8.1.2/Demo|component/os/freertos/freertos_v8.1.2/Source/portable/RVDS|component/soc/realtek/8195a/misc/iar_utility|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_3.c|RTL00_SDKV35a/project|tools|component/common/network/ssl/polarssl-1.3.8/ssl_self_test.c|component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_2.c|project/realtek_ameba1_va0_example/disasm_to_c_boot" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="project/src/adc"/>
</sourceEntries>
</configuration>
</storageModule>

View file

@ -0,0 +1,117 @@
/**
* Default styles for the dygraphs charting library.
*/
.dygraph-legend {
position: absolute;
font-size: 14px;
z-index: 10;
width: 250px; /* labelsDivWidth */
/*
dygraphs determines these based on the presence of chart labels.
It might make more sense to create a wrapper div around the chart proper.
top: 0px;
right: 2px;
*/
background: white;
line-height: normal;
text-align: left;
overflow: hidden;
}
/* styles for a solid line in the legend */
.dygraph-legend-line {
display: inline-block;
position: relative;
bottom: .5ex;
padding-left: 1em;
height: 1px;
border-bottom-width: 2px;
border-bottom-style: solid;
/* border-bottom-color is set based on the series color */
}
/* styles for a dashed line in the legend, e.g. when strokePattern is set */
.dygraph-legend-dash {
display: inline-block;
position: relative;
bottom: .5ex;
height: 1px;
border-bottom-width: 2px;
border-bottom-style: solid;
/* border-bottom-color is set based on the series color */
/* margin-right is set based on the stroke pattern */
/* padding-left is set based on the stroke pattern */
}
.dygraph-roller {
position: absolute;
z-index: 10;
}
/* This class is shared by all annotations, including those with icons */
.dygraph-annotation {
position: absolute;
z-index: 10;
overflow: hidden;
}
/* This class only applies to annotations without icons */
/* Old class name: .dygraphDefaultAnnotation */
.dygraph-default-annotation {
border: 1px solid black;
background-color: white;
text-align: center;
}
.dygraph-axis-label {
/* position: absolute; */
/* font-size: 14px; */
z-index: 10;
line-height: normal;
overflow: hidden;
color: black; /* replaces old axisLabelColor option */
}
.dygraph-axis-label-x {
}
.dygraph-axis-label-y {
}
.dygraph-axis-label-y2 {
}
.dygraph-title {
font-weight: bold;
z-index: 10;
text-align: center;
/* font-size: based on titleHeight option */
}
.dygraph-xlabel {
text-align: center;
/* font-size: based on xLabelHeight option */
}
/* For y-axis label */
.dygraph-label-rotate-left {
text-align: center;
/* See http://caniuse.com/#feat=transforms2d */
transform: rotate(90deg);
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
}
/* For y2-axis label */
.dygraph-label-rotate-right {
text-align: center;
/* See http://caniuse.com/#feat=transforms2d */
transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
}

File diff suppressed because it is too large Load diff

6
ExampleHTM/dygraph/dygraph.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="dygraph.css">
<title>Get data ADC</title>
<script type="text/javascript" src="dygraph.min.js"></script>
</head>
<body>
<h3 style="width:800px; text-align: center;">Read ADC RTL8711AM</h3>
<div id="div_g" style="width:800px; height:400px;"></div>
<script type="text/javascript">
var data = [];
var g = new Dygraph(document.getElementById("div_g"), data,
{ drawPoints: true, showRoller: true, labels: ['X', 'U']});
var oldblkid = 0;
var rdnextflg = false;
var cur_idx = 0;
ws = new WebSocket('ws://rtl871x0/web.cgi');
ws.binaryType = 'arraybuffer';
ws.onopen = function(){ws.send('adc')};
ws.onmessage = function (event) {
if(event.data instanceof ArrayBuffer) {
var wordarray = new Uint16Array(event.data);
if(wordarray.length > 2) {
var blksz = wordarray[0];
if(wordarray.length == blksz + 2) {
var blkid = wordarray[1];
if(rdnextflg) {
cur_idx += (blkid - oldblkid) & 0xFFFF;
} else rdnextflg = true;
oldblkid = blkid + blksz;
for (var i = 2; i < wordarray.length; i++) {
if(cur_idx > 1000 ) data.shift();
data.push([cur_idx++, wordarray[i]]);
}
g.updateOptions({'file':data});
}
}
ws.send("adc");
}
}
</script>
</body></html>

View file

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="dygraph.css">
<title>Get data INA219</title>
<script type="text/javascript" src="dygraph.js"></script>
</head>
<body>
<h3 style="width:800px; text-align: center;">Read regs U & I INA219</h3>
<div id="div_g" style="width:800px; height:400px;"></div>
<script type="text/javascript">
var data = [];
var g = new Dygraph(
document.getElementById("div_g"),
data,
{
drawPoints: true,
showRoller: true,
labels: ['X', 'U', 'I']});
var oldblkid = 0;
var rdnextflg = false;
var cur_idx = 0;
ws = new WebSocket('ws://rtl871x0/web.cgi');
ws.binaryType = 'arraybuffer';
ws.onopen = function(){ws.send('ina219')};
ws.onmessage = function (event) {
if(event.data instanceof ArrayBuffer) {
var wordarray = new Int16Array(event.data);
if(wordarray.length > 2) {
var blksz = wordarray[0];
if(wordarray.length == blksz*2 + 2) {
var blkid = wordarray[1] & 0xFFFF;
if(rdnextflg) {
cur_idx += (blkid - oldblkid) & 0xFFFF;
} else rdnextflg = true;
oldblkid = blkid + blksz;
for (var i=2; i<wordarray.length; i+=2) {
if(cur_idx > 2000 ) data.shift();
data.push([cur_idx, wordarray[i], wordarray[i+1]*50]);
cur_idx++;
}
g.updateOptions({'file':data});
}
}
ws.send("ina219");
}
}
</script>
</body>
</html>

39
USDK/Makefile Normal file
View file

@ -0,0 +1,39 @@
include userset.mk
all: ram_all
mp: ram_all_mp
.PHONY: ram_all
ram_all:
@$(MAKE) -f $(SDK_PATH)sdkbuild.mk
@$(MAKE) -f $(SDK_PATH)flasher.mk genbin1 genbin23
.PHONY: ram_all_mp
ram_all_mp:
@$(MAKE) -f $(SDK_PATH)sdkbuild.mk mp
@$(MAKE) -f $(SDK_PATH)flasher.mk mp
.PHONY: clean clean_all
clean:
@$(MAKE) -f $(SDK_PATH)sdkbuild.mk clean
clean_all:
@$(MAKE) -f $(SDK_PATH)sdkbuild.mk clean_all
.PHONY: flashburn runram reset test readfullflash flashwebfs
flashburn:
@$(MAKE) -f $(SDK_PATH)flasher.mk flashburn
flash_OTA:
@$(MAKE) -f $(SDK_PATH)flasher.mk flash_OTA
runram:
@$(MAKE) --f $(SDK_PATH)flasher.mk runram
reset:
@$(MAKE) -f $(SDK_PATH)flasher.mk reset
readfullflash:
@$(MAKE) -f $(SDK_PATH)flasher.mk readfullflash

View file

@ -0,0 +1,175 @@
#include <stdio.h>
#include "log_service.h"
#include "cmsis_os.h"
#include <platform/platform_stdlib.h>
#if CONFIG_JOYLINK
#if 1
void fATCJ(void *arg)
{
extern void joylink_erase(void);
printf("\r\n[ATCJ] Erase wifi and joylink info.");
if(arg){
printf("\r\n[ATCJ]Usage : ATCJ");
return;
}
joylink_erase();
}
#else
void fATCJ(void *arg)
{
extern void cmd_jd_smart(int argc, char **argv);
int argc;
char *argv[MAX_ARGC] = {0};
printf("[ATCJ]:simple config command for jdsmart\n\r");
if(!arg){
printf("[ATCJ]Usage: ATCJ=simple_config\n\r");
return;
}
argv[0] = "simple_config";
if((argc = parse_param(arg, argv)) > 1){
cmd_jd_smart(argc, argv);
}
else
printf("[ATCJ]Usage: ATCJ=simple_config\n\r");
}
#endif
#endif
#if CONFIG_GAGENT
void fATCG(void *arg)
{
example_gagent();
}
void fATCE(void *arg)
{//Erase gagent config flash
extern int GAgent_DevEraseConfigData();
GAgent_DevEraseConfigData();
}
#endif
#if CONFIG_QQ_LINK
void fATCQ(void *arg)
{
int argc;
unsigned char *argv[MAX_ARGC] = {0};
extern void device_write_sn_license(int argc, unsigned char **argv);
extern void device_erase_all(int argc, unsigned char **argv);
if(!arg)
{
printf("\r\n[ATCQ] Write sn/license into flash or Erase all info\r\n");
printf("\r\n[ATCQ] Usage: ATCQ=erase");
printf("\r\n[ATCQ] Usage: ATCQ=sn,xxxxxxxx\r\n ATCQ=licensepart1,xxxxxxxx\r\n ATCQ=licensepart2,xxxxxxxx");
return;
}
argv[0] = "sn&&license&&erase";
argc = parse_param(arg, argv);
if(argc == 3) // Write sn&&license
{
device_write_sn_license(argc, argv);
}
else if(argc == 2) // Erase all info : ATCQ=erase
{
device_erase_all(argc, argv);
}
else
{
printf("\r\n[ATCQ] Usage: ATCQ=erase");
printf("\r\n[ATCQ]Usage: ATCQ=sn,xxxxxxxx\r\n ATCQ=licensepart1,xxxxxxxx\r\n ATCQ=licensepart2,xxxxxxxx");
}
}
#endif
#if CONFIG_AIRKISS_CLOUD
void fATCW(void *arg)
{
int argc;
unsigned char *argv[MAX_ARGC] = {0};
extern void airkiss_cloud_write_device_info(int argc, unsigned char **argv);
extern void airkiss_cloud_erase_ap_profile(int argc, unsigned char **argv);
if(!arg) goto USAGE;
argv[0] = "type/id/licese/erase";
argc = parse_param(arg, argv);
if(argc == 3) // Write typw/id/license
{
airkiss_cloud_write_device_info(argc, argv);
return;
}
else if(argc == 2) // Erase wifi profile : ATCW=erase
{
airkiss_cloud_erase_ap_profile(argc, argv);
return;
}
else
goto USAGE;
USAGE:
printf("\r\n[ATCW] Write ORDERLY device's type/id/license into flash or Erase wifi profile");
printf("\r\n[ATCW] Usage: ATCW=type,xxxxxxxx");
printf("\r\n[ATCW] Usage: ATCW=id,xxxxxxxx");
printf("\r\n[ATCW] Usage: ATCW=licensepart1,xxxxxxxx\t(80-Byte long)");
printf("\r\n[ATCW] Usage: ATCW=licensepart2,xxxxxxxx\t(80-Byte long)");
printf("\r\n[ATCW] Usage: ATCW=erase");
return;
}
#endif
#if CONFIG_ALINK
extern void example_alink(void);
extern int alink_erase_wifi_config();
extern void alink_reset_to_factory(void *arg);
void fATCA(void *arg)
{
example_alink();
}
void fATCZ(void *arg)
{
//Erase alink config flash
alink_erase_wifi_config();
}
void fATCT(void *arg)
{
alink_reset_to_factory(NULL);
}
#endif
void fATCx(void *arg)
{
}
log_item_t at_cloud_items[ ] = {
#if CONFIG_JOYLINK
{"ATCJ", fATCJ,},
#endif
#if CONFIG_GAGENT
{"ATCG", fATCG,},
{"ATCE", fATCE,},
#endif
#if CONFIG_QQ_LINK
{"ATCQ", fATCQ,},
#endif
#if CONFIG_AIRKISS_CLOUD
{"ATCW", fATCW},
#endif
#if CONFIG_ALINK
{"ATCA", fATCA,},
{"ATCZ", fATCZ,},
{"ATCT", fATCT,},
#endif
{"ATC?", fATCx,},
};
void at_cloud_init(void)
{
log_service_add_table(at_cloud_items, sizeof(at_cloud_items)/sizeof(at_cloud_items[0]));
}
#if SUPPORT_LOG_SERVICE
log_module_init(at_cloud_init);
#endif

View file

@ -0,0 +1,91 @@
#include <stdio.h>
#include "log_service.h"
#include "platform_opts.h"
#include <lwip_netconf.h>
#include "cmsis_os.h"
#include <platform/platform_stdlib.h>
#include <lwip/sockets.h>
#include <lwip/tcpip.h>
#define _AT_DHCP_ETHERNET_MII_ "ATE0"
#define _AT_SET_DEFAULT_INTERFACE "ATE1"
#if CONFIG_ETHERNET
extern int dhcp_ethernet_mii;
extern int ethernet_if_default;
extern struct netif xnetif[NET_IF_NUM];
void fATE0(void *arg)
{
int argc;
char *argv[MAX_ARGC] = {0};
printf("[ATE0]:DHCP configure for ethernet\n\r");
if(!arg){
printf("[ATE0]Usage to disable DHCP: ATE0=0\n");
printf("[ATE0]Usage to enable DHCP: ATE0=1\n");
return;
}
if('0' == *(char *)arg)
{
dhcp_ethernet_mii = 0;
}
else if('1' == *(char *)arg)
{
dhcp_ethernet_mii = 1;
LwIP_DHCP(NET_IF_NUM - 1, DHCP_START);
}
else
{
printf("[ATE0]Usage to disable DHCP: ATE0=0\n");
printf("[ATE0]Usage to enable DHCP: ATE0=1\n");
}
}
void fATE1(void *arg)
{
int argc;
char *argv[MAX_ARGC] = {0};
printf("[ATE1]:Set/check the default interface\n\r");
if(!arg){
if(ethernet_if_default)
printf("Ethernet is the default interface\n");
else
printf("wlan is the default interface\n");
return;
}
if('0' == *(char *)arg)
{
ethernet_if_default = 0;
printf("wlan is set to the default interface\n");
}
else if('1' == *(char *)arg)
{
ethernet_if_default = 1;
printf("ethernet is set to the default interface\n");
}
else
{
printf("[ATE0]Usage to check the default interface: ATE1\n");
printf("[ATE0]Usage to set ethernet as default interface: ATE1=1\n");
printf("[ATE0]Usage to set wlan as default interface: ATE1=0\n");
}
}
log_item_t at_ethernet_items[ ] = {
{"ATE0", fATE0,},
{"ATE1", fATE1,}
};
void at_ethernet_init(void)
{
log_service_add_table(at_ethernet_items, sizeof(at_ethernet_items)/sizeof(at_ethernet_items[0]));
}
log_module_init(at_ethernet_init);
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,100 @@
#ifndef __ATCMD_LWIP_H__
#define __ATCMD_LWIP_H__
#include <platform_opts.h>
#ifdef CONFIG_AT_LWIP
#include "main.h"
#include <lwip/opt.h>
#include "lwip/sockets.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/igmp.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcpip.h"
#include "lwip/pbuf.h"
#include "lwip/netdb.h"
#include "lwip_netconf.h"
#define _AT_TRANSPORT_MODE_ "ATP1"
#define _AT_TRANSPORT_LOCAL_PORT_ "ATP2"
#define _AT_TRANSPORT_REMOTE_IP_ "ATP3"
#define _AT_TRANSPORT_REMOTE_PORT_ "ATP4"
#define _AT_TRANSPORT_START_SERVER_ "ATP5"
#define _AT_TRANSPORT_START_CLIENT_ "ATP6"
#define _AT_TRANSPORT_SHOW_SETTING_ "ATP?"
#define _AT_TRANSPORT_RECEIVE_DATA_ "ATR0"
#define _AT_TRANSPORT_RECEIVE_PACKET_SIZE_ "ATR1"
#define _AT_TRANSPORT_WRITE_DATA_ "ATRA"
#define _AT_TRANSPORT_WRITE_PACKET_SIZE_ "ATRB"
#define NODE_MODE_TCP 0
#define NODE_MODE_UDP 1
#define NODE_ROLE_SERVER 0
#define NODE_ROLE_CLIENT 1
#define NODE_ROLE_SEED 2
#define INVALID_SOCKET_ID (-1)
//parameters
#ifndef NET_IF_NUM
#define NET_IF_NUM 2
#endif
#define ATCMD_LWIP_TASK_PRIORITY (tskIDLE_PRIORITY + 1)
#if ATCMD_VER == ATVER_2
#define SERVER "127.0.0.1"
#define NUM_NS (MEMP_NUM_NETCONN) //maximum number of node and seed, same as NUM_SOCKETS
#define ETH_MAX_MTU 1500
#define INVALID_CON_ID (-1)
#define RECV_SELECT_TIMEOUT_SEC (0)
#define RECV_SELECT_TIMEOUT_USEC (20000) //20ms
typedef struct ns
{
int con_id;
int sockfd;
s8_t role;
int protocol;
u32_t addr;
u16_t port;
u32_t local_addr;
u16_t local_port;
xTaskHandle handletask;
struct ns* next;
struct ns* nextseed;
} node;
extern xTaskHandle atcmd_lwip_tt_task;
extern xSemaphoreHandle atcmd_lwip_tt_sema;
extern volatile int atcmd_lwip_tt_datasize;
extern volatile int atcmd_lwip_tt_lasttickcnt;
#define ATCMD_LWIP_TT_MAX_DELAY_TIME_MS (20) //transparent transmission interval
extern int atcmd_lwip_is_tt_mode(void);
extern void atcmd_lwip_set_tt_mode(int enable);
int atcmd_lwip_send_data(node *curnode, u8 *data, u16 data_sz, struct sockaddr_in cli_addr);
int atcmd_lwip_receive_data(node *curnode, u8 *buffer, u16 buffer_size, int *recv_size,
u8_t *udp_clientaddr, u16_t *udp_clientport);
node* create_node(int mode, s8_t role);
void init_node_pool(void);
void delete_node(node *n);
int hang_node(node* insert_node);
int hang_seednode(node* main_node ,node* insert_node);
node *seek_node(int con_id);
node *tryget_node(int n);
#endif
#endif //#ifdef CONFIG_AT_LWIP
#endif //#ifndef __ATCMD_LWIP_H__

View file

@ -0,0 +1,195 @@
#include <platform_stdlib.h>
#include <platform_opts.h>
#include <gpio_api.h>
#include "log_service.h"
#include "atcmd_mp.h"
#if CONFIG_ATCMD_MP_EXT0
extern void fATM0(void *arg); // MP ext0 AT command
#endif
#if CONFIG_ATCMD_MP
//-------- AT MP commands ---------------------------------------------------------------
void fATMG(void *arg)
{
gpio_t gpio_test;
int argc = 0, val, cnts, i, write=0, data=0;
char *argv[MAX_ARGC] = {0}, port, num;
PinName pin = NC;
u32 tConfigDebugInfo = ConfigDebugInfo;
AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "[ATMG]: _AT_MP_GPIO_TEST_");
if(!arg){
AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "[ATMG] Usage: ATSG=w,PINNAMES(ex:A0B1C2...),VALUE(0/1)");
AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "[ATMG] Usage: ATSG=r,PINNAMES(ex:A0B1C2...)");
return;
}
argc = parse_param(arg, argv);
cnts = strlen(argv[2]);
if(cnts % 2) return;
cnts /= 2;
if(cnts == 0) return;
if(strcmp(argv[1], "w") == 0){
write = 1;
if(strcmp(argv[3], "1") == 0)
data = 1;
}
// Remove debug info massage
_AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "[ATMG] %s: ", argv[1]);
ConfigDebugInfo = 0;
for(i=0; i<(cnts*2); i+=2){
pin = NC;
port = argv[2][i];
num = argv[2][i+1];
if(port >= 'a' && port <= 'z')
port -= ('a' - 'A');
if(num >= 'a' && num <= 'z')
num -= ('a' - 'A');
switch(port){
case 'A':
switch(num){
case '0': pin = PA_0; break; case '1': pin = PA_1; break; case '2': pin = PA_2; break; case '3': pin = PA_3; break;
case '4': pin = PA_4; break; case '5': pin = PA_5; break; case '6': pin = PA_6; break; case '7': pin = PA_7; break;
}
break;
case 'B':
switch(num){
case '0': pin = PB_0; break; case '1': pin = PB_1; break; case '2': pin = PB_2; break; case '3': pin = PB_3; break;
case '4': pin = PB_4; break; case '5': pin = PB_5; break; case '6': pin = PB_6; break; case '7': pin = PB_7; break;
}
break;
case 'C':
switch(num){
case '0': pin = PC_0; break; case '1': pin = PC_1; break; case '2': pin = PC_2; break; case '3': pin = PC_3; break;
case '4': pin = PC_4; break; case '5': pin = PC_5; break; case '6': pin = PC_6; break; case '7': pin = PC_7; break;
case '8': pin = PC_8; break; case '9': pin = PC_9; break;
}
break;
case 'D':
switch(num){
case '0': pin = PD_0; break; case '1': pin = PD_1; break; case '2': pin = PD_2; break; case '3': pin = PD_3; break;
case '4': pin = PD_4; break; case '5': pin = PD_5; break; case '6': pin = PD_6; break; case '7': pin = PD_7; break;
case '8': pin = PD_8; break; case '9': pin = PD_9; break;
}
break;
case 'E':
switch(num){
case '0': pin = PE_0; break; case '1': pin = PE_1; break; case '2': pin = PE_2; break; case '3': pin = PE_3; break;
case '4': pin = PE_4; break; case '5': pin = PE_5; break; case '6': pin = PE_6; break; case '7': pin = PE_7; break;
case '8': pin = PE_8; break; case '9': pin = PE_9; break; case 'A': pin = PE_A; break;
}
break;
case 'F':
switch(num){
case '0': pin = PF_0; break; case '1': pin = PF_1; break; case '2': pin = PF_2; break; case '3': pin = PF_3; break;
case '4': pin = PF_4; break; case '5': pin = PF_5; break;
}
break;
case 'G':
switch(num){
case '0': pin = PG_0; break; case '1': pin = PG_1; break; case '2': pin = PG_2; break; case '3': pin = PG_3; break;
case '4': pin = PG_4; break; case '5': pin = PG_5; break; case '6': pin = PG_6; break; case '7': pin = PG_7; break;
}
break;
case 'H':
switch(num){
case '0': pin = PH_0; break; case '1': pin = PH_1; break; case '2': pin = PH_2; break; case '3': pin = PH_3; break;
case '4': pin = PH_4; break; case '5': pin = PH_5; break; case '6': pin = PH_6; break; case '7': pin = PH_7; break;
}
break;
case 'I':
switch(num){
case '0': pin = PI_0; break; case '1': pin = PI_1; break; case '2': pin = PI_2; break; case '3': pin = PI_3; break;
case '4': pin = PI_4; break; case '5': pin = PI_5; break; case '6': pin = PI_6; break; case '7': pin = PI_7; break;
}
break;
case 'J':
switch(num){
case '0': pin = PJ_0; break; case '1': pin = PJ_1; break; case '2': pin = PJ_2; break; case '3': pin = PJ_3; break;
case '4': pin = PJ_4; break; case '5': pin = PJ_5; break; case '6': pin = PJ_6; break;
}
break;
case 'K':
switch(num){
case '0': pin = PK_0; break; case '1': pin = PK_1; break; case '2': pin = PK_2; break; case '3': pin = PK_3; break;
case '4': pin = PK_4; break; case '5': pin = PK_5; break; case '6': pin = PK_6; break;
}
break;
}
if(pin == NC){
_AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "X,");
continue;
}
// Initial input control pin
gpio_init(&gpio_test, pin);
if(write){
gpio_dir(&gpio_test, PIN_OUTPUT); // Direction: Output
gpio_mode(&gpio_test, PullNone); // No pull
gpio_write(&gpio_test, data);
_AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "%d,", data);
}else{
gpio_dir(&gpio_test, PIN_INPUT); // Direction: Input
gpio_mode(&gpio_test, PullUp); // Pull-High
val = gpio_read(&gpio_test);
_AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "%d,", val);
}
}
_AT_DBG_MSG(AT_FLAG_GPIO, AT_DBG_ALWAYS, "\n");
// Recover debug info massage
ConfigDebugInfo = tConfigDebugInfo;
}
void fATMR(void *arg)
{
u32 idx;
AT_PRINTK("[ATMR]: _AT_MP_SDR_TEST_");
#ifdef CONFIG_SDR_EN
for (idx = 0; idx < 0x200000; idx = idx+4){
HAL_WRITE32(0x30000000, idx, 0x12345678);
if (HAL_READ32(0x30000000, idx) != 0x12345678) {
AT_PRINTK("[ATMR]: SDR test fail addr 0x08x, value 0x08%x",(0x30000000+idx),HAL_READ32(0x30000000, idx));
return;
}
}
AT_PRINTK("[ATMR]: SDR test success");
#endif
}
void fATMt(void *arg)
{
int argc = 0;
char *argv[MAX_ARGC] = {0};
AT_PRINTK("[ATM#]: _AT_MP_TEST_");
argc = parse_param(arg, argv);
}
void fATMx(void *arg)
{
AT_PRINTK("[ATM?]: _AT_MP_HELP_");
}
log_item_t at_mp_items[] = {
{"ATMG", fATMG,}, // MP GPIO test
{"ATMR", fATMR,}, // MP SDR test
{"ATM#", fATMt,}, // test command
{"ATM?", fATMx,}, // Help
#if CONFIG_ATCMD_MP_EXT0
{"ATM0", fATM0,}, // MP ext0 AT command
#endif
};
void at_mp_init(void)
{
log_service_add_table(at_mp_items, sizeof(at_mp_items)/sizeof(at_mp_items[0]));
}
#if SUPPORT_LOG_SERVICE
log_module_init(at_mp_init);
#endif
#endif // #if CONFIG_ATCMD_MP

View file

@ -0,0 +1,12 @@
#ifndef __ATCMD_MP_H__
#define __ATCMD_MP_H__
#define CONFIG_ATCMD_MP_EXT0 0 //support MP ext0 AT command
typedef struct _at_command_mp_ext_item_{
char *mp_ext_cmd;
int (*mp_ext_fun)(void **argv, int argc);
char *mp_ext_usage;
}at_mp_ext_item_t;
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
#ifndef __ATCMD_SYS_H__
#define __ATCMD_SYS_H__
#ifdef CONFIG_AT_SYS
#endif //
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,166 @@
#ifndef __ATCMD_WIFI_H__
#define __ATCMD_WIFI_H__
#include <platform_opts.h>
#include "main.h"
#include "lwip_netconf.h"
#ifdef USE_FLASH_EEP
#include "flash_eep.h"
#include "feep_config.h"
#endif
#ifndef WLAN0_NAME
#define WLAN0_NAME "wlan0"
#endif
#ifndef WLAN1_NAME
#define WLAN1_NAME "wlan1"
#endif
/* Give default value if not defined */
#ifndef NET_IF_NUM
#ifdef CONFIG_CONCURRENT_MODE
#define NET_IF_NUM ((CONFIG_ETHERNET) + (CONFIG_WLAN) + 1)
#else
#define NET_IF_NUM ((CONFIG_ETHERNET) + (CONFIG_WLAN))
#endif // end of CONFIG_CONCURRENT_MODE
#endif // end of NET_IF_NUM
/*Static IP ADDRESS*/
#ifndef IP_ADDR0
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 3
#define IP_ADDR3 80
#endif
/*NETMASK*/
#ifndef NETMASK_ADDR0
#define NETMASK_ADDR0 255
#define NETMASK_ADDR1 255
#define NETMASK_ADDR2 255
#define NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef GW_ADDR0
#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 3
#define GW_ADDR3 1
#endif
/*Static IP ADDRESS*/
#ifndef AP_IP_ADDR0
#define AP_IP_ADDR0 192
#define AP_IP_ADDR1 168
#define AP_IP_ADDR2 43
#define AP_IP_ADDR3 1
#endif
/*NETMASK*/
#ifndef AP_NETMASK_ADDR0
#define AP_NETMASK_ADDR0 255
#define AP_NETMASK_ADDR1 255
#define AP_NETMASK_ADDR2 255
#define AP_NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef AP_GW_ADDR0
#define AP_GW_ADDR0 192
#define AP_GW_ADDR1 168
#define AP_GW_ADDR2 43
#define AP_GW_ADDR3 1
#endif
#if CONFIG_EXAMPLE_UART_ATCMD
#include "wifi_structures.h"
#include <wlan_fast_connect/example_wlan_fast_connect.h>
typedef struct _UART_LOG_CONF_{
u32 BaudRate;
u8 DataBits;
u8 StopBits;
u8 Parity;
u8 FlowControl;
}UART_LOG_CONF, *PUART_LOG_CONF;
#define ATCMD_WIFI_CONN_STORE_MAX_NUM (1)
struct atcmd_wifi_conf{
int32_t auto_enable;
rtw_wifi_setting_t setting;
int32_t reconn_num;
int32_t reconn_last_index;
struct wlan_fast_reconnect reconn[ATCMD_WIFI_CONN_STORE_MAX_NUM];
};
#define ATCMD_LWIP_CONN_STORE_MAX_NUM (1)
struct atcmd_lwip_conn_info{
int32_t role; //client, server or seed
uint32_t protocol; //tcp or udp
uint32_t remote_addr; //remote ip
uint32_t remote_port; //remote port
uint32_t local_addr; //locale ip, not used yet
uint32_t local_port; //locale port, not used yet
uint32_t reserved; //reserve for further use
};
struct atcmd_lwip_conf {
int32_t enable; //enable or not
int32_t conn_num;
int32_t last_index;
int32_t reserved; //reserve for further use
struct atcmd_lwip_conn_info conn[ATCMD_LWIP_CONN_STORE_MAX_NUM];
};
typedef enum {
AT_PARTITION_ALL = 0,
AT_PARTITION_UART = 1,
AT_PARTITION_WIFI = 2,
AT_PARTITION_LWIP = 3
} AT_PARTITION;
typedef enum {
AT_PARTITION_READ = 0,
AT_PARTITION_WRITE = 1,
AT_PARTITION_ERASE = 2
} AT_PARTITION_OP;
//first segment for uart
#define UART_SETTING_BACKUP_SECTOR (0x8000)
#define UART_CONF_DATA_OFFSET (0)
#define UART_CONF_DATA_SIZE ((((sizeof(UART_LOG_CONF)-1)>>2) + 1)<<2)
//second segment for wifi config
#define WIFI_CONF_DATA_OFFSET (UART_CONF_DATA_OFFSET+UART_CONF_DATA_SIZE)
#define WIFI_CONF_DATA_SIZE ((((sizeof(struct atcmd_wifi_conf)-1)>>2) + 1)<<2)
//fouth segment for lwip config
#define LWIP_CONF_DATA_OFFSET (WIFI_CONF_DATA_OFFSET+WIFI_CONF_DATA_SIZE)
#define LWIP_CONF_DATA_SIZE ((((sizeof(struct atcmd_lwip_conf)-1)>>2) + 1)<<2)
extern void atcmd_update_partition_info(AT_PARTITION id, AT_PARTITION_OP ops, u8 *data, u16 len);
#define ATSTRING_LEN (LOG_SERVICE_BUFLEN)
extern char at_string[ATSTRING_LEN];
extern unsigned char gAT_Echo; // default echo on
//extern void uart_at_lock(void);
//extern void uart_at_unlock(void);
extern void uart_at_send_string(char *str);
extern void uart_at_send_buf(u8 *buf, u32 len);
#define at_printf(fmt, args...) do{\
/*uart_at_lock();*/\
snprintf(at_string, ATSTRING_LEN, fmt, ##args); \
uart_at_send_string(at_string);\
/*uart_at_unlock();*/\
}while(0)
#define at_print_data(data, size) do{\
/*uart_at_lock();*/\
uart_at_send_buf(data, size);\
/*uart_at_unlock();*/\
}while(0)
#else
#define at_printf(fmt, args...) do{printf(fmt, ##args);}while(0)
#define at_print_data(data, size) do{__rtl_memDump(data, size, NULL);}while(0)
#endif//#if CONFIG_EXAMPLE_UART_ATCMD
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,171 @@
#ifndef __ATCMD_WIFI_H__
#define __ATCMD_WIFI_H__
#include <platform_opts.h>
#ifdef CONFIG_AT_WIFI
#include "main.h"
#include "lwip_netconf.h"
#ifdef USE_FLASH_EEP
#include "flash_eep.h"
#include "feep_config.h"
#endif
#ifndef WLAN0_NAME
#define WLAN0_NAME "wlan0"
#endif
#ifndef WLAN1_NAME
#define WLAN1_NAME "wlan1"
#endif
/* Give default value if not defined */
#ifndef NET_IF_NUM
#ifdef CONFIG_CONCURRENT_MODE
#define NET_IF_NUM ((CONFIG_ETHERNET) + (CONFIG_WLAN) + 1)
#else
#define NET_IF_NUM ((CONFIG_ETHERNET) + (CONFIG_WLAN))
#endif // end of CONFIG_CONCURRENT_MODE
#endif // end of NET_IF_NUM
/*Static IP ADDRESS*/
#ifndef IP_ADDR0
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 3
#define IP_ADDR3 80
#endif
/*NETMASK*/
#ifndef NETMASK_ADDR0
#define NETMASK_ADDR0 255
#define NETMASK_ADDR1 255
#define NETMASK_ADDR2 255
#define NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef GW_ADDR0
#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 3
#define GW_ADDR3 1
#endif
/*Static IP ADDRESS*/
#ifndef AP_IP_ADDR0
#define AP_IP_ADDR0 192
#define AP_IP_ADDR1 168
#define AP_IP_ADDR2 43
#define AP_IP_ADDR3 1
#endif
/*NETMASK*/
#ifndef AP_NETMASK_ADDR0
#define AP_NETMASK_ADDR0 255
#define AP_NETMASK_ADDR1 255
#define AP_NETMASK_ADDR2 255
#define AP_NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef AP_GW_ADDR0
#define AP_GW_ADDR0 192
#define AP_GW_ADDR1 168
#define AP_GW_ADDR2 43
#define AP_GW_ADDR3 1
#endif
#endif // CONFIG_AT_WIFI
#if CONFIG_EXAMPLE_UART_ATCMD
#include "wifi_structures.h"
#include <wlan_fast_connect/example_wlan_fast_connect.h>
typedef struct _UART_LOG_CONF_{
u32 BaudRate;
u8 DataBits;
u8 StopBits;
u8 Parity;
u8 FlowControl;
}UART_LOG_CONF, *PUART_LOG_CONF;
#define ATCMD_WIFI_CONN_STORE_MAX_NUM (1)
struct atcmd_wifi_conf{
struct wlan_fast_reconnect reconn[ATCMD_WIFI_CONN_STORE_MAX_NUM];
int32_t auto_enable;
rtw_wifi_setting_t setting;
int32_t reconn_num;
int32_t reconn_last_index;
};
#define ATCMD_LWIP_CONN_STORE_MAX_NUM (1)
struct atcmd_lwip_conn_info{
int32_t role; //client, server or seed
uint32_t protocol; //tcp or udp
uint32_t remote_addr; //remote ip
uint32_t remote_port; //remote port
uint32_t local_addr; //locale ip, not used yet
uint32_t local_port; //locale port, not used yet
uint32_t reserved; //reserve for further use
};
struct atcmd_lwip_conf {
int32_t enable; //enable or not
int32_t conn_num;
int32_t last_index;
int32_t reserved; //reserve for further use
struct atcmd_lwip_conn_info conn[ATCMD_LWIP_CONN_STORE_MAX_NUM];
};
typedef enum {
AT_PARTITION_ALL = 0,
AT_PARTITION_UART = FEEP_ID_UART_CFG,
AT_PARTITION_WIFI = FEEP_ID_WIFI_CFG,
AT_PARTITION_LWIP = FEEP_ID_LWIP_CFG
} AT_PARTITION;
typedef enum {
AT_PARTITION_READ = 0,
AT_PARTITION_WRITE = 1,
AT_PARTITION_ERASE = 2
} AT_PARTITION_OP;
//first segment for uart
#define UART_SETTING_BACKUP_SECTOR (0x8000)
#define UART_CONF_DATA_OFFSET (0)
#define UART_CONF_DATA_SIZE ((((sizeof(UART_LOG_CONF)-1)>>2) + 1)<<2)
//second segment for wifi config
#define WIFI_CONF_DATA_OFFSET (UART_CONF_DATA_OFFSET+UART_CONF_DATA_SIZE)
#define WIFI_CONF_DATA_SIZE ((((sizeof(struct atcmd_wifi_conf)-1)>>2) + 1)<<2)
//fouth segment for lwip config
#define LWIP_CONF_DATA_OFFSET (WIFI_CONF_DATA_OFFSET+WIFI_CONF_DATA_SIZE)
#define LWIP_CONF_DATA_SIZE ((((sizeof(struct atcmd_lwip_conf)-1)>>2) + 1)<<2)
extern void atcmd_update_partition_info(AT_PARTITION id, AT_PARTITION_OP ops, u8 *data, u16 len);
#define ATSTRING_LEN (LOG_SERVICE_BUFLEN)
extern char at_string[ATSTRING_LEN];
extern unsigned char gAT_Echo; // default echo on
//extern void uart_at_lock(void);
//extern void uart_at_unlock(void);
extern void uart_at_send_string(char *str);
extern void uart_at_send_buf(u8 *buf, u32 len);
#define at_printf(fmt, args...) do{\
/*uart_at_lock();*/\
snprintf(at_string, ATSTRING_LEN, fmt, ##args); \
uart_at_send_string(at_string);\
/*uart_at_unlock();*/\
}while(0)
#define at_print_data(data, size) do{\
/*uart_at_lock();*/\
uart_at_send_buf(data, size);\
/*uart_at_unlock();*/\
}while(0)
#else
#define at_printf(fmt, args...) do{printf(fmt, ##args);}while(0)
#define at_print_data(data, size) do{__rtl_memDump(data, size, NULL);}while(0)
#endif//#if CONFIG_EXAMPLE_UART_ATCMD
#endif

View file

@ -0,0 +1,497 @@
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "FreeRTOS.h"
#if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
#include "freertos_pmu.h"
#endif
#include "log_service.h"
#include "task.h"
#include "semphr.h"
#include "main.h"
#include "wifi_util.h"
#include "atcmd_wifi.h"
#include "osdep_api.h"
#if CONFIG_EXAMPLE_UART_ATCMD
#include "atcmd_lwip.h"
#endif
#if SUPPORT_LOG_SERVICE
//======================================================
struct list_head log_hash[ATC_INDEX_NUM];
#ifdef CONFIG_AT_USR
extern void at_user_init(void);
#endif
#ifdef CONFIG_AT_WIFI
extern void at_wifi_init(void);
#endif
//extern void at_fs_init(void);
#ifdef CONFIG_AT_SYS
extern void at_sys_init(void);
#endif
#if CONFIG_ETHERNET
extern void at_ethernet_init(void);
#endif
#if CONFIG_GOOGLE_NEST
extern void at_google_init(void);
#endif
#ifdef CONFIG_AT_LWIP
extern void at_transport_init(void);
#endif
//extern void at_app_init(void);
#if CONFIG_ALINK
extern void at_cloud_init(void);
#endif
char log_buf[LOG_SERVICE_BUFLEN];
#if CONFIG_LOG_HISTORY
char log_history[LOG_HISTORY_LEN][LOG_SERVICE_BUFLEN];
static unsigned int log_history_count = 0;
#endif
xSemaphoreHandle log_rx_interrupt_sema = NULL;
#if CONFIG_LOG_SERVICE_LOCK
xSemaphoreHandle log_service_sema = NULL;
#endif
extern xSemaphoreHandle uart_rx_interrupt_sema;
#if CONFIG_INIC_EN
extern unsigned char inic_cmd_ioctl;
#endif
#if defined (__ICCARM__)
#pragma section=".data.log_init"
unsigned int __log_init_begin__;
unsigned int __log_init_end__;
#elif defined ( __CC_ARM ) || defined(__GNUC__)
//#pragma section=".data.log_init"
log_init_t* __log_init_begin__;
log_init_t* __log_init_end__;
log_init_t log_init_table[] = {
#ifdef CONFIG_AT_WIFI
at_wifi_init,
#endif
// at_fs_init,
#ifdef CONFIG_AT_SYS
at_sys_init,
#endif
#if CONFIG_ETHERNET
at_ethernet_init
#endif
#if CONFIG_GOOGLE_NEST
at_google_init
#endif
#if CONFIG_TRANSPORT
at_transport_init
#endif
#if CONFIG_ALINK
at_cloud_init
#endif
#ifdef CONFIG_AT_USR
at_user_init
#endif
// at_app_init
};
#else
#error "not implement, add to linker script"
extern unsigned int __log_init_begin__;
extern unsigned int __log_init_end__;
#endif
#if defined(__GNUC__)
#define USE_STRSEP
#endif
//======================================================
int hash_index(char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
void log_add_new_command(log_item_t *new)
{
int index = hash_index(new->log_cmd)%ATC_INDEX_NUM;
list_add(&new->node, &log_hash[index]);
}
void start_log_service(void);
void log_service_init(void)
{
int i;
#if defined (__ICCARM__)
log_init_t *log_init_table;
__log_init_begin__ = (unsigned int)__section_begin(".data.log_init");
__log_init_end__ = (unsigned int)__section_end(".data.log_init");
log_init_table = (log_init_t *)__log_init_begin__;
#elif defined(__CC_ARM) || defined(__GNUC__)
__log_init_begin__ = log_init_table;
__log_init_end__ = log_init_table + sizeof(log_init_table);
#else
#error "not implement"
#endif
for(i=0;i<ATC_INDEX_NUM;i++)
INIT_LIST_HEAD(&log_hash[i]);
for(i=0;i<(__log_init_end__-__log_init_begin__)/sizeof(log_init_t); i++)
log_init_table[i]();
/* Initial uart rx swmaphore*/
vSemaphoreCreateBinary(log_rx_interrupt_sema);
xSemaphoreTake(log_rx_interrupt_sema, 1/portTICK_RATE_MS);
#if CONFIG_LOG_SERVICE_LOCK
log_service_lock_init();
#endif
start_log_service();
}
//sizeof(log_items)/sizeof(log_items[0])
void log_service_add_table(log_item_t *tbl, int len)
{
int i;
for(i=0;i<len;i++)
log_add_new_command(&tbl[i]);
}
void* log_action(char *cmd)
{
int search_cnt=0;
int index = hash_index(cmd)%ATC_INDEX_NUM;
struct list_head *head = &log_hash[index];
struct list_head *iterator;
log_item_t *item;
void *act = NULL;
list_for_each(iterator, head) {
item = list_entry(iterator, log_item_t, node);
search_cnt++;
if( strcmp(item->log_cmd, cmd) == 0){
//printf("%s match %s, search cnt %d\n\r", cmd, item->log_cmd, search_cnt);
act = (void*)item->at_act;
break;
}
}
return act;
}
void* log_handler(char *cmd)
{
log_act_t action=NULL;
char buf[LOG_SERVICE_BUFLEN] = {0};
char *copy=buf;
char *token = NULL;
char *param = NULL;
char tok[5] = {0};//'\0'
#if CONFIG_LOG_HISTORY
strcpy(log_history[((log_history_count++)%LOG_HISTORY_LEN)], log_buf);
#endif
strncpy(copy, cmd,LOG_SERVICE_BUFLEN-1);
#if defined(USE_STRSEP)
token = _strsep(&copy, "=");
param = copy;
#else
token = strtok(copy, "=");
param = strtok(NULL, NULL);
#endif
if(token && (strlen(token) <= 4))
strcpy(tok, token);
else{
//printf("\n\rAT Cmd format error!\n");
return NULL;
};
//printf(" Command %s \n\r ", tok);
//printf(" Param %s \n\r", param);
action = (log_act_t)log_action(tok);
if(action){
action(param);
}
return (void*)action;
}
int parse_param(char *buf, char **argv)
{
int argc = 1;
char str_buf[LOG_SERVICE_BUFLEN] = "\0";
int str_count = 0;
int buf_cnt = 0;
if(buf == NULL)
goto exit;
while((argc < MAX_ARGC) && (*buf != '\0')) {
while((*buf == ',') || (*buf == '[') || (*buf == ']')){
if((*buf == ',') && (*(buf+1) == ',')){
argv[argc] = NULL;
argc++;
}
*buf = '\0';
buf++;
}
if(*buf == '\0')
break;
else if(*buf == '"'){
memset(str_buf,'\0',LOG_SERVICE_BUFLEN);
str_count = 0;
buf_cnt = 0;
*buf = '\0';
buf ++;
if(*buf == '\0')
break;
argv[argc] = buf;
while((*buf != '"')&&(*buf != '\0')){
if(*buf == '\\'){
buf ++;
buf_cnt++;
}
str_buf[str_count] = *buf;
str_count++;
buf_cnt++;
buf ++;
}
*buf = '\0';
memcpy(buf-buf_cnt,str_buf,buf_cnt);
}
else{
argv[argc] = buf;
}
argc++;
buf++;
while( (*buf != ',')&&(*buf != '\0')&&(*buf != '[')&&(*buf != ']') )
buf++;
}
exit:
return argc;
}
unsigned char gDbgLevel = AT_DBG_ERROR;
unsigned int gDbgFlag = 0xFFFFFFFF;
void at_set_debug_level(unsigned char newDbgLevel)
{
gDbgLevel = newDbgLevel;
}
void at_set_debug_mask(unsigned int newDbgFlag)
{
gDbgFlag = newDbgFlag;
}
#if SUPPORT_INTERACTIVE_MODE
extern char uart_buf[64];
void legency_interactive_handler(unsigned char argc, unsigned char **argv)
{
#if 0 //defined(CONFIG_PLATFORM_8195A)
if(argc<1)
{
DiagPrintf("Wrong argument number!\r\n");
return;
}
DiagPrintf("Wlan Normal Mode\n");
WlanNormal( argc, argv);
#else
strncpy(uart_buf, log_buf, 63);//uart_buf[64]
xSemaphoreGive(uart_rx_interrupt_sema);
#endif
}
#endif
#if CONFIG_WLAN
#ifndef WLAN0_NAME
#define WLAN0_NAME "wlan0"
#endif
#ifndef WLAN1_NAME
#define WLAN1_NAME "wlan1"
#endif
int mp_commnad_handler(char *cmd)
{
char buf[64] = {0};
char *token = NULL;
//strcpy(buf, cmd);
strncpy(buf, cmd, (64-1));
token = strtok(buf, " ");
if(token && (strcmp(buf, "iwpriv") == 0)){
token = strtok(NULL, "");
wext_private_command(WLAN0_NAME, token, 1);
return 0;
}
return -1;
}
#endif
void print_help_msg(void){
#if CONFIG_WLAN
extern void print_wlan_help(void);
print_wlan_help();
#endif
//add other help message print here
}
int print_help_handler(char *cmd){
if(strcmp(cmd, "help") == 0){
print_help_msg();
return 0;
}
return -1;
}
#if CONFIG_LOG_SERVICE_LOCK
void log_service_lock(void)
{
rtw_down_sema(&log_service_sema);
}
u32 log_service_lock_timeout(u32 ms)
{
return rtw_down_timeout_sema(&log_service_sema, ms);
}
void log_service_unlock(void)
{
rtw_up_sema(&log_service_sema);
}
void log_service_lock_init(void){
rtw_init_sema(&log_service_sema, 1);
}
#endif
void log_service(void *param)
{
_AT_DBG_MSG(AT_FLAG_COMMON, AT_DBG_ALWAYS, "\n\rStart LOG SERVICE MODE\n\r");
_AT_DBG_MSG(AT_FLAG_COMMON, AT_DBG_ALWAYS, "\n\r# ");
while(1){
while(xSemaphoreTake(log_rx_interrupt_sema, portMAX_DELAY) != pdTRUE);
#if CONFIG_LOG_SERVICE_LOCK
log_service_lock();
#endif
if(log_handler((char *)log_buf) == NULL){
#if CONFIG_WLAN
if(mp_commnad_handler((char *)log_buf) < 0)
#endif
{
#if SUPPORT_INTERACTIVE_MODE
print_help_handler((char *)log_buf);
legency_interactive_handler(NULL, NULL);
#if CONFIG_LOG_SERVICE_LOCK
log_service_unlock();
#endif
continue;
#else
if(print_help_handler((char *)log_buf) < 0){
at_printf("\r\nunknown command '%s'", log_buf);
}
#endif
}
}
log_buf[0] = '\0';
#if CONFIG_INIC_EN
inic_cmd_ioctl = 0;
#endif
_AT_DBG_MSG(AT_FLAG_COMMON, AT_DBG_ALWAYS, "\n\r[MEM] After do cmd, available heap %d+%d\n\r", xPortGetFreeHeapSize(), tcm_heap_freeSpace());
_AT_DBG_MSG(AT_FLAG_COMMON, AT_DBG_ALWAYS, "\r\n\n# "); //"#" is needed for mp tool
#if CONFIG_EXAMPLE_UART_ATCMD
if(atcmd_lwip_is_tt_mode())
at_printf(STR_END_OF_ATDATA_RET);
else
at_printf(STR_END_OF_ATCMD_RET);
#endif
#if CONFIG_LOG_SERVICE_LOCK
log_service_unlock();
#endif
#if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
pmu_release_wakelock(WAKELOCK_LOGUART);
#endif
}
}
#define STACKSIZE 1280
void start_log_service(void)
{
xTaskHandle CreatedTask;
int result;
#if 0 // CONFIG_USE_TCM_HEAP
extern void *tcm_heap_malloc(int size);
void *stack_addr = tcm_heap_malloc(STACKSIZE * sizeof(int));
if(stack_addr == NULL){
}
result = xTaskGenericCreate(
log_service,
( signed portCHAR * ) "log_srv",
STACKSIZE,
NULL,
tskIDLE_PRIORITY + 5,
&CreatedTask,
stack_addr,
NULL);
#else
result = xTaskCreate( log_service, ( signed portCHAR * ) "log_srv", STACKSIZE, NULL, tskIDLE_PRIORITY + 5, &CreatedTask );
#endif
if(result != pdPASS) {
printf("\n\r%s xTaskCreate failed", __FUNCTION__);
}
}
void fAT_exit(void *arg){
printf("\n\rLeave LOG SERVICE");
vTaskDelete(NULL);
}
#if CONFIG_LOG_HISTORY
void fAT_log(void *arg){
int i = 0;
printf("[AT]log history:\n\n\r");
if(log_history_count > LOG_HISTORY_LEN){
for(i=0; i<4; i++)
printf(" %s\n\r", log_history[((log_history_count+i)%LOG_HISTORY_LEN)]);
}
else{
for(i=0; i<(log_history_count-1); i++)
printf(" %s\n\r", log_history[i]);
}
}
#endif
log_item_t at_log_items[ ] = {
{"AT--", fAT_exit,},
#if CONFIG_LOG_HISTORY
{"AT??", fAT_log,},
#endif
{"ATxx", fAT_exit,}
};
void at_log_init(void)
{
log_service_add_table(at_log_items, sizeof(at_log_items)/sizeof(at_log_items[0]));
}
log_module_init(at_log_init);
#endif

View file

@ -0,0 +1,123 @@
#ifndef LOG_SERVICE_H
#define LOG_SERVICE_H
#include "dlist.h"
/*
* Include user defined options first. Anything not defined in these files
* will be set to standard values. Override anything you dont like!
*/
#if defined(CONFIG_PLATFORM_8195A) || defined(CONFIG_PLATFORM_8711B)
#include "platform_opts.h"
#include "platform_stdlib.h"
#endif
#ifdef __ICCARM__
#define STRINGIFY(s) #s
#define SECTION(_name) _Pragma( STRINGIFY(location=_name))
#define log_module_init(fn) \
SECTION(".data.log_init") __root static void* log_##fn = (void*)fn
#elif defined(__CC_ARM)
#define log_module_init(fn) \
static void* log_##fn __attribute__((section(".data.log_init"))) = (void*)fn;
#define DiagPrintf printf
#elif defined(__GNUC__)
#define log_module_init(fn) \
static void* log_##fn __attribute__((section(".data.log_init"))) = (void*)fn;
#else
#error "not implement"
#endif
#define ATC_INDEX_NUM 32
#ifndef SUPPORT_LOG_SERVICE
#define SUPPORT_LOG_SERVICE 1
#endif
//LOG_SERVICE_BUFLEN: default, only 63 bytes could be used for keeping input
// cmd, the last byte is for string end ('\0').
#ifndef LOG_SERVICE_BUFLEN
#define LOG_SERVICE_BUFLEN 64
#endif
#ifndef CONFIG_LOG_HISTORY
#define CONFIG_LOG_HISTORY 0
#if CONFIG_LOG_HISTORY
#define LOG_HISTORY_LEN 5
#endif
#endif //#ifndef CONFIG_LOG_HISTORY
#ifndef MAX_ARGC
#define MAX_ARGC 12
#endif
#ifndef CONFIG_LOG_SERVICE_LOCK
#define CONFIG_LOG_SERVICE_LOCK 0 // //to protect log_buf[], only one command processed per time
#endif
#define AT_BIT(n) (1<<n)
#define AT_FLAG_DUMP AT_BIT(0)
#define AT_FLAG_EDIT AT_BIT(1)
#define AT_FLAG_ADC AT_BIT(2)
#define AT_FLAG_GPIO AT_BIT(3)
#define AT_FLAG_OTA AT_BIT(4)
#define AT_FLAG_NFC AT_BIT(5)
#define AT_FLAG_OS AT_BIT(6)
#define AT_FLAG_LWIP AT_BIT(7)
#define AT_FLAG_COMMON AT_BIT(8)
#define AT_FLAG_WIFI AT_BIT(9)
enum{
AT_DBG_OFF = 0,
AT_DBG_ALWAYS,
AT_DBG_ERROR,
AT_DBG_WARNING,
AT_DBG_INFO
};
extern unsigned char gDbgLevel;
extern unsigned int gDbgFlag;
#define AT_PRINTK(fmt, args...) printf(fmt"\r\n",## args)
#define _AT_PRINTK(fmt, args...) printf(fmt,## args)
#define AT_DBG_MSG(flag, level, fmt, args...) \
do{ \
if(((flag) & gDbgFlag) && (level <= gDbgLevel)){ \
AT_PRINTK(fmt,## args); \
} \
}while(0)
#define _AT_DBG_MSG(flag, level, fmt, args...) \
do{ \
if(((flag) & gDbgFlag) && (level <= gDbgLevel)){ \
_AT_PRINTK(fmt,## args); \
} \
}while(0)
#ifndef SUPPORT_INTERACTIVE_MODE
#define SUPPORT_INTERACTIVE_MODE 0
#endif //#ifndef SUPPORT_INTERACTIVE_MODE
typedef void (*log_init_t)(void);
typedef void (*log_act_t)(void*);
typedef struct _at_command_item_{
char *log_cmd;
log_act_t at_act;
struct list_head node;
}log_item_t;
void log_service_add_table(log_item_t *tbl, int len);
int parse_param(char *buf, char **argv);
void at_set_debug_mask(unsigned int newDbgFlag);
void at_set_debug_level(unsigned char newDbgLevel);
void log_service_init(void);
#if CONFIG_LOG_SERVICE_LOCK
void log_service_lock_init(void);
void log_service_lock(void);
u32 log_service_lock_timeout(u32 ms);
void log_service_unlock(void);
#endif
#define C_NUM_AT_CMD 4 //"ATxx", 4 characters
#define C_NUM_AT_CMD_DLT 1 //"=", 1 charater
#define STR_END_OF_ATCMD_RET "\r\n# " //each AT command response will end with this string
#define STR_END_OF_ATDATA_RET "\r\n> " //data transparent transmission indicator
#endif

View file

@ -0,0 +1,455 @@
/* Includes ------------------------------------------------------------------*/
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/dhcp.h"
#include "lwip/dns.h"
#include "ethernetif.h"
#include "main.h"
#include "lwip_netconf.h"
#if CONFIG_WLAN
#include "wifi_ind.h"
#endif
#if defined(STM32F2XX)
#include "stm322xg_eval_lcd.h"
#elif defined(STM32F4XX)
#include "stm324xg_eval_lcd.h"
#endif
#include <platform/platform_stdlib.h>
/*Static IP ADDRESS*/
#ifndef IP_ADDR0
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 1
#define IP_ADDR3 80
#endif
/*NETMASK*/
#ifndef NETMASK_ADDR0
#define NETMASK_ADDR0 255
#define NETMASK_ADDR1 255
#define NETMASK_ADDR2 255
#define NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef GW_ADDR0
#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 1
#define GW_ADDR3 1
#endif
/*Static IP ADDRESS*/
#ifndef AP_IP_ADDR0
#define AP_IP_ADDR0 192
#define AP_IP_ADDR1 168
#define AP_IP_ADDR2 43
#define AP_IP_ADDR3 1
#endif
/*NETMASK*/
#ifndef AP_NETMASK_ADDR0
#define AP_NETMASK_ADDR0 255
#define AP_NETMASK_ADDR1 255
#define AP_NETMASK_ADDR2 255
#define AP_NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef AP_GW_ADDR0
#define AP_GW_ADDR0 192
#define AP_GW_ADDR1 168
#define AP_GW_ADDR2 43
#define AP_GW_ADDR3 1
#endif
/*Static IP ADDRESS FOR ETHERNET*/
#ifndef ETH_IP_ADDR0
#define ETH_IP_ADDR0 192
#define ETH_IP_ADDR1 168
#define ETH_IP_ADDR2 0
#define ETH_IP_ADDR3 80
#endif
/*NETMASK FOR ETHERNET*/
#ifndef ETH_NETMASK_ADDR0
#define ETH_NETMASK_ADDR0 255
#define ETH_NETMASK_ADDR1 255
#define ETH_NETMASK_ADDR2 255
#define ETH_NETMASK_ADDR3 0
#endif
/*Gateway address for ethernet*/
#ifndef ETH_GW_ADDR0
#define ETH_GW_ADDR0 192
#define ETH_GW_ADDR1 168
#define ETH_GW_ADDR2 0
#define ETH_GW_ADDR3 1
#endif
/* Private define ------------------------------------------------------------*/
#define MAX_DHCP_TRIES 5
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
struct netif xnetif[NET_IF_NUM]; /* network interface structure */
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes the lwIP stack
* @param None
* @retval None
*/
#if CONFIG_WLAN
extern int error_flag;
extern rtw_mode_t wifi_mode;
#endif
int lwip_init_done = 0;
void LwIP_Init(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
int8_t idx = 0;
/* Create tcp_ip stack thread */
tcpip_init( NULL, NULL );
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
struct ip_addr *netmask, struct ip_addr *gw,
void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif))
Adds your network interface to the netif_list. Allocate a struct
netif and pass a pointer to this structure as the first argument.
Give pointers to cleared ip_addr structures when using DHCP,
or fill them with sane numbers otherwise. The state pointer may be NULL.
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
//printf("NET_IF_NUM:%d\n\r",NET_IF_NUM);
for(idx=0;idx<NET_IF_NUM;idx++){
if(idx==0){
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
}
else{
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
#if CONFIG_ETHERNET
if(idx == NET_IF_NUM - 1)
{
IP4_ADDR(&ipaddr, ETH_IP_ADDR0, ETH_IP_ADDR1, ETH_IP_ADDR2, ETH_IP_ADDR3);
IP4_ADDR(&netmask, ETH_NETMASK_ADDR0, ETH_NETMASK_ADDR1 , ETH_NETMASK_ADDR2, ETH_NETMASK_ADDR3);
IP4_ADDR(&gw, ETH_GW_ADDR0, ETH_GW_ADDR1, ETH_GW_ADDR2, ETH_GW_ADDR3);
}
#endif
xnetif[idx].name[0] = 'r';
xnetif[idx].name[1] = '0'+idx;
#if CONFIG_ETHERNET
if(idx == NET_IF_NUM - 1)
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_mii_init, &tcpip_input);
else
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
#else
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
#endif
printf("interface %d is initialized\n", idx);
}
/* Registers the default network interface. */
netif_set_default(&xnetif[0]);
/*move these operations to wifi_on/wifi_off*/
#if 0
/* When the netif is fully configured this function must be called.*/
for(idx = 0;idx < NET_IF_NUM;idx++)
netif_set_up(&xnetif[idx]);
#endif
lwip_init_done = 1;
}
/**
* @brief LwIP_DHCP_Process_Handle
* @param None
* @retval None
*/
uint8_t LwIP_DHCP(uint8_t idx, uint8_t dhcp_state)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
uint32_t IPaddress;
uint8_t iptab[4];
uint8_t DHCP_state;
int mscnt = 0;
struct netif *pnetif = NULL;
DHCP_state = dhcp_state;
#if !CONFIG_ETHERNET
if(idx > 1)
idx = 1;
#endif
pnetif = &xnetif[idx];
if(DHCP_state == 0){
pnetif->ip_addr.addr = 0;
pnetif->netmask.addr = 0;
pnetif->gw.addr = 0;
}
for (;;)
{
//printf(" ========DHCP_state:%d============\n",DHCP_state);
switch (DHCP_state)
{
case DHCP_START:
{
#if CONFIG_WLAN
wifi_unreg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl);
#endif
dhcp_start(pnetif);
IPaddress = 0;
DHCP_state = DHCP_WAIT_ADDRESS;
}
break;
case DHCP_WAIT_ADDRESS:
{
/* If DHCP stopped by wifi_disconn_hdl*/
if(pnetif->dhcp->state == 0)
{
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
printf("LwIP_DHCP: dhcp stop.\n");
return DHCP_STOP;
}
/* Read the new IP address */
IPaddress = pnetif->ip_addr.addr;
if (IPaddress!=0)
{
DHCP_state = DHCP_ADDRESS_ASSIGNED;
#if CONFIG_WLAN
wifi_reg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl, NULL);
#endif
/* Stop DHCP */
// dhcp_stop(pnetif); /* can not stop, need to renew, Robbie*/
iptab[0] = (uint8_t)(IPaddress >> 24);
iptab[1] = (uint8_t)(IPaddress >> 16);
iptab[2] = (uint8_t)(IPaddress >> 8);
iptab[3] = (uint8_t)(IPaddress);
printf("Interface %d IP address : %d.%d.%d.%d\n", idx, iptab[3], iptab[2], iptab[1], iptab[0]);
// printf("Time at start %d ms.\n", xTaskGetTickCount());
#if CONFIG_WLAN
error_flag = RTW_NO_ERROR;
#endif
return DHCP_ADDRESS_ASSIGNED;
}
else
{
/* DHCP timeout */
if (pnetif->dhcp->tries > MAX_DHCP_TRIES)
{
DHCP_state = DHCP_TIMEOUT;
/* Stop DHCP */
dhcp_stop(pnetif);
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
iptab[0] = IP_ADDR3;
iptab[1] = IP_ADDR2;
iptab[2] = IP_ADDR1;
iptab[3] = IP_ADDR0;
printf("Interface %d DHCP timeout\n",idx);
printf("Static IP address : %d.%d.%d.%d\n", iptab[3], iptab[2], iptab[1], iptab[0]);
#if CONFIG_WLAN
error_flag = RTW_DHCP_FAIL;
#endif
#if CONFIG_ETHERNET
if(idx == NET_IF_NUM -1) // This is the ethernet interface, set it up for static ip address
netif_set_up(pnetif);
#endif
return DHCP_TIMEOUT;
}else
{
//sys_msleep(DHCP_FINE_TIMER_MSECS);
vTaskDelay(DHCP_FINE_TIMER_MSECS);
dhcp_fine_tmr();
mscnt += DHCP_FINE_TIMER_MSECS;
if (mscnt >= DHCP_COARSE_TIMER_SECS*1000)
{
dhcp_coarse_tmr();
mscnt = 0;
}
}
}
}
break;
case DHCP_RELEASE_IP:
#if CONFIG_WLAN
wifi_unreg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl);
#endif
printf("LwIP_DHCP: Release ip\n");
dhcp_release_unicast(pnetif);
return DHCP_RELEASE_IP;
case DHCP_STOP:
#if CONFIG_WLAN
wifi_unreg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl);
#endif
printf("LwIP_DHCP: dhcp stop.\n");
dhcp_stop(pnetif);
return DHCP_STOP;
default:
break;
}
}
}
void LwIP_ReleaseIP(uint8_t idx)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
struct netif *pnetif = &xnetif[idx];
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 0, 0, 0, 0);
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
}
uint8_t* LwIP_GetMAC(struct netif *pnetif)
{
return (uint8_t *) (pnetif->hwaddr);
}
uint8_t* LwIP_GetIP(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->ip_addr);
}
uint8_t* LwIP_GetGW(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->gw);
}
uint8_t* LwIP_GetMASK(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->netmask);
}
uint8_t* LwIP_GetBC(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->dhcp->offered_bc_addr);
}
#if LWIP_DNS
void LwIP_GetDNS(struct ip_addr* dns)
{
*dns = dns_getserver(0);
}
void LwIP_SetDNS(struct ip_addr* dns)
{
dns_setserver(0, dns);
}
#endif
void LwIP_UseStaticIP(struct netif *pnetif)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
/* Static address used */
if(pnetif->name[1] == '0'){
#if CONFIG_WLAN
if(wifi_mode == RTW_MODE_STA){
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
}
else if(wifi_mode == RTW_MODE_AP){
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
#endif
}else{
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
}
#if LWIP_AUTOIP
#include <lwip/autoip.h>
void LwIP_AUTOIP(struct netif *pnetif)
{
uint8_t *ip = LwIP_GetIP(pnetif);
autoip_start(pnetif);
while((pnetif->autoip->state == AUTOIP_STATE_PROBING) || (pnetif->autoip->state == AUTOIP_STATE_ANNOUNCING)) {
vTaskDelay(1000);
}
if(*((uint32_t *) ip) == 0) {
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
printf("AUTOIP timeout\n");
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
printf("Static IP address : %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
}
else {
printf("Link-local address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
}
}
#endif
#if LWIP_IPV6
/* Get IPv6 address with lwip 1.5.0 */
void LwIP_AUTOIP_IPv6(struct netif *pnetif)
{
uint8_t *ipv6 = (uint8_t *) &(pnetif->ip6_addr[0].addr[0]);
netif_create_ip6_linklocal_address(pnetif, 1);
printf("IPv6 link-local address: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
ipv6[0], ipv6[1], ipv6[2], ipv6[3], ipv6[4], ipv6[5], ipv6[6], ipv6[7],
ipv6[8], ipv6[9], ipv6[10], ipv6[11], ipv6[12], ipv6[13], ipv6[14], ipv6[15]);
}
#endif

View file

@ -0,0 +1,91 @@
/**
******************************************************************************
* @file netconf.h
* @author MCD Application Team
* @version V1.1.0
* @date 07-October-2011
* @brief This file contains all the functions prototypes for the netconf.c
* file.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __NETCONF_H
#define __NETCONF_H
#ifdef __cplusplus
extern "C" {
#endif
#include "tcpip.h"
/* Includes ------------------------------------------------------------------*/
#include <platform/platform_stdlib.h>
#include "platform_opts.h"
#include "autoconf.h"
// macros
/* Give default value if not defined */
#ifndef NET_IF_NUM
#ifdef CONFIG_CONCURRENT_MODE
#define NET_IF_NUM ((CONFIG_ETHERNET) + (CONFIG_WLAN) + 1)
#else
#define NET_IF_NUM ((CONFIG_ETHERNET) + (CONFIG_WLAN))
#endif // end of CONFIG_CONCURRENT_MODE
#endif // end of NET_IF_NUM
/* Private typedef -----------------------------------------------------------*/
typedef enum
{
DHCP_START=0,
DHCP_WAIT_ADDRESS,
DHCP_ADDRESS_ASSIGNED,
DHCP_RELEASE_IP,
DHCP_STOP,
DHCP_TIMEOUT
} DHCP_State_TypeDef;
/* Extern functions ------------------------------------------------------------*/
void wifi_rx_beacon_hdl( char* buf, int buf_len, int flags, void* userdata);
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void LwIP_Init(void);
uint8_t LwIP_DHCP(uint8_t idx, uint8_t dhcp_state);
unsigned char* LwIP_GetMAC(struct netif *pnetif);
unsigned char* LwIP_GetIP(struct netif *pnetif);
unsigned char* LwIP_GetGW(struct netif *pnetif);
uint8_t* LwIP_GetMASK(struct netif *pnetif);
uint8_t* LwIP_GetBC(struct netif *pnetif);
#if LWIP_DNS
void LwIP_GetDNS(struct ip_addr* dns);
void LwIP_SetDNS(struct ip_addr* dns);
#endif
void LwIP_UseStaticIP(struct netif *pnetif);
#if LWIP_AUTOIP
void LwIP_AUTOIP(struct netif *pnetif);
#endif
#if LWIP_IPV6
void LwIP_AUTOIP_IPv6(struct netif *pnetif);
#endif
#ifdef __cplusplus
}
#endif
#endif /* __NETCONF_H */
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,405 @@
/* Includes ------------------------------------------------------------------*/
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/dhcp.h"
#include "lwip/dns.h"
#include "ethernetif.h"
#include "main.h"
#include "lwip_netconf.h"
#if CONFIG_WLAN
#include "wifi_ind.h"
#endif
#if defined(STM32F2XX)
#include "stm322xg_eval_lcd.h"
#elif defined(STM32F4XX)
#include "stm324xg_eval_lcd.h"
#endif
#include <platform/platform_stdlib.h>
/*Static IP ADDRESS*/
#ifndef IP_ADDR0
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 1
#define IP_ADDR3 80
#endif
/*NETMASK*/
#ifndef NETMASK_ADDR0
#define NETMASK_ADDR0 255
#define NETMASK_ADDR1 255
#define NETMASK_ADDR2 255
#define NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef GW_ADDR0
#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 1
#define GW_ADDR3 1
#endif
/*Static IP ADDRESS*/
#ifndef AP_IP_ADDR0
#define AP_IP_ADDR0 192
#define AP_IP_ADDR1 168
#define AP_IP_ADDR2 43
#define AP_IP_ADDR3 1
#endif
/*NETMASK*/
#ifndef AP_NETMASK_ADDR0
#define AP_NETMASK_ADDR0 255
#define AP_NETMASK_ADDR1 255
#define AP_NETMASK_ADDR2 255
#define AP_NETMASK_ADDR3 0
#endif
/*Gateway Address*/
#ifndef AP_GW_ADDR0
#define AP_GW_ADDR0 192
#define AP_GW_ADDR1 168
#define AP_GW_ADDR2 43
#define AP_GW_ADDR3 1
#endif
/*Static IP ADDRESS FOR ETHERNET*/
#ifndef ETH_IP_ADDR0
#define ETH_IP_ADDR0 192
#define ETH_IP_ADDR1 168
#define ETH_IP_ADDR2 0
#define ETH_IP_ADDR3 80
#endif
/*NETMASK FOR ETHERNET*/
#ifndef ETH_NETMASK_ADDR0
#define ETH_NETMASK_ADDR0 255
#define ETH_NETMASK_ADDR1 255
#define ETH_NETMASK_ADDR2 255
#define ETH_NETMASK_ADDR3 0
#endif
/*Gateway address for ethernet*/
#ifndef ETH_GW_ADDR0
#define ETH_GW_ADDR0 192
#define ETH_GW_ADDR1 168
#define ETH_GW_ADDR2 0
#define ETH_GW_ADDR3 1
#endif
/* Private define ------------------------------------------------------------*/
#define MAX_DHCP_TRIES 5
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
struct netif xnetif[NET_IF_NUM]; /* network interface structure */
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes the lwIP stack
* @param None
* @retval None
*/
#if CONFIG_WLAN
extern char error_flag;
extern unsigned char wifi_mode; // rtw_mode_t
#endif
int lwip_init_done = 0;
void LwIP_Init(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
int8_t idx = 0;
/* Create tcp_ip stack thread */
tcpip_init( NULL, NULL );
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
struct ip_addr *netmask, struct ip_addr *gw,
void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif))
Adds your network interface to the netif_list. Allocate a struct
netif and pass a pointer to this structure as the first argument.
Give pointers to cleared ip_addr structures when using DHCP,
or fill them with sane numbers otherwise. The state pointer may be NULL.
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
//printf("NET_IF_NUM:%d\n\r",NET_IF_NUM);
for(idx = NET_IF_NUM - 1; idx >= 0 ; idx--) {
if(idx == 0){
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
}
#if CONFIG_ETHERNET
else if(idx == NET_IF_NUM - 1)
{
IP4_ADDR(&ipaddr, ETH_IP_ADDR0, ETH_IP_ADDR1, ETH_IP_ADDR2, ETH_IP_ADDR3);
IP4_ADDR(&netmask, ETH_NETMASK_ADDR0, ETH_NETMASK_ADDR1 , ETH_NETMASK_ADDR2, ETH_NETMASK_ADDR3);
IP4_ADDR(&gw, ETH_GW_ADDR0, ETH_GW_ADDR1, ETH_GW_ADDR2, ETH_GW_ADDR3);
}
#endif
else {
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
xnetif[idx].name[0] = 'r';
xnetif[idx].name[1] = '0'+idx;
#if CONFIG_ETHERNET
if(idx == NET_IF_NUM - 1)
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_mii_init, &tcpip_input);
else
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
#else
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
#endif
info_printf("interface %d is initialized\n", idx);
}
/* Registers the default network interface. */
netif_set_default(&xnetif[0]);
/* When the netif is fully configured this function must be called.*/
for(idx = 0; idx < NET_IF_NUM; idx++)
netif_set_up(&xnetif[idx]);
lwip_init_done = 1;
}
/**
* @brief LwIP_DHCP_Process_Handle
* @param None
* @retval None
*/
uint8_t LwIP_DHCP(uint8_t idx, uint8_t dhcp_state) {
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
uint32_t IPaddress;
uint8_t iptab[4];
uint8_t DHCP_state;
int mscnt = 0;
struct netif *pnetif = NULL;
DHCP_state = dhcp_state;
#if !CONFIG_ETHERNET
if (idx > 1)
idx = 1;
#endif
pnetif = &xnetif[idx];
if (DHCP_state == 0) {
pnetif->ip_addr.addr = 0;
pnetif->netmask.addr = 0;
pnetif->gw.addr = 0;
}
for (;;) {
//info_printf("\n\r ========DHCP_state:%d============\n\r",DHCP_state);
switch (DHCP_state) {
case DHCP_START: {
#if CONFIG_WLAN
wifi_unreg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl);
#endif
dhcp_start(pnetif);
IPaddress = 0;
DHCP_state = DHCP_WAIT_ADDRESS;
}
break;
case DHCP_WAIT_ADDRESS: {
/* Read the new IP address */
IPaddress = pnetif->ip_addr.addr;
if (IPaddress != 0) {
DHCP_state = DHCP_ADDRESS_ASSIGNED;
#if CONFIG_WLAN
wifi_reg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl, NULL);
#endif
/* Stop DHCP */
// dhcp_stop(pnetif); /* can not stop, need to renew, Robbie*/
iptab[0] = (uint8_t) (IPaddress >> 24);
iptab[1] = (uint8_t) (IPaddress >> 16);
iptab[2] = (uint8_t) (IPaddress >> 8);
iptab[3] = (uint8_t) (IPaddress);
info_printf("Interface %d IP address: %d.%d.%d.%d\n", idx, iptab[3],
iptab[2], iptab[1], iptab[0]);
#if CONFIG_WLAN
error_flag = RTW_NO_ERROR;
#endif
return DHCP_ADDRESS_ASSIGNED;
} else {
/* DHCP timeout */
if (pnetif->dhcp->tries > MAX_DHCP_TRIES) {
DHCP_state = DHCP_TIMEOUT;
/* Stop DHCP */
dhcp_stop(pnetif);
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(pnetif, &ipaddr, &netmask, &gw);
iptab[0] = IP_ADDR3;
iptab[1] = IP_ADDR2;
iptab[2] = IP_ADDR1;
iptab[3] = IP_ADDR0;
info_printf("Interface %d DHCP timeout\n", idx);
info_printf("Static IP address: %d.%d.%d.%d\n", iptab[3], iptab[2], iptab[1], iptab[0]);
#if CONFIG_WLAN
error_flag = RTW_DHCP_FAIL;
#endif
return DHCP_TIMEOUT;
} else {
//sys_msleep(DHCP_FINE_TIMER_MSECS);
vTaskDelay(DHCP_FINE_TIMER_MSECS);
dhcp_fine_tmr();
mscnt += DHCP_FINE_TIMER_MSECS;
if (mscnt >= DHCP_COARSE_TIMER_SECS * 1000) {
dhcp_coarse_tmr();
mscnt = 0;
}
}
}
}
break;
case DHCP_RELEASE_IP:
#if CONFIG_WLAN
wifi_unreg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl);
#endif
info_printf("LwIP_DHCP(%d): Release ip\n", idx);
dhcp_release_unicast(pnetif);
return DHCP_RELEASE_IP;
case DHCP_STOP:
#if CONFIG_WLAN
wifi_unreg_event_handler(WIFI_EVENT_BEACON_AFTER_DHCP, wifi_rx_beacon_hdl);
#endif
info_printf("LwIP_DHCP(%d): dhcp stop.\n", idx);
dhcp_stop(pnetif);
return DHCP_STOP;
default:
break;
}
}
}
uint8_t* LwIP_GetMAC(struct netif *pnetif)
{
return (uint8_t *) (pnetif->hwaddr);
}
uint8_t* LwIP_GetIP(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->ip_addr);
}
uint8_t* LwIP_GetGW(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->gw);
}
uint8_t* LwIP_GetMASK(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->netmask);
}
uint8_t* LwIP_GetBC(struct netif *pnetif)
{
return (uint8_t *) &(pnetif->dhcp->offered_bc_addr);
}
#if LWIP_DNS
void LwIP_GetDNS(struct ip_addr* dns)
{
*dns = dns_getserver(0);
}
void LwIP_SetDNS(struct ip_addr* dns)
{
dns_setserver(0, dns);
}
#endif
void LwIP_UseStaticIP(struct netif *pnetif)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
/* Static address used */
if(pnetif->name[1] == '0'){
#if CONFIG_WLAN
if(wifi_mode == RTW_MODE_STA){
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
}
else if(wifi_mode == RTW_MODE_AP){
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
#endif
}else{
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
}
#if LWIP_AUTOIP
#include <lwip/autoip.h>
void LwIP_AUTOIP(struct netif *pnetif)
{
uint8_t *ip = LwIP_GetIP(pnetif);
autoip_start(pnetif);
while((pnetif->autoip->state == AUTOIP_STATE_PROBING) || (pnetif->autoip->state == AUTOIP_STATE_ANNOUNCING)) {
vTaskDelay(1000);
}
if(*((uint32_t *) ip) == 0) {
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
printf("AUTOIP timeout\n");
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
printf("Static IP address : %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
}
else {
printf("Link-local address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
}
}
#endif
#if LWIP_IPV6
/* Get IPv6 address with lwip 1.5.0 */
void LwIP_AUTOIP_IPv6(struct netif *pnetif)
{
uint8_t *ipv6 = (uint8_t *) &(pnetif->ip6_addr[0].addr[0]);
netif_create_ip6_linklocal_address(pnetif, 1);
printf("IPv6 link-local address: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
ipv6[0], ipv6[1], ipv6[2], ipv6[3], ipv6[4], ipv6[5], ipv6[6], ipv6[7],
ipv6[8], ipv6[9], ipv6[10], ipv6[11], ipv6[12], ipv6[13], ipv6[14], ipv6[15]);
}
#endif

View file

@ -0,0 +1,21 @@
//----------------------------------------------------------------------------//
#ifndef __MAIN_TEST_H
#define __MAIN_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
/* Exported test functions ------------------------------------------------------- */
void do_ping_test(char *ip, int size, int count, int interval);
void do_ping_call(char *ip, int loop, int count);
void interactive_question(char *question, char *choice, char *buf, int buf_size);
void start_interactive_mode(void);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_TEST_H
//----------------------------------------------------------------------------//

View file

@ -0,0 +1,50 @@
/**
******************************************************************************
* @file netconf.h
* @author MCD Application Team
* @version V1.1.0
* @date 07-October-2011
* @brief This file contains all the functions prototypes for the netconf.c
* file.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __NETCONF_H
#define __NETCONF_H
#ifdef __cplusplus
extern "C" {
#endif
// TODO: remove this file
#include "lwip_netconf.h"
#if 0
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void LwIP_Init(void);
void LwIP_DHCP(void);
#endif
#ifdef __cplusplus
}
#endif
#endif /* __NETCONF_H */
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,8 @@
#ifndef __RTL8195A_IT_H_
#define __RTL8195A_IT_H_
int irq_alloc_wlan(void *contex);
#endif //__RTL8195A_IT_H_

View file

@ -0,0 +1,47 @@
#ifndef _UTIL_H
#define _UTIL_H
#include <wireless.h>
#include <wlan_intf.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "wifi_util.h"
#if 0
typedef enum _WIFI_EVENT_INDICATE{
WIFI_EVENT_CONNECT = 0,
WIFI_EVENT_DISCONNECT = 1,
WIFI_EVENT_FOURWAY_HANDSHAKE_DONE = 2,
}WIFI_EVENT_INDICATE;
rtw_event_indicate_t
int wext_get_ssid(const char *ifname, __u8 *ssid);
int wext_set_ssid(const char *ifname, const __u8 *ssid, __u16 ssid_len);
int wext_set_auth_param(const char *ifname, __u16 idx, __u32 value);
int wext_set_key_ext(const char *ifname, __u16 alg, const __u8 *addr, int key_idx, int set_tx, const __u8 *seq, __u16 seq_len, __u8 *key, __u16 key_len);
int wext_get_enc_ext(const char *ifname, __u16 *alg);
int wext_set_passphrase(const char *ifname, const __u8 *passphrase, __u16 passphrase_len);
int wext_get_passphrase(const char *ifname, __u8 *passphrase);
int wext_set_mode(const char *ifname, int mode);
int wext_get_mode(const char *ifname, int *mode);
int wext_set_ap_ssid(const char *ifname, const __u8 *ssid, __u16 ssid_len);
int wext_set_country(const char *ifname, char *country_code);
int wext_get_rssi(const char *ifname, int *rssi);
int wext_set_channel(const char *ifname, __u8 ch);
int wext_get_channel(const char *ifname, __u8 *ch);
int wext_set_scan(const char *ifname, char *buf, __u16 buf_len);
int wext_get_scan(const char *ifname, char *buf, __u16 buf_len);
int wext_mp_command(const char *ifname, char *cmd, int show_msg);
int wext_wifi_priv(const char *ifname, int argc, char **argv);
void wext_wlan_indicate(unsigned int cmd, union iwreq_data *wrqu, char *extra);
#endif
#define wext_handshake_done rltk_wlan_handshake_done
#ifdef __cplusplus
}
#endif
#endif /* _UTIL_H */

View file

@ -0,0 +1,222 @@
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include <lwip/sockets.h>
#if LWIP_SOCKET
#include <lwip/raw.h>
#include <lwip/icmp.h>
#include <lwip/inet_chksum.h>
#include <platform/platform_stdlib.h>
//#define PING_IP "192.168.0.1"
#define PING_IP "192.168.159.1"
#define PING_TO 1000
#define PING_ID 0xABCD
#define BUF_SIZE 10000
#define STACKSIZE 1024
static unsigned short ping_seq = 0;
static int infinite_loop, ping_count, data_size, ping_interval, ping_call;
static char ping_ip[16];
static void generate_ping_echo(unsigned char *buf, int size)
{
int i;
struct icmp_echo_hdr *pecho;
for(i = 0; i < size; i ++) {
buf[sizeof(struct icmp_echo_hdr) + i] = (unsigned char) i;
}
pecho = (struct icmp_echo_hdr *) buf;
ICMPH_TYPE_SET(pecho, ICMP_ECHO);
ICMPH_CODE_SET(pecho, 0);
pecho->chksum = 0;
pecho->id = PING_ID;
pecho->seqno = htons(++ ping_seq);
//Checksum includes icmp header and data. Need to calculate after fill up icmp header
pecho->chksum = inet_chksum(pecho, sizeof(struct icmp_echo_hdr) + size);
}
void ping_test(void *param)
//void ping_test()
{
int i, ping_socket;
int pint_timeout = PING_TO;
struct sockaddr_in to_addr, from_addr;
int from_addr_len = sizeof(struct sockaddr);
int ping_size, reply_size;
unsigned char *ping_buf, *reply_buf;
unsigned int ping_time, reply_time;
struct ip_hdr *iphdr;
struct icmp_echo_hdr *pecho;
if(data_size > BUF_SIZE){
printf("[ERROR] %s: data size error, can't exceed %d\n",__func__,BUF_SIZE);
return;
}
//Ping size = icmp header(8 bytes) + data size
ping_size = sizeof(struct icmp_echo_hdr) + data_size;
ping_buf = pvPortMalloc(ping_size);
if(NULL == ping_buf){
printf("[ERROR] %s: Allocate ping_buf failed\n",__func__);
return;
}
reply_buf = pvPortMalloc(ping_size);
if(NULL == reply_buf){
vPortFree(ping_buf);
printf("[ERROR] %s: Allocate reply_buf failed\n",__func__);
return;
}
printf("[%s] PING %s %d(%d) bytes of data\n", __FUNCTION__, ping_ip, data_size, sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr) + data_size);
for(i = 0; (i < ping_count) || (infinite_loop == 1); i ++) {
ping_socket = socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
#ifdef CONFIG_LWIP_1_5_0
struct timeval timeout;
timeout.tv_sec = pint_timeout / 1000;
timeout.tv_usec = pint_timeout % 1000 * 1000;
setsockopt(ping_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
#else
setsockopt(ping_socket, SOL_SOCKET, SO_RCVTIMEO, &pint_timeout, sizeof(pint_timeout));
#endif
to_addr.sin_len = sizeof(to_addr);
to_addr.sin_family = AF_INET;
to_addr.sin_addr.s_addr = inet_addr(ping_ip);
generate_ping_echo(ping_buf, data_size);
sendto(ping_socket, ping_buf, ping_size, 0, (struct sockaddr *) &to_addr, sizeof(to_addr));
ping_time = xTaskGetTickCount();
if((reply_size = recvfrom(ping_socket, reply_buf, ping_size, 0, (struct sockaddr *) &from_addr, (socklen_t *) &from_addr_len))
>= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) {
reply_time = xTaskGetTickCount();
iphdr = (struct ip_hdr *)reply_buf;
pecho = (struct icmp_echo_hdr *)(reply_buf + (IPH_HL(iphdr) * 4));
if((pecho->id == PING_ID) && (pecho->seqno == htons(ping_seq))) {
printf("[%s] %d bytes from %s: icmp_seq=%d time=%d ms\n", __FUNCTION__, reply_size - sizeof(struct ip_hdr), inet_ntoa(from_addr.sin_addr), htons(pecho->seqno), (reply_time - ping_time) * portTICK_RATE_MS);
}
}
else
printf("[%s] Request timeout for icmp_seq %d\n", __FUNCTION__, ping_seq);
close(ping_socket);
vTaskDelay(ping_interval * configTICK_RATE_HZ);
}
vPortFree(ping_buf);
vPortFree(reply_buf);
if(!ping_call)
vTaskDelete(NULL);
}
void do_ping_call(char *ip, int loop, int count)
{
ping_call = 1;
ping_seq = 0;
data_size = 120;
ping_interval = 1;
infinite_loop = loop;
ping_count = count;
strcpy(ping_ip, ip);
ping_test(NULL);
}
void cmd_ping(int argc, char **argv)
{
int argv_count = 2;
if(argc < 2)
goto Exit;
//ping cmd default value
infinite_loop = 0;
ping_count = 4;
data_size = 32;
ping_interval = 1;
ping_call = 1;
ping_seq = 0;
while(argv_count<=argc){
//first operation
if(argv_count == 2){
memset(ping_ip, 0, sizeof(ping_ip));
strncpy(ping_ip, argv[argv_count-1], (strlen(argv[argv_count-1])>16)?16:strlen(argv[argv_count-1]));
argv_count++;
}
else{
if(strcmp(argv[argv_count-1], "-t") == 0){
infinite_loop = 1;
argv_count++;
}
else if(strcmp(argv[argv_count-1], "-n") == 0){
if(argc < (argv_count+1))
goto Exit;
ping_count = (int) atoi(argv[argv_count]);
argv_count+=2;
}
else if(strcmp(argv[argv_count-1], "-l") == 0){
if(argc < (argv_count+1))
goto Exit;
data_size = (int) atoi(argv[argv_count]);
argv_count+=2;
}
else{
goto Exit;
}
}
}
ping_test(NULL);
return;
Exit:
printf("[ATWI] Usage: ATWI=[host],[options]\n");
printf("\t-t\tPing the specified host until stopped\n");
printf("\t-n\t# Number of echo requests to send (default 4 times)\n");
printf("\t-l\t# Send buffer size (default 32 bytes)\n");
printf("\tExample:\n");
printf("\t\tATWI=192.168.1.2,-n,100,-l,5000\n");
return;
}
void do_ping_test(char *ip, int size, int count, int interval)
{
if((sizeof(struct icmp_echo_hdr) + size) > BUF_SIZE) {
printf("%s BUF_SIZE(%d) is too small\n", __FUNCTION__, BUF_SIZE);
return;
}
if(ip == NULL)
strcpy(ping_ip, PING_IP);
else
strcpy(ping_ip, ip);
ping_call = 0;
ping_seq = 0;
data_size = size;
ping_interval = interval;
if(count == 0) {
infinite_loop = 1;
ping_count = 0;
}
else {
infinite_loop = 0;
ping_count = count;
}
if(xTaskCreate(ping_test, ((const signed char*)"ping_test"), STACKSIZE, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("%s xTaskCreate failed\n", __FUNCTION__);
}
#endif // LWIP_SOCKET

View file

@ -0,0 +1,110 @@
#include "rtl8195a.h"
#include <main.h>
#include "rtl8195a_it.h"
/* os dependent*/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include <diag.h>
#define printf DiagPrintf
/*-----------------------------Global Variable ---------------------*/
//#ifdef CONFIG_WLAN
//#ifdef CONFIG_ISR_THREAD_MODE_INTERRUPT
extern xSemaphoreHandle *pExportWlanIrqSemaphore;
//#endif
//#endif
#ifdef CONFIG_WLAN
#ifdef CONFIG_ISR_THREAD_MODE_INTERRUPT
//TODO: chris
#define IRQ_HANDLED 1
#define IRQ_NONE 0
int wlan_Interrupt (
IN VOID* Data
)
{
#if 1
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
printf("wlan interrupt\n");
/* This semaphore is initialized once wlan interrupt handler thread is created and initialized*/
if(!pExportWlanIrqSemaphore)
{
printf("%s(%d)\n", __FUNCTION__, __LINE__);
goto exit;
}
printf("%s(%d)\n", __FUNCTION__, __LINE__);
xSemaphoreGiveFromISR( *pExportWlanIrqSemaphore, &xHigherPriorityTaskWoken );
printf("%s(%d)\n", __FUNCTION__, __LINE__);
/* Switch tasks if necessary. */
if( xHigherPriorityTaskWoken != pdFALSE )
{
printf("%s(%d)\n", __FUNCTION__, __LINE__);
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
}
exit:
return IRQ_HANDLED;
#else
struct dvobj_priv *dvobj = (struct dvobj_priv *)Data;
_adapter *adapter = dvobj->if1;
DBG_8192C("Dma isr\n");
if (dvobj->irq_enabled == 0) {
return IRQ_HANDLED;
}
DBG_871X("%s(%d)\n", __FUNCTION__, __LINE__);
if(rtw_hal_interrupt_handler(adapter) == _FAIL)
return IRQ_HANDLED;
//return IRQ_NONE;
DBG_871X("%s(%d)\n", __FUNCTION__, __LINE__);
return IRQ_HANDLED;
#endif
}
VOID
lextra_bus_dma_Interrupt (
IN VOID* Data
);
/*
* This function register interrupt handler and is called by wlan driver
* Return 0 if success, Others if fail
*/
int irq_alloc_wlan(void *contex)
{
int ret = 0;
IRQ_HANDLE IrqHandle = {0};
printf("Register Interrupt\n");
IrqHandle.Data = (u32) (contex);
IrqHandle.IrqNum = WL_DMA_IRQ;
IrqHandle.IrqFun = (IRQ_FUN)wlan_Interrupt;
//IrqHandle.IrqFun = (IRQ_FUN)lextra_bus_dma_Interrupt;
IrqHandle.Priority = 0;
InterruptRegister(&IrqHandle);
InterruptEn(&IrqHandle);
return ret;
}
#endif
#endif

View file

@ -0,0 +1,95 @@
/*
* Hello World
*
* Copyright (c) 2013 Realtek Semiconductor Corp.
*
* This module is a confidential and proprietary property of RealTek and
* possession or use of this module requires written permission of RealTek.
*/
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "main.h"
#include "main_test.h"
#if CONFIG_WLAN
#include "wifi_conf.h"
#include "wlan_intf.h"
#include "wifi_constants.h"
#endif
#include "lwip_netconf.h"
#include <platform/platform_stdlib.h>
//#include "wifi_interactive_ext.h"
#ifndef CONFIG_INIT_NET
#define CONFIG_INIT_NET 1
#endif
#ifndef CONFIG_INTERACTIVE_MODE
#define CONFIG_INTERACTIVE_MODE 1
#endif
#define STACKSIZE (512 + 768)
xSemaphoreHandle uart_rx_interrupt_sema = NULL;
void init_thread(void *param)
{
#if CONFIG_INIT_NET
#if CONFIG_LWIP_LAYER
/* Initilaize the LwIP stack */
// DBG_8195A("\nLwIP Init\n");
LwIP_Init();
#endif
#endif
#if CONFIG_WIFI_IND_USE_THREAD
wifi_manager_init();
#endif
#if CONFIG_WLAN
// DBG_8195A("\nWiFi_on(RTW_MODE_STA)\n");
wifi_on(RTW_MODE_STA);
#if CONFIG_AUTO_RECONNECT
//setup reconnection flag
// u8 mode;
// if(wifi_get_autoreconnect(&mode) > 0 && mode != 1)
wifi_set_autoreconnect(1);
#endif
// printf("\n\r%s(%d), Available heap %d\n\r", __FUNCTION__, __LINE__, xPortGetFreeHeapSize());
#endif
#if CONFIG_INTERACTIVE_MODE
/* Initial uart rx swmaphore*/
vSemaphoreCreateBinary(uart_rx_interrupt_sema);
xSemaphoreTake(uart_rx_interrupt_sema, 1/portTICK_RATE_MS);
start_interactive_mode();
#endif
/* Kill init thread after all init tasks done */
vTaskDelete(NULL);
}
void wlan_network()
{
#if 0
{
void *stack_addr = tcm_heap_malloc(STACKSIZE*sizeof(int));
if(stack_addr == NULL){
printf("%s: Out of TCM heap!\n", __FUNCTION__);
}
if (xTaskGenericCreate(
init_thread,
(const char *)"init",
STACKSIZE,
NULL,
tskIDLE_PRIORITY + 3 + PRIORITIE_OFFSET,
NULL,
stack_addr,
NULL) != pdTRUE)
printf("%s: xTaskCreate(init_thread) failed\n", __FUNCTION__);
}
#else
if(xTaskCreate(init_thread, ((const char*)"init"), STACKSIZE, NULL, tskIDLE_PRIORITY + 3 + PRIORITIE_OFFSET, NULL) != pdPASS) // +3
printf("%s: xTaskCreate(init_thread) failed\n", __FUNCTION__);
#endif
}

View file

@ -0,0 +1,262 @@
#ifndef __LIST_H
#define __LIST_H
#if defined ( __CC_ARM )
#ifndef inline
#define inline __inline
#endif
#endif
/* This file is from Linux Kernel (include/linux/list.h)
* and modified by simply removing hardware prefetching of list items.
* Here by copyright, credits attributed to wherever they belong.
* Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
*/
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); \
pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop counter.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member, type) \
for (pos = list_entry((head)->next, type, member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, type, member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop counter.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member, type) \
for (pos = list_entry((head)->next, type, member), \
n = list_entry(pos->member.next, type, member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, type, member))
#endif

View file

@ -0,0 +1,154 @@
#ifndef _INCLUDE_ESP_COMP_H_
#define _INCLUDE_ESP_COMP_H_
#include "platform_autoconf.h"
#include "flash_api.h"
#define ICACHE_FLASH_ATTR
#define ICACHE_RODATA_ATTR
#define DATA_IRAM_ATTR
#define ICACHE_RAM_ATTR
#define os_printf(...) rtl_printf(__VA_ARGS__)
#define os_printf_plus(...) rtl_printf(__VA_ARGS__)
#define os_sprintf_fd(...) rtl_sprintf(__VA_ARGS__)
#define ets_sprintf(...) rtl_sprintf(__VA_ARGS__)
#ifndef os_malloc
#define os_malloc pvPortMalloc
#define os_zalloc pvPortZalloc
#define os_calloc pvPortCalloc
#define os_realloc pvPortRealloc
#endif
#undef os_free
#define os_free vPortFree
#define system_get_free_heap_size xPortGetFreeHeapSize
#undef os_realloc
#define os_realloc pvPortReAlloc
#define os_bzero rtl_bzero
#define os_delay_us wait_us // HalDelayUs
//#define os_install_putc1 rtl_install_putc1
//#define os_install_putc2 rtl_install_putc2
//#define os_intr_lock rtl_intr_lock
//#define os_intr_unlock rtl_intr_unlock
//#define os_isr_attach rtl_isr_attach
//#define os_isr_mask rtl_isr_mask
//#define os_isr_unmask rtl_isr_unmask
#define os_memcmp rtl_memcmp
#define os_memcpy rtl_memcpy
#define ets_memcpy rtl_memcpy
#define os_memmove rtl_memmove
#define os_memset rtl_memset
#define os_putc rtl_putc
//#define os_str2macaddr rtl_str2macaddr
//#define os_strcat strcat
#define os_strchr rtl_strchr
#define os_strrchr rtl_strrchr
#define os_strcmp rtl_strcmp
#define os_strcpy rtl_strcpy
#define os_strlen rtl_strlen
#define os_strncmp rtl_strncmp
#define os_strncpy rtl_strncpy
#define os_strstr rtl_strstr
#define os_random Rand
//extern uint32 phy_get_rand(void);
#define system_get_os_print() 1
#ifdef USE_US_TIMER
#define os_timer_arm_us(a, b, c) rtl_timer_arm_new(a, b, c, 0)
#endif
//#define os_timer_arm(a, b, c) rtl_timer_arm_new(a, b, c, 1)
//#define os_timer_disarm rtl_timer_disarm
//#define os_timer_init rtl_timer_init
//#define os_timer_setfn rtl_timer_setfn
//#define os_timer_done rtl_timer_done
//#define os_timer_handler_isr rtl_timer_handler_isr
//#define os_update_cpu_frequency rtl_update_cpu_frequency
//#define os_sprintf ets_sprintf
/*
typedef struct{
uint32_t deviceId; //+00
uint32_t chip_size; //+04 chip size in byte
uint32_t block_size; //+08
uint32_t sector_size; //+0c
uint32_t page_size; //+10
uint32_t status_mask; //+14
} SpiFlashChip;
typedef enum {
SPI_FLASH_RESULT_OK,
SPI_FLASH_RESULT_ERR,
SPI_FLASH_RESULT_TIMEOUT
} SpiFlashOpResult;
extern SpiFlashChip * flashchip; // in RAM-BIOS: 0x3fffc714
*/
// include "flash_api.h"
#define spi_flash_real_size() flash_get_size(&flashobj)
#define Cache_Read_Disable() SpicDisableRtl8195A()
#define Cache_Read_Enable(a, b, c) flash_turnon()
#define spi_flash_get_id() ((flashobj.SpicInitPara.id[0] << 16) | (flashobj.SpicInitPara.id[1]<<8) | flashobj.SpicInitPara.id[2])
//SpiFlashOpResult spi_flash_read_status(uint32_t * sta);
//SpiFlashOpResult spi_flash_write_status(uint32_t sta);
#define spi_flash_erase_sector(sec) flash_erase_sector(&flashobj, (sec)<<12)
#define spi_flash_write(faddr, pbuf, size) flash_stream_write(&flashobj, faddr, size, (uint8_t *)pbuf)
#define spi_flash_read(faddr, pbuf, size) flash_stream_read(&flashobj, faddr, size, (uint8_t *)pbuf)
#define spi_flash_erase_block(blk) flash_erase_block(&flashobj, (blk)<<16);
#define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
#define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
#define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
#define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
/* These are cast to u16_t, with the intent that they are often arguments
* to printf using the U16_F format from cc.h. */
#define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
#define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
#define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
#define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
#define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \
ip4_addr2_16(ipaddr), \
ip4_addr3_16(ipaddr), \
ip4_addr4_16(ipaddr)
#define IPSTR "%d.%d.%d.%d"
#ifndef MAC2STR
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
#endif
#ifndef DEBUGSOO
/* CONFIG_DEBUG_LOG:
=0 Off all diag/debug msg,
=1 Only errors,
=2 errors + warning, (default)
=3 errors + warning + info,
=4 errors + warning + info + debug,
=5 full */
#if CONFIG_DEBUG_LOG > 3
#define DEBUGSOO (CONFIG_DEBUG_LOG - 1)
#elif CONFIG_DEBUG_LOG > 1
#define DEBUGSOO 2
#else
#define DEBUGSOO CONFIG_DEBUG_LOG
#endif
#endif //#ifndef DEBUGSOO
//#define system_get_sdk_version() "3.5.3"
//#define system_get_time xTaskGetTickCount
//#define ets_get_cpu_frequency HalGetCpuClk
#endif // _INCLUDE_ESP_COMP_H_

View file

@ -0,0 +1,280 @@
/******************************************************************************
* Copyright (c) 2013-2016 Realtek Semiconductor Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
#ifndef __PLATFORM_STDLIB_H__
#define __PLATFORM_STDLIB_H__
#define USE_CLIB_PATCH 0
#if defined (__GNUC__)
/* build rom should set USE_RTL_ROM_CLIB=0 */
#ifndef CONFIG_MBED_ENABLED
#include <rt_lib_rom.h>
#endif
#endif
#ifdef CONFIG_BUILD_ROM
#define USE_RTL_ROM_CLIB 0
#else
#define BUFFERED_PRINTF 0
#ifndef CONFIG_MBED_ENABLED
#define USE_RTL_ROM_CLIB 1
#else
#define USE_RTL_ROM_CLIB 0
#endif
#endif
#if defined(CONFIG_PLATFORM_8195A)
#if defined (__IARSTDLIB__)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "diag.h"
#define strsep(str, delim) _strsep(str, delim)
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "diag.h"
#include "strproc.h"
#include "basic_types.h"
#include "hal_misc.h"
#if USE_RTL_ROM_CLIB
#include "rtl_lib.h"
#endif
#undef printf
#undef sprintf
#undef snprintf
#undef atoi
#undef memcmp
#undef memcpy
#undef memset
#undef strcmp
#undef strcpy
#undef strlen
#undef strncmp
#undef strncpy
#undef strsep
#undef strtok
#if USE_RTL_ROM_CLIB
#undef memchr
#undef memmove
#undef strcat
#undef strchr
#undef strncat
#undef strstr
#endif
#if USE_RTL_ROM_CLIB
#if BUFFERED_PRINTF
extern int buffered_printf(const char* fmt, ...);
#define printf buffered_printf
#else
#define printf rtl_printf
#endif
#define sprintf rtl_sprintf
#define snprintf rtl_snprintf
#define memchr rtl_memchr
#define memcmp rtl_memcmp
#define memcpy rtl_memcpy
#define memmove rtl_memmove
#define memset rtl_memset
#define strcat rtl_strcat
#define strchr rtl_strchr
#define strcmp rtl_strcmp
#define strcpy rtl_strcpy
#define strlen rtl_strlen
#define strncat rtl_strncat
#define strncmp rtl_strncmp
#define strncpy rtl_strncpy
#define strstr rtl_strstr
#define strsep rtl_strsep
#define strtok rtl_strtok
#else
#if USE_CLIB_PATCH
extern int DiagSscanfPatch(const char *buf, const char *fmt, ...);
extern char* DiagStrtokPatch(char *str, const char* delim);
extern char* DiagStrstrPatch(char *string, char *substring);
extern int DiagSnPrintfPatch(char *buf, size_t size, const char *fmt, ...);
extern u32 DiagPrintfPatch(const char *fmt, ...);
extern u32 DiagSPrintfPatch(u8 *buf, const char *fmt, ...);
#define printf DiagPrintfPatch
#define sprintf DiagSPrintfPatch
#define snprintf DiagSnPrintfPatch
#define strstr(a, b) DiagStrstrPatch((char *)(a), (char *)(b))
#define strtok DiagStrtokPatch
#else
#define printf DiagPrintf
#define sprintf(fmt, arg...) DiagSPrintf((u8*)fmt, ##arg)
#if defined (__GNUC__)
#define snprintf DiagSnPrintf // NULL function
#define strstr(str1, str2) prvStrStr(str1, str2) // NULL function
#endif
#define strtok(str, delim) _strsep(str, delim)
#endif
#define memcmp(dst, src, sz) _memcmp(dst, src, sz)
#define memcpy(dst, src, sz) _memcpy(dst, src, sz)
#define memset(dst, val, sz) _memset(dst, val, sz)
#define strchr(s, c) _strchr(s, c) // for B-cut ROM
#define strcmp(str1, str2) prvStrCmp((const unsigned char *) str1, (const unsigned char *) str2)
#define strcpy(dest, src) _strcpy(dest, src)
#define strlen(str) prvStrLen((const unsigned char *) str)
#define strncmp(str1, str2, cnt) _strncmp(str1, str2, cnt)
#define strncpy(dest, src, count) _strncpy(dest, src, count)
#define strsep(str, delim) _strsep(str, delim)
#endif
#define atoi(str) prvAtoi(str)
#define strpbrk(cs, ct) _strpbrk(cs, ct) // for B-cut ROM
#if USE_CLIB_PATCH
#undef sscanf
#define sscanf DiagSscanfPatch
#else
#if defined (__GNUC__)
#undef sscanf //_sscanf
//extern int DiagSscanfPatch(const char *buf, const char *fmt, ...);
//#define sscanf DiagSscanfPatch
#define sscanf sscanf // use libc sscanf
#endif
#endif
#endif // defined (__IARSTDLIB__)
//
// memory management
//
#ifndef CONFIG_MBED_ENABLED
extern void *pvPortMalloc( size_t xWantedSize );
extern void vPortFree( void *pv );
#define malloc pvPortMalloc
#define zalloc pvPortZalloc
#define free vPortFree
#endif
#elif defined (CONFIG_PLATFORM_8711B)
#if defined (__IARSTDLIB__)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h> /* va_list */
#include "diag.h"
#define strsep(str, delim) _strsep(str, delim)
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> /* va_list */
#include "diag.h"
#include "strproc.h"
#include "memproc.h"
#include "basic_types.h"
#if USE_RTL_ROM_CLIB
#include "rtl_lib.h"
#include "rom_libc_string.h"
#endif
#undef printf
#undef sprintf
#undef snprintf
#undef memchr
#undef memcmp
#undef memcpy
#undef memset
#undef memmove
#undef strcmp
#undef strcpy
#undef strlen
#undef strncmp
#undef strncpy
#undef strsep
#undef strtok
#undef strcat
#undef strchr
#undef strncat
#undef strstr
#undef atol
#undef atoi
#undef strpbrk
#if USE_RTL_ROM_CLIB
#if BUFFERED_PRINTF
extern int buffered_printf(const char* fmt, ...);
#define printf buffered_printf
#else
#define printf rtl_printf
#endif
#define sprintf rtl_sprintf
#define snprintf rtl_snprintf
#define vsnprintf rtl_vsnprintf
#else
#define printf DiagPrintf
#define sprintf(fmt, arg...) DiagSPrintf((u8*)fmt, ##arg)
#define snprintf DiagSnPrintf // NULL function
#define vsnprintf(buf, size, fmt, ap) VSprintf(buf, fmt, ap)
#endif
#define memchr __rtl_memchr_v1_00
#define memcmp(dst, src, sz) _memcmp(dst, src, sz)
#define memcpy(dst, src, sz) _memcpy(dst, src, sz)
#define memmove __rtl_memmove_v1_00
#define memset(dst, val, sz) _memset(dst, val, sz)
#define strchr(s, c) _strchr(s, c) // for B-cut ROM
#define strcmp(str1, str2) prvStrCmp((const unsigned char *) str1, (const unsigned char *) str2)
#define strcpy(dest, src) _strcpy(dest, src)
#define strlen(str) prvStrLen((const unsigned char *) str)
#define strsep(str, delim) _strsep(str, delim)
#define strstr(str1, str2) prvStrStr(str1, str2) // NULL function
#define strtok(str, delim) prvStrtok(str, delim)//_strsep(str, delim)
#define strcat __rtl_strcat_v1_00
#define strncmp(str1, str2, cnt) _strncmp(str1, str2, cnt)
#define strncpy(dest, src, count) _strncpy(dest, src, count)
#define strncat __rtl_strncat_v1_00
#define atol(str) strtol(str,NULL,10)
#define atoi(str) prvAtoi(str)
#define strpbrk(cs, ct) _strpbrk(cs, ct) // for B-cut ROM
#if defined (__GNUC__)
#undef sscanf
#define sscanf _sscanf_patch
#define rand Rand
#endif
//extern int _sscanf_patch(const char *buf, const char *fmt, ...);
//#define sscanf _sscanf_patch
#endif // defined (__IARSTDLIB__)
//
// memory management
//
extern void *pvPortMalloc( size_t xWantedSize );
extern void vPortFree( void *pv );
#define malloc pvPortMalloc
#define free vPortFree
#elif defined(USE_STM322xG_EVAL) || defined(USE_STM324xG_EVAL) || defined(STM32F10X_XL)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#endif
#endif //__PLATFORM_STDLIB_H__

View file

@ -0,0 +1,915 @@
/*
this is the c lib patch, It can help when the clib provided by IAR
does not work well.
How to use this:
1.You must include platform_stdlib.h in you source file
2.There is a macro USE_CLIB_PATCH in platform_stdlib.h should be opened.
If there is some problems using this patch,
You'd better check if you code runs into these functions:
DiagSscanfPatch
DiagStrtokPatch
DiagStrstrPatch
DiagSnPrintfPatch
DiagPrintfPatch
DiagSPrintfPatch
DiagPrintfPatch
DiagSPrintfPatch
DiagSnPrintfPatch
DiagStrstrPatch
DiagStrtokPatch
*/
#ifndef CONFIG_PLATFORM_8711B
#include <stdarg.h>
#define DiagPutChar HalSerialPutcRtl8195a
#define IN
#define NULL 0
typedef unsigned int size_t;
typedef unsigned int SIZE_T;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef unsigned short int u16;
typedef unsigned char u8;
typedef signed long long s64;
typedef signed int s32;
typedef signed short int s16;
typedef unsigned char bool;
extern u8 txt0123456789ABCDEF[16];
#define tab0123456789ABCDEF txt0123456789ABCDEF // = "0123456789ABCDEF"
#define in_range(c, lo, up) ((u8)c >= lo && (u8)c <= up)
#define isprint(c) in_range(c, 0x20, 0x7f)
#define isdigit(c) in_range(c, '0', '9')
#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
#define islower(c) in_range(c, 'a', 'z')
#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == ',')
#define ULLONG_MAX (~0ULL)
#define USHRT_MAX ((u16)(~0U))
#define KSTRTOX_OVERFLOW (1U << 31)
#define SHRT_MAX ((s16)(USHRT_MAX>>1))
static inline char _tolower(const char c)
{
return c | 0x20;
}
extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
extern s64 div_s64(s64 dividend, s32 divisor);
extern inline char _tolower(const char c);
extern u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder);
extern u64 div_u64(u64 dividend, u32 divisor);
extern unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p);
extern const char *_parse_integer_fixup_radix(const char *s, unsigned int *base);
extern char *skip_spaces(const char *str);
extern int skip_atoi(const char **s);
extern void HalSerialPutcRtl8195a(u8 c);
static unsigned long long simple_strtoull_patch(const char *cp, char **endp, unsigned int base)
{
unsigned long long result;
unsigned int rv;
cp = _parse_integer_fixup_radix(cp, &base);
rv = _parse_integer(cp, base, &result);
return result;
}
static long long simple_strtoll_patch(const char *cp, char **endp, unsigned int base)
{
if(*cp == '-')
return -simple_strtoull_patch(cp + 1, endp, base);
return simple_strtoull_patch(cp, endp, base);
}
static unsigned long simple_strtoul_patch(const char *cp, char **endp, unsigned int base)
{
return simple_strtoull_patch(cp, endp, base);
}
static long simple_strtol_patch(const char *cp, char **endp, unsigned int base)
{
if(*cp == '-')
return -simple_strtoul_patch(cp + 1, endp, base);
return simple_strtoul_patch(cp, endp, base);
}
static int judge_digit_width(const char *str)
{
int width = 0;
while(isdigit(*str)) {
width++;
str++;
}
return width;
}
static int _vsscanf_patch(const char *buf, const char *fmt, va_list args)
{
const char *str = buf;
char *next;
char digit;
int num = 0;
int i =0;
u8 qualifier;
unsigned int base;
union {
long long s;
unsigned long long u;
} val;
s16 field_width;
bool is_sign;
char str_store[20] = {0};
while(*fmt) {
/* skip any white space in format */
/* white space in format matchs any amount of
* white space, including none, in the input.
*/
if(isspace(*fmt)) {
fmt = skip_spaces(++fmt);
str = skip_spaces(str);
}
/* anything that is not a conversion must match exactly */
if(*fmt != '%' && *fmt) {
if(*fmt++ != *str++) {
break;
}
continue;
}
if(!*fmt) {
break;
}
++fmt;
/* skip this conversion.
* advance both strings to next white space
*/
if(*fmt == '*') {
if(!*str) {
break;
}
while(!isspace(*fmt) && *fmt != '%' && *fmt)
fmt++;
while(!isspace(*str) && *str)
str++;
continue;
}
/* get field width */
field_width = -1;
if(isdigit(*fmt)) {
field_width = skip_atoi(&fmt);
if(field_width <= 0) {
break;
}
}
/* get conversion qualifier */
qualifier = -1;
if(*fmt == 'h' || _tolower(*fmt) == 'l' ||
_tolower(*fmt) == 'z') {
qualifier = *fmt++;
if(qualifier == *fmt) {
if(qualifier == 'h') {
qualifier = 'H';
fmt++;
} else if(qualifier == 'l') {
qualifier = 'L';
fmt++;
}
}
}
if(!*fmt) {
break;
}
if(*fmt == 'n') {
/* return number of characters read so far */
*va_arg(args, int *) = str - buf;
++fmt;
continue;
}
if(!*str) {
break;
}
base = 10;
is_sign = 0;
switch(*fmt++) {
case 'c': {
char *s = (char *)va_arg(args, char*);
if(field_width == -1)
field_width = 1;
do {
*s++ = *str++;
} while(--field_width > 0 && *str);
num++;
}
continue;
case 's': {
char *s = (char *)va_arg(args, char *);
if(field_width == -1)
field_width = SHRT_MAX;
/* first, skip leading white space in buffer */
str = skip_spaces(str);
/* now copy until next white space */
while(*str && !isspace(*str) && field_width--) {
*s++ = *str++;
}
*s = '\0';
num++;
}
continue;
case 'o':
base = 8;
break;
case 'x':
case 'X':
base = 16;
break;
case 'i':
base = 0;
case 'd':
is_sign = 1;
case 'u':
break;
case '%':
/* looking for '%' in str */
if(*str++ != '%') {
return num;
}
continue;
default:
/* invalid format; stop here */
return num;
}
/* have some sort of integer conversion.
* first, skip white space in buffer.
*/
str = skip_spaces(str);
digit = *str;
if(is_sign && digit == '-')
digit = *(str + 1);
if(!digit
|| (base == 16 && !isxdigit(digit))
|| (base == 10 && !isdigit(digit))
|| (base == 8 && (!isdigit(digit) || digit > '7'))
|| (base == 0 && !isdigit(digit))) {
break;
}
//here problem *******************************************
//troy add ,fix support %2d, but not support %d
if(field_width <= 0) {
field_width = judge_digit_width(str);
}
/////troy add, fix str passed inwidth wrong
for(i = 0; i<field_width ; i++)
str_store[i] = str[i];
next = (char*)str + field_width;
if(is_sign) {
val.s = qualifier != 'L' ?
simple_strtol_patch(str_store, &next, base) :
simple_strtoll_patch(str_store, &next, base);
} else {
val.u = qualifier != 'L' ?
simple_strtoul_patch(str_store, &next, base) :
simple_strtoull_patch(str_store, &next, base);
}
////troy add
for(i = 0; i<20 ; i++)
str_store[i] = 0;
//判断转换的字符串的宽度是否大于 %2d
if(field_width > 0 && next - str > field_width) {
if(base == 0)
_parse_integer_fixup_radix(str, &base);
while(next - str > field_width) {
if(is_sign) {
val.s = div_s64(val.s, base);
} else {
val.u = div_u64(val.u, base);
}
--next;
}
}
switch(qualifier) {
case 'H': /* that's 'hh' in format */
if(is_sign)
*va_arg(args, signed char *) = val.s;
else
*va_arg(args, unsigned char *) = val.u;
break;
case 'h':
if(is_sign)
*va_arg(args, short *) = val.s;
else
*va_arg(args, unsigned short *) = val.u;
break;
case 'l':
if(is_sign)
*va_arg(args, long *) = val.s;
else
*va_arg(args, unsigned long *) = val.u;
break;
case 'L':
if(is_sign)
*va_arg(args, long long *) = val.s;
else
*va_arg(args, unsigned long long *) = val.u;
break;
case 'Z':
case 'z':
*va_arg(args, size_t *) = val.u;
break;
default:
if(is_sign)
*va_arg(args, int *) = val.s;
else
*va_arg(args, unsigned int *) = val.u;
break;
}
num++;
if(!next) {
break;
}
str = next;
}
return num;
}
int DiagSscanfPatch(const char *buf, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i = _vsscanf_patch(buf, fmt, args);
va_end(args);
return i;
}
/*********************************************************/
char* DiagStrtokPatch(char *str, const char* delim) {
static char* _buffer;
if(str != NULL) _buffer = str;
if(_buffer[0] == '\0') return NULL;
char *ret = _buffer, *b;
const char *d;
for(b = _buffer; *b !='\0'; b++) {
for(d = delim; *d != '\0'; d++) {
if(*b == *d) {
*b = '\0';
_buffer = b+1;
// skip the beginning delimiters
if(b == ret) {
ret++;
continue;
}
return ret;
}
}
}
return ret;
}
/*********************************************************/
char *DiagStrstrPatch(char *string, char *substring)
{
register char *a, *b;
/* First scan quickly through the two strings looking for a
* single-character match. When it's found, then compare the
* rest of the substring.
*/
b = substring;
if(*b == 0) {
return string;
}
for(; *string != 0; string += 1) {
if(*string != *b) {
continue;
}
a = string;
while(1) {
if(*b == 0) {
return string;
}
if(*a++ != *b++) {
break;
}
}
b = substring;
}
return (char *) 0;
}
/*********************************************************/
int DiagSnPrintfPatch(char *buf, size_t size, const char *fmt, ...)
{
va_list ap;
char *p, *s, *buf_end = NULL;
const int *dp = ((const int *)&fmt)+1;
if(buf == NULL)
return 0;
va_start(ap, fmt);
s = buf;
buf_end = size? (buf + size):(char*)~0;
for(; *fmt != '\0'; ++fmt) {
if(*fmt != '%') {
*s++ = *fmt;
if(s >= buf_end) {
goto Exit;
}
continue;
}
if(*++fmt == 's') {
for(p = (char *)*dp++; *p != '\0'; p++) {
*s++ = *p;
if(s >= buf_end) {
goto Exit;
}
}
}
else { /* Length of item is bounded */
char tmp[20], *q = tmp;
int alt = 0;
int shift = 0;// = 12;
const long *lpforchk = (const long *)dp;
if((*lpforchk) < 0x10) {
shift = 0;
}
else if(((*lpforchk) >= 0x10) && ((*lpforchk) < 0x100)) {
shift = 4;
}
else if(((*lpforchk) >= 0x100) && ((*lpforchk) < 0x1000)) {
shift = 8;
}
else if(((*lpforchk) >= 0x1000) && ((*lpforchk) < 0x10000)) {
shift = 12;
}
else if(((*lpforchk) >= 0x10000) && ((*lpforchk) < 0x100000)) {
shift = 16;
}
else if(((*lpforchk) >= 0x100000) && ((*lpforchk) < 0x1000000)) {
shift = 20;
}
else if(((*lpforchk) >= 0x1000000) && ((*lpforchk) < 0x10000000)) {
shift = 24;
}
else if((*lpforchk) >= 0x10000000) {
shift = 28;
}
else {
shift = 28;
}
if((*fmt >= '0') && (*fmt <= '9'))
{
int width;
unsigned char fch = *fmt;
for(width=0; (fch>='0') && (fch<='9'); fch=*++fmt)
{ width = width * 10 + fch - '0';
}
shift=(width-1)*4;
}
/*
* Before each format q points to tmp buffer
* After each format q points past end of item
*/
if((*fmt == 'x')||(*fmt == 'X') || (*fmt == 'p') || (*fmt == 'P')) {
/* With x86 gcc, sizeof(long) == sizeof(int) */
const long *lp = (const long *)dp;
long h = *lp++;
int hex_count = 0;
unsigned long h_back = h;
int ncase = (*fmt & 0x20);
dp = (const int *)lp;
if((*fmt == 'p') || (*fmt == 'P'))
alt=1;
if(alt) {
*q++ = '0';
*q++ = 'X' | ncase;
}
while(h_back) {
hex_count += (h_back & 0xF) ? 1 : 0;
h_back = h_back >> 4;
}
if(shift < (hex_count - 1)*4)
shift = (hex_count - 1)*4;
for(; shift >= 0; shift -= 4)
*q++ = tab0123456789ABCDEF[(h >> shift) & 0xF] | ncase;
}
else if(*fmt == 'd') {
int i = *dp++;
char *r;
int digit_space = 0;
if(i < 0) {
*q++ = '-';
i = -i;
digit_space++;
}
p = q; /* save beginning of digits */
do {
*q++ = '0' + (i % 10);
i /= 10;
digit_space++;
} while(i);
for(; shift >= 0; shift -= 4) {
if(digit_space-- > 0) {
; //do nothing
} else {
*q++ = '0';
}
}
/* reverse digits, stop in middle */
r = q; /* don't alter q */
while(--r > p) {
i = *r;
*r = *p;
*p++ = i;
}
}
else if(*fmt == 'c')
*q++ = *dp++;
else
*q++ = *fmt;
/* now output the saved string */
for(p = tmp; p < q; ++p) {
*s++ = *p;
if(s >= buf_end) {
goto Exit;
}
}
}
}
Exit:
if(buf)
*s = '\0';
va_end(ap);
return(s-buf);
}
/*********************************************************/
static int VSprintfPatch(char *buf, const char *fmt, const int *dp)
{
char *p, *s;
s = buf;
for(; *fmt != '\0'; ++fmt) {
if(*fmt != '%') {
if(buf) {
*s++ = *fmt;
} else {
DiagPutChar(*fmt);
}
continue;
}
if(*++fmt == 's') {
for(p = (char *)*dp++; *p != '\0'; p++) {
if(buf) {
*s++ = *p;
} else {
DiagPutChar(*p);
}
}
}
else { /* Length of item is bounded */
char tmp[20], *q = tmp;
int alt = 0;
int shift = 0;// = 12;
const long *lpforchk = (const long *)dp;
if((*lpforchk) < 0x10) {
shift = 0;
}
else if(((*lpforchk) >= 0x10) && ((*lpforchk) < 0x100)) {
shift = 4;
}
else if(((*lpforchk) >= 0x100) && ((*lpforchk) < 0x1000)) {
shift = 8;
}
else if(((*lpforchk) >= 0x1000) && ((*lpforchk) < 0x10000)) {
shift = 12;
}
else if(((*lpforchk) >= 0x10000) && ((*lpforchk) < 0x100000)) {
shift = 16;
}
else if(((*lpforchk) >= 0x100000) && ((*lpforchk) < 0x1000000)) {
shift = 20;
}
else if(((*lpforchk) >= 0x1000000) && ((*lpforchk) < 0x10000000)) {
shift = 24;
}
else if((*lpforchk) >= 0x10000000) {
shift = 28;
}
else {
shift = 28;
}
#if 1 //wei patch for %02x
if((*fmt >= '0') && (*fmt <= '9'))
{
int width;
unsigned char fch = *fmt;
for(width=0; (fch>='0') && (fch<='9'); fch=*++fmt)
{ width = width * 10 + fch - '0';
}
shift=(width-1)*4;
}
#endif
/*
* Before each format q points to tmp buffer
* After each format q points past end of item
*/
if((*fmt == 'x')||(*fmt == 'X') || (*fmt == 'p') || (*fmt == 'P')) {
/* With x86 gcc, sizeof(long) == sizeof(int) */
const long *lp = (const long *)dp;
long h = *lp++;
int hex_count = 0;
unsigned long h_back = h;
int ncase = (*fmt & 0x20);
dp = (const int *)lp;
if((*fmt == 'p') || (*fmt == 'P'))
alt=1;
if(alt) {
*q++ = '0';
*q++ = 'X' | ncase;
}
//hback 是实际得到的数据hex_count是统计数据的HEX字符个数
while(h_back) {
hex_count += (h_back & 0xF) ? 1 : 0;
h_back = h_back >> 4;
}
//这里修复 example 字符有4个但是用了%02x导致字符被截断的情况
if(shift < (hex_count - 1)*4)
shift = (hex_count - 1)*4;
//printf("(%d,%d)", hex_count, shift);
for(; shift >= 0; shift -= 4) {
*q++ = tab0123456789ABCDEF[(h >> shift) & 0xF] | ncase;
}
}
else if(*fmt == 'd') {
int i = *dp++;
char *r;
int digit_space = 0;
if(i < 0) {
*q++ = '-';
i = -i;
digit_space++;
}
p = q; /* save beginning of digits */
do {
*q++ = '0' + (i % 10);
i /= 10;
digit_space++;
} while(i);
//这里修复 example用了%08d后在数字前面没有0的情况
for(; shift >= 0; shift -= 4) {
if(digit_space-- > 0) {
; //do nothing
} else {
*q++ = '0';
}
}
/* reverse digits, stop in middle */
r = q; /* don't alter q */
while(--r > p) {
i = *r;
*r = *p;
*p++ = i;
}
}
else if(*fmt == 'c')
*q++ = *dp++;
else
*q++ = *fmt;
/* now output the saved string */
for(p = tmp; p < q; ++p) {
if(buf) {
*s++ = *p;
} else {
DiagPutChar(*p);
}
if((*p) == '\n') {
DiagPutChar('\r');
}
}
}
}
if(buf)
*s = '\0';
return (s - buf);
}
int DiagPrintfPatch(
IN const char *fmt, ...
)
{
return VSprintfPatch(0, fmt, ((const int *)&fmt)+1);
}
int DiagSPrintfPatch(
IN u8 *buf,
IN const char *fmt, ...
)
{
return VSprintfPatch((char*)buf, fmt, ((const int *)&fmt)+1);
}
#endif

View file

@ -0,0 +1,149 @@
#include <PinNames.h>
#include <pinmap.h>
#include <gpio_api.h>
#include <wifi_wowlan.h>
#include <freertos_pmu.h>
#include <wifi_conf.h>
#define CONFIG_WOWLAN_DEV_NT96658 //build for Nova NT96658
//#define CONFIG_WOWLAN_DEV_OV788 //build for OmniVision OV788
#if defined(CONFIG_WOWLAN_DEV_NT96658) && defined(CONFIG_WOWLAN_DEV_OV788)
#error "CONFIG_WOWLAN_DEV_NT96658 and CONFIG_WOWLAN_DEV_OV788 are mutually exclusive. "
#endif
#ifdef CONFIG_WOWLAN_DEV_NT96658
#define WOW_WIFI_IN_PIN PE_4 // JTAG pin, so JTAG must be disable before using this pin as wakeup pin
#define WOW_TRIGGER_INTERVAL 500
#elif defined(CONFIG_WOWLAN_DEV_OV788)
#define WOW_WIFI_IN_PIN PD_5
#define WOW_WLAN_ON_PIN PB_3
#define WOW_TRIGGER_INTERVAL 200
#else
#error "Either CONFIG_WOWLAN_DEV_NT96658 or CONFIG_WOWLAN_DEV_OV788 should be defined, but not both. "
#endif
//pin assignment for SDIO, default pull high
#define SD_D2 PA_0
#define SD_D3 PA_1
#define SD_CMD PA_2
#define SD_CLK PA_3
#define SD_D0 PA_4
#define SD_D1 PA_5
#define SD_CD PA_6
gpio_t wow_gpio_wifi_in; //WOWLAN WAKEUP TRIGGER PORT
gpio_t wow_gpio_wlan_on; //RECORD WOWLAN STATUS: 1:OFF, 0:ON
int dev_wowlan_init(void){
WOWLAN_PRINTK("WOWLAN: device init!");
#ifdef CONFIG_WOWLAN_DEV_OV788
// Initial WLAN_ON pin
gpio_init(&wow_gpio_wlan_on, WOW_WLAN_ON_PIN);
gpio_dir(&wow_gpio_wlan_on, PIN_OUTPUT);
gpio_mode(&wow_gpio_wlan_on, PullNone);
gpio_write(&wow_gpio_wlan_on, 1);
#endif
return 0;
}
int dev_wowlan_enable(void){
WOWLAN_PRINTK("WOWLAN: device enable!");
// Init WIFI_IN pin (wakeup pin)
gpio_init(&wow_gpio_wifi_in, WOW_WIFI_IN_PIN);
gpio_dir(&wow_gpio_wifi_in, PIN_OUTPUT);
gpio_mode(&wow_gpio_wifi_in, PullNone);
gpio_write(&wow_gpio_wifi_in, 0);
#ifdef CONFIG_WOWLAN_DEV_OV788
gpio_write(&wow_gpio_wlan_on, 0);
#endif
#if CONFIG_WLAN
wifi_set_power_mode(0xff, 1);
#endif
return 0;
}
int dev_wowlan_wakeup_process(void){
WOWLAN_PRINTK("WOWLAN: device wake up!");
#if defined(CONFIG_WOWLAN_DEV_NT96658) || defined(CONFIG_WOWLAN_DEV_OV788)
#if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
//acquire wakelock to keep system awake
acquire_wakelock(WAKELOCK_SDIO_DEVICE);
#endif
#endif
#ifdef CONFIG_WOWLAN_DEV_OV788
//record wowlan status
gpio_write(&wow_gpio_wlan_on, 1);
#endif
#if defined(CONFIG_WOWLAN_DEV_NT96658)
//restore SDIO pin status for bus communication
pin_mode(SD_D0, PullUp);
pin_mode(SD_D1, PullUp);
pin_mode(SD_D2, PullUp);
pin_mode(SD_D3, PullUp);
pin_mode(SD_CMD, PullUp);
pin_mode(SD_CLK, PullDown);
#endif
//send signal to awake host
gpio_write(&wow_gpio_wifi_in, 0);
wowlan_mdelay_os(WOW_TRIGGER_INTERVAL);
gpio_write(&wow_gpio_wifi_in, 1);
wowlan_mdelay_os(WOW_TRIGGER_INTERVAL);
gpio_write(&wow_gpio_wifi_in, 0);
wowlan_mdelay_os(WOW_TRIGGER_INTERVAL);
return 0;
}
int dev_wowlan_sleep_process(void){
#if defined(CONFIG_WOWLAN_DEV_NT96658)
//pull control for SDIO pin only when host is already power off
if(rtw_wowlan_is_enabled() && (rtw_wowlan_get_wk_reason() == 0)){
WOWLAN_PRINTK("pull control");
//configure SDIO pin status for avoiding current leakage
pin_mode(SD_D0, PullNone);
pin_mode(SD_D1, PullNone);
pin_mode(SD_D2, PullNone);
pin_mode(SD_D3, PullNone);
pin_mode(SD_CMD, PullNone);
pin_mode(SD_CLK, PullNone);
}
#endif
return 0;
}
int dev_wowlan_disable(void){
WOWLAN_PRINTK("WOWLAN: device disable!");
#if CONFIG_WLAN
wifi_set_power_mode(0xff, 0);
#endif
#ifdef CONFIG_WOWLAN_DEV_OV788
gpio_write(&wow_gpio_wlan_on, 1);
#endif
return 0;
}
void dev_wowlan_ops_init(void *dev_ops){
struct rtw_wowlan_ops *ops = (struct rtw_wowlan_ops *)dev_ops;
WOWLAN_PRINTK("WOWLAN: device ops init!");
ops->DevWowlanInit = dev_wowlan_init;
ops->DevWowlanEnable = dev_wowlan_enable;
ops->DevWowlanDisable = dev_wowlan_disable;
ops->DevWowlanWakeUp = dev_wowlan_wakeup_process;
ops->DevWowlanSleep = dev_wowlan_sleep_process;
}

View file

@ -0,0 +1,381 @@
#ifndef _WIFI_WOWLAN_H_
#define _WIFI_WOWLAN_H_
#include <platform_stdlib.h>
#include <osdep_service.h>
#include <FreeRTOS.h>
#include <timers.h>
#define WOWLAN_DBG 1
enum{
WOWLAN_DBG_OFF = 0,
WOWLAN_DBG_ALWAYS,
WOWLAN_DBG_ERROR,
WOWLAN_DBG_WARNING,
WOWLAN_DBG_INFO
};
#if WOWLAN_DBG
//#define WOWLAN_DUMP_MSG
#define WOWLAN_DUMP_MSG_1 //dump packet when setting
static unsigned char gWowlanDbgLevel = WOWLAN_DBG_ERROR;
#define WOWLAN_PRINTK(fmt, args...) printf(fmt"\r\n",## args)
#define _WOWLAN_PRINTK(fmt, args...) printf(fmt,## args)
#define WOWLAN_DBG_MSG(level, fmt, args...) \
do{ \
if(level <= gWowlanDbgLevel){ \
WOWLAN_PRINTK(fmt,## args); \
} \
}while(0)
#else
#define WOWLAN_PRINTK(fmt, args...)
#define WOWLAN_DBG_MSG(level, fmt, args...)
#endif
#ifndef u8
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
#endif
#ifndef BIT
#define BIT(x) ((u32)1 << (x))
#endif
#ifndef le16_to_cpu //need a general definition for the whole system
#define cpu_to_le32(x) ((u32)(x))
#define le32_to_cpu(x) ((u32)(x))
#define cpu_to_le16(x) ((u16)(x))
#define le16_to_cpu(x) ((u16)(x))
#endif
#ifndef IP_FMT
#define IP_FMT "%d.%d.%d.%d"
#endif
#ifndef IP_ARG
#define IP_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3]
#endif
#ifndef MAC_FMT
#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
#endif
#ifndef MAC_ARG
#define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
#endif
#ifndef ETH_ALEN
#define ETH_ALEN 6
#endif
#ifndef ethhdr
struct ethhdr
{
unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
unsigned char h_source[ETH_ALEN]; /* source ether addr */
unsigned short h_proto; /* packet type ID field */
};
#endif
#ifndef wowlan_memcpy
#define wowlan_memcpy(d, s, n) rtw_memcpy((void*)(d), ((void*)(s)), (n))
#endif
#ifndef wowlan_malloc
#define wowlan_malloc(sz) rtw_malloc(sz)
#endif
#ifndef wowlan_zmalloc
#define wowlan_zmalloc(sz) rtw_zmalloc(sz)
#endif
#ifndef wowlan_memset
#define wowlan_memset(pbuf, c, sz) rtw_memset(pbuf, c, sz)
#endif
#ifndef wowlan_mfree
#define wowlan_mfree(p, sz) rtw_mfree(((u8*)(p)), (sz))
#endif
#ifndef wowlan_memcmp
#define wowlan_memcmp(s1, s2, n) rtw_memcmp(((void*)(s1)), ((void*)(s2)), (n))
#endif
#ifndef wowlan_mdelay_os
#define wowlan_mdelay_os(ms) rtw_mdelay_os(ms)
#endif
/*Mutex services*/
typedef _mutex _wowlock;
__inline static void _init_wowlock(_wowlock *plock)
{
rtw_mutex_init(plock);
}
__inline static void _free_wowlock(_wowlock *plock)
{
rtw_mutex_free(plock);
}
__inline static void _enter_wowlock(_wowlock *plock)
{
rtw_mutex_get(plock);
}
__inline static void _exit_wowlock(_wowlock *plock)
{
rtw_mutex_put(plock);
}
/*Timer services*/
typedef TimerHandle_t _wowTimer;
#define TMR_AUTO_RELOAD_EN _TRUE
#define TMR_AUTO_RELOAD_DIS _FALSE
__inline static void
_wowlan_init_timer(_wowTimer *ptimer, void *adapter, TIMER_FUN pfunc,void* cntx, const char *name, u32 auto_reload)
{
*ptimer = rtw_timerCreate(
(signed const char *)name, // Just a text name, not used by the RTOS kernel.
TIMER_MAX_DELAY, // Timer Period, not 0
auto_reload, // Whether timer will auto-load themselves when expires
cntx, // Uniq id used to identify which timer expire..
pfunc // Timer callback
);
}
__inline static void
_wowlan_set_timer(_wowTimer *ptimer, u32 delay_time_ms)
{
if(rtw_timerChangePeriod(*ptimer, rtw_ms_to_systime(delay_time_ms), TIMER_MAX_DELAY) == _FAIL)
WOWLAN_PRINTK("Fail to set timer period");
}
__inline static void
_wowlan_cancel_timer(_wowTimer *ptimer)
{
rtw_timerStop(*ptimer, TIMER_MAX_DELAY);
}
__inline static void
_wowlan_del_timer(_wowTimer *ptimer)
{
rtw_timerDelete(*ptimer, TIMER_MAX_DELAY);
}
__inline static void *
_wowlan_get_timer_cntx(_wowTimer timer)
{
return pvTimerGetTimerID(timer);
}
enum rtw_wowlan_wakeup_reason {
RTW_WOWLAN_WAKEUP_BY_PATTERN = BIT(0),
RTW_WOWLAN_WAKEUP_BY_DISCONNECTION = BIT(1),
RTW_WOWLAN_WAKEUP_MAX = 0x7FFFFFFF
};
enum rtw_wowlan_cmd_id{
RTW_WOWLAN_CMD_ENABLE = 0x01, // enable wowlan service
RTW_WOWLAN_CMD_PATTERNS = 0x02, // wowlan pattern setting
RTW_WOWLAN_CMD_PROT_OFFLOAD_CONFIG = 0x03, //ARP offload setting
RTW_WOWLAN_CMD_GET_STATUS = 0x04, // get rtw_wowlan_status
RTW_WOWLAN_CMD_CLEAR_ALL = 0x05, //clear wowlan content
RTW_WOWLAN_CMD_KEEPALIVE = 0x06, //for keep alive packet setting
RTW_WOWLAN_CMD_MAX = 0xff
};
#define RTW_WOWLAN_MAX_RX_FILTERS (5)
#define RTW_WOWLAN_RX_FILTER_MAX_FIELDS (8)
#define RTW_WOWLAN_ID_OFFSET (100) //to match some application, ID starts from 100
#define RTW_WOWLAN_MIN_FILTERS_ID (RTW_WOWLAN_ID_OFFSET)
#define RTW_WOWLAN_MAX_FILTERS_ID (RTW_WOWLAN_ID_OFFSET+RTW_WOWLAN_MAX_RX_FILTERS-1)
struct rtw_wowlan_rx_filter_field {
u16 offset;
u8 len;
u8 flags;
u8 *mask;
u8 *pattern;
};
struct rtw_wowlan_rx_filter {
u8 action;
u8 offset;
u8 num_fields;
struct rtw_wowlan_rx_filter_field fields[RTW_WOWLAN_RX_FILTER_MAX_FIELDS];
};
#if defined(__IAR_SYSTEMS_ICC__)
#pragma pack(1)
#else
#error "this structure needs to be packed!"
#endif
struct rtw_wowlan_status {
u32 wakeup_reasons; //record wake up reason
u32 filter_id; //record which pattern is matched
};
#if defined(__IAR_SYSTEMS_ICC__)
#pragma pack()
#else
#error "this structure needs to be packed!"
#endif
/**
* struct rtw_wowlan_keepalive_packet
*
* @payload_len: data payload length
* @payload: data payload buffer
* @data_interval: interval at which to send data packets
**/
#define RTW_WOWLAN_MAX_KPALIVE_PKT 3
#define RTW_WOWLAN_MAX_KPALIVE_PKT_SZ 512
struct rtw_wowlan_keepalive_packet{
u8 packet_id;
int payload_len;
u8 *payload;
u32 data_interval;
_wowTimer keepalive_tmr;
};
struct rtw_wowlan_ops {
int (*DevWowlanInit)(void);
int (*DevWowlanEnable)(void);
int (*DevWowlanDisable)(void);
int (*DevWowlanWakeUp)(void);
int (*DevWowlanSleep)(void);
};
/**
* enum rtw_wowlan_proto_offloads - enabled protocol offloads
* @RTW_WOWLAN_PROTO_OFFLOAD_ARP: ARP data is enabled
*/
enum rtw_wowlan_proto_offloads {
RTW_WOWLAN_PROTO_OFFLOAD_ARP = BIT(0),
RTW_WOWLAN_PROTO_OFFLOAD_MAX = 0x7FFFFFFF
};
/**
* struct rtw_wowlan_proto_offload_common - ARP/NS offload common part
* @enabled: enable flags
* @remote_ipv4_addr: remote address to answer to (or zero if all)
* @host_ipv4_addr: our IPv4 address to respond to queries for
* @arp_mac_addr: our MAC address for ARP responses
* @reserved: unused
*/
struct rtw_wowlan_proto_offload_common{
int proto_enabled;
u32 remote_ipv4_addr;
u32 host_ipv4_addr;
u8 host_mac_addr[ETH_ALEN];
u16 reserved;
};
struct rtw_wowlan {
_wowlock wow_mutex;
bool enabled;
struct rtw_wowlan_status status;
struct rtw_wowlan_ops ops;
struct rtw_wowlan_proto_offload_common proto;
bool proto_offload_enabled;
struct rtw_wowlan_rx_filter *rx_filter[RTW_WOWLAN_MAX_RX_FILTERS];
bool rx_filter_enabled[RTW_WOWLAN_MAX_RX_FILTERS];/* RX Data filter rule state - enabled/disabled */
struct rtw_wowlan_keepalive_packet *tx_keepalive[RTW_WOWLAN_MAX_KPALIVE_PKT];
bool tx_keepalive_enabled[RTW_WOWLAN_MAX_KPALIVE_PKT];/* TX keep avlive rule state - enabled/disabled */
};
#define eqMacAddr(a,b) ( ((a)[0]==(b)[0] && (a)[1]==(b)[1] && (a)[2]==(b)[2] && (a)[3]==(b)[3] && (a)[4]==(b)[4] && (a)[5]==(b)[5]) ? 1:0 )
#define cpMacAddr(des,src) ((des)[0]=(src)[0],(des)[1]=(src)[1],(des)[2]=(src)[2],(des)[3]=(src)[3],(des)[4]=(src)[4],(des)[5]=(src)[5])
#define cpIpAddr(des,src) ((des)[0]=(src)[0],(des)[1]=(src)[1],(des)[2]=(src)[2],(des)[3]=(src)[3])
#define RTW_WOWLAN_GET_ARP_PKT_OPERATION(__pHeader) ReadEF2Byte( ((u8*)(__pHeader)) + 6)
#define RTW_WOWLAN_GET_ARP_PKT_SENDER_MAC_ADDR(__pHeader, _val) cpMacAddr((u8*)(_val), ((u8*)(__pHeader))+8)
#define RTW_WOWLAN_GET_ARP_PKT_SENDER_IP_ADDR(__pHeader, _val) cpIpAddr((u8*)(_val), ((u8*)(__pHeader))+14)
#define RTW_WOWLAN_GET_ARP_PKT_TARGET_MAC_ADDR(__pHeader, _val) cpMacAddr((u8*)(_val), ((u8*)(__pHeader))+18)
#define RTW_WOWLAN_GET_ARP_PKT_TARGET_IP_ADDR(__pHeader, _val) cpIpAddr((u8*)(_val), ((u8*)(__pHeader))+24)
#define RTW_WOWLAN_SET_ARP_PKT_HW(__pHeader, __Value) WriteEF2Byte( ((u8*)(__pHeader)) + 0, __Value)
#define RTW_WOWLAN_SET_ARP_PKT_PROTOCOL(__pHeader, __Value) WriteEF2Byte( ((u8*)(__pHeader)) + 2, __Value)
#define RTW_WOWLAN_SET_ARP_PKT_HW_ADDR_LEN(__pHeader, __Value) WriteEF1Byte( ((u8*)(__pHeader)) + 4, __Value)
#define RTW_WOWLAN_SET_ARP_PKT_PROTOCOL_ADDR_LEN(__pHeader, __Value) WriteEF1Byte( ((u8*)(__pHeader)) + 5, __Value)
#define RTW_WOWLAN_SET_ARP_PKT_OPERATION(__pHeader, __Value) WriteEF2Byte( ((u8*)(__pHeader)) + 6, __Value)
#define RTW_WOWLAN_SET_ARP_PKT_SENDER_MAC_ADDR(__pHeader, _val) cpMacAddr(((u8*)(__pHeader))+8, (u8*)(_val))
#define RTW_WOWLAN_SET_ARP_PKT_SENDER_IP_ADDR(__pHeader, _val) cpIpAddr(((u8*)(__pHeader))+14, (u8*)(_val))
#define RTW_WOWLAN_SET_ARP_PKT_TARGET_MAC_ADDR(__pHeader, _val) cpMacAddr(((u8*)(__pHeader))+18, (u8*)(_val))
#define RTW_WOWLAN_SET_ARP_PKT_TARGET_IP_ADDR(__pHeader, _val) cpIpAddr(((u8*)(__pHeader))+24, (u8*)(_val))
#define RTW_WOWLAN_ARP_PKT_LEN 0x2A
#define RTW_WOWLAN_ARP_PKT_OPERATION_REQ 0x0100 //arp request
#define RTW_WOWLAN_ARP_PKT_OPERATION_RSP 0x0200 //arp response
extern u8 key_2char2num(u8 hch, u8 lch);
extern _LONG_CALL_ void __rtl_memDump_v1_00(const u8 *start, u32 size, char * strHeader);
#define rtw_wowlan_DumpForBytes(pData, Len) __rtl_memDump_v1_00(pData, Len, NULL)
#define PWOWLAN_TO_STATUS(pwowlan) (&pwowlan->status)
#define PWOWLAN_TO_OPS(pwowlan) (&pwowlan->ops)
#define PWOWLAN_TO_PROTO(pwowlan) (&pwowlan->proto)
#define PWOWLAN_TO_RX_FILTER(pwowlan) (pwowlan->rx_filter)
#define PWOWLAN_TO_TX_KEEPALIVE(pwowlan) (pwowlan->tx_keepalive)
/**
* rtw_wowlan_init: initialize wowlan service
* arg: None
* return: _SUCCESS or _FAIL
*/
extern int rtw_wowlan_init(void);
/**
* cmd_wowlan_service: input commands to configure wowlan service
* arg:
* @argc: number of input parameter
* @argv: content of input string
* return: None
*/
extern void cmd_wowlan_service(int argc, char **argv);
/**
* rtw_wowlan_process_rx_packet: entry for packet process in wowlan service once it starts
* arg:
* @rx_pkt: receive packet from wlan/ethernet
* @pkt_len: receive packet length
* return: _SUCCESS or _FAIL
*/
extern int rtw_wowlan_process_rx_packet(char *rx_pkt, u16 pkt_len);
/**
* rtw_wowlan_wakeup_process: wake up process once the reasons are matched,
* refer to enum rtw_wowlan_wakeup_reason
* arg:
* @reason: wake up reason, refer to enum rtw_wowlan_wakeup_reason
* return: None
*/
extern void rtw_wowlan_wakeup_process(int reason);
/**
* rtw_wowlan_is_enabled: if wowlan service is already enabled
* this function is called in rx path and wifi_inidication when wowlan service is running
* arg: None
* return: _True if enable or _False if disable
*/
extern int rtw_wowlan_is_enabled(void);
/**
* rtw_wowlan_get_wk_reason: query wake up reason, refer to enum rtw_wowlan_wakeup_reason
* arg: None
* return: wakeup_reason
*/
extern int rtw_wowlan_get_wk_reason(void);
/**
* rtw_wowlan_dev_sleep: sleep process on Ameba side, pull control for example
* this function is linked to dev_wowlan_sleep_process() in dev_wowlan.c
* arg: None
* return: None
*/
extern void rtw_wowlan_dev_sleep(void);
#endif

View file

@ -0,0 +1,593 @@
/*
* OS specific functions
* Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#ifndef OS_H
#define OS_H
//#include "basic_types.h"
#include <autoconf.h>
#include "osdep_service.h"
#include "freertos/wrapper.h"
#include "utils/rom/rom_wps_os.h"
typedef void* xqueue_handle_t;
typedef long os_time_t;
typedef _timer os_timer;
/**
* os_sleep - Sleep (sec, usec)
* @sec: Number of seconds to sleep
* @usec: Number of microseconds to sleep
*/
void os_sleep(os_time_t sec, os_time_t usec);
struct os_time {
os_time_t sec;
os_time_t usec;
};
struct os_reltime {
os_time_t sec;
os_time_t usec;
};
/**
* os_get_time - Get current time (sec, usec)
* @t: Pointer to buffer for the time
* Returns: 0 on success, -1 on failure
*/
int os_get_time(struct os_time *t);
int os_get_reltime(struct os_reltime *t);
/* Helper macros for handling struct os_time */
/* (&timeout->time, &tmp->time) */
#define os_time_before(a, b) \
((a)->sec < (b)->sec || \
((a)->sec == (b)->sec && (a)->usec < (b)->usec))
#define os_time_sub(a, b, res) do { \
(res)->sec = (a)->sec - (b)->sec; \
(res)->usec = (a)->usec - (b)->usec; \
if ((res)->usec < 0) { \
(res)->sec--; \
(res)->usec += 1000000; \
} \
} while (0)
/**
* os_mktime - Convert broken-down time into seconds since 1970-01-01
* @year: Four digit year
* @month: Month (1 .. 12)
* @day: Day of month (1 .. 31)
* @hour: Hour (0 .. 23)
* @min: Minute (0 .. 59)
* @sec: Second (0 .. 60)
* @t: Buffer for returning calendar time representation (seconds since
* 1970-01-01 00:00:00)
* Returns: 0 on success, -1 on failure
*
* Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
* which is used by POSIX mktime().
*/
int os_mktime(int year, int month, int day, int hour, int min, int sec,
os_time_t *t);
struct os_tm {
int sec; /* 0..59 or 60 for leap seconds */
int min; /* 0..59 */
int hour; /* 0..23 */
int day; /* 1..31 */
int month; /* 1..12 */
int year; /* Four digit year */
};
int os_gmtime(os_time_t t, struct os_tm *tm);
/* Helpers for handling struct os_time */
/* Helpers for handling struct os_reltime */
static inline int os_reltime_before(struct os_reltime *a,
struct os_reltime *b)
{
return os_time_before(a,b);
}
static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
struct os_reltime *res)
{
os_time_sub(a,b,res);
}
static inline void os_reltime_age(struct os_reltime *start,
struct os_reltime *age)
{
struct os_reltime now;
os_get_time((struct os_time *)&now);
os_reltime_sub(&now, start, age);
}
static inline int os_reltime_expired(struct os_reltime *now,
struct os_reltime *ts,
os_time_t timeout_secs)
{
struct os_reltime age;
os_reltime_sub(now, ts, &age);
return (age.sec > timeout_secs) ||
(age.sec == timeout_secs && age.usec > 0);
}
/**
* os_daemonize - Run in the background (detach from the controlling terminal)
* @pid_file: File name to write the process ID to or %NULL to skip this
* Returns: 0 on success, -1 on failure
*/
int os_daemonize(const char *pid_file);
/**
* os_daemonize_terminate - Stop running in the background (remove pid file)
* @pid_file: File name to write the process ID to or %NULL to skip this
*/
void os_daemonize_terminate(const char *pid_file);
/**
* os_get_random - Get cryptographically strong pseudo random data
* @buf: Buffer for pseudo random data
* @len: Length of the buffer
* Returns: 0 on success, -1 on failure
*/
int os_get_random(unsigned char *buf, size_t len);
/**
* os_random - Get pseudo random value (not necessarily very strong)
* Returns: Pseudo random value
*/
unsigned long os_random(void);
/**
* os_rel2abs_path - Get an absolute path for a file
* @rel_path: Relative path to a file
* Returns: Absolute path for the file or %NULL on failure
*
* This function tries to convert a relative path of a file to an absolute path
* in order for the file to be found even if current working directory has
* changed. The returned value is allocated and caller is responsible for
* freeing it. It is acceptable to just return the same path in an allocated
* buffer, e.g., return strdup(rel_path). This function is only used to find
* configuration files when os_daemonize() may have changed the current working
* directory and relative path would be pointing to a different location.
*/
char * os_rel2abs_path(const char *rel_path);
/**
* os_program_init - Program initialization (called at start)
* Returns: 0 on success, -1 on failure
*
* This function is called when a programs starts. If there are any OS specific
* processing that is needed, it can be placed here. It is also acceptable to
* just return 0 if not special processing is needed.
*/
int os_program_init(void);
/**
* os_program_deinit - Program deinitialization (called just before exit)
*
* This function is called just before a program exists. If there are any OS
* specific processing, e.g., freeing resourced allocated in os_program_init(),
* it should be done here. It is also acceptable for this function to do
* nothing.
*/
void os_program_deinit(void);
/**
* os_setenv - Set environment variable
* @name: Name of the variable
* @value: Value to set to the variable
* @overwrite: Whether existing variable should be overwritten
* Returns: 0 on success, -1 on error
*
* This function is only used for wpa_cli action scripts. OS wrapper does not
* need to implement this if such functionality is not needed.
*/
int os_setenv(const char *name, const char *value, int overwrite);
/**
* os_unsetenv - Delete environent variable
* @name: Name of the variable
* Returns: 0 on success, -1 on error
*
* This function is only used for wpa_cli action scripts. OS wrapper does not
* need to implement this if such functionality is not needed.
*/
int os_unsetenv(const char *name);
/**
* os_readfile - Read a file to an allocated memory buffer
* @name: Name of the file to read
* @len: For returning the length of the allocated buffer
* Returns: Pointer to the allocated buffer or %NULL on failure
*
* This function allocates memory and reads the given file to this buffer. Both
* binary and text files can be read with this function. The caller is
* responsible for freeing the returned buffer with os_free().
*/
char * os_readfile(const char *name, size_t *len);
//#if 0
/**
* os_zalloc - Allocate and zero memory
* @size: Number of bytes to allocate
* Returns: Pointer to allocated and zeroed memory or %NULL on failure
*
* Caller is responsible for freeing the returned buffer with os_free().
*/
void * os_zalloc(size_t size);
/**
* os_calloc - Allocate and zero memory for an array
* @nmemb: Number of members in the array
* @size: Number of bytes in each member
* Returns: Pointer to allocated and zeroed memory or %NULL on failure
*
* This function can be used as a wrapper for os_zalloc(nmemb * size) when an
* allocation is used for an array. The main benefit over os_zalloc() is in
* having an extra check to catch integer overflows in multiplication.
*
* Caller is responsible for freeing the returned buffer with os_free().
*/
static inline void * os_calloc(size_t nmemb, size_t size)
{
if (size && nmemb > (~(size_t) 0) / size)
return NULL;
return os_zalloc(nmemb * size);
}
//#endif
static inline int os_memcmp_const(const void *a, const void *b, size_t len)
{
const u8 *aa = a;
const u8 *bb = b;
size_t i;
u8 res;
for (res = 0, i = 0; i < len; i++)
res |= aa[i] ^ bb[i];
return res;
}
/*
* The following functions are wrapper for standard ANSI C or POSIX functions.
* By default, they are just defined to use the standard function name and no
* os_*.c implementation is needed for them. This avoids extra function calls
* by allowing the C pre-processor take care of the function name mapping.
*
* If the target system uses a C library that does not provide these functions,
* build_config.h can be used to define the wrappers to use a different
* function name. This can be done on function-by-function basis since the
* defines here are only used if build_config.h does not define the os_* name.
* If needed, os_*.c file can be used to implement the functions that are not
* included in the C library on the target system. Alternatively,
* OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
* these functions need to be implemented in os_*.c file for the target system.
*/
#ifdef OS_NO_C_LIB_DEFINES
/**
* os_malloc - Allocate dynamic memory
* @size: Size of the buffer to allocate
* Returns: Allocated buffer or %NULL on failure
*
* Caller is responsible for freeing the returned buffer with os_free().
*/
void * os_malloc(size_t size);
/**
* os_realloc - Re-allocate dynamic memory
* @ptr: Old buffer from os_malloc() or os_realloc()
* @size: Size of the new buffer
* Returns: Allocated buffer or %NULL on failure
*
* Caller is responsible for freeing the returned buffer with os_free().
* If re-allocation fails, %NULL is returned and the original buffer (ptr) is
* not freed and caller is still responsible for freeing it.
*/
void * os_realloc(void *ptr, size_t size);
/**
* os_free - Free dynamic memory
* @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
*/
void os_free(void *ptr);
/**
* os_memcpy - Copy memory area
* @dest: Destination
* @src: Source
* @n: Number of bytes to copy
* Returns: dest
*
* The memory areas src and dst must not overlap. os_memmove() can be used with
* overlapping memory.
*/
void * os_memcpy(void *dest, const void *src, size_t n);
/**
* os_memmove - Copy memory area
* @dest: Destination
* @src: Source
* @n: Number of bytes to copy
* Returns: dest
*
* The memory areas src and dst may overlap.
*/
void *os_memmove(void *dest, const void *src, size_t n);
/**
* os_memset - Fill memory with a constant byte
* @s: Memory area to be filled
* @c: Constant byte
* @n: Number of bytes started from s to fill with c
* Returns: s
*/
void *os_memset(void *s, int c, size_t n);
/**
* os_memcmp - Compare memory areas
* @s1: First buffer
* @s2: Second buffer
* @n: Maximum numbers of octets to compare
* Returns: An integer less than, equal to, or greater than zero if s1 is
* found to be less than, to match, or be greater than s2. Only first n
* characters will be compared.
*/
int os_memcmp(const void *s1, const void *s2, size_t n);
/**
* os_strdup - Duplicate a string
* @s: Source string
* Returns: Allocated buffer with the string copied into it or %NULL on failure
*
* Caller is responsible for freeing the returned buffer with os_free().
*/
char *os_strdup(const char *s);
/**
* os_strlen - Calculate the length of a string
* @s: '\0' terminated string
* Returns: Number of characters in s (not counting the '\0' terminator)
*/
size_t os_strlen(const char *s);
/**
* os_strcasecmp - Compare two strings ignoring case
* @s1: First string
* @s2: Second string
* Returns: An integer less than, equal to, or greater than zero if s1 is
* found to be less than, to match, or be greatred than s2
*/
int os_strcasecmp(const char *s1, const char *s2);
/**
* os_strncasecmp - Compare two strings ignoring case
* @s1: First string
* @s2: Second string
* @n: Maximum numbers of characters to compare
* Returns: An integer less than, equal to, or greater than zero if s1 is
* found to be less than, to match, or be greater than s2. Only first n
* characters will be compared.
*/
int os_strncasecmp(const char *s1, const char *s2, size_t n);
/**
* os_strchr - Locate the first occurrence of a character in string
* @s: String
* @c: Character to search for
* Returns: Pointer to the matched character or %NULL if not found
*/
char *os_strchr(const char *s, int c);
/**
* os_strrchr - Locate the last occurrence of a character in string
* @s: String
* @c: Character to search for
* Returns: Pointer to the matched character or %NULL if not found
*/
char *os_strrchr(const char *s, int c);
/**
* os_strcmp - Compare two strings
* @s1: First string
* @s2: Second string
* Returns: An integer less than, equal to, or greater than zero if s1 is
* found to be less than, to match, or be greatred than s2
*/
int os_strcmp(const char *s1, const char *s2);
/**
* os_strncmp - Compare two strings
* @s1: First string
* @s2: Second string
* @n: Maximum numbers of characters to compare
* Returns: An integer less than, equal to, or greater than zero if s1 is
* found to be less than, to match, or be greater than s2. Only first n
* characters will be compared.
*/
int os_strncmp(const char *s1, const char *s2, size_t n);
/**
* os_strncpy - Copy a string
* @dest: Destination
* @src: Source
* @n: Maximum number of characters to copy
* Returns: dest
*/
char *os_strncpy(char *dest, const char *src, size_t n);
/**
* os_strstr - Locate a substring
* @haystack: String (haystack) to search from
* @needle: Needle to search from haystack
* Returns: Pointer to the beginning of the substring or %NULL if not found
*/
char *os_strstr(const char *haystack, const char *needle);
/**
* os_snprintf - Print to a memory buffer
* @str: Memory buffer to print into
* @size: Maximum length of the str buffer
* @format: printf format
* Returns: Number of characters printed (not including trailing '\0').
*
* If the output buffer is truncated, number of characters which would have
* been written is returned. Since some C libraries return -1 in such a case,
* the caller must be prepared on that value, too, to indicate truncation.
*
* Note: Some C library implementations of snprintf() may not guarantee null
* termination in case the output is truncated. The OS wrapper function of
* os_snprintf() should provide this guarantee, i.e., to null terminate the
* output buffer if a C library version of the function is used and if that
* function does not guarantee null termination.
*
* If the target system does not include snprintf(), see, e.g.,
* http://www.ijs.si/software/snprintf/ for an example of a portable
* implementation of snprintf.
*/
int os_snprintf(char *str, size_t size, const char *format, ...);
#else /* OS_NO_C_LIB_DEFINES */
#if !defined(CONFIG_PLATFORM_8195A) && !defined(CONFIG_PLATFORM_8711B)
#ifdef CONFIG_MEM_MONITOR
u8* os_malloc(u32 sz);
void os_mfree(u8 *pbuf, u32 sz);
#ifndef os_free
#define os_free(p, sz) os_mfree(((u8*)(p)), (sz))
#endif
#else
#ifndef os_malloc
#define os_malloc(sz) _rtw_malloc(sz)
#endif
#ifndef os_free
#define os_free(p, sz) _rtw_mfree(((u8*)(p)), (sz))
#endif
#endif
#endif
extern void *os_zalloc(size_t size);
extern char *os_strdup(const char *string_copy_from);
#ifndef os_sleep
#define os_sleep(s, us) rtw_mdelay_os((s)*1000 + (us)/1000)
#endif
#ifndef os_memcpy
#define os_memcpy(d, s, n) rtw_memcpy((void*)(d), ((void*)(s)), (n))
#endif
#ifndef os_memmove
#define os_memmove(d, s, n) memmove((d), (s), (n))
#endif
#ifndef os_memset
#define os_memset(pbuf, c, sz) rtw_memset(pbuf, c, sz)
#endif
#ifndef os_memcmp
#define os_memcmp(s1, s2, n) rtw_memcmp(((void*)(s1)), ((void*)(s2)), (n))
#endif
#ifndef os_memcmp_p2p
#define os_memcmp_p2p(s1, s2, n) memcmp((s1), (s2), (n))
#endif
#ifndef os_get_random_bytes
#define os_get_random_bytes(d,sz) rtw_get_random_bytes(((void*)(d)), (sz))
#endif
#ifndef os_strlen
#define os_strlen(s) strlen(s)
#endif
#ifndef os_strcasecmp
#ifdef _MSC_VER
#define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
#else
#define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
#endif
#endif
#ifndef os_strncasecmp
#ifdef _MSC_VER
#define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
#else
#define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
#endif
#endif
#ifndef os_init_timer
#define os_init_timer(t, p, f, x, n) rtw_init_timer((t), (p), (f), (x), (n))
#endif
#ifndef os_set_timer
#define os_set_timer(t, d) rtw_set_timer((t), (d))
#endif
#ifndef os_cancel_timer
#define os_cancel_timer(t) rtw_cancel_timer(t)
#endif
#ifndef os_del_timer
#define os_del_timer(t) rtw_del_timer(t)
#endif
#ifndef os_atoi
#define os_atoi(s) rtw_atoi(s)
#endif
#ifndef os_strchr
#define os_strchr(s, c) strchr((s), (c))
#endif
#ifndef os_strcmp
#define os_strcmp(s1, s2) strcmp((s1), (s2))
#endif
#ifndef os_strncmp
#define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
#endif
#ifndef os_strncpy
#define os_strncpy(d, s, n) strncpy((d), (s), (n))
#endif
#ifndef os_strrchr
#define os_strrchr(s, c) strrchr((s), (c))
#endif
#ifndef os_strstr
#define os_strstr(h, n) strstr((h), (n))
#endif
#ifndef os_snprintf
#ifdef _MSC_VER
#define os_snprintf _snprintf
#else
#define os_snprintf snprintf
#endif
#endif
#endif /* OS_NO_C_LIB_DEFINES */
static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
{
if (size && nmemb > (~(size_t) 0) / size)
return NULL;
return os_realloc(ptr, nmemb * size, nmemb * size);
}
void *os_xqueue_create(unsigned long uxQueueLength, unsigned long uxItemSize) ;
int os_xqueue_receive(xqueue_handle_t xQueue, void * const pvBuffer, unsigned long xSecsToWait);
void os_xqueue_delete(xqueue_handle_t xQueue );
int os_xqueue_send(xqueue_handle_t xQueue, const void * const pvItemToQueue, unsigned long xSecsToWait);
#endif /* OS_H */

View file

@ -0,0 +1,119 @@
/*
* OS specific functions for UNIX/POSIX systems
* Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "utils/os.h"
//#ifdef CONFIG_WPS
#if !defined(CONFIG_PLATFORM_8195A) && !defined(CONFIG_PLATFORM_8711B)
#ifdef CONFIG_MEM_MONITOR
#if CONFIG_MEM_MONITOR & MEM_MONITOR_LEAK
_list wpa_mem_table;
int wpa_mem_used_num;
//int wpa_mem_used_size;
#endif
extern int min_free_heap_size;
u8* os_malloc(u32 sz)
{
int free_heap_size = rtw_getFreeHeapSize();
u8 *pbuf = _rtw_malloc(sz);
#if CONFIG_MEM_MONITOR & MEM_MONITOR_LEAK
add_mem_usage(&wpa_mem_table, pbuf, sz, &wpa_mem_used_num, MEM_MONITOR_FLAG_WPAS);
#else
add_mem_usage(NULL, pbuf, sz, NULL, MEM_MONITOR_FLAG_WPAS);
#endif
if(min_free_heap_size > free_heap_size)
min_free_heap_size = free_heap_size;
return pbuf;
}
void os_mfree(u8 *pbuf, u32 sz)
{
_rtw_mfree(pbuf, sz);
#if CONFIG_MEM_MONITOR & MEM_MONITOR_LEAK
del_mem_usage(&wpa_mem_table, pbuf, &wpa_mem_used_num, MEM_MONITOR_FLAG_WPAS);
#else
del_mem_usage(NULL, pbuf, NULL, MEM_MONITOR_FLAG_WPAS);
#endif
}
#endif//CONFIG_MEM_MONITOR
#endif// !defined(CONFIG_PLATFORM_8195A)
#ifndef OS_NO_C_LIB_DEFINES
char *os_strdup(const char *string_copy_from)
{
char *string_copy_to = NULL;
string_copy_to = os_zalloc(strlen(string_copy_from) + 1);
os_memcpy((void *)string_copy_to, string_copy_from, strlen(string_copy_from));
string_copy_to[strlen(string_copy_from)] = '\0';
return string_copy_to;
}
#endif
int os_get_random(unsigned char *buf, size_t len)
{
//TODO implement it
rtw_get_random_bytes(buf, len);
return 0;
}
int os_get_time(struct os_time *t){
unsigned int tt = rtw_get_current_time();
t->sec = (os_time_t) (tt / 1000);
t->usec = (os_time_t) (tt % 1000)*1000;
return 0;
}
int os_get_reltime(struct os_reltime *t){
os_get_time((struct os_time *)t);
return 0;
}
#if 0
void *os_xqueue_create(unsigned long uxQueueLength, unsigned long uxItemSize)
{
return xQueueCreate( uxQueueLength, uxItemSize );
}
int os_xqueue_receive(xqueue_handle_t xQueue, void * const pvBuffer, unsigned long xSecsToWait)
{
return xQueueReceive((xQueueHandle)xQueue, pvBuffer, (portTickType)(xSecsToWait*configTICK_RATE_HZ));
}
void os_xqueue_delete(xqueue_handle_t xQueue )
{
vQueueDelete((xQueueHandle)xQueue);
}
int os_xqueue_send(xqueue_handle_t xQueue, const void * const pvItemToQueue, unsigned long xSecsToWait)
{
return xQueueSendToBack((xQueueHandle)xQueue, pvItemToQueue, (portTickType)(xSecsToWait*configTICK_RATE_HZ));
}
#else
void *os_xqueue_create(unsigned long uxQueueLength, unsigned long uxItemSize)
{
void* xQueue = NULL;
rtw_init_xqueue(&xQueue, "queue", uxItemSize, uxQueueLength);
return xQueue;
}
int os_xqueue_receive(xqueue_handle_t xQueue, void * const pvBuffer, unsigned long xSecsToWait)
{
return rtw_pop_from_xqueue(&xQueue, pvBuffer, xSecsToWait*1000);
}
void os_xqueue_delete(xqueue_handle_t xQueue )
{
rtw_deinit_xqueue(&xQueue);
}
int os_xqueue_send(xqueue_handle_t xQueue, const void * const pvItemToQueue, unsigned long xSecsToWait)
{
return rtw_push_to_xqueue(&xQueue, (void*)pvItemToQueue, xSecsToWait*1000);
}
#endif
//#endif

View file

@ -0,0 +1,24 @@
/*
* OS specific functions
* Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#ifndef ROM_WPS_OS_H
#define ROM_WPS_OS_H
#if defined(CONFIG_PLATFORM_8195A) || defined(CONFIG_PLATFORM_8711B)
#include <rom_wlan_ram_map.h>
extern struct _rom_wlan_ram_map rom_wlan_ram_map;
#define os_malloc(sz) rom_wlan_ram_map.rtw_malloc(sz)
#define os_free(p, sz) rom_wlan_ram_map.rtw_mfree(((u8*)(p)), (sz))
#endif
extern u8 *WPS_realloc(u8 *old_buf, u32 old_sz, u32 new_sz);
#define os_realloc(p, os, ns) WPS_realloc(((u8*)(p)),(os),(ns))
#endif /* ROM_WPS_OS_H */

View file

@ -0,0 +1,316 @@
/*
* Wi-Fi Protected Setup - message definitions
* Copyright (c) 2008, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#ifndef WPS_DEFS_H
#define WPS_DEFS_H
/* Diffie-Hellman 1536-bit MODP Group; RFC 3526, Group 5 */
#define WPS_DH_GROUP (5)
#define WPS_UUID_LEN (16)
#define WPS_NONCE_LEN (16)
#define WPS_AUTHENTICATOR_LEN (8)
#define WPS_AUTHKEY_LEN (32)
#define WPS_KEYWRAPKEY_LEN (16)
#define WPS_EMSK_LEN (32)
#define WPS_PSK_LEN (16)
#define WPS_SECRET_NONCE_LEN (16)
#define WPS_HASH_LEN (32)
#define WPS_KWA_LEN (8)
#define WPS_MGMTAUTHKEY_LEN (32)
#define WPS_MGMTENCKEY_LEN (16)
#define WPS_MGMT_KEY_ID_LEN (16)
#define WPS_OOB_DEVICE_PASSWORD_MIN_LEN (16)
#define WPS_OOB_DEVICE_PASSWORD_LEN (32)
#define WPS_OOB_PUBKEY_HASH_LEN (20)
/* Attribute Types */
enum wps_attribute {
ATTR_AP_CHANNEL = 0x1001,
ATTR_ASSOC_STATE = 0x1002,
ATTR_AUTH_TYPE = 0x1003,
ATTR_AUTH_TYPE_FLAGS = 0x1004,
ATTR_AUTHENTICATOR = 0x1005,
ATTR_CONFIG_METHODS = 0x1008,
ATTR_CONFIG_ERROR = 0x1009,
ATTR_CONFIRM_URL4 = 0x100a,
ATTR_CONFIRM_URL6 = 0x100b,
ATTR_CONN_TYPE = 0x100c,
ATTR_CONN_TYPE_FLAGS = 0x100d,
ATTR_CRED = 0x100e,
ATTR_ENCR_TYPE = 0x100f,
ATTR_ENCR_TYPE_FLAGS = 0x1010,
ATTR_DEV_NAME = 0x1011,
ATTR_DEV_PASSWORD_ID = 0x1012,
ATTR_E_HASH1 = 0x1014,
ATTR_E_HASH2 = 0x1015,
ATTR_E_SNONCE1 = 0x1016,
ATTR_E_SNONCE2 = 0x1017,
ATTR_ENCR_SETTINGS = 0x1018,
ATTR_ENROLLEE_NONCE = 0x101a,
ATTR_FEATURE_ID = 0x101b,
ATTR_IDENTITY = 0x101c,
ATTR_IDENTITY_PROOF = 0x101d,
ATTR_KEY_WRAP_AUTH = 0x101e,
ATTR_KEY_ID = 0x101f,
ATTR_MAC_ADDR = 0x1020,
ATTR_MANUFACTURER = 0x1021,
ATTR_MSG_TYPE = 0x1022,
ATTR_MODEL_NAME = 0x1023,
ATTR_MODEL_NUMBER = 0x1024,
ATTR_NETWORK_INDEX = 0x1026,
ATTR_NETWORK_KEY = 0x1027,
ATTR_NETWORK_KEY_INDEX = 0x1028,
ATTR_NEW_DEVICE_NAME = 0x1029,
ATTR_NEW_PASSWORD = 0x102a,
ATTR_OOB_DEVICE_PASSWORD = 0x102c,
ATTR_OS_VERSION = 0x102d,
ATTR_POWER_LEVEL = 0x102f,
ATTR_PSK_CURRENT = 0x1030,
ATTR_PSK_MAX = 0x1031,
ATTR_PUBLIC_KEY = 0x1032,
ATTR_RADIO_ENABLE = 0x1033,
ATTR_REBOOT = 0x1034,
ATTR_REGISTRAR_CURRENT = 0x1035,
ATTR_REGISTRAR_ESTABLISHED = 0x1036,
ATTR_REGISTRAR_LIST = 0x1037,
ATTR_REGISTRAR_MAX = 0x1038,
ATTR_REGISTRAR_NONCE = 0x1039,
ATTR_REQUEST_TYPE = 0x103a,
ATTR_RESPONSE_TYPE = 0x103b,
ATTR_RF_BANDS = 0x103c,
ATTR_R_HASH1 = 0x103d,
ATTR_R_HASH2 = 0x103e,
ATTR_R_SNONCE1 = 0x103f,
ATTR_R_SNONCE2 = 0x1040,
ATTR_SELECTED_REGISTRAR = 0x1041,
ATTR_SERIAL_NUMBER = 0x1042,
ATTR_WPS_STATE = 0x1044,
ATTR_SSID = 0x1045,
ATTR_TOTAL_NETWORKS = 0x1046,
ATTR_UUID_E = 0x1047,
ATTR_UUID_R = 0x1048,
ATTR_VENDOR_EXT = 0x1049,
ATTR_VERSION = 0x104a,
ATTR_X509_CERT_REQ = 0x104b,
ATTR_X509_CERT = 0x104c,
ATTR_EAP_IDENTITY = 0x104d,
ATTR_MSG_COUNTER = 0x104e,
ATTR_PUBKEY_HASH = 0x104f,
ATTR_REKEY_KEY = 0x1050,
ATTR_KEY_LIFETIME = 0x1051,
ATTR_PERMITTED_CFG_METHODS = 0x1052,
ATTR_SELECTED_REGISTRAR_CONFIG_METHODS = 0x1053,
ATTR_PRIMARY_DEV_TYPE = 0x1054,
ATTR_SECONDARY_DEV_TYPE_LIST = 0x1055,
ATTR_PORTABLE_DEV = 0x1056,
ATTR_AP_SETUP_LOCKED = 0x1057,
ATTR_APPLICATION_EXT = 0x1058,
ATTR_EAP_TYPE = 0x1059,
ATTR_IV = 0x1060,
ATTR_KEY_PROVIDED_AUTO = 0x1061,
ATTR_802_1X_ENABLED = 0x1062,
ATTR_APPSESSIONKEY = 0x1063,
ATTR_WEPTRANSMITKEY = 0x1064,
ATTR_REQUESTED_DEV_TYPE = 0x106a,
ATTR_EXTENSIBILITY_TEST = 0x10fa /* _NOT_ defined in the spec */
};
#define WPS_VENDOR_ID_WFA 14122
/* WFA Vendor Extension subelements */
enum {
WFA_ELEM_VERSION2 = 0x00,
WFA_ELEM_AUTHORIZEDMACS = 0x01,
WFA_ELEM_NETWORK_KEY_SHAREABLE = 0x02,
WFA_ELEM_REQUEST_TO_ENROLL = 0x03,
WFA_ELEM_SETTINGS_DELAY_TIME = 0x04
};
/* Device Password ID */
enum wps_dev_password_id {
DEV_PW_DEFAULT = 0x0000,
DEV_PW_USER_SPECIFIED = 0x0001,
DEV_PW_MACHINE_SPECIFIED = 0x0002,
DEV_PW_REKEY = 0x0003,
DEV_PW_PUSHBUTTON = 0x0004,
DEV_PW_REGISTRAR_SPECIFIED = 0x0005
};
/* Message Type */
enum wps_msg_type {
WPS_START = 0x00,
WPS_Beacon = 0x01,
WPS_ProbeRequest = 0x02,
WPS_ProbeResponse = 0x03,
WPS_M1 = 0x04,
WPS_M2 = 0x05,
WPS_M2D = 0x06,
WPS_M3 = 0x07,
WPS_M4 = 0x08,
WPS_M5 = 0x09,
WPS_M6 = 0x0a,
WPS_M7 = 0x0b,
WPS_M8 = 0x0c,
WPS_WSC_ACK = 0x0d,
WPS_WSC_NACK = 0x0e,
WPS_WSC_DONE = 0x0f
};
/* Authentication Type Flags */
#define WPS_AUTH_OPEN 0x0001
#define WPS_AUTH_WPAPSK 0x0002
#define WPS_AUTH_SHARED 0x0004
#define WPS_AUTH_WPA 0x0008
#define WPS_AUTH_WPA2 0x0010
#define WPS_AUTH_WPA2PSK 0x0020
#define WPS_AUTH_TYPES (WPS_AUTH_OPEN | WPS_AUTH_WPAPSK | WPS_AUTH_SHARED | \
WPS_AUTH_WPA | WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)
/* Encryption Type Flags */
#define WPS_ENCR_NONE 0x0001
#define WPS_ENCR_WEP 0x0002
#define WPS_ENCR_TKIP 0x0004
#define WPS_ENCR_AES 0x0008
#define WPS_ENCR_TYPES (WPS_ENCR_NONE | WPS_ENCR_WEP | WPS_ENCR_TKIP | \
WPS_ENCR_AES)
/* Configuration Error */
enum wps_config_error {
WPS_CFG_NO_ERROR = 0,
WPS_CFG_OOB_IFACE_READ_ERROR = 1,
WPS_CFG_DECRYPTION_CRC_FAILURE = 2,
WPS_CFG_24_CHAN_NOT_SUPPORTED = 3,
WPS_CFG_50_CHAN_NOT_SUPPORTED = 4,
WPS_CFG_SIGNAL_TOO_WEAK = 5,
WPS_CFG_NETWORK_AUTH_FAILURE = 6,
WPS_CFG_NETWORK_ASSOC_FAILURE = 7,
WPS_CFG_NO_DHCP_RESPONSE = 8,
WPS_CFG_FAILED_DHCP_CONFIG = 9,
WPS_CFG_IP_ADDR_CONFLICT = 10,
WPS_CFG_NO_CONN_TO_REGISTRAR = 11,
WPS_CFG_MULTIPLE_PBC_DETECTED = 12,
WPS_CFG_ROGUE_SUSPECTED = 13,
WPS_CFG_DEVICE_BUSY = 14,
WPS_CFG_SETUP_LOCKED = 15,
WPS_CFG_MSG_TIMEOUT = 16,
WPS_CFG_REG_SESS_TIMEOUT = 17,
WPS_CFG_DEV_PASSWORD_AUTH_FAILURE = 18
};
/* RF Bands */
#define WPS_RF_24GHZ (0x01)
#define WPS_RF_50GHZ (0x02)
/* Config Methods */
#define WPS_CONFIG_USBA (0x0001)
#define WPS_CONFIG_ETHERNET (0x0002)
#define WPS_CONFIG_LABEL (0x0004)
#define WPS_CONFIG_DISPLAY (0x0008)
#define WPS_CONFIG_EXT_NFC_TOKEN (0x0010)
#define WPS_CONFIG_INT_NFC_TOKEN (0x0020)
#define WPS_CONFIG_NFC_INTERFACE (0x0040)
#define WPS_CONFIG_PUSHBUTTON (0x0080)
#define WPS_CONFIG_KEYPAD (0x0100)
#ifdef CONFIG_WPS2
#define WPS_CONFIG_VIRT_PUSHBUTTON (0x0280)
#define WPS_CONFIG_PHY_PUSHBUTTON (0x0480)
#define WPS_CONFIG_VIRT_DISPLAY (0x2008)
#define WPS_CONFIG_PHY_DISPLAY (0x4008)
#endif /* CONFIG_WPS2 */
/* Connection Type Flags */
#define WPS_CONN_ESS (0x01)
#define WPS_CONN_IBSS (0x02)
/* Wi-Fi Protected Setup State */
enum wps_state {
WPS_STATE_NOT_CONFIGURED = 1,
WPS_STATE_CONFIGURED = 2
};
/* Association State */
enum wps_assoc_state {
WPS_ASSOC_NOT_ASSOC = 0,
WPS_ASSOC_CONN_SUCCESS = 1,
WPS_ASSOC_CFG_FAILURE = 2,
WPS_ASSOC_FAILURE = 3,
WPS_ASSOC_IP_FAILURE = 4
};
#define WPS_DEV_OUI_WFA (0x0050f204)
enum wps_dev_categ {
WPS_DEV_COMPUTER = 1,
WPS_DEV_INPUT = 2,
WPS_DEV_PRINTER = 3,
WPS_DEV_CAMERA = 4,
WPS_DEV_STORAGE = 5,
WPS_DEV_NETWORK_INFRA = 6,
WPS_DEV_DISPLAY = 7,
WPS_DEV_MULTIMEDIA = 8,
WPS_DEV_GAMING = 9,
WPS_DEV_PHONE = 10
};
enum wps_dev_subcateg {
WPS_DEV_COMPUTER_PC = 1,
WPS_DEV_COMPUTER_SERVER = 2,
WPS_DEV_COMPUTER_MEDIA_CENTER = 3,
WPS_DEV_PRINTER_PRINTER = 1,
WPS_DEV_PRINTER_SCANNER = 2,
WPS_DEV_CAMERA_DIGITAL_STILL_CAMERA = 1,
WPS_DEV_STORAGE_NAS = 1,
WPS_DEV_NETWORK_INFRA_AP = 1,
WPS_DEV_NETWORK_INFRA_ROUTER = 2,
WPS_DEV_NETWORK_INFRA_SWITCH = 3,
WPS_DEV_DISPLAY_TV = 1,
WPS_DEV_DISPLAY_PICTURE_FRAME = 2,
WPS_DEV_DISPLAY_PROJECTOR = 3,
WPS_DEV_MULTIMEDIA_DAR = 1,
WPS_DEV_MULTIMEDIA_PVR = 2,
WPS_DEV_MULTIMEDIA_MCX = 3,
WPS_DEV_GAMING_XBOX = 1,
WPS_DEV_GAMING_XBOX360 = 2,
WPS_DEV_GAMING_PLAYSTATION = 3,
WPS_DEV_PHONE_WINDOWS_MOBILE = 1
};
/* Request Type */
enum wps_request_type {
WPS_REQ_ENROLLEE_INFO = 0,
WPS_REQ_ENROLLEE = 1,
WPS_REQ_REGISTRAR = 2,
WPS_REQ_WLAN_MANAGER_REGISTRAR = 3
};
/* Response Type */
enum wps_response_type {
WPS_RESP_ENROLLEE_INFO = 0,
WPS_RESP_ENROLLEE = 1,
WPS_RESP_REGISTRAR = 2,
WPS_RESP_AP = 3
};
/* Walk Time for push button configuration (in seconds) */
#define WPS_PBC_WALK_TIME (120)
#define WPS_MAX_AUTHORIZED_MACS (5)
#endif /* WPS_DEFS_H */

View file

@ -0,0 +1,476 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "queue.h"
#include "utils/os.h"
#include <lwip_netconf.h>
#include <lwip/netif.h>
#include "wifi/wifi_conf.h"
#include <platform/platform_stdlib.h>
#ifdef CONFIG_ENABLE_EAP
#include <polarssl/ssl.h>
#include <polarssl/memory.h>
#endif
#define WLAN0_NAME "wlan0"
#ifndef ENABLE
#define ENABLE (1)
#endif
#ifndef DISABLE
#define DISABLE (0)
#endif
#ifndef CONFIG_ENABLE_EAP
int get_eap_phase(void){
return 0;
}
int get_eap_method(void){
return 0;
}
void eap_autoreconnect_hdl(u8 method_id)
{
(void) method_id;
}
#else
u8 eap_phase = 0;
u8 eap_method = 0;
// eap config arguments
char *eap_target_ssid = NULL;
char *eap_identity = NULL;
char *eap_password = NULL;
// if set eap_ca_cert and defined(EAP_SSL_VERIFY_SERVER), client will verify server's cert
const unsigned char *eap_ca_cert = NULL;
// if set eap_client_cert, eap_client_key, and defined(EAP_SSL_VERIFY_CLIENT), client will send its cert to server
const unsigned char *eap_client_cert = NULL;
const unsigned char *eap_client_key = NULL;
char *eap_client_key_pwd = NULL;
//int max_buf_bio_size = SSL_BUFFER_LEN; // ?pvvx?
#ifdef CONFIG_ENABLE_EAP
void eap_eapol_recvd_hdl(char *buf, int buf_len, int flags, void* handler_user_data);
void eap_eapol_start_hdl(char *buf, int buf_len, int flags, void* handler_user_data);
#endif
int get_eap_phase(void){
return eap_phase;
}
int get_eap_method(void){
return eap_method;
}
void set_eap_phase(unsigned char is_trigger_eap){
eap_phase = is_trigger_eap;
}
void reset_config(void){
eap_target_ssid = NULL;
eap_identity = NULL;
eap_password = NULL;
eap_ca_cert = NULL;
eap_client_cert = NULL;
eap_client_key = NULL;
eap_client_key_pwd = NULL;
}
#ifdef CONFIG_ENABLE_EAP
void judge_station_disconnect(void)
{
int mode = 0;
unsigned char ssid[33];
wext_get_mode(WLAN0_NAME, &mode);
switch(mode) {
case IW_MODE_MASTER: //In AP mode
wifi_off();
vTaskDelay(20);
wifi_on(RTW_MODE_STA);
break;
case IW_MODE_INFRA: //In STA mode
if(wext_get_ssid(WLAN0_NAME, ssid) > 0)
wifi_disconnect();
}
}
void eap_disconnected_hdl(char *buf, int buf_len, int flags, void* handler_user_data){
// printf("disconnected\n");
wifi_unreg_event_handler(WIFI_EVENT_EAPOL_RECVD, eap_eapol_recvd_hdl);
wifi_unreg_event_handler(WIFI_EVENT_DISCONNECT, eap_disconnected_hdl);
eap_peer_unregister_methods();
eap_sm_deinit();
//reset_config();
}
#endif
/*
void eap_config(void){
eap_target_ssid = "Test_eap";
eap_identity = "guest2";
eap_password = "test2";
eap_client_cert = \
"-----BEGIN CERTIFICATE-----\r\n" \
"MIIC9zCCAd8CAQMwDQYJKoZIhvcNAQEEBQAwgZMxCzAJBgNVBAYTAkZSMQ8wDQYD\r\n" \
"VQQIEwZSYWRpdXMxEjAQBgNVBAcTCVNvbWV3aGVyZTEVMBMGA1UEChMMRXhhbXBs\r\n" \
"ZSBJbmMuMSAwHgYJKoZIhvcNAQkBFhFhZG1pbkBleGFtcGxlLmNvbTEmMCQGA1UE\r\n" \
"AxMdRXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTYwMzE1MDgwNzEx\r\n" \
"WhcNMTcwMzE1MDgwNzExWjBzMQswCQYDVQQGEwJGUjEPMA0GA1UECBMGUmFkaXVz\r\n" \
"MRUwEwYDVQQKEwxFeGFtcGxlIEluYy4xGjAYBgNVBAMUEXVzZXIyQGV4YW1wbGUu\r\n" \
"Y29tMSAwHgYJKoZIhvcNAQkBFhF1c2VyMkBleGFtcGxlLmNvbTCBnzANBgkqhkiG\r\n" \
"9w0BAQEFAAOBjQAwgYkCgYEAqESlV4OYfBcIgZ+Cs8mWpiBjhvKoa0/kIe7saqhC\r\n" \
"e5q4snox0jdkUpLcc4vOs3vQ7ZGnimqTltA9oF6XNUzTWW4vlJTKEfrCWK085l7c\r\n" \
"DHFvHavH3E6vuP71lI7jq4PLXbo2TvZK+uBul4ozjzVWihaZBtz8eLHq446h/D/p\r\n" \
"kzkCAwEAATANBgkqhkiG9w0BAQQFAAOCAQEAAfhVAIkNdeeUNJud720uUHVnIcxz\r\n" \
"GXWI+Svi1qchuTEnRNhLwXmnE+A0WWSHyfdR6FvzdT3xtz3K50iOif8jY2gCGkSK\r\n" \
"8RjKr97228SwbrGO9y9+dYIjH1uz9cBpoVKcpzdsWpKObrDPDYyReHSWo99jM2+O\r\n" \
"vfJxnBw4PLiBj7Q0/dpd6o4JXyp7Cxa0mB4/+cZqjCzzuKfuK3WP7j6laMCV6mg4\r\n" \
"wRZ528IdwDqB7OOqsDm1PVQM8vzny9PM6ikWUCRTVNQJN8RDLkrHR3FRjy15YLdt\r\n" \
"yOfDqVnT/z0wGBaxnNziSJjqPGHPpRi4bJFGXwXOhtknKmciKzfj9/npoQ==\r\n" \
"-----END CERTIFICATE-----\r\n";
eap_client_key = \
"-----BEGIN RSA PRIVATE KEY-----\r\n" \
"MIICXQIBAAKBgQCoRKVXg5h8FwiBn4KzyZamIGOG8qhrT+Qh7uxqqEJ7mriyejHS\r\n" \
"N2RSktxzi86ze9DtkaeKapOW0D2gXpc1TNNZbi+UlMoR+sJYrTzmXtwMcW8dq8fc\r\n" \
"Tq+4/vWUjuOrg8tdujZO9kr64G6XijOPNVaKFpkG3Px4serjjqH8P+mTOQIDAQAB\r\n" \
"AoGARI+LyweshssfxSkIKVc3EcNaqi6PHwJzUrw2ChM624AkR1xwllXJg7ehKVdK\r\n" \
"xmjprRLO8CASuL1qjsBb3fTKnBl+sIVxIFS0AI4Y3ri8VUKbangvSsI7pCzAFry7\r\n" \
"p1gmy9WWRV2ZEa+dV8xcrjb3bloT7hcdeLehgBCvExJIQM0CQQDXlSAKdW3AhYyj\r\n" \
"1A+pfyBSGxJbpSwNyyWgwHIHHjxendxmdUbrc8EbAu1eNKbP58TLgdCZsKcMonAv\r\n" \
"MY1Y2/nnAkEAx9CrUaCU8pJqXTRypM5JtexLKnYMJhpnA9uUILBQOq4Oe0eruyF5\r\n" \
"SaSxhyJYXY491ahWYPF0PTb3jkUhoN+l3wJBAJZthjgGDJlEFwjSFkOtYz4nib3N\r\n" \
"GVpeoFj1MBvrazCScpJDz0LIOLzCZCNSFfwIu3dNk+NKMqZMSn+D0h9pD40CQQC5\r\n" \
"K9n4NXaTLbjAU2CC9mE85JPr76XmkcUxwAWQHZTcLH1jJdIyAx1hb+zNLLjzSmRn\r\n" \
"Yi9ae6ibKhtUjyBQ87HFAkA2Bb3z7NUx+AA2g2HZocFZFShBxylACyQkl8FAFZtf\r\n" \
"osudmKdFQHyAWuBMex4tpz/OLTqJ1ecL1JQeC7OvlpEX\r\n" \
"-----END RSA PRIVATE KEY-----\r\n";
eap_ca_cert = \
"-----BEGIN CERTIFICATE-----\r\n" \
"MIIEpzCCA4+gAwIBAgIJAPvZaozpdfjkMA0GCSqGSIb3DQEBCwUAMIGTMQswCQYD\r\n" \
"VQQGEwJGUjEPMA0GA1UECBMGUmFkaXVzMRIwEAYDVQQHEwlTb21ld2hlcmUxFTAT\r\n" \
"BgNVBAoTDEV4YW1wbGUgSW5jLjEgMB4GCSqGSIb3DQEJARYRYWRtaW5AZXhhbXBs\r\n" \
"ZS5jb20xJjAkBgNVBAMTHUV4YW1wbGUgQ2VydGlmaWNhdGUgQXV0aG9yaXR5MB4X\r\n" \
"DTE2MDMxNDExMjU0OVoXDTE2MDQxMzExMjU0OVowgZMxCzAJBgNVBAYTAkZSMQ8w\r\n" \
"DQYDVQQIEwZSYWRpdXMxEjAQBgNVBAcTCVNvbWV3aGVyZTEVMBMGA1UEChMMRXhh\r\n" \
"bXBsZSBJbmMuMSAwHgYJKoZIhvcNAQkBFhFhZG1pbkBleGFtcGxlLmNvbTEmMCQG\r\n" \
"A1UEAxMdRXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwggEiMA0GCSqGSIb3\r\n" \
"DQEBAQUAA4IBDwAwggEKAoIBAQC9pireu0aCDLNfMaGv3vId7RXjUhQwSK0jV2Oc\r\n" \
"SyvlKWH3P/N+5kLrP2iL6SCzyETVDXZ0vOsAMjcBF0zHp16prXV0d51cTUqeWBb0\r\n" \
"I5UnGxleIuuOfSg8zLUJoBWZPqLv++eZ5WgOKHt7SXocjvg7TU5t/TMB0Y8OCz3H\r\n" \
"CW2vJ/XKMgMA9HDUu4g57cJu88i1JPRpyFaz/HIQBc7+UNb9z+q09uTZKWTmEMqi\r\n" \
"E2U0EEIs7EtbxnOze1/8C4XNlmztrEdwvu6UEBU/TFkUoh9M646NkkBK7wP9n9pv\r\n" \
"T0nPQRJiiCrICzVqUtlEi9lIKpbBSMbQ0KzrGF7lGTgm4rz9AgMBAAGjgfswgfgw\r\n" \
"HQYDVR0OBBYEFIVyecka74kvOKIW0BjlTc/B+a2NMIHIBgNVHSMEgcAwgb2AFIVy\r\n" \
"ecka74kvOKIW0BjlTc/B+a2NoYGZpIGWMIGTMQswCQYDVQQGEwJGUjEPMA0GA1UE\r\n" \
"CBMGUmFkaXVzMRIwEAYDVQQHEwlTb21ld2hlcmUxFTATBgNVBAoTDEV4YW1wbGUg\r\n" \
"SW5jLjEgMB4GCSqGSIb3DQEJARYRYWRtaW5AZXhhbXBsZS5jb20xJjAkBgNVBAMT\r\n" \
"HUV4YW1wbGUgQ2VydGlmaWNhdGUgQXV0aG9yaXR5ggkA+9lqjOl1+OQwDAYDVR0T\r\n" \
"BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAZYHM26sxbKOckVqJJ1QY0U2QFlGP\r\n" \
"1GYd8v27znxdnRmSonDvv3GjFfhwoyDk0JUuxkK/33ikCxihrgoO/EQTY9BV2OpW\r\n" \
"qkB1PDtb3i5ZRNvfjmW0pVA4p+GmdTGaEE5pTlcVnorzVrUeFKaZakb+IDFYzmeF\r\n" \
"xp8B3Bb5wvinDligLOaJnSlgS8QeeIab9HZfaVTTuPmVK6zE6D54Y0dJPnykvDdE\r\n" \
"cGN0FC+migfilFjJgkDJ0r78nwes55L8zjoofiZuO03rrHww6ARc3v1jYzAufddk\r\n" \
"QTiZHgjlMQb2XXMmXLn8kBgoDnqkXFNe8j0h8uxIJSrjOoIyn1h1wvX5/w==\r\n" \
"-----END CERTIFICATE-----\r\n";
}
*/
int eap_start(char *method){
#ifdef CONFIG_ENABLE_EAP
int ret = -1;
//unsigned long tick1 = xTaskGetTickCount();
//unsigned long tick2;
if(rltk_wlan_running(WLAN1_IDX)){
printf("\n\rNot support con-current mode!\n\r");
return -1;
}
judge_station_disconnect();
#if CONFIG_ENABLE_PEAP
if(strcmp(method,"peap") == 0){
ret = set_eap_peap_method();
}
#endif
#if CONFIG_ENABLE_TLS
if(strcmp(method,"tls") == 0){
ret = set_eap_tls_method();
}
#endif
#if CONFIG_ENABLE_TTLS
if(strcmp(method,"ttls") == 0){
ret = set_eap_ttls_method();
}
#endif
if(ret == -1){
printf("\r\neap method %s not supported\r\n", method);
return -1;
}
eap_method = get_eap_ctx_method();
printf("\n==================== %s_start ====================\n", method);
//eap_config();
set_eap_phase(ENABLE);
wifi_reg_event_handler(WIFI_EVENT_EAPOL_START, eap_eapol_start_hdl, NULL);
wifi_reg_event_handler(WIFI_EVENT_EAPOL_RECVD, eap_eapol_recvd_hdl, NULL);
ret = connect_by_open_system(eap_target_ssid);
#if CONFIG_LWIP_LAYER
/* Start DHCPClient */
if(ret == 0)
LwIP_DHCP(0, DHCP_START);
#endif
wifi_unreg_event_handler(WIFI_EVENT_EAPOL_START, eap_eapol_start_hdl);
// for re-authentication when session timeout
wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, eap_disconnected_hdl, NULL);
//wifi_unreg_event_handler(WIFI_EVENT_EAPOL_RECVD, eap_eapol_recvd_hdl);
set_eap_phase(DISABLE);
// eap failed, disconnect
if(ret != 0){
judge_station_disconnect();
eap_disconnected_hdl(NULL, 0, 0, NULL);
rtw_msleep_os(200); //wait handler done
printf("\r\nERROR: connect to AP by %s failed\n", method);
}
eap_sm_deinit();
printf("\n==================== %s_finish ====================\n", method);
//tick2 = xTaskGetTickCount();
//printf("\r\nConnected after %dms.\n", (tick2-tick1));
return ret;
#else
return -1;
#endif
}
#ifdef CONFIG_ENABLE_EAP
static int connect_by_open_system(char *target_ssid)
{
int retry_count = 0, ret;
if (target_ssid != NULL) {
while (1) {
rtw_msleep_os(500); //wait scan complete.
ret = wifi_connect(
NULL,
0,
target_ssid,
RTW_SECURITY_OPEN,
NULL,
0,
NULL);
if (ret == RTW_SUCCESS) {
//printf("\r\n[EAP]Associate with AP success\n");
break;
}
if (retry_count == 0) {
//printf("\r\n[EAP]Associate with AP failed %d\n", ret);
return -1;
}
retry_count --;
printf("Retry connection...\n");
judge_station_disconnect();
set_eap_phase(ENABLE);
}
} else {
printf("\r\n[EAP]Target SSID is NULL\n");
return -1;
}
return 0;
}
static void eap_autoreconnect_thread(void *method)
{
eap_start((char*)method);
vTaskDelete(NULL);
}
#endif
void eap_autoreconnect_hdl(u8 method_id){
#ifdef CONFIG_ENABLE_EAP
char *method;
switch(method_id){
case 25: // EAP_TYPE_PEAP
method = "peap";
break;
case 13: // EAP_TYPE_TLS
method = "tls";
break;
case 21: // EAP_TYPE_TTLS
method = "ttls";
break;
default:
printf("invalid eap method\n");
return;
}
if(xTaskCreate(eap_autoreconnect_thread, ((const char*)"eap_autoreconnect_thread"), 1024, (void*) method, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("\n\r%s xTaskCreate failed\n", __FUNCTION__);
#endif
}
// copy from ssl_client_ext.c
#if ENABLE_EAP_SSL_VERIFY_CLIENT
static x509_crt* _cli_crt = NULL;
static pk_context* _clikey_rsa = NULL;
#endif
#if ENABLE_EAP_SSL_VERIFY_SERVER
static x509_crt* _ca_crt = NULL;
static int eap_verify(void *data, x509_crt *crt, int depth, int *flags)
{
//char buf[1024];
((void) data);
printf("\nVerify requested for (Depth %d):\n", depth);
//x509_crt_info(buf, sizeof(buf) - 1, "", crt);
//printf("%s", buf);
if(((*flags) & BADCERT_EXPIRED) != 0)
printf("server certificate has expired\n");
if(((*flags) & BADCERT_REVOKED) != 0)
printf(" ! server certificate has been revoked\n");
if(((*flags) & BADCERT_CN_MISMATCH) != 0)
printf(" ! CN mismatch\n");
if(((*flags) & BADCERT_NOT_TRUSTED) != 0)
printf(" ! self-signed or not signed by a trusted CA\n");
if(((*flags) & BADCRL_NOT_TRUSTED) != 0)
printf(" ! CRL not trusted\n");
if(((*flags) & BADCRL_EXPIRED) != 0)
printf(" ! CRL expired\n");
if(((*flags) & BADCERT_OTHER) != 0)
printf(" ! other (unknown) flag\n");
if((*flags) == 0)
printf(" Certificate verified without error flags\n");
return(0);
}
#endif
int eap_cert_init(void)
{
#if ENABLE_EAP_SSL_VERIFY_CLIENT
if(eap_client_cert != NULL && eap_client_key != NULL){
_cli_crt = polarssl_malloc(sizeof(x509_crt));
if(_cli_crt)
x509_crt_init(_cli_crt);
else
return -1;
_clikey_rsa = polarssl_malloc(sizeof(pk_context));
if(_clikey_rsa)
pk_init(_clikey_rsa);
else
return -1;
}
#endif
#if ENABLE_EAP_SSL_VERIFY_SERVER
if(eap_ca_cert != NULL){
_ca_crt = polarssl_malloc(sizeof(x509_crt));
if(_ca_crt)
x509_crt_init(_ca_crt);
else
return -1;
}
#endif
return 0;
}
void eap_client_cert_free(void)
{
#if ENABLE_EAP_SSL_VERIFY_CLIENT
if(eap_client_cert != NULL && eap_client_key != NULL){
if(_cli_crt) {
x509_crt_free(_cli_crt);
polarssl_free(_cli_crt);
_cli_crt = NULL;
}
if(_clikey_rsa) {
pk_free(_clikey_rsa);
polarssl_free(_clikey_rsa);
_clikey_rsa = NULL;
}
}
#endif
}
void eap_server_cert_free(void)
{
#if ENABLE_EAP_SSL_VERIFY_SERVER
if(eap_ca_cert != NULL){
if(_ca_crt) {
x509_crt_free(_ca_crt);
polarssl_free(_ca_crt);
_ca_crt = NULL;
}
}
#endif
}
int eap_cert_setup(ssl_context *ssl)
{
#if ENABLE_EAP_SSL_VERIFY_CLIENT
if(eap_client_cert != NULL && eap_client_key != NULL){
if(x509_crt_parse(_cli_crt, eap_client_cert, strlen(eap_client_cert)) != 0)
return -1;
if(pk_parse_key(_clikey_rsa, eap_client_key, strlen(eap_client_key), eap_client_key_pwd, strlen(eap_client_key_pwd)) != 0)
return -1;
ssl_set_own_cert(ssl, _cli_crt, _clikey_rsa);
}
#endif
#if ENABLE_EAP_SSL_VERIFY_SERVER
if(eap_ca_cert != NULL){
if(x509_crt_parse(_ca_crt, eap_ca_cert, strlen(eap_ca_cert)) != 0)
return -1;
ssl_set_ca_chain(ssl, _ca_crt, NULL, NULL);
ssl_set_authmode(ssl, SSL_VERIFY_REQUIRED);
ssl_set_verify(ssl, eap_verify, NULL);
}
#endif
return 0;
}
#endif //#ifdef CONFIG_ENABLE_EAP

View file

@ -0,0 +1,267 @@
#include "FreeRTOS.h"
#include "task.h"
#include "utils/os.h"
#include <lwip/netif.h>
#include <wifi/wifi_conf.h>
#include "wps/wps_defs.h"
#if CONFIG_ENABLE_P2P
enum p2p_wps_method {
WPS_NOT_READY, WPS_PIN_DISPLAY, WPS_PIN_KEYPAD, WPS_PBC
};
/*NETMASK*/
#define P2P_NETMASK_ADDR0 255
#define P2P_NETMASK_ADDR1 255
#define P2P_NETMASK_ADDR2 255
#define P2P_NETMASK_ADDR3 0
/*Gateway Address*/
#define P2P_GW_ADDR0 192
#define P2P_GW_ADDR1 168
#define P2P_GW_ADDR2 42
#define P2P_GW_ADDR3 1
#define P2P_GO_NEGO_RESULT_SIZE 376//256
xqueue_handle_t queue_for_p2p_nego;
extern void dhcps_init(struct netif * pnetif);
static int hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
/**
* hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
* @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
* @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
* Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
*/
int hwaddr_aton(const char *txt, u8 *addr)
{
int i;
for (i = 0; i < 6; i++) {
int a, b;
a = hex2num(*txt++);
if (a < 0)
return -1;
b = hex2num(*txt++);
if (b < 0)
return -1;
*addr++ = (a << 4) | b;
if (i < 5 && *txt++ != ':')
return -1;
}
return 0;
}
int wifi_start_p2p_go(char *ssid, char *passphrase, u8 channel)
{
extern struct netif xnetif[NET_IF_NUM];
struct netif * pnetif = &xnetif[0];
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
IP4_ADDR(&ipaddr, P2P_GW_ADDR0, P2P_GW_ADDR1, P2P_GW_ADDR2, P2P_GW_ADDR3);
IP4_ADDR(&netmask, P2P_NETMASK_ADDR0, P2P_NETMASK_ADDR1 , P2P_NETMASK_ADDR2, P2P_NETMASK_ADDR3);
IP4_ADDR(&gw, P2P_GW_ADDR0, P2P_GW_ADDR1, P2P_GW_ADDR2, P2P_GW_ADDR3);
netif_set_addr(pnetif, &ipaddr, &netmask,&gw);
// start ap
if(wifi_start_ap( ssid,
RTW_SECURITY_WPA2_AES_PSK,
passphrase,
channel,
0) != RTW_SUCCESS) {
printf("ERROR: Operation failed!\n");
return -1;
}
netif_set_default(pnetif);
// start dhcp server
dhcps_init(pnetif);
return 0;
}
void app_callback(char *msg)
{
//From Application
}
void cmd_wifi_p2p_start(int argc, char **argv)
{
extern struct netif xnetif[NET_IF_NUM];
int listen_ch = 1;
int op_ch = 5;
int go_intent = 1;
#if 1
u32 r = 0;
os_get_random((u8 *) &r, sizeof(r));
go_intent = r%15+1; /*1-15*/
os_get_random((u8 *) &r, sizeof(r));
listen_ch = 1 + (r % 3) * 5;
os_get_random((u8 *) &r, sizeof(r));
op_ch = 1 + (r % 3) * 5;
#endif
wifi_off();
os_sleep(0, 20000);
wifi_on(RTW_MODE_P2P);
wifi_p2p_init(xnetif[0].hwaddr, go_intent, listen_ch, op_ch);
}
int cmd_wifi_p2p_auto_go_start(int argc, char **argv)
{
u8 *passphrase = "12345678";
u8 channel = 6; // 1, 6, 11
const char *ssid_in = "DIRECT-34-Ameba";
const char *dev_name = "Ameba1234"; // max strlen 32
const char *manufacturer = "by customer"; // max strlen 64
const char *model_name = "customer"; // max strlen 32
const char *model_number = "v2.0"; // max strlen 32
const char *serial_number = "9"; // max strlen 32
const u8 pri_dev_type[8] = {0x00,0x0A,0x00,0x50,0xF2,0x04,0x00,0x01}; // category ID:0x00,0x0A; sub category ID:0x00,0x01
u8 res[P2P_GO_NEGO_RESULT_SIZE];
u16 config_methods = WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_PUSHBUTTON;
if(!is_wifi_p2p_initialized())
return -1;
wifi_p2p_set_dev_name(dev_name);
wifi_p2p_set_manufacturer(manufacturer);
wifi_p2p_set_model_name(model_name);
wifi_p2p_set_model_number(model_number);
wifi_p2p_set_serial_number(serial_number);
wifi_p2p_set_pri_dev_type(pri_dev_type);
wifi_p2p_set_ssid(ssid_in);
wifi_p2p_set_config_methods(config_methods);
wifi_p2p_init_auto_go_params(res, passphrase, channel);
wifi_p2p_start_auto_go(res);
return 0;
}
void cmd_wifi_p2p_stop(int argc, char **argv)
{
wifi_p2p_deinit();
wifi_off();
}
void cmd_p2p_listen(int argc, char **argv)
{
u32 timeout = 0;
if(argc == 2){
timeout = os_atoi((u8*)argv[1]);
printf("\r\n%s(): timeout=%d\n", __func__, timeout);
if(timeout > 3600)
timeout = 3600;
}
wifi_cmd_p2p_listen(timeout);
}
void cmd_p2p_find(int argc, char **argv)
{
wifi_cmd_p2p_find();
}
void cmd_p2p_peers(int argc, char **argv)
{
wifi_cmd_p2p_peers();
}
void cmd_p2p_info(int argc, char **argv)
{
wifi_cmd_p2p_info();
}
void cmd_p2p_disconnect(int argc, char **argv)
{
wifi_cmd_p2p_disconnect();
}
void cmd_p2p_connect(int argc, char **argv)
{
enum p2p_wps_method config_method = WPS_PBC;
char *pin = NULL;
u8 dest[ETH_ALEN] = {0x44, 0x6d, 0x57, 0xd7, 0xce, 0x41};
u8 res[P2P_GO_NEGO_RESULT_SIZE];
int ret = 0;
#if 1
if((argc != 2) && (argc != 3) && (argc != 4)) {
printf("Usage: p2p_connect DEST_ADDR [pbc|pin] [pin code]\n");
printf("Example: p2p_connect 00:e0:4c:87:00:15 pin 12345678\n");
return;
}
if (hwaddr_aton(argv[1], dest)){
printf("P2P_CONNECT: dest address is not correct!\n");
return;
}
//printf("\r\nDEST: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", dest[0], dest[1], dest[2], dest[3], dest[4], dest[5]);
config_method = WPS_PBC;
if(argc == 3) {
if(os_strncmp(argv[2], "pbc", 3) == 0)
config_method = WPS_PBC;
else if(os_strncmp(argv[2], "pin", 3) == 0){
config_method = WPS_PIN_DISPLAY;
}else{
printf("Unknown config method!\n");
printf("Usage: p2p_connect DEST_ADDR [pbc|pin] \n");
printf("Example: p2p_connect 00:e0:4c:87:00:15 pin\n");
return;
}
}
else if(argc == 4) {
if(os_strncmp(argv[2], "pin", 3) == 0){
config_method = WPS_PIN_KEYPAD;
pin = argv[3];
}else{
printf("Unknown config method!\n");
printf("Usage: p2p_connect DEST_ADDR [pbc|pin] [pin code]\n");
printf("Example: p2p_connect 00:e0:4c:87:00:15 pin 12345678\n");
return;
}
}
#else //For test
u8 dest1[ETH_ALEN] = {0xea, 0x92, 0xa4, 0x9b, 0x61, 0xd6}; //NEXUS 4
//u8 dest1[ETH_ALEN] = {0x0e, 0x37, 0xdc, 0xfc, 0xc4, 0x12}; //HUAWEI U9508_c001
//u8 dest1[ETH_ALEN] = {0x42, 0xcb, 0xa8, 0xd3, 0x2c, 0x50}; //HUAWEI G610-T00
os_memcpy(dest, dest1, ETH_ALEN);
config_method = WPS_PBC;
#endif
if (queue_for_p2p_nego!= NULL) {
os_xqueue_delete(queue_for_p2p_nego);
queue_for_p2p_nego = NULL;
}
queue_for_p2p_nego = os_xqueue_create(1, P2P_GO_NEGO_RESULT_SIZE);
if(queue_for_p2p_nego != NULL) {
ret = wifi_cmd_p2p_connect(dest, config_method, pin);
if(ret == 0)
os_xqueue_receive(queue_for_p2p_nego, res, 15);
os_xqueue_delete(queue_for_p2p_nego);
queue_for_p2p_nego = NULL;
if(ret == 0)
wifi_p2p_start_wps(res);
}
}
#endif //CONFIG_ENABLE_P2P

View file

@ -0,0 +1,755 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "queue.h"
#include "utils/os.h"
#include <lwip_netconf.h>
#include <lwip/netif.h>
#include "wifi/wifi_conf.h"
#include "wps/wps_defs.h"
#include <platform/platform_stdlib.h>
/*
* @brief struct wps_credential - WPS Credential
*/
struct dev_credential {
u8 ssid[32]; /**< SSID */
size_t ssid_len; /**< Length of SSID */
u16 auth_type; /**< Authentication Type (WPS_AUTH_OPEN, .. flags) */
u16 encr_type; /**< Encryption Type (WPS_ENCR_NONE, .. flags) */
u8 key_idx; /**< Key index */
u8 key[65]; /**< Key */
size_t key_len; /**< Key length in octets */
u8 mac_addr[6]; /**< MAC address of the Credential receiver */
const u8 *cred_attr; /**< Unparsed Credential attribute data (used only in cred_cb()).
This may be NULL, if not used. */
size_t cred_attr_len; /**< Length of cred_attr in octets */
u16 ap_channel; /**< AP channel */
};
typedef struct {
char *target_ssid;
u16 config_method;
_sema scan_sema;
int isoverlap;
} internal_wps_scan_handler_arg_t;
#define WLAN0_NAME "wlan0"
#ifndef ENABLE
#define ENABLE (1)
#endif
#ifndef DISABLE
#define DISABLE (0)
#endif
#define STACKSIZE 512
//static xSemaphoreHandle wps_reconnect_semaphore;
//static struct _WIFI_NETWORK wifi_get_from_certificate = {0};
#define WPS_AUTH_TYPE_OPEN (0x0001)
#define WPS_AUTH_TYPE_WPA_PERSONAL (0x0002)
#define WPS_AUTH_TYPE_SHARED (0x0004)
#define WPS_AUTH_TYPE_WPA_ENTERPRISE (0x0008)
#define WPS_AUTH_TYPE_WPA2_PERSONAL (0x0010)
#define WPS_AUTH_TYPE_WPA2_ENTERPRISE (0x0020)
#define WPS_ENCR_TYPE_NONE (0x0001)
#define WPS_ENCR_TYPE_WEP (0x0002)
#define WPS_ENCR_TYPE_TKIP (0x0004)
#define WPS_ENCR_TYPE_AES (0x0008)
#define SCAN_BUFFER_LENGTH (4096)
#if CONFIG_ENABLE_P2P
extern void _wifi_p2p_wps_success(const u8 *peer_addr, int registrar);
extern void _wifi_p2p_wps_failed();
#endif
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
extern u32 _wps_registrar_process_msg(void *priv, u32 op_code, const void *pmsg);
extern void * _wps_registrar_get_msg(void *priv, u32 *op_code);
extern void * _wps_registrar_init(void *priv, const void* pcfg);
extern void _wps_registrar_deinit(void *priv);
extern void *_wps_registrar_alloc();
extern int _wps_registrar_add_pin(void *priv, const u8 *addr,
const u8 *uuid, const u8 *pin, size_t pin_len,
int timeout);
extern int _wps_registrar_button_pushed(void *priv,
const u8 *p2p_dev_addr);
extern int _wps_registrar_wps_cancel(void *priv);
extern void _wpas_wsc_ap_send_eap_reqidentity(void *priv, u8 *rx_buf);
extern void _wpas_wsc_ap_check_eap_rspidentity(void *priv, u8 *rx_buf);
extern void _wpas_wsc_registrar_send_eap_fail(void *priv);
extern void _wpas_wsc_registrar_handle_recvd(void *priv, u8 *rx_buf);
extern void * _eap_wsc_server_process_hdl(void *priv, void* req, u8 id);
extern void *_eap_wsc_server_reset(void *priv);
#endif
extern void wpas_wsc_sta_wps_start_hdl(char *buf, int buf_len, int flags, void *userdata);
extern void wpas_wsc_wps_finish_hdl(char *buf, int buf_len, int flags, void *userdata);
extern void wpas_wsc_eapol_recvd_hdl(char *buf, int buf_len, int flags, void *userdata);
void wifi_p2p_wps_success(const u8 *peer_addr, int registrar)
{
#if CONFIG_ENABLE_P2P
_wifi_p2p_wps_success(peer_addr, registrar);
#endif
}
void wifi_p2p_wps_failed()
{
#if CONFIG_ENABLE_P2P
_wifi_p2p_wps_failed();
#endif
}
void * wps_registrar_init(void *priv, void *pcfg)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_init(priv, pcfg);
#else
return NULL;
#endif
}
void wps_registrar_deinit(void *priv)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
_wps_registrar_deinit(priv);
#endif
}
void *wps_registrar_alloc()
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_alloc();
#else
return NULL;
#endif
}
u32 wps_registrar_process_msg(void *priv, u32 op_code, const void *pmsg)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_process_msg(priv, op_code, pmsg);
#else
return 0;
#endif
}
void * wps_registrar_get_msg(void *priv, u32 *op_code)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_get_msg(priv, op_code);
#else
return NULL;
#endif
}
int wps_registrar_add_pin(void *priv, const u8 *addr,
const u8 *uuid, const u8 *pin, size_t pin_len,
int timeout)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_add_pin(priv, NULL,NULL,pin,pin_len,0);
#else
return 0;
#endif
}
int wps_registrar_button_pushed(void *priv,
const u8 *p2p_dev_addr)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_button_pushed(priv, p2p_dev_addr);
#else
return 0;
#endif
}
int wps_registrar_wps_cancel(void *priv)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _wps_registrar_wps_cancel(priv);
#else
return 0;
#endif
}
void wpas_wsc_ap_send_eap_reqidentity(void *priv, u8 *rx_buf)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
_wpas_wsc_ap_send_eap_reqidentity(priv, rx_buf);
#endif
}
void wpas_wsc_ap_check_eap_rspidentity(void *priv, u8 *rx_buf)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
_wpas_wsc_ap_check_eap_rspidentity(priv, rx_buf);
#endif
}
void wpas_wsc_registrar_send_eap_fail(void *priv)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
_wpas_wsc_registrar_send_eap_fail(priv);
#endif
}
void wpas_wsc_registrar_handle_recvd(void *priv, u8 *rx_buf)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
_wpas_wsc_registrar_handle_recvd(priv, rx_buf);
#endif
}
void * eap_wsc_server_process_hdl(void *priv, void* req, u8 id)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
return _eap_wsc_server_process_hdl(priv, req, id);
#else
return NULL;
#endif
}
void eap_wsc_server_reset(void *priv)
{
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
_eap_wsc_server_reset(priv);
#endif
}
#if CONFIG_ENABLE_WPS
xqueue_handle_t queue_for_credential;
char wps_pin_code[32];
u16 config_method;
u8 wps_password_id;
static TaskHandle_t ap_wps_task = NULL;
void wps_check_and_show_connection_info(void)
{
rtw_wifi_setting_t setting;
#if CONFIG_LWIP_LAYER
/* Start DHCP Client */
LwIP_DHCP(0, DHCP_START);
#endif
wifi_get_setting(WLAN0_NAME, &setting);
wifi_show_setting(WLAN0_NAME, &setting);
#if CONFIG_INIC_CMD_RSP
inic_c2h_wifi_info("ATWW", RTW_SUCCESS);
#endif
}
static void wps_config_wifi_setting(rtw_network_info_t *wifi, struct dev_credential *dev_cred)
{
printf("wps_config_wifi_setting\n");
//memcpy((void *)wifi->ssid, (void *)dev_cred->ssid, dev_cred->ssid_len);
strcpy((char*)wifi->ssid.val, (char*)&dev_cred->ssid[0]);
printf("wps_wifi.ssid = %s\n", wifi->ssid.val);
wifi->ssid.len = dev_cred->ssid_len;
printf("wps_wifi.ssid_len = %d\n", wifi->ssid.len);
switch(dev_cred->auth_type) {
case WPS_AUTH_TYPE_OPEN :
case WPS_AUTH_TYPE_SHARED :
if(dev_cred->encr_type == WPS_ENCR_TYPE_WEP) {
printf("security_type = RTW_SECURITY_WEP_PSK\n");
wifi->security_type = RTW_SECURITY_WEP_PSK;
wifi->key_id = dev_cred->key_idx - 1;
}
else {
printf("security_type = RTW_SECURITY_OPEN\n");
wifi->security_type = RTW_SECURITY_OPEN;
}
break;
case WPS_AUTH_TYPE_WPA_PERSONAL :
case WPS_AUTH_TYPE_WPA_ENTERPRISE :
printf("security_type = RTW_SECURITY_WPA_AES_PSK\n");
wifi->security_type = RTW_SECURITY_WPA_AES_PSK;
break;
case WPS_AUTH_TYPE_WPA2_PERSONAL :
case WPS_AUTH_TYPE_WPA2_ENTERPRISE :
printf("security_type = RTW_SECURITY_WPA2_AES_PSK\n");
wifi->security_type = RTW_SECURITY_WPA2_AES_PSK;
break;
}
printf("wps_wifi.security_type = %d\n", wifi->security_type);
//memcpy(wifi->password, dev_cred->key, dev_cred->key_len);
wifi->password = dev_cred->key;
printf("wps_wifi.password = %s\n", wifi->password);
wifi->password_len = dev_cred->key_len;
printf("wps_wifi.password_len = %d", wifi->password_len);
//xSemaphoreGive(wps_reconnect_semaphore);
//printf("\r\nrelease wps_reconnect_semaphore");
}
static int wps_connect_to_AP_by_certificate(rtw_network_info_t *wifi)
{
#define RETRY_COUNT 3
int retry_count = RETRY_COUNT, ret;
printf("=============== wifi_certificate_info ===============\n");
printf("wps_wifi.ssid = %s\n", wifi->ssid.val);
printf("security_type = %d\n", wifi->security_type);
printf("wps_wifi.password = %s\n", wifi->password);
printf("ssid_len = %d\n", wifi->ssid.len);
printf("password_len = %d\n", wifi->password_len);
while (1) {
ret = wifi_connect(
NULL,
0,
(char*)wifi->ssid.val,
wifi->security_type,
(char*)wifi->password,
wifi->key_id,
NULL);
if (ret == RTW_SUCCESS) {
if(retry_count == RETRY_COUNT)
rtw_msleep_os(1000); //When start wps with OPEN AP, AP will send a disassociate frame after STA connected, need reconnect here.
if(RTW_SUCCESS == wifi_is_connected_to_ap( )){
//printf("\r\n[WPS]Ready to tranceive!\n");
wps_check_and_show_connection_info();
break;
}
}
if (retry_count == 0) {
printf("[WPS]Join bss failed\n");
ret = -1;
break;
}
retry_count --;
}
return ret;
}
static int wps_connect_to_AP_by_open_system(char *target_ssid)
{
int retry_count = 3, ret;
if (target_ssid != NULL) {
rtw_msleep_os(500); //wait scan complete.
while (1) {
ret = wifi_connect(
NULL,
0,
target_ssid,
RTW_SECURITY_OPEN,
NULL,
0,
NULL);
if (ret == RTW_SUCCESS) {
//wps_check_and_show_connection_info();
break;
}
if (retry_count == 0) {
printf("[WPS]Join bss failed\n");
return -1;
}
retry_count --;
}
//
} else {
printf("[WPS]Target SSID is NULL\n");
}
return 0;
}
static void process_wps_scan_result( rtw_scan_result_t* record, void * user_data )
{
internal_wps_scan_handler_arg_t *wps_arg = (internal_wps_scan_handler_arg_t *)user_data;
if (record->wps_type != 0xff) {
if (wps_arg->config_method == WPS_CONFIG_PUSHBUTTON) {
if (record->wps_type == 0x04) {
wps_password_id = record->wps_type;
if (++wps_arg->isoverlap == 0) {
memcpy(&wps_arg->target_ssid[0], record->SSID.val, record->SSID.len);
wps_arg->target_ssid[record->SSID.len] = '\0';
printf("[pbc]Record first triger wps AP = %s\n", wps_arg->target_ssid);
}
}
} else if (wps_arg->config_method == WPS_CONFIG_DISPLAY || wps_arg->config_method == WPS_CONFIG_KEYPAD) {
if (record->wps_type == 0x00) {
wps_arg->isoverlap = 0;
wps_password_id = record->wps_type;
memcpy(&wps_arg->target_ssid[0], record->SSID.val, record->SSID.len);
wps_arg->target_ssid[record->SSID.len] = '\0';
printf("[pin]find out first triger wps AP = %s\n", wps_arg->target_ssid);
}
}
}
}
static rtw_result_t wps_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
{
internal_wps_scan_handler_arg_t *wps_arg = (internal_wps_scan_handler_arg_t *)malloced_scan_result->user_data;
if (malloced_scan_result->scan_complete != RTW_TRUE)
{
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
process_wps_scan_result(record, malloced_scan_result->user_data);
}
else
{
printf("WPS scan done!\n");
rtw_up_sema(&wps_arg->scan_sema);
}
return RTW_SUCCESS;
}
extern void wifi_scan_each_report_hdl( char* buf, int buf_len, int flags, void* userdata);
extern void wifi_scan_done_hdl( char* buf, int buf_len, int flags, void* userdata);
static int wps_find_out_triger_wps_AP(char *target_ssid, u16 config_method)
{
internal_wps_scan_handler_arg_t wps_arg = {0};
wps_password_id = 0xFF;
wps_arg.isoverlap = -1;
wps_arg.config_method = config_method;
wps_arg.target_ssid = target_ssid;
rtw_init_sema(&wps_arg.scan_sema, 0);
if(wps_arg.scan_sema == NULL) return RTW_ERROR;
if(wifi_scan_networks(wps_scan_result_handler, &wps_arg ) != RTW_SUCCESS){
printf("ERROR: wifi scan failed\n");
goto exit;
}
if(rtw_down_timeout_sema(&wps_arg.scan_sema, SCAN_LONGEST_WAIT_TIME) == RTW_FALSE){
printf("WPS scan done early!\n");
}
wifi_unreg_event_handler(WIFI_EVENT_SCAN_RESULT_REPORT, wifi_scan_each_report_hdl);
wifi_unreg_event_handler(WIFI_EVENT_SCAN_DONE, wifi_scan_done_hdl);
exit:
rtw_free_sema(&wps_arg.scan_sema);
return wps_arg.isoverlap;
}
int wps_start(u16 wps_config, char *pin, u8 channel, char *ssid)
{
struct dev_credential dev_cred;
rtw_network_info_t wifi = {0};
char target_ssid[64];
int is_overlap = -1;
u32 start_time = rtw_get_current_time();
int ret = 0;
memset(&dev_cred, 0, sizeof(struct dev_credential));
memset(target_ssid, 0, 64);
if((wps_config != WPS_CONFIG_PUSHBUTTON)
&& (wps_config != WPS_CONFIG_DISPLAY)
&& (wps_config != WPS_CONFIG_KEYPAD)){
printf("WPS: Wps method(%d) is wrong. Not triger WPS.\n", wps_config);
return -1;
}
config_method = wps_config;
if(wps_config == WPS_CONFIG_DISPLAY
|| wps_config == WPS_CONFIG_KEYPAD) {
if(pin)
strcpy(wps_pin_code, pin);
else{
printf("WPS: PIN is NULL. Not triger WPS.\n");
return -1;
}
}
if(!ssid) {
while (1) {
unsigned int current_time = rtw_get_current_time();
if (rtw_systime_to_sec(current_time - start_time) < 120) {
is_overlap = wps_find_out_triger_wps_AP(&target_ssid[0], wps_config);
if ((is_overlap == 0) || (is_overlap > 0))
break;
} else {
printf("WPS: WPS Walking Time Out\n");
return -2;
}
}
if (is_overlap > 0) {
printf("WPS: WPS session overlap. Not triger WPS.\n");
return -2;
}
}else{
rtw_memcpy(target_ssid, ssid, strlen(ssid));
}
if (queue_for_credential != NULL) {
os_xqueue_delete(queue_for_credential);
queue_for_credential = NULL;
}
queue_for_credential = os_xqueue_create(1, sizeof(struct dev_credential));
if(!queue_for_credential)
return -1;
wifi_reg_event_handler(WIFI_EVENT_STA_WPS_START, wpas_wsc_sta_wps_start_hdl, NULL);
wifi_reg_event_handler(WIFI_EVENT_WPS_FINISH, wpas_wsc_wps_finish_hdl, NULL);
wifi_reg_event_handler(WIFI_EVENT_EAPOL_RECVD, wpas_wsc_eapol_recvd_hdl, NULL);
wifi_set_wps_phase(ENABLE);
ret = wps_connect_to_AP_by_open_system(target_ssid);
if(ret < 0){
printf("WPS: WPS Fail!\n");
goto exit;
}
os_xqueue_receive(queue_for_credential, &dev_cred, 120);
if (dev_cred.ssid[0] != 0 && dev_cred.ssid_len <= 32) {
wps_config_wifi_setting(&wifi, &dev_cred);
wifi_set_wps_phase(DISABLE);
ret = wps_connect_to_AP_by_certificate(&wifi);
goto exit1;
} else {
printf("WPS: WPS FAIL!\n");
// printf("\n\rWPS: WPS FAIL!\n");
// printf("\n\rWPS: WPS FAIL!\n");
ret = -1;
}
exit:
wifi_set_wps_phase(DISABLE);
exit1:
if (queue_for_credential != NULL) {
os_xqueue_delete(queue_for_credential);
queue_for_credential = NULL;
}
wifi_unreg_event_handler(WIFI_EVENT_STA_WPS_START, wpas_wsc_sta_wps_start_hdl);
wifi_unreg_event_handler(WIFI_EVENT_WPS_FINISH, wpas_wsc_wps_finish_hdl);
wifi_unreg_event_handler(WIFI_EVENT_EAPOL_RECVD, wpas_wsc_eapol_recvd_hdl);
wpas_wps_deinit();
return ret;
}
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
static int ap_wps_start(u16 wps_config, char *pin)
{
u8 authorized_mac[ETH_ALEN];
int ret = 0;
u32 pin_val = 0;
if (queue_for_credential != NULL) {
os_xqueue_delete(queue_for_credential);
queue_for_credential = NULL;
}
queue_for_credential = os_xqueue_create(1, sizeof(authorized_mac));
if(!queue_for_credential)
return -1;
wifi_reg_event_handler(WIFI_EVENT_STA_WPS_START, wpas_wsc_sta_wps_start_hdl, NULL);
wifi_reg_event_handler(WIFI_EVENT_WPS_FINISH, wpas_wsc_wps_finish_hdl, NULL);
wifi_reg_event_handler(WIFI_EVENT_EAPOL_RECVD, wpas_wsc_eapol_recvd_hdl, NULL);
wifi_set_wps_phase(ENABLE);
if(wps_config == WPS_CONFIG_KEYPAD)
{
pin_val = atoi(pin);
if (!wps_pin_valid(pin_val)) {
printf("WPS-AP: Enter pin code is unvalid.\n");
goto exit;
}
ret = wpas_wps_registrar_add_pin((unsigned char const*)pin, strlen(pin));
}
else if(wps_config == WPS_CONFIG_DISPLAY)
ret = wpas_wps_registrar_add_pin((unsigned char const*)pin, strlen(pin));
else
ret = wpas_wps_registrar_button_pushed();
if(ret<0)
goto exit;
printf("WPS-AP: wait for STA connect!\n");
os_xqueue_receive(queue_for_credential, authorized_mac, 120); //max wait 2min
if(!wpas_wps_registrar_check_done())
{
ret = -1;
wpas_wps_registrar_wps_cancel();
}
exit:
wifi_set_wps_phase(0);
os_xqueue_delete(queue_for_credential);
queue_for_credential = NULL;
printf("WPS-AP: Finished!\n");
wifi_unreg_event_handler(WIFI_EVENT_STA_WPS_START, wpas_wsc_sta_wps_start_hdl);
wifi_unreg_event_handler(WIFI_EVENT_WPS_FINISH, wpas_wsc_wps_finish_hdl);
wifi_unreg_event_handler(WIFI_EVENT_EAPOL_RECVD, wpas_wsc_eapol_recvd_hdl);
return ret;
}
static void wifi_start_ap_wps_thread_hdl( void *param)
{
ap_wps_start(config_method, wps_pin_code); //Not support WPS_CONFIG_KEYPAD
ap_wps_task = NULL;
vTaskDelete(NULL);
}
void wifi_start_ap_wps_thread(u16 config_methods, char *pin)
{
if((config_methods != WPS_CONFIG_PUSHBUTTON)
&& (config_methods != WPS_CONFIG_DISPLAY)
&& (config_methods != WPS_CONFIG_KEYPAD)){
printf("WPS-AP: Wps method(%d) is wrong. Not triger WPS.\n", config_methods);
return;
}
config_method = config_methods;
if(config_methods == WPS_CONFIG_DISPLAY
|| config_methods == WPS_CONFIG_KEYPAD) {
if(pin)
strcpy(wps_pin_code, pin);
else{
printf("WPS-AP: PIN is NULL. Not triger WPS.\n");
return;
}
}
if(ap_wps_task != NULL){ //push item to wait queue to finish last ap_wps task
printf("WPS-AP: Wait for last ap_wps task exiting...\n");
if(queue_for_credential)
os_xqueue_send(queue_for_credential, NULL, 0);
while(ap_wps_task != NULL)
vTaskDelay(1);
vTaskDelay(20);
printf("Last ap_wps task completed.\n");
}
if(xTaskCreate(wifi_start_ap_wps_thread_hdl, ((const char*)"ap_wps"), 256, NULL, tskIDLE_PRIORITY + 3, &ap_wps_task) != pdPASS)
printf("%s xTaskCreate(ap_wps thread) failed\n", __FUNCTION__);
}
#endif //CONFIG_ENABLE_WPS_AP
void wps_judge_staion_disconnect(void)
{
int mode = 0;
unsigned char ssid[33];
wext_get_mode(WLAN0_NAME, &mode);
switch(mode) {
case IW_MODE_MASTER: //In AP mode
// rltk_wlan_deinit();
// rltk_wlan_init(0,RTW_MODE_STA);
// rltk_wlan_start(0);
//modified by Chris Yang for iNIC
wifi_off();
vTaskDelay(20);
wifi_on(RTW_MODE_STA);
break;
case IW_MODE_INFRA: //In STA mode
if(wext_get_ssid(WLAN0_NAME, ssid) > 0)
wifi_disconnect();
}
}
void cmd_wps(int argc, char **argv)
{
int ret = -1;
wps_judge_staion_disconnect();
if((argc == 2 || argc == 3 ) && (argv[1] != NULL)){
if(strcmp(argv[1],"pin") == 0){
unsigned int pin_val = 0;
/* start pin */
if(argc == 2){
char device_pin[10];
pin_val = wps_generate_pin();
sprintf(device_pin, "%08d", pin_val);
/* Display PIN 3 times to prevent to be overwritten by logs from other tasks */
printf("WPS: Start WPS PIN Display. PIN: [%s]\n", device_pin);
printf("WPS: Start WPS PIN Display. PIN: [%s]\n", device_pin);
printf("WPS: Start WPS PIN Display. PIN: [%s]\n", device_pin);
ret = wps_start(WPS_CONFIG_DISPLAY, (char*)device_pin, 0, NULL);
}else{
pin_val = atoi(argv[2]);
if (!wps_pin_valid(pin_val)) {
printf("WPS: Device pin code is invalid. Not triger WPS.\n");
goto exit;
}
printf("WPS: Start WPS PIN Keypad.\n");
ret = wps_start(WPS_CONFIG_KEYPAD, argv[2], 0, NULL);
}
}else if(strcmp(argv[1],"pbc") == 0){
/* start pbc */
printf("WPS: Start WPS PBC.\n");
ret = wps_start(WPS_CONFIG_PUSHBUTTON, NULL, 0, NULL);
}else{
printf("WPS: Wps Method is wrong. Not triger WPS.\n");
goto exit;
}
}
exit:
#if CONFIG_INIC_CMD_RSP
if(ret != 0)
inic_c2h_msg("ATWW", ret, NULL, 0);
#endif
return;
}
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
/*
cmd_ap_wps for AP WSC setting. command style:
cmd_ap_wps pbc or cmd_ap_wps pin 12345678
*/
void cmd_ap_wps(int argc, char **argv)
{
int mode = 0;
if(rltk_wlan_running(WLAN1_IDX)){
printf("Not support con-current softAP WSC!\n");
return;
}
wext_get_mode(WLAN0_NAME, &mode);
if(mode != IW_MODE_MASTER){
printf("Only valid for IW_MODE_MASTER!\n");
return;
}
if((argc == 2 || argc == 3) && (argv[1] != NULL)) {
if (strcmp(argv[1],"pin") == 0 ) {
unsigned int pin_val = 0;
if(argc == 3){
pin_val = atoi(argv[2]);
if (!wps_pin_valid(pin_val)) {
printf("WPS-AP: Device pin code is invalid. Not trigger WPS.\n");
return;
}
printf("WPS-AP: Start AP WPS PIN Keypad.\n");
wifi_start_ap_wps_thread(WPS_CONFIG_KEYPAD, argv[2]);
}else{
char device_pin[10];
pin_val = wps_generate_pin();
sprintf(device_pin, "%08d", pin_val);
printf("WPS: Start WPS PIN Display. PIN: %s\n", device_pin);
wifi_start_ap_wps_thread(WPS_CONFIG_DISPLAY, (char*)device_pin);
}
}else if (strcmp(argv[1],"pbc") == 0) {
printf("WPS-AP: Start AP WPS PBC\n");
wifi_start_ap_wps_thread(WPS_CONFIG_PUSHBUTTON, NULL);
}else{
printf("WPS-AP Usage:\"wifi_ap_wps pin [pin_code]\" or \"wifi_ap_wps pbc\"\n");
return;
}
} else {
printf("WPS-AP Usage:\"wifi_ap_wps pin [pin_code]\" or \"wifi_ap_wps pbc\"\n");
}
return;
}
#endif //CONFIG_ENABLE_P2P
#endif //CONFIG_ENABLE_WPS

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,712 @@
//----------------------------------------------------------------------------//
#ifndef __WIFI_CONF_API_H
#define __WIFI_CONF_API_H
#include "FreeRTOS.h"
#include "wifi_constants.h"
#include "wifi_structures.h"
#include "wifi_util.h"
#include "wifi_ind.h"
#include <platform/platform_stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************
* Macros
******************************************************/
#define RTW_ENABLE_API_INFO
#ifdef RTW_ENABLE_API_INFO
#define RTW_API_INFO(args) do {printf args;} while(0)
#else
#define RTW_API_INFO(args)
#endif
#define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
#define CMP_MAC( a, b ) (((a[0])==(b[0]))&& \
((a[1])==(b[1]))&& \
((a[2])==(b[2]))&& \
((a[3])==(b[3]))&& \
((a[4])==(b[4]))&& \
((a[5])==(b[5])))
/******************************************************
* Constants
******************************************************/
#define SCAN_LONGEST_WAIT_TIME (4500)
#define wifi_test_timeout_step_ms 20 // ms
#define wifi_test_timeout_ms 2000 // ms
#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
#define PSCAN_ENABLE 0x01 //enable for partial channel scan
#define PSCAN_FAST_SURVEY 0x02 //set to select scan time to FAST_SURVEY_TO, otherwise SURVEY_TO
#define PSCAN_SIMPLE_CONFIG 0x04 //set to select scan time to FAST_SURVEY_TO and resend probe request
/******************************************************
* Type Definitions
******************************************************/
/** Scan result callback function pointer type
*
* @param result_ptr : A pointer to the pointer that indicates where to put the next scan result
* @param user_data : User provided data
*/
typedef void (*rtw_scan_result_callback_t)( rtw_scan_result_t** result_ptr, void* user_data );
typedef rtw_result_t (*rtw_scan_result_handler_t)( rtw_scan_handler_result_t* malloced_scan_result );
/******************************************************
* Structures
******************************************************/
typedef struct {
char *buf;
int buf_len;
} scan_buf_arg;
/******************************************************
* Structures
******************************************************/
#define SCAN_USE_SEMAPHORE 0
typedef struct internal_scan_handler{
rtw_scan_result_t** pap_details;
rtw_scan_result_t * ap_details;
rtw_scan_result_handler_t gscan_result_handler;
#if SCAN_USE_SEMAPHORE
void * scan_semaphore;
#endif
// unsigned int scan_start_time;
void * user_data;
unsigned char scan_cnt;
unsigned char max_ap_size;
volatile unsigned char scan_complete;
volatile unsigned char scan_running;
} internal_scan_handler_t;
typedef struct {
rtw_network_info_t network_info;
void *join_sema;
} internal_join_result_t;
/******************************************************
* Function Declarations
******************************************************/
/**
* Initialises Realtek WiFi API System
*
* - Initialises the required parts of the software platform
* i.e. worker, event registering, semaphore, etc.
*
* - Initialises the RTW API thread which handles the asynchronous event
*
* @return RTW_SUCCESS if initialization is successful, RTW_ERROR otherwise
*/
int wifi_manager_init(void);
/** Joins a Wi-Fi network
*
* Scans for, associates and authenticates with a Wi-Fi network.
* On successful return, the system is ready to send data packets.
*
* @param[in] ssid : A null terminated string containing the SSID name of the network to join
* @param[in] security_type : Authentication type:
* - RTW_SECURITY_OPEN - Open Security
* - RTW_SECURITY_WEP_PSK - WEP Security with open authentication
* - RTW_SECURITY_WEP_SHARED - WEP Security with shared authentication
* - RTW_SECURITY_WPA_TKIP_PSK - WPA Security
* - RTW_SECURITY_WPA2_AES_PSK - WPA2 Security using AES cipher
* - RTW_SECURITY_WPA2_TKIP_PSK - WPA2 Security using TKIP cipher
* - RTW_SECURITY_WPA2_MIXED_PSK - WPA2 Security using AES and/or TKIP ciphers
* @param[in] password : A byte array containing either the
* cleartext security key for WPA/WPA2
* secured networks, or a pointer to
* an array of rtw_wep_key_t
* structures for WEP secured networks
* @param[in] ssid_len : The length of the SSID in
* bytes.
* @param[in] password_len : The length of the security_key in
* bytes.
* @param[in] key_id : The index of the wep key.
* @param[in] semaphore : A user provided semaphore that is flagged when the join is complete
*
* @return RTW_SUCCESS : when the system is joined and ready
* to send data packets
* RTW_ERROR : if an error occurred
*/
int wifi_connect(
unsigned char bssid[ETH_ALEN],
char use_bssid,
char *ssid,
rtw_security_t security_type,
char *password,
int key_id,
void *semaphore);
/*
int wifi_connect_bssid(
unsigned char bssid[ETH_ALEN],
char *ssid,
rtw_security_t security_type,
char *password,
int bssid_len,
int ssid_len,
int password_len,
int key_id,
void *semaphore);
*/
/** Disassociates from a Wi-Fi network.
*
* @return RTW_SUCCESS : On successful disassociation from
* the AP
* RTW_ERROR : If an error occurred
*/
int wifi_disconnect(void);
/** Check if the interface specified is up.
*
* @return RTW_TRUE : If it's up
* RTW_FALSE : If it's not
*/
int wifi_is_connected_to_ap(void);
/*check if wifi has connected to AP before dhcp
*
* @return RTW_SUCCESS:if conneced
RTW_ERROR :if not connect
*/
int wifi_is_up(rtw_interface_t interface);
/** Determines if a particular interface is ready to transceive ethernet packets
*
* @param Radio interface to check, options are
* RTW_STA_INTERFACE, RTW_AP_INTERFACE
* @return RTW_SUCCESS : if the interface is ready to
* transceive ethernet packets
* @return RTW_NOTFOUND : no AP with a matching SSID was
* found
* @return RTW_NOT_AUTHENTICATED: a matching AP was found but
* it won't let you
* authenticate. This can
* occur if this device is
* in the block list on the
* AP.
* @return RTW_NOT_KEYED: the device has authenticated and
* associated but has not completed
* the key exchange. This can occur
* if the passphrase is incorrect.
* @return RTW_ERROR : if the interface is not ready to
* transceive ethernet packets
*/
int wifi_is_ready_to_transceive(rtw_interface_t interface);
/** ----------------------------------------------------------------------
* WARNING : This function is for internal use only!
* ----------------------------------------------------------------------
* This function sets the current Media Access Control (MAC) address of the
* 802.11 device.
*
* @param[in] mac Wi-Fi MAC address
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_set_mac_address(char * mac);
/** Retrieves the current Media Access Control (MAC) address
* (or Ethernet hardware address) of the 802.11 device
*
* @param mac Pointer to a variable that the current MAC address will be written to
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_get_mac_address(char * mac);
/** Enables powersave mode
*
* @return @ref rtw_result_t
*/
int wifi_enable_powersave(void);
/** Disables 802.11 power save mode
*
* @return RTW_SUCCESS : if power save mode was successfully
* disabled
* RTW_ERROR : if power save mode was not successfully
* disabled
*/
int wifi_disable_powersave(void);
/** Gets the tx power in index units
*
* @param dbm : The variable to receive the tx power in index.
*
* @return RTW_SUCCESS : if successful
* RTW_ERROR : if not successful
*/
int wifi_get_txpower(int *poweridx);
/** Sets the tx power in index units
*
* @param dbm : The desired tx power in index.
*
* @return RTW_SUCCESS : if tx power was successfully set
* RTW_ERROR : if tx power was not successfully set
*/
int wifi_set_txpower(int poweridx);
/** Get the associated clients with SoftAP
*
* @param client_list_buffer : the location where the client
* list will be stored
* @param buffer_length : the buffer length.
*
* @return RTW_SUCCESS : if result was successfully get
* RTW_ERROR : if result was not successfully get
*/
int wifi_get_associated_client_list(void * client_list_buffer, unsigned short buffer_length);
/** Get the SoftAP information
*
* @param ap_info : the location where the AP info will be
* stored
* @param security : the security type.
*
* @return RTW_SUCCESS : if result was successfully get
* RTW_ERROR : if result was not successfully get
*/
int wifi_get_ap_info(rtw_bss_info_t * ap_info, rtw_security_t* security);
/** Set the country code to driver to determine the channel set
*
* @param country_code : the country code.
*
* @return RTW_SUCCESS : if result was successfully set
* RTW_ERROR : if result was not successfully set
*/
int wifi_set_country(rtw_country_code_t country_code);
/** Retrieve the latest RSSI value
*
* @param rssi: The location where the RSSI value will be stored
*
* @return RTW_SUCCESS : if the RSSI was succesfully retrieved
* RTW_ERROR : if the RSSI was not retrieved
*/
int wifi_get_rssi(int *pRSSI);
/** Set the current channel on STA interface
*
* @param channel : The desired channel
*
* @return RTW_SUCCESS : if the channel was successfully set
* RTW_ERROR : if the channel was not successfully
* set
*/
int wifi_set_channel(int channel);
/** Get the current channel on STA interface
*
* @param channel : A pointer to the variable where the
* channel value will be written
*
* @return RTW_SUCCESS : if the channel was successfully read
* RTW_ERROR : if the channel was not successfully
* read
*/
int wifi_get_channel(int *channel);
/** Registers interest in a multicast address
* Once a multicast address has been registered, all packets detected on the
* medium destined for that address are forwarded to the host.
* Otherwise they are ignored.
*
* @param mac: Ethernet MAC address
*
* @return RTW_SUCCESS : if the address was registered
* successfully
* RTW_ERROR : if the address was not registered
*/
int wifi_register_multicast_address(rtw_mac_t *mac);
/** Unregisters interest in a multicast address
* Once a multicast address has been unregistered, all packets detected on the
* medium destined for that address are ignored.
*
* @param mac: Ethernet MAC address
*
* @return RTW_SUCCESS : if the address was unregistered
* successfully
* RTW_ERROR : if the address was not unregistered
*/
int wifi_unregister_multicast_address(rtw_mac_t *mac);
int wifi_rf_on(void);
int wifi_rf_off(void);
/** Turn on the Wi-Fi device
*
* - Bring the Wireless interface "Up"
* - Initialises the driver thread which arbitrates access
* to the SDIO/SPI bus
*
* @param mode: wifi work mode
*
* @return RTW_SUCCESS : if the WiFi chip was initialised
* successfully
* RTW_ERROR : if the WiFi chip was not initialised
* successfully
*/
int wifi_on(rtw_mode_t mode);
/**
* Turn off the Wi-Fi device
*
* - Bring the Wireless interface "Down"
* - De-Initialises the driver thread which arbitrates access
* to the SDIO/SPI bus
*
* @return RTW_SUCCESS if deinitialization is successful,
* RTW_ERROR otherwise
*/
int wifi_off(void);
/**
* Set IPS/LPS mode
*
* @param[in] ips_mode : The desired IPS mode. It become effective when wlan enter ips.
* @param[in] lps_mode : The desired LPS mode. It become effective when wlan enter lps.
*
* @return RTW_SUCCESS if setting LPS mode successful
* RTW_ERROR otherwise
*/
int wifi_set_power_mode(unsigned char ips_mode, unsigned char lps_mode);
/**
* Set TDMA parameters
*
* @param[in] slot_period : We separate TBTT into 2 or 3 slots.
* If we separate TBTT into 2 slots, then slot_period should be larger or equal to 50ms.
* It means 2 slot period is
* slot_period, 100-slot_period
* If we separate TBTT into 3 slots, then slot_period should be less or equal to 33ms.
* It means 3 slot period is
* 100 - 2 * slot_period, slot_period, slot_period
* @param[in] rfon_period_len_1: rf on period of slot 1
* @param[in] rfon_period_len_2: rf on period of slot 2
* @param[in] rfon_period_len_3: rf on period of slot 3
*
* @return RTW_SUCCESS if setting TDMA parameters successful
* RTW_ERROR otherwise
*/
int wifi_set_tdma_param(unsigned char slot_period, unsigned char rfon_period_len_1, unsigned char rfon_period_len_2, unsigned char rfon_period_len_3);
/**
* Set LPS DTIM
*
* @param[in] dtim : In LPS, the package can be buffered at AP side.
* STA leave LPS until dtim count of packages buffered at AP side.
*
* @return RTW_SUCCESS if setting LPS dtim successful
* RTW_ERROR otherwise
*/
int wifi_set_lps_dtim(unsigned char dtim);
/**
* Get LPS DTIM
*
* @param[out] dtim : In LPS, the package can be buffered at AP side.
* STA leave LPS until dtim count of packages buffered at AP side.
*
* @return RTW_SUCCESS if getting LPS dtim successful
* RTW_ERROR otherwise
*/
int wifi_get_lps_dtim(unsigned char *dtim);
/** Starts an infrastructure WiFi network
*
* @warning If a STA interface is active when this function is called, the softAP will\n
* start on the same channel as the STA. It will NOT use the channel provided!
*
* @param[in] ssid : A null terminated string containing
* the SSID name of the network to join
* @param[in] security_type : Authentication type: \n
* - RTW_SECURITY_OPEN - Open Security \n
* - RTW_SECURITY_WPA_TKIP_PSK - WPA Security \n
* - RTW_SECURITY_WPA2_AES_PSK - WPA2 Security using AES cipher \n
* - RTW_SECURITY_WPA2_MIXED_PSK - WPA2 Security using AES and/or TKIP ciphers \n
* - WEP security is NOT IMPLEMENTED. It is NOT SECURE! \n
* @param[in] password : A byte array containing the cleartext
* security key for the network
* @param[in] ssid_len : The length of the SSID in
* bytes.
* @param[in] password_len : The length of the security_key in
* bytes.
* @param[in] channel : 802.11 channel number
*
* @return RTW_SUCCESS : if successfully creates an AP
* RTW_ERROR : if an error occurred
*/
int wifi_start_ap(char *ssid, rtw_security_t security_type, char *password, int channel, char ssid_hidden);
/*
int wifi_start_ap(
char *ssid,
rtw_security_t security_type,
char *password,
int ssid_len,
int password_len,
int channel);
*/
/** Starts an infrastructure WiFi network with hidden SSID
*
* @warning If a STA interface is active when this function is called, the softAP will\n
* start on the same channel as the STA. It will NOT use the channel provided!
*
* @param[in] ssid : A null terminated string containing
* the SSID name of the network to join
* @param[in] security_type : Authentication type: \n
* - RTW_SECURITY_OPEN - Open Security \n
* - RTW_SECURITY_WPA_TKIP_PSK - WPA Security \n
* - RTW_SECURITY_WPA2_AES_PSK - WPA2 Security using AES cipher \n
* - RTW_SECURITY_WPA2_MIXED_PSK - WPA2 Security using AES and/or TKIP ciphers \n
* - WEP security is NOT IMPLEMENTED. It is NOT SECURE! \n
* @param[in] password : A byte array containing the cleartext
* security key for the network
* @param[in] ssid_len : The length of the SSID in
* bytes.
* @param[in] password_len : The length of the security_key in
* bytes.
* @param[in] channel : 802.11 channel number
*
* @return RTW_SUCCESS : if successfully creates an AP
* RTW_ERROR : if an error occurred
*/
/*
int wifi_start_ap_with_hidden_ssid(
char *ssid,
rtw_security_t security_type,
char *password,
int ssid_len,
int password_len,
int channel);
*/
/** Initiates a scan to search for 802.11 networks.
*
* The scan progressively accumulates results over time, and
* may take between 1 and 3 seconds to complete. The results of
* the scan will be individually provided to the callback
* function. Note: The callback function will be executed in
* the context of the RTW thread.
*
* @param[in] scan_type : Specifies whether the scan should
* be Active, Passive or scan
* Prohibited channels
* @param[in] bss_type : Specifies whether the scan should
* search for Infrastructure
* networks (those using an Access
* Point), Ad-hoc networks, or both
* types.
* @param result_ptr[in] : Scan specific ssid. The first 4
* bytes is ssid lenth, and ssid name
* append after it.
* If no specific ssid need to scan,
* PLEASE CLEAN result_ptr before pass
* it into parameter.
* @param result_ptr[out] : a pointer to a pointer to a result
* storage structure.
*
* @note : When scanning specific channels, devices with a
* strong signal strength on nearby channels may be
* detected
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_scan(rtw_scan_type_t scan_type,
rtw_bss_type_t bss_type,
void* result_ptr);
/** Initiates a scan to search for 802.11 networks, a higher
* level API based on wifi_scan to simplify the scan
* operation.
*
* The scan results will be list by the order of RSSI.
* It may demand hundreds bytes memory during scan
* processing according to the quantity of AP nearby.
*
* @param results_handler[in] : the callback function which
* will receive and process the result data.
* @param user_data[in] : user specific data that will be
* passed directly to the callback function
*
* @note : Callback must not use blocking functions, since it is
* called from the context of the RTW thread.
* @note : The callback, user_data variables will
* be referenced after the function returns. Those
* variables must remain valid until the scan is
* complete.
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_scan_networks(rtw_scan_result_handler_t results_handler, void* user_data);
int wifi_scan_networks_with_ssid(int (results_handler)(char*, int, char *, void *), void* user_data, int scan_buflen, char* ssid, int ssid_len);
/** Set the partical scan
*
* @param channel_list[in] : the channel set the scan will
* stay on
* @param pscan_config[in] : the pscan_config of the channel set
*
* @param length[in] : the channel list length
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_set_pscan_chan(__u8 * channel_list,__u8 * pscan_config, __u8 length);
/** Get the network information
*
* @param ifname[in] : the name of the interface we are care
* @param pSetting[in] : the location where the network
* information will be stored
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_get_setting(const char *ifname, rtw_wifi_setting_t *pSetting);
/** Show the network information
*
* @param ifname[in] : the name of the interface we are care
* @param pSetting[in] : the location where the network
* information was stored
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_show_setting(const char *ifname,rtw_wifi_setting_t *pSetting);
/** Set the network mode according to the data rate it's
* supported
*
* @param mode[in] : the network mode
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_set_network_mode(rtw_network_mode_t mode);
/** Set the chip to worke in the promisc mode
*
* @param enabled[in] : enabled can be set 0, 1 and 2. if enabled is zero, disable the promisc, else enable the promisc.
* 0 means disable the promisc
* 1 means enable the promisc
* 2 means enable the promisc special for length is used
* @param callback[in] : the callback function which will
* receive and process the netowork data.
* @param len_used[in] : specify if the the promisc length is
* used.
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_set_promisc(rtw_rcr_level_t enabled, void (*callback)(unsigned char*, unsigned int, void*), unsigned char len_used);
/** Set the wps phase
*
* @param is_trigger_wps[in] : to trigger wps function or not
*
* @return RTW_SUCCESS or RTW_ERROR
*/
int wifi_set_wps_phase(unsigned char is_trigger_wps);
/** Restarts an infrastructure WiFi network
*
* @warning If a STA interface is active when this function is called, the softAP will\n
* start on the same channel as the STA. It will NOT use the channel provided!
*
* @param[in] ssid : A null terminated string containing
* the SSID name of the network to join
* @param[in] security_type : Authentication type: \n
* - RTW_SECURITY_OPEN - Open Security \n
* - RTW_SECURITY_WPA_TKIP_PSK - WPA Security \n
* - RTW_SECURITY_WPA2_AES_PSK - WPA2 Security using AES cipher \n
* - RTW_SECURITY_WPA2_MIXED_PSK - WPA2 Security using AES and/or TKIP ciphers \n
* - WEP security is NOT IMPLEMENTED. It is NOT SECURE! \n
* @param[in] password : A byte array containing the cleartext
* security key for the network
* @param[in] ssid_len : The length of the SSID in
* bytes.
* @param[in] password_len : The length of the security_key in
* bytes.
* @param[in] channel : 802.11 channel number
*
* @return RTW_SUCCESS : if successfully creates an AP
* RTW_ERROR : if an error occurred
*/
int wifi_restart_ap(
unsigned char *ssid,
rtw_security_t security_type,
unsigned char *password,
int channel);
int wifi_config_autoreconnect(__u8 mode, __u8 retyr_times, __u16 timeout);
int wifi_set_autoreconnect(__u8 mode);
int wifi_get_autoreconnect(__u8 *mode);
int wifi_get_last_error(void);
/** Present device disconnect reason while connecting
*
*@return RTW_NO_ERROR = 0,
* RTW_NONE_NETWORK = 1,
* RTW_CONNECT_FAIL = 2,
* RTW_WRONG_PASSWORD = 3 ,
* RTW_DHCP_FAIL = 4,
* RTW_UNKNOWN, initial status
*/
#ifdef CONFIG_CUSTOM_IE
#ifndef BIT
#define BIT(x) ((__u32)1 << (x))
#endif
#ifndef _CUSTOM_IE_TYPE_
#define _CUSTOM_IE_TYPE_
enum CUSTOM_IE_TYPE{
PROBE_REQ = BIT(0),
PROBE_RSP = BIT(1),
BEACON = BIT(2),
};
#endif /* _CUSTOM_IE_TYPE_ */
/* ie format
* +-----------+--------+-----------------------+
* |element ID | length | content in length byte|
* +-----------+--------+-----------------------+
*
* type: refer to CUSTOM_IE_TYPE
*/
#ifndef _CUS_IE_
#define _CUS_IE_
typedef struct _cus_ie{
__u8 *ie;
__u8 type;
} cus_ie, *p_cus_ie;
#endif /* _CUS_IE_ */
int wifi_add_custom_ie(void *cus_ie, int ie_num);
int wifi_update_custom_ie(void *cus_ie, int ie_index);
int wifi_del_custom_ie(void);
#endif
#ifdef CONFIG_PROMISC
void wifi_init_packet_filter(void);
int wifi_add_packet_filter(unsigned char filter_id, rtw_packet_filter_pattern_t *patt, rtw_packet_filter_rule_e rule);
int wifi_enable_packet_filter(unsigned char filter_id);
int wifi_disable_packet_filter(unsigned char filter_id);
int wifi_remove_packet_filter(unsigned char filter_id);
#endif
#ifdef __cplusplus
}
#endif
#endif // __WIFI_CONF_API_H
//----------------------------------------------------------------------------//

View file

@ -0,0 +1,269 @@
//----------------------------------------------------------------------------//
#include "wifi/wifi_ind.h"
#include "wifi/wifi_conf.h"
#include "osdep_service.h"
#include "platform_stdlib.h"
/******************************************************
* Constants
******************************************************/
#if CONFIG_DEBUG_LOG > 3
#define WIFI_INDICATE_MSG 1
#else
#define WIFI_INDICATE_MSG 0
#endif
#define WIFI_MANAGER_STACKSIZE 400 // 1300
#define WIFI_MANAGER_PRIORITY (0) //Actual priority is 4 since calling rtw_create_task
#define WIFI_MANAGER_Q_SZ 8
#define WIFI_EVENT_MAX_ROW 3
/******************************************************
* Globals
******************************************************/
static event_list_elem_t event_callback_list[WIFI_EVENT_MAX][WIFI_EVENT_MAX_ROW];
#if CONFIG_WIFI_IND_USE_THREAD
static rtw_worker_thread_t wifi_worker_thread;
#endif
//----------------------------------------------------------------------------//
#if CONFIG_WIFI_IND_USE_THREAD
static rtw_result_t rtw_send_event_to_worker(int event_cmd, char *buf,
int buf_len, int flags) {
rtw_event_message_t message;
int i;
rtw_result_t ret = RTW_SUCCESS;
char *local_buf = NULL;
if (event_cmd >= WIFI_EVENT_MAX)
return RTW_BADARG;
for (i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
if (event_callback_list[event_cmd][i].handler == NULL)
continue;
message.function =
(event_handler_t) event_callback_list[event_cmd][i].handler;
message.buf_len = buf_len;
if (buf_len) {
local_buf = (char*) pvPortMalloc(buf_len);
if (local_buf == NULL)
return RTW_NOMEM;
memcpy(local_buf, buf, buf_len);
//debug_printf("Allocate %p(%d) for evcmd %d\n", local_buf, buf_len, event_cmd);
}
message.buf = local_buf;
message.flags = flags;
message.user_data = event_callback_list[event_cmd][i].handler_user_data;
ret = rtw_push_to_xqueue(&wifi_worker_thread.event_queue, &message, 0);
if (ret != RTW_SUCCESS) {
if (local_buf) {
warning_printf(
"rtw_send_event_to_worker: enqueue cmd %d failed and free %p(%d)\n",
event_cmd, local_buf, buf_len);
vPortFree(local_buf);
}
break;
}
}
return ret;
}
#else
static rtw_result_t rtw_indicate_event_handle(int event_cmd, char *buf, int buf_len, int flags)
{
rtw_event_handler_t handle = NULL;
int i;
if(event_cmd >= WIFI_EVENT_MAX)
return RTW_BADARG;
for(i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
handle = event_callback_list[event_cmd][i].handler;
if(handle == NULL)
continue;
handle(buf, buf_len, flags, event_callback_list[event_cmd][i].handler_user_data);
}
return RTW_SUCCESS;
}
#endif
#if 0 // test beacon
#include "gpio_api.h" // mbed
extern gpio_t gpio_led;
#endif
void wifi_indication(WIFI_EVENT_INDICATE event, unsigned char *buf, int buf_len,
int flags) {
//
// If upper layer application triggers additional operations on receiving of wext_wlan_indicate,
// please strictly check current stack size usage (by using uxTaskGetStackHighWaterMark() )
// , and tries not to share the same stack with wlan driver if remaining stack space is
// not available for the following operations.
// ex: using semaphore to notice another thread.
switch (event) {
case WIFI_EVENT_DISCONNECT:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): Disconnection indication received\n", __func__, event);
#endif
break;
case WIFI_EVENT_CONNECT:
// For WPA/WPA2 mode, indication of connection does not mean data can be
// correctly transmitted or received. Data can be correctly transmitted or
// received only when 4-way handshake is done.
// Please check WIFI_EVENT_FOURWAY_HANDSHAKE_DONE event
#if(WIFI_INDICATE_MSG>0)
// Sample: return mac address
if (buf != NULL && buf_len == 6) {
info_printf(
"%s(%d): Connect indication received: %02x:%02x:%02x:%02x:%02x:%02x\n",
__func__, event, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
}
#endif
break;
case WIFI_EVENT_FOURWAY_HANDSHAKE_DONE:
#if(WIFI_INDICATE_MSG>0)
if (buf != NULL)
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
case WIFI_EVENT_SCAN_RESULT_REPORT:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_SCAN_RESULT_REPORT\n", __func__, event);
#endif
break;
case WIFI_EVENT_SCAN_DONE:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_SCAN_DONE\n", __func__, event);
//#if CONFIG_DEBUG_LOG > 3
// debug_printf("Time at start %d ms.\n", xTaskGetTickCount());
//#endif
#endif
break;
case WIFI_EVENT_RECONNECTION_FAIL:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
case WIFI_EVENT_NO_NETWORK:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
#if CONFIG_ENABLE_P2P
case WIFI_EVENT_SEND_ACTION_DONE:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
case WIFI_EVENT_RX_MGNT:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_RX_MGNT\n", __func__, event);
#endif
break;
#endif //CONFIG_ENABLE_P2P
case WIFI_EVENT_STA_ASSOC:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_STA_ASSOC\n", __func__, event);
#endif
break;
case WIFI_EVENT_STA_DISASSOC:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_STA_DISASSOC\n", __func__, event);
#endif
break;
#ifdef CONFIG_WPS
case WIFI_EVENT_STA_WPS_START:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_STA_WPS_START\n", __func__, event);
#endif
break;
case WIFI_EVENT_WPS_FINISH:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_WPS_FINISH\n", __func__, event);
#endif
break;
case WIFI_EVENT_EAPOL_RECVD:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s(%d): WIFI_EVENT_EAPOL_RECVD\n", __func__, event);
#endif
break;
#endif
case WIFI_EVENT_BEACON_AFTER_DHCP:
#if(WIFI_INDICATE_MSG>1)
info_printf("%s(%d): WIFI_EVENT_BEACON_AFTER_DHCP\n", __func__, event);
#endif
#if 0 // test beacon
gpio_write(&gpio_led, 1);
gpio_write(&gpio_led, 0);
#endif
break;
}
#if CONFIG_INIC_EN
inic_indicate_event(event, buf, buf_len, flags);
#endif//CONFIG_INIC_EN
#if CONFIG_WIFI_IND_USE_THREAD
rtw_send_event_to_worker(event, buf, buf_len, flags);
#else
rtw_indicate_event_handle(event, buf, buf_len, flags);
#endif
}
void wifi_reg_event_handler(unsigned int event_cmds,
rtw_event_handler_t handler_func, void *handler_user_data) {
int i = 0, j = 0;
if (event_cmds < WIFI_EVENT_MAX) {
for (i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
if (event_callback_list[event_cmds][i].handler == NULL) {
for (j = 0; j < WIFI_EVENT_MAX_ROW; j++) {
if (event_callback_list[event_cmds][j].handler
== handler_func) {
return;
}
}
event_callback_list[event_cmds][i].handler = handler_func;
event_callback_list[event_cmds][i].handler_user_data =
handler_user_data;
return;
}
}
}
}
void wifi_unreg_event_handler(unsigned int event_cmds,
rtw_event_handler_t handler_func) {
int i;
if (event_cmds < WIFI_EVENT_MAX) {
for (i = 0; i < WIFI_EVENT_MAX_ROW; i++) {
if (event_callback_list[event_cmds][i].handler == handler_func) {
event_callback_list[event_cmds][i].handler = NULL;
event_callback_list[event_cmds][i].handler_user_data = NULL;
return;
}
}
}
}
void init_event_callback_list() {
memset(event_callback_list, 0, sizeof(event_callback_list));
}
int wifi_manager_init() {
#if CONFIG_WIFI_IND_USE_THREAD
rtw_create_worker_thread(&wifi_worker_thread,
WIFI_MANAGER_PRIORITY,
WIFI_MANAGER_STACKSIZE,
WIFI_MANAGER_Q_SZ);
#endif
return 0;
}
void rtw_wifi_manager_deinit() {
#if CONFIG_WIFI_IND_USE_THREAD
rtw_delete_worker_thread(&wifi_worker_thread);
#endif
}

View file

@ -0,0 +1,34 @@
#ifndef _WIFI_INDICATE_H
#define _WIFI_INDICATE_H
#include "wifi_conf.h"
typedef void (*rtw_event_handler_t)(char *buf, int buf_len, int flags, void* handler_user_data );
typedef struct
{
// WIFI_EVENT_INDICATE event_cmd;
rtw_event_handler_t handler;
void* handler_user_data;
} event_list_elem_t;
void init_event_callback_list(void);
extern void wifi_indication( rtw_event_indicate_t event, unsigned char *buf, int buf_len, int flags);
/** Register the event listener
*
* @param[in] event_cmds : The event command number indicated
* @param[in] handler_func : the callback function which will
* receive and process the event
* @param[in] handler_user_data : user specific data that will be
* passed directly to the callback function
*
* @note : Set the same event_cmds with empty handler_func will
* unregister the event_cmds
*
* @return RTW_SUCCESS : if successfully registers the event
* RTW_ERROR : if an error occurred
*/
extern void wifi_reg_event_handler(unsigned int event_cmds, rtw_event_handler_t handler_func, void *handler_user_data);
extern void wifi_unreg_event_handler(unsigned int event_cmds, rtw_event_handler_t handler_func);
#endif //_WIFI_INDICATE_H

View file

@ -0,0 +1,475 @@
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "tcpip.h"
#include "wifi/wifi_conf.h"
#include "wifi_simple_config_parser.h"
#ifndef CONFIG_WLAN
#define CONFIG_WLAN 1
#endif
#if CONFIG_WLAN
#include <platform/platform_stdlib.h>
#ifdef CONFIG_PROMISC
#define _adapter void
#define recv_frame void
extern void _promisc_deinit(_adapter *padapter);
extern int _promisc_recv_func(_adapter *padapter, recv_frame *rframe);
extern int _promisc_set(rtw_rcr_level_t enabled, void (*callback)(unsigned char *, unsigned int, void *), unsigned char len_used);
extern unsigned char is_promisc_enabled(void);
extern int promisc_get_fixed_channel(void *fixed_bssid, unsigned char *ssid, int *ssid_length);
extern unsigned char is_promisc_enabled(void);
#endif
// Add extra interfaces to make release sdk able to determine promisc API linking
void promisc_deinit(void *padapter)
{
#ifdef CONFIG_PROMISC
_promisc_deinit(padapter);
#endif
}
int promisc_recv_func(void *padapter, void *rframe)
{
// Never reach here if not define CONFIG_PROMISC
#ifdef CONFIG_PROMISC
return _promisc_recv_func(padapter, rframe);
#else
return 0;
#endif
}
int promisc_set(rtw_rcr_level_t enabled, void (*callback)(unsigned char*, unsigned int, void*), unsigned char len_used)
{
#ifdef CONFIG_PROMISC
return _promisc_set(enabled, callback, len_used);
#else
return -1;
#endif
}
unsigned char is_promisc_enabled(void)
{
#ifdef CONFIG_PROMISC
return _is_promisc_enabled();
#else
return 0;
#endif
}
int promisc_get_fixed_channel(void *fixed_bssid, u8 *ssid, int *ssid_length)
{
#ifdef CONFIG_PROMISC
return _promisc_get_fixed_channel(fixed_bssid, ssid, ssid_length);
#else
return 0;
#endif
}
// End of Add extra interfaces
struct eth_frame {
struct eth_frame *prev;
struct eth_frame *next;
unsigned char da[6];
unsigned char sa[6];
unsigned int len;
unsigned char type;
signed char rssi;
};
#if CONFIG_INIC_CMD_RSP
#if defined(__IAR_SYSTEMS_ICC__)
#pragma pack(1)
#endif
struct inic_eth_frame {
unsigned char da[6];
unsigned char sa[6];
unsigned int len;
unsigned char type;
};
#if defined(__IAR_SYSTEMS_ICC__)
#pragma pack()
#endif
static struct inic_eth_frame *inic_frame, *inic_frame_tail = NULL;
static int inic_frame_cnt = 0;
#define MAX_INIC_FRAME_NUM 50 //maximum packets for each channel
extern void inic_c2h_msg(const char *atcmd, char status, char *msg, u16 msg_len);
#endif
struct eth_buffer {
struct eth_frame *head;
struct eth_frame *tail;
};
static struct eth_buffer eth_buffer;
#ifdef CONFIG_PROMISC
#define MAX_PACKET_FILTER_INFO 5
#define FILTER_ID_INIT_VALUE 10
rtw_packet_filter_info_t paff_array[MAX_PACKET_FILTER_INFO]={0, 0, 0, 0, 0};
static u8 packet_filter_enable_num = 0;
void promisc_init_packet_filter()
{
int i = 0;
for(i=0; i<MAX_PACKET_FILTER_INFO; i++){
paff_array[i].filter_id = FILTER_ID_INIT_VALUE;
paff_array[i].enable = 0;
paff_array[i].patt.mask_size = 0;
paff_array[i].rule = RTW_POSITIVE_MATCHING;
paff_array[i].patt.mask = NULL;
paff_array[i].patt.pattern = NULL;
}
packet_filter_enable_num = 0;
}
int promisc_add_packet_filter(u8 filter_id, rtw_packet_filter_pattern_t *patt, rtw_packet_filter_rule_e rule)
{
int i = 0;
while(i < MAX_PACKET_FILTER_INFO){
if(paff_array[i].filter_id == FILTER_ID_INIT_VALUE){
break;
}
i++;
}
if(i == MAX_PACKET_FILTER_INFO)
return -1;
paff_array[i].filter_id = filter_id;
paff_array[i].patt.offset= patt->offset;
paff_array[i].patt.mask_size = patt->mask_size;
paff_array[i].patt.mask = pvPortMalloc(patt->mask_size);
memcpy(paff_array[i].patt.mask, patt->mask, patt->mask_size);
paff_array[i].patt.pattern= pvPortMalloc(patt->mask_size);
memcpy(paff_array[i].patt.pattern, patt->pattern, patt->mask_size);
paff_array[i].rule = rule;
paff_array[i].enable = 0;
return 0;
}
int promisc_enable_packet_filter(u8 filter_id)
{
int i = 0;
while(i < MAX_PACKET_FILTER_INFO){
if(paff_array[i].filter_id == filter_id)
break;
i++;
}
if(i == MAX_PACKET_FILTER_INFO)
return -1;
paff_array[i].enable = 1;
packet_filter_enable_num++;
return 0;
}
int promisc_disable_packet_filter(u8 filter_id)
{
int i = 0;
while(i < MAX_PACKET_FILTER_INFO){
if(paff_array[i].filter_id == filter_id)
break;
i++;
}
if(i == MAX_PACKET_FILTER_INFO)
return -1;
paff_array[i].enable = 0;
packet_filter_enable_num--;
return 0;
}
int promisc_remove_packet_filter(u8 filter_id)
{
int i = 0;
while(i < MAX_PACKET_FILTER_INFO){
if(paff_array[i].filter_id == filter_id)
break;
i++;
}
if(i == MAX_PACKET_FILTER_INFO)
return -1;
paff_array[i].filter_id = FILTER_ID_INIT_VALUE;
paff_array[i].enable = 0;
paff_array[i].patt.mask_size = 0;
paff_array[i].rule = 0;
if(paff_array[i].patt.mask){
vPortFree(paff_array[i].patt.mask);
paff_array[i].patt.mask = NULL;
}
if(paff_array[i].patt.pattern){
vPortFree(paff_array[i].patt.pattern);
paff_array[i].patt.pattern = NULL;
}
return 0;
}
#endif
/* Make callback simple to prevent latency to wlan rx when promiscuous mode */
static void promisc_callback(unsigned char *buf, unsigned int len, void* userdata)
{
struct eth_frame *frame = (struct eth_frame *) pvPortMalloc(sizeof(struct eth_frame));
if(frame) {
frame->prev = NULL;
frame->next = NULL;
memcpy(frame->da, buf, 6);
memcpy(frame->sa, buf + 6, 6);
frame->len = len;
frame->rssi = ((ieee80211_frame_info_t *)userdata)->rssi;
taskENTER_CRITICAL();
if(eth_buffer.tail) {
eth_buffer.tail->next = frame;
frame->prev = eth_buffer.tail;
eth_buffer.tail = frame;
}
else {
eth_buffer.head = frame;
eth_buffer.tail = frame;
}
taskEXIT_CRITICAL();
}
}
struct eth_frame* retrieve_frame(void)
{
struct eth_frame *frame = NULL;
taskENTER_CRITICAL();
if(eth_buffer.head) {
frame = eth_buffer.head;
if(eth_buffer.head->next) {
eth_buffer.head = eth_buffer.head->next;
eth_buffer.head->prev = NULL;
}
else {
eth_buffer.head = NULL;
eth_buffer.tail = NULL;
}
}
taskEXIT_CRITICAL();
return frame;
}
static void promisc_test(int duration, unsigned char len_used)
{
int ch;
unsigned int start_time;
struct eth_frame *frame;
eth_buffer.head = NULL;
eth_buffer.tail = NULL;
wifi_enter_promisc_mode();
wifi_set_promisc(RTW_PROMISC_ENABLE, promisc_callback, len_used);
for(ch = 1; ch <= 13; ch ++) {
if(wifi_set_channel(ch) == 0)
printf("\n\n\rSwitch to channel(%d)", ch);
start_time = xTaskGetTickCount();
while(1) {
unsigned int current_time = xTaskGetTickCount();
if((current_time - start_time) < (duration)) { // duration * configTICK_RATE_HZ
frame = retrieve_frame();
if(frame) {
int i;
printf("\n\rDA:");
for(i = 0; i < 6; i ++)
printf(" %02x", frame->da[i]);
printf(", SA:");
for(i = 0; i < 6; i ++)
printf(" %02x", frame->sa[i]);
printf(", len=%d", frame->len);
printf(", RSSI=%d", frame->rssi);
#if CONFIG_INIC_CMD_RSP
if(inic_frame_tail){
if(inic_frame_cnt < MAX_INIC_FRAME_NUM){
memcpy(inic_frame_tail->da, frame->da, 6);
memcpy(inic_frame_tail->sa, frame->sa, 6);
inic_frame_tail->len = frame->len;
inic_frame_tail++;
inic_frame_cnt++;
}
}
#endif
vPortFree((void *) frame);
}
else
vTaskDelay(1); //delay 1 tick
}
else
break;
}
#if CONFIG_INIC_CMD_RSP
if(inic_frame){
inic_c2h_msg("ATWM", RTW_SUCCESS, (char *)inic_frame, sizeof(struct inic_eth_frame)*inic_frame_cnt);
memset(inic_frame, '\0', sizeof(struct inic_eth_frame)*MAX_INIC_FRAME_NUM);
inic_frame_tail = inic_frame;
inic_frame_cnt = 0;
rtw_msleep_os(10);
}
#endif
}
wifi_set_promisc(RTW_PROMISC_DISABLE, NULL, 0);
while((frame = retrieve_frame()) != NULL)
vPortFree((void *) frame);
}
static void promisc_callback_all(unsigned char *buf, unsigned int len, void* userdata)
{
struct eth_frame *frame = (struct eth_frame *) pvPortMalloc(sizeof(struct eth_frame));
if(frame) {
frame->prev = NULL;
frame->next = NULL;
memcpy(frame->da, buf+4, 6);
memcpy(frame->sa, buf+10, 6);
frame->len = len;
/*
* type is the first byte of Frame Control Field of 802.11 frame
* If the from/to ds information is needed, type could be reused as follows:
* frame->type = ((((ieee80211_frame_info_t *)userdata)->i_fc & 0x0100) == 0x0100) ? 2 : 1;
* 1: from ds; 2: to ds
*/
frame->type = *buf;
frame->rssi = ((ieee80211_frame_info_t *)userdata)->rssi;
taskENTER_CRITICAL();
if(eth_buffer.tail) {
eth_buffer.tail->next = frame;
frame->prev = eth_buffer.tail;
eth_buffer.tail = frame;
}
else {
eth_buffer.head = frame;
eth_buffer.tail = frame;
}
taskEXIT_CRITICAL();
}
}
static void promisc_test_all(int duration, unsigned char len_used)
{
int ch;
unsigned int start_time;
struct eth_frame *frame;
eth_buffer.head = NULL;
eth_buffer.tail = NULL;
wifi_enter_promisc_mode();
wifi_set_promisc(RTW_PROMISC_ENABLE_2, promisc_callback_all, len_used);
for(ch = 1; ch <= 13; ch ++) {
if(wifi_set_channel(ch) == 0)
printf("\n\n\rSwitch to channel(%d)", ch);
start_time = xTaskGetTickCount();
while(1) {
unsigned int current_time = xTaskGetTickCount();
if((current_time - start_time) < (duration)) { // duration * configTICK_RATE_HZ
frame = retrieve_frame();
if(frame) {
int i;
printf("\n\rTYPE: 0x%x, ", frame->type);
printf("DA:");
for(i = 0; i < 6; i ++)
printf(" %02x", frame->da[i]);
printf(", SA:");
for(i = 0; i < 6; i ++)
printf(" %02x", frame->sa[i]);
printf(", len=%d", frame->len);
printf(", RSSI=%d", frame->rssi);
#if CONFIG_INIC_CMD_RSP
if(inic_frame_tail){
if(inic_frame_cnt < MAX_INIC_FRAME_NUM){
memcpy(inic_frame_tail->da, frame->da, 6);
memcpy(inic_frame_tail->sa, frame->sa, 6);
inic_frame_tail->len = frame->len;
inic_frame_tail->type = frame->type;
inic_frame_tail++;
inic_frame_cnt++;
}
}
#endif
vPortFree((void *) frame);
}
else
vTaskDelay(1); //delay 1 tick
}
else
break;
}
#if CONFIG_INIC_CMD_RSP
if(inic_frame){
inic_c2h_msg("ATWM", RTW_SUCCESS, (char *)inic_frame, sizeof(struct inic_eth_frame)*inic_frame_cnt);
memset(inic_frame, '\0', sizeof(struct inic_eth_frame)*MAX_INIC_FRAME_NUM);
inic_frame_tail = inic_frame;
inic_frame_cnt = 0;
rtw_msleep_os(10);
}
#endif
}
wifi_set_promisc(RTW_PROMISC_DISABLE, NULL, 0);
while((frame = retrieve_frame()) != NULL)
vPortFree((void *) frame);
}
void cmd_promisc(int argc, char **argv)
{
int duration;
#if CONFIG_INIC_CMD_RSP
inic_frame_tail = inic_frame = pvPortMalloc(sizeof(struct inic_eth_frame)*MAX_INIC_FRAME_NUM);
if(inic_frame == NULL){
inic_c2h_msg("ATWM", RTW_BUFFER_UNAVAILABLE_TEMPORARY, NULL, 0);
return;
}
#endif
#ifdef CONFIG_PROMISC
wifi_init_packet_filter();
#endif
if((argc == 2) && ((duration = atoi(argv[1])) > 0))
//promisc_test(duration, 0);
promisc_test_all(duration, 0);
else if((argc == 3) && ((duration = atoi(argv[1])) > 0) && (strcmp(argv[2], "with_len") == 0))
promisc_test(duration, 1);
else
printf("\n\rUsage: %s DURATION_MSECONDS [with_len]", argv[0]);
#if CONFIG_INIC_CMD_RSP
if(inic_frame)
vPortFree(inic_frame);
inic_frame_tail = NULL;
inic_frame_cnt = 0;
#endif
}
#endif //#if CONFIG_WLAN

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,20 @@
#ifndef __WIFI_SIMPLE_CONFIG_H
#define __WIFI_SIMPLE_CONFIG_H
/*****************************wifi_simple_config.h****************************/
enum sc_result {
SC_ERROR = -1, /* default error code*/
SC_NO_CONTROLLER_FOUND = 1, /* cannot get sta(controller) in the air which starts a simple config session */
SC_CONTROLLER_INFO_PARSE_FAIL, /* cannot parse the sta's info */
SC_TARGET_CHANNEL_SCAN_FAIL, /* cannot scan the target channel */
SC_JOIN_BSS_FAIL, /* fail to connect to target ap */
SC_DHCP_FAIL, /* fail to get ip address from target ap */
/* fail to create udp socket to send info to controller. note that client isolation
must be turned off in ap. we cannot know if ap has configured this */
SC_UDP_SOCKET_CREATE_FAIL,
SC_TERMINATE,
SC_SUCCESS, /* default success code */
};
int SC_send_simple_config_ack(u8 round);
#endif //__WIFI_SIMPLE_CONFIG_H

View file

@ -0,0 +1,99 @@
#ifndef __SIMPLE_CONFIG_H__
#define __SIMPLE_CONFIG_H__
#ifdef __cplusplus
extern "C" {
#endif
/* This macro means user take simple config
* lib to another platform such as linux, and
* have no rom crypto libs of simple config,
* so we take simple_config_crypto as a sw lib
* This macro is used by Realtek internal to generate simple config lib
* Please delete this macro after generation.
*/
#define SIMPLE_CONFIG_PLATFORM_LIB 0
#include "platform_opts.h"
#include "autoconf.h"
/* platform related settings */
#if (defined(CONFIG_PLATFORM_8195A)|| defined(CONFIG_PLATFORM_8711B))
#undef u32
#undef s32
#undef u8
#undef s8
#undef u16
#undef s16
typedef unsigned int u32;
typedef signed int s32;
typedef unsigned char u8;
typedef char s8;
typedef unsigned short int u16;
typedef signed short int s16;
#else
#include "osdep_service.h"
#endif
typedef int (*simple_config_printf_fn) (char const * fmt, ...);
typedef void* (*simple_config_memset_fn) (u8 *dst0, s32 Val, u32 length);
typedef void* (*simple_config_memcpy_fn) ( void *s1, const void *s2, u32 n );
typedef u32 (*simple_config_strlen_fn) (const char *s);
typedef char * (*simple_config_strcpy_fn) (char *dest, const char *src);
typedef void (*simple_config_free_fn) (u8 *pbuf, u32 sz);
typedef u8* (*simple_config_zmalloc_fn) (u32 sz);
typedef u8* (*simple_config_malloc_fn) (u32 sz);
typedef int (*simple_config_memcmp_fn) (const void *av, const void *bv, u32 len);
typedef u32 (*simple_config_ntohl_fn)(u32 x);
struct simple_config_lib_config {
simple_config_printf_fn printf;
simple_config_memset_fn memset;
simple_config_memcpy_fn memcpy;
simple_config_strlen_fn strlen;
simple_config_strcpy_fn strcpy;
simple_config_free_fn free;
simple_config_zmalloc_fn zmalloc;
simple_config_malloc_fn malloc;
simple_config_memcmp_fn memcmp;
simple_config_ntohl_fn _ntohl;
int *is_promisc_callback_unlock;
};
#pragma pack(1)
struct rtk_test_sc {
/* API exposed to user */
unsigned char ssid[32];
unsigned char password[65];
unsigned int ip_addr;
};
/* expose data */
extern s32 is_promisc_callback_unlock;
extern u8 g_bssid[6];
extern u8 get_channel_flag;
extern u8 g_security_mode;
/* expose API */
extern s32 rtk_sc_init(char *custom_pin_code, struct simple_config_lib_config* config);
extern s32 rtk_start_parse_packet(u8 *da, u8 *sa, s32 len, void * user_data, void *backup_sc);
extern void rtk_restart_simple_config(void);
extern void rtk_sc_deinit();
extern void wifi_enter_promisc_mode();
extern void whc_fix_channel();
extern void whc_unfix_channel();
#ifdef __cplusplus
}
#endif
#endif /* __SIMPLE_CONFIG_H__*/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,76 @@
#ifndef _UTIL_H
#define _UTIL_H
#include <wireless.h>
#include <wlan_intf.h>
#include <wifi_constants.h>
#include "wifi_structures.h"
#ifdef __cplusplus
extern "C" {
#endif
int wext_get_ssid(const char *ifname, __u8 *ssid);
int wext_set_ssid(const char *ifname, const __u8 *ssid, __u16 ssid_len);
int wext_set_auth_param(const char *ifname, __u16 idx, __u32 value);
int wext_set_key_ext(const char *ifname, __u16 alg, const __u8 *addr, int key_idx, int set_tx, const __u8 *seq, __u16 seq_len, __u8 *key, __u16 key_len);
int wext_get_enc_ext(const char *ifname, __u16 *alg, __u8 *key_idx, __u8 *passphrase);
int wext_set_passphrase(const char *ifname, const __u8 *passphrase, __u16 passphrase_len);
int wext_get_passphrase(const char *ifname, __u8 *passphrase);
int wext_set_mode(const char *ifname, int mode);
int wext_get_mode(const char *ifname, int *mode);
int wext_set_ap_ssid(const char *ifname, const __u8 *ssid, __u16 ssid_len);
int wext_set_country(const char *ifname, rtw_country_code_t country_code);
int wext_get_rssi(const char *ifname, int *rssi);
int wext_set_channel(const char *ifname, __u8 ch);
int wext_get_channel(const char *ifname, __u8 *ch);
int wext_register_multicast_address(const char *ifname, rtw_mac_t *mac);
int wext_unregister_multicast_address(const char *ifname, rtw_mac_t *mac);
int wext_set_scan(const char *ifname, char *buf, __u16 buf_len, __u16 flags);
int wext_get_scan(const char *ifname, char *buf, __u16 buf_len);
int wext_set_mac_address(const char *ifname, char * mac);
int wext_get_mac_address(const char *ifname, char * mac);
int wext_enable_powersave(const char *ifname, __u8 lps_mode, __u8 ips_mode);
int wext_disable_powersave(const char *ifname);
#define wext_disable_powersave(n) wext_enable_powersave(n, 0, 0)
int wext_set_tdma_param(const char *ifname, __u8 slot_period, __u8 rfon_period_len_1, __u8 rfon_period_len_2, __u8 rfon_period_len_3);
int wext_set_lps_dtim(const char *ifname, __u8 lps_dtim);
int wext_get_lps_dtim(const char *ifname, __u8 *lps_dtim);
int wext_get_tx_power(const char *ifname, __u8 *poweridx);
int wext_set_txpower(const char *ifname, int poweridx);
int wext_get_associated_client_list(const char *ifname, void * client_list_buffer, __u16 buffer_length);
int wext_get_ap_info(const char *ifname, rtw_bss_info_t * ap_info, rtw_security_t* security);
int wext_mp_command(const char *ifname, char *cmd, int show_msg);
int wext_private_command(const char *ifname, char *cmd, int show_msg);
int wext_private_command_with_retval(const char *ifname, char *cmd, char *ret_buf, int ret_len);
void wext_wlan_indicate(unsigned int cmd, union iwreq_data *wrqu, char *extra);
int wext_set_pscan_channel(const char *ifname, __u8 *ch, __u8 *pscan_config, __u8 length);
int wext_set_autoreconnect(const char *ifname, __u8 mode, __u8 retyr_times, __u16 timeout);
int wext_get_autoreconnect(const char *ifname, __u8 *mode);
int wext_set_adaptivity(rtw_adaptivity_mode_t adaptivity_mode);
int wext_set_adaptivity_th_l2h_ini(__u8 l2h_threshold);
int wext_get_auto_chl(const char *ifname, unsigned char *channel_set, unsigned char channel_num);
int wext_set_sta_num(unsigned char ap_sta_num);
int wext_del_station(const char *ifname, unsigned char* hwaddr);
int wext_init_mac_filter(void);
int wext_deinit_mac_filter(void);
int wext_add_mac_filter(unsigned char* hwaddr);
int wext_del_mac_filter(unsigned char* hwaddr);
int wext_set_tos_value(const char *ifname, __u8 *tos_value);
#ifdef CONFIG_CUSTOM_IE
int wext_add_custom_ie(const char *ifname, void * cus_ie, int ie_num);
int wext_update_custom_ie(const char *ifname, void * cus_ie, int ie_index);
int wext_del_custom_ie(const char *ifname);
#endif
#define wext_handshake_done rltk_wlan_handshake_done
int wext_send_mgnt(const char *ifname, char *buf, __u16 buf_len, __u16 flags);
int wext_send_eapol(const char *ifname, char *buf, __u16 buf_len, __u16 flags);
int wext_set_gen_ie(const char *ifname, char *buf, __u16 buf_len, __u16 flags);
#ifdef __cplusplus
}
#endif
#endif /* _UTIL_H */

View file

@ -0,0 +1,863 @@
/*
* wifi_api.c
*
* Created on: 01/11/2017
* Author: pvvx
*/
#include <platform_opts.h>
#include "rtl8195a.h"
#include "drv_types.h"
#include <autoconf.h>
#include "FreeRTOS.h"
#if 1
#include "drv_types.h" // or #include "wlan_lib.h"
#else
#include "wifi_constants.h"
#include "wifi_structures.h"
//#include "wlan_lib.h" // or #include "drv_types.h"
#endif
#if CONFIG_EXAMPLE_WLAN_FAST_CONNECT
#error "Udnef CONFIG_EXAMPLE_WLAN_FAST_CONNECT!"
#endif
#ifndef USE_FLASH_EEP
#error "Define USE_FLASH_EEP!"
#endif
#include "task.h"
#include <platform/platform_stdlib.h>
#include <wifi/wifi_conf.h>
#include "flash_api.h"
#include <lwip_netconf.h>
#include "dhcp/dhcps.h"
#include "ethernetif.h"
#if CONFIG_ETHERNET
#include "ethernet_mii/ethernet_mii.h"
#endif
#include "flash_eep.h"
#include "feep_config.h"
#include "wifi_api.h"
#include "main.h"
#include "wifi_user_set.h"
#if 0
#undef debug_printf
#define debug_printf(fmt, ...) rtl_printf(fmt, ##__VA_ARGS__)
#undef info_printf
#define info_printf(fmt, ...) rtl_printf(fmt, ##__VA_ARGS__)
#undef warning_printf
#define warning_printf(fmt, ...) rtl_printf(fmt, ##__VA_ARGS__)
#undef error_printf
#define error_printf(fmt, ...) rtl_printf(fmt, ##__VA_ARGS__)
#endif
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
extern void cmd_ap_wps(int argc, char **argv);
extern int wpas_wps_dev_config(u8 *dev_addr, u8 bregistrar);
#endif //CONFIG_WPS_AP
//=========================================
//--- Wlan Config Init-------------------
WIFI_CONFIG wifi_cfg = {
.mode = DEF_WIFI_MODE, // rtw_mode_t
.adaptivity = DEF_WIFI_ADAPTIVITY, // rtw_adaptivity_mode_t
.country_code = DEF_WIFI_COUNTRY,// rtw_country_code_t
.tx_pwr = DEF_WIFI_TX_PWR, // rtw_tx_pwr_percentage_t
.bgn = DEF_WIFI_BGN, // rtw_network_mode_t
.load_flg = DEF_LOAD_CFG,
.save_flg = DEF_SAVE_CFG
};
//---- Interface 0 - wlan0 - AP - init ---
SOFTAP_CONFIG wifi_ap_cfg = {
.ssid = DEF_AP_SSID,
.password = DEF_AP_PASSWORD,
.security = DEF_AP_SECURITY, // RTW_SECURITY_WPA2_AES_PSK or RTW_SECURITY_OPEN
.beacon_interval = DEF_AP_BEACON,
.channel = DEF_AP_CHANNEL,
.ssid_hidden = 0,
.max_sta = DEF_WIFI_AP_STATIONS // Max number of STAs, should be 1..3, default is 3
};
DHCP_CONFIG wifi_ap_dhcp = {
.ip = DEF_AP_IP,
.mask = DEF_AP_MSK,
.gw = DEF_AP_GW,
.mode = 2
};
//---- Interface 1 - wlan1 - ST - init ---
STATION_CONFIG wifi_st_cfg = {
.ssid = DEF_ST_SSID,
.password = DEF_ST_PASSWORD,
.bssid = DEF_ST_BSSID,
.flg = DEF_ST_BSSID,
.security = DEF_ST_SECURITY,
.autoreconnect = DEF_ST_AUTORECONNECT,
.reconnect_pause = DEF_ST_RECONNECT_PAUSE,
.sleep = DEF_ST_SLEEP,
.dtim = DEF_ST_LPS_DTIM
};
DHCP_CONFIG wifi_st_dhcp = {
.ip = DEF_ST_IP,
.mask = DEF_ST_MSK,
.gw = DEF_ST_GW,
.mode = 1
};
unsigned char wifi_run_mode = RTW_MODE_NONE; // rtw_mode_t
unsigned char wifi_st_status;
typedef int (*wlan_init_done_ptr)(void);
typedef int (*write_reconnect_ptr)(uint8_t *data, uint32_t len);
//Function
#if CONFIG_AUTO_RECONNECT
extern void (*p_wlan_autoreconnect_hdl)(rtw_security_t, char*, int, char*, int, int);
#endif
extern wlan_init_done_ptr p_wlan_init_done_callback;
//extern write_reconnect_ptr p_write_reconnect_ptr;
extern struct netif xnetif[NET_IF_NUM];
extern uint8_t rtw_power_percentage_idx;
extern Rltk_wlan_t rltk_wlan_info[2]; // in wrapper.h
#define PARM_RECONNECT 1
struct wifi_autoreconnect_param {
#if PARM_RECONNECT
rtw_security_t security_type;
int key_id;
#else
rtw_security_t security_type;
char *ssid;
int ssid_len;
char *password;
int password_len;
int key_id;
#endif
};
struct wifi_autoreconnect_param wifi_autoreconnect;
typedef struct _feep_element {
uint16 id;
uint16 size;
void * obj;
} FEEP_ELEMENT, *PFEEP_ELEMENT;
FEEP_ELEMENT feep_tab[] = {
{ FEEP_ID_WIFI_AP_CFG, sizeof(wifi_ap_cfg), &wifi_ap_cfg }, // Bit0 BID_WIFI_AP_CFG
{ FEEP_ID_WIFI_ST_CFG, sizeof(wifi_st_cfg), &wifi_st_cfg }, // Bit1 BID_WIFI_ST_CFG
{ FEEP_ID_AP_DHCP_CFG, sizeof(wifi_ap_dhcp), &wifi_ap_dhcp }, // Bit2 BID_AP_DHCP_CFG
{ FEEP_ID_ST_DHCP_CFG, sizeof(wifi_st_dhcp), &wifi_st_dhcp }, // Bit3 BID_ST_DHCP_CFG
{ FEEP_ID_WIFI_CFG, sizeof(wifi_cfg), &wifi_cfg }, // Bit4 BID_WIFI_CFG
{ FEEP_ID_AP_HOSTNAME, LWIP_NETIF_HOSTNAME_SIZE, &lwip_host_name[1] }, // Bit5 BID_AP_HOSTNAME
{ FEEP_ID_ST_HOSTNAME, LWIP_NETIF_HOSTNAME_SIZE, &lwip_host_name[0] }, // Bit5 BID_ST_HOSTNAME
{ 0, 0, NULL }
};
uint32 read_wifi_cfg(uint32 flg)
{
uint32 ret = 0;
PFEEP_ELEMENT p = feep_tab;
for(int m = 1; m && p->id != 0; m <<= 1, p++) {
if((flg & m)
&& flash_read_cfg(p->obj, p->id, p->size) < p->size) {
ret |= m;
};
};
return ret;
}
uint32 write_wifi_cfg(uint32 flg)
{
uint32 ret = 0;
PFEEP_ELEMENT p = feep_tab;
for(int m = 1; m && p->id != 0; m <<= 1, p++) {
if(flg & m) {
if(!flash_write_cfg(p->obj, p->id, p->size)) {
ret |= m;
};
};
};
return ret;
}
#if CONFIG_WLAN_CONNECT_CB
_WEAK void connect_start(void)
{
info_printf("%s: Time at start %d ms.\n", __func__, xTaskGetTickCount());
}
_WEAK void connect_close(void)
{
info_printf("%s: Time at start %d ms.\n", __func__, xTaskGetTickCount());
}
#endif
#ifdef NOT_USE_CALLS
LOCAL int wlan_init_done_callback(void) {
printf("WiFi Init after %d ms\n", xTaskGetTickCount());
return 0;
}
#endif // #ifdef NOT_USE_CALLS
//char wlan_st_name[] = WLAN0_NAME;
char wlan_st_name[] = WLAN0_NAME;
char wlan_ap_name[] = WLAN1_NAME;
char wlan_st_netifn = 0;
char wlan_ap_netifn = 1;
uint32 get_new_ip(void)
{
if(!(wifi_cfg.mode ^ wifi_run_mode)) {
return current_netif->ip_addr.addr;
} else if(wifi_cfg.mode == RTW_MODE_AP) {
return wifi_ap_dhcp.ip;
} else if(wifi_st_dhcp.mode == 2) {
return wifi_st_dhcp.ip;
}
return 0;
}
uint8 * get_new_hostname(void)
{
if(!(wifi_cfg.mode ^ wifi_run_mode)) {
return current_netif->hostname;
} else if(wifi_cfg.mode == RTW_MODE_AP) {
return lwip_host_name[1];
};
return lwip_host_name[0];
}
LOCAL uint8 chk_ap_netif_num(void)
{
if (wifi_mode == RTW_MODE_AP) {
wlan_st_name[4] = '1';
wlan_ap_name[4] = '0';
wlan_st_netifn = 1;
wlan_ap_netifn = 0;
}
else { // if (wifi_mode == RTW_MODE_STA) {
wlan_st_name[4] = '0';
wlan_ap_name[4] = '1';
wlan_st_netifn = 0;
wlan_ap_netifn = 1;
}
return wlan_ap_netifn;
}
extern Rltk_wlan_t rltk_wlan_info[2]; // in wrapper.h
/*LOCAL _adapter * get_padapter(int num) {
if(rltk_wlan_info[num].enable) {
return *(_adapter **)((rltk_wlan_info[0].dev)->priv);
}
return NULL;
};*/
#define get_padapter(num) (*(_adapter **)((rltk_wlan_info[num].dev)->priv));
LOCAL rtw_result_t _wext_set_lps_dtim(int adapter_num, uint8 lps_dtim ) {
_adapter * pad = get_padapter(adapter_num);
rtw_result_t ret = RTW_ERROR;
if(pad) {
ret = rtw_pm_set_lps_dtim(pad, lps_dtim);
}
return ret;
}
LOCAL rtw_result_t _wext_enable_powersave(int adapter_num, uint8 ips_mode, uint8 lps_mode) {
_adapter * pad = get_padapter(adapter_num);
rtw_result_t ret = RTW_ERROR;
if(pad) {
ret = rtw_pm_set_ips(pad, ips_mode); // 2 режима 1,2 !
if(ret == RTW_SUCCESS) {
LeaveAllPowerSaveMode(pad);
ret = rtw_pm_set_lps(pad, lps_mode);
}
}
return ret;
}
LOCAL int _wext_cmp_ssid(int adapter_num, uint8 *ssid)
{
_adapter * pad = get_padapter(adapter_num);
int ret = 0;
if((pad != NULL) && (pad->mlmepriv.fw_state & 0x41) != 0) {
int len = pad->mlmepriv.cur_network.network.Ssid.SsidLength;
if(len < 32) len++;
else len = 32;
ret = (rtl_memcmp(ssid, &pad->mlmepriv.cur_network.network.Ssid.Ssid, len) == 0);
debug_printf("%d s[%d]'%s'\n", pad->mlmepriv.fw_state, len, ssid);
}
return ret;
}
#ifdef NOT_USE_CALLS
LOCAL rtw_result_t _wext_get_mode(int adapter_num, int *mode) {
_adapter * pad = get_padapter(adapter_num);
rtw_result_t ret = RTW_ERROR;
if(pad) {
uint16 f = pad->mlmepriv.fw_state;
if(f & 8) *mode = 2;
else if(f & 0x60) *mode = 1;
else if(!(f & 0x10)) *mode = 0;
else *mode = 3;
ret = RTW_SUCCESS;
}
return ret;
}
LOCAL rtw_result_t _wext_get_channel(int adapter_num, uint8 *ch)
{
_adapter * pad = get_padapter(adapter_num);
rtw_result_t ret = RTW_ERROR;
if(pad) {
if(pad->mlmepriv.fw_state & 1) {
*ch = pad->mlmepriv.htpriv.ch_offset;
}
else {
*ch = pad->mlmeextpriv.cur_channel;
}
ret = RTW_SUCCESS;
}
return ret;
}
#endif // #ifdef NOT_USE_CALLS
LOCAL rtw_result_t wifi_run_ap(void) {
rtw_result_t ret = RTW_NOTAP;
if( (wifi_mode == RTW_MODE_AP) || (wifi_mode == RTW_MODE_STA_AP) ){
info_printf("Starting AP (%s, netif%d)...\n", wlan_ap_name, wlan_ap_netifn);
/*
netif_set_addr(&xnetif[WLAN_AP_NETIF_NUM], &wifi_ap_dhcp.ip,
&wifi_ap_dhcp.mask, &wifi_ap_dhcp.gw);
*/
if(wext_set_sta_num(wifi_ap_cfg.max_sta) != 0) { // Max number of STAs, should be 1..3, default is 3
error_printf("AP not set max connections %d!\n", wifi_ap_cfg.max_sta);
};
#if defined(CONFIG_ENABLE_WPS_AP) && CONFIG_ENABLE_WPS_AP
// todo: ветка не доделана
wpas_wps_dev_config(xnetif[WLAN_AP_NETIF_NUM].hwaddr, 1);
#endif //CONFIG_WPS_AP
if(wifi_ap_cfg.channel == 0 || wifi_ap_cfg.channel > 14) {
wifi_ap_cfg.channel = 1;
}
ret = wifi_start_ap(wifi_ap_cfg.ssid, //char *ssid,
wifi_ap_cfg.security, //rtw_security_t ecurity_type,
wifi_ap_cfg.password, //char *password,
wifi_ap_cfg.channel, //int channel
wifi_ap_cfg.ssid_hidden); //
// wifi_run_mode |= RTW_MODE_AP;
if (ret != RTW_SUCCESS) {
error_printf("Error(%d): Start AP failed!\n\n", ret);;
} else {
int timeout = wifi_test_timeout_ms / wifi_test_timeout_step_ms;
while (1) {
#if 1
if (_wext_cmp_ssid(WLAN_AP_NETIF_NUM, &wifi_ap_cfg.ssid )) {
#else
char essid[33];
if ((wext_get_ssid(wlan_ap_name, (unsigned char *) essid) > 0)
&&(strcmp((const char * ) essid, (const char * )wifi_ap_cfg.ssid) == 0)) {
#endif
#ifdef CONFIG_DONT_CARE_TP
pnetiff->flags |= NETIF_FLAG_IPSWITCH;
#endif
if(wifi_ap_dhcp.mode) {
#if defined(DEF_AP_DHCP_START) && defined(DEF_AP_DHCP_STOP)
dhcps_ip4addr_pool_start = DEF_AP_DHCP_START;
dhcps_ip4addr_pool_end = DEF_AP_DHCP_STOP;
#endif
dhcps_init(&xnetif[WLAN_AP_NETIF_NUM]);
};
info_printf("AP '%s' started after %d ms\n",
wifi_ap_cfg.ssid, xTaskGetTickCount());
show_wifi_ap_ip();
if(wifi_cfg.save_flg & (BID_WIFI_AP_CFG | BID_AP_DHCP_CFG))
write_wifi_cfg(wifi_cfg.save_flg & (BID_WIFI_AP_CFG | BID_AP_DHCP_CFG));
ret = RTW_SUCCESS;
#if CONFIG_WLAN_CONNECT_CB
// extern void connect_start(void);
connect_start();
#endif
break;
}
if (timeout == 0) {
error_printf("Start AP timeout!\n");
ret = RTW_TIMEOUT;
break;
}
vTaskDelay(wifi_test_timeout_step_ms / portTICK_RATE_MS);
timeout--;
}
}
}
return ret;
}
LOCAL rtw_result_t StartStDHCPClient(void)
{
debug_printf("Start DHCPClient...\n");
int ret = RTW_SUCCESS;
struct netif * pnetif = &xnetif[WLAN_ST_NETIF_NUM];
DHCP_CONFIG *p = (dhcp_cfg *)&wifi_st_dhcp;
unsigned char mode = p->mode;
if((mode == 3) // Auto fix
&& p->ip != IP4ADDR(255,255,255,255)
&& p->ip != IP4ADDR(0,0,0,0)) {
}
else mode = 1; // DHCP On
if(mode == 2) { // fixed ip
netif_set_addr(pnetif, (ip_addr_t *)&p->ip, (ip_addr_t *)&p->mask, (ip_addr_t *)&p->gw);
}
else if(mode) {
UBaseType_t savePriority = uxTaskPriorityGet(NULL);
/* If not rise priority, LwIP DHCP may timeout */
vTaskPrioritySet(NULL, tskIDLE_PRIORITY + 3);
/* Start DHCP Client */
ret = LwIP_DHCP(WLAN_ST_NETIF_NUM, DHCP_START);
vTaskPrioritySet(NULL, savePriority);
if (ret == DHCP_ADDRESS_ASSIGNED) {
p->ip = pnetif->ip_addr.addr;
p->gw = pnetif->gw.addr;
p->mask = pnetif->netmask.addr;
if(p->mode == 3) { // Auto fix
p->mode = 2; // fixed ip
write_wifi_cfg(BID_ST_DHCP_CFG);
}
ret = RTW_SUCCESS;
}
else {
debug_printf("LwIP_DHCP ret=%d\n", ret);
ret = RTW_ERROR;
}
}
if(ret == RTW_SUCCESS) {
show_wifi_st_ip();
wifi_st_status = WIFI_STA_CONNECTED;
#if CONFIG_WLAN_CONNECT_CB
// extern void connect_start(void);
connect_start();
#endif
}
return ret;
}
LOCAL void wifi_autoreconnect_thread_(void *param) {
int ret = RTW_ERROR;
struct wifi_autoreconnect_param *reconnect_param =
(struct wifi_autoreconnect_param *) param;
printf("auto reconnect ...\n");
wifi_st_status = WIFI_STA_RECONNECT;
ret = wifi_connect(
wifi_st_cfg.bssid,
wifi_st_cfg.flg,
#if PARM_RECONNECT
wifi_st_cfg.ssid,
reconnect_param->security_type,
wifi_st_cfg.password,
#else
reconnect_param->ssid,
reconnect_param->security_type,
reconnect_param->password,
#endif
reconnect_param->key_id,
NULL);
if (ret == RTW_SUCCESS) {
if(wifi_cfg.save_flg & BID_WIFI_ST_CFG)
write_wifi_cfg(BID_WIFI_ST_CFG);
// Start DHCPClient
StartStDHCPClient();
}
vTaskDelete(NULL);
}
LOCAL void wifi_autoreconnect_hdl_(rtw_security_t security_type, char *ssid,
int ssid_len, char *password, int password_len, int key_id) {
wifi_autoreconnect.security_type = security_type;
wifi_autoreconnect.key_id = key_id;
#if PARM_RECONNECT==0
wifi_autoreconnect.ssid = ssid;
wifi_autoreconnect.password = password;
#endif
_adapter * ad = *(_adapter **)((rltk_wlan_info[0].dev)->priv);
if(ad->mlmeextpriv.reconnect_cnt == 255) {
ad->mlmeextpriv.reconnect_cnt = 0;
};
xTaskCreate(wifi_autoreconnect_thread_, (const char * )"st_recon", 400,
&wifi_autoreconnect, tskIDLE_PRIORITY + 1, NULL);
}
LOCAL void st_set_autoreconnect(uint8 mode, uint8 count, uint16 timeout) {
p_wlan_autoreconnect_hdl = wifi_autoreconnect_hdl_;
_adapter * ad = *(_adapter **)((rltk_wlan_info[0].dev)->priv);
ad->mlmeextpriv.reconnect_times = count;
ad->mlmeextpriv.reconnect_timeout = timeout;
ad->mlmeextpriv.reconnect_cnt = 0;
ad->mlmeextpriv.auto_reconnect = (mode != 0);
}
LOCAL rtw_result_t wifi_run_st(void) {
rtw_result_t ret = RTW_SUCCESS;
// chk_ap_netif_num();
if((wifi_mode == RTW_MODE_STA) || (wifi_mode == RTW_MODE_STA_AP)) {
#if CONFIG_AUTO_RECONNECT
// p_wlan_autoreconnect_hdl = NULL;
if (wifi_st_cfg.autoreconnect) {
st_set_autoreconnect(1, wifi_st_cfg.autoreconnect, wifi_st_cfg.reconnect_pause);
ret = wext_set_autoreconnect(WLAN0_NAME, 1, wifi_st_cfg.autoreconnect, wifi_st_cfg.reconnect_pause);
if (ret != RTW_SUCCESS)
warning_printf("ERROR: Operation failed! Error=%d\n", ret);
}
#endif
info_printf("Connected to AP (%s, netif%d)...\n", wlan_st_name, wlan_st_netifn);
ret = wifi_connect(
wifi_st_cfg.bssid,
wifi_st_cfg.flg,
wifi_st_cfg.ssid,
idx_to_rtw_security(wifi_st_cfg.security),
wifi_st_cfg.password,
-1,
NULL);
wifi_st_status = WIFI_STA_START;
// wifi_run_mode |= RTW_MODE_STA;
if (ret != RTW_SUCCESS) {
error_printf("%s: Operation failed! Error(%d)\n", __func__, ret);
} else {
if(wifi_cfg.save_flg & BID_WIFI_ST_CFG)
write_wifi_cfg(BID_WIFI_ST_CFG);
// Start DHCPClient
StartStDHCPClient();
}
};
return ret;
}
LOCAL int _wifi_on(rtw_mode_t mode) {
int ret = 0;
/*
if (!((rltk_wlan_running(WLAN0_IDX) == 0) && (rltk_wlan_running(WLAN1_IDX) == 0))) {
warning_printf("WIFI is already running\n");
return 0;
}
*/
info_printf("Initializing WIFI...\n");
uint8 devnum = (mode == RTW_MODE_STA_AP); // flag = 1 -> 2 netif
wifi_mode = mode;
chk_ap_netif_num();
// set wifi mib
wext_set_adaptivity(wifi_cfg.adaptivity & 3); // rtw_adaptivity_mode_t
ret = rltk_wlan_init(WLAN0_IDX, mode); // rtw_mode_t
netif_set_up(&xnetif[0]);
if (ret < 0) return ret;
if(devnum) {
ret = rltk_wlan_init(WLAN1_IDX, mode);
if (ret < 0) return ret;
netif_set_up(&xnetif[1]);
}
else {
netif_set_down(&xnetif[1]);
}
uint32 timeout = xTaskGetTickCount();
rltk_wlan_start(WLAN0_IDX);
if(devnum) rltk_wlan_start(WLAN1_IDX);
while (1) {
if (rltk_wlan_running(WLAN0_IDX)
&& rltk_wlan_running(devnum) ) {
#if CONFIG_DEBUG_LOG > 2
printf("WIFI initialized (%d ms)\n", xTaskGetTickCount() - timeout);
#endif
break;
}
if(xTaskGetTickCount() - timeout > wifi_test_timeout_ms/portTICK_RATE_MS) {
error_printf("WIFI init timeout!\n");
break;
}
vTaskDelay(wifi_test_timeout_step_ms / portTICK_RATE_MS);
}
return ret;
}
extern int lwip_init_done;
LOCAL void _LwIP_Init(void)
{
if(!lwip_init_done) {
int idx;
debug_printf("LwIP Init (%d)\n", wifi_mode);
/* Create tcp_ip stack thread */
tcpip_init( NULL, NULL );
// chk_ap_netif_num(); // Исполняется после _wifi_on()
for(idx = 0; idx < NET_IF_NUM; idx++) {
xnetif[idx].name[0] = 'r';
xnetif[idx].name[1] = '0' + idx;
}
netif_add(&xnetif[WLAN_ST_NETIF_NUM], (struct netif *)&wifi_st_dhcp.ip, (struct netif *)&wifi_st_dhcp.mask, (struct netif *)&wifi_st_dhcp.gw, NULL, &ethernetif_init, &tcpip_input);
netif_add(&xnetif[WLAN_AP_NETIF_NUM], (struct netif *)&wifi_ap_dhcp.ip, (struct netif *)&wifi_ap_dhcp.mask, (struct netif *)&wifi_ap_dhcp.gw, NULL, &ethernetif_init, &tcpip_input);
#if CONFIG_ETHERNET // && NET_IF_NUM > 2
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
ipaddr.addr = DEF_EH_IP;
netmask.addr = DEF_EH_MSK;
gw.addr = DEF_EH_GW;
netif_add(&xnetif[2], &ipaddr, &netmask, &gw, NULL, &ethernetif_mii_init, &tcpip_input);
}
#endif
/* Registers the default network interface. */
netif_set_default(&xnetif[0]);
/* When the netif is fully configured this function must be called.*/
for(idx = 0; idx < NET_IF_NUM; idx++) {
netif_set_up(&xnetif[idx]);
}
info_printf("interface %d is initialized\n", idx);
lwip_init_done = 1;
init_event_callback_list();
}
}
int wifi_run(rtw_mode_t mode) {
int ret = 0;
#if CONFIG_DEBUG_LOG > 4
debug_printf("\n%s(%d), %d\n", __func__, mode, wifi_run_mode);
debug_printf("old mode = %d, new mode = %d\n", wifi_run_mode, mode);
#endif
if(wifi_mode) { // != mode) {
info_printf("Deinitializing WIFI ...\n");
wifi_off();
wifi_st_status = WIFI_STA_OFF;
// wifi_run_mode = RTW_MODE_NONE;
vTaskDelay(30);
}
if (mode != RTW_MODE_NONE) {
if (_wifi_on(mode) < 0) {
error_printf("Wifi On failed!\n");
goto error_end;
};
if(wifi_set_country(wifi_cfg.country_code) != RTW_SUCCESS) {
error_printf("WiFi: Error set tx country_code (%d)!", wifi_cfg.country_code);
};
if(rtw_power_percentage_idx != wifi_cfg.tx_pwr) {
if(rltk_set_tx_power_percentage(wifi_cfg.tx_pwr) != RTW_SUCCESS) {
error_printf("WiFi: Error set tx power (%d)!", wifi_cfg.tx_pwr);
};
}
if(wifi_set_network_mode(wifi_cfg.bgn) != RTW_SUCCESS) {
error_printf("WiFi: Error set network mode (%d)!", wifi_cfg.bgn);
}
debug_printf("mode=%d, wifi_mode=%d, old_mоde=%d\n", mode, wifi_mode, wifi_run_mode);
if(mode <= RTW_MODE_STA_AP) {
struct netif * pnif = &xnetif[WLAN_ST_NETIF_NUM];
#if LWIP_NETIF_HOSTNAME
// @todo ethernetif_init()...
pnif->hostname = lwip_host_name[0];
#ifdef USE_NETBIOS
netbios_set_name(WLAN_ST_NETIF_NUM, lwip_host_name[0]);
#endif
#endif
netif_set_addr(&xnetif[WLAN_ST_NETIF_NUM], &wifi_st_dhcp.ip,
&wifi_st_dhcp.mask, &wifi_st_dhcp.gw);
pnif = &xnetif[WLAN_AP_NETIF_NUM];
#if LWIP_NETIF_HOSTNAME
// @todo ethernetif_init()...
pnif->hostname = lwip_host_name[1];
#ifdef USE_NETBIOS
netbios_set_name(WLAN_AP_NETIF_NUM, lwip_host_name[1]);
#endif
#endif
netif_set_addr(&xnetif[WLAN_AP_NETIF_NUM], &wifi_ap_dhcp.ip,
&wifi_ap_dhcp.mask, &wifi_ap_dhcp.gw);
}
switch(mode) {
case RTW_MODE_STA_AP:
ret = wifi_run_ap() | wifi_run_st();
// _wext_enable_powersave(0, 0, 0);
// _wext_set_lps_dtim(0, 0);
break;
case RTW_MODE_STA:
ret = wifi_run_st();
if(_wext_set_lps_dtim(0, wifi_st_cfg.dtim)!= RTW_SUCCESS) {
error_printf("WiFi: Error set DTIM(%d)!", wifi_st_cfg.dtim);
};
if(_wext_enable_powersave(0, wifi_st_cfg.sleep & 1, (wifi_st_cfg.sleep >> 1) & 1) != RTW_SUCCESS) {
error_printf("WiFi: Error set powersave mode!");
};
break;
case RTW_MODE_AP:
ret = wifi_run_ap();
// _wext_enable_powersave(WLAN0_NAME, 0, 0);
break;
#if 0// CONFIG_ENABLE_??
case RTW_MODE_PROMISC:
// @todo
break;
#endif
#if CONFIG_ENABLE_P2P
case RTW_MODE_P2P:
// @todo
break;
#endif
default:
ret = 1;
error_printf("WiFi: Error mode(%d)\n", wifi_mode);
};
wifi_run_mode = mode;
if(ret == 0 && (wifi_cfg.save_flg & BID_WIFI_CFG)) {
wifi_cfg.mode = mode;
write_wifi_cfg(BID_WIFI_CFG);
};
} else {
ret = 0;
error_end:
#if CONFIG_WLAN_CONNECT_CB
connect_close();
#endif
if(wifi_run_mode) {
wifi_disconnect();
};
wifi_off();
wifi_st_status = WIFI_STA_OFF;
wifi_run_mode = RTW_MODE_NONE;
chk_ap_netif_num();
};
return ret;
}
/* Load cfg, init WiFi + LwIP init, WiFi start if wifi_cfg.mode != RTW_MODE_NONE */
void wifi_init(void) {
debug_printf("\nLoad Config\n");
read_wifi_cfg(wifi_cfg.load_flg); // DEF_LOAD_CFG
// Call back from wlan driver after wlan init done
// p_wlan_init_done_callback = wlan_init_done_callback;
// Call back from application layer after wifi_connection success
// p_write_reconnect_ptr = wlan_write_reconnect_data_to_flash;
p_wlan_autoreconnect_hdl = NULL;
_LwIP_Init();
#if CONFIG_WIFI_IND_USE_THREAD
wifi_manager_init();
#endif
// wifi_cfg.mode = RTW_MODE_PROMISC; //RTW_MODE_P2P;
wifi_run(wifi_cfg.mode);
}
unsigned char *tab_txt_rtw_secyrity[] = {
"OPEN", //0 Open security
"WEP", //1 WEP Security with open authentication
"WEP SHARED", //2 WEP Security with shared authentication
"WPA TKIP", //3 WPA Security with TKIP
"WPA AES", //4 WPA Security with AES
"WPA2 TKIP", //5 WPA2 Security with TKIP
"WPA2 AES", //6 WPA2 Security with AES
"WPA2 Mixed", //7 WPA2 Security with AES & TKIP
"WPA/WPA2 AES", //8 WPA/WPA2 Security
"Unknown" //9
};
unsigned int tab_code_rtw_secyrity[] = {
RTW_SECURITY_OPEN, //0 Open security
RTW_SECURITY_WEP_PSK, //1 WEP Security with open authentication
RTW_SECURITY_WEP_SHARED, //2 WEP Security with shared authentication
RTW_SECURITY_WPA_TKIP_PSK, //3 WPA Security with TKIP
RTW_SECURITY_WPA_AES_PSK, //4 WPA Security with AES
RTW_SECURITY_WPA2_TKIP_PSK, //5 WPA2 Security with TKIP
RTW_SECURITY_WPA2_AES_PSK, //6 WPA2 Security with AES
RTW_SECURITY_WPA2_MIXED_PSK, //7 WPA2 Security with AES & TKIP
RTW_SECURITY_WPA_WPA2_MIXED, //8 WPA/WPA2 Security
RTW_SECURITY_UNKNOWN //9
};
/*
unsigned char *tab_txt_rtw_eccryption[] = {
"Unknown",
"OPEN",
"WEP40",
"WPA_TKIP",
"WPA_AES",
"WPA2_TKIP",
"WPA2_AES",
"WPA2_MIXED",
"???",
"WEP104",
"Udef" // 0xff
};
*/
rtw_security_t idx_to_rtw_security(unsigned char idx)
{
if(idx > IDX_SECURITY_UNKNOWN - 1) idx = IDX_SECURITY_WPA2_AES_PSK;
return (rtw_security_t)tab_code_rtw_secyrity[idx];
}
unsigned char rtw_security_to_idx(rtw_security_t rtw_sec_type)
{
unsigned char i = 0;
while(rtw_sec_type != tab_code_rtw_secyrity[i] && tab_code_rtw_secyrity[i] != RTW_SECURITY_UNKNOWN) i++;
return i;
}
unsigned char * idx_security_to_str(unsigned char idx)
{
if(idx > IDX_SECURITY_UNKNOWN) idx = IDX_SECURITY_UNKNOWN;
return tab_txt_rtw_secyrity[idx];
}
unsigned char * rtw_security_to_str(rtw_security_t rtw_sec_type)
{
return tab_txt_rtw_secyrity[rtw_security_to_idx(rtw_sec_type)];
}
void show_wifi_ap_ip(void) {
printf("SoftAP ip: " IPSTR "\n", IP2STR(&xnetif[WLAN_AP_NETIF_NUM].ip_addr));
}
void show_wifi_st_ip(void) {
printf("Station ip: " IPSTR "\n", IP2STR(&xnetif[WLAN_ST_NETIF_NUM].ip_addr));
}
void show_wifi_MAC(void) {
printf("MAC: " IPSTR "\n", IP2STR(&xnetif[WLAN_ST_NETIF_NUM].ip_addr));
}
void show_wifi_st_cfg(void) {
printf("\tSSID: '%s'\n", wifi_st_cfg.ssid);
printf("\tPassword: '%s'\n", wifi_st_cfg.password);
printf("\tSecurity type: %s\n", idx_security_to_str(wifi_st_cfg.security));
printf("\tAuto-reconnect: %d\n", wifi_st_cfg.autoreconnect);
printf("\tReconnect pause: %d\n", wifi_st_cfg.reconnect_pause);
printf("\tSleep mode: %p\n", wifi_st_cfg.sleep);
printf("\tDTIM: %d\n", wifi_st_cfg.dtim);
}
void show_wifi_ap_cfg(void) {
printf("\tSSID: '%s'\n", wifi_ap_cfg.ssid);
printf("\tSSID hidden: %d\n", wifi_ap_cfg.ssid_hidden);
printf("\tPassword: '%s'\n", wifi_ap_cfg.password);
printf("\tSecurity type: %s\n", (wifi_ap_cfg.security)? tab_txt_rtw_secyrity[IDX_SECURITY_WPA2_AES_PSK] : tab_txt_rtw_secyrity[IDX_SECURITY_OPEN]);
printf("\tChannel: %d\n", wifi_ap_cfg.channel);
printf("\tBeacon interval: %d ms\n", wifi_ap_cfg.beacon_interval);
printf("\tMax connections: %d\n", wifi_ap_cfg.max_sta);
}
void show_wifi_cfg(void) {
printf("\tStart mode: %p\n", wifi_cfg.mode);
printf("\tCountry code: %d\n", wifi_cfg.country_code);
printf("\tNetwork mode: %d\n", wifi_cfg.bgn);
printf("\tTx power: %d\n", wifi_cfg.tx_pwr);
printf("\tAdaptivity: %d\n", wifi_cfg.adaptivity);
printf("\tLoad flags: %p\n", wifi_cfg.load_flg);
printf("\tSave flags: %p\n", wifi_cfg.save_flg);
}

View file

@ -0,0 +1,188 @@
/*
* wifi_user_set.h
*
* Created on: 01/04/2017
* Author: pvvx
*/
#ifndef _WIFI_API_H_
#define _WIFI_API_H_
#include "wifi_constants.h"
#include "queue.h"
#define ip4_addr1(ipaddr) (((uint8_t*)(ipaddr))[0])
#define ip4_addr2(ipaddr) (((uint8_t*)(ipaddr))[1])
#define ip4_addr3(ipaddr) (((uint8_t*)(ipaddr))[2])
#define ip4_addr4(ipaddr) (((uint8_t*)(ipaddr))[3])
#define IPSTR "%d.%d.%d.%d"
#define IP2STR(ipaddr) \
ip4_addr1(ipaddr), \
ip4_addr2(ipaddr), \
ip4_addr3(ipaddr), \
ip4_addr4(ipaddr)
extern char str_rom_57ch3Dch0A[]; // "=========================================================\n" 57
#define BID_WIFI_AP_CFG (1 << 0)
#define BID_WIFI_ST_CFG (1 << 1)
#define BID_AP_DHCP_CFG (1 << 2)
#define BID_ST_DHCP_CFG (1 << 3)
#define BID_WIFI_CFG (1 << 4)
#define BID_AP_HOSTNAME (1 << 5)
#define BID_ST_HOSTNAME (1 << 6)
#define BID_ALL_WIFI_CFG (BID_WIFI_AP_CFG|BID_WIFI_ST_CFG|BID_AP_DHCP_CFG|BID_ST_DHCP_CFG|BID_AP_HOSTNAME|BID_ST_HOSTNAME)
#define WLAN_ST_NETIF_NUM wlan_st_netifn
#define WLAN_AP_NETIF_NUM wlan_ap_netifn
//==== FEEP_ID ===========================
#define FEEP_ID_WIFI_CFG 0x4347 // id:'GC'
#define FEEP_ID_WIFI_AP_CFG 0x5041 // id:'AP'
#define FEEP_ID_WIFI_ST_CFG 0x5453 // id:'ST'
#define FEEP_ID_AP_DHCP_CFG 0x4144 // id:'DA'
#define FEEP_ID_ST_DHCP_CFG 0x5344 // id:'DS'
#define FEEP_ID_AP_HOSTNAME 0x4148 // id:'HA'
#define FEEP_ID_ST_HOSTNAME 0x5348 // id:'HP'
//#define FEEP_ID_UART_CFG 0x5530 // id:'0U', type: UART_LOG_CONF
//#define FEEP_ID_LWIP_CFG 0x4C30 // id:'0L', type: struct atcmd_lwip_conf
//#define FEEP_ID_DHCP_CFG 0x4430 // id:'0D', type: struct
//=========================================
#define IW_PASSPHRASE_MAX_SIZE 64
#define NDIS_802_11_LENGTH_SSID 32
#define IP4ADDR(a,b,c,d) (((unsigned int)((d) & 0xff) << 24) | \
((unsigned int)((c) & 0xff) << 16) | \
((unsigned int)((b) & 0xff) << 8) | \
(unsigned int)((a) & 0xff))
//=========================================
//--- Wlan Config struct-------------------
typedef struct _wifi_config {
unsigned char mode; // rtw_mode_t
unsigned char adaptivity; // rtw_adaptivity_mode_t
unsigned char country_code; // rtw_country_code_t
unsigned char tx_pwr; // rtw_tx_pwr_percentage_t
unsigned char bgn; // 802.11 rtw_network_mode_t
unsigned char load_flg; // см. BID_WIFI_CFG..
unsigned char save_flg;
} WIFI_CONFIG, *PWIFI_CONFIG;
//---- Interface 0 - wlan0 - AP - struct --
typedef struct _softap_config {
unsigned char ssid[NDIS_802_11_LENGTH_SSID+1];
unsigned char password[IW_PASSPHRASE_MAX_SIZE+1];
unsigned short beacon_interval; // Note: support 100 ~ 60000 ms, default 100
unsigned char ssid_hidden; // Note: default 0
unsigned char security; // 0 = RTW_SECURITY_OPEN, 1 = RTW_SECURITY_WPA2_AES_PSK
unsigned char channel; // 1..14
unsigned char max_sta; // 1..3
} SOFTAP_CONFIG, *PSOFTAP_CONFIG;
//---- Interface 1 - wlan1 - ST - struct -
typedef struct _station_config {
unsigned char ssid[NDIS_802_11_LENGTH_SSID+1];
unsigned char password[IW_PASSPHRASE_MAX_SIZE+1];
unsigned char bssid[6]; // Note: If bssid set is not ff.ff.ff.ff.ff.ff || 00:00:00:00:00:00
unsigned char flg; // station will connect to the router with both ssid[], else if set flg - bssid[] matched.
unsigned char security; // IDX_SECURITY
unsigned char autoreconnect; // 0 - none, 1..254 - count, 255 - all
unsigned char reconnect_pause; // in sec
unsigned char sleep; // 0 - Off, 1 - IPS, 2 - LPS, 3 - IPS/LPS mode
unsigned char dtim; // LPS DTIM (2..)
// rtw_adaptivity_mode_t
} STATION_CONFIG, *PSTATION_CONFIG;
//--- LwIP Config -------------------------
struct lwip_conn_info {
int32_t role; //client, server or seed
unsigned int protocol; //tcp or udp
unsigned int remote_addr; //remote ip
unsigned int remote_port; //remote port
unsigned int local_addr; //locale ip, not used yet
unsigned int local_port; //locale port, not used yet
unsigned int reserved; //reserve for further use
};
//--- DHCP Config -------------------------
typedef struct _dhcp_config {
unsigned int ip;
unsigned int mask;
unsigned int gw;
unsigned char mode; // =0 dhcp off, =1 - dhcp on, =2 Static ip, =3 - auto
} DHCP_CONFIG, *PDHCP_CONFIG;
extern WIFI_CONFIG wifi_cfg;
extern SOFTAP_CONFIG wifi_ap_cfg;
extern DHCP_CONFIG wifi_ap_dhcp;
extern STATION_CONFIG wifi_st_cfg;
extern DHCP_CONFIG wifi_st_dhcp;
extern unsigned char wifi_run_mode; // rtw_mode_t
extern unsigned char wifi_mode; // rtw_mode_t
extern unsigned char wifi_st_status; // WIFI_STA_ENUM
extern char wlan_st_name[];
extern char wlan_ap_name[];
extern char wlan_st_netifn;
extern char wlan_ap_netifn;
/* WiFi Station & scan security */
typedef enum {
IDX_SECURITY_OPEN = 0, //0 Open security
IDX_SECURITY_WEP_PSK, //1 WEP Security with open authentication
IDX_SECURITY_WEP_SHARED, //2 WEP Security with shared authentication
IDX_SECURITY_WPA_TKIP_PSK, //3 WPA Security with TKIP
IDX_SECURITY_WPA_AES_PSK, //4 WPA Security with AES
IDX_SECURITY_WPA2_TKIP_PSK, //5 WPA2 Security with TKIP
IDX_SECURITY_WPA2_AES_PSK, //6 WPA2 Security with AES
IDX_SECURITY_WPA2_MIXED_PSK, //7 WPA2 Security with AES & TKIP
IDX_SECURITY_WPA_WPA2_MIXED, //8 WPA/WPA2 Security
IDX_SECURITY_UNKNOWN //9
} IDX_SECURITY;
/* wifi_st_status */
typedef enum {
WIFI_STA_OFF,
WIFI_STA_START,
WIFI_STA_RECONNECT,
WIFI_STA_CONNECTED
} WIFI_STA_ENUM;
uint8 * get_new_hostname(void);
uint32 get_new_ip(void);
void show_wifi_ap_ip(void);
void show_wifi_st_ip(void);
void show_wifi_cfg(void);
void show_wifi_st_cfg(void);
void show_wifi_ap_cfg(void);
uint32 read_wifi_cfg(uint32 flg);
uint32 write_wifi_cfg(uint32 flg);
int wifi_run(rtw_mode_t mode);
void wifi_init(void);
rtw_security_t idx_to_rtw_security(unsigned char idx);
unsigned char rtw_security_to_idx(rtw_security_t rtw_sec_type);
unsigned char * rtw_security_to_str(rtw_security_t rtw_sec_type);
unsigned char * idx_security_to_str(unsigned char idx);
/* -------- Api WiFi Scan ------------------------------- */
#include "wifi_conf.h"
#include "timers.h"
#define SCAN_CHANNELS 14
#define MAX_AP_SIZE 32
extern QueueHandle_t xQueueWebSrv;
typedef struct web_scan_handler{
TimerHandle_t timer;
rtw_scan_result_t * ap_details;
unsigned char ap_count;
unsigned char start_show;
unsigned char used_data;
volatile unsigned char flg;
} web_scan_handler_t;
extern web_scan_handler_t web_scan_handler_ptr;
typedef rtw_result_t (*api_scan_result_handler_t)(internal_scan_handler_t * ap_scan_result);
rtw_result_t api_wifi_scan(api_scan_result_handler_t scan_result_cb);
void wifi_close_scan(void);
#endif // _WIFI_API_H_

View file

@ -0,0 +1,201 @@
/*
* wifi_api_scan.c
*
* Created on: 23/04/2017
* Author: pvvx
*/
#include "FreeRTOS.h"
#include <autoconf.h>
#include "main.h"
#include <platform_opts.h>
#if CONFIG_EXAMPLE_WLAN_FAST_CONNECT
#error "Udnef CONFIG_EXAMPLE_WLAN_FAST_CONNECT!"
#endif
#ifndef USE_FLASH_EEP
#error "Define USE_FLASH_EEP!"
#endif
#include "task.h"
#include <platform/platform_stdlib.h>
#include <wifi/wifi_conf.h>
#include "flash_api.h"
#include <lwip_netconf.h>
#include "dhcp/dhcps.h"
#include "ethernetif.h"
#if CONFIG_ETHERNET
#include "ethernet_mii/ethernet_mii.h"
#endif
#if 1
#include "drv_types.h" // or #include "wlan_lib.h"
#else
#include "wifi_constants.h"
#include "wifi_structures.h"
#include "wlan_lib.h" // or #include "drv_types.h"
#endif
#include "flash_eep.h"
#include "feep_config.h"
#include "wifi_api.h"
#include "main.h"
#include "wifi_user_set.h"
/******************************************************
*
******************************************************/
web_scan_handler_t web_scan_handler_ptr;
/* -------- WiFi Scan ------------------------------- */
volatile uint8_t scan_end;
extern internal_scan_handler_t scan_result_handler_ptr;
void wifi_scan_each_report_hdl(char* buf, int buf_len, int flags, void* userdata);
LOCAL void _wifi_scan_done_hdl(char* buf, int buf_len, int flags, void* userdata);
void wifi_set_timer_scan(int ms) {
if(web_scan_handler_ptr.flg) {
if(xTimerChangePeriod(web_scan_handler_ptr.timer, ms, portMAX_DELAY) != pdPASS) {
error_printf("Error xTimerChangePeriod\n");
}
}
}
/* -------- WiFi Scan Close ------------------------- */
void wifi_close_scan(void)
{
internal_scan_handler_t * pscan_rec = &scan_result_handler_ptr;
web_scan_handler_t * pwscn_rec = &web_scan_handler_ptr;
printf("Close scan rec\n");
if(pscan_rec->scan_running) {
wifi_unreg_event_handler(WIFI_EVENT_SCAN_RESULT_REPORT, wifi_scan_each_report_hdl);
wifi_unreg_event_handler(WIFI_EVENT_SCAN_DONE, _wifi_scan_done_hdl);
if(pscan_rec->ap_details) rtw_free(pscan_rec->ap_details);
rtw_memset((void *) pscan_rec, 0, sizeof(internal_scan_handler_t));
}
if(pwscn_rec->flg) {
if(pwscn_rec->timer) xTimerDelete(pwscn_rec->timer, portMAX_DELAY);
if(pwscn_rec->ap_details) rtw_free(pwscn_rec->ap_details);
rtw_memset(pwscn_rec, 0, sizeof(web_scan_handler_t));
}
// pscan_rec->scan_complete = 1;
}
/* -------- WiFi Scan Done ------------------------- */
LOCAL void _wifi_scan_done_hdl(char* buf, int buf_len, int flags, void* userdata) {
internal_scan_handler_t * pscan_rec = &scan_result_handler_ptr;
web_scan_handler_t * pwscn_rec = &web_scan_handler_ptr;
if(pscan_rec->gscan_result_handler) {
// сторонний вывод
(*pscan_rec->gscan_result_handler)(pscan_rec);
}
else {
// оставить структуру pscan_rec->pap_details[i] для вывода в web scan на 5 сек
if(pwscn_rec->flg && pscan_rec->scan_cnt) {
debug_printf("\nScan done, wait read rec\n");
if(xTimerChangePeriod(pwscn_rec->timer, 5000, portMAX_DELAY) != pdPASS) {
// error_printf("Error xTimerChangePeriod\n");
} else {
if(pwscn_rec->ap_details) rtw_free(pwscn_rec->ap_details);
pwscn_rec->ap_details = pscan_rec->ap_details;
pwscn_rec->ap_count = pscan_rec->scan_cnt;
wifi_unreg_event_handler(WIFI_EVENT_SCAN_RESULT_REPORT, wifi_scan_each_report_hdl);
wifi_unreg_event_handler(WIFI_EVENT_SCAN_DONE, _wifi_scan_done_hdl);
rtw_memset((void *) pscan_rec, 0, sizeof(internal_scan_handler_t));
pwscn_rec->flg = 2;
}
return;
};
}
wifi_close_scan();
return;
}
/* -------- WiFi Scan Start ------------------------- */
LOCAL int _wifi_scan_networks(rtw_scan_result_handler_t results_handler) {
internal_scan_handler_t * pscan_rec = &scan_result_handler_ptr;
pscan_rec->gscan_result_handler = results_handler;
pscan_rec->max_ap_size = MAX_AP_SIZE;
pscan_rec->ap_details = (rtw_scan_result_t*) rtw_zmalloc(MAX_AP_SIZE * sizeof(rtw_scan_result_t) + MAX_AP_SIZE * sizeof(rtw_scan_result_t*));
if (pscan_rec->ap_details != NULL) {
pscan_rec->pap_details = (rtw_scan_result_t**) (&pscan_rec->ap_details[MAX_AP_SIZE]);
pscan_rec->scan_cnt = 0;
pscan_rec->scan_complete = RTW_FALSE;
pscan_rec->user_data = NULL;
wifi_reg_event_handler(WIFI_EVENT_SCAN_RESULT_REPORT, wifi_scan_each_report_hdl, NULL);
wifi_reg_event_handler(WIFI_EVENT_SCAN_DONE, _wifi_scan_done_hdl, NULL);
if(wext_set_scan(WLAN0_NAME, NULL, 0, RTW_SCAN_TYPE_ACTIVE | (RTW_SCAN_COMMAMD << 4) | (RTW_BSS_TYPE_ANY << 8)) == RTW_SUCCESS) {
return RTW_SUCCESS;
}
};
wifi_close_scan();
return RTW_ERROR;
}
/* -------- wext_set_pscan_channels ----------------- */
LOCAL int wext_set_pscan_channels(void) {
struct iwreq iwr;
int ret = -1;
__u8 *para;
int i = 0;
rtw_memset(&iwr, 0, sizeof(iwr));
//Format of para:function_name num_channel chan1... pscan_config1 ...
iwr.u.data.length = (SCAN_CHANNELS + SCAN_CHANNELS + 1) + 12;
para = rtw_malloc(iwr.u.data.length); //size:num_chan + num_time + length + function_name
iwr.u.data.pointer = para;
if (para != NULL) {
rtw_memcpy((char*) para, "PartialScan", 12); //Cmd
*(para + 12) = SCAN_CHANNELS; // length
for (i = 0; i < SCAN_CHANNELS; i++) {
*(para + 13 + i) = i + 1;
*((__u16 *) (para + 13 + SCAN_CHANNELS + i)) = PSCAN_ENABLE;
}
ret = iw_ioctl(WLAN0_NAME, SIOCDEVPRIVATE, &iwr);
rtw_free(para);
}
#if CONFIG_DEBUG_LOG > 3
else {
error_printf("%s: Can't malloc memory!\n", __func__);
}
#endif
return ret;
}
/* -------- WiFi Scan ------------------------------- */
rtw_result_t api_wifi_scan(api_scan_result_handler_t scan_result_cb)
{
internal_scan_handler_t * pscan_rec = &scan_result_handler_ptr;
web_scan_handler_t * pwscn_rec = &web_scan_handler_ptr;
if ((!pscan_rec->scan_running)
&& (!pwscn_rec->flg)) {
pscan_rec->scan_running = 1;
rtw_memset(pwscn_rec, 0, sizeof(web_scan_handler_t));
pwscn_rec->flg = 1;
debug_printf("\nStart scan...\n");
pwscn_rec->timer = xTimerCreate("webscan", 2500, pdFALSE, NULL, (TimerCallbackFunction_t)wifi_close_scan);
if(!pwscn_rec->timer) {
// error_printf("Error xTimerCreate\n");
} else if(xTimerStart(pwscn_rec->timer, portMAX_DELAY) != pdPASS) {
// error_printf("Error xTimerStart\n");
} else if(wext_set_pscan_channels() < 0) {
// error_printf("ERROR: wifi set partial scan channel fail\n");
} else if(_wifi_scan_networks(scan_result_cb) != RTW_SUCCESS) {
// error_printf("ERROR: wifi scan failed\n");
} else if(scan_result_cb) {
int i = 300;
while(i-- && pscan_rec->scan_running) {
vTaskDelay(10);
};
return RTW_SUCCESS;
} else
return RTW_SUCCESS;
wifi_close_scan();
return RTW_ERROR;
};
return RTW_TIMEOUT;
}

View file

@ -0,0 +1,42 @@
#define CONFIG_EXTERN_TEST 0
#define CONFIG_EXTERN_HW 0
#define CONFIG_EXTERN_CLOUD 0
#define CONFIG_TTCP 0
/* External Function */
#if CONFIG_EXTERN_TEST
extern void cmd_tcpecho(int argc, char **argv);
#endif
#if CONFIG_EXTERN_HW
extern void cmd_led(int argc, char **argv);
extern void cmd_tmp75(int argc, char **argv);
#endif
#if CONFIG_EXTERN_CLOUD
extern void cmd_cloud(int argc, char **argv);
extern void cmd_reboot(int argc, char **argv);
extern void cmd_config(int argc, char **argv);
#endif
#if CONFIG_TTCP
extern void cmd_ttcp(int argc, char **argv);
#endif
static const cmd_entry ext_cmd_table[] = {
#if CONFIG_EXTERN_TEST
{"tcpecho", cmd_tcpecho},
#endif
#if CONFIG_EXTERN_HW
{"led", cmd_led},
{"tmp75", cmd_tmp75},
#endif
#if CONFIG_EXTERN_CLOUD
{"cloud", cmd_cloud},
{"reboot", cmd_reboot},
{"config", cmd_config},
#endif
#if CONFIG_TTCP
{"ttcp", cmd_ttcp},
#endif
{"", NULL}
};

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,66 @@
/*
File: curve25519-donna.h
Package: WACServer
Version: WACServer-1.14
Disclaimer: IMPORTANT: This Apple software is supplied to you, by Apple Inc. ("Apple"), in your
capacity as a current, and in good standing, Licensee in the MFi Licensing Program. Use of this
Apple software is governed by and subject to the terms and conditions of your MFi License,
including, but not limited to, the restrictions specified in the provision entitled Public
Software, and is further subject to your agreement to the following additional terms, and your
agreement that the use, installation, modification or redistribution of this Apple software
constitutes acceptance of these additional terms. If you do not agree with these additional terms,
please do not use, install, modify or redistribute this Apple software.
Subject to all of these terms and in consideration of your agreement to abide by them, Apple grants
you, for as long as you are a current and in good-standing MFi Licensee, a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, and modify the Apple Software in source form, and to use, reproduce, modify, and
redistribute the Apple Software, with or without modifications, in binary form. While you may not
redistribute the Apple Software in source form, should you redistribute the Apple Software in binary
form, you must retain this notice and the following text and disclaimers in all such redistributions
of the Apple Software. Neither the name, trademarks, service marks, or logos of Apple Inc. may be
used to endorse or promote products derived from the Apple Software without specific prior written
permission from Apple. Except as expressly stated in this notice, no other rights or licenses,
express or implied, are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple Software may be
incorporated.
Unless you explicitly state otherwise, if you provide any ideas, suggestions, recommendations, bug
fixes or enhancements to Apple in connection with this software (Feedback), you hereby grant to
Apple a non-exclusive, fully paid-up, perpetual, irrevocable, worldwide license to make, use,
reproduce, incorporate, modify, display, perform, sell, make or have made derivative works of,
distribute (directly or indirectly) and sublicense, such Feedback in connection with Apple products
and services. Providing this Feedback is voluntary, but if you do provide Feedback to Apple, you
acknowledge and agree that Apple may exercise the license granted above without the payment of
royalties or further consideration to Participant.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR
IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR
IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2009 Apple Inc. All Rights Reserved.
*/
#ifndef __curve25519_donnaDotH__
#define __curve25519_donnaDotH__
#ifdef __cplusplus
extern "C" {
#endif
void curve25519_donna( unsigned char *outKey, const unsigned char *inSecret, const unsigned char *inBasePoint );
#ifdef __cplusplus
}
#endif
#endif // __curve25519_donnaDotH__

View file

@ -0,0 +1,220 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
This file contains the definitions required to use AES in C. See aesopt.h
for optimisation details.
*/
#ifndef _AES_H
#define _AES_H
#include <stdlib.h>
/* This include is used to find 8 & 32 bit unsigned integer types */
#include "rom_wac_brg_types.h"
/* Use AES encrypt/decrypt in wlan ROM codes */
#include "rom_aes.h"
extern int aes_set_key( aes_context *ctx, u8 *key, int nbits );
#if defined(__cplusplus)
extern "C"
{
#endif
#define AES_128 /* if a fast 128 bit key scheduler is needed */
#define AES_192 /* if a fast 192 bit key scheduler is needed */
#define AES_256 /* if a fast 256 bit key scheduler is needed */
#define AES_VAR /* if variable key size scheduler is needed */
#define AES_MODES /* if support is needed for modes */
/* The following must also be set in assembler files if being used */
#define AES_ENCRYPT /* if support for encryption is needed */
#define AES_DECRYPT /* if support for decryption is needed */
#define AES_REV_DKS /* define to reverse decryption key schedule */
#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
#define N_COLS 4 /* the number of columns in the state */
/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
/* or 44, 52 or 60 32-bit words. */
#if defined( AES_VAR ) || defined( AES_256 )
#define KS_LENGTH 60
#elif defined( AES_192 )
#define KS_LENGTH 52
#else
#define KS_LENGTH 44
#endif
#define AES_RETURN INT_RETURN
/* the character array 'inf' in the following structures is used */
/* to hold AES context information. This AES code uses cx->inf.b[0] */
/* to hold the number of rounds multiplied by 16. The other three */
/* elements can be used by code that implements additional modes */
typedef union
{ uint_32t l;
uint_8t b[4];
} aes_inf;
typedef struct
{
#if 0
uint_32t ks[KS_LENGTH];
#else
aes_context ctx;
#endif
aes_inf inf;
} aes_encrypt_ctx;
typedef struct
{
#if 0
uint_32t ks[KS_LENGTH];
#else
aes_context ctx;
#endif
aes_inf inf;
} aes_decrypt_ctx;
/* This routine must be called before first use if non-static */
/* tables are being used */
AES_RETURN aes_init(void);
/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
/* those in the range 128 <= key_len <= 256 are given in bits */
#if defined( AES_ENCRYPT )
#if defined( AES_128 ) || defined( AES_VAR)
AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
#endif
#if defined( AES_192 ) || defined( AES_VAR)
AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
#endif
#if defined( AES_256 ) || defined( AES_VAR)
AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
#endif
#if defined( AES_VAR )
AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
#endif
#if 0
AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
#else
extern void aes_encrypt( aes_context *ctx, u8 input[16], u8 output[16] );
#endif
#endif
#if defined( AES_DECRYPT )
#if defined( AES_128 ) || defined( AES_VAR)
AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
#endif
#if defined( AES_192 ) || defined( AES_VAR)
AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
#endif
#if defined( AES_256 ) || defined( AES_VAR)
AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
#endif
#if defined( AES_VAR )
AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
#endif
#if 0
AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
#else
extern void aes_decrypt( aes_context *ctx, u8 input[16], u8 output[16] );
#endif
#endif
#if defined( AES_MODES )
/* Multiple calls to the following subroutines for multiple block */
/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
/* long messages incremantally provided that the context AND the iv */
/* are preserved between all such calls. For the ECB and CBC modes */
/* each individual call within a series of incremental calls must */
/* process only full blocks (i.e. len must be a multiple of 16) but */
/* the CFB, OFB and CTR mode calls can handle multiple incremental */
/* calls of any length. Each mode is reset when a new AES key is */
/* set but ECB and CBC operations can be reset without setting a */
/* new key by setting a new IV value. To reset CFB, OFB and CTR */
/* without setting the key, aes_mode_reset() must be called and the */
/* IV must be set. NOTE: All these calls update the IV on exit so */
/* this has to be reset if a new operation with the same IV as the */
/* previous one is required (or decryption follows encryption with */
/* the same IV array). */
AES_RETURN aes_test_alignment_detection(unsigned int n);
AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
int len, const aes_encrypt_ctx cx[1]);
AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
int len, const aes_decrypt_ctx cx[1]);
AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);
AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
#define aes_ofb_encrypt aes_ofb_crypt
#define aes_ofb_decrypt aes_ofb_crypt
AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
typedef void cbuf_inc(unsigned char *cbuf);
#define aes_ctr_encrypt aes_ctr_crypt
#define aes_ctr_decrypt aes_ctr_crypt
AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
#endif
#if defined(__cplusplus)
}
#endif
#endif

View file

@ -0,0 +1,229 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
The unsigned integer types defined here are of the form uint_<nn>t where
<nn> is the length of the type; for example, the unsigned 32-bit type is
'uint_32t'. These are NOT the same as the 'C99 integer types' that are
defined in the inttypes.h and stdint.h headers since attempts to use these
types have shown that support for them is still highly variable. However,
since the latter are of the form uint<nn>_t, a regular expression search
and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t')
can be used to convert the types used here to the C99 standard types.
*/
#ifndef _BRG_TYPES_H
#define _BRG_TYPES_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <limits.h>
#if 0
#if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
# include <stddef.h>
# define ptrint_t intptr_t
#elif defined( __ECOS__ )
# define intptr_t unsigned int
# define ptrint_t intptr_t
#elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
# include <stdint.h>
# define ptrint_t intptr_t
#else
# define ptrint_t int
#endif
#else
# include <stdint.h>
# define ptrint_t intptr_t
#ifndef u8
typedef uint8_t u8;
#endif
#ifndef u32
typedef uint32_t u32;
#endif
#endif
#ifndef BRG_UI8
# define BRG_UI8
# if UCHAR_MAX == 255u
typedef unsigned char uint_8t;
# else
# error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h
# endif
#endif
#ifndef BRG_UI16
# define BRG_UI16
# if USHRT_MAX == 65535u
typedef unsigned short uint_16t;
# else
# error Please define uint_16t as a 16-bit unsigned short type in brg_types.h
# endif
#endif
#ifndef BRG_UI32
# define BRG_UI32
# if UINT_MAX == 4294967295u
# define li_32(h) 0x##h##u
typedef unsigned int uint_32t;
# elif ULONG_MAX == 4294967295u
# define li_32(h) 0x##h##ul
typedef unsigned long uint_32t;
# elif defined( _CRAY )
# error This code needs 32-bit data types, which Cray machines do not provide
# else
# error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h
# endif
#endif
#ifndef BRG_UI64
# if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
# define BRG_UI64
# define li_64(h) 0x##h##ui64
typedef unsigned __int64 uint_64t;
# elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */
# define BRG_UI64
# define li_64(h) 0x##h##ui64
typedef unsigned __int64 uint_64t;
# elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
# elif defined( __MVS__ )
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned int long long uint_64t;
# elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
# if UINT_MAX == 18446744073709551615u
# define BRG_UI64
# define li_64(h) 0x##h##u
typedef unsigned int uint_64t;
# endif
# elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
# if ULONG_MAX == 18446744073709551615ul
# define BRG_UI64
# define li_64(h) 0x##h##ul
typedef unsigned long uint_64t;
# endif
# elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
# if ULLONG_MAX == 18446744073709551615ull
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
# endif
# elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
# if ULONG_LONG_MAX == 18446744073709551615ull
# define BRG_UI64
# define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
# endif
# endif
#endif
#if !defined( BRG_UI64 )
# if defined( NEED_UINT_64T )
# error Please define uint_64t as an unsigned 64 bit type in brg_types.h
# endif
#endif
#ifndef RETURN_VALUES
# define RETURN_VALUES
# if defined( DLL_EXPORT )
# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
# define VOID_RETURN __declspec( dllexport ) void __stdcall
# define INT_RETURN __declspec( dllexport ) int __stdcall
# elif defined( __GNUC__ )
# define VOID_RETURN __declspec( __dllexport__ ) void
# define INT_RETURN __declspec( __dllexport__ ) int
# else
# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
# endif
# elif defined( DLL_IMPORT )
# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
# define VOID_RETURN __declspec( dllimport ) void __stdcall
# define INT_RETURN __declspec( dllimport ) int __stdcall
# elif defined( __GNUC__ )
# define VOID_RETURN __declspec( __dllimport__ ) void
# define INT_RETURN __declspec( __dllimport__ ) int
# else
# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
# endif
# elif defined( __WATCOMC__ )
# define VOID_RETURN void __cdecl
# define INT_RETURN int __cdecl
# else
# define VOID_RETURN void
# define INT_RETURN int
# endif
#endif
/* These defines are used to detect and set the memory alignment of pointers.
Note that offsets are in bytes.
ALIGN_OFFSET(x,n) return the positive or zero offset of
the memory addressed by the pointer 'x'
from an address that is aligned on an
'n' byte boundary ('n' is a power of 2)
ALIGN_FLOOR(x,n) return a pointer that points to memory
that is aligned on an 'n' byte boundary
and is not higher than the memory address
pointed to by 'x' ('n' is a power of 2)
ALIGN_CEIL(x,n) return a pointer that points to memory
that is aligned on an 'n' byte boundary
and is not lower than the memory address
pointed to by 'x' ('n' is a power of 2)
*/
#define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1))
#define ALIGN_FLOOR(x,n) ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
#define ALIGN_CEIL(x,n) ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))
/* These defines are used to declare buffers in a way that allows
faster operations on longer variables to be used. In all these
defines 'size' must be a power of 2 and >= 8. NOTE that the
buffer size is in bytes but the type length is in bits
UNIT_TYPEDEF(x,size) declares a variable 'x' of length
'size' bits
BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize'
bytes defined as an array of variables
each of 'size' bits (bsize must be a
multiple of size / 8)
UNIT_CAST(x,size) casts a variable to a type of
length 'size' bits
UPTR_CAST(x,size) casts a pointer to a pointer to a
varaiable of length 'size' bits
*/
#define UI_TYPE(size) uint_##size##t
#define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x
#define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)]
#define UNIT_CAST(x,size) ((UI_TYPE(size) )(x))
#define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x))
#if defined(__cplusplus)
}
#endif
#endif

View file

@ -0,0 +1,23 @@
#ifndef GOOGLENEST_H
#define GOOGLENEST_H
#include <polarssl/ssl.h>
typedef struct {
int socket;
char *host;
ssl_context ssl;
} googlenest_context;
int gn_connect(googlenest_context *googlenest, char *host, int port);
void gn_close(googlenest_context *googlenest);
int gn_put(googlenest_context *googlenest, char *uri, char *content);
int gn_patch(googlenest_context *googlenest, char *uri, char *content);
int gn_post(googlenest_context *googlenest, char *uri, char *content, unsigned char *out_buffer, size_t out_len);
int gn_get(googlenest_context *googlenest, char *uri, unsigned char *out_buffer, size_t out_len);
int gn_delete(googlenest_context *googlenest, char *uri);
int gn_stream(googlenest_context *googlenest, char *uri);
void google_retrieve_data_hook_callback(void (*callback)(char *));
#endif

View file

@ -0,0 +1,679 @@
/*******************************example_joinlink **************************/
#include "autoconf.h"
#include "platform_stdlib.h"
#include "wifi_conf.h"
#include "wifi_structures.h"
#include "osdep_service.h"
#include "lwip_netconf.h"
#include "task.h"
#include "joinlink.h"
#include "cJSON.h"
#include <lwip/sockets.h>
#include <lwip/raw.h>
#include <lwip/icmp.h>
#include <lwip/inet_chksum.h>
#include <platform/platform_stdlib.h>
#define MASK_SIZE_JOINLINK 3
#define SOURCE_PORT 101
//gloable
static unsigned char cur_channel = 1;
static unsigned char lock_channel = 1;
static _timer timer_handler_phase2;
static _timer timer_handler_phase1;
static u8 joinlink_finished = 0;
static u8 security_type = 0xff;
static u8 jl_rx_flag = 0;
static rtw_scan_result_t *all_channel_scan_result = NULL;
static rtw_scan_result_t *p_result = NULL;
static int all_channel_ret = 0;
static int phase1_finished = 0;
static int phase2_started = 0;
static u32 start_time = 0;
static u32 current_time = 0;
static int idx = 1;
static int phase1_scanned_channel[14];
static char ap_bssid[6];
static char aes_key[] = "123456789";
static void* pre_scan_sema;
static int ack_socket;
static struct sockaddr_in to_addr;
static struct sockaddr_in from_addr;
static char header_cmd[] = "cmd";
static cJSON *ack_content = NULL;
extern struct netif xnetif[];
void example_joinlink(void);
static rtw_result_t joinlink_scan_result_handler(rtw_scan_handler_result_t* malloced_scan_result )
{
static int ApNum = 0;
//TODO: add timer of 2s, wf, 1021
if (malloced_scan_result->scan_complete != RTW_TRUE) {
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
++ApNum;
if(malloced_scan_result->user_data)
memcpy((void *)((char *)malloced_scan_result->user_data+(ApNum-1)*sizeof(rtw_scan_result_t)), (char *)record, sizeof(rtw_scan_result_t));
}
// scan finished, wf, 1022
else
{
rtw_up_sema(&pre_scan_sema);
ApNum = 0;
}
return RTW_SUCCESS;
}
void* joinlink_all_scan()
{
int ret = 0;
rtw_scan_result_t *joinlink_scan_buf = NULL;
if(joinlink_scan_buf != NULL)
free(joinlink_scan_buf);
joinlink_scan_buf = (rtw_scan_result_t *)malloc(65*sizeof(rtw_scan_result_t));
if(joinlink_scan_buf == NULL){
return 0;
}
memset(joinlink_scan_buf, 0, 65*sizeof(rtw_scan_result_t));
if((ret = wifi_scan_networks(joinlink_scan_result_handler, joinlink_scan_buf)) != RTW_SUCCESS){
printf("[ATWS]ERROR: wifi scan failed\n\r");
free(joinlink_scan_buf);
return 0;
}
return joinlink_scan_buf;
}
void joinlink_deinit_content()
{
rtw_del_timer(&timer_handler_phase2);
rtw_del_timer(&timer_handler_phase1);
if(all_channel_scan_result)
{
free(all_channel_scan_result);
all_channel_scan_result = NULL;
}
rtw_free_sema(&pre_scan_sema);
joinlink_deinit();
return;
}
static char *jl_itoa(int value)
{
char *val_str;
int tmp = value, len = 1;
while((tmp /= 10) > 0)
len ++;
val_str = (char *) malloc(len + 1);
sprintf(val_str, "%d", value);
return val_str;
}
static void get_ip_str(int *ip_int, char *ip_ch)
{
char *ip_single = NULL;
u8 pos = 0, len = 0;
for(int i = 0; i < 4; i++)
{
ip_single = jl_itoa(ip_int[i]);
len = strlen(ip_single);
memcpy(ip_ch + pos, ip_single,len);
free(ip_single);
ip_single = NULL;
pos += len;
if(i == 3)
{
*(ip_ch + pos) = 0;
break;
}
*(ip_ch + pos) = '.';
pos++;
}
}
static int joinlink_set_ack_content(u8 check_sum)
{
cJSON_Hooks memoryHook;
memoryHook.malloc_fn = malloc;
memoryHook.free_fn = free;
cJSON_InitHooks(&memoryHook);
if(ack_content != NULL)
{
cJSON_Delete(ack_content);
ack_content = NULL;
}
if((ack_content = cJSON_CreateObject()) != NULL)
{
char mac_str[18];
u8 pos = 0;
memset(mac_str, 0, sizeof(mac_str));
for(int i = 0; i < 6; i++)
{
sprintf(mac_str + pos, "%02x", xnetif[0].hwaddr[i]);
pos += 2;
if(i != 5)
mac_str[pos++] = ':';
}
cJSON_AddItemToObject(ack_content, "deviceid", cJSON_CreateString(mac_str));
cJSON_AddItemToObject(ack_content, "code", cJSON_CreateNumber(check_sum));
}
else
{
printf("create jSON object failure\n");
return -1;
}
return 0;
}
#if 1
static void recv_cmd(void *para)
{
int rev_len = 0;
char pkt_buf[16];
while(1)
{
vTaskDelay(500);
if((rev_len = recvfrom(ack_socket, pkt_buf, sizeof(pkt_buf), 0, NULL, NULL)) >= 0)
{
if(memcmp(pkt_buf, header_cmd, sizeof(header_cmd)) == 0)
{
printf("received reboot command, restart join_link process\n");
// do we need to reboot?
example_joinlink();
close(ack_socket);
break;
}
}
}
vTaskDelete(NULL);
return;
}
static int send_ack(int *dest_ip, int dest_port, u8 sum)
{
#if CONFIG_LWIP_LAYER
int ack_transmit_round;
char ip[16];
char *jsonString = NULL;
int sended_data = 0;
if(joinlink_set_ack_content(sum) == -1)
return -1;
jsonString = cJSON_Print(ack_content);
if(jsonString == NULL)
{
printf("json string convert failure\n");
cJSON_Delete(ack_content);
return -1;
}
get_ip_str(dest_ip, ip);
ack_socket = socket(PF_INET, SOCK_DGRAM, IP_PROTO_UDP);
if (ack_socket == -1) {
printf("create socket failure\n");
return -1;
}
FD_ZERO(&to_addr);
to_addr.sin_family = AF_INET;
to_addr.sin_port = htons(dest_port);
to_addr.sin_addr.s_addr = htonl(INADDR_ANY);
FD_ZERO(&from_addr);
from_addr.sin_family = AF_INET;
from_addr.sin_port = htons(SOURCE_PORT);
to_addr.sin_addr.s_addr = inet_addr(ip);
if(bind(ack_socket, (struct sockaddr *)&from_addr, sizeof(from_addr)) < 0)
{
printf("bind to source port error\n");
return -1;
}
for (ack_transmit_round = 0; ack_transmit_round < 5; ack_transmit_round++) {
sended_data = sendto(ack_socket, (unsigned char *)jsonString, strlen(jsonString), 0, (struct sockaddr *) &to_addr, sizeof(struct sockaddr));
//printf("\r\nAlready send %d bytes data\n", sended_data);
vTaskDelay(100); /* delay 100 ms */
}
if(xTaskCreate(recv_cmd, (char const *)"recv_cmd", 1512, NULL, tskIDLE_PRIORITY + 2, NULL) != pdPASS)
printf("%s xTaskCreate failed\n", __FUNCTION__);
close(ack_socket);
if(jsonString)
{
free(jsonString);
jsonString = NULL;
}
cJSON_Delete(ack_content);
#endif
return 0;
}
#endif
static void remove_filter()
{
wifi_disable_packet_filter(1);
wifi_disable_packet_filter(2);
wifi_remove_packet_filter(1);
wifi_remove_packet_filter(2);
}
int joinlink_finish(unsigned char security_type)
{
int ret = 0;
int retry = 3;
unsigned char pscan_config = 1;
joinlink_result_t result;
rtw_security_t security_mode;
wifi_set_promisc(RTW_PROMISC_DISABLE,NULL,0);
remove_filter();
pscan_config = PSCAN_ENABLE | PSCAN_SIMPLE_CONFIG;
ret = joinlink_get_result(&result);
if (ret == 0) {
printf("get result OK\n");
//printf("\r\n joinlink get result ok,ssid = %s, pwd = %s,ssid length = %d,pwd length = %d",
// result.ssid, result.pwd, result.ssid_length,result.pwd_length);
}
else{
printf("joinlink result not get!\n");
joinlink_deinit_content();
return -1;
}
//ap security type
switch(security_type){
case RTW_ENCRYPTION_OPEN:
security_mode = RTW_SECURITY_OPEN;
break;
case RTW_ENCRYPTION_WEP40:
case RTW_ENCRYPTION_WEP104:
security_mode = RTW_SECURITY_WEP_PSK;
break;
case RTW_ENCRYPTION_WPA_TKIP:
case RTW_ENCRYPTION_WPA_AES:
case RTW_ENCRYPTION_WPA2_TKIP:
case RTW_ENCRYPTION_WPA2_AES:
case RTW_ENCRYPTION_WPA2_MIXED:
security_mode = RTW_SECURITY_WPA2_AES_PSK;
break;
case RTW_ENCRYPTION_UNKNOWN:
case RTW_ENCRYPTION_UNDEF:
default:
printf("unknow security mode,connect fail!\n");
}
#if 1
while(1){
if(wifi_set_pscan_chan(&lock_channel, &pscan_config, 1) < 0){
printf("ERROR: wifi set partial scan channel fail\n");
break;
}
printf("wifi_connect\n");
//printf("ap_bssid: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", ap_bssid[0],ap_bssid[1],ap_bssid[2],ap_bssid[3],ap_bssid[4],ap_bssid[5]);
ret = wifi_connect(
NULL,
0,
(unsigned char *)result.ssid,
security_mode,
(unsigned char *)result.pwd,
0,
NULL);
if(ret == RTW_SUCCESS){
printf("Connect ok!\n");
#if CONFIG_LWIP_LAYER
/* If not rise priority, LwIP DHCP may timeout */
vTaskPrioritySet(NULL, tskIDLE_PRIORITY + 3);
/* Start DHCP Client */
ret = LwIP_DHCP(0, DHCP_START);
vTaskPrioritySet(NULL, tskIDLE_PRIORITY + 1);
#endif
break;
}
if (retry == 0) {
break;
}
retry--;
}
if(send_ack(result.source_ip, result.source_port, result.sum) != 0)
printf("send ack failure\n");
#endif
joinlink_deinit_content();
return 0;
}
// handler for phase2
void timer_handler_phase2_func(void *FunctionContext)
{
// do not switch channel while handle frames, wf, 1021
if(jl_rx_flag){
rtw_set_timer(&timer_handler_phase2, CHANNEL_SWITCH_TIME - 25);
} else {
if(cur_channel >= 13)
{cur_channel = 1;}
else
cur_channel ++;
wifi_set_channel(cur_channel);
rtw_set_timer(&timer_handler_phase2, CHANNEL_SWITCH_TIME);
}
//printf("phase2:wifi switch channel to %d\n",cur_channel);
return;
}
// timer handler for the 1st phase, wf, 1022
void timer_handler_phase1_func(void *FunctionContext)
{
// do not switch channel while handle frames, wf, 1021
if(jl_rx_flag){
rtw_set_timer(&timer_handler_phase1, SSID_SWITCH_TIME - 25);
}
// switch ssid, wf, 1022
else
{
if(idx >= 14)
{
phase1_finished = 1;
printf("wifi: phase1 scan finished\n");
printf("wifi: start phase2 scan\n");
// move from pkt handler to here in case no pkt to trigue phase2
#if 1
if(phase1_finished)
{
phase1_finished = 0;
phase2_started = 1;
rtw_cancel_timer(&timer_handler_phase1);
//start phase2 for ch1~ch13
cur_channel = 1;
wifi_set_channel(cur_channel);
rtw_init_timer(&timer_handler_phase2, NULL, &timer_handler_phase2_func, NULL, "phase_2");
rtw_set_timer(&timer_handler_phase2, CHANNEL_SWITCH_TIME);
}
#endif
return;
}
while(idx < 14)
{
if(phase1_scanned_channel[idx])
{
wifi_set_channel(idx);
rtw_set_timer(&timer_handler_phase1, SSID_SWITCH_TIME);
//printf("phase1: wifi switch channel to %d\n",idx);
idx++;
break;
}
else
{
if(idx == 13)
rtw_set_timer(&timer_handler_phase1, SSID_SWITCH_TIME);
idx++;
}
}
}
return;
}
static void rtl_frame_recv_handle(unsigned char *buf, int len, unsigned char *da, unsigned char *sa, void *user_data) {
int ret = 0;
int fixed_channel;
char scanned_ssid[50] = {0};
unsigned char *current_bssid = NULL;
int scanned_ssid_len = 0;
//set this flag prevent joinlink_recv interruptted by timer,since timer has higher priority
jl_rx_flag = 1;
if (joinlink_finished) {
jl_rx_flag = 0;
return;
}
ret = joinlink_recv(da, sa, len, user_data);
if(ret == JOINLINK_STATUS_CHANNEL_LOCKED)
{
if(phase2_started)
{
phase2_started = 0;
rtw_cancel_timer(&timer_handler_phase2);
}
else
rtw_cancel_timer(&timer_handler_phase1);
lock_channel = cur_channel;
security_type = ((ieee80211_frame_info_t *)user_data)->encrypt;
printf("JoinLink locked to channel[%d]\n",lock_channel);
current_bssid = buf + 4 + ETH_ALEN;
memcpy(ap_bssid, current_bssid, 6);
fixed_channel = promisc_get_fixed_channel(current_bssid, scanned_ssid, &scanned_ssid_len);
if (fixed_channel != 0) {
printf("JoinLink force fixed to channel[%d]\r\n",fixed_channel);
printf("JoinLink ssid scanned[%s]\r\n",scanned_ssid);
wifi_set_channel(fixed_channel);
}
}
else if(ret == JOINLINK_STATUS_COMPLETE){
//wifi_set_promisc(RTW_PROMISC_DISABLE,NULL,0);
joinlink_finished = 1;
printf("quit promisc mode!\r\n");
}
//release flag
jl_rx_flag = 0;
return;
}
// callback for promisc packets, like rtk_start_parse_packet in SC, wf, 1021
void wifi_promisc_rx(unsigned char* buf, unsigned int len, void* user_data)
{
unsigned char * da = buf;
unsigned char * sa = buf + ETH_ALEN;
if (joinlink_finished)
return;
rtl_frame_recv_handle(buf, len, da, sa, user_data);
return;
}
// the entry point for joinlink
void joinlink_process(void *param)
{
while(1)
{
current_time = xTaskGetTickCount();
if(joinlink_finished)
{
printf("joinlink finished\n");
break;
}
if((current_time - start_time) > JOINLINK_TIME * configTICK_RATE_HZ)
{
printf("joinlink timeout\n");
break;
}
vTaskDelay(500);
}
joinlink_finish(security_type);
vTaskDelete(NULL);
return;
}
int joinlink_init_content()
{
int ret = 0;
ret = joinlink_init();
if(ret < 0){
printf("JoinLink init failed!\n");
return ret;
}
memset(phase1_scanned_channel, 0, sizeof(phase1_scanned_channel));
security_type = 0xff;
cur_channel = 1;
lock_channel = 1;
joinlink_finished = 0;
jl_rx_flag = 0;
p_result = NULL;
all_channel_ret = 0;
phase1_finished = 0;
phase2_started = 0;
idx = 1;
rtw_init_sema(&pre_scan_sema, 0);
memset(ap_bssid, 0, sizeof(ap_bssid));
set_aes_key(aes_key, sizeof(aes_key) - 1);
return 0;
}
// ret:1 indicate suc, else fail
int scan_all_channel()
{
all_channel_scan_result = (rtw_scan_result_t *)joinlink_all_scan();
if(all_channel_scan_result == NULL)
return 0;
else
return 1;
}
void get_phase1_channel()
{
p_result = all_channel_scan_result;
while(p_result->channel)
{
if((p_result->channel >= 1) && (p_result->channel <= 13))
phase1_scanned_channel[p_result->channel] = 1;
p_result++;
}
return;
}
// now only accept mcast and bcast pkt
static void filter_add_enable()
{
u8 mask[MASK_SIZE_JOINLINK]={0xFF,0xFF,0xFF};
u8 pattern[MASK_SIZE_JOINLINK]={0x01,0x00,0x5e};
u8 pattern_bcast[MASK_SIZE_JOINLINK]={0xff,0xff,0xff};
rtw_packet_filter_pattern_t packet_filter;
rtw_packet_filter_pattern_t packet_filter_bcast;
rtw_packet_filter_rule_e rule;
packet_filter.offset = 0;
packet_filter.mask_size = 3;
packet_filter.mask = mask;
packet_filter.pattern = pattern;
packet_filter_bcast.offset = 0;
packet_filter_bcast.mask_size = 3;
packet_filter_bcast.mask = mask;
packet_filter_bcast.pattern = pattern_bcast;
rule = RTW_POSITIVE_MATCHING;
wifi_init_packet_filter();
wifi_add_packet_filter(1, &packet_filter,rule);
wifi_add_packet_filter(2, &packet_filter_bcast,rule);
wifi_enable_packet_filter(1);
wifi_enable_packet_filter(2);
}
void joinlink_start(void *param)
{
joinlink_finished = 0;
start_time = xTaskGetTickCount();
if(xTaskCreate(joinlink_process, (char const *)"JoinLink", 1512, NULL, tskIDLE_PRIORITY + 2, NULL) != pdPASS)
printf("%s xTaskCreate failed\n", __FUNCTION__);
if (joinlink_init_content() < 0)
printf("joinlink init fail!\n");
while(1)
{
if(wifi_is_ready_to_transceive(RTW_STA_INTERFACE) == RTW_SUCCESS)
break;
else
vTaskDelay(3000);
}
all_channel_ret = scan_all_channel();
if (rtw_down_sema(&pre_scan_sema) == _FAIL)
printf("%s, Take Semaphore Fail\n", __FUNCTION__);
//printf("\npre scan finished\n");
//set wifi to station mode,enable promisc mode and timer to change channel
wifi_enter_promisc_mode();
filter_add_enable();
/* enable all 802.11 packets*/
wifi_set_promisc(RTW_PROMISC_ENABLE, wifi_promisc_rx, 1);
//init timer handler,and set timer hanler funcion
if(all_channel_ret)
{
printf("\nstart the phase1 scan\n");
get_phase1_channel();
rtw_init_timer(&timer_handler_phase1, NULL, &timer_handler_phase1_func, NULL, "phase1_timer");
rtw_set_timer(&timer_handler_phase1, SSID_SWITCH_TIME);
}
else
{
printf("phase1 scan fail, start phase2 scan\n");
rtw_init_timer(&timer_handler_phase2, NULL, &timer_handler_phase2_func, NULL, "phase2_timer");
wifi_set_channel(cur_channel);
rtw_set_timer(&timer_handler_phase2, CHANNEL_SWITCH_TIME);
}
vTaskDelete(NULL);
return;
}
void example_joinlink(void)
{
if(xTaskCreate(joinlink_start, (char const *)"JoinLink_entry", 1512, NULL, tskIDLE_PRIORITY + 2, NULL) != pdPASS)
printf("%s xTaskCreate failed\n", __FUNCTION__);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,64 @@
/******************************* joinlink **************************/
#ifndef __JOINLINK_H
#define __JOINLINK_H
#include "autoconf.h"
#include "platform_stdlib.h"
#include "wifi_conf.h"
#include "wifi_structures.h"
#include "osdep_service.h"
#include "lwip_netconf.h"
#include "task.h"
#include "hal_crypto.h"
#define SSID_SWITCH_TIME 500 //ssid switch time in phase1,units:ms, 50
#define CHANNEL_SWITCH_TIME 500 //channel switch time in phase2,units:ms, 50
#define JOINLINK_TIME 120 //timeout for joinlink process, units: s
/*
* store AP profile after successfully decode
* SUM +£¨length£¬pass£©+£¨IP+Port£©+£¨length£¬SSID)
*/
typedef struct
{
unsigned char sum;
unsigned char pwd_length;
char pwd[65];
int source_ip[4];
unsigned int source_port;
unsigned char ssid_length;
char ssid[65];
} joinlink_result_t;
/*
* return value of joinlink_recv()
*/
typedef enum
{
JOINLINK_STATUS_CONTINUE = 0,
JOINLINK_STATUS_CHANNEL_LOCKED = 1,
JOINLINK_STATUS_COMPLETE = 2
} joinlink_status_t;
//initialize the related data structure
int joinlink_init();
/*
handler to decode pkt
*/
joinlink_status_t joinlink_recv(unsigned char *da, unsigned char *sa, int len, void *user_data);
/*
* get the AP profile after decode
*/
int joinlink_get_result(joinlink_result_t *result);
/*
* set the aes_key, the max len should be 16
* ret 1: success; ret 0: the len is invalid;
*/
int set_aes_key(char *key, int len);
// call this after finish join_link process
void joinlink_deinit();
#endif //__JOINLINK_H

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,272 @@
#include "osdep_api.h"
#include "serial_api.h"
#include <timer_api.h>
#include "freertos_pmu.h"
#include <mDNS/mDNS.h>
#if CONFIG_UART_SOCKET
/******************************************************
* Macros
******************************************************/
#define UA_ERROR 0
#define UA_WARNING 1
#define UA_INFO 2
#define UA_DEBUG 2 //pvvx! было 3
#define UA_NONE 0xFF
#define UA_DEBUG_LEVEL UA_INFO
#define UA_UART_THREAD_PRIORITY 5
#define UA_UART_THREAD_STACKSIZE 512
#define UA_TCP_SERVER_FD_NUM 1
#define UA_TCP_CLIENT_FD_NUM 1
#define UA_UART_RECV_BUFFER_LEN 8196
#define UA_UART_FRAME_LEN 1400
#define UA_UART_MAX_DELAY_TIME 100
#define UA_CHAT_SOCKET_PORT 5001
#define UA_CONTROL_SOCKET_PORT 6001
#define UA_UART_TX_PIN PA_4
#define UA_UART_RX_PIN PA_0
#define UA_UART_RTS_PIN PA_2
#define UA_UART_CTS_PIN PA_1
#define UA_GPIO_LED_PIN PC_5
#define UA_GPIO_IRQ_PIN PC_4
#define UA_CONTROL_PREFIX "AMEBA_UART"
#define UA_PS_ENABLE 0
#define UA_GPIO_WAKEUP_PIN PC_3
#define UA_WAKELOCK WAKELOCK_USER_BASE
#define UA_FAST_RECONNECT_TCP_DATA (0x80000 - 0x2000)
#if (UA_DEBUG_LEVEL== UA_NONE)
#define ua_printf(level, fmt, arg...)
#else
#define ua_printf(level, fmt, arg...) \
do {\
if (level <= UA_DEBUG_LEVEL) {\
if (level <= UA_ERROR) {\
RtlDownSema(&ua_print_sema);\
printf("\r\nERROR: " fmt, ##arg);\
RtlUpSema(&ua_print_sema);\
} \
else {\
RtlDownSema(&ua_print_sema);\
printf("\r\n"fmt, ##arg);\
RtlUpSema(&ua_print_sema);\
} \
}\
}while(0)
#endif
#define UA_PRINT_DATA(_HexData, _HexDataLen) \
if(UA_DEBUG_LEVEL == UA_DEBUG) \
{ \
int __i; \
u8 *ptr = (u8 *)_HexData; \
printf("--------Len=%d\n\r", _HexDataLen); \
for( __i=0; __i<(int)_HexDataLen; __i++ ) \
{ \
printf("%02X%s", ptr[__i], (((__i + 1) % 4) == 0)?" ":" "); \
if (((__i + 1) % 16) == 0) printf("\n\r"); \
} \
printf("\n\r"); \
}
#define UA_SOCKET_CHECK(_ua_socket) \
if(_ua_socket == NULL) \
{ \
printf("ERROR: ua_socket = NULL\n\r"); \
return; \
}
#define UA_SOCKET_CHECK_2(_ua_socket) \
if(_ua_socket == NULL) \
{ \
printf("ERROR: ua_socket = NULL\n\r"); \
return -1; \
}
/******************************************************
* Constants
******************************************************/
typedef enum
{
UART_ADAPTER_LED_ON = 0,
UART_ADAPTER_LED_OFF = 1,
UART_ADAPTER_LED_FAST_TWINKLE = 2,
UART_ADAPTER_LED_SLOW_TWINKLE = 3,
}ua_led_mode_t;
typedef enum
{
UART_CTRL_MODE_SET_REQ = 0,
UART_CTRL_MODE_SET_RSP = 1,
UART_CTRL_MODE_GET_REQ = 2,
UART_CTRL_MODE_GET_RSP = 3,
}ua_ctrl_mode_t;
typedef enum
{
UART_CTRL_TYPE_BAUD_RATE = 0x01,
UART_CTRL_TYPE_WORD_LEN = 0x02,
UART_CTRL_TYPE_PARITY = 0x04,
UART_CTRL_TYPE_STOP_BIT = 0x08,
UART_CTRL_TYPE_TCP_SERVER_CREATE = 0x10,
UART_CTRL_TYPE_TCP_SERVER_DELETE = 0x20,
UART_CTRL_TYPE_TCP_CLIENT_CONNECT = 0x40,
UART_CTRL_TYPE_TCP_CLIENT_DISCONNECT = 0x80,
UART_CTRL_TYPE_TCP_GROUP_ID = 0x100,
}ua_ctrl_type_t;
enum sc_result {
SC_ERROR = -1, /* default error code*/
SC_NO_CONTROLLER_FOUND = 1, /* cannot get sta(controller) in the air which starts a simple config session */
SC_CONTROLLER_INFO_PARSE_FAIL, /* cannot parse the sta's info */
SC_TARGET_CHANNEL_SCAN_FAIL, /* cannot scan the target channel */
SC_JOIN_BSS_FAIL, /* fail to connect to target ap */
SC_DHCP_FAIL, /* fail to get ip address from target ap */
/* fail to create udp socket to send info to controller. note that client isolation
must be turned off in ap. we cannot know if ap has configured this */
SC_UDP_SOCKET_CREATE_FAIL,
SC_SUCCESS, /* default success code */
};
/******************************************************
* Structures
******************************************************/
typedef struct _ua_uart_param_t
{
u8 WordLen;
u8 Parity;
u8 StopBit;
u8 FlowControl;
int BaudRate;
}ua_uart_param_t;
typedef struct _ua_uart_socket_t
{
int fd;
char rcv_ch;
volatile char overlap;
int recv_bytes;
volatile int pread;
volatile int pwrite;
volatile unsigned int tick_last_update;
unsigned int tick_current;
volatile int tx_busy;
volatile int uart_ps;
volatile int uart_ps_cnt;
char recv_buf[UA_UART_RECV_BUFFER_LEN];
long rx_cnt;
long miss_cnt;
serial_t uart_sobj;
ua_uart_param_t uart_param;
_Sema action_sema;
_Sema tcp_tx_rx_sema;
_Sema dma_tx;
}ua_uart_socket_t;
typedef struct _ua_tcp_socket_t
{
int chat_socket;
int control_socket;
int chat_server_listen_socket;
int control_server_listen_socket;
int transmit_recv_socket;
int transmit_send_socket;
int transmit_server_listen_socket;
int group_id;
u32 server_port;
u32 client_port;
char client_ip[16];
int send_flag;
int recv_flag;
long rx_cnt;
long tx_cnt;
volatile int tcp_ps;
volatile int tcp_ps_cnt;
}ua_tcp_socket_t;
typedef struct _ua_gpio_t
{
gpio_t gpio_led;
gpio_t gpio_btn;
gpio_irq_t gpio_btn_irq;
gtimer_t gpio_timer;
}ua_gpio_t;
typedef struct _ua_socket_t
{
ua_uart_socket_t uart;
ua_tcp_socket_t tcp;
ua_gpio_t gpio;
ip_addr_t ip;
DNSServiceRef dnsServiceRef;
DNSServiceRef dnsServiceRef2;
}ua_socket_t;
typedef struct _ua_mbox_buffer
{
char data[UA_UART_FRAME_LEN];
int data_len;
}ua_mbox_buffer_t;
//Save Uart Settings when get uart information
typedef struct _ua_uart_get_str
{
int BaudRate; //The baud rate
char number; //The number of data bits
char parity; //The parity(0: none, 1:odd, 2:evn, default:0)
char StopBits; //The number of stop bits
char FlowControl; //support flow control is 1
}ua_uart_get_str;
//Uart Setting information
typedef struct _ua_uart_set_str
{
char UartName[8]; // the name of uart
int BaudRate; //The baud rate
char number; //The number of data bits
char parity; //The parity(default NONE)
char StopBits; //The number of stop bits
char FlowControl; //support flow control is 1
}ua_uart_set_str;
int uartadapter_init();
void uartadapter_tcp_send_data(ua_socket_t *ua_socket, char *buffer, int size);
void uartadapter_tcp_send_control(ua_socket_t *ua_socket, char *buffer, int size);
void uartadapter_tcp_transmit_server_thread(void *param);
void uartadapter_tcp_transmit_client_thread(void *param);
int uartadapter_tcpclient(ua_socket_t *ua_socket, const char *host_ip, unsigned short usPort);
void uartadapter_tcp_transmit_client_forever_thread(void *param);
void example_uart_adapter_init();
void cmd_uart_adapter(int argc, char **argv);
void uartadapter_tcp_transmit_socket_handler(ua_socket_t *ua_socket, char *tcp_rxbuf);
#endif // CONFIG_UART_SOCKET

View file

@ -0,0 +1,83 @@
#include "FreeRTOS.h"
#include "task.h"
#include "wifi_conf.h"
#include "wifi_ind.h"
#include "google_nest.h"
#include "flash_api.h"
#include "wigadget.h"
#include <lwip/netif.h>
#include "lwip_netconf.h"
#include "shtc1.h"
#define CLOUD_PORT 443
extern struct netif xnetif[NET_IF_NUM];
void cloud_link_task(void *param){
googlenest_context googlenest;
unsigned char URI[64];
unsigned char data[97] = {0};
unsigned char host_addr[64] = {0};
flash_t cloud_flash;
u8 *mac = (u8 *)LwIP_GetMAC(&xnetif[0]);
char i[16], j[16];
float temperature = 1.123f;
float humidity = 2.456f;
int ret = 0;
vTaskDelay(2000);
sprintf(URI, "ht_sensor/%02x%02x%02x%02x%02x%02x.json", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
memset(host_addr, 0, sizeof(host_addr));
if(flash_stream_read(&cloud_flash, FLASH_IOT_DATA, 97, (uint8_t *) data) == 1){
memset(host_addr, 0 , 64);
memcpy(host_addr, data+33, 64);
while(1) {
printf("\n=====>START CLOUD LINKING\n");
memset(i, 0, 16);
memset(j, 0, 16);
#if PSEUDO_DATA
sprintf(i,"%.2f", temperature++);
sprintf(j, "%.2f", humidity++);
if(temperature > 60)
temperature = 1.123f;
if(humidity > 98)
humidity = 2.456f;
#else
ret = SHTC_GetTempAndHumi(&temperature, &humidity);
sprintf(i,"%.2f", temperature);
sprintf(j, "%.2f", humidity);
#endif
if(ret < 0)
printf("\n<-----LOCAL LINK FAILED!(get infor failed)\n");
else{
gen_json_data(i,j, data);
printf("\nCLOUD-LINK--Sending data : \n%s\n", data);
memset(&googlenest, 0, sizeof(googlenest_context));
if(gn_connect(&googlenest, host_addr, CLOUD_PORT) == 0) {
if(gn_put(&googlenest, URI, data) != 0)
printf("PUT data failed!\n");
gn_close(&googlenest);
printf("\n<=====CLOUD LINK OK!\n");
}
else{
printf("\n<=====CLOUD LINK FAILED!(google nest connecting)\n");
}
free(data);
vTaskDelay(10000);
}
}
}
else
printf("\n<=====CLOUD LINK FAILED!(flash reading)\n");
}
void start_cloud_link(void)
{
if(xTaskCreate(cloud_link_task, ((const char*)"cloud_link_task"), 3584, NULL, tskIDLE_PRIORITY + 4, NULL) != pdPASS)
printf("%s xTaskCreate failed\n", __FUNCTION__);
}

View file

@ -0,0 +1,11 @@
#ifndef CLOUD_LINK_H
#define CLOUD_LINK_THREAD_H
#include "wigadget.h"
void start_cloud_link(void);
void cloud_link_task(void *param);
#endif

View file

@ -0,0 +1,186 @@
#include "device.h"
#include "PinNames.h"
#include "osdep_api.h"
#include "i2c_api.h"
#include "pinmap.h"
#include "shtc1.h"
#include "platform_stdlib.h"
#ifdef CONFIG_I2C_EN
#define MBED_I2C_MTR_SDA PB_3
#define MBED_I2C_MTR_SCL PB_2
#define MBED_I2C_SLAVE_ADDR0 0x70
#define POLYNOMIAL 0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
#define MBED_I2C_BUS_CLK 100000 //hz
#define I2C_DATA_MAX_LENGTH 16
static uint8_t i2cdata_write[I2C_DATA_MAX_LENGTH];
static uint8_t i2cdata_read[I2C_DATA_MAX_LENGTH];
static int i2cdata_read_pos;
static i2c_t i2cmaster;
// Sensor Commands
#define READ_ID 0xEFC8 // command: read ID register
#define SOFT_RESET 0x805D // soft resetSample Code for SHTC1
#define MEAS_T_RH_POLLING 0x7866 // meas. read T first, clock stretching disabled
#define MEAS_T_RH_CLOCKSTR 0x7CA2 // meas. read T first, clock stretching enabled
#define MEAS_RH_T_POLLING 0x58E0 // meas. read RH first, clock stretching disabled
#define MEAS_RH_T_CLOCKSTR 0x5C24 // meas. read RH first, clock stretching enabled
static int SHTC1_GetID(uint16_t *id);
static void SHTC1_WriteCommand(uint16_t cmd);
static int SHTC1_Read2BytesAndCrc(uint16_t *data);
static int SHTC1_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum);
static float SHTC1_CalcTemperature(uint16_t rawValue);
static float SHTC1_CalcHumidity(uint16_t rawValue);
int SHTC_Init(uint16_t *pID)
{
int error = NO_ERROR;
DiagPrintf("SHTC1_Init\n");
i2c_init((i2c_t*)&i2cmaster, MBED_I2C_MTR_SDA ,MBED_I2C_MTR_SCL);
i2c_frequency((i2c_t*)&i2cmaster,MBED_I2C_BUS_CLK);
if (pID == NULL )
return NULL_ERROR;
error = SHTC1_GetID(pID);
return error;
}
static int SHTC1_GetID(uint16_t *id)
{
int error = NO_ERROR;
uint8_t bytes[2];
uint8_t checksum;
SHTC1_WriteCommand(READ_ID);
i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 3, 1);
i2cdata_read_pos = 0;
error = SHTC1_Read2BytesAndCrc(id);
return error;
}
static int SHTC1_Read2BytesAndCrc(uint16_t *data)
{
int error;
int readed;
uint8_t bytes[2];
uint8_t checksum;
bytes[0] = i2cdata_read[i2cdata_read_pos++];
bytes[1] = i2cdata_read[i2cdata_read_pos++];
checksum = i2cdata_read[i2cdata_read_pos++];
error = SHTC1_CheckCrc(bytes, 2, checksum);
*data = (bytes[0] << 8) | bytes[1];
return error;
}
static int SHTC1_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
{
uint8_t bit; // bit mask
uint8_t crc = 0xFF; // calculated checksum
uint8_t byteCtr; // byte counter
for(byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++){
crc ^= (data[byteCtr]);
for(bit = 8; bit > 0; --bit){
if(crc & 0x80)
crc = (crc << 1) ^ POLYNOMIAL;
else
crc = (crc << 1);
}
}
if(crc != checksum)
return CHECKSUM_ERROR;
else
return NO_ERROR;
}
static void SHTC1_WriteCommand(uint16_t cmd)
{
int writebytes;
i2cdata_write[0] = (uint8_t)(cmd >>8);
i2cdata_write[1] = (uint8_t)(cmd&0xFF);
i2c_write((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, &i2cdata_write[0], 2, 1);
}
static float SHTC1_CalcTemperature(uint16_t rawValue)
{
return 175.0 * (float)rawValue / 65536.0 - 45.0;
}
static float SHTC1_CalcHumidity(uint16_t rawValue)
{
return 100.0 * (float)rawValue / 65536.0;
}
int SHTC_GetTempAndHumi(float *temp, float *humi)
{
int error;
uint16_t rawValueTemp;
uint16_t rawValueHumi;
SHTC1_WriteCommand(MEAS_T_RH_CLOCKSTR);
i2c_read((i2c_t*)&i2cmaster, MBED_I2C_SLAVE_ADDR0, (char*)&i2cdata_read[0], 6, 1);
i2cdata_read_pos = 0;
error = NO_ERROR;
error |= SHTC1_Read2BytesAndCrc(&rawValueTemp);
error |= SHTC1_Read2BytesAndCrc(&rawValueHumi);
if ( error == NO_ERROR ) {
*temp = SHTC1_CalcTemperature(rawValueTemp);
*humi = SHTC1_CalcHumidity(rawValueHumi);
}
return error;
}
static void example_shtc1_thread(void *param)
{
int error;
uint16_t shtc1_id;
float temperature = 1.123f;
float humidity = 2.456f;
DBG_8195A("sleep 10 sec. to wait for UART console\n");
RtlMsleepOS(10000);
DBG_8195A("start i2c example - SHTC1\n");
error = SHTC_Init(&shtc1_id);
if ( error == NO_ERROR )
DiagPrintf("SHTC1 init ok, id=0x%x\n", shtc1_id);
else {
DiagPrintf("SHTC1 init FAILED!\n");
for(;;);
}
while(1){
error = SHTC_GetTempAndHumi(&temperature, &humidity);
rtl_printf("temp=%f, humidity=%f, error=%d\n", temperature, humidity, error);
RtlMsleepOS(1000);
}
}
void example_shtc1(void)
{
if(xTaskCreate(example_shtc1_thread, ((const char*)"example_shtc1_thread"), 512, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("%s xTaskCreate(init_thread) failed\n", __FUNCTION__);
}
#endif //#ifdef CONFIG_I2C_EN

View file

@ -0,0 +1,13 @@
#ifndef SHTC1_H
#define SHTC1_H
#define NO_ERROR 0x00
#define ACK_ERROR 0x01
#define CHECKSUM_ERROR 0x02
#define NULL_ERROR 0x03
int SHTC_GetTempAndHumi(float *temp, float *humi);
int SHTC_Init(uint16_t *pID);
void example_shtc1(void);
#endif

View file

@ -0,0 +1,743 @@
#include "FreeRTOS.h"
#include "task.h"
#include "wifi_conf.h"
#include "wifi_ind.h"
#include "sockets.h"
#include <mDNS/mDNS.h>
#include <lwip_netconf.h>
#include <lwip/netif.h>
#include "flash_api.h"
#include <rom_wac_aes.h>
#include "rom_wac_curve25519-donna.h"
#include "gpio_api.h"
#include "gpio_irq_api.h"
#include "cJSON.h"
#include "cloud_link.h"
#include "wigadget.h"
#include "shtc1.h"
#if LWIP_SOCKET
#define PORT 6866
#define MAX_BUFFER_SIZE 256
#define ENC_SIZE 64
#define CONTROL_TYPE 1
#define GPIO_SOFTAP_RESET_PIN PC_4
flash_t iot_flash;
uint8_t aes_key[16];
static unsigned char tx_buffer[MAX_BUFFER_SIZE];
static unsigned char rx_buffer[MAX_BUFFER_SIZE];
extern struct netif xnetif[NET_IF_NUM];
#define DEBUG_IOT 1
#define IOT_LOG(level, fmt, ...) printf("\n\r[IOT %s] %s: " fmt "\n", level, __FUNCTION__, ##__VA_ARGS__)
#if DEBUG_IOT == 2
#define IOT_DEBUG(fmt, ...) IOT_LOG("DEBUG", fmt, ##__VA_ARGS__)
#else
#define IOT_DEBUG(fmt, ...)
#endif
#if DEBUG_IOT
#define IOT_ERROR(fmt, ...) IOT_LOG("ERROR", fmt, ##__VA_ARGS__)
#else
#define IOT_ERROR(fmt, ...)
#endif
void encrypt_data_aes(unsigned char *plaint_text, unsigned char *enc_data);
void decrypt_data_aes(unsigned char *enc_data, unsigned char *dec_data, int data_len);
#ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#else
#define GCC_VERSION 0
#endif
/* Test for GCC < 5.4.0 */
#if GCC_VERSION > 50300
extern unsigned int _freertos_arc4random(void);
#define wgg_arc4random _freertos_arc4random
// or #define wgg_arc4random _ws_arc4random
#else
static unsigned int wgg_arc4random(void)
{
unsigned int res = xTaskGetTickCount();
static unsigned int seed = 0xDEADB00B;
seed = ((seed & 0x007F00FF) << 7) ^
((seed & 0x0F80FF00) >> 8) ^ // be sure to stir those low bits
(res << 13) ^ (res >> 9); // using the clock too!
return seed;
}
#endif
static char *iot_itoa(int value)
{
char *val_str;
int tmp = value, len = 1;
while((tmp /= 10) > 0)
len ++;
val_str = (char *) malloc(len + 1);
sprintf(val_str, "%d", value);
return val_str;
}
void gen_json_data(char *i, char *j, unsigned char *json_data)
{
cJSON_Hooks memoryHook;
memoryHook.malloc_fn = malloc;
memoryHook.free_fn = free;
cJSON_InitHooks(&memoryHook);
memset(json_data, 0, ENC_SIZE);
cJSON *IOTJSObject = NULL;
char *data;
if((IOTJSObject = cJSON_CreateObject()) != NULL) {
cJSON_AddItemToObject(IOTJSObject, "TEM", cJSON_CreateString(i));
cJSON_AddItemToObject(IOTJSObject, "HUM", cJSON_CreateString(j));
data = cJSON_Print(IOTJSObject);
memcpy(json_data, data, strlen(data));
cJSON_Delete(IOTJSObject);
free(data);
}
}
void encrypt_data_aes(unsigned char *plaint_text, unsigned char *enc_data)
{
unsigned char iv[16] = {0};
unsigned char* iv_bak = "AAAAAAAAAAAAAAAA";
aes_encrypt_ctx enc_ctx;
memset(&enc_ctx, 0, sizeof(enc_ctx));
memset(iv, 0, sizeof(iv));
memcpy(iv, iv_bak, sizeof(iv));
memset(enc_data, 0, sizeof(enc_data));
aes_init();
aes_encrypt_key(aes_key, 16, &enc_ctx);
aes_cbc_encrypt(plaint_text, enc_data, ENC_SIZE, iv, &enc_ctx);
}
void decrypt_data_aes(unsigned char *enc_data, unsigned char *dec_data, int data_len)
{
unsigned char iv[16] = {0};
unsigned char* iv_bak = "AAAAAAAAAAAAAAAA";
aes_decrypt_ctx dec_ctx;
memset(&dec_ctx, 0, sizeof(dec_ctx));
memset(iv, 0, sizeof(iv));
memcpy(iv, iv_bak, sizeof(iv));
memset(dec_data, 0, sizeof(dec_data));
aes_init();
aes_decrypt_key(aes_key, 16, &dec_ctx);
aes_cbc_decrypt(enc_data, dec_data, data_len, iv, &dec_ctx);
IOT_DEBUG("Decrypt data: %s\r\n",dec_data);
}
void iotapp_platform_reset(void)
{
HAL_WRITE32(SYSTEM_CTRL_BASE, 0x14, 0x00000021);
osDelay(100);
HAL_WRITE32(0xE000ED00, 0x0C, (0x5FA << 16) |
(HAL_READ32(0xE000ED00, 0x0C) & (7 << 8)) |
(1 << 2));
while(1) osDelay(1000);
}
void iotapp_reset_irq_handler(uint32_t id, gpio_irq_event event)
{
printf("\n\r\n\r\n\r\n\r<<<<<<Reset the device using PC_4>>>>>>>\n\r\n\r\n\r\n\r");
flash_erase_sector(&iot_flash, FLASH_IOT_DATA);
iotapp_platform_reset();
}
int local_link(unsigned char *tx_data)
{
int sockfd, newsockfd;
socklen_t client;
struct sockaddr_in serv_addr, cli_addr;
uint8_t rx_data[ENC_SIZE];
unsigned char enc_data[ENC_SIZE];
unsigned char dec_data[ENC_SIZE];
int ret = 0, opt = 1, k = 1, j;
char *result = NULL, *backup = NULL;
unsigned char *data = NULL;
char *delims = ", ";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
IOT_ERROR("ERROR opening socket");
ret = -1;
goto exit2;
}
if((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt))) < 0){
IOT_ERROR("ERROR on setting socket option");
ret = -1;
goto exit2;
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
IOT_ERROR("ERROR on binding");
ret = -1;
goto exit2;
}
if(listen(sockfd , 20) < 0){
IOT_ERROR("ERROR on listening");
ret = -1;
goto exit2;
}
client = sizeof(cli_addr);
if((newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&client)) < 0){
IOT_ERROR("ERROR on accept");
ret = -1;
goto exit;
}
if ((ret = read(newsockfd,rx_buffer,sizeof(rx_buffer))) < 0){
IOT_ERROR("ERROR reading from socket");
ret = -1;
goto exit;
}
IOT_DEBUG("cmd received: %s, length: %d\r\n",rx_buffer, ret);
//Changing received data to string
if (!strncmp(rx_buffer, "[", strlen("["))){
data = rx_buffer + strlen("[");
for(j = 1; j < 5; j++){
if (data[ret - j] == ']')
data[ret -j] = '\0';
}
}
else
strcpy(data, rx_buffer);
memset(rx_data, 0, sizeof(rx_data));
result = strtok_r(data, delims, &backup);
rx_data[0]=(uint8_t)atoi(result);
while((result = strtok_r(NULL, delims, &backup)) != NULL)
rx_data[k++]=(uint8_t)atoi(result);
memset(dec_data, 0, sizeof(dec_data));
//Decrpyt the received data
decrypt_data_aes(rx_data, dec_data, 16);
if(strncmp(dec_data, "request", strlen("request")) == 0){
//Encrypt the sending data
memset(enc_data, 0, strlen(enc_data));
encrypt_data_aes(tx_data, enc_data);
//Changing encrpyt data to JAVA type string
for (j = 0; j < ENC_SIZE; j++){
char *temp;
temp = iot_itoa(enc_data[j]);
if(j == 0)
strcpy(tx_buffer, "[");
strcat(tx_buffer,temp);
if (j == (ENC_SIZE - 1))
strcat(tx_buffer,"]");
else
strcat(tx_buffer,",");
free(temp);
temp = NULL;
}
IOT_DEBUG("Data reply to APP: %s\r\nLength of data: %d\r\n", tx_buffer, strlen(tx_buffer));
if ((ret = write(newsockfd,tx_buffer,strlen(tx_buffer))) < 0){
IOT_ERROR("ERROR writing to socket");
ret = -1;
goto exit;
}
else
IOT_DEBUG("Sending %d bytes data OK!\r\n", ret);
}
else if(strncmp(dec_data, "remove", strlen("remove")) == 0){
printf("\n\r\n\r\n\r\n\r<<<<<<Reset the device >>>>>>>\n\r\n\r\n\r\n\r");
flash_erase_sector(&iot_flash, FLASH_IOT_DATA);
write(newsockfd,"Remove OK",strlen("Remove OK"));
close(newsockfd);
close(sockfd);
iotapp_platform_reset();
}
else{
IOT_ERROR("ERROR wrong KEY or wrong request!");
write(newsockfd,"The KEY or the request is not correct!",strlen("The KEY or the request is not correct!"));
ret = -1;
goto exit;
}
exit:
if(close(newsockfd) != 0)
goto exit;
exit2:
if(close(sockfd) != 0)
goto exit2;
return ret;
}
static void local_link_task(void *param)
{
unsigned char data[ENC_SIZE] = {0};
vTaskDelay(1000);
char i[16], j[16];
float temperature = 1.123f;
float humidity = 2.456f;
int ret = 0;
while(1){
memset(i, 0, 16);
memset(j, 0, 16);
#if PSEUDO_DATA
sprintf(i,"%.2f", temperature++);
sprintf(j, "%.2f", humidity++);
if(temperature > 60)
temperature = 1.123f;
if(humidity > 98)
humidity = 2.456f;
#else
ret = SHTC_GetTempAndHumi(&temperature, &humidity);
sprintf(i,"%.2f", temperature);
sprintf(j, "%.2f", humidity);
#endif
if(ret < 0)
printf("\r\n\r\n<-----LOCAL LINK FAILED!(get infor failed)\r\n\r\n");
else{
printf("\r\n\r\n----->START LOCAL LINKING\r\n\r\n");
gen_json_data(i, j, data);
printf("Sending data : %s\r\n", data);
if (local_link(data) < 0)
printf("\r\n\r\n<-----LOCAL LINK FAILED!\r\n\r\n");
else
printf("\r\n\r\n<-----LOCAL LINK OK!\r\n\r\n");
vTaskDelay(1000);
}
}
}
void start_local_link(void)
{
if(xTaskCreate(local_link_task, ((const char*)"local_link_task"), 5376, NULL, tskIDLE_PRIORITY + 4, NULL) != pdPASS)
printf("\n\r%s xTaskCreate failed", __FUNCTION__);
}
int pair_device(unsigned char *tx_buffer, unsigned char *rx_buffer, int handshake)
{
int sockfd, newsockfd;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int ret = 0;
int opt = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
IOT_ERROR("ERROR opening socket");
ret = -1;
goto exit;
}
if((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt))) < 0){
IOT_ERROR("ERROR on setting socket option");
ret = -1;
goto exit;
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_len = sizeof(serv_addr);
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
if ((bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) < 0) {
IOT_ERROR("ERROR on binding");
ret = -1;
goto exit;
}
if ((listen(sockfd, 5)) < 0){
IOT_ERROR("ERROR on listening tcp server socket fd");
ret = -1;
goto exit;
}
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*)&clilen);
if (newsockfd < 0) {
IOT_ERROR("ERROR on accept");
ret = -1;
goto exit2;
}
ret = read(newsockfd, rx_buffer, MAX_BUFFER_SIZE);
if (ret <= 0){
IOT_ERROR("ERROR reading from socket");
ret = -1;
goto exit2;
}
IOT_DEBUG("Request received: %s, byte: %d\r\n",rx_buffer, ret);
if(handshake == 1){
if(strncmp(rx_buffer,"PAIR", strlen("PAIR")) != 0){
write(newsockfd, "ERROR", strlen("ERROR"));
IOT_ERROR("ERROR on first handshake!");
ret = -1;
goto exit2;
}
}
else if(handshake == 2){
if((rx_buffer == NULL) ||(strlen(rx_buffer) < 32)){
write(newsockfd, "ERROR", strlen("ERROR"));
IOT_ERROR("ERROR on second handshake!");
ret = -1;
goto exit2;
}
}
else if(handshake == 3){
unsigned char account[64];
unsigned char enc_acc[64];
char *result = NULL, *backup = NULL;
unsigned char *data = NULL;
char *delims = ", ";
int j, k = 1;
if (!strncmp(rx_buffer, "[", strlen("["))){
data = rx_buffer + strlen("[");
for(j = 1; j < 5; j++){
if (data[ret - j] == ']')
data[ret -j] = '\0';
}
}
else
strcpy(data, rx_buffer);
memset(enc_acc, 0, sizeof(enc_acc));
result = strtok_r(data, delims, &backup);
enc_acc[0]=(uint8_t)atoi(result);
while((result = strtok_r(NULL, delims, &backup)) != NULL)
enc_acc[k++]=(uint8_t)atoi(result);
IOT_DEBUG("The value of k: %d", k);
memset(account, 0, sizeof(account));
decrypt_data_aes(enc_acc, account, k);
if((strncmp(account,"https://", strlen("https://"))) != 0){
write(newsockfd, "ERROR", strlen("ERROR"));
IOT_ERROR("ERROR on third handshake!");
ret = -1;
goto exit2;
}
else{
IOT_DEBUG("The received Firebase URL:%s", account);
memset(rx_buffer, 0, strlen(rx_buffer));
memcpy(rx_buffer, (account+strlen("https://")), (strlen(account) + strlen("https://")));
}
}
ret = write(newsockfd, tx_buffer, strlen(tx_buffer));
IOT_DEBUG("Data send: %s\r\n",tx_buffer);
if (ret < 0){
IOT_ERROR("ERROR writing to socket");
}
exit:
if(close(newsockfd) != 0)
goto exit;
exit2:
if(close(sockfd) != 0)
goto exit2;
return ret;
}
static void pair_device_task(void)
{
int i, j, k, HANDSHAKE;
uint8_t PAIR_STATE[1] = {0};
if(CONTROL_TYPE == 1){
printf("\r\n\r\n<<<<<<CONTROL_TYPE = 1 Need 3 times handshake>>>>>>\r\n\r\n");
HANDSHAKE = 3;
}
else{
printf("\r\n\r\n<<<<<<CONTROL_TYPE = 0 Need 2 times handshake>>>>>>\r\n\r\n");
HANDSHAKE = 2;
}
printf("\r\n\r\n=========>PAIR_STATE = 0 Start to pair\r\n\r\n");
for(i = 0; i < HANDSHAKE; i++){
static const uint8_t basepoint[32] = {9};
uint8_t mysecret[32];
uint8_t mypublic[32];
uint8_t theirpublic[32] = {0};
uint8_t shared_key[32];
//First handshake
if(i == 0){
printf("\r\n\r\n===>Start the first handshake\r\n\r\n");
memset(tx_buffer, 0, sizeof(tx_buffer));
memset(rx_buffer, 0, sizeof(rx_buffer));
for(j = 0; j < 32; j ++)
mysecret[j] = (uint8_t) wgg_arc4random();
mysecret[j] = '\0';
curve25519_donna(mypublic, mysecret, basepoint);
for (j = 0; j < 32; j++){
char *temp;
temp = iot_itoa(mypublic[j]);
if(j == 0)
strcpy(tx_buffer, "[");
strcat(tx_buffer,temp);
if (j == 31)
strcat(tx_buffer,"]");
else
strcat(tx_buffer,",");
free(temp);
temp = NULL;
}
if(pair_device(tx_buffer, rx_buffer, 1) >= 0)
printf("\r\n\r\n<===First handshake OK!\r\n\r\n");
else{
i--;
printf("\r\n\r\n<===First handshake FAILED!\r\n\r\n");
}
}
//Second handshake
if(i == 1){
printf("\r\n\r\n=====>Start the second handshake\r\n\r\n");
vTaskDelay(200);
memset(tx_buffer, 0, sizeof(tx_buffer));
if(CONTROL_TYPE == 1)
memcpy(tx_buffer, "FIREBASE URL", sizeof("FIREBASE URL"));
else
memcpy(tx_buffer, "PAIR OK", sizeof("PAIR OK"));
memset(rx_buffer, 0, sizeof(rx_buffer));
if(pair_device(tx_buffer, rx_buffer, 2) >= 0){
char *result = NULL, *backup = NULL;
unsigned char *data = NULL;
char *delims = ", ";
k = 1;
if (!strncmp(rx_buffer, "[", strlen("["))){
data = rx_buffer + strlen("[");
int len;
len = strlen(data);
for(j = 1; j < 5; j++){
if (data[len - j] == ']')
data[len -j] = '\0';
}
}
else
strcpy(data, rx_buffer);
memset(theirpublic, 0, sizeof(theirpublic));
result = strtok_r(data, delims, &backup);
theirpublic[0]=(uint8_t)atoi(result);
while((result = strtok_r(NULL, delims, &backup)) != NULL)
theirpublic[k++] = (uint8_t)atoi(result);
curve25519_donna(shared_key, mysecret, theirpublic);
for(j = 0; j < 16; j ++)
aes_key[j] = shared_key[j];
//Store the KEY in FLASH
if(CONTROL_TYPE == 0){
PAIR_STATE[0] = 1;
uint8_t data[33];
memset(data, 0, 33);
memcpy(data, PAIR_STATE, 1);
memcpy(data+1, shared_key, 32);
flash_erase_sector(&iot_flash, FLASH_IOT_DATA);
flash_stream_write(&iot_flash, FLASH_IOT_DATA, 33, (uint8_t *) data);
IOT_DEBUG("PAIR_STATE: %d\r\n", PAIR_STATE[0]);
}
printf("\r\n\r\n<=====Second handshake OK!\r\n\r\n");
}
else{
i = i - 2;
printf("\r\n\r\n<=====Second handshake FAILED!\r\n\r\n");
}
}
//Third handshake
if(i == 2){
printf("\r\n\r\n=======>Start the third handshake\r\n\r\n");
vTaskDelay(200);
memset(tx_buffer, 0, sizeof(tx_buffer));
memcpy(tx_buffer, "PAIR OK", sizeof("PAIR OK"));
memset(rx_buffer, 0, sizeof(rx_buffer));
if(pair_device(tx_buffer, rx_buffer, 3) >= 0){
IOT_DEBUG("rx_buffer: %s, sizeof rx_buffer:%d\r\n", rx_buffer, sizeof(rx_buffer));
PAIR_STATE[0] = 1;
uint8_t data[97];
memset(data, 0, 97);
memcpy(data, PAIR_STATE, 1);
memcpy(data+1, shared_key, 32);
memcpy(data+33, rx_buffer, 64);
flash_erase_sector(&iot_flash, FLASH_IOT_DATA);
flash_stream_write(&iot_flash, FLASH_IOT_DATA, 97, (uint8_t *) data);
IOT_DEBUG("PAIR_STATE: %d\r\n", PAIR_STATE[0]);
printf("\r\n\r\n<=======Third handshake OK!\r\n\r\n");
}
else{
i = i - 3;
printf("\r\n\r\n<=======Third handshake FAILED!\r\n\r\n");
}
}
}
printf("\r\n\r\n<=========Pairing OK!\r\n\r\n");
}
static void mdns_task(void *param)
{
DNSServiceRef dnsServiceRef = NULL;
TXTRecordRef txtRecord;
unsigned char txt_buf[128];
uint8_t *mac, *ip;
int j, ret = 0;
uint8_t *flash_data;
uint8_t PAIR_STATE[1] = {0};
static unsigned char MAC_ADD[21];
static unsigned char IP[16];
static unsigned char port[6];
uint16_t shtc1_id;
// Delay to wait for IP by DHCP and get the information of IP and MAC
printf("\n\r\n\r\n\r\n\r<<<<<<Waiting for 20 seconds to connect Wi-Fi>>>>>>>\n\r\n\r\n\r\n\r");
vTaskDelay(20000);
ip = LwIP_GetIP(&xnetif[0]);
mac = LwIP_GetMAC(&xnetif[0]);
sprintf(MAC_ADD, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(IP, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
sprintf(port, "%d", PORT);
IOT_DEBUG("MAC => %s\r\n", MAC_ADD) ;
IOT_DEBUG("IP => %s\r\n", IP);
IOT_DEBUG("PORT => %s\r\n", port);
//Get the value of PAIR_STATE and the AES key in flash
flash_data = (uint8_t *)malloc(33);
flash_stream_read(&iot_flash, FLASH_IOT_DATA, 33, (uint8_t *)flash_data);
memcpy(PAIR_STATE, flash_data, 1);
if(PAIR_STATE[0] != 0x1)
PAIR_STATE[0] = 0;
else{
for(j = 0;j < 16; j++){
aes_key[j] = flash_data[j+1];
}
}
free(flash_data);
IOT_DEBUG("PAIR_STATE now: %d\r\n", PAIR_STATE[0]);
IOT_DEBUG("=>mDNS Init\r\n");
if(mDNSResponderInit() == 0) {
printf("\r\n\r\n========>Start to register mDNS service\r\n\r\n");
//The device not paired before
if(PAIR_STATE[0] == 0){
TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
TXTRecordSetValue(&txtRecord, "IP", strlen(IP), IP);
TXTRecordSetValue(&txtRecord, "PORT", strlen(port), port);
TXTRecordSetValue(&txtRecord, "MAC_ADDR", strlen(MAC_ADD), MAC_ADD);
TXTRecordSetValue(&txtRecord, "PAIR_STATE", strlen("0"), "0");
TXTRecordSetValue(&txtRecord, "SERVICE_NAME", strlen("ht_sensor"), "ht_sensor");
if(CONTROL_TYPE == 1)
TXTRecordSetValue(&txtRecord, "CONTROL_TYPE", strlen("1"), "1");
else
TXTRecordSetValue(&txtRecord, "CONTROL_TYPE", strlen("0"), "0");
dnsServiceRef = mDNSRegisterService("ht_sensor", "_Ameba._tcp", "local", PORT, &txtRecord);
TXTRecordDeallocate(&txtRecord);
printf("\r\n\r\n<========Registering mDNS service OK!\r\n\r\n");
pair_device_task();
}
//The device was paired
else if(PAIR_STATE[0] == 0x1){
TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
TXTRecordSetValue(&txtRecord, "IP", strlen(ip), ip);
TXTRecordSetValue(&txtRecord, "PORT", strlen(port), port);
TXTRecordSetValue(&txtRecord, "MAC_ADDR", strlen(MAC_ADD), MAC_ADD);
TXTRecordSetValue(&txtRecord, "PAIR_STATE", strlen("1"), "1");
TXTRecordSetValue(&txtRecord, "SERVICE_NAME", strlen("ht_sensor"), "ht_sensor");
if(CONTROL_TYPE == 1)
TXTRecordSetValue(&txtRecord, "CONTROL_TYPE", strlen("1"), "1");
else
TXTRecordSetValue(&txtRecord, "CONTROL_TYPE", strlen("0"), "0");
dnsServiceRef = mDNSRegisterService("ht_sensor", "_Ameba._tcp", "local", PORT, &txtRecord);
TXTRecordDeallocate(&txtRecord);
printf("\r\n\r\n<========Registering mDNS service OK! PAIR_STATE = 1\r\n\r\n");
}
#if PSEUDO_DATA
printf("\r\n\r\n========>Using the speudo data\r\n\r\n");
if(CONTROL_TYPE == 1) start_cloud_link();
start_local_link();
#else
//Init the shtc1 sensor
printf("\r\n\r\n========>Init the temperature and humidity sensor\r\n\r\n");
ret = SHTC_Init(&shtc1_id);
if ( ret == NO_ERROR ){
printf("\r\n\r\n<========Senser init OK! ID = 0x%x \r\n\r\n", shtc1_id);
if(CONTROL_TYPE == 1) start_cloud_link();
start_local_link();
}
else {
printf("\r\n\r\n<========Senser init FAILED! ID = 0x%x \r\n\r\n", shtc1_id);
ret = -1;
}
#endif
}
else
ret = -1;
if(ret == 0){
while(1){
IOT_DEBUG("Update the mDNS textrecord!\r\n");
TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
TXTRecordSetValue(&txtRecord, "IP", strlen(IP), IP);
TXTRecordSetValue(&txtRecord, "PORT", strlen(port), port);
TXTRecordSetValue(&txtRecord, "MAC_ADDR", strlen(MAC_ADD), MAC_ADD);
TXTRecordSetValue(&txtRecord, "PAIR_STATE", strlen("1"), "1");
if(CONTROL_TYPE == 1)
TXTRecordSetValue(&txtRecord, "CONTROL_TYPE", strlen("1"), "1");
else
TXTRecordSetValue(&txtRecord, "CONTROL_TYPE", strlen("0"), "0");
TXTRecordSetValue(&txtRecord, "SERVICE_NAME", strlen("ht_sensor"), "ht_sensor");
mDNSUpdateService(dnsServiceRef, &txtRecord, 0);
TXTRecordDeallocate(&txtRecord);
vTaskDelay(2*60*1000);
}
}
else{
if(dnsServiceRef)
mDNSDeregisterService(dnsServiceRef);
IOT_DEBUG("<=mDNS Deinit\r\n\r\n");
mDNSResponderDeinit();
}
}
void example_wigadget(void)
{
if(xTaskCreate(mdns_task, ((const char*)"mdns_task"), 3072, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("\n\r%s xTaskCreate failed", __FUNCTION__);
gpio_t gpio_softap_reset_button;
gpio_irq_t gpioirq_softap_reset_button;
gpio_irq_init(&gpioirq_softap_reset_button, GPIO_SOFTAP_RESET_PIN, iotapp_reset_irq_handler, (uint32_t)(&gpio_softap_reset_button));
gpio_irq_set(&gpioirq_softap_reset_button, IRQ_FALL, 1);
gpio_irq_enable(&gpioirq_softap_reset_button);
}
#endif // LWIP_SOCKET

View file

@ -0,0 +1,11 @@
#ifndef WIGADGET_H
#define WIGADGET_H
#define FLASH_IOT_DATA (0x0007E000)
#define PSEUDO_DATA 1
void example_wigadget(void);
void gen_json_data(char *i, char *j, unsigned char *json_data);
#endif /* WIGADGET_H */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,86 @@
/*
X-Modem Header File
1999/09/03 sprite, support Xmode Tx & Rx
*/
#ifndef _XMODE_H_
#define _XMODE_H_
#include <basic_types.h>
/*****************
* X-Modem status
*****************/
#define XMODEM_OK 1
#define XMODEM_CANCEL 2
#define XMODEM_ACK 3
#define XMODEM_NAK 4
#define XMODEM_COMPLETE 5
#define XMODEM_NO_SESSION 6
#define XMODEM_ABORT 7
#define XMODEM_TIMEOUT 8
/****************************
* flow control character
****************************/
#define SOH 0x01 /* Start of header */
#define STX 0x02 /* Start of header XModem-1K */
#define EOT 0x04 /* End of transmission */
#define ACK 0x06 /* Acknowledge */
#define NAK 0x15 /* Not acknowledge */
#define CAN 0x18 /* Cancel */
#define ESC 0x1b /* User Break */
/****************************
* Xmode paramters
****************************/
#define FRAME_SIZE 132 /* X-modem structure */
#define FRAME_SIZE_1K 1028 /* X-modem structure */
#define XM_BUFFER_SIZE 1024 /* X-modem buffer */
#define TIMEOUT 180 /* max timeout */
#define RETRY_COUNT 20 /* Try times */
#define xWAITTIME 0x00400000 /* waitiing time */
#define WAIT_FRAME_TIME (10000*100) /* 10 sec, wait frame timeout */
#define WAIT_CHAR_TIME (1000*100) /* 1 sec, wait char timeout */
/***********************
* frame structure
***********************/
typedef struct
{
unsigned char soh;
unsigned char recordNo;
unsigned char recordNoInverted;
unsigned char buffer[XM_BUFFER_SIZE];
unsigned char CRC;
} XMODEM_FRAME;
typedef struct _XMODEM_COM_PORT_ {
char (*poll) (void);
char (*get)(void);
void (*put)(char c);
}XMODEM_COM_PORT, *PXMODEM_COM_PORT;
typedef struct _XMODEM_CTRL_ {
u16 xMUsing;
u16 currentFrame; /* current frame number */
u16 previousFrame; /* previous frame number */
u16 expected;
s16 rStatus;
s32 rFinish;
u32 total_frame;
u32 rx_len;
char *pXFrameBuf;
u32 (*RxFrameHandler)(char *ptr, u32 frame_num, u32 frame_size);
XMODEM_COM_PORT ComPort;
}XMODEM_CTRL, *PXMODEM_CTRL;
typedef u32 (*RxFrameHandler_t)(char *ptr, u32 frame_num, u32 frame_size);
extern s16 xModemStart(XMODEM_CTRL *pXMCtrl, char *FrameBuf, RxFrameHandler_t RxFrameHdl);
extern s16 xModemEnd(XMODEM_CTRL *pXMCtrl);
extern s32 xModemRxBuffer(XMODEM_CTRL *pXMCtrl, s32 MaxSize);
#endif /* _XMODE_H_ */

View file

@ -0,0 +1,25 @@
/*
* Routines to access hardware
*
* Copyright (c) 2013 Realtek Semiconductor Corp.
*
* This module is a confidential and proprietary property of RealTek and
* possession or use of this module requires written permission of RealTek.
*/
#ifndef _XMPORT_LOGUART_H_
#define _XMPORT_LOGUART_H_
#include "xmodem.h"
//void xmodem_loguart_init(void);
void xmodem_loguart_init(u32 BaudRate);
void xmodem_loguart_func_hook(XMODEM_COM_PORT *pXComPort);
void xmodem_loguart_deinit(void);
char xmodem_loguart_readable(void);
char xmodem_loguart_writable(void);
char xmodem_loguart_getc(void);
void xmodem_loguart_putc(char c);
#endif // end of "#define _XMPORT_LOGUART_H_"

View file

@ -0,0 +1,24 @@
/*
* Routines to access hardware
*
* Copyright (c) 2013 Realtek Semiconductor Corp.
*
* This module is a confidential and proprietary property of RealTek and
* possession or use of this module requires written permission of RealTek.
*/
#ifndef _XMPORT_UART_H_
#define _XMPORT_UART_H_
#include "xmodem.h"
void xmodem_uart_init(u8 uart_idx, u8 pin_mux, u32 baud_rate);
void xmodem_uart_func_hook(XMODEM_COM_PORT *pXComPort);
void xmodem_uart_deinit(void);
char xmodem_uart_readable(void);
char xmodem_uart_writable(void);
char xmodem_uart_getc(void);
void xmodem_uart_putc(char c);
#endif // end of "#define _XMPORT_UART_H_"

View file

@ -0,0 +1,24 @@
#ifndef _G711_CODEC_H
#define _G711_CODEC_H
#include "basic_types.h"
typedef enum {
I2S_MODE_UNKNOWN = -1,
I2S_MODE_G711A = 1,
I2S_MODE_G711U = 2
}I2S_mode;
typedef struct {
I2S_mode mode; //intend to encode as G711A or G711U
int len; //data length
// _sema RWSema;
unsigned char *raw_data; //address of buffered data (not encoded
int data_start; // a shift value to show where the data starts
int data_end; // a shift value shows data ending
}i2s_buf_context;
//void encoder_init(int mode);
int G711_encoder(short *source, unsigned char *des, int mode, int size);
int G711_decoder(unsigned char *source, short *des, int mode, int size);
int mono2stereo(unsigned char *src, unsigned char *dst, int size, int wl);
#endif

View file

@ -0,0 +1,251 @@
#include "rtl8195a.h"
#include "build_info.h"
#ifdef PLATFORM_FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#endif
#include "osdep_service.h"
#include "lwip_netconf.h"
#include "ethernet_api.h"
#include "lwip_intf.h"
#include "ethernet_mii.h"
#include "platform_opts.h"
#include "ethernet_ex_api.h"
#if defined(CONFIG_MII_EN)
static _sema mii_rx_sema;
static _mutex mii_tx_mutex;
extern struct netif xnetif[NET_IF_NUM];
static u8 TX_BUFFER[1518];
static u8 RX_BUFFER[1518];
static u8 *pTmpTxDesc = NULL;
static u8 *pTmpRxDesc = NULL;
static u8 *pTmpTxPktBuf = NULL;
static u8 *pTmpRxPktBuf = NULL;
int dhcp_ethernet_mii = 1;
int ethernet_if_default = 0;
extern int lwip_init_done;
static _sema mii_linkup_sema;
void mii_rx_thread(void* param){
u32 len = 0;
u8* pbuf = RX_BUFFER;
while(1){
if (rtw_down_sema(&mii_rx_sema) == _FAIL){
DBG_8195A("%s, Take Semaphore Fail\n", __FUNCTION__);
goto exit;
}
// continues read the rx ring until its empty
while(1){
len = ethernet_receive();
if(len){
//DBG_8195A("mii_recv len = %d\n\r", len);
ethernet_read(pbuf, len);
// calculate the time duration
ethernetif_mii_recv(&xnetif[NET_IF_NUM - 1], len);
//__rtl_memDump_v1_00(pbuf, len, "ethernet_receive Data:");
//rtw_memset(pbuf, 0, len);
}else if(len == 0){
break;
}
}
}
exit:
rtw_free_sema(&mii_rx_sema);
vTaskDelete(NULL);
}
void dhcp_start_mii(void* param)
{
while(1)
{
if (rtw_down_sema(&mii_linkup_sema) == _FAIL){
DBG_8195A("%s, Take Semaphore Fail\n", __FUNCTION__);
break;
}
LwIP_DHCP(NET_IF_NUM - 1, DHCP_START);
}
rtw_free_sema(&mii_linkup_sema);
vTaskDelete(NULL);
}
void mii_intr_handler(u32 Event, u32 Data)
{
switch(Event)
{
case ETH_TXDONE:
//DBG_8195A("TX Data = %d\n", Data);
break;
case ETH_RXDONE:
//DBG_8195A("\r\nRX Data = %d\n", Data);
// wake up rx thread to receive data
rtw_up_sema_from_isr(&mii_rx_sema);
break;
case ETH_LINKUP:
DBG_8195A("Link Up\n");
if(dhcp_ethernet_mii == 1)
rtw_up_sema_from_isr(&mii_linkup_sema);
break;
case ETH_LINKDOWN:
DBG_8195A("Link Down\n");
break;
default:
DBG_8195A("Unknown event!\n");
break;
}
}
void ethernet_demo(void* param){
u8 mac[6];
/* Initilaize the LwIP stack */
// can not init twice
if(!lwip_init_done)
LwIP_Init();
DBG_8195A("LWIP Init done\n");
ethernet_irq_hook(mii_intr_handler);
if(pTmpTxDesc)
{
free(pTmpTxDesc);
pTmpTxDesc = NULL;
}
if(pTmpRxDesc)
{
free(pTmpRxDesc);
pTmpRxDesc = NULL;
}
if(pTmpTxPktBuf)
{
free(pTmpTxPktBuf);
pTmpTxPktBuf = NULL;
}
if(pTmpRxPktBuf)
{
free(pTmpRxPktBuf);
pTmpRxPktBuf = NULL;
}
pTmpTxDesc = (u8 *)malloc(/*MII_TX_DESC_CNT*/MII_TX_DESC_NO * ETH_TX_DESC_SIZE);
pTmpRxDesc = (u8 *)malloc(/*MII_RX_DESC_CNT*/MII_RX_DESC_NO * ETH_RX_DESC_SIZE);
pTmpTxPktBuf = (u8 *)malloc(/*MII_TX_DESC_CNT*/MII_TX_DESC_NO * ETH_PKT_BUF_SIZE);
pTmpRxPktBuf = (u8 *)malloc(/*MII_RX_DESC_CNT*/MII_RX_DESC_NO * ETH_PKT_BUF_SIZE);
if(pTmpTxDesc == NULL || pTmpRxDesc == NULL || pTmpTxPktBuf == NULL || pTmpRxPktBuf == NULL)
{
printf("TX/RX descriptor malloc fail\n");
return;
}
memset(pTmpTxDesc, 0, MII_TX_DESC_NO * ETH_TX_DESC_SIZE);
memset(pTmpRxDesc, 0, MII_RX_DESC_NO * ETH_RX_DESC_SIZE);
memset(pTmpTxPktBuf, 0, MII_TX_DESC_NO * ETH_PKT_BUF_SIZE);
memset(pTmpRxPktBuf, 0, MII_RX_DESC_NO * ETH_PKT_BUF_SIZE);
//size 160 128 12288 12288
ethernet_set_descnum(MII_TX_DESC_NO, MII_RX_DESC_NO);
printf("TRX descriptor number setting done\n");
ethernet_trx_pre_setting(pTmpTxDesc, pTmpRxDesc, pTmpTxPktBuf, pTmpRxPktBuf);
printf("TRX pre setting done\n");
ethernet_init();
#if 0
DBG_INFO_MSG_OFF(_DBG_MII_);
DBG_WARN_MSG_OFF(_DBG_MII_);
DBG_ERR_MSG_ON(_DBG_MII_);
#endif
/*get mac*/
ethernet_address(mac);
memcpy((void*)xnetif[NET_IF_NUM - 1].hwaddr,(void*)mac, 6);
rtw_init_sema(&mii_rx_sema,0);
rtw_mutex_init(&mii_tx_mutex);
if(xTaskCreate(mii_rx_thread, ((const char*)"mii_rx_th"), 1024, NULL, tskIDLE_PRIORITY+5, NULL) != pdPASS)
DBG_8195A("\n\r%s xTaskCreate(mii_rx_thread) failed", __FUNCTION__);
DBG_8195A("\nEthernet_mii Init done, interface %d",NET_IF_NUM - 1);
if(dhcp_ethernet_mii == 1)
LwIP_DHCP(NET_IF_NUM - 1, DHCP_START);
vTaskDelete(NULL);
}
void ethernet_mii_init()
{
printf("\ninitializing Ethernet_mii......\n");
// set the ethernet interface as default
ethernet_if_default = 1;
rtw_init_sema(&mii_linkup_sema,0);
if( xTaskCreate((TaskFunction_t)dhcp_start_mii, "DHCP_MII", 1024, NULL, 2, NULL) != pdPASS) {
DBG_8195A("Cannot create demo task\n\r");
}
if( xTaskCreate((TaskFunction_t)ethernet_demo, "ETH_DEMO", 1024, NULL, 2, NULL) != pdPASS) {
DBG_8195A("Cannot create demo task\n\r");
}
}
void rltk_mii_recv(struct eth_drv_sg *sg_list, int sg_len){
struct eth_drv_sg *last_sg;
u8* pbuf = RX_BUFFER;
for (last_sg = &sg_list[sg_len]; sg_list < last_sg; ++sg_list) {
if (sg_list->buf != 0) {
rtw_memcpy((void *)(sg_list->buf), pbuf, sg_list->len);
pbuf+=sg_list->len;
}
}
}
s8 rltk_mii_send(struct eth_drv_sg *sg_list, int sg_len, int total_len){
int ret =0;
struct eth_drv_sg *last_sg;
u8* pdata = TX_BUFFER;
u8 retry_cnt = 0;
u32 size = 0;
for (last_sg = &sg_list[sg_len]; sg_list < last_sg; ++sg_list) {
rtw_memcpy(pdata, (void *)(sg_list->buf), sg_list->len);
pdata += sg_list->len;
size += sg_list->len;
}
pdata = TX_BUFFER;
//DBG_8195A("mii_send len= %d\n\r", size);
rtw_mutex_get(&mii_tx_mutex);
while(1){
ret = ethernet_write(pdata, size);
if(ret > 0){
ethernet_send();
ret = 0;
break;
}
if(++retry_cnt > 3){
DBG_8195A("TX drop\n\r");
ret = -1;
}
else
rtw_udelay_os(1);
}
rtw_mutex_put(&mii_tx_mutex);
return ret;
}
#endif //CONFIG_MII_EN

View file

@ -0,0 +1,12 @@
#ifndef __MII_ETHERNETIF_H__
#define __MII_ETHERNETIF_H__
#include "lwip_netconf.h"
#define MII_TX_DESC_CNT 4
#define MII_RX_DESC_CNT 10
#if CONFIG_ETHERNET
extern s8 rltk_mii_send(struct eth_drv_sg *sg_list, int sg_len, int total_len);
extern void rltk_mii_recv(struct eth_drv_sg *sg_list, int sg_len);
#endif
#endif // __MII_ETHERNETIF_H__

View file

@ -0,0 +1,196 @@
#include <stdio.h>
#include "PinNames.h"
#include "basic_types.h"
#include "diag.h"
#include <osdep_api.h>
#include "i2c_api.h"
#include "pinmap.h"
//#define I2C_MTR_SDA PC_4//PB_3
//#define I2C_MTR_SCL PC_5//PB_2
#define I2C_MTR_SDA PB_3
#define I2C_MTR_SCL PB_2
#define I2C_BUS_CLK 100000 //hz
#define I2C_ALC5651_ADDR (0x34/2)
#define RT5651_PRIV_INDEX 0x6a
#define RT5651_PRIV_DATA 0x6c
#if defined (__ICCARM__)
i2c_t alc5651_i2c;
#else
volatile i2c_t alc5651_i2c;
#define printf DBG_8195A
#endif
static void alc5651_delay(void)
{
int i;
i=10000;
while (i) {
i--;
asm volatile ("nop\n\t");
}
}
void alc5651_reg_write(unsigned int reg, unsigned int value)
{
char buf[4];
buf[0] = (char)reg;
buf[1] = (char)(value>>8);
buf[2] = (char)(value&0xff);
i2c_write(&alc5651_i2c, I2C_ALC5651_ADDR, &buf[0], 3, 1);
alc5651_delay();
}
void alc5651_reg_read(unsigned int reg, unsigned int *value)
{
int tmp;
char *buf = (char*)&tmp;
buf[0] = (char)reg;
i2c_write(&alc5651_i2c, I2C_ALC5651_ADDR, &buf[0], 1, 1);
alc5651_delay();
buf[0] = 0xaa;
buf[1] = 0xaa;
i2c_read(&alc5651_i2c, I2C_ALC5651_ADDR, &buf[0], 2, 1);
alc5651_delay();
*value= ((buf[0]&0xFF)<<8)|(buf[1]&0xFF);
}
void alc5651_index_write(unsigned int reg, unsigned int value)
{
alc5651_reg_write(RT5651_PRIV_INDEX, reg);
alc5651_reg_write(RT5651_PRIV_DATA, value);
}
void alc5651_index_read(unsigned int reg, unsigned int *value)
{
alc5651_reg_write(RT5651_PRIV_INDEX, reg);
alc5651_reg_read(RT5651_PRIV_DATA, value);
}
void alc5651_reg_dump(void)
{
int i;
unsigned int value;
printf("alc5651 codec reg dump\n\r");
printf("------------------------\n\r");
for(i=0;i<=0xff;i++){
alc5651_reg_read(i, &value);
printf("%02x : %04x\n\r", i, (unsigned short)value);
}
printf("------------------------\n\r");
}
void alc5651_index_dump(void)
{
int i;
unsigned int value;
printf("alc5651 codec index dump\n\r");
printf("------------------------\n\r");
for(i=0;i<=0xff;i++){
alc5651_index_read(i, &value);
printf("%02x : %04x\n\r", i, (unsigned short)value);
}
printf("------------------------\n\r");
}
void alc5651_init(void)
{
i2c_init(&alc5651_i2c, I2C_MTR_SDA, I2C_MTR_SCL);
i2c_frequency(&alc5651_i2c, I2C_BUS_CLK);
}
void alc5651_set_word_len(int len_idx) // interface2
{
// 0: 16 1: 20 2: 24 3: 8
unsigned int val;
alc5651_reg_read(0x71,&val);
val &= (~(0x3<<2));
val |= (len_idx<<2);
alc5651_reg_write(0x71,val);
alc5651_reg_read(0x70,&val);
val &= (~(0x3<<2));
val |= (len_idx<<2);
alc5651_reg_write(0x70,val);
}
void alc5651_init_interface1(void)
{
alc5651_reg_write(0x00,0x0021);
alc5651_reg_write(0x63,0xE8FE);
alc5651_reg_write(0x61,0x5800);
alc5651_reg_write(0x62,0x0C00);
alc5651_reg_write(0x73,0x0000);
alc5651_reg_write(0x2A,0x4242);
alc5651_reg_write(0x45,0x2000);
alc5651_reg_write(0x02,0x4848);
alc5651_reg_write(0x8E,0x0019);
alc5651_reg_write(0x8F,0x3100);
alc5651_reg_write(0x91,0x0E00);
alc5651_index_write(0x3D,0x3E00);
alc5651_reg_write(0xFA,0x0011);
alc5651_reg_write(0x83,0x0800);
alc5651_reg_write(0x84,0xA000);
alc5651_reg_write(0xFA,0x0C11);
alc5651_reg_write(0x64,0x4010);
alc5651_reg_write(0x65,0x0C00);
alc5651_reg_write(0x61,0x5806);
alc5651_reg_write(0x62,0xCC00);
alc5651_reg_write(0x3C,0x004F);
alc5651_reg_write(0x3E,0x004F);
alc5651_reg_write(0x27,0x3820);
alc5651_reg_write(0x77,0x0000);
}
void alc5651_init_interface2(void)
{
int reg_value=0;
alc5651_reg_write(0x00,0x0021);//reset all, device id 1
alc5651_reg_write(0x63,0xE8FE);//Power managerment control 3:
//VREF1&2 on, both slow VREF, MBIAS on, MBIAS bandcap power on, L & R HP Amp on, improve HP Amp driving enabled
alc5651_reg_write(0x61,0x5800);//power managerment control 1:
//I2S2 digital interface on, Analog DACL1 & DACR1 on.
alc5651_reg_write(0x62,0x0C00);//stereo1 & 2 DAC filter power on
alc5651_reg_write(0x73,0x0000);//ADC/DAC Clock control 1:
//I2S Clock Pre-Divider 1 & 2: /1. Stereo DAC Over Sample Rate : 128Fs
alc5651_reg_write(0x2A,0x4242);//Stereo DAC digital mixer control
//Un-mute DACL2 to Stereo DAC Left & Right Mixer
alc5651_reg_write(0x45,0x2000);//HPOMIX: Un-mute DAC1 to HPOMIX
alc5651_reg_write(0x02,0x4848);//HP Output Control:
//Unmute HPOL, HPOR
// alc5651_reg_write(0x0F,0x1F1F);//INL & INR Volume Control
// alc5651_reg_write(0x0D,0x0800);//IN1/2 Input Control
// alc5651_reg_write(0x1C,0x7F7F);//Stereo1 ADC Digital Volume Control
// alc5651_reg_write(0x1E,0xF000);// ADC Digital Boost Gain Control
alc5651_reg_write(0x8E,0x0019);//HP Amp Control 1
// Enable HP Output, Charge Pump Power On, HP Amp All Power On
alc5651_reg_write(0x8F,0x3100);//HP Amp Control 2, HP Depop Mode 2
alc5651_reg_write(0x91,0x0E00);//HP Amp Control 3, select HP capless power mode
alc5651_index_write(0x3D,0x3E00);//unknown
alc5651_reg_write(0xFA,0x0011);//enable input clock
alc5651_reg_write(0x83,0x0800);//default ASRC control 1
alc5651_reg_write(0x84,0xA000);//ASRC control 2: I2S1 enable ASRC mode, Sterol1 DAC filter ASRC mode.
// alc5651_reg_write(0xFA,0x0C11);//? ? ? MX-FAh[15:4]reserved
alc5651_reg_write(0x64,0x4010);//power managerment control 4:
//MIC BST2 Power On; MIC2 SE Mode single-end mode or line-input mode
alc5651_reg_write(0x65,0x0C00);//power managerment control 5: RECMIX L & R power on
alc5651_reg_write(0x61,0x5806);//power managerment control 1:
// I2S2 Digital Interface On, Analog DACL1, DACR1 power on; Analog ADCL, ADCR power on
alc5651_reg_write(0x62,0xCC00);//power managerment control 2: Stereo1&2 ADC/DAC digital filter power on
alc5651_reg_write(0x3C,0x004F);//RECMIXL
alc5651_reg_write(0x3E,0x004F);//RECMIXR
alc5651_reg_write(0x28,0x3030);//stereo2 ADC digital mixer control : Mute Stereo2 ADC L&R channel, ADCR
alc5651_reg_write(0x2F,0x0080); //Interface DAC/ADC Data control: Select IF2 ADCDAT Data Source IF1_ADC2
}

View file

@ -0,0 +1,39 @@
#ifndef _SD_DRIVER_H
#define _SD_DRIVER_H
#include "basic_types.h"
#define CONFIG_SD_SDIO 1
#define CONFIG_SD_SPI 0
typedef enum
{
SD_OK = 0,
SD_PROTECTED,
SD_NODISK,
SD_INITERR,
SD_ERROR,
}SD_RESULT;
typedef enum{
SD_CLK_LOW, // 10.4MHz
SD_CLK_MID, // 20.8MHz
SD_CLK_HIGH, // 41.6MHz
SD_CLK_RSV, // 5.2MHz
}SD_CLK;
SD_RESULT SD_WaitReady(void);
SD_RESULT SD_Init(void);
SD_RESULT SD_DeInit(void);
SD_RESULT SD_SetCLK(SD_CLK CLK);
SD_RESULT SD_Status(void);
SD_RESULT SD_GetCID(u8 *cid_data); // read sd card CID
SD_RESULT SD_GetCSD(u8 *csd_data); // read sd card CSD
SD_RESULT SD_GetCapacity(u32* sector_count); // read sd card Capacity
SD_RESULT SD_ReadBlocks(u32 sector,u8 *data,u32 count); //read multi sector
SD_RESULT SD_WriteBlocks(u32 sector,const u8 *data,u32 count); //write multi sector
#endif

View file

@ -0,0 +1,43 @@
#ifndef _SDIO_HOST_H
#define _SDIO_HOST_H
#include "basic_types.h"
#include "rtl8195a_sdio_host.h"
#define SDIO_HOST_BYTES_ALINGMENT 4
typedef enum{
SDIO_INIT_NONE = -1,
SDIO_INIT_FAIL = 0,
SDIO_INIT_OK = 1,
SDIO_SD_NONE = 2,
SDIO_SD_OK = 3,
}_sdio_init_s;
extern _sdio_init_s sdio_status;
typedef void (*sdio_sd_irq_handler)(void* param);
s8 sdio_init_host(void); // init sdio host interface
void sdio_deinit_host(void);
s8 sdio_sd_init(void); // init sd card through sdio
void sdio_sd_deinit(void); //de-init sd card through sdio
s8 sdio_sd_status(void);
u32 sdio_sd_getCapacity(void);
s8 sdio_sd_getProtection(void);
s8 sdio_sd_setProtection(bool protection);
s8 sdio_sd_getCSD(u8* CSD);
s8 sdio_sd_isReady();
s8 sdio_sd_setClock(SD_CLK_FREQUENCY SDCLK);
s8 sdio_read_blocks(u32 sector, u8 *buffer, u32 count);
s8 sdio_write_blocks(u32 sector, const u8 *buffer, u32 count);
s8 sdio_sd_hook_xfer_cmp_cb(IN sdio_sd_irq_handler CallbackFun,IN VOID *param);
s8 sdio_sd_hook_remove_cb(IN sdio_sd_irq_handler CallbackFun,IN VOID *param);
s8 sdio_sd_hook_insert_cb(IN sdio_sd_irq_handler CallbackFun,IN VOID *param);
s8 sdio_sd_hook_xfer_err_cb(IN sdio_sd_irq_handler CallbackFun,IN VOID *param);
#endif

View file

@ -0,0 +1,176 @@
/*
* sd.c (disasm ds.o)
*
* RTL8710/11 pvvx 12/2016
*/
#include "rtl8195a.h"
#ifdef CONFIG_SDIO_HOST_EN
#include "sd.h"
#include "sdio_host.h"
#define SIZE_BLOCK_ADMA 512
SemaphoreHandle_t sdWSema;
void sd_xfer_done_callback(void *obj) {
RtlUpSema(&sdWSema);
}
void sd_xfer_err_callback(void *obj) {
DBG_SDIO_ERR("sd_xfer_err_callback \r\n");
}
//----- SD_WaitReady
SD_RESULT SD_WaitReady() {
return SD_OK;
}
//----- SD_Init
SD_RESULT SD_Init() {
SD_RESULT result;
if (sdio_sd_init() != 0)
result = SD_INITERR;
else {
if (sdio_sd_getProtection() != 0)
result = SD_PROTECTED;
RtlInitSema(&sdWSema, 0);
sdio_sd_hook_xfer_cmp_cb(sd_xfer_done_callback, 0);
sdio_sd_hook_xfer_err_cb(sd_xfer_err_callback, 0);
}
return result;
}
//----- SD_DeInit
SD_RESULT SD_DeInit() {
sdio_sd_deinit();
RtlFreeSema(&sdWSema);
return SD_OK;
}
//----- SD_SetCLK
SD_RESULT SD_SetCLK(SD_CLK CLK) {
SD_RESULT result;
switch (CLK) {
case SD_CLK_HIGH:
result = sdio_sd_setClock(SD_CLK_41_6MHZ);
break;
case SD_CLK_MID:
result = sdio_sd_setClock(SD_CLK_20_8MHZ);
break;
case SD_CLK_LOW:
result = sdio_sd_setClock(SD_CLK_10_4MHZ);
break;
case SD_CLK_RSV:
result = sdio_sd_setClock(SD_CLK_5_2MHZ);
break;
default:
// DBG_SDIO_INFO("clk = %d ?\n", CLK);
return SD_ERROR;
}
if (result)
return SD_ERROR;
return SD_OK;
}
//----- SD_Status
SD_RESULT SD_Status() {
if (sdio_sd_isReady())
return SD_NODISK;
else
return sdio_sd_getProtection() != 0;
}
//----- SD_GetCID
SD_RESULT SD_GetCID(u8 *cid_data) {
return SD_OK;
}
//----- SD_GetCSD
SD_RESULT SD_GetCSD(u8 *csd_data) {
if (sdio_sd_getCSD(csd_data))
return SD_ERROR;
else
return SD_OK;
}
//----- SD_GetCapacity
SD_RESULT SD_GetCapacity(uint32_t *sector_count) {
u32 sc = sdio_sd_getCapacity();
*sector_count = sc;
if (sc != 0)
return SD_OK;
else
return SD_ERROR;
}
//----- SD_ReadBlocks
SD_RESULT SD_ReadBlocks(u32 sector, u8 *data, u32 count) {
int rd_count;
unsigned char * buf;
if ((u32) data & 3) {
buf = pvPortMalloc(SIZE_BLOCK_ADMA);
if (buf == NULL)
DBG_SDIO_ERR("Fail to malloc cache for SDIO host!\n");
u32 end_sector = count + sector;
while (sector < end_sector) {
rd_count = sdio_read_blocks(sector, buf, 1);
// rtl_printf("rd_counts = %d\n", rd_count);
if (rd_count == 0 && RtlDownSemaWithTimeout(&sdWSema, 1000) != 1) {
DBG_SDIO_ERR("SD_ReadBlocks timeout\n");
return SD_ERROR;
}
rtl_memcpy(data, buf, SIZE_BLOCK_ADMA);
sector++;
data += SIZE_BLOCK_ADMA;
}
vPortFree(buf);
if (rd_count)
return SD_ERROR;
return SD_OK;
} else {
if (sdio_read_blocks(sector, data, count) == 0) {
if (RtlDownSemaWithTimeout(&sdWSema, 1000) == 1)
return SD_OK;
DBG_SDIO_ERR("SD_ReadBlocks timeout\n");
}
}
return SD_ERROR;
}
//----- SD_WriteBlocks
SD_RESULT SD_WriteBlocks(u32 sector, const u8 *data, u32 count) {
int wr_count;
unsigned char * buf;
if ((u32) data & 3) {
buf = pvPortMalloc(SIZE_BLOCK_ADMA);
if (buf == NULL)
DBG_SDIO_ERR("Fail to malloc cache for SDIO host!\n");
u32 end_sector = count + sector;
while (sector != end_sector) {
rtl_memcpy(buf, data, SIZE_BLOCK_ADMA);
wr_count = sdio_write_blocks(sector, buf, 1);
if (wr_count == 0 && RtlDownSemaWithTimeout(&sdWSema, 1000) != 1) {
DBG_SDIO_ERR("SD_WriteBlocks timeout\n");
return SD_ERROR;
}
sector++;
data += SIZE_BLOCK_ADMA;
}
vPortFree(buf);
if (wr_count == 0)
return SD_OK;
} else if (sdio_write_blocks(sector, data, count) == 0) {
if (RtlDownSemaWithTimeout(&sdWSema, 1000) == 1)
return SD_OK;
DBG_SDIO_ERR("SD_WriteBlocks timeout\n");
}
return SD_ERROR;
}
#endif // CONFIG_SDIO_HOST_EN

View file

@ -0,0 +1,408 @@
/*
* sdio_host.c (disasm sdio_host.o)
*
* RTL8710/11 pvvx 12/2016
*/
#include "rtl8195a.h"
#ifdef CONFIG_SDIO_HOST_EN
#include "sd.h"
#include "sdio_host.h"
#include "hal_sdio_host.h"
#include "rtl8195a_sdio_host.h"
//#include "hal_sdio.h"
//#include "PinNames.h"
//#include "pinmap.h"
#define MAX_BUF_ADMA 64
#define SIZE_BLOCK_ADMA 512
//-------------------------------------------------------------------------
// Function declarations
//-------------------------------------------------------------------------
// Data declarations
sdio_sd_irq_handler xfer_done_irq_handler;
uint32_t xfer_err_irq_data;
uint32_t xfer_done_irq_data;
sdio_sd_irq_handler xfer_err_irq_handler;
_sdio_init_s sdio_status = SDIO_INIT_NONE;
sdio_sd_irq_handler card_remove_irq_handler;
uint32_t card_remove_irq_data;
sdio_sd_irq_handler card_insert_irq_handler;
uint32_t card_insert_irq_data;
HAL_SDIO_HOST_OP HalSdioHostOp;
s8 sd_protected = -1;
HAL_SDIO_HOST_ADAPTER SdioHostAdapter;
SRAM_BF_DATA_SECTION ADMA2_DESC_FMT gAdmaTbls[MAX_BUF_ADMA];
//-------------------------------------------------------------------------
void xfer_done_callback(void *param) {
if (xfer_done_irq_handler)
xfer_done_irq_handler((void *) xfer_done_irq_data);
}
void xfer_err_callback(void *param) {
if (xfer_err_irq_handler)
xfer_err_irq_handler((void *) xfer_err_irq_data);
}
void card_insert_callback(void *param) {
#if CONFIG_DEBUG_LOG > 1
rtl_printf("SD card insert\n");
#endif
if (card_insert_irq_handler)
card_insert_irq_handler((void *) card_insert_irq_data);
}
void card_remove_callback(void *param) {
#if CONFIG_DEBUG_LOG > 1
rtl_printf("SD card removed\n");
#endif
sdio_status = SDIO_SD_NONE;
if (card_remove_irq_handler)
card_remove_irq_handler((void *) card_remove_irq_data);
}
//----- sdio_init_host
s8 sdio_init_host() {
s8 result;
HAL_Status stat;
DBG_SDIO_INFO("SDIO Init Host Begin...\n");
if (sdio_status > SDIO_INIT_FAIL) {
DBG_SDIO_INFO("SDIO Host init already.\n");
result = 0;
} else {
rtl_memset(&SdioHostAdapter, 0, sizeof(SdioHostAdapter));
HalSdioHostOpInit(&HalSdioHostOp);
stat = HalSdioHostOp.HalSdioHostInitHost(&SdioHostAdapter);
SdioHostAdapter.CardInsertCbPara = &SdioHostAdapter;
SdioHostAdapter.CardInsertCallBack =
(void (*)(void *)) card_insert_callback;
SdioHostAdapter.CardRemoveCbPara = &SdioHostAdapter;
SdioHostAdapter.CardRemoveCallBack =
(void (*)(void *)) card_remove_callback;
SdioHostAdapter.XferCompCbPara = &SdioHostAdapter;
SdioHostAdapter.XferCompCallback =
(void (*)(void *)) xfer_done_callback;
SdioHostAdapter.ErrorCbPara = &SdioHostAdapter;
SdioHostAdapter.ErrorCallback = (void (*)(void *)) xfer_err_callback;
if (stat == HAL_OK) {
sdio_status = SDIO_INIT_OK;
DBG_SDIO_INFO("SDIO Host init Success.\n");
result = 0;
} else {
sdio_status = SDIO_INIT_FAIL;
DBG_SDIO_ERR("SDIO Host init Fail.\n");
result = -1;
}
}
return result;
}
//-----
void sdio_deinit_host(void) {
if (sdio_status > 0) {
HAL_Status hs = HalSdioHostOp.HalSdioHostDeInit(&SdioHostAdapter);
if (hs == HAL_OK) {
SdioHostAdapter.CardInsertCallBack = NULL;
SdioHostAdapter.CardRemoveCallBack = NULL;
sdio_status = SDIO_INIT_NONE;
}
}
}
//-----
s8 sdio_read_blocks(u32 sector, u8 *buffer, u32 count) {
if (!count) {
DBG_SDIO_ERR("Parameter error, try to read %d count\n");
return -1;
}
if (sdio_status <= SDIO_SD_NONE) {
DBG_SDIO_ERR("SD card is not ready\n");
return -1;
}
if (count > MAX_BUF_ADMA) {
count = MAX_BUF_ADMA;
DBG_SDIO_ERR("Not enough ADMA table(maximum %d), reduce blocks count\n",
count);
}
rtl_memset(gAdmaTbls, 0, sizeof(ADMA2_DESC_FMT) * count);
if (1) {
ADMA2_DESC_FMT *p = gAdmaTbls;
u8 * pbuf = buffer;
int i = 0;
while (i < count) {
i++;
p->Addr1 = (u32) pbuf;
p->Len1 = SIZE_BLOCK_ADMA;
if (i == count) {
p->Attrib1.Valid = 1; // 0x23
p->Attrib1.End = 1;
p->Attrib1.Act2 = 1;
} else {
p->Attrib1.Valid = 1; // 0x21
p->Attrib1.Act2 = 1;
p->Attrib2.Valid = 1; // 0x31
p->Attrib2.Act1 = 1;
p->Attrib2.Act2 = 1;
p->Len2 = 0;
p->Addr2 = (u32) (&p[1]);
}
pbuf += SIZE_BLOCK_ADMA;
p++;
}
SdioHostAdapter.AdmaDescTbl = gAdmaTbls;
}
HAL_Status result = HalSdioHostOp.HalSdioHostReadBlocksDma(&SdioHostAdapter,
(unsigned long long) sector * SIZE_BLOCK_ADMA, count);
if (result) {
DBG_SDIO_ERR("sdio_read_blocks fail(0x%02x)\n", result);
return -1;
}
return 0;
}
//-----
s8 sdio_write_blocks(uint32_t sector, const uint8_t *buffer, uint32_t count) {
if (!count) {
DBG_SDIO_ERR("Parameter error, try to read %d count\n");
return -1;
}
if (sdio_status <= SDIO_SD_NONE) {
DBG_SDIO_ERR("SD card is not ready\n");
return -1;
}
if (count > MAX_BUF_ADMA) {
count = MAX_BUF_ADMA;
DBG_SDIO_ERR("Not enough ADMA table(maximum %d), reduce blocks count\n",
count);
}
if (sd_protected) {
DBG_SDIO_ERR("SD card is write protected\n");
return -1;
}
rtl_memset(gAdmaTbls, 0, sizeof(ADMA2_DESC_FMT) * count);
if (1) {
ADMA2_DESC_FMT *p = gAdmaTbls;
u8 * pbuf = buffer;
int i = 0;
while (i < count) {
i++;
p->Addr1 = (u32) pbuf;
p->Len1 = SIZE_BLOCK_ADMA;
if (i == count) {
p->Attrib1.Valid = 1; // 0x23
p->Attrib1.End = 1;
p->Attrib1.Act2 = 1;
} else {
p->Attrib1.Valid = 1; // 0x21
p->Attrib1.Act2 = 1;
p->Attrib2.Valid = 1; // 0x31
p->Attrib2.Act1 = 1;
p->Attrib2.Act2 = 1;
p->Len2 = 0;
p->Addr2 = (u32) (&p[1]);
}
pbuf += SIZE_BLOCK_ADMA;
p++;
}
SdioHostAdapter.AdmaDescTbl = gAdmaTbls;
}
HAL_Status result = HalSdioHostOp.HalSdioHostWriteBlocksDma(
&SdioHostAdapter, (unsigned long long) sector * SIZE_BLOCK_ADMA,
count);
if (result != HAL_OK) {
DBG_SDIO_ERR("write fail(0x%02x)\n", result);
return -1;
}
return 0;
}
//-----
s8 sdio_sd_init(void) {
if (sdio_status != SDIO_SD_OK) {
SdioHostAdapter.AdmaDescTbl = gAdmaTbls;
if (sdio_status <= SDIO_INIT_FAIL)
sdio_init_host();
if (sdio_status != SDIO_INIT_OK) {
return -1;
}
DBG_SDIO_INFO("Init sd card.\n");
if (HalSdioHostOp.HalSdioHostInitCard(&SdioHostAdapter)) {
return -1;
}
sdio_status = SDIO_SD_OK;
if (HalSdioHostOp.HalSdioHostChangeSdClock(&SdioHostAdapter,
SD_CLK_41_6MHZ) != HAL_OK)
DBG_SDIO_INFO("SD card does not support high speed.\n");
}
return 0;
}
//-----
void sdio_sd_deinit() {
if (sdio_status > SDIO_SD_NONE)
sdio_status = SDIO_INIT_OK;
sdio_deinit_host(); // add pvvx (fix SD_DeInit())
}
//-----
s8 sdio_sd_setClock(SD_CLK_FREQUENCY SDCLK) {
HAL_Status sta;
if (sdio_status <= SDIO_SD_NONE) {
return -1;
}
ADMA2_DESC_FMT * padma = rtw_zmalloc(sizeof(ADMA2_DESC_FMT));
if (!padma) {
DBG_SDIO_ERR("Malloc ADMA2 table fail.\n");
return -1;
}
DBG_SDIO_INFO("SD card set CLK %d Hz\n", PLATFORM_CLOCK/(4<<(8-SDCLK)));
sta = HalSdioHostOp.HalSdioHostChangeSdClock(&SdioHostAdapter, SDCLK);
rtw_mfree(padma, sizeof(ADMA2_DESC_FMT));
if (sta)
return -1;
return 0;
}
//-----
s8 sdio_sd_setProtection(bool protection) {
s8 result;
ADMA2_DESC_FMT *padma;
HAL_Status hls;
if (sdio_status > SDIO_SD_NONE) {
padma = (ADMA2_DESC_FMT *) rtw_zmalloc(sizeof(ADMA2_DESC_FMT));
if (!padma) {
DBG_SDIO_ERR("Malloc ADMA2 table fail.\n");
return -1;
}
SdioHostAdapter.AdmaDescTbl = padma;
if (protection) {
if (sd_protected == 1)
goto LABEL_8;
hls = HalSdioHostOp.HalSdioHostSetWriteProtect(&SdioHostAdapter, 1);
} else {
if (sd_protected == 0)
goto LABEL_8;
hls = HalSdioHostOp.HalSdioHostSetWriteProtect(&SdioHostAdapter, 0);
}
if (hls) {
DBG_SDIO_ERR("Set SD card Protection fail.\n");
result = -1;
goto LABEL_17;
}
LABEL_8:
sd_protected = protection;
LABEL_7:
DBG_SDIO_INFO("Set SD card Protection done.\n");
result = 0;
LABEL_17:
rtw_mfree(padma, sizeof(ADMA2_DESC_FMT));
return result;
}
return -1;
}
//----- sdio_sd_getProtection
s8 sdio_sd_getProtection() {
s8 result;
result = sd_protected;
if (sd_protected != -1)
return result;
if (sdio_status <= SDIO_SD_NONE) {
result = -1;
return result;
}
if (HalSdioHostOp.HalSdioHostGetWriteProtect(&SdioHostAdapter)) {
DBG_SDIO_ERR("Get SD card Protection fail.\n");
result = -1;
return result;
}
if (SdioHostAdapter.IsWriteProtect)
DBG_SDIO_INFO("SD card is Write Protected.\n");
result = (s8) SdioHostAdapter.IsWriteProtect;
sd_protected = SdioHostAdapter.IsWriteProtect;
return result;
}
//----- sdio_sd_status
s8 sdio_sd_status() {
s8 result;
DBG_SDIO_INFO("sdio_get_sdcard_status.\n");
if (sdio_status <= SDIO_SD_NONE
|| HalSdioHostOp.HalSdioHostGetCardStatus(&SdioHostAdapter))
result = -1;
else
result = SdioHostAdapter.CardCurState;
return result;
}
//----- sdio_sd_getCSD
s8 sdio_sd_getCSD(u8* CSD) {
s8 result;
if (sdio_status <= SDIO_SD_NONE)
result = -1;
else {
rtl_memcpy(CSD, SdioHostAdapter.Csd, CSD_REG_LEN);
result = 0;
}
return result;
}
//----- sdio_sd_isReady
s8 sdio_sd_isReady() {
s8 result = sdio_status - SDIO_SD_OK;
if (sdio_status != SDIO_SD_OK)
result = -1;
return result;
}
//----- sdio_sd_getCapacity
u32 sdio_sd_getCapacity(void) {
u32 result;
uint8_t csd[CSD_REG_LEN];
if (sdio_status <= SDIO_SD_NONE)
result = 0; // -1;
else {
rtl_memcpy(csd, SdioHostAdapter.Csd, CSD_REG_LEN);
if ((csd[0] & 0xC0) == 64)
result = (u16) (csd[9] + 1 + (csd[8] << 8)) << 9;
else
result = (4 * csd[7] + ((u32) csd[8] >> 6) + 1
+ ((csd[6] & 3) << 10))
<< ((csd[5] & 0xF) + (csd[10] >> 7) + 2 * (csd[9] & 3) - 8);
result *= 2;
}
return result;
}
s8 sdio_sd_hook_insert_cb(sdio_sd_irq_handler CallbackFun, void *param) {
card_insert_irq_handler = CallbackFun;
card_insert_irq_data = (uint32_t) param;
return 0;
}
s8 sdio_sd_hook_remove_cb(sdio_sd_irq_handler CallbackFun, void *param) {
card_remove_irq_handler = CallbackFun;
card_remove_irq_data = (uint32_t) param;
return 0;
}
s8 sdio_sd_hook_xfer_cmp_cb(sdio_sd_irq_handler CallbackFun, void *param) {
xfer_done_irq_handler = CallbackFun;
xfer_done_irq_data = (uint32_t) param;
return 0;
}
s8 sdio_sd_hook_xfer_err_cb(sdio_sd_irq_handler CallbackFun, void *param) {
xfer_err_irq_handler = CallbackFun;
xfer_err_irq_data = (uint32_t) param;
return 0;
}
#endif // CONFIG_SDIO_HOST_EN

View file

@ -0,0 +1,22 @@
#ifndef _ETH_DEBUG_H_
#define _ETH_DEBUG_H_
#define ETH_DEBUG 0
#if ETH_DEBUG
#define ETH_PRINT(fmt, args...) printf("\n\r[%s]%s: " fmt, __FUNCTION__, ## args)
#define ETH_ERROR(fmt, args...) printf("\n\r[%s]%s: " fmt, __FUNCTION__, ## args)
#define ETH_WARM(fmt, args...) printf("\n\r[%s]%s: " fmt, __FUNCTION__, ## args)
#define FUN_ENTER printf("\n\r[%s ==>]\n", __func__)
#define FUN_EXIT printf("\n\r[%s <==]\n", __func__)
#define FUN_TRACE printf("\n\r[%s]:%d \n", __func__, __LINE__)
#else
#define ETH_PRINT(fmt, args...)
#define ETH_ERROR(fmt, args...) printf("\n\r%s: " fmt,__FUNCTION__, ## args)
#define ETH_WARM(fmt, args...)
#define FUN_ENTER
#define FUN_EXIT
#define FUN_TRACE
#endif
#endif

View file

@ -0,0 +1,358 @@
/*
* Ethernet gadget driver -- with CDC and non-CDC options
* Builds on hardware support for a full duplex link.
*
* CDC Ethernet is the standard USB solution for sending Ethernet frames
* using USB. Real hardware tends to use the same framing protocol but look
* different for control features. This driver strongly prefers to use
* this USB-IF standard as its open-systems interoperability solution;
* most host side USB stacks (except from Microsoft) support it.
*
* There's some hardware that can't talk CDC. We make that hardware
* implement a "minimalist" vendor-agnostic CDC core: same framing, but
* link-level setup only requires activating the configuration.
* Linux supports it, but other host operating systems may not.
* (This is a subset of CDC Ethernet.)
*
* A third option is also in use. Rather than CDC Ethernet, or something
* simpler, Microsoft pushes their own approach: RNDIS. The published
* RNDIS specs are ambiguous and appear to be incomplete, and are also
* needlessly complex.
*/
#ifndef __USB_ETHERNET_H
#define __USB_ETHERNET_H
#include "usb.h"
#include "usb_gadget.h"
#include "core/inc/usb_composite.h"
//#define DRIVER_DESC "Ethernet Gadget"
#define DRIVER_DESC "USB Network Interface"
#define DRIVER_VERSION "May Day 2015"
#define ETH_ADDR "00E04C8196C8"
static const char shortname [] = "ether";
static const char driver_desc [] = DRIVER_DESC;
#define CONFIG_USB_ETH_RNDIS 1
#define RNDIS_VENDOR_NUM ULINKER_ETHER_VID
#define RNDIS_PRODUCT_NUM ULINKER_ETHER_PID
/* Thanks to NetChip Technologies for donating this product ID.
* It's for devices with only CDC Ethernet configurations.
*/
#define CDC_VENDOR_NUM 0x0525 /* NetChip */
#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
* ep0 implementation: descriptors, config management, setup().
* also optional class-specific notification interrupt transfer.
*/
/*
* DESCRIPTORS ... most are static, but strings and (full) configuration
* descriptors are built on demand. For now we do either full CDC, or
* our simple subset, with RNDIS as an optional second configuration.
*
* RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
* the class descriptors match a modem (they're ignored; it's really just
* Ethernet functionality), they don't need the NOP altsetting, and the
* status transfer endpoint isn't optional.
*/
#define STRING_MANUFACTURER 1
#define STRING_PRODUCT 2
#define STRING_ETHADDR 3
#define STRING_DATA 4
#define STRING_CONTROL 5
#define STRING_RNDIS_CONTROL 6
#define STRING_CDC 7
#define STRING_SUBSET 8
#define STRING_RNDIS 9
#define STRING_SERIALNUMBER 10
/* holds our biggest descriptor (or RNDIS response) */
#define USB_BUFSIZ 256
#define BUFSIZ_IN 512
#define BUFSIZ_OUT 512
/*
* This device advertises one configuration, eth_config, unless RNDIS
* is enabled (rndis_config) on hardware supporting at least two configs.
*
* NOTE: Controllers like superh_udc should probably be able to use
* an RNDIS-only configuration.
*
* FIXME define some higher-powered configurations to make it easier
* to recharge batteries ...
*/
//#define DEV_CONFIG_VALUE 1 /* cdc or subset */
//#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
#define DEV_CONFIG_VALUE 2 /* cdc or subset */
#define DEV_RNDIS_CONFIG_VALUE 1 /* rndis; optional */
#define DEVSPEED USB_SPEED_HIGH
/* descriptors that are built on-demand */
static char manufacturer [50];
static char product_desc [40] = DRIVER_DESC;
static char serial_number [20];
/* address that the host will use ... usually assigned at random */
//ModifiedByJD static char ethaddr [2 * ETH_ALEN + 1];
static char ethaddr [2 * 6 + 1] = ETH_ADDR;
/* static strings, in UTF-8 */
static struct usb_string strings [] = {
{ STRING_MANUFACTURER, manufacturer, },
{ STRING_PRODUCT, product_desc, },
{ STRING_SERIALNUMBER, serial_number, },
{ STRING_DATA, "Ethernet Data", },
#if 1//def DEV_CONFIG_CDC//ModifiedByJD
{ STRING_CDC, "CDC Ethernet", },
{ STRING_ETHADDR, ethaddr, },
{ STRING_CONTROL, "CDC Communications Control", },
#endif
#if 1//def DEV_CONFIG_SUBSET//ModifiedByJD
{ STRING_SUBSET, "CDC Ethernet Subset", },
#endif
#if 1//def CONFIG_USB_ETH_RNDIS//ModifiedByJD
{ STRING_RNDIS, "RNDIS", },
{ STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
#endif /* end of list */
};
static struct usb_gadget_strings stringtab = {
.language = 0x0409, /* en-us */
.strings = strings,
};
static struct usb_gadget_strings *dev_strings[] = {
&stringtab,
NULL,
};
static struct usb_device_descriptor
device_desc = {
.bLength = sizeof device_desc,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = (0x0200),
.bDeviceClass = USB_CLASS_COMM,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.idVendor = (CDC_VENDOR_NUM),
.idProduct = (CDC_PRODUCT_NUM),
.iManufacturer = STRING_MANUFACTURER,
.iProduct = STRING_PRODUCT,
.bNumConfigurations = 1,
};
static struct usb_config_descriptor
eth_config = {
.bLength = sizeof eth_config,
.bDescriptorType = USB_DT_CONFIG,
/* compute wTotalLength on the fly */
.bNumInterfaces = 1,
.bConfigurationValue = DEV_CONFIG_VALUE,
.iConfiguration = STRING_CDC,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 50,
};
static struct usb_otg_descriptor
otg_descriptor = {
.bLength = sizeof otg_descriptor,
.bDescriptorType = USB_DT_OTG,
.bmAttributes = USB_OTG_SRP,
};
#ifdef CONFIG_USB_ETH_RNDIS
/* RNDIS doesn't activate by changing to the "real" altsetting */
static struct usb_interface_descriptor
rndis_data_intf = {
.bLength = sizeof rndis_data_intf,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_CDC_DATA,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = STRING_DATA,
};
#endif
static struct usb_endpoint_descriptor
hs_source_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = (512),//ModifiedByJD
};
static struct usb_endpoint_descriptor
hs_sink_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = (512),//ModifiedByJD
};
static struct usb_endpoint_descriptor
fs_source_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_endpoint_descriptor
fs_sink_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static const struct usb_descriptor_header *fs_rndis_function [] = {
(struct usb_descriptor_header *) &otg_descriptor,
/* control interface matches ACM, not Ethernet */
#if 0//ModifiedByJD
(struct usb_descriptor_header *) &rndis_control_intf,
(struct usb_descriptor_header *) &header_desc,
(struct usb_descriptor_header *) &call_mgmt_descriptor,
(struct usb_descriptor_header *) &acm_descriptor,
(struct usb_descriptor_header *) &union_desc,
(struct usb_descriptor_header *) &fs_status_desc,
#endif
/* data interface has no altsetting */
(struct usb_descriptor_header *) &rndis_data_intf,
(struct usb_descriptor_header *) &fs_source_desc,
(struct usb_descriptor_header *) &fs_sink_desc,
NULL,
};
static const struct usb_descriptor_header *fs_eth_function [11] = {
(struct usb_descriptor_header *) &otg_descriptor,
#ifdef DEV_CONFIG_CDC
/* "cdc" mode descriptors */
(struct usb_descriptor_header *) &control_intf,
(struct usb_descriptor_header *) &header_desc,
(struct usb_descriptor_header *) &union_desc,
(struct usb_descriptor_header *) &ether_desc,
/* NOTE: status endpoint may need to be removed */
(struct usb_descriptor_header *) &fs_status_desc,
/* data interface, with altsetting */
(struct usb_descriptor_header *) &data_nop_intf,
(struct usb_descriptor_header *) &data_intf,
(struct usb_descriptor_header *) &fs_source_desc,
(struct usb_descriptor_header *) &fs_sink_desc,
NULL,
#endif /* DEV_CONFIG_CDC */
};
#ifdef CONFIG_USB_ETH_RNDIS
static const struct usb_descriptor_header *hs_rndis_function [] = {
(struct usb_descriptor_header *) &otg_descriptor,
/* control interface matches ACM, not Ethernet */
#if 0//ModifiedByJD
(struct usb_descriptor_header *) &rndis_control_intf,
(struct usb_descriptor_header *) &header_desc,
(struct usb_descriptor_header *) &call_mgmt_descriptor,
(struct usb_descriptor_header *) &acm_descriptor,
(struct usb_descriptor_header *) &union_desc,
(struct usb_descriptor_header *) &hs_status_desc,
#endif
/* data interface has no altsetting */
(struct usb_descriptor_header *) &rndis_data_intf,
(struct usb_descriptor_header *) &hs_source_desc,
(struct usb_descriptor_header *) &hs_sink_desc,
NULL,
};
#endif
static struct usb_config_descriptor
rndis_config = {
.bLength = sizeof rndis_config,
.bDescriptorType = USB_DT_CONFIG,
/* compute wTotalLength on the fly */
.bNumInterfaces = 1,
.bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
.iConfiguration = STRING_RNDIS,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 50,
};
static struct usb_configuration eth_configuration = {
.label = "eth_configuration",
.bConfigurationValue = DEV_CONFIG_VALUE,
// .bConfigurationValue = 1,
/* .iConfiguration = DYNAMIC */
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
};
static struct eth_dev{
struct usb_gadget *gadget;
struct usb_request *req; /* for control responses */
/* when configured, we have one of two configs:
* - source data (in to host) and sink it (out from host)
* - or loop it back (out from host back in to host)
*/
u8 config;
struct usb_ep *in_ep;
struct usb_ep *out_ep;
const struct usb_endpoint_descriptor
*in, *out, *status;
// lock is held when accessing usb
_Lock lock;
struct usb_function func;
/*send (depends on host)*/
_Sema xmit_sema;
xTaskHandle xmit_task;
unsigned int qlen;
_Mutex xmit_mutex;
_LIST eth2wlan_list;
/*receive (debuf_poolpends on host)*/
_Sema recv_sema;
xTaskHandle recv_task;
_Mutex recv_mutex;
_LIST wlan2eth_list;
};
extern int usb_eth_init(void);
#endif

View file

@ -0,0 +1,183 @@
#ifndef USBD_MSC_H
#define USBD_MSC_H
#include "usb.h"
#include "usb_gadget.h"
#include "core/inc/usb_composite.h"
#include "msc/inc/usbd_msc_config.h"
/* config usb msc device debug inforation */
#define USBD_MSC_DEBUG 0
#if USBD_MSC_DEBUG
#define USBD_PRINTF(fmt, args...) DBG_8195A("\n\r%s: " fmt, __FUNCTION__, ## args)
#define USBD_ERROR(fmt, args...) DBG_8195A("\n\r%s: " fmt, __FUNCTION__, ## args)
#define USBD_WARN(fmt, args...) DBG_8195A("\n\r%s: " fmt, __FUNCTION__, ## args)
#define FUN_ENTER DBG_8195A("\n\r%s ==>\n", __func__)
#define FUN_EXIT DBG_8195A("\n\r%s <==\n", __func__)
#define FUN_TRACE DBG_8195A("\n\r%s:%d \n", __func__, __LINE__)
#else
#define USBD_PRINTF(fmt, args...)
#define USBD_ERROR(fmt, args...) DBG_8195A("\n\r%s: " fmt, __FUNCTION__, ## args)
#define USBD_WARN(fmt, args...)
#define FUN_ENTER
#define FUN_EXIT
#define FUN_TRACE
#endif
/* MSC Request Codes */
#define MSC_REQUEST_RESET 0xFF
#define MSC_REQUEST_GET_MAX_LUN 0xFE
/* MSC LUN */
#define MSC_MAX_LOGIC_UNIT_NUMBER 1
enum data_direction{
DATA_DIR_UNKNOWN = 0,
DATA_DIR_FROM_HOST,
DATA_DIR_TO_HOST,
DATA_DIR_NONE
};
typedef enum _disk_type{
DISK_SDCARD,
DISK_FLASH
}disk_type;
//structure predefine
struct msc_dev;
struct msc_bufhd;
struct msc_opts{
int (*disk_init)(void);
int (*disk_deinit)(void);
int (*disk_getcapacity)(u32* sectors);
int (*disk_read)(u32 sector,u8 *buffer,u32 count);
int (*disk_write)(u32 sector,const u8 *buffer,u32 count);
};
struct msc_lun {
unsigned int initially_ro:1;
unsigned int ro:1;
unsigned int removable:1;
unsigned int cdrom:1;
unsigned int prevent_medium_removal:1;
unsigned int registered:1;
unsigned int info_valid:1;
unsigned int nofua:1;
u32 sense_data;
u32 sense_data_info;
u32 unit_attention_data;
u64 file_length;
unsigned int num_sectors; /* */
unsigned int blkbits; /* Bits of logical block size
of bound block device */
unsigned int blksize; /* logical block size of bound block device */
const char *name; /* "lun.name" */
unsigned int lba; // the current read and write logical block address
u8 is_open;
_mutex lun_mutex;
struct msc_opts *lun_opts;
};
struct msc_common{
struct msc_dev *mscdev;
struct msc_lun **luns;
struct msc_lun *curlun;
struct usb_gadget *gadget;
struct usb_ep *ep0;
struct usb_request *req0; /* for control responses */
/* scsi cbw relevant */
enum data_direction data_dir;
u32 data_size;
u32 data_size_from_cmnd;
u32 tag;
u32 residue;
u32 usb_amount_left;
u8 scsi_cmnd[16]; // max command
u8 cmnd_size;
u8 lun; /* current lun*/
u8 nluns;
u8 nbufhd;
u8 nbufhd_a;
_list bufhd_pool;
_mutex bufhd_mutex;
/* bulk out cmd*/
_list boc_list;
_mutex boc_mutex;
/* bolk out data*/
_mutex bod_mutex;
_list bod_list;
/**/
struct msc_bufhd* curbh; // current buffer header
struct msc_bufhd* cbw_bh; // buffer header for CBW
struct msc_bufhd* csw_bh; // buffer header for CSW
unsigned int can_stall:1;
unsigned int phase_error:1;
unsigned int short_packet_received:1;
unsigned int bad_lun_okay:1;
unsigned int running:1;
};
typedef enum _bufhd_type{
BUFHD_CBW = 0,
BUFHD_CSW,
BUFHD_DATA,
}bufhd_type;
struct msc_bufhd{
u8* buf;
int buf_size;
bufhd_type type;
_list list;
struct usb_request *reqin; /* for bulkin responses */
struct usb_request *reqout;
};
struct msc_dev{
struct msc_common *common;
u16 interface_number;
u8 config;
struct usb_ep *in_ep;
struct usb_ep *out_ep;
unsigned int bulk_in_enabled:1;
unsigned int bulk_out_enabled:1;
const struct usb_endpoint_descriptor
*in, *out, *status;
// lock is held when accessing usb
struct task_struct msc_outCmdTask;
struct task_struct msc_outDataTask;
struct usb_function func;
};
u32 min(u32 value1,u32 value2);
int usbd_msc_halt_bulk_in_endpoint(struct msc_dev *mscdev);
void usbd_msc_put_bufhd(struct msc_common *common, struct msc_bufhd* bufhd);
struct msc_bufhd* usbd_msc_get_bufhd(struct msc_common *common);
int usbd_msc_bulk_in_transfer(struct msc_dev *mscdev, struct usb_request *req);
int usbd_msc_bulk_out_transfer(struct msc_dev *mscdev, struct usb_request *req);
/*
* N_bh : number of buffer header
* Size_bh: buffer size per buffer
* type:msc physical disk type
*/
int usbd_msc_init(int N_bh, int Size_bh, disk_type type);
void usbd_msc_deinit(void);
#endif

View file

@ -0,0 +1,8 @@
#ifndef _USBD_MSC_CONFIG_H
#define _USBD_MSC_CONFIG_H
/* config usb MSC device buffer resource */
#define MSC_NBR_BUFHD 2 /* number of buffer header for bulk in/out data*/
#define MSC_BUFLEN (20*512)/* Default size of buffer length. Minmun of 512 byte*/
#endif

View file

@ -0,0 +1,196 @@
#include "usb_ch9.h"
#include "usb_defs.h"
#include "usb_gadget.h"
// <i> Enable high-speed functionality (if device supports it)
#define USBD_HS_ENABLE 1
// define string index
#define STRING_MANUFACTURER 1
#define STRING_PRODUCT 2
#define STRING_SERIALNUMBER 3
#define STRING_INTERFACE 4
#define STRING_MSC 5
#define DEV_CONFIG_VALUE 1
#define DRIVER_DESC "USB Mass Storage"
#define DRIVER_VERSION "Feb 2016"
#define MANUFACTURER "Realtek Singapore Semiconductor"
static char string_manufacturer [50] = MANUFACTURER;
static char string_product [40] = DRIVER_DESC;
static char string_serial [20] = "0123456789";
struct usb_string
usbd_msc_strings [] = {
{ STRING_MANUFACTURER, string_manufacturer, },
{ STRING_PRODUCT, string_product, },
{ STRING_SERIALNUMBER, string_serial, },
{ STRING_INTERFACE, "USB MSC Interface", },
{ STRING_MSC, "USB MSC", },
};
struct usb_gadget_strings msc_stringtab = {
.language = 0x0409, /* en-us */
.strings = usbd_msc_strings,
};
struct usb_gadget_strings *dev_msc_strings[] = {
&msc_stringtab,
NULL,
};
static struct usb_device_descriptor
usbd_msc_device_desc = {
.bLength = sizeof usbd_msc_device_desc,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = (0x0200),
.bDeviceClass = 0x00,// define in interface descriptor
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64, // this will be set automatically depends on ep0 setting
.idVendor = 0x0BDA,
.idProduct = 0x8195,
// .bcdDevice = ,
.iManufacturer = STRING_MANUFACTURER,
.iProduct = STRING_PRODUCT,
.iSerialNumber = STRING_SERIALNUMBER,
.bNumConfigurations=0x01,
};
#if 0
struct usb_config_descriptor
usbd_msc_config_desc = {
.bLength = sizeof usbd_msc_config_desc,
.bDescriptorType = USB_DT_CONFIG,
/* compute wTotalLength on the fly */
.bNumInterfaces = 1,
.bConfigurationValue = DEV_CONFIG_VALUE,
.iConfiguration = STRING_MSC,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 0x32,
};
#endif
#if USBD_HS_ENABLE
/* USB Device Qualifier Descriptor (for Full Speed) */
static struct usb_qualifier_descriptor
usbd_msc_qualifier_desc_FS = {
.bLength = sizeof usbd_msc_qualifier_desc_FS,
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
.bcdUSB = 0x0200,
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64,
.bNumConfigurations = 0x01,
.bRESERVED = 0x00,
};
/* USB Device Qualifier Descriptor for High Speed */
static struct usb_qualifier_descriptor
usbd_msc_qualifier_desc_HS = {
.bLength = sizeof usbd_msc_qualifier_desc_HS,
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
.bcdUSB = 0x0200,
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64,
.bNumConfigurations = 0x01,
.bRESERVED = 0x00,
};
#else
/* USB Device Qualifier Descriptor (for Full Speed) */
static struct usb_qualifier_descriptor
usbd_msc_qualifier_desc_FS = { 0 };
/* USB Device Qualifier Descriptor for High Speed */
static struct usb_qualifier_descriptor
usbd_msc_qualifier_desc_HS = { 0 };
#endif
/* MSC Interface, Alternate Setting 0*/
struct usb_interface_descriptor
usbd_msc_intf_desc = {
.bLength = sizeof usbd_msc_intf_desc,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0x00, // this will be assign automatically
.bAlternateSetting =0x00,
.bNumEndpoints = 0x02,
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
.bInterfaceSubClass = US_SC_SCSI,
.bInterfaceProtocol = US_PR_BULK,
.iInterface = STRING_INTERFACE,
};
/* MSC Endpoints for Low-speed/Full-speed */
/* Endpoint, EP Bulk IN */
struct usb_endpoint_descriptor
usbd_msc_source_desc_FS = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0x00,
};
/* Endpoint, EP Bulk OUT */
struct usb_endpoint_descriptor
usbd_msc_sink_desc_FS = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = (64),
.bInterval = 0x00,
};
/* MSC Endpoints for High-speed */
/* Endpoint, EP Bulk IN */
struct usb_endpoint_descriptor
usbd_msc_source_desc_HS = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = (512),
.bInterval = 0x00,
};
/* Endpoint, EP Bulk OUT */
struct usb_endpoint_descriptor
usbd_msc_sink_desc_HS = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = (512),
.bInterval = 0x00,
};
struct usb_descriptor_header *usbd_msc_descriptors_FS [] = {
/* data interface has no altsetting */
(struct usb_descriptor_header *) &usbd_msc_intf_desc,
(struct usb_descriptor_header *) &usbd_msc_source_desc_FS,
(struct usb_descriptor_header *) &usbd_msc_sink_desc_FS,
NULL,
};
struct usb_descriptor_header *usbd_msc_descriptors_HS [] = {
/* data interface has no altsetting */
(struct usb_descriptor_header *) &usbd_msc_intf_desc,
(struct usb_descriptor_header *) &usbd_msc_source_desc_HS,
(struct usb_descriptor_header *) &usbd_msc_sink_desc_HS,
NULL,
};

View file

@ -0,0 +1,110 @@
#ifndef USBD_SCSI_H
#define USBD_SCSI_H
#include "basic_types.h"
#include "msc/inc/usbd_msc.h"
#define MAX_COMMAND_SIZE 16
#define MSC_MAX_LUNS 8
/* SCSI Commands */
#define SCSI_FORMAT_UNIT 0x04
#define SCSI_INQUIRY 0x12
#define SCSI_MODE_SELECT6 0x15
#define SCSI_MODE_SELECT10 0x55
#define SCSI_MODE_SENSE6 0x1A
#define SCSI_MODE_SENSE10 0x5A
#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1E
#define SCSI_READ6 0x08
#define SCSI_READ10 0x28
#define SCSI_READ12 0xA8
#define SCSI_READ16 0x88
#define SCSI_READ_CAPACITY10 0x25
#define SCSI_READ_CAPACITY16 0x9E
#define SCSI_SYNCHRONIZE_CACHE 0x35
#define SCSI_REQUEST_SENSE 0x03
#define SCSI_START_STOP_UNIT 0x1B
#define SCSI_TEST_UNIT_READY 0x00
#define SCSI_WRITE6 0x0A
#define SCSI_WRITE10 0x2A
#define SCSI_WRITE12 0xAA
#define SCSI_WRITE16 0x8A
#define SCSI_VERIFY10 0x2F
#define SCSI_VERIFY12 0xAF
#define SCSI_VERIFY16 0x8F
#define SCSI_SEND_DIAGNOSTIC 0x1D
#define SCSI_READ_FORMAT_CAPACITIES 0x23
#define READ_FORMAT_CAPACITY_DATA_LEN 0x0C
#define READ_CAPACITY10_DATA_LEN 0x08
#define MODE_SENSE10_DATA_LEN 0x08
#define MODE_SENSE6_DATA_LEN 0x04
#define REQUEST_SENSE_DATA_LEN 0x12
#define STANDARD_INQUIRY_DATA_LEN 0x24
/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
#define SS_NO_SENSE 0
#define SS_COMMUNICATION_FAILURE 0x040800
#define SS_INVALID_COMMAND 0x052000
#define SS_INVALID_FIELD_IN_CDB 0x052400
#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
#define SS_MEDIUM_NOT_PRESENT 0x023a00
#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
#define SS_RESET_OCCURRED 0x062900
#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
#define SS_UNRECOVERED_READ_ERROR 0x031100
#define SS_WRITE_ERROR 0x030c02
#define SS_WRITE_PROTECTED 0x072700
#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
#define ASC(x) ((u8) ((x) >> 8))
#define ASCQ(x) ((u8) (x))
/*
* Bulk only data structures
*/
/* command block wrapper */
struct bulk_cb_wrap {
unsigned int Signature; /* contains 'USBC', denote bulk_cb_wrap */
unsigned int Tag; /* unique per command id */
unsigned int DataTransferLength; /* size of data for transfer */
unsigned char Flags; /* data transfer direction */
unsigned char Lun; /* LUN normally 0, (which command block is sent) */
unsigned char Length; /* length of the CDB */
unsigned char CDB[16]; /* max command */
};
#define US_BULK_CB_WRAP_LEN 31
#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
#define US_BULK_FLAG_IN (1 << 7)
#define US_BULK_FLAG_OUT 0
/* command status wrapper */
struct bulk_cs_wrap {
unsigned int Signature; /* should = 'USBS' */
unsigned int Tag; /* same as original command, echoed by the device as received */
unsigned int Residue; /* amount not transferred */
unsigned char Status; /* execute command status */
};
#define US_BULK_CS_WRAP_LEN 13
#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
// execute command status
#define US_BULK_STAT_OK 0
#define US_BULK_STAT_FAIL 1
#define US_BULK_STAT_PHASE 2
/* bulk-only class specific requests */
#define US_BULK_RESET_REQUEST 0xff
#define US_BULK_GET_MAX_LUN 0xfe
extern int usbd_msc_receive_cbw(struct msc_dev *mscdev, struct usb_request *req);
#endif

View file

@ -0,0 +1,24 @@
#ifndef _GADEGT_DEBUG_H_
#define _GADGET_DEBUG_H_
#include "diag.h"
#define GADGET_DEBUG 0
#if GADGET_DEBUG
#define GADGET_PRINT(fmt, args...) DBG_8195A("\n\r[%s]: " fmt, __FUNCTION__, ## args)
#define GADGET_ERROR(fmt, args...) DBG_8195A("\n\r[%s]: " fmt, __FUNCTION__, ## args)
#define GADGET_WARN(fmt, args...) DBG_8195A("\n\r[%s]: " fmt, __FUNCTION__, ## args)
#define FUN_ENTER DBG_8195A("\n\r[%s ==>]\n", __func__)
#define FUN_EXIT DBG_8195A("\n\r[%s <==]\n", __func__)
#define FUN_TRACE DBG_8195A("\n\r[%s]:%d \n", __func__, __LINE__)
#else
#define GADGET_PRINT(fmt, args...)
#define GADGET_ERROR(fmt, args...) DBG_8195A("\n\r[%s]: " fmt, __FUNCTION__, ## args)
#define GADGET_WARN(fmt, args...)
#define FUN_ENTER
#define FUN_EXIT
#define FUN_TRACE
#endif
#endif

View file

@ -0,0 +1,30 @@
#ifndef _OS_WRAPPER_H_
#define _OS_WRAPPER_H_
#include "osdep_api.h"
#ifndef spinlock_t
#define spinlock_t _Lock
#endif
#ifndef _atomic_spin_lock_irqsave
#define _atomic_spin_lock_irqsave(p, flags) SaveAndCli()
#endif
#ifndef _atomic_spin_unlock_irqrestore
#define _atomic_spin_unlock_irqrestore(p, flags) RestoreFlags()
#endif
/* spin lock */
#ifndef spin_lock_init
#define spin_lock_init(plock) RtlSpinlockInit((plock))
#endif
#ifndef spin_lock_free
#define spin_lock_free(plock) RtlSpinlockFree((plock))
#endif
#ifndef spin_lock
#define spin_lock(plock) RtlSpinlock((plock))
#endif
#ifndef spin_unlock
#define spin_unlock(plock) RtlSpinunlock((plock))
#endif
#endif

View file

@ -0,0 +1,398 @@
#ifndef _USB_COMPOSITE_H_
#define _USB_COMPOSITE_H_
#include "usb_gadget.h"
#include "usb.h"
/*
* USB function drivers should return USB_GADGET_DELAYED_STATUS if they
* wish to delay the data/status stages of the control transfer till they
* are ready. The control transfer will then be kept from completing till
* all the function drivers that requested for USB_GADGET_DELAYED_STAUS
* invoke usb_composite_setup_continue().
*/
#define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */
/* big enough to hold our biggest descriptor */
#define USB_COMP_EP0_BUFSIZ 1024+24
#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
// predefine structure
struct usb_composite_dev;
struct usb_composite_driver;
enum control_request_return_codes {
USBD_REQ_NOTSUPP = 0,
USBD_REQ_HANDLED = 1,
USBD_REQ_NEXT_CALLBACK = 2,
};
/**
* struct usb_composite_driver - groups configurations into a gadget
* @name: For diagnostics, identifies the driver.
* @dev: Template descriptor for the device, including default device
* identifiers.
* @strings: tables of strings, keyed by identifiers assigned during @bind
* and language IDs provided in control requests. Note: The first entries
* are predefined. The first entry that may be used is
* USB_GADGET_FIRST_AVAIL_IDX
* @max_speed: Highest speed the driver supports.
* @needs_serial: set to 1 if the gadget needs userspace to provide
* a serial number. If one is not provided, warning will be printed.
* @bind: (REQUIRED) Used to allocate resources that are shared across the
* whole device, such as string IDs, and add its configurations using
* @usb_add_config(). This may fail by returning a negative errno
* value; it should return zero on successful initialization.
* @unbind: Reverses @bind; called as a side effect of unregistering
* this driver.
* @disconnect: optional driver disconnect method
* @suspend: Notifies when the host stops sending USB traffic,
* after function notifications
* @resume: Notifies configuration when the host restarts USB traffic,
* before function notifications
* @gadget_driver: Gadget driver controlling this driver
*
* Devices default to reporting self powered operation. Devices which rely
* on bus powered operation should report this in their @bind method.
*
* Before returning from @bind, various fields in the template descriptor
* may be overridden. These include the idVendor/idProduct/bcdDevice values
* normally to bind the appropriate host side driver, and the three strings
* (iManufacturer, iProduct, iSerialNumber) normally used to provide user
* meaningful device identifiers. (The strings will not be defined unless
* they are defined in @dev and @strings.) The correct ep0 maxpacket size
* is also reported, as defined by the underlying controller driver.
*/
struct usb_composite_driver {
const char *name;
const struct usb_device_descriptor *dev;
struct usb_gadget_strings **strings;
enum usb_device_speed max_speed;
unsigned needs_serial:1;
int (*bind)(struct usb_composite_dev *cdev);
int (*unbind)(struct usb_composite_dev *);
void (*disconnect)(struct usb_composite_dev *);
/* global suspend hooks */
void (*suspend)(struct usb_composite_dev *);
void (*resume)(struct usb_composite_dev *);
struct usb_gadget_driver gadget_driver;
};
/**
* struct usb_composite_device - represents one composite usb gadget
* @gadget: read-only, abstracts the gadget's usb peripheral controller
* @req: used for control responses; buffer is pre-allocated
* @os_desc_req: used for OS descriptors responses; buffer is pre-allocated
* @config: the currently active configuration
* @qw_sign: qwSignature part of the OS string
* @b_vendor_code: bMS_VendorCode part of the OS string
* @use_os_string: false by default, interested gadgets set it
* @os_desc_config: the configuration to be used with OS descriptors
*
* One of these devices is allocated and initialized before the
* associated device driver's bind() is called.
*
* OPEN ISSUE: it appears that some WUSB devices will need to be
* built by combining a normal (wired) gadget with a wireless one.
* This revision of the gadget framework should probably try to make
* sure doing that won't hurt too much.
*
* One notion for how to handle Wireless USB devices involves:
* (a) a second gadget here, discovery mechanism TBD, but likely
* needing separate "register/unregister WUSB gadget" calls;
* (b) updates to usb_gadget to include flags "is it wireless",
* "is it wired", plus (presumably in a wrapper structure)
* bandgroup and PHY info;
* (c) presumably a wireless_ep wrapping a usb_ep, and reporting
* wireless-specific parameters like maxburst and maxsequence;
* (d) configurations that are specific to wireless links;
* (e) function drivers that understand wireless configs and will
* support wireless for (additional) function instances;
* (f) a function to support association setup (like CBAF), not
* necessarily requiring a wireless adapter;
* (g) composite device setup that can create one or more wireless
* configs, including appropriate association setup support;
* (h) more, TBD.
*/
#define MAX_USER_CONTROL_CALLBACK 2
typedef int (*user_control_callback)(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl);
struct usb_composite_dev {
struct usb_gadget *gadget;
struct usb_request *req;
struct usb_request *os_desc_req;
struct usb_configuration *config;
//
// /* OS String is a custom (yet popular) extension to the USB standard. */
// u8 qw_sign[OS_STRING_QW_SIGN_LEN];
// u8 b_vendor_code;
// struct usb_configuration *os_desc_config;
// unsigned int use_os_string:1;
//
// /* private: */
// /* internals */
unsigned int suspended:1;
struct usb_device_descriptor desc;
//_LIST config_list;
dwc_list_link_t config_list; // by jimmy
//_LIST gstring_list;
dwc_list_link_t gstring_list;// by jimmy
struct usb_composite_driver *driver;
// u8 next_string_id;
// char *def_manufacturer;
//
// /* the gadget driver won't enable the data pullup
// * while the deactivation count is nonzero.
// */
// unsigned deactivations;
//
// /* the composite driver won't complete the control transfer's
// * data/status stages till delayed_status is zero.
// */
// int delayed_status;
//
// /* protects deactivations and delayed_status counts*/
_Lock lock;
/* for unstandard control request handler */
user_control_callback control_cb[MAX_USER_CONTROL_CALLBACK];
};
#if 0
#define container_of(p,t,n) (t*)((p)-&(((t*)0)->n))
static inline struct usb_composite_driver *to_cdriver(
struct usb_gadget_driver *gdrv)
{
return container_of(gdrv, struct usb_composite_driver, gadget_driver);
}
#endif
#if 1
/**
* struct usb_configuration - represents one gadget configuration
* @label: For diagnostics, describes the configuration.
* @strings: Tables of strings, keyed by identifiers assigned during @bind()
* and by language IDs provided in control requests.
* @descriptors: Table of descriptors preceding all function descriptors.
* Examples include OTG and vendor-specific descriptors.
* @unbind: Reverses @bind; called as a side effect of unregistering the
* driver which added this configuration.
* @setup: Used to delegate control requests that aren't handled by standard
* device infrastructure or directed at a specific interface.
* @bConfigurationValue: Copied into configuration descriptor.
* @iConfiguration: Copied into configuration descriptor.
* @bmAttributes: Copied into configuration descriptor.
* @MaxPower: Power consumtion in mA. Used to compute bMaxPower in the
* configuration descriptor after considering the bus speed.
* @cdev: assigned by @usb_add_config() before calling @bind(); this is
* the device associated with this configuration.
*
* Configurations are building blocks for gadget drivers structured around
* function drivers. Simple USB gadgets require only one function and one
* configuration, and handle dual-speed hardware by always providing the same
* functionality. Slightly more complex gadgets may have more than one
* single-function configuration at a given speed; or have configurations
* that only work at one speed.
*
* Composite devices are, by definition, ones with configurations which
* include more than one function.
*
* The lifecycle of a usb_configuration includes allocation, initialization
* of the fields described above, and calling @usb_add_config() to set up
* internal data and bind it to a specific device. The configuration's
* @bind() method is then used to initialize all the functions and then
* call @usb_add_function() for them.
*
* Those functions would normally be independent of each other, but that's
* not mandatory. CDC WMC devices are an example where functions often
* depend on other functions, with some functions subsidiary to others.
* Such interdependency may be managed in any way, so long as all of the
* descriptors complete by the time the composite driver returns from
* its bind() routine.
*/
struct usb_configuration {
const char *label;
struct usb_gadget_strings **strings;
const struct usb_descriptor_header **descriptors;
/* REVISIT: bind() functions can be marked __init, which
* makes trouble for section mismatch analysis. See if
* we can't restructure things to avoid mismatching...
*/
/* configuration management: unbind/setup */
void (*unbind)(struct usb_configuration *);
int (*setup)(struct usb_configuration *,
const struct usb_ctrlrequest *);
/* fields in the config descriptor */
u8 bConfigurationValue;
u8 iConfiguration;
u8 bmAttributes;
u16 MaxPower;
struct usb_composite_dev *cdev;
/* private: */
/* internals */
//_LIST list;
//_LIST function_lists;
dwc_list_link_t list;
dwc_list_link_t function_lists; // by jimmy
u8 next_interface_id;
unsigned superspeed:1;
unsigned highspeed:1;
unsigned fullspeed:1;
struct usb_function *interface[MAX_CONFIG_INTERFACES];
};
_LONG_CALL_ int usb_interface_id(struct usb_configuration *config,
struct usb_function *function);
_LONG_CALL_ int usb_add_config(struct usb_composite_dev *,
struct usb_configuration *,
int (*)(struct usb_configuration *));
_LONG_CALL_ void usb_remove_config(struct usb_composite_dev *,
struct usb_configuration *);
/**
* struct usb_function - describes one function of a configuration
* @name: For diagnostics, identifies the function.
* @strings: tables of strings, keyed by identifiers assigned during bind()
* and by language IDs provided in control requests
* @fs_descriptors: Table of full (or low) speed descriptors, using interface and
* string identifiers assigned during @bind(). If this pointer is null,
* the function will not be available at full speed (or at low speed).
* @hs_descriptors: Table of high speed descriptors, using interface and
* string identifiers assigned during @bind(). If this pointer is null,
* the function will not be available at high speed.
* @ss_descriptors: Table of super speed descriptors, using interface and
* string identifiers assigned during @bind(). If this
* pointer is null after initiation, the function will not
* be available at super speed.
* @config: assigned when @usb_add_function() is called; this is the
* configuration with which this function is associated.
* @os_desc_table: Table of (interface id, os descriptors) pairs. The function
* can expose more than one interface. If an interface is a member of
* an IAD, only the first interface of IAD has its entry in the table.
* @os_desc_n: Number of entries in os_desc_table
* @bind: Before the gadget can register, all of its functions bind() to the
* available resources including string and interface identifiers used
* in interface or class descriptors; endpoints; I/O buffers; and so on.
* @unbind: Reverses @bind; called as a side effect of unregistering the
* driver which added this function.
* @free_func: free the struct usb_function.
* @mod: (internal) points to the module that created this structure.
* @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
* initialize usb_ep.driver data at this time (when it is used).
* Note that setting an interface to its current altsetting resets
* interface state, and that all interfaces have a disabled state.
* @get_alt: Returns the active altsetting. If this is not provided,
* then only altsetting zero is supported.
* @disable: (REQUIRED) Indicates the function should be disabled. Reasons
* include host resetting or reconfiguring the gadget, and disconnection.
* @setup: Used for interface-specific control requests.
* @suspend: Notifies functions when the host stops sending USB traffic.
* @resume: Notifies functions when the host restarts USB traffic.
* @get_status: Returns function status as a reply to
* GetStatus() request when the recipient is Interface.
* @func_suspend: callback to be called when
* SetFeature(FUNCTION_SUSPEND) is reseived
*
* A single USB function uses one or more interfaces, and should in most
* cases support operation at both full and high speeds. Each function is
* associated by @usb_add_function() with a one configuration; that function
* causes @bind() to be called so resources can be allocated as part of
* setting up a gadget driver. Those resources include endpoints, which
* should be allocated using @usb_ep_autoconfig().
*
* To support dual speed operation, a function driver provides descriptors
* for both high and full speed operation. Except in rare cases that don't
* involve bulk endpoints, each speed needs different endpoint descriptors.
*
* Function drivers choose their own strategies for managing instance data.
* The simplest strategy just declares it "static', which means the function
* can only be activated once. If the function needs to be exposed in more
* than one configuration at a given speed, it needs to support multiple
* usb_function structures (one for each configuration).
*
* A more complex strategy might encapsulate a @usb_function structure inside
* a driver-specific instance structure to allows multiple activations. An
* example of multiple activations might be a CDC ACM function that supports
* two or more distinct instances within the same configuration, providing
* several independent logical data links to a USB host.
*/
struct usb_function {
const char *name;
struct usb_gadget_strings **strings;
struct usb_descriptor_header **fs_descriptors;
struct usb_descriptor_header **hs_descriptors;
// struct usb_descriptor_header **ss_descriptors;
struct usb_configuration *config;
// struct usb_os_desc_table *os_desc_table;
// unsigned os_desc_n;
/* REVISIT: bind() functions can be marked __init, which
* makes trouble for section mismatch analysis. See if
* we can't restructure things to avoid mismatching.
* Related: unbind() may kfree() but bind() won't...
*/
/* configuration management: bind/unbind */
int (*bind)(struct usb_configuration *,
struct usb_function *);
void (*unbind)(struct usb_configuration *,
struct usb_function *);
void (*free_func)(struct usb_function *f);
struct module *mod;
/* runtime state management */
int (*set_alt)(struct usb_function *,
unsigned interface, unsigned alt);
int (*get_alt)(struct usb_function *,
unsigned interface);
void (*disable)(struct usb_function *);
int (*setup)(struct usb_function *,
const struct usb_ctrlrequest *);
void (*suspend)(struct usb_function *);
void (*resume)(struct usb_function *);
/* USB 3.0 additions */
int (*get_status)(struct usb_function *);
int (*func_suspend)(struct usb_function *,
u8 suspend_opt);
/* private: */
/* internals */
//_LIST list;
dwc_list_link_t list; // by jimmy
// DECLARE_BITMAP(endpoints, 32);
// const struct usb_function_instance *fi;
};
#endif
extern _LONG_CALL_ int usb_add_function(struct usb_configuration *, struct usb_function *);
extern _LONG_CALL_ void usb_remove_function(struct usb_configuration *, struct usb_function *);
extern _LONG_CALL_ void usb_put_function(struct usb_function *);
extern _LONG_CALL_ int usb_function_deactivate(struct usb_function *);
extern _LONG_CALL_ int usb_function_activate(struct usb_function *);
extern _LONG_CALL_ int usb_interface_id(struct usb_configuration *, struct usb_function *);
extern _LONG_CALL_ int usb_composite_probe(struct usb_composite_driver *driver);
extern _LONG_CALL_ int register_class_vendor_control_request_cb(struct usb_composite_dev *cdev, user_control_callback cb);
extern _LONG_CALL_ void usb_composite_unregister(struct usb_composite_driver *driver);
#endif

View file

@ -0,0 +1,12 @@
#ifndef _USB_CONFIG_H_
#define _USB_CONFIG_H_
#include "core/inc/usb_composite.h"
extern _LONG_CALL_ int usb_assign_descriptors(struct usb_function *f,
struct usb_descriptor_header **fs,
struct usb_descriptor_header **hs,
struct usb_descriptor_header **ss);
extern _LONG_CALL_ void usb_free_all_descriptors(struct usb_function *f);
#endif

View file

@ -0,0 +1,5 @@
#ifndef _USB_BOT_H
#define _USB_BOT_H
#include "basic_types.h"
#endif

View file

@ -0,0 +1,34 @@
/*
* This file holds the definitions of quirks found in USB devices.
* Only quirks that affect the whole device, not an interface,
* belong here.
*/
#ifndef __QUIRKS_H
#define __QUIRKS_H
/* string descriptors must not be fetched using a 255-byte read */
#define USB_QUIRK_STRING_FETCH_255 0x00000001
/* device can't resume correctly so reset it instead */
#define USB_QUIRK_RESET_RESUME 0x00000002
/* device can't handle Set-Interface requests */
#define USB_QUIRK_NO_SET_INTF 0x00000004
/* device can't handle its Configuration or Interface strings */
#define USB_QUIRK_CONFIG_INTF_STRINGS 0x00000008
/* device can't be reset(e.g morph devices), don't use reset */
#define USB_QUIRK_RESET 0x00000010
/* device has more interface descriptions than the bNumInterfaces count,
and can't handle talking to these interfaces */
#define USB_QUIRK_HONOR_BNUMINTERFACES 0x00000020
/* device needs a pause during initialization, after we read the device
descriptor */
#define USB_QUIRK_DELAY_INIT 0x00000040
#endif /* __QUIRKS_H */

View file

@ -0,0 +1,20 @@
#ifndef _SCATTERLIST_H
#define _SCATTERLIST_H
struct scatterlist {
unsigned long sg_magic;
unsigned long page_link;
unsigned int offset;
unsigned int length;
dma_addr_t dma_address;
__u32 dma_length;
};
struct sg_table {
struct scatterlist *sgl; /* the list */
unsigned int nents; /* number of mapped entries */
unsigned int orig_nents; /* original size of list */
};
#endif

View file

@ -0,0 +1,12 @@
#ifndef _DMA_DIRECTION_H
#define _DMA_DIRECTION_H
enum dma_data_direction {
DMA_BIDIRECTIONAL = 0,
DMA_TO_DEVICE = 1,
DMA_FROM_DEVICE = 2,
DMA_NONE = 3,
};
#endif

Some files were not shown because too many files have changed in this diff Show more