xref: /OK3568_Linux_fs/external/rk_pcba_test/echo_led_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  led_test.c  --  led test application
3  *
4  *  Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5  *  Author:
6  *  Panzhenzhuan Wang <randy.wang@rock-chips.com>
7  *  Bin Yang <yangbin@rock-chips.com>
8  *
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * 	 http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <sys/time.h>
29 #include <signal.h>
30 
31 #include <string.h>
32 #include <unistd.h>
33 #include "led_test.h"
34 
35 #define LOG_TAG "led_test"
36 #include "common.h"
37 
38 #define LED_BRIGHTNESS_FILE "/sys/class/leds/led%d/brightness"
39 #define LED_TRIGGER_FILE "/sys/class/leds/led%d/trigger"
40 #define LED_SHOT_FILE "/sys/class/leds/led%d/shot"
41 #define LED_DELAY_ON_FILE "/sys/class/leds/led%d/delay_on"
42 #define LED_DELAY_OFF_FILE "/sys/class/leds/led%d/delay_off"
43 
44 #define LED_NUM 36
45 #define COLOR_NUM 3
46 //����LED�������ر�
47 #define LED_ON "128"
48 #define LED_OFF "0"
49 
50 #define TRIGGER_NONE 0
51 
52 #define LOG_TAG "led_test"
53 #include "common.h"
54 
55 #define LED_TEST_TIMEOUT -51
56 #define LED_PROC_ERR -55
57 
58 //����LED����ɫ�ṹ��
59 typedef enum
60 {
61     LED_BLUE = 0,
62     LED_GREEN,
63     LED_RED,
64 } LED_colors;
65 
66 void *led_test(void *argv);    //LED���Գ���
67 int write_int(char const* path,char const* value);  //��ֵд���Ӧ·���ļ�
68 
led_test(void * argv)69 void *led_test(void *argv)
70 {
71     char buf[128];
72     char cmd[128];
73     int led_num,color_num =1;
74     LED_colors test_color;
75 
76 
77     printf("===================LED test start======================\n\n");
78     fprintf(stderr,"Start testing LED, order is Red, Green, Blue, White.\n Testing:\n");
79     while(color_num < 4)
80     {
81         switch(color_num%3)  //��һ��RED�ơ��ڶ���GREEN�ơ�������BLUE�ơ����һ��RGB�ϳɰ׵�
82             {
83             case 1:
84                 printf(" The testing color is RED: \n");
85                 break;
86             case 2:
87                 printf(" The testing color is GREEN: \n");
88                 break;
89             case 0:
90                 printf(" The testing color is BLUE: \n");
91                 break;
92             default :
93                 fprintf(stderr,"select color error");
94             }
95         for(led_num=1;led_num<=LED_NUM;led_num++)
96         {
97             switch(led_num % 3)
98             {
99             case LED_RED :
100                 if(color_num % 3 == 1)
101                 {
102                     sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
103                     if(-1==write_int(buf,LED_ON))
104                         goto fail;
105                 }
106                 else
107                 {
108                     sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
109                     if(-1==write_int(buf,LED_OFF))
110                         goto fail;
111                 }
112                 break;
113             case LED_GREEN :
114                 if(color_num % 3 == 2)
115                 {
116                     sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
117                     if(-1==write_int(buf,LED_ON))
118                         goto fail;
119                 }
120                 else
121                 {
122                     sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
123                     if(-1==write_int(buf,LED_OFF))
124                         goto fail;
125                 }
126                 break;
127             case LED_BLUE :
128                 if(color_num % 3 == 0)
129                 {
130                     sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
131                     if(-1==write_int(buf,LED_ON))
132                         goto fail;
133                 }
134                 else
135                 {
136                     sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
137                     if(-1==write_int(buf,LED_OFF))
138                         goto fail;
139                 }
140                 break;
141             default :
142                 fprintf(stderr,"select color error");
143             }
144         }
145         printf("LED shining 1 seconds:\n");
146         sleep(1);  //���1��
147         color_num++;
148     }
149 
150     //RGB������ɫ����һ���γɰ׵�
151     for(led_num=1;led_num<=LED_NUM;led_num++)
152     {
153         sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
154         if(-1==write_int(buf,LED_ON))
155             goto fail;
156     }
157 
158     sleep(1);       //��ͣ1��
159 
160     for(led_num=1;led_num<=LED_NUM;led_num++)
161     {
162         sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
163         if(-1==write_int(buf,LED_OFF))
164         goto fail;
165     }
166     fprintf(stderr, "===========LED Test finished=============\n");
167 
168     return (void*)0;
169     fail:
170 
171         return (void*)-1;
172 }
173 
174 //* �źŴ��������ڽ�������ǰ���ر����е�
leds_all_off(int sign_no)175 static int leds_all_off(int sign_no)
176 {
177     char buf[128];
178     int led_num;
179 
180     printf("====================function : %s start =================\n",__func__);
181     for(led_num=1; led_num<=LED_NUM; led_num++)
182     {
183         sprintf(buf,LED_BRIGHTNESS_FILE,led_num);
184         if(-1==write_int(buf,LED_OFF))
185             goto fail;
186     }
187 
188     printf("====================function : %s finished =================\n",__func__);
189     exit(0);
190     fail:
191         exit(-1);
192 }
193 
194 /*���������*/
main(int argc,char * argv[])195 int main(int argc, char *argv[])
196 {
197     int test_flag = 0,err_code = 0;
198     char buf[COMMAND_VALUESIZE] = "led_test";
199     char result[COMMAND_VALUESIZE] = RESULT_PASS;
200     int delay_t = 0;
201 	struct timeval t1, t2;
202 
203 	log_info("led test start...\n");
204 
205 	//* ע���źŴ�����
206 	signal(SIGTERM,(__sighandler_t)leds_all_off);
207 
208 	gettimeofday(&t1, NULL);
209     while(1)
210     {
211         test_flag = (int)led_test(argv[0]);
212         if(test_flag < 0)
213         {
214             err_code = LED_PROC_ERR;
215             break;
216         }
217 
218         gettimeofday(&t2, NULL);
219 		delay_t = (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
220 		if (delay_t > MANUAL_TEST_TIMEOUT) {
221 			log_warn("led test end, timeout 60s\n");
222 			err_code = LED_TEST_TIMEOUT;
223 			break;
224 		}
225     }
226     if (err_code)
227 		strcpy(result, RESULT_FAIL);
228     send_msg_to_server(buf, result, err_code);
229     return 0;
230 }
231