2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
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.
21 #include <bundle_internal.h>
26 #include "aul_error.h"
27 #include "aul_running_context.h"
31 struct aul_running_context_s {
38 static void __destroy_running_context(gpointer data)
40 struct aul_running_context_s *context = data;
42 free(context->inst_id);
43 free(context->comp_id);
44 free(context->app_id);
48 static struct aul_running_context_s *__create_running_context(bundle *b)
50 struct aul_running_context_s *context;
53 context = calloc(1, sizeof(struct aul_running_context_s));
59 val = bundle_get_val(b, AUL_K_APPID);
61 context->app_id = strdup(val);
63 val = bundle_get_val(b, AUL_K_COMPONENT_ID);
65 context->comp_id = strdup(val);
67 val = bundle_get_val(b, AUL_K_INSTANCE_ID);
69 context->inst_id = strdup(val);
71 val = bundle_get_val(b, AUL_K_PID);
73 context->pid = atoi(val);
78 static bundle *__create_bundle(const char *app_id,
90 bundle_add(b, AUL_K_APPID, app_id);
93 bundle_add(b, AUL_K_COMPONENT_ID, comp_id);
96 bundle_add(b, AUL_K_INSTANCE_ID, inst_id);
101 API int aul_running_context_create(const char *app_id,
104 aul_running_context_h *handle)
106 struct aul_running_context_s *context;
107 app_pkt_t *pkt = NULL;
112 if (!app_id || !handle) {
113 _E("Invalid parameter");
117 b = __create_bundle(app_id, comp_id, inst_id);
121 fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
122 APP_GET_RUNNING_CONTEXT,
126 _E("Failed to send the request. error(%d)", fd);
127 return aul_error_convert(fd);
130 ret = aul_sock_recv_reply_pkt(fd, &pkt);
131 if (ret < 0 || pkt == NULL) {
132 _E("Failed to get reply. error(%d)", ret);
133 return aul_error_convert(ret);
136 if (pkt->cmd != APP_GET_INFO_OK) {
137 _E("Failed to get running context. error(%d)", pkt->cmd);
138 ret = aul_error_convert(pkt->cmd);
143 if (!(pkt->opt & AUL_SOCK_BUNDLE)) {
144 _E("Invalid protocol");
149 b = bundle_decode(pkt->data, pkt->len);
152 _E("Failed to decode bundle data");
156 context = __create_running_context(b);
159 _E("Failed to create running context");
168 API int aul_running_context_destroy(aul_running_context_h handle)
171 _E("Invalid parameter");
175 __destroy_running_context(handle);
180 API int aul_running_context_get_app_id(aul_running_context_h handle,
183 if (!handle || !app_id) {
184 _E("Invalid parameter");
188 *app_id = handle->app_id;
193 API int aul_running_context_get_comp_id(aul_running_context_h handle,
194 const char **comp_id)
196 if (!handle || !comp_id) {
197 _E("Invalid parameter");
201 *comp_id = handle->comp_id;
206 API int aul_running_context_get_inst_id(aul_running_context_h handle,
207 const char **inst_id)
209 if (!handle || !inst_id) {
210 _E("Invalid parameter");
214 *inst_id = handle->inst_id;
219 API int aul_running_context_get_pid(aul_running_context_h handle,
222 if (!handle || !pid) {
223 _E("Invalid parameter");