Split libhomescreen from agl-homescreen-service-2017
[src/libhomescreen.git] / include / libhomescreen.hpp
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 #ifndef LIBHOMESCREEN_H
18 #define LIBHOMESCREEN_H
19 #include <vector>
20 #include <map>
21 #include <string>
22 #include <functional>
23 #include <json-c/json.h>
24 #include <systemd/sd-event.h>
25 extern "C"
26 {
27 #include <afb/afb-binding.h>
28 #include <afb/afb-wsj1.h>
29 #include <afb/afb-ws-client.h>
30 }
31
32 class LibHomeScreen
33 {
34 public:
35     LibHomeScreen();
36     ~LibHomeScreen();
37
38                 LibHomeScreen(const LibHomeScreen &) = delete;
39                 LibHomeScreen &operator=(const LibHomeScreen &) = delete;
40
41                 using handler_func = std::function<void(const char*)>;
42
43                 enum EventType {
44                         Event_TapShortcut = 1,
45                         Event_OnScreenMessage
46                 };
47
48                 static const std::vector<std::string> api_list;
49                 static const std::vector<std::string> event_list;
50
51     /* Method */
52                 int init(const int port, const std::string& token);
53
54                 int tapShortcut(const char* application_name);
55                 int onScreenMessage(const char* display_message);
56
57                 void set_event_handler(enum EventType et, handler_func f);
58
59     void registerCallback(
60         void (*event_cb)(const std::string& event, struct json_object* event_contents),
61         void (*reply_cb)(struct json_object* reply_contents),
62         void (*hangup_cb)(void) = nullptr);
63
64     int call(const std::string& verb, struct json_object* arg);
65     int call(const char* verb, struct json_object* arg);
66     int subscribe(const std::string& event_name);
67     int unsubscribe(const std::string& event_name);
68
69 private:
70     int initialize_websocket();
71     int runEventloop();
72     
73     void (*onEvent)(const std::string& event, struct json_object* event_contents);
74     void (*onReply)(struct json_object* reply);
75     void (*onHangup)(void);
76
77     struct afb_wsj1* sp_websock;
78     struct afb_wsj1_itf minterface;
79     sd_event* mploop;
80     std::string muri;
81
82     int mport = 2000;
83     std::string mtoken = "hs";
84
85                 std::map<EventType, handler_func> handlers;
86
87 public:
88     /* Don't use/ Internal only */
89     void on_hangup(void *closure, struct afb_wsj1 *wsj);
90     void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
91     void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
92     void on_reply(void *closure, struct afb_wsj1_msg *msg);
93 };
94
95 #endif /* LIBHOMESCREEN_H */