first commit

This commit is contained in:
jeffrey 2015-11-17 10:30:14 +08:00
parent 48de61fed7
commit 28cd8da44d
1181 changed files with 784669 additions and 0 deletions

View file

@ -0,0 +1,84 @@
#include "cmsis_os.h"
#include <cJSON.h>
#define malloc pvPortMalloc
#define free vPortFree
/* The data structure for this example
{
"Motion_Sensor" : "i",
"Light" : {
"Red" : "0",
"Green" : "0",
"Blue" : "0",
}
}
*/
static void gen_json_data(int i, int r, int g, int b)
{
cJSON_Hooks memoryHook;
memoryHook.malloc_fn = malloc;
memoryHook.free_fn = free;
cJSON_InitHooks(&memoryHook);
cJSON *IOTJSObject = NULL, *colorJSObject = NULL;
char *iot_json = NULL;
if((IOTJSObject = cJSON_CreateObject()) != NULL) {
cJSON_AddItemToObject(IOTJSObject, "Motion_Sensor", cJSON_CreateNumber(i));
cJSON_AddItemToObject(IOTJSObject, "Light", colorJSObject = cJSON_CreateObject());
cJSON_AddItemToObject(colorJSObject, "Red", cJSON_CreateNumber(r));
cJSON_AddItemToObject(colorJSObject, "Green", cJSON_CreateNumber(g));
cJSON_AddItemToObject(colorJSObject, "Blue", cJSON_CreateNumber(b));
iot_json = cJSON_Print(IOTJSObject);
cJSON_Delete(IOTJSObject);
}
}
static void handle_json_data(char *iot_json)
{
cJSON_Hooks memoryHook;
memoryHook.malloc_fn = malloc;
memoryHook.free_fn = free;
cJSON_InitHooks(&memoryHook);
cJSON *IOTJSObject, *sensorJSObject, *lightJSObject, *redJSObject, *greenJSObject, *blueJSObject;
int sensor_data, red, green, blue;
if((IOTJSObject = cJSON_Parse(iot_json)) != NULL) {
sensorJSObject = cJSON_GetObjectItem(IOTJSObject, "Motion_Sensor");
if(sensorJSObject)
sensor_data = sensorJSObject->valueint;
lightJSObject = cJSON_GetObjectItem(IOTJSObject, "Light");
if(lightJSObject){
redJSObject = cJSON_GetObjectItem(lightJSObject, "Red");
greenJSObject = cJSON_GetObjectItem(lightJSObject, "Green");
blueJSObject = cJSON_GetObjectItem(lightJSObject, "Blue");
if(redJSObject)
red = redJSObject->valueint;
if(greenJSObject)
green = greenJSObject->valueint;
if(blueJSObject)
blue = blueJSObject->valueint;
}
cJSON_Delete(IOTJSObject);
}
}

View file

@ -0,0 +1,59 @@
/******************************************************************************
*
* Copyright(c) 2007 - 2015 Realtek Corporation. All rights reserved.
*
*
******************************************************************************/
#include <platform_opts.h>
#if CONFIG_EXAMPLE_MDNS
#include <mdns/example_mdns.h>
#endif
#if CONFIG_EXAMPLE_MCAST
#include <mcast/example_mcast.h>
#endif
#if CONFIG_EXAMPLE_GOOGLE_NEST
#include <googlenest/example_google.h>
#define FromDevice 1
#define ToDevice 2
#define TYPE "ToDevice"
#endif
#if CONFIG_EXAMPLE_WLAN_FAST_CONNECT
#include <wlan_fast_connect/example_wlan_fast_connect.h>
#endif
/*
Preprocessor of example
*/
void pre_example_entry(void)
{
#if CONFIG_EXAMPLE_WLAN_FAST_CONNECT
example_wlan_fast_connect();
#endif
#if CONFIG_EXAMPLE_UART_ADAPTER
example_uart_adapter_init();
#endif
}
/*
All of the examples are disabled by default for code size consideration
The configuration is enabled in platform_opts.h
*/
void example_entry(void)
{
#if (CONFIG_EXAMPLE_MDNS && !CONFIG_EXAMPLE_UART_ADAPTER)
example_mdns();
#endif
#if CONFIG_EXAMPLE_MCAST
example_mcast();
#endif
#if CONFIG_EXAMPLE_GOOGLE_NEST
example_google(TYPE);
#endif
}

View file

@ -0,0 +1,8 @@
#ifndef __EXAMPLE_ENTRY_H__
#define __EXAMPLE_ENTRY_H__
void example_entry(void);
void pre_example_entry(void);
#endif //#ifndef __EXAMPLE_ENTRY_H__

View file

@ -0,0 +1,38 @@
<html>
<head>
<script src="https://cdn.firebase.com/js/client/2.1.1/firebase.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<h1>Realtek Google Nest API example</h1>
<hr />
<h3 style="text-align:center;margin-left:auto;margin-right:auto;">Data To Device</h3>
<h4>Input the value of RGB to change the state of light:</h4>
<p>Red <input type="text" id="redInput" placeholder="Red" />
Green <input type="text" id="greenInput" placeholder="Green" />
Blue <input type="text" id="blueInput" placeholder="Blue" />
<input type="button" value="submit" name="submit" onClick="submit()" />
</p>
<script>
var myDataRef = new Firebase('https://your_firebase_address.firebaseio.com/light');
function submit()
{
var red = $('#redInput').val();
var green = $('#greenInput').val();
var blue = $('#blueInput').val();
myDataRef.set({
Red: red,
Green: green,
Blue: blue
});
};
</script>
</body>
</html>

View file

@ -0,0 +1,185 @@
#include "cmsis_os.h"
#include "diag.h"
#include "wifi_conf.h"
#include "wifi_ind.h"
#include "google_nest.h"
#include <googlenest/example_google.h>
#include "cJSON.h"
osThreadId google_thread_id;
#define malloc pvPortMalloc
#define free vPortFree
void google_data_retrieve(char *response_buf);
void google_data_retrieve(char *response_buf) {
//printf("\r\n\r\n\r\nResponse_buf:\r\n%s\r\n", response_buf);
char *event = NULL;
char *delims = "\n";
char *data = NULL, *backup = NULL;
char *info = NULL;
cJSON_Hooks memoryHook;
event = strtok_r(response_buf, delims, &backup);
data = strtok_r( NULL, delims, &backup );
if (!strncmp(data, "data: ", strlen("data: "))){
info = data + strlen("data: ");
if(info != NULL){
memoryHook.malloc_fn = malloc;
memoryHook.free_fn = free;
cJSON_InitHooks(&memoryHook);
cJSON *infoJSObject, *pathJSObject, *dataJSObject;
cJSON *redJSObject, *greenJSObject, *blueJSObject;
char *red, *green, *blue;
if((infoJSObject = cJSON_Parse(info)) != NULL) {
pathJSObject = cJSON_GetObjectItem(infoJSObject, "path");
dataJSObject = cJSON_GetObjectItem(infoJSObject, "data");
if(dataJSObject != NULL) {
redJSObject = cJSON_GetObjectItem(dataJSObject, "Red");
greenJSObject = cJSON_GetObjectItem(dataJSObject, "Green");
blueJSObject = cJSON_GetObjectItem(dataJSObject, "Blue");
if(redJSObject)
red = redJSObject->valuestring;
if(greenJSObject)
green = greenJSObject->valuestring;
if(blueJSObject)
blue = blueJSObject->valuestring;
printf("\n\rThe latest RGB information: RGB(%s, %s, %s)\n\r", red, green, blue);
cJSON_Delete(dataJSObject);
}
cJSON_Delete(infoJSObject);
}
else
printf("\r\nCannot parse the message to JSON!\r\n");
}
else
printf("\r\n This is the keep alive message or cannot get the information!\r\n");
}
else
printf("\r\nData structure may wrong!\r\n");
}
void gn_todevice_start(void) {
googlenest_context googlenest;
char *googlenest_host = HOST_ADDR;
char *googlenest_uri = "light.json";
printf("\r\nStart connecting to Google Nest Server...\r\n");
memset(&googlenest, 0, sizeof(googlenest_context));
reconnect:
if(gn_connect(&googlenest, googlenest_host, GN_PORT) == 0) {
printf("\r\n Connection is OK!\r\n");
google_retrieve_data_hook_callback(google_data_retrieve);
if(gn_stream(&googlenest, googlenest_uri) != 0){
printf("\r\n Connection is fail! \r\n Start Reconnecting...\r\n");
goto reconnect;
}
gn_close(&googlenest);
}
else{
printf("\r\n Connection is fail! \r\n\r\n\r\n\r\nStart Reconnecting...\r\n");
goto reconnect;
}
}
void gn_fromdevice_start(void) {
googlenest_context googlenest;
char *googlenest_uri = "MotionSensor.json";
cJSON_Hooks memoryHook;
int j = 0;
memoryHook.malloc_fn = malloc;
memoryHook.free_fn = free;
cJSON_InitHooks(&memoryHook);
printf("\r\nStart connecting to Google Nest Server!\r\n");
while(1) {
memset(&googlenest, 0, sizeof(googlenest_context));
if(gn_connect(&googlenest, HOST_ADDR, GN_PORT) == 0) {
cJSON *MSJSObject;
char *data;
if((MSJSObject = cJSON_CreateObject()) != NULL) {
cJSON_AddItemToObject(MSJSObject, "MotionSenser", cJSON_CreateNumber(j++));
data = cJSON_Print(MSJSObject);
cJSON_Delete(MSJSObject);
}
if(gn_put(&googlenest, googlenest_uri, data) == 0)
printf("\n\rUpdate the Motion Sensor's data to %d\n\r", (j-1));
free(data);
gn_close(&googlenest);
}
else{
printf("\n\rConnection failed!\n\r");
break;
}
vTaskDelay(5 * configTICK_RATE_HZ);
}
}
void gn_fromdevice_task(void *arg) {
int i;
printf("\n\r\n\r\n\r\n\r<<<<<<Waiting for 2 mins to connect Wi-Fi>>>>>>>\n\r\n\r\n\r\n\r");
for (i = 0; i<120; i++)
vTaskDelay(1000 / portTICK_PERIOD_MS);
gn_fromdevice_start();
}
void gn_todevice_task(void *arg) {
int i;
printf("\n\r\n\r\n\r\n\r<<<<<<Waiting for 2 mins to connect Wi-Fi>>>>>>>\n\r\n\r\n\r\n\r");
for (i = 0; i<120; i++)
vTaskDelay(1000 / portTICK_PERIOD_MS);
gn_todevice_start();
}
void example_google(char *type) {
if(strcmp(type, "FromDevice") == 0){
osThreadDef_t google_thread;
google_thread.instances = 1;
google_thread.name = "google thread";
google_thread.stacksize = 4096;
google_thread.pthread = (os_pthread)gn_fromdevice_task;
google_thread.tpriority = tskIDLE_PRIORITY+6;
google_thread_id = osThreadCreate(&google_thread, NULL);
}
else if(strcmp(type, "ToDevice") == 0){
osThreadDef_t google_thread;
google_thread.instances = 1;
google_thread.name = "google thread";
google_thread.stacksize = 4096;
google_thread.pthread = (os_pthread)gn_todevice_task;
google_thread.tpriority = tskIDLE_PRIORITY+6;
google_thread_id = osThreadCreate(&google_thread, NULL);
}
}

View file

@ -0,0 +1,10 @@
#ifndef GOOGLE_THREAD_H
#define GOOGLE_THREAD_H
#define HOST_ADDR "your_firebase_address.firebaseio.com"
#define GN_PORT 443
void example_google(char *type);
#endif

View file

@ -0,0 +1,98 @@
#include "FreeRTOS.h"
#include "task.h"
#include <platform/platform_stdlib.h>
#include <lwip/sockets.h>
#include <lwip_netconf.h>
#include <lwip/netif.h>
extern struct netif xnetif[];
static void example_mcast_thread(void *param)
{
#if LWIP_IGMP
int err = 0;
int socket = -1;
char *group_ip = "224.0.0.251";
uint16_t port = 5353;
// Set NETIF_FLAG_IGMP flag for netif which should process IGMP messages
xnetif[0].flags |= NETIF_FLAG_IGMP;
if((socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("ERROR: socket - AF_INET, SOCK_DGRAM\n");
err = -1;
}
// Add multicast group membership on this interface
if(err == 0) {
struct ip_mreq imr;
imr.imr_multiaddr.s_addr = inet_addr(group_ip);
imr.imr_interface.s_addr = INADDR_ANY;
err = setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(imr));
if(err < 0) printf("ERROR: setsockopt - IP_ADD_MEMBERSHIP\n");
}
// Specify outgoing interface too
if(err == 0) {
struct in_addr intfAddr;
intfAddr.s_addr = INADDR_ANY;
err = setsockopt(socket, IPPROTO_IP, IP_MULTICAST_IF, &intfAddr, sizeof(struct in_addr));
if(err < 0) printf("ERROR: setsockopt - IP_MULTICAST_IF\n");
}
// And start listening for packets
if(err == 0) {
struct sockaddr_in bindAddr;
bindAddr.sin_family = AF_INET;
bindAddr.sin_port = htons(port);
bindAddr.sin_addr.s_addr = INADDR_ANY;
err = bind(socket, (struct sockaddr *) &bindAddr, sizeof(bindAddr));
if(err < 0) printf("ERROR: bind\n");
}
if(err == 0) {
unsigned char packet[1024];
while(1) {
// Receive multicast
int packetLen;
struct sockaddr from;
struct sockaddr_in *from_sin = (struct sockaddr_in*) &from;
socklen_t fromLen = sizeof(from);
if((packetLen = recvfrom(socket, &packet, sizeof(packet), 0, &from, &fromLen)) >= 0) {
uint8_t *ip = (uint8_t *) &from_sin->sin_addr.s_addr;
uint16_t from_port = ntohs(from_sin->sin_port);
printf("recvfrom - %d bytes from %d.%d.%d.%d:%d\n", packetLen, ip[0], ip[1], ip[2], ip[3], from_port);
}
// Send multicast
if(packetLen > 0) {
int sendLen;
struct sockaddr to;
struct sockaddr_in *to_sin = (struct sockaddr_in*) &to;
to_sin->sin_family = AF_INET;
to_sin->sin_port = htons(port);
to_sin->sin_addr.s_addr = inet_addr(group_ip);
if((sendLen = sendto(socket, packet, packetLen, 0, &to, sizeof(struct sockaddr))) < 0)
printf("ERROR: sendto %s\n", group_ip);
else
printf("sendto - %d bytes to %s:%d\n", sendLen, group_ip, port);
}
}
}
else if(socket != -1) {
close(socket);
}
#else
printf("\nSHOULD ENABLE LWIP_IGMP\n");
#endif
vTaskDelete(NULL);
}
void example_mcast(void)
{
if(xTaskCreate(example_mcast_thread, ((const char*)"example_mcast_thread"), 2048, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("\n\r%s xTaskCreate(init_thread) failed", __FUNCTION__);
}

View file

@ -0,0 +1,6 @@
#ifndef EXAMPLE_MCAST_H
#define EXAMPLE_MCAST_H
void example_mcast(void);
#endif /* EXAMPLE_MCAST_H */

View file

@ -0,0 +1,17 @@
LWIP MULTICAST EXAMPLE
Description:
Join multicast group of 224.0.0.251 and listen on port 5353.
Send packet with the content of received packet to multicast group of 224.0.0.251.
Configuration:
[lwipopts.h]
#define LWIP_UDP 1
#define LWIP_IGMP 1
[platform_opts.h]
#define CONFIG_EXAMPLE_MCAST 1
Execution:
Can make automatical Wi-Fi connection when booting by using wlan fast connect example.
A multicast example thread will be started automatically when booting.

View file

@ -0,0 +1,55 @@
#include "FreeRTOS.h"
#include "task.h"
#include <platform/platform_stdlib.h>
#include <mDNS/mDNS.h>
#include <lwip_netconf.h>
#include <lwip/netif.h>
extern struct netif xnetif[];
static void example_mdns_thread(void *param)
{
DNSServiceRef dnsServiceRef = NULL;
TXTRecordRef txtRecord;
unsigned char txt_buf[100]; // use fixed buffer for text record to prevent malloc/free
// Delay to wait for IP by DHCP
vTaskDelay(10000);
printf("\nmDNS Init\n");
if(mDNSResponderInit() == 0) {
printf("mDNS Register service\n");
TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
TXTRecordSetValue(&txtRecord, "text1", strlen("text1_content"), "text1_content");
TXTRecordSetValue(&txtRecord, "text2", strlen("text2_content"), "text2_content");
dnsServiceRef = mDNSRegisterService("ameba", "_service1._tcp", "local", 5000, &txtRecord);
TXTRecordDeallocate(&txtRecord);
printf("wait for 30s ... \n");
vTaskDelay(30*1000);
printf("mDNS Update service\n");
TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf);
TXTRecordSetValue(&txtRecord, "text1", strlen("text1_content_new"), "text1_content_new");
mDNSUpdateService(dnsServiceRef, &txtRecord);
TXTRecordDeallocate(&txtRecord);
printf("wait for 30s ... \n");
vTaskDelay(30*1000);
if(dnsServiceRef)
mDNSDeregisterService(dnsServiceRef);
// deregister service before mdns deinit is not necessary
// mDNS deinit will also deregister all services
printf("mDNS Deinit\n");
mDNSResponderDeinit();
}
vTaskDelete(NULL);
}
void example_mdns(void)
{
if(xTaskCreate(example_mdns_thread, ((const char*)"example_mdns_thread"), 1024, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("\n\r%s xTaskCreate(init_thread) failed", __FUNCTION__);
}

View file

@ -0,0 +1,6 @@
#ifndef EXAMPLE_MDNS_H
#define EXAMPLE_MDNS_H
void example_mdns(void);
#endif /* EXAMPLE_MDNS_H */

View file

@ -0,0 +1,125 @@
/******************************************************************************
*
* Copyright(c) 2007 - 2015 Realtek Corporation. All rights reserved.
*
*
******************************************************************************/
/** @file
This example demonstrate how to implement wifi fast reconnection
**/
#include <platform_opts.h>
#include <wlan_fast_connect/example_wlan_fast_connect.h>
#include "task.h"
#include <platform/platform_stdlib.h>
#include <wifi/wifi_conf.h>
#include "flash_api.h"
write_reconnect_ptr p_write_reconnect_ptr;
extern void fATW0(void *arg);
extern void fATW1(void *arg);
extern void fATW2(void *arg);
extern void fATWC(void *arg);
/*
* Usage:
* wifi connection indication trigger this function to save current
* wifi profile in flash
*
* Condition:
* CONFIG_EXAMPLE_WLAN_FAST_CONNECT flag is set
*/
int wlan_wrtie_reconnect_data_to_flash(uint8_t *data, uint32_t len)
{
flash_t flash;
uint8_t read_data [FAST_RECONNECT_DATA_LEN];
if(!data)
return -1;
flash_stream_read(&flash, FAST_RECONNECT_DATA, FAST_RECONNECT_DATA_LEN, (uint8_t *) read_data);
//wirte it to flash if different content: SSID, Passphrase, Channel
if(memcmp((void *)data, (void *)read_data, NDIS_802_11_LENGTH_SSID) ||
memcmp((void *)(data + NDIS_802_11_LENGTH_SSID + 4), (void *)(read_data + NDIS_802_11_LENGTH_SSID + 4), 32) ||
memcmp((void *)(data + NDIS_802_11_LENGTH_SSID + 4 + 32 + A_SHA_DIGEST_LEN * 2), (void *)(read_data + NDIS_802_11_LENGTH_SSID + 4 + 32 + A_SHA_DIGEST_LEN * 2), 4)){
printf("\r\n %s():not the same ssid/passphrase/channel, need to flash it", __func__);
flash_erase_sector(&flash, FAST_RECONNECT_DATA);
flash_stream_write(&flash, FAST_RECONNECT_DATA, len, (uint8_t *) data);
}
return 0;
}
/*
* Usage:
* After wifi init done, waln driver call this function to check whether
* auto-connect is required.
*
* This function read previous saved wlan profile in flash and execute connection.
*
* Condition:
* CONFIG_EXAMPLE_WLAN_FAST_CONNECT flag is set
*/
int wlan_init_done_callback()
{
flash_t flash;
uint8_t *data;
uint32_t channel;
uint8_t pscan_config;
char key_id;
data = (uint8_t *)rtw_zmalloc(FAST_RECONNECT_DATA_LEN);
flash_stream_read(&flash, FAST_RECONNECT_DATA, FAST_RECONNECT_DATA_LEN, (uint8_t *)data);
if(*((uint32_t *) data) != ~0x0){
memcpy(psk_essid, (uint8_t *)data, NDIS_802_11_LENGTH_SSID + 4);
memcpy(psk_passphrase, (uint8_t *)data + NDIS_802_11_LENGTH_SSID + 4, (IW_PASSPHRASE_MAX_SIZE + 1));
memcpy(wpa_global_PSK, (uint8_t *)data + NDIS_802_11_LENGTH_SSID + 4 + (IW_PASSPHRASE_MAX_SIZE + 1), A_SHA_DIGEST_LEN * 2);
memcpy(&channel, (uint8_t *)data + NDIS_802_11_LENGTH_SSID + 4 + (IW_PASSPHRASE_MAX_SIZE + 1) + A_SHA_DIGEST_LEN * 2, 4);
sprintf(&key_id,"%d",(channel >> 28));
channel &= 0xff;
pscan_config = PSCAN_ENABLE | PSCAN_FAST_SURVEY;
//set partial scan for entering to listen beacon quickly
wifi_set_pscan_chan((uint8_t *)&channel, &pscan_config, 1);
#ifdef CONFIG_AUTO_RECONNECT
wifi_set_autoreconnect(1);
#endif
//set wifi connect
//open mode
if(!strlen((char*)psk_passphrase)){
fATW0((char*)psk_essid);
}
//wep mode
else if( strlen((char*)psk_passphrase) == 5 || strlen((char*)psk_passphrase) == 13){
fATW0((char*)psk_essid);
fATW1((char*)psk_passphrase);
fATW2(&key_id);
}
//WPA/WPA2
else{
fATW0((char*)psk_essid);
fATW1((char*)psk_passphrase);
}
fATWC(NULL);
}
if(data)
rtw_mfree(data);
return 0;
}
void example_wlan_fast_connect()
{
// 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_wrtie_reconnect_data_to_flash;
}

View file

@ -0,0 +1,35 @@
#ifndef __EXAMPLE_FAST_RECONNECTION_H__
#define __EXAMPLE_FAST_RECONNECTION_H__
/******************************************************************************
*
* Copyright(c) 2007 - 2015 Realtek Corporation. All rights reserved.
*
*
******************************************************************************/
#include "FreeRTOS.h"
#include <autoconf.h>
#define IW_PASSPHRASE_MAX_SIZE 64
#define FAST_RECONNECT_DATA (0xC0000 - 0x1000)
#define NDIS_802_11_LENGTH_SSID 32
#define A_SHA_DIGEST_LEN 20
#define FAST_RECONNECT_DATA_LEN (NDIS_802_11_LENGTH_SSID+4 +(IW_PASSPHRASE_MAX_SIZE + 1)+ A_SHA_DIGEST_LEN * 2 + 4)
typedef int (*wlan_init_done_ptr)(void);
typedef int (*write_reconnect_ptr)(uint8_t *data, uint32_t len);
//Variable
extern unsigned char psk_essid[NET_IF_NUM][NDIS_802_11_LENGTH_SSID+4];
extern unsigned char psk_passphrase[NET_IF_NUM][IW_PASSPHRASE_MAX_SIZE + 1];
extern unsigned char wpa_global_PSK[NET_IF_NUM][A_SHA_DIGEST_LEN * 2];
extern unsigned char psk_passphrase64[IW_PASSPHRASE_MAX_SIZE + 1];
//Function
extern wlan_init_done_ptr p_wlan_init_done_callback;
extern write_reconnect_ptr p_write_reconnect_ptr;
void example_wlan_fast_connect(void);
#endif //#ifndef __EXAMPLE_FAST_RECONNECTION_H__