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