3 * @author Vyacheslav Cherkashin
4 * @author Vitaliy Cherepanov
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Copyright (C) Samsung Electronics, 2013
26 * @section DESCRIPTION
28 * Message parsing implementation.
32 #include <linux/slab.h>
33 #include "msg_parser.h"
35 #include "parser_defs.h"
38 static int str_to_u32(const char* str, u32 *val)
44 for (result = 0 ; *str; ++str) {
45 if (*str < '0' || *str> '9')
48 result = result * 10 + (*str - '0');
60 /* ============================================================================
62 * ============================================================================
66 * @brief Creates and fills app_info_data struct.
68 * @param mb Pointer to the message buffer.
69 * @return Pointer to the filled app_info_data struct on success;\n
72 struct app_info_data *create_app_info(struct msg_buf *mb)
75 struct app_info_data *ai;
77 char *ta_id, *exec_path;
79 print_parse_debug("app_info:\n");
81 print_parse_debug("type:");
82 ret = get_u32(mb, &app_type);
84 print_err("failed to read target application type\n");
88 print_parse_debug("id:");
89 ret = get_string(mb, &ta_id);
91 print_err("failed to read target application ID\n");
95 print_parse_debug("exec path:");
96 ret = get_string(mb, &exec_path);
98 print_err("failed to read executable path\n");
102 ai = kmalloc(sizeof(*ai), GFP_KERNEL);
104 print_err("out of memory\n");
109 case AT_TIZEN_NATIVE_APP:
110 case AT_TIZEN_WEB_APP:
117 if (*ta_id != '\0') {
118 ret = str_to_u32(ta_id, &tgid);
120 print_err("converting string to PID, "
121 "str='%s'\n", ta_id);
130 print_err("wrong application type(%u)\n", app_type);
135 ai->app_type = (enum APP_TYPE)app_type;
137 ai->exec_path = exec_path;
145 put_string(exec_path);
154 * @brief app_info_data cleanup.
156 * @param ai Pointer to the target app_info_data.
159 void destroy_app_info(struct app_info_data *ai)
161 put_string(ai->exec_path);
162 put_string(ai->app_id);
170 /* ============================================================================
172 * ============================================================================
176 * @brief Creates and fills conf_data struct.
178 * @param mb Pointer to the message buffer.
179 * @return Pointer to the filled conf_data struct on success;\n
182 struct conf_data *create_conf_data(struct msg_buf *mb)
184 struct conf_data *conf;
185 u64 use_features0, use_features1;
188 print_parse_debug("conf_data:\n");
190 print_parse_debug("features:");
191 if (get_u64(mb, &use_features0)) {
192 print_err("failed to read use_features\n");
196 if (get_u64(mb, &use_features1)) {
197 print_err("failed to read use_features\n");
201 print_parse_debug("sys trace period:");
202 if (get_u32(mb, &stp)) {
203 print_err("failed to read sys trace period\n");
207 print_parse_debug("data msg period:");
208 if (get_u32(mb, &dmp)) {
209 print_err("failed to read data message period\n");
213 conf = kmalloc(sizeof(*conf), GFP_KERNEL);
215 print_err("out of memory\n");
219 conf->use_features0 = use_features0;
220 conf->use_features1 = use_features1;
221 conf->sys_trace_period = stp;
222 conf->data_msg_period = dmp;
228 * @brief conf_data cleanup.
230 * @param conf Pointer to the target conf_data.
233 void destroy_conf_data(struct conf_data *conf)
238 static struct conf_data config;
241 * @brief Saves config to static config variable.
243 * @param conf Variable to save.
246 void save_config(const struct conf_data *conf)
248 memcpy(&config, conf, sizeof(config));
252 * @brief Restores config from static config variable.
254 * @param conf Variable to restore.
257 void restore_config(struct conf_data *conf)
259 memcpy(conf, &config, sizeof(*conf));
264 /* ============================================================================
266 * ============================================================================
270 * @brief Creates and fills func_inst_data struct.
272 * @param mb Pointer to the message buffer.
273 * @return Pointer to the filled func_inst_data struct on success;\n
276 struct func_inst_data *create_func_inst_data(struct msg_buf *mb)
278 struct func_inst_data *fi;
283 print_parse_debug("func addr:");
284 if (get_u64(mb, &addr)) {
285 print_err("failed to read data function address\n");
289 print_parse_debug("funct args:");
290 if (get_string(mb, &args)) {
291 print_err("failed to read data function arguments\n");
295 print_parse_debug("funct ret type:");
296 if (get_u8(mb, (u8 *)&ret_type)) {
297 print_err("failed to read data function arguments\n");
301 fi = kmalloc(sizeof(*fi), GFP_KERNEL);
303 print_err("out of memory\n");
309 fi->ret_type = ret_type;
319 * @brief func_inst_data cleanup.
321 * @param fi Pointer to the target func_inst_data.
324 void destroy_func_inst_data(struct func_inst_data *fi)
326 put_string(fi->args);
334 /* ============================================================================
336 * ============================================================================
340 * @brief Creates and fills lib_inst_data struct.
342 * @param mb Pointer to the message buffer.
343 * @return Pointer to the filled lib_inst_data struct on success;\n
346 struct lib_inst_data *create_lib_inst_data(struct msg_buf *mb)
348 struct lib_inst_data *li;
349 struct func_inst_data *fi;
353 print_parse_debug("bin path:");
354 if (get_string(mb, &path)) {
355 print_err("failed to read path of binary\n");
359 print_parse_debug("func count:");
360 if (get_u32(mb, &cnt)) {
361 print_err("failed to read count of functions\n");
365 if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt) {
366 print_err("to match count of functions(%u)\n", cnt);
370 li = kmalloc(sizeof(*li), GFP_KERNEL);
373 print_err("out of memory\n");
377 li->func = kmalloc(sizeof(struct func_inst_data *) * cnt, GFP_KERNEL);
378 if (li->func == NULL)
379 if (li->func == NULL) {
380 print_err("out of memory\n");
384 for (i = 0; i < cnt; ++i) {
385 print_parse_debug("func #%d:\n", i + 1);
386 fi = create_func_inst_data(mb);
399 for (j = 0; j < i; ++j)
400 destroy_func_inst_data(li->func[j]);
413 * @brief lib_inst_data cleanup.
415 * @param li Pointer to the target lib_inst_data.
418 void destroy_lib_inst_data(struct lib_inst_data *li)
422 put_string(li->path);
424 for (i = 0; i < li->cnt_func; ++i)
425 destroy_func_inst_data(li->func[i]);
435 /* ============================================================================
437 * ============================================================================
441 * @brief Creates and fills app_inst_data struct.
443 * @param mb Pointer to the message buffer.
444 * @return Pointer to the filled app_inst_data struct on success;\n
447 struct app_inst_data *create_app_inst_data(struct msg_buf *mb)
449 struct app_inst_data *app_inst;
450 struct app_info_data *app_info;
451 struct func_inst_data *func;
452 struct lib_inst_data *lib;
453 u32 cnt_func, i_func = 0, cnt_lib, i_lib = 0, i;
455 app_info = create_app_info(mb);
456 if (app_info == NULL)
459 print_parse_debug("func count:");
460 if (get_u32(mb, &cnt_func)) {
461 print_err("failed to read count of functions\n");
465 if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt_func) {
466 print_err("to match count of functions(%u)\n", cnt_func);
470 app_inst = kmalloc(sizeof(*app_inst), GFP_KERNEL);
471 if (app_inst == NULL) {
472 print_err("out of memory\n");
476 app_inst->func = kmalloc(sizeof(struct func_inst_data *) * cnt_func,
478 if (app_inst->func == NULL) {
479 print_err("out of memory\n");
483 for (i_func = 0; i_func < cnt_func; ++i_func) {
484 print_parse_debug("func #%d:\n", i_func + 1);
485 func = create_func_inst_data(mb);
489 app_inst->func[i_func] = func;
492 print_parse_debug("lib count:");
493 if (get_u32(mb, &cnt_lib)) {
494 print_err("failed to read count of libraries\n");
498 if (remained_mb(mb) / MIN_SIZE_LIB_INST < cnt_lib) {
499 print_err("to match count of libraries(%u)\n", cnt_lib);
503 app_inst->lib = kmalloc(sizeof(struct lib_inst_data *) * cnt_lib,
505 if (app_inst->lib == NULL) {
506 print_err("out of memory\n");
510 for (i_lib = 0; i_lib < cnt_lib; ++i_lib) {
511 print_parse_debug("lib #%d:\n", i_lib + 1);
512 lib = create_lib_inst_data(mb);
516 app_inst->lib[i_lib] = lib;
519 app_inst->app_info = app_info;
520 app_inst->cnt_func = cnt_func;
521 app_inst->cnt_lib = cnt_lib;
526 for (i = 0; i < i_lib; ++i)
527 destroy_lib_inst_data(app_inst->lib[i]);
528 kfree(app_inst->lib);
531 for (i = 0; i < i_func; ++i)
532 destroy_func_inst_data(app_inst->func[i]);
533 kfree(app_inst->func);
539 destroy_app_info(app_info);
545 * @brief app_inst_data cleanup.
547 * @param ai Pointer to the target app_inst_data.
550 void destroy_app_inst_data(struct app_inst_data *ai)
554 for (i = 0; i < ai->cnt_lib; ++i)
555 destroy_lib_inst_data(ai->lib[i]);
558 for (i = 0; i < ai->cnt_func; ++i)
559 destroy_func_inst_data(ai->func[i]);
562 destroy_app_info(ai->app_info);
570 /* ============================================================================
572 * ============================================================================
576 * @brief Creates and fills us_inst_data struct.
578 * @param mb Pointer to the message buffer.
579 * @return Pointer to the filled us_inst_data struct on success;\n
582 struct us_inst_data *create_us_inst_data(struct msg_buf *mb)
584 struct us_inst_data *ui;
585 struct app_inst_data *ai;
588 print_parse_debug("us_inst_data:\n");
590 print_parse_debug("app count:");
591 if (get_u32(mb, &cnt)) {
592 print_err("failed to read count of applications\n");
596 if (remained_mb(mb) / MIN_SIZE_APP_INST < cnt) {
597 print_err("to match count of applications(%u)\n", cnt);
601 ui = kmalloc(sizeof(struct us_inst_data), GFP_KERNEL);
603 print_err("out of memory\n");
607 ui->app_inst = kmalloc(sizeof(struct app_inst_data *) * cnt,
609 if (ui->app_inst == NULL) {
610 print_err("out of memory\n");
614 for (i = 0; i < cnt; ++i) {
615 print_parse_debug("app #%d:\n",i+1);
616 ai = create_app_inst_data(mb);
620 ui->app_inst[i] = ai;
628 for (j = 0; j < i; ++j)
629 destroy_app_inst_data(ui->app_inst[j]);
639 * @brief us_inst_data cleanup.
641 * @param ui Pointer to the target us_inst_data.
644 void destroy_us_inst_data(struct us_inst_data *ui)
648 for (i = 0; i < ui->cnt; ++i)
649 destroy_app_inst_data(ui->app_inst[i]);