summaryrefslogtreecommitdiffstats
path: root/homescreen.pro
blob: 7c4938322ef54405ef4de46ca36ba173ed7accdc (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
# Copyright (c) 2017 TOYOTA MOTOR CORPORATION
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

TEMPLATE = subdirs

load(configure)

SUBDIRS = interfaces \
    homescreen \
    package

homescreen.depends = interfaces
package.depends += homescreen
color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <QFileInfo>
#include "homescreenvoice.h"
#include <functional>
#include <QProcess>
#include <dirent.h>
#include <stdio.h>
#include <thread>
#include "hmi-debug.h"



static const char API[] = "vshl-core";
const string vshl_core_event = "{\"va_id\": \"VA-001\", \"events\": [\"voice_dialogstate_event\",\"voice_connectionstate_event\"]}";

static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj)
{
    static_cast<HomescreenVoice*>(closure)->on_hangup(NULL,wsj);
}

static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
    /* HomescreenVoice is not called from other process */
}

static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg)
{
    static_cast<HomescreenVoice*>(closure)->on_event(NULL,event,msg);
}

static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
{
    static_cast<HomescreenVoice*>(closure)->on_reply(NULL,msg);
}

const std::vector<std::string> HomescreenVoice::state_lists {
    std::string("IDLE"),
    std::string("LISTENING"),
    std::string("THINKING"),
    std::string("UNKNOWN"),
    std::string("SPEAKING")
};

const std::vector<std::string> HomescreenVoice::connect_lists {
    std::string("DISCONNECTED"),
    std::string("CONNECTED")
};

const std::vector<std::string> HomescreenVoice::event_lists {
    std::string("vshl-core/voice_dialogstate_event#VA-001"),
    std::string("vshl-core/voice_connectionstate_event#VA-001")
};

static void event_loop_run(struct sd_event* loop){
    sd_event_loop(loop);
    sd_event_unref(loop);
}

HomescreenVoice::HomescreenVoice(QObject *parent) :
    QObject(parent)
{

}

HomescreenVoice::~HomescreenVoice()
{
    if(sp_websock != NULL)
    {
        afb_wsj1_unref(sp_websock);
    }
    if(mploop)
    {
        sd_event_exit(mploop, 0);
    }
}

int HomescreenVoice::init(int port, const string& token)
{
    int ret = 0;
    if(port > 0 && token.size() > 0)
    {
        mport = port;
        mtoken = token;
    }
    else
    {
        HMI_ERROR("HomescreenVoice","port and token should be > 0, Initial port and token uses.");
    }

    ret = initialize_websocket();
    if(ret != 0 )
    {
        HMI_ERROR("HomescreenVoice","Failed to initialize websocket");
    }
    else{
        HMI_DEBUG("HomescreenVoice","Initialized");
        subscribe(vshl_core_event);
    }

    return ret;
}

int HomescreenVoice::initialize_websocket(void)
{
    HMI_DEBUG("HomescreenVoice"," initialize_websocket called");
    mploop = NULL;
    int ret = sd_event_new(&mploop);
    if(ret < 0)
    {
        HMI_ERROR("HomescreenVoice","Failed to create event loop");
        return -1;
    }

    {
        // enforce context to avoid initialization/goto error
        std::thread th(event_loop_run, mploop);
        th.detach();
    }

    /* Initialize interface from websocket */
    minterface.on_hangup = _on_hangup_static;
    minterface.on_call = _on_call_static;
    minterface.on_event = _on_event_static;
    muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/
    sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this);

    if(sp_websock == NULL)
    {
        HMI_ERROR("HomescreenVoice","Failed to create websocket connection");
       return -1;
    }

    return 0;
}

int HomescreenVoice::subscribe(const string event_name)
{
    HMI_DEBUG("HomescreenVoice"," subscribe called");
    if(!sp_websock)
    {
        return -1;
    }

    json_object* j_obj = json_tokener_parse(event_name.c_str());

    int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this);
    if (ret < 0) {
        HMI_ERROR("HomescreenVoice","Failed to call verb");
    }
    return ret;
}

int HomescreenVoice::startListening(void)
{
    HMI_DEBUG("HomescreenVoice"," startListening called");
    struct json_object* j_obj = json_object_new_object();
    int ret = afb_wsj1_call_j(sp_websock, API, "startListening", j_obj, _on_reply_static, this);
    if (ret < 0) {
        HMI_ERROR("HomescreenVoice"," startListening Failed to call verb");
    }
    return ret;
}

/************* Callback Function *************/

void HomescreenVoice::on_hangup(void *closure, struct afb_wsj1 *wsj)
{
    HMI_DEBUG("HomescreenVoice"," on_hangup called");
}

void HomescreenVoice::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
    HMI_DEBUG("HomescreenVoice"," on_call called");
}

/*
* event is like "homescreen/hvac"
* msg is like {"event":"homescreen\/hvac","data":{"type":"tap_shortcut"},"jtype":"afb-event"}
* so you can get
    event name : struct json_object obj = json_object_object_get(msg,"event")
*/
void HomescreenVoice::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
    HMI_DEBUG("HomescreenVoice","event: (%s) msg: (%s).", event, afb_wsj1_msg_object_s(msg));

    if (strstr(event, API) == NULL) {
        return;
    }
    struct json_object* ev_contents = afb_wsj1_msg_object_j(msg);
    struct json_object *json_event_str;

    if(!json_object_object_get_ex(ev_contents, "event", &json_event_str)) {
        HMI_ERROR("HomescreenVoice", "got json_event_str error.");
        return;
    }
    struct json_object *json_data_str;
    if(!json_object_object_get_ex(ev_contents, "data", &json_data_str)) {
        HMI_ERROR("HomescreenVoice", "got data error.");
        return;
    }

    struct json_object *json_state;
    struct json_object *json_data = json_tokener_parse(json_object_get_string(json_data_str));
    if(!json_object_object_get_ex(json_data, "state", &json_state)) {
        HMI_ERROR("HomescreenVoice", "got state error.");
        return;
    }

    const char* info = json_object_get_string(json_state);
    const char* eventinfo = json_object_get_string(json_event_str);
    if(strcasecmp(eventinfo, HomescreenVoice::event_lists[0].c_str()) == 0){
        if (strcasecmp(info, HomescreenVoice::state_lists[0].c_str()) == 0) {
            if(connect){
                emit statusChanged(true);
                emit showInformation(QString(QLatin1String(info)));
            }
        }
        else if ((strcasecmp(info, HomescreenVoice::state_lists[1].c_str()) == 0)||
                 (strcasecmp(info, HomescreenVoice::state_lists[2].c_str()) == 0)||
                 (strcasecmp(info, HomescreenVoice::state_lists[3].c_str()) == 0)||
                 (strcasecmp(info, HomescreenVoice::state_lists[4].c_str()) == 0)){
            emit statusChanged(false);
            emit showInformation(QString(QLatin1String(info)));
        }
    }else if(strcasecmp(eventinfo, HomescreenVoice::event_lists[1].c_str()) == 0){
        if (strcasecmp(info, HomescreenVoice::connect_lists[0].c_str()) == 0) {
            HMI_DEBUG("HomescreenVoice", "connect false!");
            connect = false;
        }
        else if (strcasecmp(info, HomescreenVoice::connect_lists[1].c_str()) == 0){
            connect = true;
            emit statusChanged(true);
        }
    }
}

/**
 * msg is like ({"response":{"verb":"subscribe","error":0},"jtype":"afb-reply","request":{"status":"success","info":"homescreen binder subscribe event name [on_screen_message]"}})
 * msg is like ({"response":{"verb":"tap_shortcut","error":0},"jtype":"afb-reply","request":{"status":"success","info":"afb_event_push event [tap_shortcut]"}})
 */
void HomescreenVoice::on_reply(void *closure, struct afb_wsj1_msg *msg)
{
    HMI_DEBUG("HomescreenVoice"," on_reply called");
}