app.c
1 /*********************************************************************************
2 * Copyright: (C) 2015 guanlei
3 * All rights reserved.
4 *
5 * Filename: ledapp.c
6 * Description: This file ledapp.c
7 *
8 * Version: 1.0.0(2015年11月04日)
9 * Author: guanlei <[email protected]>
10 * ChangeLog: 1, Release initial version on "2015年11月04日 10時42分39秒"
11 *
12
13 ********************************************************************************/
14
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <errno.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #define PLATDRV_MAGIC 0x60
25 #define LED_OFF _IO (PLATDRV_MAGIC, 0x18)
26 #define LED_ON _IO (PLATDRV_MAGIC, 0x19)
27 #define LED_BLINK _IO (PLATDRV_MAGIC, 0x1A)
28 #define DEVNAMELEN 10
29
30 /********************************************************************************
31 * Description:
32 * Input Args:
33 * Output Args:
34 * Return Value:
35
36 ********************************************************************************/
37
38
39 int main (int argc, char **argv)
40 {
41 int fd;
42 int led_num;
43 int led_cmd;
44 char dev_name[DEVNAMELEN]={0};
45
46
47 if(argc<3)
48 {
49 printf("the correct form is ./a.out 0|1|2|3|(choose led ) 0(ON)|1(OFF)|2(BLINK)");
50 return -1;
51 }
52
53
54 snprintf(dev_name,sizeof(dev_name),"/dev/led");
55 fd=open(dev_name,O_RDWR);
56 if(fd<0)
57 {
58 printf("open file %s deflout,%s",dev_name,strerror(errno));
59 return -2;
60 }
61
62 led_num=atoi(argv[1]);
63 led_cmd=atoi(argv[2]);
64
65 switch(led_cmd)
66 {
67 case 0: ioctl(fd,LED_ON,led_num); break;
68 case 1: ioctl(fd,LED_OFF,led_num); break;
69 case 2: ioctl(fd,LED_BLINK,led_num); break;
70 default : printf("it is a bad number\n");
71
72 }
73 close(fd);
74
75
76 return 0;
77 } /* ----- End of main() ----- */
>: tftp -gr ledapp 192.168.1.110
ledapp 100% |*******************************| 6037 0:00:00 ETA
>: ls
apps dev init lib proc sbin usr
bin etc led.ko linuxrc psb(2).jpg sys var
data info ledapp mnt root tmp
>: ./led
led.ko ledapp
>: ./ledapp 0 2
-sh: ./ledapp: Permission denied
>: ./ledapp 0 1
-sh: ./ledapp: Permission denied
>: chmod 777 ledapp
>: ./ledapp 0 1
>: ./ledapp 0 2
>: ./ledapp 3 1
>: ./ledapp 3 2
>: ./ledapp 2 2
>: ./ledapp 1 2
>: ./ledapp 1 4
it is a bad number
>: ./ledapp 1
the correct form is ./a.out 0|1|2|3|(choose led ) 0(ON)|1(OFF)|2(BLINK)>: