diff options
author | Scott Murray <scott.murray@konsulko.com> | 2019-08-30 11:40:23 -0400 |
---|---|---|
committer | Scott Murray <scott.murray@konsulko.com> | 2019-08-30 11:42:40 -0400 |
commit | f14ecdd52975ae365af5ed32648bf55dddacb8d9 (patch) | |
tree | e33d2af8d2e129b372caf34afac37366b641553a /libnavi | |
parent | dd61aa309b3a082488c9d3c383c1601fd3efbc2a (diff) |
Initial check in
Initial check in of contents of the original repository:
git://github.com/AGLExport/agl-service-navigation
as of commit 1f1ffc92fcc882aa5e885badbc91a3384f5d77b1.
Bug-AGL: SPEC-2787
Signed-off-by: Scott Murray <scott.murray@konsulko.com>
Change-Id: I02d008ee73cdfd88f1e7587ee57101187b9c4d6d
Diffstat (limited to 'libnavi')
-rw-r--r-- | libnavi/include/BinderClient.h | 57 | ||||
-rw-r--r-- | libnavi/include/JsonRequestGenerator.h | 29 | ||||
-rw-r--r-- | libnavi/include/JsonResponseAnalyzer.h | 25 | ||||
-rw-r--r-- | libnavi/include/RequestManage.h | 64 | ||||
-rw-r--r-- | libnavi/include/RequestManageListener.h | 17 | ||||
-rw-r--r-- | libnavi/include/libnavicore.hpp | 69 | ||||
-rw-r--r-- | libnavi/include/traces.h | 38 | ||||
-rw-r--r-- | libnavi/src/BinderClient.cpp | 315 | ||||
-rw-r--r-- | libnavi/src/JsonRequestGenerator.cpp | 188 | ||||
-rw-r--r-- | libnavi/src/JsonResponseAnalyzer.cpp | 254 | ||||
-rw-r--r-- | libnavi/src/RequestManage.cpp | 204 | ||||
-rw-r--r-- | libnavi/src/navicore.cpp | 81 | ||||
-rw-r--r-- | libnavi/src/navicorelistener.cpp | 28 |
13 files changed, 1369 insertions, 0 deletions
diff --git a/libnavi/include/BinderClient.h b/libnavi/include/BinderClient.h new file mode 100644 index 0000000..70a6558 --- /dev/null +++ b/libnavi/include/BinderClient.h @@ -0,0 +1,57 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#pragma once + +#include <map> +#include <tuple> +#include <vector> +#include <string> + +#include "libnavicore.hpp" + +#include "RequestManageListener.h" +#include "RequestManage.h" + +#define API_NAME "naviapi" + +/** + * @brief API name + */ +#define VERB_GETPOSITION "navicore_getposition" +#define VERB_GETALLROUTES "navicore_getallroutes" +#define VERB_CREATEROUTE "navicore_createroute" +#define VERB_PAUSESIMULATION "navicore_pausesimulation" +#define VERB_SETSIMULATIONMODE "navicore_setsimulationmode" +#define VERB_CANCELROUTECALCULATION "navicore_cancelroutecalculation" +#define VERB_SETWAYPOINTS "navicore_setwaypoints" +#define VERB_CALCULATEROUTE "navicore_calculateroute" +#define VERB_GETALLSESSIONS "navicore_getallsessions" + +/** + * @brief Binder client class + */ +class BinderClient : public RequestManageListener +{ +public: + BinderClient(); + ~BinderClient(); + + bool ConnectServer(std::string url , naviapi::NavicoreListener* listener); + void NavicoreGetPosition(const std::vector< int32_t >& valuesToReturn); + void NavicoreGetAllRoutes(); + void NavicoreCreateRoute(const uint32_t& sessionHandle); + void NavicorePauseSimulation(const uint32_t& sessionHandle); + void NavicoreSetSimulationMode(const uint32_t& sessionHandle, const bool& activate); + void NavicoreCancelRouteCalculation(const uint32_t& sessionHandle, const uint32_t& routeHandle); + void NavicoreSetWaypoints(const uint32_t& sessionHandle, const uint32_t& routeHandle, const bool& startFromCurrentPosition, const std::vector<naviapi::Waypoint>& waypointsList); + void NavicoreCalculateRoute(const uint32_t& sessionHandle, const uint32_t& routeHandle); + void NavicoreGetAllSessions(); + +private: + void OnReply(struct json_object *reply); + +private: + naviapi::NavicoreListener* navicoreListener; + RequestManage* requestMng; +}; + diff --git a/libnavi/include/JsonRequestGenerator.h b/libnavi/include/JsonRequestGenerator.h new file mode 100644 index 0000000..7cd6979 --- /dev/null +++ b/libnavi/include/JsonRequestGenerator.h @@ -0,0 +1,29 @@ +// Copyright 2017 AW SOFTWARE CO.,LTD +// Copyright 2017 AISIN AW CO.,LTD + +#pragma once + +#include <stdint.h> +#include <string> +#include <vector> + +#include "libnavicore.hpp" + +/** +* @brief Class for generating Json request +*/ +class JsonRequestGenerator +{ +public: + static std::string CreateRequestGetPosition(const std::vector< int32_t >& valuesToReturn); + static std::string CreateRequestGetAllRoutes(); + static std::string CreateRequestCreateRoute(const uint32_t* sessionHandle); + static std::string CreateRequestPauseSimulation(const uint32_t* sessionHandle); + static std::string CreateRequestSetSimulationMode(const uint32_t* sessionHandle, const bool* activate); + static std::string CreateRequestCancelRouteCalculation(const uint32_t* sessionHandle, const uint32_t* routeHandle); + static std::string CreateRequestSetWaypoints(const uint32_t* sessionHandle, const uint32_t* routeHandle, + const bool* startFromCurrentPosition, const std::vector<naviapi::Waypoint>* waypointsList); + static std::string CreateRequestCalculateroute(const uint32_t* sessionHandle, const uint32_t* routeHandle); + static std::string CreateRequestGetAllSessions(); +}; + diff --git a/libnavi/include/JsonResponseAnalyzer.h b/libnavi/include/JsonResponseAnalyzer.h new file mode 100644 index 0000000..50b2cd2 --- /dev/null +++ b/libnavi/include/JsonResponseAnalyzer.h @@ -0,0 +1,25 @@ +// Copyright 2017 AW SOFTWARE CO.,LTD +// Copyright 2017 AISIN AW CO.,LTD + +#pragma once + +#include <json-c/json.h> +#include <stdint.h> +#include <string> +#include <vector> +#include <map> + +#include "libnavicore.hpp" + +/** +* @brief JSON response analysis class +*/ +class JsonResponseAnalyzer +{ +public: + static std::map< int32_t, naviapi::variant > AnalyzeResponseGetPosition( std::string& res_json ); + static std::vector< uint32_t > AnalyzeResponseGetAllRoutes( std::string& res_json ); + static uint32_t AnalyzeResponseCreateRoute( std::string& res_json ); + static std::map<uint32_t, std::string> AnalyzeResponseGetAllSessions( std::string& res_json ); +}; + diff --git a/libnavi/include/RequestManage.h b/libnavi/include/RequestManage.h new file mode 100644 index 0000000..05ffcdf --- /dev/null +++ b/libnavi/include/RequestManage.h @@ -0,0 +1,64 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#pragma once + +#include <stdint.h> +#include <string> +#include <pthread.h> + +extern "C" { + #include <afb/afb-wsj1.h> + #include <afb/afb-ws-client.h> +} + +#include "RequestManageListener.h" + +/** +* @brief Class for request +*/ +class RequestManage +{ +public: + pthread_cond_t cond; + pthread_mutex_t mutex; + + struct afb_wsj1* wsj1; + std::string* requestURL; + struct afb_wsj1_itf wsj1_itf; + +private: + RequestManageListener* listener; + int request_cnt; + uint32_t sessionHandle; + uint32_t routeHandle; + + // Function called from thread + static void* BinderThread(void* param); + + // Callback function + void OnReply(struct afb_wsj1_msg *msg); + void OnHangup(struct afb_wsj1 *wsj1); + void OnCallStatic(const char *api, const char *verb, struct afb_wsj1_msg *msg); + void OnEventStatic(const char *event, struct afb_wsj1_msg *msg); + + static void OnReplyStatic(void *closure, struct afb_wsj1_msg *msg); + static void OnHangupStatic(void *closure, struct afb_wsj1 *wsj1); + static void OnCallStatic(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg); + static void OnEventStatic(void *closure, const char *event, struct afb_wsj1_msg *msg); + +// ================================================================================================== +// public +// ================================================================================================== +public: + RequestManage(); + ~RequestManage(); + + bool Connect(const char* api_url, RequestManageListener* listener); + bool IsConnect(); + bool CallBinderAPI(const char *api, const char *verb, const char *object); + void SetSessionHandle(uint32_t session); + uint32_t GetSessionHandle(); + void SetRouteHandle(uint32_t route); + uint32_t GetRouteHandle(); +}; + diff --git a/libnavi/include/RequestManageListener.h b/libnavi/include/RequestManageListener.h new file mode 100644 index 0000000..3b0c932 --- /dev/null +++ b/libnavi/include/RequestManageListener.h @@ -0,0 +1,17 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#pragma once + +#include <json-c/json.h> + +class RequestManageListener +{ +public: + RequestManageListener() { + } + virtual ~RequestManageListener() { + } + + virtual void OnReply(struct json_object *reply) = 0; +}; + diff --git a/libnavi/include/libnavicore.hpp b/libnavi/include/libnavicore.hpp new file mode 100644 index 0000000..66200d9 --- /dev/null +++ b/libnavi/include/libnavicore.hpp @@ -0,0 +1,69 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#pragma once + +#include <map> +#include <string> +#include <tuple> +#include <vector> + +#include <stdint.h> + +namespace naviapi { + +static const uint32_t NAVICORE_TIMESTAMP = 0x0010; +static const uint32_t NAVICORE_LATITUDE = 0x00a0; +static const uint32_t NAVICORE_LONGITUDE = 0x00a1; +static const uint32_t NAVICORE_HEADING = 0x00a3; +static const uint32_t NAVICORE_SPEED = 0x00a4; +static const uint32_t NAVICORE_SIMULATION_MODE = 0x00e3; + +typedef union +{ + bool _bool; + int32_t _int32_t; + uint32_t _uint32_t; + double _double; +} variant; + +typedef std::tuple<double, double> Waypoint; + +class NavicoreListener +{ +public: + NavicoreListener(); + virtual ~NavicoreListener(); + + virtual void getAllSessions_reply(const std::map< uint32_t, std::string >& allSessions); + virtual void getPosition_reply(std::map< int32_t, variant > position); + virtual void getAllRoutes_reply(std::vector< uint32_t > allRoutes); + virtual void createRoute_reply(uint32_t routeHandle); +}; // class NavicoreListener + +class Navicore +{ +private: + NavicoreListener* mListener; + +public: + Navicore(); + virtual ~Navicore(); + + bool connect(int argc, char *argv[], NavicoreListener* listener); + void disconnect(); + + void getAllSessions(); + void getPosition(std::vector<int32_t> params); + void getAllRoutes(); + void createRoute(uint32_t session); + + void pauseSimulation(uint32_t session); + void setSimulationMode(uint32_t session, bool activate); + void cancelRouteCalculation(uint32_t session, uint32_t routeHandle); + void setWaypoints(uint32_t session, uint32_t routeHandle, bool flag, std::vector<Waypoint>); + void calculateRoute(uint32_t session, uint32_t routeHandle); + +}; // class Navicore + +}; // namespace naviapi + diff --git a/libnavi/include/traces.h b/libnavi/include/traces.h new file mode 100644 index 0000000..a1f96ec --- /dev/null +++ b/libnavi/include/traces.h @@ -0,0 +1,38 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#ifndef __TRACE_H__ +#define __TRACE_H__ + +#include <stdio.h> + +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define PURPLE "\033[35m" +#define DGREEN "\033[6m" +#define WHITE "\033[7m" +#define CYAN "\x1b[36m" +#define NONE "\033[0m" + +#ifdef NDEBUG + +#define TRACE_DEBUG_JSON(fmt, args...) +#define TRACE_DEBUG(fmt, args...) +#define TRACE_INFO(fmt, args...) +#define TRACE_WARN(fmt, args...) +#define TRACE_ERROR(fmt, args...) + +#else + +#define TRACE_DEBUG(fmt, args...) do { fprintf(stderr, "[%s:%d] " CYAN "DEBUG" NONE ": " fmt "\n", __func__, __LINE__, ##args); } while(0) +#define TRACE_INFO(fmt, args...) do { fprintf(stderr, "[%s:%d] " GREEN "INFO" NONE ": " fmt "\n", __func__, __LINE__, ##args); } while(0) +#define TRACE_WARN(fmt, args...) do { fprintf(stderr, "[%s:%d] " YELLOW "WARN" NONE": " fmt "\n", __func__, __LINE__, ##args); } while(0) +#define TRACE_ERROR(fmt, args...) do { fprintf(stderr, "[%s:%d] " RED "ERROR" NONE ": " fmt "\n", __func__, __LINE__, ##args); } while(0) + +#define TRACE_DEBUG_JSON(fmt, args...) + +#endif + +#endif // __TRACE_H__ diff --git a/libnavi/src/BinderClient.cpp b/libnavi/src/BinderClient.cpp new file mode 100644 index 0000000..1e1e9e9 --- /dev/null +++ b/libnavi/src/BinderClient.cpp @@ -0,0 +1,315 @@ +// Copyright 2017 AW SOFTWARE CO.,LTD +// Copyright 2017 AISIN AW CO.,LTD + +#include <cstring> + +#include "BinderClient.h" +#include "JsonRequestGenerator.h" +#include "JsonResponseAnalyzer.h" + +#include "traces.h" + +/** + * @brief constructor + */ +BinderClient::BinderClient() : navicoreListener(nullptr) +{ + requestMng = new RequestManage(); +} + +/** + * @brief Destructor + */ +BinderClient::~BinderClient() +{ + delete requestMng; +} + +/** + * @brief Connect with the Binder server + */ +bool BinderClient::ConnectServer(std::string url, naviapi::NavicoreListener* listener) +{ + this->navicoreListener = listener; + + if( !requestMng->Connect(url.c_str(), this)) + { + TRACE_ERROR("cannot connect to binding service.\n"); + return false; + } + + return true; +} + +/** + * @brief Call Genivi's GetPosition via Binder and get the result + */ +void BinderClient::NavicoreGetPosition(const std::vector< int32_t >& valuesToReturn) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + std::string req_json = JsonRequestGenerator::CreateRequestGetPosition(valuesToReturn); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_GETPOSITION, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_getposition success.\n"); + } + else + { + TRACE_ERROR("navicore_getposition failed.\n"); + } + } +} + +/** + * @brief Get route handle + */ +void BinderClient::NavicoreGetAllRoutes() +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + std::string req_json = JsonRequestGenerator::CreateRequestGetAllRoutes(); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_GETALLROUTES, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_getallroutes success.\n"); + } + else + { + TRACE_ERROR("navicore_getallroutes failed.\n"); + } + } +} + +/** + * @brief Generate route handle + */ +void BinderClient::NavicoreCreateRoute(const uint32_t& sessionHandle) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + uint32_t session = requestMng->GetSessionHandle(); + std::string req_json = JsonRequestGenerator::CreateRequestCreateRoute(&session); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_CREATEROUTE, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_createroute success.\n"); + } + else + { + TRACE_ERROR("navicore_createroute failed.\n"); + } + } +} + +/** + * @brief Pause demo + */ +void BinderClient::NavicorePauseSimulation(const uint32_t& sessionHandle) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + uint32_t session = requestMng->GetSessionHandle(); + std::string req_json = JsonRequestGenerator::CreateRequestPauseSimulation(&session); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_PAUSESIMULATION, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_pausesimulationmode success.\n"); + } + else + { + TRACE_ERROR("navicore_pausesimulationmode failed.\n"); + } + } +} + +/** + * @brief Simulation mode setting + */ +void BinderClient::NavicoreSetSimulationMode(const uint32_t& sessionHandle, const bool& activate) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + uint32_t session = requestMng->GetSessionHandle(); + std::string req_json = JsonRequestGenerator::CreateRequestSetSimulationMode(&session, &activate); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_SETSIMULATIONMODE, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_setsimulationmode success.\n"); + } + else + { + TRACE_ERROR("navicore_setsimulationmode failed.\n"); + } + } +} + +/** + * @brief Delete route information + */ +void BinderClient::NavicoreCancelRouteCalculation(const uint32_t& sessionHandle, const uint32_t& routeHandle) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + uint32_t session = requestMng->GetSessionHandle(); + std::string req_json = JsonRequestGenerator::CreateRequestCancelRouteCalculation(&session, &routeHandle); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_CANCELROUTECALCULATION, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_cancelroutecalculation success.\n"); + } + else + { + TRACE_ERROR("navicore_cancelroutecalculation failed.\n"); + } + } +} + +/** + * @brief Destination setting + */ +void BinderClient::NavicoreSetWaypoints(const uint32_t& sessionHandle, const uint32_t& routeHandle, const bool& startFromCurrentPosition, const std::vector<naviapi::Waypoint>& waypointsList) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + uint32_t session = requestMng->GetSessionHandle(); + uint32_t route = requestMng->GetRouteHandle(); + std::string req_json = JsonRequestGenerator::CreateRequestSetWaypoints(&session, &route, + &startFromCurrentPosition, &waypointsList); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_SETWAYPOINTS, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_setwaypoints success.\n"); + } + else + { + TRACE_ERROR("navicore_setwaypoints failed.\n"); + } + } +} + +/** + * @brief Route calculation processing + */ +void BinderClient::NavicoreCalculateRoute(const uint32_t& sessionHandle, const uint32_t& routeHandle) +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + uint32_t session = requestMng->GetSessionHandle(); + uint32_t route = requestMng->GetRouteHandle(); + std::string req_json = JsonRequestGenerator::CreateRequestCalculateroute(&session, &route); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_CALCULATEROUTE, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_calculateroute success.\n"); + } + else + { + TRACE_ERROR("navicore_calculateroute failed.\n"); + } + } +} + +/** + * @brief Retrieve session information + * @return Map of session information + */ +void BinderClient::NavicoreGetAllSessions() +{ + // Check if it is connected + if( requestMng->IsConnect() ) + { + // JSON request generation + std::string req_json = JsonRequestGenerator::CreateRequestGetAllSessions(); + + // Send request + if( requestMng->CallBinderAPI(API_NAME, VERB_GETALLSESSIONS, req_json.c_str()) ) + { + TRACE_DEBUG("navicore_getallsessions success.\n"); + } + else + { + TRACE_ERROR("navicore_getallsessions failed.\n"); + } + } +} + +void BinderClient::OnReply(struct json_object* reply) +{ + struct json_object* requestObject = nullptr; + json_object_object_get_ex(reply, "request", &requestObject); + + struct json_object* infoObject = nullptr; + json_object_object_get_ex(requestObject, "info", &infoObject); + + const char* info = json_object_get_string(infoObject); + + char tmpVerb[256]; + strcpy(tmpVerb, info); + + // Create a new JSON response + const char* json_str = json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY); + std::string response_json = std::string( json_str ); + + if (strcmp(VERB_GETALLSESSIONS, tmpVerb) == 0) + { + std::map<uint32_t, std::string> ret = JsonResponseAnalyzer::AnalyzeResponseGetAllSessions(response_json); + + // keep session handle + requestMng->SetSessionHandle( ret.begin()->first ); + + this->navicoreListener->getAllSessions_reply(ret); + } + else if (strcmp(VERB_GETPOSITION, tmpVerb) == 0) + { + std::map< int32_t, naviapi::variant > ret = JsonResponseAnalyzer::AnalyzeResponseGetPosition(response_json); + + this->navicoreListener->getPosition_reply(ret); + } + else if (strcmp(VERB_GETALLROUTES, tmpVerb) == 0) + { + std::vector< uint32_t > ret = JsonResponseAnalyzer::AnalyzeResponseGetAllRoutes(response_json); + + // route handle + if(ret.size() > 0) + { + requestMng->SetRouteHandle(ret[0]); + } + + this->navicoreListener->getAllRoutes_reply(ret); + } + else if (strcmp(VERB_CREATEROUTE, tmpVerb) == 0) + { + uint32_t ret = JsonResponseAnalyzer::AnalyzeResponseCreateRoute(response_json); + + // keep route handle + requestMng->SetRouteHandle(ret); + + this->navicoreListener->createRoute_reply(ret); + } +} + diff --git a/libnavi/src/JsonRequestGenerator.cpp b/libnavi/src/JsonRequestGenerator.cpp new file mode 100644 index 0000000..09d68c0 --- /dev/null +++ b/libnavi/src/JsonRequestGenerator.cpp @@ -0,0 +1,188 @@ +// Copyright 2017 AW SOFTWARE CO.,LTD +// Copyright 2017 AISIN AW CO.,LTD + +#include <json-c/json.h> +#include <traces.h> +#include "JsonRequestGenerator.h" + +/** + * @brief Generate request for navicore_getposition + * @param valuesToReturn Key information you want to obtain + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestGetPosition(const std::vector< int32_t >& valuesToReturn) +{ + std::vector< int32_t >::const_iterator itr; + + struct json_object* request_json = json_object_new_object(); + struct json_object* json_array = json_object_new_array(); + + for (itr = valuesToReturn.begin(); itr != valuesToReturn.end(); itr++) + { + json_object_array_add(json_array, json_object_new_int(*itr)); + } + + json_object_object_add(request_json, "valuesToReturn", json_array); + TRACE_DEBUG("CreateRequestGetPosition request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_getallroutes + * @return json strin + */ +std::string JsonRequestGenerator::CreateRequestGetAllRoutes() +{ + // Request is empty and OK + struct json_object* request_json = json_object_new_object(); + TRACE_DEBUG("CreateRequestGetAllRoutes request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_createroute + * @param sessionHandle session handle + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestCreateRoute(const uint32_t* sessionHandle) +{ + struct json_object* request_json = json_object_new_object(); + json_object_object_add(request_json, "sessionHandle", json_object_new_int(*sessionHandle)); + TRACE_DEBUG("CreateRequestCreateRoute request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_pausesimulation + * @param sessionHandle session handle + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestPauseSimulation(const uint32_t* sessionHandle) +{ + struct json_object* request_json = json_object_new_object(); + // sessionHandle + json_object_object_add(request_json, "sessionHandle", json_object_new_int(*sessionHandle)); + TRACE_DEBUG("CreateRequestPauseSimulation request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_pausesimulation + * @param sessionHandle session handle + * @param active Simulation state + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestSetSimulationMode(const uint32_t* sessionHandle, const bool* activate) +{ + struct json_object* request_json = json_object_new_object(); + + // "sessionHandle" + json_object_object_add(request_json, "sessionHandle", json_object_new_int(*sessionHandle)); + + // "simulationMode" + json_object_object_add(request_json, "simulationMode", json_object_new_boolean(*activate)); + TRACE_DEBUG("CreateRequestSetSimulationMode request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_pausesimulation + * @param sessionHandle session handle + * @param routeHandle route handle + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestCancelRouteCalculation(const uint32_t* sessionHandle, const uint32_t* routeHandle) +{ + struct json_object* request_json = json_object_new_object(); + + // "sessionHandle" + json_object_object_add(request_json, "sessionHandle", json_object_new_int(*sessionHandle)); + + // "route" + json_object_object_add(request_json, "route", json_object_new_int(*routeHandle)); + TRACE_DEBUG("CreateRequestCancelRouteCalculation request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_setwaypoints + * @param sessionHandle session handle + * @param routeHandle route handle + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestSetWaypoints(const uint32_t* sessionHandle, const uint32_t* routeHandle, + const bool* startFromCurrentPosition, const std::vector<naviapi::Waypoint>* waypointsList) +{ + naviapi::Waypoint destWp; + + struct json_object* request_json = json_object_new_object(); + struct json_object* json_array = json_object_new_array(); + + // "sessionHandle" + json_object_object_add(request_json, "sessionHandle", json_object_new_int(*sessionHandle)); + + // "route" + json_object_object_add(request_json, "route", json_object_new_int(*routeHandle)); + + // "startFromCurrentPosition" + json_object_object_add(request_json, "startFromCurrentPosition", json_object_new_boolean(*startFromCurrentPosition)); + + // "latitude", "longitude" + std::vector<naviapi::Waypoint>::const_iterator it; + for (it = waypointsList->begin(); it != waypointsList->end(); ++it) + { + struct json_object* destpoint = json_object_new_object(); + + double latitude = std::get<0>(*it); + json_object_object_add(destpoint, "latitude", json_object_new_double(latitude)); + + double longitude = std::get<1>(*it); + json_object_object_add(destpoint, "longitude", json_object_new_double(longitude)); + + json_object_array_add(json_array, destpoint); + } + + json_object_object_add(request_json, "", json_array); + TRACE_DEBUG("CreateRequestSetWaypoints request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_calculateroute + * @param sessionHandle session handle + * @param routeHandle route handle + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestCalculateroute(const uint32_t* sessionHandle, const uint32_t* routeHandle) +{ + struct json_object* request_json = json_object_new_object(); + // "sessionHandle" + json_object_object_add(request_json, "sessionHandle", json_object_new_int(*sessionHandle)); + + // "route" + json_object_object_add(request_json, "route", json_object_new_int(*routeHandle)); + TRACE_DEBUG("CreateRequestCalculateroute request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + +/** + * @brief Generate request for navicore_getallsessions + * @return json string + */ +std::string JsonRequestGenerator::CreateRequestGetAllSessions() +{ + // Request is empty and OK + struct json_object* request_json = json_object_new_object(); + TRACE_DEBUG("CreateRequestGetAllSessions request_json:\n%s\n", json_object_to_json_string(request_json)); + + return std::string( json_object_to_json_string( request_json ) ); +} + diff --git a/libnavi/src/JsonResponseAnalyzer.cpp b/libnavi/src/JsonResponseAnalyzer.cpp new file mode 100644 index 0000000..b0d943f --- /dev/null +++ b/libnavi/src/JsonResponseAnalyzer.cpp @@ -0,0 +1,254 @@ +// Copyright 2017 AW SOFTWARE CO.,LTD +// Copyright 2017 AISIN AW CO.,LTD + +#include <traces.h> + +#include "JsonResponseAnalyzer.h" + +/** + * @brief Response analysis of navicore_getallroutes + * @param res_json JSON string of response + * @return Map information for the key sent in the request + */ +std::map< int32_t, naviapi::variant > JsonResponseAnalyzer::AnalyzeResponseGetPosition( std::string& res_json ) +{ + std::map< int32_t, naviapi::variant > ret; + + TRACE_DEBUG("AnalyzeResponseGetPosition json_obj:\n%s\n", json_object_to_json_string(json_obj)); + + // convert to Json Object + struct json_object *json_obj = json_tokener_parse( res_json.c_str() ); + + // Check key + struct json_object *json_map_ary = NULL; + if( json_object_object_get_ex(json_obj, "response", &json_map_ary) ) + { + // Check if the response is array information + if( json_object_is_type(json_map_ary, json_type_array) ) + { + for (int i = 0; i < json_object_array_length(json_map_ary); ++i) + { + struct json_object* j_elem = json_object_array_get_idx(json_map_ary, i); + + if( json_object_is_type( j_elem, json_type_object) ) + { + // Check key + struct json_object* key = NULL; + struct json_object* value = NULL; + if( json_object_object_get_ex(j_elem, "key", &key) + && json_object_object_get_ex(j_elem, "value", &value) ) + { + if( json_object_is_type(key, json_type_int) ) + { + uint32_t req_key = (uint32_t)json_object_get_int(key); + + switch( req_key ) + { + case naviapi::NAVICORE_LATITUDE: + ret[req_key]._double = json_object_get_double(value); + break; + + case naviapi::NAVICORE_LONGITUDE: + ret[req_key]._double = json_object_get_double(value); + break; + + case naviapi::NAVICORE_TIMESTAMP: + ret[req_key]._uint32_t = (uint32_t)json_object_get_int(value); + break; + + case naviapi::NAVICORE_HEADING: + ret[req_key]._uint32_t = (uint32_t)json_object_get_int(value); + break; + + case naviapi::NAVICORE_SPEED: + ret[req_key]._int32_t = json_object_get_int(value); + break; + + case naviapi::NAVICORE_SIMULATION_MODE: + ret[req_key]._bool = json_object_get_boolean(value); + break; + + default: + TRACE_WARN("unknown key type.\n"); + break; + } + } + } + } + else + { + TRACE_WARN("element type is not object.\n"); + break; + } + } + } + else + { + TRACE_WARN("response type is not array.\n"); + } + } + + json_object_put(json_obj); + return ret; +} + +/** + * @brief Response analysis of navicore_getallroutes + * @param res_json JSON string of response + * @return Route handle array + */ +std::vector< uint32_t > JsonResponseAnalyzer::AnalyzeResponseGetAllRoutes( std::string& res_json ) +{ + std::vector< uint32_t > routeList; + + TRACE_DEBUG("AnalyzeResponseGetAllRoutes json_obj:\n%s\n", json_object_to_json_string(json_obj)); + + // convert to Json Object + struct json_object *json_obj = json_tokener_parse( res_json.c_str() ); + + // Check key + struct json_object *json_route_ary = NULL; + if( json_object_object_get_ex(json_obj, "response", &json_route_ary) ) + { + // Check if the response is array information + if( json_object_is_type(json_route_ary, json_type_array) ) + { + for (int i = 0; i < json_object_array_length(json_route_ary); ++i) + { + struct json_object* j_elem = json_object_array_get_idx(json_route_ary, i); + + // Check that it is an element + struct json_object *json_route_obj = NULL; + if( json_object_object_get_ex(j_elem, "route", &json_route_obj) ) + { + // Check if it is an int value + if( json_object_is_type(json_route_obj, json_type_int) ) + { + uint32_t routeHandle = (uint32_t)json_object_get_int(json_route_obj); + routeList.push_back( routeHandle ); + } + else + { + TRACE_WARN("route value is not integer.\n"); + break; + } + } + else + { + TRACE_WARN("key route is not found.\n"); + break; + } + } + } + else + { + TRACE_WARN("response type is not array.\n"); + } + } + + json_object_put(json_obj); + return routeList; +} + +/** + * @brief Response analysis of navicore_createroute + * @param res_json JSON string of response + * @return Route handle + */ +uint32_t JsonResponseAnalyzer::AnalyzeResponseCreateRoute( std::string& res_json ) +{ + uint32_t routeHandle = 0; + + TRACE_DEBUG("AnalyzeResponseCreateRoute json_obj:\n%s\n", json_object_to_json_string(json_obj)); + + // convert to Json Object + struct json_object *json_obj = json_tokener_parse( res_json.c_str() ); + + // Check key + struct json_object *json_root_obj = NULL; + struct json_object *json_route_obj = NULL; + if( json_object_object_get_ex(json_obj, "response", &json_root_obj) + && json_object_object_get_ex(json_root_obj, "route", &json_route_obj) ) + { + // Check if the response is array information + if( json_object_is_type(json_route_obj, json_type_int) ) + { + // Get route handle + routeHandle = (uint32_t)json_object_get_int(json_route_obj); + } + else + { + TRACE_WARN("response type is not integer.\n"); + } + } + + json_object_put(json_obj); + return routeHandle; +} + +/** + * @brief Response analysis of navicore_getallsessions + * @param res_json JSON string of response + * @return Map of session information + */ +std::map<uint32_t, std::string> JsonResponseAnalyzer::AnalyzeResponseGetAllSessions( std::string& res_json ) +{ + std::map<uint32_t, std::string> session_map; + + TRACE_DEBUG("AnalyzeResponseGetAllSessions json_obj:\n%s\n", json_object_to_json_string(json_obj)); + + // convert to Json Object + struct json_object *json_obj = json_tokener_parse( res_json.c_str() ); + + // Check key + struct json_object *json_map_ary = NULL; + if( json_object_object_get_ex(json_obj, "response", &json_map_ary) ) + { + // Check if the response is array information + if( json_object_is_type(json_map_ary, json_type_array) ) + { + for (int i = 0; i < json_object_array_length(json_map_ary); ++i) + { + struct json_object* j_elem = json_object_array_get_idx(json_map_ary, i); + + if( json_object_is_type( j_elem, json_type_object) ) + { + // Check key + struct json_object* handle = NULL; + struct json_object* client = NULL; + if( json_object_object_get_ex(j_elem, "sessionHandle", &handle) + && json_object_object_get_ex(j_elem, "client", &client) ) + { + if( json_object_is_type(handle, json_type_int) + && json_object_is_type(client, json_type_string) ) + { + uint32_t sessionHandle = (uint32_t)json_object_get_int(handle); + std::string clientName = std::string( json_object_get_string(client) ); + + // add to map + session_map[sessionHandle] = clientName; + } + else + { + TRACE_WARN("invalid response.\n"); + break; + } + } + } + else + { + TRACE_WARN("element type is not object.\n"); + break; + } + } + } + else + { + TRACE_WARN("response type is not array.\n"); + } + } + + json_object_put(json_obj); + return session_map; +} + diff --git a/libnavi/src/RequestManage.cpp b/libnavi/src/RequestManage.cpp new file mode 100644 index 0000000..7db34e1 --- /dev/null +++ b/libnavi/src/RequestManage.cpp @@ -0,0 +1,204 @@ +// Copyright 2017 AW SOFTWARE CO.,LTD +// Copyright 2017 AISIN AW CO.,LTD + +#include <string.h> +#include <errno.h> +#include <pthread.h> +#include <time.h> +#include <unistd.h> +#include <systemd/sd-event.h> +#include <json-c/json.h> +#include <traces.h> +#include "RequestManage.h" + +/** + * @brief constructor + */ +RequestManage::RequestManage() : listener(nullptr) +{ + // Callback setting + this->wsj1_itf.on_hangup = RequestManage::OnHangupStatic; + this->wsj1_itf.on_call = RequestManage::OnCallStatic; + this->wsj1_itf.on_event = RequestManage::OnEventStatic; + + pthread_cond_init(&this->cond, nullptr); + pthread_mutex_init(&this->mutex, nullptr); +} + +/** + * @brief Destructor + */ +RequestManage::~RequestManage() +{ +} + +void* RequestManage::BinderThread(void* param) +{ + RequestManage* instance = (RequestManage*) param; + sd_event *loop; + + int rc = sd_event_default(&loop); + if (rc < 0) { + TRACE_ERROR("connection to default event loop failed: %s\n", strerror(-rc)); + return nullptr; + } + + instance->wsj1 = afb_ws_client_connect_wsj1(loop, instance->requestURL->c_str(), &instance->wsj1_itf, nullptr); + if (instance->wsj1 == nullptr) + { + TRACE_ERROR("connection to %s failed: %m\n", api_url); + return nullptr; + } + + // Signal + pthread_mutex_unlock(&instance->mutex); + pthread_cond_signal(&instance->cond); + + while (1) + { + sd_event_run(loop, 1000 * 1000); // 1sec + } + + return nullptr; +} + +/** + * @brief Connect with a service + * @param URL + * @return Success or failure of connection + */ +bool RequestManage::Connect(const char* api_url, RequestManageListener* listener) +{ + this->listener = listener; + this->requestURL = new std::string(api_url); + + pthread_t thread_id; + pthread_create(&thread_id, nullptr, RequestManage::BinderThread, (void*)this); + + // Wait until response comes + pthread_mutex_lock(&this->mutex); + pthread_cond_wait(&this->cond, &this->mutex); + pthread_mutex_unlock(&this->mutex); + + if (this->wsj1 == nullptr) + { + return false; + } + + return true; +} + +/** + * @brief Connection status check with service + * @return Connection status + */ +bool RequestManage::IsConnect(){ + return (this->wsj1 != NULL); +} + +/** + * @brief Call Binder's API + * @param api api + * @param verb method + * @param req_json Json style request + * @return Success or failure of processing + */ +bool RequestManage::CallBinderAPI(const char* api, const char* verb, const char* req_json) +{ + // Send request + int rc = afb_wsj1_call_s(this->wsj1, api, verb, req_json, RequestManage::OnReplyStatic, this); + if (rc < 0) + { + TRACE_ERROR("calling %s/%s(%s) failed: %m\n", api, verb, req_json); + return false; + } + + return true; +} + +/** + * @brief Set session handle + * @param session Session handle + */ +void RequestManage::SetSessionHandle( uint32_t session ) +{ + this->sessionHandle = session; +} + +/** + * @brief Get session handle + * @return Session handle + */ +uint32_t RequestManage::GetSessionHandle() +{ + return this->sessionHandle; +} + +/** + * @brief Set route handle + * @param route Route handle + */ +void RequestManage::SetRouteHandle( uint32_t route ) +{ + this->routeHandle = route; +} + +/** + * @brief Get route handle + * @return Route handle + */ +uint32_t RequestManage::GetRouteHandle() +{ + return this->routeHandle; +} + +void RequestManage::OnReply(struct afb_wsj1_msg *msg) +{ + struct json_object * json = afb_wsj1_msg_object_j(msg); + + this->listener->OnReply(json); +} + +void RequestManage::OnHangup(struct afb_wsj1 *wsj1) +{ +} + +void RequestManage::OnCallStatic(const char *api, const char *verb, struct afb_wsj1_msg *msg) +{ +} + +void RequestManage::OnEventStatic(const char *event, struct afb_wsj1_msg *msg) +{ +} + + +/** + * @brief Answer callback from service + */ +void RequestManage::OnReplyStatic(void *closure, struct afb_wsj1_msg *msg) +{ + RequestManage* instance = (RequestManage *)closure; + instance->OnReply(msg); +} + +/** + * @brief Service hang notification + */ +void RequestManage::OnHangupStatic(void *closure, struct afb_wsj1 *wsj1) +{ + printf("DEBUG:%s:%d (%p,%p)\n", __func__, __LINE__, closure, wsj1); + fflush(stdout); +} + +void RequestManage::OnCallStatic(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg) +{ + printf("DEBUG:%s:%d (%p,%s,%s,%p)\n", __func__, __LINE__, closure, api, verb, msg); + fflush(stdout); +} + +void RequestManage::OnEventStatic(void *closure, const char *event, struct afb_wsj1_msg *msg) +{ + printf("DEBUG:%s:%d (%p,%s,%p)\n", __func__, __LINE__, closure, event, msg); + fflush(stdout); +} + diff --git a/libnavi/src/navicore.cpp b/libnavi/src/navicore.cpp new file mode 100644 index 0000000..5402aa8 --- /dev/null +++ b/libnavi/src/navicore.cpp @@ -0,0 +1,81 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#include "libnavicore.hpp" +#include "BinderClient.h" + +static BinderClient mBinderClient; + +naviapi::Navicore::Navicore() +{ +} + +naviapi::Navicore::~Navicore() +{ +} + +bool naviapi::Navicore::connect(int argc, char *argv[], NavicoreListener* listener) +{ + this->mListener = listener; + + if (argc != 3) + { + printf("Error: argc != 3 : argc = %d\n", argc); + return false; + } + + char url[1024]; + sprintf(url, "ws://localhost:%d/api?token=%s", atoi(argv[1]), argv[2]); + + return mBinderClient.ConnectServer(url, this->mListener); +} + +void naviapi::Navicore::disconnect() +{ + // TODO +} + +void naviapi::Navicore::getAllSessions() +{ + mBinderClient.NavicoreGetAllSessions(); +} + +void naviapi::Navicore::getPosition(std::vector<int32_t> params) +{ + mBinderClient.NavicoreGetPosition(params); +} + +void naviapi::Navicore::getAllRoutes() +{ + mBinderClient.NavicoreGetAllRoutes(); +} + +void naviapi::Navicore::createRoute(uint32_t session) +{ + mBinderClient.NavicoreCreateRoute(session); +} + +void naviapi::Navicore::pauseSimulation(uint32_t session) +{ + mBinderClient.NavicorePauseSimulation(session); +} + +void naviapi::Navicore::setSimulationMode(uint32_t session, bool activate) +{ + mBinderClient.NavicoreSetSimulationMode(session, activate); +} + +void naviapi::Navicore::cancelRouteCalculation(uint32_t session, uint32_t routeHandle) +{ + mBinderClient.NavicoreCancelRouteCalculation(session, routeHandle); +} + +void naviapi::Navicore::setWaypoints(uint32_t session, uint32_t routeHandle, bool flag, std::vector<Waypoint> waypoints) +{ + mBinderClient.NavicoreSetWaypoints(session, routeHandle, flag, waypoints); +} + +void naviapi::Navicore::calculateRoute(uint32_t session, uint32_t routeHandle) +{ + mBinderClient.NavicoreCalculateRoute(session, routeHandle); +} + diff --git a/libnavi/src/navicorelistener.cpp b/libnavi/src/navicorelistener.cpp new file mode 100644 index 0000000..2aa2b5d --- /dev/null +++ b/libnavi/src/navicorelistener.cpp @@ -0,0 +1,28 @@ +// Copyright 2017 AISIN AW CO.,LTD + +#include "libnavicore.hpp" + +naviapi::NavicoreListener::NavicoreListener() +{ +} + +naviapi::NavicoreListener::~NavicoreListener() +{ +} + +void naviapi::NavicoreListener::getAllSessions_reply(const std::map< uint32_t, std::string >& allSessions) +{ +} + +void naviapi::NavicoreListener::getPosition_reply(std::map< int32_t, variant > position) +{ +} + +void naviapi::NavicoreListener::getAllRoutes_reply(std::vector< uint32_t > allRoutes) +{ +} + +void naviapi::NavicoreListener::createRoute_reply(uint32_t routeHandle) +{ +} + |