summaryrefslogtreecommitdiff
path: root/main.c
blob: 8a30638ac6b3b321f12053fa5a05eba35d43df4c (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * This program uses the PortAudio Portable Audio Library.
 * For more information see: http://www.portaudio.com
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>

#include "src/record.h"
#include "src/stt.h"

#define SSDP_MULTICAST              "239.255.255.250"
#define SSDP_PORT                   1900

#define BUTTON_HOME                 "Home"
#define BUTTON_REW                  "Rew"
#define BUTTON_FWD                  "Fwd"
#define BUTTON_PLAY                 "Play"
#define BUTTON_SELECT               "Select"
#define BUTTON_LEFT                 "Left"
#define BUTTON_RIGHT                "Right"
#define BUTTON_DOWN                 "Down"
#define BUTTON_UP                   "Up"
#define BUTTON_BACK                 "Back"
#define BUTTON_INSTANTREPLAY	    "InstantReplay"
#define BUTTON_INFO		    "Info"
#define BUTTON_BACKSPACE	    "Backspace"
#define BUTTON_SEARCH		    "Search"
#define BUTTON_ENTER		    "Enter"
#define BUTTON_LIT		    "Lit_"

#define NETFLIX_ID                  "12"
#define SPOTIFY_ID                  "19977"

#define RESPONSE_BUFFER_LEN         8192
#define URL_BUFFER_LEN              1024
char buffer[RESPONSE_BUFFER_LEN];
unsigned int len = RESPONSE_BUFFER_LEN;

char url[URL_BUFFER_LEN];
char host[URL_BUFFER_LEN];


int invocation();
void speak(int);
void getCommand();
int tvCommands(int);
int http_req_resp(char* host, int port, char* req, char** resp);
int ssdp_get_roku_ecp_url(char* url);
void rokuWrite(char *, int, char*);
int sendTVcommand(int, char *, int);

int main(void){
    while(1){
	if(invocation()){
	    speak(1);
	    getCommand();
	}	    	
    }    
}

int invocation(){
    char text[100];
    int invoked=0;
    int res= 0;
    record(2);
    speechToText(text);
    printf("\n\nTEXT: %s\n\n", text);
    if(strstr(text, "HAL PAUSE") || strstr(text, "HAL PLAY"))
	res = tvCommands(5);
    else if(strstr(text, "HAL SKIP"))
	res = tvCommands(6);
    else if(strstr(text, "HAL RESPOND") || strstr(text, "HAL")){
	invoked=1;
    }
    speak(res);
    return invoked;
}

void speak(int num){
    switch(num){
    case 1:
	system("festival --tts speech/greetings");
	break;
    case 2:
	system("festival --tts speech/TVnotConnectedError");
	break;
    }
}

void getCommand(){
    char text[100];
    int res=0;
    record(3);
    speechToText(text);
    printf("\n\nTEXT: %s\n\n", text);
    if(strstr(text, "SKIP"))
    	res = tvCommands(6);
    else if(strstr(text, "NET FLICKS")){
    	if(strstr(text, "STAR TREK"))
    	    res = tvCommands(1);
    }
    else if(strstr(text, "BEETHOVEN SYMPHONIES"))
	res = tvCommands(2);
    else if(strstr(text, "BEETHOVEN PIANO SONATAS"))
	res = tvCommands(3);
    else if(strstr(text, "RANDOM BEETHOVEN"))
	res = tvCommands(4);
    else if(strstr(text, "PAUSE") || strstr(text, "PLAY"))
    	res = tvCommands(5);
    speak(res);
}

/*
  Return values: 
  1 - successful execution
  2 - unable to reach TV
  3 - connection error
*/
int tvCommands(int command){
    int i;
    int ret = -1;
    char* hostbegin;
    char* hostend;
    char* portbegin;
    char* portend;
    char  portStr[10];
    int   port;
    
    /* int netflix_id = 12, spotify_id = 19977; */

    //check for broadcast every 20 mins
    ret = ssdp_get_roku_ecp_url(url);
    if(ret < 0)
	return 2;

    hostbegin = strstr(buffer, "http://") + 7;
    hostend = strstr(hostbegin, ":");
    strncpy(host, hostbegin, hostend-hostbegin);
    host[hostend-hostbegin] = 0;

    portbegin = hostend+1;
    portend = strstr(portbegin, "/");
    strncpy(portStr, portbegin, portend-portbegin);
    portStr[portend-portbegin] = 0;
    port = atoi(portStr);

    sendTVcommand(command, host, port);
}

int ssdp_get_roku_ecp_url(char* url)
{
    int sock;
    size_t ret;
    unsigned int socklen;
    struct sockaddr_in sockname;
    struct sockaddr clientsock;
    struct hostent *hostname = 0;
    char ssdproku[] =
	"M-SEARCH * HTTP/1.1\r\n"
	"Host: 239.255.255.250:1900\r\n"
	"Man: \"ssdp:discover\"\r\n"
	"ST: roku:ecp\r\n"
	"\r\n";
    fd_set fds;
    struct timeval timeout;
    char* urlbegin;
    char* urlend;

    /* SSDP Request */
    hostname = gethostbyname(SSDP_MULTICAST);
    if (!hostname) {

	printf("gethostbyname returns null for host %s", SSDP_MULTICAST);
    }
    hostname->h_addrtype = AF_INET;

    if((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1){
	printf("err: socket() failed");
	return -1;
    }

    memset((char*)&sockname, 0, sizeof(sockname));
    sockname.sin_family=AF_INET;
    sockname.sin_port=htons(SSDP_PORT);
    sockname.sin_addr.s_addr=*((unsigned long*)(hostname->h_addr_list[0]));
    
    ret=sendto(sock, ssdproku, strlen(ssdproku), 0, (struct sockaddr*) &sockname, sizeof(sockname));
    if(ret != strlen(ssdproku)){
	printf("err:sendto");
	return -1;
    }

    /* SSDP Response */
    FD_ZERO(&fds);
    FD_SET(sock, &fds);
    timeout.tv_sec=10;
    timeout.tv_usec=10;

    if(select(sock+1, &fds, NULL, NULL, &timeout) < 0){
	printf("err:select");
	return -1;
    }
    if(FD_ISSET(sock, &fds)){
	socklen=sizeof(clientsock);
	if((len = recvfrom(sock, buffer, len, MSG_PEEK, &clientsock, &socklen)) == (size_t)-1){
	    printf("err: recvfrom");
	    return -1;
	}
	buffer[len]='\0';
	//    close(sock);

	if(strncmp(buffer, "HTTP/1.1 200 OK", 12) != 0){
	    printf("err: ssdp parsing ");
	    return -1;
	}

	/* Parse out Location url */
	urlbegin = strstr(buffer, "LOCATION: ") + 10;
	urlend = strstr(urlbegin, "/\r\n");
	strncpy(url, urlbegin, urlend-urlbegin);
	url[urlend-urlbegin] = 0;
    } else {
	printf("err: no ssdp answer");
	return -1;
    }
    return 0;
}

/*
Command dir
1 - Star Trek (Netflix)
2 - Beethoven (Spotify)
3 - Bach (Spotify)
 */
int sendTVcommand(int command, char* host, int port){
    char *resp;

    char enter[] = 
	"POST /keypress/" BUTTON_ENTER  " HTTP/1.0\r\n"
	"\r\n";
    
    char homeKey[] = 
	"POST /keypress/" BUTTON_HOME  " HTTP/1.0\r\n"
	"\r\n";

    char rightKey[] = 
	"POST /keypress/" BUTTON_RIGHT " HTTP/1.0\r\n"
	"\r\n";

    char upKey[] =
	"POST /keypress/" BUTTON_UP " HTTP/1.0\r\n"
	"\r\n";

    char play_pause[] =
	"POST /keypress/" BUTTON_PLAY " HTTP/1.0\r\n"
	"\r\n";

    char downKey[] =
	"POST /keypress/" BUTTON_DOWN " HTTP/1.0\r\n"
	"\r\n";
    
    char launchNetflix[] =
	"POST /launch/" NETFLIX_ID " HTTP/1.0\r\n"
	"\r\n";
    
    char launchSpotify[] =
	"POST /launch/" SPOTIFY_ID " HTTP/1.0\r\n"
	"\r\n";

    char select[] =
	"POST /keypress/" BUTTON_SELECT " HTTP/1.0\r\n"
	"\r\n";

    char fwd[] =
	"POST /keypress/" BUTTON_FWD " HTTP/1.0\r\n"
	"\r\n";	
    int randup, randdown;
    switch(command){
    case 1:
	http_req_resp(host, port, launchNetflix, &resp);
	sleep(10);
	http_req_resp(host, port, upKey, &resp);
	sleep(1);
	http_req_resp(host, port, select, &resp);
	sleep(1);
	http_req_resp(host, port, upKey, &resp);
	sleep(1);
	http_req_resp(host, port, select, &resp);
	sleep(1);
	rokuWrite(host, port, "Star Trek The Next");
	http_req_resp(host, port, enter, &resp);
	sleep(7);
	for(int i=0; i<5; i++)
	    http_req_resp(host, port, rightKey, &resp);
	sleep(1);
	http_req_resp(host, port, select, &resp);
	sleep(4);
	http_req_resp(host, port, select, &resp);
	break;
    case 2:
	http_req_resp(host, port, launchSpotify, &resp);
	sleep(10);
	http_req_resp(host, port, select, &resp);
	http_req_resp(host, port, select, &resp);
	rokuWrite(host, port, "karajan symphony edition");
	sleep(1);
	for(int i=0; i<4; i++)
	    http_req_resp(host, port, rightKey, &resp);
	http_req_resp(host, port, downKey, &resp);
	http_req_resp(host, port, select, &resp);
	srand(time(NULL));
	randup = rand() % 30;
	randdown = rand() % 30;
	for(int i=0; i<randup; i++)
	    http_req_resp(host, port, upKey, &resp);
	for(int i=0; i<randdown; i++)
	    http_req_resp(host, port, downKey, &resp);
	http_req_resp(host, port, select, &resp);
	break;
    case 3:
	http_req_resp(host, port, launchSpotify, &resp);
	sleep(10);
	http_req_resp(host, port, select, &resp);
	http_req_resp(host, port, select, &resp);
	rokuWrite(host, port, "brendel piano sonatas");
	sleep(1);
	for(int i=0; i<4; i++)
	    http_req_resp(host, port, rightKey, &resp);
	http_req_resp(host, port, downKey, &resp);
	http_req_resp(host, port, select, &resp);
	srand(time(NULL));
	randup = rand() % 30;
	randdown = rand() % 30;
	for(int i=0; i<randup; i++)
	    http_req_resp(host, port, upKey, &resp);
	for(int i=0; i<randdown; i++)
	    http_req_resp(host, port, downKey, &resp);
	http_req_resp(host, port, select, &resp);
	break;
    case 4:
	http_req_resp(host, port, launchSpotify, &resp);
	sleep(10);
	http_req_resp(host, port, select, &resp);
	http_req_resp(host, port, select, &resp);
	rokuWrite(host, port, "beethoven");
	sleep(1);
	for(int i=0; i<4; i++)
	    http_req_resp(host, port, rightKey, &resp);
	http_req_resp(host, port, downKey, &resp);
	srand(time(NULL));
	randup = rand() % 30;
	randdown = rand() % 30;
	for(int i=0; i<randdown; i++)
	    http_req_resp(host, port, downKey, &resp);
	for(int i=0; i<randup; i++)
	    http_req_resp(host, port, upKey, &resp);
	http_req_resp(host, port, select, &resp);
	http_req_resp(host, port, select, &resp);
	break;
    case 5:
	http_req_resp(host, port, play_pause, &resp);
	break;
    case 6:
	http_req_resp(host, port, fwd, &resp);
	break;
    }
}

void rokuWrite(char *host, int port, char * text){
    char *resp;
    char select_start[] =
	"POST /keypress/" BUTTON_LIT;
    char select_end[] =
	" HTTP/1.0\r\n"
	"\r\n";
    
    char send_text[50];
    char curr_char[4];
    for(int i = 0; i<strlen(text); i++){
	if(text[i] == ' ')
	    strcpy(curr_char, "%20");
	
	else
	    curr_char[0] = text[i];
	strcat(send_text, select_start);
	strcat(send_text, curr_char);
	strcat(send_text, select_end);
	http_req_resp(host, port, send_text, &resp);
	send_text[0]=0;
    }
}


#define DWORD int
int http_req_resp(char* host, int port, char* req, char** resp)
{
    int sock;
    size_t ret;
    unsigned int socklen;
    struct sockaddr_in sockname;
    struct sockaddr clientsock;
    struct hostent *hostname;
    fd_set fds;
    struct timeval timeout;

    hostname = gethostbyname(host);
    if (hostname == NULL) {
	printf("gethostbyname returns null for host %s", host);
    }
    hostname->h_addrtype = AF_INET;

    if((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1){
	printf("err: socket() failed");
	return -1;
    }

    memset((char*)&sockname, 0, sizeof(sockname));
    sockname.sin_family=AF_INET;
    sockname.sin_port=htons(port);
    sockname.sin_addr.s_addr=*((unsigned long*)(hostname->h_addr_list[0]));

    ret=connect(sock, (struct sockaddr*) &sockname, sizeof(sockname));
    ret=send(sock, req, strlen(req), 0);
    
    //Send will not account for partial data writes -- for now
    if(ret != strlen(req)){	
	printf("err:sendto");
	return -1;
    }

    //Recv will not account for partial data reads -- for now
    FD_ZERO(&fds);
    FD_SET(sock, &fds);
    timeout.tv_sec=10;
    timeout.tv_usec=10;

    if(select(sock+1, &fds, NULL, NULL, &timeout) < 0){
	printf("err:select");
	return -1;
    }

    if(FD_ISSET(sock, &fds)){
	if((len = recv(sock, buffer, sizeof(buffer), 0)) == (size_t)-1){
	    printf("err: recvfrom");
	    return -1;
	}
	buffer[len]='\0';
	//    close(sock);

	if(strncmp(buffer+9, "200 OK", 6) != 0){
	    printf("err: http req parsing ");
	}

	resp = (char **)&buffer;
	return 0;

    }else{

	printf("err: no http answer");
	return -1;
    }
}