diff options
author | Scott Murray <scott.murray@konsulko.com> | 2019-08-30 11:40:23 -0400 |
---|---|---|
committer | Scott Murray <scott.murray@konsulko.com> | 2019-09-05 00:18:27 +0000 |
commit | da9d177514fe438edd8262226cdc770df6c9eb82 (patch) | |
tree | e33d2af8d2e129b372caf34afac37366b641553a /libnavi/src | |
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
(cherry picked from commit f14ecdd52975ae365af5ed32648bf55dddacb8d9)
Diffstat (limited to 'libnavi/src')
-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 |
6 files changed, 1070 insertions, 0 deletions
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) +{ +} + |