3
Fork 0

Updated D-BUS proxy to be compliant to the last releases of BrandMeister

This commit is contained in:
Artem Prilutskiy 2019-02-02 20:46:23 +03:00
parent a89bc58ede
commit 1c09eace91
4 changed files with 250 additions and 272 deletions

View file

@ -1,9 +1,9 @@
// Copyright 2015-2016 by Artem Prilutskiy
#include "PatchCord.h"
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdio.h>
@ -15,9 +15,8 @@
#define AUTOPATCH_LINK_NAME "AutoPatch"
// From PatchCord.h
#define VALUE_CORD_OUTGOING_SOURCE_ID 1
#define VALUE_CORD_INCOMING_SOURCE_ID 4
#define VALUE_CORD_TALKER_ALIAS 7
#define VALUE_CORD_OUTGOING_SOURCE_ID 1
#define VALUE_CORD_INCOMING_SOURCE_ID 4
#define BANNER_RENEWAL_INTERVAL 60
@ -51,176 +50,151 @@ uint32_t PatchCord::getTalkerID()
void PatchCord::setTalkerAlias(const char* value)
{
char* buffer;
asprintf(&buffer, "set alias %s", value);
size_t length = strlen(value) + 12;
char* buffer = (char*)alloca(length);
sprintf(buffer, "set alias %s", value);
getContextBanner();
invokeCommand(buffer);
free(buffer);
}
void PatchCord::getContextBanner()
{
time_t now = time(NULL);
if ((banner != NULL) &&
(renewal >= now))
if ((banner == NULL) ||
(renewal < now))
{
// Save RPC calls and use cached value
return;
}
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextList");
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextList");
const char* name = AUTOPATCH_LINK_NAME;
const char* name = AUTOPATCH_LINK_NAME;
dbus_message_append_args(request,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
dbus_message_append_args(message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
DBusMessage* response = makeCall(request);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
char** array;
int count;
if ((dbus_message_get_args(message, NULL,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, &count,
DBUS_TYPE_INVALID)) &&
(count > 0))
if (response != NULL)
{
if ((banner == NULL) ||
(strcmp(banner, *array) != 0))
char** array;
int count;
if ((dbus_message_get_args(response, NULL,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, &count,
DBUS_TYPE_INVALID)) &&
(count > 0))
{
free(banner);
banner = strdup(*array);
if ((banner == NULL) ||
(strcmp(banner, array[0]) != 0))
{
free(banner);
banner = strdup(array[0]);
}
dbus_free_string_array(array);
}
dbus_free_string_array(array);
}
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
renewal = now + BANNER_RENEWAL_INTERVAL;
dbus_message_unref(response);
}
renewal = now + BANNER_RENEWAL_INTERVAL;
}
}
void PatchCord::invokeCommand(const char* command)
{
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "invokeCommand");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &command,
DBUS_TYPE_INVALID);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (banner != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "invokeCommand");
dbus_message_append_args(request,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &command,
DBUS_TYPE_INVALID);
makeCall(request, true);
}
dbus_message_unref(message);
}
void PatchCord::setSpecificValue(uint32_t key, uint32_t value)
{
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "setSpecificValue");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_UINT32, &key,
DBUS_TYPE_UINT32, &value,
DBUS_TYPE_INVALID);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (banner != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "setSpecificValue");
dbus_message_append_args(request,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_UINT32, &key,
DBUS_TYPE_UINT32, &value,
DBUS_TYPE_INVALID);
makeCall(request, true);
}
dbus_message_unref(message);
}
uint32_t PatchCord::getSpecificValue(uint32_t key)
{
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextData");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_INVALID);
uint32_t value = 0;
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (banner != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextData");
message = dbus_pending_call_steal_reply(pending);
dbus_message_append_args(request,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_INVALID);
const char* name;
const char* address;
dbus_uint32_t type;
dbus_uint32_t state;
dbus_uint32_t number;
dbus_uint32_t* values;
int count;
DBusMessage* response = makeCall(request);
if (dbus_message_get_args(message, NULL,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &type,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_UINT32, &state,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &values, &count,
DBUS_TYPE_INVALID))
value = values[key];
if (response != NULL)
{
const char* name;
const char* address;
dbus_uint32_t type;
dbus_uint32_t mode;
dbus_uint32_t state;
dbus_uint32_t number;
dbus_uint32_t* values;
int count;
dbus_pending_call_unref(pending);
if (dbus_message_get_args(response, NULL,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &type,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_UINT32, &mode,
DBUS_TYPE_UINT32, &state,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &values, &count,
DBUS_TYPE_INVALID))
value = values[key];
dbus_message_unref(response);
}
}
dbus_message_unref(message);
return value;
}
uint32_t PatchCord::getPrivateIDForCall(const char* call)
{
DBusMessage* message = dbus_message_new_method_call(
uint32_t value = 0;
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getStationData");
dbus_message_append_args(message,
dbus_message_append_args(request,
DBUS_TYPE_STRING, &call,
DBUS_TYPE_INVALID);
uint32_t value = 0;
DBusMessage* response = makeCall(request);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (response != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_uint32_t number;
dbus_uint32_t algorithm;
dbus_uint32_t key;
@ -232,7 +206,7 @@ uint32_t PatchCord::getPrivateIDForCall(const char* call)
const char* text;
const char* symbol;
if (dbus_message_get_args(message, NULL,
if (dbus_message_get_args(response, NULL,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_UINT32, &algorithm,
DBUS_TYPE_UINT32, &key,
@ -246,33 +220,27 @@ uint32_t PatchCord::getPrivateIDForCall(const char* call)
DBUS_TYPE_INVALID))
value = number;
dbus_pending_call_unref(pending);
dbus_message_unref(response);
}
dbus_message_unref(message);
return value;
}
bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
{
DBusMessage* message = dbus_message_new_method_call(
bool result = false;
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getStationData");
dbus_message_append_args(message,
dbus_message_append_args(request,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
bool result = false;
DBusMessage* response = makeCall(request);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (response != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_uint32_t number;
dbus_uint32_t algorithm;
dbus_uint32_t key;
@ -284,7 +252,7 @@ bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
const char* value2;
const char* symbol;
if (dbus_message_get_args(message, NULL,
if (dbus_message_get_args(response, NULL,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_UINT32, &algorithm,
DBUS_TYPE_UINT32, &key,
@ -302,9 +270,28 @@ bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
result = true;
}
dbus_pending_call_unref(pending);
dbus_message_unref(response);
}
dbus_message_unref(message);
return result;
}
DBusMessage* PatchCord::makeCall(DBusMessage* request, bool omission)
{
DBusPendingCall* pending;
DBusMessage* response = NULL;
if (dbus_connection_send_with_reply(connection, request, &pending, -1))
{
dbus_connection_flush(connection);
dbus_pending_call_block(pending);
response = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
}
dbus_message_unref(request);
if (omission && response)
{
dbus_message_unref(response);
return NULL;
}
return response;
}

View file

@ -39,6 +39,8 @@ class PatchCord
void setSpecificValue(uint32_t key, uint32_t value);
uint32_t getSpecificValue(uint32_t key);
DBusMessage* makeCall(DBusMessage* request, bool omission = false);
};
#endif

View file

@ -1,9 +1,9 @@
// Copyright 2015-2016 by Artem Prilutskiy
#include "PatchCord.h"
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdio.h>
@ -15,9 +15,8 @@
#define AUTOPATCH_LINK_NAME "AutoPatch"
// From PatchCord.h
#define VALUE_CORD_OUTGOING_SOURCE_ID 1
#define VALUE_CORD_INCOMING_SOURCE_ID 4
#define VALUE_CORD_TALKER_ALIAS 7
#define VALUE_CORD_OUTGOING_SOURCE_ID 1
#define VALUE_CORD_INCOMING_SOURCE_ID 4
#define BANNER_RENEWAL_INTERVAL 60
@ -51,176 +50,151 @@ uint32_t PatchCord::getTalkerID()
void PatchCord::setTalkerAlias(const char* value)
{
char* buffer;
asprintf(&buffer, "set alias %s", value);
size_t length = strlen(value) + 12;
char* buffer = (char*)alloca(length);
sprintf(buffer, "set alias %s", value);
getContextBanner();
invokeCommand(buffer);
free(buffer);
}
void PatchCord::getContextBanner()
{
time_t now = time(NULL);
if ((banner != NULL) &&
(renewal >= now))
if ((banner == NULL) ||
(renewal < now))
{
// Save RPC calls and use cached value
return;
}
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextList");
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextList");
const char* name = AUTOPATCH_LINK_NAME;
const char* name = AUTOPATCH_LINK_NAME;
dbus_message_append_args(request,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
dbus_message_append_args(message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
DBusMessage* response = makeCall(request);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
char** array;
int count;
if ((dbus_message_get_args(message, NULL,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, &count,
DBUS_TYPE_INVALID)) &&
(count > 0))
if (response != NULL)
{
if ((banner == NULL) ||
(strcmp(banner, *array) != 0))
char** array;
int count;
if ((dbus_message_get_args(response, NULL,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, &count,
DBUS_TYPE_INVALID)) &&
(count > 0))
{
free(banner);
banner = strdup(*array);
if ((banner == NULL) ||
(strcmp(banner, array[0]) != 0))
{
free(banner);
banner = strdup(array[0]);
}
dbus_free_string_array(array);
}
dbus_free_string_array(array);
}
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
renewal = now + BANNER_RENEWAL_INTERVAL;
dbus_message_unref(response);
}
renewal = now + BANNER_RENEWAL_INTERVAL;
}
}
void PatchCord::invokeCommand(const char* command)
{
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "invokeCommand");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &command,
DBUS_TYPE_INVALID);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (banner != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "invokeCommand");
dbus_message_append_args(request,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &command,
DBUS_TYPE_INVALID);
makeCall(request, true);
}
dbus_message_unref(message);
}
void PatchCord::setSpecificValue(uint32_t key, uint32_t value)
{
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "setSpecificValue");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_UINT32, &key,
DBUS_TYPE_UINT32, &value,
DBUS_TYPE_INVALID);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (banner != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "setSpecificValue");
dbus_message_append_args(request,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_UINT32, &key,
DBUS_TYPE_UINT32, &value,
DBUS_TYPE_INVALID);
makeCall(request, true);
}
dbus_message_unref(message);
}
uint32_t PatchCord::getSpecificValue(uint32_t key)
{
DBusMessage* message = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextData");
dbus_message_append_args(message,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_INVALID);
uint32_t value = 0;
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (banner != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getContextData");
message = dbus_pending_call_steal_reply(pending);
dbus_message_append_args(request,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_INVALID);
const char* name;
const char* address;
dbus_uint32_t type;
dbus_uint32_t state;
dbus_uint32_t number;
dbus_uint32_t* values;
int count;
DBusMessage* response = makeCall(request);
if (dbus_message_get_args(message, NULL,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &type,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_UINT32, &state,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &values, &count,
DBUS_TYPE_INVALID))
value = values[key];
if (response != NULL)
{
const char* name;
const char* address;
dbus_uint32_t type;
dbus_uint32_t mode;
dbus_uint32_t state;
dbus_uint32_t number;
dbus_uint32_t* values;
int count;
dbus_pending_call_unref(pending);
if (dbus_message_get_args(response, NULL,
DBUS_TYPE_STRING, &banner,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &type,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_UINT32, &mode,
DBUS_TYPE_UINT32, &state,
DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &values, &count,
DBUS_TYPE_INVALID))
value = values[key];
dbus_message_unref(response);
}
}
dbus_message_unref(message);
return value;
}
uint32_t PatchCord::getPrivateIDForCall(const char* call)
{
DBusMessage* message = dbus_message_new_method_call(
uint32_t value = 0;
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getStationData");
dbus_message_append_args(message,
dbus_message_append_args(request,
DBUS_TYPE_STRING, &call,
DBUS_TYPE_INVALID);
uint32_t value = 0;
DBusMessage* response = makeCall(request);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (response != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_uint32_t number;
dbus_uint32_t algorithm;
dbus_uint32_t key;
@ -232,7 +206,7 @@ uint32_t PatchCord::getPrivateIDForCall(const char* call)
const char* text;
const char* symbol;
if (dbus_message_get_args(message, NULL,
if (dbus_message_get_args(response, NULL,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_UINT32, &algorithm,
DBUS_TYPE_UINT32, &key,
@ -246,33 +220,27 @@ uint32_t PatchCord::getPrivateIDForCall(const char* call)
DBUS_TYPE_INVALID))
value = number;
dbus_pending_call_unref(pending);
dbus_message_unref(response);
}
dbus_message_unref(message);
return value;
}
bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
{
DBusMessage* message = dbus_message_new_method_call(
bool result = false;
DBusMessage* request = dbus_message_new_method_call(
name, OBJECT_PATH, INTERFACE_NAME, "getStationData");
dbus_message_append_args(message,
dbus_message_append_args(request,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
bool result = false;
DBusMessage* response = makeCall(request);
DBusPendingCall* pending;
if (dbus_connection_send_with_reply(connection, message, &pending, -1))
if (response != NULL)
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_uint32_t number;
dbus_uint32_t algorithm;
dbus_uint32_t key;
@ -284,7 +252,7 @@ bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
const char* value2;
const char* symbol;
if (dbus_message_get_args(message, NULL,
if (dbus_message_get_args(response, NULL,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_UINT32, &algorithm,
DBUS_TYPE_UINT32, &key,
@ -302,9 +270,28 @@ bool PatchCord::getCredentialsForID(uint32_t number, char* call, char* text)
result = true;
}
dbus_pending_call_unref(pending);
dbus_message_unref(response);
}
dbus_message_unref(message);
return result;
}
DBusMessage* PatchCord::makeCall(DBusMessage* request, bool omission)
{
DBusPendingCall* pending;
DBusMessage* response = NULL;
if (dbus_connection_send_with_reply(connection, request, &pending, -1))
{
dbus_connection_flush(connection);
dbus_pending_call_block(pending);
response = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
}
dbus_message_unref(request);
if (omission && response)
{
dbus_message_unref(response);
return NULL;
}
return response;
}

View file

@ -39,6 +39,8 @@ class PatchCord
void setSpecificValue(uint32_t key, uint32_t value);
uint32_t getSpecificValue(uint32_t key);
DBusMessage* makeCall(DBusMessage* request, bool omission = false);
};
#endif