forked from olajep/rpi-cecd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cec.c
308 lines (249 loc) · 8.12 KB
/
cec.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <unistd.h>
#include <curl/curl.h>
#include <interface/vmcs_host/vc_cecservice.h>
#include <interface/vchiq_arm/vchiq_if.h>
#ifdef __cplusplus
}
#endif
#include "config.h"
#include "Key.h"
#ifndef VC_TRUE
#define VC_TRUE 1
#endif
/* Remove this block when everyone is using the latest headers from
* RPi firmware github page
*/
#if 1
extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
extern int32_t vchi_exit( void );
extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
const uint32_t num_connections,
VCHI_INSTANCE_T instance_handle );
#endif
volatile int might_be_dimmed=0;
int port = 80;
size_t curl_write_nop(void *buffer, size_t size, size_t nmemb, void *userp)
{
return size*nmemb;
}
void curl_get(char *url)
{
CURL *curl;
CURLcode err;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_nop);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000);
err = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if ( err ) {
printf("curl_get: error=%d \"%s\"\n", err, curl_easy_strerror(err));
}
}
void xbmc_sendkey(uint32_t keysym)
{
char url[256];
snprintf(url, 255,
"http://localhost:%d/xbmcCmds/xbmcHttp?command=SendKey(%d)",
port, keysym);
curl_get(url);
}
void xbmc_sendaction(uint32_t action)
{
char url[256];
snprintf(url, 255,
"http://localhost:%d/xbmcCmds/xbmcHttp?command=Action(%d)",
port, action);
curl_get(url);
}
void button_pressed(uint32_t param)
{
uint32_t initiator, follower, opcode, operand1, operand2;
initiator = CEC_CB_INITIATOR(param);
follower = CEC_CB_FOLLOWER(param);
opcode = CEC_CB_OPCODE(param);
operand1 = CEC_CB_OPERAND1(param);
operand2 = CEC_CB_OPERAND2(param);
if (opcode != CEC_Opcode_UserControlPressed) {
printf("button_pressed: unknown operand operand1=0x%x: "
"initiator=0x%x, follower=0x%x, opcode=0x%x, "
"operand1=0x%x, operand2=0x%x\n",
operand1, initiator, follower, opcode, operand1, operand2);
return;
}
// Hack to make xbmc light up
if (might_be_dimmed) {
might_be_dimmed = 0;
xbmc_sendkey(KEY_ASCII);
}
switch (operand1) {
case CEC_User_Control_Select:
xbmc_sendaction(ACTION_SELECT_ITEM);
break;
case CEC_User_Control_Up:
xbmc_sendaction(ACTION_MOVE_UP);
break;
case CEC_User_Control_Down:
xbmc_sendaction(ACTION_MOVE_DOWN);
break;
case CEC_User_Control_Left:
xbmc_sendaction(ACTION_MOVE_LEFT);
break;
case CEC_User_Control_Right:
xbmc_sendaction(ACTION_MOVE_RIGHT);
break;
case CEC_User_Control_RootMenu:
xbmc_sendaction(ACTION_PREVIOUS_MENU);
break;
case CEC_User_Control_SetupMenu:
case CEC_User_Control_DisplayInformation:
xbmc_sendaction(ACTION_CONTEXT_MENU);
break;
case CEC_User_Control_Exit:
xbmc_sendaction(ACTION_NAV_BACK);
break;
case CEC_User_Control_EPG:
xbmc_sendaction(ACTION_SHOW_PLAYLIST);
break;
case CEC_User_Control_Play:
case CEC_User_Control_Pause:
xbmc_sendaction(ACTION_PLAYER_PLAYPAUSE );
break;
case CEC_User_Control_Stop:
xbmc_sendaction(ACTION_STOP);
break;
case CEC_User_Control_Rewind:
xbmc_sendaction(ACTION_PLAYER_REWIND);
break;
case CEC_User_Control_FastForward:
xbmc_sendaction(ACTION_PLAYER_FORWARD);
break;
case CEC_User_Control_Forward:
xbmc_sendaction(ACTION_FORWARD);
break;
case CEC_User_Control_Backward:
xbmc_sendaction(ACTION_REWIND);
break;
// Colored buttons from left to right
case CEC_User_Control_F2Red:
xbmc_sendaction(ACTION_TELETEXT_RED);
break;
case CEC_User_Control_F3Green:
xbmc_sendaction(ACTION_TELETEXT_GREEN);
break;
case CEC_User_Control_F4Yellow:
xbmc_sendaction(ACTION_TELETEXT_YELLOW);
break;
case CEC_User_Control_F1Blue:
xbmc_sendaction(ACTION_TELETEXT_BLUE);
break;
default:
printf("button_pressed: operand1=0x%x has no binding\n", operand1);
}
}
void cec_callback(void *callback_data, uint32_t param0,
uint32_t param1, uint32_t param2,
uint32_t param3, uint32_t param4)
{
VC_CEC_NOTIFY_T reason;
uint32_t len, retval;
reason = (VC_CEC_NOTIFY_T) CEC_CB_REASON(param0);
len = CEC_CB_MSG_LENGTH(param0);
retval = CEC_CB_RC(param0);
#ifdef DEBUG
printf("cec_callback: debug: "
"reason=0x%04x, len=0x%02x, retval=0x%02x, "
"param1=0x%08x, param2=0x%08x, param3=0x%08x, param4=0x%08x\n",
reason, len, retval, param1, param2, param3, param4);
#endif
if ( reason == VC_CEC_BUTTON_PRESSED ) {
if ( len > 4 ) {
printf("cec_callback: warning: len > 4, only using first parameter "
"reason=0x%04x, len=0x%02x, retval=0x%02x, "
"param1=0x%08x, param2=0x%08x, param3=0x%08x, param4=0x%08x\n",
reason, len, retval, param1, param2, param3, param4);
}
button_pressed(param1);
} else if (reason == VC_CEC_RX && CEC_CB_OPCODE(param1) == CEC_Opcode_MenuRequest ) {
if (CEC_CB_OPERAND1(param1) == CEC_MENU_STATE_QUERY ) {
uint8_t msg[2];
uint32_t initiator;
initiator = CEC_CB_INITIATOR(param1);
msg[0] = CEC_Opcode_MenuStatus;
msg[1] = CEC_MENU_STATE_ACTIVATED;
vc_cec_send_message(initiator, msg, 2, VC_TRUE);
}
} else if ( reason != VC_CEC_BUTTON_RELEASE ) {
printf("cec_callback: unknown event: "
"reason=0x%04x, len=0x%02x, retval=0x%02x, "
"param1=0x%08x, param2=0x%08x, param3=0x%08x, param4=0x%08x\n",
reason, len, retval, param1, param2, param3, param4);
}
}
int main(int argc, char **argv)
{
int res = 0;
CURLcode curlerr;
VCHI_INSTANCE_T vchiq_instance;
VCHI_CONNECTION_T *vchi_connection;
CEC_AllDevices_T logical_address;
uint16_t physical_address;
/* Make sure logs are written to disk */
setlinebuf(stdout);
setlinebuf(stderr);
if (argc > 2) {
printf("usage: %s [port]\n", argv[0]);
return -1;
}
if (argc == 2) {
port = atoi(argv[1]);
}
curlerr = curl_global_init(CURL_GLOBAL_NOTHING);
if ( curlerr ) {
printf("failed to init curl error=%d \"%s\"\n", curlerr,
curl_easy_strerror(curlerr));
return -1;
}
res = vchi_initialise(&vchiq_instance);
if ( res != VCHIQ_SUCCESS ) {
printf("failed to open vchiq instance\n");
return -1;
}
res = vchi_connect( NULL, 0, vchiq_instance );
if ( res != 0 ) {
printf( "VCHI connection failed\n" );
return -1;
}
vc_vchi_cec_init(vchiq_instance, &vchi_connection, 1);
if ( res != 0 ) {
printf( "VCHI CEC connection failed\n" );
return -1;
}
vc_cec_register_callback(((CECSERVICE_CALLBACK_T) cec_callback), NULL);
#if 0
vc_cec_register_all();
#endif
vc_cec_register_command(CEC_Opcode_MenuRequest);
vc_cec_get_logical_address(&logical_address);
printf("logical_address: 0x%x\n", logical_address);
vc_cec_set_vendor_id(CEC_VENDOR_ID_BROADCOM);
vc_cec_set_osd_name("XBMC");
vc_cec_get_physical_address(&physical_address);
printf("physical_address: 0x%x\n", physical_address);
vc_cec_send_ActiveSource(physical_address, 0);
while (1) {
might_be_dimmed = 1;
sleep(10);
}
vchi_exit();
curl_global_cleanup();
return 0;
}