Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace parhand with axparameter #78

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ARG ARCH=armv7hf
ARG REPO=axisecp
ARG VERSION=1.7
ARG VERSION=1.14
ARG UBUNTU_VERSION=22.04

FROM arm64v8/ubuntu:${UBUNTU_VERSION} AS containerized_aarch64
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ PKG_CONFIG_CFLAGS_OTHER := $(shell PKG_CONFIG_SYSROOT_DIR="" PKG_CONFIG_PATH=$(P
PKG_CONFIG_LDFLAGS := $(shell PKG_CONFIG_SYSROOT_DIR="" PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs-only-L $(PKGS))
PKG_CONFIG_LDLIBS := $(shell PKG_CONFIG_SYSROOT_DIR="" PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs-only-l $(PKGS))

PKGS = gio-2.0 glib-2.0 vdostream
PKGS = gio-2.0 glib-2.0 vdostream axparameter
PKG_CONFIG_CFLAGS_I += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --cflags-only-I $(PKGS))
PKG_CONFIG_CFLAGS_OTHER += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --cflags-only-other $(PKGS))
PKG_CONFIG_LDFLAGS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs-only-L $(PKGS))
Expand Down
61 changes: 28 additions & 33 deletions src/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,44 @@ namespace acap_runtime {
bool Parameter::Init(const bool verbose) {
_verbose = verbose;
TRACELOG << "Init" << endl;
_error = NULL;
if (ax_parameter == NULL)
ax_parameter = ax_parameter_new(APP_NAME, &_error);
if (ax_parameter == NULL) {
ERRORLOG << "Error when creating axparameter: " << _error->message << endl;
g_clear_error(&_error);
return false;
}

return true;
}

// Logic and data behind the server's behavior.
Status Parameter::GetValues(ServerContext* context, ServerReaderWriter<Response, Request>* stream) {
char parhand_result[BUFSIZ];

Request request;
Response response;
while (stream->Read(&request)) {
size_t pos = 0;
const char* parameter_value = NULL;
string parhand_cmd = "parhandclient get ";
string parameter_key = request.key().c_str();
const regex pattern("[a-zA-Z0-9.]+");
if (regex_match(parameter_key, pattern)) {
string parhandclient_cmd = parhand_cmd + parameter_key;

FILE* fp = popen(parhandclient_cmd.c_str(), "r");
if (!fp) {
throw std::runtime_error("popen() failed!");
}
std::string value;
if (fgets(parhand_result, BUFSIZ, fp) != NULL) {
value = parhand_result;
while ((pos = value.find('"', pos)) != std::string::npos)
value = value.erase(pos, 1);
parameter_value = value.c_str();
}
if (parameter_value != nullptr) {
TRACELOG << request.key().c_str() << ": " << parameter_value << endl;
} else {
parameter_value = "";
TRACELOG << request.key().c_str() << ": " << parameter_value << endl;
}
if (ax_parameter == NULL) {
ERRORLOG << "axparameter not initalized" << endl;
return Status::CANCELLED;
}

Response response;
response.set_value(parameter_value);
stream->Write(response);
pclose(fp);
} else {
const gchar* parameter_key = request.key().c_str();
const regex pattern("[a-zA-Z0-9.]+");
if (!regex_match(parameter_key, pattern)) {
TRACELOG << "No valid input request" << endl;
return Status(StatusCode::INVALID_ARGUMENT, "No valid input request");
}
char* parameter_value = NULL;
if (!ax_parameter_get(ax_parameter, parameter_key, &parameter_value, &_error)) {
ERRORLOG << "Error when getting axparameter: " << _error->message << endl;
parameter_value = g_strdup("");
g_clear_error(&_error);
}
TRACELOG << parameter_key << ": " << parameter_value << endl;

response.set_value(parameter_value);
stream->Write(response);
free(parameter_value);
}

return Status::OK;
Expand Down
3 changes: 3 additions & 0 deletions src/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "keyvaluestore.grpc.pb.h"
#include <axsdk/axparameter.h>

#ifdef TEST
#define APP_NAME "acapruntimetest"
Expand All @@ -35,6 +36,8 @@ class Parameter final : public KeyValueStore::Service {
private:
Status GetValues(ServerContext* context, ServerReaderWriter<Response, Request>* stream);

AXParameter* ax_parameter;
GError* _error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for this to be a class member. It can be a local variable in each function it is used.

bool _verbose;
};
} // namespace acap_runtime