00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <cstdlib>
00025 #include <iostream>
00026 #include <fstream>
00027 #include <map>
00028 #include <string>
00029 #include <string.h>
00030 #include <sys/socket.h>
00031 #include <netinet/in.h>
00032 #include <arpa/inet.h>
00033
00034 #include "../../../../fecom-lib/include/fecom.h"
00035
00036 #include "InterfaceConfig.h"
00037
00038 using namespace std;
00039
00040
00041
00042
00043
00044 InterfaceConfig::InterfaceConfig()
00045 {
00046 m_iError = 0;
00047 m_ifSettings.init();
00048 }
00049
00050
00051
00052
00053
00054 InterfaceConfig::~InterfaceConfig()
00055 {
00056 }
00057
00058
00059
00060
00061
00062 void InterfaceConfig::init()
00063 {
00064 m_ifSettings.init();
00065 }
00066
00067
00068
00069
00070
00071 int InterfaceConfig::checkIfIPAddress(char *ip, int len)
00072 {
00073
00074 for (int i=0; i<len; i++)
00075 {
00076 if (ip[i] == '.')
00077 continue;
00078 if (!isdigit(ip[i]))
00079 return -1;
00080 }
00081
00082 in_addr a;
00083 int t = inet_aton(ip, &a);
00084 if (t == 0)
00085 return -1;
00086
00087 return 0;
00088 }
00089
00090
00091
00092
00093
00094 int InterfaceConfig::writeConfigToFile(char* fileName)
00095 {
00096 map<string, string> values;
00097 string var;
00098 char value[32] = {0};
00099 int ret = 0;
00100
00101 sprintf(value, "%d", m_ifSettings.iCommMode);
00102 values["CommMode"] = value;
00103
00104 values["Host"] = m_ifSettings.cHost;
00105
00106 sprintf(value, "%d", m_ifSettings.iIpPort);
00107 values["IpPort"] = value;
00108
00109 sprintf(value, "%d", m_ifSettings.iComPortNumber);
00110 values["ComPortNumber"] = value;
00111
00112 sprintf(value, "%d", m_ifSettings.iBaud);
00113 values["Baud"] = value;
00114
00115 sprintf(value, "%d", m_ifSettings.iBusAddress);
00116 values["BusAddress"] = value;
00117
00118 values["Frame"] = m_ifSettings.cFrame;
00119
00120 sprintf(value, "%d", m_ifSettings.iTimeout);
00121 values["Timeout"] = value;
00122
00123 values["TxTimeControl"] = m_ifSettings.bTxTimeControl == true ? "1" : "0";
00124
00125 sprintf(value, "%d", m_ifSettings.iTxDelayTime);
00126 values["TxDelayTime"] = value;
00127
00128 sprintf(value, "%d", m_ifSettings.iCharTimeoutMpy);
00129 values["CharTimeoutMpy"] = value;
00130
00131 values["Rts"] = m_ifSettings.bRts == true ? "1" : "0";
00132 values["Dtr"] = m_ifSettings.bDtr == true ? "1" : "0";
00133
00134 ofstream cfgFile;
00135 cfgFile.open (fileName);
00136 if (cfgFile.is_open())
00137 {
00138 map<string, string>::const_iterator iter;
00139 for (iter=values.begin(); iter != values.end(); ++iter)
00140 {
00141 char configVar[32];
00142 char configValue[32];
00143
00144 strcpy(configVar, iter->first.data());
00145 strcpy(configValue, iter->second.data());
00146
00147 cfgFile << configVar << " " << configValue << endl;
00148 }
00149 cfgFile.close();
00150 }
00151 else
00152 ret = -1;
00153
00154 return ret;
00155 }
00156
00157
00158
00159
00160
00161 int InterfaceConfig::readConfigFromFile(char* fileName)
00162 {
00163 char configVar[64] = {0};
00164 char configValue[64] = {0};
00165 int ret = 0;
00166
00167 m_ifSettings.init();
00168
00169 ifstream cfgFile;
00170 cfgFile.open (fileName);
00171 if (cfgFile.is_open())
00172 {
00173 int wordCount = 0;
00174 while (cfgFile.good())
00175 {
00176
00177 if (wordCount % 2 == 0)
00178 {
00179 wordCount++;
00180 cfgFile >> configVar;
00181 continue;
00182 }
00183 else
00184 cfgFile >> configValue;
00185
00186 wordCount++;
00187
00188 if (strcmp(configVar, "CommMode") == 0)
00189 {
00190 int mode;
00191 mode = atoi(configValue);
00192 if (mode == 0 || mode == 1 || mode == 2)
00193 m_ifSettings.iCommMode = mode;
00194 }
00195 else if (strcmp(configVar, "Host") == 0)
00196 {
00197 if (checkIfIPAddress(configValue, strlen(configValue)) == 0)
00198 strcpy(m_ifSettings.cHost, configValue);
00199 }
00200 else if (strcmp(configVar, "IpPort") == 0)
00201 {
00202 int port;
00203 port = atoi(configValue);
00204 if (port >= 0)
00205 m_ifSettings.iIpPort = port;
00206 }
00207 else if (strcmp(configVar, "ComPortNumber") == 0)
00208 {
00209 int comPort;
00210 comPort = atoi(configValue);
00211 if (comPort >= 1 && comPort <= 256)
00212 m_ifSettings.iComPortNumber = comPort;
00213 }
00214 else if (strcmp(configVar, "Baud") == 0)
00215 {
00216 int baud;
00217 baud = atoi(configValue);
00218 if (baud > 0)
00219 m_ifSettings.iBaud = baud;
00220 }
00221 else if (strcmp(configVar, "BusAddress") == 0)
00222 {
00223 int busAddress;
00224 busAddress = atoi(configValue);
00225 if (busAddress > 0 && busAddress < 256)
00226 m_ifSettings.iBusAddress = busAddress;
00227 }
00228 else if (strcmp(configVar, "Frame") == 0)
00229 {
00230 strcpy(m_ifSettings.cFrame, configValue);
00231 }
00232 else if (strcmp(configVar, "Timeout") == 0)
00233 {
00234 int timeout;
00235 timeout = atoi(configValue);
00236 if (timeout >= 0)
00237 m_ifSettings.iTimeout = timeout;
00238 }
00239 else if (strcmp(configVar, "TxTimeControl") == 0)
00240 {
00241 int timeCtrl;
00242 timeCtrl = atoi(configValue);
00243 if (timeCtrl == 1)
00244 m_ifSettings.bTxTimeControl = true;
00245 else
00246 m_ifSettings.bTxTimeControl = false;
00247 }
00248 else if (strcmp(configVar, "CharTimeoutMpy") == 0)
00249 {
00250 int timeoutMpy;
00251 timeoutMpy = atoi(configValue);
00252 if (timeoutMpy >= 0)
00253 m_ifSettings.iCharTimeoutMpy = timeoutMpy;
00254 }
00255 else if (strcmp(configVar, "Rts") == 0)
00256 {
00257 int rts;
00258 rts = atoi(configValue);
00259 if (rts == 1)
00260 m_ifSettings.bRts = true;
00261 else
00262 m_ifSettings.bRts = false;
00263 }
00264 else if (strcmp(configVar, "Dtr") == 0)
00265 {
00266 int dtr;
00267 dtr = atoi(configValue);
00268 if (dtr == 1)
00269 m_ifSettings.bDtr = true;
00270 else
00271 m_ifSettings.bDtr = false;
00272 }
00273 }
00274 cfgFile.close();
00275 }
00276 else
00277 ret = -1;
00278
00279 return ret;
00280 }
00281
00282
00283
00284
00285
00286 int InterfaceConfig::getComPorts(int* comPorts)
00287 {
00288 int count = 0;
00289
00290 for (int port = 1; port <= 256; port++)
00291 {
00292 if (0 == FECOM_DetectPort(port))
00293 {
00294 comPorts[count] = port;
00295 count++;
00296 }
00297 }
00298
00299 return count;
00300 }
00301
00302
00303
00304
00305
00306 int InterfaceConfig::setComPortConfiguration(int portHandle)
00307 {
00308 char str[256];
00309
00310 sprintf(str, "%d", m_ifSettings.iBaud);
00311 m_iError = FECOM_SetPortPara(portHandle, "Baud", str);
00312 if (m_iError < 0)
00313 return m_iError;
00314
00315 m_iError = FECOM_SetPortPara(portHandle, "Frame", m_ifSettings.cFrame);
00316 if (m_iError < 0)
00317 return m_iError;
00318
00319 sprintf(str, "%d", m_ifSettings.iTimeout);
00320 m_iError = FECOM_SetPortPara(portHandle, "Timeout", str);
00321 if (m_iError < 0)
00322 return m_iError;
00323
00324 if (m_ifSettings.bTxTimeControl) {
00325 m_iError = FECOM_SetPortPara(portHandle, "TxTimeControl", "1");
00326 }
00327 else {
00328 m_iError = FECOM_SetPortPara(portHandle, "TxTimeControl", "0");
00329 }
00330 if (m_iError < 0)
00331 return m_iError;
00332
00333 if (m_ifSettings.bDtr) {
00334 m_iError = FECOM_DoPortCmd(portHandle, "SetDTR", "ON");
00335 }
00336 else {
00337 m_iError = FECOM_DoPortCmd(portHandle, "SetDTR", "OFF");
00338 }
00339 if (m_iError < 0)
00340 return m_iError;
00341
00342 if (m_ifSettings.bRts) {
00343 m_iError = FECOM_DoPortCmd(portHandle, "SetRTS", "ON");
00344 }
00345 else {
00346 m_iError = FECOM_DoPortCmd(portHandle, "SetRTS", "OFF");
00347 }
00348 if (m_iError < 0)
00349 return m_iError;
00350
00351 sprintf(str, "%d", m_ifSettings.iTxDelayTime);
00352 m_iError = FECOM_SetPortPara(portHandle, "TxDelayTime", str);
00353 if (m_iError < 0)
00354 return m_iError;
00355
00356 sprintf(str, "%d", m_ifSettings.iCharTimeoutMpy);
00357 m_iError = FECOM_SetPortPara(portHandle, "CharTimeoutMpy", str);
00358 if (m_iError < 0)
00359 return m_iError;
00360
00361 return 0;
00362 }
00363
00364
00365
00366
00367
00368 int InterfaceConfig::getComPortConfiguration(int portHandle)
00369 {
00370 char str[256];
00371
00372 m_iError = FECOM_GetPortPara(portHandle, "PortNr", str);
00373 if (m_iError < 0)
00374 return m_iError;
00375 m_ifSettings.iComPortNumber = atoi(str);
00376
00377 m_iError = FECOM_GetPortPara(portHandle, "Baud", str);
00378 if (m_iError < 0)
00379 return m_iError;
00380 m_ifSettings.iBaud = atoi(str);
00381
00382 m_iError = FECOM_GetPortPara(portHandle, "Frame", str);
00383 if (m_iError < 0)
00384 return m_iError;
00385 strcpy(m_ifSettings.cFrame, str);
00386
00387 m_iError = FECOM_GetPortPara(portHandle, "Timeout", str);
00388 if (m_iError < 0)
00389 return m_iError;
00390 m_ifSettings.iTimeout = atoi(str);
00391
00392 m_iError = FECOM_GetPortPara(portHandle, "TxTimeControl", str);
00393 if (m_iError < 0)
00394 return m_iError;
00395 if (strcmp(str, "1") == 0) {
00396 m_ifSettings.bTxTimeControl = true;
00397 }
00398 else {
00399 m_ifSettings.bTxTimeControl = false;
00400 }
00401
00402 m_iError = FECOM_GetPortPara(portHandle, "GetDTR", str);
00403 if (m_iError < 0)
00404 return m_iError;
00405 if (strcmp(str, "ON")==0) {
00406 m_ifSettings.bDtr = true;
00407 }
00408 else {
00409 m_ifSettings.bDtr = false;
00410 }
00411
00412 m_iError = FECOM_GetPortPara(portHandle, "GetRTS", str);
00413 if (m_iError < 0)
00414 return m_iError;
00415 if (strcmp(str, "ON")== 0) {
00416 m_ifSettings.bRts = true;
00417 }
00418 else {
00419 m_ifSettings.bRts = false;
00420 }
00421
00422 m_iError = FECOM_GetPortPara(portHandle, "TxDelayTime", str);
00423 if (m_iError < 0)
00424 return m_iError;
00425 m_ifSettings.iTxDelayTime = atoi(str);
00426
00427 m_iError = FECOM_GetPortPara(portHandle, "CharTimeoutMpy", str);
00428 if (m_iError < 0)
00429 return m_iError;
00430 m_ifSettings.iCharTimeoutMpy = atoi(str);
00431
00432 return 0;
00433 }
00434
00435
00436
00437
00438
00439 int InterfaceConfig::getLastError()
00440 {
00441 return m_iError;
00442 }
00443
00444
00445
00446
00447
00448 void InterfaceConfig::setCommMode(int iCommMode)
00449 {
00450 m_ifSettings.iCommMode = iCommMode;
00451 }
00452
00453
00454
00455
00456
00457 int InterfaceConfig::getCommMode()
00458 {
00459 return m_ifSettings.iCommMode;
00460 }
00461
00462
00463
00464
00465
00466 void InterfaceConfig::setHost(char* host)
00467 {
00468 if (checkIfIPAddress(host, strlen(host)) == 0)
00469 {
00470 memset(m_ifSettings.cHost, 0, sizeof(m_ifSettings.cHost));
00471 strcpy(m_ifSettings.cHost, host);
00472 }
00473 }
00474
00475
00476
00477
00478
00479 char* InterfaceConfig::getHost()
00480 {
00481 return m_ifSettings.cHost;
00482 }
00483
00484
00485
00486
00487
00488 void InterfaceConfig::setIPPort(int ipPort)
00489 {
00490 if (ipPort > 65536 || ipPort < 0)
00491 return;
00492 m_ifSettings.iIpPort = ipPort;
00493 }
00494
00495
00496
00497
00498
00499 int InterfaceConfig::getIPPort()
00500 {
00501 return m_ifSettings.iIpPort;
00502 }
00503
00504
00505
00506
00507
00508 void InterfaceConfig::setComPortNumber(int comPortNumber)
00509 {
00510 m_ifSettings.iComPortNumber = comPortNumber;
00511 }
00512
00513
00514
00515
00516
00517 int InterfaceConfig::getComPortNumber()
00518 {
00519 return m_ifSettings.iComPortNumber;
00520 }
00521
00522
00523
00524
00525
00526 void InterfaceConfig::setBaud(int baud)
00527 {
00528 m_ifSettings.iBaud = baud;
00529 }
00530
00531
00532
00533
00534
00535 int InterfaceConfig::getBaud()
00536 {
00537 return m_ifSettings.iBaud;
00538 }
00539
00540
00541
00542
00543
00544 void InterfaceConfig::setBusAddress(int address)
00545 {
00546 m_ifSettings.iBusAddress = address;
00547 }
00548
00549
00550
00551
00552
00553 int InterfaceConfig::getBusAddress()
00554 {
00555 return m_ifSettings.iBusAddress;
00556 }
00557
00558
00559
00560
00561
00562 void InterfaceConfig::setFrame(char* frame)
00563 {
00564 if (strlen(frame) > 0)
00565 {
00566 memset(m_ifSettings.cFrame, 0, sizeof(m_ifSettings.cFrame));
00567 strcpy(m_ifSettings.cFrame, frame);
00568 }
00569 }
00570
00571
00572
00573
00574
00575 char* InterfaceConfig::getFrame()
00576 {
00577 return (char*)m_ifSettings.cFrame;
00578 }
00579
00580
00581
00582
00583
00584 void InterfaceConfig::setTimeout(int timeout)
00585 {
00586 m_ifSettings.iTimeout = timeout;
00587 }
00588
00589
00590
00591
00592
00593 int InterfaceConfig::getTimeout()
00594 {
00595 return m_ifSettings.iTimeout;
00596 }
00597
00598
00599
00600
00601
00602 void InterfaceConfig::setTxTimeControl(bool txTimeControl)
00603 {
00604 m_ifSettings.bTxTimeControl = txTimeControl;
00605 }
00606
00607
00608
00609
00610
00611 bool InterfaceConfig::getTxTimeControl()
00612 {
00613 return m_ifSettings.bTxTimeControl;
00614 }
00615
00616
00617
00618
00619
00620 void InterfaceConfig::setTxDelayTime(int txDelayTime)
00621 {
00622 m_ifSettings.iTxDelayTime = txDelayTime;
00623 }
00624
00625
00626
00627
00628
00629 int InterfaceConfig::getTxDelayTime()
00630 {
00631 return m_ifSettings.iTxDelayTime;
00632 }
00633
00634
00635
00636
00637
00638 void InterfaceConfig::setCharTimeoutMpy(int charTimeoutMpy)
00639 {
00640 m_ifSettings.iCharTimeoutMpy = charTimeoutMpy;
00641 }
00642
00643
00644
00645
00646
00647 int InterfaceConfig::getCharTimeoutMpy()
00648 {
00649 return m_ifSettings.iCharTimeoutMpy;
00650 }
00651
00652
00653
00654
00655
00656 void InterfaceConfig::setRTS(bool rts)
00657 {
00658 m_ifSettings.bRts = rts;
00659 }
00660
00661
00662
00663
00664
00665 bool InterfaceConfig::getRTS()
00666 {
00667 return m_ifSettings.bRts;
00668 }
00669
00670
00671
00672
00673
00674 void InterfaceConfig::setDTR(bool dtr)
00675 {
00676 m_ifSettings.bDtr = dtr;
00677 }
00678
00679
00680
00681
00682
00683 bool InterfaceConfig::getDTR()
00684 {
00685 return m_ifSettings.bDtr;
00686 }