saddr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /****************************************************************************
  2. Filename: saddr.c
  3. Revised: $Date: 2009-12-10 08:32:15 -0800 (Thu, 10 Dec 2009) $
  4. Revision: $Revision: 21311 $
  5. Description: Zigbee and 802.15.4 device address utility functions.
  6. Copyright 2005-2010 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * INCLUDES
  35. */
  36. #include "hal_types.h"
  37. #include "OSAL.h"
  38. #include "saddr.h"
  39. /****************************************************************************
  40. * @fn sAddrCmp
  41. *
  42. * @brief Compare two device addresses.
  43. *
  44. * input parameters
  45. *
  46. * @param pAddr1 - Pointer to first address.
  47. * @param pAddr2 - Pointer to second address.
  48. *
  49. * output parameters
  50. *
  51. * @return TRUE if addresses are equal, FALSE otherwise
  52. */
  53. bool sAddrCmp(const sAddr_t *pAddr1, const sAddr_t *pAddr2)
  54. {
  55. if (pAddr1->addrMode != pAddr2->addrMode)
  56. {
  57. return FALSE;
  58. }
  59. else if (pAddr1->addrMode == SADDR_MODE_NONE)
  60. {
  61. return FALSE;
  62. }
  63. else if (pAddr1->addrMode == SADDR_MODE_SHORT)
  64. {
  65. return (bool) (pAddr1->addr.shortAddr == pAddr2->addr.shortAddr);
  66. }
  67. else if (pAddr1->addrMode == SADDR_MODE_EXT)
  68. {
  69. return (sAddrExtCmp(pAddr1->addr.extAddr, pAddr2->addr.extAddr));
  70. }
  71. else
  72. {
  73. return FALSE;
  74. }
  75. }
  76. /****************************************************************************
  77. * @fn sAddrIden
  78. *
  79. * @brief Check if two device addresses are identical.
  80. *
  81. * This routine is virtually the same as sAddrCmp, which is used
  82. * to determine if two different addresses are the same. However,
  83. * this routine can be used to determine if an address is the
  84. * same as a previously stored address. The key difference is in
  85. * the former case, if the address mode is "none", then the
  86. * assumption is that the two addresses can not be the same. But
  87. * in the latter case, the address mode itself is being compared.
  88. * So two addresses can be identical even if the address mode is
  89. * "none", as long as the address mode of both addresses being
  90. * compared is "none".
  91. *
  92. * input parameters
  93. *
  94. * @param pAddr1 - Pointer to first address.
  95. * @param pAddr2 - Pointer to second address.
  96. *
  97. * output parameters
  98. *
  99. * @return TRUE if addresses are identical, FALSE otherwise
  100. */
  101. bool sAddrIden(const sAddr_t *pAddr1, const sAddr_t *pAddr2)
  102. {
  103. // first check if the address modes are the same
  104. if (pAddr1->addrMode != pAddr2->addrMode)
  105. {
  106. // no, so no point in comparing any further
  107. return FALSE;
  108. }
  109. // the address modes are the same; check if there is no address
  110. else if (pAddr1->addrMode == SADDR_MODE_NONE)
  111. {
  112. // no address, so no need to compare any further as both addresses have the
  113. // same address mode but no address, so they are identical
  114. return TRUE;
  115. }
  116. // there's an address; check if it is short
  117. else if (pAddr1->addrMode == SADDR_MODE_SHORT)
  118. {
  119. // compare short addresses
  120. return (bool) (pAddr1->addr.shortAddr == pAddr2->addr.shortAddr);
  121. }
  122. // there's an address; check if it is extended
  123. else if (pAddr1->addrMode == SADDR_MODE_EXT)
  124. {
  125. // compare extended addresses
  126. return (sAddrExtCmp(pAddr1->addr.extAddr, pAddr2->addr.extAddr));
  127. }
  128. else // unknown error
  129. {
  130. return FALSE;
  131. }
  132. }
  133. /****************************************************************************
  134. * @fn sAddrCpy
  135. *
  136. * @brief Copy a device address.
  137. *
  138. * input parameters
  139. *
  140. * @param pSrc - Pointer to address to copy.
  141. *
  142. * output parameters
  143. *
  144. * @param pDest - Pointer to address of copy.
  145. *
  146. * @return None.
  147. */
  148. void sAddrCpy(sAddr_t *pDest, const sAddr_t *pSrc)
  149. {
  150. pDest->addrMode = pSrc->addrMode;
  151. if (pDest->addrMode == SADDR_MODE_EXT)
  152. {
  153. sAddrExtCpy(pDest->addr.extAddr, pSrc->addr.extAddr);
  154. }
  155. else
  156. {
  157. pDest->addr.shortAddr = pSrc->addr.shortAddr;
  158. }
  159. }
  160. /****************************************************************************
  161. * @fn sAddrExtCmp
  162. *
  163. * @brief Compare two extended addresses.
  164. *
  165. * input parameters
  166. *
  167. * @param pAddr1 - Pointer to first address.
  168. * @param pAddr2 - Pointer to second address.
  169. *
  170. * output parameters
  171. *
  172. * @return TRUE if addresses are equal, FALSE otherwise
  173. */
  174. bool sAddrExtCmp(const uint8 * pAddr1, const uint8 * pAddr2)
  175. {
  176. uint8 i;
  177. for (i = SADDR_EXT_LEN; i != 0; i--)
  178. {
  179. if (*pAddr1++ != *pAddr2++)
  180. {
  181. return FALSE;
  182. }
  183. }
  184. return TRUE;
  185. }
  186. /****************************************************************************
  187. * @fn sAddrExtCpy
  188. *
  189. * @brief Copy an extended address.
  190. *
  191. * input parameters
  192. *
  193. * @param pSrc - Pointer to address to copy.
  194. *
  195. * output parameters
  196. *
  197. * @param pDest - Pointer to address of copy.
  198. *
  199. * @return pDest + SADDR_EXT_LEN.
  200. */
  201. void *sAddrExtCpy(uint8 * pDest, const uint8 * pSrc)
  202. {
  203. return osal_memcpy(pDest, pSrc, SADDR_EXT_LEN);
  204. }