123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705 |
- #define BUILDING_OPENDOORS
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #include "OpenDoor.h"
- #ifdef ODPLAT_NIX
- #include <sys/ioctl.h>
- #include <signal.h>
- #include <termios.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #ifdef __sun
- #include <sys/filio.h>
- #endif
- #endif
- #include "ODCore.h"
- #include "ODGen.h"
- #include "ODPlat.h"
- #include "ODCom.h"
- #include "ODUtil.h"
- #define ASSERT_ON_INVALID_CALLS
- #ifdef ASSERT_ON_INVALID_CALLS
- #define VERIFY_CALL(x) ASSERT(x)
- #else
- #define VERIFY_CALL(x) if(x) return(kODRCInvalidCall)
- #endif
- #ifdef ODPLAT_DOS
- #define INCLUDE_FOSSIL_COM
- #define INCLUDE_UART_COM
- #endif
- #ifdef ODPLAT_WIN32
- #define INCLUDE_WIN32_COM
- #define INCLUDE_DOOR32_COM
- #define INCLUDE_SOCKET_COM
- #endif
- #ifdef ODPLAT_NIX
- #define INCLUDE_STDIO_COM
- #define INCLUDE_SOCKET_COM
- #define SOCKET int
- #define WSAEWOULDBLOCK EAGAIN
- #define SOCKET_ERROR -1
- #define WSAGetLastError() errno
- #define ioctlsocket ioctl
- #define closesocket close
- #endif
- #ifdef INCLUDE_WIN32_COM
- #include "windows.h"
- #endif
- #ifdef INCLUDE_STDIO_COM
- struct termios tio_default;
- #endif
- #if defined(_WIN32) && defined(INCLUDE_SOCKET_COM)
- #include <winsock.h>
- static WSADATA WSAData;
- #endif
- #ifdef INCLUDE_WIN32_COM
- typedef enum
- {
- kNotSet,
- kBlocking,
- kNonBlocking
- } tReadTimeoutState;
- #endif
- typedef struct
- {
- BOOL bIsOpen;
- BOOL bUsingClientsHandle;
- BYTE btFlowControlSetting;
- long lSpeed;
- BYTE btPort;
- int nPortAddress;
- BYTE btIRQLevel;
- BYTE btWordFormat;
- int nReceiveBufferSize;
- int nTransmitBufferSize;
- BYTE btFIFOSetting;
- tComMethod Method;
- void (*pfIdleCallback)(void);
- #ifdef INCLUDE_WIN32_COM
- HANDLE hCommDev;
- tReadTimeoutState ReadTimeoutState;
- #endif
- #ifdef INCLUDE_DOOR32_COM
- HINSTANCE hinstDoor32DLL;
- BOOL (WINAPI *pfDoorInitialize)(void);
- BOOL (WINAPI *pfDoorShutdown)(void);
- BOOL (WINAPI *pfDoorWrite)(const BYTE *pbData, DWORD dwSize);
- DWORD (WINAPI *pfDoorRead)(BYTE *pbData, DWORD dwSize);
- HANDLE (WINAPI *pfDoorGetAvailableEventHandle)(void);
- HANDLE (WINAPI *pfDoorGetOfflineEventHandle)(void);
- #endif
- #ifdef INCLUDE_SOCKET_COM
- SOCKET socket;
- int old_delay;
- #endif
- } tPortInfo;
- #ifdef INCLUDE_UART_COM
- static void ODComSetVect(BYTE btVector, void (INTERRUPT far *pfISR)(void));
- static void (INTERRUPT far *ODComGetVect(BYTE btVector))(void);
- static void INTERRUPT ODComInternalISR();
- static BOOL ODComInternalTXReady(void);
- static void ODComInternalResetRX(void);
- static void ODComInternalResetTX(void);
- #define TXBUFF 0
- #define RXBUFF 0
- #define DLLSB 0
- #define DLMSB 1
- #define IER 1
- #define IIR 2
- #define LCR 3
- #define MCR 4
- #define LSR 5
- #define MSR 6
- #define FE 0x01
- #define RR 0x02
- #define TR 0x04
- #define FTS_1 0x00
- #define FTS_4 0x40
- #define FTS_8 0x80
- #define FTS_14 0xc0
- #define DTR 0x01
- #define NOT_DTR 0xfe
- #define RTS 0x02
- #define NOT_RTS 0xfd
- #define OUT1 0x04
- #define OUT2 0x08
- #define LPBK 0x10
- #define DCTS 0x01
- #define DDSR 0x02
- #define TERI 0x04
- #define DRLSD 0x08
- #define CTS 0x10
- #define DSR 0x20
- #define RI 0x40
- #define RLSD 0x80
- #define DATA5 0x00
- #define DATA6 0x01
- #define DATA7 0x02
- #define DATA8 0x03
- #define STOP1 0x00
- #define STOP2 0x04
- #define NOPAR 0x00
- #define ODDPAR 0x08
- #define EVNPAR 0x18
- #define STKPAR 0x28
- #define ZROPAR 0x38
- #define DLATCH 0x80
- #define NOT_DL 0x7f
- #define RDR 0x01
- #define ERRS 0x1E
- #define TXR 0x20
- #define DR 0x01
- #define THRE 0x02
- #define RLS 0x04
- #define MS 0x08
- #define RECEIVE_LOW_NUM 1
- #define RECEIVE_LOW_DENOM 4
- #define RECEIVE_HIGH_NUM 3
- #define RECEIVE_HIGH_DENOM 4
- static int anDefaultPortAddr[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8,
- 0x4220, 0x4228, 0x5220, 0x5228};
- static int nDataRegAddr;
- static int nIntEnableRegAddr;
- static int nIntIDRegAddr;
- static int nLineCtrlRegAddr;
- static int nModemCtrlRegAddr;
- static int nLineStatusRegAddr;
- static int nModemStatusRegAddr;
- static BYTE btIntVector;
- static char btI8259Bit;
- static char btI8259Mask;
- static int nI8259MaskRegAddr;
- static int nI8259EndOfIntRegAddr;
- static int nI8259MasterEndOfIntRegAddr;
- static char btOldIntEnableReg;
- static char btOldModemCtrlReg;
- static void (INTERRUPT far *pfOldISR)();
- static char bUsingFIFO = FALSE;
- static unsigned char btBaseFIFOCtrl;
- static int nTXQueueSize;
- static char *pbtTXQueue;
- static int nTXInIndex;
- static int nTXOutIndex;
- static int nTXChars;
- static int nRXQueueSize;
- static char *pbtRXQueue;
- static int nRXInIndex;
- static int nRXOutIndex;
- static int nRXChars;
- static int nRXHighWaterMark;
- static int nRXLowWaterMark;
- static BYTE btFlowControl;
- static BOOL bStopTrans;
- static void ODComSetVect(BYTE btVector, void (INTERRUPT far *pfISR)(void))
- {
- ASM push ds
- ASM mov ah, 0x25
- ASM mov al, btVector
- ASM lds dx, pfISR
- ASM int 0x21
- ASM pop ds
- }
- static void (INTERRUPT far *ODComGetVect(BYTE btVector))(void)
- {
- void (INTERRUPT far *pfISR)(void);
- ASM push es
- ASM mov ah, 0x35
- ASM mov al, btVector
- ASM int 0x21
- ASM mov word ptr pfISR, bx
- ASM mov word ptr [pfISR+2], bx
- ASM pop es
- return(pfISR);
- }
- static BOOL ODComInternalTXReady(void)
- {
-
- return(nTXChars < nTXQueueSize);
- }
- static void ODComInternalResetTX(void)
- {
-
- ASM cli
-
-
- if(bUsingFIFO)
- {
- ASM mov al, btBaseFIFOCtrl
- ASM or al, TR
- ASM mov dx, nIntIDRegAddr
- ASM out dx, al
- }
-
-
-
- nTXChars = nTXInIndex = nTXOutIndex = 0;
-
- ASM sti
- }
- static void ODComInternalResetRX(void)
- {
-
- ASM cli
-
-
- if(bUsingFIFO)
- {
- ASM mov al, btBaseFIFOCtrl
- ASM or al, RR
- ASM mov dx, nIntIDRegAddr
- ASM out dx, al
- }
-
-
-
- nRXChars = nRXInIndex = nRXOutIndex = 0;
-
- ASM sti
- }
- static void INTERRUPT ODComInternalISR()
- {
- char btIIR;
- BYTE btTemp;
-
-
- for(;;)
- {
-
-
- ASM mov dx, nIntIDRegAddr
- ASM in al, dx
- ASM mov btIIR, al
-
- if(btIIR & 0x01) break;
-
-
-
- switch(btIIR & 0x06)
- {
- case 0x00:
-
-
- ASM mov dx, nModemStatusRegAddr
- ASM in al, dx
- ASM mov btTemp, al
-
-
-
- if((btFlowControl & FLOW_RTSCTS) && (btTemp & DCTS))
- {
- if(btTemp & CTS)
- {
-
- bStopTrans = FALSE;
-
-
- if(nTXChars > 0)
- {
-
- ASM mov dx, nIntEnableRegAddr
- ASM in al, dx
- ASM or al, THRE
- ASM out dx, al
- }
- }
- else
- {
-
- bStopTrans = TRUE;
- }
- }
- break;
- case 0x02:
-
-
- if(nTXChars <= 0 || bStopTrans)
- {
-
-
- ASM mov dx, nIntEnableRegAddr
- ASM in al, dx
- ASM and al, 0xfd
- ASM out dx, al
- }
- else
- {
-
-
-
-
-
- ASM mov dx, nLineStatusRegAddr
- ASM in al, dx
- ASM mov btTemp, al
- if(btTemp & TXR)
- {
-
-
- btTemp = pbtTXQueue[nTXOutIndex++];
-
- ASM mov dx, nDataRegAddr
- ASM mov al, btTemp
- ASM out dx, al
-
- if (nTXOutIndex == nTXQueueSize)
- {
- nTXOutIndex = 0;
- }
-
- nTXChars--;
- }
- }
- break;
- case 0x04:
-
-
- ASM mov dx, nDataRegAddr
- ASM in al, dx
- ASM mov btTemp, al
-
- if(nRXChars >= nRXHighWaterMark)
- {
-
-
- if(btFlowControl & FLOW_RTSCTS)
- {
-
- ASM mov dx, nModemCtrlRegAddr
- ASM in al, dx
- ASM and al, NOT_RTS
- ASM out dx, al
- }
- }
-
- if(nRXChars < nRXQueueSize)
- {
-
- pbtRXQueue[nRXInIndex++] = btTemp;
-
- if (nRXInIndex == nRXQueueSize)
- nRXInIndex = 0;
-
- nRXChars++;
- }
- break;
- case 0x06:
-
-
- ASM mov dx, nLineStatusRegAddr
- ASM in al, dx
- break;
- }
- }
-
- ASM mov dx, nI8259EndOfIntRegAddr
- ASM mov al, 0x20
- ASM out dx, al
- if(nI8259MasterEndOfIntRegAddr != 0)
- {
- ASM mov dx, nI8259MasterEndOfIntRegAddr
- ASM mov al, 0x20
- ASM out dx, al
- }
- }
- #endif
- #ifdef INCLUDE_WIN32_COM
- static tODResult ODComWin32SetReadTimeouts(tPortInfo *pPortInfo,
- tReadTimeoutState RequiredTimeoutState);
- static tODResult ODComWin32SetReadTimeouts(tPortInfo *pPortInfo,
- tReadTimeoutState RequiredTimeoutState)
- {
- ASSERT(pPortInfo != NULL);
-
- if(RequiredTimeoutState != pPortInfo->ReadTimeoutState)
- {
- COMMTIMEOUTS CommTimeouts;
-
- if(!GetCommTimeouts(pPortInfo->hCommDev, &CommTimeouts))
- {
- return(kODRCGeneralFailure);
- }
-
- switch(RequiredTimeoutState)
- {
- case kBlocking:
- CommTimeouts.ReadIntervalTimeout = 0;
- CommTimeouts.ReadTotalTimeoutMultiplier = 0;
- CommTimeouts.ReadTotalTimeoutConstant = 0;
- break;
- case kNonBlocking:
- CommTimeouts.ReadIntervalTimeout = INFINITE;
- CommTimeouts.ReadTotalTimeoutMultiplier = 0;
- CommTimeouts.ReadTotalTimeoutConstant = 0;
- break;
- default:
- ASSERT(FALSE);
- }
-
- if(!SetCommTimeouts(pPortInfo->hCommDev, &CommTimeouts))
- {
- return(kODRCGeneralFailure);
- }
-
-
- pPortInfo->ReadTimeoutState = RequiredTimeoutState;
- }
- return(kODRCSuccess);
- }
- #endif
- tODResult ODComAlloc(tPortHandle *phPort)
- {
- tPortInfo *pPortInfo;
- VERIFY_CALL(phPort != NULL);
-
- pPortInfo = malloc(sizeof(tPortInfo));
-
- if(pPortInfo == NULL)
- {
- *phPort = ODPTR2HANDLE(NULL, tPortInfo);
- return(kODRCNoMemory);
- }
-
- pPortInfo->bIsOpen = FALSE;
- pPortInfo->bUsingClientsHandle = FALSE;
- pPortInfo->btFlowControlSetting = FLOW_DEFAULT;
- pPortInfo->lSpeed = SPEED_UNSPECIFIED;
- pPortInfo->btWordFormat = ODPARITY_NONE | DATABITS_EIGHT | STOP_ONE;
- pPortInfo->nReceiveBufferSize = 1024;
- pPortInfo->nTransmitBufferSize = 1024;
- pPortInfo->btFIFOSetting = FIFO_ENABLE | FIFO_TRIGGER_8;
- pPortInfo->Method = kComMethodUnspecified;
- pPortInfo->pfIdleCallback = NULL;
-
- *phPort = ODPTR2HANDLE(pPortInfo, tPortInfo);
-
- ODComSetPort(*phPort, 0);
- #if defined(INCLUDE_SOCKET_COM) && defined(_WINSOCKAPI_)
- WSAStartup(MAKEWORD(1,1), &WSAData);
- #endif
-
- return(kODRCSuccess);
- }
- tODResult ODComFree(tPortHandle hPort)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
-
- free(pPortInfo);
-
- return(kODRCSuccess);
- }
- tODResult ODComSetIdleFunction(tPortHandle hPort,
- void (*pfCallback)(void))
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->pfIdleCallback = pfCallback;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetFlowControl(tPortHandle hPort, BYTE btFlowControlSetting)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->btFlowControlSetting = btFlowControlSetting;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetSpeed(tPortHandle hPort, long lSpeed)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->lSpeed = lSpeed;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetPort(tPortHandle hPort, BYTE btPort)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
-
- pPortInfo->btPort = btPort;
- #ifdef INCLUDE_UART_COM
-
- pPortInfo->nPortAddress = 0;
- if(btPort < 4)
- {
-
- pPortInfo->nPortAddress = *(((int far *)0x400) + btPort);
- }
-
-
- if(pPortInfo->nPortAddress == 0
- && btPort < DIM(anDefaultPortAddr))
- {
- pPortInfo->nPortAddress = anDefaultPortAddr[btPort];
- }
-
-
-
- if(btPort == 0 || btPort == 2)
- {
- pPortInfo->btIRQLevel = 4;
- }
- else
- {
- pPortInfo->btIRQLevel = 3;
- }
- #endif
-
- return(kODRCSuccess);
- }
- tODResult ODComSetPortAddress(tPortHandle hPort, int nPortAddress)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->nPortAddress = nPortAddress;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetIRQ(tPortHandle hPort, BYTE btIRQLevel)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->btIRQLevel = btIRQLevel;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetWordFormat(tPortHandle hPort, BYTE btWordFormat)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->btWordFormat = btWordFormat;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetRXBuf(tPortHandle hPort, int nReceiveBufferSize)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->nReceiveBufferSize = nReceiveBufferSize;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetTXBuf(tPortHandle hPort, int nTransmitBufferSize)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->nTransmitBufferSize = nTransmitBufferSize;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetFIFO(tPortHandle hPort, BYTE btFIFOSetting)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->btFIFOSetting = btFIFOSetting;
-
- return(kODRCSuccess);
- }
- tODResult ODComSetPreferredMethod(tPortHandle hPort, tComMethod Method)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- pPortInfo->Method = Method;
-
- return(kODRCSuccess);
- }
- tODResult ODComGetMethod(tPortHandle hPort, tComMethod *pMethod)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pMethod != NULL);
- *pMethod = pPortInfo->Method;
- return(kODRCSuccess);
- }
- tODResult ODComOpen(tPortHandle hPort)
- {
- #if defined(INCLUDE_FOSSIL_COM) || defined(INCLUDE_UART_COM)
- unsigned int uDivisor;
- unsigned long ulQuotient, ulRemainder;
- BYTE btTemp;
- #endif
- #ifdef INCLUDE_STDIO_COM
- struct termios tio_raw;
- #endif
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- nPort = (int)pPortInfo->btPort;
-
- VERIFY_CALL(!pPortInfo->bIsOpen);
-
-
- #ifdef INCLUDE_FOSSIL_COM
-
-
- if(pPortInfo->Method == kComMethodFOSSIL ||
- pPortInfo->Method == kComMethodUnspecified)
- {
-
- ASM push si
- ASM push di
- ASM mov ah, 4
- ASM mov dx, nPort
- ASM mov bx, 0
- ASM int 20
- ASM pop di
- ASM pop si
- ASM cmp ax, 6484
- ASM je fossil
- goto no_fossil;
- fossil:
- pPortInfo->Method = kComMethodFOSSIL;
-
-
-
-
- if(pPortInfo->btFlowControlSetting & FLOW_DEFAULT)
- {
- btTemp = FLOW_RTSCTS | 0xf0;
- }
- else
- {
- btTemp = pPortInfo->btFlowControlSetting | 0xf0;
- }
- ASM push si
- ASM push di
- ASM mov ah, 0x0f
- ASM mov al, btTemp
- ASM mov dx, nPort
- ASM int 20
- ASM pop di
- ASM pop si
-
- if(pPortInfo->lSpeed == SPEED_UNSPECIFIED)
- {
-
- pPortInfo->bIsOpen = TRUE;
-
- return(kODRCSuccess);
- }
-
- switch(pPortInfo->lSpeed)
- {
- case 300L:
- btTemp = 0x40;
- break;
- case 600L:
- btTemp = 0x60;
- break;
- case 1200L:
- btTemp = 0x80;
- break;
- case 2400L:
- btTemp = 0xa0;
- break;
- case 4800L:
- btTemp = 0xc0;
- break;
- case 9600L:
- btTemp = 0xe0;
- break;
- case 19200L:
- btTemp = 0x00;
- break;
- case 38400L:
- btTemp = 0x20;
- break;
- default:
-
-
- pPortInfo->bIsOpen = TRUE;
-
- return(kODRCSuccess);
- }
-
- btTemp |= pPortInfo->btWordFormat;
-
- ASM push si
- ASM push di
- ASM mov al, btTemp
- ASM mov ah, 0
- ASM mov dx, nPort
- ASM int 20
- ASM pop di
- ASM pop si
-
- pPortInfo->bIsOpen = TRUE;
-
- return(kODRCSuccess);
- }
- no_fossil:
- #endif
-
-
- #ifdef INCLUDE_UART_COM
- if(pPortInfo->Method == kComMethodUART ||
- pPortInfo->Method == kComMethodUnspecified)
- {
-
-
- if(pPortInfo->btFlowControlSetting & FLOW_DEFAULT)
- {
- btFlowControl = FLOW_RTSCTS;
- }
- else
- {
- btFlowControl = pPortInfo->btFlowControlSetting;
- }
-
- pPortInfo->Method = kComMethodUART;
-
-
- nRXHighWaterMark = (pPortInfo->nReceiveBufferSize * RECEIVE_HIGH_NUM)
- / RECEIVE_HIGH_DENOM;
- nRXLowWaterMark = (pPortInfo->nReceiveBufferSize * RECEIVE_LOW_NUM)
- / RECEIVE_LOW_DENOM;
-
- pbtTXQueue = malloc(nTXQueueSize = pPortInfo->nTransmitBufferSize);
- pbtRXQueue = malloc(nRXQueueSize = pPortInfo->nReceiveBufferSize);
- if(pbtTXQueue == NULL || pbtRXQueue == NULL)
- {
- return(kODRCNoMemory);
- }
-
- if(pPortInfo->nPortAddress == 0)
- {
- return(kODRCNoPortAddress);
- }
-
- nDataRegAddr = pPortInfo->nPortAddress;
- nIntEnableRegAddr = nDataRegAddr + IER;
- nIntIDRegAddr = nDataRegAddr + IIR;
- nLineCtrlRegAddr = nDataRegAddr + LCR;
- nModemCtrlRegAddr = nDataRegAddr + MCR;
- nLineStatusRegAddr = nDataRegAddr + LSR;
- nModemStatusRegAddr = nDataRegAddr + MSR;
-
-
- if(pPortInfo->btIRQLevel <= 7)
- {
- btIntVector = 0x08 + (pPortInfo->btIRQLevel);
- btI8259Bit = 1 << (pPortInfo->btIRQLevel);
- nI8259MaskRegAddr = 0x21;
- nI8259EndOfIntRegAddr = 0x20;
- nI8259MasterEndOfIntRegAddr = 0x00;
- }
- else
- {
- btIntVector = 0x68 + (pPortInfo->btIRQLevel);
- btI8259Bit = 1 << (pPortInfo->btIRQLevel - 8);
- nI8259MaskRegAddr = 0xA1;
- nI8259EndOfIntRegAddr = 0xA0;
- nI8259MasterEndOfIntRegAddr = 0x20;
- }
-
- ASM mov dx, nIntEnableRegAddr
- ASM in al, dx
- ASM mov btOldIntEnableReg, al
-
- ASM mov dx, nIntEnableRegAddr
- ASM mov al, 0
- ASM out dx, al
- ASM mov dx, nIntEnableRegAddr
- ASM in al, dx
- ASM mov btTemp, al
- if (btTemp != 0)
- {
- return(kODRCNoUART);
- }
-
- if(btFlowControl & FLOW_RTSCTS)
- {
-
- ASM mov dx, nModemStatusRegAddr
- ASM in al, dx
- ASM mov btTemp, al
-
- bStopTrans = !(btTemp & CTS);
- }
-
-
- ASM cli
- ASM mov dx, nI8259MaskRegAddr
- ASM in al, dx
- ASM mov btI8259Mask, al
- ASM or al, btI8259Bit
- ASM out dx, al
-
- ODComInternalResetTX();
- ODComInternalResetRX();
-
- ASM sti
-
- pfOldISR = ODComGetVect(btIntVector);
-
- #ifdef _MSC_VER
- ODComSetVect(btIntVector, (void far *)ODComInternalISR);
- #else
- ODComSetVect(btIntVector, ODComInternalISR);
- #endif
-
-
- btTemp = pPortInfo->btWordFormat;
- ASM mov dx, nLineCtrlRegAddr
- ASM mov al, btTemp
- ASM out dx, al
-
- ASM cli
- ASM mov dx, nModemCtrlRegAddr
- ASM in al, dx
- ASM mov btOldModemCtrlReg, al
-
- btTemp = (btOldModemCtrlReg & DTR) | (OUT2 + RTS);
- ASM mov dx, nModemCtrlRegAddr
- ASM mov al, btTemp
- ASM out dx, al
-
- if(pPortInfo->btFIFOSetting & FIFO_ENABLE)
- {
-
- btBaseFIFOCtrl = pPortInfo->btFIFOSetting;
-
- ASM mov al, btBaseFIFOCtrl
- ASM mov dx, nIntIDRegAddr
- ASM out dx, al
-
-
- ASM mov dx, nIntIDRegAddr
- ASM in al, dx
- ASM mov btTemp, al
- bUsingFIFO = btTemp & 0xc0;
- }
- ASM sti
-
- ASM mov dx, nIntEnableRegAddr
- ASM mov al, DR + MS
- ASM out dx, al
- ASM cli
- ASM mov dx, nI8259MaskRegAddr
- ASM in al, dx
- ASM mov ah, btI8259Bit
- ASM not ah
- ASM and al, ah
- ASM out dx, al
- ASM sti
-
-
- if(pPortInfo->lSpeed != SPEED_UNSPECIFIED)
- {
- ODDWordDivide(&ulQuotient, &ulRemainder, 115200UL, pPortInfo->lSpeed);
-
-
-
-
- if(ulRemainder == 0L)
- {
- uDivisor = (unsigned int)ulQuotient;
-
- ASM cli
-
-
-
-
- ASM mov dx, nLineCtrlRegAddr
- ASM in al, dx
- ASM or al, DLATCH
- ASM out dx, al
-
- ASM mov dx, nDataRegAddr
- ASM mov ax, uDivisor
- ASM out dx, al
-
- ASM mov dx, nIntEnableRegAddr
- ASM mov al, ah
- ASM out dx, al
-
- ASM mov dx, nLineCtrlRegAddr
- ASM in al, dx
- ASM and al, NOT_DL
- ASM out dx, al
-
- ASM sti
- }
- }
-
- pPortInfo->Method = kComMethodUART;
-
- pPortInfo->bIsOpen = TRUE;
-
- return(kODRCSuccess);
- }
- #endif
-
- #ifdef INCLUDE_DOOR32_COM
- if(pPortInfo->Method == kComMethodDoor32 ||
- pPortInfo->Method == kComMethodUnspecified)
- {
-
- pPortInfo->hinstDoor32DLL = LoadLibrary("DOOR32.DLL");
- if(pPortInfo->hinstDoor32DLL != NULL)
- {
-
- pPortInfo->pfDoorInitialize = (BOOL (WINAPI *)(void))
- GetProcAddress(pPortInfo->hinstDoor32DLL, "DoorInitialize");
- pPortInfo->pfDoorShutdown = (BOOL (WINAPI *)(void))
- GetProcAddress(pPortInfo->hinstDoor32DLL, "DoorShutdown");
- pPortInfo->pfDoorWrite = (BOOL (WINAPI *)(const BYTE *, DWORD))
- GetProcAddress(pPortInfo->hinstDoor32DLL, "DoorWrite");
- pPortInfo->pfDoorRead = (DWORD (WINAPI *)(BYTE *, DWORD))
- GetProcAddress(pPortInfo->hinstDoor32DLL, "DoorRead");
- pPortInfo->pfDoorGetAvailableEventHandle = (HANDLE (WINAPI *)(void))
- GetProcAddress(pPortInfo->hinstDoor32DLL,
- "DoorGetAvailableEventHandle");
- pPortInfo->pfDoorGetOfflineEventHandle = (HANDLE (WINAPI *)(void))
- GetProcAddress(pPortInfo->hinstDoor32DLL,
- "DoorGetOfflineEventHandle");
-
-
- if(pPortInfo->pfDoorInitialize != NULL
- && pPortInfo->pfDoorShutdown != NULL
- && pPortInfo->pfDoorWrite != NULL
- && pPortInfo->pfDoorRead != NULL
- && pPortInfo->pfDoorGetAvailableEventHandle != NULL
- && pPortInfo->pfDoorGetOfflineEventHandle != NULL)
- {
- if((*pPortInfo->pfDoorInitialize)())
- {
-
- pPortInfo->bIsOpen = TRUE;
-
- pPortInfo->Method = kComMethodDoor32;
-
- return(kODRCSuccess);
- }
- }
-
-
- FreeLibrary(pPortInfo->hinstDoor32DLL);
- }
-
-
- }
- #endif
-
-
- #ifdef INCLUDE_WIN32_COM
- if(pPortInfo->Method == kComMethodWin32 ||
- pPortInfo->Method == kComMethodUnspecified)
- {
- char szDevName[7];
- DCB dcb;
-
- sprintf(szDevName, "COM%u", (unsigned)pPortInfo->btPort + 1);
-
- pPortInfo->hCommDev = CreateFile(szDevName, GENERIC_READ | GENERIC_WRITE,
- 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-
- if(pPortInfo->hCommDev == INVALID_HANDLE_VALUE)
- {
- return(kODRCGeneralFailure);
- }
-
- pPortInfo->ReadTimeoutState = kNotSet;
-
-
- if(!SetupComm(pPortInfo->hCommDev, pPortInfo->nReceiveBufferSize,
- pPortInfo->nTransmitBufferSize))
- {
- CloseHandle(pPortInfo->hCommDev);
- return(kODRCGeneralFailure);
- }
-
- if(!GetCommState(pPortInfo->hCommDev, &dcb))
- {
- CloseHandle(pPortInfo->hCommDev);
- return(kODRCGeneralFailure);
- }
-
-
- if(pPortInfo->lSpeed != SPEED_UNSPECIFIED)
- {
- dcb.BaudRate = pPortInfo->lSpeed;
- }
-
- if(!(pPortInfo->btFlowControlSetting & FLOW_DEFAULT))
- {
- if(pPortInfo->btFlowControlSetting & FLOW_RTSCTS)
- {
- dcb.fOutxCtsFlow = 1;
- dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
- }
- else
- {
- dcb.fOutxCtsFlow = 0;
- dcb.fRtsControl = RTS_CONTROL_ENABLE;
- }
- }
-
- if((pPortInfo->btWordFormat & DATABITS_MASK) == DATABITS_FIVE)
- {
- dcb.ByteSize = 5;
- }
- else if((pPortInfo->btWordFormat & DATABITS_MASK) == DATABITS_SIX)
- {
- dcb.ByteSize = 6;
- }
- else if((pPortInfo->btWordFormat & DATABITS_MASK) == DATABITS_SEVEN)
- {
- dcb.ByteSize = 7;
- }
- else if((pPortInfo->btWordFormat & DATABITS_MASK) == DATABITS_EIGHT)
- {
- dcb.ByteSize = 8;
- }
-
- if((pPortInfo->btWordFormat & ODPARITY_MASK) == ODPARITY_NONE)
- {
- dcb.Parity = NOPARITY;
- }
- else if((pPortInfo->btWordFormat & ODPARITY_MASK) == ODPARITY_ODD)
- {
- dcb.Parity = ODDPARITY;
- }
- else if((pPortInfo->btWordFormat & ODPARITY_MASK) == ODPARITY_EVEN)
- {
- dcb.Parity = EVENPARITY;
- }
-
- dcb.fDtrControl = DTR_CONTROL_ENABLE;
-
- if((pPortInfo->btWordFormat & STOP_MASK) == STOP_ONE)
- {
- dcb.StopBits = ONESTOPBIT;
- }
- else if((pPortInfo->btWordFormat & STOP_MASK) == STOP_ONE_POINT_FIVE)
- {
- dcb.StopBits = ONE5STOPBITS;
- }
- else if((pPortInfo->btWordFormat & STOP_MASK) == STOP_TWO)
- {
- dcb.StopBits = TWOSTOPBITS;
- }
-
- if(!SetCommState(pPortInfo->hCommDev, &dcb))
- {
- CloseHandle(pPortInfo->hCommDev);
- return(kODRCGeneralFailure);
- }
-
- pPortInfo->bIsOpen = TRUE;
-
- pPortInfo->Method = kComMethodWin32;
-
- return(kODRCSuccess);
- }
- #endif
- #ifdef INCLUDE_STDIO_COM
- if(pPortInfo->Method == kComMethodStdIO ||
- pPortInfo->Method == kComMethodUnspecified)
- {
- if (isatty(STDIN_FILENO)) {
- tcgetattr(STDIN_FILENO,&tio_default);
- tio_raw = tio_default;
- #ifdef __sun
- tio_raw.c_iflag &= ~(IMAXBEL|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
- tio_raw.c_oflag &= ~OPOST;
- tio_raw.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
- tio_raw.c_cflag &= ~(CSIZE|PARENB);
- tio_raw.c_cflag |= CS8;
- tio_raw.c_cc[VMIN] = 1;
- tio_raw.c_cc[VTIME] = 0;
- #else
- cfmakeraw(&tio_raw);
- #endif
- tcsetattr(STDIN_FILENO,TCSANOW,&tio_raw);
- setvbuf(stdout, NULL, _IONBF, 0);
- } else {
- setvbuf(stdout, NULL, _IONBF, 0);
- }
-
- pPortInfo->bIsOpen = TRUE;
-
- pPortInfo->Method = kComMethodStdIO;
-
- return(kODRCSuccess);
- }
- #endif
-
-
- return(kODRCGeneralFailure);
- }
- tODResult ODComOpenFromExistingHandle(tPortHandle hPort,
- DWORD dwExistingHandle)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(!pPortInfo->bIsOpen);
- #ifdef INCLUDE_SOCKET_COM
- if(pPortInfo->Method == kComMethodSocket) {
- socklen_t delay=FALSE;
- pPortInfo->socket = dwExistingHandle;
- getsockopt(pPortInfo->socket, IPPROTO_TCP, TCP_NODELAY, &(pPortInfo->old_delay), &delay);
- delay=FALSE;
- setsockopt(pPortInfo->socket, IPPROTO_TCP, TCP_NODELAY, &delay, sizeof(delay));
- pPortInfo->bIsOpen = TRUE;
- return(kODRCSuccess);
- }
- #endif
- #ifdef INCLUDE_WIN32_COM
-
- pPortInfo->hCommDev = (HANDLE)dwExistingHandle;
-
- pPortInfo->ReadTimeoutState = kNotSet;
-
-
-
- pPortInfo->bUsingClientsHandle = TRUE;
-
- pPortInfo->bIsOpen = TRUE;
- return(kODRCSuccess);
- #else
- UNUSED(dwExistingHandle);
- UNUSED(pPortInfo);
-
-
- return(kODRCInvalidCall);
- #endif
- }
- tODResult ODComClose(tPortHandle hPort)
- {
- #ifdef INCLUDE_UART_COM
- BYTE btTemp;
- #endif
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
-
- if(pPortInfo->bUsingClientsHandle)
- {
- pPortInfo->bIsOpen = FALSE;
- return(kODRCSuccess);
- }
- nPort = (int)pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- ASM mov ah, 5
- ASM mov dx, nPort
- ASM int 20
- break;
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
-
- ASM mov dx, nModemCtrlRegAddr
- ASM mov al, btOldModemCtrlReg
- ASM out dx, al
- ASM mov dx, nIntEnableRegAddr
- ASM mov al, btOldIntEnableReg
- ASM out dx, al
-
- ASM cli
-
-
- ASM mov dx, nI8259MaskRegAddr
- ASM in al, dx
- ASM mov btTemp, al
- btTemp = (btTemp & ~btI8259Bit) | (btI8259Mask & btI8259Bit);
- ASM mov dx, nI8259MaskRegAddr
- ASM mov al, btTemp
- ASM out dx, al
-
- ASM sti
-
- #ifdef _MSC_VER
- ODComSetVect(btIntVector, (void far *)pfOldISR);
- #else
- ODComSetVect(btIntVector, pfOldISR);
- #endif
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- CloseHandle(pPortInfo->hCommDev);
- break;
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- ASSERT(pPortInfo->pfDoorShutdown != NULL);
- (*pPortInfo->pfDoorShutdown)();
- ASSERT(pPortInfo->hinstDoor32DLL != NULL);
- FreeLibrary(pPortInfo->hinstDoor32DLL);
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- setsockopt(pPortInfo->socket, IPPROTO_TCP, TCP_NODELAY, &(pPortInfo->old_delay), sizeof(pPortInfo->old_delay));
- closesocket(pPortInfo->socket);
- break;
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- if(isatty(STDIN_FILENO))
- tcsetattr(STDIN_FILENO,TCSANOW,&tio_default);
- break;
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- pPortInfo->bIsOpen = FALSE;
-
- return(kODRCSuccess);
- }
- tODResult ODComCarrier(tPortHandle hPort, BOOL *pbIsCarrier)
- {
- #ifdef ODPLAT_NIX
- sigset_t sigs;
- #endif
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pbIsCarrier != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- {
- int to_return;
- ASM mov ah, 3
- ASM mov dx, nPort
- ASM int 20
- ASM and ax, 128
- ASM mov to_return, ax
- *pbIsCarrier = to_return;
- break;
- }
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- {
- BYTE btMSR;
- ASM mov dx, nModemStatusRegAddr
- ASM in al, dx
- ASM mov btMSR, al
- *pbIsCarrier = btMSR & RLSD;
- break;
- }
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwModemStats;
-
- if(!GetCommModemStatus(pPortInfo->hCommDev, &dwModemStats))
- {
- return(kODRCGeneralFailure);
- }
- *pbIsCarrier = (dwModemStats & MS_RLSD_ON) ? TRUE : FALSE;
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- ASSERT(pPortInfo->pfDoorGetOfflineEventHandle != NULL);
- *pbIsCarrier = (WaitForSingleObject(
- (*pPortInfo->pfDoorGetOfflineEventHandle)(),
- 0) != WAIT_OBJECT_0);
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- {
- int i;
- char ch;
- fd_set socket_set;
- struct timeval tv;
- FD_ZERO(&socket_set);
- FD_SET(pPortInfo->socket,&socket_set);
- tv.tv_sec=0;
- tv.tv_usec=0;
- i=select(pPortInfo->socket+1,&socket_set,NULL,NULL,&tv);
- if(i==0
- || (i==1 && recv(pPortInfo->socket,&ch,1,MSG_PEEK)==1))
- *pbIsCarrier = TRUE;
- else
- *pbIsCarrier = FALSE;
- break;
- }
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- {
- sigpending(&sigs);
- if(sigismember(&sigs,SIGHUP))
- *pbIsCarrier = FALSE;
- else
- *pbIsCarrier = TRUE;
- break;
- }
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
- return(kODRCSuccess);
- }
- tODResult ODComSetDTR(tPortHandle hPort, BOOL bHigh)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- ASM cmp byte ptr bHigh, 0
- ASM je lower
- ASM mov al, 1
- ASM jmp set_dtr
- lower:
- ASM xor al, al
- set_dtr:
- ASM mov ah, 6
- ASM mov dx, nPort
- ASM int 20
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- if(bHigh)
- {
- ASM cli
- ASM mov dx, nModemCtrlRegAddr
- ASM in al, dx
- ASM or al, DTR
- ASM out dx, al
- ASM sti
- }
- else
- {
- ASM cli
- ASM mov dx, nModemCtrlRegAddr
- ASM in al, dx
- ASM and al, NOT_DTR
- ASM out dx, al
- ASM sti
- }
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
-
- if(!EscapeCommFunction(pPortInfo->hCommDev, bHigh ? SETDTR : CLRDTR))
- {
- return(kODRCGeneralFailure);
- }
- break;
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- if(bHigh)
- return(kODRCUnsupported);
- closesocket(pPortInfo->socket);
- break;
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- return(kODRCUnsupported);
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
- return(kODRCSuccess);
- }
- tODResult ODComOutbound(tPortHandle hPort, int *pnOutboundWaiting)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pnOutboundWaiting != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- ASM mov ah, 0x03
- ASM mov dx, nPort
- ASM int 20
- ASM and ah, 0x40
- ASM jz still_sending
- *pnOutboundWaiting = 0;
- break;
- still_sending:
- *pnOutboundWaiting = SIZE_NON_ZERO;
- break;
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- *pnOutboundWaiting = (int)nTXChars;
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwErrors;
- COMSTAT ComStat;
-
- if(!ClearCommError(pPortInfo->hCommDev, &dwErrors, &ComStat))
- {
- return(kODRCGeneralFailure);
- }
-
- *pnOutboundWaiting = (int)ComStat.cbOutQue;
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
-
-
- *pnOutboundWaiting = 0;
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- *pnOutboundWaiting = 0;
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- *pnOutboundWaiting = 0;
- return(kODRCUnsupported);
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
- return(kODRCSuccess);
- }
- tODResult ODComClearOutbound(tPortHandle hPort)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- ASM mov ah, 9
- ASM mov dx, nPort
- ASM int 20
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- ODComInternalResetTX();
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- if(!PurgeComm(pPortInfo->hCommDev, PURGE_TXCLEAR))
- {
- return(kODRCGeneralFailure);
- }
- break;
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- return(kODRCUnsupported);
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- return(kODRCSuccess);
- }
- tODResult ODComClearInbound(tPortHandle hPort)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- ASM mov ah, 10
- ASM mov dx, nPort
- ASM int 20
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- ODComInternalResetRX();
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- if(!PurgeComm(pPortInfo->hCommDev, PURGE_RXCLEAR))
- {
- return(kODRCGeneralFailure);
- }
- break;
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- return(kODRCUnsupported);
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- return(kODRCSuccess);
- }
- tODResult ODComInbound(tPortHandle hPort, int *pnInboundWaiting)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pnInboundWaiting != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- {
- BOOL bDataInBuffer = FALSE;
- ASM mov ah, 3
- ASM mov dx, nPort
- ASM push si
- ASM push di
- ASM int 20
- ASM pop di
- ASM pop si
- ASM and ah, 1
- ASM mov bDataInBuffer, ah
- *pnInboundWaiting = bDataInBuffer ? SIZE_NON_ZERO : 0;
- break;
- }
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- *pnInboundWaiting = (int)nRXChars;
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwErrors;
- COMSTAT ComStat;
-
- if(!ClearCommError(pPortInfo->hCommDev, &dwErrors, &ComStat))
- {
- return(kODRCGeneralFailure);
- }
-
- *pnInboundWaiting = (int)ComStat.cbInQue;
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- ASSERT(pPortInfo->pfDoorGetAvailableEventHandle != NULL);
- if(WaitForSingleObject(
- (*pPortInfo->pfDoorGetAvailableEventHandle)(),
- 0) == WAIT_OBJECT_0)
- {
- *pnInboundWaiting = SIZE_NON_ZERO;
- }
- else
- {
- *pnInboundWaiting = 0;
- }
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- if(ioctlsocket(pPortInfo->socket,FIONREAD,pnInboundWaiting) != 0)
- *pnInboundWaiting = 0;
- break;
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- if(ioctl(0,FIONREAD,pnInboundWaiting) == -1)
- *pnInboundWaiting = 0;
- break;
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
- return(kODRCSuccess);
- }
- tODResult ODComGetByte(tPortHandle hPort, char *pbtNext, BOOL bWait)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pbtNext != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- {
- BYTE btToReturn;
- int nInboundSize;
-
- if(!bWait)
- {
-
- ODComInbound(hPort, &nInboundSize);
-
-
- if(nInboundSize == 0) return(kODRCNothingWaiting);
- }
- ASM mov ah, 2
- ASM mov dx, nPort
- ASM push si
- ASM push di
- ASM int 20
- ASM pop di
- ASM pop si
- ASM mov btToReturn, al
- *pbtNext = btToReturn;
- break;
- }
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
-
- if(!bWait)
- {
-
-
- if(!nRXChars) return(kODRCNothingWaiting);
- }
-
- while(!nRXChars)
- {
- if(pPortInfo->pfIdleCallback != NULL)
- {
- (*pPortInfo->pfIdleCallback)();
- }
- }
-
- ASM cli
-
- *pbtNext = pbtRXQueue[nRXOutIndex++];
-
- if (nRXOutIndex == nRXQueueSize)
- {
- nRXOutIndex = 0;
- }
-
- nRXChars--;
-
- ASM sti
-
- if(nRXChars <= nRXLowWaterMark)
- {
-
-
- if(btFlowControl & FLOW_RTSCTS)
- {
-
- ASM mov dx, nModemCtrlRegAddr
- ASM in al, dx
- ASM or al, RTS
- ASM out dx, al
- }
- }
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwBytesRead;
- DWORD dwErrors;
-
- if(bWait)
- {
- ODComWin32SetReadTimeouts(pPortInfo, kBlocking);
- }
- else
- {
- ODComWin32SetReadTimeouts(pPortInfo, kNonBlocking);
- }
-
- if(!ReadFile(pPortInfo->hCommDev, pbtNext, 1, &dwBytesRead, NULL))
- {
- ClearCommError(pPortInfo->hCommDev, &dwErrors, NULL);
- return(kODRCGeneralFailure);
- }
-
- if(dwBytesRead == 0)
- {
-
-
-
- return(bWait ? kODRCGeneralFailure : kODRCNothingWaiting);
- }
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- if(WaitForSingleObject((*pPortInfo->pfDoorGetAvailableEventHandle)(),
- bWait ? INFINITE : 0) == WAIT_OBJECT_0)
- {
- (*pPortInfo->pfDoorRead)(pbtNext, 1);
- break;
- }
- return(bWait ? kODRCGeneralFailure : kODRCNothingWaiting);
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- {
- fd_set socket_set;
- struct timeval tv;
- int select_ret, recv_ret;
- FD_ZERO(&socket_set);
- FD_SET(pPortInfo->socket,&socket_set);
- tv.tv_sec=0;
- tv.tv_usec=100;
- select_ret = select(pPortInfo->socket+1, &socket_set, NULL, NULL, bWait ? NULL : &tv);
- if (select_ret == SOCKET_ERROR)
- return (kODRCGeneralFailure);
- if (select_ret == 0)
- return (kODRCNothingWaiting);
- do {
- recv_ret = recv(pPortInfo->socket, pbtNext, 1, 0);
- if(recv_ret != SOCKET_ERROR)
- break;
- if(WSAGetLastError() != WSAEWOULDBLOCK)
- return (kODRCGeneralFailure);
- od_sleep(50);
- } while (bWait);
- if (recv_ret == 0)
- return (kODRCNothingWaiting);
- break;
- }
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- {
- fd_set socket_set;
- struct timeval tv;
- int select_ret=-1;
- int recv_ret;
- while(select_ret==-1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO,&socket_set);
- tv.tv_sec=0;
- tv.tv_usec=100;
- select_ret = select(STDIN_FILENO+1, &socket_set, NULL, NULL, bWait ? NULL : &tv);
- if (select_ret == -1) {
- if(errno==EINTR)
- continue;
- return (kODRCGeneralFailure);
- }
- if (select_ret == 0)
- return (kODRCNothingWaiting);
- }
- recv_ret = read(STDIN_FILENO, pbtNext, 1);
- if(recv_ret == 1)
- break;
- return (kODRCGeneralFailure);
- break;
- }
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
- return(0);
- }
- tODResult ODComSendByte(tPortHandle hPort, BYTE btToSend)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- try_again:
- ASM mov ah, 0x0b
- ASM mov dx, nPort
- ASM mov al, btToSend
- ASM int 20
- ASM cmp ax, 0
- ASM jne keep_going
-
- if(pPortInfo->pfIdleCallback != NULL)
- {
- (*pPortInfo->pfIdleCallback)();
- }
- goto try_again;
- keep_going:
- break;
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
-
-
- while(!ODComInternalTXReady())
- {
-
- if(pPortInfo->pfIdleCallback != NULL)
- {
- (*pPortInfo->pfIdleCallback)();
- }
- }
-
- ASM cli
-
- pbtTXQueue[nTXInIndex++] = btToSend;
-
- if (nTXInIndex == nTXQueueSize)
- {
- nTXInIndex = 0;
- }
-
- nTXChars++;
-
- ASM mov dx, nIntEnableRegAddr
- ASM in al, dx
- ASM or al, THRE
- ASM out dx, al
- ASM sti
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwErrors;
- DWORD dwBytesWritten;
-
- if(!WriteFile(pPortInfo->hCommDev, &btToSend, 1, &dwBytesWritten,
- NULL) || dwBytesWritten != 1)
- {
- ClearCommError(pPortInfo->hCommDev, &dwErrors, NULL);
- return(kODRCGeneralFailure);
- }
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- ASSERT(pPortInfo->pfDoorWrite != NULL);
- if(!(*pPortInfo->pfDoorWrite)(&btToSend, 1))
- {
- return(kODRCGeneralFailure);
- }
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- {
- fd_set socket_set;
- struct timeval tv;
- int send_ret;
- FD_ZERO(&socket_set);
- FD_SET(pPortInfo->socket,&socket_set);
- tv.tv_sec=1;
- tv.tv_usec=0;
- if(select(pPortInfo->socket+1,NULL,&socket_set,NULL,&tv) != 1)
- return(kODRCGeneralFailure);
- do {
- send_ret = send(pPortInfo->socket, &btToSend, 1, 0);
- if (send_ret != 1)
- od_sleep(50);
- } while ((send_ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK));
- if (send_ret == SOCKET_ERROR)
- return (kODRCGeneralFailure);
- break;
- }
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- {
- fd_set fdset;
- struct timeval tv;
- int retval=-1;
- int loopcount=0;
- while(retval==-1 && loopcount < 10) {
- FD_ZERO(&fdset);
- FD_SET(STDOUT_FILENO,&fdset);
- tv.tv_sec=1;
- tv.tv_usec=0;
- retval=select(STDOUT_FILENO+1,NULL,&fdset,NULL,&tv);
- if(retval!=1) {
- if(retval==0) {
- retval=-1;
- loopcount++;
- continue;
- }
- if(retval==-1 && errno==EINTR)
- continue;
- return(kODRCGeneralFailure);
- }
- }
- if(fwrite(&btToSend,1,1,stdout)!=1)
- return(kODRCGeneralFailure);
- break;
- }
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- return(kODRCSuccess);
- }
- tODResult ODComGetBuffer(tPortHandle hPort, BYTE *pbtBuffer, int nSize,
- int *pnBytesRead)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pbtBuffer != NULL);
- VERIFY_CALL(nSize > 0);
- VERIFY_CALL(pnBytesRead != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- {
- int nReceived;
- ASM push di
- ASM mov cx, nSize
- ASM mov dx, nPort
- #ifdef LARGEDATA
- ASM les di, pbtBuffer
- #else
- ASM mov ax, ds
- ASM mov es, ax
- ASM mov di, pbtBuffer
- #endif
- ASM mov ah, 0x18
- ASM int 20
- ASM pop di
- ASM mov nReceived, ax
- *pnBytesRead = nReceived;
- break;
- }
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- {
- int nTransferSize;
- int nFirstHalfSize;
- int nSecondHalfSize;
- char *pbtSource;
-
- ASM cli
-
-
- nTransferSize = MIN(nRXChars, nSize);
-
-
- nFirstHalfSize = nRXQueueSize - nRXOutIndex;
- nFirstHalfSize = MIN(nFirstHalfSize, nTransferSize);
-
- nSecondHalfSize = nTransferSize - nFirstHalfSize;
-
- pbtSource = pbtRXQueue + nRXOutIndex;
- while(nFirstHalfSize--)
- {
- *pbtBuffer++ = *pbtSource++;
- }
-
- if(nSecondHalfSize)
- {
-
- pbtSource = pbtRXQueue;
-
- nRXOutIndex = nSecondHalfSize;
-
- while(nSecondHalfSize--)
- {
- *pbtBuffer++ = *pbtSource++;
- }
- }
-
- else
- {
-
- nRXOutIndex += nTransferSize;
-
- if(nRXOutIndex == nRXQueueSize) nRXOutIndex = 0;
- }
-
-
- nRXChars -= nTransferSize;
-
- *pnBytesRead = nTransferSize;
-
- ASM sti
- break;
- }
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwBytesRead;
- DWORD dwErrors;
-
- ODComWin32SetReadTimeouts(pPortInfo, kNonBlocking);
-
- if(!ReadFile(pPortInfo->hCommDev, pbtBuffer, nSize, &dwBytesRead,
- NULL))
- {
- ClearCommError(pPortInfo->hCommDev, &dwErrors, NULL);
- return(kODRCGeneralFailure);
- }
-
- *pnBytesRead = (int)dwBytesRead;
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- ASSERT(pPortInfo->pfDoorRead != NULL);
- *pnBytesRead = (int)((*pPortInfo->pfDoorRead)(pbtBuffer, nSize));
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- {
- fd_set socket_set;
- struct timeval tv;
- FD_ZERO(&socket_set);
- FD_SET(pPortInfo->socket,&socket_set);
- tv.tv_sec=0;
- tv.tv_usec=100;
- if(select(pPortInfo->socket+1,&socket_set,NULL,NULL,&tv) != 1) {
- *pnBytesRead = 0;
- break;
- }
- *pnBytesRead = recv(pPortInfo->socket,pbtBuffer,nSize,0);
- break;
- }
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- {
- for(*pnBytesRead=0;
- *pnBytesRead<nSize && (ODComGetByte(hPort, (pbtBuffer+*pnBytesRead), FALSE)==kODRCSuccess);
- *pnBytesRead++);
- }
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- return(kODRCSuccess);
- }
- tODResult ODComSendBuffer(tPortHandle hPort, BYTE *pbtBuffer, int nSize)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- int nPort;
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pbtBuffer != NULL);
- VERIFY_CALL(nSize >= 0);
- VERIFY_CALL(pPortInfo->bIsOpen);
- nPort = pPortInfo->btPort;
-
-
- if(nSize == 0)
- {
- return(kODRCSuccess);
- }
- switch(pPortInfo->Method)
- {
- #ifdef INCLUDE_FOSSIL_COM
- case kComMethodFOSSIL:
- {
- int nCount;
- try_again:
- ASM push di
- ASM mov cx, nSize
- ASM mov dx, nPort
- #ifdef LARGEDATA
- ASM les di, pbtBuffer
- #else
- ASM mov ax, ds
- ASM mov es, ax
- ASM mov di, pbtBuffer
- #endif
- ASM mov ah, 0x19
- ASM int 20
- ASM pop di
- ASM mov nCount, ax
- if(nCount<nSize)
- {
-
- if(pPortInfo->pfIdleCallback != NULL)
- {
- (*pPortInfo->pfIdleCallback)();
- }
- nSize-=nCount;
- pbtBuffer+=nCount;
- goto try_again;
- }
- break;
- }
- #endif
- #ifdef INCLUDE_UART_COM
- case kComMethodUART:
- {
- int nTransferSize;
- int nFirstHalfSize;
- int nSecondHalfSize;
- char *pbtDest;
-
-
-
-
- for(;;)
- {
-
- ASM cli
-
- nTransferSize = nSize;
-
-
- if(nTransferSize > (nTXQueueSize - nTXChars))
- {
- nTransferSize = (nTXQueueSize - nTXChars);
- }
-
-
-
-
- nFirstHalfSize = nTXQueueSize - nTXInIndex;
- if(nFirstHalfSize > nTransferSize) nFirstHalfSize = nTransferSize;
-
- nSecondHalfSize = nTransferSize - nFirstHalfSize;
-
- pbtDest = pbtTXQueue + nTXInIndex;
- while(nFirstHalfSize--)
- {
- *pbtDest++ = *pbtBuffer++;
- }
-
- if(nSecondHalfSize)
- {
-
- pbtDest = pbtTXQueue;
-
- nTXInIndex = nSecondHalfSize;
-
- while(nSecondHalfSize--)
- {
- *pbtDest++ = *pbtBuffer++;
- }
- }
-
- else
- {
-
- nTXInIndex += nTransferSize;
-
-
-
-
-
- if(nTXInIndex == nTXQueueSize) nTXInIndex = 0;
- }
-
- nTXChars += nTransferSize;
-
- ASM mov dx, nIntEnableRegAddr
- ASM in al, dx
- ASM or al, THRE
- ASM out dx, al
-
- ASM sti
-
-
- nSize -= nTransferSize;
-
-
- if(nSize == 0) break;
-
- if(pPortInfo->pfIdleCallback != NULL)
- {
- (*pPortInfo->pfIdleCallback)();
- }
- }
- break;
- }
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwErrors;
- DWORD dwBytesWritten;
-
- if(!WriteFile(pPortInfo->hCommDev, pbtBuffer, nSize, &dwBytesWritten,
- NULL) || dwBytesWritten != (DWORD)nSize)
- {
- ClearCommError(pPortInfo->hCommDev, &dwErrors, NULL);
- return(kODRCGeneralFailure);
- }
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- ASSERT(pPortInfo->pfDoorWrite != NULL);
- if(!(*pPortInfo->pfDoorWrite)(pbtBuffer, nSize))
- {
- return(kODRCGeneralFailure);
- }
- break;
- return(kODRCUnsupported);
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- {
- fd_set socket_set;
- struct timeval tv;
- int send_ret;
- FD_ZERO(&socket_set);
- FD_SET(pPortInfo->socket,&socket_set);
- tv.tv_sec=1;
- tv.tv_usec=0;
- if(select(pPortInfo->socket+1,NULL,&socket_set,NULL,&tv) != 1)
- return(kODRCGeneralFailure);
- do {
- send_ret = send(pPortInfo->socket, pbtBuffer, nSize, 0);
- if (send_ret != SOCKET_ERROR)
- break;
- od_sleep(25);
- } while (WSAGetLastError() == WSAEWOULDBLOCK);
- if (send_ret != nSize)
- return (kODRCGeneralFailure);
- break;
- }
- #endif
- #ifdef INCLUDE_STDIO_COM
- case kComMethodStdIO:
- {
- int pos=0;
- fd_set fdset;
- struct timeval tv;
- int retval;
- int loopcount=0;
- while(pos<nSize) {
- FD_ZERO(&fdset);
- FD_SET(STDOUT_FILENO,&fdset);
- tv.tv_sec=1;
- tv.tv_usec=0;
- retval=select(STDOUT_FILENO+1,NULL,&fdset,NULL,&tv);
- if(retval!=1) {
- if(retval==0) {
- if(++loopcount>10)
- return(kODRCGeneralFailure);
- continue;
- }
- if(retval==-1 && errno==EINTR)
- continue;
- return(kODRCGeneralFailure);
- }
- retval=fwrite(pbtBuffer+pos,1,nSize-pos,stdout);
- if(retval!=nSize-pos) {
- od_sleep(1);
- }
- pos+=retval;
- }
- break;
- }
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- return(kODRCSuccess);
- }
- tODResult ODComWaitEvent(tPortHandle hPort, tComEvent Event)
- {
- tPortInfo *pPortInfo = ODHANDLE2PTR(hPort, tPortInfo);
- VERIFY_CALL(pPortInfo != NULL);
- VERIFY_CALL(pPortInfo->bIsOpen);
- switch(pPortInfo->Method)
- {
- #if defined(INCLUDE_UART_COM) || defined(INCLUDE_FOSSIL_COM) || defined(INCLUDE_STDIO_COM)
- case kComMethodFOSSIL:
- case kComMethodUART:
- case kComMethodStdIO:
- switch(Event)
- {
- case kNoCarrier:
- {
- BOOL bCarrier;
- for(;;)
- {
- ODComCarrier(hPort, &bCarrier);
- if(!bCarrier) break;
- if(pPortInfo->pfIdleCallback != NULL)
- {
- (*pPortInfo->pfIdleCallback)();
- }
- }
- break;
- }
- default:
- VERIFY_CALL(FALSE);
- }
- break;
- #endif
- #ifdef INCLUDE_WIN32_COM
- case kComMethodWin32:
- {
- DWORD dwEvtMask;
-
- if(!GetCommMask(pPortInfo->hCommDev, &dwEvtMask))
- {
- return(kODRCGeneralFailure);
- }
-
- switch(Event)
- {
- case kNoCarrier:
- dwEvtMask |= EV_RLSD;
- break;
- default:
- VERIFY_CALL(FALSE);
- }
-
- if(!SetCommMask(pPortInfo->hCommDev, dwEvtMask))
- {
- return(kODRCGeneralFailure);
- }
-
- for(;;)
- {
-
- if(!WaitCommEvent(pPortInfo->hCommDev, &dwEvtMask, NULL))
- {
- return(kODRCGeneralFailure);
- }
-
- switch(Event)
- {
- case kNoCarrier:
- if(dwEvtMask | EV_RLSD)
- {
- BOOL bCarrier;
- ODComCarrier(hPort, &bCarrier);
- if(!bCarrier)
- {
- return(kODRCSuccess);
- }
- }
- break;
- }
-
-
- }
- break;
- }
- #endif
- #ifdef INCLUDE_DOOR32_COM
- case kComMethodDoor32:
- switch(Event)
- {
- case kNoCarrier:
- ASSERT(pPortInfo->pfDoorGetOfflineEventHandle != NULL);
- WaitForSingleObject(
- (*pPortInfo->pfDoorGetOfflineEventHandle)(), INFINITE);
- break;
- default:
- VERIFY_CALL(FALSE);
- }
- break;
- #endif
- #ifdef INCLUDE_SOCKET_COM
- case kComMethodSocket:
- {
- if(Event == kNoCarrier)
- {
-
- fd_set socket_set;
- char ch;
- int recv_ret;
- while(1)
- {
- FD_ZERO(&socket_set);
- FD_SET(pPortInfo->socket,&socket_set);
- if(select(pPortInfo->socket+1,&socket_set,NULL,NULL,NULL)
- ==SOCKET_ERROR)
- break;
- recv_ret = recv(pPortInfo->socket, &ch, 1, MSG_PEEK);
- if(recv_ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
- continue;
- if (recv_ret != 1)
- break;
- }
- }
- else
- {
- VERIFY_CALL(FALSE);
- }
- break;
- }
- #endif
- default:
-
-
- ASSERT(FALSE);
- }
-
- return(kODRCSuccess);
- }
|