4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <libxml/parser.h>
24 #include "core/list.h"
25 #include "usb-client.h"
29 #define DEFAULT_IPRODUCT_NAME "TIZEN"
31 #define CONF_PATH "/usr/share/deviced/usb-configurations"
32 #define SUPPORTED_CONF_PATH CONF_PATH"/conf-supported.xml"
33 #define USB_CONFIGURATIONS_PATH CONF_PATH"/usb-configurations.xml"
35 #define USB_CON_SET "set"
36 #define USB_CON_UNSET "unset"
38 #define NODE_NAME_TEXT "text"
39 #define NODE_NAME_COMMENT "comment"
40 #define NODE_NAME_CONFIG_NODES "config-nodes"
41 #define NODE_NAME_CONFIG_files "config-files"
42 #define NODE_NAME_USB_CONFIG "usb-config"
43 #define NODE_NAME_CONF_FILE "conf-file"
44 #define NODE_NAME_USB_CONFS "usb-configurations"
45 #define NODE_NAME_USB_OPERS "usb-operations"
46 #define NODE_NAME_DRIVER "driver"
47 #define NODE_NAME_USB_DRV "usb-drv"
48 #define NODE_NAME_MODE "mode"
49 #define NODE_NAME_ACTION "action"
50 #define NODE_NAME_IPRODUCT "iProduct"
51 #define ATTR_VALUE "value"
52 #define ATTR_VERSION "ver"
53 #define ATTR_BACKGROUND "background"
55 static dd_list *supported_list; /* Supported Configurations */
56 static dd_list *oper_list; /* Operations for USB mode */
57 static dd_list *conf_list; /* Configurations for usb mode */
58 static char *driver; /* usb driver version (ex. 1.0, 1.1, none, ... ) */
60 int get_operations_list(dd_list **list)
69 int get_configurations_list(dd_list **list)
79 static void show_supported_confs_list(void)
83 struct xmlSupported *sp;
86 if (!supported_list) {
87 _D("Supported Confs list is empty");
91 _D("********************************");
92 _D("** Supported Confs list");
93 _D("********************************");
95 DD_LIST_FOREACH(supported_list, l, sp) {
96 _D("** Mode : %d", sp->mode);
97 _D("********************************");
101 static void show_operation_list(void)
105 struct xmlOperation *oper;
107 _D("********************************");
108 _D("** Operation list");
109 _D("********************************");
111 _D("Operation list is empty");
112 _D("********************************");
117 DD_LIST_FOREACH(oper_list, l, oper) {
118 _D("** Number : %d", i++);
119 _D("** Name : %s", oper->name);
120 _D("** Operation : %s", oper->oper);
121 _D("** Background: %d", oper->background);
122 _D("********************************");
126 static void show_configuration_list(void)
130 struct xmlConfiguration *conf;
133 _D("Configuration list is empty");
137 _D("********************************");
138 _D("** Configuration list");
139 _D("********************************");
142 DD_LIST_FOREACH(conf_list, l, conf) {
143 _D("** Number: %d", i++);
144 _D("** Name : %s", conf->name);
145 _D("** Path : %s", conf->path);
146 _D("** Value : %s", conf->value);
147 _D("********************************");
151 #define show_supported_confs_list() do {} while(0)
152 #define show_operation_list() do {} while(0)
153 #define show_configuration_list() do {} while(0)
156 static xmlDocPtr xml_open(const char *xml)
161 return xmlReadFile(xml, NULL, 0);
164 static void xml_close(xmlDocPtr doc)
169 static bool skip_node(xmlNodePtr node)
174 if (!xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_TEXT))
176 if (!xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_COMMENT))
182 static int get_xml_property(xmlNodePtr node, char *property, char *value, int len)
186 xmlprop = xmlGetProp(node, (const xmlChar *)property);
190 snprintf(value, len, "%s", (char *)xmlprop);
196 static bool is_usb_mode_supported(int usb_mode)
199 struct xmlSupported *sp;
201 DD_LIST_FOREACH(supported_list, l, sp) {
202 if (sp->mode == usb_mode)
209 static int get_iproduct_name(char *name, int size)
214 /* TODO: Product name should be set using device model
215 * ex) TIZEN_SM-Z9005 */
217 snprintf(name, size, "%s", DEFAULT_IPRODUCT_NAME);
221 static int get_driver_version(xmlNodePtr root)
232 for (node = root->children ; node ; node = node->next) {
236 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_DRIVER))
239 ret = get_xml_property(node, ATTR_VALUE, path, sizeof(path));
241 _E("Failed to get property(%d)", ret);
245 if (access(path, F_OK) != 0) {
246 /* TODO: If the path does not exist,
247 * usb gadgets are not from Galaxy, but from Linux Kernel */
252 fp = fopen(path, "r");
254 _E("fopen() failed(%s)", path);
258 if (!fgets(buf, sizeof(buf), fp)) {
259 _E("fgets() failed");
266 driver = strdup(buf);
272 /* Getting sysfs nodes to set usb configurations */
273 static int add_conf_nodes_to_list(xmlNodePtr root)
277 struct xmlConfiguration *conf;
283 for (node = root->children ; node ; node = node->next) {
287 ret = get_xml_property(node, ATTR_VALUE, path, sizeof(path));
289 _E("Failed to get property(%d)", ret);
293 conf = (struct xmlConfiguration *)malloc(sizeof(struct xmlConfiguration));
295 _E("malloc() failed");
299 conf->name = strdup((const char *)(node->name));
300 conf->path = strdup(path);
303 if (!(conf->name) || !(conf->path)) {
304 _E("strdup() failed");
313 DD_LIST_APPEND(conf_list, conf);
319 static int get_configuration_nodes(xmlNodePtr root)
328 for (node = root->children ; node ; node = node->next) {
332 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_USB_DRV))
335 ret = get_xml_property(node, ATTR_VERSION, ver, sizeof(ver));
337 _E("Failed to get property(%d)", ret);
341 if (strncmp(ver, driver, strlen(ver)))
344 return add_conf_nodes_to_list(node);
350 int make_empty_configuration_list(void)
357 doc = xml_open(USB_CONFIGURATIONS_PATH);
359 _E("fail to open xml file (%s)", USB_CONFIGURATIONS_PATH);
363 root = xmlDocGetRootElement(doc);
365 _E("FAIL: xmlDocGetRootElement()");
370 for (node = root->children ; node ; node = node->next) {
374 if (!xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_CONFIG_NODES)) {
375 ret = get_driver_version(node);
377 _E("Failed to get usb driver version");
382 ret = get_configuration_nodes(node);
384 _E("Failed to get conf nodes");
389 show_configuration_list();
401 static int add_configurations_to_list(xmlNodePtr root)
405 struct xmlConfiguration *conf;
413 DD_LIST_FOREACH(conf_list, l, conf) {
419 if (!strncmp(conf->name, NODE_NAME_IPRODUCT, strlen(NODE_NAME_IPRODUCT))) {
420 ret = get_iproduct_name(buf, sizeof(buf));
422 conf->value = strdup(buf);
426 for (node = root->children ; node ; node = node->next) {
430 if (xmlStrcmp((const xmlChar *)conf->name, node->name))
433 ret = get_xml_property(node, ATTR_VALUE, value, sizeof(value));
435 _E("Failed to get property(%d)", ret);
439 conf->value = strdup(value);
447 static int get_configurations_by_usb_mode(xmlNodePtr root, int mode)
456 for (node = root->children ; node ; node = node->next) {
460 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_MODE))
463 ret = get_xml_property(node, ATTR_VALUE, cMode, sizeof(cMode));
465 _E("Failed to get property(%d)", ret);
473 return add_configurations_to_list(node);
478 static int get_configurations_by_version(xmlNodePtr root, int mode)
487 for (node = root->children ; node ; node = node->next) {
491 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_USB_DRV))
494 ret = get_xml_property(node, ATTR_VERSION, ver, sizeof(ver));
496 _E("Failed to get property(%d)", ret);
500 if (strncmp(ver, driver, strlen(ver)))
503 return get_configurations_by_usb_mode(node, mode);
509 static int get_configurations_info(xmlNodePtr root, int mode)
516 for (node = root->children ; node ; node = node->next) {
520 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_USB_CONFS))
523 return get_configurations_by_version(node, mode);
529 int make_configuration_list(int usb_mode)
536 if (!is_usb_mode_supported(usb_mode)) {
537 _E("USB mode (%d) is not supported", usb_mode);
541 doc = xml_open(USB_CONFIGURATIONS_PATH);
543 _E("fail to open xml file (%s)", USB_CONFIGURATIONS_PATH);
547 root = xmlDocGetRootElement(doc);
549 _E("FAIL: xmlDocGetRootElement()");
554 ret = get_configurations_info(root, usb_mode);
556 _E("Failed to get operations for usb mode(%d)", usb_mode);
561 show_configuration_list();
570 void release_configuration_list(void)
573 struct xmlConfiguration *conf;
578 DD_LIST_FOREACH(conf_list, l, conf) {
589 DD_LIST_FREE_LIST(conf_list);
593 /* Getting configurations supported */
594 static int get_supported_confs(xmlNodePtr root)
600 struct xmlSupported *sp;
605 for (node = root->children ; node ; node = node->next) {
609 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_MODE))
612 ret = get_xml_property(node, ATTR_VALUE, cMode, sizeof(cMode));
614 _E("Failed to get property(%d)", ret);
620 sp = (struct xmlSupported *)malloc(sizeof(struct xmlSupported));
622 _E("malloc() failed");
628 DD_LIST_APPEND(supported_list, sp);
634 int make_supported_confs_list(void)
641 doc = xml_open(SUPPORTED_CONF_PATH);
643 _E("fail to open xml file (%s)", SUPPORTED_CONF_PATH);
647 root = xmlDocGetRootElement(doc);
649 _E("FAIL: xmlDocGetRootElement()");
654 ret = get_supported_confs(root);
656 _E("Failed to get supported confs");
661 show_supported_confs_list();
669 void release_supported_confs_list(void)
671 struct xmlSupported *sp;
677 DD_LIST_FOREACH(supported_list, l, sp) {
681 DD_LIST_FREE_LIST(supported_list);
682 supported_list = NULL;
685 /* Getting operations for each usb mode */
686 static int get_operations_info(xmlNodePtr node, struct xmlOperation **oper)
689 char background[BUF_MAX];
690 char operation[BUF_MAX];
695 ret = get_xml_property(node, ATTR_VALUE, operation, sizeof(operation));
697 _E("Failed to get property(%d)", ret);
701 *oper = (struct xmlOperation *)malloc(sizeof(struct xmlOperation));
703 _E("malloc() failed");
707 (*oper)->name = strdup((const char *)(node->name));
708 (*oper)->oper = strdup(operation);
710 if (!((*oper)->name) || !((*oper)->oper)) {
722 ret = get_xml_property(node, ATTR_BACKGROUND, background, sizeof(background));
724 (*oper)->background = false;
728 if (strncmp(background, "true", strlen(background)))
729 (*oper)->background = false;
731 (*oper)->background = true;
736 static int add_operations_to_list(xmlNodePtr root)
740 struct xmlOperation *oper;
745 for (node = root->children ; node ; node = node->next) {
749 ret = get_operations_info(node, &oper);
750 if (ret < 0 || !oper) {
751 _E("Failed to get operations info");
755 DD_LIST_APPEND(oper_list, oper);
761 static int get_operations_by_usb_mode(xmlNodePtr root, int usb_mode)
771 for (node = root->children ; node ; node = node->next) {
775 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_MODE))
778 ret = get_xml_property(node, ATTR_VALUE, cMode, sizeof(cMode));
780 _E("Failed to get property(%d)", ret);
786 if (usb_mode != iMode)
789 ret = add_operations_to_list(node);
791 _E("Failed to add operations to list ");
800 static int get_operations_by_action(xmlNodePtr root, int usb_mode, char *action)
806 if (!root || !action)
809 for (node = root->children ; node ; node = node->next) {
813 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_ACTION))
816 ret = get_xml_property(node, ATTR_VALUE, act, sizeof(act));
818 _E("Failed to get property(%d)", ret);
822 if (strncmp(act, action, strlen(act)))
825 ret = get_operations_by_usb_mode(node, usb_mode);
827 _E("Failed to get operations for usb_mode (%d)", usb_mode);
836 static int get_usb_operations(xmlNodePtr root, int usb_mode, char *action)
840 if (!root || !action)
843 for (node = root->children ; node ; node = node->next) {
847 if (xmlStrcmp(node->name, (const xmlChar *)NODE_NAME_USB_OPERS))
850 return get_operations_by_action(node, usb_mode, action);
856 int make_operation_list(int usb_mode, char *action)
862 char conf_file_path[BUF_MAX];
867 if (!is_usb_mode_supported(usb_mode)) {
868 _E("USB mode (%d) is not supported", usb_mode);
872 doc = xml_open(USB_CONFIGURATIONS_PATH);
874 _E("fail to open xml file (%s)", USB_CONFIGURATIONS_PATH);
878 root = xmlDocGetRootElement(doc);
880 _E("FAIL: xmlDocGetRootElement()");
885 ret = get_usb_operations(root, usb_mode, action);
887 _E("Failed to get operations info(%d, %s)", usb_mode, action);
892 show_operation_list();
900 void release_operations_list(void)
903 struct xmlOperation *oper;
908 DD_LIST_FOREACH(oper_list, l, oper) {
917 DD_LIST_FREE_LIST(oper_list);