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));
44 /* connect the websocket wsj1 to the uri given by the first argument */
45 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
47 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
51 printf("init() <--\n");
55 void AFBClient::requestSurface(const char *label)
57 printf("requestSurface(%s) -->\n", label);
63 const char begin[] = "{\"drawing_name\":\"";
64 const char end[] = "\"}";
65 const char verb[] = "request_surface";
66 char *parameter = (char *)malloc(strlen(begin) +
69 strcpy(parameter, begin);
70 strcat(parameter, label);
71 strcat(parameter, end);
73 /* allocates an id for the request */
74 rc = asprintf(&key, "%d:%s/%s", ++num, AFBClient::wmAPI, verb);
76 /* send the request */
77 rc = afb_wsj1_call_s(wsj1, AFBClient::wmAPI, verb, parameter, AFBClient::onRequestSurfaceReply, key);
79 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb, parameter);
81 printf("requestSurface(%s) <--\n", label);
85 void AFBClient::activateSurface(const char *label)
87 printf("activateSurface(%s) -->\n", label);
90 const char begin[] = "{\"drawing_name\":\"";
91 const char end[] = "\"}";
92 const char verb[] = "activate_surface";
93 char *parameter = (char *)malloc(strlen(begin) +
96 strcpy(parameter, begin);
97 strcat(parameter, label);
98 strcat(parameter, end);
99 call(AFBClient::wmAPI, verb, parameter);
101 printf("activateSurface(%s) <--\n", label);
105 int AFBClient::dispatch() {
106 return sd_event_run(loop, -1);
109 void AFBClient::deactivateSurface(const char *label)
114 void AFBClient::endDraw(const char *label)
119 /* called when wsj1 receives a method invocation */
120 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
124 printf("ON-CALL %s/%s:\n%s\n", api, verb,
125 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
126 JSON_C_TO_STRING_PRETTY));
128 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
130 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)
137 printf("ON-EVENT %s:\n%s\n", event,
138 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
139 JSON_C_TO_STRING_PRETTY));
143 /* called when wsj1 hangsup */
144 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
148 printf("ON-HANGUP\n");
153 /* called when wsj1 receives a reply */
154 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
156 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
157 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
158 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
159 JSON_C_TO_STRING_PRETTY));
164 void AFBClient::onRequestSurfaceReply(void *closure, afb_wsj1_msg *msg)
166 printf("onRequestSurfaceReply %s: %s\n%s\n", (char*)closure,
167 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
168 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
169 JSON_C_TO_STRING_PRETTY));
170 if(afb_wsj1_msg_is_reply_ok(msg))
172 const char begin[] = "QT_IVI_SURFACE_ID=\"";
173 const char end[] = "\"";
174 char *parameter = (char *)malloc(strlen(begin) +
175 strlen(json_object_get_string(json_object_object_get(afb_wsj1_msg_object_j(msg), "response"))) +
177 strcpy(parameter, begin);
178 strcat(parameter, json_object_get_string(json_object_object_get(afb_wsj1_msg_object_j(msg), "response")));
179 strcat(parameter, end);
180 printf("\n\n===>RETURN STR: %s\n\n", parameter);
185 fprintf(stderr, "wrong request surface reply received!");
191 void AFBClient::call(const char *api, const char *verb, const char *object)
197 printf("call(%s, %s, %s) -->\n", api, verb, object);
200 /* allocates an id for the request */
201 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
203 /* send the request */
204 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
206 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
208 printf("call(%s, %s, %s) <--\n", api, verb, object);
213 void AFBClient::event(const char *event, const char *object)
217 rc = afb_wsj1_send_event_s(wsj1, event, object);
219 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);