1 #include <QApplication>
2 #include <QDebug>
3 #include <QDesktopWidget>
4 #include <QListWidgetItem>
5 #include <QProcess>
6 #include <QTextStream>
7 #include "qtkeyboard.h"
8 #include "qtinputdialog.h"
9 #include "qtwifi.h"
10 #include "Rk_softap.h"
11 #include "Rk_softap.h"
12 qtWifi* qtWifi::_instance = nullptr;
13
qtWifi(QWidget * parent,QLabel * label,QPushButton * btn,bool on)14 qtWifi::qtWifi(QWidget *parent, QLabel *label, QPushButton *btn, bool on)
15 {
16 const QRect availableGeometry = QApplication::desktop()->availableGeometry(parent);
17 resize(availableGeometry.width(), availableGeometry.height());
18
19 QFont font;
20 font.setPixelSize(availableGeometry.height()/20);
21
22 if(btn){
23 switchBtn = btn;
24 switchBtn->setCheckable(true);
25 switchBtn->setVisible(true);
26 switchBtn->setStyleSheet("QPushButton{background-color:green;}");
27 switchBtn->setStyleSheet("QPushButton:checked{background-color:red;}");
28 if (on){
29 switchBtn->setChecked(true);
30 switchBtn->setText("on");
31 } else {
32 switchBtn->setChecked(false);
33 switchBtn->setText("off");
34 }
35 connect(switchBtn, SIGNAL(clicked(bool)), this, SLOT(on_btnClicked()));
36 }
37
38 if(label){
39 text = label;
40 text->setText("");
41 text->setVisible(true);
42 }else {
43 text = nullptr;
44 }
45 setObjectName("WiFi");
46 setFont(font);
47
48 Timer = new QTimer(this);
49
50 connect(this, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(on_itemClicked(QListWidgetItem *)));
51 show();
52 if (on)
53 turnOn();
54 }
55
~qtWifi()56 qtWifi::~qtWifi()
57 {
58 if (switchBtn)
59 switchBtn->setVisible(false);
60
61 if (text)
62 text->setVisible(false);
63 _instance = nullptr;
64 }
65
turnOn()66 void qtWifi::turnOn()
67 {
68 RK_wifi_register_callback(wifi_callback);
69 if (RK_wifi_enable(1) < 0)
70 printf("[%s] Rk_wifi_enable 1 fail!\n", __func__);
71 Timer->stop();
72 Timer->setInterval(10000);
73 connect(Timer, SIGNAL(timeout()), this, SLOT(on_timer_timeout()));
74
75 if (text)
76 text->setVisible(true);
77
78 Timer->start();
79 text->setText("Scaning");
80 }
81
turnOff()82 void qtWifi::turnOff()
83 {
84 if (RK_wifi_enable(0) < 0)
85 printf("RK_wifi_enable 0 fail!\n");
86 Timer->stop();
87 clear();
88 if (text)
89 text->setVisible(false);
90 }
91
search_for_ssid(const char * str)92 static int search_for_ssid(const char *str)
93 {
94 const char key[] = "\"ssid\"";
95 int i;
96
97 if (strlen(str) < strlen(key))
98 return -1;
99
100 for (i = 0; i < (strlen(str) - strlen(key)); i++) {
101 if (!strncmp(key, &str[i], strlen(key)))
102 return i;
103 }
104 return -1;
105 }
106
get_string(const char * str)107 static char *get_string(const char *str)
108 {
109 int i, begin = -1, count;
110 char *dst;
111
112 for (i = 0; i < strlen(str); i++) {
113 if (str[i] == '\"') {
114 if (begin == -1) {
115 begin = i;
116 continue;
117 } else {
118 count = i - begin -1;
119 if (!count)
120 return NULL;
121 dst = strndup(&str[begin + 1], count);
122 return dst;
123 }
124 }
125 }
126 return NULL;
127 }
128
wifi_callback(RK_WIFI_RUNNING_State_e state,RK_WIFI_INFO_Connection_s * info)129 int qtWifi::wifi_callback(RK_WIFI_RUNNING_State_e state,
130 RK_WIFI_INFO_Connection_s *info)
131 {
132 qtWifi *wifi = qtWifi::getInstance();
133
134 if (state == RK_WIFI_State_CONNECTED) {
135 printf("RK_WIFI_State_CONNECTED\n");
136 //wifi->ssid = QLatin1String(info->ssid);
137 wifi->ssid = QString(info->ssid);
138 wifi->text->setText(wifi->ssid + " Connected");
139 } else if (state == RK_WIFI_State_CONNECTFAILED) {
140 printf("RK_WIFI_State_CONNECTFAILED\n");
141 } else if (state == RK_WIFI_State_CONNECTFAILED_WRONG_KEY) {
142 printf("RK_WIFI_State_CONNECTFAILED_WRONG_KEY\n");
143 } else if (state == RK_WIFI_State_OPEN) {
144 printf("RK_WIFI_State_OPEN\n");
145 } else if (state == RK_WIFI_State_OFF) {
146 printf("RK_WIFI_State_OFF\n");
147 } else if (state == RK_WIFI_State_DISCONNECTED) {
148 printf("RK_WIFI_State_DISCONNECTED\n");
149 wifi->text->setText("Scaning");
150 } else if (state == RK_WIFI_State_SCAN_RESULTS) {
151 char *scan_r, *str = nullptr;
152 int cnt = 0, tmp = 0;
153 QString line;
154 QStringList list;
155
156 if (wifi == nullptr)
157 return 0;
158 scan_r = strdup(RK_wifi_scan_r());
159 wifi->clear();
160 while (1) {
161 tmp = search_for_ssid(&scan_r[cnt]);
162 if (tmp == -1)
163 break;
164 str = get_string(&scan_r[cnt + tmp + 6]);
165 if (str == NULL) {
166 line = QString("NULL");
167 } else {
168 line = QString(str);
169 free(str);
170 }
171 list.removeAll("NULL");
172 list << line;
173 cnt += tmp + 6;
174 }
175 wifi->addItems(list);
176 free(scan_r);
177 }
178 return 0;
179 }
180
isOn()181 bool qtWifi::isOn()
182 {
183 if(switchBtn){
184 if (! switchBtn->text().compare("on")){
185 return true;
186 } else {
187 return false;
188 }
189 }
190 return false;
191 }
192
on_itemClicked(QListWidgetItem * item)193 void qtWifi::on_itemClicked(QListWidgetItem *item)
194 {
195 QKeyBoard::getInstance();
196 inputDialog *dialog = inputDialog::getInstance(this);
197 const char *c_ssid, *pswd;
198
199 ssid = item->text();
200 dialog->setText("Connect", "Cancel", "Password of " + item->text());
201 if (dialog->isRunning())
202 dialog->exit(false);
203
204 int result = dialog->exec();
205 if(result){
206 QString str = dialog->getEditText();
207 QProcess p;
208 QStringList arguments;
209
210 std::string s_ssid = ssid.toStdString();
211 c_ssid = s_ssid.c_str();
212
213 std::string s_pswd = str.toStdString();
214 pswd = s_pswd.c_str();
215
216 printf("ssid: %s, %s\n", c_ssid, pswd);
217 if (RK_wifi_connect((char *)c_ssid, pswd) < 0)
218 printf("RK_wifi_connect1 fail!\n");
219 }
220 }
221
on_btnClicked()222 void qtWifi::on_btnClicked()
223 {
224 if(switchBtn){
225 if (! switchBtn->text().compare("on")){
226 switchBtn->setText("off");
227 turnOff();
228 } else if (! switchBtn->text().compare("off")){
229 switchBtn->setText("on");
230 turnOn();
231 }
232 }
233 }
234
on_timer_timeout()235 void qtWifi::on_timer_timeout()
236 {
237 printf("refresh\n");
238 if (RK_wifi_scan() < 0)
239 printf("RK_wifi_scan fail!\n");
240 Timer->start();
241 }
242