2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <libhomescreen.hpp>
19 #include <glib-2.0/glib.h>
22 #include <sys/types.h>
32 static vector<string> split(const string& str, char sep);
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)
45 cout << " " << *itr << endl;
47 // Todo output api list
51 static void call_test()
55 cout << "input verb and argments" << endl;
60 cin.getline(line, sizeof(line));
66 vector<string> v_command = split(command, ' ');
67 /*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr)
71 size_t num = v_command.size();
73 cout << "If command contains args, please input <key,value> in argument part" << endl;
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);
83 /* call verb via LibHomeScreen */
84 hs->call(v_command[0], j_obj);
86 vector<string>().swap(v_command);
87 string().swap(command);
91 static void onRep(struct json_object* reply_contents)
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!!!
98 static void onEv(const string& event, struct json_object* event_contents)
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!!!
105 static vector<string> split(const string& str, char sep)
108 stringstream ss(str);
110 while( getline(ss, buffer, sep) ) {
117 int main(int argc, char **argv)
122 printf("Please input port num in first argument, and token in second argument");
129 if( (av == "-h") || (av == "--help"))
136 string port_string(argv[1]);
137 string token(argv[2]);
139 long port = strtol(port_string.c_str(),&endptr,10);
141 /* error check of range */
142 if( (port > 20000) || (port < 0) )
144 printf("input under 20000(temporary number)");
149 printf("not number");
153 cout << "Call test for LibHomeScreen" << endl;
154 hs = new LibHomeScreen();
155 hs->init(port, token);
157 // hs->registerCallback(&onEv, &onRep);
159 // hs->subscribe(event_list[0]); // tap_shortcut event subscribe
160 // hs->subscribe(event_list[1]);
162 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](const char* application_name){
163 cout << "set_event_handler Event_TapShortcut application_name = " << application_name << endl;
166 hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](const char* display_message){
167 cout << "set_event_handler Event_OnScreenMessage display_message = " << display_message << endl;
172 printf("failed to create event loop");