summaryrefslogtreecommitdiffstats
path: root/libhomescreen/test.cpp
blob: 1a61616dc097d30368d75bb6177074425fe773c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <libhomescreen.hpp>
#include <iostream>
#include <glib-2.0/glib.h>
#include <fcntl.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <thread>
#include <exception>
#include <vector>
#include <sstream>
#include <functional>

using namespace std;

static vector<string> split(const string& str, char sep);
LibHomeScreen* hs;

static void usage()
{
    cout << "verb "<< "key:arg" << endl;
	cout << "example:" << endl;
	cout << "ping" << endl;
	cout << "------- -------- --- " << endl;
	cout << "  verb    key    value" << endl;
	cout << "verb list:" << endl;
	for(auto itr = LibHomeScreen::api_list.begin(); itr != LibHomeScreen::api_list.end(); ++itr)
	{
		cout << "  " << *itr << endl;
	}
	// Todo output api list
	exit(0);
}

static void call_test()
{
	string command;

	cout << "input verb and argments" << endl;

	/* read the buffer */
	for(;;){
		char line[1023];
		cin.getline(line, sizeof(line));
		command = line;
		if(command.empty()){
			continue;
		}

		vector<string> v_command = split(command, ' ');
		/*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr)
		{
			cout << *itr <<endl;
		}*/
		size_t num = v_command.size();
		if(num % 2 == 0){
			cout << "If command contains args, please input <key,value> in argument part" << endl;
			continue;
		}
		/* create json object */
		struct json_object* j_obj = json_object_new_object();
		for(int i = 1;i < (v_command.size()) ;++i){
			struct json_object* val		= json_object_new_string(v_command[i+1].c_str());
			json_object_object_add(j_obj, v_command[i].c_str(), val);
			++i;
		}
		/* call verb via LibHomeScreen */
		hs->call(v_command[0], j_obj);
		/* free vector */
		vector<string>().swap(v_command);
		string().swap(command);
	}
}

static void onRep(struct json_object* reply_contents)
{
    const char* str = json_object_to_json_string(reply_contents);
    cout << "test.cpp [CB onRep]: " << str << endl;
    //json_object_put(reply_contents); do not release!!!
}

static void onEv(const string& event, struct json_object* event_contents)
{
    const char* str = json_object_to_json_string(event_contents);
    cout << "test.cpp [CB onEvent]: event:  " << event.c_str() << "  contents: " << str << endl;
    //json_object_put(event_contents); do not release!!!
}

static vector<string> split(const string& str, char sep)
{
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while( getline(ss, buffer, sep) ) {
		if(!buffer.empty())
        	v.push_back(buffer);
    }
    return v;
}

int main(int argc, char **argv)
{
	int ret;
	if(argc == 1)
	{
		printf("Please input port num in first argument, and token in second argument");
		usage();
		return 0;
	}
	if(argc == 2)
	{
		string av(argv[1]);
		if( (av == "-h") || (av == "--help"))
		{
			usage();
			return 0;
		}
	}

	string port_string(argv[1]);
	string token(argv[2]);
	char* endptr;
	long port = strtol(port_string.c_str(),&endptr,10);

    /* error check of range */
    if( (port > 20000) || (port < 0) )
    {
		printf("input under 20000(temporary number)");
        return 0;
    }
    if(*endptr != '\0')
    {
		printf("not number");
        return 0;
    }

  cout << "Call test for LibHomeScreen" << endl;
	hs = new LibHomeScreen();
  hs->init(port, token);

	// hs->registerCallback(&onEv, &onRep);
  //
  // hs->subscribe(event_list[0]); // tap_shortcut event subscribe
  // hs->subscribe(event_list[1]);

  hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [](const char* application_name){
    cout << "set_event_handler Event_TapShortcut application_name = " << application_name << endl;
  });

  hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [](const char* display_message){
    cout << "set_event_handler Event_OnScreenMessage display_message = " << display_message << endl;
  });


	if (ret < 0) {
		printf("failed to create event loop");
		return -1;
	}

	call_test();

	return 0;
}