MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / wireless / rtlink / Utility / raconfig.cpp
blob03b6ed69297b4b1bbb2f3df46e7f76516cae8a4b
1 #include <qapplication.h>
3 #include <qmessagebox.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/socket.h>
8 #include <sys/ioctl.h>
9 #include <unistd.h>
10 #include <linux/wireless.h>
11 #include <asm/errno.h>
12 #include <fcntl.h>
13 #include <sys/stat.h> /* for mode definitions */
14 #include <signal.h> /* for disable CTRL-C */
15 #include <ctype.h>
16 #include <dirent.h>
17 #include <errno.h>
18 #include "raconfigui.h"
19 #include "countryform.h"
20 #include "cardselect.h"
21 #include "configapi.h"
22 #include "rt_tool.h"
24 #define LOCK_FILE "/tmp/.RaConfig25STA.lck"
26 PRT_PROFILE_SETTING RTMPProfile=NULL;
27 PRT_DEVICE_ADAPTER pDevice_Adpater=NULL;
28 bool G_bUserAdmin = FALSE;
29 bool G_bSupportAMode = FALSE;
30 extern int errno;
31 char Configfile_name[255];
32 UINT G_nCountryRegion = REGSTR_COUNTRYREGION_FCC;
35 int m_open_cards(int socket_id)
37 int i;
38 char name[25];
39 char Device_Name[255];
40 int count=0;
41 PRT_DEVICE_ADAPTER prtAdapter=NULL;
43 for (i=0; i<8; i++)
45 sprintf(name, "ra%d", i);
46 memset(Device_Name, 0x00, 255);
47 if( OidQueryInformation(RT_OID_DEVICE_NAME, socket_id, name, Device_Name, 255) != 0)
48 continue;
50 if(memcmp(Device_Name, NIC_DEVICE_NAME, sizeof(NIC_DEVICE_NAME)) == 0)
52 count++;
53 if(!pDevice_Adpater)
55 pDevice_Adpater = (PRT_DEVICE_ADAPTER) malloc(sizeof(RT_DEVICE_ADAPTER));
56 if(pDevice_Adpater)
58 pDevice_Adpater->Device_Name = (char *)malloc(strlen(name)+1);
59 if(!pDevice_Adpater->Device_Name)
60 return (-1); // Not enough memory
61 strcpy(pDevice_Adpater->Device_Name, name);
62 pDevice_Adpater->Next = NULL;
63 prtAdapter = pDevice_Adpater;
65 else
66 return (-1); // Not enough memory
68 else
70 prtAdapter->Next = (PRT_DEVICE_ADAPTER) malloc(sizeof(RT_DEVICE_ADAPTER));
71 if(prtAdapter->Next)
73 prtAdapter = prtAdapter->Next;
74 prtAdapter->Device_Name = (char *)malloc(strlen(name)+1);
75 if(!prtAdapter->Device_Name)
76 return (-1); // Not enough memory
77 strcpy(prtAdapter->Device_Name, name);
78 prtAdapter->Next = NULL;
80 else
81 return (-1); // Not enough memory
86 return count;
89 UCHAR Get_CountryRegion()
91 char tmpCountryRegion[10];
92 int getCountryFlag=FALSE;
93 int country=0;
95 if(getkeystring("Default", "CountryRegion", tmpCountryRegion, 10, RT2500_SYSTEM_PATH, RT2500_SYSTEM_DATA) == TRUE)
97 country = atoi(tmpCountryRegion);
98 if ((country >= REGSTR_COUNTRYREGION_MINIMUM) && (country <= REGSTR_COUNTRYREGION_MAXIMUM))
99 getCountryFlag = TRUE;
102 if( !getCountryFlag )
104 CountryForm *ctyForm = new CountryForm;
105 ctyForm->show();
106 if(ctyForm->exec() == QDialog::Accepted)
108 country = ctyForm->GetRegionID();
109 sprintf(tmpCountryRegion, "%d", country);
110 if (G_bUserAdmin)
111 writekeystring("Default", "CountryRegion", tmpCountryRegion, RT2500_SYSTEM_PATH, RT2500_SYSTEM_DATA);
113 else
115 QMessageBox *mb = new QMessageBox();
116 mb->setCaption("Information");
117 mb->setText("This message dialog will pop up again next time, if no country region is selected.");
118 mb->setIcon(QMessageBox::Information);
119 mb->setButtonText(0, "&Ok");
120 mb->exec();
121 delete mb;
125 return ((UCHAR)country);
128 int main( int argc, char ** argv )
130 int fd;
131 char buffer[255];
132 int pid;
133 FILE *fileLCK;
134 int socket_id;
135 int adapter_num;
136 int isRuning=FALSE;
137 DIR *dirptr;
138 RaConfigForm *w;
140 signal( SIGINT, SIG_IGN ); /* disable CTRL-C */
141 QApplication App( argc, argv );
143 if(getuid() != 0)
145 G_bUserAdmin = FALSE;
146 QMessageBox *mb = new QMessageBox();
147 mb->setCaption("Information");
148 mb->setText("You do not have supper user's privilege!");
149 mb->setIcon(QMessageBox::Information);
150 mb->setButtonText(0, "&Ok");
151 mb->exec();
152 delete mb;
154 else
155 G_bUserAdmin = TRUE;
157 fd=open(LOCK_FILE, O_RDWR | O_CREAT | O_EXCL);
158 if (fd<0 && errno==EEXIST)
160 if((fileLCK=fopen(LOCK_FILE, "rb")) != NULL)
162 fgets(buffer, 255, fileLCK);
163 sscanf(buffer, "%d", &pid);
164 fclose(fileLCK);
165 sprintf(buffer, "/proc/%d", pid);
166 dirptr = opendir(buffer);
167 if(!dirptr)
169 isRuning = FALSE;
170 if((fileLCK=fopen(LOCK_FILE, "wb")) != NULL)
172 fprintf(fileLCK, "%d", getpid());
173 fclose(fileLCK);
176 else
178 isRuning = TRUE;
182 else if(fd > 0)
184 isRuning = FALSE;
185 system("chmod a+rw " LOCK_FILE);
186 system("chmod u-s " LOCK_FILE); //remove -rwsrw-rw- *s*
187 sprintf(buffer, "%d", getpid());
188 write(fd, buffer, strlen(buffer));
190 close(fd);
192 if(isRuning)
194 QMessageBox *mb = new QMessageBox();
195 mb->setCaption("Warning");
196 sprintf(buffer, "There is an other program running, please close it and try again!\nPID=%d", pid);
197 mb->setText(buffer);
198 mb->setIcon(QMessageBox::Critical);
199 mb->setButtonText(0, "&Ok");
200 mb->exec();
201 delete mb;
202 return -1;
205 if((socket_id = Open_Socket()) < 0)
207 QMessageBox *mb = new QMessageBox();
208 mb->setCaption("Warning");
209 mb->setText("Open socket error!");
210 mb->setIcon(QMessageBox::Critical);
211 mb->setButtonText(0, "&Exit");
212 mb->exec();
213 delete mb;
214 return -1;
217 adapter_num = m_open_cards(socket_id);
218 if( adapter_num < 0)
219 { //Not enough memory
220 QMessageBox *mb = new QMessageBox();
221 mb->setCaption("Error");
222 mb->setText("Device driver not found!");
223 mb->setIcon(QMessageBox::Critical);
224 mb->setButtonText(0, "&Ok");
225 mb->exec();
226 delete mb;
227 unlink(LOCK_FILE);
228 return -1;
230 else if( adapter_num ==0)
231 {//not found.
232 QMessageBox *mb = new QMessageBox();
233 mb->setCaption("Error");
234 mb->setText("Device driver not found!");
235 mb->setIcon(QMessageBox::Critical);
236 mb->setButtonText(0, "&Ok");
237 mb->exec();
238 delete mb;
239 unlink(LOCK_FILE);
240 return -1;
242 else if (adapter_num == 1)
244 if (G_bUserAdmin)
246 G_nCountryRegion = Get_CountryRegion();
247 OidSetInformation(RT_OID_802_11_COUNTRY_REGION, socket_id, (char *)pDevice_Adpater->Device_Name, &G_nCountryRegion, sizeof(G_nCountryRegion));
250 w = new RaConfigForm(socket_id, pDevice_Adpater->Device_Name);
251 w->resize( 600, 480 );
252 App.setMainWidget(w);
254 else
256 CardSelect *cardSel = new CardSelect(socket_id, pDevice_Adpater);
257 cardSel->show();
258 cardSel->exec();
260 if (!cardSel->isClickOk())
262 unlink(LOCK_FILE);
263 return 0;
266 if (G_bUserAdmin)
268 G_nCountryRegion = Get_CountryRegion();
269 OidSetInformation(RT_OID_802_11_COUNTRY_REGION, socket_id, (char *)cardSel->Get_Device_Name(), &G_nCountryRegion, sizeof(G_nCountryRegion));
272 w = new RaConfigForm(socket_id, cardSel->Get_Device_Name());
273 w->resize( 600, 480 );
274 App.setMainWidget(w);
277 w->show();
278 App.connect( &App, SIGNAL( lastWindowClosed() ), &App, SLOT( quit() ) );
279 App.exec();
280 unlink(LOCK_FILE);
281 return 0;