Debian Codegraph @

ccast/cctest.c

raw symbols SCIP
1
2 /*
3 * Argyll Color Correction System
4 * ChromCast test harness
5 *
6 * Author: Graeme W. Gill
7 * Date: 28/8/2014
8 *
9 * Copyright 2014 Graeme W. Gill
10 * All rights reserved.
11 *
12 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
13 * see the License2.txt file for licencing details.
14 *
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #include <sys/types.h>
21 #include <time.h>
22 #include "copyright.h"
23 #include "aconfig.h"
24 #ifndef SALONEINSTLIB
25 #include "numlib.h"
26 #else
27 #include "numsup.h"
28 #endif
29 #include "yajl.h"
30 #include "conv.h"
31 #include "ccmdns.h"
32 #include "ccpacket.h"
33 #include "ccmes.h"
34
35 /* Check if JSON is invalid */
36 /* Exits if invalid */
37 static void check_json(char *mesbuf) {
38 yajl_val node;
39 char errbuf[1024];
40
41 if ((node = yajl_tree_parse(mesbuf, errbuf, sizeof(errbuf))) == NULL) {
42 printf("yajl_tree_parse of send message failed with '%s'\n",errbuf);
43 printf("JSON = '%s'\n",mesbuf);
44 exit(1);
45 }
46 yajl_tree_free(node);
47 }
48
49 /* Return nz on error */
50 static int get_a_reply(ccmessv *mesv, ORD8 **pdata) {
51 ccmessv_err merr;
52 ccmes mes;
53 char *source_id;
54 char *destination_id;
55 char *namespace;
56 int binary; /* Flag */
57 ORD8 *data; /* String or binary */
58 ORD32 bin_len; /* Binary data length */
59
60 if ((merr = mesv->receive(mesv, &mes)) != ccmessv_OK) {
61 printf("mesv->receive failed with '%s'\n",ccmessv_emes(merr));
62 return -1;
63 } else {
64 printf("Got message:\n");
65 printf(" source_id = '%s'\n",mes.source_id);
66 printf(" destination_id = '%s'\n",mes.destination_id);
67 printf(" namespace = '%s'\n",mes.namespace);
68 printf(" binary = %d\n",mes.binary);
69 if (binary) {
70 printf(" payload =\n");
71 adump_bytes(g_log, " ", data, 0, mes.bin_len);
72 } else {
73 printf(" payload = '%s'\n",mes.data);
74 }
75 if (pdata != NULL)
76 *pdata = mes.data;
77 else
78 free(data);
79 }
80 return 0;
81 }
82
83 /* Get replies until we get one with the matching Id, then return */
84 static int get_a_reply_id(ccmessv *mesv, int tid, ORD8 **pdata) {
85 ORD8 *data;
86 int id, rv;
87 yajl_val node, v;
88 char errbuf[1024];
89
90 //printf("~~~ looking for id %d\n",tid);
91 for (;;) {
92 int id = -1;
93
94 if ((rv = get_a_reply(mesv, &data)) != 0)
95 return rv;
96
97 // printf("\nGot JSON data to parse: '%s'\n",data);
98 if ((node = yajl_tree_parse(data, errbuf, sizeof(errbuf))) == NULL)
99 error("yajl_tree_parse failed with '%s'",errbuf);
100
101 if ((v = yajl_tree_get_first(node, "requestId", yajl_t_number)) != NULL) {
102 printf("~~~~ Got id %d\n",id);
103 id = YAJL_GET_INTEGER(v);
104 }
105 yajl_tree_free(node);
106
107 if (id == tid) {
108 //printf("~~~ got our message\n");
109 break;
110 }
111 free(data);
112 //printf("~~~ round we go again\n");
113 }
114 if (pdata != NULL)
115 *pdata = data;
116 else
117 free(data);
118 return 0;
119 }
120
121 /* Do the authentication sequence */
122 static void authenticate(ccmessv *mesv) {
123
124 /* (Not needed) */
125 }
126
127 int
128 main(
129 int argc,
130 char *argv[]
131 ) {
132 ccast_id **ids;
133 int i;
134 ccpacket *pk;
135 ccpacket_err perr;
136 ccmessv *mesv;
137 ccmessv_err merr;
138 char *sessionId = NULL;
139 char *transportId = NULL;
140 char mesbuf[1024];
141
142 if ((ids = get_ccids()) == NULL) {
143 error("get_ccids() failed");
144 }
145
146 if (ids[0] == NULL) {
147 error("Found no ChromCasts");
148 }
149
150 for (i = 0; ids[i] != NULL; i++) {
151 printf("Entry %d:\n",i);
152 printf(" Name: %s\n",ids[i]->name);
153 printf(" IP: %s\n",ids[i]->ip);
154 }
155
156 if ((pk = new_ccpacket()) == NULL) {
157 error("new_ccpacket() failed");
158 }
159
160 if ((perr = pk->connect(pk, ids[0]->ip, 8009)) != ccpacket_OK) {
161 error("ccpacket connect failed with '%s",ccpacket_emes(perr));
162 }
163 printf("Got TLS connection to '%s\n'",ids[0]->name);
164
165 if ((mesv = new_ccmessv(pk)) == NULL) {
166 error("new_ccmessv() failed");
167 }
168
169 /* Attempt a connection */
170 if ((merr = mesv->send(mesv, "sender-0", "receiver-0",
171 "urn:x-cast:com.google.cast.tp.connection", 0,
172 "{ \"type\": \"CONNECT\" }", 0)) != ccmessv_OK) {
173 error("mesv->send CONNECT failed with '%s'",ccmessv_emes(merr));
174 }
175
176 /* Send a ping */
177 if ((merr = mesv->send(mesv, "sender-0", "receiver-0",
178 "urn:x-cast:com.google.cast.tp.heartbeat", 0,
179 "{ \"type\": \"PING\" }", 0)) != ccmessv_OK) {
180 error("mesv->send PING failed with '%s'",ccmessv_emes(merr));
181 }
182
183 /* Wait for a PONG */
184 get_a_reply(mesv, NULL);
185
186 authenticate(mesv);
187
188 /* Launch the default application */
189 /* (Presumably we would use the com.google.cast.receiver channel */
190 /* for monitoring and controlling the reciever) */
191 if ((merr = mesv->send(mesv, "sender-0", "receiver-0",
192 "urn:x-cast:com.google.cast.receiver", 0,
193 "{ \"requestId\": 1, \"type\": \"LAUNCH\", \"appId\": \"CC1AD845\" }", 0)) != ccmessv_OK) {
194 error("mesv->send LAUNCH failed with '%s'",ccmessv_emes(merr));
195 }
196
197 /* Receive the RECEIVER_STATUS status messages until it is ready to cast */
198 /* and get the sessionId and transportId */
199 /* We get periodic notification messages (requestId=0) as well as */
200 /* a response messages (requestId=1) */
201 for (;sessionId == NULL || transportId == NULL;) {
202 ORD8 *data;
203 yajl_val node, v;
204 char errbuf[1024];
205
206 get_a_reply(mesv, &data);
207
208 // printf("\nGot JSON data to parse: '%s'\n",data);
209 if ((node = yajl_tree_parse(data, errbuf, sizeof(errbuf))) == NULL)
210 error("yajl_tree_parse failed with '%s'",errbuf);
211
212 if ((v = yajl_tree_get_first(node, "sessionId", yajl_t_string)) != NULL) {
213 sessionId = strdup(YAJL_GET_STRING(v));
214 // printf("~1 got sessionId = '%s'\n",sessionId);
215 }
216
217 if ((v = yajl_tree_get_first(node, "transportId", yajl_t_string)) != NULL) {
218 transportId = strdup(YAJL_GET_STRING(v));
219 // printf("~1 got transportId = '%s'\n",transportId);
220 }
221 free(data);
222 yajl_tree_free(node);
223 }
224
225 printf("\ngot sessionId = '%s', transportId = '%s'\n",sessionId, transportId);
226
227 printf("\nAbout to send URL\n");
228
229 /* Connect up to the reciever media channels */
230 if ((merr = mesv->send(mesv, "med_send", transportId,
231 "urn:x-cast:com.google.cast.tp.connection", 0,
232 "{ \"type\": \"CONNECT\" }", 0)) != ccmessv_OK) {
233 error("mesv->send CONNECT failed with '%s'",ccmessv_emes(merr));
234 }
235
236 // "urn:x-cast:com.google.cast.player.message"
237 // "urn:x-cast:com.google.cast.media"}
238
239 sprintf(mesbuf, "{ \"requestId\": 2, \"type\": \"LOAD\", \"media\": "
240 "{ \"contentId\": \"http://www.argyllcms.com/ArgyllCMSLogo.gif\",\"streamType\": \"LIVE\","
241 "\"contentType\": \"image/gif\" } }");
242
243 // "\"contentType\": \"image/gif\", \"duration\" : 0.1 }, \"autplay\": \"true\" }");
244
245 check_json(mesbuf);
246
247 /* Send the LOAD command */
248 if ((merr = mesv->send(mesv, "med_send", transportId,
249 "urn:x-cast:com.google.cast.media", 0,
250 mesbuf, 0
251 )) != ccmessv_OK) {
252 error("mesv->send LOAD failed with '%s'",ccmessv_emes(merr));
253 }
254
255 if (get_a_reply_id(mesv, 2, NULL) != 0)
256 exit(1);
257
258 msec_sleep(2000);
259
260 #ifdef NEVER
261 /* Check the media status */
262 if ((merr = mesv->send(mesv, "med_send", transportId,
263 "urn:x-cast:com.google.cast.media", 0,
264 "{ \"requestId\": 3, \"type\": \"GET_STATUS\" }", 0)) != ccmessv_OK) {
265 error("mesv->send GET_STATUS failed with '%s'",ccmessv_emes(merr));
266 }
267
268 if (get_a_reply_id(mesv, 3, NULL) != 0)
269 exit(1);
270 #endif
271
272 sprintf(mesbuf, "{ \"requestId\": 4, \"type\": \"LOAD\", \"media\": { \"contentId\": \"http://www.argyllcms.com/testing.png\",\"streamType\": \"LIVE\",\"contentType\": \"image/png\" } }");
273
274 // sprintf(mesbuf, "{ \"requestId\": 4, \"type\": \"LOAD\", \"media\": { \"contentId\": \"http://www.argyllcms.com/testing.tif\",\"streamType\": \"LIVE\",\"contentType\": \"image/tiff\" } }");
275 // sprintf(mesbuf, "{ \"requestId\": 4, \"type\": \"LOAD\", \"media\": { \"contentId\": \"http://www.argyllcms.com/doc/sl.jpg\",\"streamType\": \"LIVE\",\"contentType\": \"image/jpeg\" } }");
276 check_json(mesbuf);
277
278 /* Send the LOAD command */
279 if ((merr = mesv->send(mesv, "med_send", transportId,
280 "urn:x-cast:com.google.cast.media", 0,
281 mesbuf, 0
282 )) != ccmessv_OK) {
283 error("mesv->send LOAD failed with '%s'",ccmessv_emes(merr));
284 }
285
286 if (get_a_reply_id(mesv, 4, NULL) != 0)
287 exit(1);
288
289 msec_sleep(2000);
290
291 #ifdef NEVER
292 /* Check the media status */
293 if ((merr = mesv->send(mesv, "med_send", transportId,
294 "urn:x-cast:com.google.cast.media", 0,
295 "{ \"requestId\": 5, \"type\": \"GET_STATUS\" }", 0)) != ccmessv_OK) {
296 error("mesv->send GET_STATUS failed with '%s'",ccmessv_emes(merr));
297 }
298
299 if (get_a_reply_id(mesv, 5, NULL) != 0)
300 exit(1);
301 #endif
302
303 #ifdef NEVER
304 /* Try and send it an image URL */
305 if ((merr = mesv->send(mesv, "med_send", transportId,
306 "urn:x-cast:com.google.cast.media", 0,
307 // "{ \"imageUrl\": \"http://www.argyllcms.com/ArgyllCMSLogo.gif\", \"requestId\": 6 }", 0)) != ccmessv_OK) {
308 "{ \"url\": \"http://www.argyllcms.com/ArgyllCMSLogo.gif\", \"requestId\": 2 }", 0)) != ccmessv_OK) {
309 error("mesv->send imageUrl failed with '%s'",ccmessv_emes(merr));
310 }
311
312 #endif
313 /* Wait for any reply */
314 for (;;) {
315 if (get_a_reply(mesv, NULL) != 0)
316 break;
317 }
318
319 // ~~999
320
321 mesv->del(mesv);
322 pk->del(pk);
323
324 printf("Disconnected from '%s'\n",ids[0]->name);
325
326 free_ccids(ids);
327
328 return 0;
329 }
330