⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appcore.cpp

📁 基于Nuleus操作系统和s3c4510的编写的EFC。已经包含了该EFC的设计说明。这是个实际产品的代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2003, Ulink Telecom Equipment Co., Ltd. All rights reserved.
//
// File:
//
//    APPCORE.CPP
//
// Abstract:
//
//    Implementation of the CRTApp class.  The CRTApp class is the base class
//    from which you derive an application object. An application object provides
//	  member functions for initializing your application and for  running the
//	  tasks. Each application can only contain one object derived from CRTApp.
//
// History:
//
//    V1.0	2003-03-06	Alex Duan	Original version.
//    V1.1	2003-06-25	Alex Duan	Module of RTOS moved to CRTObject class.
//    V1.2	2003-11-16	Alex Duan	Add a group of functions for profile saving
//									and restoring.
//	  V1.3	2006-09-11	Bozhong Xu	Support distribute system and sub system. 
//									Move initialize task from InitTask to InitInstance function.
//									
///////////////////////////////////////////////////////////////////////////////
#define GLOBALS

#include "APPCORE.H"
#include "rtcDriver.h"
#include "FlashFile.h"

///////////////////////////////////////////////////////////////////////////////
// external variables and functions
extern "C"
{
	// system reset mode (Software reset/Hardware reset)(defined in init.s)
	extern BYTE RESET_MODE;
	// initializes the library and calls the global object's constructors
	void __rt_lib_init(void);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// default file content of "System.ini"
//
//; System.ini
//; ==========
//;
//; This INI file contains information that is necessary for
//; firmware to support a device. The firmware reads this file
//; on startup and when the device changes. The format of the
//; contents may change at any time when the EFC updates.
//;
//; The entries have the following format, spaces can be added
//; before or after the equal sign:
//;
//; <Entry> = <Value>
//;
//; The [Device] section contains parameters of the whole device.
//; The [Message] section contains parameters of the communication.
//; The [Users] section contains parameters of system users.
//; The [Boards(m)] section contains parameters of one board.
//;   m(=0,1,...) is the index of the board in the device.
//; The [Interfaces(m,n)] section contains parameters of one interface.
//;   m(=0,1,...) is the index of the board;
//;   n(=0,1,...) is the index of the interface in the board.
//; Other sections can be added to this file too.
//; Last updated: 11/12/2003, Alex Duan

static LPCSTR s_pszSystem = "\
[Users]\r\n\
user1 = dvlp,dvlp,1\r\n\
user2 = support,support,2\r\n\
user3 = sundata,sundata,3\r\n\
user4 = ulinkcom,ulinkcom,4\r\n\
\r\n\
[Device]\r\n\
m_diInfo.strName = unit 1\r\n\
m_diInfo.aAddr.uAddrL = 1\r\n\
m_diInfo.aAddr.uAddrH = 0\r\n\
m_diInfo.aAddr.uGroup = 0\r\n\
m_diInfo.dMadeDate.uDay = 0\r\n\
m_diInfo.dMadeDate.uMonth = 0\r\n\
m_diInfo.dMadeDate.wYear = 0\r\n\
m_diInfo.strSerial =  \r\n\
m_diInfo.strProducer = Ulink Telecom Equipment Co., Ltd.\r\n\
m_ndDevice.strName = s3c4510\r\n\
m_ndDevice.auIP = 192.168.0.201\r\n\
m_ndDevice.auSubnet = 255.255.255.0\r\n\
m_ndDevice.auGateway = 192.168.0.1\r\n\
m_ndDevice.nPort = 5000\r\n\
m_ndDevice.auMac = 00-0B-9A-00-00-06\r\n\
\r\n\
[Message]\r\n\
SESSION::nTimeout = 0\r\n\
m_usUart[0].uPort = 0\r\n\
m_usUart[0].dwBaudRate = 9600\r\n\
m_usUart[0].uDataBits = 8\r\n\
m_usUart[0].uParity = 0\r\n\
m_usUart[0].uStopBits = 1\r\n\
m_usUart[1].uPort = 1\r\n\
m_usUart[1].dwBaudRate = 57600\r\n\
m_usUart[1].uDataBits = 8\r\n\
m_usUart[1].uParity = 0\r\n\
m_usUart[1].uStopBits = 1\r\n\
";
///////////////////////////////////////////////////////////////////////////////
// CProfileEntry

CProfileEntry::CProfileEntry()
{
	nReadRights  = ACCESS_LEVEL_READONLY;
	nWriteRights = ACCESS_LEVEL_GENERAL;
	pNext = NULL;
}

CProfileEntry::~CProfileEntry()
{
	if (pNext != NULL)
	{
		delete pNext;
		pNext = NULL;
	}
}

///////////////////////////////////////////////////////////////////////////////
// CProfileSection
CProfileSection::CProfileSection()
{
	nReadRights  = ACCESS_LEVEL_READONLY;
	nWriteRights = ACCESS_LEVEL_GENERAL;
	pEntryHeader = NULL;
	pNext  = NULL;
}

CProfileSection::~CProfileSection()
{
	if (pEntryHeader != NULL)
	{
		delete pEntryHeader;
		pEntryHeader = NULL;
	}
	if (pNext != NULL)
	{
		delete pNext;
		pNext = NULL;
	}
}

///////////////////////////////////////////////////////////////////////////////
// CRTApp

IMPLEMENT_DYNAMIC(CRTApp, CRTObject)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRTApp* CRTApp::ms_pInstance = NULL;
CRTApp::CRTApp(DWORD dwPoolSize, DWORD dwMinAlloc)
{
	ASSERT_VALID(this);
	ASSERT(ms_pInstance == NULL); // insure only one instance

	ms_pInstance = this;
	m_dwPoolSize = dwPoolSize;
	m_dwMinAlloc = dwMinAlloc;
	m_pcdDevice	 = NULL;
#ifdef __EFC_MASTER // Added by xbz	
	m_pcmMessage = NULL;	
	m_pceEventLog= NULL;
	m_pcuUser	 = NULL;
#endif
	m_bModified	 = FALSE;
	m_pNextMemory= NULL;
	m_pSectionHeader = NULL;

	m_bDistributed = FALSE;
	m_bSubsystem = FALSE;
	m_bWatchdog = TRUE;
	m_bRTC = TRUE;	
}

///////////////////////////////////////////////////////////////////////////////
// Remarks:
//		This destructor will not be called in real-time system, so if you are
//		sensitive the size of the code, you can comment out the lines inside
//		this function. I write these codes only to keep integrality of class.
CRTApp::~CRTApp()
{
	// free memory of system default objects
	if (m_pcdDevice) 
		delete m_pcdDevice;
#ifdef __EFC_MASTER // Added by xbz
	if (m_pcuUser)
		delete m_pcuUser;
	if (m_pceEventLog) 
		delete m_pceEventLog;	
	if (m_pcmMessage) 
		delete m_pcmMessage;
#endif
	if (m_pSectionHeader)
		delete m_pSectionHeader;
}

///////////////////////////////////////////////////////////////////////////////
// Application entry point

void Application_Initialize(void* pMemory)
{
#if !defined(WIN32)
	__rt_lib_init();
#endif // !defined(WIN32)

	ASSERT(GetApp());
	ASSERT(GetApp()->m_dwPoolSize);
	ASSERT(GetApp()->m_dwMinAlloc);

	//
	// create memory pool for the application
	//
	VERIFY(NU_Create_Memory_Pool(&System_Memory, (LPSTR)"SystemMemory", 
		pMemory, GetApp()->m_dwPoolSize, GetApp()->m_dwMinAlloc, 
		NU_FIFO) == NU_SUCCESS);

	pMemory = (BYTE*)pMemory + GetApp()->m_dwPoolSize;
	GetApp()->m_pNextMemory = pMemory; // Added by Bozhong Xu

	if (!GetApp()->IsSubSystem()) // Added by Bozhong Xu
	{
		//
		// create memory pool for the network
		//
		NU_MEMORY_POOL NetMemory;
		VERIFY(NU_Create_Memory_Pool(&NetMemory, (LPSTR)"NetMemory",
			pMemory, NU_NET_BUFFER_POOL_SIZE, 50, NU_FIFO) == NU_SUCCESS);
		
		GetApp()->m_pNextMemory = (BYTE*)pMemory + NU_NET_BUFFER_POOL_SIZE;
		
		// call network initialization
		VERIFY(NU_Init_Net(&NetMemory) == NU_SUCCESS);
	}	

	// create application initialization task.
	VERIFY(GetApp()->CreateTask("InitTask", (PFNTASK)&CRTApp::InitTask));
}

// This task is used to init the system only.
void CRTApp::InitTask(DWORD argc, void *argv)
{
	UNUSED_ARG(argc, argv);	
	
	// Set file section name
	SetSectionString("App");
	
	if (m_bWatchdog)
	{
		// reset watchdog
		SetBitVal(IOPDATA, IO_WATCHDOG, !GetBitVal(IOPDATA, IO_WATCHDOG));
	}

	// Create a event for saving data
	m_evSave = CreateEventGroup("Event_Save");
	ASSERT(m_evSave);

	
	if (m_bRTC)
	{
		// (1) Initializes the Real-Time Clock
		rtcInit();
		
		if (m_bWatchdog)
		{
			// reset watchdog
			SetBitVal(IOPDATA, IO_WATCHDOG, !GetBitVal(IOPDATA, IO_WATCHDOG));	
		}
	}

	// (2) Creates device and startup watchdog timer
	OnCreateDevice();
	if (m_bWatchdog)
		VERIFY(m_pcdDevice->OnWatchdogTimer());

	// It it is not a sub system, we should create CUser, CEventLog,
	// and CMessage object
#ifdef __EFC_MASTER // Added by xbz
	if (!m_bSubsystem)
	{		
		OnCreateEventLog();		
		OnCreateUser();
		OnCreateMessage();
	}	
#endif
	// (3) Load system parameters
	CFlashFile file;
	if (!file.Open("System.ini", CFile::modeRead)) // modeRead: do not save
	{// if file not found, then default file text been read
		LPCSTR lpszBase = CRTApp::GetDefaultConfig(); // Parent class's config
		LPCSTR lpszDerived = GetDefaultConfig();	  // Child class's config
		file.WriteLine(lpszBase);
		if (lpszDerived != lpszBase)
		{
			file.WriteLine(lpszDerived);
		}
		file.SeekToBegin();
	}
	LoadFromFile(file);
	file.Close();

	// (4) call system object's init function.
#ifdef __EFC_MASTER // Added by xbz	
	if (m_pcmMessage)
		m_pcmMessage->OnInitMessage();
#endif	
	VERIFY(m_pcdDevice->OnInitDevice());

	// (5) Creates a task saving configuration to flash
	VERIFY(CreateTask("FileTask", (PFNTASK)&CRTApp::FileTask));
	
	// (6) Adds the system reset event	
//	BYTE pReset;
//	memcpy(&pReset,&RESET_MODE,1);
//	BYTE auParam[5] = {EVENT_SYSTEM_RESET, pReset, 1, 0, 0};
//	m_pcdDevice->OnAddEventToTable(auParam);
	RESET_MODE = 1; // 0: Hardware reset, 1: software reset
//	AddEvent(EVENT_SYSTEM_RESET, &RESET_MODE, 1);

	// Calls initialization
	VERIFY(InitInstance());
	
	// Deletes the init task
	VERIFY(DeleteTask("InitTask"));
}

// Creates the system CDevice object
void CRTApp::OnCreateDevice()
{
	ASSERT(m_pcdDevice == NULL);		
	m_pcdDevice = new CDevice();
	VERIFY(m_pcdDevice);
}

#ifdef __EFC_MASTER // Added by xbz
// Creates the system CEventLog object
void CRTApp::OnCreateEventLog()
{
	ASSERT(m_pceEventLog == NULL);
	m_pceEventLog = new CEventLog();
	VERIFY(m_pceEventLog);
}

// Creates the system CUser object
void CRTApp::OnCreateUser()
{
	ASSERT(m_pcuUser == NULL);
	m_pcuUser = new CUser();
	VERIFY(m_pcuUser);
}

// Creates the system CMessage object
void CRTApp::OnCreateMessage()
{
	ASSERT(m_pcmMessage == NULL);
	m_pcmMessage = new CMessage();
	VERIFY(m_pcmMessage);
}
#endif

// Set Save flags
void CRTApp::SetModifiedFlag(BOOL bModified)
{ 
	m_bModified = bModified; 

	if (bModified)
		NU_Set_Events(m_evSave, 0x01, NU_OR);
}

// Task used to save configuration file & events
void CRTApp::FileTask(DWORD argc, void *argv)
{
	UNUSED_ARG(argc, argv);
	CFlashFile file;
	UNSIGNED flags;
	while (1) 
	{
		if (NU_Retrieve_Events(m_evSave, 0x01, NU_OR_CONSUME, 
			&flags, NU_SUSPEND) == NU_SUCCESS)		
		{
			file.Open("System.ini", CFile::modeCreate);
			file.SeekToBegin();
			SaveToFile(file);
			file.Close(); // save to flash
			SetModifiedFlag(FALSE);
		}
	}
}

// read data from file
BOOL CRTApp::LoadFromFile(CFile &file, int nRights)
{
	if (nRights == 0)
		return FALSE; // no rights to load

	CString strLine, strUnit;
	CProfileSection *pSection = NULL;
	CProfileEntry *pEntry = NULL;
	int pos;

//	BOOL bAppFile = FALSE;//Added by bella yu 2005/01/25
	
	while (file.ReadLine(strLine))
	{
/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -