aboutsummaryrefslogtreecommitdiff
path: root/daemon/peripheral_manager_client.cc
blob: bd055374b2f41487002aa64d88b5e515faa5fffe (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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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 "peripheral_manager_client.h"

#include <base/logging.h>

namespace android {

PeripheralManagerClient::PeripheralManagerClient() {}
PeripheralManagerClient::~PeripheralManagerClient() {}

Status PeripheralManagerClient::ListGpio(std::vector<std::string>* gpios) {
  *gpios = GpioManager::GetGpioManager()->GetGpioPins();
  return Status::ok();
}

Status PeripheralManagerClient::OpenGpio(const std::string& name) {
  if (!GpioManager::GetGpioManager()->HasGpio(name))
    return Status::fromServiceSpecificError(ENODEV);

  auto gpio = GpioManager::GetGpioManager()->OpenGpioPin(name);
  if (!gpio) {
    LOG(ERROR) << "Failed to open GPIO " << name;
    return Status::fromServiceSpecificError(EBUSY);
  }

  gpios_.emplace(name, std::move(gpio));
  return Status::ok();
}

Status PeripheralManagerClient::ReleaseGpio(const std::string& name) {
  gpios_.erase(name);
  return Status::ok();
}

Status PeripheralManagerClient::SetGpioEdge(const std::string& name, int type) {
  if (!gpios_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (gpios_.find(name)->second->SetEdgeType(GpioEdgeType(type)))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SetGpioActiveType(const std::string& name,
                                                  int type) {
  if (!gpios_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (gpios_.find(name)->second->SetActiveType(GpioActiveType(type)))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SetGpioDirection(const std::string& name,
                                                 int direction) {
  if (!gpios_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (gpios_.find(name)->second->SetDirection(GpioDirection(direction)))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SetGpioValue(const std::string& name,
                                             bool value) {
  if (!gpios_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (gpios_.find(name)->second->SetValue(value))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::GetGpioValue(const std::string& name,
                                             bool* value) {
  if (!gpios_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (gpios_.find(name)->second->GetValue(value))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::GetGpioPollingFd(
    const std::string& name,
    ::android::base::unique_fd* fd) {
  if (!gpios_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (gpios_.find(name)->second->GetPollingFd(fd))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::ListSpiBuses(std::vector<std::string>* buses) {
  *buses = SpiManager::GetSpiManager()->GetSpiDevBuses();
  return Status::ok();
}

Status PeripheralManagerClient::OpenSpiDevice(const std::string& name) {
  if (!SpiManager::GetSpiManager()->HasSpiDevBus(name))
    return Status::fromServiceSpecificError(ENODEV);

  std::unique_ptr<SpiDevice> device =
      SpiManager::GetSpiManager()->OpenSpiDevice(name);

  if (!device) {
    LOG(ERROR) << "Failed to open device " << name;
    return Status::fromServiceSpecificError(EBUSY);
  }

  spi_devices_.emplace(name, std::move(device));
  return Status::ok();
}

Status PeripheralManagerClient::ReleaseSpiDevice(const std::string& name) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  spi_devices_.erase(name);
  return Status::ok();
}

Status PeripheralManagerClient::SpiDeviceWriteByte(const std::string& name,
                                                   int8_t byte) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (spi_devices_.find(name)->second->WriteByte(byte))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceWriteBuffer(
    const std::string& name,
    const std::vector<uint8_t>& buffer) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (spi_devices_.find(name)->second->WriteBuffer(buffer.data(),
                                                   buffer.size()))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceTransfer(
    const std::string& name,
    const std::unique_ptr<std::vector<uint8_t>>& tx_data,
    std::unique_ptr<std::vector<uint8_t>>* rx_data,
    int size) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (!rx_data) {
    if (spi_devices_.find(name)->second->Transfer(tx_data->data(), nullptr,
                                                  size)) {
      return Status::ok();
    }
  } else {
    rx_data->reset(new std::vector<uint8_t>(size));
    if (spi_devices_.find(name)->second->Transfer(tx_data->data(),
                                                  (*rx_data)->data(), size)) {
      return Status::ok();
    }
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceSetMode(const std::string& name,
                                                 int mode) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (spi_devices_.find(name)->second->SetMode(SpiMode(mode)))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceSetFrequency(const std::string& name,
                                                      int frequency_hz) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (frequency_hz > 0 &&
      spi_devices_.find(name)->second->SetFrequency(frequency_hz))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceSetBitJustification(
    const std::string& name,
    bool lsb_first) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (spi_devices_.find(name)->second->SetBitJustification(lsb_first))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceSetBitsPerWord(const std::string& name,
                                                        int nbits) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (spi_devices_.find(name)->second->SetBitsPerWord(nbits))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::SpiDeviceSetDelay(const std::string& name,
                                                  int delay_usecs) {
  if (!spi_devices_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  // |delay_usecs| must be positive and fit in an unsigned 16 bit.
  if (delay_usecs < 0 || delay_usecs > INT16_MAX)
    return Status::fromServiceSpecificError(EINVAL);

  if (spi_devices_.find(name)->second->SetDelay(
          static_cast<uint16_t>(delay_usecs)))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::ListLeds(std::vector<std::string>* leds) {
  *leds = LedManager::GetLedManager()->GetLeds();
  return Status::ok();
}

Status PeripheralManagerClient::OpenLed(const std::string& name) {
  if (!LedManager::GetLedManager()->HasLed(name)) {
    return Status::fromServiceSpecificError(ENODEV);
  }
  auto led = LedManager::GetLedManager()->OpenLed(name);
  if (!led) {
    LOG(ERROR) << "Failed to open LED " << name;
    return Status::fromServiceSpecificError(EBUSY);
  }

  leds_.emplace(name, std::move(led));
  return Status::ok();
}

Status PeripheralManagerClient::ReleaseLed(const std::string& name) {
  leds_.erase(name);
  return Status::ok();
}

Status PeripheralManagerClient::LedGetBrightness(const std::string& name,
                                                 int* brightness) {
  if (!leds_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  uint32_t temp;
  if (leds_.find(name)->second->GetBrightness(&temp)) {
    *brightness = temp;
    return Status::ok();
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::LedGetMaxBrightness(const std::string& name,
                                                    int* max_brightness) {
  if (!leds_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  uint32_t temp;
  if (leds_.find(name)->second->GetMaxBrightness(&temp)) {
    *max_brightness = temp;
    return Status::ok();
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::LedSetBrightness(const std::string& name,
                                                 int brightness) {
  if (!leds_.count(name))
    return Status::fromServiceSpecificError(EPERM);

  if (leds_.find(name)->second->SetBrightness(brightness))
    return Status::ok();

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::ListI2cBuses(std::vector<std::string>* buses) {
  *buses = I2cManager::GetI2cManager()->GetI2cDevBuses();

  return Status::ok();
}

Status PeripheralManagerClient::OpenI2cDevice(const std::string& name,
                                              int32_t address) {
  if (!I2cManager::GetI2cManager()->HasI2cDevBus(name))
    return Status::fromServiceSpecificError(ENODEV);

  std::unique_ptr<I2cDevice> device =
      I2cManager::GetI2cManager()->OpenI2cDevice(name, address);

  if (!device) {
    LOG(ERROR) << "Failed to open device " << name;
    return Status::fromServiceSpecificError(EBUSY);
  }
  std::pair<std::string, uint32_t> i2c_dev(name, address);
  i2c_devices_.emplace(i2c_dev, std::move(device));
  return Status::ok();
}

Status PeripheralManagerClient::ReleaseI2cDevice(const std::string& name,
                                                 int32_t address) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  i2c_devices_.erase({name, address});
  return Status::ok();
}

Status PeripheralManagerClient::I2cRead(const std::string& name,
                                        int32_t address,
                                        std::vector<uint8_t>* data,
                                        int32_t size,
                                        int32_t* bytes_read) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  if (size < 0) {
    return Status::fromServiceSpecificError(EINVAL);
  }
  data->resize(size);

  uint32_t* nread = reinterpret_cast<uint32_t*>(bytes_read);
  int32_t ret = i2c_devices_.find({name, address})
                    ->second->Read(data->data(), data->size(), nread);
  data->resize(*nread);

  return ret == 0 ? Status::ok() : Status::fromServiceSpecificError(ret);
}

Status PeripheralManagerClient::I2cReadRegByte(const std::string& name,
                                               int32_t address,
                                               int32_t reg,
                                               int32_t* val) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  uint8_t tmp_val = 0;
  if (i2c_devices_.find({name, address})->second->ReadRegByte(reg, &tmp_val) ==
      0) {
    *val = tmp_val;
    return Status::ok();
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::I2cReadRegWord(const std::string& name,
                                               int32_t address,
                                               int32_t reg,
                                               int32_t* val) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  uint16_t tmp_val = 0;
  if (i2c_devices_.find({name, address})->second->ReadRegWord(reg, &tmp_val) ==
      0) {
    *val = tmp_val;
    return Status::ok();
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::I2cReadRegBuffer(const std::string& name,
                                                 int32_t address,
                                                 int32_t reg,
                                                 std::vector<uint8_t>* data,
                                                 int32_t size,
                                                 int32_t* bytes_read) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  if (size < 0) {
    return Status::fromServiceSpecificError(EINVAL);
  }
  data->resize(size);

  uint32_t* nread = reinterpret_cast<uint32_t*>(bytes_read);
  int32_t ret =
      i2c_devices_.find({name, address})
          ->second->ReadRegBuffer(reg, data->data(), data->size(), nread);

  data->resize(*nread);
  return ret == 0 ? Status::ok() : Status::fromServiceSpecificError(ret);
}

Status PeripheralManagerClient::I2cWrite(const std::string& name,
                                         int32_t address,
                                         const std::vector<uint8_t>& data,
                                         int32_t* bytes_written) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  int ret = i2c_devices_.find({name, address})
                ->second->Write(data.data(),
                                data.size(),
                                reinterpret_cast<uint32_t*>(bytes_written));

  return ret == 0 ? Status::ok() : Status::fromServiceSpecificError(ret);
}

Status PeripheralManagerClient::I2cWriteRegByte(const std::string& name,
                                                int32_t address,
                                                int32_t reg,
                                                int8_t val) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  if (i2c_devices_.find({name, address})->second->WriteRegByte(reg, val) == 0) {
    return Status::ok();
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::I2cWriteRegWord(const std::string& name,
                                                int32_t address,
                                                int32_t reg,
                                                int32_t val) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  if (i2c_devices_.find({name, address})->second->WriteRegWord(reg, val) == 0) {
    return Status::ok();
  }

  return Status::fromServiceSpecificError(EREMOTEIO);
}

Status PeripheralManagerClient::I2cWriteRegBuffer(
    const std::string& name,
    int32_t address,
    int32_t reg,
    const std::vector<uint8_t>& data,
    int32_t* bytes_written) {
  if (!i2c_devices_.count({name, address}))
    return Status::fromServiceSpecificError(EPERM);

  int32_t ret =
      i2c_devices_.find({name, address})
          ->second->WriteRegBuffer(reg,
                                   data.data(),
                                   data.size(),
                                   reinterpret_cast<uint32_t*>(bytes_written));

  return ret == 0 ? Status::ok() : Status::fromServiceSpecificError(ret);
}

}  // namespace android