NULLMGR.CPP 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /counterstrike/NULLMGR.CPP 2 3/07/97 6:40p Steve_tall $ */
  15. /***************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : NULLMGR.CPP *
  22. * *
  23. * Programmer : Bill Randolph *
  24. * *
  25. * Start Date : April 5, 1995 *
  26. * *
  27. * Last Update : May 1, 1995 [BRR] *
  28. * *
  29. *-------------------------------------------------------------------------*
  30. * Functions: *
  31. * NullModemClass::NullModemClass -- class constructor *
  32. * NullModemClass::~NullModemClass -- class destructor *
  33. * NullModemClass::Init -- initialization *
  34. * NullModemClass::Send_Message -- sends a message *
  35. * NullModemClass::Get_Message -- polls the Queue for a message *
  36. * NullModemClass::Service -- main polling routine *
  37. * NullModemClass::Num_Send -- Returns # of unACK'd send entries *
  38. * NullModemClass::Num_Receive -- Returns # entries in the receive queue *
  39. * NullModemClass::Response_Time -- Returns Queue's avg response time *
  40. * NullModemClass::Reset_Response_Time -- Resets response time computatio*
  41. * NullModemClass::Oldest_Send -- Returns ptr to oldest unACK'd send buf *
  42. * NullModemClass::Detect_Modem -- Detects and initializes the modem *
  43. * NullModemClass::Dial_Modem -- dials a number passed *
  44. * NullModemClass::Answer_Modem -- waits for call and answers *
  45. * NullModemClass::Hangup_Modem -- hangs up the modem *
  46. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  47. #if (0)//PG
  48. #include "function.h"
  49. #ifdef WIN32
  50. #include "wincomm.h"
  51. #include "modemreg.h"
  52. extern ModemRegistryEntryClass *ModemRegistry;
  53. #endif //WIN32
  54. #include "i86.h"
  55. // Turn off "expression is not meaningful".
  56. #pragma warning 628 9
  57. // the following line was taken from Greenleaf's <ibmkeys.h> <asciidef.h>
  58. // because of other define conflicts
  59. #define ESC 27
  60. #define NOKEY 0xffff
  61. #define INIT_COMMAND_RETRIES 2
  62. // this time is in milliseconds
  63. #define DEFAULT_TIMEOUT 1500
  64. //
  65. // the following is for a fix around a greenleaf bug
  66. // where they do not check for the value of abortkey
  67. // to determine whether or not they call the abort modem function.
  68. //
  69. extern "C" {
  70. extern void (*_AbortModemFunctionPtr)(int);
  71. }
  72. static void (*NullModemClass::OrigAbortModemFunc)(int);
  73. static KeyNumType NullModemClass::Input;
  74. static GadgetClass *NullModemClass::Commands; // button list
  75. /*
  76. ** Ugly hack: this string stores the string received from the modem
  77. */
  78. char ModemRXString[80];
  79. /***************************************************************************
  80. * NullModemClass::NullModemClass -- class constructor *
  81. * *
  82. * INPUT: *
  83. * numsend # desired entries for the send queue *
  84. * numreceive # desired entries for the receive queue *
  85. * maxlen application's max packet length *
  86. * magicnum application-specific magic # (so we don't *
  87. * accidentally end up talking to another one of our own *
  88. * products using the same protocol) *
  89. * *
  90. * OUTPUT: *
  91. * none. *
  92. * *
  93. * WARNINGS: *
  94. * none. *
  95. * *
  96. * HISTORY: *
  97. * 12/20/1994 BR : Created. *
  98. *=========================================================================*/
  99. NullModemClass::NullModemClass (int numsend, int numreceive, int maxlen,
  100. unsigned short magicnum) : ConnManClass()
  101. {
  102. /*------------------------------------------------------------------------
  103. Init Port to NULL; we haven't opened Greenleaf yet.
  104. ------------------------------------------------------------------------*/
  105. #ifdef WIN32
  106. PortHandle = NULL;
  107. #else //WIN32
  108. Port = NULL;
  109. #endif //WIN32
  110. Connection = NULL;
  111. NumSend = numsend;
  112. NumReceive = numreceive;
  113. MaxLen = maxlen;
  114. MagicNum = magicnum;
  115. RXBuf = 0;
  116. BuildBuf = 0;
  117. EchoSize = 500;
  118. EchoBuf = 0;
  119. OldIRQPri = -1;
  120. ModemVerboseOn = false; // default true
  121. ModemEchoOn = false; // default true
  122. ModemWaitCarrier = 50000; // default 50 * 1000ms = 50 secs
  123. ModemCarrierDetect = 600; // default 6 * 100ms = .6 secs
  124. ModemCarrierLoss = 1400; // default 14 * 100ms = 1.4 secs
  125. ModemHangupDelay = 20000; // default 20 * 1000ms = 20 secs
  126. ModemGuardTime = 1000; // default 50 * 20ms = 1 sec
  127. ModemEscapeCode = '+'; // default ASCII 43
  128. SendOverflows = 0;
  129. ReceiveOverflows = 0;
  130. CRCErrors = 0;
  131. NumConnections = 0;
  132. /*------------------------------------------------------------------------
  133. Init timing parameters
  134. ------------------------------------------------------------------------*/
  135. RetryDelta = 60; // 60 ticks between retries
  136. MaxRetries = -1; // disregard # retries
  137. Timeout = 1200; // report bad connection after 20 seconds
  138. } /* end of NullModemClass */
  139. /***************************************************************************
  140. * NullModemClass::~NullModemClass -- class destructor *
  141. * *
  142. * INPUT: *
  143. * none. *
  144. * *
  145. * OUTPUT: *
  146. * none. *
  147. * *
  148. * WARNINGS: *
  149. * none. *
  150. * *
  151. * HISTORY: *
  152. * 12/20/1994 BR : Created. *
  153. *=========================================================================*/
  154. NullModemClass::~NullModemClass ()
  155. {
  156. Delete_Connection();
  157. } /* end of ~NullModemClass */
  158. /***************************************************************************
  159. * NullModemClass::Init -- initialization *
  160. * *
  161. * INPUT: *
  162. * port address *
  163. * irq 2-15 *
  164. * dev_name name of communications device (win32 only) *
  165. * baud 300, 1200, 9600, etc *
  166. * parity 'O' (odd), 'E' (even), 'N' (none), 'S' (space), *
  167. * 'M' (mark) *
  168. * wordlength 5, 6, 7, or 8 *
  169. * stopbits 1 or 2 *
  170. * *
  171. * OUTPUT: *
  172. * 1 = OK, 0 = error *
  173. * *
  174. * WARNINGS: *
  175. * none. *
  176. * *
  177. * HISTORY: *
  178. * 12/20/1994 BR : Created. *
  179. * 10/9/1996 ST : Modified to take device name in win32 *
  180. *=========================================================================*/
  181. int NullModemClass::Init (int port, int irq, char *dev_name, int baud, char parity, int wordlen, int stopbits, int flowcontrol)
  182. {
  183. int com;
  184. #ifdef WIN32
  185. /*
  186. ** Get rid of the 'no reference to' warning
  187. */
  188. irq = irq;
  189. /*
  190. ** Make sure the port is closed before we start
  191. */
  192. if (PortHandle) {
  193. CloseHandle (PortHandle);
  194. PortHandle = NULL;
  195. }
  196. if (!Connection) {
  197. #else //WIN32
  198. int irqnum;
  199. int address;
  200. int status;
  201. /*
  202. ** Get rid of the 'no reference to' warning
  203. */
  204. dev_name = dev_name;
  205. if (Port) {
  206. PortClose(Port);
  207. Port = NULL;
  208. }else {
  209. #endif //WIN32
  210. /*------------------------------------------------------------------------
  211. Init our Connection
  212. ------------------------------------------------------------------------*/
  213. Connection = new NullModemConnClass (NumSend, NumReceive, MaxLen,
  214. MagicNum);
  215. Connection->Set_Retry_Delta (RetryDelta);
  216. Connection->Set_Max_Retries (MaxRetries);
  217. Connection->Set_TimeOut (Timeout);
  218. /*---------------------------------------------------------------------
  219. Allocate our packet parsing buffer; make it the same # of packets as the
  220. # of receive queue entries the application has requested. Use the
  221. "Actual" maximum packet size, given from the connection; this allows for
  222. both headers that get added to the packet.
  223. ---------------------------------------------------------------------*/
  224. RXSize = Connection->Actual_Max_Packet() * NumReceive;
  225. RXBuf = new char [RXSize];
  226. BuildBuf = new char [MaxLen];
  227. EchoBuf = new char [ EchoSize ];
  228. }
  229. RXCount = 0;
  230. EchoCount = 0;
  231. /*------------------------------------------------------------------------
  232. This call allocates all necessary queue buffers
  233. ------------------------------------------------------------------------*/
  234. switch (port) {
  235. case 0x3f8:
  236. com = COM1;
  237. break;
  238. case 0x2f8:
  239. com = COM2;
  240. break;
  241. case 0x3e8:
  242. com = COM3;
  243. break;
  244. case 0x2e8:
  245. com = COM4;
  246. break;
  247. default:
  248. com = COM5;
  249. break;
  250. }
  251. #ifdef WIN32
  252. int i;
  253. /*
  254. ** Create a new modem class for our com port
  255. */
  256. if (!SerialPort) {
  257. SerialPort = new WinModemClass;
  258. }
  259. /*
  260. ** Shift up the baud rate to sensible values
  261. */
  262. // if (baud == 14400) baud = 19200;
  263. // if (baud == 28800) baud = 38400;
  264. static char com_ids[9][5]={
  265. "COM1",
  266. "COM2",
  267. "COM3",
  268. "COM4",
  269. "COM5",
  270. "COM6",
  271. "COM7",
  272. "COM8",
  273. "COM9"
  274. };
  275. char *device;
  276. switch ( port ) {
  277. case 0x3f8:
  278. device = com_ids[0];
  279. break;
  280. case 0x2f8:
  281. device = com_ids[1];
  282. break;
  283. case 0x3e8:
  284. device = com_ids[2];
  285. break;
  286. case 0x2e8:
  287. device = com_ids[3];
  288. break;
  289. case 1:
  290. /*
  291. ** 1 is a special value. It means use the device name not the port address.
  292. */
  293. device = dev_name;
  294. /*
  295. ** If we can match a registry entry with the device name then use that, otherwise use
  296. ** the device name directly to open the port with.
  297. */
  298. if (ModemRegistry){
  299. delete ModemRegistry;
  300. ModemRegistry = NULL;
  301. }
  302. for ( i=0 ; i<10 ; i++ ){
  303. ModemRegistry = new ModemRegistryEntryClass (i);
  304. if (ModemRegistry->Get_Modem_Name()){
  305. if (!strcmp (dev_name, ModemRegistry->Get_Modem_Name() )){
  306. device = ModemRegistry->Get_Modem_Device_Name();
  307. break;
  308. }
  309. }
  310. delete ModemRegistry;
  311. ModemRegistry = NULL;
  312. }
  313. break;
  314. default:
  315. device = NULL;
  316. }
  317. /*
  318. ** Open the com port
  319. */
  320. PortHandle = SerialPort->Serial_Port_Open (device, baud, parity, wordlen, stopbits, flowcontrol);
  321. if (PortHandle == INVALID_HANDLE_VALUE) {
  322. Shutdown();
  323. return(false);
  324. }
  325. Connection->Init (PortHandle);
  326. #else //WIN32
  327. status = FastGetPortHardware( com, &irqnum, &address );
  328. if (status == ASSUCCESS) {
  329. if (port != address || irq != irqnum) {
  330. status = FastSetPortHardware( com, irq, port );
  331. if (status < ASSUCCESS) {
  332. Mono_Printf( "Unable to set Com port status %d\n", status );
  333. Mono_Printf( "Com port number %d with address %x, irq %d\n",
  334. com + 1, port, irq );
  335. } else {
  336. Mono_Printf( "Changed Com port number %d to address %x, irq %d\n",
  337. com + 1, port, irq );
  338. }
  339. } else {
  340. Mono_Printf( "No changes to Com port number %d with address %x, irq %d\n",
  341. com + 1, port, irq );
  342. }
  343. } else {
  344. Mono_Printf( "Com port number %d\n", com + 1 );
  345. }
  346. if (status != ASSUCCESS) {
  347. Delete_Connection();
  348. return(false);
  349. }
  350. /*------------------------------------------------------------------------
  351. Open the Greenleaf port
  352. ------------------------------------------------------------------------*/
  353. Port = PortOpenGreenleafFast (com, baud, parity, wordlen, stopbits);
  354. if (Port->status != ASSUCCESS) {
  355. Shutdown();
  356. return(false);
  357. }
  358. // UseRtsCts( Port, 1 ); // use RTS CTS hardware flow control
  359. /*------------------------------------------------------------------------
  360. Init the Connection
  361. ------------------------------------------------------------------------*/
  362. Connection->Init(Port);
  363. //because Watcom is so stupid
  364. flowcontrol = flowcontrol;
  365. #endif //WIN32
  366. NumConnections = 1;
  367. return(true);
  368. }
  369. /***********************************************************************************************
  370. * NMC::Num_Connections -- returns NumConnections member *
  371. * *
  372. * *
  373. * *
  374. * INPUT: Nothing *
  375. * *
  376. * OUTPUT: NumConnections *
  377. * *
  378. * WARNINGS: None *
  379. * *
  380. * HISTORY: *
  381. * 8/2/96 11:44AM ST : Documented / Win32 support *
  382. *=============================================================================================*/
  383. int NullModemClass::Num_Connections( void )
  384. {
  385. return(NumConnections);
  386. }
  387. /***********************************************************************************************
  388. * NMC::Delete_Connection -- removes the connection *
  389. * *
  390. * *
  391. * *
  392. * INPUT: Nothing *
  393. * *
  394. * OUTPUT: true *
  395. * *
  396. * WARNINGS: None *
  397. * *
  398. * HISTORY: *
  399. * 8/2/96 11:44AM ST : Documented / Win32 support *
  400. *=============================================================================================*/
  401. int NullModemClass::Delete_Connection( void )
  402. {
  403. if (Connection) {
  404. delete Connection;
  405. Connection = NULL;
  406. }
  407. if (RXBuf) {
  408. delete [] RXBuf;
  409. RXBuf = NULL;
  410. }
  411. if (BuildBuf) {
  412. delete [] BuildBuf;
  413. BuildBuf = NULL;
  414. }
  415. if (EchoBuf) {
  416. delete [] EchoBuf;
  417. EchoBuf = NULL;
  418. }
  419. NumConnections = 0;
  420. return( true );
  421. } /* end of Delete_Connection */
  422. /***********************************************************************************************
  423. * NMC::Init_Send_Queue -- Initialises the connections send queue *
  424. * *
  425. * *
  426. * *
  427. * INPUT: Nothing *
  428. * *
  429. * OUTPUT: true *
  430. * *
  431. * WARNINGS: None *
  432. * *
  433. * HISTORY: *
  434. * 8/2/96 11:46AM ST : Documented / Win32 support *
  435. *=============================================================================================*/
  436. int NullModemClass::Init_Send_Queue( void )
  437. {
  438. /*------------------------------------------------------------------------
  439. Init the send queue
  440. ------------------------------------------------------------------------*/
  441. if ( Connection ) {
  442. Connection->Queue->Init_Send_Queue();
  443. }
  444. return(true);
  445. }
  446. /***********************************************************************************************
  447. * NMC::Detect_Port -- Checks that the specified com port exists *
  448. * *
  449. * *
  450. * *
  451. * INPUT: ptr to SerialSettingsType *
  452. * *
  453. * OUTPUT: true if port is valid *
  454. * *
  455. * WARNINGS: Win32 version always returns true as win95 shouldnt allow us to open the *
  456. * port if it doesnt exist or is in use by the mouse *
  457. * *
  458. * HISTORY: *
  459. * 8/2/96 11:47AM ST : Documented / Win32 support *
  460. *=============================================================================================*/
  461. DetectPortType NullModemClass::Detect_Port( SerialSettingsType *settings )
  462. {
  463. #ifdef WIN32
  464. static char com_ids[9][5]={
  465. "COM1",
  466. "COM2",
  467. "COM3",
  468. "COM4",
  469. "COM5",
  470. "COM6",
  471. "COM7",
  472. "COM8",
  473. "COM9"
  474. };
  475. int i;
  476. /*
  477. ** Create a new modem class for our com port
  478. */
  479. if (!SerialPort) {
  480. SerialPort = new WinModemClass;
  481. }else{
  482. SerialPort->Serial_Port_Close();
  483. }
  484. /*
  485. ** Shift up the baud rate to sensible values
  486. */
  487. int baud = settings->Baud;
  488. // if (baud == 14400) baud = 19200;
  489. // if (baud == 28800) baud = 38400;
  490. /*
  491. ** Translate the port address into a usable device name
  492. */
  493. char *device;
  494. switch ( settings->Port ) {
  495. case 0x3f8:
  496. device = com_ids[0];
  497. break;
  498. case 0x2f8:
  499. device = com_ids[1];
  500. break;
  501. case 0x3e8:
  502. device = com_ids[2];
  503. break;
  504. case 0x2e8:
  505. device = com_ids[3];
  506. break;
  507. case 1:
  508. /*
  509. ** 1 is a special value. It means use the device name not the port address.
  510. */
  511. device = settings->ModemName;
  512. /*
  513. ** If we can match a registry entry with the device name then use that, otherwise use
  514. ** the device name directly to open the port with.
  515. */
  516. if (ModemRegistry){
  517. delete ModemRegistry;
  518. ModemRegistry = NULL;
  519. }
  520. for ( i=0 ; i<10 ; i++){
  521. ModemRegistry = new ModemRegistryEntryClass (i);
  522. if (ModemRegistry->Get_Modem_Name()){
  523. if (!strcmp (device, ModemRegistry->Get_Modem_Name() )){
  524. /*
  525. ** Got a match. Break out leaving the registry info intact.
  526. */
  527. device = ModemRegistry->Get_Modem_Device_Name();
  528. break;
  529. }
  530. }
  531. delete ModemRegistry;
  532. ModemRegistry = NULL;
  533. }
  534. break;
  535. default:
  536. return (PORT_INVALID);
  537. }
  538. /*
  539. ** Open the com port
  540. */
  541. HANDLE porthandle = SerialPort->Serial_Port_Open (device, baud, 0, 8, 1, settings->HardwareFlowControl);
  542. if (porthandle == INVALID_HANDLE_VALUE){
  543. return (PORT_INVALID);
  544. }
  545. SerialPort->Serial_Port_Close();
  546. return (PORT_VALID);
  547. #else //WIN32
  548. union REGS regs ;
  549. int com;
  550. int irqnum;
  551. int address;
  552. int status;
  553. // shutdown previous port
  554. Shutdown();
  555. if (settings->IRQ > 0xf) {
  556. return( PORT_INVALID );
  557. }
  558. //
  559. // check if the IRQ is the same as the mouse
  560. // call mouse func to get mouse IRQ number
  561. //
  562. regs.x.eax = 0x0024;
  563. int386( 0x33, &regs, &regs );
  564. //
  565. // check for error
  566. //
  567. if (regs.w.ax != 0xffff) {
  568. //
  569. // is the mouse IRQ the same as that they selected
  570. //
  571. if (regs.h.cl == 0) { // PS/2 IRQ 0xc
  572. if (settings->IRQ == 0xc) {
  573. return( PORT_IRQ_INUSE );
  574. }
  575. } else if (regs.h.cl == (unsigned char)(settings->IRQ)) {
  576. return( PORT_IRQ_INUSE );
  577. }
  578. }
  579. if (settings->IRQ < 2 // 0 timer, 1 keyboard
  580. || settings->IRQ == 6 // floppy disk
  581. || settings->IRQ == 8 // CMOS real-time clock
  582. || settings->IRQ == 0xd // math coprocessor error
  583. || settings->IRQ == 0xe) { // hard disk
  584. return( PORT_IRQ_INUSE );
  585. }
  586. /*------------------------------------------------------------------------
  587. This call allocates all necessary queue buffers
  588. ------------------------------------------------------------------------*/
  589. switch ( settings->Port ) {
  590. case 0x3f8:
  591. com = COM1;
  592. break;
  593. case 0x2f8:
  594. com = COM2;
  595. break;
  596. case 0x3e8:
  597. com = COM3;
  598. break;
  599. case 0x2e8:
  600. com = COM4;
  601. break;
  602. default:
  603. com = COM5;
  604. break;
  605. }
  606. status = FastGetPortHardware( com, &irqnum, &address );
  607. if (status == ASSUCCESS) {
  608. if (settings->Port != address || settings->IRQ != irqnum) {
  609. status = FastSetPortHardware( com, settings->IRQ, settings->Port );
  610. }
  611. }
  612. if (status != ASSUCCESS) {
  613. return( PORT_INVALID );
  614. }
  615. /*------------------------------------------------------------------------
  616. Open the Greenleaf port
  617. ------------------------------------------------------------------------*/
  618. Port = PortOpenGreenleafFast (com, settings->Baud, 'N', 8, 1);
  619. status = Port->status;
  620. PortClose(Port);
  621. Port = NULL;
  622. if (status == ASIRQINUSE) {
  623. return( PORT_IRQ_INUSE );
  624. } else if (status != ASSUCCESS) {
  625. return( PORT_INVALID );
  626. }
  627. return( PORT_VALID );
  628. #endif //WIN32
  629. }
  630. /***********************************************************************************************
  631. * NullModemClass::ShutDown -- Closes serial port and removes the connection *
  632. * *
  633. * *
  634. * *
  635. * INPUT: Nothing *
  636. * *
  637. * OUTPUT: Nothing *
  638. * *
  639. * WARNINGS: None *
  640. * *
  641. * HISTORY: *
  642. * 8/2/96 11:43AM ST : Documented / Win32 support *
  643. *=============================================================================================*/
  644. void NullModemClass::Shutdown ( void )
  645. {
  646. #ifdef WIN32
  647. if (PortHandle && SerialPort) {
  648. SerialPort->Serial_Port_Close();
  649. delete SerialPort;
  650. SerialPort = NULL;
  651. PortHandle = NULL;
  652. Delete_Connection();
  653. }
  654. #else //WIN32
  655. if (Port) {
  656. PortClose(Port);
  657. Port = NULL;
  658. Delete_Connection();
  659. }
  660. #endif //WIN32
  661. } /* end of Shutdown */
  662. /***************************************************************************
  663. * NullModemClass::Set_Timing -- sets timing for all connections *
  664. * *
  665. * This will set the timing parameters. This allows an application to *
  666. * measure the Response_Time while running, and adjust timing accordingly. *
  667. * *
  668. * INPUT: *
  669. * retrydelta value to set for retry delta *
  670. * maxretries value to set for max # retries *
  671. * timeout value to set for connection timeout *
  672. * *
  673. * OUTPUT: *
  674. * none. *
  675. * *
  676. * WARNINGS: *
  677. * none. *
  678. * *
  679. * HISTORY: *
  680. * 08/07/1995 DRD : Created. *
  681. *=========================================================================*/
  682. void NullModemClass::Set_Timing (unsigned long retrydelta,
  683. unsigned long maxretries, unsigned long timeout)
  684. {
  685. RetryDelta = retrydelta;
  686. MaxRetries = maxretries;
  687. Timeout = timeout;
  688. Connection->Set_Retry_Delta (RetryDelta);
  689. Connection->Set_Max_Retries (MaxRetries);
  690. Connection->Set_TimeOut (Timeout);
  691. } /* end of Set_Timing */
  692. /***************************************************************************
  693. * NullModemClass::Send_Message -- sends a message *
  694. * *
  695. * For clarity's sake, here's what happens to the buffer passed in: *
  696. * - It gets passed to the Connection's Send_Packet() routine *
  697. * - The CommHeaderType header gets tacked onto it *
  698. * - The resulting buffer gets added to the Connection's Send Queue *
  699. * - When Service() determines that it needs to send the data, it *
  700. * copies the entire packet (CommHeaderType and all) into its local *
  701. * SendBuf, adds the packet start ID, length, and CRC, then sends it out.*
  702. * *
  703. * The ack_req argument will almost always be '1' (the default). The only *
  704. * reason to use 0 is if you don't know whether the other system is *
  705. * ready or not, so you have to periodically send out a query packet, *
  706. * and wait for a response. (Using the connection's built-in retry *
  707. * system would just blast out useless data if the other system isn't *
  708. * even there.) *
  709. * *
  710. * INPUT: *
  711. * buf buffer to send *
  712. * buflen length of buffer *
  713. * ack_req 1 = ACK is required; 0 = not *
  714. * *
  715. * OUTPUT: *
  716. * 1 = OK; 0 = error *
  717. * *
  718. * WARNINGS: *
  719. * none. *
  720. * *
  721. * HISTORY: *
  722. * 12/20/1994 BR : Created. *
  723. *=========================================================================*/
  724. int NullModemClass::Send_Message (void *buf, int buflen, int ack_req)
  725. {
  726. int rc;
  727. if (NumConnections == 0) {
  728. return( false );
  729. }
  730. rc = Connection->Send_Packet(buf,buflen,ack_req);
  731. if (!rc)
  732. SendOverflows++;
  733. return(rc);
  734. } /* end of Send_Message */
  735. /***************************************************************************
  736. * NullModemClass::Get_Message -- polls the Queue for a message *
  737. * *
  738. * INPUT: *
  739. * buf buffer to store message in *
  740. * buflen ptr filled in with length of message *
  741. * *
  742. * OUTPUT: *
  743. * 1 = message was received; 0 = wasn't *
  744. * *
  745. * WARNINGS: *
  746. * none. *
  747. * *
  748. * HISTORY: *
  749. * 12/20/1994 BR : Created. *
  750. *=========================================================================*/
  751. int NullModemClass::Get_Message (void *buf, int *buflen)
  752. {
  753. if (NumConnections == 0) {
  754. return( false );
  755. }
  756. return( Connection->Get_Packet( buf, buflen ) );
  757. }
  758. /***************************************************************************
  759. * NullModemClass::Service -- main polling routine *
  760. * *
  761. * INPUT: *
  762. * none. *
  763. * *
  764. * OUTPUT: *
  765. * 1 = OK, 0 = connection has gone bad *
  766. * *
  767. * WARNINGS: *
  768. * none. *
  769. * *
  770. * HISTORY: *
  771. * 12/20/1994 BR : Created. *
  772. * 8/2/96 ST : Win32 support *
  773. *=========================================================================*/
  774. int NullModemClass::Service (void)
  775. {
  776. int pos; // current position in RXBuf
  777. int i; // loop counter
  778. unsigned short length;
  779. SerialHeaderType *header; // decoded packet start, length
  780. SerialCRCType *crc; // decoded packet CRC
  781. char moredata = 0;
  782. if (NumConnections == 0) {
  783. return( false );
  784. }
  785. #ifdef WIN32
  786. RXCount += SerialPort->Read_From_Serial_Port((unsigned char*)(RXBuf + RXCount), int(RXSize - RXCount) );
  787. #else //WIN32
  788. int status;
  789. static unsigned long last_time = 0;
  790. /*------------------------------------------------------------------------
  791. First, copy all the bytes we can from the Greenleaf RX buffer to our
  792. own buffer.
  793. (For Win95, only call GetLineStatus() 60 times a second at most.
  794. Otherwise, we don't receive any packets!)
  795. ------------------------------------------------------------------------*/
  796. if ( (TickCount - last_time) > 0) {
  797. if ( GetLineStatus( Port ) ) {
  798. Mono_Set_Cursor(0,0);
  799. ClearLineStatus( Port );
  800. }
  801. last_time = TickCount;
  802. }
  803. status = SpaceUsedInRXBuffer( Port );
  804. if ( status < ASSUCCESS) {
  805. // Smart_Printf( "SpaceUsedInRXBuffer status = %d, port status = %d \n", status, Port->status );
  806. if ( Port->status < ASSUCCESS ) {
  807. ClearError( Port );
  808. }
  809. } else if (status > 0) {
  810. status = ReadBuffer( Port, RXBuf + RXCount, RXSize - RXCount );
  811. #if (CONN_DEBUG)
  812. printf( "ReadBuffer status = %d, port status = %d, count = %d \n", status, Port->status, Port->count );
  813. #endif
  814. // Smart_Printf( "ReadBuffer status = %d, port status = %d, count = %d \n", status, Port->status, Port->count );
  815. if (status < ASSUCCESS && status != ASBUFREMPTY) {
  816. // Smart_Printf( "ReadBuffer ERRRRRRORRRRRR! \n" );
  817. } else {
  818. moredata = 1;
  819. }
  820. if ( Port->status < ASSUCCESS ) {
  821. ClearError( Port );
  822. }
  823. RXCount += Port->count;
  824. }
  825. #endif //WIN32
  826. // minimum packet size
  827. if ( RXCount < (PACKET_SERIAL_OVERHEAD_SIZE + 1) ) {
  828. return( Connection->Service() );
  829. }
  830. /*------------------------------------------------------------------------
  831. Now scan the buffer for the start of a packet.
  832. ------------------------------------------------------------------------*/
  833. pos = -1;
  834. for (i = 0; i <= RXCount - sizeof( short ); i++) {
  835. if ( *((unsigned short *)(RXBuf + i)) == PACKET_SERIAL_START ) {
  836. pos = i;
  837. break;
  838. }
  839. }
  840. /*------------------------------------------------------------------------
  841. No start code was found; throw away all bytes except the last few, and
  842. return.
  843. ------------------------------------------------------------------------*/
  844. if (pos==-1) {
  845. // Smart_Printf( "No magic number found \n" );
  846. /*.....................................................................
  847. move the remaining, un-checked bytes to the start of the buffer
  848. .....................................................................*/
  849. memmove (RXBuf, RXBuf + i, sizeof( short ) - 1);
  850. RXCount = sizeof( short ) - 1;
  851. return( Connection->Service() );
  852. }
  853. /*------------------------------------------------------------------------
  854. Check to see if there are enough bytes for the header to be decoded
  855. ------------------------------------------------------------------------*/
  856. if ( (RXCount - pos) < sizeof( SerialHeaderType ) ) {
  857. memmove (RXBuf, RXBuf + pos, RXCount - pos);
  858. RXCount -= pos;
  859. return(Connection->Service());
  860. }
  861. /*------------------------------------------------------------------------
  862. A start code was found; check the packet's length & CRC
  863. ------------------------------------------------------------------------*/
  864. header = (SerialHeaderType *)(RXBuf + pos);
  865. /*------------------------------------------------------------------------
  866. If we lost a byte in the length, we may end up waiting a very long time
  867. for the buffer to get to the right length; check the verify value to
  868. make sure this didn't happen.
  869. ------------------------------------------------------------------------*/
  870. if ( header->MagicNumber2 != PACKET_SERIAL_VERIFY ) {
  871. // Smart_Printf( "Verify failed\n");
  872. // Hex_Dump_Data( (RXBuf + pos), PACKET_SERIAL_OVERHEAD_SIZE );
  873. pos += sizeof ( short ); // throw away the bogus start code
  874. memmove (RXBuf, RXBuf + pos, RXCount - pos);
  875. RXCount -= pos;
  876. return(Connection->Service());
  877. }
  878. length = header->Length;
  879. /*------------------------------------------------------------------------
  880. Special case: if the length comes out too long for us to process:
  881. - Assume the packet is bad
  882. - Throw away the bogus packet-start code
  883. - Return; we'll search for another packet-start code next time.
  884. ------------------------------------------------------------------------*/
  885. if (length > MaxLen) {
  886. #if (CONN_DEBUG)
  887. printf( "length too lonnng\n" );
  888. #endif
  889. // Smart_Printf( "length too lonnng %d, max %d \n", length, MaxLen );
  890. pos += sizeof ( short ); // throw away the bogus start code
  891. memmove (RXBuf, RXBuf + pos, RXCount - pos);
  892. RXCount -= pos;
  893. return(Connection->Service());
  894. }
  895. /*------------------------------------------------------------------------
  896. If the entire packet isn't stored in our buffer, copy the remaining bytes
  897. to the front of the buffer & return.
  898. ------------------------------------------------------------------------*/
  899. if ( (pos + length + PACKET_SERIAL_OVERHEAD_SIZE) > RXCount) {
  900. if ( moredata ) {
  901. // Smart_Printf( "waiting for more data %d, pos = %d \n", ((length + PACKET_SERIAL_OVERHEAD_SIZE) - (RXCount - pos)), pos );
  902. }
  903. if (pos) {
  904. memmove (RXBuf, RXBuf + pos, RXCount - pos);
  905. RXCount -= pos;
  906. }
  907. return(Connection->Service());
  908. }
  909. /*------------------------------------------------------------------------
  910. Now grab the CRC value in the packet, & compare it to the CRC value
  911. computed from the actual data. If they don't match, throw away the bogus
  912. start-code, move the rest to the front of the buffer, & return.
  913. We'll continue parsing this data when we're called next time.
  914. ------------------------------------------------------------------------*/
  915. crc = (SerialCRCType *)(RXBuf + pos + sizeof( SerialHeaderType ) + length);
  916. if (NullModemConnClass::Compute_CRC(RXBuf + pos +
  917. sizeof( SerialHeaderType ), length) != crc->SerialCRC) {
  918. CRCErrors++;
  919. #if (CONN_DEBUG)
  920. printf( "CRC check failed\n" );
  921. #endif
  922. // Smart_Printf( "CRC check failed for packet of length %d \n", length );
  923. // if (length < 100) {
  924. // Hex_Dump_Data( (RXBuf + pos), (PACKET_SERIAL_OVERHEAD_SIZE + length) );
  925. // }
  926. pos += sizeof ( short ); // throw away the bogus start code
  927. memmove (RXBuf, RXBuf + pos, RXCount - pos);
  928. RXCount -= pos;
  929. return(Connection->Service());
  930. }
  931. #if(0)
  932. // Mono_Printf( "received %d bytes \n", sendlen );
  933. Debug_Modem_Dump = true;
  934. Smart_Printf( "Received tick=%d, Protocol=%d \n", TickCount, Session.CommProtocol );
  935. Hex_Dump_Data( (RXBuf + pos), (sizeof( SerialHeaderType) + length) );
  936. Debug_Modem_Dump = false;
  937. #endif
  938. /*------------------------------------------------------------------------
  939. Give the new packet to the Connection to process.
  940. ------------------------------------------------------------------------*/
  941. if (!Connection->Receive_Packet(RXBuf + pos + sizeof( SerialHeaderType ), length)) {
  942. ReceiveOverflows++;
  943. // Smart_Printf( "Received overflows %d \n", ReceiveOverflows );
  944. }
  945. #if(0)
  946. else {
  947. // Mono_Printf( "added packet \n", sendlen );
  948. Debug_Modem_Dump = true;
  949. Smart_Printf( "Received Packet \n" );
  950. Debug_Modem_Dump = false;
  951. }
  952. #endif
  953. #if (0)
  954. Hex_Dump_Data( (RXBuf + pos), (PACKET_SERIAL_OVERHEAD_SIZE + length) );
  955. #endif
  956. /*------------------------------------------------------------------------
  957. Move all data past this packet to the front of the buffer.
  958. ------------------------------------------------------------------------*/
  959. pos += (PACKET_SERIAL_OVERHEAD_SIZE + length);
  960. memmove (RXBuf, RXBuf + pos, RXCount - pos);
  961. RXCount -= pos;
  962. /*------------------------------------------------------------------------
  963. Now, service the connection's Queue's; this will handle ACK & Retries.
  964. ------------------------------------------------------------------------*/
  965. return(Connection->Service());
  966. } /* end of Service */
  967. /***************************************************************************
  968. * NullModemClass::Num_Send -- Returns # of unACK'd send entries *
  969. * *
  970. * INPUT: *
  971. * *
  972. * OUTPUT: *
  973. * *
  974. * WARNINGS: *
  975. * *
  976. * HISTORY: *
  977. * 05/01/1995 BRR : Created. *
  978. *=========================================================================*/
  979. int NullModemClass::Num_Send(void)
  980. {
  981. if (Connection)
  982. return( Connection->Queue->Num_Send() );
  983. else
  984. return (0);
  985. } /* end of Num_Send */
  986. /***************************************************************************
  987. * NullModemClass::Num_Receive -- Returns # entries in the receive queue *
  988. * *
  989. * INPUT: *
  990. * *
  991. * OUTPUT: *
  992. * *
  993. * WARNINGS: *
  994. * *
  995. * HISTORY: *
  996. * 05/01/1995 BRR : Created. *
  997. *=========================================================================*/
  998. int NullModemClass::Num_Receive(void)
  999. {
  1000. if (Connection)
  1001. return( Connection->Queue->Num_Receive() );
  1002. else
  1003. return (0);
  1004. } /* end of Num_Receive */
  1005. /***************************************************************************
  1006. * NullModemClass::Response_Time -- Returns Queue's avg response time *
  1007. * *
  1008. * INPUT: *
  1009. * *
  1010. * OUTPUT: *
  1011. * *
  1012. * WARNINGS: *
  1013. * *
  1014. * HISTORY: *
  1015. * 05/01/1995 BRR : Created. *
  1016. *=========================================================================*/
  1017. unsigned long NullModemClass::Response_Time(void)
  1018. {
  1019. if (Connection)
  1020. return( Connection->Queue->Avg_Response_Time() );
  1021. else
  1022. return (0);
  1023. } /* end of Response_Time */
  1024. /***************************************************************************
  1025. * NullModemClass::Reset_Response_Time -- Resets response time computation *
  1026. * *
  1027. * INPUT: *
  1028. * *
  1029. * OUTPUT: *
  1030. * *
  1031. * WARNINGS: *
  1032. * *
  1033. * HISTORY: *
  1034. * 05/01/1995 BRR : Created. *
  1035. *=========================================================================*/
  1036. void NullModemClass::Reset_Response_Time(void)
  1037. {
  1038. if (Connection)
  1039. Connection->Queue->Reset_Response_Time();
  1040. } /* end of Reset_Response_Time */
  1041. /***************************************************************************
  1042. * Oldest_Send -- Returns ptr to oldest unACK'd send buffer *
  1043. * *
  1044. * INPUT: *
  1045. * *
  1046. * OUTPUT: *
  1047. * *
  1048. * WARNINGS: *
  1049. * *
  1050. * HISTORY: *
  1051. * 05/01/1995 BRR : Created. *
  1052. *=========================================================================*/
  1053. void * NullModemClass::Oldest_Send(void)
  1054. {
  1055. int i;
  1056. SendQueueType *send_entry; // ptr to send entry header
  1057. CommHeaderType *packet;
  1058. void *buf = NULL;
  1059. for (i = 0; i < Connection->Queue->Num_Send(); i++) {
  1060. send_entry = Connection->Queue->Get_Send(i);
  1061. if (send_entry) {
  1062. packet = (CommHeaderType *)send_entry->Buffer;
  1063. if (packet->Code == ConnectionClass::PACKET_DATA_ACK && send_entry->IsACK == 0) {
  1064. buf = send_entry->Buffer;
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. return(buf);
  1070. } /* end of Oldest_Send */
  1071. /***************************************************************************
  1072. * NullModemClass::Configure_Debug -- sets up special debug values *
  1073. * *
  1074. * Mono_Debug_Print2() can look into a packet to pull out a particular *
  1075. * ID, and can print both that ID and a string corresponding to *
  1076. * that ID. This routine configures these values so it can find *
  1077. * and decode the ID. This ID is used in addition to the normal *
  1078. * CommHeaderType values. *
  1079. * *
  1080. * INPUT: *
  1081. * index (not used) *
  1082. * type_offset ID's byte offset into packet *
  1083. * type_size size of ID, in bytes; 0 if none *
  1084. * names ptr to array of names; use ID as an index into this *
  1085. * namestart numerical start of the 1st name value *
  1086. * namecount # in the names array; 0 if none. *
  1087. * *
  1088. * OUTPUT: *
  1089. * none. *
  1090. * *
  1091. * WARNINGS: *
  1092. * Names shouldn't be longer than 12 characters. *
  1093. * *
  1094. * HISTORY: *
  1095. * 05/31/1995 BRR : Created. *
  1096. *=========================================================================*/
  1097. void NullModemClass::Configure_Debug(int, int type_offset, int type_size,
  1098. char **names, int namestart, int namecount)
  1099. {
  1100. if (Connection)
  1101. Connection->Queue->Configure_Debug (type_offset, type_size, names,
  1102. namestart, namecount);
  1103. }
  1104. #ifdef CHEAT_KEYS
  1105. /***************************************************************************
  1106. * Mono_Debug_Print -- Debug output routine *
  1107. * *
  1108. * INPUT: *
  1109. * index (not used) *
  1110. * refresh 1 = clear screen & completely refresh *
  1111. * *
  1112. * OUTPUT: *
  1113. * none. *
  1114. * *
  1115. * WARNINGS: *
  1116. * none. *
  1117. * *
  1118. * HISTORY: *
  1119. * 05/02/1995 BRR : Created. *
  1120. *=========================================================================*/
  1121. void NullModemClass::Mono_Debug_Print(int,int refresh)
  1122. {
  1123. if (!Connection)
  1124. return;
  1125. Connection->Queue->Mono_Debug_Print (refresh);
  1126. if (refresh) {
  1127. Mono_Set_Cursor (31,1);
  1128. Mono_Printf ("Serial Port Queues");
  1129. Mono_Set_Cursor (9,2);
  1130. Mono_Printf ("Average Response Time:");
  1131. Mono_Set_Cursor (20,3);
  1132. Mono_Printf ("CRC Errors:");
  1133. Mono_Set_Cursor (43,2);
  1134. Mono_Printf ("Send Overflows:");
  1135. Mono_Set_Cursor (40,3);
  1136. Mono_Printf ("Receive Overflows:");
  1137. }
  1138. Mono_Set_Cursor (32,2);
  1139. Mono_Printf ("%d ", Connection->Queue->Avg_Response_Time());
  1140. Mono_Set_Cursor (32,3);
  1141. Mono_Printf ("%d ", CRCErrors);
  1142. Mono_Set_Cursor (59,2);
  1143. Mono_Printf ("%d ", SendOverflows);
  1144. Mono_Set_Cursor (59,3);
  1145. Mono_Printf ("%d ", ReceiveOverflows);
  1146. Mono_Set_Cursor (2,5);
  1147. Mono_Printf ("%d ", Num_Send());
  1148. Mono_Set_Cursor (41,5);
  1149. Mono_Printf ("%d ", Num_Receive());
  1150. } /* end of Mono_Debug_Print */
  1151. #endif
  1152. /***************************************************************************
  1153. * NullModemClass::Detect_Modem -- Detects and initializes the modem *
  1154. * *
  1155. * INPUT: *
  1156. * settings ptr to SerialSettings structure *
  1157. * *
  1158. * OUTPUT: *
  1159. * -1 init string invalid *
  1160. * 0 no modem found *
  1161. * 1 modem found *
  1162. * *
  1163. * WARNINGS: *
  1164. * none. *
  1165. * *
  1166. * HISTORY: *
  1167. * 06/02/1995 DRD : Created. *
  1168. * 8/2/96 ST : Added Win32 support *
  1169. *=========================================================================*/
  1170. int NullModemClass::Detect_Modem( SerialSettingsType *settings, bool reconnect )
  1171. {
  1172. /*------------------------------------------------------------------------
  1173. Button Enumerations
  1174. ------------------------------------------------------------------------*/
  1175. int status;
  1176. int error_count = 0;
  1177. int x,y,width,height; // dialog dimensions
  1178. char buffer[80*3];
  1179. /*
  1180. ** Get resolution factor
  1181. */
  1182. // int factor = SeenBuff.Get_Width()/320;
  1183. /*------------------------------------------------------------------------
  1184. Determine the dimensions of the text to be used for the dialog box.
  1185. These dimensions will control how the dialog box looks.
  1186. ------------------------------------------------------------------------*/
  1187. strcpy( buffer, Text_String( TXT_INITIALIZING_MODEM ) );
  1188. Fancy_Text_Print(TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TEXT);
  1189. int lines = Format_Window_String(buffer, SeenBuff.Get_Height(), width, height);
  1190. width = max(width, 90 * RESFACTOR);
  1191. width += 40 * RESFACTOR;
  1192. height += 40 * RESFACTOR;
  1193. x = (SeenBuff.Get_Width() - width) / 2;
  1194. y = (SeenBuff.Get_Height() - height) / 2;
  1195. /*------------------------------------------------------------------------
  1196. Initialize
  1197. ------------------------------------------------------------------------*/
  1198. Set_Logic_Page(SeenBuff);
  1199. /*------------------------------------------------------------------------
  1200. Draw the dialog
  1201. ------------------------------------------------------------------------*/
  1202. Hide_Mouse();
  1203. if ( !reconnect ) {
  1204. Load_Title_Page(true);
  1205. }
  1206. Dialog_Box(x, y, width, height);
  1207. Draw_Caption(TXT_NONE, x, y, width);
  1208. if (lines == 1) {
  1209. Fancy_Text_Print(buffer, x + width/2, y + 25, GadgetClass::Get_Color_Scheme(), TBLACK, TPF_TEXT|TPF_CENTER);
  1210. } else {
  1211. Fancy_Text_Print(buffer, x + 20*RESFACTOR, y + 25, GadgetClass::Get_Color_Scheme(), TBLACK, TPF_TEXT);
  1212. }
  1213. Show_Mouse();
  1214. HMWaitForOK( 0, NULL );
  1215. /*
  1216. ** OK, lets not mess about any more. Just turn on echo, verbose, and result codes
  1217. ** before we even begin. At least this way when we get an error later on we have already
  1218. ** removed all the steps we use to try and recover.
  1219. ** The timeouts need to be quite small in case the modem is turned off.
  1220. */
  1221. /*
  1222. ** Turn on result codes.
  1223. */
  1224. Send_Modem_Command ( "ATQ0", '\r', buffer, 81, DEFAULT_TIMEOUT / 2, 2);
  1225. /*
  1226. ** Make result codes verbose.
  1227. */
  1228. Send_Modem_Command ( "ATV1", '\r', buffer, 81, DEFAULT_TIMEOUT / 2, 2);
  1229. /*
  1230. ** Turn on echo.
  1231. */
  1232. Send_Modem_Command ( "ATE1", '\r', buffer, 81, DEFAULT_TIMEOUT / 2, 2);
  1233. ModemVerboseOn = true;
  1234. ModemEchoOn = true;
  1235. /*
  1236. ** Try sending a plain old AT command to the modem. Now that we have theoretically
  1237. ** turned on verbose result codes we should get an 'OK' back.
  1238. **
  1239. */
  1240. status = Send_Modem_Command( "AT", '\r', buffer, 81, DEFAULT_TIMEOUT, 2 );
  1241. if (status < ASSUCCESS) {
  1242. #ifdef FIXIT_MULTI_SAVE
  1243. return(false);
  1244. #else
  1245. if (WWMessageBox().Process(TXT_ERROR_NO_RESP, TXT_IGNORE, TXT_CANCEL)) return( false );
  1246. error_count++;
  1247. #endif
  1248. }
  1249. /*
  1250. ** Send the user supplied modem init string
  1251. */
  1252. if (settings->InitStringIndex == -1) {
  1253. status = Send_Modem_Command( "", '\r', buffer, 81, 300, 1 );
  1254. } else {
  1255. /*
  1256. ** Split up the init string into seperate strings if it contains one or more '|' characters.
  1257. ** This character acts as a carriage return/pause.
  1258. */
  1259. char *istr = new char [2 + strlen( Session.InitStrings [settings->InitStringIndex] )];
  1260. char *tokenptr;
  1261. strcpy (istr, Session.InitStrings [settings->InitStringIndex] );
  1262. /*
  1263. ** Tokenise the string and send it in chunks
  1264. */
  1265. tokenptr = strtok ( istr, "|" );
  1266. while ( tokenptr ) {
  1267. status = Send_Modem_Command( tokenptr, '\r', buffer, 81, 3000, 1 );
  1268. /*
  1269. ** Handle error case.
  1270. */
  1271. if (status < ASSUCCESS) {
  1272. if (WWMessageBox().Process(TXT_ERROR_NO_INIT, TXT_IGNORE, TXT_CANCEL)) {
  1273. delete istr;
  1274. return( false );
  1275. }
  1276. #ifdef WIN32
  1277. error_count++;
  1278. #endif //WIN32
  1279. break;
  1280. }
  1281. tokenptr = strtok ( NULL, "|");
  1282. }
  1283. }
  1284. #ifdef WIN32
  1285. if (settings->Port == 1 && ModemRegistry) {
  1286. /*
  1287. ** Send the init strings from the registry if available
  1288. */
  1289. char send_string[256] = {"AT"};
  1290. /*
  1291. ** Send the init string for hardware flow control
  1292. */
  1293. if (settings->HardwareFlowControl){
  1294. if (ModemRegistry->Get_Modem_Hardware_Flow_Control()) {
  1295. strcpy (&send_string[2], ModemRegistry->Get_Modem_Hardware_Flow_Control());
  1296. status = Send_Modem_Command (send_string, '\r', buffer, 81, 300, 1);
  1297. if (status != MODEM_CMD_OK && status != MODEM_CMD_0) {
  1298. if (WWMessageBox().Process(TXT_NO_FLOW_CONTROL_RESPONSE, TXT_IGNORE, TXT_CANCEL)) return (false);
  1299. }
  1300. }
  1301. }else{
  1302. /*
  1303. ** Send the init string for no flow control
  1304. */
  1305. if (ModemRegistry->Get_Modem_No_Flow_Control()) {
  1306. strcpy (&send_string[2], ModemRegistry->Get_Modem_No_Flow_Control());
  1307. status = Send_Modem_Command (send_string, '\r', buffer, 81, DEFAULT_TIMEOUT, 1);
  1308. if (status != MODEM_CMD_OK && status != MODEM_CMD_0) {
  1309. if (WWMessageBox().Process(TXT_NO_FLOW_CONTROL_RESPONSE, TXT_IGNORE, TXT_CANCEL)) return (false);
  1310. }
  1311. }
  1312. }
  1313. /*
  1314. ** Send the string for data compresseion
  1315. */
  1316. if (settings->Compression){
  1317. if (ModemRegistry->Get_Modem_Compression_Enable()) {
  1318. strcpy (&send_string[2], ModemRegistry->Get_Modem_Compression_Enable());
  1319. Send_Modem_Command (send_string, '\r', buffer, 81, DEFAULT_TIMEOUT, 1);
  1320. if (status != MODEM_CMD_OK && status != MODEM_CMD_0) {
  1321. if (WWMessageBox().Process(TXT_NO_COMPRESSION_RESPONSE, TXT_IGNORE, TXT_CANCEL)) return (false);
  1322. }
  1323. }
  1324. }else{
  1325. if (ModemRegistry->Get_Modem_Compression_Disable()) {
  1326. strcpy (&send_string[2], ModemRegistry->Get_Modem_Compression_Disable());
  1327. Send_Modem_Command (send_string, '\r', buffer, 81, DEFAULT_TIMEOUT, 1);
  1328. if (status != MODEM_CMD_OK && status != MODEM_CMD_0) {
  1329. if (WWMessageBox().Process(TXT_NO_COMPRESSION_RESPONSE, TXT_IGNORE, TXT_CANCEL)) return (false);
  1330. }
  1331. }
  1332. }
  1333. /*
  1334. ** Send the string for error correction
  1335. */
  1336. if (settings->ErrorCorrection){
  1337. if (ModemRegistry->Get_Modem_Error_Correction_Enable()) {
  1338. strcpy (&send_string[2], ModemRegistry->Get_Modem_Error_Correction_Enable());
  1339. Send_Modem_Command (send_string, '\r', buffer, 81, DEFAULT_TIMEOUT, 1);
  1340. if (status != MODEM_CMD_OK && status != MODEM_CMD_0) {
  1341. if (WWMessageBox().Process(TXT_NO_ERROR_CORRECTION_RESPONSE, TXT_IGNORE, TXT_CANCEL)) return (false);
  1342. }
  1343. }
  1344. }else{
  1345. if (ModemRegistry->Get_Modem_Error_Correction_Disable()) {
  1346. strcpy (&send_string[2], ModemRegistry->Get_Modem_Error_Correction_Disable());
  1347. Send_Modem_Command (send_string, '\r', buffer, 81, DEFAULT_TIMEOUT, 1);
  1348. if (status != MODEM_CMD_OK && status != MODEM_CMD_0) {
  1349. if (WWMessageBox().Process(TXT_NO_ERROR_CORRECTION_RESPONSE, TXT_IGNORE, TXT_CANCEL)) return (false);
  1350. }
  1351. }
  1352. }
  1353. }
  1354. #endif //WIN32
  1355. /*
  1356. ** We require that auto-answer be disabled so turn it off now.
  1357. */
  1358. status = Send_Modem_Command( "ATS0=0", '\r', buffer, 81, DEFAULT_TIMEOUT, INIT_COMMAND_RETRIES );
  1359. if (status != MODEM_CMD_OK) {
  1360. if (WWMessageBox().Process(TXT_ERROR_NO_DISABLE, TXT_IGNORE, TXT_CANCEL)) return( false );
  1361. error_count++;
  1362. }
  1363. /*
  1364. ** If we had an unreasonable number of ignored errors then return failure
  1365. */
  1366. if (error_count >= 3) {
  1367. WWMessageBox().Process(TXT_ERROR_TOO_MANY, TXT_OK);
  1368. return (false);
  1369. }
  1370. return( true );
  1371. }
  1372. /***************************************************************************
  1373. * NullModemClass::Dial_Modem -- dials a number passed *
  1374. * *
  1375. * INPUT: *
  1376. * settings ptr to SerialSettings structure *
  1377. * *
  1378. * OUTPUT: *
  1379. * status DialStatus *
  1380. * *
  1381. * WARNINGS: *
  1382. * none. *
  1383. * *
  1384. * HISTORY: *
  1385. * 06/02/1995 DRD : Created. *
  1386. * 8/2/96 ST : Win32 support *
  1387. *=========================================================================*/
  1388. DialStatusType NullModemClass::Dial_Modem( char *string, DialMethodType method, bool reconnect )
  1389. {
  1390. /*
  1391. ** Get the resolution factor
  1392. */
  1393. // int factor = SeenBuff.Get_Width()/320;
  1394. /*------------------------------------------------------------------------
  1395. Button Enumerations
  1396. ------------------------------------------------------------------------*/
  1397. enum {
  1398. BUTTON_CANCEL = 100,
  1399. };
  1400. /*------------------------------------------------------------------------
  1401. Dialog variables
  1402. ------------------------------------------------------------------------*/
  1403. bool process = true; // process while true
  1404. #ifndef WIN32
  1405. int status;
  1406. #endif //WIN32
  1407. int delay;
  1408. DialStatusType dialstatus;
  1409. int x,y,width,height; // dialog dimensions
  1410. char buffer[80*3];
  1411. /*------------------------------------------------------------------------
  1412. Determine the dimensions of the text to be used for the dialog box.
  1413. These dimensions will control how the dialog box looks.
  1414. ------------------------------------------------------------------------*/
  1415. if (reconnect) {
  1416. strcpy( buffer, Text_String( TXT_MODEM_CONNERR_REDIALING ) );
  1417. } else {
  1418. strcpy( buffer, Text_String( TXT_DIALING ) );
  1419. }
  1420. Fancy_Text_Print(TXT_NONE,0,0,TBLACK,TBLACK,TPF_TEXT);
  1421. Format_Window_String(buffer, SeenBuff.Get_Height(), width, height);
  1422. int text_width = width;
  1423. width = max(width, 90 * RESFACTOR);
  1424. width += 40 * RESFACTOR;
  1425. height += 60 * RESFACTOR;
  1426. x = (SeenBuff.Get_Width() - width) / 2;
  1427. y = (SeenBuff.Get_Height() - height) / 2;
  1428. TextButtonClass cancelbtn(BUTTON_CANCEL, TXT_CANCEL, TPF_BUTTON,
  1429. x + ((width - (String_Pixel_Width( Text_String( TXT_CANCEL ) ) + 8 * RESFACTOR)) >> 1),
  1430. y + height - (FontHeight + FontYSpacing + 2 * RESFACTOR) - 10 * RESFACTOR);
  1431. /*------------------------------------------------------------------------
  1432. Initialize
  1433. ------------------------------------------------------------------------*/
  1434. Set_Logic_Page(SeenBuff);
  1435. /*------------------------------------------------------------------------
  1436. Create the list
  1437. ------------------------------------------------------------------------*/
  1438. Commands = &cancelbtn;
  1439. Commands->Flag_List_To_Redraw();
  1440. /*------------------------------------------------------------------------
  1441. Draw the dialog
  1442. ------------------------------------------------------------------------*/
  1443. Hide_Mouse();
  1444. if ( !reconnect ) {
  1445. Load_Title_Page(true);
  1446. }
  1447. Dialog_Box(x, y, width, height);
  1448. Draw_Caption(TXT_NONE, x, y, width);
  1449. Fancy_Text_Print(buffer, SeenBuff.Get_Width()/2 - text_width/2, y + 25 *RESFACTOR, GadgetClass::Get_Color_Scheme(), TBLACK, TPF_TEXT);
  1450. Commands->Draw_All();
  1451. Show_Mouse();
  1452. /*
  1453. ** Start waiting for connection response
  1454. */
  1455. #ifdef WIN32
  1456. SerialPort->Set_Modem_Dial_Type((WinCommDialMethodType) method);
  1457. /*
  1458. ** Clear out any old modem results that might be hanging around
  1459. */
  1460. SerialPort->Get_Modem_Result(60, buffer, 81);
  1461. /*
  1462. ** Dial that sucker
  1463. */
  1464. SerialPort->Dial_Modem(string);
  1465. #else //WIN32
  1466. HMSetDialingMethod( Port, (int)method );
  1467. status = HMDial( Port, string );
  1468. #endif //WIN32
  1469. /*
  1470. ** Sets up the ability to abort modem commands when any input is in the
  1471. ** Keyboard buffer. This also calls the game CallBack().
  1472. */
  1473. Setup_Abort_Modem();
  1474. /*------------------------------------------------------------------------
  1475. Main Processing Loop
  1476. ------------------------------------------------------------------------*/
  1477. process = true;
  1478. delay = ModemWaitCarrier;
  1479. while (process) {
  1480. #ifdef WIN32
  1481. /*
  1482. ** If we have just received input focus again after running in the background then
  1483. ** we need to redraw.
  1484. */
  1485. if (AllSurfaces.SurfacesRestored) {
  1486. AllSurfaces.SurfacesRestored=FALSE;
  1487. Commands->Draw_All();
  1488. }
  1489. delay = SerialPort->Get_Modem_Result(delay, buffer, 81);
  1490. #else //WIN32
  1491. delay = HMInputLine( Port, delay, buffer, 81 );
  1492. #endif //WIN32
  1493. /*.....................................................................
  1494. Process input
  1495. .....................................................................*/
  1496. switch (Input) {
  1497. case (KN_ESC):
  1498. case (BUTTON_CANCEL | KN_BUTTON):
  1499. dialstatus = DIAL_CANCELED;
  1500. process = false;
  1501. break;
  1502. default:
  1503. break;
  1504. }
  1505. if (process) {
  1506. if ( strncmp( buffer, "CON", 3 ) == 0 ) {
  1507. memset (ModemRXString, 0, 80);
  1508. strncpy (ModemRXString, buffer, 79);
  1509. dialstatus = DIAL_CONNECTED;
  1510. process = false;
  1511. } else if ( strncmp( buffer, "BUSY", 4 ) == 0 ) {
  1512. dialstatus = DIAL_BUSY;
  1513. process = false;
  1514. } else if ( strncmp( buffer, "NO C", 4 ) == 0 ) {
  1515. dialstatus = DIAL_NO_CARRIER;
  1516. process = false;
  1517. } else if ( strncmp( buffer, "NO D", 4 ) == 0 ) {
  1518. dialstatus = DIAL_NO_DIAL_TONE;
  1519. process = false;
  1520. } else if ( strncmp( buffer, "ERRO", 4 ) == 0 ) {
  1521. dialstatus = DIAL_ERROR;
  1522. process = false;
  1523. }
  1524. }
  1525. if (delay <= 0) {
  1526. process = false;
  1527. }
  1528. }
  1529. Remove_Abort_Modem();
  1530. return( dialstatus );
  1531. } /* end of Dial_Modem */
  1532. /***************************************************************************
  1533. * NullModemClass::Answer_Modem -- waits for call and answers *
  1534. * *
  1535. * INPUT: *
  1536. * reconnect whether this is to reconnect *
  1537. * *
  1538. * OUTPUT: *
  1539. * status DialStatus *
  1540. * *
  1541. * WARNINGS: *
  1542. * none. *
  1543. * *
  1544. * HISTORY: *
  1545. * 06/02/1995 DRD : Created. *
  1546. * 8/2/96 ST : Added Win32 support *
  1547. *=========================================================================*/
  1548. DialStatusType NullModemClass::Answer_Modem( bool reconnect )
  1549. {
  1550. /*
  1551. ** Get the resolution factor
  1552. */
  1553. // int factor = (SeenBuff.Get_Width() == 320) ? 1 : 2;
  1554. /*------------------------------------------------------------------------
  1555. Button Enumerations
  1556. ------------------------------------------------------------------------*/
  1557. enum {
  1558. BUTTON_CANCEL = 100,
  1559. };
  1560. /*------------------------------------------------------------------------
  1561. Redraw values: in order from "top" to "bottom" layer of the dialog
  1562. ------------------------------------------------------------------------*/
  1563. typedef enum {
  1564. REDRAW_NONE = 0,
  1565. REDRAW_BUTTONS,
  1566. REDRAW_BACKGROUND,
  1567. REDRAW_ALL = REDRAW_BACKGROUND
  1568. } RedrawType;
  1569. /*------------------------------------------------------------------------
  1570. Dialog variables
  1571. ------------------------------------------------------------------------*/
  1572. RedrawType display = REDRAW_ALL; // redraw level
  1573. bool process = true; // process while true
  1574. #ifndef WIN32
  1575. int status;
  1576. #endif //WIN32
  1577. int delay;
  1578. DialStatusType dialstatus;
  1579. bool ring = false;
  1580. int x,y,width,height; // dialog dimensions
  1581. int text_width;
  1582. char text_buffer[80*3];
  1583. char comm_buffer[80*3];
  1584. /*------------------------------------------------------------------------
  1585. Determine the dimensions of the text to be used for the dialog box.
  1586. These dimensions will control how the dialog box looks.
  1587. ------------------------------------------------------------------------*/
  1588. if (reconnect) {
  1589. strcpy( text_buffer, Text_String( TXT_MODEM_CONNERR_WAITING ) );
  1590. } else {
  1591. strcpy( text_buffer, Text_String( TXT_WAITING_FOR_CALL ) );
  1592. }
  1593. Fancy_Text_Print(TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TEXT);
  1594. Format_Window_String(text_buffer, SeenBuff.Get_Height(), width, height);
  1595. text_width = width;
  1596. width = max(width, 90 * RESFACTOR);
  1597. width += 40 * RESFACTOR;
  1598. height += 60 * RESFACTOR;
  1599. x = (SeenBuff.Get_Width() - width) / 2;
  1600. y = (SeenBuff.Get_Height() - height) / 2;
  1601. TextButtonClass cancelbtn(BUTTON_CANCEL, TXT_CANCEL, TPF_BUTTON,
  1602. x + ((width - (String_Pixel_Width( Text_String( TXT_CANCEL ) ) + 8 * RESFACTOR)) >> 1),
  1603. y + height - (FontHeight + FontYSpacing + 2 * RESFACTOR) - 10 * RESFACTOR);
  1604. /*------------------------------------------------------------------------
  1605. Initialize
  1606. ------------------------------------------------------------------------*/
  1607. Set_Logic_Page(SeenBuff);
  1608. Load_Title_Page(true);
  1609. Input = KN_NONE;
  1610. /*------------------------------------------------------------------------
  1611. Create the list
  1612. ------------------------------------------------------------------------*/
  1613. Commands = &cancelbtn;
  1614. Commands->Flag_List_To_Redraw();
  1615. /*
  1616. ** Sets up the ability to abort modem commands when any input is in the
  1617. ** Keyboard buffer. This also calls the game CallBack() and Input().
  1618. */
  1619. Setup_Abort_Modem();
  1620. /*------------------------------------------------------------------------
  1621. Main Processing Loop
  1622. ------------------------------------------------------------------------*/
  1623. process = true;
  1624. delay = 60000;
  1625. while (process) {
  1626. #ifdef WIN32
  1627. /*
  1628. ** If we have just received input focus again after running in the background then
  1629. ** we need to redraw.
  1630. */
  1631. if (AllSurfaces.SurfacesRestored) {
  1632. AllSurfaces.SurfacesRestored=FALSE;
  1633. display=REDRAW_ALL;
  1634. }
  1635. #endif //WIN32
  1636. /*.....................................................................
  1637. Refresh display if needed
  1638. .....................................................................*/
  1639. if (display) {
  1640. Hide_Mouse();
  1641. if (display >= REDRAW_BACKGROUND) {
  1642. /*...............................................................
  1643. Refresh the backdrop
  1644. ...............................................................*/
  1645. if ( !reconnect ) {
  1646. Load_Title_Page(true);
  1647. }
  1648. /*...............................................................
  1649. Draw the background
  1650. ...............................................................*/
  1651. Dialog_Box(x, y, width, height);
  1652. /*...............................................................
  1653. Draw the labels
  1654. ...............................................................*/
  1655. Draw_Caption(TXT_NONE, x, y, width);
  1656. Fancy_Text_Print(text_buffer, SeenBuff.Get_Width()/2 - text_width/2, y + 25 * RESFACTOR,
  1657. GadgetClass::Get_Color_Scheme(), TBLACK, TPF_TEXT);
  1658. Commands->Draw_All();
  1659. }
  1660. Show_Mouse();
  1661. display = REDRAW_NONE;
  1662. }
  1663. #ifdef WIN32
  1664. delay = SerialPort->Get_Modem_Result(delay, comm_buffer, 81);
  1665. #else //WIN32
  1666. delay = HMInputLine( Port, delay, comm_buffer, 81 );
  1667. #endif //WIN32
  1668. /*.....................................................................
  1669. Process input
  1670. .....................................................................*/
  1671. if (!Input) Input = Commands->Input();
  1672. switch (Input) {
  1673. case (KN_ESC):
  1674. case (BUTTON_CANCEL | KN_BUTTON):
  1675. dialstatus = DIAL_CANCELED;
  1676. process = false;
  1677. break;
  1678. default:
  1679. break;
  1680. }
  1681. if (process) {
  1682. if ( strncmp( comm_buffer, "RING", 4 ) == 0 ) {
  1683. strcpy( text_buffer, Text_String( TXT_ANSWERING ) );
  1684. Fancy_Text_Print(TXT_NONE,0,0,TBLACK,TBLACK,TPF_TEXT);
  1685. Format_Window_String(text_buffer, SeenBuff.Get_Height(), width, height);
  1686. text_width = width;
  1687. width = max(width, 90 * RESFACTOR);
  1688. width += 40 * RESFACTOR;
  1689. height += 60 * RESFACTOR;
  1690. x = (SeenBuff.Get_Width() - width) / 2;
  1691. y = (SeenBuff.Get_Height() - height) / 2;
  1692. #ifdef WIN32
  1693. SerialPort->Write_To_Serial_Port ((unsigned char*)"ATA\r", strlen("ATA\r"));
  1694. #else //WIN32
  1695. PortKillTime( Port, 100 );
  1696. HMWaitForOK( 0, NULL );
  1697. status = HMAnswer( Port );
  1698. #endif //WIN32
  1699. ring = true;
  1700. delay = ModemWaitCarrier;
  1701. display = REDRAW_ALL;
  1702. } else if ( strncmp( comm_buffer, "CON", 3 ) == 0 ) {
  1703. memset (ModemRXString, 0, 80);
  1704. strncpy (ModemRXString, comm_buffer, 79);
  1705. dialstatus = DIAL_CONNECTED;
  1706. process = false;
  1707. } else if ( strncmp( comm_buffer, "BUSY", 4 ) == 0 ) {
  1708. dialstatus = DIAL_BUSY;
  1709. process = false;
  1710. } else if ( strncmp( comm_buffer, "NO C", 4 ) == 0 ) {
  1711. dialstatus = DIAL_NO_CARRIER;
  1712. process = false;
  1713. } else if ( strncmp( comm_buffer, "ERRO", 4 ) == 0 ) {
  1714. dialstatus = DIAL_ERROR;
  1715. WWMessageBox().Process(TXT_ERROR_ERROR, TXT_OK);
  1716. process = false;
  1717. }
  1718. }
  1719. #ifdef WIN32
  1720. if (delay <= 0) {
  1721. if (ring) {
  1722. if (SerialPort->Get_Modem_Status() & CD_SET) {
  1723. sprintf(ModemRXString, "%s", "Connected");
  1724. dialstatus = DIAL_CONNECTED;
  1725. } else {
  1726. dialstatus = DIAL_ERROR;
  1727. WWMessageBox().Process(TXT_ERROR_TIMEOUT, TXT_OK);
  1728. }
  1729. process = false;
  1730. } else {
  1731. delay = 60000;
  1732. }
  1733. }
  1734. #else //WIN32
  1735. if (delay <= 0) {
  1736. if (ring) {
  1737. process = false;
  1738. } else {
  1739. delay = 60000;
  1740. }
  1741. }
  1742. #endif //WIN32
  1743. }
  1744. Remove_Abort_Modem();
  1745. return( dialstatus );
  1746. } /* end of Answer_Modem */
  1747. /***************************************************************************
  1748. * NullModemClass::Hangup_Modem -- hangs up the modem *
  1749. * *
  1750. * INPUT: *
  1751. * none *
  1752. * *
  1753. * OUTPUT: *
  1754. * status successful or not *
  1755. * *
  1756. * WARNINGS: *
  1757. * none. *
  1758. * *
  1759. * HISTORY: *
  1760. * 06/02/1995 DRD : Created. *
  1761. * 8/2/96 : Added Win32 support *
  1762. *=========================================================================*/
  1763. bool NullModemClass::Hangup_Modem( void )
  1764. {
  1765. int status;
  1766. int delay;
  1767. char buffer[81];
  1768. char escape[4];
  1769. /*
  1770. ** Turn modem servicing off in the callback routine.
  1771. */
  1772. Session.ModemService = false;
  1773. status = Send_Modem_Command( "AT", '\r', buffer, 81, DEFAULT_TIMEOUT, 1 );
  1774. if (status == MODEM_CMD_OK) {
  1775. Session.ModemService = true;
  1776. return( true );
  1777. }
  1778. /*
  1779. ** Toggle DTR low then high
  1780. */
  1781. #ifdef WIN32
  1782. SerialPort->Set_Serial_DTR(FALSE);
  1783. Delay(3200/60);
  1784. SerialPort->Set_Serial_DTR(TRUE);
  1785. #else //WIN32
  1786. SetDtr( Port, 0 );
  1787. PortKillTime( Port, 3200 );
  1788. SetDtr( Port, 1 );
  1789. #endif //WIN32
  1790. status = Send_Modem_Command( "AT", '\r', buffer, 81, DEFAULT_TIMEOUT, 1 );
  1791. if (status == MODEM_CMD_OK) {
  1792. Session.ModemService = true;
  1793. return( true );
  1794. }
  1795. delay = ModemGuardTime;
  1796. while ( delay > 0 ) {
  1797. #ifdef WIN32
  1798. delay = SerialPort->Get_Modem_Result(delay, buffer, 81);
  1799. #else //WIN32
  1800. delay = HMInputLine( Port, delay, buffer, 81 );
  1801. #endif //WIN32
  1802. }
  1803. /*
  1804. ** Send modem break commmand
  1805. */
  1806. escape[0] = ModemEscapeCode;
  1807. escape[1] = ModemEscapeCode;
  1808. escape[2] = ModemEscapeCode;
  1809. escape[3] = 0;
  1810. #ifdef WIN32
  1811. SerialPort->Write_To_Serial_Port((unsigned char*)escape, 3);
  1812. #else //WIN32
  1813. status = HMSendStringNoWait( Port, escape, -1 );
  1814. #endif //WIN32
  1815. delay = ModemGuardTime;
  1816. while ( delay > 0 ) {
  1817. #ifdef WIN32
  1818. delay = SerialPort->Get_Modem_Result(delay, buffer, 81);
  1819. #else //WIN32
  1820. delay = HMInputLine( Port, delay, buffer, 81 );
  1821. #endif //WIN32
  1822. if ( strncmp( buffer, "OK", 2 ) == 0 ) {
  1823. break;
  1824. }
  1825. }
  1826. /*
  1827. ** Send the hangup command
  1828. */
  1829. status = Send_Modem_Command( "ATH", '\r', buffer, 81, ModemHangupDelay, 1 );
  1830. if (status == MODEM_CMD_OK) {
  1831. } else {
  1832. Session.ModemService = true;
  1833. return( false );
  1834. }
  1835. /*
  1836. ** Send spurious ATZ command for no apparent reason
  1837. */
  1838. status = Send_Modem_Command( "ATZ", '\r', buffer, 81, 5000, 1 );
  1839. if (status != MODEM_CMD_OK) {
  1840. Session.ModemService = true;
  1841. return( false );
  1842. }
  1843. Session.ModemService = true;
  1844. return( true );
  1845. } /* end of Hangup_Modem */
  1846. /***********************************************************************************************
  1847. * NMC::Setup_Modem_Echo -- Sets the echo callback function pointer *
  1848. * *
  1849. * *
  1850. * *
  1851. * INPUT: Ptr to callback function *
  1852. * *
  1853. * OUTPUT: Nothing *
  1854. * *
  1855. * WARNINGS: None *
  1856. * *
  1857. * HISTORY: *
  1858. * 8/2/96 12:48PM ST : Documented and added WIn32 support *
  1859. *=============================================================================================*/
  1860. void NullModemClass::Setup_Modem_Echo( void ( *func )( char c) )
  1861. {
  1862. #ifdef WIN32
  1863. SerialPort->Set_Echo_Function(func);
  1864. #else //WIN32
  1865. HMSetUpEchoRoutine( func );
  1866. #endif //WIN32
  1867. } /* end of Setup_Modem_Echo */
  1868. /***********************************************************************************************
  1869. * NMC::Remove_Modem_Echo -- Set the echo function callback pointer to null *
  1870. * *
  1871. * *
  1872. * *
  1873. * INPUT: Nothing *
  1874. * *
  1875. * OUTPUT: Nothing *
  1876. * *
  1877. * WARNINGS: None *
  1878. * *
  1879. * HISTORY: *
  1880. * 8/2/96 12:50PM ST : Documented / Win32 support added *
  1881. *=============================================================================================*/
  1882. void NullModemClass::Remove_Modem_Echo( void )
  1883. {
  1884. // Smart_Printf( "Remove Echo modem code\n" );
  1885. HMSetUpEchoRoutine( NULL );
  1886. } /* end of Remove_Modem_Echo */
  1887. /***********************************************************************************************
  1888. * NMC::Print_EchoBuf -- Print out the contents of the echo buffer *
  1889. * *
  1890. * *
  1891. * *
  1892. * INPUT: Nothing *
  1893. * *
  1894. * OUTPUT: Nothing *
  1895. * *
  1896. * WARNINGS: None *
  1897. * *
  1898. * HISTORY: *
  1899. * 8/2/96 12:51PM ST : Documented *
  1900. *=============================================================================================*/
  1901. void NullModemClass::Print_EchoBuf( void )
  1902. {
  1903. for (int i = 0; i < strlen(NullModem.EchoBuf); i++) {
  1904. if (NullModem.EchoBuf[i] == '\r') {
  1905. NullModem.EchoBuf[i] = 1;
  1906. } else {
  1907. if (NullModem.EchoBuf[i] == '\n') {
  1908. NullModem.EchoBuf[i] = 2;
  1909. }
  1910. }
  1911. }
  1912. // Smart_Printf( "Echo buffer length %d (%s)\n", NullModem.EchoCount, NullModem.EchoBuf );
  1913. }
  1914. /***********************************************************************************************
  1915. * NMC::Reset_EchoBuf -- Empties the echo buffer *
  1916. * *
  1917. * *
  1918. * *
  1919. * INPUT: Nothing *
  1920. * *
  1921. * OUTPUT: Nothing *
  1922. * *
  1923. * WARNINGS: None *
  1924. * *
  1925. * HISTORY: *
  1926. * 8/2/96 12:51PM ST : Documented *
  1927. *=============================================================================================*/
  1928. void NullModemClass::Reset_EchoBuf( void )
  1929. {
  1930. *EchoBuf = 0;
  1931. EchoCount = 0;
  1932. }
  1933. /***********************************************************************************************
  1934. * NMC::Abort_Modem -- Checks for user input so that modem operations can be aborted *
  1935. * *
  1936. * *
  1937. * *
  1938. * INPUT: Nothing *
  1939. * *
  1940. * OUTPUT: ASUSERABORT if abort key pressed. ASSUCESS otherwise. *
  1941. * *
  1942. * WARNINGS: None *
  1943. * *
  1944. * HISTORY: *
  1945. * 8/2/96 12:52PM ST : Documented *
  1946. *=============================================================================================*/
  1947. int NullModemClass::Abort_Modem( PORT * )
  1948. {
  1949. /*
  1950. ** Button Enumerations
  1951. */
  1952. enum {
  1953. BUTTON_CANCEL = 100,
  1954. };
  1955. /*
  1956. ** Invoke game callback
  1957. */
  1958. Call_Back();
  1959. /*
  1960. ** Get user input
  1961. */
  1962. Input = Commands->Input();
  1963. switch ( Input ) {
  1964. case (KN_ESC):
  1965. case (BUTTON_CANCEL | KN_BUTTON):
  1966. return( ASUSERABORT );
  1967. }
  1968. return( ASSUCCESS );
  1969. } /* end of Abort_Modem */
  1970. /***********************************************************************************************
  1971. * NMC::Setup_Abort_Modem -- sets the modem abort function pointer *
  1972. * *
  1973. * *
  1974. * *
  1975. * INPUT: Nothing *
  1976. * *
  1977. * OUTPUT: Nothing *
  1978. * *
  1979. * WARNINGS: None *
  1980. * *
  1981. * HISTORY: *
  1982. * 8/2/96 2:59PM ST : Documented / Win32 support added *
  1983. *=============================================================================================*/
  1984. void NullModemClass::Setup_Abort_Modem( void )
  1985. {
  1986. #ifdef WIN32
  1987. SerialPort->Set_Abort_Function((int(*)(void))Abort_Modem);
  1988. #else //WIN32
  1989. /*
  1990. ** save off original abort modem function to later restore
  1991. */
  1992. OrigAbortModemFunc = _AbortModemFunctionPtr;
  1993. HMSetUpAbortKey( ESC );
  1994. SetAbortModemFunctionPtr( Abort_Modem );
  1995. #endif //WIN32
  1996. } /* end of Setup_Abort_Modem */
  1997. /***********************************************************************************************
  1998. * NMC::Remove_Abort_Modem -- Removes the modem abort function pointer *
  1999. * *
  2000. * *
  2001. * *
  2002. * INPUT: Nothing *
  2003. * *
  2004. * OUTPUT: Nothing *
  2005. * *
  2006. * WARNINGS: None *
  2007. * *
  2008. * HISTORY: *
  2009. * 8/2/96 3:01PM ST : Documented / Win32 support added *
  2010. *=============================================================================================*/
  2011. void NullModemClass::Remove_Abort_Modem( void )
  2012. {
  2013. #ifdef WIN32
  2014. SerialPort->Set_Abort_Function(NULL);
  2015. #else //WIN32
  2016. HMSetUpAbortKey( NOKEY );
  2017. /*
  2018. ** Restore the original abort modem function
  2019. */
  2020. _AbortModemFunctionPtr = OrigAbortModemFunc;
  2021. #endif //WIN32
  2022. } /* end of Remove_Abort_Modem */
  2023. /***********************************************************************************************
  2024. * NMC::Change_IRQ_Priority -- Increases the priority of the serial interrupt *
  2025. * *
  2026. * *
  2027. * *
  2028. * INPUT: Interrupt request number *
  2029. * *
  2030. * OUTPUT: ASSUCCESS if changed *
  2031. * *
  2032. * WARNINGS: The Win32 version of this function does nothing. *
  2033. * Priorities are controlled by windoze *
  2034. * *
  2035. * HISTORY: *
  2036. * 8/2/96 3:03PM ST : Documented / Win32 support added *
  2037. *=============================================================================================*/
  2038. int NullModemClass::Change_IRQ_Priority( int irq )
  2039. {
  2040. #ifdef WIN32
  2041. irq = irq;
  2042. return ( ASSUCCESS );
  2043. #else //WIN32
  2044. if (irq != OldIRQPri) {
  2045. OldIRQPri = irq;
  2046. return( Change8259Priority( irq ) );
  2047. } else {
  2048. return( ASSUCCESS );
  2049. }
  2050. #endif //WIN32
  2051. } /* end of Change_IRQ_Priority */
  2052. /***********************************************************************************************
  2053. * NMC::Get_Modem_Status -- returns status of modem control bits *
  2054. * *
  2055. * *
  2056. * *
  2057. * INPUT: Nothing *
  2058. * *
  2059. * OUTPUT: Modem status *
  2060. * *
  2061. * WARNINGS: None *
  2062. * *
  2063. * HISTORY: *
  2064. * 8/2/96 3:06PM ST : Documented / Win32 support added *
  2065. *=============================================================================================*/
  2066. int NullModemClass::Get_Modem_Status( void )
  2067. {
  2068. int modemstatus;
  2069. int status;
  2070. char buffer[81];
  2071. #ifdef WIN32
  2072. modemstatus = SerialPort->Get_Modem_Status();
  2073. #else //WIN32
  2074. modemstatus = GetModemStatus( Port );
  2075. #endif //WIN32
  2076. status = Send_Modem_Command( "AT", '\r', buffer, 81, DEFAULT_TIMEOUT, 1 );
  2077. if (status == MODEM_CMD_OK) {
  2078. modemstatus &= (~CD_SET);
  2079. }
  2080. return( modemstatus );
  2081. } /* end of Get_Modem_Status */
  2082. /***********************************************************************************************
  2083. * NMC::Send_Modem_Command -- Sends an 'AT' command to the modem and gets the response *
  2084. * *
  2085. * *
  2086. * *
  2087. * INPUT: command to send to modem. e.g. 'ATZ' *
  2088. * terminator byte for command string *
  2089. * buffer to put modem response into *
  2090. * length of above buffer *
  2091. * delay to wait for response *
  2092. * number of times to retry when modem doesnt respond *
  2093. * *
  2094. * OUTPUT: input delay less the time it took the modem to respond *
  2095. * *
  2096. * WARNINGS: None *
  2097. * *
  2098. * HISTORY: *
  2099. * 8/2/96 3:09PM ST : Documented / Win32 support added *
  2100. *=============================================================================================*/
  2101. int NullModemClass::Send_Modem_Command( char *command, char terminator, char *buffer, int buflen, int delay, int retries )
  2102. {
  2103. #ifdef WIN32
  2104. return (SerialPort->Send_Command_To_Modem(command, terminator, buffer, buflen, delay, retries));
  2105. #else //WIN32
  2106. int status, tmpdelay, retry;
  2107. char tempbuf[81];
  2108. *buffer = 0;
  2109. for ( retry = 0; retry < retries; retry++ ) {
  2110. PortKillTime( Port, 100 );
  2111. status = HMSendStringNoWait( Port, command, terminator );
  2112. if (status < ASSUCCESS) {
  2113. return( status );
  2114. }
  2115. tmpdelay = delay;
  2116. while ( tmpdelay > 0 ) {
  2117. tmpdelay = HMInputLine( Port, tmpdelay, tempbuf, 81 );
  2118. if ( strcmp( tempbuf, "OK" ) == 0 ) {
  2119. return( MODEM_CMD_OK );
  2120. } else if ( strcmp( tempbuf, "0" ) == 0 ) {
  2121. return( MODEM_CMD_0 );
  2122. } else if ( strcmp( tempbuf, "ERROR" ) == 0 ) {
  2123. return( MODEM_CMD_ERROR );
  2124. } else if ( tempbuf[0] != 0 ) {
  2125. strncpy( buffer, tempbuf, buflen );
  2126. }
  2127. }
  2128. PortKillTime( Port, 100 );
  2129. /*
  2130. ** Send and extra return to help clear out any problems with the modem (if any).
  2131. */
  2132. HMWaitForOK( 300, NULL );
  2133. status = HMSendString( Port, "" );
  2134. HMWaitForOK( 0, NULL );
  2135. }
  2136. return( tmpdelay );
  2137. #endif //WIN32
  2138. }
  2139. /***********************************************************************************************
  2140. * NMC::Verify_And_Convert_To_Int -- converts a text string of numbers to an int *
  2141. * *
  2142. * *
  2143. * *
  2144. * INPUT: ptr to buffer *
  2145. * *
  2146. * OUTPUT: value of text number in buffer *
  2147. * *
  2148. * WARNINGS: None *
  2149. * *
  2150. * HISTORY: *
  2151. * 8/2/96 3:13PM ST : Documented *
  2152. *=============================================================================================*/
  2153. int NullModemClass::Verify_And_Convert_To_Int( char *buffer )
  2154. {
  2155. int value = 0;
  2156. int len = strlen(buffer);
  2157. for (int i = 0; i < len; i++) {
  2158. if ( !isdigit( *(buffer + i) ) ) {
  2159. value = -1;
  2160. break;
  2161. }
  2162. }
  2163. if (value == 0) {
  2164. value = atoi( buffer );
  2165. }
  2166. return( value );
  2167. } /* end of Verify_And_Convert_To_Int */
  2168. /*************************** end of nullmgr.cpp ****************************/
  2169. #endif