summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-08-31 14:15:53 +0200
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-09-14 14:04:51 +0200
commit2f3a00b5f628b9db684505fc6566148cf6701d0a (patch)
treee676218f58f11a828b5d73591fb88ec0779ef873
parentf203185be97a192a4174c7d5f89be3bfb4e29ac2 (diff)
Add set_event_handler, added int returns for all API methods.
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
-rw-r--r--AFBClient.cpp70
-rw-r--r--AFBClient.h25
2 files changed, 75 insertions, 20 deletions
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 <cctype>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
@@ -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<void(char const *)> func) {
+ // XXX todo
+}
diff --git a/AFBClient.h b/AFBClient.h
index 5b8bb6d..f8f824a 100644
--- a/AFBClient.h
+++ b/AFBClient.h
@@ -3,6 +3,8 @@
#include <systemd/sd-event.h>
+#include <functional>
+
extern "C"
{
#include <json-c/json.h>
@@ -18,19 +20,32 @@ class AFBClient
virtual~AFBClient();
public:
+ enum EventType {
+ Event_Active = 1,
+ Event_Inactive,
+ Event_Visible,
+ Event_Invisible,
+ Event_SyncDraw,
+ Event_FlushDraw,
+ };
+
static AFBClient &instance()
{
static AFBClient obj;
return obj;
}
- bool init();
- int requestSurface(const char *label);
- void activateSurface(const char *label);
- void deactivateSurface(const char *label);
- void endDraw(const char *label);
+ int init(int port, char const *token);
int dispatch(uint64_t timeout);
+ // WM API
+ int requestSurface(const char *label);
+ int activateSurface(const char *label);
+ int deactivateSurface(const char *label);
+ int endDraw(const char *label);
+
+ void set_event_handler(enum EventType et, std::function<void(char const *label)> f);
+
static void onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
static void onEvent(void *closure, const char *event, struct afb_wsj1_msg *msg);
static void onHangup(void *closure, struct afb_wsj1 *wsj1);