[ML] Added binary communication in getTensorRawData()
[platform/core/api/webapi-plugins.git] / doc / src / assets / webapi-plugins-devel-test / src / tool / desc_gentool.cc
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <map>
5 #include <functional>
6
7 #include <dlfcn.h>
8 #include <dirent.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <errno.h>
12
13 #include <common/extension.h>
14
15 static std::string prefix_ = "libwebapis";
16 static std::string postfix_ = ".so";
17 static std::vector<std::string> apinamespaces = {"tizen", "xwalk", "webapis"};
18
19 typedef common::Extension *(*CreateExtensionFunc)(void);
20
21 struct module_description {
22   std::string name;
23   std::string lib;
24   std::vector<std::string> entries;
25 };
26
27
28 static XW_Extension ext = 0;
29 static std::map<XW_Extension, module_description> descriptions;
30
31 #ifndef JSON_MINIFY
32   #define PRINT_TAB() std::cout << "\t"
33 #else
34   #define PRINT_TAB()
35 #endif
36
37 void print_json() {
38   std::cout << "[" << std::endl;
39   for (const auto& kv : descriptions) {
40     const module_description &desc = kv.second;
41
42     std::string::size_type n = desc.name.find('.');
43     std::string ns =
44         n == std::string::npos ? desc.name : desc.name.substr(0, n);
45
46     if (std::find(apinamespaces.begin(), apinamespaces.end(), ns) ==
47         apinamespaces.end()) {
48       continue;
49     }
50
51     PRINT_TAB();
52     std::cout << "{" << std::endl;
53     PRINT_TAB();
54     PRINT_TAB();
55     std::cout << "\"name\":\"" << desc.name << "\"," << std::endl;
56     PRINT_TAB();
57     PRINT_TAB();
58     std::cout << "\"lib\":\"" << desc.lib << "\"," << std::endl;
59     PRINT_TAB();
60     PRINT_TAB();
61     std::cout << "\"entry_points\": [";
62     for (std::vector<std::string>::size_type i=0; i<desc.entries.size(); i++) {
63       if (i != 0) {
64         std::cout << ",";
65       }
66       std::cout << "\"" << desc.entries[i] << "\"";
67     }
68     std::cout << "]" << std::endl;
69     PRINT_TAB();
70     std::cout << "}";
71     if (kv.first != ext) {
72       std::cout << ",";
73     }
74     std::cout << std::endl;
75   }
76   std::cout << "]" << std::endl;
77 }
78
79 const void* get_interface(const char* name) {
80   if (!strcmp(name, XW_CORE_INTERFACE_1)) {
81     static const XW_CoreInterface coreInterface1 = {
82       [](XW_Extension extension, const char* name) {
83         module_description *desc = &descriptions[extension];
84         desc->name = name;
85       },
86       [](XW_Extension extension, const char* api) {},
87       [](XW_Extension extension, XW_CreatedInstanceCallback created,
88          XW_DestroyedInstanceCallback destroyed) {},
89       [](XW_Extension extension, XW_ShutdownCallback shutdown_callback) {},
90       [](XW_Instance instance, void* data) {},
91       [](XW_Instance instance) -> void* { return nullptr; }
92       };
93     return &coreInterface1;
94   }
95
96   if (!strcmp(name, XW_INTERNAL_ENTRY_POINTS_INTERFACE_1)) {
97     static const XW_Internal_EntryPointsInterface entryPointsInterface1 = {
98       [](XW_Extension extension, const char** entries) {
99         module_description *desc = &descriptions[extension];
100         for (int i=0; entries[i]; i++) {
101           desc->entries.push_back(std::string(entries[i]));
102         }
103       }
104     };
105     return &entryPointsInterface1;
106   }
107
108   if (!strcmp(name, XW_MESSAGING_INTERFACE_2)) {
109     static const XW_MessagingInterface_1 messagingInterface1 = {
110       [](XW_Extension extension, XW_HandleMessageCallback handle_message) {
111       },
112       [](XW_Instance instance, const char* message) {
113       }
114     };
115     return &messagingInterface1;
116   }
117
118   if (!strcmp(name, XW_INTERNAL_SYNC_MESSAGING_INTERFACE_1)) {
119     static const XW_Internal_SyncMessagingInterface syncMessagingInterface1 = {
120         [](XW_Extension extension,
121            XW_HandleSyncMessageCallback handle_sync_msg) {},
122         [](XW_Instance instance, const char* reply) {},
123         [](XW_Instance instance, const char* reply, int size) {}};
124     return &syncMessagingInterface1;
125   }
126
127   if (!strcmp(name, XW_INTERNAL_RUNTIME_INTERFACE_1)) {
128     static const XW_Internal_RuntimeInterface_1 runtimeInterface1 = {
129       [](XW_Extension extension, const char* key, char* value, size_t vlen) {
130       }
131     };
132     return &runtimeInterface1;
133   }
134
135   if (!strcmp(name, XW_INTERNAL_PERMISSIONS_INTERFACE_1)) {
136     static const XW_Internal_PermissionsInterface_1 permissionsInterface1 = {
137       [](XW_Extension extension, const char* api_name) -> int {
138         return XW_ERROR;
139       },
140       [](XW_Extension extension, const char* perm_table) -> int {
141         return XW_ERROR;
142       }
143     };
144     return &permissionsInterface1;
145   }
146
147   return NULL;
148 }
149
150 int main(int argc, char* argv[]) {
151   if (argc < 3) {
152     std::cerr << "Need tizen crosswalk path" << std::endl;
153     return -1;
154   }
155   std::string lib_path = argv[1];
156   if (lib_path.empty()) {
157     std::cerr << "Invalid libpath for tec." << std::endl;
158     return -1;
159   }
160
161   std::string tec_path = argv[2];
162   if (tec_path.empty()) {
163     std::cerr << "Invalid tizen crosswalk path" << std::endl;
164     return -1;
165   }
166
167   struct dirent** namelist;
168   int num_entries = scandir(tec_path.c_str(), &namelist, NULL, alphasort);
169   if( num_entries >= 0 ) {
170     for( int i = 0; i < num_entries; ++i ) {
171       std::string fname = namelist[i]->d_name;
172
173       if (fname.size() >= prefix_.size() + postfix_.size() &&
174           !fname.compare(0, prefix_.size(), prefix_) &&
175           !fname.compare(fname.size() - postfix_.size(), postfix_.size(),
176                         postfix_)) {
177         std::string so_path = tec_path + "/" + fname;
178         void *handle = dlopen(so_path.c_str(), RTLD_LAZY);
179         if (handle == NULL) {
180           std::cerr << "cannot open " << so_path << std::endl;
181           char* error = dlerror();
182           std::cerr << "Error >>" << ((error == NULL) ? "NULL" : error) << std::endl;
183           return -1;
184         }
185
186         XW_Initialize_Func initialize = reinterpret_cast<XW_Initialize_Func>(
187                 dlsym(handle, "XW_Initialize"));
188
189         if (!initialize) {
190           std::cerr << "Can not loading extension " << fname << std::endl;
191         } else {
192           ext++;
193           descriptions[ext] = module_description();
194           descriptions[ext].lib = lib_path + "/" + fname;
195           int ret = initialize(ext, get_interface);
196           if (ret != XW_OK) {
197             std::cerr << "Error loading extension " << fname << std::endl;
198           }
199         }
200
201         // some Shared libraries have static finalizer.
202         // __attribute__((destructor)) this gcc extension makes finalizer.
203         // if close it, it can makes segfault.
204         // True, It's shared object's problem. but we can't fix it.
205         // so don't close it in only this tool. just finish process.
206         //
207         // dlclose(handle);
208       }
209       free(namelist[i]);
210     }
211     free(namelist);
212     print_json();
213   } else {
214     perror("scandir");
215     if( errno == ENOENT )
216       std::cerr << "path not exist : " << tec_path << std::endl;
217     return -1;
218   }
219
220   // it would be need for ignore loaded libraries destructor
221   _exit(0);
222 }