Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
a33e3a4a
Commit
a33e3a4a
authored
Oct 17, 2015
by
Vowstar
Browse files
Merge pull request #687 from DiUS/dev140
Major upgrade to SDK 1.4.0 & open LWIP
parents
093a8959
8fba0f47
Changes
282
Hide whitespace changes
Inline
Side-by-side
app
/include/ssl/cert.h
→
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo
/include/ssl/cert.h
View file @
a33e3a4a
File moved
app
/include/ssl/private_key.h
→
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo
/include/ssl/private_key.h
View file @
a33e3a4a
File moved
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_config.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_CONFIG_H__
#define __USER_CONFIG_H__
#define ESP_PLATFORM 1
#define LEWEI_PLATFORM 0
#define USE_OPTIMIZE_PRINTF
#if ESP_PLATFORM
#define PLUG_DEVICE 0
#define LIGHT_DEVICE 1
#define SENSOR_DEVICE 0
#if SENSOR_DEVICE
#define HUMITURE_SUB_DEVICE 1
#define FLAMMABLE_GAS_SUB_DEVICE 0
#endif
//#define SERVER_SSL_ENABLE
//#define CLIENT_SSL_ENABLE
//#define UPGRADE_SSL_ENABLE
#define USE_DNS
#ifdef USE_DNS
#define ESP_DOMAIN "iot.espressif.cn"
#endif
//#define SOFTAP_ENCRYPT
#ifdef SOFTAP_ENCRYPT
#define PASSWORD "v*%W>L<@i&Nxe!"
#endif
#if SENSOR_DEVICE
#define SENSOR_DEEP_SLEEP
#if HUMITURE_SUB_DEVICE
#define SENSOR_DEEP_SLEEP_TIME 30000000
#elif FLAMMABLE_GAS_SUB_DEVICE
#define SENSOR_DEEP_SLEEP_TIME 60000000
#endif
#endif
#if LIGHT_DEVICE
#define USE_US_TIMER
#endif
#if PLUG_DEVICE || LIGHT_DEVICE
#define BEACON_TIMEOUT 150000000
#define BEACON_TIME 50000
#endif
#define AP_CACHE 1
#if AP_CACHE
#define AP_CACHE_NUMBER 5
#endif
#elif LEWEI_PLATFORM
#endif
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_devicefind.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_DEVICEFIND_H__
#define __USER_DEVICEFIND_H__
void
user_devicefind_init
(
void
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_esp_platform.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_DEVICE_H__
#define __USER_DEVICE_H__
/* NOTICE---this is for 512KB spi flash.
* you can change to other sector if you use other size spi flash. */
#define ESP_PARAM_START_SEC 0x3D
#define packet_size (2 * 1024)
#define token_size 41
struct
esp_platform_saved_param
{
uint8
devkey
[
40
];
uint8
token
[
40
];
uint8
activeflag
;
uint8
pad
[
3
];
};
enum
{
DEVICE_CONNECTING
=
40
,
DEVICE_ACTIVE_DONE
,
DEVICE_ACTIVE_FAIL
,
DEVICE_CONNECT_SERVER_FAIL
};
struct
dhcp_client_info
{
ip_addr_t
ip_addr
;
ip_addr_t
netmask
;
ip_addr_t
gw
;
uint8
flag
;
uint8
pad
[
3
];
};
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_esp_platform_timer.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_DEVICEFIND_H__
#define __USER_DEVICEFIND_H__
void
user_platform_timer_start
(
char
*
pbuffer
,
struct
espconn
*
pespconn
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_iot_version.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_IOT_VERSION_H__
#define __USER_IOT_VERSION_H__
#include "user_config.h"
#define IOT_VERSION_MAJOR 1U
#define IOT_VERSION_MINOR 0U
#define IOT_VERSION_REVISION 5U
#define VERSION_NUM (IOT_VERSION_MAJOR * 1000 + IOT_VERSION_MINOR * 100 + IOT_VERSION_REVISION)
//#define VERSION_TYPE "b"
#define VERSION_TYPE "v"
#if LIGHT_DEVICE
#define device_type 45772
#elif PLUG_DEVICE
#define device_type 23701
#elif SENSOR_DEVICE
#define device_type 12335
#endif
#define ONLINE_UPGRADE 0
#define LOCAL_UPGRADE 0
#define ALL_UPGRADE 1
#define NONE_UPGRADE 0
#if ONLINE_UPGRADE
#define UPGRADE_FALG "O"
#elif LOCAL_UPGRADE
#define UPGRADE_FALG "l"
#elif ALL_UPGRADE
#define UPGRADE_FALG "a"
#elif NONE_UPGRADE
#define UPGRADE_FALG "n"
#endif
#define IOT_VERSION
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_json.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_JSON_H__
#define __USER_JSON_H__
#include "json/jsonparse.h"
#include "json/jsontree.h"
#define jsonSize 2*1024
void
json_parse
(
struct
jsontree_context
*
json
,
char
*
ptrJSONMessage
);
void
json_ws_send
(
struct
jsontree_value
*
tree
,
const
char
*
path
,
char
*
pbuf
);
int
json_putchar
(
int
c
);
struct
jsontree_value
*
find_json_path
(
struct
jsontree_context
*
json
,
const
char
*
path
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_light.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_LIGHT_H__
#define __USER_LIGHT_H__
/*pwm.h: function and macro definition of PWM API , driver level */
/*user_light.h: user interface for light API, user level*/
/*user_light_adj: API for color changing and lighting effects, user level*/
#include "pwm.h"
/* NOTICE !!! ---this is for 512KB spi flash.*/
/* You can change to other sector if you use other size spi flash. */
/* Refer to the documentation about OTA support and flash mapping*/
#define PRIV_PARAM_START_SEC 0x3C
#define PRIV_PARAM_SAVE 0
/*Define the channel number of PWM*/
/*In this demo, we can set 3 for 3 PWM channels: RED, GREEN, BLUE*/
/*Or , we can choose 5 channels : RED,GREEN,BLUE,COLD-WHITE,WARM-WHITE*/
#define PWM_CHANNEL 5 // 5:5channel ; 3:3channel
#define LIGHT_RED 0
#define LIGHT_GREEN 1
#define LIGHT_BLUE 2
#define LIGHT_COLD_WHITE 3
#define LIGHT_WARM_WHITE 4
/*Definition of GPIO PIN params, for GPIO initialization*/
#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_0_OUT_IO_NUM 12
#define PWM_0_OUT_IO_FUNC FUNC_GPIO12
#define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_1_OUT_IO_NUM 15
#define PWM_1_OUT_IO_FUNC FUNC_GPIO15
#define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_2_OUT_IO_NUM 13
#define PWM_2_OUT_IO_FUNC FUNC_GPIO13
#define PWM_3_OUT_IO_MUX PERIPHS_IO_MUX_MTMS_U
#define PWM_3_OUT_IO_NUM 14
#define PWM_3_OUT_IO_FUNC FUNC_GPIO14
#define PWM_4_OUT_IO_MUX PERIPHS_IO_MUX_GPIO5_U
#define PWM_4_OUT_IO_NUM 5
#define PWM_4_OUT_IO_FUNC FUNC_GPIO5
struct
light_saved_param
{
uint32
pwm_period
;
uint32
pwm_duty
[
PWM_CHANNEL
];
};
void
user_light_init
(
void
);
uint32
user_light_get_duty
(
uint8
channel
);
void
user_light_set_duty
(
uint32
duty
,
uint8
channel
);
uint32
user_light_get_period
(
void
);
void
user_light_set_period
(
uint32
period
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_light_adj.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_LIGHT_ADJ_H__
#define __USER_LIGHT_ADJ_H__
/*pwm.h: function and macro definition of PWM API , driver level */
/*user_light.h: user interface for light setting, user level*/
/*user_light_adj: API for color changing and lighting effects, user level*/
/*save RGB params to flash when calling light_set_aim*/
#define SAVE_LIGHT_PARAM 0 //set to 0: do not save color params
/*check current consumption and limit the total current for LED driver IC*/
/*NOTE: YOU SHOULD REPLACE WIHT THE LIMIT CURRENT OF YOUR OWN APPLICATION*/
#define LIGHT_CURRENT_LIMIT 0 //set to 0: do not limit total current
#if LIGHT_CURRENT_LIMIT
#define LIGHT_TOTAL_CURRENT_MAX (450*1000) //450000/1000 MA AT MOST
#define LIGHT_CURRENT_MARGIN (80*1000) //80000/1000 MA CURRENT RAISES WHILE TEMPERATURE INCREASING
#define LIGHT_CURRENT_MARGIN_L2 (110*1000) //110000/1000 MA
#define LIGHT_CURRENT_MARGIN_L3 (140*1000) //140000/1000 MA
#endif
/*set target duty for PWM channels, change each channel duty gradually */
void
light_set_aim
(
uint32
r
,
uint32
g
,
uint32
b
,
uint32
cw
,
uint32
ww
,
uint32
period
);
//'white' channel is not used in default demo
void
light_set_aim_r
(
uint32
r
);
void
light_set_aim_g
(
uint32
g
);
void
light_set_aim_b
(
uint32
b
);
void
light_set_aim_cw
(
uint32
cw
);
void
light_set_aim_ww
(
uint32
ww
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_plug.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_ESPSWITCH_H__
#define __USER_ESPSWITCH_H__
#include "driver/key.h"
/* NOTICE---this is for 512KB spi flash.
* you can change to other sector if you use other size spi flash. */
#define PRIV_PARAM_START_SEC 0x3C
#define PRIV_PARAM_SAVE 0
#define PLUG_KEY_NUM 1
#define PLUG_KEY_0_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PLUG_KEY_0_IO_NUM 13
#define PLUG_KEY_0_IO_FUNC FUNC_GPIO13
#define PLUG_WIFI_LED_IO_MUX PERIPHS_IO_MUX_GPIO0_U
#define PLUG_WIFI_LED_IO_NUM 0
#define PLUG_WIFI_LED_IO_FUNC FUNC_GPIO0
#define PLUG_LINK_LED_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PLUG_LINK_LED_IO_NUM 12
#define PLUG_LINK_LED_IO_FUNC FUNC_GPIO12
#define PLUG_RELAY_LED_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PLUG_RELAY_LED_IO_NUM 15
#define PLUG_RELAY_LED_IO_FUNC FUNC_GPIO15
#define PLUG_STATUS_OUTPUT(pin, on) GPIO_OUTPUT_SET(pin, on)
struct
plug_saved_param
{
uint8_t
status
;
uint8_t
pad
[
3
];
};
void
user_plug_init
(
void
);
uint8
user_plug_get_status
(
void
);
void
user_plug_set_status
(
bool
status
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_sensor.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_SENSOR_H__
#define __USER_SENSOR_H__
#include "user_config.h"
#include "driver/key.h"
#define SENSOR_KEY_NUM 1
#define SENSOR_KEY_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define SENSOR_KEY_IO_NUM 13
#define SENSOR_KEY_IO_FUNC FUNC_GPIO13
#define SENSOR_WIFI_LED_IO_MUX PERIPHS_IO_MUX_GPIO0_U
#define SENSOR_WIFI_LED_IO_NUM 0
#define SENSOR_WIFI_LED_IO_FUNC FUNC_GPIO0
#define SENSOR_LINK_LED_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define SENSOR_LINK_LED_IO_NUM 12
#define SENSOR_LINK_LED_IO_FUNC FUNC_GPIO12
#define SENSOR_UNUSED_LED_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define SENSOR_UNUSED_LED_IO_NUM 15
#define SENSOR_UNUSED_LED_IO_FUNC FUNC_GPIO15
#if HUMITURE_SUB_DEVICE
bool
user_mvh3004_read_th
(
uint8
*
data
);
void
user_mvh3004_init
(
void
);
#endif
void
user_sensor_init
(
uint8
active
);
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/include/user_webserver.h
0 → 100644
View file @
a33e3a4a
#ifndef __USER_WEBSERVER_H__
#define __USER_WEBSERVER_H__
#define SERVER_PORT 80
#define SERVER_SSL_PORT 443
#define URLSize 10
typedef
enum
Result_Resp
{
RespFail
=
0
,
RespSuc
,
}
Result_Resp
;
typedef
enum
ProtocolType
{
GET
=
0
,
POST
,
}
ProtocolType
;
typedef
enum
_ParmType
{
SWITCH_STATUS
=
0
,
INFOMATION
,
WIFI
,
SCAN
,
REBOOT
,
DEEP_SLEEP
,
LIGHT_STATUS
,
CONNECT_STATUS
,
USER_BIN
}
ParmType
;
typedef
struct
URL_Frame
{
enum
ProtocolType
Type
;
char
pSelect
[
URLSize
];
char
pCommand
[
URLSize
];
char
pFilename
[
URLSize
];
}
URL_Frame
;
typedef
struct
_rst_parm
{
ParmType
parmtype
;
struct
espconn
*
pespconn
;
}
rst_parm
;
void
user_webserver_init
(
uint32
port
);
#endif
app/ssl
/Makefile
→
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user
/Makefile
View file @
a33e3a4a
#############################################################
#############################################################
# Required variables for each makefile
# Required variables for each makefile
# Discard this section from all parent makefiles
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
# a generated lib/image xxx.a ()
#
#
ifndef
PDIR
ifndef
PDIR
UP_EXTRACT_DIR
=
..
GEN_LIBS
=
libuser.a
GEN_LIBS
=
libssl.a
endif
COMPONENTS_libssl
=
crypto/libsslcrypto.a
\
ssl/libsslssl.a
\
app/libsslapp.a
#############################################################
endif
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
#############################################################
# makefile at its root level - these are then overridden
# Configuration i.e. compile options etc.
# for a subtree within the makefile rooted therein
# Target specific stuff (defines etc.) goes in here!
#
# Generally values applying to a tree are captured in the
#DEFINES +=
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#############################################################
#
# Recursion Magic - Don't touch this!!
#DEFINES +=
#
# Each subtree potentially has an include directory
#############################################################
# corresponding to the common APIs applicable to modules
# Recursion Magic - Don't touch this!!
# rooted at that subtree. Accordingly, the INCLUDE PATH
#
# of a module can only contain the include directories up
# Each subtree potentially has an include directory
# its parent path, and not its siblings
# corresponding to the common APIs applicable to modules
#
# rooted at that subtree. Accordingly, the INCLUDE PATH
# Required for each makefile to inherit from the parent
# of a module can only contain the include directories up
#
# its parent path, and not its siblings
#
INCLUDES
:=
$(INCLUDES)
-I
$(PDIR)
include
# Required for each makefile to inherit from the parent
INCLUDES
+=
-I
./
#
INCLUDES
+=
-I
../../include/ets
PDIR
:=
../
$(PDIR)
INCLUDES
:=
$(INCLUDES)
-I
$(PDIR)
include
sinclude
$(PDIR)Makefile
INCLUDES
+=
-I
./
PDIR
:=
../
$(PDIR)
sinclude
$(PDIR)Makefile
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user/user_devicefind.c
0 → 100644
View file @
a33e3a4a
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_devicefind.c
*
* Description: Find your hardware's information while working any mode.
*
* Modification history:
* 2014/3/12, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "espconn.h"
#include "user_json.h"
#include "user_devicefind.h"
const
char
*
device_find_request
=
"Are You Espressif IOT Smart Device?"
;
#if PLUG_DEVICE
const
char
*
device_find_response_ok
=
"I'm Plug."
;
#elif LIGHT_DEVICE
const
char
*
device_find_response_ok
=
"I'm Light."
;
#elif SENSOR_DEVICE
#if HUMITURE_SUB_DEVICE
const
char
*
device_find_response_ok
=
"I'm Humiture."
;
#elif FLAMMABLE_GAS_SUB_DEVICE
const
char
*
device_find_response_ok
=
"I'm Flammable Gas."
;
#endif
#endif
/*---------------------------------------------------------------------------*/
LOCAL
struct
espconn
ptrespconn
;
/******************************************************************************
* FunctionName : user_devicefind_recv
* Description : Processing the received data from the host
* Parameters : arg -- Additional argument to pass to the callback function
* pusrdata -- The received data (or NULL when the connection has been closed!)
* length -- The length of received data
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_devicefind_recv
(
void
*
arg
,
char
*
pusrdata
,
unsigned
short
length
)
{
char
DeviceBuffer
[
40
]
=
{
0
};
char
Device_mac_buffer
[
60
]
=
{
0
};
char
hwaddr
[
6
];
remot_info
*
premot
=
NULL
;
struct
ip_info
ipconfig
;
if
(
wifi_get_opmode
()
!=
STATION_MODE
)
{
wifi_get_ip_info
(
SOFTAP_IF
,
&
ipconfig
);
wifi_get_macaddr
(
SOFTAP_IF
,
hwaddr
);
if
(
!
ip_addr_netcmp
((
struct
ip_addr
*
)
ptrespconn
.
proto
.
udp
->
remote_ip
,
&
ipconfig
.
ip
,
&
ipconfig
.
netmask
))
{
wifi_get_ip_info
(
STATION_IF
,
&
ipconfig
);
wifi_get_macaddr
(
STATION_IF
,
hwaddr
);
}
}
else
{
wifi_get_ip_info
(
STATION_IF
,
&
ipconfig
);
wifi_get_macaddr
(
STATION_IF
,
hwaddr
);
}
if
(
pusrdata
==
NULL
)
{
return
;
}
if
(
length
==
os_strlen
(
device_find_request
)
&&
os_strncmp
(
pusrdata
,
device_find_request
,
os_strlen
(
device_find_request
))
==
0
)
{
os_sprintf
(
DeviceBuffer
,
"%s"
MACSTR
" "
IPSTR
,
device_find_response_ok
,
MAC2STR
(
hwaddr
),
IP2STR
(
&
ipconfig
.
ip
));
os_printf
(
"%s
\n
"
,
DeviceBuffer
);
length
=
os_strlen
(
DeviceBuffer
);
if
(
espconn_get_connection_info
(
&
ptrespconn
,
&
premot
,
0
)
!=
ESPCONN_OK
)
return
;
os_memcpy
(
ptrespconn
.
proto
.
udp
->
remote_ip
,
premot
->
remote_ip
,
4
);
ptrespconn
.
proto
.
udp
->
remote_port
=
premot
->
remote_port
;
espconn_sent
(
&
ptrespconn
,
DeviceBuffer
,
length
);
}
else
if
(
length
==
(
os_strlen
(
device_find_request
)
+
18
))
{
os_sprintf
(
Device_mac_buffer
,
"%s "
MACSTR
,
device_find_request
,
MAC2STR
(
hwaddr
));
os_printf
(
"%s"
,
Device_mac_buffer
);
if
(
os_strncmp
(
Device_mac_buffer
,
pusrdata
,
os_strlen
(
device_find_request
)
+
18
)
==
0
)
{
//os_printf("%s\n", Device_mac_buffer);
length
=
os_strlen
(
DeviceBuffer
);
os_sprintf
(
DeviceBuffer
,
"%s"
MACSTR
" "
IPSTR
,
device_find_response_ok
,
MAC2STR
(
hwaddr
),
IP2STR
(
&
ipconfig
.
ip
));
os_printf
(
"%s
\n
"
,
DeviceBuffer
);
length
=
os_strlen
(
DeviceBuffer
);
if
(
espconn_get_connection_info
(
&
ptrespconn
,
&
premot
,
0
)
!=
ESPCONN_OK
)
return
;
os_memcpy
(
ptrespconn
.
proto
.
udp
->
remote_ip
,
premot
->
remote_ip
,
4
);
ptrespconn
.
proto
.
udp
->
remote_port
=
premot
->
remote_port
;
espconn_sent
(
&
ptrespconn
,
DeviceBuffer
,
length
);
}
else
{
return
;
}
}
}
/******************************************************************************
* FunctionName : user_devicefind_init
* Description : the espconn struct parame init
* Parameters : none
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_devicefind_init
(
void
)
{
ptrespconn
.
type
=
ESPCONN_UDP
;
ptrespconn
.
proto
.
udp
=
(
esp_udp
*
)
os_zalloc
(
sizeof
(
esp_udp
));
ptrespconn
.
proto
.
udp
->
local_port
=
1025
;
espconn_regist_recvcb
(
&
ptrespconn
,
user_devicefind_recv
);
espconn_create
(
&
ptrespconn
);
}
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user/user_esp_platform.c
0 → 100644
View file @
a33e3a4a
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_esp_platform.c
*
* Description: The client mode configration.
* Check your hardware connection with the host while use this mode.
*
* Modification history:
* 2014/5/09, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "os_type.h"
#include "mem.h"
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "user_esp_platform.h"
#include "user_iot_version.h"
#include "upgrade.h"
#if ESP_PLATFORM
#define ESP_DEBUG
#ifdef ESP_DEBUG
#define ESP_DBG os_printf
#else
#define ESP_DBG
#endif
#define ACTIVE_FRAME "{\"nonce\": %d,\"path\": \"/v1/device/activate/\", \"method\": \"POST\", \"body\": {\"encrypt_method\": \"PLAIN\", \"token\": \"%s\", \"bssid\": \""MACSTR"\",\"rom_version\":\"%s\"}, \"meta\": {\"Authorization\": \"token %s\"}}\n"
#if PLUG_DEVICE
#include "user_plug.h"
#define RESPONSE_FRAME "{\"status\": 200, \"datapoint\": {\"x\": %d}, \"nonce\": %d, \"deliver_to_device\": true}\n"
#define FIRST_FRAME "{\"nonce\": %d, \"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
#elif LIGHT_DEVICE
#include "user_light.h"
#define RESPONSE_FRAME "{\"status\": 200,\"nonce\": %d, \"datapoint\": {\"x\": %d,\"y\": %d,\"z\": %d,\"k\": %d,\"l\": %d},\"deliver_to_device\":true}\n"
#define FIRST_FRAME "{\"nonce\": %d, \"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
#elif SENSOR_DEVICE
#include "user_sensor.h"
#if HUMITURE_SUB_DEVICE
#define UPLOAD_FRAME "{\"nonce\": %d, \"path\": \"/v1/datastreams/tem_hum/datapoint/\", \"method\": \"POST\", \
\
"body
\"
: {
\"
datapoint
\"
: {
\"
x
\"
: %s%d.%02d,
\"
y
\"
: %d.%02d}},
\"
meta
\"
: {
\"
Authorization
\"
:
\"
token %s
\"
}}
\n
"
#elif FLAMMABLE_GAS_SUB_DEVICE
#define UPLOAD_FRAME "{\"nonce\": %d, \"path\": \"/v1/datastreams/flammable_gas/datapoint/\", \"method\": \"POST\", \
\
"body
\"
: {
\"
datapoint
\"
: {
\"
x
\"
: %d.%03d}},
\"
meta
\"
: {
\"
Authorization
\"
:
\"
token %s
\"
}}
\n
"
#endif
LOCAL
uint32
count
=
0
;
#endif
#define UPGRADE_FRAME "{\"path\": \"/v1/messages/\", \"method\": \"POST\", \"meta\": {\"Authorization\": \"token %s\"},\
\
"get
\"
:{
\"
action
\"
:
\"
%s
\"
},
\"
body
\"
:{
\"
pre_rom_version
\"
:
\"
%s
\"
,
\"
rom_version
\"
:
\"
%s
\"
}}
\n
"
#if PLUG_DEVICE || LIGHT_DEVICE
#define BEACON_FRAME "{\"path\": \"/v1/ping/\", \"method\": \"POST\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
#define RPC_RESPONSE_FRAME "{\"status\": 200, \"nonce\": %d, \"deliver_to_device\": true}\n"
#define TIMER_FRAME "{\"body\": {}, \"get\":{\"is_humanize_format_simple\":\"true\"},\"meta\": {\"Authorization\": \"Token %s\"},\"path\": \"/v1/device/timers/\",\"post\":{},\"method\": \"GET\"}\n"
#define pheadbuffer "Connection: keep-alive\r\n\
Cache
-
Control
:
no
-
cache
\
r
\
n
\
User
-
Agent
:
Mozilla
/
5
.
0
(
Windows
NT
5
.
1
)
AppleWebKit
/
537
.
36
(
KHTML
,
like
Gecko
)
Chrome
/
30
.
0
.
1599
.
101
Safari
/
537
.
36
\
r
\
n
\
Accept
:
*/
*
\
r
\
n
\
Authorization
:
token
%
s
\
r
\
n
\
Accept
-
Encoding
:
gzip
,
deflate
,
sdch
\
r
\
n
\
Accept
-
Language
:
zh
-
CN
,
zh
;
q
=
0
.
8
\
r
\
n
\
r
\
n
"
LOCAL uint8 ping_status;
LOCAL os_timer_t beacon_timer;
#endif
#ifdef USE_DNS
ip_addr_t esp_server_ip;
#endif
LOCAL struct espconn user_conn;
LOCAL struct _esp_tcp user_tcp;
LOCAL os_timer_t client_timer;
struct esp_platform_saved_param esp_param;
LOCAL uint8 device_status;
LOCAL uint8 device_recon_count = 0;
LOCAL uint32 active_nonce = 0;
LOCAL uint8 iot_version[20] = {0};
struct rst_info rtc_info;
void user_esp_platform_check_ip(uint8 reset_flag);
/******************************************************************************
* FunctionName : user_esp_platform_get_token
* Description : get the espressif's device token
* Parameters : token -- the parame point which write the flash
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_get_token(uint8_t *token)
{
if (token == NULL) {
return;
}
os_memcpy(token, esp_param.token, sizeof(esp_param.token));
}
/******************************************************************************
* FunctionName : user_esp_platform_set_token
* Description : save the token for the espressif's device
* Parameters : token -- the parame point which write the flash
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_set_token(uint8_t *token)
{
if (token == NULL) {
return;
}
esp_param.activeflag = 0;
os_memcpy(esp_param.token, token, os_strlen(token));
system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
}
/******************************************************************************
* FunctionName : user_esp_platform_set_active
* Description : set active flag
* Parameters : activeflag -- 0 or 1
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_set_active(uint8 activeflag)
{
esp_param.activeflag = activeflag;
system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
}
void ICACHE_FLASH_ATTR
user_esp_platform_set_connect_status(uint8 status)
{
device_status = status;
}
/******************************************************************************
* FunctionName : user_esp_platform_get_connect_status
* Description : get each connection step's status
* Parameters : none
* Returns : status
*******************************************************************************/
uint8 ICACHE_FLASH_ATTR
user_esp_platform_get_connect_status(void)
{
uint8 status = wifi_station_get_connect_status();
if (status == STATION_GOT_IP) {
status = (device_status == 0) ? DEVICE_CONNECTING : device_status;
}
ESP_DBG("
status
%
d
\
n
", status);
return status;
}
/******************************************************************************
* FunctionName : user_esp_platform_parse_nonce
* Description : parse the device nonce
* Parameters : pbuffer -- the recivce data point
* Returns : the nonce
*******************************************************************************/
int ICACHE_FLASH_ATTR
user_esp_platform_parse_nonce(char *pbuffer)
{
char *pstr = NULL;
char *pparse = NULL;
char noncestr[11] = {0};
int nonce = 0;
pstr = (char *)os_strstr(pbuffer, "
\
"nonce
\"
: "
);
if
(
pstr
!=
NULL
)
{
pstr
+=
9
;
pparse
=
(
char
*
)
os_strstr
(
pstr
,
","
);
if
(
pparse
!=
NULL
)
{
os_memcpy
(
noncestr
,
pstr
,
pparse
-
pstr
);
}
else
{
pparse
=
(
char
*
)
os_strstr
(
pstr
,
"}"
);
if
(
pparse
!=
NULL
)
{
os_memcpy
(
noncestr
,
pstr
,
pparse
-
pstr
);
}
else
{
pparse
=
(
char
*
)
os_strstr
(
pstr
,
"]"
);
if
(
pparse
!=
NULL
)
{
os_memcpy
(
noncestr
,
pstr
,
pparse
-
pstr
);
}
else
{
return
0
;
}
}
}
nonce
=
atoi
(
noncestr
);
}
return
nonce
;
}
/******************************************************************************
* FunctionName : user_esp_platform_get_info
* Description : get and update the espressif's device status
* Parameters : pespconn -- the espconn used to connect with host
* pbuffer -- prossing the data point
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_esp_platform_get_info
(
struct
espconn
*
pconn
,
uint8
*
pbuffer
)
{
char
*
pbuf
=
NULL
;
int
nonce
=
0
;
pbuf
=
(
char
*
)
os_zalloc
(
packet_size
);
nonce
=
user_esp_platform_parse_nonce
(
pbuffer
);
if
(
pbuf
!=
NULL
)
{
#if PLUG_DEVICE
os_sprintf
(
pbuf
,
RESPONSE_FRAME
,
user_plug_get_status
(),
nonce
);
#elif LIGHT_DEVICE
uint32
white_val
;
white_val
=
(
PWM_CHANNEL
>
LIGHT_COLD_WHITE
?
user_light_get_duty
(
LIGHT_COLD_WHITE
)
:
0
);
os_sprintf
(
pbuf
,
RESPONSE_FRAME
,
nonce
,
user_light_get_period
(),
user_light_get_duty
(
LIGHT_RED
),
user_light_get_duty
(
LIGHT_GREEN
),
user_light_get_duty
(
LIGHT_BLUE
),
white_val
);
//50);
#endif
ESP_DBG
(
"%s
\n
"
,
pbuf
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
os_free
(
pbuf
);
pbuf
=
NULL
;
}
}
/******************************************************************************
* FunctionName : user_esp_platform_set_info
* Description : prossing the data and controling the espressif's device
* Parameters : pespconn -- the espconn used to connect with host
* pbuffer -- prossing the data point
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_esp_platform_set_info
(
struct
espconn
*
pconn
,
uint8
*
pbuffer
)
{
#if PLUG_DEVICE
char
*
pstr
=
NULL
;
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"plug-status"
);
if
(
pstr
!=
NULL
)
{
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"body"
);
if
(
pstr
!=
NULL
)
{
if
(
os_strncmp
(
pstr
+
27
,
"1"
,
1
)
==
0
)
{
user_plug_set_status
(
0x01
);
}
else
if
(
os_strncmp
(
pstr
+
27
,
"0"
,
1
)
==
0
)
{
user_plug_set_status
(
0x00
);
}
}
}
#elif LIGHT_DEVICE
char
*
pstr
=
NULL
;
char
*
pdata
=
NULL
;
char
*
pbuf
=
NULL
;
char
recvbuf
[
10
];
uint16
length
=
0
;
uint32
data
=
0
;
static
uint32
rr
,
gg
,
bb
,
cw
,
ww
,
period
;
ww
=
0
;
cw
=
0
;
extern
uint8
light_sleep_flg
;
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
path
\"
:
\"
/v1/datastreams/light/datapoint/
\"
"
);
if
(
pstr
!=
NULL
)
{
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"{
\"
datapoint
\"
: "
);
if
(
pstr
!=
NULL
)
{
pbuf
=
(
char
*
)
os_strstr
(
pbuffer
,
"}}"
);
length
=
pbuf
-
pstr
;
length
+=
2
;
pdata
=
(
char
*
)
os_zalloc
(
length
+
1
);
os_memcpy
(
pdata
,
pstr
,
length
);
pstr
=
(
char
*
)
os_strchr
(
pdata
,
'x'
);
if
(
pstr
!=
NULL
)
{
pstr
+=
4
;
pbuf
=
(
char
*
)
os_strchr
(
pstr
,
','
);
if
(
pbuf
!=
NULL
)
{
length
=
pbuf
-
pstr
;
os_memset
(
recvbuf
,
0
,
10
);
os_memcpy
(
recvbuf
,
pstr
,
length
);
data
=
atoi
(
recvbuf
);
period
=
data
;
//user_light_set_period(data);
}
}
pstr
=
(
char
*
)
os_strchr
(
pdata
,
'y'
);
if
(
pstr
!=
NULL
)
{
pstr
+=
4
;
pbuf
=
(
char
*
)
os_strchr
(
pstr
,
','
);
if
(
pbuf
!=
NULL
)
{
length
=
pbuf
-
pstr
;
os_memset
(
recvbuf
,
0
,
10
);
os_memcpy
(
recvbuf
,
pstr
,
length
);
data
=
atoi
(
recvbuf
);
rr
=
data
;
os_printf
(
"r: %d
\r\n
"
,
rr
);
//user_light_set_duty(data, 0);
}
}
pstr
=
(
char
*
)
os_strchr
(
pdata
,
'z'
);
if
(
pstr
!=
NULL
)
{
pstr
+=
4
;
pbuf
=
(
char
*
)
os_strchr
(
pstr
,
','
);
if
(
pbuf
!=
NULL
)
{
length
=
pbuf
-
pstr
;
os_memset
(
recvbuf
,
0
,
10
);
os_memcpy
(
recvbuf
,
pstr
,
length
);
data
=
atoi
(
recvbuf
);
gg
=
data
;
os_printf
(
"g: %d
\r\n
"
,
gg
);
//user_light_set_duty(data, 1);
}
}
pstr
=
(
char
*
)
os_strchr
(
pdata
,
'k'
);
if
(
pstr
!=
NULL
)
{
pstr
+=
4
;;
pbuf
=
(
char
*
)
os_strchr
(
pstr
,
','
);
if
(
pbuf
!=
NULL
)
{
length
=
pbuf
-
pstr
;
os_memset
(
recvbuf
,
0
,
10
);
os_memcpy
(
recvbuf
,
pstr
,
length
);
data
=
atoi
(
recvbuf
);
bb
=
data
;
os_printf
(
"b: %d
\r\n
"
,
bb
);
//user_light_set_duty(data, 2);
}
}
pstr
=
(
char
*
)
os_strchr
(
pdata
,
'l'
);
if
(
pstr
!=
NULL
)
{
pstr
+=
4
;;
pbuf
=
(
char
*
)
os_strchr
(
pstr
,
','
);
if
(
pbuf
!=
NULL
)
{
length
=
pbuf
-
pstr
;
os_memset
(
recvbuf
,
0
,
10
);
os_memcpy
(
recvbuf
,
pstr
,
length
);
data
=
atoi
(
recvbuf
);
cw
=
data
;
ww
=
data
;
os_printf
(
"cw: %d
\r\n
"
,
cw
);
os_printf
(
"ww:%d
\r\n
"
,
ww
);
//chg
//user_light_set_duty(data, 2);
}
}
os_free
(
pdata
);
}
}
if
((
rr
|
gg
|
bb
|
cw
|
ww
)
==
0
){
if
(
light_sleep_flg
==
0
){
}
}
else
{
if
(
light_sleep_flg
==
1
){
os_printf
(
"modem sleep en
\r\n
"
);
wifi_set_sleep_type
(
MODEM_SLEEP_T
);
light_sleep_flg
=
0
;
}
}
light_set_aim
(
rr
,
gg
,
bb
,
cw
,
ww
,
period
);
//user_light_restart();
#endif
user_esp_platform_get_info
(
pconn
,
pbuffer
);
}
/******************************************************************************
* FunctionName : user_esp_platform_reconnect
* Description : reconnect with host after get ip
* Parameters : pespconn -- the espconn used to reconnect with host
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_reconnect
(
struct
espconn
*
pespconn
)
{
ESP_DBG
(
"user_esp_platform_reconnect
\n
"
);
user_esp_platform_check_ip
(
0
);
}
/******************************************************************************
* FunctionName : user_esp_platform_discon_cb
* Description : disconnect successfully with the host
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_discon_cb
(
void
*
arg
)
{
struct
espconn
*
pespconn
=
arg
;
struct
ip_info
ipconfig
;
struct
dhcp_client_info
dhcp_info
;
ESP_DBG
(
"user_esp_platform_discon_cb
\n
"
);
#if (PLUG_DEVICE || LIGHT_DEVICE)
os_timer_disarm
(
&
beacon_timer
);
#endif
if
(
pespconn
==
NULL
)
{
return
;
}
pespconn
->
proto
.
tcp
->
local_port
=
espconn_port
();
#if (PLUG_DEVICE || SENSOR_DEVICE)
user_link_led_output
(
1
);
#endif
#if SENSOR_DEVICE
#ifdef SENSOR_DEEP_SLEEP
if
(
wifi_get_opmode
()
==
STATION_MODE
)
{
/***add by tzx for saving ip_info to avoid dhcp_client start****/
wifi_get_ip_info
(
STATION_IF
,
&
ipconfig
);
dhcp_info
.
ip_addr
=
ipconfig
.
ip
;
dhcp_info
.
netmask
=
ipconfig
.
netmask
;
dhcp_info
.
gw
=
ipconfig
.
gw
;
dhcp_info
.
flag
=
0x01
;
os_printf
(
"dhcp_info.ip_addr = %d
\n
"
,
dhcp_info
.
ip_addr
);
system_rtc_mem_write
(
64
,
&
dhcp_info
,
sizeof
(
struct
dhcp_client_info
));
user_sensor_deep_sleep_enter
();
}
else
{
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_reconnect
,
pespconn
);
os_timer_arm
(
&
client_timer
,
SENSOR_DEEP_SLEEP_TIME
/
1000
,
0
);
}
#else
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_reconnect
,
pespconn
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
#endif
#else
user_esp_platform_reconnect
(
pespconn
);
#endif
}
/******************************************************************************
* FunctionName : user_esp_platform_discon
* Description : A new incoming connection has been disconnected.
* Parameters : espconn -- the espconn used to disconnect with host
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_discon
(
struct
espconn
*
pespconn
)
{
ESP_DBG
(
"user_esp_platform_discon
\n
"
);
#if (PLUG_DEVICE || SENSOR_DEVICE)
user_link_led_output
(
1
);
#endif
#ifdef CLIENT_SSL_ENABLE
espconn_secure_disconnect
(
pespconn
);
#else
espconn_disconnect
(
pespconn
);
#endif
}
/******************************************************************************
* FunctionName : user_esp_platform_sent_cb
* Description : Data has been sent successfully and acknowledged by the remote host.
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_sent_cb
(
void
*
arg
)
{
struct
espconn
*
pespconn
=
arg
;
ESP_DBG
(
"user_esp_platform_sent_cb
\n
"
);
}
/******************************************************************************
* FunctionName : user_esp_platform_sent
* Description : Processing the application data and sending it to the host
* Parameters : pespconn -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_sent
(
struct
espconn
*
pespconn
)
{
uint8
devkey
[
token_size
]
=
{
0
};
uint32
nonce
;
char
*
pbuf
=
(
char
*
)
os_zalloc
(
packet_size
);
os_memcpy
(
devkey
,
esp_param
.
devkey
,
40
);
if
(
esp_param
.
activeflag
==
0xFF
)
{
esp_param
.
activeflag
=
0
;
}
if
(
pbuf
!=
NULL
)
{
if
(
esp_param
.
activeflag
==
0
)
{
uint8
token
[
token_size
]
=
{
0
};
uint8
bssid
[
6
];
active_nonce
=
os_random
()
&
0x7FFFFFFF
;
os_memcpy
(
token
,
esp_param
.
token
,
40
);
wifi_get_macaddr
(
STATION_IF
,
bssid
);
os_sprintf
(
pbuf
,
ACTIVE_FRAME
,
active_nonce
,
token
,
MAC2STR
(
bssid
),
iot_version
,
devkey
);
}
#if SENSOR_DEVICE
#if HUMITURE_SUB_DEVICE
else
{
#if 0
uint16 tp, rh;
uint8 data[4];
if (user_mvh3004_read_th(data)) {
rh = data[0] << 8 | data[1];
tp = data[2] << 8 | data[3];
}
#else
uint16
tp
,
rh
;
uint8
*
data
;
uint32
tp_t
,
rh_t
;
data
=
(
uint8
*
)
user_mvh3004_get_poweron_th
();
rh
=
data
[
0
]
<<
8
|
data
[
1
];
tp
=
data
[
2
]
<<
8
|
data
[
3
];
#endif
tp_t
=
(
tp
>>
2
)
*
165
*
100
/
(
16384
-
1
);
rh_t
=
(
rh
&
0x3fff
)
*
100
*
100
/
(
16384
-
1
);
if
(
tp_t
>=
4000
)
{
os_sprintf
(
pbuf
,
UPLOAD_FRAME
,
count
,
""
,
tp_t
/
100
-
40
,
tp_t
%
100
,
rh_t
/
100
,
rh_t
%
100
,
devkey
);
}
else
{
tp_t
=
4000
-
tp_t
;
os_sprintf
(
pbuf
,
UPLOAD_FRAME
,
count
,
"-"
,
tp_t
/
100
,
tp_t
%
100
,
rh_t
/
100
,
rh_t
%
100
,
devkey
);
}
}
#elif FLAMMABLE_GAS_SUB_DEVICE
else
{
uint32
adc_value
=
system_adc_read
();
os_sprintf
(
pbuf
,
UPLOAD_FRAME
,
count
,
adc_value
/
1024
,
adc_value
*
1000
/
1024
,
devkey
);
}
#endif
#else
else
{
nonce
=
os_random
()
&
0x7FFFFFFF
;
os_sprintf
(
pbuf
,
FIRST_FRAME
,
nonce
,
devkey
);
}
#endif
ESP_DBG
(
"%s
\n
"
,
pbuf
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
os_free
(
pbuf
);
}
}
#if PLUG_DEVICE || LIGHT_DEVICE
/******************************************************************************
* FunctionName : user_esp_platform_sent_beacon
* Description : sent beacon frame for connection with the host is activate
* Parameters : pespconn -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_sent_beacon
(
struct
espconn
*
pespconn
)
{
if
(
pespconn
==
NULL
)
{
return
;
}
if
(
pespconn
->
state
==
ESPCONN_CONNECT
)
{
if
(
esp_param
.
activeflag
==
0
)
{
ESP_DBG
(
"please check device is activated.
\n
"
);
user_esp_platform_sent
(
pespconn
);
}
else
{
uint8
devkey
[
token_size
]
=
{
0
};
os_memcpy
(
devkey
,
esp_param
.
devkey
,
40
);
ESP_DBG
(
"user_esp_platform_sent_beacon %u
\n
"
,
system_get_time
());
if
(
ping_status
==
0
)
{
ESP_DBG
(
"user_esp_platform_sent_beacon sent fail!
\n
"
);
user_esp_platform_discon
(
pespconn
);
}
else
{
char
*
pbuf
=
(
char
*
)
os_zalloc
(
packet_size
);
if
(
pbuf
!=
NULL
)
{
os_sprintf
(
pbuf
,
BEACON_FRAME
,
devkey
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
ping_status
=
0
;
os_timer_arm
(
&
beacon_timer
,
BEACON_TIME
,
0
);
os_free
(
pbuf
);
}
}
}
}
else
{
ESP_DBG
(
"user_esp_platform_sent_beacon sent fail!
\n
"
);
user_esp_platform_discon
(
pespconn
);
}
}
/******************************************************************************
* FunctionName : user_platform_rpc_set_rsp
* Description : response the message to server to show setting info is received
* Parameters : pespconn -- the espconn used to connetion with the host
* nonce -- mark the message received from server
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_platform_rpc_set_rsp
(
struct
espconn
*
pespconn
,
int
nonce
)
{
char
*
pbuf
=
(
char
*
)
os_zalloc
(
packet_size
);
if
(
pespconn
==
NULL
)
{
return
;
}
os_sprintf
(
pbuf
,
RPC_RESPONSE_FRAME
,
nonce
);
ESP_DBG
(
"%s
\n
"
,
pbuf
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
os_free
(
pbuf
);
}
/******************************************************************************
* FunctionName : user_platform_timer_get
* Description : get the timers from server
* Parameters : pespconn -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_platform_timer_get
(
struct
espconn
*
pespconn
)
{
uint8
devkey
[
token_size
]
=
{
0
};
char
*
pbuf
=
(
char
*
)
os_zalloc
(
packet_size
);
os_memcpy
(
devkey
,
esp_param
.
devkey
,
40
);
if
(
pespconn
==
NULL
)
{
return
;
}
os_sprintf
(
pbuf
,
TIMER_FRAME
,
devkey
);
ESP_DBG
(
"%s
\n
"
,
pbuf
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
os_free
(
pbuf
);
}
/******************************************************************************
* FunctionName : user_esp_platform_upgrade_cb
* Description : Processing the downloaded data from the server
* Parameters : pespconn -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_upgrade_rsp
(
void
*
arg
)
{
struct
upgrade_server_info
*
server
=
arg
;
struct
espconn
*
pespconn
=
server
->
pespconn
;
uint8
devkey
[
41
]
=
{
0
};
uint8
*
pbuf
=
NULL
;
char
*
action
=
NULL
;
os_memcpy
(
devkey
,
esp_param
.
devkey
,
40
);
pbuf
=
(
char
*
)
os_zalloc
(
packet_size
);
if
(
server
->
upgrade_flag
==
true
)
{
ESP_DBG
(
"user_esp_platform_upgarde_successfully
\n
"
);
action
=
"device_upgrade_success"
;
os_sprintf
(
pbuf
,
UPGRADE_FRAME
,
devkey
,
action
,
server
->
pre_version
,
server
->
upgrade_version
);
ESP_DBG
(
"%s
\n
"
,
pbuf
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
if
(
pbuf
!=
NULL
)
{
os_free
(
pbuf
);
pbuf
=
NULL
;
}
}
else
{
ESP_DBG
(
"user_esp_platform_upgrade_failed
\n
"
);
action
=
"device_upgrade_failed"
;
os_sprintf
(
pbuf
,
UPGRADE_FRAME
,
devkey
,
action
,
server
->
pre_version
,
server
->
upgrade_version
);
ESP_DBG
(
"%s
\n
"
,
pbuf
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#else
espconn_sent
(
pespconn
,
pbuf
,
os_strlen
(
pbuf
));
#endif
if
(
pbuf
!=
NULL
)
{
os_free
(
pbuf
);
pbuf
=
NULL
;
}
}
os_free
(
server
->
url
);
server
->
url
=
NULL
;
os_free
(
server
);
server
=
NULL
;
}
/******************************************************************************
* FunctionName : user_esp_platform_upgrade_begin
* Description : Processing the received data from the server
* Parameters : pespconn -- the espconn used to connetion with the host
* server -- upgrade param
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_upgrade_begin
(
struct
espconn
*
pespconn
,
struct
upgrade_server_info
*
server
)
{
uint8
user_bin
[
9
]
=
{
0
};
uint8
devkey
[
41
]
=
{
0
};
server
->
pespconn
=
pespconn
;
os_memcpy
(
devkey
,
esp_param
.
devkey
,
40
);
os_memcpy
(
server
->
ip
,
pespconn
->
proto
.
tcp
->
remote_ip
,
4
);
#ifdef UPGRADE_SSL_ENABLE
server
->
port
=
443
;
#else
server
->
port
=
80
;
#endif
server
->
check_cb
=
user_esp_platform_upgrade_rsp
;
server
->
check_times
=
120000
;
if
(
server
->
url
==
NULL
)
{
server
->
url
=
(
uint8
*
)
os_zalloc
(
512
);
}
if
(
system_upgrade_userbin_check
()
==
UPGRADE_FW_BIN1
)
{
os_memcpy
(
user_bin
,
"user2.bin"
,
10
);
}
else
if
(
system_upgrade_userbin_check
()
==
UPGRADE_FW_BIN2
)
{
os_memcpy
(
user_bin
,
"user1.bin"
,
10
);
}
os_sprintf
(
server
->
url
,
"GET /v1/device/rom/?action=download_rom&version=%s&filename=%s HTTP/1.0
\r\n
Host: "
IPSTR
":%d
\r\n
"
pheadbuffer
""
,
server
->
upgrade_version
,
user_bin
,
IP2STR
(
server
->
ip
),
server
->
port
,
devkey
);
ESP_DBG
(
"%s
\n
"
,
server
->
url
);
#ifdef UPGRADE_SSL_ENABLE
if
(
system_upgrade_start_ssl
(
server
)
==
false
)
{
#else
if
(
system_upgrade_start
(
server
)
==
false
)
{
#endif
ESP_DBG
(
"upgrade is already started
\n
"
);
}
}
#endif
/******************************************************************************
* FunctionName : user_esp_platform_recv_cb
* Description : Processing the received data from the server
* Parameters : arg -- Additional argument to pass to the callback function
* pusrdata -- The received data (or NULL when the connection has been closed!)
* length -- The length of received data
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_recv_cb
(
void
*
arg
,
char
*
pusrdata
,
unsigned
short
length
)
{
char
*
pstr
=
NULL
;
LOCAL
char
pbuffer
[
1024
*
2
]
=
{
0
};
struct
espconn
*
pespconn
=
arg
;
ESP_DBG
(
"user_esp_platform_recv_cb %s
\n
"
,
pusrdata
);
#if (PLUG_DEVICE || LIGHT_DEVICE)
os_timer_disarm
(
&
beacon_timer
);
#endif
if
(
length
==
1460
)
{
os_memcpy
(
pbuffer
,
pusrdata
,
length
);
}
else
{
struct
espconn
*
pespconn
=
(
struct
espconn
*
)
arg
;
os_memcpy
(
pbuffer
+
os_strlen
(
pbuffer
),
pusrdata
,
length
);
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
activate_status
\"
: "
))
!=
NULL
&&
user_esp_platform_parse_nonce
(
pbuffer
)
==
active_nonce
)
{
if
(
os_strncmp
(
pstr
+
19
,
"1"
,
1
)
==
0
)
{
ESP_DBG
(
"device activates successful.
\n
"
);
device_status
=
DEVICE_ACTIVE_DONE
;
esp_param
.
activeflag
=
1
;
system_param_save_with_protect
(
ESP_PARAM_START_SEC
,
&
esp_param
,
sizeof
(
esp_param
));
user_esp_platform_sent
(
pespconn
);
if
(
LIGHT_DEVICE
){
system_restart
();
}
}
else
{
ESP_DBG
(
"device activates failed.
\n
"
);
device_status
=
DEVICE_ACTIVE_FAIL
;
}
}
#if (PLUG_DEVICE || LIGHT_DEVICE)
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
action
\"
:
\"
sys_upgrade
\"
"
))
!=
NULL
)
{
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
version
\"
:"
))
!=
NULL
)
{
struct
upgrade_server_info
*
server
=
NULL
;
int
nonce
=
user_esp_platform_parse_nonce
(
pbuffer
);
user_platform_rpc_set_rsp
(
pespconn
,
nonce
);
server
=
(
struct
upgrade_server_info
*
)
os_zalloc
(
sizeof
(
struct
upgrade_server_info
));
os_memcpy
(
server
->
upgrade_version
,
pstr
+
12
,
16
);
server
->
upgrade_version
[
15
]
=
'\0'
;
os_sprintf
(
server
->
pre_version
,
"%s%d.%d.%dt%d(%s)"
,
VERSION_TYPE
,
IOT_VERSION_MAJOR
,
\
IOT_VERSION_MINOR
,
IOT_VERSION_REVISION
,
device_type
,
UPGRADE_FALG
);
user_esp_platform_upgrade_begin
(
pespconn
,
server
);
}
}
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
action
\"
:
\"
sys_reboot
\"
"
))
!=
NULL
)
{
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
system_upgrade_reboot
,
NULL
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
}
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"/v1/device/timers/"
))
!=
NULL
)
{
int
nonce
=
user_esp_platform_parse_nonce
(
pbuffer
);
user_platform_rpc_set_rsp
(
pespconn
,
nonce
);
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_platform_timer_get
,
pespconn
);
os_timer_arm
(
&
client_timer
,
2000
,
0
);
}
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
method
\"
: "
))
!=
NULL
)
{
if
(
os_strncmp
(
pstr
+
11
,
"GET"
,
3
)
==
0
)
{
user_esp_platform_get_info
(
pespconn
,
pbuffer
);
}
else
if
(
os_strncmp
(
pstr
+
11
,
"POST"
,
4
)
==
0
)
{
user_esp_platform_set_info
(
pespconn
,
pbuffer
);
}
}
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"ping success"
))
!=
NULL
)
{
ESP_DBG
(
"ping success
\n
"
);
ping_status
=
1
;
}
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"send message success"
))
!=
NULL
)
{
}
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"timers"
))
!=
NULL
)
{
user_platform_timer_start
(
pusrdata
,
pespconn
);
}
#elif SENSOR_DEVICE
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
status
\"
:"
))
!=
NULL
)
{
if
(
os_strncmp
(
pstr
+
10
,
"200"
,
3
)
!=
0
)
{
ESP_DBG
(
"message upload failed.
\n
"
);
}
else
{
count
++
;
ESP_DBG
(
"message upload sucessful.
\n
"
);
}
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_discon
,
pespconn
);
os_timer_arm
(
&
client_timer
,
10
,
0
);
}
#endif
else
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"device"
))
!=
NULL
)
{
#if PLUG_DEVICE || LIGHT_DEVICE
user_platform_timer_get
(
pespconn
);
#elif SENSOR_DEVICE
#endif
}
os_memset
(
pbuffer
,
0
,
sizeof
(
pbuffer
));
}
#if (PLUG_DEVICE || LIGHT_DEVICE)
os_timer_arm
(
&
beacon_timer
,
BEACON_TIME
,
0
);
#endif
}
#if AP_CACHE
/******************************************************************************
* FunctionName : user_esp_platform_ap_change
* Description : add the user interface for changing to next ap ID.
* Parameters :
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_ap_change
(
void
)
{
uint8
current_id
;
uint8
i
=
0
;
ESP_DBG
(
"user_esp_platform_ap_is_changing
\n
"
);
current_id
=
wifi_station_get_current_ap_id
();
ESP_DBG
(
"current ap id =%d
\n
"
,
current_id
);
if
(
current_id
==
AP_CACHE_NUMBER
-
1
)
{
i
=
0
;
}
else
{
i
=
current_id
+
1
;
}
while
(
wifi_station_ap_change
(
i
)
!=
true
)
{
i
++
;
if
(
i
==
AP_CACHE_NUMBER
-
1
)
{
i
=
0
;
}
}
/* just need to re-check ip while change AP */
device_recon_count
=
0
;
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_check_ip
,
NULL
);
os_timer_arm
(
&
client_timer
,
100
,
0
);
}
#endif
LOCAL
bool
ICACHE_FLASH_ATTR
user_esp_platform_reset_mode
(
void
)
{
if
(
wifi_get_opmode
()
==
STATION_MODE
)
{
wifi_set_opmode
(
STATIONAP_MODE
);
}
#if AP_CACHE
/* delay 5s to change AP */
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_ap_change
,
NULL
);
os_timer_arm
(
&
client_timer
,
5000
,
0
);
return
true
;
#endif
return
false
;
}
/******************************************************************************
* FunctionName : user_esp_platform_recon_cb
* Description : The connection had an error and is already deallocated.
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_recon_cb
(
void
*
arg
,
sint8
err
)
{
struct
espconn
*
pespconn
=
(
struct
espconn
*
)
arg
;
ESP_DBG
(
"user_esp_platform_recon_cb
\n
"
);
#if (PLUG_DEVICE || LIGHT_DEVICE)
os_timer_disarm
(
&
beacon_timer
);
#endif
#if (PLUG_DEVICE || SENSOR_DEVICE)
user_link_led_output
(
1
);
#endif
if
(
++
device_recon_count
==
5
)
{
device_status
=
DEVICE_CONNECT_SERVER_FAIL
;
if
(
user_esp_platform_reset_mode
())
{
return
;
}
}
#if SENSOR_DEVICE
#ifdef SENSOR_DEEP_SLEEP
if
(
wifi_get_opmode
()
==
STATION_MODE
)
{
user_esp_platform_reset_mode
();
//user_sensor_deep_sleep_enter();
}
else
{
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_reconnect
,
pespconn
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
}
#else
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_reconnect
,
pespconn
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
#endif
#else
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_reconnect
,
pespconn
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
#endif
}
/******************************************************************************
* FunctionName : user_esp_platform_connect_cb
* Description : A new incoming connection has been connected.
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_connect_cb
(
void
*
arg
)
{
struct
espconn
*
pespconn
=
arg
;
ESP_DBG
(
"user_esp_platform_connect_cb
\n
"
);
if
(
wifi_get_opmode
()
==
STATIONAP_MODE
)
{
wifi_set_opmode
(
STATION_MODE
);
}
#if (PLUG_DEVICE || SENSOR_DEVICE)
user_link_led_timer_done
();
#endif
device_recon_count
=
0
;
espconn_regist_recvcb
(
pespconn
,
user_esp_platform_recv_cb
);
espconn_regist_sentcb
(
pespconn
,
user_esp_platform_sent_cb
);
user_esp_platform_sent
(
pespconn
);
}
/******************************************************************************
* FunctionName : user_esp_platform_connect
* Description : The function given as the connect with the host
* Parameters : espconn -- the espconn used to connect the connection
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_connect
(
struct
espconn
*
pespconn
)
{
ESP_DBG
(
"user_esp_platform_connect
\n
"
);
#ifdef CLIENT_SSL_ENABLE
espconn_secure_connect
(
pespconn
);
#else
espconn_connect
(
pespconn
);
#endif
}
#ifdef USE_DNS
/******************************************************************************
* FunctionName : user_esp_platform_dns_found
* Description : dns found callback
* Parameters : name -- pointer to the name that was looked up.
* ipaddr -- pointer to an ip_addr_t containing the IP address of
* the hostname, or NULL if the name could not be found (or on any
* other error).
* callback_arg -- a user-specified callback argument passed to
* dns_gethostbyname
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_dns_found
(
const
char
*
name
,
ip_addr_t
*
ipaddr
,
void
*
arg
)
{
struct
espconn
*
pespconn
=
(
struct
espconn
*
)
arg
;
if
(
ipaddr
==
NULL
)
{
ESP_DBG
(
"user_esp_platform_dns_found NULL
\n
"
);
if
(
++
device_recon_count
==
5
)
{
device_status
=
DEVICE_CONNECT_SERVER_FAIL
;
user_esp_platform_reset_mode
();
}
return
;
}
ESP_DBG
(
"user_esp_platform_dns_found %d.%d.%d.%d
\n
"
,
*
((
uint8
*
)
&
ipaddr
->
addr
),
*
((
uint8
*
)
&
ipaddr
->
addr
+
1
),
*
((
uint8
*
)
&
ipaddr
->
addr
+
2
),
*
((
uint8
*
)
&
ipaddr
->
addr
+
3
));
if
(
esp_server_ip
.
addr
==
0
&&
ipaddr
->
addr
!=
0
)
{
os_timer_disarm
(
&
client_timer
);
esp_server_ip
.
addr
=
ipaddr
->
addr
;
os_memcpy
(
pespconn
->
proto
.
tcp
->
remote_ip
,
&
ipaddr
->
addr
,
4
);
pespconn
->
proto
.
tcp
->
local_port
=
espconn_port
();
#ifdef CLIENT_SSL_ENABLE
pespconn
->
proto
.
tcp
->
remote_port
=
8443
;
#else
pespconn
->
proto
.
tcp
->
remote_port
=
8000
;
#endif
#if (PLUG_DEVICE || LIGHT_DEVICE)
ping_status
=
1
;
#endif
espconn_regist_connectcb
(
pespconn
,
user_esp_platform_connect_cb
);
espconn_regist_disconcb
(
pespconn
,
user_esp_platform_discon_cb
);
espconn_regist_reconcb
(
pespconn
,
user_esp_platform_recon_cb
);
user_esp_platform_connect
(
pespconn
);
}
}
/******************************************************************************
* FunctionName : user_esp_platform_dns_check_cb
* Description : 1s time callback to check dns found
* Parameters : arg -- Additional argument to pass to the callback function
* Returns : none
*******************************************************************************/
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_dns_check_cb
(
void
*
arg
)
{
struct
espconn
*
pespconn
=
arg
;
ESP_DBG
(
"user_esp_platform_dns_check_cb
\n
"
);
espconn_gethostbyname
(
pespconn
,
ESP_DOMAIN
,
&
esp_server_ip
,
user_esp_platform_dns_found
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
}
LOCAL
void
ICACHE_FLASH_ATTR
user_esp_platform_start_dns
(
struct
espconn
*
pespconn
)
{
esp_server_ip
.
addr
=
0
;
espconn_gethostbyname
(
pespconn
,
ESP_DOMAIN
,
&
esp_server_ip
,
user_esp_platform_dns_found
);
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_dns_check_cb
,
pespconn
);
os_timer_arm
(
&
client_timer
,
1000
,
0
);
}
#endif
#if LIGHT_DEVICE
void
user_mdns_conf
()
{
struct
ip_info
ipconfig
;
wifi_get_ip_info
(
STATION_IF
,
&
ipconfig
);
struct
mdns_info
*
info
=
(
struct
mdns_info
*
)
os_zalloc
(
sizeof
(
struct
mdns_info
));
info
->
host_name
=
"espressif_light_demo"
;
info
->
ipAddr
=
ipconfig
.
ip
.
addr
;
//sation ip
info
->
server_name
=
"espLight"
;
info
->
server_port
=
80
;
info
->
txt_data
[
0
]
=
"version = 1.0.1"
;
espconn_mdns_init
(
info
);
}
#endif
/******************************************************************************
* FunctionName : user_esp_platform_check_ip
* Description : espconn struct parame init when get ip addr
* Parameters : none
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_esp_platform_check_ip
(
uint8
reset_flag
)
{
struct
ip_info
ipconfig
;
os_timer_disarm
(
&
client_timer
);
wifi_get_ip_info
(
STATION_IF
,
&
ipconfig
);
if
(
wifi_station_get_connect_status
()
==
STATION_GOT_IP
&&
ipconfig
.
ip
.
addr
!=
0
)
{
#if (PLUG_DEVICE || SENSOR_DEVICE)
user_link_led_timer_init
();
#endif
//***************************
#if LIGHT_DEVICE
user_mdns_conf
();
#endif
//***************************
user_conn
.
proto
.
tcp
=
&
user_tcp
;
user_conn
.
type
=
ESPCONN_TCP
;
user_conn
.
state
=
ESPCONN_NONE
;
device_status
=
DEVICE_CONNECTING
;
if
(
reset_flag
)
{
device_recon_count
=
0
;
}
#if (PLUG_DEVICE || LIGHT_DEVICE)
os_timer_disarm
(
&
beacon_timer
);
os_timer_setfn
(
&
beacon_timer
,
(
os_timer_func_t
*
)
user_esp_platform_sent_beacon
,
&
user_conn
);
#endif
#ifdef USE_DNS
user_esp_platform_start_dns
(
&
user_conn
);
#else
const
char
esp_server_ip
[
4
]
=
{
114
,
215
,
177
,
97
};
os_memcpy
(
user_conn
.
proto
.
tcp
->
remote_ip
,
esp_server_ip
,
4
);
user_conn
.
proto
.
tcp
->
local_port
=
espconn_port
();
#ifdef CLIENT_SSL_ENABLE
user_conn
.
proto
.
tcp
->
remote_port
=
8443
;
#else
user_conn
.
proto
.
tcp
->
remote_port
=
8000
;
#endif
espconn_regist_connectcb
(
&
user_conn
,
user_esp_platform_connect_cb
);
espconn_regist_reconcb
(
&
user_conn
,
user_esp_platform_recon_cb
);
user_esp_platform_connect
(
&
user_conn
);
#endif
}
else
{
/* if there are wrong while connecting to some AP, then reset mode */
if
((
wifi_station_get_connect_status
()
==
STATION_WRONG_PASSWORD
||
wifi_station_get_connect_status
()
==
STATION_NO_AP_FOUND
||
wifi_station_get_connect_status
()
==
STATION_CONNECT_FAIL
))
{
user_esp_platform_reset_mode
();
}
else
{
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_check_ip
,
NULL
);
os_timer_arm
(
&
client_timer
,
100
,
0
);
}
}
}
/******************************************************************************
* FunctionName : user_esp_platform_init
* Description : device parame init based on espressif platform
* Parameters : none
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_esp_platform_init
(
void
)
{
os_sprintf
(
iot_version
,
"%s%d.%d.%dt%d(%s)"
,
VERSION_TYPE
,
IOT_VERSION_MAJOR
,
\
IOT_VERSION_MINOR
,
IOT_VERSION_REVISION
,
device_type
,
UPGRADE_FALG
);
os_printf
(
"IOT VERSION = %s
\n
"
,
iot_version
);
system_param_load
(
ESP_PARAM_START_SEC
,
0
,
&
esp_param
,
sizeof
(
esp_param
));
struct
rst_info
*
rtc_info
=
system_get_rst_info
();
os_printf
(
"reset reason: %x
\n
"
,
rtc_info
->
reason
);
if
(
rtc_info
->
reason
==
REASON_WDT_RST
||
rtc_info
->
reason
==
REASON_EXCEPTION_RST
||
rtc_info
->
reason
==
REASON_SOFT_WDT_RST
)
{
if
(
rtc_info
->
reason
==
REASON_EXCEPTION_RST
)
{
os_printf
(
"Fatal exception (%d):
\n
"
,
rtc_info
->
exccause
);
}
os_printf
(
"epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x
\n
"
,
rtc_info
->
epc1
,
rtc_info
->
epc2
,
rtc_info
->
epc3
,
rtc_info
->
excvaddr
,
rtc_info
->
depc
);
}
/***add by tzx for saving ip_info to avoid dhcp_client start****/
struct
dhcp_client_info
dhcp_info
;
struct
ip_info
sta_info
;
system_rtc_mem_read
(
64
,
&
dhcp_info
,
sizeof
(
struct
dhcp_client_info
));
if
(
dhcp_info
.
flag
==
0x01
)
{
if
(
true
==
wifi_station_dhcpc_status
())
{
wifi_station_dhcpc_stop
();
}
sta_info
.
ip
=
dhcp_info
.
ip_addr
;
sta_info
.
gw
=
dhcp_info
.
gw
;
sta_info
.
netmask
=
dhcp_info
.
netmask
;
if
(
true
!=
wifi_set_ip_info
(
STATION_IF
,
&
sta_info
))
{
os_printf
(
"set default ip wrong
\n
"
);
}
}
os_memset
(
&
dhcp_info
,
0
,
sizeof
(
struct
dhcp_client_info
));
system_rtc_mem_write
(
64
,
&
dhcp_info
,
sizeof
(
struct
rst_info
));
#if AP_CACHE
wifi_station_ap_number_set
(
AP_CACHE_NUMBER
);
#endif
#if 0
{
char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab};
struct ip_info info;
wifi_set_macaddr(SOFTAP_IF, sofap_mac);
wifi_set_macaddr(STATION_IF, sta_mac);
IP4_ADDR(&info.ip, 192, 168, 3, 200);
IP4_ADDR(&info.gw, 192, 168, 3, 1);
IP4_ADDR(&info.netmask, 255, 255, 255, 0);
wifi_set_ip_info(STATION_IF, &info);
IP4_ADDR(&info.ip, 10, 10, 10, 1);
IP4_ADDR(&info.gw, 10, 10, 10, 1);
IP4_ADDR(&info.netmask, 255, 255, 255, 0);
wifi_set_ip_info(SOFTAP_IF, &info);
}
#endif
if
(
esp_param
.
activeflag
!=
1
)
{
#ifdef SOFTAP_ENCRYPT
struct
softap_config
config
;
char
password
[
33
];
char
macaddr
[
6
];
wifi_softap_get_config
(
&
config
);
wifi_get_macaddr
(
SOFTAP_IF
,
macaddr
);
os_memset
(
config
.
password
,
0
,
sizeof
(
config
.
password
));
os_sprintf
(
password
,
MACSTR
"_%s"
,
MAC2STR
(
macaddr
),
PASSWORD
);
os_memcpy
(
config
.
password
,
password
,
os_strlen
(
password
));
config
.
authmode
=
AUTH_WPA_WPA2_PSK
;
wifi_softap_set_config
(
&
config
);
#endif
wifi_set_opmode
(
STATIONAP_MODE
);
}
#if PLUG_DEVICE
user_plug_init
();
#elif LIGHT_DEVICE
user_light_init
();
#elif SENSOR_DEVICE
user_sensor_init
(
esp_param
.
activeflag
);
#endif
if
(
wifi_get_opmode
()
!=
SOFTAP_MODE
)
{
os_timer_disarm
(
&
client_timer
);
os_timer_setfn
(
&
client_timer
,
(
os_timer_func_t
*
)
user_esp_platform_check_ip
,
1
);
os_timer_arm
(
&
client_timer
,
100
,
0
);
}
}
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user/user_esp_platform_timer.c
0 → 100644
View file @
a33e3a4a
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: esp_platform_user_timer.c
*
* Description:
*
* Modification history:
* 2014/5/09, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "os_type.h"
#include "mem.h"
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "user_esp_platform.h"
#define ESP_DEBUG
#ifdef ESP_DEBUG
#define ESP_DBG os_printf
#else
#define ESP_DBG
#endif
LOCAL
os_timer_t
device_timer
;
uint32
min_wait_second
;
char
timestamp_str
[
11
];
int
timestamp
=
0
;
char
*
timer_splits
[
20
]
=
{
NULL
};
struct
esp_platform_wait_timer_param
{
uint8
wait_time_param
[
11
];
uint8
wait_action
[
15
];
int
wait_time_second
;
};
struct
wait_param
{
uint8
action
[
20
][
15
];
uint16
action_number
;
uint16
count
;
uint32
min_time_backup
;
};
void
esp_platform_timer_action
(
struct
esp_platform_wait_timer_param
*
timer_wait_param
,
uint16
count
);
/******************************************************************************
* FunctionName : split
* Description : split string p1 according to sting p2 and save the splits
* Parameters : p1 , p2 ,splits[]
* Returns : the number of splits
*******************************************************************************/
uint16
ICACHE_FLASH_ATTR
split
(
char
*
p1
,
char
*
p2
,
char
*
splits
[])
{
int
i
=
0
;
int
j
=
0
;
while
(
i
!=
-
1
)
{
int
start
=
i
;
int
end
=
indexof
(
p1
,
p2
,
start
);
if
(
end
==
-
1
)
{
end
=
os_strlen
(
p1
);
}
char
*
p
=
(
char
*
)
os_zalloc
(
100
);
os_memcpy
(
p
,
p1
+
start
,
end
-
start
);
p
[
end
-
start
]
=
'\0'
;
splits
[
j
]
=
p
;
j
++
;
i
=
end
+
1
;
if
(
i
>
os_strlen
(
p1
))
{
break
;
}
}
return
j
;
}
/******************************************************************************
* FunctionName : indexof
* Description : calculate the offset of p2 relate to start of p1
* Parameters : p1,p1,start
* Returns : the offset of p2 relate to the start
*******************************************************************************/
int
ICACHE_FLASH_ATTR
indexof
(
char
*
p1
,
char
*
p2
,
int
start
)
{
char
*
find
=
(
char
*
)
os_strstr
(
p1
+
start
,
p2
);
if
(
find
!=
NULL
)
{
return
(
find
-
p1
);
}
return
-
1
;
}
/******************************************************************************
* FunctionName : esp_platform_find_min_time
* Description : find the minimum wait second in timer list
* Parameters : timer_wait_param -- param of timer action and wait time param
* count -- The number of timers given by server
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
esp_platform_find_min_time
(
struct
esp_platform_wait_timer_param
*
timer_wait_param
,
uint16
count
)
{
uint16
i
=
0
;
min_wait_second
=
0xFFFFFFF
;
for
(
i
=
0
;
i
<
count
;
i
++
)
{
if
(
timer_wait_param
[
i
].
wait_time_second
<
min_wait_second
&&
timer_wait_param
[
i
].
wait_time_second
>=
0
)
{
min_wait_second
=
timer_wait_param
[
i
].
wait_time_second
;
}
}
}
/******************************************************************************
* FunctionName : user_platform_timer_first_start
* Description : calculate the wait time of each timer
* Parameters : count -- The number of timers given by server
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_platform_timer_first_start
(
uint16
count
)
{
int
i
=
0
;
struct
esp_platform_wait_timer_param
timer_wait_param
[
100
]
=
{
0
};
ESP_DBG
(
"current timestamp= %ds
\n
"
,
timestamp
);
timestamp
=
timestamp
+
min_wait_second
;
for
(
i
=
0
;
i
<
count
;
i
++
)
{
char
*
str
=
timer_splits
[
i
];
if
(
indexof
(
str
,
"f"
,
0
)
==
0
)
{
char
*
fixed_wait
[
2
];
ESP_DBG
(
"timer is fixed mode
\n
"
);
split
(
str
,
"="
,
fixed_wait
);
os_memcpy
(
timer_wait_param
[
i
].
wait_time_param
,
fixed_wait
[
0
]
+
1
,
os_strlen
(
fixed_wait
[
0
])
-
1
);
os_memcpy
(
timer_wait_param
[
i
].
wait_action
,
fixed_wait
[
1
],
os_strlen
(
fixed_wait
[
1
]));
timer_wait_param
[
i
].
wait_time_second
=
atoi
(
timer_wait_param
[
i
].
wait_time_param
)
-
timestamp
;
os_free
(
fixed_wait
[
0
]);
os_free
(
fixed_wait
[
1
]);
}
else
if
(
indexof
(
str
,
"l"
,
0
)
==
0
)
{
char
*
loop_wait
[
2
];
ESP_DBG
(
"timer is loop mode
\n
"
);
split
(
str
,
"="
,
loop_wait
);
os_memcpy
(
timer_wait_param
[
i
].
wait_time_param
,
loop_wait
[
0
]
+
1
,
os_strlen
(
loop_wait
[
0
])
-
1
);
os_memcpy
(
timer_wait_param
[
i
].
wait_action
,
loop_wait
[
1
],
os_strlen
(
loop_wait
[
1
]));
timer_wait_param
[
i
].
wait_time_second
=
atoi
(
timer_wait_param
[
i
].
wait_time_param
)
-
(
timestamp
%
atoi
(
timer_wait_param
[
i
].
wait_time_param
));
os_free
(
loop_wait
[
0
]);
os_free
(
loop_wait
[
1
]);
}
else
if
(
indexof
(
str
,
"w"
,
0
)
==
0
)
{
char
*
week_wait
[
2
];
int
monday_wait_time
=
0
;
ESP_DBG
(
"timer is weekend mode
\n
"
);
split
(
str
,
"="
,
week_wait
);
os_memcpy
(
timer_wait_param
[
i
].
wait_time_param
,
week_wait
[
0
]
+
1
,
os_strlen
(
week_wait
[
0
])
-
1
);
os_memcpy
(
timer_wait_param
[
i
].
wait_action
,
week_wait
[
1
],
os_strlen
(
week_wait
[
1
]));
monday_wait_time
=
(
timestamp
-
1388937600
)
%
(
7
*
24
*
3600
);
ESP_DBG
(
"monday_wait_time == %d"
,
monday_wait_time
);
if
(
atoi
(
timer_wait_param
[
i
].
wait_time_param
)
>
monday_wait_time
)
{
timer_wait_param
[
i
].
wait_time_second
=
atoi
(
timer_wait_param
[
i
].
wait_time_param
)
-
monday_wait_time
;
}
else
{
timer_wait_param
[
i
].
wait_time_second
=
7
*
24
*
3600
-
monday_wait_time
+
atoi
(
timer_wait_param
[
i
].
wait_time_param
);
}
os_free
(
week_wait
[
0
]);
os_free
(
week_wait
[
1
]);
}
}
esp_platform_find_min_time
(
timer_wait_param
,
count
);
if
(
min_wait_second
==
0
)
{
return
;
}
esp_platform_timer_action
(
timer_wait_param
,
count
);
}
/******************************************************************************
* FunctionName : user_esp_platform_device_action
* Description : Execute the actions of minimum wait time
* Parameters : pwait_action -- point the list of actions which need execute
*
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_esp_platform_device_action
(
struct
wait_param
*
pwait_action
)
{
uint8
i
=
0
;
uint16
count
=
pwait_action
->
count
;
uint16
action_number
=
pwait_action
->
action_number
;
ESP_DBG
(
"there is %d action at the same time
\n
"
,
pwait_action
->
action_number
);
#if PLUG_DEVICE
for
(
i
=
0
;
i
<
action_number
&&
pwait_action
->
action
[
i
][
0
]
!=
'0'
;
i
++
)
{
ESP_DBG
(
"%s
\n
"
,
pwait_action
->
action
[
i
]);
if
(
os_strcmp
(
pwait_action
->
action
[
i
],
"on_switch"
,
9
)
==
0
)
{
user_plug_set_status
(
0x01
);
}
else
if
(
os_strcmp
(
pwait_action
->
action
[
i
],
"off_switch"
,
10
)
==
0
)
{
user_plug_set_status
(
0x00
);
}
else
if
(
os_strcmp
(
pwait_action
->
action
[
i
],
"on_off_switch"
,
13
)
==
0
)
{
if
(
user_plug_get_status
()
==
0
)
{
user_plug_set_status
(
0x01
);
}
else
{
user_plug_set_status
(
0x00
);
}
}
else
{
return
;
}
}
user_platform_timer_first_start
(
count
);
#endif
}
/******************************************************************************
* FunctionName : user_platform_timer_start
* Description : Processing the message about timer from the server
* Parameters : timer_wait_param -- The received data from the server
* count -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_esp_platform_wait_time_overflow_check
(
struct
wait_param
*
pwait_action
)
{
ESP_DBG
(
"min_wait_second = %d"
,
min_wait_second
);
if
(
pwait_action
->
min_time_backup
>=
3600
)
{
os_timer_disarm
(
&
device_timer
);
os_timer_setfn
(
&
device_timer
,
(
os_timer_func_t
*
)
user_esp_platform_wait_time_overflow_check
,
pwait_action
);
os_timer_arm
(
&
device_timer
,
3600000
,
0
);
ESP_DBG
(
"min_wait_second is extended
\n
"
);
}
else
{
os_timer_disarm
(
&
device_timer
);
os_timer_setfn
(
&
device_timer
,
(
os_timer_func_t
*
)
user_esp_platform_device_action
,
pwait_action
);
os_timer_arm
(
&
device_timer
,
pwait_action
->
min_time_backup
*
1000
,
0
);
ESP_DBG
(
"min_wait_second is = %dms
\n
"
,
pwait_action
->
min_time_backup
*
1000
);
}
pwait_action
->
min_time_backup
-=
3600
;
}
/******************************************************************************
* FunctionName : user_platform_timer_start
* Description : Processing the message about timer from the server
* Parameters : timer_wait_param -- The received data from the server
* count -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
esp_platform_timer_action
(
struct
esp_platform_wait_timer_param
*
timer_wait_param
,
uint16
count
)
{
uint16
i
=
0
;
uint16
action_number
;
struct
wait_param
pwait_action
=
{
0
};
pwait_action
.
count
=
count
;
action_number
=
0
;
for
(
i
=
0
;
i
<
count
;
i
++
)
{
if
(
timer_wait_param
[
i
].
wait_time_second
==
min_wait_second
)
{
os_memcpy
(
pwait_action
.
action
[
action_number
],
timer_wait_param
[
i
].
wait_action
,
os_strlen
(
timer_wait_param
[
i
].
wait_action
));
ESP_DBG
(
"*****%s*****
\n
"
,
timer_wait_param
[
i
].
wait_action
);
action_number
++
;
}
}
pwait_action
.
action_number
=
action_number
;
pwait_action
.
min_time_backup
=
min_wait_second
;
user_esp_platform_wait_time_overflow_check
(
&
pwait_action
);
}
/******************************************************************************
* FunctionName : user_platform_timer_start
* Description : Processing the message about timer from the server
* Parameters : pbuffer -- The received data from the server
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_platform_timer_start
(
char
*
pbuffer
)
{
int
str_begin
=
0
;
int
str_end
=
0
;
uint8
i
=
0
;
char
*
pstr_start
=
NULL
;
char
*
pstr_end
=
NULL
;
struct
esp_platform_wait_timer_param
timer_wait_param
[
20
];
char
*
pstr
=
NULL
;
min_wait_second
=
0
;
if
((
pstr
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
timestamp
\"
:"
))
!=
NULL
)
{
pstr_start
=
pstr
+
13
;
pstr_end
=
(
char
*
)
os_strstr
(
pstr_start
,
","
);
if
(
pstr
!=
NULL
)
{
os_memcpy
(
timestamp_str
,
pstr_start
,
pstr_end
-
pstr_start
);
timestamp
=
atoi
(
timestamp_str
);
}
}
for
(
i
=
0
;
i
<
20
;
i
++
)
{
if
(
timer_splits
[
i
]
!=
NULL
)
{
os_free
(
timer_splits
[
i
]);
timer_splits
[
i
]
=
NULL
;
}
}
if
((
pstr_start
=
(
char
*
)
os_strstr
(
pbuffer
,
"
\"
timers
\"
:
\"
"
))
!=
NULL
)
{
str_begin
=
11
;
str_end
=
indexof
(
pstr_start
,
"
\"
"
,
str_begin
);
if
(
str_begin
==
str_end
)
{
os_timer_disarm
(
&
device_timer
);
return
;
}
char
*
split_buffer
=
(
char
*
)
os_zalloc
(
str_end
-
str_begin
+
1
);
os_memcpy
(
split_buffer
,
pstr_start
+
str_begin
,
str_end
-
str_begin
);
uint16
count
=
split
(
split_buffer
,
";"
,
timer_splits
);
os_free
(
split_buffer
);
user_platform_timer_first_start
(
count
);
}
}
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user/user_json.c
0 → 100644
View file @
a33e3a4a
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_json.c
*
* Description: JSON format set up and parse.
* Check your hardware transmation while use this data format.
*
* Modification history:
* 2014/5/09, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "mem.h"
#include "user_json.h"
LOCAL
char
*
json_buf
;
LOCAL
int
pos
;
LOCAL
int
size
;
/******************************************************************************
* FunctionName : find_json_path
* Description : find the JSON format tree's path
* Parameters : json -- A pointer to a JSON set up
* path -- A pointer to the JSON format tree's path
* Returns : A pointer to the JSON format tree
*******************************************************************************/
struct
jsontree_value
*
ICACHE_FLASH_ATTR
find_json_path
(
struct
jsontree_context
*
json
,
const
char
*
path
)
{
struct
jsontree_value
*
v
;
const
char
*
start
;
const
char
*
end
;
int
len
;
v
=
json
->
values
[
0
];
start
=
path
;
do
{
end
=
(
const
char
*
)
os_strstr
(
start
,
"/"
);
if
(
end
==
start
)
{
break
;
}
if
(
end
!=
NULL
)
{
len
=
end
-
start
;
end
++
;
}
else
{
len
=
os_strlen
(
start
);
}
if
(
v
->
type
!=
JSON_TYPE_OBJECT
)
{
v
=
NULL
;
}
else
{
struct
jsontree_object
*
o
;
int
i
;
o
=
(
struct
jsontree_object
*
)
v
;
v
=
NULL
;
for
(
i
=
0
;
i
<
o
->
count
;
i
++
)
{
if
(
os_strncmp
(
start
,
o
->
pairs
[
i
].
name
,
len
)
==
0
)
{
v
=
o
->
pairs
[
i
].
value
;
json
->
index
[
json
->
depth
]
=
i
;
json
->
depth
++
;
json
->
values
[
json
->
depth
]
=
v
;
json
->
index
[
json
->
depth
]
=
0
;
break
;
}
}
}
start
=
end
;
}
while
(
end
!=
NULL
&&
*
end
!=
'\0'
&&
v
!=
NULL
);
json
->
callback_state
=
0
;
return
v
;
}
/******************************************************************************
* FunctionName : json_putchar
* Description : write the value to the JSON format tree
* Parameters : c -- the value which write the JSON format tree
* Returns : result
*******************************************************************************/
int
ICACHE_FLASH_ATTR
json_putchar
(
int
c
)
{
if
(
json_buf
!=
NULL
&&
pos
<=
size
)
{
json_buf
[
pos
++
]
=
c
;
return
c
;
}
return
0
;
}
/******************************************************************************
* FunctionName : json_ws_send
* Description : set up the JSON format tree for string
* Parameters : tree -- A pointer to the JSON format tree
* path -- A pointer to the JSON format tree's path
* pbuf -- A pointer for the data sent
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
json_ws_send
(
struct
jsontree_value
*
tree
,
const
char
*
path
,
char
*
pbuf
)
{
struct
jsontree_context
json
;
/* maxsize = 128 bytes */
json_buf
=
(
char
*
)
os_malloc
(
jsonSize
);
/* reset state and set max-size */
/* NOTE: packet will be truncated at 512 bytes */
pos
=
0
;
size
=
jsonSize
;
json
.
values
[
0
]
=
(
struct
jsontree_value
*
)
tree
;
jsontree_reset
(
&
json
);
find_json_path
(
&
json
,
path
);
json
.
path
=
json
.
depth
;
json
.
putchar
=
json_putchar
;
while
(
jsontree_print_next
(
&
json
)
&&
json
.
path
<=
json
.
depth
);
json_buf
[
pos
]
=
0
;
os_memcpy
(
pbuf
,
json_buf
,
pos
);
os_free
(
json_buf
);
}
/******************************************************************************
* FunctionName : json_parse
* Description : parse the data as a JSON format
* Parameters : js_ctx -- A pointer to a JSON set up
* ptrJSONMessage -- A pointer to the data
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
json_parse
(
struct
jsontree_context
*
json
,
char
*
ptrJSONMessage
)
{
/* Set value */
struct
jsontree_value
*
v
;
struct
jsontree_callback
*
c
;
struct
jsontree_callback
*
c_bak
=
NULL
;
while
((
v
=
jsontree_find_next
(
json
,
JSON_TYPE_CALLBACK
))
!=
NULL
)
{
c
=
(
struct
jsontree_callback
*
)
v
;
if
(
c
==
c_bak
)
{
continue
;
}
c_bak
=
c
;
if
(
c
->
set
!=
NULL
)
{
struct
jsonparse_state
js
;
jsonparse_setup
(
&
js
,
ptrJSONMessage
,
os_strlen
(
ptrJSONMessage
));
c
->
set
(
json
,
&
js
);
}
}
}
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user/user_light.c
0 → 100644
View file @
a33e3a4a
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_light.c
*
* Description: light demo's function realization
*
* Modification history:
* 2014/5/1, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "mem.h"
#include "user_interface.h"
#include "user_light.h"
#include "pwm.h"
#if LIGHT_DEVICE
struct
light_saved_param
light_param
;
/******************************************************************************
* FunctionName : user_light_get_duty
* Description : get duty of each channel
* Parameters : uint8 channel : LIGHT_RED/LIGHT_GREEN/LIGHT_BLUE
* Returns : NONE
*******************************************************************************/
uint32
ICACHE_FLASH_ATTR
user_light_get_duty
(
uint8
channel
)
{
return
light_param
.
pwm_duty
[
channel
];
}
/******************************************************************************
* FunctionName : user_light_set_duty
* Description : set each channel's duty params
* Parameters : uint8 duty : 0 ~ PWM_DEPTH
* uint8 channel : LIGHT_RED/LIGHT_GREEN/LIGHT_BLUE
* Returns : NONE
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_light_set_duty
(
uint32
duty
,
uint8
channel
)
{
if
(
duty
!=
light_param
.
pwm_duty
[
channel
])
{
pwm_set_duty
(
duty
,
channel
);
light_param
.
pwm_duty
[
channel
]
=
pwm_get_duty
(
channel
);
}
}
/******************************************************************************
* FunctionName : user_light_get_period
* Description : get pwm period
* Parameters : NONE
* Returns : uint32 : pwm period
*******************************************************************************/
uint32
ICACHE_FLASH_ATTR
user_light_get_period
(
void
)
{
return
light_param
.
pwm_period
;
}
/******************************************************************************
* FunctionName : user_light_set_duty
* Description : set pwm frequency
* Parameters : uint16 freq : 100hz typically
* Returns : NONE
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_light_set_period
(
uint32
period
)
{
if
(
period
!=
light_param
.
pwm_period
)
{
pwm_set_period
(
period
);
light_param
.
pwm_period
=
pwm_get_period
();
}
}
void
ICACHE_FLASH_ATTR
user_light_restart
(
void
)
{
spi_flash_erase_sector
(
PRIV_PARAM_START_SEC
+
PRIV_PARAM_SAVE
);
spi_flash_write
((
PRIV_PARAM_START_SEC
+
PRIV_PARAM_SAVE
)
*
SPI_FLASH_SEC_SIZE
,
(
uint32
*
)
&
light_param
,
sizeof
(
struct
light_saved_param
));
pwm_start
();
}
/******************************************************************************
* FunctionName : user_light_init
* Description : light demo init, mainy init pwm
* Parameters : none
* Returns : none
*******************************************************************************/
void
ICACHE_FLASH_ATTR
user_light_init
(
void
)
{
spi_flash_read
((
PRIV_PARAM_START_SEC
+
PRIV_PARAM_SAVE
)
*
SPI_FLASH_SEC_SIZE
,
(
uint32
*
)
&
light_param
,
sizeof
(
struct
light_saved_param
));
if
(
light_param
.
pwm_period
>
10000
||
light_param
.
pwm_period
<
1000
){
light_param
.
pwm_period
=
1000
;
}
uint32
io_info
[][
3
]
=
{
{
PWM_0_OUT_IO_MUX
,
PWM_0_OUT_IO_FUNC
,
PWM_0_OUT_IO_NUM
},
{
PWM_1_OUT_IO_MUX
,
PWM_1_OUT_IO_FUNC
,
PWM_1_OUT_IO_NUM
},
{
PWM_2_OUT_IO_MUX
,
PWM_2_OUT_IO_FUNC
,
PWM_2_OUT_IO_NUM
},
{
PWM_3_OUT_IO_MUX
,
PWM_3_OUT_IO_FUNC
,
PWM_3_OUT_IO_NUM
},
{
PWM_4_OUT_IO_MUX
,
PWM_4_OUT_IO_FUNC
,
PWM_4_OUT_IO_NUM
},
};
uint32
pwm_duty_init
[
PWM_CHANNEL
]
=
{
0
};
/*PIN FUNCTION INIT FOR PWM OUTPUT*/
pwm_init
(
light_param
.
pwm_period
,
pwm_duty_init
,
PWM_CHANNEL
,
io_info
);
os_printf
(
"LIGHT PARAM: R: %d
\r\n
"
,
light_param
.
pwm_duty
[
LIGHT_RED
]);
os_printf
(
"LIGHT PARAM: G: %d
\r\n
"
,
light_param
.
pwm_duty
[
LIGHT_GREEN
]);
os_printf
(
"LIGHT PARAM: B: %d
\r\n
"
,
light_param
.
pwm_duty
[
LIGHT_BLUE
]);
if
(
PWM_CHANNEL
>
LIGHT_COLD_WHITE
){
os_printf
(
"LIGHT PARAM: CW: %d
\r\n
"
,
light_param
.
pwm_duty
[
LIGHT_COLD_WHITE
]);
os_printf
(
"LIGHT PARAM: WW: %d
\r\n
"
,
light_param
.
pwm_duty
[
LIGHT_WARM_WHITE
]);
}
os_printf
(
"LIGHT PARAM: P: %d
\r\n
"
,
light_param
.
pwm_period
);
uint32
light_init_target
[
8
]
=
{
0
};
os_memcpy
(
light_init_target
,
light_param
.
pwm_duty
,
sizeof
(
light_param
.
pwm_duty
));
light_set_aim
(
light_init_target
[
LIGHT_RED
],
light_init_target
[
LIGHT_GREEN
],
light_init_target
[
LIGHT_BLUE
],
light_init_target
[
LIGHT_COLD_WHITE
],
light_init_target
[
LIGHT_WARM_WHITE
],
light_param
.
pwm_period
);
set_pwm_debug_en
(
0
);
//disable debug print in pwm driver
os_printf
(
"PWM version : %08x
\r\n
"
,
get_pwm_version
());
}
#endif
sdk/esp_iot_sdk_v1.4.0/examples/IoT_Demo/user/user_light_adj.c
0 → 100644
View file @
a33e3a4a
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "mem.h"
#include "user_interface.h"
#include "user_light.h"
#include "user_light_adj.h"
#include "pwm.h"
#define ABS_MINUS(x,y) (x<y?(y-x):(x-y))
uint8
light_sleep_flg
=
0
;
uint16
min_ms
=
15
;
uint32
current_duty
[
PWM_CHANNEL
]
=
{
0
};
bool
change_finish
=
true
;
os_timer_t
timer_pwm_adj
;
static
u32
duty_now
[
PWM_CHANNEL
]
=
{
0
};
//-----------------------------------Light para storage---------------------------
#define LIGHT_EVT_QNUM (40)
static
struct
pwm_param
LightEvtArr
[
LIGHT_EVT_QNUM
];
static
u8
CurFreeLightEvtIdx
=
0
;
static
u8
TotalUsedLightEvtNum
=
0
;
static
u8
CurEvtIdxToBeUse
=
0
;
static
struct
pwm_param
*
LightEvtMalloc
(
void
)
{
struct
pwm_param
*
tmp
=
NULL
;
TotalUsedLightEvtNum
++
;
if
(
TotalUsedLightEvtNum
>
LIGHT_EVT_QNUM
){
TotalUsedLightEvtNum
--
;
}
else
{
tmp
=
&
(
LightEvtArr
[
CurFreeLightEvtIdx
]);
CurFreeLightEvtIdx
++
;
if
(
CurFreeLightEvtIdx
>
(
LIGHT_EVT_QNUM
-
1
)
)
CurFreeLightEvtIdx
=
0
;
}
os_printf
(
"malloc:%u
\n
"
,
TotalUsedLightEvtNum
);
return
tmp
;
}
static
void
ICACHE_FLASH_ATTR
LightEvtFree
(
void
)
{
TotalUsedLightEvtNum
--
;
os_printf
(
"free:%u
\n
"
,
TotalUsedLightEvtNum
);
}
//------------------------------------------------------------------------------------
static
void
ICACHE_FLASH_ATTR
light_pwm_smooth_adj_proc
(
void
);
void
ICACHE_FLASH_ATTR
light_save_target_duty
()
{
extern
struct
light_saved_param
light_param
;
os_memcpy
(
light_param
.
pwm_duty
,
current_duty
,
sizeof
(
light_param
.
pwm_duty
));
light_param
.
pwm_period
=
pwm_get_period
();
#if SAVE_LIGHT_PARAM
spi_flash_erase_sector
(
PRIV_PARAM_START_SEC
+
PRIV_PARAM_SAVE
);
spi_flash_write
((
PRIV_PARAM_START_SEC
+
PRIV_PARAM_SAVE
)
*
SPI_FLASH_SEC_SIZE
,
(
uint32
*
)
&
light_param
,
sizeof
(
struct
light_saved_param
));
#endif
}
void
ICACHE_FLASH_ATTR
light_set_aim_r
(
uint32
r
)
{
current_duty
[
LIGHT_RED
]
=
r
;
light_pwm_smooth_adj_proc
();
}
void
ICACHE_FLASH_ATTR
light_set_aim_g
(
uint32
g
)
{
current_duty
[
LIGHT_GREEN
]
=
g
;
light_pwm_smooth_adj_proc
();
}
void
ICACHE_FLASH_ATTR
light_set_aim_b
(
uint32
b
)
{
current_duty
[
LIGHT_BLUE
]
=
b
;
light_pwm_smooth_adj_proc
();
}
void
ICACHE_FLASH_ATTR
light_set_aim_cw
(
uint32
cw
)
{
current_duty
[
LIGHT_COLD_WHITE
]
=
cw
;
light_pwm_smooth_adj_proc
();
}
void
ICACHE_FLASH_ATTR
light_set_aim_ww
(
uint32
ww
)
{
current_duty
[
LIGHT_WARM_WHITE
]
=
ww
;
light_pwm_smooth_adj_proc
();
}
LOCAL
bool
ICACHE_FLASH_ATTR
check_pwm_current_duty_diff
()
{
int
i
;
for
(
i
=
0
;
i
<
PWM_CHANNEL
;
i
++
){
if
(
pwm_get_duty
(
i
)
!=
current_duty
[
i
]){
return
true
;
}
}
return
false
;
}
void
light_dh_pwm_adj_proc
(
void
*
Targ
)
{
uint8
i
;
for
(
i
=
0
;
i
<
PWM_CHANNEL
;
i
++
){
duty_now
[
i
]
=
(
duty_now
[
i
]
*
15
+
current_duty
[
i
])
>>
4
;
if
(
ABS_MINUS
(
duty_now
[
i
],
current_duty
[
i
])
<
20
)
duty_now
[
i
]
=
current_duty
[
i
];
user_light_set_duty
(
duty_now
[
i
],
i
);
}
//os_printf("duty:%u,%u,%u\r\n", pwm.duty[0],pwm.duty[1],pwm.duty[2] );
pwm_start
();
if
(
check_pwm_current_duty_diff
()){
change_finish
=
0
;
os_timer_disarm
(
&
timer_pwm_adj
);
os_timer_setfn
(
&
timer_pwm_adj
,
(
os_timer_func_t
*
)
light_dh_pwm_adj_proc
,
NULL
);
os_timer_arm
(
&
timer_pwm_adj
,
min_ms
,
0
);
}
else
{
os_printf
(
"finish
\n
"
);
change_finish
=
1
;
//light_save_target_duty();
os_timer_disarm
(
&
timer_pwm_adj
);
light_pwm_smooth_adj_proc
();
}
}
LOCAL
bool
ICACHE_FLASH_ATTR
check_pwm_duty_zero
()
{
int
i
;
for
(
i
=
0
;
i
<
PWM_CHANNEL
;
i
++
){
if
(
pwm_get_duty
(
i
)
!=
0
){
return
false
;
}
}
return
true
;
}
static
void
ICACHE_FLASH_ATTR
light_pwm_smooth_adj_proc
(
void
)
{
if
(
TotalUsedLightEvtNum
>
0
){
user_light_set_period
(
LightEvtArr
[
CurEvtIdxToBeUse
].
period
);
os_memcpy
(
current_duty
,
LightEvtArr
[
CurEvtIdxToBeUse
].
duty
,
sizeof
(
current_duty
));
CurEvtIdxToBeUse
++
;
if
(
CurEvtIdxToBeUse
>
(
LIGHT_EVT_QNUM
-
1
)
){
CurEvtIdxToBeUse
=
0
;
}
LightEvtFree
();
if
(
change_finish
){
light_dh_pwm_adj_proc
(
NULL
);
}
}
if
(
change_finish
){
light_save_target_duty
();
if
(
check_pwm_duty_zero
()){
if
(
light_sleep_flg
==
0
){
os_printf
(
"light sleep en
\r\n
"
);
wifi_set_sleep_type
(
LIGHT_SLEEP_T
);
light_sleep_flg
=
1
;
}
}
}
}
#if LIGHT_CURRENT_LIMIT
uint32
light_get_cur
(
uint32
duty
,
uint8
channel
,
uint32
period
)
{
uint32
duty_max_limit
=
(
period
*
1000
/
45
);
uint32
duty_mapped
=
duty
*
22727
/
duty_max_limit
;
switch
(
channel
){
case
LIGHT_RED
:
if
(
duty_mapped
>=
0
&&
duty_mapped
<
23000
){
return
(
duty_mapped
*
151000
/
22727
);
}
break
;
case
LIGHT_GREEN
:
if
(
duty_mapped
>=
0
&&
duty_mapped
<
23000
){
return
(
duty_mapped
*
82000
/
22727
);
}
break
;
case
LIGHT_BLUE
:
if
(
duty_mapped
>=
0
&&
duty_mapped
<
23000
){
return
(
duty_mapped
*
70000
/
22727
);
}
break
;
case
LIGHT_COLD_WHITE
:
case
LIGHT_WARM_WHITE
:
if
(
duty_mapped
>=
0
&&
duty_mapped
<
23000
){
return
(
duty_mapped
*
115000
/
22727
);
}
break
;
default:
os_printf
(
"CHANNEL ERROR IN GET_CUR
\r\n
"
);
break
;
}
}
#endif
void
ICACHE_FLASH_ATTR
light_set_aim
(
uint32
r
,
uint32
g
,
uint32
b
,
uint32
cw
,
uint32
ww
,
uint32
period
)
{
struct
pwm_param
*
tmp
=
LightEvtMalloc
();
if
(
tmp
!=
NULL
){
tmp
->
period
=
(
period
<
10000
?
period
:
10000
);
uint32
duty_max_limit
=
(
period
*
1000
/
45
);
tmp
->
duty
[
LIGHT_RED
]
=
(
r
<
duty_max_limit
?
r
:
duty_max_limit
);
tmp
->
duty
[
LIGHT_GREEN
]
=
(
g
<
duty_max_limit
?
g
:
duty_max_limit
);
tmp
->
duty
[
LIGHT_BLUE
]
=
(
b
<
duty_max_limit
?
b
:
duty_max_limit
);
tmp
->
duty
[
LIGHT_COLD_WHITE
]
=
(
cw
<
duty_max_limit
?
cw
:
duty_max_limit
);
tmp
->
duty
[
LIGHT_WARM_WHITE
]
=
(
ww
<
duty_max_limit
?
ww
:
duty_max_limit
);
//chg
#if LIGHT_CURRENT_LIMIT
uint32
cur_r
,
cur_g
,
cur_b
,
cur_rgb
;
//if(cw>0 || ww>0){
cur_r
=
light_get_cur
(
tmp
->
duty
[
LIGHT_RED
]
,
LIGHT_RED
,
tmp
->
period
);
cur_g
=
light_get_cur
(
tmp
->
duty
[
LIGHT_GREEN
]
,
LIGHT_GREEN
,
tmp
->
period
);
cur_b
=
light_get_cur
(
tmp
->
duty
[
LIGHT_BLUE
]
,
LIGHT_BLUE
,
tmp
->
period
);
cur_rgb
=
(
cur_r
+
cur_g
+
cur_b
);
//}
uint32
cur_cw
=
light_get_cur
(
tmp
->
duty
[
LIGHT_COLD_WHITE
],
LIGHT_COLD_WHITE
,
tmp
->
period
);
uint32
cur_ww
=
light_get_cur
(
tmp
->
duty
[
LIGHT_WARM_WHITE
],
LIGHT_WARM_WHITE
,
tmp
->
period
);
uint32
cur_remain
,
cur_mar
;
cur_remain
=
(
LIGHT_TOTAL_CURRENT_MAX
-
cur_rgb
-
LIGHT_CURRENT_MARGIN
);
cur_mar
=
LIGHT_CURRENT_MARGIN
;
/*
if((cur_cw < 50000) || (cur_ww < 50000)){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
cur_mar = LIGHT_CURRENT_MARGIN;
}else if((cur_cw < 99000) || (cur_ww < 99000)){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L2);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}else{
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L3);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}
*/
/*
if((LIGHT_TOTAL_CURRENT_MAX-cur_rgb)>120){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
cur_mar = LIGHT_CURRENT_MARGIN;
}else if((LIGHT_TOTAL_CURRENT_MAX-cur_rgb)>100){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L2);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}else{
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L3);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}
*/
os_printf
(
"cur_remain: %d
\r\n
"
,
cur_remain
);
while
((
cur_cw
+
cur_ww
)
>
cur_remain
){
tmp
->
duty
[
LIGHT_COLD_WHITE
]
=
tmp
->
duty
[
LIGHT_COLD_WHITE
]
*
9
/
10
;
tmp
->
duty
[
LIGHT_WARM_WHITE
]
=
tmp
->
duty
[
LIGHT_WARM_WHITE
]
*
9
/
10
;
cur_cw
=
light_get_cur
(
tmp
->
duty
[
LIGHT_COLD_WHITE
],
LIGHT_COLD_WHITE
,
tmp
->
period
);
cur_ww
=
light_get_cur
(
tmp
->
duty
[
LIGHT_WARM_WHITE
],
LIGHT_WARM_WHITE
,
tmp
->
period
);
}
os_printf
(
"debug : %d %d %d %d %d
\r\n
"
,
cur_r
/
1000
,
cur_g
/
1000
,
cur_b
/
1000
,
cur_cw
/
1000
,
cur_ww
/
1000
);
os_printf
(
"debug:total current after adj : %d + %d mA
\r\n
"
,(
cur_cw
+
cur_ww
+
cur_r
+
cur_g
+
cur_b
)
/
1000
,
cur_mar
/
1000
);
#endif
os_printf
(
"prd:%u r : %u g: %u b: %u cw: %u ww: %u
\r\n
"
,
period
,
tmp
->
duty
[
0
],
tmp
->
duty
[
1
],
tmp
->
duty
[
2
],
tmp
->
duty
[
3
],
tmp
->
duty
[
4
]);
light_pwm_smooth_adj_proc
();
}
else
{
os_printf
(
"light para full
\n
"
);
}
}
Prev
1
…
5
6
7
8
9
10
11
12
13
…
15
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment