Add a new wifi error type for association failure
[platform/core/api/wifi-manager.git] / tools / manager-test / wman_test_common.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include "wman_test_common.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <wifi-manager.h>
23
24 const char *wman_test_strerror(wifi_manager_error_e err_type)
25 {
26         switch (err_type) {
27         case WIFI_MANAGER_ERROR_NONE:
28                 return "NONE";
29         case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
30                 return "INVALID_PARAMETER";
31         case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
32                 return "OUT_OF_MEMORY";
33         case WIFI_MANAGER_ERROR_INVALID_OPERATION:
34                 return "INVALID_OPERATION";
35         case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
36                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
37         case WIFI_MANAGER_ERROR_OPERATION_FAILED:
38                 return "OPERATION_FAILED";
39         case WIFI_MANAGER_ERROR_NO_CONNECTION:
40                 return "NO_CONNECTION";
41         case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
42                 return "NOW_IN_PROGRESS";
43         case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
44                 return "ALREADY_EXISTS";
45         case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
46                 return "OPERATION_ABORTED";
47         case WIFI_MANAGER_ERROR_DHCP_FAILED:
48                 return "DHCP_FAILED";
49         case WIFI_MANAGER_ERROR_INVALID_KEY:
50                 return "INVALID_KEY";
51         case WIFI_MANAGER_ERROR_NO_REPLY:
52                 return "NO_REPLY";
53         case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
54                 return "SECURITY_RESTRICTED";
55         case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED:
56                 return "ALREADY_INITIALIZED";
57         case WIFI_MANAGER_ERROR_OUT_OF_RANGE:
58                 return "OUT_OF_RANGE";
59         case WIFI_MANAGER_ERROR_CONNECT_FAILED:
60                 return "CONNECT_FAILED";
61         case WIFI_MANAGER_ERROR_LOGIN_FAILED:
62                 return "LOGIN_FAILED";
63         case WIFI_MANAGER_ERROR_AUTHENTICATION_FAILED:
64                 return "AUTH_FAILED";
65         case WIFI_MANAGER_ERROR_ASSOCIATION_FAILED:
66                 return "ASSOC_FAILED";
67         case WIFI_MANAGER_ERROR_PIN_MISSING:
68                 return "PIN_MISSING";
69         case WIFI_MANAGER_ERROR_WPS_OVERLAP:
70                 return "WPS_OVERLAP";
71         case WIFI_MANAGER_ERROR_WPS_TIMEOUT:
72                 return "WPS_TIMEOUT";
73         case WIFI_MANAGER_ERROR_WPS_WEP_PROHIBITED:
74                 return "WPS_WEP_PROHIBITED";
75         case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
76                 return "PERMISSION_DENIED";
77         case WIFI_MANAGER_ERROR_OFFLINE:
78                 return "OFFLINE";
79         case WIFI_MANAGER_ERROR_INVALID_GATEWAY:
80                 return "INVALID_GATEWAY";
81         case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
82                 return "NOT_SUPPORTED";
83         case WIFI_MANAGER_ERROR_NOT_INITIALIZED:
84                 return "NOT_INITIALIZED";
85         default:
86                 return "UNKNOWN";
87         }
88 }
89
90 bool wman_test_compare_ap_name(const char *ap_name, const char *ap_name_part)
91 {
92         int ap_name_len = strlen(ap_name);
93         int ap_name_part_len = strlen(ap_name_part);
94
95         if (strncmp(ap_name, ap_name_part,
96                 ap_name_len > ap_name_part_len ? ap_name_len : ap_name_part_len) == 0)
97                 return true;
98         else
99                 return false;
100 }
101
102 void wman_test_connected_cb(wifi_manager_error_e result, void* user_data)
103 {
104         if (result == WIFI_MANAGER_ERROR_NONE)
105                 printf("Wi-Fi Connection Succeeded\n");
106         else
107                 printf("Wi-Fi Connection Failed! error : %s\n", wman_test_strerror(result));
108 }
109
110 bool test_get_user_int(const char *msg, int *num)
111 {
112         if (msg == NULL || num == NULL)
113                 return false;
114
115         int rv;
116         char buf[32] = {0,};
117         printf("%s\n", msg);
118         rv = read(0, buf, 32);
119
120         if (rv < 0 || *buf == 0 || *buf == '\n' || *buf == '\r')
121                 return false;
122
123         *num = atoi(buf);
124         return true;
125 }
126
127 bool test_get_user_string(const char *msg, char *buf, int buf_size)
128 {
129         if (msg == NULL || buf == NULL || buf_size < 2)
130                 return false;
131
132         int rv;
133         printf("%s\n", msg);
134         memset(buf, 0, buf_size);
135         rv = read(0, buf, buf_size - 1);
136
137         if (rv < 0 || buf[0] == '\0' || buf[0] == '\n' || buf[0] == '\r') {
138                 buf[0] = '\0';
139                 return false;
140         }
141
142         buf[rv - 1] = '\0';
143         return true;
144 }
145
146 static inline int __test_convert_byte_to_txt(const unsigned char *src, char **dst, int src_len)
147 {
148         int dst_length = 0;
149         int i = 0;
150         char *buf = NULL;
151
152         if (src_len <= 0) {
153                 printf("Invalid source length\n");
154                 return -1;
155         }
156
157         *dst = malloc((2 * src_len) + 1);
158         if (!(*dst)) {
159                 printf("failed to allocate memory to buffer\n");
160                 return -1;
161         }
162
163         buf = (*dst);
164
165         for (i = 0; i < src_len; i++) {
166                 snprintf(buf, 3, "%02x", src[i]);
167                 buf += 2;
168                 dst_length += 2;
169         }
170
171         return dst_length;
172 }
173
174 bool __test_vendor_specific_cb(unsigned char *vsie_bytes, int vsie_len, void *user_data)
175 {
176         if (!vsie_bytes)
177                 return false;
178
179         char *vsie = NULL;
180         int buf_len = __test_convert_byte_to_txt(vsie_bytes, &vsie, vsie_len);
181         if (buf_len > 0)
182                 printf("vsie_len: %d, vsie: %s\n", vsie_len, vsie);
183
184         free(vsie);
185
186         return true;
187 }