2 * Network Configuration Module
4 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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.
20 #include <sys/utsname.h>
21 #include <sys/socket.h>
31 #define WIFI_MAC_INFO_PATH "/opt/etc/.mac.info"
33 #define WIFI_MODULE_NAME "sprdwl.ko"
34 #define WIFI_MODULE_PATH_PREFIX "/lib/modules/"
35 #define WIFI_MODULE_PATH_POSTFIX "/kernel/drivers/net/wireless/sc2331/"WIFI_MODULE_NAME
37 #ifndef TEMP_FAILURE_RETRY
38 /* Used to retry syscalls that can return EINTR. */
39 #define TEMP_FAILURE_RETRY(exp) ({ \
43 } while (_rc == -1 && errno == EINTR); \
47 #define WCND_SOCKET_NAME "/tmp/wcnd"
48 #define WCND_CMD_STR_START_CP2 "wcn WIFI-OPEN"
49 #define WCND_CMD_STR_STOP_CP2 "wcn WIFI-CLOSE"
50 #define WCND_RESP_STR_WIFI_OK "WCNBTWIFI-CMD OK"
52 static int wcnd_socket = -1;
54 static int socket_local_client(const char *path, int type)
57 struct sockaddr_un addr;
61 s = socket(AF_LOCAL, type, 0);
64 memset(&addr, 0, sizeof(addr));
66 addr.sun_family = AF_LOCAL;
68 pathlen = strlen(path);
69 if (pathlen > sizeof(addr)
70 - offsetof(struct sockaddr_un, sun_path) - 1) {
76 memcpy(addr.sun_path + 1, path, pathlen);
78 alen = pathlen + offsetof(struct sockaddr_un, sun_path) + 1;
80 if (connect(s, (struct sockaddr *)&addr, alen) < 0) {
88 static int connect_wcnd(void)
92 client_fd = socket_local_client(WCND_SOCKET_NAME, SOCK_STREAM);
96 WIFI_LOGE("%s: Unable bind server %s, waiting...", __func__, WCND_SOCKET_NAME);
98 client_fd = socket_local_client(WCND_SOCKET_NAME, SOCK_STREAM);
104 static int start_cp2(void)
110 WIFI_LOGE("%s: send %s to %s",__func__, WCND_CMD_STR_START_CP2, WCND_SOCKET_NAME);
112 memset(buffer, 0, 128);
114 n = strlen(WCND_CMD_STR_START_CP2) + 1;
116 TEMP_FAILURE_RETRY(write(wcnd_socket, WCND_CMD_STR_START_CP2, n));
118 memset(buffer, 0, 128);
120 WIFI_LOGE("%s: waiting for server %s",__func__, WCND_SOCKET_NAME);
121 n = read(wcnd_socket, buffer, sizeof(buffer) - 1);
123 WIFI_LOGE("%s: get %d bytes %s", __func__, n, buffer);
125 if (!strstr(buffer, WCND_RESP_STR_WIFI_OK)) ret = -1;
130 static int stop_cp2(void)
136 WIFI_LOGE("%s: send %s to %s",__func__, WCND_CMD_STR_STOP_CP2, WCND_SOCKET_NAME);
138 memset(buffer, 0, 128);
140 n = strlen(WCND_CMD_STR_STOP_CP2) + 1;
142 TEMP_FAILURE_RETRY(write(wcnd_socket, WCND_CMD_STR_STOP_CP2, n));
144 memset(buffer, 0, 128);
146 WIFI_LOGE("%s: waiting for server %s",__func__, WCND_SOCKET_NAME);
147 n = read(wcnd_socket, buffer, sizeof(buffer) - 1);
149 WIFI_LOGE("%s: get %d bytes %s", __func__, n, buffer);
151 if (!strstr(buffer, WCND_RESP_STR_WIFI_OK)) ret = -1;
156 static int wifi_find_module_path(char *path)
162 strcat(path, buf.release);
163 strcat(path, WIFI_MODULE_PATH_POSTFIX);
165 WIFI_LOGE("path = %s", path);
169 static void *wifi_load_module(const char *path, int *len)
174 fd = open(path, O_RDONLY);
178 size = lseek(fd, 0, SEEK_END);
182 if (lseek(fd, 0, SEEK_SET) != 0)
185 buf = (char *) malloc(size+1);
189 if (read(fd, buf, size) != size)
208 static int insmod(const char *path, const char *args)
213 module = wifi_load_module(path, &len);
217 ret = init_module(module, len, args);
220 WIFI_LOGE("insmod failed ret = %d, %s", ret, strerror(errno));
230 static int rmmod(char *modname)
235 len = strlen(modname);
238 WIFI_LOGE("modname = %s", modname);
240 ret = delete_module(modname, O_NONBLOCK | O_EXCL);
242 WIFI_LOGE("rmmmod failed %s: ret = %d, %s", modname, ret, strerror(errno));
247 static int wifi_check_mac_info()
253 file = fopen(WIFI_MAC_INFO_PATH, "r");
255 WIFI_LOGE("Failed to open %s", WIFI_MAC_INFO_PATH);
259 if(!fgets(buf, sizeof(buf), file)) {
260 WIFI_LOGE("Failed to read %s. Remove file", WIFI_MAC_INFO_PATH);
262 ret = remove(WIFI_MAC_INFO_PATH);
264 WIFI_LOGE("%s file remove failed", WIFI_MAC_INFO_PATH);
269 WIFI_LOGE("Successfully read %s. MAC : ", WIFI_MAC_INFO_PATH, buf);
274 static int __wifi_start(int mode)
276 char wifi_module_path[100] = WIFI_MODULE_PATH_PREFIX;
279 WIFI_LOGE("mode = %d", mode);
281 ret = wifi_find_module_path(wifi_module_path);
283 WIFI_LOGE("wifi_find_module_path failed");
288 wcnd_socket = connect_wcnd();
290 if (wcnd_socket > 0 && (start_cp2() < 0))
291 WIFI_LOGE("start CP2 FAIL");
294 ret = insmod(wifi_module_path, "con_mode=5");
296 ret = insmod(wifi_module_path, "");
301 static int wifi_start()
305 ret = __wifi_start(1);
307 WIFI_LOGE("__wifi_start(1) failed");
314 static int wifi_softap()
318 ret = __wifi_start(2);
320 WIFI_LOGE("__wifi_start(2) failed");
327 static int wifi_rftest()
331 ret = __wifi_start(3);
333 WIFI_LOGE("__wifi_start(3) failed");
340 static int wifi_stop()
342 char wifi_module_name[20] = WIFI_MODULE_NAME;
345 WIFI_LOGE("wifi_stop called");
347 ret = rmmod(wifi_module_name);
349 WIFI_LOGE("rmmod failed");
354 wcnd_socket = connect_wcnd();
356 if (wcnd_socket > 0 && (stop_cp2() < 0))
357 WIFI_LOGE("stop CP2 FAIL");
362 static int wifi_check_hw()
366 ret = wifi_check_mac_info();
368 WIFI_LOGE("There is no %s file", WIFI_MAC_INFO_PATH);
370 ret = __wifi_start(4);
372 WIFI_LOGE("__wifi_start(4) failed");
380 WIFI_LOGE("wifi_stop() failed");
388 int main(int argc, char *argv[])
393 WIFI_LOGE("Usage: wifi <mode>");
397 ret = wifi_check_hw();
401 if (strcmp(argv[1], "start") == 0 || strcmp(argv[1], "p2p") == 0)
403 else if (strcmp(argv[1], "softap") == 0)
405 else if (strcmp(argv[1], "rftest") == 0)
407 else if (strcmp(argv[1], "check_hw") == 0);
408 else if (strcmp(argv[1], "stop") == 0)