-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotled.c
217 lines (184 loc) · 4.57 KB
/
dotled.c
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
/*
* dotled.c - set or clear a bit corresponding to a led in memory.
*
* include LICENSE
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dotled.h>
#include <jsonroot.h>
#include <fileutil.h>
#include <stutil.h>
typedef struct _TestDotval TestDotval;
struct _TestDotval {
const char *name;
App_Run_FP func;
};
/*
* local prototypes
*/
int dotled_test_net(AppClass *data, void *user_data );
int dotled_test_hdmi(AppClass *data, void *user_data );
int dotled_test_colon(AppClass *data, void *user_data );
int dotled_test_usb(AppClass *data, void *user_data );
int dotled_test_bluetooth(AppClass *data, void *user_data );
int dotled_test_alarm(AppClass *data, void *user_data );
/* */
/*
*** \brief Allocates memory for a new DotLed object.
* node : configuration node
* target: address of ram to modify
*/
DotLed *dotled_new( AppClass *xnode, uint16_t * target )
{
DotLed *led;
led = app_new0(DotLed, 1);
dotled_construct( led, xnode, target );
app_class_overload_destroy( (AppClass *) led, dotled_destroy );
return led;
}
/** \brief Constructor for the DotLed object. */
void dotled_construct( DotLed *led, AppClass *xnode, uint16_t * target )
{
char *name;
app_class_construct( (AppClass *) led );
JsonNode *node = (JsonNode *) xnode;
led->target = target;
led->name = app_strdup(node->keyname);
JsonNode *n = json_root_get_item_string(node, "sysfile", &name );
if ( n ){
led->sysfile = app_strdup(name);
}
n = json_root_get_item_string(node, "driver", &name );
if ( n ){
dotled_set_test_func(led, name );
}
n = json_root_get_item_int(node, "bit", &led->bit );
if ( ! n ){
msg_error( "dotled bit not defined" );
}
}
/** \brief Destructor for the DotLed object. */
void dotled_destroy(void *led)
{
DotLed *this = (DotLed *) led;
if (led == NULL) {
return;
}
app_free(this->name);
app_free(this->sysfile);
app_class_destroy( led );
}
void dotled_set_cb_func(DotLed *led, App_Run_FP func, void *user_data )
{
led->cb_func = func;
led->cb_app = user_data;
}
int dotled_name_str_cmp(AppClass *d1, AppClass *d2 )
{
DotLed *led = (DotLed *) d1 ;
char *name = (char *) d2 ;
return app_strcmp( led->name, name );
}
int dotled_iter_update(AppClass *data, void *user_data )
{
DotLed *led = (DotLed *) data;
char tmpbuf[256];
FILE *fd = NULL;
if ( led->sysfile ){
if ( ! file_exists( led->sysfile ) ){
return 0;
}
fd = fopen( led->sysfile, "r");
if ( ! fd ) {
msg_error("Failed to open file '%s' - %s", led->sysfile, strerror(errno) );
return 0;
}
led->tmplen = fread( tmpbuf, 1, sizeof(tmpbuf), fd);
fclose (fd);
led->tmpbuf = tmpbuf;
}
if ( led->test_func ) {
int val = led->test_func(data, user_data);
if ( val ){
*led->target |= (1 << led->bit);
}
}
return 0;
}
int dotled_test_net(AppClass *data, void *user_data )
{
int ret = 0;
DotLed *led = (DotLed *) data;
int val = strtoul( led->tmpbuf, NULL, 16 );
if ( val & 1 ){
ret = 1;
}
return 1;
}
int dotled_test_hdmi(AppClass *data, void *user_data )
{
int ret = 0;
DotLed *led = (DotLed *) data;
char *msg = "connected";
if ( app_strncmp( led->tmpbuf, msg, strlen(msg)) == 0 ) {
ret = 1;
}
return 0;
}
int dotled_test_usb(AppClass *data, void *user_data )
{
int ret = 0;
DotLed *led = (DotLed *) data;
int i;
char *sn = led->tmpbuf;
char *tok;
for ( i = 0 ; i < 11 ; i++ ){
tok = stu_token_next(&sn, " ", " ");
}
int val = strtoul( tok, NULL, 10 );
if ( val > 0 ){
ret = 1;
}
return 0;
}
int dotled_test_alarm(AppClass *data, void *user_data )
{
int ret = 0;
DotLed *led = (DotLed *) data;
if ( *led->tmpbuf == '1' ){
ret = 1;
}
return ret;
}
int dotled_test_colon(AppClass *data, void *user_data )
{
int ret = 0;
DotLed *led = (DotLed *) data;
if ( led->cb_func ) {
ret = led->cb_func ( led->cb_app, data );
}
return ret;
}
static TestDotval test_fun_tbl[] = {
{ "net", dotled_test_net },
{ "hdmi", 0 },
{ "colon", dotled_test_colon },
{ "bluetooth", 0 },
{ "usb", 0 },
{ "alarm", 0 },
{ NULL, NULL },
};
void dotled_set_test_func(DotLed *led, char *name)
{
TestDotval *fp = test_fun_tbl;
while ( fp->name ) {
if ( app_strcmp( fp->name, name ) == 0 ){
led->test_func = fp->func;
return;
}
fp++;
}
msg_warning( "driver for '%s' not found\n", name );
}