@ -15,12 +15,20 @@
# include "hsf.h"
# include "hilink_entry.h"
extern void handle_device_online ( void ) ;
extern void handle_device_unbind ( void ) ;
extern void handle_device_offline ( void ) ;
// 声明外部函数
# include "switch_panel/switch_panel.h"
/*
*
* 服务信息定义
* 注意:(1)适配格式{svcType, svcId}
* (2)与devicepartner平台物模型定义必须保持一致
*/
// 四开关面板服务信息定义
static const HILINK_SvcInfo SVC_INFO [ ] = {
{ " switch " , " switch3 " } ,
{ " switch " , " switch2 " } ,
@ -31,6 +39,7 @@ static const HILINK_SvcInfo SVC_INFO[] = {
// { "checkSum", "checkSum" },
# endif
} ;
int HILINK_GetDevInfo ( HILINK_DevInfo * devinfo )
{
if ( devinfo = = NULL ) {
@ -126,9 +135,9 @@ int HILINK_GetSvcInfo(HILINK_SvcInfo *svcInfo[], unsigned int size)
return - 1 ;
}
}
e_printf ( " HILINK_GetSvcInfo svcNum:%d \r \n " , svcNum ) ;
return svcNum ;
}
/* AC参数 */
unsigned char A_C [ 48 ] = {
0x66 , 0x22 , 0x33 , 0x5F , 0x75 , 0x31 , 0x29 , 0x6A , 0x27 , 0x34 , 0x57 , 0x44 , 0x32 , 0x42 , 0x27 , 0x59 , 0xE1 , \
@ -145,21 +154,6 @@ unsigned char *HILINK_GetAutoAc(void)
extern int HILINK_HILINK_Printf ( const char * format , . . . ) ;
// 设备状态定义
typedef struct {
int switch3_on ;
int switch3_name ;
int switch2_on ;
int switch2_name ;
int switch1_on ;
int switch1_name ;
int switch_on ;
int switch4_on ;
int switch4_name ;
} t_device_info ;
// 分配一个对象记录设备状态
static t_device_info g_device_info = { 0 } ;
// 服务处理函数定义
typedef int ( * handle_put_func ) ( const char * svc_id , const char * payload , unsigned int len ) ;
typedef int ( * handle_get_func ) ( const char * svc_id , const char * in , unsigned int in_len , char * * out , unsigned int * out_len ) ;
@ -178,148 +172,13 @@ int not_support_put(const char* svc_id, const char* payload, unsigned int len)
HILINK_Printf ( " sid:%s NOT SUPPORT PUT function \r \n " , svc_id ) ;
return 0 ;
}
// 处理从 HILINK_PutCharState 传递过来的信息
int handle_put_switch ( const char * svc_id , const char * payload , unsigned int len )
{
cJSON * pJson = cJSON_Parse ( payload ) ;
if ( pJson = = NULL ) {
HILINK_Printf ( " JSON parse failed in PUT cmd: ID-%s \r \n " , svc_id ) ;
return - 1 ;
}
cJSON * item = cJSON_GetObjectItem ( pJson , " on " ) ;
if ( item ! = NULL )
g_device_info . switch_on = item - > valueint ;
if ( pJson ! = NULL )
cJSON_Delete ( pJson ) ;
HILINK_Printf ( " handle func:%s, sid:%s \r \n " , __FUNCTION__ , svc_id ) ;
return 0 ;
}
// 处理从 HILINK_GetCharState 传递过来的信息
int handle_get_switch ( const char * svc_id , const char * in , unsigned int in_len , char * * out , unsigned int * out_len )
{
* out_len = 20 ;
* out = ( char * ) malloc ( * out_len ) ;
if ( NULL = = * out )
return - 1 ;
* out_len = sprintf_s ( * out , * out_len , " { \" on \" :%d} " , g_device_info . switch_on ) ;
HILINK_Printf ( " %s:%d svcId:%s, *out:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , * out ) ;
return 0 ;
}
// 服务处理信息注册
int handle_put_switch3 ( const char * svc_id , const char * payload , unsigned int len )
{
cJSON * pJson = cJSON_Parse ( payload ) ;
if ( ! pJson )
return - 1 ;
cJSON * on_item = cJSON_GetObjectItem ( pJson , " on " ) ;
if ( on_item )
g_device_info . switch3_on = on_item - > valueint ;
cJSON * name_item = cJSON_GetObjectItem ( pJson , " name " ) ;
if ( name_item )
g_device_info . switch3_name = name_item - > valueint ;
HILINK_Printf ( " %s:%d svcId:%s, payload:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , payload ) ;
cJSON_Delete ( pJson ) ;
return 0 ;
}
int handle_get_switch3 ( const char * svc_id , const char * in , unsigned int in_len , char * * out , unsigned int * out_len )
{
* out_len = 64 ;
* out = ( char * ) malloc ( * out_len ) ;
if ( * out = = NULL )
return - 1 ;
* out_len = sprintf_s ( * out , * out_len , " { \" on \" :%d, \" name \" :%d} " , g_device_info . switch3_on , g_device_info . switch3_name ) ;
HILINK_Printf ( " %s:%d svcId:%s, *out:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , * out ) ;
return 0 ;
}
int handle_put_switch2 ( const char * svc_id , const char * payload , unsigned int len )
{
cJSON * pJson = cJSON_Parse ( payload ) ;
if ( ! pJson )
return - 1 ;
cJSON * on_item = cJSON_GetObjectItem ( pJson , " on " ) ;
if ( on_item )
g_device_info . switch2_on = on_item - > valueint ;
cJSON * name_item = cJSON_GetObjectItem ( pJson , " name " ) ;
if ( name_item )
g_device_info . switch2_name = name_item - > valueint ;
HILINK_Printf ( " %s:%d svcId:%s, payload:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , payload ) ;
cJSON_Delete ( pJson ) ;
return 0 ;
}
int handle_get_switch2 ( const char * svc_id , const char * in , unsigned int in_len , char * * out , unsigned int * out_len )
{
* out_len = 64 ;
* out = ( char * ) malloc ( * out_len ) ;
if ( * out = = NULL )
return - 1 ;
* out_len = sprintf_s ( * out , * out_len , " { \" on \" :%d, \" name \" :%d} " , g_device_info . switch2_on , g_device_info . switch2_name ) ;
HILINK_Printf ( " %s:%d svcId:%s, *out:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , * out ) ;
return 0 ;
}
int handle_put_switch1 ( const char * svc_id , const char * payload , unsigned int len )
{
cJSON * pJson = cJSON_Parse ( payload ) ;
if ( ! pJson )
return - 1 ;
cJSON * on_item = cJSON_GetObjectItem ( pJson , " on " ) ;
if ( on_item )
g_device_info . switch1_on = on_item - > valueint ;
cJSON * name_item = cJSON_GetObjectItem ( pJson , " name " ) ;
if ( name_item )
g_device_info . switch1_name = name_item - > valueint ;
HILINK_Printf ( " %s:%d svcId:%s, payload:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , payload ) ;
cJSON_Delete ( pJson ) ;
return 0 ;
}
int handle_get_switch1 ( const char * svc_id , const char * in , unsigned int in_len , char * * out , unsigned int * out_len )
{
* out_len = 64 ;
* out = ( char * ) malloc ( * out_len ) ;
if ( * out = = NULL )
return - 1 ;
* out_len = sprintf_s ( * out , * out_len , " { \" on \" :%d, \" name \" :%d} " , g_device_info . switch1_on , g_device_info . switch1_name ) ;
HILINK_Printf ( " %s:%d svcId:%s, *out:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , * out ) ;
return 0 ;
}
int handle_put_switch4 ( const char * svc_id , const char * payload , unsigned int len )
{
cJSON * pJson = cJSON_Parse ( payload ) ;
if ( ! pJson )
return - 1 ;
cJSON * on_item = cJSON_GetObjectItem ( pJson , " on " ) ;
if ( on_item )
g_device_info . switch4_on = on_item - > valueint ;
cJSON * name_item = cJSON_GetObjectItem ( pJson , " name " ) ;
if ( name_item )
g_device_info . switch4_name = name_item - > valueint ;
HILINK_Printf ( " %s:%d svcId:%s, payload:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , payload ) ;
cJSON_Delete ( pJson ) ;
return 0 ;
}
int handle_get_switch4 ( const char * svc_id , const char * in , unsigned int in_len , char * * out , unsigned int * out_len )
{
* out_len = 64 ;
* out = ( char * ) malloc ( * out_len ) ;
if ( * out = = NULL )
return - 1 ;
* out_len = sprintf_s ( * out , * out_len , " { \" on \" :%d, \" name \" :%d} " , g_device_info . switch4_on , g_device_info . switch4_name ) ;
HILINK_Printf ( " %s:%d svcId:%s, *out:%s \r \n " , __FUNCTION__ , __LINE__ , svc_id , * out ) ;
return 0 ;
}
// 服务处理函数
HANDLE_SVC_INFO g_device_profile [ ] = {
{ " switch3 " , handle_put_switch3 , handle_get_switch3 } ,
{ " switch2 " , handle_put_switch2 , handle_get_switch2 } ,
{ " switch1 " , handle_put_switch1 , handle_get_switch1 } ,
{ " switch " , handle_put_switch , handle_get_switch } ,
{ " switch4 " , handle_put_switch4 , handle_get_switch4 } ,
{ " switch " , handle_put_switch , handle_get_switch } ,
{ " switch1 " , handle_put_switch1 , handle_get_switch1 } ,
{ " switch2 " , handle_put_switch2 , handle_get_switch2 } ,
{ " switch3 " , handle_put_switch3 , handle_get_switch3 } ,
{ " switch4 " , handle_put_switch4 , handle_get_switch4 } ,
} ;
// 服务总数量
int g_device_profile_count = sizeof ( g_device_profile ) / sizeof ( HANDLE_SVC_INFO ) ;
@ -366,6 +225,7 @@ int handle_get_cmd(const char* svc_id, const char* in, unsigned int in_len, char
}
return function ( svc_id , in , in_len , out , out_len ) ;
}
// 快速上报函数,用于上报服务状态信息
int fast_report ( const char * svc_id )
{
@ -377,7 +237,7 @@ int fast_report(const char* svc_id)
return err ;
}
err = HILINK_ReportCharState ( svc_id , payload , len ) ;
HILINK_Printf ( " report %s result is %d \r \n " , svc_id , err ) ;
HILINK_Printf ( " report %s result is %d, payload:%s \r \n " , svc_id , err , payload );
return err ;
}
/*
@ -389,7 +249,10 @@ int fast_report(const char* svc_id)
*/
int HILINK_PutCharState ( const char * svcId , const char * payload , unsigned int len )
{
e_printf ( " [HILINK_PutCharState] 收到控制指令: svcId=%s, payload=%s \r \n " , svcId , payload ) ;
if ( ( svcId = = NULL ) | | ( payload = = NULL ) ) {
e_printf ( " [HILINK_PutCharState] 参数无效 \r \n " ) ;
return - 1 ;
}
@ -397,8 +260,11 @@ int HILINK_PutCharState(const char *svcId, const char *payload, unsigned int len
if ( json = = NULL ) {
return - 1 ;
}
int err = handle_put_cmd ( svcId , payload , le n ) ;
return err ;
cJSON_Delete ( jso n ) ;
int err = handle_put_cmd ( svcId , payload , len ) ;
e_printf ( " [HILINK_PutCharState] 控制指令处理完成,返回值: %d \r \n " , err ) ;
return err ;
}
# ifdef CONFIG_SUPPORT_HILINK_INDIE_UPGRADE
static int GetFwvCheckSum ( char * * out , unsigned int * outLen )
@ -498,6 +364,7 @@ void HILINK_NotifyDevStatus(int status)
BLE_CfgNetAdvCtrl ( 0 ) ;
( void ) BLE_CfgNetAdvCtrl ( 0xFFFFFFFF ) ;
# endif
handle_device_offline ( ) ;
break ;
case HILINK_M2M_CLOUD_ONLINE :
/* 设备连接云端成功,请在此处添加实现 */
@ -506,6 +373,8 @@ void HILINK_NotifyDevStatus(int status)
BLE_CfgNetAdvCtrl ( 0 ) ;
( void ) BLE_CfgNetAdvCtrl ( 0xFFFFFFFF ) ;
# endif
/* 设备连接云端成功,请在此处添加实现 */
handle_device_online ( ) ; // 处理设备上线
break ;
case HILINK_M2M_LONG_OFFLINE :
/* 设备与云端连接长时间断开,请在此处添加实现 */
@ -542,9 +411,11 @@ void HILINK_NotifyDevStatus(int status)
break ;
case HILINK_DEVICE_UNREGISTER :
/* 设备被解绑,请在此处添加实现 */
handle_device_unbind ( ) ;
break ;
case HILINK_REVOKE_FLAG_SET :
/* 设备被复位标记置位,请在此处添加实现 */
handle_device_unbind ( ) ;
break ;
case HILINK_NEGO_REG_INFO_FAIL :
/* 设备协商配网信息失败 */
@ -589,6 +460,7 @@ int HILINK_ProcessBeforeRestart(int flag)
/* APP删除设备触发模组重启 */
if ( flag = = 1 ) {
/* 实现模组重启前的操作(如:保存系统状态等) */
handle_device_unbind ( ) ; // 处理设备解绑
return 1 ;
}