<filename>Jetson/hbs2/src/nodes/service/tts_srv.cpp
/*
Cite as:

IEEE
<NAME>, <NAME>, <NAME>, <NAME>, and <NAME>, " A wearable sensor vest for social 
humanoid robots with GPGPU, IoT, and modular software architecture," Robotics and Autonomous Systems.


Bibtex
@article{jafarzadeh2020wearable, 
title={A wearable sensor vest for social humanoid robots with GPGPU, IoT, and modular software architecture}, 
author={<NAME> and <NAME> and <NAME> and <NAME> and <NAME>}, 
journal={Robotics and Autonomous Systems}, 
publisher={Elsevier} }


Copyright (c) 2020 <NAME> and <NAME>. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions 
and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software must display the following 
acknowledgement: This product includes software developed by <NAME>, <NAME>, 
<NAME>, <NAME>, <NAME>, <NAME>, and <NAME>.

4. Neither the name of the Mohsen Jafarzadeh nor the names of its contributors may be used to endorse or 
promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY MOHSEN JAFARZADEH "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL MOHSEN JAFARZADEH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/



// System
#include <stdlib.h>
#include <stdio.h>
#include <string>

// ROS
#include "ros/ros.h"
#include "hbs2/iot.h"
#include "hbs2/tts.h"
#include "hbs2/sonar.h"

// Declare a global NodeHandle
ros::NodeHandlePtr n = NULL;

/*  Function: handle_req (Handle request)
    desc: Callback function for the TTS service. Sends system call to TTS application, festival.
    inputs:
        &req: req.text: text to be sent to TTS application
    outputs:
        &res: response.success: boolean for successful system call
*/
bool handle_req(hbs2::tts::Request &req, hbs2::tts::Response &res)
{
    char buf[150];
    int str_len = sprintf(buf, "/bin/bash -c 'echo \"%s\" \u007C festival --tts'", req.text.c_str());
    if (system(buf) != 0) {
        ROS_ERROR("System call to festival failed.");
        return false;
    }
    else {
        ROS_INFO("System call to festival succeeded.");
        return true;
    }  
}

/*  Function: main
    desc: Entry point for the Node
    inputs:
        argc: count of command line arguments
        argv: array of command line arguments
    outputs:
        int: always 0 if exits gracefully
*/
int main(int argc, char **argv)
{  
    ros::init(argc, argv, "tts_srv");
    n = ros::NodeHandlePtr(new ros::NodeHandle);
    
    ros::ServiceServer srv = n->advertiseService("tts_srv", handle_req);
    ROS_INFO("ROS TTS service has started.");
    ros::spin();

    return 0;
}
