HomeScreenBinding
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 
17 #include <libhomescreen.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 #include <functional>
29 
30 using namespace std;
31 
32 static vector<string> split(const string& str, char sep);
34 
35 static void usage()
36 {
37  cout << "verb "<< "key:arg" << endl;
38  cout << "example:" << endl;
39  cout << "ping" << endl;
40  cout << "------- -------- --- " << endl;
41  cout << " verb key value" << endl;
42  cout << "verb list:" << endl;
43  for(auto itr = LibHomeScreen::api_list.begin(); itr != LibHomeScreen::api_list.end(); ++itr)
44  {
45  cout << " " << *itr << endl;
46  }
47  // Todo output api list
48  exit(0);
49 }
50 
51 static void call_test()
52 {
53  string command;
54 
55  cout << "input verb and argments" << endl;
56 
57  /* read the buffer */
58  for(;;){
59  char line[1023];
60  cin.getline(line, sizeof(line));
61  command = line;
62  if(command.empty()){
63  continue;
64  }
65 
66  vector<string> v_command = split(command, ' ');
67  /*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr)
68  {
69  cout << *itr <<endl;
70  }*/
71  size_t num = v_command.size();
72  if(num % 2 == 0){
73  cout << "If command contains args, please input <key,value> in argument part" << endl;
74  continue;
75  }
76  /* create json object */
77  struct json_object* j_obj = json_object_new_object();
78  for(int i = 1;i < (v_command.size()) ;++i){
79  struct json_object* val = json_object_new_string(v_command[i+1].c_str());
80  json_object_object_add(j_obj, v_command[i].c_str(), val);
81  ++i;
82  }
83  /* call verb via LibHomeScreen */
84  hs->call(v_command[0], j_obj);
85  /* free vector */
86  vector<string>().swap(v_command);
87  string().swap(command);
88  }
89 }
90 
91 static void onRep(struct json_object* reply_contents)
92 {
93  const char* str = json_object_to_json_string(reply_contents);
94  cout << "test.cpp [CB onRep]: " << str << endl;
95  //json_object_put(reply_contents); do not release!!!
96 }
97 
98 static void onEv(const string& event, struct json_object* event_contents)
99 {
100  const char* str = json_object_to_json_string(event_contents);
101  cout << "test.cpp [CB onEvent]: event: " << event.c_str() << " contents: " << str << endl;
102  //json_object_put(event_contents); do not release!!!
103 }
104 
105 static vector<string> split(const string& str, char sep)
106 {
107  vector<string> v;
108  stringstream ss(str);
109  string buffer;
110  while( getline(ss, buffer, sep) ) {
111  if(!buffer.empty())
112  v.push_back(buffer);
113  }
114  return v;
115 }
116 
117 int main(int argc, char **argv)
118 {
119  int ret;
120  if(argc == 1)
121  {
122  printf("Please input port num in first argument, and token in second argument");
123  usage();
124  return 0;
125  }
126  if(argc == 2)
127  {
128  string av(argv[1]);
129  if( (av == "-h") || (av == "--help"))
130  {
131  usage();
132  return 0;
133  }
134  }
135 
136  string port_string(argv[1]);
137  string token(argv[2]);
138  char* endptr;
139  long port = strtol(port_string.c_str(),&endptr,10);
140 
141  /* error check of range */
142  if( (port > 20000) || (port < 0) )
143  {
144  printf("input under 20000(temporary number)");
145  return 0;
146  }
147  if(*endptr != '\0')
148  {
149  printf("not number");
150  return 0;
151  }
152 
153  cout << "Call test for LibHomeScreen" << endl;
154  hs = new LibHomeScreen();
155  hs->init(port, token);
156 
157  // hs->registerCallback(&onEv, &onRep);
158  //
159  // hs->subscribe(event_list[0]); // tap_shortcut event subscribe
160  // hs->subscribe(event_list[1]);
161 
162  hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](json_object *object){
163  const char *application_name = json_object_get_string(
164  json_object_object_get(object, "application_name"));
165  cout << "set_event_handler Event_TapShortcut application_name = " << application_name << endl;
166  });
167 
168  hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](json_object *object){
169  const char *display_message = json_object_get_string(
170  json_object_object_get(object, "display_message"));
171  cout << "set_event_handler Event_OnScreenMessage display_message = " << display_message << endl;
172  });
173 
174  hs->set_event_handler(LibHomeScreen::Event_OnScreenReply, [](json_object *object){
175  const char *reply_message = json_object_get_string(
176  json_object_object_get(object, "reply_message"));
177  cout << "set_event_handler Event_OnScreenReply reply_message = " << reply_message << endl;
178  });
179 
180  if (ret < 0) {
181  printf("failed to create event loop");
182  return -1;
183  }
184 
185  call_test();
186 
187  return 0;
188 }
static void usage()
Definition: test.cpp:35
static vector< string > split(const string &str, char sep)
Definition: test.cpp:105
LibHomeScreen * hs
Definition: test.cpp:33
int call(const std::string &verb, struct json_object *arg)
static void onEv(const string &event, struct json_object *event_contents)
Definition: test.cpp:98
int main(int argc, char **argv)
Definition: test.cpp:117
static void call_test()
Definition: test.cpp:51
static void onRep(struct json_object *reply_contents)
Definition: test.cpp:91
int init(const int port, const std::string &token)
static const std::vector< std::string > api_list
void set_event_handler(enum EventType et, handler_func f)