2 * Copyright (c) 2013-2015 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.
19 #include <dbus/dbus.h>
20 #include <sys/types.h>
23 #include "minicontrol-error.h"
24 #include "minicontrol-type.h"
25 #include "minicontrol-internal.h"
26 #include "minicontrol-monitor.h"
27 #include "minicontrol-log.h"
28 #include "minicontrol-handler.h"
30 #define BUNDLE_KEY_PID "__KEY_PID__"
31 #define BUNDLE_KEY_STATE "__KEY_STATE__"
32 #define BUNDLE_KEY_SERVICE_NAME "__KEY_SERVICE_NAME__"
33 #define BUNDLE_KEY_CATEGORY "__KEY_CATEGORY__"
34 #define BUNDLE_KEY_OPERATION "__KEY_OPERATION__"
35 #define BUNDLE_KEY_PRIORITY "__KEY_PRIORITY__"
36 #define BUNDLE_KEY_TIMESTAMP "__KEY_TIMESTAMP__"
41 static int _bundle_set_byte(bundle *b, const char *key, void *value, int length)
45 void *ret_byte = NULL;
47 ret = bundle_get_byte(b, key, &ret_byte, &ret_size);
48 if (ret == 0 && ret_byte != NULL) {
49 if(bundle_del(b, key) != 0) {
50 ERR("Failed to del a previous value(key):%s", key);
51 return MINICONTROL_ERROR_INVALID_PARAMETER;
56 return MINICONTROL_ERROR_INVALID_PARAMETER;
59 if (bundle_add_byte(b, key, value, length) != 0) {
60 ERR("Failed to set a new value(key):%s", key);
61 return MINICONTROL_ERROR_BUNDLE;
64 return MINICONTROL_ERROR_NONE;
67 static int _bundle_set_str(bundle *b, const char *key, const char *value)
69 const char *val = NULL;
71 val = bundle_get_val(b, key);
73 if(bundle_del(b, key) != 0) {
74 ERR("Failed to del a previous value(key):%s", key);
75 return MINICONTROL_ERROR_BUNDLE;
80 return MINICONTROL_ERROR_INVALID_PARAMETER;
83 if(bundle_add(b, key, value) != 0){
84 ERR("Failed to set a new value(key):%s", key);
85 return MINICONTROL_ERROR_BUNDLE;
88 return MINICONTROL_ERROR_NONE;
94 static int _handler_validation_check(minicontrol_h handler) {
95 if (handler != NULL) {
96 if (handler->data != NULL) {
97 return MINICONTROL_ERROR_NONE;
100 return MINICONTROL_ERROR_INVALID_PARAMETER;
103 static int _handler_extradata_key_validation_check(const char *key) {
105 if (strcmp(key, BUNDLE_KEY_PID) == 0) {
106 return MINICONTROL_ERROR_INVALID_PARAMETER;
108 } else if (strcmp(key, BUNDLE_KEY_SERVICE_NAME) == 0) {
109 return MINICONTROL_ERROR_INVALID_PARAMETER;
111 } else if (strcmp(key, BUNDLE_KEY_SERVICE_NAME) == 0) {
112 return MINICONTROL_ERROR_INVALID_PARAMETER;
114 } else if (strcmp(key, BUNDLE_KEY_OPERATION) == 0) {
115 return MINICONTROL_ERROR_INVALID_PARAMETER;
117 } else if (strcmp(key, BUNDLE_KEY_PRIORITY) == 0) {
118 return MINICONTROL_ERROR_INVALID_PARAMETER;
120 } else if (strcmp(key, BUNDLE_KEY_TIMESTAMP) == 0) {
121 return MINICONTROL_ERROR_INVALID_PARAMETER;
124 return MINICONTROL_ERROR_INVALID_PARAMETER;
127 return MINICONTROL_ERROR_INVALID_PARAMETER;
131 * handler bundle utility
133 static minicontrol_error_e __set_str_to_handler(minicontrol_h handler, const char *key, const char *value)
135 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE
137 return MINICONTROL_ERROR_INVALID_PARAMETER;
141 return _bundle_set_str(handler->data, key, value);
143 bundle_del(handler->data, key);
146 return MINICONTROL_ERROR_NONE;
149 static minicontrol_error_e __get_str_from_handler(minicontrol_h handler, const char *key, char **value)
151 const char *data_value;
153 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE
156 return MINICONTROL_ERROR_INVALID_PARAMETER;;
159 data_value = bundle_get_val(handler->data, key);
160 if (data_value == NULL) {
161 if (errno == ENOTSUP) {
162 return MINICONTROL_ERROR_BUNDLE;
164 return MINICONTROL_ERROR_NO_DATA;
168 *value = strdup(data_value);
170 return MINICONTROL_ERROR_NONE;
176 minicontrol_error_e _minictrl_handler_create(minicontrol_h *handler)
178 minicontrol_h handler_new = NULL;
179 if (handler == NULL) {
180 return MINICONTROL_ERROR_INVALID_PARAMETER;
183 handler_new = calloc(1, sizeof(struct _minicontrol_h));
184 if (handler_new == NULL) {
185 ERR("Failed to create a handler");
186 return MINICONTROL_ERROR_OUT_OF_MEMORY;
189 handler_new->data = bundle_create();
190 if (handler_new->data == NULL)
193 ERR("Failed to create a bundle");
194 return MINICONTROL_ERROR_OUT_OF_MEMORY;
197 if (_minictrl_handler_set_pid(handler_new) != MINICONTROL_ERROR_NONE) {
198 ERR("Failed to set PID to handler");
200 if (_minictrl_handler_set_state(handler_new, 0) != MINICONTROL_ERROR_NONE) {
201 ERR("Failed to set state to handler");
203 if (_minictrl_handler_set_category(handler_new, MINICONTROL_HDL_CATEGORY_UNKNOWN) != MINICONTROL_ERROR_NONE) {
204 ERR("Failed to set category to handler");
207 *handler = handler_new;
209 return MINICONTROL_ERROR_NONE;
212 minicontrol_error_e _minictrl_handler_destroy(minicontrol_h handler)
214 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE) {
215 return MINICONTROL_ERROR_INVALID_PARAMETER;
218 bundle_free(handler->data);
219 handler->data = NULL;
222 return MINICONTROL_ERROR_NONE;
225 minicontrol_error_e _minictrl_handler_clone(minicontrol_h handler, minicontrol_h *handler_new)
229 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE || handler_new == NULL) {
230 return MINICONTROL_ERROR_INVALID_PARAMETER;
233 if ((ret = _minictrl_handler_create(handler_new)) == MINICONTROL_ERROR_NONE) {
234 (*handler_new)->data = bundle_dup(handler->data);
235 if ((*handler_new)->data == NULL) {
236 _minictrl_handler_destroy(*handler_new);
237 return MINICONTROL_ERROR_OUT_OF_MEMORY;
243 return MINICONTROL_ERROR_NONE;
246 minicontrol_error_e _minictrl_handler_check_validation(minicontrol_h handler)
248 return _handler_validation_check(handler);
251 minicontrol_error_e _minictrl_handler_set_service_name(minicontrol_h handler, const char *name)
253 return __set_str_to_handler(handler, BUNDLE_KEY_SERVICE_NAME, name);
256 minicontrol_error_e _minictrl_handler_get_service_name(minicontrol_h handler, char **name)
258 return __get_str_from_handler(handler, BUNDLE_KEY_SERVICE_NAME, name);
261 minicontrol_error_e _minictrl_handler_set_category(minicontrol_h handler, const char *category)
263 return __set_str_to_handler(handler, BUNDLE_KEY_CATEGORY, category);
266 minicontrol_error_e _minictrl_handler_get_category(minicontrol_h handler, char **category)
268 return __get_str_from_handler(handler, BUNDLE_KEY_CATEGORY, category);
271 minicontrol_error_e _minictrl_handler_set_operation(minicontrol_h handler, const char *operation)
273 return __set_str_to_handler(handler, BUNDLE_KEY_OPERATION, operation);
276 minicontrol_error_e _minictrl_handler_get_operation(minicontrol_h handler, char **operation)
278 return __get_str_from_handler(handler, BUNDLE_KEY_OPERATION, operation);
281 minicontrol_error_e _minictrl_handler_set_priority(minicontrol_h handler, const char *priority)
283 return __set_str_to_handler(handler, BUNDLE_KEY_PRIORITY, priority);
286 minicontrol_error_e _minictrl_handler_get_priority(minicontrol_h handler, char **priority)
288 return __get_str_from_handler(handler, BUNDLE_KEY_PRIORITY, priority);
291 minicontrol_error_e _minictrl_handler_set_timestamp(minicontrol_h handler, time_t timestamp)
293 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE) {
294 return MINICONTROL_ERROR_INVALID_PARAMETER;
298 return _bundle_set_byte(handler->data, BUNDLE_KEY_TIMESTAMP, (void *)×tamp, sizeof(time_t));
300 bundle_del(handler->data, BUNDLE_KEY_TIMESTAMP);
303 return MINICONTROL_ERROR_NONE;
306 minicontrol_error_e _minictrl_handler_get_timestamp(minicontrol_h handler, time_t *timestamp)
310 void *ret_value = NULL;
312 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE || timestamp == NULL) {
313 return MINICONTROL_ERROR_INVALID_PARAMETER;
316 ret = bundle_get_byte(handler->data, BUNDLE_KEY_TIMESTAMP, &ret_value, &ret_size);
317 if (ret == 0 && ret_value != NULL && ret_size == sizeof(time_t)) {
318 *timestamp = *((time_t *)ret_value);
323 return MINICONTROL_ERROR_NONE;
326 minicontrol_error_e _minictrl_handler_set_pid(minicontrol_h handler)
329 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE) {
330 return MINICONTROL_ERROR_INVALID_PARAMETER;
335 return _bundle_set_byte(handler->data, BUNDLE_KEY_PID, (void *)&pid, sizeof(int));
337 bundle_del(handler->data, BUNDLE_KEY_PID);
340 return MINICONTROL_ERROR_NONE;
343 minicontrol_error_e _minictrl_handler_get_pid(minicontrol_h handler, int *pid)
347 void *ret_value = NULL;
349 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE || pid == NULL) {
350 return MINICONTROL_ERROR_INVALID_PARAMETER;
353 ret = bundle_get_byte(handler->data, BUNDLE_KEY_PID, &ret_value, &ret_size);
354 if (ret == 0 && ret_value != NULL && ret_size == sizeof(int)) {
355 *pid = *((int *)ret_value);
360 return MINICONTROL_ERROR_NONE;
363 minicontrol_error_e _minictrl_handler_set_state(minicontrol_h handler, int state)
365 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE) {
366 return MINICONTROL_ERROR_INVALID_PARAMETER;
370 return _bundle_set_byte(handler->data, BUNDLE_KEY_STATE, (void *)&state, sizeof(int));
372 bundle_del(handler->data, BUNDLE_KEY_STATE);
375 return MINICONTROL_ERROR_NONE;
378 minicontrol_error_e _minictrl_handler_get_state(minicontrol_h handler, int *state)
382 void *ret_value = NULL;
384 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE || state == NULL) {
385 return MINICONTROL_ERROR_INVALID_PARAMETER;
388 ret = bundle_get_byte(handler->data, BUNDLE_KEY_STATE, &ret_value, &ret_size);
389 if (ret == 0 && ret_value != NULL && ret_size == sizeof(int)) {
390 *state = *((int *)ret_value);
395 return MINICONTROL_ERROR_NONE;
398 minicontrol_error_e _minictrl_handler_add_extradata(minicontrol_h handler, const char *key, const char *value)
400 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE
401 || _handler_extradata_key_validation_check(key) != MINICONTROL_ERROR_NONE
403 return MINICONTROL_ERROR_INVALID_PARAMETER;;
406 return _bundle_set_str(handler->data, key, value);
409 minicontrol_error_e _minictrl_handler_remove_extradata(minicontrol_h handler, const char *key)
411 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE
412 || _handler_extradata_key_validation_check(key) != MINICONTROL_ERROR_NONE) {
413 return MINICONTROL_ERROR_INVALID_PARAMETER;;
416 if (bundle_del(handler->data, key)) {
417 return MINICONTROL_ERROR_NO_DATA;
420 return MINICONTROL_ERROR_NONE;
423 minicontrol_error_e _minictrl_handler_get_extradata(minicontrol_h handler, const char *key, char **value)
425 const char *data_value;
427 if (_handler_validation_check(handler) != MINICONTROL_ERROR_NONE
428 || _handler_extradata_key_validation_check(key) != MINICONTROL_ERROR_NONE
430 return MINICONTROL_ERROR_INVALID_PARAMETER;;
433 data_value = bundle_get_val(handler->data, key);
434 if (data_value == NULL) {
435 if (errno == ENOTSUP) {
436 return MINICONTROL_ERROR_BUNDLE;
438 return MINICONTROL_ERROR_NO_DATA;
442 *value = strdup(data_value);
444 return MINICONTROL_ERROR_NONE;
447 minicontrol_error_e _minictrl_handler_get_raw_data(minicontrol_h handler, char **raw_data, int *length)
449 if (handler == NULL || raw_data == NULL || length == NULL) {
450 return MINICONTROL_ERROR_INVALID_PARAMETER;
453 if (handler->data != NULL) {
454 bundle_encode(handler->data,
455 (bundle_raw **) raw_data, length);
457 return MINICONTROL_ERROR_INVALID_PARAMETER;
460 return MINICONTROL_ERROR_NONE;
463 static void iterate_bundleforeach(const char *key,const int type, bundle_keyval_t *kv, void *data)
467 case BUNDLE_TYPE_STR:
470 char *basic_val = NULL;
472 if (basic_val != NULL) {
473 bundle_keyval_get_basic_val(kv,(void *)&basic_val,&size);
474 ERR("Key: %s ---- Val: %s\n",key,basic_val);
478 case BUNDLE_TYPE_BYTE:
481 char *basic_val = NULL;
483 if (basic_val != NULL) {
484 bundle_keyval_get_basic_val(kv,(void *)&basic_val,&size);
485 ERR("Key: %s ---- Val: %d\n",key, *((int *)basic_val));
491 DBG("\n no match found");
492 DBG("Key: %s is of type %d\n",key, type);
498 minicontrol_error_e _minictrl_handler_get_handler_from_raw_data(minicontrol_h *handler, char *raw_data, int length)
500 if (_minictrl_handler_create(handler) == MINICONTROL_ERROR_NONE) {
501 (*handler)->data = bundle_decode((bundle_raw *)raw_data, length);
502 bundle_foreach((*handler)->data,(void *)iterate_bundleforeach,NULL);
504 return MINICONTROL_ERROR_INVALID_PARAMETER;
507 return MINICONTROL_ERROR_NONE;