add vui button
[apps/homescreen.git] / homescreen / src / homescreenvoice.cpp
1 /*
2  * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <QFileInfo>
18 #include "homescreenvoice.h"
19 #include <functional>
20 #include <QProcess>
21 #include <dirent.h>
22 #include <stdio.h>
23 #include <thread>
24 #include "hmi-debug.h"
25
26
27
28 static const char API[] = "vshl-core";
29 const string vshl_core_event = "{\"va_id\": \"VA-001\", \"events\": [\"voice_dialogstate_event\"]}";
30
31 static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
32 {
33     static_cast<HomescreenVoice*>(closure)->on_hangup(NULL,wsj);
34 }
35
36 static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
37 {
38     /* HomescreenVoice is not called from other process */
39 }
40
41 static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
42 {
43     static_cast<HomescreenVoice*>(closure)->on_event(NULL,event,msg);
44 }
45
46 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
47 {
48     static_cast<HomescreenVoice*>(closure)->on_reply(NULL,msg);
49 }
50
51 const std::vector<std::string> HomescreenVoice::state_lists {
52     std::string("IDLE"),
53     std::string("LISTENING"),
54     std::string("THINKING"),
55     std::string("UNKNOWN"),
56     std::string("SPEAKING")
57 };
58
59
60 static void event_loop_run(struct sd_event* loop){
61     sd_event_loop(loop);
62     sd_event_unref(loop);
63 }
64
65 HomescreenVoice::HomescreenVoice(QObject *parent) :
66     QObject(parent)
67 {
68
69 }
70
71 HomescreenVoice::~HomescreenVoice()
72 {
73     if(sp_websock != NULL)
74     {
75         afb_wsj1_unref(sp_websock);
76     }
77     if(mploop)
78     {
79         sd_event_exit(mploop, 0);
80     }
81 }
82
83 int HomescreenVoice::init(int port, const string& token)
84 {
85     int ret = 0;
86     if(port > 0 && token.size() > 0)
87     {
88         mport = port;
89         mtoken = token;
90     }
91     else
92     {
93         HMI_ERROR("HomescreenVoice","port and token should be > 0, Initial port and token uses.");
94     }
95
96     ret = initialize_websocket();
97     if(ret != 0 )
98     {
99         HMI_ERROR("HomescreenVoice","Failed to initialize websocket");
100     }
101     else{
102         HMI_DEBUG("HomescreenVoice","Initialized");
103         subscribe(vshl_core_event);
104     }
105
106     return ret;
107 }
108
109 int HomescreenVoice::initialize_websocket(void)
110 {
111     HMI_DEBUG("HomescreenVoice"," initialize_websocket called");
112     mploop = NULL;
113     int ret = sd_event_new(&mploop);
114     if(ret < 0)
115     {
116         HMI_ERROR("HomescreenVoice","Failed to create event loop");
117         return -1;
118     }
119
120     {
121         // enforce context to avoid initialization/goto error
122         std::thread th(event_loop_run, mploop);
123         th.detach();
124     }
125
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);
132
133     if(sp_websock == NULL)
134     {
135         HMI_ERROR("HomescreenVoice","Failed to create websocket connection");
136        return -1;
137     }
138
139     return 0;
140 }
141
142 int HomescreenVoice::subscribe(const string event_name)
143 {
144     HMI_DEBUG("HomescreenVoice"," subscribe called");
145     if(!sp_websock)
146     {
147         return -1;
148     }
149
150     json_object* j_obj = json_tokener_parse(event_name.c_str());
151
152     int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
153     if (ret < 0) {
154         HMI_ERROR("HomescreenVoice","Failed to call verb");
155     }
156     return ret;
157 }
158
159 int HomescreenVoice::startListening(void)
160 {
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);
164     if (ret < 0) {
165         HMI_ERROR("HomescreenVoice"," startListening Failed to call verb");
166     }
167     return ret;
168 }
169
170 /************* Callback Function *************/
171
172 void HomescreenVoice::on_hangup(void *closure, struct afb_wsj1 *wsj)
173 {
174     HMI_DEBUG("HomescreenVoice"," on_hangup called");
175 }
176
177 void HomescreenVoice::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
178 {
179     HMI_DEBUG("HomescreenVoice"," on_call called");
180 }
181
182 /*
183 * event is like "homescreen/hvac"
184 * msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
185 * so you can get
186     event name : struct json_object obj = json_object_object_get(msg,"event")
187 */
188 void HomescreenVoice::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
189 {
190     HMI_DEBUG("HomescreenVoice","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));
191
192     if (strstr(event, API) == NULL) {
193         return;
194     }
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.");
199         return;
200     }
201
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.");
206         return;
207     }
208
209     const char* corestatus = json_object_get_string(json_state);
210
211     if (strcasecmp(corestatus, HomescreenVoice::state_lists[0].c_str()) == 0) {
212         emit statusChanged(true);
213     }
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);
219     }
220 }
221
222 /**
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]"}})
225  */
226 void HomescreenVoice::on_reply(void *closure, struct afb_wsj1_msg *msg)
227 {
228     HMI_DEBUG("HomescreenVoice"," on_reply called");
229 }