antplus_common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2012
  6. All rights reserved.
  7. */
  8. #pragma once
  9. #define COMMON_REV 1.1 // Device Profile Revision Number
  10. public ref class CommonData
  11. {
  12. public:
  13. static const UCHAR START_COMMON_PAGE = 0x40;
  14. static const UCHAR PAGE70 = 70;
  15. static const UCHAR PAGE80 = 80;
  16. static const UCHAR PAGE81 = 81;
  17. static const UCHAR PAGE82 = 82;
  18. static const UCHAR PAGE83 = 83;
  19. static const UCHAR PAGE85 = 85;
  20. static const UCHAR PAGE86 = 86;
  21. static const UCHAR RESERVED = 0xFF;
  22. static const USHORT BATTERY_VOLTAGE_INVALID = 0xFFF;
  23. static const ULONG MAX_2SEC = (ULONG) 0xFFFFFF << 1;
  24. static const ULONG MAX_16SEC = (ULONG) 0xFFFFFF << 4;
  25. static const ULONG OPERATING_TIME_MASK = 0x00FFFFFF;
  26. static const UCHAR COMMAND_TYPE_DATA = 0x01;
  27. static const UCHAR COMMAND_TYPE_FS = 0x02;
  28. enum class BatStatus : UCHAR // Battery status definitions
  29. {
  30. NEW = 1,
  31. GOOD = 2,
  32. OK = 3,
  33. LOW = 4,
  34. CRITICAL = 5,
  35. INVALID = 7
  36. };
  37. enum class TimeResolution : UCHAR // Time resolution definitions
  38. {
  39. TWO = 2, // Time in interval of 2 seconds, up to 1.1 years rollover
  40. SIXTEEN = 16 // Time in interval of 16 seconds, up to 8.5 years rollover
  41. };
  42. enum class DayOfWeek : UCHAR // defines the day of the week
  43. {
  44. SUNDAY = 0,
  45. MONDAY,
  46. TUESDAY,
  47. WEDNESDAY,
  48. THURSDAY,
  49. FRIDAY,
  50. SATURDAY,
  51. INVALID
  52. };
  53. enum class TotalSizeUnits : UCHAR
  54. {
  55. BIT = 0x00,
  56. BYTE = 0x80,
  57. BASE_UNIT = 0x08,
  58. KILO = 0x01,
  59. MEGA = 0x02,
  60. TERA = 0x03,
  61. };
  62. enum class PairingStates : UCHAR
  63. {
  64. PAIRED = 0x00,
  65. UNPAIRED = 0x80,
  66. };
  67. enum class ConnectionStates : UCHAR
  68. {
  69. CLOSED = 0x00,
  70. SEARCHING = 0x08,
  71. TRACKING = 0x10,
  72. };
  73. enum class NetworkKeys : UCHAR
  74. {
  75. NETWORK_PUBLIC = 0x00,
  76. NETWORK_PRIVATE = 0x01,
  77. NETWORK_ANTPLUS = 0x02,
  78. NETWORK_ANTFS = 0x03,
  79. };
  80. // Error handling
  81. // Flags indicate errors causing the exception
  82. ref class Error : public System::Exception{
  83. public:
  84. BOOL bUndefPage; // Undefined page
  85. BOOL bBadReserved; // Invalid values on reserved fields
  86. BOOL bUndefBatStatus; // Undefined battery status value
  87. enum class Code : UCHAR // Error code definitions
  88. {
  89. UNDEF_COMMON_PAGE,
  90. UNDEF_BAT_STATUS,
  91. INVALID_RESERVED
  92. };
  93. Error()
  94. {
  95. ClearFlags();
  96. }
  97. Error(Code eCode1_)
  98. {
  99. ClearFlags();
  100. SetFlags(eCode1_);
  101. }
  102. Error(Code eCode1_, Code eCode2_)
  103. {
  104. ClearFlags();
  105. SetFlags(eCode1_);
  106. SetFlags(eCode2_);
  107. }
  108. private:
  109. void ClearFlags()
  110. {
  111. bUndefPage = FALSE;
  112. bBadReserved = FALSE;
  113. bUndefBatStatus = FALSE;
  114. }
  115. void SetFlags(Code eCode_)
  116. {
  117. switch(eCode_)
  118. {
  119. case Code::UNDEF_COMMON_PAGE:
  120. bUndefPage = TRUE;
  121. break;
  122. case Code::UNDEF_BAT_STATUS:
  123. bUndefBatStatus = TRUE;
  124. break;
  125. case Code::INVALID_RESERVED:
  126. bBadReserved = TRUE;
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. };
  133. public:
  134. //Request (page 70)
  135. UCHAR ucDescriptorByte1;
  136. UCHAR ucDescriptorByte2;
  137. UCHAR ucReqTransResp;
  138. UCHAR ucReqPageNum;
  139. UCHAR ucCommandType;
  140. //Manufacturer's Information (page 80)
  141. UCHAR ucHwVersion; // Hardware revision
  142. USHORT usMfgID; // Manufacturer ID
  143. USHORT usModelNum; // Model number
  144. // Product Information (Page 81)
  145. //Product Information (page 81)
  146. UCHAR ucSwVersion; // Software revision
  147. ULONG ulSerialNum; // Serial number
  148. // Battery Voltage (Page 82)
  149. //Battery Status (page 82)
  150. BOOL bBattPageEnabled; // variable to determine if the optional battery page is enabled
  151. ULONG ulOpTime; // Cumulative operating time since battery insertion (resolution specified by bit field)
  152. USHORT usBatVoltage256; // Battery voltage (1/256 V)
  153. BatStatus eBatStatus; // Battery status
  154. //Time and Date (page 83)
  155. TimeResolution eTimeResolution; // Resolution of cumulative operating time
  156. // Common Time Page (Page 83)
  157. BOOL bTimePageEnabled; // determines if this page is to be sent
  158. UCHAR ucSeconds;
  159. UCHAR ucMinutes;
  160. UCHAR ucHours;
  161. UCHAR ucDays;
  162. UCHAR ucMonth;
  163. UCHAR ucYears;
  164. DayOfWeek eDayOfWeek;
  165. //Memory Level (page 85)
  166. UCHAR ucPercentUsed;
  167. UCHAR ucTotalSizeExponent;
  168. UCHAR ucTotalSizeUnit;
  169. //Paired Devices (page 86)
  170. UCHAR ucPeripheralDeviceIndex;
  171. UCHAR ucTotalConnectedDevices;
  172. PairingStates ePairingState;
  173. ConnectionStates eConnectionState;
  174. NetworkKeys eNetworkKey;
  175. ULONG ulDeviceChannelId;
  176. // Error handling
  177. BOOL bValidation; // Turns validation on/off
  178. public:
  179. CommonData()
  180. {
  181. bValidation = FALSE;
  182. }
  183. ~CommonData()
  184. {
  185. }
  186. public:
  187. /**************************************************************************
  188. * CommonData::Encode
  189. *
  190. * Encodes common data
  191. * Exceptions are thrown when dealing with invalid data
  192. *
  193. * ucPageNum_: number of page to encode
  194. * pucTxBuffer_: pointer to the buffer that will store the encoded data
  195. *
  196. * returns: N/A
  197. *
  198. **************************************************************************/
  199. void Encode(UCHAR ucPageNum_, UCHAR* pucTxBuffer_)
  200. {
  201. switch(ucPageNum_)
  202. {
  203. case PAGE70:
  204. pucTxBuffer_[0] = ucPageNum_; //Page Number (70)
  205. pucTxBuffer_[1] = RESERVED;
  206. pucTxBuffer_[2] = RESERVED;
  207. pucTxBuffer_[3] = ucDescriptorByte1; //First descriptor byte
  208. pucTxBuffer_[4] = ucDescriptorByte2; //Second descriptor byte
  209. pucTxBuffer_[5] = ucReqTransResp; //The requested transmission response
  210. pucTxBuffer_[6] = ucReqPageNum; //Request page number
  211. pucTxBuffer_[7] = ucCommandType; //Command type (Data Page or ANTFS Session)
  212. break;
  213. case PAGE80:
  214. pucTxBuffer_[0] = ucPageNum_; // Page number
  215. pucTxBuffer_[1] = RESERVED;
  216. pucTxBuffer_[2] = RESERVED;
  217. pucTxBuffer_[3] = ucHwVersion; // Hw revision
  218. pucTxBuffer_[4] = usMfgID & 0xFF; // Low byte of Mfg ID
  219. pucTxBuffer_[5] = (usMfgID >> 8) & 0xFF; // High byte of Mfg ID
  220. pucTxBuffer_[6] = usModelNum & 0xFF; // Low byte of Model Number
  221. pucTxBuffer_[7] = (usModelNum >> 8) & 0xFF; // High byte of Model Number
  222. break;
  223. case PAGE81:
  224. pucTxBuffer_[0] = ucPageNum_; // Page number
  225. pucTxBuffer_[1] = RESERVED;
  226. pucTxBuffer_[2] = RESERVED;
  227. pucTxBuffer_[3] = ucSwVersion; // Sw revision
  228. pucTxBuffer_[4] = ulSerialNum & 0xFF; // Serial number (bits 0-7)
  229. pucTxBuffer_[5] = (ulSerialNum >> 8) & 0xFF; // Serial number (bits 8-15)
  230. pucTxBuffer_[6] = (ulSerialNum >> 16) & 0xFF; // Serial number (bits 16-23)
  231. pucTxBuffer_[7] = (ulSerialNum >> 24) & 0xFF; // Serial number (bits 24-31)
  232. break;
  233. case PAGE82:
  234. pucTxBuffer_[0] = ucPageNum_; // Page number
  235. pucTxBuffer_[1] = RESERVED;
  236. pucTxBuffer_[2] = RESERVED;
  237. pucTxBuffer_[3] = ulOpTime & 0xFF; // Operating time (bits 0-7)
  238. pucTxBuffer_[4] = (ulOpTime >> 8) & 0xFF; // Operating time (bits 8-15)
  239. pucTxBuffer_[5] = (ulOpTime >> 16) & 0xFF; // Operating time (bits 16-23)
  240. pucTxBuffer_[6] = usBatVoltage256 & 0XFF; // Fractional battery voltage (1/256V)
  241. pucTxBuffer_[7] = (usBatVoltage256 >> 8) & 0x0F;// Coarse battery voltage (V) in bits 0-3 of bit field
  242. pucTxBuffer_[7] |= ((UCHAR)eBatStatus << 4) & 0x70;// Battery status in bits 4-6 of bit field
  243. if(eTimeResolution == TimeResolution::TWO)
  244. pucTxBuffer_[7] |= 0x80; // If using two second resolution, set msb
  245. break;
  246. case PAGE83:
  247. pucTxBuffer_[0] = ucPageNum_; // Page number
  248. pucTxBuffer_[1] = RESERVED;
  249. pucTxBuffer_[2] = ucSeconds;
  250. pucTxBuffer_[3] = ucMinutes;
  251. pucTxBuffer_[4] = ucHours;
  252. pucTxBuffer_[5] = ((UCHAR) eDayOfWeek << 5) & 0xE0; // day of week (bits 7-5)
  253. pucTxBuffer_[5] |= ucDays & 0x1F; // the & ensures clean bits
  254. pucTxBuffer_[6] = ucMonth;
  255. pucTxBuffer_[7] = ucYears;
  256. break;
  257. case PAGE85:
  258. pucTxBuffer_[0] = ucPageNum_;
  259. pucTxBuffer_[1] = RESERVED;
  260. pucTxBuffer_[2] = RESERVED;
  261. pucTxBuffer_[3] = RESERVED;
  262. pucTxBuffer_[4] = RESERVED;
  263. pucTxBuffer_[5] = ucPercentUsed;
  264. pucTxBuffer_[6] = ucTotalSizeExponent;
  265. pucTxBuffer_[7] = ucTotalSizeUnit;
  266. break;
  267. case PAGE86:
  268. pucTxBuffer_[0] = ucPageNum_;
  269. pucTxBuffer_[1] = ucPeripheralDeviceIndex;
  270. pucTxBuffer_[2] = ucTotalConnectedDevices;
  271. pucTxBuffer_[3] = (UCHAR)ePairingState;
  272. pucTxBuffer_[3] |= (UCHAR) eConnectionState;
  273. pucTxBuffer_[3] |= (UCHAR)eNetworkKey;
  274. pucTxBuffer_[4] = ulDeviceChannelId & 0xFF;
  275. pucTxBuffer_[5] = (ulDeviceChannelId >> 8) & 0xFF;
  276. pucTxBuffer_[6] = (ulDeviceChannelId >> 16) & 0xFF;
  277. pucTxBuffer_[7] = (ulDeviceChannelId >> 24) & 0xFF;
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. /**************************************************************************
  284. * CommonData::Decode
  285. *
  286. * Decodes received Common Data Pages
  287. * Exceptions are thrown when dealing with invalid data
  288. *
  289. * pucRxBuffer_: pointer to the buffer containing the received data
  290. *
  291. * returns: N/A
  292. *
  293. **************************************************************************/
  294. void Decode(UCHAR* pucRxBuffer_)
  295. {
  296. BOOL bUndefBatStatus = FALSE;
  297. switch(pucRxBuffer_[0])
  298. {
  299. case PAGE70:
  300. ucDescriptorByte1 = pucRxBuffer_[3];
  301. ucDescriptorByte2 = pucRxBuffer_[4];
  302. ucReqTransResp = pucRxBuffer_[5];
  303. ucReqPageNum = pucRxBuffer_[6];
  304. ucCommandType = pucRxBuffer_[7];
  305. break;
  306. case PAGE80:
  307. ucHwVersion = pucRxBuffer_[3]; // Hw revision
  308. usMfgID = pucRxBuffer_[4] | pucRxBuffer_[5] <<8; // Mfg ID
  309. usModelNum = pucRxBuffer_[6] | pucRxBuffer_[7] <<8; // Model number
  310. break;
  311. case PAGE81:
  312. ucSwVersion = pucRxBuffer_[3]; // Sw revision
  313. ulSerialNum = pucRxBuffer_[4] | pucRxBuffer_[5]<<8 | pucRxBuffer_[6]<<16 | pucRxBuffer_[7]<<24; // Serial number
  314. break;
  315. case PAGE82:
  316. ulOpTime = pucRxBuffer_[3] | pucRxBuffer_[4] << 8 | pucRxBuffer_[5] << 16; // Operating time
  317. usBatVoltage256 = pucRxBuffer_[6] | (pucRxBuffer_[7] & 0x0F) << 8; // Battery voltage
  318. if((pucRxBuffer_[7] & 0x70) == 0 || (pucRxBuffer_[7] & 0x70) == 0x60)
  319. bUndefBatStatus = TRUE; // Undefined battery status
  320. eBatStatus = (BatStatus) ((pucRxBuffer_[7] & 0x70) >> 4); // Battery status
  321. if(pucRxBuffer_[7] & 0x80) // Time resolution
  322. eTimeResolution = TimeResolution::TWO;
  323. else
  324. eTimeResolution = TimeResolution::SIXTEEN;
  325. break;
  326. case PAGE83:
  327. ucSeconds = pucRxBuffer_[2];
  328. ucMinutes = pucRxBuffer_[3];
  329. ucHours = pucRxBuffer_[4];
  330. eDayOfWeek = (DayOfWeek) ((pucRxBuffer_[5] & 0xE0) >> 5);
  331. ucDays = pucRxBuffer_[5] & 0x1F;
  332. ucMonth = pucRxBuffer_[6];
  333. ucYears = pucRxBuffer_[7];
  334. break;
  335. case PAGE85:
  336. ucPercentUsed = pucRxBuffer_[5];
  337. ucTotalSizeExponent = pucRxBuffer_[6];
  338. ucTotalSizeUnit = pucRxBuffer_[7];
  339. break;
  340. case PAGE86:
  341. ucPeripheralDeviceIndex = pucRxBuffer_[1];
  342. ucTotalConnectedDevices = pucRxBuffer_[2];
  343. ePairingState = (PairingStates)(pucRxBuffer_[3] & 0x80);
  344. eConnectionState = (ConnectionStates)(pucRxBuffer_[3] & 0x78);
  345. eNetworkKey = (NetworkKeys)(pucRxBuffer_[3] & 0x07);
  346. ulDeviceChannelId = pucRxBuffer_[4] | (pucRxBuffer_[5] << 8) | (pucRxBuffer_[6] << 16) | (pucRxBuffer_[7] << 24);
  347. default:
  348. break;
  349. }
  350. }
  351. /**************************************************************************
  352. * CommonData::IsBatteryVoltageInvalid
  353. *
  354. * Checks whether the battery voltage is set as invalid
  355. *
  356. * usBatVoltage256_: battery voltage (1/256 V)
  357. *
  358. * returns: N/A
  359. *
  360. **************************************************************************/
  361. BOOL IsBatteryVoltageInvalid(USHORT usBatVoltage256_)
  362. {
  363. if((usBatVoltage256 >> 8) == BATTERY_VOLTAGE_INVALID)
  364. return TRUE;
  365. else
  366. return FALSE;
  367. }
  368. /**************************************************************************
  369. * CommonData::IsBatteryStatusInvalid
  370. *
  371. * Checks whether the battery status is set as invalid
  372. *
  373. * eBatStatus_: battery status code
  374. *
  375. * returns: N/A
  376. *
  377. **************************************************************************/
  378. BOOL IsBatteryStatusInvalid(BatStatus eBatStatus_)
  379. {
  380. if(eBatStatus_ == BatStatus::INVALID)
  381. return TRUE;
  382. else
  383. return FALSE;
  384. }
  385. /**************************************************************************
  386. * CommonData::IsDayofWeekValid
  387. *
  388. * Checks whether the battery status is set as invalid
  389. *
  390. * eBatStatus_: battery status code
  391. *
  392. * returns: N/A
  393. *
  394. **************************************************************************/
  395. BOOL IsDayOfWeekValid(DayOfWeek eDay_)
  396. {
  397. if (eDay_ == DayOfWeek::INVALID)
  398. return FALSE;
  399. else
  400. return TRUE;
  401. }
  402. };