FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openauto/openauto/App.cpp at develop · openDsh/openauto · GitHub
openDsh
openauto
Repository navigation
Code
Issues
1
(1)
Pull requests
2
(2)
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
openauto
/
openauto
/
App.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
186 lines (155 loc) · 5.76 KB
Breadcrumbs
openauto
/
openauto
/
App.cpp
Copy path
File metadata and controls
186 lines (155 loc) · 5.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
180
181
182
183
184
185
186
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
<
thread
>
#
include
"
aasdk/USB/AOAPDevice.hpp
"
#
include
"
aasdk/TCP/TCPEndpoint.hpp
"
#
include
"
openauto/App.hpp
"
#
include
"
OpenautoLog.hpp
"
namespace
openauto
{
App::App
(boost::asio::io_service& ioService, aasdk::usb::USBWrapper& usbWrapper, aasdk::tcp::ITCPWrapper& tcpWrapper, openauto::service::IAndroidAutoEntityFactory& androidAutoEntityFactory,
aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator)
: ioService_(ioService)
, usbWrapper_(usbWrapper)
, tcpWrapper_(tcpWrapper)
, strand_(ioService_)
, androidAutoEntityFactory_(androidAutoEntityFactory)
, usbHub_(std::move(usbHub))
, acceptor_(ioService_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),
5000
))
, connectedAccessoriesEnumerator_(std::move(connectedAccessoriesEnumerator))
, isStopped_(
false
)
{
}
void
App::waitForDevice
(
bool
enumerate)
{
this
->
waitForUSBDevice
();
this
->
waitForWirelessDevice
();
if
(enumerate)
{
strand_.
dispatch
([
this
, self =
this
->
shared_from_this
()]() {
this
->
enumerateDevices
();
});
}
}
void
App::start
(aasdk::tcp::ITCPEndpoint::SocketPointer socket)
{
OPENAUTO_LOG
(info) <<
"
[App] Wireless Device connected.
"
;
strand_.
dispatch
([
this
, self =
this
->
shared_from_this
(), socket =
std::move
(socket)]()
mutable
{
if
(androidAutoEntity_ !=
nullptr
)
{
tcpWrapper_.
close
(*socket);
OPENAUTO_LOG
(warning) <<
"
[App] android auto entity is still running.
"
;
return
;
}
try
{
usbHub_->
cancel
();
connectedAccessoriesEnumerator_->
cancel
();
auto
tcpEndpoint
(std::make_shared<aasdk::tcp::TCPEndpoint>(tcpWrapper_,
std::move
(socket)));
androidAutoEntity_ = androidAutoEntityFactory_.
create
(
std::move
(tcpEndpoint));
androidAutoEntity_->
start
(*
this
);
}
catch
(
const
aasdk::error::Error& error)
{
OPENAUTO_LOG
(error) <<
"
[App] TCP AndroidAutoEntity create error:
"
<< error.
what
();
androidAutoEntity_.
reset
();
this
->
waitForDevice
();
}
});
}
void
App::stop
()
{
strand_.
dispatch
([
this
, self =
this
->
shared_from_this
()]() {
isStopped_ =
true
;
connectedAccessoriesEnumerator_->
cancel
();
usbHub_->
cancel
();
if
(androidAutoEntity_ !=
nullptr
)
{
androidAutoEntity_->
stop
();
androidAutoEntity_.
reset
();
}
});
}
void
App::aoapDeviceHandler
(aasdk::usb::DeviceHandle deviceHandle)
{
OPENAUTO_LOG
(info) <<
"
[App] USB Device connected.
"
;
if
(androidAutoEntity_ !=
nullptr
)
{
OPENAUTO_LOG
(warning) <<
"
[App] android auto entity is still running.
"
;
return
;
}
try
{
connectedAccessoriesEnumerator_->
cancel
();
auto
aoapDevice
(
aasdk::usb::AOAPDevice::create
(usbWrapper_, ioService_, deviceHandle));
androidAutoEntity_ = androidAutoEntityFactory_.
create
(
std::move
(aoapDevice));
androidAutoEntity_->
start
(*
this
);
}
catch
(
const
aasdk::error::Error& error)
{
OPENAUTO_LOG
(error) <<
"
[App] USB AndroidAutoEntity create error:
"
<< error.
what
();
androidAutoEntity_.
reset
();
this
->
waitForDevice
();
}
}
void
App::enumerateDevices
()
{
auto
promise =
aasdk::usb::IConnectedAccessoriesEnumerator::Promise::defer
(strand_);
promise->
then
([
this
, self =
this
->
shared_from_this
()](
auto
result) {
OPENAUTO_LOG
(info) <<
"
[App] Devices enumeration result:
"
<< result;
},
[
this
, self =
this
->
shared_from_this
()](
auto
e) {
OPENAUTO_LOG
(error) <<
"
[App] Devices enumeration failed:
"
<< e.
what
();
});
connectedAccessoriesEnumerator_->enumerate(
std::move
(promise));
}
void
App::waitForUSBDevice
()
{
OPENAUTO_LOG
(info) <<
"
[App] Waiting for USB device...
"
;
auto
promise =
aasdk::usb::IUSBHub::Promise::defer
(strand_);
promise->
then
(
std::bind
(&App::aoapDeviceHandler,
this
->
shared_from_this
(), std::placeholders::_1),
std::bind
(&App::onUSBHubError,
this
->
shared_from_this
(), std::placeholders::_1));
usbHub_->
start
(
std::move
(promise));
}
void
App::waitForWirelessDevice
()
{
OPENAUTO_LOG
(info) <<
"
[App] Waiting for Wireless device...
"
;
auto
socket = std::make_shared<boost::asio::ip::tcp::socket>(ioService_);
acceptor_.
async_accept
(*socket, [
this
, socket](
const
boost::system::error_code &) {
this
->
start
(socket); });
}
void
App::onAndroidAutoQuit
()
{
strand_.
dispatch
([
this
, self =
this
->
shared_from_this
()]() {
OPENAUTO_LOG
(info) <<
"
[App] quit.
"
;
androidAutoEntity_->
stop
();
androidAutoEntity_.
reset
();
if
(!isStopped_)
{
this
->
waitForDevice
();
}
});
}
void
App::onUSBHubError
(
const
aasdk::error::Error& error)
{
OPENAUTO_LOG
(error) <<
"
[App] usb hub error:
"
<< error.
what
();
if
(error != aasdk::error::ErrorCode::
OPERATION_ABORTED
&&
error != aasdk::error::ErrorCode::
OPERATION_IN_PROGRESS
)
{
this
->
waitForDevice
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL