#ifndef __IKONKE_USER_H______________________________________
#define __IKONKE_USER_H______________________________________


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "ctype.h"

#include "driver/ikk-led.h"
#include "driver/ikk-uart.h"
#include "driver/ikk-button.h"
#include "driver/ikk-relay.h"
#include "general/ikk-common-utils.h"
#include "general/ikk-module-def.h"
#include "general/ikk-network.h"
#include "general/ikk-cluster.h"
#include "general/ikk-opt-tunnel.h"
#include "general/ikk-debug.h"
#include "./general/ikk-command.h"
#include "driver/ikk-pwm.h"

#if Z30_DEVICE_OTA_ENABLE
#include "general/ikk-ota.h"
#endif
#if Z30_DEVICE_FACTORY_TEST_ENABLE
#include "general/ikk-factory-test.h"
#endif


// ENDPOINT DEFINEs
#define USER_ENDPOINT 				(1)

// DEVICE HEARTBEAT PERIOD
#define HEARTBEAT_PERIOD_S			14400UL	// UNIT: S

// iHORN device type
#define iHORN_DEV_NATURAL_GAS 		0X41	// 天燃气
#define iHORN_DEV_SMOKE 			0X51	// 烟雾
#define iHORN_DEV_WATER	 			0X73	// 水浸

// HEADERs

#define OUT_LINE                	0   //未入网
#define IN_LINE                 	1   //已入网
#define IN_LINE_NETWORKING      	2   //已入网且允许其他设备入网
#define JOIN_FAILED      			3	//贾汪失败
// OPCODEs
#define CMD_OFFSET					2
#define CMD_RETRY_SENT				0X00	// 重发缓存消息
#define CMD_REPORT_DATA				0X01	// 数据上报
#define CMD_UP_JOIN_RESUME			0X02	// 入网 & 恢复出厂
#define CMD_DOWN_LED				0X03	// LED控制

typedef enum {
	RUN_STATUS_SINGLE_BOARD_TEST = 0x01,
	RUN_STATUS_FULL_DEVICE_TEST,
	RUN_STATUS_NORMAL,
	RUN_STATUS_REBOOT1,
	RUN_STATUS_REBOOT2,
	RUN_STATUS_REBOOT3,
	RUN_STATUS_INIT = 0xFF,
}RUN_SATUS_E;


typedef enum { EJSP_UNKNOW = 0, EJSP_DEVSNAP, EJSP_CMEI_CODE, EJSP_ISN_CODE, EJSP_ENDED }JoinedSPEnum;

typedef enum {
	ERM_LEAVE_NETWORK = 0,	// Leave Network
	ERM_RESUME_FACTORY,		// Resume to Factory
}ResumeMethodEm;

// iHORN CMD data Format
#pragma pack(1)
typedef struct tag_ihorn_cmd_data {
	unsigned char preamble;
	unsigned char length;
	unsigned char opcode;
	unsigned int  seq_devid;
	unsigned char vendor_id;
	unsigned char dev_type;
	unsigned char data1;
	unsigned char data2;
	unsigned char extend[16];
}iHORNCmdFormatSt;

// iHORN  sensor staus buffer
#pragma pack(1)
typedef struct tag_ihorn_sensor_status_data {
	unsigned char mcu_version;
	unsigned char status;
}SensorStatusSt;


// Automatic execution procedure after joined successed.
#define JOINED_PROCEDURE_NONE			(0)		// 不做任何处理
#define JOINED_PROCEDURE_REP_SNAPSHOT	(1) 	// 上报设备快照
#define JOINED_PROCEDURE_REP_CMEI		(2) 	// 上报CMEI
#define JOINED_PROCEDURE_REP_ISN		(3) 	// 上报ISN
#define JOINED_PROCEDURE_END			(0XFF) 	// 流程结束

#define RETRY_BANDING_CLUSTER_MAX_TIMES	2 // 入网成功后尝试自绑定此处

// Add by dingmz_frc.20190626: define for device instance object.
typedef struct {
	// 设备join网络后，JoinSucceedEvent事件处理操作索引，用于入网成功后续分步操作
	uint8_t u8ActionIndexAfterJoined;
}KKDeviceObjectSt;


//__no_init uint8_t ramAddrTest1 @ 0x2000FFFC;
//__no_init uint8_t ramAddrTest2 @ 0x2000FFFD;
//uint8_t ramAddrTest3;

extern uint8_t runStatus;
extern EmberEventControl kkSystemSetUpEventControl;
extern EmberEventControl kkFindingAndBindingEvent;
extern EmberEventControl kUserJoinSucceedProcedureEventControl;
extern EmberEventControl kkHeartBeatLoopEvent;
extern KKDeviceObjectSt g_stDeviceObject;

/*	DESP: User Module init interface.
 * 	Auth: dingmz_frc.20190623.
 * */
void UserModuleInit(void );


#endif
