7db34e105b4baaec092c75b0a0daa76512243b2f
[apps/agl-service-navigation.git] / libnavi / src / RequestManage.cpp
1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
3
4 #include <string.h>
5 #include <errno.h>
6 #include <pthread.h>
7 #include <time.h>
8 #include <unistd.h>
9 #include <systemd/sd-event.h>
10 #include <json-c/json.h>
11 #include <traces.h>
12 #include "RequestManage.h"
13
14 /**
15  *  @brief constructor
16  */
17 RequestManage::RequestManage() : listener(nullptr)
18 {
19         // Callback setting
20         this->wsj1_itf.on_hangup    = RequestManage::OnHangupStatic;
21         this->wsj1_itf.on_call      = RequestManage::OnCallStatic;
22         this->wsj1_itf.on_event     = RequestManage::OnEventStatic;
23
24         pthread_cond_init(&this->cond, nullptr);
25         pthread_mutex_init(&this->mutex, nullptr);
26 }
27
28 /**
29  *  @brief Destructor
30  */
31 RequestManage::~RequestManage()
32 {
33 }
34
35 void* RequestManage::BinderThread(void* param)
36 {
37         RequestManage* instance = (RequestManage*) param;
38         sd_event *loop;
39
40         int rc = sd_event_default(&loop);
41         if (rc < 0) {
42                 TRACE_ERROR("connection to default event loop failed: %s\n", strerror(-rc));
43                 return nullptr;
44         }
45
46         instance->wsj1 = afb_ws_client_connect_wsj1(loop, instance->requestURL->c_str(), &instance->wsj1_itf, nullptr);
47         if (instance->wsj1 == nullptr)
48         {
49                 TRACE_ERROR("connection to %s failed: %m\n", api_url);
50                 return nullptr;
51         }
52
53         // Signal
54         pthread_mutex_unlock(&instance->mutex);
55         pthread_cond_signal(&instance->cond);
56
57         while (1)
58         {
59                 sd_event_run(loop, 1000 * 1000); // 1sec
60         }
61
62         return nullptr;
63 }
64
65 /**
66  *  @brief  Connect with a service
67  *  @param  URL
68  *  @return Success or failure of connection
69  */
70 bool RequestManage::Connect(const char* api_url, RequestManageListener* listener)
71 {
72         this->listener = listener;
73         this->requestURL = new std::string(api_url);
74
75         pthread_t thread_id;
76         pthread_create(&thread_id, nullptr, RequestManage::BinderThread, (void*)this);
77
78         // Wait until response comes
79         pthread_mutex_lock(&this->mutex);
80         pthread_cond_wait(&this->cond, &this->mutex);
81         pthread_mutex_unlock(&this->mutex);
82
83         if (this->wsj1 == nullptr)
84         {
85                 return false;
86         }
87
88         return true;
89 }
90
91 /**
92  *  @brief  Connection status check with service
93  *  @return Connection status
94  */
95 bool RequestManage::IsConnect(){
96         return (this->wsj1 != NULL);
97 }
98
99 /**
100  *  @brief  Call Binder's API
101  *  @param  api      api
102  *  @param  verb     method
103  *  @param  req_json Json style request
104  *  @return Success or failure of processing
105  */
106 bool RequestManage::CallBinderAPI(const char* api, const char* verb, const char* req_json)
107 {
108         // Send request
109         int rc = afb_wsj1_call_s(this->wsj1, api, verb, req_json, RequestManage::OnReplyStatic, this);
110         if (rc < 0)
111         {
112                 TRACE_ERROR("calling %s/%s(%s) failed: %m\n", api, verb, req_json);
113                 return false;
114         }
115
116         return true;
117 }
118
119 /**
120  *  @brief  Set session handle
121  *  @param session Session handle
122  */
123 void RequestManage::SetSessionHandle( uint32_t session )
124 {
125         this->sessionHandle = session;
126 }
127
128 /**
129  *  @brief  Get session handle
130  *  @return Session handle
131  */
132 uint32_t RequestManage::GetSessionHandle()
133 {
134         return this->sessionHandle;
135 }
136
137 /**
138  *  @brief  Set route handle
139  *  @param route Route handle
140  */
141 void RequestManage::SetRouteHandle( uint32_t route )
142 {
143         this->routeHandle = route;
144 }
145
146 /**
147  *  @brief  Get route handle
148  *  @return Route handle
149  */
150 uint32_t RequestManage::GetRouteHandle()
151 {
152         return this->routeHandle;
153 }
154
155 void RequestManage::OnReply(struct afb_wsj1_msg *msg)
156 {
157         struct json_object * json = afb_wsj1_msg_object_j(msg);
158
159         this->listener->OnReply(json);
160 }
161
162 void RequestManage::OnHangup(struct afb_wsj1 *wsj1)
163 {
164 }
165
166 void RequestManage::OnCallStatic(const char *api, const char *verb, struct afb_wsj1_msg *msg)
167 {
168 }
169
170 void RequestManage::OnEventStatic(const char *event, struct afb_wsj1_msg *msg)
171 {
172 }
173
174
175 /**
176  *  @brief  Answer callback from service
177  */
178 void RequestManage::OnReplyStatic(void *closure, struct afb_wsj1_msg *msg)
179 {
180         RequestManage* instance = (RequestManage *)closure;
181         instance->OnReply(msg);
182 }
183
184 /**
185  *  @brief  Service hang notification
186  */
187 void RequestManage::OnHangupStatic(void *closure, struct afb_wsj1 *wsj1)
188 {
189         printf("DEBUG:%s:%d (%p,%p)\n", __func__, __LINE__, closure, wsj1);
190         fflush(stdout);
191 }
192
193 void RequestManage::OnCallStatic(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
194 {
195         printf("DEBUG:%s:%d (%p,%s,%s,%p)\n", __func__, __LINE__, closure, api, verb, msg);
196         fflush(stdout);
197 }
198
199 void RequestManage::OnEventStatic(void *closure, const char *event, struct afb_wsj1_msg *msg)
200 {
201         printf("DEBUG:%s:%d (%p,%s,%p)\n", __func__, __LINE__, closure, event, msg);
202         fflush(stdout);
203 }
204