Table-driven programming in C Language (dynamic loading)

Source: Internet
Author: User

 

C-language table-Driven Dynamic Loading of Application Instances

At present, mobile terminals are becoming more and more powerful, and there are various customer needs. In addition, the same phone may be at different time and locations, requiring different phone functions. How can we use a remote method?

The modulation phone function has also become a difficult problem for various manufacturers. In this case, a method has emerged to modify terminal parameters through commands and data. How to Set and design company secrets is not disclosed here,

This article focuses on the description, a key technical application that implements this function, the table-driven Application in C and the linked list application.

To put it bluntly, prepare for the work first.

If the data to be processed is a command + data, the defined function is as follows:

Typedef NMCMD_RETURN (* COMMAND_FUNCTION) (C_UINT16 using _id, const C_CHAR * p_using _data, C_UINT8 data_len );

Specifically, the cmd_id command p_cmd_data indicates that the data data_len indicates the Data Length.

Define struct to expand the processing of different function modules. You can add function modules based on applications, such as encryption card processing, lock machine lock card processing, security protection processing, and billing processing.

/*-----------------------------------------------------------------------------

* File _ Data Type Definition

*-----------------------------------------------------------------------------*/

Typedef struct stnmcmd

{

Struct stnmcmd * pNext;/* linked list */

COMMAND_FUNCTION fProc;/* points to the next */

} Xnnm_1_proc;

 

Define global variables pointing to the linked list Header

Static xnnm_1_proc * m_nmcmdproc = C_PNULL;

 

 

Define the corresponding processing functions of different commands

// The ing structure between commands and command processing functions

Typedef struct

{

C_UINT16 sequence _id; // command

COMMAND_FUNCTION performance_fun; // command processing function

} NMCMD_FUNCTION;

 

 

Below are the basic framework functions to add module commands to the linked list.

/*---------------------------------------------------------------------------

* Function name: xnnm_loadproc

* Function Description: load the processor.

*

* Input parameter: COMMAND_FUNCTION fProc // Processor

* Output parameter: None

* Return value:

*

* Historical records:

* Modify the date log of a person

* 2010-7-9

*---------------------------------------------------------------------------*/

C_UINT16 xnnm_LoadProc (COMMAND_FUNCTION fProc)

{

Xnnm_1_proc * pNew = C_PNULL;

If (fProc = C_PNULL)

{

Return C_FALSE;

}

PNew = NM_MALLOC (sizeof (* pNew ));

If (pNew = C_PNULL)

{

Return C_FALSE;

}

Memset (pNew, 0x00, sizeof (* pNew ));

PNew-> fProc = fProc;

PNew-> pNext = m_nmcmdproc;

M_nmcmdproc = pNew;

Return C_TRUE;

}

 

Import commands and data for processing

/*---------------------------------------------------------------------------

* Function name: xnnm_ExecProc

* Function Description: Execution Processor

*

* Input parameters:

* Output parameters:

* Return value:

*

* Historical records:

* Modify the date log of a person

* 2010-7-9

*---------------------------------------------------------------------------*/

C_UINT16 xnnm_ExecProc (C_UINT16 rj_id, const C_CHAR * p_1__data, C_UINT8 data_len)

{

Xnnm_1_proc * pList = m_nmcmdproc;

While (pList)

{

If (pList-> fProc (using _id, p_using _data, data_len) = RET_NMCMD_SUCCESS)

{

Break;

}

PList = pList-> pNext;

}

Return C_TRUE;

}

 

 

The following describes some functions of the lock card to implement the command processing function application of the lock card module.

 

Lock pin code command processing function

Static NMCMD_RETURN nmcmd_lock_pin (C_UINT16 1__id, const C_CHAR * p_1__data, C_UINT8 data_len)

{

If (data_len <1) | (data_len> 2) | (XINO_IsNumStr (p_1__data, data_len) = C_FALSE ))

{

Return RET_NMCMD_INVALID_DATA;

}

// Process begin based on the data content

If (XINO_STR2INT (const C_CHAR *) p_1__data, data_len) = 0)

{

Locknv_enable_pin (0 );

}

Else

{

Locknv_enable_pin (1 );

}

// Process the end based on the data content

Return RET_NMCMD_SUCCESS;

}

 

/*-----------------------------------------------------------------------------*/

 

// Table of the command and command processing functions of the lock card function module

 

Static const NMCMD_FUNCTION m_acstcmd [] =

{

{ID_NMCMD_LOCK_PIN, nmcmd_lock_pin}, // modify the Pin code of the automatic lock upon startup.

{ID_NMCMD_ENABLE_RAND_PIN, nmcmd_enable_rand_pin}, // modify whether to enable random PIN codes.

{ID_NMCMD_SET_FIXED_PIN, nmcmd_fix_pin},/* set the fixed PIN code */

{ID_NMCMD_INIT_PIN, nmcmd_init_pin}, // modify the initial PIN code.

{ID_NMCMD_LOCK_IMSI, nmcmd_lock_imsi}, // modify the IMSI number automatically bound upon startup

{ID_NMCMD_CELL_LOCKED, nmcmd_cell_locked}, // modify whether to lock the CELL

{ID_NMCMD_CELL_AUTOINC, nmcmd_cell_autoinc}, // modify whether the CELL automatically grows.

{ID_NMCMD_CELL_LOCK_MAX, nmcmd_cell_lock_max}, // modify the maximum number of CELL locks.

{ID_NMCMD_CLEAN_CELL_LIST, nmcmd_clean_cell_list}, // clear the CELL lock list

{ID_NMCMD_CELL_LIST_TYPE, nmcmd_cell_list_type}, // lock the CELL type-blacklist and whitelist

{ID_NMCMD_LOCK_CELL_APPEND, nmcmd_lock_cell_append}, // Add the locked CELL

{ID_NMCMD_LAC_LOCKED, nmcmd_lac_locked}, // modify whether to lock the lAC

{ID_NMCMD_LAC_LIST_TYPE, nmcmd_lac_list_type}, // lock the lac type-blacklist and whitelist

{ID_NMCMD_LAC_AUTOINC, nmcmd_lac_autoinc}, // modify whether the lac automatically grows

{ID_NMCMD_LAC_LOCK_MAX, nmcmd_lac_lock_max}, // modify the maximum number of lac locks.

{ID_NMCMD_CLEAN_LAC_LIST, nmcmd_clean_lac_list}, // clear the lac lock list

{ID_NMCMD_LOCK_LAC_APPEND, nmcmd_lock_lac_append}, // Add the locked lac

/* Lock the carrier */

{ID_NMCMD_LOCK_SP_CLEAN, nmcmd_lock_sp_clean}, // lock the carrier table-clear

{ID_NMCMD_LOCK_SP_APPEND_MODIFY, nmcmd_lock_sp_append_modify}, // lock the carrier table-Add/modify

// {ID_NMCMD_LOCK_SP_DELETE, nmcmd_lock_sp_delete}, // lock the carrier table-delete

{C_PNULL, C_PNULL },

};

 

Among them, ID_NMCMD_LOCK_PIN ID_NMCMD_CLEAN_LAC_LIST and so on are defined commands.

Nmcmd_lock_pin nmcmd_enable_rand_pin is the processing function corresponding to the command.

 

/*-----------------------------------------------------------------------------

* Global _ function implementation

*-----------------------------------------------------------------------------*/

 

/*---------------------------------------------------------------------------

* Function name: locknm_handlercmd

* Function Description: The processor processes the lock card and finds the corresponding setting function based on the command.

*

* Input parameters:

* Output parameters:

* Return value:

*

* Historical records:

* Modify the date log of a person

* 2010-7-9

*---------------------------------------------------------------------------*/

NMCMD_RETURN locknm_handlercmd (C_UINT16 cmd_id, const char * p_cmd_data, C_UINT8 data_len)

{

NMCMD_RETURN bRet = RET_NMCMD_FAIL;

C_UINT16 nTableSize = sizeof (m_acstcmd)/sizeof (m_acstcmd [0]);

NMCMD_FUNCTION * pTable = m_acstcmd;

C_UINT16 I = 0;

XINO_TRACE_LOW ("[xjp] locknm_handlercmd enter ");

For (I = 0; I <nTableSize; I ++)

{

If (pTable [I]. Partition _id = partition _id) & (pTable [I]. Partition _fun! = C_PNULL ))

{

/* Processing successful */

PTable [I]. pai_fun (pai_id, p_pai_data, data_len );

BRet = RET_NMCMD_SUCCESS;

Break;

}

}

XINO_TRACE_LOW ("[xjp] locknm_handlercmd leave ");

Return bRet;

}

 

During initialization, call lock_init_proc to load the lock card command and run locknm_handlercmd to the linked list.

C_VOID lock_init_proc (C_VOID)

{

/* Load command processing */

Xnnm_LoadProc (locknm_handlercmd);/* load the boot processor */

 

}

 

Call xnnm_ExecProc (C_UINT16 cmd_id, const C_CHAR * p_cmd_data, C_UINT8 data_len) at the command receiving site for processing.

 

If a module is added

What you need to do is

1. New command and function ing table. In this example, It is static const NMCMD_FUNCTION m_acstcmd [] =

2. added the handler function based on the command. In this example, NMCMD_RETURN locknm_handlercmd (C_UINT16 using _id, const char * p_using _data, C_UINT8 data_len)

3. added the corresponding command processing function. In this example, It is static NMCMD_RETURN nmcmd_lock_pin (C_UINT16 1__id, const C_CHAR * p_1__data, C_UINT8 data_len)

4. Load the corresponding processing function according to the command in terminal initialization. In this example, C_VOID lock_init_proc (C_VOID)

 

 

Now the table-driven programming in C language is finished, and the dynamic loading of instances is finished. I hope it will be helpful to my friends. Please contact us.

 

From the column dainiao01

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.