1 /*
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 
17 #ifndef NOS_NUGGET_CLIENT_H
18 #define NOS_NUGGET_CLIENT_H
19 
20 #include <cstdint>
21 #include <string>
22 #include <vector>
23 
24 #include <nos/device.h>
25 #include <nos/NuggetClientInterface.h>
26 
27 namespace nos {
28 
29 /**
30  * Client to communicate with a Nugget device via the transport API.
31  */
32 class NuggetClient : public NuggetClientInterface {
33 public:
34     /**
35      * Create a client for the named Nugget device
36      *
37      * An empty device name causes the default device to be selected.
38      * An empty config uses default configurations.
39      */
40     NuggetClient(const std::string& name);
41     NuggetClient(const char* name = 0, uint32_t config = 0);
42 
43     ~NuggetClient() override;
44 
45     /**
46      * Opens a connection to the default Nugget device.
47      *
48      * If this fails, isOpen() will return false.
49      */
50     void Open() override;
51 
52     /**
53      * Closes the connection to Nugget.
54      */
55     void Close() override;
56 
57     /**
58      * Checked whether a connection is open to Nugget.
59      */
60     bool IsOpen() const override;
61 
62     /**
63      * Call into and app running on Nugget.
64      *
65      * @param app_id   The ID of the app to call.
66      * @param arg      Argument to pass to the app.
67      * @param request  Data to send to the app.
68      * @param response Buffer to receive data from the app.
69      * @return         Status code from the app.
70      */
71     uint32_t CallApp(uint32_t appId, uint16_t arg,
72                      const std::vector<uint8_t>& request,
73                      std::vector<uint8_t>* response) override;
74 
75     /**
76      * Call into an app running on Nugget.
77      *
78      * @param app_id   The ID of the app to call.
79      * @param arg      Argument to pass to the app.
80      * @param req_ptr  Data to send to the app.
81      * @param req_len  Number of bytes to send to the app.
82      * @param resp_ptr Buffer to receive data from the app.
83      * @param resp_len In: Max number of bytes to receive from the app.
84      *                 Out: Actual number of bytes received from the app.
85      * @return         Status code from the app.
86      */
87     uint32_t CallApp(uint32_t appId, uint16_t arg, const void* req_ptr,
88                      uint32_t req_len, void* resp_ptr,
89                      uint32_t* resp_len) override;
90 
91     /**
92      * Reset the device. Use with caution; context may be lost.
93      */
94     uint32_t Reset() const override;
95 
96     /**
97      * Access the underlying device.
98      *
99      * NULL is returned if the connection to the device is not open.
100      *
101      * This can be used by subclasses or to use to use the C API directly.
102      */
103     nos_device* Device();
104     const nos_device* Device() const;
105 
106     /**
107      * Access the name of the device this is a client for.
108      */
109     const std::string& DeviceName() const;
110 
111 protected:
112     std::string device_name_;
113     nos_device device_;
114     bool open_;
115 };
116 
117 } // namespace nos
118 
119 #endif // NOS_NUGGET_CLIENT_H
120