rtlduino rtl8710af gcc base version

This commit is contained in:
RtlduinoMan 2016-09-12 17:16:56 +08:00
parent 7675bdb95f
commit 9c0c9edf61
2097 changed files with 779974 additions and 2 deletions

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,319 @@
/*
* 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,450 @@
#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>
#include <polarssl/ssl.h>
#include <polarssl/memory.h>
#define WLAN0_NAME "wlan0"
#ifndef ENABLE
#define ENABLE (1)
#endif
#ifndef DISABLE
#define DISABLE (0)
#endif
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;
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);
void set_eap_phase(unsigned char is_trigger_eap){
eap_phase = is_trigger_eap;
}
int get_eap_phase(void){
return eap_phase;
}
int get_eap_method(void){
return eap_method;
}
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;
}
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();
}
/*
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
}
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(target_ssid,
RTW_SECURITY_OPEN,
NULL,
strlen(target_ssid),
0,
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);
}
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;
}

View file

@ -0,0 +1,269 @@
#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,
strlen(ssid),
strlen(passphrase),
channel
) != RTW_SUCCESS) {
printf("\n\rERROR: Operation failed!");
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("\n\rUsage: p2p_connect DEST_ADDR [pbc|pin] [pin code]\n");
printf("\n\rExample: p2p_connect 00:e0:4c:87:00:15 pin 12345678\n");
return;
}
if (hwaddr_aton(argv[1], dest)){
printf("\r\nP2P_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("\n\rUnknown config method!\n");
printf("\n\rUsage: p2p_connect DEST_ADDR [pbc|pin] \n");
printf("\n\rExample: 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("\n\rUnknown config method!\n");
printf("\n\rUsage: p2p_connect DEST_ADDR [pbc|pin] [pin code]\n");
printf("\n\rExample: 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,753 @@
#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("\r\nwps_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("\r\nwps_wifi.ssid = %s\n", wifi->ssid.val);
wifi->ssid.len = dev_cred->ssid_len;
printf("\r\nwps_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("\r\nsecurity_type = RTW_SECURITY_WEP_PSK\n");
wifi->security_type = RTW_SECURITY_WEP_PSK;
wifi->key_id = dev_cred->key_idx - 1;
}
else {
printf("\r\nsecurity_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("\r\nsecurity_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("\r\nsecurity_type = RTW_SECURITY_WPA2_AES_PSK\n");
wifi->security_type = RTW_SECURITY_WPA2_AES_PSK;
break;
}
printf("\r\nwps_wifi.security_type = %d\n", wifi->security_type);
//memcpy(wifi->password, dev_cred->key, dev_cred->key_len);
wifi->password = dev_cred->key;
printf("\r\nwps_wifi.password = %s\n", wifi->password);
wifi->password_len = dev_cred->key_len;
printf("\r\nwps_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("\r\n=============== wifi_certificate_info ===============\n");
printf("\r\nwps_wifi.ssid = %s\n", wifi->ssid.val);
printf("\r\nsecurity_type = %d\n", wifi->security_type);
printf("\r\nwps_wifi.password = %s\n", wifi->password);
printf("\r\nssid_len = %d\n", wifi->ssid.len);
printf("\r\npassword_len = %d\n", wifi->password_len);
while (1) {
ret = wifi_connect((char*)wifi->ssid.val,
wifi->security_type,
(char*)wifi->password,
wifi->ssid.len,
wifi->password_len,
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("\r\n[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(target_ssid,
RTW_SECURITY_OPEN,
NULL,
strlen(target_ssid),
0,
0,
NULL);
if (ret == RTW_SUCCESS) {
//wps_check_and_show_connection_info();
break;
}
if (retry_count == 0) {
printf("\r\n[WPS]Join bss failed\n");
return -1;
}
retry_count --;
}
//
} else {
printf("\r\n[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("\r\n[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("\r\n[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("\r\nWPS scan done!\r\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("\n\rERROR: wifi scan failed");
goto exit;
}
if(rtw_down_timeout_sema(&wps_arg.scan_sema, SCAN_LONGEST_WAIT_TIME) == RTW_FALSE){
printf("\r\nWPS scan done early!\r\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("\n\rWPS: 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("\n\rWPS: 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("\r\nWPS: WPS Walking Time Out\n");
return -2;
}
}
if (is_overlap > 0) {
printf("\r\nWPS: 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("\n\rWPS: 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("\n\rWPS: 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("\n\rWPS-AP: Enter pin code is unvalid.");
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("\n\rWPS-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("\n\rWPS-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("\n\rWPS-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("\n\rWPS-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("\n\rWPS-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("\n\rLast 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("\n\r%s xTaskCreate(ap_wps thread) failed", __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("\n\rWPS: Start WPS PIN Display. PIN: [%s]\n\r", device_pin);
printf("\n\rWPS: Start WPS PIN Display. PIN: [%s]\n\r", device_pin);
printf("\n\rWPS: Start WPS PIN Display. PIN: [%s]\n\r", 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("\n\rWPS: Device pin code is invalid. Not triger WPS.\n");
goto exit;
}
printf("\n\rWPS: Start WPS PIN Keypad.\n\r");
ret = wps_start(WPS_CONFIG_KEYPAD, argv[2], 0, NULL);
}
}else if(strcmp(argv[1],"pbc") == 0){
/* start pbc */
printf("\n\rWPS: Start WPS PBC.\n\r");
ret = wps_start(WPS_CONFIG_PUSHBUTTON, NULL, 0, NULL);
}else{
printf("\n\rWPS: 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("\n\rNot support con-current softAP WSC!\n\r");
return;
}
wext_get_mode(WLAN0_NAME, &mode);
if(mode != IW_MODE_MASTER){
printf("\n\rOnly valid for IW_MODE_MASTER!\n\r");
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("\n\rWPS-AP: Device pin code is invalid. Not trigger WPS.\n\r");
return;
}
printf("\n\rWPS-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("\n\rWPS: Start WPS PIN Display. PIN: %s\n\r", device_pin);
wifi_start_ap_wps_thread(WPS_CONFIG_DISPLAY, (char*)device_pin);
}
}else if (strcmp(argv[1],"pbc") == 0) {
printf("\n\rWPS-AP: Start AP WPS PBC\n");
wifi_start_ap_wps_thread(WPS_CONFIG_PUSHBUTTON, NULL);
}else{
printf("\n\rWPS-AP Usage:\"wifi_ap_wps pin [pin_code]\" or \"wifi_ap_wps pbc\"\n");
return;
}
} else {
printf("\n\rWPS-AP Usage:\"wifi_ap_wps pin [pin_code]\" or \"wifi_ap_wps pbc\"\n");
}
return;
}
#endif //CONFIG_ENABLE_P2P
#endif //CONFIG_ENABLE_WPS