2 * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "homescreenvoice.h"
24 #include "hmi-debug.h"
28 static const char API[] = "vshl-core";
29 const string vshl_core_event = "{\"va_id\": \"VA-001\", \"events\": [\"voice_dialogstate_event\"]}";
31 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
33 static_cast<HomescreenVoice*>(closure)->on_hangup(NULL,wsj);
36 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
38 /* HomescreenVoice is not called from other process */
41 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
43 static_cast<HomescreenVoice*>(closure)->on_event(NULL,event,msg);
46 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
48 static_cast<HomescreenVoice*>(closure)->on_reply(NULL,msg);
51 const std::vector<std::string> HomescreenVoice::state_lists {
53 std::string("LISTENING"),
54 std::string("THINKING"),
55 std::string("UNKNOWN"),
56 std::string("SPEAKING")
60 static void event_loop_run(struct sd_event* loop){
65 HomescreenVoice::HomescreenVoice(QObject *parent) :
71 HomescreenVoice::~HomescreenVoice()
73 if(sp_websock != NULL)
75 afb_wsj1_unref(sp_websock);
79 sd_event_exit(mploop, 0);
83 int HomescreenVoice::init(int port, const string& token)
86 if(port > 0 && token.size() > 0)
93 HMI_ERROR("HomescreenVoice","port and token should be > 0, Initial port and token uses.");
96 ret = initialize_websocket();
99 HMI_ERROR("HomescreenVoice","Failed to initialize websocket");
102 HMI_DEBUG("HomescreenVoice","Initialized");
103 subscribe(vshl_core_event);
109 int HomescreenVoice::initialize_websocket(void)
111 HMI_DEBUG("HomescreenVoice"," initialize_websocket called");
113 int ret = sd_event_new(&mploop);
116 HMI_ERROR("HomescreenVoice","Failed to create event loop");
121 // enforce context to avoid initialization/goto error
122 std::thread th(event_loop_run, mploop);
126 /* Initialize interface from websocket */
127 minterface.on_hangup = _on_hangup_static;
128 minterface.on_call = _on_call_static;
129 minterface.on_event = _on_event_static;
130 muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
131 sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);
133 if(sp_websock == NULL)
135 HMI_ERROR("HomescreenVoice","Failed to create websocket connection");
142 int HomescreenVoice::subscribe(const string event_name)
144 HMI_DEBUG("HomescreenVoice"," subscribe called");
150 json_object* j_obj = json_tokener_parse(event_name.c_str());
152 int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
154 HMI_ERROR("HomescreenVoice","Failed to call verb");
159 int HomescreenVoice::startListening(void)
161 HMI_DEBUG("HomescreenVoice"," startListening called");
162 struct json_object* j_obj = json_object_new_object();
163 int ret = afb_wsj1_call_j(sp_websock, API, "startListening", j_obj, _on_reply_static, this);
165 HMI_ERROR("HomescreenVoice"," startListening Failed to call verb");
170 /************* Callback Function *************/
172 void HomescreenVoice::on_hangup(void *closure, struct afb_wsj1 *wsj)
174 HMI_DEBUG("HomescreenVoice"," on_hangup called");
177 void HomescreenVoice::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
179 HMI_DEBUG("HomescreenVoice"," on_call called");
183 * event is like "homescreen/hvac"
184 * msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
186 event name : struct json_object obj = json_object_object_get(msg,"event")
188 void HomescreenVoice::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
190 HMI_DEBUG("HomescreenVoice","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
192 if (strstr(event, API) == NULL) {
195 struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
196 struct json_object *json_data_str;
197 if(!json_object_object_get_ex(ev_contents, "data", &json_data_str)) {
198 HMI_ERROR("HomescreenVoice", "got ev_contents error.");
202 struct json_object *json_state;
203 struct json_object *json_data = json_tokener_parse(json_object_get_string(json_data_str));
204 if(!json_object_object_get_ex(json_data, "state", &json_state)) {
205 HMI_ERROR("HomescreenVoice", "got json_data1 error.");
209 const char* corestatus = json_object_get_string(json_state);
211 if (strcasecmp(corestatus, HomescreenVoice::state_lists[0].c_str()) == 0) {
212 emit statusChanged(true);
214 else if ((strcasecmp(corestatus, HomescreenVoice::state_lists[1].c_str()) == 0)||
215 (strcasecmp(corestatus, HomescreenVoice::state_lists[2].c_str()) == 0)||
216 (strcasecmp(corestatus, HomescreenVoice::state_lists[3].c_str()) == 0)||
217 (strcasecmp(corestatus, HomescreenVoice::state_lists[4].c_str()) == 0)){
218 emit statusChanged(false);
223 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
224 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
226 void HomescreenVoice::on_reply(void *closure, struct afb_wsj1_msg *msg)
228 HMI_DEBUG("HomescreenVoice"," on_reply called");