/**********************************************************************
 * INCLUDES
 */
#include "ikk-token.h"




extern addrExt_t g_Eui64GatewayAddr;


/*********************************************************************
 * @fn      Get Gateway Addr
 *
 * @brief
 *
 * @param   None
 *
 * @return
 */
uint8_t* Gateway_IEEE_Addr_Get(void)
{
	nv_sts_t st = NV_SUCC;

	st = nv_flashReadNew(1, NV_MODULE_APP,  NV_GATEWAY_ADDR, sizeof(addrExt_t), (u8*)g_Eui64GatewayAddr);
	iKonkeAfSelfPrint("flashRead:::\r\n");
	DEBUG_ARRAY(1,g_Eui64GatewayAddr,sizeof(addrExt_t));
	if(st != NV_SUCC){
		memset(g_Eui64GatewayAddr,0xff,sizeof(addrExt_t));
	}
	return g_Eui64GatewayAddr;
}
/*********************************************************************
 * @fn      Save Gateway Addr
 *
 * @brief
 *
 * @param   None
 *
 * @return
 */
nv_sts_t Gateway_IEEE_Addr_Save(addrExt_t addr)
{
	nv_sts_t st = NV_SUCC;

	addrExt_t temp_addr;
	st = nv_flashReadNew(1, NV_MODULE_APP,  NV_GATEWAY_ADDR, sizeof(addrExt_t), (u8*)temp_addr);
	iKonkeAfSelfPrint("flashRead:::temp_addr\r\n");
	DEBUG_ARRAY(1,temp_addr,sizeof(addrExt_t));
	if(st == NV_SUCC){
		if(memcmp(temp_addr,addr,sizeof(addrExt_t)))
			st = nv_flashWriteNew(1, NV_MODULE_APP, NV_GATEWAY_ADDR, sizeof(addrExt_t), (u8*)addr);
	}else if(st == NV_ITEM_NOT_FOUND){
		st = nv_flashWriteNew(1, NV_MODULE_APP, NV_GATEWAY_ADDR, sizeof(addrExt_t), (u8*)addr);
	}
	iKonkeAfSelfPrint("Gateway_IEEE_Addr_Save result = %d\r\n",st);
	return st;
}
/*********************************************************************
 * @fn      Get MFG CODE
 *
 * @brief
 *
 * @param   None
 *
 * @return
 */
uint16_t MFG_Code_Get(void)
{
	nv_sts_t st = NV_SUCC;
	uint16_t mfg_code_temp;
	st = nv_flashReadNew(1, NV_MODULE_APP,  NV_MFG_CODE, sizeof(mfg_code_temp), (u8*)&mfg_code_temp);
	if(st != NV_SUCC){
		mfg_code_temp = 0;
	}
	return mfg_code_temp;
}
/*********************************************************************
 * @fn      Save mfg_code
 *
 * @brief
 *
 * @param   None
 *
 * @return
 */
nv_sts_t MFG_Code_Save(uint16_t mfgcode)
{
	nv_sts_t st = NV_SUCC;

	uint16_t mfg_code_temp;
	st = nv_flashReadNew(1, NV_MODULE_APP,  NV_MFG_CODE, sizeof(mfg_code_temp), (u8*)&mfg_code_temp);
	if(st == NV_SUCC){
		if(mfg_code_temp != mfgcode)
			st = nv_flashWriteNew(1, NV_MODULE_APP, NV_MFG_CODE, sizeof(uint16_t), (u8*)&mfgcode);
	}else if(st == NV_ITEM_NOT_FOUND){
		st = nv_flashWriteNew(1, NV_MODULE_APP, NV_MFG_CODE, sizeof(uint16_t), (u8*)&mfgcode);
	}
	iKonkeAfSelfPrint("MFG_CODE_Save result = %d\r\n",st);
	return st;
}
void falsh_test(void)
{
	addrExt_t addr;
	memcpy(addr,Gateway_IEEE_Addr_Get(),sizeof(addrExt_t));
	iKonkeAfSelfPrint("flashRead11111:::\r\n");
	DEBUG_ARRAY(1,addr,sizeof(addrExt_t));
	addr[0] = 11;
	addr[1] = 22;
	Gateway_IEEE_Addr_Save(addr);
	iKonkeAfSelfPrint("flashRead22222:::\r\n");
	DEBUG_ARRAY(1,addr,sizeof(addrExt_t));
}


