2 * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "hs-appinfo.h"
20 #include "hs-clientmanager.h"
24 const char _keyName[] = "name";
25 const char _keyVersion[] = "version";
26 const char _keyInstall[] = "install";
27 const char _keyUninstall[] = "uninstall";
28 const char _keyOperation[] = "operation";
29 const char _keyRunnables[] = "runnables";
30 const char _keyStart[] = "start";
31 const char _keyApplistChanged[] = "application-list-changed";
33 HS_AppInfo* HS_AppInfo::me = nullptr;
36 * get application property function
39 * - key : retrieve keyword
45 std::string AppDetail::getProperty(std::string key) const
47 struct json_object *j_obj;
48 struct json_object *j_detail = json_tokener_parse(this->detail.c_str());
49 if(json_object_object_get_ex(j_detail, key.c_str(), &j_obj) == 0) {
50 AFB_WARNING("can't find key=%s.", key.c_str());
53 return std::string(json_object_get_string(j_obj));
57 * HS_AppInfo destruction function
66 HS_AppInfo::~HS_AppInfo()
79 * HS_AppInfo instance pointer
82 HS_AppInfo* HS_AppInfo::instance(void)
85 me = new HS_AppInfo();
91 * HS_AppInfo initialize function
94 * - api : the api serving the request
101 int HS_AppInfo::init(afb_api_t api)
103 afmmain = new HS_AfmMainProxy();
104 if(afmmain == nullptr) {
105 AFB_ERROR("new HS_AfmMainProxy failed");
109 struct json_object* j_runnable = nullptr;
112 if(afmmain->runnables(api, &j_runnable) == 0) {
113 createAppDetailList(j_runnable);
114 json_object_put(j_runnable);
119 if(retry == RETRY_CNT) {
120 AFB_ERROR("get runnables list failed");
121 json_object_put(j_runnable);
124 AFB_DEBUG("retry to get runnables list %d", retry);
125 usleep(100000); // 100ms
135 * - api : the api serving the request
136 * - event : event name
137 * - object : event json object
143 void HS_AppInfo::onEvent(afb_api_t api, const char *event, struct json_object *object)
145 auto ip = concerned_event_list.find(std::string(event));
146 if(ip != concerned_event_list.end()) {
147 AFB_INFO("[%s] event received.", event);
148 (this->*(ip->second))(api, object);
153 * create application detail list function
156 * - object : the detail of all applications
162 void HS_AppInfo::createAppDetailList(struct json_object *object)
164 AFB_DEBUG("applist:%s", json_object_to_json_string(object));
166 if(json_object_get_type(object) == json_type_array) {
167 int array_len = json_object_array_length(object);
168 for (int i = 0; i < array_len; ++i) {
169 struct json_object *obj = json_object_array_get_idx(object, i);
174 AFB_ERROR("Apps information input error.");
179 * update application detail function
182 * - object : the detail of all applications
188 void HS_AppInfo::updateAppDetailList(afb_api_t api, struct json_object *object)
190 AFB_DEBUG("update:%s", json_object_to_json_string(object));
191 if(json_object_get_type(object) != json_type_object) {
192 AFB_ERROR("input detail object error.");
196 struct json_object *obj_oper, *obj_data;
197 if(json_object_object_get_ex(object, _keyOperation, &obj_oper) == 0
198 || json_object_object_get_ex(object, _keyData, &obj_data) == 0) {
199 AFB_ERROR("can't find key=%s, %s.", _keyOperation, _keyData);
203 std::string id = json_object_get_string(obj_data);
204 std::string appid = id2appid(id);
205 if(isPeripheryApp(appid.c_str())) {
206 AFB_INFO( "install/uninstall application is periphery.");
210 std::string oper = json_object_get_string(obj_oper);
211 if(oper == _keyInstall) {
212 struct json_object* j_runnable = nullptr;
213 int ret = afmmain->runnables(api, &j_runnable);
215 struct json_object *j_found = retrieveRunnables(j_runnable, id);
216 if(j_found == nullptr) {
217 AFB_INFO( "installed application isn't runnables.");
218 json_object_put(j_runnable);
221 addAppDetail(j_found);
222 pushAppListChangedEvent(_keyInstall, j_found);
225 AFB_ERROR("get runnalbes failed.");
227 json_object_put(j_runnable);
229 else if(oper == _keyUninstall) {
230 std::string appid_checked = checkAppId(appid);
231 if(appid_checked.empty()) {
232 AFB_INFO("uninstalled application isn't in runnables list, appid=%s.", appid.c_str());
235 pushAppListChangedEvent(_keyUninstall, obj_data);
236 removeAppDetail(appid);
239 AFB_ERROR("operation error.");
244 * parse application detail function
247 * - object : [IN] the detail of application
248 * - info : [OUT] parsed application detail
251 * the appid of application liked "dashboard"
254 std::string HS_AppInfo::parseAppDetail(struct json_object *object, AppDetail &info) const
256 struct json_object *name, *id;
257 if(json_object_object_get_ex(object, _keyName, &name) == 0
258 || json_object_object_get_ex(object, _keyId, &id) == 0) {
259 AFB_ERROR("can't find key=%s, %s.", _keyName, _keyId);
260 return std::string();
262 std::string appid = id2appid(json_object_get_string(id));
263 bool periphery = isPeripheryApp(appid.c_str());
265 info = { json_object_get_string(name),
266 json_object_get_string(id),
267 json_object_to_json_string(object),
274 * add application detail to list function
277 * - object : application detail
283 void HS_AppInfo::addAppDetail(struct json_object *object)
286 std::string appid = parseAppDetail(object, info);
288 AFB_ERROR("application id error");
292 std::lock_guard<std::mutex> lock(this->mtx);
293 appid2name[appid] = info.name;
294 name2appid[info.name] = appid;
295 app_detail_list[appid] = std::move(info);
299 * remove application detail from list function
302 * - appid : application id
308 void HS_AppInfo::removeAppDetail(std::string appid)
310 std::lock_guard<std::mutex> lock(this->mtx);
311 auto it = app_detail_list.find(appid);
312 if(it != app_detail_list.end()) {
313 appid2name.erase(appid);
314 name2appid.erase(it->second.name);
315 app_detail_list.erase(it);
318 AFB_WARNING("erase application(%s) wasn't in applist.", appid.c_str());
323 * push app_list_changed event function
326 * - oper: install/uninstall
327 * - object: event data
333 void HS_AppInfo::pushAppListChangedEvent(const char *oper, struct json_object *object)
335 struct json_object *push_obj = json_object_new_object();
336 json_object_object_add(push_obj, _keyOperation, json_object_new_string(oper));
337 json_object_object_add(push_obj, _keyData, object);
339 HS_ClientManager::instance()->pushEvent(_keyApplistChanged, push_obj);
343 * retrieve runnables function
346 * - obj_runnables: runnables array
347 * - id: application id
350 * found application detail
353 struct json_object* HS_AppInfo::retrieveRunnables(struct json_object *obj_runnables, std::string id)
355 struct json_object *j_found = nullptr;
356 if(json_object_get_type(obj_runnables) == json_type_array) {
357 int array_len = json_object_array_length(obj_runnables);
358 for (int i = 0; i < array_len; ++i) {
359 struct json_object *obj = json_object_array_get_idx(obj_runnables, i);
360 struct json_object *j_id;
361 if(json_object_object_get_ex(obj, _keyId, &j_id) == 0) {
362 AFB_WARNING("can't find id.");
365 if(id == json_object_get_string(j_id)) {
372 AFB_ERROR("Apps information input error.");
378 * convert id to appid function
381 * - id : the id of application liked "dashboard@0.1"
384 * the appid of application liked "dashboard"
387 std::string HS_AppInfo::id2appid(const std::string &id) const
390 std::size_t pos = id.find("@");
391 if(pos != std::string::npos) {
392 appid = id.substr(0,pos);
395 AFB_WARNING("input id error.");
404 * - object : runnables list,json array
410 void HS_AppInfo::getRunnables(struct json_object **object)
412 if(json_object_get_type(*object) != json_type_array) {
413 AFB_ERROR("json type error.");
417 std::lock_guard<std::mutex> lock(this->mtx);
418 for(auto it : app_detail_list) {
419 if(!it.second.periphery)
420 json_object_array_add(*object, json_tokener_parse(it.second.detail.c_str()));
425 * check appid function
428 * - appid : appid liked "dashboard"
431 * success : the correct appid
432 * fail : empty string
435 std::string HS_AppInfo::checkAppId(const std::string &appid)
437 std::lock_guard<std::mutex> lock(this->mtx);
438 auto it_appid = appid2name.find(appid);
439 if(it_appid != appid2name.end())
440 return it_appid->first;
442 auto it_name = name2appid.find(appid);
443 if(it_name != name2appid.end())
444 return it_name->second;
446 return std::string();
450 * check if application is a runnable periphery application function
453 * - appid : appid liked "launcher"
457 * false : not periphery
460 bool HS_AppInfo::isPeripheryApp(const char *appid) const
463 for(auto m : periphery_app_list) {
464 if(strcasecmp(appid, m) == 0) {
473 * get application specific property
476 * - appid : appid liked "launcher"
477 * - key : the keyword
480 * application property
483 std::string HS_AppInfo::getAppProperty(const std::string appid, std::string key) const
485 std::string value = "";
486 auto it = app_detail_list.find(appid);
487 if(it != app_detail_list.end()) {
488 value = it->second.getProperty(key);