blob: f7757568c667392f2b02fd1b7f2578b2f820a39b [file] [log] [blame]
Andrew Scull96517d82017-10-09 12:53:19 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
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
Allen Webbfbf534f2017-09-14 14:29:00 -070017#include <nos/NuggetClient.h>
18
Andrew Scull96517d82017-10-09 12:53:19 +010019#include <limits>
20
21#include <nos/transport.h>
22
Allen Webbfbf534f2017-09-14 14:29:00 -070023#include <application.h>
24
25namespace nos {
26
Andrew Scull96517d82017-10-09 12:53:19 +010027NuggetClient::NuggetClient()
28 : NuggetClient("") {
29}
Allen Webbfbf534f2017-09-14 14:29:00 -070030
Andrew Scull96517d82017-10-09 12:53:19 +010031NuggetClient::NuggetClient(const std::string& device_name)
32 : device_name_(device_name), open_(false) {
33}
Allen Webbfbf534f2017-09-14 14:29:00 -070034
Andrew Scull96517d82017-10-09 12:53:19 +010035NuggetClient::~NuggetClient() {
36 Close();
37}
38
39void NuggetClient::Open() {
40 if (!open_) {
41 open_ = nos_device_open(
42 device_name_.empty() ? nullptr : device_name_.c_str(), &device_) == 0;
Allen Webbfbf534f2017-09-14 14:29:00 -070043 }
44}
45
Andrew Scull96517d82017-10-09 12:53:19 +010046void NuggetClient::Close() {
47 if (open_) {
48 device_.ops.close(device_.ctx);
49 open_ = false;
50 }
51}
52
53bool NuggetClient::IsOpen() const {
54 return open_;
55}
56
57uint32_t NuggetClient::CallApp(uint32_t appId, uint16_t arg,
58 const std::vector<uint8_t>& request,
59 std::vector<uint8_t>* response) {
60 if (!open_) {
Andrew Scull3935b182017-10-11 16:02:39 +010061 return APP_ERROR_IO;
Andrew Scull96517d82017-10-09 12:53:19 +010062 }
63
64 if (request.size() > std::numeric_limits<uint32_t>::max()) {
65 return APP_ERROR_TOO_MUCH;
66 }
67
68 const uint32_t requestSize = request.size();
69 uint32_t replySize = 0;
70 uint8_t* replyData = nullptr;
71
72 if (response != nullptr) {
73 response->resize(response->capacity());
74 replySize = response->size();
75 replyData = response->data();
76 }
77
78 uint32_t status_code = nos_call_application(&device_, appId, arg,
79 request.data(), requestSize,
80 replyData, &replySize);
81
82 if (response != nullptr) {
83 response->resize(replySize);
84 }
85
86 return status_code;
87}
88
89nos_device* NuggetClient::Device() {
Andrew Sculle0b457e2018-01-13 16:03:50 +000090 return open_ ? &device_ : nullptr;
Andrew Scull96517d82017-10-09 12:53:19 +010091}
92
93const nos_device* NuggetClient::Device() const {
Andrew Sculle0b457e2018-01-13 16:03:50 +000094 return open_ ? &device_ : nullptr;
Andrew Scull96517d82017-10-09 12:53:19 +010095}
96
97const std::string& NuggetClient::DeviceName() const {
98 return device_name_;
99}
100
Allen Webbfbf534f2017-09-14 14:29:00 -0700101} // namespace nos