www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / userapps / opensource / busybox / miscutils / ledctrl.c
1 /***************************************************************************
2  * File Name  : ledctrl.c
3  *
4  * Description: 
5  *
6  *    This application control the led action by calling sysLedCtrl.
7  *    smaple calls:     sysLedCtrl(kLedAdsl, kLedStateBlinkOnce);           // kLedxxx defines in board_api.h
8  *
9  * Created on :  11/01/2002  seanl
10  *
11  ***************************************************************************/
12 #include <stdlib.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>  /* ioctl */
17 #include "board.h"
18 #include "busybox.h"
19
20
21
22 static int boardIoctl(int boardFd, int board_ioctl, BOARD_IOCTL_ACTION action, char *string, int strLen, int offset)
23 {
24     BOARD_IOCTL_PARMS IoctlParms;
25     
26     IoctlParms.string = string;
27     IoctlParms.strLen = strLen;
28     IoctlParms.offset = offset;
29     IoctlParms.action = action;
30
31     ioctl(boardFd, board_ioctl, &IoctlParms);
32
33     return (IoctlParms.result);
34 }
35
36 static void sysLedCtrl(BOARD_LED_NAME ledName, BOARD_LED_STATE ledState)
37 {
38     int boardFd;
39
40     if ((boardFd = open("/dev/brcmboard", O_RDWR)) == -1) 
41         printf("Unable to open device /dev/brcmboard.\n");
42     else {
43         boardIoctl(boardFd, BOARD_IOCTL_LED_CTRL, 0, "", (int)ledName, (int)ledState);
44         close(boardFd);
45     }
46 }
47
48 // ******* end board_api.c/h
49
50 typedef struct
51 {
52     char *validLedName;
53     BOARD_LED_NAME ledName;
54 } LED_NAME_MAP, *PLED_NAME_MAP;
55
56
57 // LED name as the first param
58 const LED_NAME_MAP ledNameMap[] =
59 {
60     {"Adsl",         kLedAdsl},
61     {"Wireless",     kLedWireless},
62     {"Usb",          kLedUsb},
63     {"Hpna",         kLedHpna},
64     {"WanData",      kLedWanData},
65     {"PPP",          kLedPPP},
66     {"Voip",         kLedSes},
67     {"Ses",          kLedSes},
68     {NULL,           kLedEnd},             
69 };
70
71 typedef struct 
72 {
73     char *validLedState;
74     BOARD_LED_STATE ledState;
75 } LED_STATE_MAP, *PLED_STATE_MAP;
76
77 const LED_STATE_MAP ledStateMap[] =
78 {
79     {"Off",                 kLedStateOff},               
80     {"On",                  kLedStateOn},                
81     {"BlinkOnce",           kLedStateBlinkOnce},         
82     {"SlowBlinkContinues",  kLedStateSlowBlinkContinues},
83     {"FastBlinkContinues",  kLedStateFastBlinkContinues},
84     {NULL,                  -1},
85 };  
86
87 void showUsage(void)
88 {
89     PLED_NAME_MAP namePtr = ledNameMap;
90     PLED_STATE_MAP statePtr = ledStateMap;
91
92     printf("\nUsage: ledctrl led_name led_state\n\n");
93     printf("Supported led names:\n");
94     while (namePtr->validLedName)
95         printf("%s\n", namePtr++->validLedName);
96     
97     printf("\nSupported led states:\n");
98     while (statePtr->validLedState)
99         printf("%s\n", statePtr++->validLedState);
100
101     exit(-1);
102 }
103
104 int ledctrl_main(int argc, char **argv)
105 {
106     PLED_NAME_MAP namePtr = ledNameMap;
107     PLED_STATE_MAP statePtr = ledStateMap;
108
109     if (argc < 3)
110         showUsage();
111
112     while (namePtr->validLedName)
113         if (strcmp(namePtr->validLedName, argv[1]) == 0)
114             break;
115         else
116             namePtr++;
117     if (!namePtr->validLedName)
118         showUsage();
119
120     while (statePtr->validLedState)
121         if (strcmp(statePtr->validLedState, argv[2]) == 0)
122             break;
123         else
124             statePtr++;
125
126     if (!statePtr->validLedState)
127         showUsage();
128
129     sysLedCtrl(namePtr->ledName, statePtr->ledState);
130
131         return 0;
132
133 }