Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
Settings.cpp
00001 /* 00002 * Settings.cpp 00003 * 00004 * Created on: 00005 * Author: 00006 */ 00007 00008 #include "Settings.h" 00009 00010 LocalFileSystem local("local"); 00011 00012 char* Settings::m_ipaddress = NULL; 00013 char* Settings::m_netmask = NULL; 00014 char* Settings::m_gateway = NULL; 00015 char* Settings::m_serverurl = NULL; 00016 int Settings::m_dhcp = 0; 00017 int Settings::m_module_number = 0; 00018 int Settings::m_MaxChannels = 0; 00019 int Settings::m_MaxOutlets = 0; 00020 int Settings::m_FreqBase = 0; 00021 int Settings::m_MaxHarmonics = 0; 00022 int Settings::m_Samples = 0; 00023 int Settings::m_EventLimit = 0; 00024 int Settings::m_MBoxLength = 0; 00025 int Settings::m_NumNeighbors = 0; 00026 int Settings::m_PortTCP = 0; 00027 int Settings::m_MaxTries = 0; 00028 int Settings::m_DelayTry = 0; 00029 int Settings::m_DelaySend = 0; 00030 00031 00032 TCPSocketServer Settings::m_ServerSocket; 00033 TCPSocketConnection Settings::m_Socket[NEIGHBORS]; 00034 00035 float Settings::m_gain[NUMBER_OF_CHANNELS]; 00036 int Settings::m_offset[NUMBER_OF_CHANNELS]; 00037 float Settings::m_limit[NUMBER_OF_CHANNELS]; 00038 int Settings::m_outlet_number[NUMBER_OF_CHANNELS]; 00039 char Settings::m_purpose[NUMBER_OF_CHANNELS]; 00040 int Settings::m_outlet[NUMBER_OF_OUTLETS]; 00041 char *Settings::m_Neighbor[NEIGHBORS]; 00042 00043 void Settings::LoadDefaults() 00044 { 00045 set_ServerUrl("192.168.1.26"); 00046 00047 set_IpAddress("192.168.1.100"); 00048 00049 set_Netmask("255.255.255.0"); 00050 set_Gateway("192.168.1.5"); 00051 00052 set_Dhcp(1); 00053 00054 set_ModuleNumber(1); 00055 set_MaxChannels(NUMBER_OF_CHANNELS); 00056 set_MaxOutlets(NUMBER_OF_OUTLETS); 00057 set_FreqBase(60); 00058 set_MaxHarmonics(12); 00059 set_Samples(256); 00060 set_EventLimit(3); 00061 set_MBoxLength(10); 00062 set_NumNeighbors(3); 00063 set_PortTCP(7890); 00064 set_MaxTries(10); 00065 set_DelayTry(500); 00066 set_DelaySend(50); 00067 00068 int i; 00069 00070 for(i=0;i<get_MaxChannels();i++) 00071 { 00072 set_Gain(i,1); 00073 set_Offset(i,2048); 00074 set_Limit(i,2048); 00075 set_OutletNumber(i,i/2); 00076 set_Purpose(i,(i%2)?'d':'p'); 00077 } 00078 for(i=0;i<get_MaxOutlets();i++) 00079 { 00080 set_Outlet(i,i+1); 00081 } 00082 00083 set_Neighbor(0, "192.168.1.6"); 00084 set_Neighbor(1, "192.168.1.7"); 00085 set_Neighbor(2, "192.168.1.8"); 00086 00087 /* 00088 m_ServerSocket.bind(get_PortTCP()); // liga o serversocket a porta 00089 printf("Settings Default: fez bind na porta %d\n", get_PortTCP()); 00090 00091 for(i=0;i<get_NumNeighbors();i++){ 00092 m_Socket[i].connect(get_Neighbor(i), get_PortTCP()); // conecta os sockets de envio aos IPs dos vizinhos 00093 printf("Settings Default: conectou socket com %s:%d\n", get_Neighbor(i), get_PortTCP()); 00094 } 00095 */ 00096 00097 } 00098 00099 void Settings::ReadFile() 00100 { 00101 int i; 00102 FILE *f = fopen(FILENAME,"r"); 00103 00104 if(f == NULL) 00105 { 00106 LoadDefaults(); 00107 WriteFile(); 00108 return; 00109 } 00110 char buf[50]; 00111 while(fgets(buf,50,f)!= NULL) 00112 { 00113 char* p = strchr(buf,'\n'); 00114 if(p) 00115 { 00116 if(isprint(*(p-1)) == 0) *(p-1) = '\0'; 00117 *p = '\0'; 00118 } 00119 char **line; 00120 int l = split(buf,"=",&line); 00121 if(l!=2)continue; 00122 if(!strcmp(line[0],"server")) 00123 { 00124 set_ServerUrl(line[1]); 00125 } 00126 if(!strcmp(line[0],"address")) 00127 { 00128 set_IpAddress(line[1]); 00129 } 00130 if(!strcmp(line[0],"netmask")) 00131 { 00132 set_Netmask(line[1]); 00133 } 00134 if(!strcmp(line[0],"gateway")) 00135 { 00136 set_Gateway(line[1]); 00137 } 00138 if(!strcmp(line[0],"dhcp")) 00139 { 00140 if(!strcmp(line[1],"false")) 00141 set_Dhcp(0); 00142 else 00143 set_Dhcp(1); 00144 } 00145 if(!strcmp(line[0],"module")) 00146 { 00147 set_ModuleNumber(atoi(line[1])); 00148 } 00149 00150 if(!strcmp(line[0],"FreqBase")) 00151 { 00152 set_FreqBase(atoi(line[1])); 00153 } 00154 if(!strcmp(line[0],"MaxChannels")) 00155 { 00156 set_MaxChannels(atoi(line[1])); 00157 } 00158 if(!strcmp(line[0],"MaxOutlets")) 00159 { 00160 set_MaxOutlets(atoi(line[1])); 00161 } 00162 if(!strcmp(line[0],"Samples")) 00163 { 00164 set_Samples(atoi(line[1])); 00165 } 00166 if(!strcmp(line[0],"EventLimit")) 00167 { 00168 set_EventLimit(atoi(line[1])); 00169 } 00170 if(!strcmp(line[0],"MBoxLength")) 00171 { 00172 set_MBoxLength(atoi(line[1])); 00173 } 00174 00175 for(i=0;i<get_MaxChannels();i++) 00176 { 00177 char x[10]; 00178 sprintf(x,"gain%d",i); 00179 if(!strcmp(line[0],x)) 00180 { 00181 set_Gain(i,atof(line[1])); 00182 } 00183 sprintf(x,"offset%d",i); 00184 if(!strcmp(line[0],x)) 00185 { 00186 set_Offset(i,atoi(line[1])); 00187 } 00188 sprintf(x,"limit%d",i); 00189 if(!strcmp(line[0],x)) 00190 { 00191 set_Limit(i,atof(line[1])); 00192 } 00193 sprintf(x,"type%d",i); 00194 if(!strcmp(line[0],x)) 00195 { 00196 set_Purpose(i,line[1][0]); 00197 set_OutletNumber(i,line[1][1]-'0'); 00198 } 00199 } 00200 00201 for(i=0;i<get_MaxOutlets();i++) 00202 { 00203 char x[10]; 00204 sprintf(x,"outlet%d",i); 00205 if(!strcmp(line[0],x)) 00206 { 00207 set_Outlet(i,atoi(line[1])); 00208 } 00209 } 00210 if(!strcmp(line[0],"MaxHarmonics")) 00211 { 00212 set_MaxHarmonics(atoi(line[1])); 00213 } 00214 00215 if(!strcmp(line[0],"NumNeighbors")) 00216 { 00217 set_NumNeighbors(atoi(line[1])); 00218 } 00219 00220 if(!strcmp(line[0],"TcpPort")) 00221 { 00222 set_PortTCP(atoi(line[1])); 00223 //m_ServerSocket.bind(get_PortTCP()); // liga o serversocket a porta 00224 } 00225 00226 for(i=0;i<get_NumNeighbors();i++) 00227 { 00228 char x[15]; 00229 sprintf(x,"Neighbor%d",i); 00230 //printf("Vai buscar %d -> %s\n", i, x); 00231 if(!strcmp(line[0],x)) 00232 { 00233 // printf("Vai usar %d -> %s\n", i, line[1]); 00234 set_Neighbor(i, line[1]) ; 00235 //m_Socket[i].connect(get_Neighbor(i), get_PortTCP()); // conecta os sockets de envio aos IPs dos vizinhos 00236 } 00237 } 00238 00239 if(!strcmp(line[0],"MaxTries")) 00240 { 00241 set_MaxTries(atoi(line[1])); 00242 //m_ServerSocket.bind(get_PortTCP()); // liga o serversocket a porta 00243 } 00244 00245 if(!strcmp(line[0],"DelayTry")) 00246 { 00247 set_DelayTry(atoi(line[1])); 00248 //m_ServerSocket.bind(get_PortTCP()); // liga o serversocket a porta 00249 } 00250 00251 if(!strcmp(line[0],"DelaySend")) 00252 { 00253 set_DelaySend(atoi(line[1])); 00254 //m_ServerSocket.bind(get_PortTCP()); // liga o serversocket a porta 00255 } 00256 00257 //printf("Param=%s Value=%s\r\n",line[0],line[1]); 00258 } 00259 00260 /* 00261 m_ServerSocket.bind(get_PortTCP()); // liga o serversocket a porta 00262 printf("Settings LoadFile: fez bind na porta %d\n", get_PortTCP()); 00263 00264 for(i=0;i<get_NumNeighbors();i++){ 00265 m_Socket[i].connect(get_Neighbor(i), get_PortTCP()); // conecta os sockets de envio aos IPs dos vizinhos 00266 printf("Settings LoadFile: conectou socket com %s:%d\n", get_Neighbor(i), get_PortTCP()); 00267 } 00268 */ 00269 00270 fclose(f); 00271 } 00272 00273 00274 void Settings::WriteFile() 00275 { 00276 FILE *f = fopen(FILENAME,"w"); 00277 int i; 00278 00279 if(f == NULL) 00280 { 00281 printf("Error creating settings file\r\n"); 00282 return; 00283 } 00284 00285 fprintf(f,"server=%s\r\n",get_ServerUrl()); 00286 fprintf(f,"address=%s\r\n",get_IpAddress()); 00287 fprintf(f,"netmask=%s\r\n",get_Netmask()); 00288 fprintf(f,"gateway=%s\r\n",get_Gateway()); 00289 00290 if(get_Dhcp()) 00291 fprintf(f,"dhcp=true\r\n"); 00292 else 00293 fprintf(f,"dhcp=false\r\n"); 00294 00295 fprintf(f,"module=%d\r\n",get_ModuleNumber()); 00296 fprintf(f,"MaxChannels=%d\r\n",get_MaxChannels()); 00297 fprintf(f,"MaxOutlets=%d\r\n",get_MaxOutlets()); 00298 fprintf(f,"FreqBase=%d\r\n",get_FreqBase()); 00299 fprintf(f,"Samples=%d\r\n",get_Samples()); 00300 fprintf(f,"EventLimit=%d\r\n",get_EventLimit()); 00301 fprintf(f,"MBoxLength=%d\r\n",get_MBoxLength()); 00302 00303 for(i=0;i<get_MaxChannels();i++) 00304 { 00305 fprintf(f,"gain%d=%0.4f\r\n",i,get_Gain(i)); 00306 fprintf(f,"offset%d=%d\r\n",i,get_Offset(i)); 00307 fprintf(f,"limit%d=%0.4f\r\n",i,get_Limit(i)); 00308 fprintf(f,"type%d=%c%d\r\n",i,get_Purpose(i),get_OutletNumber(i)); 00309 } 00310 00311 for(i=0;i<get_MaxOutlets();i++) 00312 { 00313 fprintf(f,"outlet%d=%d\r\n",i,get_Outlet(i)); 00314 } 00315 fprintf(f,"MaxHarmonics=%d\r\n",get_MaxHarmonics()); 00316 00317 fprintf(f,"NumNeighbors=%d\r\n",get_NumNeighbors()); 00318 fprintf(f,"TcpPort=%d\r\n",get_PortTCP()); 00319 for(i=0;i<get_NumNeighbors();i++) 00320 { 00321 fprintf(f,"Neighbor%d=%s\r\n",i,get_Neighbor(i)); 00322 } 00323 fprintf(f,"MaxTries=%d\r\n",get_MaxTries()); 00324 fprintf(f,"DelayTry=%d\r\n",get_DelayTry()); 00325 fprintf(f,"DelaySend=%d\r\n",get_DelaySend()); 00326 00327 fclose(f); 00328 } 00329 00330 void Settings::ShowValues() 00331 { 00332 printf("ServerUrl: %s\n", get_ServerUrl()); 00333 printf("IpAddress: %s\n", get_IpAddress()); 00334 printf("NetMask: %s\n", get_Netmask()); 00335 printf("Gateway: %s\n", get_Gateway()); 00336 printf("Dhcp: %d\n", get_Dhcp()); 00337 printf("ModuleNumber: %d\n", get_ModuleNumber() ); 00338 printf("FreqBase : %d\n", get_FreqBase() ); 00339 printf("Samples : %d\n" , get_Samples() ); 00340 printf("EventLimit : %d\n" , get_EventLimit() ); 00341 printf("MBoxLength : %d\n" , get_MBoxLength() ); 00342 printf("MaxChannels : %d\n", get_MaxChannels() ); 00343 printf("MaxOutlets : %d\n", get_MaxOutlets() ); 00344 printf("Per Channel\n"); 00345 int i; 00346 for(i=0;i<get_MaxChannels();i++) 00347 { 00348 printf("Channel %d Gain %f Offset %d Limmit %f Outlet %d Purpose %c\n ", i, get_Gain(i), get_Offset(i), get_Limit(i), get_OutletNumber(i), get_Purpose(i)); 00349 } 00350 printf("Per Outlet \n"); 00351 for(i=0;i<get_MaxOutlets();i++) 00352 { 00353 printf("Outlet %d Number %d \n ", i, get_Outlet(i)); 00354 } 00355 printf("MaxHarmonics : %d\n", get_MaxHarmonics() ); 00356 00357 printf("NumNeighbors : %d\n", get_NumNeighbors() ); 00358 for(i=0;i<get_NumNeighbors();i++) 00359 { 00360 printf("Neighbor %d Value %s \n ", i, get_Neighbor(i)); 00361 } 00362 printf("TcpPort : %d\n", get_PortTCP() ); 00363 printf("MaxTries : %d\n", get_MaxTries() ); 00364 printf("DelayTry : %d\n", get_DelayTry() ); 00365 printf("DelaySend : %d\n", get_DelaySend() ); 00366 }
Generated on Fri Jul 15 2022 22:11:24 by
1.7.2