Add sound manager initial source code
[staging/soundmanager.git] / libsoundmanager / test.cpp
1 /*
2  * Copyright (c) 2017 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 <libsoundmanager/libsoundmanager.hpp>
18 #include <iostream>
19 #include <glib-2.0/glib.h>
20 #include <fcntl.h>
21 #include <string>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <thread>
25 #include <exception>
26 #include <vector>
27 #include <sstream>
28
29 using namespace std;
30
31 static vector<string> split(const string& str, char sep);
32 LibSoundmanager* sm;
33
34 static void usage()
35 {
36     cout << "verb "<< "key:arg" << endl;
37         cout << "example:" << endl; 
38         cout << "connect sourceID 100 sinkID 100" << endl;
39         cout << "------- -------- --- " << endl;
40         cout << "  verb    key    value" << endl;
41         cout << "verb list:" << endl;
42         for(auto itr = api_list.begin(); itr != api_list.end(); ++itr)
43         {
44                 cout << "  " << *itr << endl;
45         }
46         // Todo output api list
47         exit(0);
48 }
49
50 static void call_test()
51 {
52         string command;
53
54         cout << "input verb and argments" << endl;
55
56         /* read the buffer */
57         for(;;){
58                 char line[1023];
59                 cin.getline(line, sizeof(line));
60                 command = line;
61                 if(command.empty()){
62                         continue;
63                 }
64                 
65                 vector<string> v_command = split(command, ' ');
66                 /*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr)
67                 {
68                         cout << *itr <<endl;
69                 }*/
70                 size_t num = v_command.size();
71                 if(num % 2 == 0){
72                         cout << "If command contains args, please input <key,value> in argument part" << endl;
73                         continue;
74                 }
75                 /* create json object */
76                 struct json_object* j_obj = json_object_new_object();
77                 for(int i = 1;i < (v_command.size()) ;++i){
78                         struct json_object* val         = json_object_new_string(v_command[i+1].c_str());
79                         json_object_object_add(j_obj, v_command[i].c_str(), val);
80                         ++i;
81                 }
82                 /* call verb via libsoundmanager */
83                 sm->call(v_command[0], j_obj);
84                 /* free vector */
85                 vector<string>().swap(v_command);
86                 string().swap(command);
87         }
88 }
89
90 static void onRep(struct json_object* reply_contents)
91 {
92     const char* str = json_object_to_json_string(reply_contents);
93     cout << "[CB onRep]: " << str << endl;
94     json_object_put(reply_contents);
95 }
96
97 static void onEv(const string& event, struct json_object* event_contents)
98 {
99     const char* str = json_object_to_json_string(event_contents);
100     cout << "[CB onEvent]: event" << event.c_str() << "  contents:" << str << endl;
101     json_object_put(event_contents);
102 }
103
104 static vector<string> split(const string& str, char sep)
105 {
106     vector<string> v;
107     stringstream ss(str);
108     string buffer;
109     while( getline(ss, buffer, sep) ) {
110                 if(!buffer.empty())
111                 v.push_back(buffer);
112     }
113     return v;
114 }
115
116 int main(int argc, char **argv)
117 {
118         int ret;
119         if(argc == 1)
120         {
121                 printf("Please input port num in first argument, and token in second argument");
122                 usage();
123                 return 0;
124         }
125         if(argc == 2)
126         {
127                 string av(argv[1]);
128                 if( (av == "-h") || (av == "--help"))
129                 {
130                         usage();
131                         return 0;
132                 }       
133         }
134
135         string port_string(argv[1]);
136         string token(argv[2]);
137         char* endptr;
138         long port = strtol(port_string.c_str(),&endptr,10);
139
140     /* error check of range */
141     if( (port > 20000) || (port < 0) )
142     {
143                 printf("input under 20000(temporary number)");
144         return 0;
145     }
146     if(*endptr != '\0')
147     {
148                 printf("not number");
149         return 0;
150     }
151         
152     cout << "Call test for libsoundmanager" << endl;
153         cout << "Call example: registerSource appname radio" << endl;
154         sm = new LibSoundmanager(port, token);  
155         sm->register_callback(&onEv, &onRep);
156
157         if (ret < 0) {
158                 printf("failed to create event loop");
159                 return -1;
160         }
161         sm->run_eventloop();
162         call_test();
163
164         return 0;
165 }