Commit 49d369c4 authored by Johan Ström's avatar Johan Ström

added agoutils with uuid & portable strerror_r

Part of effort to make agoclient.cpp slimmer
parent aca76396
......@@ -10,6 +10,7 @@
#include <cerrno>
#include "agoapp.h"
#include "agoutils.h"
#include "bool.h"
#ifndef EVENTMAPFILE
......@@ -225,7 +226,7 @@ Json::Value AgoEvent::commandHandler(const Json::Value& content)
Json::Value newevent = content["eventmap"];
std::string eventuuid = content["event"].asString();
if (eventuuid.empty())
eventuuid = generateUuid();
eventuuid = agocontrol::utils::generateUuid();
AGO_TRACE() << "event content:" << newevent;
AGO_TRACE() << "event uuid:" << eventuuid;
......
......@@ -32,6 +32,7 @@
#include <boost/asio/deadline_timer.hpp>
#include "agoapp.h"
#include "agoutils.h"
#include "build_config.h"
......@@ -328,7 +329,7 @@ Json::Value AgoResolver::commandHandler(const Json::Value& content)
{
std::string roomUuid = content["room"].asString();
// if no uuid is provided, we need to generate one for a new room
if (roomUuid == "") roomUuid = generateUuid();
if (roomUuid == "") roomUuid = agocontrol::utils::generateUuid();
if (inv->setRoomName(roomUuid, content["name"].asString()))
{
// return room UUID
......@@ -429,7 +430,7 @@ Json::Value AgoResolver::commandHandler(const Json::Value& content)
// if no uuid is provided, we need to generate one for a new floorplan
if (uuid == "")
{
uuid = generateUuid();
uuid = agocontrol::utils::generateUuid();
}
if (inv->setFloorplanName(uuid, content["name"].asString()))
......
......@@ -26,6 +26,7 @@
#include <boost/tokenizer.hpp>
#include "agoapp.h"
#include "agoutils.h"
#include "agohttp/agohttp.h"
......@@ -381,7 +382,7 @@ bool AgoRpc::handleJsonRpcRequest(JsonRpcReqRep *reqRep, const Json::Value &requ
else if (method == "subscribe")
{
// Local call possible
const std::string subscriptionId = generateUuid();
const std::string subscriptionId = agocontrol::utils::generateUuid();
if (subscriptionId == "")
return jsonrpcErrorResponse(responseRoot, JSONRPC_INTERNAL_ERROR, "Failed to generate UUID");
......
......@@ -8,6 +8,7 @@
#include <cerrno>
#include "agoapp.h"
#include "agoutils.h"
#ifndef SCENARIOMAPFILE
#define SCENARIOMAPFILE "maps/scenariomap.json"
......@@ -87,7 +88,7 @@ Json::Value AgoScenario::commandHandler(const Json::Value& content)
if(content.isMember("scenario"))
scenariouuid = content["scenario"].asString();
else
scenariouuid = generateUuid();
scenariouuid = agocontrol::utils::generateUuid();
AGO_TRACE() << "Scenario content:" << newscenario;
AGO_TRACE() << "scenario uuid:" << scenariouuid;
......
......@@ -19,6 +19,7 @@
#include "agoapp.h"
#include "base64.h"
#include "frameprovider.h"
#include "agoutils.h"
#ifndef VIDEOMAPFILE
#define VIDEOMAPFILE "maps/videomap.json"
......@@ -1210,7 +1211,7 @@ Json::Value AgoSurveillance::commandHandler(const Json::Value& content)
fillStream(stream, content);
//and save it
std::string internalid = generateUuid();
std::string internalid = agocontrol::utils::generateUuid();
streams[internalid] = stream;
videomap["streams"] = streams;
......@@ -1259,7 +1260,7 @@ Json::Value AgoSurveillance::commandHandler(const Json::Value& content)
fillTimelapse(timelapse, content);
//and save it
std::string internalid = generateUuid();
std::string internalid = agocontrol::utils::generateUuid();
timelapses[internalid] = timelapse;
videomap["timelapses"] = timelapses;
if( writeJsonFile(videomap, getConfigPath(VIDEOMAPFILE)) )
......@@ -1336,7 +1337,7 @@ Json::Value AgoSurveillance::commandHandler(const Json::Value& content)
fillMotion(motion, content_);
//and save it
std::string internalid = generateUuid();
std::string internalid = agocontrol::utils::generateUuid();
motions[internalid] = motion;
if( writeJsonFile(videomap, getConfigPath(VIDEOMAPFILE)) )
{
......
......@@ -29,6 +29,7 @@
#include "Telegram.h"
#include "agoapp.h"
#include "agoutils.h"
#ifndef KNXDEVICEMAPFILE
#define KNXDEVICEMAPFILE "maps/knx.json"
......@@ -386,7 +387,7 @@ Json::Value AgoKnx::commandHandler(const Json::Value& content) {
if(content.isMember("device"))
deviceuuid = content["device"].asString();
else
deviceuuid = generateUuid();
deviceuuid = agocontrol::utils::generateUuid();
/* XXX: No control over what's feed in here.
* Web UI currently sends something like this:
......
......@@ -17,6 +17,7 @@
#include "wsaapi.h"
#include "agoclient.h"
#include "agoutils.h"
using namespace agocontrol;
......@@ -494,7 +495,7 @@ int main (int argc, char ** argv)
printf("Sending probes\n");
for (int i=0;i<2;i++) {
std::string tmpuuid = "urn:uuid:" + generateUuid();
std::string tmpuuid = "urn:uuid:" + agocontrol::utils::generateUuid();
wsddProxy *discoverProxy = new wsddProxy("soap.udp://239.255.255.250:3702/");
discoverProxy->soap_header((char*)tmpuuid.c_str(), NULL, NULL, NULL, NULL, (char*)"urn:schemas-xmlsoap-org:ws:2005:04:discovery", (char*)"http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
......
......@@ -10,6 +10,7 @@ set(LIBAGOCLIENT_SOURCE_FILES
agolog.cpp
agoapp.cpp
agoconfig.cpp
agoutils.cpp
base64.cpp
)
......
......@@ -16,6 +16,7 @@
#include <boost/bind.hpp>
#include "agoclient.h"
#include "agoutils.h"
#include "agojson.h"
#include "agojson-qpid.h"
......@@ -77,15 +78,6 @@ std::vector<std::string> agocontrol::split(const std::string &s, char delimiter)
return elements;
}
// generates a uuid as std::string via libuuid
std::string agocontrol::generateUuid() {
std::unique_ptr<char> name(new char[38]);
uuid_t tmpuuid;
uuid_generate(tmpuuid);
uuid_unparse(tmpuuid, name.get());
return std::string(name.get());
}
std::string agocontrol::uint64ToString(uint64_t i) {
std::stringstream tmp;
tmp << i;
......@@ -350,7 +342,7 @@ bool agocontrol::AgoConnection::addDevice(const std::string& internalId, const s
bool agocontrol::AgoConnection::addDevice(const std::string& internalId, const std::string& deviceType, const std::string& initialName) {
if (internalIdToUuid(internalId).size()==0) {
// need to generate new uuid
uuidMap[generateUuid()] = internalId;
uuidMap[agocontrol::utils::generateUuid()] = internalId;
storeUuidMap();
}
Json::Value device(Json::ValueType::objectValue);
......
......@@ -18,7 +18,6 @@
#include "agoproto.h"
#include <chrono>
#include <uuid/uuid.h>
namespace agocontrol {
bool nameval(const std::string& in, std::string& name, std::string& value);
......@@ -30,9 +29,6 @@ namespace agocontrol {
std::vector<std::string> split(const std::string &s, char delimiter);
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
/// helper to generate a string containing a uuid.
std::string generateUuid();
/// helper for conversions
std::string uint64ToString(uint64_t i);
unsigned int stringToUint(const std::string& v);
......
#include "agoutils.h"
#include <string>
#include <memory>
#include <string.h> // for strerror_r
#include <uuid/uuid.h>
std::string agocontrol::utils::generateUuid() {
std::unique_ptr<char> name(new char[38]);
uuid_t tmpuuid;
uuid_generate(tmpuuid);
uuid_unparse(tmpuuid, name.get());
return std::string(name.get());
}
/*
* Thread-safe and portable version of strerror_r
* On some platforms, strerror returns a char*, and on some others a int code.
*
* Using this templatized solution, the compiler picks the right one..
* except we get unused warnings.. use the macros suggested in Linux manpage.
*
* https://stackoverflow.com/questions/41953104/strerror-r-is-incorrectly-declared-on-alpine-linux
*/
char* check_error(int result, char* buffer, int err) {
if(result)
sprintf(buffer, "unknown error: %d", err);
return buffer;
}
char* check_error(char* result, char*, int) {
return result;
}
std::string agocontrol::utils::strerror(int errno_) {
char buffer[1024];
return std::string(check_error(strerror_r(errno_, buffer, 1024), buffer, errno_));
}
#ifndef AGOCONTROL_AGOUTILS_H
#define AGOCONTROL_AGOUTILS_H
#include <string>
namespace agocontrol {
namespace utils {
/**
* Helper to generate a UUID as a string.
* @return
*/
std::string generateUuid();
/**
* A portable & threadsafe version of strerror, for simpler C++ use.
*/
std::string strerror(int errno_);
}
}
#endif //AGOCONTROL_AGOUTILS_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment