1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/nnapi/nnapi_util.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "tensorflow/lite/nnapi/nnapi_implementation.h"
22 #include "tensorflow/lite/util.h"
23 
24 namespace tflite {
25 namespace nnapi {
26 
27 namespace {
SimpleJoin(const std::vector<const char * > & elements,const char * separator)28 std::string SimpleJoin(const std::vector<const char*>& elements,
29                        const char* separator) {
30   // Note that we avoid use of sstream to avoid binary size bloat.
31   std::string joined_elements;
32   for (auto it = elements.begin(); it != elements.end(); ++it) {
33     if (separator && it != elements.begin()) {
34       joined_elements += separator;
35     }
36     if (*it) {
37       joined_elements += *it;
38     }
39   }
40   return joined_elements;
41 }
42 
43 }  // namespace
44 
GetDeviceNamesList()45 std::vector<const char*> GetDeviceNamesList() {
46   std::vector<const char*> device_names;
47 
48   // Only build the list if NnApiImplementation has the methods we need,
49   // leaving it empty otherwise.
50   if (NnApiImplementation()->ANeuralNetworks_getDeviceCount != nullptr) {
51     uint32_t num_devices = 0;
52 
53     NnApiImplementation()->ANeuralNetworks_getDeviceCount(&num_devices);
54 
55     for (uint32_t i = 0; i < num_devices; i++) {
56       ANeuralNetworksDevice* device = nullptr;
57       const char* buffer = nullptr;
58       NnApiImplementation()->ANeuralNetworks_getDevice(i, &device);
59       NnApiImplementation()->ANeuralNetworksDevice_getName(device, &buffer);
60       device_names.push_back(buffer);
61     }
62   }
63 
64   return device_names;
65 }
66 
GetStringDeviceNamesList()67 std::string GetStringDeviceNamesList() {
68   std::vector<const char*> device_names = GetDeviceNamesList();
69   return SimpleJoin(device_names, ",");
70 }
71 
72 }  // namespace nnapi
73 }  // namespace tflite
74