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.

224 lines
5.2 KiB

  1. /*++
  2. Microsoft Windows
  3. Copyright (C) Microsoft Corporation, 1981 - 1999
  4. Module Name:
  5. devicelist.cpp
  6. Abstract:
  7. Author:
  8. Rahul Thombre (RahulTh) 4/30/1998
  9. Revision History:
  10. 4/30/1998 RahulTh
  11. Created this module.
  12. --*/
  13. ///////////////////////////////////////////////////////////////////////
  14. // DeviceList.cpp: implementation of the CDeviceList class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "precomp.hxx"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. //////////////////////////////////////////////////////////////////////
  24. // Construction/Destruction
  25. //////////////////////////////////////////////////////////////////////
  26. CDeviceList::CDeviceList()
  27. {
  28. m_pDeviceInfo = NULL;
  29. m_lNumDevices = 0;
  30. InitializeCriticalSection(&m_criticalSection);
  31. }
  32. CDeviceList::~CDeviceList()
  33. {
  34. if(m_pDeviceInfo)
  35. {
  36. delete [] m_pDeviceInfo;
  37. m_pDeviceInfo = NULL;
  38. m_lNumDevices = 0;
  39. }
  40. }
  41. CDeviceList& CDeviceList::operator=(POBEX_DEVICE_LIST pDevList)
  42. {
  43. CError error (NULL, IDS_DEFAULT_ERROR_TITLE, ERROR_OUTOFMEMORY);
  44. EnterCriticalSection(&m_criticalSection);
  45. if (m_pDeviceInfo)
  46. {
  47. delete [] m_pDeviceInfo;
  48. m_pDeviceInfo = NULL;
  49. m_lNumDevices = 0;
  50. }
  51. LeaveCriticalSection(&m_criticalSection);
  52. if (!pDevList)
  53. return *this;
  54. //if there is a device list
  55. EnterCriticalSection(&m_criticalSection);
  56. m_lNumDevices = pDevList->DeviceCount;
  57. try
  58. {
  59. m_pDeviceInfo = new OBEX_DEVICE [m_lNumDevices];
  60. memcpy ((LPVOID)m_pDeviceInfo, (LPVOID)pDevList->DeviceList, sizeof(OBEX_DEVICE) * m_lNumDevices);
  61. }
  62. catch (CMemoryException* e)
  63. {
  64. error.ShowMessage (IDS_DEVLIST_ERROR);
  65. m_pDeviceInfo = NULL; //the best we can do here is to pretend that there
  66. m_lNumDevices = 0; //are no devices and move on
  67. e->Delete();
  68. }
  69. LeaveCriticalSection(&m_criticalSection);
  70. return *this;
  71. }
  72. ULONG CDeviceList::GetDeviceCount(void)
  73. {
  74. return m_lNumDevices;
  75. }
  76. LONG CDeviceList::SelectDevice(CWnd * pWnd, TCHAR* lpszDevName)
  77. {
  78. ASSERT(pWnd);
  79. int iSel;
  80. LONG lDeviceID;
  81. int numDevices;
  82. lpszDevName[0] = '\0';
  83. EnterCriticalSection(&m_criticalSection);
  84. numDevices = m_lNumDevices;
  85. if (numDevices > 0) {
  86. if (m_pDeviceInfo->DeviceType == TYPE_IRDA) {
  87. lDeviceID = m_pDeviceInfo->DeviceSpecific.s.Irda.DeviceId; //store the id and name of the
  88. } else {
  89. lDeviceID = m_pDeviceInfo->DeviceSpecific.s.Ip.IpAddress;
  90. }
  91. lstrcpy(lpszDevName,m_pDeviceInfo->DeviceName);
  92. }
  93. LeaveCriticalSection(&m_criticalSection);
  94. if (numDevices == 0)
  95. return errIRFTP_NODEVICE; //in the rare case that there are no devices in range, return -1
  96. if (1 == numDevices) //there is only one device. No need to choose a device
  97. return lDeviceID;
  98. CMultDevices dlgChooseDevice(pWnd, this);
  99. if (IDOK == dlgChooseDevice.DoModal())
  100. return errIRFTP_MULTDEVICES; //OnOK will fill in the required data structures
  101. else
  102. return errIRFTP_SELECTIONCANCELLED; //return -1 if cancel was chosen
  103. }
  104. ULONG CDeviceList::GetDeviceID(int iDevIndex)
  105. {
  106. if (iDevIndex < m_lNumDevices) //sanity checks
  107. return m_pDeviceInfo[iDevIndex].DeviceSpecific.s.Irda.DeviceId;
  108. else
  109. return -1;
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////////
  112. //this function should only be called after a critical section has been
  113. //obtained.
  114. void CDeviceList::GetDeviceName(LONG iDevID, TCHAR* lpszDevName)
  115. {
  116. int len;
  117. int i;
  118. lpszDevName[0] = '\0'; //better be safe
  119. //EnterCriticalSection(&m_criticalSection);
  120. for(i = 0; i < m_lNumDevices; i++)
  121. {
  122. if(iDevID == (LONG)m_pDeviceInfo[i].DeviceSpecific.s.Irda.DeviceId)
  123. break;
  124. }
  125. if(i == m_lNumDevices) //the device was not found
  126. lstrcpy (lpszDevName, TEXT("???"));
  127. else
  128. {
  129. wcscpy(lpszDevName, m_pDeviceInfo[i].DeviceName);
  130. }
  131. //LeaveCriticalSection(&m_criticalSection);
  132. }
  133. ///////////////////////////////////////////////////////////////////////////////////
  134. //this function should only be called after a critical section has been
  135. //obtained.
  136. BOOL
  137. CDeviceList::GetDeviceType(
  138. LONG iDevID,
  139. OBEX_DEVICE_TYPE *Type
  140. )
  141. {
  142. int i;
  143. BOOL bResult;
  144. *Type=TYPE_UNKNOWN;
  145. EnterCriticalSection(&m_criticalSection);
  146. for(i = 0; i < m_lNumDevices; i++)
  147. {
  148. if (m_pDeviceInfo[i].DeviceType == TYPE_IRDA) {
  149. if (iDevID == (LONG)m_pDeviceInfo[i].DeviceSpecific.s.Irda.DeviceId) {
  150. *Type=m_pDeviceInfo[i].DeviceType;
  151. break;
  152. }
  153. } else {
  154. if (iDevID == (LONG)m_pDeviceInfo[i].DeviceSpecific.s.Ip.IpAddress) {
  155. *Type=m_pDeviceInfo[i].DeviceType;
  156. break;
  157. }
  158. }
  159. }
  160. if (i == m_lNumDevices) {
  161. //
  162. // the device was not found
  163. //
  164. bResult=FALSE;
  165. } else {
  166. bResult=TRUE;
  167. }
  168. LeaveCriticalSection(&m_criticalSection);
  169. return bResult;
  170. }