move command handling functions to cmd func module
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-cmd-func.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <glib.h>
18 #include "ttd-log.h"
19 #include "ttd-cmd.h"
20 #include "ttd-config.h"
21 #include "ttd-worker-handle.h"
22
23 static int __config_write(ttd_cmd_data *c_data)
24 {
25         void *data = NULL;
26         unsigned int data_len = 0;
27         int ret = 0;
28
29         retv_if(!c_data, -1);
30
31         ret = ttd_cmd_get_data(c_data, &data, &data_len);
32         retv_if(ret < 0, -1);
33
34         switch (ttd_cmd_get_command(c_data))
35         {
36         case TTD_CMD_CONFIG_SET_SERVER_URL:
37                 ret = ttd_config_write_string("url", "server", (char *)data);
38                 if (ret < 0) {
39                         _E("Failed to set server url[%s]", (char *)data);
40                         return -1;
41                 }
42                 break;
43         case TTD_CMD_CONFIG_SET_APP_ID:
44                 ret = ttd_config_write_string("worker", "appid", (char *)data);
45                 if (ret < 0) {
46                         _E("Failed to set app id[%s]", (char *)data);
47                         return -1;
48                 }
49                 break;
50         default:
51                 _E("Strange action");
52                 return -1;
53                 break;
54         }
55
56         return 0;
57 }
58
59 static int __worker_launch(ttd_cmd_data *c_data)
60 {
61         int ret = 0;
62         ttd_cmd_type_e cmd_type = TTD_CMD_TYPE_UNKNOWN;
63
64         retv_if(!c_data, -1);
65
66         cmd_type = ttd_cmd_get_type(c_data);
67
68         if (cmd_type == TTD_CMD_TYPE_PACKAGE) {
69                 ret = ttd_worker_handle_pkgmgr(c_data);
70         } else if (cmd_type == TTD_CMD_TYPE_INFO) {
71                 ret = ttd_worker_handle_info(c_data);
72         } else {
73                 _E("not supported cmd type - %d", cmd_type);
74                 ret = -1;
75         }
76         return ret;
77 }
78
79 ttd_cmd_launch_func ttd_cmd_get_launch_func(ttd_cmd_type_e type)
80 {
81         ttd_cmd_launch_func func = NULL;
82
83         switch (type) {
84         case TTD_CMD_TYPE_POWER:
85                 break;
86         case TTD_CMD_TYPE_CONFIG:
87                 func = __config_write;
88                 break;
89         case TTD_CMD_TYPE_PACKAGE:
90         case TTD_CMD_TYPE_INFO:
91                 func = __worker_launch;
92                 break;
93         case TTD_CMD_TYPE_DIAGNOSIS:
94                 break;
95         case TTD_CMD_TYPE_LOCAL:
96                 break;
97         case TTD_CMD_TYPE_UNKNOWN:
98         case TTD_CMD_TYPE_MAX:
99         default:
100                 break;
101         }
102
103         return func;
104 }