NULLMGR.CPP 94 KB

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