Add unit tests for component status API
[platform/core/appfw/aul-1.git] / src / aul_comp_status.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21
22 #include <bundle.h>
23 #include <bundle_internal.h>
24
25 #include "aul_comp_status.h"
26 #include "aul_util.h"
27 #include "aul_sock.h"
28 #include "aul_api.h"
29 #include "aul_error.h"
30 #include "aul.h"
31
32 API int aul_comp_status_update(const char *instance_id, int status)
33 {
34         char buf[32];
35         bundle *b;
36         int r;
37
38         if (!instance_id || status > STATUS_TERMINATE) {
39                 _E("Invalid parameter");
40                 return AUL_R_EINVAL;
41         }
42
43         b = bundle_create();
44         if (!b) {
45                 _E("Failed to create bundle");
46                 return AUL_R_ERROR;
47         }
48
49         bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
50         snprintf(buf, sizeof(buf), "%d", status);
51         bundle_add(b, AUL_K_STATUS, buf);
52
53         r = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
54                         COMP_STATUS_UPDATE, b, AUL_SOCK_NOREPLY);
55         if (r < 0) {
56                 _E("Failed to send the update request. error(%d)", r);
57                 bundle_free(b);
58                 return r;
59         }
60         bundle_free(b);
61
62         return AUL_R_OK;
63 }
64
65 static int __send_request(int cmd, int opt, const char *instance_id)
66 {
67         bundle *b;
68         int ret;
69
70         b = bundle_create();
71         if (!b) {
72                 _E("Failed to create bundle");
73                 return AUL_R_ENOMEM;
74         }
75
76         ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
77         if (ret != BUNDLE_ERROR_NONE) {
78                 bundle_free(b);
79                 if (ret == BUNDLE_ERROR_OUT_OF_MEMORY)
80                         return AUL_R_ENOMEM;
81                 else if (ret == BUNDLE_ERROR_INVALID_PARAMETER)
82                         return AUL_R_EINVAL;
83
84                 return AUL_R_ERROR;
85         }
86
87         ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), cmd, b, opt);
88         bundle_free(b);
89         if (ret < 0) {
90                 _E("Failed to send command(%d). error(%d)", cmd, ret);
91                 return aul_error_convert(ret);
92         }
93
94         return ret;
95 }
96
97 API int aul_comp_notify_start(const char *instance_id)
98 {
99         int ret;
100
101         if (!instance_id) {
102                 _E("Invalid parameter");
103                 return AUL_R_EINVAL;
104         }
105
106         ret = __send_request(COMP_NOTIFY_START, AUL_SOCK_NOREPLY,
107                         instance_id);
108         if (ret < 0)
109                 return ret;
110
111         return AUL_R_OK;
112 }
113
114 API int aul_comp_notify_exit(const char *instance_id)
115 {
116         int ret;
117
118         if (!instance_id) {
119                 _E("Invalid parameter");
120                 return AUL_R_EINVAL;
121         }
122
123         ret = __send_request(COMP_NOTIFY_EXIT, AUL_SOCK_NOREPLY,
124                         instance_id);
125         if (ret < 0)
126                 return ret;
127
128         return AUL_R_OK;
129 }
130
131 API int aul_comp_resume(const char *instance_id)
132 {
133         int ret;
134
135         if (!instance_id) {
136                 _E("Invalid parameter");
137                 return AUL_R_EINVAL;
138         }
139
140         ret = __send_request(COMP_CONTEXT_RESUME, AUL_SOCK_NONE,
141                         instance_id);
142         if (ret < 0)
143                 return ret;
144
145         return AUL_R_OK;
146 }
147
148 API int aul_comp_terminate(const char *instance_id)
149 {
150         int ret;
151
152         if (!instance_id) {
153                 _E("Invalid parameter");
154                 return AUL_R_EINVAL;
155         }
156
157         ret = __send_request(COMP_CONTEXT_TERMINATE, AUL_SOCK_NONE,
158                         instance_id);
159         if (ret < 0)
160                 return ret;
161
162         return AUL_R_OK;
163 }
164
165 API int aul_comp_is_running(const char *instance_id, bool *running)
166 {
167         int ret;
168
169         if (!instance_id || !running) {
170                 _E("Invalid parameter");
171                 return AUL_R_EINVAL;
172         }
173
174         ret = __send_request(COMP_CONTEXT_IS_RUNNING, AUL_SOCK_NONE,
175                         instance_id);
176         if (ret < 0)
177                 return ret;
178
179         *running = (bool)ret;
180
181         return AUL_R_OK;
182 }