Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4630 lines
132 KiB

  1. /*****************************************************************************
  2. *
  3. * Copyright (c) 1995 Microsoft Corporation
  4. *
  5. * @doc
  6. * @module irlap.c | Provides IrLAP API
  7. *
  8. * Author: mbert
  9. *
  10. * Date: 4/15/95
  11. *
  12. * @comm
  13. *
  14. * This module exports the following API's:
  15. *
  16. * IrlapDown(Message)
  17. * Receives from LMP:
  18. * - Discovery request
  19. * - Connect request/response
  20. * - Disconnect request
  21. * - Data/UData request
  22. *
  23. * IrlapUp(Message)
  24. * Receives from MAC:
  25. * - Data indications
  26. * - Control confirmations
  27. *
  28. * IrlapTimerExp(Timer)
  29. * Receives from timer thread timer expiration notifications
  30. *
  31. * IrlapCloseLink()
  32. * Shut down IRLAP and IRMAC.
  33. *
  34. * IrlapGetQosParmVal()
  35. * Allows IRLMP to decode Qos.
  36. *
  37. * |---------|
  38. * | IRLMP |
  39. * |---------|
  40. * /|\ |
  41. * | |
  42. * IrlmpUp() | | IrlapDown()
  43. * | |
  44. * | \|/
  45. * |---------| IRDA_TimerStart/Stop() |-------|
  46. * | |-------------------------->| |
  47. * | IRLAP | | TIMER |
  48. * | |<--------------------------| |
  49. * |---------| XTimerExp() |-------|
  50. * /|\ |
  51. * | |
  52. * IrlapUp() | |IrmacDown()
  53. * | |
  54. * | \|/
  55. * |---------|
  56. * | IRMAC |
  57. * |---------|
  58. *
  59. *
  60. * Discovery Request
  61. *
  62. * |-------| IRLAP_DISCOVERY_REQ |-------|
  63. * | |---------------------------------------------------->| |
  64. * | IRLMP | | IRLAP |
  65. * | |<----------------------------------------------------| |
  66. * |-------| IRLAP_DISCOVERY_CONF |-------|
  67. * DscvStatus = IRLAP_DISCOVERY_COMPLETE
  68. * IRLAP_DISCOVERY_COLLISION
  69. * MAC_MEDIA_BUSY
  70. *
  71. * Connect Request
  72. *
  73. * |-------| IRLAP_CONNECT_REQ |-------|
  74. * | |---------------------------------------------------->| |
  75. * | IRLMP | | IRLAP |
  76. * | |<----------------------------------------------------| |
  77. * |-------| IRLAP_CONNECT_CONF |-------|
  78. * ConnStatus = IRLAP_CONNECTION_COMPLETE
  79. * IRLAP_DISCONNECT_IND
  80. * DiscStatus = IRLAP_NO_RESPONSE
  81. * MAC_MEDIA_BUSY
  82. *
  83. * Disconnect Request
  84. *
  85. * |-------| IRLAP_DISCONNECT_REQ |-------|
  86. * | |---------------------------------------------------->| |
  87. * | IRLMP | | IRLAP |
  88. * | |<----------------------------------------------------| |
  89. * |-------| IRLAP_DISCONNECT_IND |-------|
  90. * DiscStatus = IRLAP_DISCONNECT_COMPLETE
  91. * IRLAP_NO_RESPONSE
  92. *
  93. * UData/Data Request
  94. *
  95. * |-------| IRLAP_DATA/UDATA_REQ |-------|
  96. * | |---------------------------------------------------->| |
  97. * | IRLMP | | IRLAP |
  98. * | |<----------------------------------------------------| |
  99. * |-------| IRLAP_DATA_CONF |-------|
  100. * DataStatus = IRLAP_DATA_REQUEST_COMPLETED
  101. * IRLAP_DATA_REQUEST_FAILED_LINK_RESET
  102. *
  103. * See irda.h for complete message definitions
  104. */
  105. #include <irda.h>
  106. #include <irioctl.h>
  107. #include <irlap.h>
  108. #include <irlmp.h>
  109. #include <irlapp.h>
  110. #include <irlapio.h>
  111. #include <irlaplog.h>
  112. #ifdef TEMPERAMENTAL_SERIAL_DRIVER
  113. int TossedDups;
  114. #endif
  115. #ifdef ALLOC_DATA_PRAGMA
  116. #pragma data_seg("PAGE")
  117. #endif
  118. BOOLEAN MonitoringLinkStatus;
  119. UINT IrlapSlotTable[] = {1, 6, 8, 16};
  120. UCHAR IrlapBroadcastDevAddr[IRDA_DEV_ADDR_LEN] = {0xFF,0xFF,0xFF,0xFF};
  121. // Parameter Value (PV) tables used for negotation
  122. // bit0 1 2 3 4
  123. // 5 6 7 8
  124. // --------------------------------------------
  125. UINT vBaudTable[] = {2400, 9600, 19200, 38400, 57600,\
  126. 115200, 576000, 1152000, 4000000,16000000};
  127. UINT vMaxTATTable[] = {500, 250, 100, 50, 25, \
  128. 10, 5, 0, 0, 0 };
  129. UINT vMinTATTable[] = {10000, 5000, 1000, 500, 100, \
  130. 50, 10, 0, 0, 0 };
  131. UINT vDataSizeTable[] = {64, 128, 256, 512, 1024, \
  132. 2048, 0, 0, 0, 0 };
  133. UINT vWinSizeTable[] = {1, 2, 3, 4, 5, \
  134. 6, 7, 0, 0, 0 };
  135. UINT vBOFSTable[] = {48, 24, 12, 5, 3, \
  136. 2, 1, 0, 0, 0 };
  137. UINT vDiscTable[] = {3, 8, 12, 16, 20, \
  138. 25, 30, 40, 0, 0 };
  139. UINT vThreshTable[] = {0, 3, 3, 3, 3, \
  140. 3, 3, 3, 0, 0 };
  141. UINT vBOFSDivTable[] = {48, 12, 6, 3, 2, \
  142. 1, 1, 1, 1, 1 };
  143. // Tables for determining number of BOFS for baud and min turn time
  144. // min turn time - 10ms 5ms 1ms 0.5ms 0.1ms 0.05ms 0.01ms
  145. // -------------------------------------------------------------
  146. UINT BOFS_9600[] = {10, 5, 1, 0, 0, 0, 0};
  147. UINT BOFS_19200[] = {20, 10, 2, 1, 0, 0, 0};
  148. UINT BOFS_38400[] = {40, 20, 4, 2, 0, 0, 0};
  149. UINT BOFS_57600[] = {58, 29, 6, 3, 1, 0, 0};
  150. UINT BOFS_115200[] = {115, 58, 12, 6, 1, 1, 0};
  151. UINT BOFS_576000[] = {720, 360, 72, 36, 7, 4, 2};
  152. UINT BOFS_1152000[] = {1140, 720, 144, 72, 14, 7, 1};
  153. UINT BOFS_4000000[] = {5000,2500, 500, 250, 50, 25, 5};
  154. UINT BOFS_16000000[] = {20000,10000,2000,1000, 200, 100, 20};
  155. // Tables for determining maximum line capacity for baud, max turn time
  156. // max turn time - 500ms 250ms 100ms 50ms 25ms 10ms 5ms
  157. // -------------------------------------------------------------
  158. UINT MAXCAP_9600[] = {400, 200, 80, 0, 0, 0, 0};
  159. UINT MAXCAP_19200[] = {800, 400, 160, 0, 0, 0, 0};
  160. UINT MAXCAP_38400[] = {1600, 800, 320, 0, 0, 0, 0};
  161. UINT MAXCAP_57600[] = {2360, 1180, 472, 0, 0, 0, 0};
  162. UINT MAXCAP_115200[] = {4800, 2400, 960, 480, 240, 96, 48};
  163. UINT MAXCAP_576000[] = {28800, 11520, 5760, 2880, 1440, 720, 360};
  164. UINT MAXCAP_1152000[] = {57600, 28800, 11520, 5760, 2880, 1440, 720};
  165. UINT MAXCAP_4000000[] = {200000,100000, 40000,20000,10000, 5000, 2500};
  166. UINT MAXCAP_16000000[] ={800000,400000, 160000,80000,40000,20000,10000};
  167. #ifdef ALLOC_DATA_PRAGMA
  168. #pragma data_seg()
  169. #endif
  170. // prototypes
  171. STATIC VOID InitializeState(PIRLAP_CB, IRLAP_STN_TYPE);
  172. STATIC VOID ReturnRxTxWinMsgs(PIRLAP_CB);
  173. STATIC UINT ProcessConnectReq(PIRLAP_CB, PIRDA_MSG);
  174. STATIC VOID ProcessConnectResp(PIRLAP_CB, PIRDA_MSG);
  175. STATIC UINT ProcessDiscoveryReq(PIRLAP_CB, PIRDA_MSG);
  176. STATIC VOID ProcessDisconnectReq(PIRLAP_CB);
  177. STATIC UINT ProcessDataAndUDataReq(PIRLAP_CB, PIRDA_MSG);
  178. STATIC VOID XmitTxMsgList(PIRLAP_CB, BOOLEAN, BOOLEAN *);
  179. STATIC VOID GotoPCloseState(PIRLAP_CB);
  180. STATIC VOID GotoNDMThenDscvOrConn(PIRLAP_CB);
  181. STATIC VOID ProcessMACControlConf(PIRLAP_CB, PIRDA_MSG);
  182. STATIC VOID ProcessMACDataInd(PIRLAP_CB, PIRDA_MSG);
  183. STATIC VOID ProcessDscvXIDCmd(PIRLAP_CB, IRLAP_XID_DSCV_FORMAT *, UCHAR *);
  184. STATIC VOID ProcessDscvXIDRsp(PIRLAP_CB, IRLAP_XID_DSCV_FORMAT *, UCHAR *);
  185. STATIC VOID ExtractQosParms(IRDA_QOS_PARMS *, UCHAR *, UCHAR *);
  186. STATIC VOID InitDscvCmdProcessing(PIRLAP_CB, IRLAP_XID_DSCV_FORMAT *);
  187. STATIC VOID ExtractDeviceInfo(IRDA_DEVICE *, IRLAP_XID_DSCV_FORMAT *, UCHAR *);
  188. STATIC BOOLEAN DevInDevList(UCHAR[], LIST_ENTRY *);
  189. STATIC VOID AddDevToList(PIRLAP_CB, IRLAP_XID_DSCV_FORMAT *, UCHAR *);
  190. STATIC VOID FreeDevList(LIST_ENTRY *);
  191. STATIC VOID ProcessSNRM(PIRLAP_CB, IRLAP_SNRM_FORMAT *, UCHAR *);
  192. STATIC VOID ProcessUA(PIRLAP_CB, IRLAP_UA_FORMAT *, UCHAR *);
  193. STATIC VOID ProcessDISC(PIRLAP_CB);
  194. STATIC VOID ProcessRD(PIRLAP_CB);
  195. STATIC VOID ProcessRNRM(PIRLAP_CB);
  196. STATIC VOID ProcessDM(PIRLAP_CB);
  197. STATIC VOID ProcessFRMR(PIRLAP_CB);
  198. STATIC VOID ProcessTEST(PIRLAP_CB, PIRDA_MSG, IRLAP_UA_FORMAT *, int, int);
  199. STATIC VOID ProcessUI(PIRLAP_CB, PIRDA_MSG, int, int);
  200. STATIC VOID ProcessREJ_SREJ(PIRLAP_CB, int, PIRDA_MSG, int, int, UINT);
  201. STATIC VOID ProcessRR_RNR(PIRLAP_CB, int, PIRDA_MSG, int, int, UINT);
  202. STATIC VOID ProcessIFrame(PIRLAP_CB, PIRDA_MSG, int, int, UINT, UINT);
  203. STATIC BOOLEAN InvalidNs(PIRLAP_CB, UINT);
  204. STATIC BOOLEAN InvalidNr(PIRLAP_CB, UINT);
  205. STATIC BOOLEAN InWindow(UINT, UINT, UINT);
  206. STATIC VOID ProcessInvalidNsOrNr(PIRLAP_CB, int);
  207. STATIC VOID ProcessInvalidNr(PIRLAP_CB, int);
  208. STATIC VOID InsertRxWinAndForward(PIRLAP_CB, PIRDA_MSG, UINT);
  209. STATIC VOID ResendRejects(PIRLAP_CB, UINT);
  210. STATIC VOID ConfirmAckedTxMsgs(PIRLAP_CB, UINT);
  211. STATIC VOID MissingRxFrames(PIRLAP_CB);
  212. STATIC VOID IFrameOtherStates(PIRLAP_CB, int, int);
  213. STATIC UINT NegotiateQosParms(PIRLAP_CB, IRDA_QOS_PARMS *);
  214. STATIC VOID ApplyQosParms(PIRLAP_CB);
  215. STATIC VOID StationConflict(PIRLAP_CB);
  216. STATIC VOID ApplyDefaultParms(PIRLAP_CB);
  217. STATIC VOID ResendDISC(PIRLAP_CB);
  218. STATIC BOOLEAN IgnoreState(PIRLAP_CB);
  219. STATIC BOOLEAN MyDevAddr(PIRLAP_CB, UCHAR []);
  220. STATIC VOID SlotTimerExp(PVOID);
  221. STATIC VOID FinalTimerExp(PVOID);
  222. STATIC VOID PollTimerExp(PVOID);
  223. STATIC VOID BackoffTimerExp(PVOID);
  224. STATIC VOID WDogTimerExp(PVOID);
  225. STATIC VOID QueryTimerExp(PVOID);
  226. // STATIC VOID StatusTimerExp(PVOID);
  227. STATIC VOID IndicateLinkStatus(PIRLAP_CB);
  228. STATIC VOID StatusReq(PIRLAP_CB, IRDA_MSG *pMsg);
  229. #ifdef ALLOC_PRAGMA
  230. #pragma alloc_text(PAGEIRDA, IrlapOpenLink)
  231. #pragma alloc_text(PAGEIRDA, InitializeState)
  232. #pragma alloc_text(PAGEIRDA, ReturnRxTxWinMsgs)
  233. #pragma alloc_text(PAGEIRDA, ProcessConnectReq)
  234. #pragma alloc_text(PAGEIRDA, ProcessConnectResp)
  235. #pragma alloc_text(PAGEIRDA, ProcessDiscoveryReq)
  236. #pragma alloc_text(PAGEIRDA, ProcessDisconnectReq)
  237. #pragma alloc_text(PAGEIRDA, GotoPCloseState)
  238. #pragma alloc_text(PAGEIRDA, GotoNDMThenDscvOrConn)
  239. #pragma alloc_text(PAGEIRDA, ProcessMACControlConf)
  240. #pragma alloc_text(PAGEIRDA, ExtractQosParms)
  241. #pragma alloc_text(PAGEIRDA, ExtractDeviceInfo)
  242. #pragma alloc_text(PAGEIRDA, FreeDevList)
  243. #pragma alloc_text(PAGEIRDA, ProcessSNRM)
  244. #pragma alloc_text(PAGEIRDA, ProcessUA)
  245. #pragma alloc_text(PAGEIRDA, ProcessRD)
  246. #pragma alloc_text(PAGEIRDA, ProcessRNRM)
  247. #pragma alloc_text(PAGEIRDA, ProcessDM)
  248. #pragma alloc_text(PAGEIRDA, ProcessFRMR)
  249. #pragma alloc_text(PAGEIRDA, ProcessTEST)
  250. #pragma alloc_text(PAGEIRDA, ProcessUI)
  251. #pragma alloc_text(PAGEIRDA, NegotiateQosParms)
  252. #pragma alloc_text(PAGEIRDA, ApplyQosParms)
  253. #pragma alloc_text(PAGEIRDA, StationConflict)
  254. #pragma alloc_text(PAGEIRDA, ApplyDefaultParms)
  255. #endif
  256. #if DBG
  257. void _inline IrlapTimerStart(PIRLAP_CB pIrlapCb, PIRDA_TIMER pTmr)
  258. {
  259. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Start %hs timer for %dms"), pTmr->pName,
  260. pTmr->Timeout));
  261. IrdaTimerStart(pTmr);
  262. }
  263. void _inline IrlapTimerStop(PIRLAP_CB pIrlapCb, PIRDA_TIMER pTmr)
  264. {
  265. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Stop %hs timer"), pTmr->pName));
  266. IrdaTimerStop(pTmr);
  267. }
  268. #else
  269. #define IrlapTimerStart(c,t) IrdaTimerStart(t)
  270. #define IrlapTimerStop(c,t) IrdaTimerStop(t)
  271. #endif
  272. VOID
  273. IrlapOpenLink(OUT PNTSTATUS Status,
  274. IN PIRDA_LINK_CB pIrdaLinkCb,
  275. IN IRDA_QOS_PARMS *pQos,
  276. IN UCHAR *pDscvInfo,
  277. IN int DscvInfoLen,
  278. IN UINT MaxSlot,
  279. IN UCHAR *pDeviceName,
  280. IN int DeviceNameLen,
  281. IN UCHAR CharSet)
  282. {
  283. UINT rc = SUCCESS;
  284. int i;
  285. IRDA_MSG IMsg;
  286. PIRLAP_CB pIrlapCb;
  287. NDIS_STRING AStr = NDIS_STRING_CONST("InfraredTransceiverType");
  288. PAGED_CODE();
  289. DEBUGMSG(DBG_IRLAP, (TEXT("IrlapOpenLink\n")));
  290. if (IRDA_ALLOC_MEM(pIrlapCb, sizeof(IRLAP_CB), MT_IRLAPCB) == NULL)
  291. {
  292. DEBUGMSG(DBG_ERROR, (TEXT("Alloc failed\n")));
  293. *Status = STATUS_INSUFFICIENT_RESOURCES;
  294. return;
  295. }
  296. CTEMemSet(pIrlapCb, 0, sizeof(IRLAP_CB));
  297. IrlmpOpenLink(Status,
  298. pIrdaLinkCb,
  299. pDeviceName,
  300. DeviceNameLen,
  301. CharSet);
  302. if (*Status != STATUS_SUCCESS)
  303. {
  304. DEBUGMSG(DBG_ERROR, (TEXT("IrlmpOpenLink failed\n")));
  305. IRDA_FREE_MEM(pIrlapCb);
  306. return;
  307. }
  308. pIrdaLinkCb->IrlapContext = pIrlapCb;
  309. DscvInfoLen = DscvInfoLen > IRLAP_DSCV_INFO_LEN ?
  310. IRLAP_DSCV_INFO_LEN : DscvInfoLen;
  311. CTEMemCopy(pIrlapCb->LocalDevice.DscvInfo, pDscvInfo, DscvInfoLen);
  312. pIrlapCb->LocalDevice.DscvInfoLen = DscvInfoLen;
  313. CTEMemCopy(&pIrlapCb->LocalQos, pQos, sizeof(IRDA_QOS_PARMS));
  314. pIrlapCb->MaxSlot = MaxSlot;
  315. pIrlapCb->pIrdaLinkCb = pIrdaLinkCb;
  316. InitializeListHead(&pIrlapCb->TxMsgList);
  317. InitializeListHead(&pIrlapCb->ExTxMsgList);
  318. InitializeListHead(&pIrlapCb->DevList);
  319. for (i = 0; i < IRLAP_MOD; i++)
  320. {
  321. pIrlapCb->TxWin.pMsg[i] = NULL;
  322. pIrlapCb->RxWin.pMsg[i] = NULL;
  323. }
  324. // Get the local MAX TAT (for final timeout)
  325. if ((pIrlapCb->LocalMaxTAT =
  326. IrlapGetQosParmVal(vMaxTATTable,
  327. pIrlapCb->LocalQos.bfMaxTurnTime, NULL)) == -1)
  328. {
  329. *Status = STATUS_UNSUCCESSFUL;
  330. return /*IRLAP_BAD_QOS*/;
  331. }
  332. if ((pIrlapCb->LocalWinSize =
  333. IrlapGetQosParmVal(vWinSizeTable,
  334. pIrlapCb->LocalQos.bfWindowSize, NULL)) == -1)
  335. {
  336. *Status = STATUS_UNSUCCESSFUL;
  337. return /*IRLAP_BAD_QOS*/;
  338. }
  339. // initialize as PRIMARY so UI frames in contention
  340. // state sends CRBit = cmd
  341. InitializeState(pIrlapCb, PRIMARY);
  342. pIrlapCb->State = NDM;
  343. // Generate random local address
  344. StoreULAddr(pIrlapCb->LocalDevice.DevAddr, (ULONG) GetMyDevAddr(FALSE));
  345. pIrlapCb->LocalDevice.IRLAP_Version = 0;
  346. pIrlapCb->Baud = IRLAP_CONTENTION_BAUD;
  347. pIrlapCb->RemoteMaxTAT = IRLAP_CONTENTION_MAX_TAT;
  348. pIrlapCb->RemoteDataSize = IRLAP_CONTENTION_DATA_SIZE;
  349. pIrlapCb->RemoteWinSize = IRLAP_CONTENTION_WIN_SIZE;
  350. pIrlapCb->RemoteNumBOFS = IRLAP_CONTENTION_BOFS;
  351. pIrlapCb->ConnAddr = IRLAP_BROADCAST_CONN_ADDR;
  352. pIrlapCb->N1 = 0; // calculated at negotiation
  353. pIrlapCb->N2 = 0;
  354. pIrlapCb->N3 = 5; // recalculated after negotiation ??
  355. #if DBG
  356. pIrlapCb->PollTimer.pName = "Poll";
  357. pIrlapCb->FinalTimer.pName = "Final" ;
  358. pIrlapCb->SlotTimer.pName = "Slot";
  359. pIrlapCb->QueryTimer.pName = "Query";
  360. pIrlapCb->WDogTimer.pName = "WatchDog";
  361. pIrlapCb->BackoffTimer.pName = "Backoff";
  362. // pIrlapCb->StatusTimer.pName = "Status";
  363. #endif
  364. IrdaTimerInitialize(&pIrlapCb->PollTimer,
  365. PollTimerExp,
  366. pIrlapCb->RemoteMaxTAT,
  367. pIrlapCb,
  368. pIrdaLinkCb);
  369. IrdaTimerInitialize(&pIrlapCb->FinalTimer,
  370. FinalTimerExp,
  371. pIrlapCb->LocalMaxTAT,
  372. pIrlapCb,
  373. pIrdaLinkCb);
  374. IrdaTimerInitialize(&pIrlapCb->SlotTimer,
  375. SlotTimerExp,
  376. IRLAP_SLOT_TIMEOUT,
  377. pIrlapCb,
  378. pIrdaLinkCb);
  379. IrdaTimerInitialize(&pIrlapCb->QueryTimer,
  380. QueryTimerExp,
  381. (IRLAP_MAX_SLOTS + 4) * IRLAP_SLOT_TIMEOUT*2,
  382. pIrlapCb,
  383. pIrdaLinkCb);
  384. IrdaTimerInitialize(&pIrlapCb->WDogTimer,
  385. WDogTimerExp,
  386. 3000,
  387. pIrlapCb,
  388. pIrdaLinkCb);
  389. IrdaTimerInitialize(&pIrlapCb->BackoffTimer,
  390. BackoffTimerExp,
  391. 0,
  392. pIrlapCb,
  393. pIrdaLinkCb);
  394. /*
  395. IrdaTimerInitialize(&pIrlapCb->StatusTimer,
  396. StatusTimerExp,
  397. 250,
  398. pIrlapCb,
  399. pIrdaLinkCb);
  400. // Only monitor the link status of the first link
  401. if (!MonitoringLinkStatus)
  402. {
  403. MonitoringLinkStatus = TRUE;
  404. pIrlapCb->MonitorLink = TRUE;
  405. }
  406. else
  407. {
  408. pIrlapCb->MonitorLink = FALSE;
  409. }
  410. */
  411. // Initialize Link
  412. IMsg.Prim = MAC_CONTROL_REQ;
  413. IMsg.IRDA_MSG_Op = MAC_INITIALIZE_LINK;
  414. IMsg.IRDA_MSG_Baud = IRLAP_CONTENTION_BAUD;
  415. IMsg.IRDA_MSG_NumBOFs = IRLAP_CONTENTION_BOFS;
  416. IMsg.IRDA_MSG_DataSize = IRLAP_CONTENTION_DATA_SIZE;
  417. IMsg.IRDA_MSG_MinTat = 0;
  418. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  419. *Status = STATUS_SUCCESS;
  420. }
  421. /*****************************************************************************
  422. */
  423. VOID
  424. IrlapCloseLink(PIRDA_LINK_CB pIrdaLinkCb)
  425. {
  426. IRDA_MSG IMsg;
  427. PIRLAP_CB pIrlapCb = (PIRLAP_CB) pIrdaLinkCb->IrlapContext;
  428. IRLAP_LOG_START((pIrlapCb, TEXT("IRLAP: CloseLink")));
  429. ReturnRxTxWinMsgs(pIrlapCb);
  430. IrlapTimerStop(pIrlapCb, &pIrlapCb->SlotTimer);
  431. IrlapTimerStop(pIrlapCb, &pIrlapCb->QueryTimer);
  432. IrlapTimerStop(pIrlapCb, &pIrlapCb->PollTimer);
  433. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  434. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  435. IrlapTimerStop(pIrlapCb, &pIrlapCb->BackoffTimer);
  436. /*
  437. if (pIrlapCb->MonitorLink)
  438. {
  439. IrdaTimerStop(&pIrlapCb->StatusTimer);
  440. }
  441. */
  442. IRLAP_LOG_COMPLETE(pIrlapCb);
  443. IMsg.Prim = MAC_CONTROL_REQ;
  444. IMsg.IRDA_MSG_Op = MAC_CLOSE_LINK;
  445. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  446. return;
  447. }
  448. /*****************************************************************************
  449. *
  450. * Delete memory associated with an Irlap instance
  451. *
  452. */
  453. VOID
  454. IrlapDeleteInstance(PVOID Context)
  455. {
  456. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  457. #if DBG
  458. int i;
  459. for (i = 0; i < IRLAP_MOD; i++)
  460. {
  461. ASSERT(pIrlapCb->TxWin.pMsg[i] == NULL);
  462. ASSERT(pIrlapCb->RxWin.pMsg[i] == NULL);
  463. }
  464. #endif
  465. FreeDevList(&pIrlapCb->DevList);
  466. DEBUGMSG(DBG_ERROR, (TEXT("IRLAP: Delete instance %X\n"), pIrlapCb));
  467. IRDA_FREE_MEM(pIrlapCb);
  468. }
  469. /*****************************************************************************
  470. *
  471. * InitializeState - resets link control block
  472. *
  473. */
  474. VOID
  475. InitializeState(PIRLAP_CB pIrlapCb,
  476. IRLAP_STN_TYPE StationType)
  477. {
  478. PAGED_CODE();
  479. pIrlapCb->StationType = StationType;
  480. if (StationType == PRIMARY)
  481. pIrlapCb->CRBit = IRLAP_CMD;
  482. else
  483. pIrlapCb->CRBit = IRLAP_RSP;
  484. pIrlapCb->RemoteBusy = FALSE;
  485. pIrlapCb->LocalBusy = FALSE;
  486. pIrlapCb->ClrLocalBusy = FALSE;
  487. pIrlapCb->NoResponse = FALSE;
  488. pIrlapCb->LocalDiscReq = FALSE;
  489. pIrlapCb->ConnAfterClose = FALSE;
  490. pIrlapCb->DscvAfterClose = FALSE;
  491. pIrlapCb->GenNewAddr = FALSE;
  492. pIrlapCb->StatusSent = FALSE;
  493. pIrlapCb->Vs = 0;
  494. pIrlapCb->Vr = 0;
  495. pIrlapCb->WDogExpCnt = 0;
  496. pIrlapCb->StatusFlags = 0;
  497. pIrlapCb->FastPollCount = 0;
  498. FreeDevList(&pIrlapCb->DevList);
  499. memset(&pIrlapCb->RemoteQos, 0, sizeof(IRDA_QOS_PARMS));
  500. memset(&pIrlapCb->NegotiatedQos, 0, sizeof(IRDA_QOS_PARMS));
  501. // Return msgs on tx list and in tx/rx windows
  502. ReturnRxTxWinMsgs(pIrlapCb);
  503. }
  504. /*****************************************************************************
  505. *
  506. * IrlapDown - Entry point into IRLAP for LMP
  507. *
  508. */
  509. UINT
  510. IrlapDown(PVOID Context,
  511. PIRDA_MSG pMsg)
  512. {
  513. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  514. UINT rc = SUCCESS;
  515. IRLAP_LOG_START((pIrlapCb, IRDA_PrimStr[pMsg->Prim]));
  516. switch (pMsg->Prim)
  517. {
  518. case IRLAP_DISCOVERY_REQ:
  519. rc = ProcessDiscoveryReq(pIrlapCb, pMsg);
  520. break;
  521. case IRLAP_CONNECT_REQ:
  522. rc = ProcessConnectReq(pIrlapCb, pMsg);
  523. break;
  524. case IRLAP_CONNECT_RESP:
  525. ProcessConnectResp(pIrlapCb, pMsg);
  526. break;
  527. case IRLAP_DISCONNECT_REQ:
  528. ProcessDisconnectReq(pIrlapCb);
  529. break;
  530. case IRLAP_DATA_REQ:
  531. case IRLAP_UDATA_REQ:
  532. rc = ProcessDataAndUDataReq(pIrlapCb, pMsg);
  533. break;
  534. case IRLAP_FLOWON_REQ:
  535. if (pIrlapCb->LocalBusy)
  536. {
  537. IRLAP_LOG_ACTION((pIrlapCb,TEXT("Local busy condition cleared")));
  538. pIrlapCb->LocalBusy = FALSE;
  539. pIrlapCb->ClrLocalBusy = TRUE;
  540. }
  541. break;
  542. case IRLAP_STATUS_REQ:
  543. StatusReq(pIrlapCb, pMsg);
  544. break;
  545. default:
  546. ASSERT(0);
  547. rc = IRLAP_BAD_PRIM;
  548. }
  549. IRLAP_LOG_COMPLETE(pIrlapCb);
  550. return rc;
  551. }
  552. /*****************************************************************************
  553. *
  554. * IrlapUp - Entry point into IRLAP for MAC
  555. *
  556. */
  557. VOID
  558. IrlapUp(PVOID Context, PIRDA_MSG pMsg)
  559. {
  560. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  561. switch (pMsg->Prim)
  562. {
  563. case MAC_DATA_IND:
  564. // IRLAP_LOG_START((pIrlapCb, TEXT("MAC_DATA_IND: %s"), FrameToStr(pMsg)));
  565. IRLAP_LOG_START((pIrlapCb, TEXT("MAC_DATA_IND")));
  566. ProcessMACDataInd(pIrlapCb, pMsg);
  567. break;
  568. case MAC_DATA_CONF:
  569. IRLAP_LOG_START((pIrlapCb, TEXT("IRLAP: MAC_DATA_CONF pMsg:%X"), pMsg));
  570. ASSERT(pMsg->IRDA_MSG_RefCnt == 0);
  571. pMsg->Prim = IRLAP_DATA_CONF;
  572. pMsg->IRDA_MSG_DataStatus = IRLAP_DATA_REQUEST_COMPLETED;
  573. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  574. break;
  575. case MAC_CONTROL_CONF:
  576. IRLAP_LOG_START((pIrlapCb, IRDA_PrimStr[pMsg->Prim]));
  577. ProcessMACControlConf(pIrlapCb, pMsg);
  578. break;
  579. default:
  580. IRLAP_LOG_START((pIrlapCb, IRDA_PrimStr[pMsg->Prim]));
  581. ASSERT(0); //rc = IRLAP_BAD_PRIM;
  582. }
  583. IRLAP_LOG_COMPLETE(pIrlapCb);
  584. }
  585. /*****************************************************************************
  586. *
  587. */
  588. VOID
  589. ReturnRxTxWinMsgs(PIRLAP_CB pIrlapCb)
  590. {
  591. int i;
  592. IRDA_MSG *pMsg;
  593. PAGED_CODE();
  594. // Return messages on ExTxMsgList and TxMsgList to LMP
  595. pMsg = (PIRDA_MSG) RemoveHeadList(&pIrlapCb->ExTxMsgList);
  596. while (pMsg != (PIRDA_MSG) &pIrlapCb->ExTxMsgList)
  597. {
  598. pMsg->Prim += 2; // make it a confirm
  599. pMsg->IRDA_MSG_DataStatus = IRLAP_DATA_REQUEST_FAILED_LINK_RESET;
  600. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  601. pMsg = (PIRDA_MSG) RemoveHeadList(&pIrlapCb->ExTxMsgList);
  602. }
  603. pMsg = (PIRDA_MSG) RemoveHeadList(&pIrlapCb->TxMsgList);
  604. while (pMsg != (PIRDA_MSG) &pIrlapCb->TxMsgList)
  605. {
  606. pMsg->Prim += 2; // make it a confirm
  607. pMsg->IRDA_MSG_DataStatus = IRLAP_DATA_REQUEST_FAILED_LINK_RESET;
  608. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  609. pMsg = (PIRDA_MSG) RemoveHeadList(&pIrlapCb->TxMsgList);
  610. }
  611. pIrlapCb->TxWin.Start = 0;
  612. pIrlapCb->TxWin.End = 0;
  613. // Transmit window
  614. for (i = 0; i < IRLAP_MOD; i++)
  615. {
  616. pMsg = pIrlapCb->TxWin.pMsg[i];
  617. pIrlapCb->TxWin.pMsg[i] = NULL;
  618. if (pMsg != NULL)
  619. {
  620. ASSERT(pMsg->IRDA_MSG_RefCnt);
  621. if (InterlockedDecrement(&pMsg->IRDA_MSG_RefCnt) == 0)
  622. {
  623. pMsg->Prim = IRLAP_DATA_CONF;
  624. pMsg->IRDA_MSG_DataStatus = IRLAP_DATA_REQUEST_FAILED_LINK_RESET;
  625. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  626. }
  627. #if DBG
  628. else
  629. {
  630. DEBUGMSG(DBG_ERROR, (TEXT("IRLAP: Outstanding msg %X, SendCnt is %d at disconnect\n"),
  631. pMsg, pMsg->IRDA_MSG_RefCnt));
  632. }
  633. #endif
  634. }
  635. }
  636. // Cleanup RxWin
  637. pIrlapCb->RxWin.Start = 0;
  638. pIrlapCb->RxWin.End = 0;
  639. for (i = 0; i < IRLAP_MOD; i++)
  640. {
  641. // Receive window
  642. if ((pMsg = pIrlapCb->RxWin.pMsg[i]) != NULL)
  643. {
  644. pMsg->IRDA_MSG_RefCnt = 0;
  645. pMsg->Prim = MAC_DATA_RESP;
  646. IrmacDown(pIrlapCb->pIrdaLinkCb, pMsg);
  647. pIrlapCb->RxWin.pMsg[i] = NULL;
  648. }
  649. }
  650. return;
  651. }
  652. /*****************************************************************************
  653. *
  654. * MyDevAddr - Determines if DevAddr matches the local
  655. * device address or is the broadcast
  656. *
  657. * TRUE if address is mine or broadcast else FALS
  658. *
  659. *
  660. */
  661. BOOLEAN
  662. MyDevAddr(PIRLAP_CB pIrlapCb,
  663. UCHAR DevAddr[])
  664. {
  665. if (CTEMemCmp(DevAddr, IrlapBroadcastDevAddr,
  666. IRDA_DEV_ADDR_LEN) &&
  667. CTEMemCmp(DevAddr, pIrlapCb->LocalDevice.DevAddr,
  668. IRDA_DEV_ADDR_LEN))
  669. {
  670. return FALSE;
  671. }
  672. return TRUE;
  673. }
  674. /*****************************************************************************
  675. *
  676. * ProcessConnectReq - Process connect request from LMP
  677. *
  678. */
  679. UINT
  680. ProcessConnectReq(PIRLAP_CB pIrlapCb,
  681. PIRDA_MSG pMsg)
  682. {
  683. IRDA_MSG IMsg;
  684. PAGED_CODE();
  685. DEBUGMSG(DBG_IRLAP, (TEXT("IRLAP: ProcessConnectReq: state=%d\n"),pIrlapCb->State));
  686. switch (pIrlapCb->State)
  687. {
  688. case NDM:
  689. // Save Remote Address for later use
  690. CTEMemCopy(pIrlapCb->RemoteDevice.DevAddr,
  691. pMsg->IRDA_MSG_RemoteDevAddr,
  692. IRDA_DEV_ADDR_LEN);
  693. IMsg.Prim = MAC_CONTROL_REQ;
  694. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  695. IMsg.IRDA_MSG_SenseTime = IRLAP_MEDIA_SENSE_TIME;
  696. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  697. pIrlapCb->State = CONN_MEDIA_SENSE;
  698. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_CONTROL_REQ (media sense)")));
  699. break;
  700. case DSCV_REPLY:
  701. return IRLAP_REMOTE_DISCOVERY_IN_PROGRESS_ERR;
  702. case P_CLOSE:
  703. CTEMemCopy(pIrlapCb->RemoteDevice.DevAddr,
  704. pMsg->IRDA_MSG_RemoteDevAddr, IRDA_DEV_ADDR_LEN);
  705. pIrlapCb->ConnAfterClose = TRUE;
  706. break;
  707. case CONN_MEDIA_SENSE:
  708. IRLAP_LOG_ACTION((pIrlapCb, TEXT("already doing media sense")));
  709. ASSERT(0);
  710. return IRLAP_BAD_STATE;
  711. break;
  712. default:
  713. ASSERT(0);
  714. return IRLAP_BAD_STATE;
  715. }
  716. return SUCCESS;
  717. }
  718. /*****************************************************************************
  719. *
  720. * ProcessConnectResp - Process connect response from LMP
  721. *
  722. */
  723. VOID
  724. ProcessConnectResp(PIRLAP_CB pIrlapCb,
  725. PIRDA_MSG pMsg)
  726. {
  727. PAGED_CODE();
  728. DEBUGMSG(DBG_IRLAP, (TEXT("IRLAP: ProcessConnectResp: state=%d\n"),pIrlapCb->State));
  729. if (pIrlapCb->State != SNRM_RECEIVED)
  730. {
  731. ASSERT(0);
  732. return;
  733. }
  734. pIrlapCb->ConnAddr = pIrlapCb->SNRMConnAddr;
  735. SendUA(pIrlapCb, TRUE);
  736. ApplyQosParms(pIrlapCb);
  737. InitializeState(pIrlapCb, SECONDARY);
  738. // start watchdog timer with poll timeout
  739. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  740. pIrlapCb->State = S_NRM;
  741. }
  742. /*****************************************************************************
  743. *
  744. * ProcessDiscoveryReq - Process Discovery request from LMP
  745. *
  746. */
  747. UINT
  748. ProcessDiscoveryReq(PIRLAP_CB pIrlapCb,
  749. PIRDA_MSG pMsg)
  750. {
  751. IRDA_MSG IMsg;
  752. PAGED_CODE();
  753. DEBUGMSG(DBG_IRLAP, (TEXT("IRLAP: ProcessDiscoveryReq: state=%d\n"),pIrlapCb->State));
  754. switch (pIrlapCb->State)
  755. {
  756. case NDM:
  757. if (pMsg->IRDA_MSG_SenseMedia == TRUE)
  758. {
  759. IMsg.Prim = MAC_CONTROL_REQ;
  760. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  761. IMsg.IRDA_MSG_SenseTime = IRLAP_MEDIA_SENSE_TIME;
  762. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  763. pIrlapCb->State = DSCV_MEDIA_SENSE;
  764. IRLAP_LOG_ACTION((pIrlapCb,TEXT("MAC_CONTROL_REQ (mediasense)")));
  765. }
  766. else
  767. {
  768. pIrlapCb->SlotCnt = 0;
  769. pIrlapCb->GenNewAddr = FALSE;
  770. FreeDevList(&pIrlapCb->DevList);
  771. SendDscvXIDCmd(pIrlapCb);
  772. IMsg.Prim = MAC_CONTROL_REQ;
  773. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  774. IMsg.IRDA_MSG_SenseTime = IRLAP_DSCV_SENSE_TIME;
  775. IRLAP_LOG_ACTION((pIrlapCb,TEXT("MAC_CONTROL_REQ (dscvsense)")));
  776. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  777. pIrlapCb->State = DSCV_QUERY;
  778. }
  779. break;
  780. case DSCV_REPLY:
  781. return IRLAP_REMOTE_DISCOVERY_IN_PROGRESS_ERR;
  782. case SNRM_RECEIVED:
  783. return IRLAP_REMOTE_CONNECTION_IN_PROGRESS_ERR;
  784. case P_CLOSE:
  785. pIrlapCb->DscvAfterClose = TRUE;
  786. break;
  787. default:
  788. ASSERT(0);
  789. return IRLAP_BAD_STATE;
  790. }
  791. return SUCCESS;
  792. }
  793. /*****************************************************************************
  794. *
  795. * ProcessDisconnectReq - Process disconnect request from LMP
  796. *
  797. */
  798. VOID
  799. ProcessDisconnectReq(PIRLAP_CB pIrlapCb)
  800. {
  801. ReturnRxTxWinMsgs(pIrlapCb);
  802. DEBUGMSG(DBG_IRLAP, (TEXT("IRLAP: ProcessDisconnectReq: state=%d\n"),pIrlapCb->State));
  803. switch (pIrlapCb->State)
  804. {
  805. case NDM:
  806. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  807. break;
  808. case SNRM_SENT:
  809. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  810. pIrlapCb->State = NDM;
  811. break;
  812. case DSCV_REPLY:
  813. IrlapTimerStop(pIrlapCb, &pIrlapCb->QueryTimer);
  814. pIrlapCb->State = NDM;
  815. break;
  816. case DSCV_MEDIA_SENSE:
  817. case DSCV_QUERY:
  818. case CONN_MEDIA_SENSE:
  819. pIrlapCb->State = NDM;
  820. break;
  821. case BACKOFF_WAIT:
  822. IrlapTimerStop(pIrlapCb, &pIrlapCb->BackoffTimer);
  823. pIrlapCb->State = NDM;
  824. break;
  825. case SNRM_RECEIVED:
  826. pIrlapCb->ConnAddr = pIrlapCb->SNRMConnAddr;
  827. SendDM(pIrlapCb);
  828. pIrlapCb->ConnAddr = IRLAP_BROADCAST_CONN_ADDR;
  829. pIrlapCb->State = NDM;
  830. break;
  831. case P_XMIT:
  832. pIrlapCb->LocalDiscReq = TRUE;
  833. IrlapTimerStop(pIrlapCb, &pIrlapCb->PollTimer);
  834. SendDISC(pIrlapCb);
  835. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  836. pIrlapCb->RetryCnt = 0;
  837. pIrlapCb->State = P_CLOSE;
  838. break;
  839. case P_RECV:
  840. pIrlapCb->LocalDiscReq = TRUE;
  841. pIrlapCb->State = P_DISCONNECT_PEND;
  842. break;
  843. case S_NRM:
  844. pIrlapCb->LocalDiscReq = TRUE;
  845. pIrlapCb->State = S_DISCONNECT_PEND;
  846. break;
  847. default:
  848. ASSERT(0);
  849. // return IRLAP_BAD_STATE;
  850. }
  851. }
  852. /*****************************************************************************
  853. *
  854. * ProcessDataReq - Process data request from LMP
  855. *
  856. */
  857. UINT
  858. ProcessDataAndUDataReq(PIRLAP_CB pIrlapCb,
  859. PIRDA_MSG pMsg)
  860. {
  861. BOOLEAN LinkTurned;
  862. LONG_PTR DataSize = (pMsg->IRDA_MSG_pHdrWrite - pMsg->IRDA_MSG_pHdrRead) +
  863. (pMsg->IRDA_MSG_pWrite - pMsg->IRDA_MSG_pRead);
  864. DEBUGMSG(DBG_IRLAP, (TEXT("IRLAP: ProcessDataAndUDataReq: state=%d\n"),pIrlapCb->State));
  865. if (DataSize > pIrlapCb->RemoteDataSize)
  866. {
  867. return IRLAP_BAD_DATA_REQUEST;
  868. }
  869. switch (pIrlapCb->State)
  870. {
  871. case P_XMIT:
  872. // Enque message, then drain the message list. If the link
  873. // was turned in the process of draining messages stop Poll Timer,
  874. // start Final Timer and enter P_RECV. Otherwise we'll stay in P_XMIT
  875. // waiting for more data requests from LMP or Poll Timer expiration
  876. if (pMsg->IRDA_MSG_Expedited)
  877. {
  878. InsertTailList(&pIrlapCb->ExTxMsgList, &pMsg->Linkage);
  879. }
  880. else
  881. {
  882. InsertTailList(&pIrlapCb->TxMsgList, &pMsg->Linkage);
  883. }
  884. XmitTxMsgList(pIrlapCb, FALSE, &LinkTurned);
  885. if (LinkTurned)
  886. {
  887. IrlapTimerStop(pIrlapCb, &pIrlapCb->PollTimer);
  888. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  889. pIrlapCb->State = P_RECV;
  890. }
  891. return SUCCESS;
  892. case P_DISCONNECT_PEND: //For pending disc states, take the message.
  893. case S_DISCONNECT_PEND: // They will be returned when the link discs
  894. case P_RECV:
  895. case S_NRM:
  896. // Que the message for later transmission
  897. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Queueing request")));
  898. if (pMsg->IRDA_MSG_Expedited)
  899. {
  900. InsertTailList(&pIrlapCb->ExTxMsgList, &pMsg->Linkage);
  901. }
  902. else
  903. {
  904. InsertTailList(&pIrlapCb->TxMsgList, &pMsg->Linkage);
  905. }
  906. return SUCCESS;
  907. default:
  908. if (pMsg->Prim == IRLAP_DATA_REQ)
  909. {
  910. ASSERT(0);
  911. return IRLAP_BAD_STATE;
  912. }
  913. else
  914. {
  915. if (pIrlapCb->State == NDM)
  916. {
  917. SendUIFrame(pIrlapCb, pMsg);
  918. }
  919. else
  920. {
  921. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  922. }
  923. }
  924. }
  925. return SUCCESS;
  926. }
  927. /*****************************************************************************
  928. *
  929. */
  930. VOID
  931. XmitTxMsgList(PIRLAP_CB pIrlapCb, BOOLEAN AlwaysTurnLink,
  932. BOOLEAN *pLinkTurned)
  933. {
  934. IRDA_MSG *pMsg;
  935. BOOLEAN LinkTurned;
  936. LinkTurned = FALSE;
  937. // If the remote is not busy send data
  938. // If we need to clear the local busy condition, don't send data send RR
  939. if (!pIrlapCb->RemoteBusy && !pIrlapCb->ClrLocalBusy)
  940. {
  941. while (!LinkTurned)
  942. {
  943. if (!IsListEmpty(&pIrlapCb->ExTxMsgList))
  944. {
  945. pMsg = (PIRDA_MSG) RemoveHeadList(&pIrlapCb->ExTxMsgList);
  946. }
  947. else if (!IsListEmpty(&pIrlapCb->TxMsgList))
  948. {
  949. pMsg = (PIRDA_MSG) RemoveHeadList(&pIrlapCb->TxMsgList);
  950. }
  951. else
  952. {
  953. break;
  954. }
  955. pIrlapCb->FastPollCount = IRLAP_FAST_POLL_COUNT;
  956. pIrlapCb->PollTimer.Timeout = IRLAP_FAST_POLL_TIME >
  957. pIrlapCb->RemoteMaxTAT ? pIrlapCb->RemoteMaxTAT : IRLAP_FAST_POLL_TIME;
  958. if (pMsg->IRDA_MSG_pWrite - pMsg->IRDA_MSG_pRead)
  959. pIrlapCb->StatusFlags |= LF_TX;
  960. if (pMsg->Prim == IRLAP_DATA_REQ)
  961. {
  962. // Insert message into transmit window
  963. pIrlapCb->TxWin.pMsg[pIrlapCb->Vs] = pMsg;
  964. pMsg->IRDA_MSG_RefCnt = 1;
  965. // Send message. If full window or there are no
  966. // more data requests, send with PF Set (turns link).
  967. if ((pIrlapCb->Vs == (pIrlapCb->TxWin.Start +
  968. pIrlapCb->RemoteWinSize-1) % IRLAP_MOD) ||
  969. (IsListEmpty(&pIrlapCb->TxMsgList) &&
  970. IsListEmpty(&pIrlapCb->ExTxMsgList)))
  971. {
  972. SendIFrame(pIrlapCb,
  973. pMsg,
  974. pIrlapCb->Vs,
  975. IRLAP_PFBIT_SET);
  976. LinkTurned = TRUE;
  977. }
  978. else
  979. {
  980. SendIFrame(pIrlapCb,
  981. pMsg,
  982. pIrlapCb->Vs,
  983. IRLAP_PFBIT_CLEAR);
  984. }
  985. pIrlapCb->Vs = (pIrlapCb->Vs + 1) % IRLAP_MOD;
  986. }
  987. else // IRLAP_UDATA_REQUEST
  988. {
  989. // For now, always turn link
  990. SendUIFrame(pIrlapCb, pMsg);
  991. pMsg->Prim = IRLAP_UDATA_CONF;
  992. pMsg->IRDA_MSG_DataStatus = IRLAP_DATA_REQUEST_COMPLETED;
  993. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  994. LinkTurned = TRUE;
  995. }
  996. }
  997. pIrlapCb->TxWin.End = pIrlapCb->Vs;
  998. }
  999. if ((AlwaysTurnLink && !LinkTurned) || pIrlapCb->ClrLocalBusy)
  1000. {
  1001. SendRR_RNR(pIrlapCb);
  1002. LinkTurned = TRUE;
  1003. if (pIrlapCb->ClrLocalBusy)
  1004. {
  1005. pIrlapCb->ClrLocalBusy = FALSE;
  1006. }
  1007. }
  1008. if (pLinkTurned != NULL)
  1009. {
  1010. *pLinkTurned = LinkTurned;
  1011. }
  1012. }
  1013. VOID
  1014. GotoPCloseState(PIRLAP_CB pIrlapCb)
  1015. {
  1016. IRDA_MSG IMsg;
  1017. PAGED_CODE();
  1018. if (!pIrlapCb->LocalDiscReq)
  1019. {
  1020. IMsg.Prim = IRLAP_DISCONNECT_IND;
  1021. IMsg.IRDA_MSG_DiscStatus = IRLAP_REMOTE_INITIATED;
  1022. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1023. }
  1024. ReturnRxTxWinMsgs(pIrlapCb);
  1025. pIrlapCb->State = P_CLOSE;
  1026. }
  1027. VOID
  1028. GotoNDMThenDscvOrConn(PIRLAP_CB pIrlapCb)
  1029. {
  1030. IRDA_MSG IMsg;
  1031. PAGED_CODE();
  1032. if (pIrlapCb->ConnAfterClose)
  1033. {
  1034. pIrlapCb->ConnAfterClose = FALSE;
  1035. IMsg.Prim = MAC_CONTROL_REQ;
  1036. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  1037. IMsg.IRDA_MSG_SenseTime = IRLAP_MEDIA_SENSE_TIME;
  1038. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  1039. pIrlapCb->State = CONN_MEDIA_SENSE;
  1040. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_CONTROL_REQ (conn media sense)")));
  1041. return;
  1042. }
  1043. if (pIrlapCb->DscvAfterClose)
  1044. {
  1045. pIrlapCb->DscvAfterClose = FALSE;
  1046. IMsg.Prim = MAC_CONTROL_REQ;
  1047. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  1048. IMsg.IRDA_MSG_SenseTime = IRLAP_MEDIA_SENSE_TIME;
  1049. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  1050. pIrlapCb->State = DSCV_MEDIA_SENSE;
  1051. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_CONTROL_REQ (disc media sense)")));
  1052. return;
  1053. }
  1054. pIrlapCb->State = NDM;
  1055. return;
  1056. }
  1057. /*****************************************************************************
  1058. *
  1059. * ProcessMACControlConf - Process a control confirm from MAC
  1060. *
  1061. */
  1062. VOID
  1063. ProcessMACControlConf(PIRLAP_CB pIrlapCb, PIRDA_MSG pMsg)
  1064. {
  1065. IRDA_MSG IMsg;
  1066. PAGED_CODE();
  1067. if (pMsg->IRDA_MSG_Op != MAC_MEDIA_SENSE)
  1068. {
  1069. ASSERT(0);
  1070. return; //IRLAP_BAD_OP;
  1071. }
  1072. switch (pIrlapCb->State)
  1073. {
  1074. case DSCV_MEDIA_SENSE:
  1075. switch (pMsg->IRDA_MSG_OpStatus)
  1076. {
  1077. case MAC_MEDIA_CLEAR:
  1078. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_MEDIA_CLEAR")));
  1079. //IndicateLinkStatus(pIrlapCb, LINK_STATUS_DISCOVERING);
  1080. pIrlapCb->SlotCnt = 0;
  1081. pIrlapCb->GenNewAddr = FALSE;
  1082. FreeDevList(&pIrlapCb->DevList);
  1083. SendDscvXIDCmd(pIrlapCb);
  1084. IMsg.Prim = MAC_CONTROL_REQ;
  1085. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  1086. IMsg.IRDA_MSG_SenseTime = IRLAP_DSCV_SENSE_TIME;
  1087. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_CONTROL_REQ (dscvsense)")));
  1088. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  1089. pIrlapCb->State = DSCV_QUERY;
  1090. break;
  1091. case MAC_MEDIA_BUSY:
  1092. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_MEDIA_BUSY")));
  1093. pIrlapCb->State = NDM;
  1094. IMsg.Prim = IRLAP_DISCOVERY_CONF;
  1095. IMsg.IRDA_MSG_pDevList = NULL;
  1096. IMsg.IRDA_MSG_DscvStatus = MAC_MEDIA_BUSY;
  1097. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1098. break;
  1099. default:
  1100. ASSERT(0);
  1101. return;// IRLAP_BAD_OPSTATUS;
  1102. }
  1103. break;
  1104. case CONN_MEDIA_SENSE:
  1105. switch (pMsg->IRDA_MSG_OpStatus)
  1106. {
  1107. case MAC_MEDIA_CLEAR:
  1108. // Generate a random connection address
  1109. pIrlapCb->ConnAddr = IRLAP_RAND(1, 0x7e);
  1110. pIrlapCb->RetryCnt = 0;
  1111. SendSNRM(pIrlapCb, TRUE);
  1112. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  1113. pIrlapCb->State = SNRM_SENT;
  1114. break;
  1115. case MAC_MEDIA_BUSY:
  1116. pIrlapCb->State = NDM;
  1117. IMsg.Prim = IRLAP_DISCONNECT_IND;
  1118. IMsg.IRDA_MSG_DiscStatus = MAC_MEDIA_BUSY;
  1119. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1120. break;
  1121. default:
  1122. ASSERT(0);
  1123. return;// IRLAP_BAD_OPSTATUS;
  1124. }
  1125. break;
  1126. case DSCV_QUERY:
  1127. switch (pMsg->IRDA_MSG_OpStatus)
  1128. {
  1129. case MAC_MEDIA_CLEAR:
  1130. // Nobody responded, procede as if the slot timer expired
  1131. IRLAP_LOG_ACTION((pIrlapCb,
  1132. TEXT("Media clear, making fake slot exp")));
  1133. SlotTimerExp(pIrlapCb);
  1134. break;
  1135. case MAC_MEDIA_BUSY:
  1136. // Some responding, give'm more time
  1137. IRLAP_LOG_ACTION((pIrlapCb,
  1138. TEXT("Media busy, starting slot timer")));
  1139. IrlapTimerStart(pIrlapCb, &pIrlapCb->SlotTimer);
  1140. break;
  1141. }
  1142. break;
  1143. default:
  1144. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  1145. }
  1146. }
  1147. /*****************************************************************************
  1148. *
  1149. * ProcessMACDataInd - Processes MAC Data
  1150. *
  1151. *
  1152. */
  1153. VOID
  1154. ProcessMACDataInd(PIRLAP_CB pIrlapCb, PIRDA_MSG pMsg)
  1155. {
  1156. UCHAR Addr = IRLAP_GET_ADDR(*(pMsg->IRDA_MSG_pRead));
  1157. UCHAR CRBit = IRLAP_GET_CRBIT(*(pMsg->IRDA_MSG_pRead));
  1158. UCHAR Cntl = *(pMsg->IRDA_MSG_pRead + 1);
  1159. UCHAR PFBit = IRLAP_GET_PFBIT(Cntl);
  1160. UCHAR FrameType = IRLAP_FRAME_TYPE(Cntl);
  1161. UINT Ns = IRLAP_GET_NS(Cntl);
  1162. UINT Nr = IRLAP_GET_NR(Cntl);
  1163. UCHAR XIDFormatID = *(pMsg->IRDA_MSG_pRead+2);
  1164. IRLAP_XID_DSCV_FORMAT *pXIDFormat = (IRLAP_XID_DSCV_FORMAT *)
  1165. (pMsg->IRDA_MSG_pRead + 3);
  1166. IRLAP_SNRM_FORMAT *pSNRMFormat = (IRLAP_SNRM_FORMAT *)
  1167. (pMsg->IRDA_MSG_pRead + 2);
  1168. IRLAP_UA_FORMAT *pUAFormat = (IRLAP_UA_FORMAT *)
  1169. (pMsg->IRDA_MSG_pRead + 2);
  1170. if (Addr != pIrlapCb->ConnAddr && Addr != IRLAP_BROADCAST_CONN_ADDR)
  1171. {
  1172. IRLAP_LOG_ACTION((pIrlapCb,
  1173. TEXT("Ignoring, connection address %02X"), Addr));
  1174. return;
  1175. }
  1176. pIrlapCb->StatusSent = FALSE;
  1177. pIrlapCb->Frmr.CntlField = Cntl; // for later maybe
  1178. // Peer has sent a frame so clear the NoResponse condition
  1179. // Unnumbered frame shouldn't reset the no response condition
  1180. // (ie only frames received in the connected state).
  1181. if (pIrlapCb->NoResponse && FrameType != IRLAP_U_FRAME)
  1182. {
  1183. pIrlapCb->NoResponse = FALSE;
  1184. pIrlapCb->RetryCnt = 0;
  1185. pIrlapCb->WDogExpCnt = 0;
  1186. pIrlapCb->StatusFlags = LF_CONNECTED;
  1187. IndicateLinkStatus(pIrlapCb);
  1188. }
  1189. switch (FrameType)
  1190. {
  1191. /*****************/
  1192. case IRLAP_I_FRAME:
  1193. /*****************/
  1194. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: I-frame)")));
  1195. ProcessIFrame(pIrlapCb, pMsg, CRBit, PFBit, Ns, Nr);
  1196. return;
  1197. /*****************/
  1198. case IRLAP_S_FRAME:
  1199. /*****************/
  1200. switch (IRLAP_GET_SCNTL(Cntl))
  1201. {
  1202. /*-----------*/
  1203. case IRLAP_RR:
  1204. case IRLAP_RNR:
  1205. /*-----------*/
  1206. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(RR/RNR-frame)")));
  1207. ProcessRR_RNR(pIrlapCb, IRLAP_GET_SCNTL(Cntl),
  1208. pMsg, CRBit, PFBit, Nr);
  1209. return;
  1210. /*------------*/
  1211. case IRLAP_SREJ:
  1212. case IRLAP_REJ:
  1213. /*------------*/
  1214. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(SJREJ/REJ-frame)")));
  1215. ProcessREJ_SREJ(pIrlapCb, IRLAP_GET_SCNTL(Cntl),
  1216. pMsg, CRBit, PFBit, Nr);
  1217. return;
  1218. }
  1219. break;
  1220. /*****************/
  1221. case IRLAP_U_FRAME:
  1222. /*****************/
  1223. switch (IRLAP_GET_UCNTL(Cntl))
  1224. {
  1225. /*---------------*/
  1226. case IRLAP_XID_CMD:
  1227. /*---------------*/
  1228. // Should always be a command
  1229. if (CRBit != IRLAP_CMD)
  1230. {
  1231. IRLAP_LOG_ACTION((pIrlapCb,
  1232. TEXT("Received XID cmd with CRBit = rsp")));
  1233. ASSERT(0);
  1234. return; // IRLAP_XID_CMD_RSP;
  1235. }
  1236. // Poll bit should always be set
  1237. if (PFBit != IRLAP_PFBIT_SET)
  1238. {
  1239. IRLAP_LOG_ACTION((pIrlapCb,
  1240. TEXT("Received XID command without Poll set")));
  1241. ASSERT(0);
  1242. return; // IRLAP_XID_CMD_NOT_P;
  1243. }
  1244. if (XIDFormatID == IRLAP_XID_DSCV_FORMAT_ID)
  1245. {
  1246. // Slot No is less than max slot or 0xff
  1247. if (pXIDFormat->SlotNo > IrlapSlotTable[pXIDFormat->NoOfSlots]
  1248. && pXIDFormat->SlotNo != IRLAP_END_DSCV_SLOT_NO)
  1249. {
  1250. IRLAP_LOG_ACTION((pIrlapCb,
  1251. TEXT("Invalid slot number %d"),
  1252. pXIDFormat->SlotNo));
  1253. ASSERT(0);
  1254. return;// IRLAP_BAD_SLOTNO;
  1255. }
  1256. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: DscvXidCmd slot=%d)"),pXIDFormat->SlotNo));
  1257. ProcessDscvXIDCmd(pIrlapCb, pXIDFormat,
  1258. pMsg->IRDA_MSG_pWrite);
  1259. return;
  1260. }
  1261. else
  1262. {
  1263. return; // ignore per errata
  1264. }
  1265. /*---------------*/
  1266. case IRLAP_XID_RSP:
  1267. /*---------------*/
  1268. if (XIDFormatID == IRLAP_XID_DSCV_FORMAT_ID)
  1269. {
  1270. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: DscvXidRsp)")));
  1271. ProcessDscvXIDRsp(pIrlapCb, pXIDFormat,pMsg->IRDA_MSG_pWrite);
  1272. return;
  1273. }
  1274. else
  1275. {
  1276. return; // ignore per errata
  1277. }
  1278. /*------------*/
  1279. case IRLAP_SNRM: // or IRLAP_RNRM
  1280. /*------------*/
  1281. if (IRLAP_PFBIT_SET != PFBit)
  1282. {
  1283. IRLAP_LOG_ACTION((pIrlapCb,
  1284. TEXT("Received SNRM/RNRM without P set")));
  1285. return;// IRLAP_SNRM_NOT_P;
  1286. }
  1287. if (IRLAP_CMD == CRBit)
  1288. {
  1289. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: SNRM)")));
  1290. ProcessSNRM(pIrlapCb, pSNRMFormat, pMsg->IRDA_MSG_pWrite);
  1291. return;
  1292. }
  1293. else
  1294. {
  1295. ProcessRNRM(pIrlapCb);
  1296. return;
  1297. }
  1298. /*----------*/
  1299. case IRLAP_UA:
  1300. /*----------*/
  1301. if (CRBit != IRLAP_RSP)
  1302. {
  1303. IRLAP_LOG_ACTION((pIrlapCb,
  1304. TEXT("Received UA as a command")));
  1305. return;// IRLAP_UA_NOT_RSP;
  1306. }
  1307. if (PFBit != IRLAP_PFBIT_SET)
  1308. {
  1309. IRLAP_LOG_ACTION((pIrlapCb,
  1310. TEXT("Received UA without F set")));
  1311. return;// IRLAP_UA_NOT_F;
  1312. }
  1313. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: UA)")));
  1314. ProcessUA(pIrlapCb, pUAFormat, pMsg->IRDA_MSG_pWrite);
  1315. return;
  1316. /*------------*/
  1317. case IRLAP_DISC: // or IRLAP_RD
  1318. /*------------*/
  1319. if (IRLAP_PFBIT_SET != PFBit)
  1320. {
  1321. IRLAP_LOG_ACTION((pIrlapCb,
  1322. TEXT("Received DISC/RD command without Poll set")));
  1323. return;// IRLAP_DISC_CMD_NOT_P;
  1324. }
  1325. if (IRLAP_CMD == CRBit)
  1326. {
  1327. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: DISC)")));
  1328. ProcessDISC(pIrlapCb);
  1329. return;
  1330. }
  1331. else
  1332. {
  1333. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: RD)")));
  1334. ProcessRD(pIrlapCb);
  1335. return;
  1336. }
  1337. /*----------*/
  1338. case IRLAP_UI:
  1339. /*----------*/
  1340. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: UI)")));
  1341. ProcessUI(pIrlapCb, pMsg, CRBit, PFBit);
  1342. return;
  1343. /*------------*/
  1344. case IRLAP_TEST:
  1345. /*------------*/
  1346. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: TEST)")));
  1347. // ProcessTEST(pIrlapCb, pMsg, pUAFormat, CRBit, PFBit);
  1348. return;
  1349. /*------------*/
  1350. case IRLAP_FRMR:
  1351. /*------------*/
  1352. if (IRLAP_RSP != CRBit)
  1353. {
  1354. IRLAP_LOG_ACTION((pIrlapCb,
  1355. TEXT("Received FRMR cmd (must be resp)")));
  1356. return;// IRLAP_FRMR_RSP_CMD;
  1357. }
  1358. if (IRLAP_PFBIT_SET != PFBit)
  1359. {
  1360. IRLAP_LOG_ACTION((pIrlapCb,
  1361. TEXT("Received FRMR resp without Final set")));
  1362. return;// IRLAP_FRMR_RSP_NOT_F;
  1363. }
  1364. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: FRMR)")));
  1365. ProcessFRMR(pIrlapCb);
  1366. return;
  1367. /*----------*/
  1368. case IRLAP_DM:
  1369. /*----------*/
  1370. if (IRLAP_RSP != CRBit)
  1371. {
  1372. IRLAP_LOG_ACTION((pIrlapCb,
  1373. TEXT("Received DM command (must be response)")));
  1374. return;// IRLAP_DM_RSP_CMD;
  1375. }
  1376. if (IRLAP_PFBIT_SET != PFBit)
  1377. {
  1378. IRLAP_LOG_ACTION((pIrlapCb,
  1379. TEXT("Received DM response without Final set")));
  1380. return;// IRLAP_DM_RSP_NOT_F;
  1381. }
  1382. IRLAP_LOG_ACTION((pIrlapCb, TEXT("(Rx: DM)")));
  1383. ProcessDM(pIrlapCb);
  1384. return;
  1385. }
  1386. break;
  1387. }
  1388. }
  1389. /*****************************************************************************
  1390. *
  1391. * ProcessDscvXIDCmd - Process received XID Discovery command
  1392. *
  1393. */
  1394. VOID
  1395. ProcessDscvXIDCmd(PIRLAP_CB pIrlapCb,
  1396. IRLAP_XID_DSCV_FORMAT *pXidFormat,
  1397. UCHAR *pEndDscvInfoUCHAR)
  1398. {
  1399. IRDA_MSG IMsg;
  1400. if (!MyDevAddr(pIrlapCb, pXidFormat->DestAddr))
  1401. {
  1402. /* IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring XID addressed to:%02X%02X%02X%02X"),
  1403. EXPAND_ADDR(pXidFormat->DestAddr)));*/
  1404. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring XID addressed to %X"),
  1405. pXidFormat->DestAddr));
  1406. return;
  1407. }
  1408. if (pXidFormat->SlotNo == IRLAP_END_DSCV_SLOT_NO)
  1409. {
  1410. pIrlapCb->GenNewAddr = FALSE;
  1411. switch (pIrlapCb->State)
  1412. {
  1413. case DSCV_QUERY:
  1414. IrlapTimerStop(pIrlapCb, &pIrlapCb->SlotTimer);
  1415. IMsg.Prim = IRLAP_DISCOVERY_CONF;
  1416. IMsg.IRDA_MSG_pDevList = NULL;
  1417. IMsg.IRDA_MSG_DscvStatus =
  1418. IRLAP_REMOTE_DISCOVERY_IN_PROGRESS;
  1419. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1420. // fall through. Send indication to LMP
  1421. case DSCV_REPLY:
  1422. if (pIrlapCb->State == DSCV_REPLY)
  1423. {
  1424. IrlapTimerStop(pIrlapCb, &pIrlapCb->QueryTimer);
  1425. }
  1426. // Place the device information in the control block
  1427. ExtractDeviceInfo(&pIrlapCb->RemoteDevice, pXidFormat,
  1428. pEndDscvInfoUCHAR);
  1429. if (!DevInDevList(pXidFormat->SrcAddr, &pIrlapCb->DevList))
  1430. {
  1431. AddDevToList(pIrlapCb, pXidFormat, pEndDscvInfoUCHAR);
  1432. }
  1433. // Notifiy LMP
  1434. pIrlapCb->State = NDM;
  1435. IMsg.Prim = IRLAP_DISCOVERY_IND;
  1436. IMsg.IRDA_MSG_pDevList = &pIrlapCb->DevList;
  1437. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1438. break;
  1439. default:
  1440. IRLAP_LOG_ACTION((pIrlapCb,
  1441. TEXT("Ignoring End XID in this state")));
  1442. }
  1443. }
  1444. else // in middle of discovery process
  1445. {
  1446. switch (pIrlapCb->State)
  1447. {
  1448. case DSCV_MEDIA_SENSE:
  1449. IMsg.Prim = IRLAP_DISCOVERY_CONF;
  1450. IMsg.IRDA_MSG_pDevList = NULL;
  1451. IMsg.IRDA_MSG_DscvStatus =
  1452. IRLAP_REMOTE_DISCOVERY_IN_PROGRESS;
  1453. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1454. // fall through
  1455. case NDM:
  1456. InitDscvCmdProcessing(pIrlapCb, pXidFormat);
  1457. pIrlapCb->State = DSCV_REPLY;
  1458. break;
  1459. case DSCV_QUERY:
  1460. pIrlapCb->State = NDM;
  1461. IMsg.Prim = IRLAP_DISCOVERY_CONF;
  1462. IMsg.IRDA_MSG_pDevList = NULL;
  1463. IMsg.IRDA_MSG_DscvStatus = IRLAP_DISCOVERY_COLLISION;
  1464. IrlapTimerStop(pIrlapCb, &pIrlapCb->SlotTimer);
  1465. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1466. break;
  1467. case DSCV_REPLY:
  1468. if (pXidFormat->GenNewAddr)
  1469. {
  1470. pIrlapCb->GenNewAddr = TRUE;
  1471. IrlapTimerStop(pIrlapCb, &pIrlapCb->QueryTimer);
  1472. InitDscvCmdProcessing(pIrlapCb, pXidFormat);
  1473. }
  1474. else
  1475. {
  1476. if (pIrlapCb->RespSlot <= pXidFormat->SlotNo &&
  1477. !pIrlapCb->DscvRespSent)
  1478. {
  1479. SendDscvXIDRsp(pIrlapCb);
  1480. pIrlapCb->DscvRespSent = TRUE;
  1481. }
  1482. }
  1483. break;
  1484. default:
  1485. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  1486. }
  1487. }
  1488. return;
  1489. }
  1490. /*****************************************************************************
  1491. *
  1492. *
  1493. */
  1494. void
  1495. ExtractDeviceInfo(IRDA_DEVICE *pDevice, IRLAP_XID_DSCV_FORMAT *pXidFormat,
  1496. UCHAR *pEndDscvInfoUCHAR)
  1497. {
  1498. PAGED_CODE();
  1499. CTEMemCopy(pDevice->DevAddr, pXidFormat->SrcAddr, IRDA_DEV_ADDR_LEN);
  1500. pDevice->IRLAP_Version = pXidFormat->Version;
  1501. // ??? what about DscvMethod
  1502. pDevice->DscvInfoLen =
  1503. pEndDscvInfoUCHAR > &pXidFormat->FirstDscvInfoByte ?
  1504. (int) (pEndDscvInfoUCHAR-&pXidFormat->FirstDscvInfoByte) : 0;
  1505. if (pDevice->DscvInfoLen > IRLAP_DSCV_INFO_LEN)
  1506. {
  1507. pDevice->DscvInfoLen = IRLAP_DSCV_INFO_LEN;
  1508. }
  1509. CTEMemCopy(pDevice->DscvInfo, &pXidFormat->FirstDscvInfoByte,
  1510. pDevice->DscvInfoLen);
  1511. }
  1512. /*****************************************************************************
  1513. *
  1514. *
  1515. */
  1516. VOID
  1517. InitDscvCmdProcessing(PIRLAP_CB pIrlapCb,
  1518. IRLAP_XID_DSCV_FORMAT *pXidFormat)
  1519. {
  1520. pIrlapCb->RemoteMaxSlot = IrlapSlotTable[pXidFormat->NoOfSlots];
  1521. pIrlapCb->RespSlot = IRLAP_RAND(pXidFormat->SlotNo,
  1522. pIrlapCb->RemoteMaxSlot - 1);
  1523. CTEMemCopy(pIrlapCb->RemoteDevice.DevAddr, pXidFormat->SrcAddr,
  1524. IRDA_DEV_ADDR_LEN);
  1525. IRLAP_LOG_ACTION((pIrlapCb,
  1526. TEXT("Responding in slot %d to dev %02X%02X%02X%02X"),
  1527. pIrlapCb->RespSlot,
  1528. pIrlapCb->RemoteDevice.DevAddr[0],
  1529. pIrlapCb->RemoteDevice.DevAddr[1],
  1530. pIrlapCb->RemoteDevice.DevAddr[2],
  1531. pIrlapCb->RemoteDevice.DevAddr[3]));
  1532. if (pIrlapCb->RespSlot == pXidFormat->SlotNo)
  1533. {
  1534. SendDscvXIDRsp(pIrlapCb);
  1535. pIrlapCb->DscvRespSent = TRUE;
  1536. }
  1537. else
  1538. {
  1539. pIrlapCb->DscvRespSent = FALSE;
  1540. }
  1541. IrlapTimerStart(pIrlapCb, &pIrlapCb->QueryTimer);
  1542. }
  1543. /*****************************************************************************
  1544. *
  1545. */
  1546. VOID
  1547. ProcessDscvXIDRsp(PIRLAP_CB pIrlapCb,
  1548. IRLAP_XID_DSCV_FORMAT *pXidFormat,
  1549. UCHAR *pEndDscvInfoUCHAR)
  1550. {
  1551. IRDA_MSG IMsg;
  1552. if (pIrlapCb->State == DSCV_QUERY)
  1553. {
  1554. if (DevInDevList(pXidFormat->SrcAddr, &pIrlapCb->DevList))
  1555. {
  1556. IrlapTimerStop(pIrlapCb, &pIrlapCb->SlotTimer);
  1557. pIrlapCb->SlotCnt = 0;
  1558. pIrlapCb->GenNewAddr = TRUE;
  1559. FreeDevList(&pIrlapCb->DevList);
  1560. SendDscvXIDCmd(pIrlapCb);
  1561. IMsg.Prim = MAC_CONTROL_REQ;
  1562. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  1563. IMsg.IRDA_MSG_SenseTime = IRLAP_DSCV_SENSE_TIME;
  1564. IRLAP_LOG_ACTION((pIrlapCb,
  1565. TEXT("MAC_CONTROL_REQ (dscv sense)")));
  1566. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  1567. }
  1568. else
  1569. {
  1570. AddDevToList(pIrlapCb, pXidFormat, pEndDscvInfoUCHAR);
  1571. }
  1572. }
  1573. else
  1574. {
  1575. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  1576. }
  1577. }
  1578. /*****************************************************************************
  1579. *
  1580. * DevInDevList - Determines if given device is already in list
  1581. *
  1582. */
  1583. BOOLEAN
  1584. DevInDevList(UCHAR DevAddr[], LIST_ENTRY *pDevList)
  1585. {
  1586. IRDA_DEVICE *pDevice;
  1587. pDevice = (IRDA_DEVICE *) pDevList->Flink;
  1588. while (pDevList != (LIST_ENTRY *) pDevice)
  1589. {
  1590. if (CTEMemCmp(pDevice->DevAddr, DevAddr,
  1591. IRDA_DEV_ADDR_LEN) == 0)
  1592. return (TRUE);
  1593. pDevice = (IRDA_DEVICE *) pDevice->Linkage.Flink;
  1594. }
  1595. return (FALSE);
  1596. }
  1597. /*****************************************************************************
  1598. *
  1599. * AddDevToList - Adds elements in a device list
  1600. *
  1601. */
  1602. VOID
  1603. AddDevToList(PIRLAP_CB pIrlapCb,
  1604. IRLAP_XID_DSCV_FORMAT *pXidFormat,
  1605. UCHAR *pEndDscvInfoUCHAR)
  1606. {
  1607. IRDA_DEVICE *pDevice;
  1608. if (IRDA_ALLOC_MEM(pDevice, sizeof(IRDA_DEVICE), MT_IRLAP_DEVICE) == NULL)
  1609. {
  1610. ASSERT(0);
  1611. return;// (IRLAP_MALLOC_FAILED);
  1612. }
  1613. else
  1614. {
  1615. ExtractDeviceInfo(pDevice, pXidFormat, pEndDscvInfoUCHAR);
  1616. InsertTailList(&pIrlapCb->DevList, &(pDevice->Linkage));
  1617. IRLAP_LOG_ACTION((pIrlapCb,
  1618. TEXT("%02X%02X%02X%02X added to Device List"),
  1619. EXPAND_ADDR(pDevice->DevAddr)));
  1620. }
  1621. }
  1622. /*****************************************************************************
  1623. *
  1624. */
  1625. void
  1626. FreeDevList(LIST_ENTRY *pDevList)
  1627. {
  1628. IRDA_DEVICE *pDevice;
  1629. PAGED_CODE();
  1630. while (IsListEmpty(pDevList) == FALSE)
  1631. {
  1632. pDevice = (IRDA_DEVICE *) RemoveHeadList(pDevList);
  1633. IRDA_FREE_MEM(pDevice);
  1634. }
  1635. //IRLAP_LOG_ACTION((pIrlapCb, TEXT("Device list cleared")));
  1636. }
  1637. /*****************************************************************************
  1638. *
  1639. */
  1640. int
  1641. AddressGreaterThan(UCHAR A1[], UCHAR A2[])
  1642. {
  1643. int i;
  1644. for (i = 0; i < IRDA_DEV_ADDR_LEN; i++)
  1645. {
  1646. if (A1[i] > A2[i])
  1647. return TRUE;
  1648. if (A1[i] != A2[1])
  1649. return FALSE;
  1650. }
  1651. return FALSE;
  1652. }
  1653. /*****************************************************************************
  1654. *
  1655. */
  1656. VOID
  1657. ProcessSNRM(PIRLAP_CB pIrlapCb,
  1658. IRLAP_SNRM_FORMAT *pSnrmFormat,
  1659. UCHAR *pEndQosUCHAR)
  1660. {
  1661. IRDA_MSG IMsg;
  1662. BOOLEAN QosInSNRM = &pSnrmFormat->FirstQosByte < pEndQosUCHAR;
  1663. BOOLEAN AddrsInSNRM = (UCHAR *)pSnrmFormat < pEndQosUCHAR;
  1664. UINT rc;
  1665. PAGED_CODE();
  1666. if (AddrsInSNRM)
  1667. {
  1668. if (!MyDevAddr(pIrlapCb, pSnrmFormat->DestAddr))
  1669. {
  1670. IRLAP_LOG_ACTION((pIrlapCb,
  1671. TEXT("Ignoring SNRM addressed to:%02X%02X%02X%02X"),
  1672. EXPAND_ADDR(pSnrmFormat->DestAddr)));
  1673. return;
  1674. }
  1675. CTEMemCopy(pIrlapCb->RemoteDevice.DevAddr,
  1676. pSnrmFormat->SrcAddr, IRDA_DEV_ADDR_LEN);
  1677. }
  1678. switch (pIrlapCb->State)
  1679. {
  1680. case DSCV_MEDIA_SENSE:
  1681. case DSCV_QUERY:
  1682. // In the middle of discovery... End discovery and reply to SNRM
  1683. IMsg.Prim = IRLAP_DISCOVERY_CONF;
  1684. IMsg.IRDA_MSG_pDevList = NULL;
  1685. IMsg.IRDA_MSG_DscvStatus = IRLAP_REMOTE_CONNECTION_IN_PROGRESS;
  1686. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1687. // fall through and send connect indication
  1688. case DSCV_REPLY:
  1689. case NDM:
  1690. if (pIrlapCb->State == DSCV_REPLY)
  1691. {
  1692. IrlapTimerStop(pIrlapCb, &pIrlapCb->QueryTimer);
  1693. }
  1694. if (AddrsInSNRM)
  1695. {
  1696. pIrlapCb->SNRMConnAddr =
  1697. (int)IRLAP_GET_ADDR(pSnrmFormat->ConnAddr);
  1698. }
  1699. if (QosInSNRM)
  1700. {
  1701. ExtractQosParms(&pIrlapCb->RemoteQos, &pSnrmFormat->FirstQosByte,
  1702. pEndQosUCHAR);
  1703. if ((rc = NegotiateQosParms(pIrlapCb, &pIrlapCb->RemoteQos)))
  1704. {
  1705. DEBUGMSG(1, (TEXT("IRLAP: SNRM/UA negotiation failed, rc=%d\n"), rc));
  1706. #if DBG
  1707. DbgPrint("IRLAP: SNRM/UA negotiation failed, rc=%d\n", rc);
  1708. #endif
  1709. return;
  1710. }
  1711. }
  1712. CTEMemCopy(IMsg.IRDA_MSG_RemoteDevAddr,
  1713. pIrlapCb->RemoteDevice.DevAddr, IRDA_DEV_ADDR_LEN);
  1714. IMsg.IRDA_MSG_pQos = &pIrlapCb->NegotiatedQos;
  1715. IMsg.Prim = IRLAP_CONNECT_IND;
  1716. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1717. pIrlapCb->State = SNRM_RECEIVED;
  1718. break;
  1719. case BACKOFF_WAIT: // CROSSED SNRM
  1720. // if Remote address greater than mine we'll respond to SNRM
  1721. if (AddrsInSNRM)
  1722. {
  1723. if (AddressGreaterThan(pSnrmFormat->SrcAddr,
  1724. pIrlapCb->LocalDevice.DevAddr))
  1725. {
  1726. IrlapTimerStop(pIrlapCb, &pIrlapCb->BackoffTimer);
  1727. }
  1728. }
  1729. // fall through
  1730. case CONN_MEDIA_SENSE: // CROSSED SNRM
  1731. case SNRM_SENT:
  1732. // if Remote address greater than mine we'll respond to SNRM
  1733. if (AddrsInSNRM && AddressGreaterThan(pSnrmFormat->SrcAddr,
  1734. pIrlapCb->LocalDevice.DevAddr))
  1735. {
  1736. if (pIrlapCb->State != BACKOFF_WAIT)
  1737. {
  1738. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  1739. }
  1740. InitializeState(pIrlapCb, SECONDARY);
  1741. if (QosInSNRM)
  1742. {
  1743. ExtractQosParms(&pIrlapCb->RemoteQos,
  1744. &pSnrmFormat->FirstQosByte, pEndQosUCHAR);
  1745. if ((rc = NegotiateQosParms(pIrlapCb, &pIrlapCb->RemoteQos)))
  1746. {
  1747. DEBUGMSG(1, (TEXT("IRLAP: SNRM/UA negotiation failed, rc=%d\n"), rc));
  1748. ASSERT(0);
  1749. pIrlapCb->State = NDM;
  1750. return;
  1751. }
  1752. }
  1753. if (AddrsInSNRM)
  1754. {
  1755. pIrlapCb->ConnAddr =
  1756. (int)IRLAP_GET_ADDR(pSnrmFormat->ConnAddr);
  1757. }
  1758. SendUA(pIrlapCb, TRUE);
  1759. if (QosInSNRM)
  1760. {
  1761. ApplyQosParms(pIrlapCb);
  1762. }
  1763. IMsg.IRDA_MSG_pQos = &pIrlapCb->NegotiatedQos;
  1764. IMsg.Prim = IRLAP_CONNECT_CONF;
  1765. IMsg.IRDA_MSG_ConnStatus = IRLAP_CONNECTION_COMPLETED;
  1766. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1767. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  1768. pIrlapCb->State = S_NRM;
  1769. }
  1770. break;
  1771. case P_RECV:
  1772. case P_DISCONNECT_PEND:
  1773. case P_CLOSE:
  1774. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  1775. pIrlapCb->State = NDM;
  1776. StationConflict(pIrlapCb);
  1777. ReturnRxTxWinMsgs(pIrlapCb);
  1778. if (pIrlapCb->State == P_CLOSE)
  1779. {
  1780. GotoNDMThenDscvOrConn(pIrlapCb);
  1781. }
  1782. break;
  1783. case S_NRM:
  1784. case S_CLOSE:
  1785. case S_DISCONNECT_PEND:
  1786. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  1787. SendDM(pIrlapCb);
  1788. ApplyDefaultParms(pIrlapCb);
  1789. IMsg.Prim = IRLAP_DISCONNECT_IND;
  1790. if (pIrlapCb->State == S_NRM)
  1791. {
  1792. IMsg.IRDA_MSG_DiscStatus = IRLAP_DECLINE_RESET;
  1793. }
  1794. else
  1795. {
  1796. IMsg.IRDA_MSG_DiscStatus = IRLAP_DISCONNECT_COMPLETED;
  1797. }
  1798. pIrlapCb->State = NDM;
  1799. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1800. break;
  1801. case S_ERROR:
  1802. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  1803. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  1804. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  1805. pIrlapCb->State = S_NRM;
  1806. break;
  1807. default:
  1808. IRLAP_LOG_ACTION((pIrlapCb, TEXT("SNRM ignored in this state")));
  1809. }
  1810. return;
  1811. }
  1812. /*****************************************************************************
  1813. *
  1814. */
  1815. VOID
  1816. ProcessUA(PIRLAP_CB pIrlapCb,
  1817. IRLAP_UA_FORMAT *pUAFormat,
  1818. UCHAR *pEndQosUCHAR)
  1819. {
  1820. BOOLEAN QosInUA = &pUAFormat->FirstQosByte < pEndQosUCHAR;
  1821. BOOLEAN AddrsInUA = (UCHAR *)pUAFormat < pEndQosUCHAR;
  1822. int Tmp;
  1823. IRDA_MSG IMsg;
  1824. UINT rc;
  1825. PAGED_CODE();
  1826. if (AddrsInUA && !MyDevAddr(pIrlapCb, pUAFormat->DestAddr))
  1827. {
  1828. IRLAP_LOG_ACTION((pIrlapCb,
  1829. TEXT("Ignoring UA addressed to:%02X%02X%02X%02X"),
  1830. EXPAND_ADDR(pUAFormat->DestAddr)));
  1831. return;
  1832. }
  1833. switch (pIrlapCb->State)
  1834. {
  1835. case BACKOFF_WAIT:
  1836. IrlapTimerStop(pIrlapCb, &pIrlapCb->BackoffTimer);
  1837. // fall through
  1838. case SNRM_SENT:
  1839. if (pIrlapCb->State != BACKOFF_WAIT)
  1840. {
  1841. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  1842. }
  1843. InitializeState(pIrlapCb, PRIMARY);
  1844. if (QosInUA)
  1845. {
  1846. ExtractQosParms(&pIrlapCb->RemoteQos, &pUAFormat->FirstQosByte,
  1847. pEndQosUCHAR);
  1848. if ((rc = NegotiateQosParms(pIrlapCb, &pIrlapCb->RemoteQos)))
  1849. {
  1850. DEBUGMSG(1, (TEXT("IRLAP: SNRM/UA negotiation failed, rc=%d\n"), rc));
  1851. ASSERT(0);
  1852. pIrlapCb->State = NDM;
  1853. return;
  1854. }
  1855. ApplyQosParms(pIrlapCb);
  1856. }
  1857. IMsg.IRDA_MSG_pQos = &pIrlapCb->NegotiatedQos;
  1858. IMsg.Prim = IRLAP_CONNECT_CONF;
  1859. IMsg.IRDA_MSG_ConnStatus = IRLAP_CONNECTION_COMPLETED;
  1860. // notify LMP of connection
  1861. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1862. // send RR (turn link), start FinalTimer/2
  1863. SendRR_RNR(pIrlapCb);
  1864. Tmp = pIrlapCb->FinalTimer.Timeout;
  1865. pIrlapCb->FinalTimer.Timeout = pIrlapCb->FinalTimer.Timeout/2;
  1866. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  1867. pIrlapCb->FinalTimer.Timeout = Tmp;
  1868. pIrlapCb->State = P_RECV;
  1869. break;
  1870. case P_RECV: // Unsolicited UA, may want to do something else ???
  1871. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  1872. IrlapTimerStart(pIrlapCb, &pIrlapCb->PollTimer);
  1873. pIrlapCb->State = P_XMIT;
  1874. break;
  1875. case P_DISCONNECT_PEND:
  1876. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  1877. SendDISC(pIrlapCb);
  1878. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  1879. pIrlapCb->RetryCnt = 0;
  1880. GotoPCloseState(pIrlapCb);
  1881. break;
  1882. case P_CLOSE:
  1883. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  1884. ApplyDefaultParms(pIrlapCb);
  1885. if (pIrlapCb->LocalDiscReq == TRUE)
  1886. {
  1887. pIrlapCb->LocalDiscReq = FALSE;
  1888. IMsg.Prim = IRLAP_DISCONNECT_IND;
  1889. IMsg.IRDA_MSG_DiscStatus = IRLAP_DISCONNECT_COMPLETED;
  1890. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  1891. }
  1892. GotoNDMThenDscvOrConn(pIrlapCb);
  1893. break;
  1894. case S_NRM:
  1895. case S_DISCONNECT_PEND:
  1896. case S_ERROR:
  1897. case S_CLOSE:
  1898. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  1899. StationConflict(pIrlapCb);
  1900. pIrlapCb->State = NDM;
  1901. break;
  1902. default:
  1903. IRLAP_LOG_ACTION((pIrlapCb, TEXT("UA ignored in this state")));
  1904. }
  1905. }
  1906. UCHAR *
  1907. GetPv(UCHAR *pQosUCHAR,
  1908. UINT *pBitField)
  1909. {
  1910. int Pl = (int) *pQosUCHAR++;
  1911. *pBitField = 0;
  1912. if (Pl == 1)
  1913. {
  1914. *pBitField = (UINT) *pQosUCHAR;
  1915. }
  1916. else
  1917. {
  1918. *pBitField = ((UINT) *(pQosUCHAR+1))<<8;
  1919. *pBitField |= (UINT) *(pQosUCHAR);
  1920. }
  1921. return pQosUCHAR + Pl;
  1922. }
  1923. /*****************************************************************************
  1924. *
  1925. * THIS WILL BREAK IF PARAMETER LENGTH (PL) IS GREATER THAN 2
  1926. */
  1927. void
  1928. ExtractQosParms(IRDA_QOS_PARMS *pQos,
  1929. UCHAR *pQosUCHAR,
  1930. UCHAR *pEndQosUCHAR)
  1931. {
  1932. PAGED_CODE();
  1933. while (pQosUCHAR + 2 < pEndQosUCHAR)
  1934. {
  1935. switch (*pQosUCHAR)
  1936. {
  1937. case QOS_PI_BAUD:
  1938. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfBaud);
  1939. break;
  1940. case QOS_PI_MAX_TAT:
  1941. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfMaxTurnTime);
  1942. break;
  1943. case QOS_PI_DATA_SZ:
  1944. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfDataSize);
  1945. break;
  1946. case QOS_PI_WIN_SZ:
  1947. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfWindowSize);
  1948. break;
  1949. case QOS_PI_BOFS:
  1950. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfBofs);
  1951. break;
  1952. case QOS_PI_MIN_TAT:
  1953. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfMinTurnTime);
  1954. break;
  1955. case QOS_PI_DISC_THRESH:
  1956. pQosUCHAR = GetPv(pQosUCHAR+1, &pQos->bfDisconnectTime);
  1957. break;
  1958. default:
  1959. DEBUGMSG(1, (TEXT("IRLAP: Invalid Qos parameter type %X\n"), *pQosUCHAR));
  1960. ASSERT(0);
  1961. pQosUCHAR = pEndQosUCHAR;
  1962. }
  1963. }
  1964. }
  1965. /*****************************************************************************
  1966. *
  1967. * @func UINT | NegotiateQosParms | Take the received Qos build
  1968. * negotiated Qos.
  1969. *
  1970. * @rdesc SUCCESS, otherwise one of the folowing:
  1971. * @flag IRLAP_BAUD_NEG_ERR | Failed to negotiate baud
  1972. * @flag IRLAP_DISC_NEG_ERR | Failed to negotiate disconnect time
  1973. * @flag IRLAP_MAXTAT_NEG_ERR | Failed to negotiate max turn time
  1974. * @flag IRLAP_DATASIZE_NEG_ERR | Failed to negotiate data size
  1975. * @flag IRLAP_WINSIZE_NEG_ERR | Failed to negotiate window size
  1976. * @flag IRLAP_BOFS_NEG_ERR | Failed to negotiate number of BOFS
  1977. * @flag IRLAP_WINSIZE_NEG_ERR | Failed to window size
  1978. * @flag IRLAP_LINECAP_ERR | Failed to determine valid line capacity
  1979. *
  1980. * @parm IRDA_QOS_PARMS * | pRemoteQos | Pointer to QOS parm struct
  1981. */
  1982. UINT
  1983. NegotiateQosParms(PIRLAP_CB pIrlapCb,
  1984. IRDA_QOS_PARMS *pRemoteQos)
  1985. {
  1986. UINT BitSet;
  1987. BOOLEAN ParmSet = FALSE;
  1988. UINT BOFSDivisor = 1;
  1989. UINT MaxLineCap = 0;
  1990. UINT LineCapacity;
  1991. UINT DataSizeBit = 0;
  1992. UINT WinSizeBit = 0;
  1993. #ifdef GET_LARGEST_DATA_SIZE
  1994. UINT WSBit;
  1995. #else
  1996. UINT DataBit;
  1997. #endif
  1998. int RemoteDataSize = 0;
  1999. int RemoteWinSize = 0;
  2000. PAGED_CODE();
  2001. // Baud rate is Type 0 parm
  2002. pIrlapCb->Baud = IrlapGetQosParmVal(vBaudTable,
  2003. pIrlapCb->LocalQos.bfBaud & pRemoteQos->bfBaud,
  2004. &BitSet);
  2005. BOFSDivisor = IrlapGetQosParmVal(vBOFSDivTable,
  2006. pIrlapCb->LocalQos.bfBaud & pRemoteQos->bfBaud,
  2007. &BitSet);
  2008. pIrlapCb->NegotiatedQos.bfBaud = BitSet;
  2009. if (-1 == pIrlapCb->Baud)
  2010. {
  2011. return IRLAP_BAUD_NEG_ERR;
  2012. }
  2013. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Negotiated Baud:%d"), pIrlapCb->Baud));
  2014. // Disconnect/Threshold time is Type 0 parm
  2015. pIrlapCb->DisconnectTime = IrlapGetQosParmVal(vDiscTable,
  2016. pIrlapCb->LocalQos.bfDisconnectTime & pRemoteQos->bfDisconnectTime,
  2017. &BitSet);
  2018. pIrlapCb->ThresholdTime = IrlapGetQosParmVal(vThreshTable,
  2019. pIrlapCb->LocalQos.bfDisconnectTime & pRemoteQos->bfDisconnectTime,
  2020. &BitSet);
  2021. pIrlapCb->NegotiatedQos.bfDisconnectTime = BitSet;
  2022. if (-1 == pIrlapCb->DisconnectTime)
  2023. {
  2024. return IRLAP_DISC_NEG_ERR;
  2025. }
  2026. IRLAP_LOG_ACTION((pIrlapCb,
  2027. TEXT("Negotiated Disconnect/Threshold time:%d/%d"),
  2028. pIrlapCb->DisconnectTime, pIrlapCb->ThresholdTime));
  2029. pIrlapCb->RemoteMaxTAT = IrlapGetQosParmVal(vMaxTATTable,
  2030. pRemoteQos->bfMaxTurnTime,
  2031. &BitSet);
  2032. pIrlapCb->NegotiatedQos.bfMaxTurnTime = BitSet;
  2033. if (-1 == pIrlapCb->RemoteMaxTAT)
  2034. {
  2035. return IRLAP_MAXTAT_NEG_ERR;
  2036. }
  2037. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Remote max turnaround time:%d"),
  2038. pIrlapCb->RemoteMaxTAT));
  2039. pIrlapCb->RemoteMinTAT = IrlapGetQosParmVal(vMinTATTable,
  2040. pRemoteQos->bfMinTurnTime,
  2041. &BitSet);
  2042. pIrlapCb->NegotiatedQos.bfMinTurnTime = BitSet;
  2043. if (-1 == pIrlapCb->RemoteMinTAT)
  2044. {
  2045. return IRLAP_MINTAT_NEG_ERR;
  2046. }
  2047. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Remote min turnaround time:%d"),
  2048. pIrlapCb->RemoteMinTAT));
  2049. // DataSize ISNOT A TYPE 0 PARAMETER. BUT WIN95's IRCOMM implementation
  2050. // ASSUMES THAT IT IS. SO FOR NOW, NEGOTIATE IT. grrrr..
  2051. /* WIN95 out
  2052. pIrlapCb->RemoteDataSize = IrlapGetQosParmVal(vDataSizeTable,
  2053. (UCHAR) (pIrlapCb->LocalQos.bfDataSize &
  2054. pRemoteQos->bfDataSize), &BitSet);
  2055. */
  2056. pIrlapCb->RemoteDataSize = IrlapGetQosParmVal(vDataSizeTable,
  2057. pRemoteQos->bfDataSize, &BitSet);
  2058. DataSizeBit = BitSet;
  2059. pIrlapCb->NegotiatedQos.bfDataSize = BitSet;
  2060. if (-1 == pIrlapCb->RemoteDataSize)
  2061. {
  2062. return IRLAP_DATASIZE_NEG_ERR;
  2063. }
  2064. IRLAP_LOG_ACTION((pIrlapCb,
  2065. TEXT("Remote data size:%d"), pIrlapCb->RemoteDataSize));
  2066. pIrlapCb->RemoteWinSize = IrlapGetQosParmVal(vWinSizeTable,
  2067. pRemoteQos->bfWindowSize, &BitSet);
  2068. WinSizeBit = BitSet;
  2069. pIrlapCb->NegotiatedQos.bfWindowSize = BitSet;
  2070. if (-1 == pIrlapCb->RemoteWinSize)
  2071. {
  2072. return IRLAP_WINSIZE_NEG_ERR;
  2073. }
  2074. IRLAP_LOG_ACTION((pIrlapCb,
  2075. TEXT("Remote window size:%d"),
  2076. pIrlapCb->RemoteWinSize));
  2077. pIrlapCb->RemoteNumBOFS=(IrlapGetQosParmVal(vBOFSTable,
  2078. pRemoteQos->bfBofs, &BitSet)
  2079. / BOFSDivisor)+1;
  2080. pIrlapCb->NegotiatedQos.bfBofs = BitSet;
  2081. if (-1 == pIrlapCb->RemoteNumBOFS)
  2082. {
  2083. return IRLAP_BOFS_NEG_ERR;
  2084. }
  2085. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Remote number of BOFS:%d"),
  2086. pIrlapCb->RemoteNumBOFS));
  2087. // The maximum line capacity is in UCHARs and comes from a table in spec.
  2088. // (can't calc because table isn't linear). It is determined by the
  2089. // maximum line capacity and baud rate.
  2090. //
  2091. // Later note: Errata corrected table so values could be calculated.
  2092. // Could get rid of tables
  2093. switch (pIrlapCb->Baud)
  2094. {
  2095. case 9600:
  2096. MaxLineCap = IrlapGetQosParmVal(MAXCAP_9600,
  2097. pRemoteQos->bfMaxTurnTime, &BitSet);
  2098. break;
  2099. case 19200:
  2100. MaxLineCap = IrlapGetQosParmVal(MAXCAP_19200,
  2101. pRemoteQos->bfMaxTurnTime, &BitSet);
  2102. break;
  2103. case 38400:
  2104. MaxLineCap = IrlapGetQosParmVal(MAXCAP_38400,
  2105. pRemoteQos->bfMaxTurnTime, &BitSet);
  2106. break;
  2107. case 57600:
  2108. MaxLineCap = IrlapGetQosParmVal(MAXCAP_57600,
  2109. pRemoteQos->bfMaxTurnTime, &BitSet);
  2110. break;
  2111. case 115200:
  2112. MaxLineCap = IrlapGetQosParmVal(MAXCAP_115200,
  2113. pRemoteQos->bfMaxTurnTime, &BitSet);
  2114. break;
  2115. case 576000:
  2116. MaxLineCap = IrlapGetQosParmVal(MAXCAP_576000,
  2117. pRemoteQos->bfMaxTurnTime, &BitSet);
  2118. break;
  2119. case 1152000:
  2120. MaxLineCap = IrlapGetQosParmVal(MAXCAP_1152000,
  2121. pRemoteQos->bfMaxTurnTime, &BitSet);
  2122. break;
  2123. case 4000000:
  2124. MaxLineCap = IrlapGetQosParmVal(MAXCAP_4000000,
  2125. pRemoteQos->bfMaxTurnTime, &BitSet);
  2126. break;
  2127. case 16000000:
  2128. MaxLineCap = IrlapGetQosParmVal(MAXCAP_16000000,
  2129. pRemoteQos->bfMaxTurnTime, &BitSet);
  2130. break;
  2131. default:
  2132. ASSERT(0);
  2133. }
  2134. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Maximum line capacity:%d"), MaxLineCap));
  2135. LineCapacity = LINE_CAPACITY(pIrlapCb);
  2136. IRLAP_LOG_ACTION((pIrlapCb,
  2137. TEXT("Requested line capacity:%d"), LineCapacity));
  2138. if (LineCapacity > MaxLineCap)
  2139. {
  2140. ParmSet = FALSE;
  2141. // Adjust data and window size to fit within the line capacity.
  2142. #ifdef GET_LARGEST_DATA_SIZE
  2143. // Get largest possible datasize
  2144. for (; DataSizeBit != 0 && !ParmSet; DataSizeBit >>= 1)
  2145. {
  2146. pIrlapCb->RemoteDataSize = IrlapGetQosParmVal(vDataSizeTable,
  2147. DataSizeBit, NULL);
  2148. // Start with smallest window
  2149. for (WSBit=1; WSBit <= WinSizeBit; WSBit <<=1)
  2150. {
  2151. pIrlapCb->RemoteWinSize = IrlapGetQosParmVal(vWinSizeTable,
  2152. WSBit, NULL);
  2153. LineCapacity = LINE_CAPACITY(pIrlapCb);
  2154. IRLAP_LOG_ACTION((pIrlapCb,
  2155. TEXT("adjusted data size=%d, window size= %d, line cap=%d"),
  2156. pIrlapCb->RemoteDataSize, pIrlapCb->RemoteWinSize,
  2157. LineCapacity));
  2158. if (LineCapacity > MaxLineCap)
  2159. {
  2160. // Get a smaller data size (only if ParmSet is false)
  2161. break;
  2162. }
  2163. ParmSet = TRUE;
  2164. // Save the last good one,then loop and try a larger window
  2165. RemoteDataSize = pIrlapCb->RemoteDataSize;
  2166. RemoteWinSize = pIrlapCb->RemoteWinSize;
  2167. pIrlapCb->NegotiatedQos.bfWindowSize = WSBit;
  2168. pIrlapCb->NegotiatedQos.bfDataSize = DataSizeBit;
  2169. }
  2170. }
  2171. #else
  2172. // Get largest possible windowsize
  2173. for (; WinSizeBit != 0 && !ParmSet; WinSizeBit >>= 1)
  2174. {
  2175. pIrlapCb->RemoteWinSize = IrlapGetQosParmVal(vWinSizeTable,
  2176. WinSizeBit, NULL);
  2177. // Start with smallest datasize
  2178. for (DataBit=1; DataBit <= DataSizeBit; DataBit <<=1)
  2179. {
  2180. pIrlapCb->RemoteDataSize = IrlapGetQosParmVal(vDataSizeTable,
  2181. DataBit, NULL);
  2182. LineCapacity = LINE_CAPACITY(pIrlapCb);
  2183. IRLAP_LOG_ACTION((pIrlapCb,
  2184. TEXT("adjusted data size=%d, window size= %d, line cap=%d"),
  2185. pIrlapCb->RemoteDataSize, pIrlapCb->RemoteWinSize,
  2186. LineCapacity));
  2187. if (LineCapacity > MaxLineCap)
  2188. {
  2189. // Get a smaller window size (only if ParmSet is false)
  2190. break;
  2191. }
  2192. ParmSet = TRUE;
  2193. // Save the last good one,then loop and try a larger Data size
  2194. RemoteWinSize = pIrlapCb->RemoteWinSize;
  2195. RemoteDataSize = pIrlapCb->RemoteDataSize;
  2196. pIrlapCb->NegotiatedQos.bfDataSize = DataBit;
  2197. pIrlapCb->NegotiatedQos.bfWindowSize = WinSizeBit;
  2198. }
  2199. }
  2200. #endif
  2201. if (!ParmSet)
  2202. {
  2203. return IRLAP_LINECAP_ERR;
  2204. }
  2205. pIrlapCb->RemoteDataSize = RemoteDataSize;
  2206. pIrlapCb->RemoteWinSize = RemoteWinSize;
  2207. IRLAP_LOG_ACTION((pIrlapCb,
  2208. TEXT("final data size=%d, window size= %d, line cap=%d"),
  2209. pIrlapCb->RemoteDataSize, pIrlapCb->RemoteWinSize,
  2210. LINE_CAPACITY(pIrlapCb)));
  2211. }
  2212. return SUCCESS;
  2213. }
  2214. /*****************************************************************************
  2215. *
  2216. *
  2217. */
  2218. VOID
  2219. ApplyQosParms(PIRLAP_CB pIrlapCb)
  2220. {
  2221. IRDA_MSG IMsg;
  2222. PAGED_CODE();
  2223. pIrlapCb->PollTimer.Timeout = pIrlapCb->RemoteMaxTAT;
  2224. pIrlapCb->FinalTimer.Timeout = pIrlapCb->LocalMaxTAT;
  2225. if (pIrlapCb->Baud <= 115200)
  2226. {
  2227. pIrlapCb->FinalTimer.Timeout += 150; // fudge factor for SIR
  2228. }
  2229. // convert disconnect/threshold time to ms and divide by final timer
  2230. // to get number of retries
  2231. pIrlapCb->N1 = pIrlapCb->ThresholdTime * 1000 / pIrlapCb->FinalTimer.Timeout;
  2232. pIrlapCb->N2 = pIrlapCb->DisconnectTime * 1000 / pIrlapCb->FinalTimer.Timeout;
  2233. IMsg.Prim = MAC_CONTROL_REQ;
  2234. IMsg.IRDA_MSG_Op = MAC_RECONFIG_LINK;
  2235. IMsg.IRDA_MSG_Baud = pIrlapCb->Baud;
  2236. IMsg.IRDA_MSG_NumBOFs = pIrlapCb->RemoteNumBOFS; // Number of BOFS
  2237. // to add to tx
  2238. IMsg.IRDA_MSG_DataSize = pIrlapCb->RemoteDataSize; // Max rx size packet
  2239. // causes major heap
  2240. // problems later
  2241. IMsg.IRDA_MSG_MinTat = pIrlapCb->RemoteMinTAT;
  2242. IRLAP_LOG_ACTION((pIrlapCb,
  2243. TEXT("Reconfig link for Baud:%d, Remote BOFS:%d"),
  2244. pIrlapCb->Baud, pIrlapCb->RemoteNumBOFS));
  2245. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Retry counts N1=%d, N2=%d"),
  2246. pIrlapCb->N1, pIrlapCb->N2));
  2247. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  2248. /*
  2249. if (pIrlapCb->MonitorLink)
  2250. {
  2251. IrdaTimerStart(&pIrlapCb->StatusTimer);
  2252. }
  2253. */
  2254. }
  2255. /*****************************************************************************
  2256. *
  2257. * @func UINT | IrlapGetQosParmVal |
  2258. * retrieves the parameters value from table
  2259. *
  2260. * @rdesc value contained in parmeter value table, 0 if not found
  2261. * (0 is a valid parameter in some tables though)
  2262. *
  2263. * @parm UINT [] | PVTable | table containing parm values
  2264. * USHORT | BitField | contains bit indicating which parm to select
  2265. *
  2266. * @comm
  2267. */
  2268. UINT
  2269. IrlapGetQosParmVal(UINT PVTable[], UINT BitField, UINT *pBitSet)
  2270. {
  2271. int i;
  2272. UINT Mask;
  2273. for (i = PV_TABLE_MAX_BIT, Mask = (1<<PV_TABLE_MAX_BIT);
  2274. Mask > 0; i--, Mask = Mask >> 1)
  2275. {
  2276. if (Mask & BitField)
  2277. {
  2278. if (pBitSet != NULL)
  2279. {
  2280. *pBitSet = Mask;
  2281. }
  2282. return (PVTable[i]);
  2283. }
  2284. }
  2285. *pBitSet = 0;
  2286. return (UINT) -1;
  2287. }
  2288. /*****************************************************************************
  2289. *
  2290. */
  2291. VOID
  2292. ProcessTEST(PIRLAP_CB pIrlapCb,
  2293. PIRDA_MSG pMsg,
  2294. IRLAP_UA_FORMAT *pTestFormat,
  2295. int CRBit,
  2296. int PFBit)
  2297. {
  2298. PAGED_CODE();
  2299. if (IRLAP_CMD == CRBit && IRLAP_PFBIT_SET == PFBit)
  2300. {
  2301. // bounce it back
  2302. *(pMsg->IRDA_MSG_pRead) ^= 1; // swap cr bit
  2303. // copy the header to the header portion for an outbound message
  2304. pMsg->IRDA_MSG_pHdrRead = pMsg->IRDA_MSG_Header;
  2305. *pMsg->IRDA_MSG_pHdrRead = *pMsg->IRDA_MSG_pRead;
  2306. *(pMsg->IRDA_MSG_pHdrRead+1) = *(pMsg->IRDA_MSG_pRead+1);
  2307. pMsg->IRDA_MSG_pHdrWrite = pMsg->IRDA_MSG_Header + 2;
  2308. pMsg->IRDA_MSG_pRead+=2;
  2309. pMsg->Prim = MAC_DATA_REQ;
  2310. IrmacDown(pIrlapCb->pIrdaLinkCb, pMsg);
  2311. }
  2312. else
  2313. {
  2314. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring")));
  2315. }
  2316. // Not implementing TEST responses for now
  2317. }
  2318. /*****************************************************************************
  2319. *
  2320. */
  2321. VOID
  2322. ProcessUI(PIRLAP_CB pIrlapCb,
  2323. PIRDA_MSG pMsg,
  2324. int CRBit,
  2325. int PFBit)
  2326. {
  2327. BOOLEAN LinkTurned = TRUE;
  2328. PAGED_CODE();
  2329. pMsg->IRDA_MSG_pRead += 2; // chop the IRLAP header
  2330. switch (pIrlapCb->State)
  2331. {
  2332. case NDM:
  2333. case DSCV_MEDIA_SENSE:
  2334. case DSCV_QUERY:
  2335. case DSCV_REPLY:
  2336. case CONN_MEDIA_SENSE:
  2337. case SNRM_SENT:
  2338. case BACKOFF_WAIT:
  2339. case SNRM_RECEIVED:
  2340. pMsg->Prim = IRLAP_UDATA_IND;
  2341. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  2342. return;
  2343. case P_XMIT:
  2344. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  2345. return;
  2346. }
  2347. if (PRIMARY == pIrlapCb->StationType)
  2348. {
  2349. // stop timers if PF bit set or invalid CRBit (matches mine)
  2350. if (IRLAP_PFBIT_SET == PFBit || pIrlapCb->CRBit == CRBit)
  2351. {
  2352. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2353. }
  2354. }
  2355. else
  2356. {
  2357. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2358. }
  2359. if (pIrlapCb->CRBit == CRBit)
  2360. {
  2361. StationConflict(pIrlapCb);
  2362. pIrlapCb->State = NDM;
  2363. return;
  2364. }
  2365. // Send the Unnumber information to LMP
  2366. pMsg->Prim = IRLAP_UDATA_IND;
  2367. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  2368. if (IRLAP_PFBIT_SET == PFBit)
  2369. {
  2370. switch (pIrlapCb->State)
  2371. {
  2372. case P_RECV:
  2373. XmitTxMsgList(pIrlapCb, FALSE, &LinkTurned);
  2374. break;
  2375. case P_DISCONNECT_PEND:
  2376. SendDISC(pIrlapCb);
  2377. pIrlapCb->RetryCnt = 0;
  2378. GotoPCloseState(pIrlapCb);
  2379. break;
  2380. case P_CLOSE:
  2381. ResendDISC(pIrlapCb);
  2382. break;
  2383. case S_NRM:
  2384. XmitTxMsgList(pIrlapCb, TRUE, NULL);
  2385. break;
  2386. case S_DISCONNECT_PEND:
  2387. SendRD(pIrlapCb);
  2388. pIrlapCb->State = S_CLOSE;
  2389. break;
  2390. case S_ERROR:
  2391. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  2392. pIrlapCb->State = S_NRM;
  2393. break;
  2394. case S_CLOSE:
  2395. SendRD(pIrlapCb);
  2396. }
  2397. }
  2398. if (PRIMARY == pIrlapCb->StationType)
  2399. {
  2400. if (IRLAP_PFBIT_SET == PFBit && pIrlapCb->State != NDM)
  2401. {
  2402. if (LinkTurned)
  2403. {
  2404. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2405. }
  2406. else
  2407. {
  2408. IrlapTimerStart(pIrlapCb, &pIrlapCb->PollTimer);
  2409. pIrlapCb->State = P_XMIT;
  2410. }
  2411. }
  2412. }
  2413. else
  2414. {
  2415. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  2416. }
  2417. return;
  2418. }
  2419. /*****************************************************************************
  2420. *
  2421. */
  2422. VOID
  2423. ProcessDM(PIRLAP_CB pIrlapCb)
  2424. {
  2425. IRDA_MSG IMsg;
  2426. BOOLEAN LinkTurned;
  2427. PAGED_CODE();
  2428. switch (pIrlapCb->State)
  2429. {
  2430. case NDM:
  2431. case DSCV_MEDIA_SENSE:
  2432. case DSCV_QUERY:
  2433. case DSCV_REPLY:
  2434. case CONN_MEDIA_SENSE:
  2435. case BACKOFF_WAIT:
  2436. case SNRM_RECEIVED:
  2437. case P_XMIT:
  2438. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  2439. return;
  2440. }
  2441. if (PRIMARY != pIrlapCb->StationType)
  2442. {
  2443. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2444. StationConflict(pIrlapCb);
  2445. pIrlapCb->State = NDM;
  2446. return;
  2447. }
  2448. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2449. switch (pIrlapCb->State)
  2450. {
  2451. case P_RECV: // I'm not sure why I am doing this ???
  2452. XmitTxMsgList(pIrlapCb, FALSE, &LinkTurned);
  2453. if (LinkTurned)
  2454. {
  2455. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2456. }
  2457. else
  2458. {
  2459. IrlapTimerStart(pIrlapCb, &pIrlapCb->PollTimer);
  2460. pIrlapCb->State = P_XMIT;
  2461. }
  2462. break;
  2463. case P_DISCONNECT_PEND:
  2464. pIrlapCb->RetryCnt = 0;
  2465. SendDISC(pIrlapCb);
  2466. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2467. GotoPCloseState(pIrlapCb);
  2468. break;
  2469. case SNRM_SENT:
  2470. ApplyDefaultParms(pIrlapCb);
  2471. pIrlapCb->State = NDM;
  2472. if (pIrlapCb->LocalDiscReq || pIrlapCb->State == SNRM_SENT)
  2473. {
  2474. IMsg.Prim = IRLAP_DISCONNECT_IND;
  2475. IMsg.IRDA_MSG_DiscStatus = IRLAP_REMOTE_INITIATED;
  2476. pIrlapCb->LocalDiscReq = FALSE;
  2477. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  2478. }
  2479. break;
  2480. case P_CLOSE:
  2481. pIrlapCb->State = NDM;
  2482. ApplyDefaultParms(pIrlapCb);
  2483. if (pIrlapCb->LocalDiscReq || pIrlapCb->State == SNRM_SENT)
  2484. {
  2485. IMsg.Prim = IRLAP_DISCONNECT_IND;
  2486. IMsg.IRDA_MSG_DiscStatus = IRLAP_DISCONNECT_COMPLETED;
  2487. pIrlapCb->LocalDiscReq = FALSE;
  2488. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  2489. }
  2490. GotoNDMThenDscvOrConn(pIrlapCb);
  2491. break;
  2492. }
  2493. }
  2494. /*****************************************************************************
  2495. *
  2496. *
  2497. */
  2498. VOID
  2499. ProcessDISC(PIRLAP_CB pIrlapCb)
  2500. {
  2501. IRDA_MSG IMsg;
  2502. if (IgnoreState(pIrlapCb))
  2503. {
  2504. return;
  2505. }
  2506. if (SECONDARY != pIrlapCb->StationType)
  2507. {
  2508. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2509. StationConflict(pIrlapCb);
  2510. pIrlapCb->State = NDM;
  2511. return;
  2512. }
  2513. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2514. // Acknowledge primary's disconnect request
  2515. SendUA(pIrlapCb, FALSE /* No Qos */);
  2516. // notify LMP of disconnect
  2517. IMsg.Prim = IRLAP_DISCONNECT_IND;
  2518. if (pIrlapCb->LocalDiscReq)
  2519. {
  2520. IMsg.IRDA_MSG_DiscStatus = IRLAP_DISCONNECT_COMPLETED;
  2521. pIrlapCb->LocalDiscReq = FALSE;
  2522. }
  2523. else
  2524. {
  2525. IMsg.IRDA_MSG_DiscStatus = IRLAP_REMOTE_INITIATED;
  2526. }
  2527. pIrlapCb->State = NDM;
  2528. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  2529. ReturnRxTxWinMsgs(pIrlapCb);
  2530. ApplyDefaultParms(pIrlapCb);
  2531. return;
  2532. }
  2533. /*****************************************************************************
  2534. *
  2535. * @func ret_type | func_name | funcdesc
  2536. *
  2537. * @rdesc SUCCESS, otherwise one of the following errors:
  2538. * @flag val | desc
  2539. *
  2540. * @parm data_type | parm_name | description
  2541. *
  2542. * @comm
  2543. * comments
  2544. *
  2545. * @ex
  2546. * example
  2547. */
  2548. VOID
  2549. ProcessRD(PIRLAP_CB pIrlapCb)
  2550. {
  2551. PAGED_CODE();
  2552. if (IgnoreState(pIrlapCb))
  2553. {
  2554. return;
  2555. }
  2556. if (PRIMARY != pIrlapCb->StationType)
  2557. {
  2558. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2559. StationConflict(pIrlapCb);
  2560. pIrlapCb->State = NDM;
  2561. return;
  2562. }
  2563. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2564. if (pIrlapCb->State == P_CLOSE)
  2565. {
  2566. ResendDISC(pIrlapCb);
  2567. }
  2568. else
  2569. {
  2570. ReturnRxTxWinMsgs(pIrlapCb);
  2571. pIrlapCb->RetryCnt = 0;
  2572. SendDISC(pIrlapCb);
  2573. GotoPCloseState(pIrlapCb);
  2574. }
  2575. if (pIrlapCb->State != NDM)
  2576. {
  2577. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2578. }
  2579. }
  2580. /*****************************************************************************
  2581. *
  2582. */
  2583. VOID
  2584. ProcessFRMR(PIRLAP_CB pIrlapCb)
  2585. {
  2586. PAGED_CODE();
  2587. if (IgnoreState(pIrlapCb))
  2588. {
  2589. return;
  2590. }
  2591. if (PRIMARY != pIrlapCb->StationType)
  2592. {
  2593. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2594. StationConflict(pIrlapCb);
  2595. pIrlapCb->State = NDM;
  2596. return;
  2597. }
  2598. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2599. switch (pIrlapCb->State)
  2600. {
  2601. case P_RECV:
  2602. ReturnRxTxWinMsgs(pIrlapCb);
  2603. // fall through
  2604. case P_DISCONNECT_PEND:
  2605. pIrlapCb->RetryCnt = 0;
  2606. SendDISC(pIrlapCb);
  2607. GotoPCloseState(pIrlapCb);
  2608. break;
  2609. case P_CLOSE:
  2610. ResendDISC(pIrlapCb);
  2611. break;
  2612. }
  2613. if (pIrlapCb->State != NDM)
  2614. {
  2615. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2616. }
  2617. }
  2618. /*****************************************************************************
  2619. *
  2620. */
  2621. VOID
  2622. ProcessRNRM(PIRLAP_CB pIrlapCb)
  2623. {
  2624. PAGED_CODE();
  2625. if (IgnoreState(pIrlapCb))
  2626. {
  2627. return;
  2628. }
  2629. if (PRIMARY != pIrlapCb->StationType)
  2630. {
  2631. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2632. StationConflict(pIrlapCb);
  2633. pIrlapCb->State = NDM;
  2634. return;
  2635. }
  2636. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2637. switch (pIrlapCb->State)
  2638. {
  2639. case P_RECV:
  2640. case P_DISCONNECT_PEND:
  2641. pIrlapCb->RetryCnt = 0;
  2642. SendDISC(pIrlapCb);
  2643. GotoPCloseState(pIrlapCb);
  2644. break;
  2645. case P_CLOSE:
  2646. ResendDISC(pIrlapCb);
  2647. break;
  2648. }
  2649. if (pIrlapCb->State != NDM)
  2650. {
  2651. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2652. }
  2653. return;
  2654. }
  2655. /*****************************************************************************
  2656. *
  2657. */
  2658. VOID
  2659. ProcessREJ_SREJ(PIRLAP_CB pIrlapCb,
  2660. int FrameType,
  2661. PIRDA_MSG pMsg,
  2662. int CRBit,
  2663. int PFBit,
  2664. UINT Nr)
  2665. {
  2666. if (IgnoreState(pIrlapCb))
  2667. {
  2668. return;
  2669. }
  2670. if (PRIMARY == pIrlapCb->StationType)
  2671. {
  2672. // stop timers if PF bit set or invalid CRBit (matches mine)
  2673. if (IRLAP_PFBIT_SET == PFBit || pIrlapCb->CRBit == CRBit)
  2674. {
  2675. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2676. }
  2677. }
  2678. else
  2679. {
  2680. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2681. }
  2682. if (pIrlapCb->CRBit == CRBit)
  2683. {
  2684. StationConflict(pIrlapCb);
  2685. pIrlapCb->State = NDM;
  2686. return;
  2687. }
  2688. switch (pIrlapCb->State)
  2689. {
  2690. case P_RECV:
  2691. case S_NRM:
  2692. if (IRLAP_PFBIT_SET == PFBit)
  2693. {
  2694. if (InvalidNr(pIrlapCb,Nr) || Nr == pIrlapCb->TxWin.End)
  2695. {
  2696. ProcessInvalidNr(pIrlapCb, PFBit);
  2697. }
  2698. else
  2699. {
  2700. ConfirmAckedTxMsgs(pIrlapCb, Nr);
  2701. if (FrameType == IRLAP_REJ)
  2702. {
  2703. ResendRejects(pIrlapCb, Nr); // link turned here
  2704. }
  2705. else // selective reject
  2706. {
  2707. IRLAP_LOG_ACTION((pIrlapCb, TEXT("RETRANSMISSION:")));
  2708. SendIFrame(pIrlapCb,
  2709. pIrlapCb->TxWin.pMsg[Nr],
  2710. Nr, IRLAP_PFBIT_SET);
  2711. }
  2712. }
  2713. }
  2714. break;
  2715. case P_DISCONNECT_PEND:
  2716. if (IRLAP_PFBIT_SET == PFBit)
  2717. {
  2718. pIrlapCb->RetryCnt = 0;
  2719. SendDISC(pIrlapCb);
  2720. GotoPCloseState(pIrlapCb);
  2721. }
  2722. break;
  2723. case P_CLOSE:
  2724. if (IRLAP_PFBIT_SET == PFBit)
  2725. {
  2726. ResendDISC(pIrlapCb);
  2727. }
  2728. break;
  2729. case S_DISCONNECT_PEND:
  2730. if (IRLAP_PFBIT_SET == PFBit)
  2731. {
  2732. SendRD(pIrlapCb);
  2733. pIrlapCb->State = S_CLOSE;
  2734. }
  2735. break;
  2736. case S_ERROR:
  2737. if (IRLAP_PFBIT_SET == PFBit)
  2738. {
  2739. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  2740. pIrlapCb->State = S_NRM;
  2741. }
  2742. break;
  2743. case S_CLOSE:
  2744. if (IRLAP_PFBIT_SET == PFBit)
  2745. {
  2746. SendRD(pIrlapCb);
  2747. }
  2748. break;
  2749. }
  2750. if (PRIMARY == pIrlapCb->StationType)
  2751. {
  2752. if (IRLAP_PFBIT_SET == PFBit && pIrlapCb->State != NDM)
  2753. {
  2754. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2755. }
  2756. }
  2757. else
  2758. {
  2759. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  2760. }
  2761. }
  2762. /*****************************************************************************
  2763. *
  2764. */
  2765. VOID
  2766. ProcessRR_RNR(PIRLAP_CB pIrlapCb,
  2767. int FrameType,
  2768. PIRDA_MSG pMsg,
  2769. int CRBit,
  2770. int PFBit,
  2771. UINT Nr)
  2772. {
  2773. BOOLEAN LinkTurned = TRUE;
  2774. if (IgnoreState(pIrlapCb))
  2775. {
  2776. return;
  2777. }
  2778. if (pIrlapCb->FastPollCount == 0)
  2779. {
  2780. pIrlapCb->PollTimer.Timeout = pIrlapCb->RemoteMaxTAT;
  2781. }
  2782. else
  2783. {
  2784. pIrlapCb->FastPollCount -= 1;
  2785. }
  2786. if (PRIMARY == pIrlapCb->StationType)
  2787. {
  2788. // stop timers if PF bit set or invalid CRBit (matches mine)
  2789. if (IRLAP_PFBIT_SET == PFBit || pIrlapCb->CRBit == CRBit)
  2790. {
  2791. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  2792. }
  2793. }
  2794. else // SECONDARY, restart WDog
  2795. {
  2796. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  2797. if (pIrlapCb->CRBit != CRBit)
  2798. {
  2799. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  2800. }
  2801. }
  2802. if (pIrlapCb->CRBit == CRBit)
  2803. {
  2804. StationConflict(pIrlapCb);
  2805. pIrlapCb->State = NDM;
  2806. return;
  2807. }
  2808. if (FrameType == IRLAP_RR)
  2809. {
  2810. pIrlapCb->RemoteBusy = FALSE;
  2811. }
  2812. else // RNR
  2813. {
  2814. pIrlapCb->RemoteBusy = TRUE;
  2815. }
  2816. switch (pIrlapCb->State)
  2817. {
  2818. case P_RECV:
  2819. case S_NRM:
  2820. if (PFBit == IRLAP_PFBIT_SET)
  2821. {
  2822. if (InvalidNr(pIrlapCb, Nr))
  2823. {
  2824. ProcessInvalidNr(pIrlapCb, PFBit);
  2825. }
  2826. else
  2827. {
  2828. ConfirmAckedTxMsgs(pIrlapCb,Nr);
  2829. if (Nr != pIrlapCb->Vs) // Implicit reject
  2830. {
  2831. if (PRIMARY == pIrlapCb->StationType &&
  2832. IRLAP_RNR == FrameType)
  2833. {
  2834. LinkTurned = FALSE;
  2835. }
  2836. else
  2837. {
  2838. ResendRejects(pIrlapCb, Nr); // always turns link
  2839. }
  2840. }
  2841. else
  2842. {
  2843. if (pIrlapCb->Vr != pIrlapCb->RxWin.End)
  2844. {
  2845. MissingRxFrames(pIrlapCb); // Send SREJ or REJ
  2846. }
  2847. else
  2848. {
  2849. if (PRIMARY == pIrlapCb->StationType)
  2850. {
  2851. LinkTurned = FALSE;
  2852. if (IRLAP_RR == FrameType)
  2853. {
  2854. XmitTxMsgList(pIrlapCb, FALSE, &LinkTurned);
  2855. }
  2856. }
  2857. else
  2858. {
  2859. // Always turn link if secondary
  2860. // with data or an RR if remote is busy
  2861. if (IRLAP_RR == FrameType)
  2862. {
  2863. XmitTxMsgList(pIrlapCb, TRUE, NULL);
  2864. }
  2865. else
  2866. {
  2867. SendRR_RNR(pIrlapCb);
  2868. }
  2869. }
  2870. }
  2871. }
  2872. }
  2873. // If the link was turned, restart Final timer,
  2874. // else start the Poll timer and enter the transmit state
  2875. if (PRIMARY == pIrlapCb->StationType)
  2876. {
  2877. if (LinkTurned)
  2878. {
  2879. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2880. }
  2881. else
  2882. {
  2883. IrlapTimerStart(pIrlapCb, &pIrlapCb->PollTimer);
  2884. pIrlapCb->State = P_XMIT;
  2885. }
  2886. }
  2887. }
  2888. break;
  2889. case P_DISCONNECT_PEND:
  2890. SendDISC(pIrlapCb);
  2891. pIrlapCb->RetryCnt = 0;
  2892. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2893. GotoPCloseState(pIrlapCb);
  2894. break;
  2895. case P_CLOSE:
  2896. ResendDISC(pIrlapCb);
  2897. if (pIrlapCb->State != NDM)
  2898. {
  2899. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  2900. }
  2901. break;
  2902. case S_DISCONNECT_PEND:
  2903. case S_CLOSE:
  2904. if (IRLAP_PFBIT_SET == PFBit)
  2905. {
  2906. SendRD(pIrlapCb);
  2907. if (pIrlapCb->State != S_CLOSE)
  2908. pIrlapCb->State = S_CLOSE;
  2909. }
  2910. break;
  2911. case S_ERROR:
  2912. if (IRLAP_PFBIT_SET == PFBit)
  2913. {
  2914. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  2915. pIrlapCb->State = S_NRM;
  2916. }
  2917. break;
  2918. default:
  2919. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  2920. }
  2921. }
  2922. /*****************************************************************************
  2923. *
  2924. */
  2925. VOID
  2926. ProcessInvalidNr(PIRLAP_CB pIrlapCb,
  2927. int PFBit)
  2928. {
  2929. DEBUGMSG(DBG_ERROR, (TEXT("IRLAP: ERROR, Invalid Nr\r\n")));
  2930. ReturnRxTxWinMsgs(pIrlapCb);
  2931. if (PRIMARY == pIrlapCb->StationType)
  2932. {
  2933. if (PFBit == IRLAP_PFBIT_SET)
  2934. {
  2935. SendDISC(pIrlapCb);
  2936. pIrlapCb->RetryCnt = 0;
  2937. // F-timer will be started by caller
  2938. GotoPCloseState(pIrlapCb);
  2939. }
  2940. else
  2941. {
  2942. pIrlapCb->State = P_DISCONNECT_PEND;
  2943. }
  2944. }
  2945. else // SECONDARY
  2946. {
  2947. if (PFBit == IRLAP_PFBIT_SET)
  2948. {
  2949. pIrlapCb->Frmr.Vs = (UCHAR) pIrlapCb->Vs;
  2950. pIrlapCb->Frmr.Vr = (UCHAR) pIrlapCb->Vr;
  2951. pIrlapCb->Frmr.W = 0;
  2952. pIrlapCb->Frmr.X = 0;
  2953. pIrlapCb->Frmr.Y = 0;
  2954. pIrlapCb->Frmr.Z = 1; // bad NR
  2955. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  2956. }
  2957. }
  2958. }
  2959. /*****************************************************************************
  2960. *
  2961. */
  2962. VOID
  2963. ProcessIFrame(PIRLAP_CB pIrlapCb,
  2964. PIRDA_MSG pMsg,
  2965. int CRBit,
  2966. int PFBit,
  2967. UINT Ns,
  2968. UINT Nr)
  2969. {
  2970. #if DBG_OUT
  2971. UCHAR *p1, *p2;
  2972. #endif
  2973. if ((pMsg->IRDA_MSG_pWrite - pMsg->IRDA_MSG_pRead) > IRDA_HEADER_LEN)
  2974. {
  2975. pIrlapCb->StatusFlags |= LF_RX;
  2976. }
  2977. pMsg->IRDA_MSG_pRead += IRLAP_HEADER_LEN; // chop the IRLAP header
  2978. #if DBG_CHECKSUM
  2979. // print first and last 4 bytes of frame to help isolate
  2980. // data corruption problem. Should be used with sledge
  2981. if ((pMsg->IRDA_MSG_pWrite - pMsg->IRDA_MSG_pRead) > 20)
  2982. DEBUGMSG(1, (TEXT("R(%X): %c%c%c%c, %c%c%c%c\n"),
  2983. pMsg->IRDA_MSG_pRead+3,
  2984. *(pMsg->IRDA_MSG_pRead+3),
  2985. *(pMsg->IRDA_MSG_pRead+4),
  2986. *(pMsg->IRDA_MSG_pRead+5),
  2987. *(pMsg->IRDA_MSG_pRead+6),
  2988. *(pMsg->IRDA_MSG_pWrite-4),
  2989. *(pMsg->IRDA_MSG_pWrite-3),
  2990. *(pMsg->IRDA_MSG_pWrite-2),
  2991. *(pMsg->IRDA_MSG_pWrite-1)));
  2992. #endif
  2993. switch (pIrlapCb->State)
  2994. {
  2995. case S_NRM:
  2996. case P_RECV:
  2997. // Stop Timers: if PFSet stop Final (I frame from secondary)
  2998. // Always stop WDog (I from primary)
  2999. if (PRIMARY == pIrlapCb->StationType)
  3000. {
  3001. if (PFBit == IRLAP_PFBIT_SET)
  3002. {
  3003. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  3004. }
  3005. }
  3006. else
  3007. {
  3008. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3009. }
  3010. if (pIrlapCb->CRBit == CRBit)
  3011. {
  3012. StationConflict(pIrlapCb);
  3013. pIrlapCb->State = NDM;
  3014. return;
  3015. }
  3016. if (InvalidNs(pIrlapCb, Ns))
  3017. {
  3018. DEBUGMSG(DBG_ERROR, (TEXT("IRLAP: ignoring invalid NS frame\n")));
  3019. }
  3020. else if (InvalidNr(pIrlapCb, Nr))
  3021. {
  3022. #if DBG_OUT
  3023. p1 = pMsg->IRDA_MSG_pRead - 2; // Get header back
  3024. p2 = pMsg->IRDA_MSG_pWrite + 2; // and FCS
  3025. while (p1 < p2)
  3026. DEBUGMSG(1, (TEXT("%02X "), *p1++));
  3027. DEBUGMSG(1, (TEXT("\n")));
  3028. #endif
  3029. #ifdef TEMPERAMENTAL_SERIAL_DRIVER
  3030. if (pIrlapCb->RxWin.FCS[Ns] == pMsg->IRDA_MSG_FCS)
  3031. TossedDups++;
  3032. else
  3033. ProcessInvalidNsOrNr(pIrlapCb, PFBit);
  3034. #else
  3035. ProcessInvalidNsOrNr(pIrlapCb, PFBit);
  3036. #endif
  3037. }
  3038. else
  3039. {
  3040. ConfirmAckedTxMsgs(pIrlapCb, Nr);
  3041. if (PFBit == IRLAP_PFBIT_SET)
  3042. {
  3043. InsertRxWinAndForward(pIrlapCb, pMsg, Ns);
  3044. if (Nr != pIrlapCb->Vs)
  3045. {
  3046. ResendRejects(pIrlapCb, Nr); // always turns link
  3047. }
  3048. else // Nr == Vs, Good Nr
  3049. {
  3050. // Link will always be turned here
  3051. if (pIrlapCb->Vr != pIrlapCb->RxWin.End)
  3052. {
  3053. MissingRxFrames(pIrlapCb);
  3054. }
  3055. else
  3056. {
  3057. XmitTxMsgList(pIrlapCb, TRUE, NULL);
  3058. }
  3059. }
  3060. }
  3061. else // PF Bit not set
  3062. {
  3063. InsertRxWinAndForward(pIrlapCb, pMsg, Ns);
  3064. }
  3065. }
  3066. // Start Timers: If PFBit set, link was turned so start final
  3067. // WDog is always stopped, so restart
  3068. if (PRIMARY == pIrlapCb->StationType)
  3069. {
  3070. if (PFBit == IRLAP_PFBIT_SET)
  3071. {
  3072. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3073. }
  3074. }
  3075. else // command from primary
  3076. {
  3077. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  3078. }
  3079. break;
  3080. default:
  3081. IFrameOtherStates(pIrlapCb, CRBit, PFBit);
  3082. }
  3083. }
  3084. /*****************************************************************************
  3085. *
  3086. */
  3087. BOOLEAN
  3088. InvalidNs(PIRLAP_CB pIrlapCb,
  3089. UINT Ns)
  3090. {
  3091. // Valididate ns
  3092. if (!InWindow(pIrlapCb->Vr,
  3093. (pIrlapCb->RxWin.Start + pIrlapCb->LocalWinSize-1) % IRLAP_MOD, Ns)
  3094. || !InWindow(pIrlapCb->RxWin.Start,
  3095. (pIrlapCb->RxWin.Start + pIrlapCb->LocalWinSize-1) % IRLAP_MOD, Ns))
  3096. {
  3097. DEBUGMSG(DBG_ERROR,
  3098. (TEXT("IRLAP: ERROR, Invalid Ns=%d! Vr=%d, RxStrt=%d Win=%d\r\n"),
  3099. Ns, pIrlapCb->Vr, pIrlapCb->RxWin.Start,
  3100. pIrlapCb->LocalWinSize));
  3101. IRLAP_LOG_ACTION((pIrlapCb, TEXT("** INVALID Ns **")));
  3102. return TRUE;
  3103. }
  3104. return FALSE;
  3105. }
  3106. /*****************************************************************************
  3107. *
  3108. */
  3109. BOOLEAN
  3110. InvalidNr(PIRLAP_CB pIrlapCb,
  3111. UINT Nr)
  3112. {
  3113. if (!InWindow(pIrlapCb->TxWin.Start, pIrlapCb->Vs, Nr))
  3114. {
  3115. DEBUGMSG(DBG_ERROR,
  3116. (TEXT("IRLAP: ERROR, Invalid Nr=%d! Vs=%d, TxStrt=%d\r\n"),
  3117. Nr, pIrlapCb->Vs, pIrlapCb->TxWin.Start));
  3118. return TRUE; // Invalid Nr
  3119. }
  3120. return FALSE;
  3121. }
  3122. /*****************************************************************************
  3123. *
  3124. */
  3125. BOOLEAN
  3126. InWindow(UINT Start, UINT End, UINT i)
  3127. {
  3128. if (Start <= End)
  3129. {
  3130. if (i >= Start && i <= End)
  3131. return TRUE;
  3132. }
  3133. else
  3134. {
  3135. if (i >= Start || i <= End)
  3136. return TRUE;
  3137. }
  3138. return FALSE;
  3139. }
  3140. /*****************************************************************************
  3141. *
  3142. */
  3143. VOID
  3144. ProcessInvalidNsOrNr(PIRLAP_CB pIrlapCb,
  3145. int PFBit)
  3146. {
  3147. ReturnRxTxWinMsgs(pIrlapCb);
  3148. if (PRIMARY == pIrlapCb->StationType)
  3149. {
  3150. if (PFBit == IRLAP_PFBIT_SET)
  3151. {
  3152. SendDISC(pIrlapCb);
  3153. pIrlapCb->RetryCnt = 0;
  3154. // F-timer will be started by caller
  3155. GotoPCloseState(pIrlapCb);
  3156. }
  3157. else
  3158. {
  3159. pIrlapCb->State = P_DISCONNECT_PEND;
  3160. }
  3161. }
  3162. else // SECONDARY
  3163. {
  3164. pIrlapCb->Frmr.Vs = (UCHAR) pIrlapCb->Vs;
  3165. pIrlapCb->Frmr.Vr = (UCHAR) pIrlapCb->Vr;
  3166. pIrlapCb->Frmr.W = 0;
  3167. pIrlapCb->Frmr.X = 0;
  3168. pIrlapCb->Frmr.Y = 0;
  3169. pIrlapCb->Frmr.Z = 1; // bad NR
  3170. if (PFBit == IRLAP_PFBIT_SET)
  3171. {
  3172. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  3173. }
  3174. else
  3175. {
  3176. pIrlapCb->State = S_ERROR;
  3177. }
  3178. }
  3179. }
  3180. /*****************************************************************************
  3181. *
  3182. */
  3183. VOID
  3184. InsertRxWinAndForward(PIRLAP_CB pIrlapCb,
  3185. PIRDA_MSG pIrdaMsg,
  3186. UINT Ns)
  3187. {
  3188. UINT rc = SUCCESS;
  3189. PIRDA_MSG pMsg;
  3190. if (pIrlapCb->RxWin.pMsg[Ns] != NULL)
  3191. {
  3192. DEBUGMSG(DBG_ERROR, (TEXT("IRLAP: RxFrame Ns:%d already in RxWin\n"),Ns));
  3193. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ns:%d already in window\n"), Ns));
  3194. return;
  3195. }
  3196. // insert message into receive window
  3197. pIrlapCb->RxWin.pMsg[Ns] = pIrdaMsg;
  3198. #ifdef TEMPERAMENTAL_SERIAL_DRIVER
  3199. pIrlapCb->RxWin.FCS[Ns] = pIrdaMsg->IRDA_MSG_FCS;
  3200. #endif
  3201. #if DBG_BADDRIVER
  3202. // What if the MAC driver modifies the buffer that we may hold onto?
  3203. {
  3204. UINT CheckVal = 0;
  3205. UCHAR *pByte = pIrdaMsg->IRDA_MSG_pRead;
  3206. while (pByte != pIrdaMsg->IRDA_MSG_pWrite)
  3207. {
  3208. CheckVal += *pByte++;
  3209. }
  3210. *(UINT *) pIrdaMsg->IRDA_MSG_Header = CheckVal;
  3211. }
  3212. #endif
  3213. // Advance RxWin.End to Ns+1 if Ns is at or beyond RxWin.End
  3214. if (!InWindow(pIrlapCb->RxWin.Start, pIrlapCb->RxWin.End, Ns) ||
  3215. Ns == pIrlapCb->RxWin.End)
  3216. {
  3217. pIrlapCb->RxWin.End = (Ns + 1) % IRLAP_MOD;
  3218. }
  3219. //
  3220. // Increment reference. It may be out of sequence so
  3221. // we'll will have to hold onto it.
  3222. //
  3223. ASSERT(pIrdaMsg->IRDA_MSG_RefCnt == 1);
  3224. pIrdaMsg->IRDA_MSG_RefCnt += 1;
  3225. // Forward in sequence frames starting from Vr
  3226. pMsg = pIrlapCb->RxWin.pMsg[pIrlapCb->Vr];
  3227. while (pMsg != NULL && !pIrlapCb->LocalBusy)
  3228. {
  3229. #if DBG_BADDRIVER
  3230. // What if the MAC driver modifies the buffer that we may hold onto?
  3231. {
  3232. UINT CheckVal = 0;
  3233. UCHAR *pByte = pMsg->IRDA_MSG_pRead;
  3234. while (pByte != pMsg->IRDA_MSG_pWrite)
  3235. {
  3236. CheckVal += *pByte++;
  3237. }
  3238. if (CheckVal != *(UINT *) pMsg->IRDA_MSG_Header)
  3239. {
  3240. DEBUGMSG(1, (TEXT("IRLAP: MAC driver has modified buffer owned by IrLAP! SEVERE ERROR\n")));
  3241. ASSERT(0);
  3242. }
  3243. }
  3244. #endif
  3245. pMsg->Prim = IRLAP_DATA_IND;
  3246. rc = IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  3247. if (rc == SUCCESS || rc == IRLMP_LOCAL_BUSY)
  3248. {
  3249. // Delivered successfully. Done with this message. Remove it from
  3250. // the RxWin and return msg to MAC. Update Vr
  3251. pIrlapCb->RxWin.pMsg[pIrlapCb->Vr] = NULL;
  3252. ASSERT(pMsg->IRDA_MSG_RefCnt);
  3253. pMsg->IRDA_MSG_RefCnt -=1;
  3254. if (pMsg->IRDA_MSG_RefCnt == 0)
  3255. {
  3256. // if this is the current irda message then don't
  3257. // do the data_response. It will be taken care of
  3258. // by the caller.
  3259. pMsg->Prim = MAC_DATA_RESP;
  3260. IrmacDown(pIrlapCb->pIrdaLinkCb, pMsg);
  3261. }
  3262. pIrlapCb->Vr = (pIrlapCb->Vr + 1) % IRLAP_MOD;
  3263. pMsg = pIrlapCb->RxWin.pMsg[pIrlapCb->Vr];
  3264. // LMP doesn't want anymore messages
  3265. if (rc == IRLMP_LOCAL_BUSY)
  3266. {
  3267. // The receive window will be cleaned out when RNR is sent
  3268. pIrlapCb->LocalBusy = TRUE;
  3269. }
  3270. }
  3271. else
  3272. {
  3273. ASSERT(0);
  3274. return;
  3275. }
  3276. }
  3277. if (pIrdaMsg->IRDA_MSG_RefCnt > 1)
  3278. {
  3279. //
  3280. // The message was not indicated to Irlmp.
  3281. // We'll have to copy the data out of the buffer
  3282. // since some miniports can't handle us
  3283. // holding onto the packets
  3284. if (pIrdaMsg->DataContext)
  3285. {
  3286. UCHAR *pCurRead, *pCurWrite;
  3287. LONG_PTR Len;
  3288. pCurRead = pIrdaMsg->IRDA_MSG_pRead;
  3289. pCurWrite = pIrdaMsg->IRDA_MSG_pWrite;
  3290. Len = pCurWrite - pCurRead;
  3291. ASSERT(Len <= pIrlapCb->pIrdaLinkCb->RxMsgDataSize);
  3292. pIrdaMsg->IRDA_MSG_pRead = (UCHAR *) pIrdaMsg + sizeof(IRDA_MSG);
  3293. ASSERT(pIrdaMsg->IRDA_MSG_pRead != pCurRead);
  3294. CTEMemCopy(pIrdaMsg->IRDA_MSG_pRead, pCurRead, Len);
  3295. pIrdaMsg->IRDA_MSG_pWrite = pIrdaMsg->IRDA_MSG_pRead + Len;
  3296. }
  3297. }
  3298. }
  3299. /*****************************************************************************
  3300. *
  3301. */
  3302. VOID
  3303. ResendRejects(PIRLAP_CB pIrlapCb, UINT Nr)
  3304. {
  3305. if (!pIrlapCb->RemoteBusy)
  3306. {
  3307. // Set Vs back
  3308. for (pIrlapCb->Vs=Nr; pIrlapCb->Vs !=
  3309. (pIrlapCb->TxWin.End-1)%IRLAP_MOD;
  3310. pIrlapCb->Vs = (pIrlapCb->Vs + 1) % IRLAP_MOD)
  3311. {
  3312. pIrlapCb->RetranCnt++;
  3313. IRLAP_LOG_ACTION((pIrlapCb, TEXT("RETRANSMISSION:")));
  3314. SendIFrame(pIrlapCb,
  3315. pIrlapCb->TxWin.pMsg[pIrlapCb->Vs],
  3316. pIrlapCb->Vs,
  3317. IRLAP_PFBIT_CLEAR);
  3318. }
  3319. IRLAP_LOG_ACTION((pIrlapCb, TEXT("RETRANSMISSION:")));
  3320. // Send last one with PFBit set
  3321. SendIFrame(pIrlapCb, pIrlapCb->TxWin.pMsg[pIrlapCb->Vs],
  3322. pIrlapCb->Vs, IRLAP_PFBIT_SET);
  3323. pIrlapCb->Vs = (pIrlapCb->Vs + 1) % IRLAP_MOD; // Vs == TxWin.End
  3324. }
  3325. else
  3326. {
  3327. SendRR_RNR(pIrlapCb);
  3328. }
  3329. }
  3330. /*****************************************************************************
  3331. *
  3332. */
  3333. VOID
  3334. ConfirmAckedTxMsgs(PIRLAP_CB pIrlapCb,
  3335. UINT Nr)
  3336. {
  3337. UINT i = pIrlapCb->TxWin.Start;
  3338. IRDA_MSG *pMsg;
  3339. while (i != Nr)
  3340. {
  3341. pMsg = pIrlapCb->TxWin.pMsg[i];
  3342. pIrlapCb->TxWin.pMsg[i] = NULL;
  3343. if (pMsg != NULL)
  3344. {
  3345. ASSERT(pMsg->IRDA_MSG_RefCnt);
  3346. if (InterlockedDecrement(&pMsg->IRDA_MSG_RefCnt) == 0)
  3347. {
  3348. pMsg->Prim = IRLAP_DATA_CONF;
  3349. pMsg->IRDA_MSG_DataStatus = IRLAP_DATA_REQUEST_COMPLETED;
  3350. IrlmpUp(pIrlapCb->pIrdaLinkCb, pMsg);
  3351. }
  3352. #if DBG
  3353. else
  3354. {
  3355. pIrlapCb->DelayedConf++;
  3356. }
  3357. #endif
  3358. }
  3359. i = (i + 1) % IRLAP_MOD;
  3360. }
  3361. pIrlapCb->TxWin.Start = i;
  3362. }
  3363. /*****************************************************************************
  3364. *
  3365. */
  3366. VOID
  3367. MissingRxFrames(PIRLAP_CB pIrlapCb)
  3368. {
  3369. int MissingFrameCnt = 0;
  3370. int MissingFrame = -1;
  3371. UINT i;
  3372. i = pIrlapCb->Vr;
  3373. // Count missing frame, determine first missing frame
  3374. for (i = pIrlapCb->Vr; (i + 1) % IRLAP_MOD != pIrlapCb->RxWin.End;
  3375. i = (i+1) % IRLAP_MOD)
  3376. {
  3377. if (pIrlapCb->RxWin.pMsg[i] == NULL)
  3378. {
  3379. MissingFrameCnt++;
  3380. if (MissingFrame == -1)
  3381. {
  3382. MissingFrame = i;
  3383. }
  3384. }
  3385. }
  3386. // if there are missing frames send SREJ (1) or RR (more than 1)
  3387. // and turn link around
  3388. if (MissingFrameCnt == 1 && !pIrlapCb->LocalBusy)
  3389. {
  3390. // we don't want to send the SREJ when local is busy because
  3391. // peer *MAY* interpret it as a clearing of the local busy condition
  3392. SendSREJ(pIrlapCb, MissingFrame);
  3393. }
  3394. else
  3395. {
  3396. // The RR/RNR will serve as an implicit REJ
  3397. SendRR_RNR(pIrlapCb);
  3398. }
  3399. }
  3400. /*****************************************************************************
  3401. *
  3402. */
  3403. VOID
  3404. IFrameOtherStates(PIRLAP_CB pIrlapCb,
  3405. int CRBit,
  3406. int PFBit)
  3407. {
  3408. switch (pIrlapCb->State)
  3409. {
  3410. case NDM:
  3411. case DSCV_MEDIA_SENSE:
  3412. case DSCV_QUERY:
  3413. case DSCV_REPLY:
  3414. case CONN_MEDIA_SENSE:
  3415. case SNRM_SENT:
  3416. case BACKOFF_WAIT:
  3417. case SNRM_RECEIVED:
  3418. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  3419. return;
  3420. }
  3421. if (pIrlapCb->CRBit == CRBit) // should be opposite of mine
  3422. {
  3423. if (pIrlapCb->StationType == PRIMARY)
  3424. {
  3425. if (pIrlapCb->State == P_XMIT)
  3426. {
  3427. IrlapTimerStop(pIrlapCb, &pIrlapCb->PollTimer);
  3428. }
  3429. else
  3430. {
  3431. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  3432. }
  3433. }
  3434. else
  3435. {
  3436. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3437. }
  3438. StationConflict(pIrlapCb);
  3439. pIrlapCb->State = NDM;
  3440. return;
  3441. }
  3442. if (pIrlapCb->StationType == PRIMARY) // I'm PRIMARY, this is a
  3443. { // response from secondary
  3444. switch (pIrlapCb->State)
  3445. {
  3446. case P_DISCONNECT_PEND:
  3447. if (PFBit == IRLAP_PFBIT_CLEAR)
  3448. {
  3449. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  3450. }
  3451. else
  3452. {
  3453. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  3454. SendDISC(pIrlapCb);
  3455. pIrlapCb->RetryCnt = 0;
  3456. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3457. GotoPCloseState(pIrlapCb);
  3458. }
  3459. break;
  3460. case P_CLOSE:
  3461. if (PFBit == IRLAP_PFBIT_CLEAR)
  3462. {
  3463. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  3464. }
  3465. else
  3466. {
  3467. IrlapTimerStop(pIrlapCb, &pIrlapCb->FinalTimer);
  3468. ResendDISC(pIrlapCb);
  3469. if (pIrlapCb->State != NDM)
  3470. {
  3471. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3472. }
  3473. }
  3474. break;
  3475. case S_CLOSE:
  3476. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3477. break;
  3478. default:
  3479. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  3480. }
  3481. }
  3482. else
  3483. {
  3484. switch (pIrlapCb->State)
  3485. {
  3486. case S_DISCONNECT_PEND:
  3487. if (IRLAP_PFBIT_SET == PFBit)
  3488. {
  3489. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3490. SendRD(pIrlapCb);
  3491. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  3492. pIrlapCb->State = S_CLOSE;
  3493. }
  3494. else
  3495. {
  3496. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  3497. }
  3498. break;
  3499. case S_ERROR:
  3500. if (IRLAP_PFBIT_SET == PFBit)
  3501. {
  3502. SendFRMR(pIrlapCb, &pIrlapCb->Frmr);
  3503. pIrlapCb->State = S_NRM;
  3504. }
  3505. else
  3506. {
  3507. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3508. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  3509. }
  3510. break;
  3511. case S_CLOSE:
  3512. if (IRLAP_PFBIT_SET == PFBit)
  3513. {
  3514. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3515. SendRD(pIrlapCb);
  3516. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  3517. }
  3518. else
  3519. {
  3520. IrlapTimerStop(pIrlapCb, &pIrlapCb->WDogTimer);
  3521. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  3522. }
  3523. default:
  3524. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignore in this state")));
  3525. }
  3526. }
  3527. }
  3528. /*****************************************************************************
  3529. *
  3530. */
  3531. VOID
  3532. StationConflict(PIRLAP_CB pIrlapCb)
  3533. {
  3534. IRDA_MSG IMsg;
  3535. PAGED_CODE();
  3536. InitializeState(pIrlapCb, PRIMARY); // Primary doesn't mean anything here
  3537. ApplyDefaultParms(pIrlapCb);
  3538. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3539. IMsg.IRDA_MSG_DiscStatus = IRLAP_PRIMARY_CONFLICT;
  3540. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3541. }
  3542. /*****************************************************************************
  3543. *
  3544. */
  3545. VOID
  3546. ApplyDefaultParms(PIRLAP_CB pIrlapCb)
  3547. {
  3548. IRDA_MSG IMsg;
  3549. PAGED_CODE();
  3550. //IndicateLinkStatus(pIrlapCb, LINK_STATUS_IDLE);
  3551. pIrlapCb->StatusFlags = 0;
  3552. pIrlapCb->Baud = IRLAP_CONTENTION_BAUD;
  3553. pIrlapCb->RemoteMaxTAT = IRLAP_CONTENTION_MAX_TAT;
  3554. pIrlapCb->RemoteDataSize = IRLAP_CONTENTION_DATA_SIZE;
  3555. pIrlapCb->RemoteWinSize = IRLAP_CONTENTION_WIN_SIZE;
  3556. pIrlapCb->RemoteNumBOFS = IRLAP_CONTENTION_BOFS;
  3557. pIrlapCb->ConnAddr = IRLAP_BROADCAST_CONN_ADDR;
  3558. pIrlapCb->NoResponse = FALSE;
  3559. IMsg.Prim = MAC_CONTROL_REQ;
  3560. IMsg.IRDA_MSG_Op = MAC_RECONFIG_LINK;
  3561. IMsg.IRDA_MSG_Baud = IRLAP_CONTENTION_BAUD;
  3562. IMsg.IRDA_MSG_NumBOFs = IRLAP_CONTENTION_BOFS;
  3563. IMsg.IRDA_MSG_DataSize = IRLAP_CONTENTION_DATA_SIZE;
  3564. IMsg.IRDA_MSG_MinTat = 0;
  3565. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_CONTROL_REQ - reconfig link")));
  3566. IrmacDown(pIrlapCb->pIrdaLinkCb, &IMsg);
  3567. }
  3568. /*****************************************************************************
  3569. *
  3570. */
  3571. VOID
  3572. ResendDISC(PIRLAP_CB pIrlapCb)
  3573. {
  3574. IRDA_MSG IMsg;
  3575. if (pIrlapCb->RetryCnt >= pIrlapCb->N3)
  3576. {
  3577. ApplyDefaultParms(pIrlapCb);
  3578. pIrlapCb->RetryCnt = 0;
  3579. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3580. IMsg.IRDA_MSG_DiscStatus = IRLAP_NO_RESPONSE;
  3581. pIrlapCb->State = NDM;
  3582. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3583. }
  3584. else
  3585. {
  3586. SendDISC(pIrlapCb);
  3587. pIrlapCb->RetryCnt++;
  3588. }
  3589. }
  3590. /*****************************************************************************
  3591. *
  3592. */
  3593. BOOLEAN
  3594. IgnoreState(PIRLAP_CB pIrlapCb)
  3595. {
  3596. switch (pIrlapCb->State)
  3597. {
  3598. case NDM:
  3599. case DSCV_MEDIA_SENSE:
  3600. case DSCV_QUERY:
  3601. case DSCV_REPLY:
  3602. case CONN_MEDIA_SENSE:
  3603. case SNRM_SENT:
  3604. case BACKOFF_WAIT:
  3605. case SNRM_RECEIVED:
  3606. case P_XMIT:
  3607. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring in this state")));
  3608. return TRUE;
  3609. }
  3610. return FALSE;
  3611. }
  3612. /*****************************************************************************
  3613. *
  3614. */
  3615. VOID
  3616. QueryTimerExp(PVOID Context)
  3617. {
  3618. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3619. IRLAP_LOG_START((pIrlapCb, TEXT("Query timer expired")));
  3620. if (pIrlapCb->State == DSCV_REPLY)
  3621. {
  3622. pIrlapCb->State = NDM;
  3623. }
  3624. else
  3625. {
  3626. IRLAP_LOG_ACTION((pIrlapCb,
  3627. TEXT("Ignoring QueryTimer Expriation in state %s"),
  3628. IRLAP_StateStr[pIrlapCb->State]));
  3629. }
  3630. IRLAP_LOG_COMPLETE(pIrlapCb);
  3631. return;
  3632. }
  3633. /*****************************************************************************
  3634. *
  3635. */
  3636. VOID
  3637. SlotTimerExp(PVOID Context)
  3638. {
  3639. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3640. IRDA_MSG IMsg;
  3641. IRLAP_LOG_START((pIrlapCb, TEXT("Slot timer expired, slot=%d"),pIrlapCb->SlotCnt+1));
  3642. if (pIrlapCb->State == DSCV_QUERY)
  3643. {
  3644. pIrlapCb->SlotCnt++;
  3645. SendDscvXIDCmd(pIrlapCb);
  3646. if (pIrlapCb->SlotCnt < pIrlapCb->MaxSlot)
  3647. {
  3648. IMsg.Prim = MAC_CONTROL_REQ;
  3649. IMsg.IRDA_MSG_Op = MAC_MEDIA_SENSE;
  3650. IMsg.IRDA_MSG_SenseTime = IRLAP_DSCV_SENSE_TIME;
  3651. IRLAP_LOG_ACTION((pIrlapCb, TEXT("MAC_CONTROL_REQ (dscv sense)")));
  3652. IrmacDown(pIrlapCb->pIrdaLinkCb,&IMsg);
  3653. }
  3654. else
  3655. {
  3656. //IndicateLinkStatus(pIrlapCb, LINK_STATUS_IDLE);
  3657. pIrlapCb->GenNewAddr = FALSE;
  3658. IMsg.Prim = IRLAP_DISCOVERY_CONF;
  3659. IMsg.IRDA_MSG_pDevList = &pIrlapCb->DevList;
  3660. IMsg.IRDA_MSG_DscvStatus = IRLAP_DISCOVERY_COMPLETED;
  3661. // Change state now so IRLMP can do DISCOVERY_REQ on this thread
  3662. pIrlapCb->State = NDM;
  3663. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3664. }
  3665. }
  3666. else
  3667. {
  3668. IRLAP_LOG_ACTION((pIrlapCb, TEXT("Ignoring SlotTimer Expriation in state %s"),
  3669. IRLAP_StateStr[pIrlapCb->State]));
  3670. ; // maybe return bad state ???
  3671. }
  3672. IRLAP_LOG_COMPLETE(pIrlapCb);
  3673. return;
  3674. }
  3675. /*****************************************************************************
  3676. *
  3677. */
  3678. VOID
  3679. FinalTimerExp(PVOID Context)
  3680. {
  3681. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3682. IRDA_MSG IMsg;
  3683. IRLAP_LOG_START((pIrlapCb, TEXT("Final timer expired")));
  3684. pIrlapCb->NoResponse = TRUE;
  3685. pIrlapCb->FTimerExpCnt++;
  3686. switch (pIrlapCb->State)
  3687. {
  3688. case SNRM_SENT:
  3689. if (pIrlapCb->RetryCnt < pIrlapCb->N3)
  3690. {
  3691. pIrlapCb->BackoffTimer.Timeout = IRLAP_BACKOFF_TIME();
  3692. IrlapTimerStart(pIrlapCb, &pIrlapCb->BackoffTimer);
  3693. pIrlapCb->State = BACKOFF_WAIT;
  3694. }
  3695. else
  3696. {
  3697. ApplyDefaultParms(pIrlapCb);
  3698. pIrlapCb->RetryCnt = 0;
  3699. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3700. IMsg.IRDA_MSG_DiscStatus = IRLAP_NO_RESPONSE;
  3701. pIrlapCb->State = NDM;
  3702. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3703. }
  3704. break;
  3705. case P_RECV:
  3706. if (pIrlapCb->RetryCnt == pIrlapCb->N2)
  3707. {
  3708. pIrlapCb->RetryCnt = 0; // Don't have to, do it for logger
  3709. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3710. IMsg.IRDA_MSG_DiscStatus = IRLAP_NO_RESPONSE;
  3711. pIrlapCb->State = NDM;
  3712. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3713. ReturnRxTxWinMsgs(pIrlapCb);
  3714. ApplyDefaultParms(pIrlapCb);
  3715. }
  3716. else
  3717. {
  3718. pIrlapCb->RetryCnt++;
  3719. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3720. SendRR_RNR(pIrlapCb);
  3721. if (pIrlapCb->RetryCnt == pIrlapCb->N1)
  3722. {
  3723. pIrlapCb->StatusFlags = LF_INTERRUPTED;
  3724. IndicateLinkStatus(pIrlapCb);
  3725. }
  3726. }
  3727. break;
  3728. case P_DISCONNECT_PEND:
  3729. SendDISC(pIrlapCb);
  3730. pIrlapCb->RetryCnt = 0;
  3731. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3732. GotoPCloseState(pIrlapCb);
  3733. break;
  3734. case P_CLOSE:
  3735. if (pIrlapCb->RetryCnt >= pIrlapCb->N3)
  3736. {
  3737. ApplyDefaultParms(pIrlapCb);
  3738. pIrlapCb->RetryCnt = 0; // Don't have to, do it for logger
  3739. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3740. IMsg.IRDA_MSG_DiscStatus = IRLAP_NO_RESPONSE;
  3741. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3742. GotoNDMThenDscvOrConn(pIrlapCb);
  3743. }
  3744. else
  3745. {
  3746. pIrlapCb->RetryCnt++;
  3747. SendDISC(pIrlapCb);
  3748. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3749. }
  3750. break;
  3751. default:
  3752. IRLAP_LOG_ACTION((pIrlapCb,
  3753. TEXT("Ignoring Final Expriation in state %s"),
  3754. IRLAP_StateStr[pIrlapCb->State]));
  3755. }
  3756. IRLAP_LOG_COMPLETE(pIrlapCb);
  3757. return;
  3758. }
  3759. /*****************************************************************************
  3760. *
  3761. */
  3762. VOID
  3763. PollTimerExp(PVOID Context)
  3764. {
  3765. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3766. IRLAP_LOG_START((pIrlapCb, TEXT("Poll timer expired")));
  3767. if (pIrlapCb->State == P_XMIT)
  3768. {
  3769. SendRR_RNR(pIrlapCb);
  3770. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3771. pIrlapCb->State = P_RECV;
  3772. }
  3773. else
  3774. {
  3775. IRLAP_LOG_ACTION((pIrlapCb,
  3776. TEXT("Ignoring Poll Expriation in state %s"),
  3777. IRLAP_StateStr[pIrlapCb->State]));
  3778. }
  3779. IRLAP_LOG_COMPLETE(pIrlapCb);
  3780. return;
  3781. }
  3782. /*****************************************************************************
  3783. *
  3784. */
  3785. VOID
  3786. BackoffTimerExp(PVOID Context)
  3787. {
  3788. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3789. IRLAP_LOG_START((pIrlapCb, TEXT("Backoff timer expired")));
  3790. if (pIrlapCb->State == BACKOFF_WAIT)
  3791. {
  3792. SendSNRM(pIrlapCb, TRUE);
  3793. IrlapTimerStart(pIrlapCb, &pIrlapCb->FinalTimer);
  3794. pIrlapCb->RetryCnt += 1;
  3795. pIrlapCb->State = SNRM_SENT;
  3796. }
  3797. else
  3798. {
  3799. IRLAP_LOG_ACTION((pIrlapCb,
  3800. TEXT("Ignoring BackoffTimer Expriation in this state ")));
  3801. }
  3802. IRLAP_LOG_COMPLETE(pIrlapCb);
  3803. return;
  3804. }
  3805. /*****************************************************************************
  3806. *
  3807. */
  3808. VOID
  3809. WDogTimerExp(PVOID Context)
  3810. {
  3811. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3812. IRDA_MSG IMsg;
  3813. IRLAP_LOG_START((pIrlapCb, TEXT("WDog timer expired")));
  3814. pIrlapCb->NoResponse = TRUE;
  3815. switch (pIrlapCb->State)
  3816. {
  3817. case S_DISCONNECT_PEND:
  3818. case S_NRM:
  3819. pIrlapCb->WDogExpCnt++;
  3820. // Disconnect/threshold time is in seconds
  3821. if (pIrlapCb->WDogExpCnt * (int)pIrlapCb->WDogTimer.Timeout >=
  3822. pIrlapCb->DisconnectTime * 1000)
  3823. {
  3824. pIrlapCb->State = NDM;
  3825. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3826. IMsg.IRDA_MSG_DiscStatus = IRLAP_NO_RESPONSE;
  3827. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3828. ReturnRxTxWinMsgs(pIrlapCb);
  3829. ApplyDefaultParms(pIrlapCb);
  3830. }
  3831. else
  3832. {
  3833. if ((pIrlapCb->WDogExpCnt * (int) pIrlapCb->WDogTimer.Timeout >=
  3834. pIrlapCb->ThresholdTime * 1000) && !pIrlapCb->StatusSent)
  3835. {
  3836. pIrlapCb->StatusFlags = LF_INTERRUPTED;
  3837. IndicateLinkStatus(pIrlapCb);
  3838. pIrlapCb->StatusSent = TRUE;
  3839. }
  3840. IrlapTimerStart(pIrlapCb, &pIrlapCb->WDogTimer);
  3841. }
  3842. break;
  3843. case S_CLOSE:
  3844. ApplyDefaultParms(pIrlapCb);
  3845. IMsg.Prim = IRLAP_DISCONNECT_IND;
  3846. IMsg.IRDA_MSG_DiscStatus = IRLAP_NO_RESPONSE;
  3847. pIrlapCb->State = NDM;
  3848. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3849. break;
  3850. default:
  3851. IRLAP_LOG_ACTION((pIrlapCb,
  3852. TEXT("Ignore WDogTimer expiration in state %s"),
  3853. IRLAP_StateStr[pIrlapCb->State]));
  3854. }
  3855. IRLAP_LOG_COMPLETE(pIrlapCb);
  3856. return;
  3857. }
  3858. /*
  3859. VOID
  3860. StatusTimerExp(PVOID Context)
  3861. {
  3862. PIRLAP_CB pIrlapCb = (PIRLAP_CB) Context;
  3863. IndicateLinkStatus(pIrlapCb);
  3864. }
  3865. */
  3866. /*****************************************************************************
  3867. *
  3868. * @func ret_type | func_name | funcdesc
  3869. *
  3870. * @rdesc return desc
  3871. * @flag val | desc
  3872. *
  3873. * @parm data_type | parm_name | description
  3874. *
  3875. * @comm
  3876. * comments
  3877. *
  3878. * @ex
  3879. * example
  3880. */
  3881. /* !!!
  3882. void
  3883. IRLAP_PrintState()
  3884. {
  3885. #if DBG
  3886. DEBUGMSG(1, (TEXT("IRLAP State %s\n"), IRLAP_StateStr[pIrlapCb->State]));
  3887. #else
  3888. DEBUGMSG(1, (TEXT("IRLAP State %d\n"), pIrlapCb->State));
  3889. #endif
  3890. DEBUGMSG(1,
  3891. (TEXT(" Vs=%d Vr=%d RxWin(%d,%d) TxWin(%d,%d) TxMsgListLen=%d RxMsgFreeListLen=%d\r\n"),
  3892. pIrlapCb->Vs, pIrlapCb->Vr,
  3893. pIrlapCb->RxWin.Start, pIrlapCb->RxWin.End,
  3894. pIrlapCb->TxWin.Start, pIrlapCb->TxWin.End,
  3895. pIrlapCb->TxMsgList.Len, pIrlapCb->RxMsgFreeList.Len));
  3896. #ifdef TEMPERAMENTAL_SERIAL_DRIVER
  3897. DEBUGMSG(1, (TEXT(" Tossed duplicates %d\n"), TossedDups));
  3898. #endif
  3899. IRMAC_PrintState();
  3900. return;
  3901. }
  3902. */
  3903. int
  3904. GetMyDevAddr(BOOLEAN New)
  3905. {
  3906. #ifndef UNDER_CE
  3907. int DevAddr, NewDevAddr;
  3908. LARGE_INTEGER li;
  3909. KeQueryTickCount(&li);
  3910. NewDevAddr = (int) li.LowPart;
  3911. DevAddr = NewDevAddr;
  3912. #else
  3913. int DevAddr = GetTickCount();
  3914. HKEY hKey;
  3915. LONG hRes;
  3916. TCHAR KeyName[32];
  3917. ULONG RegDevAddr = 0;
  3918. TCHAR ValName[] = TEXT("DevAddr");
  3919. // Get the device address from the registry. If the key exists and the
  3920. // value is 0, store a new random address. If no key, then return
  3921. // a random address.
  3922. _tcscpy (KeyName, COMM_REG_KEY);
  3923. _tcscat (KeyName, TEXT("IrDA"));
  3924. hRes = RegOpenKeyEx (HKEY_LOCAL_MACHINE, KeyName, 0, 0, &hKey);
  3925. if (hRes == ERROR_SUCCESS &&
  3926. GetRegDWORDValue(hKey, ValName, &RegDevAddr))
  3927. {
  3928. if (RegDevAddr == 0)
  3929. {
  3930. RegDevAddr = GetTickCount();
  3931. SetRegDWORDValue(hKey, ValName, RegDevAddr);
  3932. }
  3933. RegCloseKey(hKey);
  3934. DevAddr = (int) RegDevAddr;
  3935. }
  3936. #endif
  3937. return DevAddr;
  3938. }
  3939. VOID
  3940. StatusReq(
  3941. PIRLAP_CB pIrlapCb,
  3942. IRDA_MSG *pMsg)
  3943. {
  3944. PIRLINK_STATUS pLinkStatus = (IRLINK_STATUS *) pMsg->IRDA_MSG_pLinkStatus;
  3945. CTEMemCopy(pLinkStatus->ConnectedDeviceId,
  3946. pIrlapCb->RemoteDevice.DevAddr,
  3947. IRDA_DEV_ADDR_LEN);
  3948. pLinkStatus->ConnectSpeed = pIrlapCb->Baud;
  3949. }
  3950. VOID
  3951. IndicateLinkStatus(PIRLAP_CB pIrlapCb)
  3952. {
  3953. IRDA_MSG IMsg;
  3954. IRLINK_STATUS LinkStatus;
  3955. CTEMemCopy(LinkStatus.ConnectedDeviceId,
  3956. pIrlapCb->RemoteDevice.DevAddr,
  3957. IRDA_DEV_ADDR_LEN);
  3958. LinkStatus.ConnectSpeed = pIrlapCb->Baud;
  3959. if (pIrlapCb->StatusFlags & LF_INTERRUPTED)
  3960. {
  3961. LinkStatus.Flags = LF_INTERRUPTED;
  3962. }
  3963. else
  3964. {
  3965. if (pIrlapCb->State >= P_XMIT)
  3966. {
  3967. LinkStatus.Flags = LF_CONNECTED;
  3968. }
  3969. else
  3970. {
  3971. LinkStatus.Flags = 0;
  3972. }
  3973. LinkStatus.Flags |= pIrlapCb->StatusFlags;
  3974. pIrlapCb->StatusFlags = 0;
  3975. }
  3976. IMsg.Prim = IRLAP_STATUS_IND;
  3977. IMsg.IRDA_MSG_pLinkStatus = &LinkStatus;
  3978. IrlmpUp(pIrlapCb->pIrdaLinkCb, &IMsg);
  3979. /*
  3980. if (pIrlapCb->State >= P_XMIT && pIrlapCb->MonitorLink)
  3981. {
  3982. IrdaTimerStart(&pIrlapCb->StatusTimer);
  3983. }
  3984. */
  3985. }
  3986. /*
  3987. VOID
  3988. IrlapGetLinkStatus(PIRLINK_STATUS pLinkStatus)
  3989. {
  3990. PIRDA_LINK_CB pIrdaLinkCb = (PIRDA_LINK_CB) IrdaLinkCbList.Flink;
  3991. PIRLAP_CB pIrlapCb = (PIRLAP_CB) pIrdaLinkCb->IrlapContext;
  3992. pLinkStatus->Flags = 0;
  3993. if (IrdaLinkCbList.Flink == &IrdaLinkCbList)
  3994. {
  3995. return;
  3996. }
  3997. CTEMemCopy(pLinkStatus->ConnectedDeviceId,
  3998. pIrlapCb->RemoteDevice.DevAddr,
  3999. IRDA_DEV_ADDR_LEN);
  4000. pLinkStatus->ConnectSpeed = pIrlapCb->Baud;
  4001. if (pIrlapCb->StatusFlags & LF_INTERRUPTED)
  4002. {
  4003. pLinkStatus->Flags = LF_INTERRUPTED;
  4004. return;
  4005. }
  4006. else if (pIrlapCb->State >= P_XMIT)
  4007. {
  4008. pLinkStatus->Flags = LF_CONNECTED;
  4009. }
  4010. pLinkStatus->Flags |= pIrlapCb->StatusFlags;
  4011. pIrlapCb->StatusFlags = 0;
  4012. return;
  4013. }
  4014. BOOLEAN
  4015. IrlapConnectionActive(PVOID Context)
  4016. {
  4017. PIRLAP_CB pIrlapCb = Context;
  4018. if (pIrlapCb->State >= P_XMIT)
  4019. {
  4020. return TRUE;
  4021. }
  4022. else
  4023. {
  4024. return FALSE;
  4025. }
  4026. }
  4027. */