From 9a4ec5a5793ce904333f6087cf31d7819206c101 Mon Sep 17 00:00:00 2001 From: Raquel Medina Date: Mon, 9 Dec 2019 02:25:25 +0100 Subject: Add CBL processing CBL: Code Based Linking in the case of Alexa voice agent, or more generally Code Based Logging. It provides a url and code which allows the user to complete the authorization process and start using the authorized voice services. - add subscribeToLoginEvents verb - add cbl events handling - include new verb in vshl-core-api.json Bug-AGL: SPEC-2981 Signed-off-by: Raquel Medina Change-Id: If342d45638125682621cba707eac1d4ff5ad244c (cherry picked from commit ed834a643e3843c0f805ea33363c9f2889d2bab3) --- src/plugins/VshlCoreApi.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 4 deletions(-) (limited to 'src/plugins/VshlCoreApi.cpp') diff --git a/src/plugins/VshlCoreApi.cpp b/src/plugins/VshlCoreApi.cpp index 4c32391..abe4577 100644 --- a/src/plugins/VshlCoreApi.cpp +++ b/src/plugins/VshlCoreApi.cpp @@ -48,6 +48,7 @@ static std::string VA_JSON_ATTR_DESCRIPTION = "description"; static std::string VA_JSON_ATTR_VENDOR = "vendor"; static std::string STARTLISTENING_JSON_ATTR_REQUEST = "request_id"; +static std::string SUBSCRIBE2LOGINEVENTS_JSON_ATTR_REQUEST = "request_id"; static std::string EVENTS_JSON_ATTR_VA_ID = "va_id"; static std::string EVENTS_JSON_ATTR_EVENTS = "events"; @@ -158,6 +159,42 @@ CTLP_CAPI(onDialogStateEvent, source, argsJ, eventJ) { return 0; } +CTLP_CAPI(onLoginPairReceivedEvent, source, argsJ, eventJ) { + if (sEventRouter == nullptr) { + return -1; + } + + string eventName = vshlcore::voiceagents::VSHL_EVENT_LOGINPAIR_RECEIVED_EVENT; + json eventJson = json::parse(json_object_to_json_string(eventJ)); + if (eventJson.find(EVENTS_JSON_ATTR_VA_ID) == eventJson.end()) { + sLogger->log(Level::ERROR, TAG, "onLoginPairReceivedEvent: No voiceagent id found."); + return -1; + } + std::string voiceAgentId(eventJson[EVENTS_JSON_ATTR_VA_ID].get()); + + sEventRouter->handleIncomingEvent(eventName, voiceAgentId, json_object_to_json_string(eventJ)); + + return 0; +} + +CTLP_CAPI(onLoginPairExpiredEvent, source, argsJ, eventJ) { + if (sEventRouter == nullptr) { + return -1; + } + + string eventName = vshlcore::voiceagents::VSHL_EVENT_LOGINPAIR_EXPIRED_EVENT; + json eventJson = json::parse(json_object_to_json_string(eventJ)); + if (eventJson.find(EVENTS_JSON_ATTR_VA_ID) == eventJson.end()) { + sLogger->log(Level::ERROR, TAG, "onLoginPairExpiredEvent: No voiceagent id found."); + return -1; + } + std::string voiceAgentId(eventJson[EVENTS_JSON_ATTR_VA_ID].get()); + + sEventRouter->handleIncomingEvent(eventName, voiceAgentId, json_object_to_json_string(eventJ)); + + return 0; +} + // Helper function to add events for voice agent to controller // configuration. It relies on the controller configuration being // available via the userdata of the API pointer. @@ -208,6 +245,22 @@ static int AddVoiceAgentEvents(std::string &agentApi) { json_object_object_add(eventJ, "action", actionJ); json_object_array_add(eventsJ, eventJ); + eventJ = json_object_new_object(); + uid = agentApi + "/voice_cbl_codepair_received_event"; + uidJ = json_object_new_string(uid.c_str()); + json_object_object_add(eventJ, "uid", uidJ); + actionJ = json_object_new_string("plugin://vshl-core#onLoginPairReceivedEvent"); + json_object_object_add(eventJ, "action", actionJ); + json_object_array_add(eventsJ, eventJ); + + eventJ = json_object_new_object(); + uid = agentApi + "/voice_cbl_codepair_expired_event"; + uidJ = json_object_new_string(uid.c_str()); + json_object_object_add(eventJ, "uid", uidJ); + actionJ = json_object_new_string("plugin://vshl-core#onLoginPairExpiredEvent"); + json_object_object_add(eventJ, "action", actionJ); + json_object_array_add(eventsJ, eventJ); + // Call into controller to add event actions // NOTE: AFAICT the JSON objects end up reused by the controller data // structures, so eventsJ should not be freed with a put after @@ -299,7 +352,7 @@ CTLP_CAPI(loadVoiceAgentsConfig, source, argsJ, eventJ) { return -1; } - string defaultVoiceAgent; + std::string defaultVoiceAgent; if (argsJ != nullptr) { // Parse any agent configs from controller arguments @@ -363,7 +416,7 @@ CTLP_CAPI(startListening, source, argsJ, eventJ) { } int result = 0; - string requestId = sVRRequestProcessor->startListening(); + std::string requestId = sVRRequestProcessor->startListening(); if (!requestId.empty()) { json responseJson; @@ -384,7 +437,7 @@ CTLP_CAPI(enumerateVoiceAgents, source, argsJ, eventJ) { if (sVoiceAgentsDataManager == nullptr) { return -1; } - + auto agents = sVoiceAgentsDataManager->getAllVoiceAgents(); std::string defaultAgentId(sVoiceAgentsDataManager->getDefaultVoiceAgent()); @@ -442,7 +495,7 @@ CTLP_CAPI(subscribe, source, argsJ, eventJ) { sLogger->log(Level::ERROR, TAG, "subscribe: No events array found in subscribe json"); return -1; } - list events(subscribeJson[EVENTS_JSON_ATTR_EVENTS].get>()); + std::list events(subscribeJson[EVENTS_JSON_ATTR_EVENTS].get>()); // Subscribe this client for the listed events. auto request = vshlcore::afb::AFBRequestImpl::create(source->request); @@ -483,3 +536,43 @@ CTLP_CAPI(setDefaultVoiceAgent, source, argsJ, eventJ) { afb_req_success(source->request, NULL, NULL); return 0; } + +CTLP_CAPI(subscribeToLoginEvents, source, argsJ, eventJ) { + if (sVoiceAgentsDataManager == nullptr) { + return -1; + } + + if (eventJ == nullptr) { + sLogger->log(Level::WARNING, TAG, "subscribeToLoginEvents: No arguments supplied."); + return -1; + } + + if (sVRRequestProcessor == nullptr) { + return -1; + } + + json subscribeJson = json::parse(json_object_to_json_string(eventJ)); + if (subscribeJson.find(EVENTS_JSON_ATTR_VA_ID) == subscribeJson.end()) { + sLogger->log(Level::ERROR, TAG, "subscribeToLoginEvents: No voiceagent id found in subscribe json"); + return -1; + } + std::string voiceAgentId(subscribeJson[EVENTS_JSON_ATTR_VA_ID].get()); + if (subscribeJson.find(EVENTS_JSON_ATTR_EVENTS) == subscribeJson.end()) { + sLogger->log(Level::ERROR, TAG, "subscribeToLoginEvents: No events array found in subscribe json"); + return -1; + } + std::list events(subscribeJson[EVENTS_JSON_ATTR_EVENTS].get>()); + + int result = 0; + std::string requestId = sVRRequestProcessor->subscribeToLoginEvents(voiceAgentId, &events); + + if (!requestId.empty()) { + json responseJson; + responseJson[SUBSCRIBE2LOGINEVENTS_JSON_ATTR_REQUEST] = requestId; + afb_req_success(source->request, json_tokener_parse(responseJson.dump().c_str()), NULL); + } else { + afb_req_fail(source->request, NULL, "Failed to subscribeToLoginEvents..."); + } + + return 0; +} -- cgit 1.2.3-korg