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_APP_CLIENT_H
18 #define NOS_APP_CLIENT_H
19 
20 #include <cstdint>
21 #include <vector>
22 
23 #include <nos/NuggetClientInterface.h>
24 
25 namespace nos {
26 
27 /**
28  * Client to communicate with an app running on Nugget.
29  */
30 class AppClient {
31 public:
32     /**
33      * Create a client for an app with the given ID that communicates with
34      * Nugget through the given NuggetClient.
35      *
36      * @param client Client for Nugget.
37      * @param appId  ID of the target app.
38      */
AppClient(NuggetClientInterface & client,uint32_t appId)39     AppClient(NuggetClientInterface& client, uint32_t appId)
40             : _client(client), _appId(appId) {}
41 
42     /**
43      * Call the app.
44      *
45      * @param arg      Argument to pass to the app.
46      * @param request  Data to send to the app.
47      * @param response Buffer to receive data from the app.
48      */
Call(uint16_t arg,const std::vector<uint8_t> & request,std::vector<uint8_t> * response)49     uint32_t Call(uint16_t arg, const std::vector<uint8_t>& request,
50                   std::vector<uint8_t>* response) {
51         return _client.CallApp(_appId, arg, request, response);
52     }
53 
54     /**
55      * Call the app.
56      *
57      * @param arg      Argument to pass to the app.
58      * @param req_ptr  Data to send to the app.
59      * @param req_len  Number of bytes to send to the app.
60      * @param resp_ptr Buffer to receive data from the app.
61      * @param resp_len In: Max number of bytes to receive from the app.
62      *                 Out: Actual number of bytes received from the app.
63      */
Call(uint16_t arg,const void * req_ptr,uint32_t req_len,void * resp_ptr,uint32_t * resp_len)64     uint32_t Call(uint16_t arg, const void* req_ptr, uint32_t req_len,
65                   void* resp_ptr, uint32_t* resp_len) {
66         return _client.CallApp(_appId, arg, req_ptr, req_len, resp_ptr,
67                                resp_len);
68     }
69 
70 private:
71     NuggetClientInterface& _client;
72     uint32_t _appId;
73 };
74 
75 } // namespace nos
76 
77 #endif // NOS_APP_CLIENT_H
78