adds command id to command data
[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_type_e type;
23         union {
24                 ttd_cmd_power_e pwr_cmd;
25                 ttd_cmd_config_e cfg_cmd;
26                 ttd_cmd_package_e pkg_cmd;
27                 ttd_cmd_diagnosis_e diag_cmd;
28                 ttd_cmd_info_e info_cmd;
29                 ttd_cmd_local_e local_cmd;
30         } command;
31         char *cmd_id;
32         void *data;
33         unsigned int data_len;
34 };
35
36 ttd_cmd_data *ttd_cmd_new(void)
37 {
38         ttd_cmd_data *data = NULL;
39
40         data = g_malloc0(sizeof(struct _ttd_cmd_data_s));
41
42         return data;
43 }
44
45 int ttd_cmd_set_type(ttd_cmd_data *cmd, ttd_cmd_type_e type)
46 {
47         retv_if(!cmd, -1);
48
49         cmd->type = type;
50
51         return 0;
52 }
53
54 int ttd_cmd_set_id(ttd_cmd_data *cmd, const char *id)
55 {
56         retv_if(!cmd, -1);
57
58         if (cmd->cmd_id)
59                 g_free(cmd->cmd_id);
60
61         cmd->cmd_id = g_strdup(id);
62
63         return 0;
64 }
65
66 int ttd_cmd_set_command(ttd_cmd_data *cmd, int command)
67 {
68         retv_if(!cmd, -1);
69
70         switch (cmd->type) {
71         case TTD_CMD_TYPE_POWER:
72                 cmd->command.pwr_cmd = command;
73                 break;
74         case TTD_CMD_TYPE_CONFIG:
75                 cmd->command.cfg_cmd = command;
76                 break;
77         case TTD_CMD_TYPE_PACKAGE:
78                 cmd->command.pkg_cmd = command;
79                 break;
80         case TTD_CMD_TYPE_DIAGNOSIS:
81                 cmd->command.diag_cmd = command;
82                 break;
83         case TTD_CMD_TYPE_INFO:
84                 cmd->command.info_cmd = command;
85                 break;
86         case TTD_CMD_TYPE_LOCAL:
87                 cmd->command.local_cmd = command;
88                 break;
89         case TTD_CMD_TYPE_MAX:
90         default:
91                 _E("Unknown cmd type : %d", cmd->type);
92                 return -1;
93                 break;
94         }
95         return 0;
96 }
97
98 int ttd_cmd_set_data(ttd_cmd_data *cmd, void *data, unsigned int length)
99 {
100         retv_if(!cmd, -1);
101
102         cmd->data = data;
103         cmd->data_len = length;
104
105         return 0;
106 }
107
108 void ttd_cmd_free(ttd_cmd_data *cmd)
109 {
110         if (!cmd)
111                 return;
112
113         _D("free cmd type - %d", cmd->type);
114         if (cmd->cmd_id) {
115                 _D("free cmd id - %s", cmd->cmd_id);
116                 g_free(cmd->cmd_id);
117         }
118
119         g_free(cmd);
120 }
121
122 ttd_cmd_type_e ttd_cmd_get_type(ttd_cmd_data *cmd)
123 {
124         retv_if(!cmd, TTD_CMD_TYPE_UNKNOWN);
125
126         return cmd->type;
127 }
128
129 const char *ttd_cmd_get_id(ttd_cmd_data *cmd)
130 {
131         retv_if(!cmd, NULL);
132
133         return cmd->cmd_id;
134 }
135
136 int ttd_cmd_get_command(ttd_cmd_data *cmd)
137 {
138         int command = -1;
139         retv_if(!cmd, -1);
140
141         switch (cmd->type) {
142         case TTD_CMD_TYPE_POWER:
143                 command = cmd->command.pwr_cmd;
144                 break;
145         case TTD_CMD_TYPE_CONFIG:
146                 command = cmd->command.cfg_cmd;
147                 break;
148         case TTD_CMD_TYPE_PACKAGE:
149                 command = cmd->command.pkg_cmd;
150                 break;
151         case TTD_CMD_TYPE_DIAGNOSIS:
152                 command = cmd->command.diag_cmd;
153                 break;
154         case TTD_CMD_TYPE_INFO:
155                 command = cmd->command.info_cmd;
156                 break;
157         case TTD_CMD_TYPE_LOCAL:
158                 command = cmd->command.local_cmd;
159                 break;
160         case TTD_CMD_TYPE_MAX:
161         default:
162                 _E("Unknown cmd type : %d", cmd->type);
163                 return -1;
164                 break;
165         }
166         return command;
167 }
168
169 int ttd_cmd_get_data(ttd_cmd_data *cmd, void **data, unsigned int *length)
170 {
171         retv_if(!cmd, -1);
172
173         *data = cmd->data;
174         *length = cmd->data_len;
175
176         return 0;
177 }