summaryrefslogtreecommitdiffstats
path: root/meta-speech-framework/meta-aac/recipes-apis/alexa-voiceagent-service/alexa-voiceagent-service/0006-fix-event-argument-json.patch
blob: 8c6f1e4b19cc7d9fc20525243d96efdd9a63168c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Fix event argument JSON

It was discovered while trying to use some of the capabilities events
that the argument JSON was incorrectly formatted, with instances of
all or part of it being double-quoted as strings with escaping.  A
couple instances of this had previously been worked around by hacks
involving reparsing all or some parts of the arguments a second time
with a JSON parser, but it seems better to fix it at the source so
that the events match documentation and are usable as is.

Note that it is ATM not clear if all affected event argument payloads
are correct, e.g. LocalMediaSource may need some more work.

Upstream-Status: Pending

Signed-off-by: Scott Murray <scott.murray@konsulko.com>

diff --git a/src/plugins/aasb-client/AlexaCapabilityDirectiveRouterImpl.cpp b/src/plugins/aasb-client/AlexaCapabilityDirectiveRouterImpl.cpp
index 6aea920..23ed90c 100644
--- a/src/plugins/aasb-client/AlexaCapabilityDirectiveRouterImpl.cpp
+++ b/src/plugins/aasb-client/AlexaCapabilityDirectiveRouterImpl.cpp
@@ -17,6 +17,8 @@
 
 #include <sstream>
 
+#include <json-c/json.h>
+
 #include <aasb/Consts.h>
 
 #include "AlexaConsts.h"
@@ -322,7 +324,16 @@ void AlexaCapabilityDirectiveRouterImpl::processTemplateRuntimeAction(
 
     json_object* argsJ = json_object_new_object();
     json_object* actionJ = json_object_new_string(vshlCapabilityAction.c_str());
-    json_object* payloadJ = json_object_new_string(payload.c_str());
+    json_object* payloadJ = NULL;
+    if(payload.length()) {
+        payloadJ = json_tokener_parse(payload.c_str());
+    } else {
+        payloadJ = json_object_new_string("");
+    }
+    if(!payloadJ) {
+        m_logger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
+        return;
+    }
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_ACTION.c_str(), actionJ);
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_PAYLOAD.c_str(), payloadJ);
 
@@ -343,24 +354,40 @@ void AlexaCapabilityDirectiveRouterImpl::processCBLAction(
     const std::string& payload) {
     m_logger->log(Level::DEBUG, TAG, "Processing CBL action: " + action);
 
-    json_object* eventDataJ = json_object_new_object();
+    json_object* payloadJ = NULL;
+    if(payload.length()) {
+        payloadJ = json_tokener_parse(payload.c_str());
+    } else {
+        payloadJ = json_object_new_string("");
+    }
+    if(!payloadJ) {
+        m_logger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
+        return;
+    }
+    // The payload string may already be of the form of a document like
+    // "{ "payload" : { ... } }", the simplest way to handle that is to use
+    // it as the event json_object if that's the case, that way we avoid
+    // having to worry about copying.
+    json_object* eventDataJ = NULL;
+    if(json_object_object_get_ex(payloadJ, "payload", NULL)) {
+        eventDataJ = payloadJ;
+    } else {
+        eventDataJ = json_object_new_object();
+        json_object_object_add(eventDataJ, JSON_ATTR_PAYLOAD.c_str(), payloadJ);
+    }
     json_object* vaIdJ = json_object_new_string(m_alexaVoiceAgentId.c_str());
-
     json_object_object_add(eventDataJ, JSON_ATTR_VOICEAGENT_ID.c_str(), vaIdJ);
 
     int observers = 0;
     if (action == aasb::bridge::ACTION_CBL_CODEPAIR_RECEIVED) {
         m_logger->log(Level::INFO, TAG, "CBL codepair received: " + payload);
-        json_object* payloadJ = json_object_new_string(payload.c_str());
-        json_object_object_add(eventDataJ, JSON_ATTR_PAYLOAD.c_str(), payloadJ);
         observers = m_cblCodePairReceivedEvent->publishEvent(eventDataJ);
     } else if (action == aasb::bridge::ACTION_CBL_CODEPAIR_EXPIRED) {
         m_logger->log(Level::INFO, TAG, "CBL codepair expired: " + payload);
-        json_object* payloadJ = json_object_new_string(payload.c_str());
-        json_object_object_add(eventDataJ, JSON_ATTR_PAYLOAD.c_str(), payloadJ);
         observers = m_cblCodePairExpiredEvent->publishEvent(eventDataJ);
     } else {
         m_logger->log(Level::INFO, TAG, "Unhandled action: " + action);
+        json_object_put(eventDataJ);
     }
 
     std::stringstream logMsg;
diff --git a/src/plugins/dispatchers/local-voice-control/car-control/CarControlDispatcher.cpp b/src/plugins/dispatchers/local-voice-control/car-control/CarControlDispatcher.cpp
index 096f72f..75108d4 100644
--- a/src/plugins/dispatchers/local-voice-control/car-control/CarControlDispatcher.cpp
+++ b/src/plugins/dispatchers/local-voice-control/car-control/CarControlDispatcher.cpp
@@ -72,7 +72,16 @@ void CarControlDispatcher::onReceivedDirective(
 
     json_object* argsJ = json_object_new_object();
     json_object* actionJ = json_object_new_string(vshlCapabilityAction.c_str());
-    json_object* payloadJ = json_object_new_string(payload.c_str());
+    json_object* payloadJ = NULL;
+    if(payload.length()) {
+        payloadJ = json_tokener_parse(payload.c_str());
+    } else {
+        payloadJ = json_object_new_string("");
+    }
+    if(!payloadJ) {
+        m_logger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
+        return;
+    }
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_ACTION.c_str(), actionJ);
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_PAYLOAD.c_str(), payloadJ);
 
diff --git a/src/plugins/dispatchers/localmediasource/LocalMediaSourceDispatcher.cpp b/src/plugins/dispatchers/localmediasource/LocalMediaSourceDispatcher.cpp
index c261a56..04ac10c 100644
--- a/src/plugins/dispatchers/localmediasource/LocalMediaSourceDispatcher.cpp
+++ b/src/plugins/dispatchers/localmediasource/LocalMediaSourceDispatcher.cpp
@@ -71,7 +71,16 @@ void LocalMediaSourceDispatcher::onReceivedDirective(
 
     json_object* argsJ = json_object_new_object();
     json_object* actionJ = json_object_new_string(vshlCapabilityAction.c_str());
-    json_object* payloadJ = json_object_new_string(payload.c_str());
+    json_object* payloadJ = NULL;
+    if(payload.length()) {
+        payloadJ = json_tokener_parse(payload.c_str());
+    } else {
+        payloadJ = json_object_new_string("");
+    }
+    if(!payloadJ) {
+        m_logger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
+        return;
+    }
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_ACTION.c_str(), actionJ);
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_PAYLOAD.c_str(), payloadJ);
 
diff --git a/src/plugins/dispatchers/navigation/NavigationDispatcher.cpp b/src/plugins/dispatchers/navigation/NavigationDispatcher.cpp
index 55a6017..283b42b 100644
--- a/src/plugins/dispatchers/navigation/NavigationDispatcher.cpp
+++ b/src/plugins/dispatchers/navigation/NavigationDispatcher.cpp
@@ -68,7 +68,16 @@ void NavigationDispatcher::onReceivedDirective(
 
     json_object* argsJ = json_object_new_object();
     json_object* actionJ = json_object_new_string(vshlCapabilityAction.c_str());
-    json_object* payloadJ = json_object_new_string(payload.c_str());
+    json_object* payloadJ = NULL;
+    if(payload.length()) {
+        payloadJ = json_tokener_parse(payload.c_str());
+    } else {
+        payloadJ = json_object_new_string("");
+    }
+    if(!payloadJ) {
+        m_logger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
+        return;
+    }
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_ACTION.c_str(), actionJ);
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_PAYLOAD.c_str(), payloadJ);
 
diff --git a/src/plugins/dispatchers/phonecall/PhoneCallDispatcher.cpp b/src/plugins/dispatchers/phonecall/PhoneCallDispatcher.cpp
index 29ad96a..3432892 100644
--- a/src/plugins/dispatchers/phonecall/PhoneCallDispatcher.cpp
+++ b/src/plugins/dispatchers/phonecall/PhoneCallDispatcher.cpp
@@ -86,7 +86,16 @@ void PhoneCallDispatcher::onReceivedDirective(
 
     json_object* argsJ = json_object_new_object();
     json_object* actionJ = json_object_new_string(vshlCapabilityAction.c_str());
-    json_object* payloadJ = json_object_new_string(payload.c_str());
+    json_object* payloadJ = NULL;
+    if(payload.length()) {
+        payloadJ = json_tokener_parse(payload.c_str());
+    } else {
+        payloadJ = json_object_new_string("");
+    }
+    if(!payloadJ) {
+        m_logger->log(Level::ERROR, TAG, "Unable to parse payload JSON: " + payload);
+        return;
+    }
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_ACTION.c_str(), actionJ);
     json_object_object_add(argsJ, agl::alexa::JSON_ATTR_PAYLOAD.c_str(), payloadJ);