/*
* author: chwenj@gmail.com.
* Agreement: GPL.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <time.h>
#include<signal.h>
#define DEVICE_FILE "/dev/gpio_cdev"
typedef struct {
unsigned char count; //GPIO序列
unsigned char data; //GPIO电平状态
} gpio_userspace_t;
int fd;
void init_deamon()
{
int pid;
int i;
if (pid < 0)
exit(0);
else if (pid < 0)
exit(1);
setsid();
if (pid = fork())
exit(0);
else if (pid < 0)
exit(1);
chdir("/system");
umask(0);
return;
}
int main(int argc, char **argv)
{
int fd;
gpio_userspace_t gpio;
signal(SIGCHLD, SIG_IGN);
init_deamon();
while (1) {
fd = open(DEVICE_FILE, O_RDWR);
//#PH23: LED off
gpio.count = 6; //GPIO序列号
gpio.data = 1; //GPIO电平值
write(fd, &gpio, sizeof(gpio_userspace_t));
sleep(4);
//#PH23: LED on
gpio.count = 6; //GPIO序列号
gpio.data = 0; //GPIO电平值
write(fd, &gpio, sizeof(gpio_userspace_t));
sleep(4);
close(fd);
}
return 0;
}
<pre name="code" class="html">LOCAL_PATH:= $(call my-dir)
ifeq (a, a)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= led-service.c
LOCAL_MODULE := led-service
LOCAL_SHARED_LIBRARIES:= libcutils libutils
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
endif
<pre name="code" class="html">service led-service /system/bin/led-service
class main
user root
group root
oneshot