replace value to macro for HTTP OK
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-cmd.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-type.h"
20
21 struct _ttd_cmd_data_s {
22         ttd_cmd_state_e state;
23         ttd_cmd_type_e type;
24         union {
25                 ttd_cmd_power_e pwr_cmd;
26                 ttd_cmd_config_e cfg_cmd;
27                 ttd_cmd_package_e pkg_cmd;
28                 ttd_cmd_diagnosis_e diag_cmd;
29                 ttd_cmd_info_e info_cmd;
30                 ttd_cmd_local_e local_cmd;
31         } command;
32         char *cmd_id;
33         void *data;
34         unsigned int data_len;
35         cmd_data_free_func data_free_func;
36 };
37
38 ttd_cmd_data *ttd_cmd_new(const char *cmd_id)
39 {
40         ttd_cmd_data *data = NULL;
41         char *id = NULL;
42
43         retv_if(!cmd_id, NULL);
44
45         id = g_strdup(cmd_id);
46         retv_if(!id, NULL);
47
48         data = g_try_malloc0(sizeof(struct _ttd_cmd_data_s));
49         if (!data) {
50                 _E("failed to malloc");
51                 g_free(id);
52                 return NULL;
53         }
54
55         data->state = TTD_CMD_STATE_UNKNOWN;
56         data->type = TTD_CMD_TYPE_UNKNOWN;
57         data->cmd_id = id;
58
59         return data;
60 }
61
62 int ttd_cmd_set_state(ttd_cmd_data *cmd, ttd_cmd_state_e state)
63 {
64         retv_if(!cmd, -1);
65         retvm_if(state == TTD_CMD_STATE_UNKNOWN, -1, "invaild state");
66
67         cmd->state = state;
68
69         return 0;
70 }
71
72 int ttd_cmd_set_type(ttd_cmd_data *cmd, ttd_cmd_type_e type)
73 {
74         retv_if(!cmd, -1);
75         retvm_if((type == TTD_CMD_TYPE_UNKNOWN) | (type == TTD_CMD_TYPE_MAX),
76                 -1, "invaild type");
77
78         cmd->type = type;
79
80         return 0;
81 }
82
83 int ttd_cmd_set_command(ttd_cmd_data *cmd, int command)
84 {
85         retv_if(!cmd, -1);
86
87         switch (cmd->type) {
88         case TTD_CMD_TYPE_POWER:
89                 cmd->command.pwr_cmd = command;
90                 break;
91         case TTD_CMD_TYPE_CONFIG:
92                 cmd->command.cfg_cmd = command;
93                 break;
94         case TTD_CMD_TYPE_PACKAGE:
95                 cmd->command.pkg_cmd = command;
96                 break;
97         case TTD_CMD_TYPE_DIAGNOSIS:
98                 cmd->command.diag_cmd = command;
99                 break;
100         case TTD_CMD_TYPE_INFO:
101                 cmd->command.info_cmd = command;
102                 break;
103         case TTD_CMD_TYPE_LOCAL:
104                 cmd->command.local_cmd = command;
105                 break;
106         case TTD_CMD_TYPE_MAX:
107         default:
108                 _E("Unknown cmd type : %d", cmd->type);
109                 return -1;
110                 break;
111         }
112         return 0;
113 }
114
115 int ttd_cmd_set_data(ttd_cmd_data *cmd,
116         void *data, unsigned int length, cmd_data_free_func free_fn)
117 {
118         retv_if(!cmd, -1);
119
120         cmd->data = data;
121         cmd->data_len = length;
122         cmd->data_free_func = free_fn;
123
124         return 0;
125 }
126
127 void ttd_cmd_free(ttd_cmd_data *cmd)
128 {
129         if (!cmd)
130                 return;
131
132         _D("free cmd type - %d", cmd->type);
133         if (cmd->cmd_id) {
134                 _D("free cmd id - %s", cmd->cmd_id);
135                 g_free(cmd->cmd_id);
136         }
137
138         if (cmd->data_free_func)
139                 cmd->data_free_func(cmd->data);
140         else
141                 g_free(cmd->data);
142
143         g_free(cmd);
144 }
145
146 ttd_cmd_state_e ttd_cmd_get_state(ttd_cmd_data *cmd)
147 {
148         retv_if(!cmd, TTD_CMD_STATE_UNKNOWN);
149
150         return cmd->state;
151 }
152
153 ttd_cmd_type_e ttd_cmd_get_type(ttd_cmd_data *cmd)
154 {
155         retv_if(!cmd, TTD_CMD_TYPE_UNKNOWN);
156
157         return cmd->type;
158 }
159
160 const char *ttd_cmd_get_id(ttd_cmd_data *cmd)
161 {
162         retv_if(!cmd, NULL);
163
164         return cmd->cmd_id;
165 }
166
167 int ttd_cmd_get_command(ttd_cmd_data *cmd)
168 {
169         int command = -1;
170         retv_if(!cmd, -1);
171
172         switch (cmd->type) {
173         case TTD_CMD_TYPE_POWER:
174                 command = cmd->command.pwr_cmd;
175                 break;
176         case TTD_CMD_TYPE_CONFIG:
177                 command = cmd->command.cfg_cmd;
178                 break;
179         case TTD_CMD_TYPE_PACKAGE:
180                 command = cmd->command.pkg_cmd;
181                 break;
182         case TTD_CMD_TYPE_DIAGNOSIS:
183                 command = cmd->command.diag_cmd;
184                 break;
185         case TTD_CMD_TYPE_INFO:
186                 command = cmd->command.info_cmd;
187                 break;
188         case TTD_CMD_TYPE_LOCAL:
189                 command = cmd->command.local_cmd;
190                 break;
191         case TTD_CMD_TYPE_MAX:
192         default:
193                 _E("Unknown cmd type : %d", cmd->type);
194                 return -1;
195                 break;
196         }
197         return command;
198 }
199
200 int ttd_cmd_get_data(ttd_cmd_data *cmd, void **data, unsigned int *length)
201 {
202         retv_if(!cmd, -1);
203
204         *data = cmd->data;
205         *length = cmd->data_len;
206
207         return 0;
208 }