9 #define UNUSED(x) (void)(x)
12 extern struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure);
13 extern int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, const char *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure);
14 extern int afb_wsj1_msg_is_reply_ok(struct afb_wsj1_msg *msg);
15 extern int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object);
16 static inline int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token);
19 const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm";
20 const char * AFBClient::wmAPI = "winman";
22 AFBClient::AFBClient() : itf()
24 ///* itinializing the callback interface for wsj1 */
25 itf.on_hangup = AFBClient::onHangup;
26 itf.on_call = AFBClient::onCall;
27 itf.on_event = AFBClient::onEvent;
30 AFBClient::~AFBClient()
34 bool AFBClient::init()
36 printf("init() -->\n");
37 /* get the default event loop */
38 int rc = sd_event_default(&loop);
40 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
45 /* connect the websocket wsj1 to the uri given by the first argument */
46 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
48 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
53 printf("init() <--\n");
57 void AFBClient::requestSurface(const char *label)
59 printf("requestSurface(%s) -->\n", label);
65 const char begin[] = "{\"drawing_name\":\"";
66 const char end[] = "\"}";
67 const char verb[] = "request_surface";
68 char *parameter = (char *)malloc(strlen(begin) +
71 strcpy(parameter, begin);
72 strcat(parameter, label);
73 strcat(parameter, end);
75 /* allocates an id for the request */
76 rc = asprintf(&key, "%d:%s/%s", ++num, AFBClient::wmAPI, verb);
78 /* send the request */
79 rc = afb_wsj1_call_s(wsj1, AFBClient::wmAPI, verb, parameter, AFBClient::onRequestSurfaceReply, key);
81 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb, parameter);
83 printf("requestSurface(%s) <--\n", label);
87 void AFBClient::activateSurface(const char *label)
89 printf("activateSurface(%s) -->\n", label);
92 const char begin[] = "{\"drawing_name\":\"";
93 const char end[] = "\"}";
94 const char verb[] = "activate_surface";
95 char *parameter = (char *)malloc(strlen(begin) +
98 strcpy(parameter, begin);
99 strcat(parameter, label);
100 strcat(parameter, end);
101 call(AFBClient::wmAPI, verb, parameter);
103 printf("activateSurface(%s) <--\n", label);
107 void AFBClient::deactivateSurface(const char *label)
112 void AFBClient::endDraw(const char *label)
117 /* called when wsj1 receives a method invocation */
118 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
123 printf("ON-CALL %s/%s:\n%s\n", api, verb,
124 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
125 JSON_C_TO_STRING_PRETTY));
127 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
129 fprintf(stderr, "replying failed: %m\n");
133 /* called when wsj1 receives an event */
134 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
138 printf("ON-EVENT %s:\n%s\n", event,
139 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
140 JSON_C_TO_STRING_PRETTY));
145 /* called when wsj1 hangsup */
146 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
150 printf("ON-HANGUP\n");
155 /* called when wsj1 receives a reply */
156 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
159 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
160 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
161 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
162 JSON_C_TO_STRING_PRETTY));
168 void AFBClient::onRequestSurfaceReply(void *closure, afb_wsj1_msg *msg)
171 printf("onRequestSurfaceReply %s: %s\n%s\n", (char*)closure,
172 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
173 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
174 JSON_C_TO_STRING_PRETTY));
175 printf("\n\n===>RETURN STR: %s\n\n", afb_wsj1_msg_object_s(msg));
176 // putenv("QT_IVI_SURFACE_ID=16778219");
183 void AFBClient::call(const char *api, const char *verb, const char *object)
190 printf("call(%s, %s, %s) -->\n", api, verb, object);
193 /* allocates an id for the request */
194 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
196 /* send the request */
197 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
199 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
201 printf("call(%s, %s, %s) <--\n", api, verb, object);
207 void AFBClient::event(const char *event, const char *object)
212 rc = afb_wsj1_send_event_s(wsj1, event, object);
214 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);