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

📄 atbpb.c

📁 GSM手机设计软件代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*******************************************************************************

					CONDAT (UK)

********************************************************************************                                                                              

 This software product is the property of Condat (UK) Ltd and may not be
 disclosed to any third party without the express permission of the owner.                                 
                                                                              
********************************************************************************

 $Project name:	                                                      
 $Project code:	                                                           
 $Module:		
 $File:		    ATBPb.c
 $Revision:		                                                      
                                                                              
 $Author:		Condat(UK)                                                         
 $Date:		                                                          
                                                                               
********************************************************************************
                                                                              
 Description:
    
                        
********************************************************************************

 $History: ATBPb.c
	
	   
 $End

*******************************************************************************/

#if defined (NEW_FRAME)

#include "typedefs.h"
#include "vsi.h"
#include "pei.h"
#include "custom.h"
#include "gsm.h"

#else

#include "stddefs.h"
#include "custom.h"
#include "gsm.h"
#include "vsi.h"

#endif

#include <stdio.h>
#include <string.h>

#include "mfw_mfw.h"
#include "mfw_sys.h"

#include "cus_aci.h"
#include "p_sim.h"
#include "pcm.h"

#include "ATBPb.h"
#include "ATBPb_i.h"

/* Global data for phonebook */

static T_PB_DATA *phonebook[PB_PHONEBOOKS_MAX] = {0};


/*******************************************************************************

 $Function:    	ATB_pb_GetPhonebook
 
 $Description:	Checks to see which file system is selected

 $Returns:		PB_OK					Action completed OK.
				PB_FILEWRITEFAIL		File write encountered an error

 $Arguments:	phonebook_id	The phonebook identifier
 				current_type	Place to store type of phonebook selected.
 
*******************************************************************************/

PB_RET	ATB_pb_GetPhonebook(SHORT phonebook_id, T_PB_TYPE *current_type)
{
	PB_RET 		result;
	T_PB_DATA	*data	= ATB_hnd_GetPbData(phonebook_id);

	tracefunction("ATB_pb_GetPhonebook");

	if (data)
	{
		*current_type = data->current_type;
		return PB_OK;
	}
		
	result = FS_pb_GetPhonebook(phonebook_id, current_type);

	return result;
}


/*******************************************************************************

 $Function:    	ATB_pb_SetPhonebook
 
 $Description:	Select file system

 $Returns:		PB_OK					Action completed OK.
				PB_FILEREADFAIL			File read encountered an error

 $Arguments:	phonebook_id	The phonebook identifier
 				current_type	Type of phonebook selected.
 
*******************************************************************************/

PB_RET	ATB_pb_SetPhonebook(SHORT phonebook_id, T_PB_TYPE current_type)
{
	PB_RET 		result;
	T_PB_DATA	*data	= ATB_hnd_GetPbData(phonebook_id);

	tracefunction("ATB_pb_SetPhonebook");
	
	if (data)
		data->current_type = current_type;
	
	result = FS_pb_SetPhonebook(phonebook_id, current_type);

	return result;
}

		
/*******************************************************************************

 $Function:    	ATB_pb_Initialise
 
 $Description:	Initialise an instance of phonebook.  Dynamically allocates a record
 				cache in RAM.  Creates the necessary file(s) if they do not already exist.
 				Sorts the phonebook by name and by number, creating the appropriate
 				index tables.
				IMPORTANT: if the file reading is non-blocking - i.e. if a request to read
				a file does not return with an answer straight away, but the system
				awaits a callback - then "cache_max" must equal "records_max".  This
				ensures that all records are stored in RAM and can be retrieved without
				waiting for a response.  If the file reading is blocking, then cache_max
				can be smaller than records_max.

				The memory allocated by this operation will not be freed until
				ATB_pb_Exit() is called.

 $Returns:		PB_OK					Action completed OK.
				PB_EXCT					Action currently executing, callback awaited.
										GI_pb_OK will be called if successful,
										GI_pb_Error otherwise.
				PB_BUSY					Failed, phonebook is busy.
				PB_FILEREADFAIL			File read encountered an error
				PB_FILEWRITEFAIL		File write encountered an error
				PB_BOOKALREADYEXISTS 	Tried to initialise a phonebook that already
										exists

 $Arguments:	phonebook_id	The phonebook identifier
				type			Type of phonebook.
				records_max		Indicates the maximum number of entries the
								phonebook can hold.
				cache_max		Indicates the maximum number of records that the
								PB will store concurrently in RAM.
				alpha_max		Maximum size of unicode alpha tag in characters
				number_max		Maximum size of phone number in digits
				ext_max			Maximum size of extended data in bytes
								
*******************************************************************************/

PB_RET	ATB_pb_Initialise(SHORT phonebook_id, T_PB_TYPE type, SHORT records_max, SHORT cache_max,
	SHORT alpha_max, SHORT number_max, SHORT ext_max)
{
	T_PB_DATA *data;
	
	tracefunction("ATB_pb_Initialise()");
	
	/* Check to see if this phonebook has already been allocated */
	
	if (ATB_hnd_GetPbData(phonebook_id)!=NULL)
	{
		trace("* ERROR * - Phonebook already exists");
		GI_pb_Error(phonebook_id, PB_INITIALISE, PB_BOOKALREADYEXISTS);
		return PB_BOOKALREADYEXISTS;
	}

	/* Allocate memory for phonebook data */

	data = (T_PB_DATA *)GI_pb_MemAlloc(sizeof(T_PB_DATA));
	ATB_hnd_SetPbData(phonebook_id, data);
	
	data->records_max = records_max;
	data->records_used = 0;
	data->search_results = 0;

	data->alpha_max = alpha_max;
	data->number_max = number_max;
	data->ext_max = ext_max;
	
	/* Allocate memory for index tables */
	
	data->name_table = (SHORT *)GI_pb_MemAlloc(records_max*sizeof(SHORT));
	memset(data->name_table, 0, records_max*sizeof(SHORT));
	data->number_table = (SHORT *)GI_pb_MemAlloc(records_max*sizeof(SHORT));
	memset(data->number_table, 0, records_max*sizeof(SHORT));
	data->search_table = (SHORT *)GI_pb_MemAlloc(records_max*sizeof(SHORT));
	memset(data->search_table, 0, records_max*sizeof(SHORT));
	data->in_memory = (SHORT *)GI_pb_MemAlloc(records_max*sizeof(SHORT));
	memset(data->in_memory, PB_EMPTY_RECORD, records_max*sizeof(SHORT));

	/* Allocate memory for cache */

	data->cache_max = cache_max;
	data->cache_size = 0;
	data->cache	= (T_PB_RECORD **)GI_pb_MemAlloc(records_max*sizeof(T_PB_RECORD *));
	memset(data->cache, 0, records_max*sizeof(T_PB_RECORD *));
	
	/* Set up command */
	
	data->command_id = PB_INITIALISE;
	data->status	= PB_STATUS_INIT;
	data->param.Initialise.type = type;
	data->param.Initialise.records_max = records_max;
	
	return ATB_status_Initialise(phonebook_id);
}

/* Status function for Initialise */

PB_RET ATB_status_Initialise(SHORT phonebook_id)
{
	T_PB_DATA		*data		= ATB_hnd_GetPbData(phonebook_id);
	T_PB_INITIALISE	*Initialise;
	SHORT				blocking;
	PB_RET			result;

	/* Ensure that phonebook exists */
	if (!data)
	{
		trace("**ERROR** Phonebook does not exist");
		return PB_BOOKDOESNOTEXIST;
	}
	
	Initialise = &data->param.Initialise;
	blocking = TRUE;
	while (blocking)
	{
		switch(data->status)
		{
			case PB_STATUS_INIT:
				trace("ATB_status_Initialise: INIT");
				result = FS_pb_Initialise(phonebook_id, Initialise->type, Initialise->records_max,
					data->alpha_max, data->number_max, data->ext_max);
				
				/* Select next state */
				
				data->status = PB_STATUS_EXEC;
				Initialise->phys_index = 0;
				Initialise->first_record = TRUE;
				Initialise->record = ATB_pb_AllocRec(phonebook_id);
				
				/* Allow exit from function */
				
				if (result!=PB_OK)
					blocking = FALSE;
				break;

			case PB_STATUS_EXEC:
				trace("ATB_status_Initialise: EXEC");

				/* Try to read in index tables from file system.  If we can't find them, the
				 * records will be read in and sorted. */

				if (Initialise->first_record)
				{
					result = FS_pb_ReadTables(phonebook_id, &data->records_used, data->name_table, data->number_table);
					if (result==PB_OK)
					{
						/* Escape to PB_STATUS_COMPLETE */
						result = PB_OK;
						data->status = PB_STATUS_COMPLETE;
						break;
					}
				}
				/* If we've already read a record, and it's an existing record, add it to the
				 * index tables and to the cache */
				 
				else
				{	
					if (Initialise->record->alpha.data[0]!=(USHORT)0xFFFF)
					{
						ATB_mem_UpdateCache(phonebook_id, Initialise->phys_index, Initialise->record);
						ATB_index_AddRec(phonebook_id, INDEX_NAME, Initialise->phys_index, Initialise->record, NULL);
						ATB_index_AddRec(phonebook_id, INDEX_NUMBER, Initialise->phys_index, Initialise->record, NULL);
						data->records_used++;
					}

					Initialise->phys_index++;
				}

				/* Start processing after first record is read */
				
				Initialise->first_record = FALSE;
				
				/* If we haven't just read the last record, read the next one */
				
				if (Initialise->phys_index<data->records_max)
				{
					result = FS_pb_ReadRec(phonebook_id, Initialise->phys_index, Initialise->record);
				}
				else
				{
					/* Set next state as finished */
					result = PB_OK;
					data->status = PB_STATUS_COMPLETE;
				}


				/* Allow exit from function */
				
				if (result!=PB_OK)
					blocking = FALSE;
				break;

			case PB_STATUS_COMPLETE:
				trace("ATB_status_Initialise: COMPLETE");
				blocking = FALSE;

				/* Close file */

				FS_pb_Finished(phonebook_id);
				FS_pb_WriteTables(phonebook_id, data->records_used, data->name_table, data->number_table);
				
				/* Free the allocated record */

				ATB_pb_FreeRec(phonebook_id, Initialise->record);
				
				/* Notify the GI of success */
				GI_pb_OK(phonebook_id, data->command_id, NULL);
				result = PB_OK;
				data->status = PB_STATUS_NONE;
				break;
		}
	}

	return result;
}


/*******************************************************************************

 $Function:    	ATB_pb_Exit
 
 $Description:	Frees memory associated with phonebook.  To be called when the
 				phonebook is no longer required, or at shutdown.

 $Returns:		PB_OK		Action completed OK.

 $Arguments:	phonebook_id	The phonebook identifier
 
*******************************************************************************/

PB_RET ATB_pb_Exit(SHORT phonebook_id)
{
	T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
	SHORT phys_index;

	trace("ATB_pb_Exit");

	/* Ensure that phonebook exists */
	if (!data)
	{
		trace("**ERROR** Phonebook does not exist");
		return PB_BOOKDOESNOTEXIST;
	}
	
	for (phys_index = 0; phys_index<data->records_max; phys_index++)
	{
		if (data->cache[phys_index]!=NULL)
			ATB_pb_FreeRec(phonebook_id, data->cache[phys_index]);
	}

	GI_pb_MemFree((UBYTE *)data->cache, data->records_max*sizeof(T_PB_RECORD *));
	GI_pb_MemFree((UBYTE *)data->in_memory, data->records_max*sizeof(SHORT));
	GI_pb_MemFree((UBYTE *)data->search_table, data->records_max*sizeof(SHORT));
	GI_pb_MemFree((UBYTE *)data->number_table, data->records_max*sizeof(SHORT));
	GI_pb_MemFree((UBYTE *)data->name_table, data->records_max*sizeof(SHORT));
	
	GI_pb_MemFree((UBYTE *)data, sizeof(T_PB_DATA));

	ATB_hnd_SetPbData(phonebook_id, NULL);	
	return PB_OK;
}


/*******************************************************************************

 $Function:    	ATB_pb_Status
 
 $Description:	Returns PB_OK if the phonebook is ready for a new command,
 				or PB_BUSY if the phonebook is busy.

 $Returns:		PB_OK		All tasks completed, phonebook ready.
 				PB_BUSY		Commands currently executing.

 $Arguments:	phonebook_id	The phonebook identifier
 
*******************************************************************************/

PB_RET	ATB_pb_Status(SHORT phonebook_id)
{
	T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);
	PB_RET	result;

	/* Ensure that phonebook exists */
	if (!data)
	{
		trace("**ERROR** Phonebook does not exist");
		return PB_BOOKDOESNOTEXIST;
	}
	
	if (data->status==PB_STATUS_NONE)
		result = PB_OK;
	else
		result = PB_BUSY;
	
	return result;
}


/*******************************************************************************

 $Function:    	ATB_pb_Info
 
 $Description:	Returns information about the phonebook, which will be returned
 				in the data structure pointed to by phonebook_info.  The caller must
 				allocate the T_PB_INFO structure.

 $Returns:		PB_OK		Action completed OK.

 $Arguments:	phonebook_id	The phonebook identifier
				phonebook_info	Pointer to data structure to contain phonebook
								information (allocated by caller).
 
*******************************************************************************/

PB_RET	ATB_pb_Info(SHORT phonebook_id, T_PB_INFO *phonebook_info)
{
	T_PB_DATA *data = ATB_hnd_GetPbData(phonebook_id);

	/* Ensure that phonebook exists */
	if (!data)
	{
		trace("**ERROR** Phonebook does not exist");
		return PB_BOOKDOESNOTEXIST;
	}

⌨️ 快捷键说明

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