X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=AFBClient.cpp;h=2d15c4f2e4ae93ca6d1617ee00d84008e803f06a;hb=2f3a00b5f628b9db684505fc6566148cf6701d0a;hp=283e22c5b73137ee562e2172aa21375801079f03;hpb=4a2c73eca3713fa049c86d021aae27ac66c4e481;p=staging%2Fwindowmanager.git diff --git a/AFBClient.cpp b/AFBClient.cpp index 283e22c..2d15c4f 100644 --- a/AFBClient.cpp +++ b/AFBClient.cpp @@ -1,5 +1,6 @@ #include "AFBClient.h" +#include #include #include #include @@ -8,15 +9,7 @@ #define UNUSED(x) (void)(x) -extern "C" { -extern struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure); -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); -extern int afb_wsj1_msg_is_reply_ok(struct afb_wsj1_msg *msg); -extern int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object); -static inline int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token); -} - -const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm"; +constexpr const int token_maxlen = 20; const char * AFBClient::wmAPI = "winman"; AFBClient::AFBClient() : itf() @@ -31,32 +24,67 @@ AFBClient::~AFBClient() { } -bool AFBClient::init() +int AFBClient::init(int port, char const *token) { - printf("init() -->\n"); + char *uribuf = NULL; + int rc = -1; + + printf("AFBClient::init() -->\n"); + + if (!token || strlen(token) > token_maxlen) { + fprintf(stderr, "Token is invalid\n"); + rc = -EINVAL; + goto fail; + } + + for (char const *p = token; *p; p++) { + if (!isalnum(*p)) { + fprintf(stderr, "Token is invalid\n"); + rc = -EINVAL; + goto fail; + } + } + + if (port < 1 && port > 0xffff) { + fprintf(stderr, "Port is invalid\n"); + rc = -EINVAL; + goto fail; + } + /* get the default event loop */ - int rc = sd_event_default(&loop); + rc = sd_event_default(&loop); if (rc < 0) { fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc)); - return false; + goto fail; } + asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token); + /* connect the websocket wsj1 to the uri given by the first argument */ - wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL); + wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, NULL); if (wsj1 == NULL) { - fprintf(stderr, "Connection to %s failed: %m\n", wmURI); - return false; + sd_event_unref(loop); + fprintf(stderr, "Connection to %s failed: %m\n", uribuf); + rc = -errno; + goto fail; } - printf("init() <--\n"); - return true; + printf("AFBClient::init() <--\n"); + return 0; + +fail: + printf("AFBClient::init() <--\n"); + return rc; +} + +int AFBClient::dispatch(uint64_t timeout) { + return sd_event_run(loop, timeout); } int AFBClient::requestSurface(const char *label) { - printf("requestSurface(%s) -->\n", label); + printf("AFBClient::requestSurface(%s) -->\n", label); constexpr char const *verb = "request_surface"; - int ret = -1; json_object *jp = json_object_new_object(); json_object_object_add(jp, "drawing_name", json_object_new_string(label)); @@ -98,21 +126,22 @@ int AFBClient::requestSurface(const char *label) if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) { fprintf(stderr, "putenv failed: %m\n"); } else { - ret = 0; // Single point of success + rc = 0; // Single point of success } } else { fprintf(stderr, "Could not get surface ID from WM\n"); + rc = -EINVAL; } } - printf("requestSurface(%s) = %d <--\n", label, ret); + printf("AFBClient::requestSurface(%s) = %d <--\n", label, rc); - return ret; + return rc; } -void AFBClient::activateSurface(const char *label) +int AFBClient::activateSurface(const char *label) { - printf("activateSurface(%s) -->\n", label); + printf("AFBClient::activateSurface(%s) -->\n", label); fflush(stdout); const char begin[] = "{\"drawing_name\":\""; @@ -129,30 +158,37 @@ void AFBClient::activateSurface(const char *label) // Sync this one too dispatch(-1); - printf("activateSurface(%s) <--\n", label); + printf("AFBClient::activateSurface(%s) <--\n", label); fflush(stdout); + return 0; } -int AFBClient::dispatch(uint64_t timeout) { - return sd_event_run(loop, timeout); -} - -void AFBClient::deactivateSurface(const char *label) +int AFBClient::deactivateSurface(const char *label) { + printf("AFBClient::deactivateSurface(%s) -->\n", label); + fflush(stdout); json_object *j = json_object_new_object(); json_object_object_add(j, "drawing_name", json_object_new_string(label)); call(AFBClient::wmAPI, "deactivate_surface", json_object_to_json_string(j)); json_object_put(j); dispatch(-1); + printf("AFBClient::deactivateSurface(%s) <--\n", label); + fflush(stdout); + return 0; } -void AFBClient::endDraw(const char *label) +int AFBClient::endDraw(const char *label) { + printf("AFBClient::endDraw(%s) -->\n", label); + fflush(stdout); json_object *j = json_object_new_object(); json_object_object_add(j, "drawing_name", json_object_new_string(label)); call(AFBClient::wmAPI, "enddraw", json_object_to_json_string(j)); json_object_put(j); dispatch(-1); + printf("AFBClient::endDraw(%s) <--\n", label); + fflush(stdout); + return 0; } /* called when wsj1 receives a method invocation */ @@ -231,3 +267,7 @@ void AFBClient::event(const char *event, const char *object) if (rc < 0) fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object); } + +void AFBClient::set_event_handler(enum EventType at, std::function func) { + // XXX todo +}