#ifndef _IKK_NETWORK_H_
#define _IKK_NETWORK_H_
#include "ikk-module-def.h"
#include "ikk-opt-tunnel.h"
#include "ikk-debug.h"
#include "tl_common.h"
#include "zb_api.h"
#include "zcl_include.h"
#include "bdb.h"


// auto binging objectors
#define BINDING_CLUSTER_MAXN		8
#define BINDING_EP_DEF				1	// default endpoint for binding

#define NWK_MIN_DELAY_TIME_MS_AFTER_POWER_ON	(500)
#define RAND_MIN_NUM							(10 * 1000) //最小随机数,单位MS
#define RAND_MAX_NUM 							(40 * 1000 - RAND_MIN_NUM) //最大随机数

#define	MAX_PALOAD_LEN				128
typedef struct tag_bind_objector_st {
	uint16_t cluster;
	uint16_t attribute; //as heartbeat report when gataway isnot kongke
	uint8_t endpoint;
}BindObjSt;

// Network status.
typedef enum {
	ENS_LEAVED = 0,				// Leaved network.
	ENS_ONLINE, 			// Joined, and connected!
	ENS_JOINING, 			// Finding and Joining the network.
	ENS_OFFLINE, 		// Joined, but disconnect to its parent!
	ENS_UNKNOW,				// UNKNOW.
}NwkStatusEnum;

typedef enum {
	EJC_JOIN_FAILED = 0,	// Join network failed!!
	EJC_JOIN_SUCCEED,		// Join network succeed.
}NwkJoinCompltEnum;

typedef enum {
	NWK_STEERING_STATUS_FAILED = 0,	// Join network failed!!
	NWK_STEERING_STATUS_SUCCESS,		// Join network succeed.
}NwkSteeringStatusEnum;


typedef struct {
	uint16_t attribute;
	uint8_t  attri_type;
	uint8_t  value[MAX_PALOAD_LEN];
}ReportAttrData_ST;

#define Print_Ieee_Addr(x)   do{	\
								iKonkeAfSelfPrintBuffer(x,8);}while(0);


NwkStatusEnum kNwkGetCurrentStatus(void );

// Network status notify callback
typedef void (*pNetworkStatusNotifyCallback)(NwkStatusEnum );
// Joining procedure complete result callback
typedef void (*pJoinCompleteCallback)(NwkJoinCompltEnum );


typedef void (*pScheduleTaskHandlerCallback)(void);
//add by maozj 20200511
typedef void (*pNetworkExitCompleteCallback)(void);

void NetworkLeaveHandler(nlme_leave_cnf_t *pLeaveCnf);
bool JoinNetworkHandler(zdo_tc_join_ind_t *join_info);
kk_err_t kNwkScheduleTaskRegister(uint32_t TimeOut);
status_t General_Report(uint8_t src_ep,uint8_t des_ep,uint16_t cluster,uint16_t Attribute,uint8_t Data_Type,uint8_t *value);

void kNwkNetworkStatusNotifyCall(NwkStatusEnum nwkst );

extern void Set_Nwk_Steering_Status(NwkSteeringStatusEnum Status);

void Set2NwkJoiningCountdownCounter(u32 time_ms);

#define Report_OnOff(src_ep,value)    General_Report(src_ep,1,0x0006,0x0000,0x10,&value)

void kNwkGetGateWayEui64(addrExt_t eui64);
bool kNwkIsExiting(void);
uint32_t kGetRandNum(uint32_t minNum, uint32_t maxNum );
/*	DESP: Check whether the specified cluster is binded.
 * 	RETURN[KET_OK]: is binded!
 * 	RETURN[KET_ERR_NON_EXIST]: is unbinded!
 * 	RETURN[KET_ERR_NO_PERMISSIONS]: this binding item is not configured!
 * 	Auth: dingmz_frc.20191112.
 * */
kk_err_t kNwkClusterBindingObjIsOK(uint8_t endpoint, uint16_t cluster );
///*	DESP: Traverse print the cluster binding table content.
// * 	Auth: dingmz_frc.20191106.
// * */
kk_err_t kNwkClusterBindingTrigger(void );
/*	DESP: steering action trigger interface: eg. start or stop.
 * 	Note: The joining procedure will be triggered only when the network is not connected.
 * 	Auth: dingmz_frc.20190627.
 * */
void kNwkJoiningStart(uint32_t u32JoiningDuration/* MS */, pJoinCompleteCallback callback );


void kNwkModuleInit(BindObjSt cluster_obj_list[], uint8_t cluster_obj_num, pNetworkStatusNotifyCallback pNSCallback
		, pNetworkExitCompleteCallback pNECallback);

void kNwkFactoryReset(bool is_nwk_indicator);
#endif
