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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
#define LOG_TAG "PoseClient"
#include <dvr/dvr_shared_buffers.h>
#include <dvr/pose_client.h>
#include <stdint.h>
#include <log/log.h>
#include <pdx/client.h>
#include <pdx/default_transport/client_channel_factory.h>
#include <pdx/file_handle.h>
#include <private/dvr/buffer_hub_queue_client.h>
#include <private/dvr/consumer_buffer.h>
#include <private/dvr/display_client.h>
#include <private/dvr/pose-ipc.h>
#include <private/dvr/shared_buffer_helpers.h>
using android::dvr::ConsumerQueue;
using android::pdx::LocalHandle;
using android::pdx::LocalChannelHandle;
using android::pdx::Status;
using android::pdx::Transaction;
namespace android {
namespace dvr {
namespace {
typedef CPUMappedBroadcastRing<DvrPoseRing> SensorPoseRing;
constexpr static int32_t MAX_CONTROLLERS = 2;
} // namespace
// PoseClient is a remote interface to the pose service in sensord.
class PoseClient : public pdx::ClientBase<PoseClient> {
public:
~PoseClient() override {}
// Casts C handle into an instance of this class.
static PoseClient* FromC(DvrPoseClient* client) {
return reinterpret_cast<PoseClient*>(client);
}
// Polls the pose service for the current state and stores it in *state.
// Returns zero on success, a negative error code otherwise.
int Poll(DvrPose* state) {
// Allocate the helper class to access the sensor pose buffer.
if (sensor_pose_buffer_ == nullptr) {
sensor_pose_buffer_ = std::make_unique<SensorPoseRing>(
DvrGlobalBuffers::kSensorPoseBuffer, CPUUsageMode::READ_RARELY);
}
if (state) {
if (sensor_pose_buffer_->GetNewest(state)) {
return 0;
} else {
return -EAGAIN;
}
}
return -EINVAL;
}
int GetPose(uint32_t vsync_count, DvrPoseAsync* out_pose) {
const auto vsync_buffer = GetVsyncBuffer();
if (vsync_buffer) {
*out_pose =
vsync_buffer
->vsync_poses[vsync_count & DvrVsyncPoseBuffer::kIndexMask];
return 0;
} else {
return -EAGAIN;
}
}
uint32_t GetVsyncCount() {
const auto vsync_buffer = GetVsyncBuffer();
if (vsync_buffer) {
return vsync_buffer->vsync_count;
}
return 0;
}
int GetControllerPose(int32_t controller_id, uint32_t vsync_count,
DvrPoseAsync* out_pose) {
if (controller_id < 0 || controller_id >= MAX_CONTROLLERS) {
return -EINVAL;
}
if (!controllers_[controller_id].mapped_pose_buffer) {
int ret = GetControllerRingBuffer(controller_id);
if (ret < 0)
return ret;
}
*out_pose =
controllers_[controller_id]
.mapped_pose_buffer[vsync_count & DvrVsyncPoseBuffer::kIndexMask];
return 0;
}
int LogController(bool enable) {
Transaction trans{*this};
Status<int> status = trans.Send<int>(DVR_POSE_LOG_CONTROLLER, &enable,
sizeof(enable), nullptr, 0);
ALOGE_IF(!status, "Pose LogController() failed because: %s",
status.GetErrorMessage().c_str());
return ReturnStatusOrError(status);
}
// Freezes the pose to the provided state. Future poll operations will return
// this state until a different state is frozen or SetMode() is called with a
// different mode.
// Returns zero on success, a negative error code otherwise.
int Freeze(const DvrPose& frozen_state) {
Transaction trans{*this};
Status<int> status = trans.Send<int>(DVR_POSE_FREEZE, &frozen_state,
sizeof(frozen_state), nullptr, 0);
ALOGE_IF(!status, "Pose Freeze() failed because: %s\n",
status.GetErrorMessage().c_str());
return ReturnStatusOrError(status);
}
// Sets the data mode for the pose service.
int SetMode(DvrPoseMode mode) {
Transaction trans{*this};
Status<int> status =
trans.Send<int>(DVR_POSE_SET_MODE, &mode, sizeof(mode), nullptr, 0);
ALOGE_IF(!status, "Pose SetPoseMode() failed because: %s",
status.GetErrorMessage().c_str());
return ReturnStatusOrError(status);
}
// Gets the data mode for the pose service.
int GetMode(DvrPoseMode* out_mode) {
int mode;
Transaction trans{*this};
Status<int> status =
trans.Send<int>(DVR_POSE_GET_MODE, nullptr, 0, &mode, sizeof(mode));
ALOGE_IF(!status, "Pose GetPoseMode() failed because: %s",
status.GetErrorMessage().c_str());
if (status)
*out_mode = DvrPoseMode(mode);
return ReturnStatusOrError(status);
}
int GetTangoReaderHandle(uint64_t data_type, ConsumerQueue** queue_out) {
// Get buffer.
Transaction trans{*this};
Status<LocalChannelHandle> status = trans.Send<LocalChannelHandle>(
DVR_POSE_GET_TANGO_READER, &data_type, sizeof(data_type), nullptr, 0);
if (!status) {
ALOGE("PoseClient GetTangoReaderHandle() failed because: %s",
status.GetErrorMessage().c_str());
*queue_out = nullptr;
return -status.error();
}
std::unique_ptr<ConsumerQueue> consumer_queue =
ConsumerQueue::Import(status.take());
*queue_out = consumer_queue.release();
return 0;
}
int DataCapture(const DvrPoseDataCaptureRequest* request) {
Transaction trans{*this};
Status<int> status = trans.Send<int>(DVR_POSE_DATA_CAPTURE, request,
sizeof(*request), nullptr, 0);
ALOGE_IF(!status, "PoseClient DataCapture() failed because: %s\n",
status.GetErrorMessage().c_str());
return ReturnStatusOrError(status);
}
int DataReaderDestroy(uint64_t data_type) {
Transaction trans{*this};
Status<int> status = trans.Send<int>(DVR_POSE_TANGO_READER_DESTROY,
&data_type, sizeof(data_type), nullptr,
0);
ALOGE_IF(!status, "PoseClient DataReaderDestroy() failed because: %s\n",
status.GetErrorMessage().c_str());
return ReturnStatusOrError(status);
}
// Enables or disables all pose processing from sensors
int EnableSensors(bool enabled) {
Transaction trans{*this};
Status<int> status = trans.Send<int>(DVR_POSE_SENSORS_ENABLE, &enabled,
sizeof(enabled), nullptr, 0);
ALOGE_IF(!status, "Pose EnableSensors() failed because: %s\n",
status.GetErrorMessage().c_str());
return ReturnStatusOrError(status);
}
int GetRingBuffer(DvrPoseRingBufferInfo* out_info) {
// First time mapping the buffer?
const auto vsync_buffer = GetVsyncBuffer();
if (vsync_buffer) {
if (out_info) {
out_info->min_future_count = DvrVsyncPoseBuffer::kMinFutureCount;
out_info->total_count = DvrVsyncPoseBuffer::kSize;
out_info->buffer = vsync_buffer->vsync_poses;
}
return -EINVAL;
}
return -EAGAIN;
}
int GetControllerRingBuffer(int32_t controller_id) {
if (controller_id < 0 || controller_id >= MAX_CONTROLLERS) {
return -EINVAL;
}
ControllerClientState& client_state = controllers_[controller_id];
if (client_state.pose_buffer.get()) {
return 0;
}
Transaction trans{*this};
Status<LocalChannelHandle> status = trans.Send<LocalChannelHandle>(
DVR_POSE_GET_CONTROLLER_RING_BUFFER, &controller_id,
sizeof(controller_id), nullptr, 0);
if (!status) {
return -status.error();
}
auto buffer = ConsumerBuffer::Import(status.take());
if (!buffer) {
ALOGE("Pose failed to import ring buffer");
return -EIO;
}
constexpr size_t size = DvrVsyncPoseBuffer::kSize * sizeof(DvrPoseAsync);
void* addr = nullptr;
int ret = buffer->GetBlobReadWritePointer(size, &addr);
if (ret < 0 || !addr) {
ALOGE("Pose failed to map ring buffer: ret:%d, addr:%p", ret, addr);
return -EIO;
}
client_state.pose_buffer.swap(buffer);
client_state.mapped_pose_buffer = static_cast<const DvrPoseAsync*>(addr);
ALOGI(
"Mapped controller %d pose data translation %f,%f,%f quat %f,%f,%f,%f",
controller_id, client_state.mapped_pose_buffer[0].position[0],
client_state.mapped_pose_buffer[0].position[1],
client_state.mapped_pose_buffer[0].position[2],
client_state.mapped_pose_buffer[0].orientation[0],
client_state.mapped_pose_buffer[0].orientation[1],
client_state.mapped_pose_buffer[0].orientation[2],
client_state.mapped_pose_buffer[0].orientation[3]);
return 0;
}
private:
friend BASE;
// Set up a channel to the pose service.
PoseClient()
: BASE(pdx::default_transport::ClientChannelFactory::Create(
DVR_POSE_SERVICE_CLIENT)) {
// TODO(eieio): Cache the pose and make timeout 0 so that the API doesn't
// block while waiting for the pose service to come back up.
EnableAutoReconnect(kInfiniteTimeout);
}
PoseClient(const PoseClient&) = delete;
PoseClient& operator=(const PoseClient&) = delete;
const DvrVsyncPoseBuffer* GetVsyncBuffer() {
if (mapped_vsync_pose_buffer_ == nullptr) {
if (vsync_pose_buffer_ == nullptr) {
// The constructor tries mapping it so we do not need TryMapping after.
vsync_pose_buffer_ = std::make_unique<CPUMappedBuffer>(
DvrGlobalBuffers::kVsyncPoseBuffer, CPUUsageMode::READ_OFTEN);
} else if (vsync_pose_buffer_->IsMapped() == false) {
vsync_pose_buffer_->TryMapping();
}
if (vsync_pose_buffer_->IsMapped()) {
mapped_vsync_pose_buffer_ =
static_cast<DvrVsyncPoseBuffer*>(vsync_pose_buffer_->Address());
}
}
return mapped_vsync_pose_buffer_;
}
// The vsync pose buffer if already mapped.
std::unique_ptr<CPUMappedBuffer> vsync_pose_buffer_;
// The direct sensor pose buffer.
std::unique_ptr<SensorPoseRing> sensor_pose_buffer_;
const DvrVsyncPoseBuffer* mapped_vsync_pose_buffer_ = nullptr;
struct ControllerClientState {
std::unique_ptr<ConsumerBuffer> pose_buffer;
const DvrPoseAsync* mapped_pose_buffer = nullptr;
};
ControllerClientState controllers_[MAX_CONTROLLERS];
};
int dvrPoseClientGetDataReaderHandle(DvrPoseClient* client, uint64_t type,
ConsumerQueue** queue_out) {
return PoseClient::FromC(client)->GetTangoReaderHandle(type, queue_out);
}
} // namespace dvr
} // namespace android
using android::dvr::PoseClient;
extern "C" {
DvrPoseClient* dvrPoseClientCreate() {
auto* client = PoseClient::Create().release();
return reinterpret_cast<DvrPoseClient*>(client);
}
void dvrPoseClientDestroy(DvrPoseClient* client) {
delete PoseClient::FromC(client);
}
int dvrPoseClientGet(DvrPoseClient* client, uint32_t vsync_count,
DvrPoseAsync* out_pose) {
return PoseClient::FromC(client)->GetPose(vsync_count, out_pose);
}
uint32_t dvrPoseClientGetVsyncCount(DvrPoseClient* client) {
return PoseClient::FromC(client)->GetVsyncCount();
}
int dvrPoseClientGetController(DvrPoseClient* client, int32_t controller_id,
uint32_t vsync_count, DvrPoseAsync* out_pose) {
return PoseClient::FromC(client)->GetControllerPose(controller_id,
vsync_count, out_pose);
}
int dvrPoseClientLogController(DvrPoseClient* client, bool enable) {
return PoseClient::FromC(client)->LogController(enable);
}
int dvrPoseClientPoll(DvrPoseClient* client, DvrPose* state) {
return PoseClient::FromC(client)->Poll(state);
}
int dvrPoseClientFreeze(DvrPoseClient* client, const DvrPose* frozen_state) {
return PoseClient::FromC(client)->Freeze(*frozen_state);
}
int dvrPoseClientModeSet(DvrPoseClient* client, DvrPoseMode mode) {
return PoseClient::FromC(client)->SetMode(mode);
}
int dvrPoseClientModeGet(DvrPoseClient* client, DvrPoseMode* mode) {
return PoseClient::FromC(client)->GetMode(mode);
}
int dvrPoseClientSensorsEnable(DvrPoseClient* client, bool enabled) {
return PoseClient::FromC(client)->EnableSensors(enabled);
}
int dvrPoseClientDataCapture(DvrPoseClient* client,
const DvrPoseDataCaptureRequest* request) {
return PoseClient::FromC(client)->DataCapture(request);
}
int dvrPoseClientDataReaderDestroy(DvrPoseClient* client, uint64_t data_type) {
return PoseClient::FromC(client)->DataReaderDestroy(data_type);
}
} // extern "C"
|