4
Fork 0

Initial import

This commit is contained in:
R3ABM Artem 2016-03-23 20:08:20 +03:00
commit 660133c812
28 changed files with 3983 additions and 0 deletions

View file

@ -0,0 +1,90 @@
// Copyright 2015 by Artem Prilutskiy
#include "BrandMeisterBridge.h"
#include <string.h>
#include <stdlib.h>
#include <syslog.h>
#include <stdio.h>
#define ECHOLINK_DEFAULT_USER_NUMBER 1
#define ECHOLINK_DEFAULT_CORD_NUMBER 10
#define REGISTRY_CONFIGURATION_FILE "/opt/BrandMeister/Registry.cnf"
BrandMeisterBridge::BrandMeisterBridge() :
proxy(ECHOLINK_DEFAULT_CORD_NUMBER),
store(REGISTRY_CONFIGURATION_FILE),
talker(NULL)
{
}
BrandMeisterBridge::~BrandMeisterBridge()
{
free(talker);
}
// Interface methods for ModuleEchoLink
const char* BrandMeisterBridge::getTalker()
{
free(talker);
uint32_t number = proxy.getTalkerID();
char call[LONG_CALLSIGN_LENGTH];
char text[SLOW_DATA_TEXT_LENGTH];
if ((number != 0) &&
(store.getCredentialsForID(number, call, text)))
{
asprintf(&talker, "%s %s", call, text);
return talker;
}
asprintf(&talker, "DMR ID: %d", number);
return talker;
}
void BrandMeisterBridge::setTalker(const char* call, const char* name)
{
if (*call == '*')
{
// Do not process conference call-sign
return;
}
const char* delimiter = strpbrk(call, " -\n");
if (delimiter != NULL)
{
// Remove characters after call-sign
size_t length = delimiter - call;
char* buffer = (char*)alloca(length + 1);
strncpy(buffer, call, length);
call = buffer;
}
uint32_t number = store.getPrivateIDForCall(call);
if (number == 0)
{
syslog(LOG_INFO, "DMR ID for call-sign %s not found", call);
number = ECHOLINK_DEFAULT_USER_NUMBER;
}
syslog(LOG_DEBUG, "Set talker DMR ID to %d", number);
proxy.setTalkerID(number);
}
void BrandMeisterBridge::handleChatMessage(const char* text)
{
// CONF Russian Reflector, Open 24/7, Contacts: rv3dhc.link@qip.ru * Call CQ / Use pauses 2sec * [28/500]
// R3ABM-L *DSTAR.SU DMR Bridge*
// UB3AMO Moscow T I N A O
// ->UA0LQE-L USSURIISK
const char* delimiter;
if ((strncmp(text, "CONF ", 5) == 0) &&
((delimiter = strstr(text, "\n->")) != NULL))
{
const char* call = delimiter + 3;
setTalker(call, NULL);
}
}

View file

@ -0,0 +1,28 @@
// Copyright 2015 by Artem Prilutskiy
#ifndef BRANDMEISTERBRIDGE_H
#define BRANDMEISTERBRIDGE_H
#include "PatchCordProxy.h"
#include "UserDataStore.h"
class BrandMeisterBridge
{
public:
BrandMeisterBridge();
~BrandMeisterBridge();
const char* getTalker();
void setTalker(const char* call, const char* name);
void handleChatMessage(const char* text);
private:
PatchCordProxy proxy;
UserDataStore store;
char* talker;
};
#endif

113
CallCapture/CallCapture.cpp Normal file
View file

@ -0,0 +1,113 @@
#include <stdlib.h>
#include <getopt.h>
#include <syslog.h>
#include <stdio.h>
#include <pcre.h>
#include "PatchCordProxy.h"
#include "UserDataStore.h"
#define VECTORS_COUNT 8
#define DEFAULT_USER_NUMBER 1
int main(int argc, const char* argv[])
{
printf("\n");
printf("CallCapture for BrandMeister DMR Master Server\n");
printf("Copyright 2015 Artem Prilutskiy (R3ABM, cyanide.burnout@gmail.com)\n");
printf("\n");
// Start up
struct option options[] =
{
{ "expression", required_argument, NULL, 'e' },
{ "connection", required_argument, NULL, 'c' },
{ "identity", required_argument, NULL, 'i' },
{ "link", required_argument, NULL, 'l' },
{ NULL, 0, NULL, 0 }
};
pcre* expression = NULL;
const char* file = NULL;
uint32_t number = 10;
int position = 0;
const char* error = NULL;
int selection = 0;
while ((selection = getopt_long(argc, const_cast<char* const*>(argv), "e:c:l:", options, NULL)) != EOF)
switch (selection)
{
case 'e':
expression = pcre_compile(optarg, 0, &error, &position, NULL);
break;
case 'c':
file = optarg;
break;
case 'i':
openlog(optarg, 0, LOG_USER);
break;
case 'l':
number = atoi(optarg);
break;
}
if ((expression == NULL) &&
(error != NULL))
{
printf("Error compiling regular expression: %s (at %d)\n", error, position);
return EXIT_FAILURE;
}
if ((expression == NULL) ||
(file == NULL))
{
printf(
"Usage: %s"
" --expression <regular expression>"
" --connection <path to configuration file>"
" --link <link number>"
" [--identity <identity>]"
"\n",
argv[0]);
return EXIT_FAILURE;
}
// Main
UserDataStore store(file);
PatchCordProxy proxy(number);
char* line = NULL;
size_t length = 0;
ssize_t read;
while ((read = getline(&line, &length, stdin)) != EOF)
{
syslog(LOG_DEBUG, "%s", line);
int vectors[VECTORS_COUNT];
int count = pcre_exec(expression, NULL, line, length, 0, 0, vectors, VECTORS_COUNT);
if (count > 0)
{
const char* call;
pcre_get_substring(line, vectors, count, 1, &call);
uint32_t number = store.getPrivateIDForCall(call);
if (number == 0)
number = DEFAULT_USER_NUMBER;
proxy.setTalkerID(number);
syslog(LOG_INFO, "*** Found call-sign: %s (ID: %d)", call, number);
pcre_free_substring(call);
}
}
pcre_free(expression);
return EXIT_SUCCESS;
};

40
CallCapture/Makefile Normal file
View file

@ -0,0 +1,40 @@
OBJECTS = \
PatchCordProxy.o \
UserDataStore.o \
CallCapture.o
LIBRARIES = \
mysqlclient \
pcre
DEPENDENCIES = \
dbus-1
CXXFLAGS := -fno-implicit-templates -D__STDC_CONSTANT_MACROS
FLAGS := -g -rdynamic -fno-omit-frame-pointer -O3 -MMD $(foreach directory, $(DIRECTORIES), -I$(directory)) $(shell pkg-config --cflags $(DEPENDENCIES)) -DBUILD=\"$(BUILD)\" -DVERSION=$(VERSION)
LIBS := -lstdc++ $(foreach library, $(LIBRARIES), -l$(library)) $(shell pkg-config --libs $(DEPENDENCIES))
# PREREQUISITES =
CC = gcc
CXX = g++
COMPILER_VERSION_FLAG := $(shell expr `${CXX} -dumpversion | cut -f1-2 -d.` \>= 4.9)
CFLAGS += $(FLAGS) -std=gnu99
ifeq ($(COMPILER_VERSION_FLAG), 1)
CXXFLAGS += $(FLAGS) -std=gnu++11
else
CXXFLAGS += $(FLAGS) -std=gnu++0x
endif
all: build
build: $(PREREQUISITES) $(OBJECTS)
$(CC) $(OBJECTS) $(FLAGS) $(LIBS) -o callcapture
clean:
rm -f $(PREREQUISITES) $(OBJECTS) callcapture
rm -f *.d */*.d
.PHONY: all build clean install

View file

@ -0,0 +1,154 @@
// Copyright 2015 by Artem Prilutskiy
#include "PatchCordProxy.h"
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdio.h>
#define INTERFACE_NAME "me.burnaway.BrandMeister"
#define SERVICE_NAME INTERFACE_NAME
#define OBJECT_PATH "/me/burnaway/BrandMeister"
// From AutoPatch.cpp
#define AUTOPATCH_LINK_NAME "AutoPatch"
// From PatchCord.h
#define VALUE_CORD_OUTGOING_SOURCE_ID 1
#define VALUE_CORD_INCOMING_SOURCE_ID 4
PatchCordProxy::PatchCordProxy(uint32_t number) :
number(number),
banner(NULL)
{
connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
}
PatchCordProxy::~PatchCordProxy()
{
free(banner);
dbus_connection_unref(connection);
}
void PatchCordProxy::setTalkerID(uint32_t value)
{
getContextBanner();
setVendorSpecificValue(VALUE_CORD_OUTGOING_SOURCE_ID, value);
}
uint32_t PatchCordProxy::getTalkerID()
{
getContextBanner();
return getVendorSpecificValue(VALUE_CORD_INCOMING_SOURCE_ID);
}
void PatchCordProxy::getContextBanner()
{
DBusMessage* message = dbus_message_new_method_call(
SERVICE_NAME, OBJECT_PATH,
INTERFACE_NAME, "getContextList");
const char* name = AUTOPATCH_LINK_NAME;
dbus_message_append_args(message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &number,
DBUS_TYPE_INVALID);
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))
{
free(banner);
banner = strdup(*array);
dbus_free_string_array(array);
}
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
}
void PatchCordProxy::setVendorSpecificValue(uint32_t key, uint32_t value)
{
DBusMessage* message = dbus_message_new_method_call(
SERVICE_NAME, OBJECT_PATH,
INTERFACE_NAME, "setVendorSpecificValue");
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))
{
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);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
}
uint32_t PatchCordProxy::getVendorSpecificValue(uint32_t key)
{
DBusMessage* message = dbus_message_new_method_call(
SERVICE_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))
{
dbus_connection_flush(connection);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
const char* name;
const char* address;
dbus_uint32_t type;
dbus_uint32_t state;
dbus_uint32_t number;
dbus_uint32_t* values;
int count;
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];
dbus_pending_call_unref(pending);
}
dbus_message_unref(message);
// Each call of dbus_message_unref removes banner from the heap
banner = NULL;
return value;
}

View file

@ -0,0 +1,31 @@
// Copyright 2015 by Artem Prilutskiy
#ifndef PATCHCORDPROXY_H
#define PATCHCORDPROXY_H
#include <stdint.h>
#include <dbus/dbus.h>
class PatchCordProxy
{
public:
PatchCordProxy(uint32_t number);
~PatchCordProxy();
void setTalkerID(uint32_t value);
uint32_t getTalkerID();
private:
DBusConnection* connection;
uint32_t number;
char* banner;
void getContextBanner();
void setVendorSpecificValue(uint32_t key, uint32_t value);
uint32_t getVendorSpecificValue(uint32_t key);
};
#endif

View file

@ -0,0 +1,137 @@
// Copyright 2015 by Artem Prilutskiy
#include "UserDataStore.h"
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#define GET_ID_FOR_CALL 0
#define GET_CREDENTIALS_FOR_ID 1
UserDataStore::UserDataStore(const char* file) :
connection(NULL)
{
const char* queries[] =
{
// GET_ID_FOR_CALL
"SELECT `ID`"
" FROM Users"
" WHERE `Call` = ?"
" ORDER BY `Priority`"
" LIMIT 1",
// GET_CREDENTIALS_FOR_ID
"SELECT `Call`, `Text`"
" FROM Users"
" WHERE `ID` = ?",
};
connection = mysql_init(NULL);
mysql_options(connection, MYSQL_READ_DEFAULT_FILE, realpath(file, NULL));
mysql_real_connect(connection, NULL, NULL, NULL, NULL, 0, NULL, CLIENT_REMEMBER_OPTIONS);
my_bool active = true;
mysql_options(connection, MYSQL_OPT_RECONNECT, &active);
for (size_t index = 0; index < PREPARED_STATEMENT_COUNT; index ++)
{
const char* query = queries[index];
statements[index] = mysql_stmt_init(connection);
mysql_stmt_prepare(statements[index], query, strlen(query));
}
const char* error = mysql_error(connection);
if ((error != NULL) && (*error != '\0'))
syslog(LOG_CRIT, "MySQL error: %s\n", error);
}
UserDataStore::~UserDataStore()
{
for (size_t index = 0; index < PREPARED_STATEMENT_COUNT; index ++)
mysql_stmt_close(statements[index]);
mysql_close(connection);
}
// Public methods
uint32_t UserDataStore::getPrivateIDForCall(const char* call)
{
uint32_t number;
size_t length = strlen(call);
return
execute(GET_ID_FOR_CALL, 1, 1,
MYSQL_TYPE_STRING, call, length,
MYSQL_TYPE_LONG, &number) *
number;
}
bool UserDataStore::getCredentialsForID(uint32_t number, char* call, char* text)
{
return
execute(GET_CREDENTIALS_FOR_ID, 1, 2,
MYSQL_TYPE_LONG, &number,
MYSQL_TYPE_STRING, call, LONG_CALLSIGN_LENGTH + 1,
MYSQL_TYPE_STRING, text, SLOW_DATA_TEXT_LENGTH + 1);
}
// Internals
MYSQL_BIND* UserDataStore::bind(int count, va_list* arguments)
{
MYSQL_BIND* bindings = NULL;
if (count > 0)
{
bindings = (MYSQL_BIND*)calloc(count, sizeof(MYSQL_BIND));
for (int index = 0; index < count; index ++)
{
int type = va_arg(*arguments, int);
bindings[index].buffer_type = (enum_field_types)type;
bindings[index].buffer = va_arg(*arguments, void*);
if ((type == MYSQL_TYPE_STRING) ||
(type == MYSQL_TYPE_DATETIME))
{
bindings[index].buffer_length = va_arg(*arguments, int);
bindings[index].length = &bindings[index].buffer_length;
}
}
}
return bindings;
}
bool UserDataStore::execute(int index, int count1, int count2, ...)
{
bool result = true;
MYSQL_STMT* statement = statements[index];
va_list arguments;
va_start(arguments, count2);
MYSQL_BIND* parameters = bind(count1, &arguments);
MYSQL_BIND* columns = bind(count2, &arguments);
va_end(arguments);
if ((parameters && mysql_stmt_bind_param(statement, parameters)) ||
(columns && mysql_stmt_bind_result(statement, columns)) ||
mysql_stmt_execute(statement) ||
(columns && mysql_stmt_store_result(statement)))
{
const char* error = mysql_stmt_error(statement);
syslog(LOG_CRIT, "MySQL error while processing statement %d: %s\n", index, error);
result = false;
}
if (result && columns)
{
result = !mysql_stmt_fetch(statement);
mysql_stmt_free_result(statement);
}
free(columns);
free(parameters);
return result;
}

View file

@ -0,0 +1,35 @@
// Copyright 2015 by Artem Prilutskiy
#ifndef USERDATASTORE_H
#define USERDATASTORE_H
#include <stdint.h>
#include <stdarg.h>
#include <mysql/mysql.h>
#define PREPARED_STATEMENT_COUNT 2
#define LONG_CALLSIGN_LENGTH 8
#define SLOW_DATA_TEXT_LENGTH 20
class UserDataStore
{
public:
UserDataStore(const char* file);
~UserDataStore();
uint32_t getPrivateIDForCall(const char* call);
bool getCredentialsForID(uint32_t number, char* call, char* text);
protected:
MYSQL* connection;
MYSQL_STMT* statements[PREPARED_STATEMENT_COUNT];
MYSQL_BIND* bind(int count, va_list* arguments);
bool execute(int index, int count1, int count2, ...);
};
#endif