tizen 2.3 release
[framework/system/deviced.git] / src / usb / usb-host-naming.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20 #include "usb-host.h"
21 #include <string.h>
22
23 #define VENDOR_HP_1         "Hewlett-Packard"
24 #define VENDOR_HP_2         "Hewlett Packard"
25 #define VENDOR_HP_3         "HP"
26 #define VENDOR_SAMSUNG      "Samsung"
27
28 static void remove_underbar(char *name)
29 {
30         int i;
31         char *temp = name;
32
33         if (!temp)
34                 return;
35
36         for (i = 0 ; i < strlen(name); temp++, i++) {
37                 if (*temp == '_')
38                         *temp = ' ';
39         }
40 }
41
42 static void remove_non_alphabet(char *name)
43 {
44         int i;
45         char *pos, *temp;
46
47         if (!name)
48                 return;
49
50         pos = name + strlen(name) - 1;
51
52         for (i = strlen(name); i > 0 ; i--, pos--) {
53                 if ((*pos >= 48 && *pos <= 57)          /* number */
54                                 || (*pos >= 65 && *pos <= 90)   /* uppercase letter */
55                                 || (*pos >= 97 && *pos <= 122)  /* lowercase letter */
56                                 || *pos == '-'                 /* hyphen  */
57                                 || *pos == ' ')                /* white space */
58                         continue;
59
60                 temp = pos;
61                 while (*temp != '\0') {
62                         *temp = *(temp + 1);
63                         temp++;
64                 }
65         }
66 }
67
68 static void change_to_short(char *name, int len)
69 {
70         if (strcasestr(name, VENDOR_HP_1)) {
71                 snprintf(name, len, "%s", VENDOR_HP_3);
72                 return;
73         }
74
75         if (strcasestr(name, VENDOR_HP_2)) {
76                 snprintf(name, len, "%s", VENDOR_HP_3);
77                 return;
78         }
79
80         if (strcasestr(name, VENDOR_HP_3)) {
81                 snprintf(name, len, "%s", VENDOR_HP_3);
82                 return;
83         }
84
85         if (strcasestr(name, VENDOR_SAMSUNG)) {
86                 snprintf(name, len, "%s", VENDOR_SAMSUNG);
87                 return;
88         }
89 }
90
91 static void remove_vendor(char *model, char *vendor)
92 {
93         char *pos, *temp;
94         int i;
95
96         pos = strcasestr(model, vendor);
97         if (!pos)
98                 return;
99
100         temp = pos + strlen(vendor);
101
102         while(*pos != '\0') {
103                 *pos = *temp;
104                 pos++;
105                 temp++;
106         }
107 }
108
109 int verify_vendor_name(const char *vendor, char *buf, int len)
110 {
111         char name[BUF_MAX];
112
113         if (!vendor || !buf || len <= 0)
114                 return -EINVAL;
115
116         snprintf(name, sizeof(name), "%s", vendor);
117
118         remove_underbar(name);
119
120         remove_non_alphabet(name);
121
122         change_to_short(name, sizeof(name));
123
124         snprintf(buf, len, "%s", name);
125         return 0;
126 }
127
128 int verify_model_name(const char *model, char *vendor, char *buf, int len)
129 {
130         char name[BUF_MAX];
131
132         if (!model || !vendor || !buf || len <= 0)
133                 return -EINVAL;
134
135         snprintf(name, sizeof(name), "%s", model);
136
137         remove_underbar(name);
138
139         remove_non_alphabet(name);
140
141         change_to_short(name, sizeof(name));
142
143         remove_vendor(name, vendor);
144
145         snprintf(buf, len, "%s", name);
146         return 0;
147 }