X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=AFBClient.cpp;h=2d15c4f2e4ae93ca6d1617ee00d84008e803f06a;hb=2f3a00b5f628b9db684505fc6566148cf6701d0a;hp=a009610151263a3c565d5ce380c19aaabcc57c25;hpb=f203185be97a192a4174c7d5f89be3bfb4e29ac2;p=staging%2Fwindowmanager.git diff --git a/AFBClient.cpp b/AFBClient.cpp index a009610..2d15c4f 100644 --- a/AFBClient.cpp +++ b/AFBClient.cpp @@ -1,5 +1,6 @@ #include "AFBClient.h" +#include #include #include #include @@ -8,7 +9,7 @@ #define UNUSED(x) (void)(x) -const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm"; +constexpr const int token_maxlen = 20; const char * AFBClient::wmAPI = "winman"; AFBClient::AFBClient() : itf() @@ -23,25 +24,57 @@ AFBClient::~AFBClient() { } -bool AFBClient::init() +int AFBClient::init(int port, char const *token) { + 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("AFBClient::init() <--\n"); - return true; + return 0; + +fail: + printf("AFBClient::init() <--\n"); + return rc; } int AFBClient::dispatch(uint64_t timeout) { @@ -52,7 +85,6 @@ int AFBClient::requestSurface(const char *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)); @@ -94,19 +126,20 @@ 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("AFBClient::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("AFBClient::activateSurface(%s) -->\n", label); fflush(stdout); @@ -127,9 +160,10 @@ void AFBClient::activateSurface(const char *label) printf("AFBClient::activateSurface(%s) <--\n", label); fflush(stdout); + return 0; } -void AFBClient::deactivateSurface(const char *label) +int AFBClient::deactivateSurface(const char *label) { printf("AFBClient::deactivateSurface(%s) -->\n", label); fflush(stdout); @@ -140,9 +174,10 @@ void AFBClient::deactivateSurface(const char *label) 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); @@ -153,6 +188,7 @@ void AFBClient::endDraw(const char *label) 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 +}