Sound Manager
test.cpp
Go to the documentation of this file.
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 
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 int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
32 static vector<string> split(const string& str, char sep);
34 static sd_event_source *evsrc;
35 static GMainLoop *loop = NULL;
36 static void call_test();
37 
38 static void usage()
39 {
40  cout << "verb "<< "key:arg" << endl;
41  cout << "example: connect sourceID 100 sinkID 100" << endl;
42  cout << "verb list:" << endl;
43  // Todo output api list
44  exit(0);
45 }
46 
47 static void *event_loop_run(void *args)
48 {
49  call_test();
50 }
51 
52 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
53 {
54  /* This is for Receiving Thread */
55 }
56 
57 static void call_test()
58 {
59  string command;
60 
61  cout << "input verb and argments" << endl;
62 
63  /* read the buffer */
64  for(;;){
65  char line[1023];
66  cin.getline(line, sizeof(line));
67  command = line;
68  if(command.empty()){
69  continue;
70  }
71 
72  vector<string> v_command = split(command, ' ');
73  /*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr)
74  {
75  cout << *itr <<endl;
76  }*/
77  size_t num = v_command.size();
78  if(num % 2 == 0){
79  cout << "If command contains args, please input <key,value> in argument part" << endl;
80  continue;
81  }
82  /* create json object */
83  struct json_object* j_obj = json_object_new_object();
84  for(int i = 1;i < (v_command.size()) ;++i){
85  struct json_object* val = json_object_new_string(v_command[i+1].c_str());
86  json_object_object_add(j_obj, v_command[i].c_str(), val);
87  ++i;
88  }
89  /* call verb via libsoundmanager */
90  sm->call(v_command[0], j_obj);
91  /* free vector */
92  vector<string>().swap(v_command);
93  string().swap(command);
94  }
95 }
96 
97 static void onRep(struct json_object* reply_contents)
98 {
99  const char* str = json_object_to_json_string(reply_contents);
100  cout << "[CB onRep]: " << str << endl;
101  json_object_put(reply_contents);
102 }
103 
104 static void onEv(const string& event, struct json_object* event_contents)
105 {
106  const char* str = json_object_to_json_string(event_contents);
107  cout << "[CB onEvent]: event" << event.c_str() << " contents:" << str << endl;
108  json_object_put(event_contents);
109 }
110 
111 static vector<string> split(const string& str, char sep)
112 {
113  vector<string> v;
114  stringstream ss(str);
115  string buffer;
116  while( getline(ss, buffer, sep) ) {
117  if(!buffer.empty())
118  v.push_back(buffer);
119  }
120  return v;
121 }
122 
123 int main(int argc, char **argv)
124 {
125  int ret;
126  if(argc == 1)
127  {
128  printf("Please input port num in first argument, and token in second argument");
129  usage();
130  return 0;
131  }
132  if(argc == 2)
133  {
134  string av(argv[1]);
135  if( (av == "-h") || (av == "--help"))
136  {
137  usage();
138  return 0;
139  }
140  }
141 
142  string port_string(argv[1]);
143  string token(argv[2]);
144  char* endptr;
145  long port = strtol(port_string.c_str(),&endptr,10);
146 
147  /* error check of range */
148  if( (port > 20000) || (port < 0) )
149  {
150  printf("input under 20000(temporary number)");
151  return 0;
152  }
153  if(*endptr != '\0')
154  {
155  printf("not number");
156  return 0;
157  }
158 
159  cout << "Call test for libsoundmanager" << endl;
160  sm = new LibSoundmanager(port, token);
161  sm->register_callback(&onEv, &onRep);
162  pthread_t thread_id;
163  ret = pthread_create(&thread_id, NULL, event_loop_run, NULL);
164 
165  sd_event *sloop;
166  ret = sd_event_default(&sloop);
167 
168  if (ret < 0) {
169  printf("failed to create event loop");
170  return -1;
171  }
172 
173  sd_event_add_io(sloop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
174  for(;;)
175  sd_event_run(sloop, 30000000);
176  return 0;
177 }
int call(const std::string &verb, struct json_object *arg)
void register_callback(void(*event_cb)(const std::string &event, struct json_object *event_contents), void(*reply_cb)(struct json_object *reply_contents))
LibSoundmanager * sm
Definition: test.cpp:33
int main(int argc, char **argv)
Definition: test.cpp:123