API Application Example

API Header file

#ifndef	__KEY_BACK_LIGHT_H__
#define	__KEY_BACK_LIGHT_H__

#include	<stdint.h>		/* uint??_t */
#include	<stdbool.h>		/* Bool */
/***************************************************************************/
/*	For Key Pad Back Light
*/
/***************************************************************************/
//	#define	DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE

#define	DEVICE_LED_KEY_BACK_LIGHT	"/sys/class/leds/key_light/brightness"


/*	Key Back Light On/Off
Input
	true = Key Back Light On
	false= Key Back Light Off
Return
	0	= Ok to control(On/Off)
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
extern	int	key_back_light(bool Switch/*true=on, false=Off*/);

/*	get Key Back Light state
Return
	0	= Ok to read state
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation

*pIsOn	: true = Key Back Light is On, false = Key Back Light is Off
*/
extern	int key_back_light_is_on(bool *pIsOn);

#endif	/* __KEY_BACK_LIGHT_H__ */

API Source File

#include	<stdio.h>
#include	<stdint.h>		/* uint??_t */
#include	<stdbool.h>		/* bool */
#include	<stdlib.h>		/* strtoul */
#include	<string.h>		/* for memset */
#include	<unistd.h>		/* for open/close .. */
#include	<fcntl.h>		/* for O_RDWR */
#include	<sys/ioctl.h>	/* for ioctl */
#include	<sys/fcntl.h>	/* for O_RDWR */

#include	"KeyBackLight.h"

/*	Key Back Light On/Off
Input
	true = Key Back Light On
	false= Key Back Light Off
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
int	key_back_light(bool Switch/*true=on, false=Off*/)
{
	FILE *	pDeviceFile;
	char	pString[2] = { 0, 0 };
	int		StringLength;

	pDeviceFile = fopen ( DEVICE_LED_KEY_BACK_LIGHT, "w+");
	if ( pDeviceFile == (FILE *)0 )
	{
#ifdef	DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE
		printf ( "Failed To Access Key Pad Back Light Device : %s\n", DEVICE_LED_KEY_BACK_LIGHT );
#endif	/* DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE */
		return	-2;
	}

	if ( Switch == true )
	{
		pString[0] = '1';
	}
	else
	{
		pString[0] = '0';
	}
	StringLength = (int)strlen(pString);
	if ( fwrite ( pString, StringLength, 1, pDeviceFile ) < 0 )
	{
#ifdef	DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE
		printf ( "Failed To Control Key Pad Back Light\n" );
#endif	/* DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE */
		return	-3;
	}

	fclose ( pDeviceFile );

	return	0;
}


/*	get Key Back Light state
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation

*pIsOn	: true = Key Back Light is On, false = Key Back Light is Off
*/
int key_back_light_is_on(bool *pIsOn)
{
	FILE *	pDeviceFile;
	char	pCommand[128] = {0};
	char	pString[10] = {0};
	int		Value;

	if ( pIsOn == (bool *)0 )
	{
#ifdef	DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE
		printf( "key_back_light_is_on() : Input Parameter Error\n" );
#endif	/* DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE */
		return	-1;
	}

	sprintf(pCommand, "cat %s", DEVICE_LED_KEY_BACK_LIGHT);
	pDeviceFile = popen(pCommand, "r");
	if ( pDeviceFile == (FILE *)0 )
	{
#ifdef	DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE
		printf ( "Failed To Access Key Pad Back Light Device : %s\n", DEVICE_LED_KEY_BACK_LIGHT );
#endif	/* DEVICE_KEY_BACK_LIGHT_DEBUG_MESSAGE */
		return	-2;
	}

	while( fgets (pString, 10, pDeviceFile) );
	Value = (uint32_t) strtoul( pString, NULL, 10 );
	fclose (pDeviceFile);

	*pIsOn = ((Value == 0) ? false:true);

	return	0;
}