ble_6lowpan.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /**
  2. * Copyright (c) 2013 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup ble_sdk_6lowpan 6LoWPAN Adaptation Layer
  43. * @{
  44. * @ingroup ble_sdk_iot
  45. * @brief 6LoWPAN Adaptation Layer
  46. *
  47. * @details This module enables 6LoWPAN over Bluetooth Low Energy.
  48. *
  49. */
  50. #include <stdbool.h>
  51. #include <stdint.h>
  52. #include <string.h>
  53. #include "nrf_soc.h"
  54. #include "nordic_common.h"
  55. #include "ble_ipsp.h"
  56. #include "ble_6lowpan.h"
  57. #include "iot_common.h"
  58. #include "iot_context_manager.h"
  59. #include "app_util_platform.h"
  60. #include "mem_manager.h"
  61. /**
  62. * @defgroup ble_sdk_6lowpan Module's Log Macros
  63. * @details Macros used for creating module logs which can be useful in understanding handling
  64. * of events or actions on API requests. These are intended for debugging purposes and
  65. * can be enabled by defining the IOT_BLE_6LOWPAN_CONFIG_LOG_ENABLED.
  66. * @note If NRF_LOG_ENABLED is disabled, having IOT_BLE_6LOWPAN_CONFIG_LOG_ENABLED
  67. * has no effect.
  68. * @{
  69. */
  70. #if IOT_BLE_6LOWPAN_CONFIG_LOG_ENABLED
  71. #define NRF_LOG_MODULE_NAME 6lowpan
  72. #define NRF_LOG_LEVEL IOT_BLE_6LOWPAN_CONFIG_LOG_LEVEL
  73. #define NRF_LOG_INFO_COLOR IOT_BLE_6LOWPAN_CONFIG_INFO_COLOR
  74. #define NRF_LOG_DEBUG_COLOR IOT_BLE_6LOWPAN_CONFIG_DEBUG_COLOR
  75. #include "nrf_log.h"
  76. NRF_LOG_MODULE_REGISTER();
  77. #define BLE_6LOWPAN_TRC NRF_LOG_DEBUG /**< Used for getting trace of execution in the module. */
  78. #define BLE_6LOWPAN_ERR NRF_LOG_ERROR /**< Used for logging errors in the module. */
  79. #define BLE_6LOWPAN_DUMP NRF_LOG_HEXDUMP_DEBUG /**< Used for dumping octet information to get details of bond information etc. */
  80. #define BLE_6LOWPAN_ENTRY() BLE_6LOWPAN_TRC(">> %s", __func__)
  81. #define BLE_6LOWPAN_EXIT() BLE_6LOWPAN_TRC("<< %s", __func__)
  82. #else // IOT_BLE_6LOWPAN_CONFIG_LOG_ENABLED
  83. #define BLE_6LOWPAN_TRC(...) /**< Disables traces. */
  84. #define BLE_6LOWPAN_DUMP(...) /**< Disables dumping of octet streams. */
  85. #define BLE_6LOWPAN_ERR(...) /**< Disables error logs. */
  86. #define BLE_6LOWPAN_ENTRY(...)
  87. #define BLE_6LOWPAN_EXIT(...)
  88. #endif // IOT_BLE_6LOWPAN_CONFIG_LOG_ENABLED
  89. /** @} */
  90. /**
  91. * @defgroup ble_6lowpan_mutex_lock_unlock Module's Mutex Lock/Unlock Macros.
  92. *
  93. * @details Macros used to lock and unlock modules. Currently, SDK does not use mutexes but
  94. * framework is provided in case need arises to use an alternative architecture.
  95. * @{
  96. */
  97. #define BLE_6LOWPAN_MUTEX_LOCK() SDK_MUTEX_LOCK(m_6lowpan_mutex) /**< Lock module using mutex */
  98. #define BLE_6LOWPAN_MUTEX_UNLOCK() SDK_MUTEX_UNLOCK(m_6lowpan_mutex) /**< Unlock module using mutex */
  99. /** @} */
  100. /**
  101. * @defgroup api_param_check API Parameters check macros.
  102. *
  103. * @details Macros that verify parameters passed to the module in the APIs. These macros
  104. * could be mapped to nothing in final versions of code to save execution and size.
  105. * BLE_6LOWPAN_DISABLE_API_PARAM_CHECK should be set to 0 to enable these checks.
  106. *
  107. * @{
  108. */
  109. #if (BLE_6LOWPAN_DISABLE_API_PARAM_CHECK == 0)
  110. /**@brief Macro to check is module is initialized before requesting one of the module procedures. */
  111. #define VERIFY_MODULE_IS_INITIALIZED() \
  112. if (m_event_handler == NULL) \
  113. { \
  114. return (SDK_ERR_MODULE_NOT_INITIALIZED | BLE_6LOWPAN_ERR_BASE); \
  115. }
  116. /**@brief Verify NULL parameters are not passed to API by application. */
  117. #define NULL_PARAM_CHECK(PARAM) \
  118. if ((PARAM) == NULL) \
  119. { \
  120. return (NRF_ERROR_NULL | BLE_6LOWPAN_ERR_BASE); \
  121. }
  122. /**@brief Check if packet has at least IP Header in it (40 bytes). */
  123. #define PACKET_LENGTH_CHECK(PARAM) \
  124. if ((PARAM) < IPV6_IP_HEADER_SIZE) \
  125. { \
  126. return (NRF_ERROR_INVALID_PARAM | BLE_6LOWPAN_ERR_BASE); \
  127. }
  128. #else // BLE_6LOWPAN_DISABLE_API_PARAM_CHECK
  129. #define VERIFY_MODULE_IS_INITIALIZED()
  130. #define NULL_PARAM_CHECK(PARAM)
  131. #define PACKET_LENGTH_CHECK(PARAM)
  132. #endif // BLE_6LOWPAN_DISABLE_API_PARAM_CHECK
  133. /** @} */
  134. /**@brief Maximum different between compressed and uncompressed packet. */
  135. #define IPHC_MAX_COMPRESSED_DIFF (IPV6_IP_HEADER_SIZE + UDP_HEADER_SIZE - 7)
  136. /**@brief Transmit FIFO mask. */
  137. #define TX_FIFO_MASK (BLE_6LOWPAN_TX_FIFO_SIZE - 1)
  138. /**@brief Value and position of IPHC dispatch. */
  139. #define IPHC_START_DISPATCH 0x03
  140. #define IPHC_START_DISPATCH_POS 5
  141. /**@brief Values and positions of IPHC fields. */
  142. #define IPHC_TF_MASK 0x18
  143. #define IPHC_TF_POS 3
  144. #define IPHC_NH_MASK 0x04
  145. #define IPHC_NH_POS 2
  146. #define IPHC_HLIM_MASK 0x03
  147. #define IPHC_HLIM_POS 0
  148. #define IPHC_CID_MASK 0x80
  149. #define IPHC_CID_POS 7
  150. #define IPHC_SAC_MASK 0x40
  151. #define IPHC_SAC_POS 6
  152. #define IPHC_SAM_MASK 0x30
  153. #define IPHC_SAM_POS 4
  154. #define IPHC_M_MASK 0x08
  155. #define IPHC_M_POS 3
  156. #define IPHC_DAC_MASK 0x04
  157. #define IPHC_DAC_POS 2
  158. #define IPHC_DAM_MASK 0x03
  159. #define IPHC_DAM_POS 0
  160. /**@brief IPHC Traffic Flow compression. */
  161. #define IPHC_TF_DSCP_MASK 0x3F
  162. #define IPHC_TF_ECN_MASK 0xC0
  163. #define IPHC_TF_ECN_POS 6
  164. /**@brief IPHC values of fields. */
  165. #define IPHC_TF_00 0x00
  166. #define IPHC_TF_01 0x01
  167. #define IPHC_TF_10 0x02
  168. #define IPHC_TF_11 0x03
  169. #define IPHC_NH_0 0x00
  170. #define IPHC_NH_1 0x01
  171. #define IPHC_HLIM_00 0x00
  172. #define IPHC_HLIM_01 0x01
  173. #define IPHC_HLIM_10 0x02
  174. #define IPHC_HLIM_11 0x03
  175. #define IPHC_CID_0 0x00
  176. #define IPHC_CID_1 0x01
  177. #define IPHC_SAC_0 0x00
  178. #define IPHC_SAC_1 0x01
  179. #define IPHC_SAM_00 0x00
  180. #define IPHC_SAM_01 0x01
  181. #define IPHC_SAM_10 0x02
  182. #define IPHC_SAM_11 0x03
  183. #define IPHC_M_0 0x00
  184. #define IPHC_M_1 0x01
  185. #define IPHC_DAC_0 0x00
  186. #define IPHC_DAC_1 0x01
  187. #define IPHC_DAM_00 0x00
  188. #define IPHC_DAM_01 0x01
  189. #define IPHC_DAM_10 0x02
  190. #define IPHC_DAM_11 0x03
  191. /**@brief IPHC Context Identifier compression. */
  192. #define IPHC_CID_SOURCE_MASK 0xF0
  193. #define IPHC_CID_SOURCE_POS 4
  194. #define IPHC_CID_DESTINATION_MASK 0x0F
  195. #define IPHC_CID_DESTINATION_POS 0
  196. /**@brief IPHC Next Header Compression dispatches. */
  197. #define IPHC_NHC_UDP_DISPATCH 0xF0
  198. #define IPHC_NHC_UDP_MASK 0xF8
  199. #define IPHC_NHC_EXT_DISPATCH 0xE0
  200. #define IPHC_NHC_EXT_MASK 0xF0
  201. /**@brief IPHC Next Header Compression UDP fields. */
  202. #define IPHC_NHC_UDP_CSUM_MASK 0x04
  203. #define IPHC_NHC_UDP_CSUM_POS 0x02
  204. #define IPHC_NHC_UDP_PORTS_MASK 0x03
  205. #define IPHC_NHC_UDP_PORTS_POS 0x00
  206. #define IPHC_NHC_UDP_PORTS_00 0x00
  207. #define IPHC_NHC_UDP_PORTS_01 0x01
  208. #define IPHC_NHC_UDP_PORTS_10 0x02
  209. #define IPHC_NHC_UDP_PORTS_11 0x03
  210. #define IPHC_NHC_UDP_COMPRESSION_MAX_MASK 0xFFF0
  211. #define IPHC_NHC_UDP_COMPRESSION_MAX 0xF0B0
  212. #define IPHC_NHC_UDP_COMPRESSION_MIN_MASK 0xFF00
  213. #define IPHC_NHC_UDP_COMPRESSION_MIN 0xF000
  214. /**@brief IPHC Next Header Compression Extended Header fields. */
  215. #define IPHC_NHC_EXT_EID_MASK 0x0E
  216. #define IPHC_NHC_EXT_EID_POS 0x01
  217. #define IPHC_NHC_EXT_EID_HOP_BY_HOP 0x00
  218. #define IPHC_NHC_EXT_EID_ROUTING 0x01
  219. #define IPHC_NHC_EXT_EID_FRAGMENT 0x02
  220. #define IPHC_NHC_EXT_EID_DESTINATION 0x03
  221. #define IPHC_NHC_EXT_EID_MOBILITY 0x04
  222. #define IPHC_NHC_EXT_EID_IPV6 0x07
  223. /**@brief IPHC default value of IPv6 Header fields. */
  224. #define IPHC_IPHEADER_VER_TC 0x60
  225. #define IPHC_IPHEADER_TC_FL 0x00
  226. #define IPHC_IPHEADER_FL 0x00
  227. /**@brief Check if address can be fully elidable. */
  228. #define IPV6_ADDRESS_IS_FULLY_ELIDABLE(ll_addr, addr) \
  229. (((addr)->u8[8] == (((ll_addr[0]) ^ IPV6_IID_FLIP_VALUE))) && \
  230. ((addr)->u8[9] == ll_addr[1]) && \
  231. ((addr)->u8[10] == ll_addr[2]) && \
  232. ((addr)->u8[11] == ll_addr[3]) && \
  233. ((addr)->u8[12] == ll_addr[4]) && \
  234. ((addr)->u8[11] == 0xff) && \
  235. ((addr)->u8[12] == 0xfe) && \
  236. ((addr)->u8[13] == ll_addr[5]) && \
  237. ((addr)->u8[14] == ll_addr[6]) && \
  238. ((addr)->u8[15] == ll_addr[7]) \
  239. )
  240. /**@brief Check if address is 16-bit and can be compressed.
  241. * 16-bit COMPRESSABLE format: ::0000:00ff:fe00:XXXX.
  242. */
  243. #define IPV6_ADDRESS_IS_16_BIT_COMPRESSABLE(addr) \
  244. (((addr)->u8[8] == 0) && \
  245. ((addr)->u8[9] == 0) && \
  246. ((addr)->u8[10] == 0) && \
  247. ((addr)->u8[11] == 0xff) && \
  248. ((addr)->u8[12] == 0xfe) && \
  249. ((addr)->u8[13] == 0) \
  250. )
  251. /**@brief Check if address is 48-bit multi-cast and can be compressed.
  252. * 48-bit COMPRESSABLE format: FFXX::00XX:XXXX:XXXX.
  253. */
  254. #define IPV6_ADDRESS_IS_48_BIT_MCAST_COMPRESSABLE(addr) \
  255. (((addr)->u16[1] == 0) && \
  256. ((addr)->u16[2] == 0) && \
  257. ((addr)->u16[3] == 0) && \
  258. ((addr)->u16[4] == 0) && \
  259. ((addr)->u8[10] == 0) \
  260. )
  261. /**@brief Check if address is 32-bit multi-cast and can be compressed.
  262. * 32-bit COMPRESSABLE format: FFXX::00XX:XXXX.
  263. */
  264. #define IPV6_ADDRESS_IS_32_BIT_MCAST_COMPRESSABLE(addr) \
  265. (((addr)->u16[1] == 0) && \
  266. ((addr)->u32[1] == 0) && \
  267. ((addr)->u32[2] == 0) && \
  268. ((addr)->u8[12] == 0) \
  269. )
  270. /**@brief Check if address is 8-bit multi-cast and can be compressed.
  271. * 8-bit COMPRESSABLE format: FF02::XX.
  272. */
  273. #define IPV6_ADDRESS_IS_8_BIT_MCAST_COMPRESSABLE(addr) \
  274. (((addr)->u8[1] == 2) && \
  275. ((addr)->u16[1] == 0) && \
  276. ((addr)->u32[1] == 0) && \
  277. ((addr)->u32[2] == 0) && \
  278. ((addr)->u16[6] == 0) && \
  279. ((addr)->u8[14] == 0) \
  280. )
  281. /******************************************************************************
  282. * 6LoWPAN Core and Transport structures.
  283. ******************************************************************************/
  284. /**@brief Element of TX Queue. */
  285. typedef struct
  286. {
  287. uint8_t * p_mem_block; /**< Base address of memory block, using for release the buffer. */
  288. uint8_t * p_data; /**< Pointer to TX Data. */
  289. uint16_t data_len; /**< Size of TX data. */
  290. } tx_packet_t;
  291. /**@brief A TX Queue (FIFO) structure. */
  292. typedef struct
  293. {
  294. tx_packet_t packets[BLE_6LOWPAN_TX_FIFO_SIZE]; /**< Array of TX packet in FIFO. */
  295. volatile uint32_t read_pos; /**< Next read position in the FIFO buffer. */
  296. volatile uint32_t write_pos; /**< Next write position in the FIFO buffer. */
  297. } tx_fifo_t;
  298. /**@brief Transport instance structure. */
  299. typedef struct
  300. {
  301. iot_interface_t interface;
  302. ble_ipsp_handle_t handle;
  303. tx_fifo_t tx_fifo;
  304. tx_packet_t * p_tx_cur_packet;
  305. } transport_instance_t;
  306. /******************************************************************************
  307. * @name Global variables
  308. *****************************************************************************/
  309. /**@brief Application Event Handler. */
  310. static ble_6lowpan_evt_handler_t m_event_handler = NULL;
  311. /**@brief Hop Limit options. */
  312. static const uint8_t m_hop_limit[] = {0, 1, 64, 255};
  313. /**@brief Link-local prefix. */
  314. static const uint8_t m_link_local_prefix[] = {0xFE, 0x80};
  315. /**@brief Additional extenders for EUI-48. */
  316. static const uint8_t m_link_local_16_middle[] = {0xFF, 0xFE};
  317. /**@brief nRF EUI-64 link-layer address */
  318. static eui64_t m_ll_addr = {{0, 0, 0, 0, 0, 0, 0, 0}};
  319. /**@brief Transport interfaces table. */
  320. static transport_instance_t m_instances[BLE_6LOWPAN_MAX_INTERFACE];
  321. /**@brief Mutex variable. Currently unused, this declaration does not occupy any space in RAM. */
  322. SDK_MUTEX_DEFINE(m_6lowpan_mutex)
  323. /******************************************************************************
  324. * @name 6LoWPAN core functions
  325. *****************************************************************************/
  326. /**@brief Function for checking if IID can be completely elided. This situation
  327. * may happen when receiver can still reconstruct IPv6 address by using context
  328. * prefix and Link Layer address.
  329. *
  330. * @param[in] p_addr Pointer to IPv6 address.
  331. * @param[in] p_context Pointer to context entry that is compressed with.
  332. * @param[in] p_ll_addr Pointer to link layer address of BT-LE device.
  333. *
  334. * @return True if IID can be elided, False otherwise.
  335. */
  336. static bool is_context_cover_iid(const ipv6_addr_t * p_addr,
  337. const iot_context_t * p_context,
  338. const eui64_t * p_ll_addr)
  339. {
  340. uint32_t start_byte, offset;
  341. // Context covers IPv6 address by its size.
  342. if (p_context->prefix_len == 128)
  343. {
  344. return true;
  345. }
  346. // Check if IID can be retrieved in case of longer prefix than 64 bits.
  347. if (p_context->prefix_len > 64)
  348. {
  349. // Check only IID fields that are not covered by context prefix.
  350. start_byte = p_context->prefix_len >> 3;
  351. offset = p_context->prefix_len % 8;
  352. // Check all bytes from the second one.
  353. if (start_byte == 15 ||
  354. 0 == memcmp(&p_addr->u8[start_byte+1], &p_ll_addr->identifier[start_byte-7], 15-start_byte))
  355. {
  356. // Then check first byte.
  357. return (p_addr->u8[start_byte] << offset) == (p_ll_addr->identifier[start_byte-8] << offset);
  358. }
  359. }
  360. return false;
  361. }
  362. /**@brief Function for decoding Next Header Compression.
  363. * It supports UDP header decompression.
  364. *
  365. * @param[in] p_iphc Pointer to currently process IPHC field.
  366. * @param[in] p_data Pointer to constructing uncompressed IP packet.
  367. * @param[in] p_length Place of UDP header in case of Extension Header.
  368. * @param[out] p_length Length of the constructed uncompressed header.
  369. *
  370. * @return Number of processed IPHC field.
  371. */
  372. static uint32_t iphc_nhc_decode(uint8_t * p_iphc, uint8_t * p_data, uint16_t * p_length)
  373. {
  374. uint8_t nhc_dispatch = *p_iphc;
  375. uint8_t * p_nhc = p_iphc;
  376. ipv6_header_t * iphdr = (ipv6_header_t *)&p_data[0];
  377. // UDP Next Header Compression.
  378. if ((nhc_dispatch & IPHC_NHC_UDP_MASK) == IPHC_NHC_UDP_DISPATCH)
  379. {
  380. udp6_header_t * udphdr = (udp6_header_t * )&p_data[IPV6_IP_HEADER_SIZE + *p_length];
  381. iphdr->next_header = IPV6_NEXT_HEADER_UDP;
  382. // Start length from UDP Header Size.
  383. *p_length += UDP_HEADER_SIZE;
  384. p_nhc += 1;
  385. switch ((nhc_dispatch & IPHC_NHC_UDP_PORTS_MASK) >> IPHC_NHC_UDP_PORTS_POS)
  386. {
  387. case IPHC_NHC_UDP_PORTS_00:
  388. memcpy(&udphdr->srcport, p_nhc, 2);
  389. memcpy(&udphdr->destport, p_nhc + 2, 2);
  390. p_nhc += 4;
  391. break;
  392. case IPHC_NHC_UDP_PORTS_01:
  393. memcpy(&udphdr->srcport, p_nhc, 2);
  394. udphdr->destport = HTONS(IPHC_NHC_UDP_COMPRESSION_MIN | *(p_nhc + 2));
  395. p_nhc += 3;
  396. break;
  397. case IPHC_NHC_UDP_PORTS_10:
  398. udphdr->srcport = HTONS(IPHC_NHC_UDP_COMPRESSION_MIN | *p_nhc);
  399. memcpy(&udphdr->destport, p_nhc + 1, 2);
  400. p_nhc += 3;
  401. break;
  402. case IPHC_NHC_UDP_PORTS_11:
  403. udphdr->srcport = HTONS((IPHC_NHC_UDP_COMPRESSION_MAX | ((*p_nhc & 0xf0) >> 4)));
  404. udphdr->destport = HTONS((IPHC_NHC_UDP_COMPRESSION_MAX | ((*p_nhc & 0x0f))));
  405. p_nhc += 1;
  406. break;
  407. }
  408. if ((nhc_dispatch & IPHC_NHC_UDP_CSUM_MASK) >> IPHC_NHC_UDP_CSUM_POS)
  409. {
  410. udphdr->checksum = 0;
  411. }
  412. else
  413. {
  414. memcpy(&udphdr->checksum, p_nhc, 2);
  415. p_nhc += 2;
  416. }
  417. }
  418. return (p_nhc - p_iphc);
  419. }
  420. /**@brief Function for encoding Next Header Compression.
  421. * It supports UDP header compression.
  422. *
  423. * @param[in] p_iphc Pointer to currently process IPHC field.
  424. * @param[in] p_data Pointer to constructing uncompressed IP packet.
  425. * @param[in] p_length Place of UDP header in case of Extension Header.
  426. * @param[out] p_length Length of the constructed uncompressed header.
  427. *
  428. * @return Number of processed IPHC field.
  429. */
  430. static uint32_t iphc_nhc_encode(uint8_t * p_iphc, const uint8_t * p_data, uint16_t * p_length)
  431. {
  432. uint8_t * p_nhc = p_iphc;
  433. ipv6_header_t * iphdr = (ipv6_header_t *)p_data;
  434. switch (iphdr->next_header)
  435. {
  436. case IPV6_NEXT_HEADER_UDP:
  437. {
  438. udp6_header_t * udphdr = (udp6_header_t * )&p_data[IPV6_IP_HEADER_SIZE + *p_length];
  439. *p_iphc = IPHC_NHC_UDP_DISPATCH;
  440. p_nhc += 1;
  441. // Full 4-bit compression for source and destination ports.
  442. if ( ((HTONS(udphdr->srcport) & IPHC_NHC_UDP_COMPRESSION_MAX_MASK) ==
  443. IPHC_NHC_UDP_COMPRESSION_MAX) &&
  444. ((HTONS(udphdr->destport) & IPHC_NHC_UDP_COMPRESSION_MAX_MASK) ==
  445. IPHC_NHC_UDP_COMPRESSION_MAX))
  446. {
  447. *p_iphc |= (IPHC_NHC_UDP_PORTS_11 >> IPHC_NHC_UDP_PORTS_POS);
  448. *p_nhc =
  449. (((HTONS(udphdr->srcport) & 0x0f) << 4) | (HTONS(udphdr->destport) & 0x0f));
  450. p_nhc += 1;
  451. }
  452. // Source port compressed, destination in-line.
  453. else if ((HTONS(udphdr->srcport) & IPHC_NHC_UDP_COMPRESSION_MIN_MASK) ==
  454. IPHC_NHC_UDP_COMPRESSION_MIN)
  455. {
  456. *p_iphc |= (IPHC_NHC_UDP_PORTS_10 >> IPHC_NHC_UDP_PORTS_POS);
  457. *p_nhc = (HTONS(udphdr->srcport) & 0xff);
  458. p_nhc += 1;
  459. memcpy(p_nhc, &udphdr->destport, 2);
  460. p_nhc += 2;
  461. }
  462. // Source port in-line, destination compressed.
  463. else if ((HTONS(udphdr->destport) & IPHC_NHC_UDP_COMPRESSION_MIN_MASK) ==
  464. IPHC_NHC_UDP_COMPRESSION_MIN)
  465. {
  466. *p_iphc |= (IPHC_NHC_UDP_PORTS_01 >> IPHC_NHC_UDP_PORTS_POS);
  467. memcpy(p_nhc, &udphdr->srcport, 2);
  468. p_nhc += 2;
  469. *p_nhc = (HTONS(udphdr->destport) & 0xff);
  470. p_nhc += 1;
  471. }
  472. // Source and destination port in-line.
  473. else
  474. {
  475. *p_iphc |= (IPHC_NHC_UDP_PORTS_00 >> IPHC_NHC_UDP_PORTS_POS);
  476. memcpy(p_nhc, &udphdr->srcport, 2);
  477. memcpy(p_nhc + 2, &udphdr->destport, 2);
  478. p_nhc += 4;
  479. }
  480. // Checksum always in-line, [RFC4944] disallows the compression of the
  481. // UDP checksum. The compressor MUST NOT set the C bit unless it has received
  482. // authorization.
  483. memcpy(p_nhc, &udphdr->checksum, 2);
  484. p_nhc += 2;
  485. // Set UDP ext header size.
  486. *p_length = UDP_HEADER_SIZE;
  487. break;
  488. }
  489. }
  490. return (p_nhc - p_iphc);
  491. }
  492. /**@brief Function for checking if it's possible to use NHC.
  493. *
  494. * @param[in] next_header Value of Next Header field in IPv6 packet.
  495. *
  496. * @return Returns 1 if header can be compressed, otherwise 0.
  497. */
  498. static uint32_t iphc_nhc_compressable(uint8_t next_header)
  499. {
  500. switch (next_header)
  501. {
  502. case IPV6_NEXT_HEADER_UDP:
  503. return 1;
  504. }
  505. return 0;
  506. }
  507. /**@brief Function for decoding IPHC (IP Header Compression) defined in
  508. * IETF RFC 6282.
  509. *
  510. * @param[in] p_instance Transport instance from where packet came.
  511. * @param[in] p_input Pointer to received packet from IPSP module.
  512. * @param[in] input_len Length of received packet.
  513. * @param[in] p_output Pointer to allocated buffer for decompressed packet.
  514. * @param[out] p_output Pointer to decompressed IPv6 packet.
  515. * @param[out] p_output_len Length of decompressed IPv6 packet.
  516. *
  517. * @return NRF_SUCCESS on success, otherwise an error code.
  518. */
  519. static uint32_t iphc_decode(iot_interface_t * p_interface,
  520. uint8_t * p_output,
  521. uint16_t * p_output_len,
  522. uint8_t * p_input,
  523. uint16_t input_len,
  524. iot_context_id_t * p_rx_contexts)
  525. {
  526. uint32_t retval = NRF_SUCCESS;
  527. uint32_t err_code = NRF_SUCCESS;
  528. uint8_t * p_iphc = p_input;
  529. uint8_t sci = IPV6_CONTEXT_IDENTIFIER_NONE;
  530. uint8_t dci = IPV6_CONTEXT_IDENTIFIER_NONE;
  531. uint16_t nhc_length = 0;
  532. iot_context_t * p_ctx = NULL;
  533. // IPv6 headers used in decompression.
  534. ipv6_header_t * p_iphdr = (ipv6_header_t *)p_output;
  535. udp6_header_t * p_udphdr = (udp6_header_t *)&p_output[IPV6_IP_HEADER_SIZE];
  536. // Check if format of packet is correct.
  537. if ((p_input[0] >> IPHC_START_DISPATCH_POS) != IPHC_START_DISPATCH)
  538. {
  539. BLE_6LOWPAN_ERR("[6LoWPAN] Packet has incorrect IPHC structure!");
  540. return NRF_ERROR_INVALID_DATA;
  541. }
  542. // IPHC basic form has 2 bytes.
  543. p_iphc += 2;
  544. // RFC6282: An additional 8-bit Context Identifier Extension field
  545. // immediately follows the Destination Address Mode (DAM) field.
  546. if ((p_input[1] & IPHC_CID_MASK))
  547. {
  548. sci = ((*p_iphc & IPHC_CID_SOURCE_MASK) >> IPHC_CID_SOURCE_POS);
  549. dci = ((*p_iphc & IPHC_CID_DESTINATION_MASK) >> IPHC_CID_DESTINATION_POS);
  550. p_iphc += 1;
  551. }
  552. // Set proper context identifiers.
  553. p_rx_contexts->src_cntxt_id = sci;
  554. p_rx_contexts->dest_cntxt_id = dci;
  555. switch ((p_input[0] & IPHC_TF_MASK) >> IPHC_TF_POS)
  556. {
  557. case IPHC_TF_11:
  558. // Elide Traffic Class and Flow Label.
  559. p_iphdr->version_traffic_class = IPHC_IPHEADER_VER_TC;
  560. p_iphdr->traffic_class_flowlabel = IPHC_IPHEADER_TC_FL;
  561. p_iphdr->flowlabel = IPHC_IPHEADER_FL;
  562. break;
  563. case IPHC_TF_10:
  564. // Elide Flow Label.
  565. p_iphdr->version_traffic_class = IPHC_IPHEADER_VER_TC | ((*p_iphc & IPHC_TF_DSCP_MASK) >> 2);
  566. p_iphdr->traffic_class_flowlabel = ((*p_iphc & 0x03) << 6) |
  567. ((*p_iphc & IPHC_TF_ECN_MASK) >> 2);
  568. p_iphdr->flowlabel = IPHC_IPHEADER_FL;
  569. p_iphc += 1;
  570. break;
  571. case IPHC_TF_01:
  572. // Elide DSCP, carry ECN and Flow Label.
  573. p_iphdr->version_traffic_class = IPHC_IPHEADER_VER_TC;
  574. p_iphdr->traffic_class_flowlabel = ((*p_iphc & IPHC_TF_ECN_MASK) >> 2) |
  575. ((*p_iphc & 0x0f));
  576. memcpy(&p_iphdr->flowlabel, p_iphc + 1, 2);
  577. p_iphc += 3;
  578. break;
  579. case IPHC_TF_00:
  580. // Flow Label and Traffic Class in-line.
  581. p_iphdr->version_traffic_class = IPHC_IPHEADER_VER_TC | ((*p_iphc & IPHC_TF_DSCP_MASK) >> 2);
  582. p_iphdr->traffic_class_flowlabel = ((*p_iphc & 0x03) << 6) |
  583. ((*p_iphc & IPHC_TF_ECN_MASK) >> 2) |
  584. ((*(p_iphc + 1) & 0x0f));
  585. memcpy(&p_iphdr->flowlabel, p_iphc + 2, 2);
  586. p_iphc += 4;
  587. break;
  588. }
  589. if (!((p_input[0] & IPHC_NH_MASK)))
  590. {
  591. // Set next header.
  592. p_iphdr->next_header = *p_iphc++;
  593. }
  594. if ((p_input[0] & IPHC_HLIM_MASK))
  595. {
  596. p_iphdr->hoplimit = m_hop_limit[((p_input[0] & IPHC_HLIM_MASK) >> IPHC_HLIM_POS)];
  597. }
  598. else
  599. {
  600. p_iphdr->hoplimit = *p_iphc++;
  601. }
  602. // Clear IPv6 addresses.
  603. memset(p_iphdr->srcaddr.u8, 0, IPV6_ADDR_SIZE);
  604. memset(p_iphdr->destaddr.u8, 0, IPV6_ADDR_SIZE);
  605. // Source address decompression.
  606. if ((p_input[1] & IPHC_SAC_MASK) >> IPHC_SAC_POS)
  607. {
  608. if ( ((p_input[1] & IPHC_SAM_MASK) >> IPHC_SAM_POS) == IPHC_SAM_00 )
  609. {
  610. // Unspecified address.
  611. memset(p_iphdr->srcaddr.u8, 0, IPV6_ADDR_SIZE);
  612. }
  613. else
  614. {
  615. switch ((p_input[1] & IPHC_SAM_MASK) >> IPHC_SAM_POS)
  616. {
  617. case IPHC_SAM_01:
  618. // 64 bits in-line, first IID then prefix in case prefix > 64.
  619. memcpy(p_iphdr->srcaddr.u8 + 8, p_iphc, 8);
  620. p_iphc += 8;
  621. break;
  622. case IPHC_SAM_10:
  623. // 16 bits in-line.
  624. memcpy(p_iphdr->srcaddr.u8 + 14, p_iphc, 2);
  625. memcpy(p_iphdr->srcaddr.u8 + 11, m_link_local_16_middle, 2);
  626. p_iphc += 2;
  627. break;
  628. case IPHC_SAM_11:
  629. // Take the address from lower layer.
  630. memcpy(p_iphdr->srcaddr.u8 + 8, p_interface->peer_addr.identifier, 8);
  631. p_iphdr->srcaddr.u8[8] ^= IPV6_IID_FLIP_VALUE;
  632. break;
  633. }
  634. /* Look up for context */
  635. BLE_6LOWPAN_MUTEX_UNLOCK();
  636. err_code = iot_context_manager_get_by_cid(p_interface, sci, &p_ctx);
  637. BLE_6LOWPAN_MUTEX_LOCK();
  638. if (err_code == NRF_SUCCESS)
  639. {
  640. /* Add prefix */
  641. IPV6_ADDRESS_PREFIX_SET(p_iphdr->srcaddr.u8, p_ctx->prefix.u8, p_ctx->prefix_len);
  642. }
  643. else
  644. {
  645. /* Set error and continue decompression. */
  646. retval = BLE_6LOWPAN_CID_NOT_FOUND;
  647. BLE_6LOWPAN_ERR("Cannot find context identifier in the context table.");
  648. }
  649. }
  650. }
  651. else
  652. {
  653. switch ((p_input[1] & IPHC_SAM_MASK) >> IPHC_SAM_POS)
  654. {
  655. case IPHC_SAM_00:
  656. // Full 128-bits in-line.
  657. memcpy(p_iphdr->srcaddr.u8, p_iphc, IPV6_ADDR_SIZE);
  658. p_iphc += IPV6_ADDR_SIZE;
  659. break;
  660. case IPHC_SAM_01:
  661. // 64 bits in-line, first IID then prefix in case prefix > 64.
  662. memcpy(p_iphdr->srcaddr.u8, m_link_local_prefix, 2);
  663. memcpy(p_iphdr->srcaddr.u8 + 8, p_iphc, 8);
  664. p_iphc += 8;
  665. break;
  666. case IPHC_SAM_10:
  667. // 16 bits in-line.
  668. memcpy(p_iphdr->srcaddr.u8, m_link_local_prefix, 2);
  669. memcpy(p_iphdr->srcaddr.u8 + 11, m_link_local_16_middle, 2);
  670. memcpy(p_iphdr->srcaddr.u8 + 14, p_iphc, 2);
  671. p_iphc += 2;
  672. break;
  673. case IPHC_SAM_11:
  674. memcpy(p_iphdr->srcaddr.u8, m_link_local_prefix, 2);
  675. memcpy(p_iphdr->srcaddr.u8 + 8, p_interface->peer_addr.identifier, 8);
  676. p_iphdr->srcaddr.u8[8] ^= IPV6_IID_FLIP_VALUE;
  677. break;
  678. }
  679. }
  680. // Destination address decompression.
  681. if ((p_input[1] & IPHC_DAC_MASK) >> IPHC_DAC_POS)
  682. {
  683. if ((p_input[1] & IPHC_M_MASK) >> IPHC_M_POS)
  684. {
  685. switch ((p_input[1] & IPHC_DAM_MASK) >> IPHC_DAM_POS)
  686. {
  687. case IPHC_DAM_00:
  688. // 48-bits in-line.
  689. p_iphdr->destaddr.u8[0] = 0xff;
  690. memcpy(p_iphdr->destaddr.u8 + 1, p_iphc, 2);
  691. memcpy(p_iphdr->destaddr.u8 + 12, p_iphc + 2, 4);
  692. p_iphc += 6;
  693. break;
  694. default:
  695. BLE_6LOWPAN_ERR("Reserved value in IPHC header!");
  696. return NRF_ERROR_INVALID_DATA;
  697. }
  698. }
  699. else
  700. {
  701. switch ((p_input[1] & IPHC_DAM_MASK) >> IPHC_DAM_POS)
  702. {
  703. case IPHC_DAM_01:
  704. // 64 bits in-line.
  705. memcpy(p_iphdr->destaddr.u8 + 8, p_iphc, 8);
  706. p_iphc += 8;
  707. break;
  708. case IPHC_DAM_10:
  709. // 16 bits in-line.
  710. memcpy(p_iphdr->destaddr.u8 + 11, m_link_local_16_middle, 2);
  711. memcpy(p_iphdr->destaddr.u8 + 14, p_iphc, 2);
  712. p_iphc += 2;
  713. break;
  714. case IPHC_DAM_11:
  715. // Take the address from lower layer.
  716. memcpy(p_iphdr->destaddr.u8 + 8, p_interface->local_addr.identifier, 8);
  717. p_iphdr->destaddr.u8[8] ^= IPV6_IID_FLIP_VALUE;
  718. break;
  719. default:
  720. BLE_6LOWPAN_ERR("Reserved value in IPHC header!");
  721. return NRF_ERROR_INVALID_DATA;
  722. }
  723. /* Look up for context */
  724. BLE_6LOWPAN_MUTEX_UNLOCK();
  725. err_code = iot_context_manager_get_by_cid(p_interface, dci, &p_ctx);
  726. BLE_6LOWPAN_MUTEX_LOCK();
  727. if (err_code == NRF_SUCCESS)
  728. {
  729. /* Add prefix */
  730. IPV6_ADDRESS_PREFIX_SET(p_iphdr->destaddr.u8, p_ctx->prefix.u8, p_ctx->prefix_len);
  731. }
  732. else
  733. {
  734. /* Set error and continue decompression. */
  735. retval = BLE_6LOWPAN_CID_NOT_FOUND;
  736. BLE_6LOWPAN_ERR("Cannot find context identifier in the context table.");
  737. }
  738. }
  739. }
  740. else
  741. {
  742. if ((p_input[1] & IPHC_M_MASK) >> IPHC_M_POS)
  743. {
  744. switch ((p_input[1] & IPHC_DAM_MASK) >> IPHC_DAM_POS)
  745. {
  746. case IPHC_DAM_00:
  747. // 128 bits in-line.
  748. memcpy(p_iphdr->destaddr.u8, p_iphc, IPV6_ADDR_SIZE);
  749. p_iphc += IPV6_ADDR_SIZE;
  750. break;
  751. case IPHC_DAM_01:
  752. // 48 bits in-line.
  753. p_iphdr->destaddr.u8[0] = 0xFF;
  754. p_iphdr->destaddr.u8[1] = *p_iphc;
  755. memcpy(p_iphdr->destaddr.u8 + 11, p_iphc + 1, 5);
  756. p_iphc += 6;
  757. break;
  758. case IPHC_DAM_10:
  759. // 32 bits in-line.
  760. p_iphdr->destaddr.u8[0] = 0xFF;
  761. p_iphdr->destaddr.u8[1] = *p_iphc;
  762. memcpy(p_iphdr->destaddr.u8 + 13, p_iphc + 1, 3);
  763. p_iphc += 4;
  764. break;
  765. case IPHC_DAM_11:
  766. // 8 bits in-line.
  767. p_iphdr->destaddr.u8[0] = 0xFF;
  768. p_iphdr->destaddr.u8[1] = 0x02;
  769. p_iphdr->destaddr.u8[15] = *p_iphc;
  770. p_iphc += 1;
  771. break;
  772. }
  773. }
  774. else
  775. {
  776. switch ((p_input[1] & IPHC_DAM_MASK) >> IPHC_DAM_POS)
  777. {
  778. case IPHC_DAM_00:
  779. // 128 bits in-line.
  780. memcpy(p_iphdr->destaddr.u8, p_iphc, IPV6_ADDR_SIZE);
  781. p_iphc += IPV6_ADDR_SIZE;
  782. break;
  783. case IPHC_DAM_01:
  784. // 64 bits in-line, first IID then prefix in case prefix > 64.
  785. memcpy(p_iphdr->destaddr.u8, m_link_local_prefix, 2);
  786. memcpy(p_iphdr->destaddr.u8 + 8, p_iphc, 8);
  787. p_iphc += 8;
  788. break;
  789. case IPHC_DAM_10:
  790. // 16 bits in-line.
  791. memcpy(p_iphdr->destaddr.u8, m_link_local_prefix, 2);
  792. memcpy(p_iphdr->destaddr.u8 + 11, m_link_local_16_middle, 2);
  793. memcpy(p_iphdr->destaddr.u8 + 14, p_iphc, 2);
  794. p_iphc += 2;
  795. break;
  796. case IPHC_DAM_11:
  797. // Take the address from lower layer.
  798. memcpy(p_iphdr->destaddr.u8, m_link_local_prefix, 2);
  799. memcpy(p_iphdr->destaddr.u8 + 8, p_interface->local_addr.identifier, 8);
  800. p_iphdr->destaddr.u8[8] ^= IPV6_IID_FLIP_VALUE;
  801. break;
  802. }
  803. }
  804. }
  805. // Perform next header compression.
  806. if (((p_input[0] & IPHC_NH_MASK) >> IPHC_NH_POS))
  807. {
  808. p_iphc += iphc_nhc_decode(p_iphc, p_output, &nhc_length);
  809. if (nhc_length == 0)
  810. {
  811. // Unknown NHC field.
  812. BLE_6LOWPAN_ERR("IPHC contains unsupported NHC header!");
  813. return NRF_ERROR_INVALID_DATA;
  814. }
  815. }
  816. // Calculate IPv6 Header Length.
  817. p_iphdr->length = input_len - (p_iphc - p_input);
  818. // Check if IPHC contains more bytes than whole packet.
  819. if (p_iphdr->length > input_len)
  820. {
  821. // We cannot decompress it.
  822. BLE_6LOWPAN_ERR("IPHC contains more bytes than expected!");
  823. return NRF_ERROR_INVALID_DATA;
  824. }
  825. // Copy the data.
  826. memcpy(p_output + IPV6_IP_HEADER_SIZE + nhc_length, p_iphc, p_iphdr->length);
  827. // Add uncompressed headers length if any.
  828. p_iphdr->length += nhc_length;
  829. // Return length of whole IPv6 packet.
  830. *p_output_len = p_iphdr->length + IPV6_IP_HEADER_SIZE;
  831. // Keep proper endianness.
  832. p_iphdr->length = HTONS(p_iphdr->length);
  833. // Restore UDP length if compression was used.
  834. if ( p_iphdr->next_header == IPV6_NEXT_HEADER_UDP )
  835. {
  836. memcpy(&p_udphdr->length, &p_iphdr->length, 2);
  837. }
  838. return retval;
  839. }
  840. /**@brief Function for encoding IPHC (IP Header Compression) defined in
  841. * IETF RFC 6282. Instead of having separate buffer for compression,
  842. * needed compression is performed on the IPv6 packet and buffer holding
  843. * the packet is reused to overwrite the headers compressed.
  844. *
  845. * @param[in] p_interface IoT interface where packet must be sent.
  846. * @param[in] p_input Pointer to full IPv6 packet.
  847. * @param[in] input_len Length of IPv6 packet.
  848. * @param[out] p_output Pointer to place of start IPHC packet.
  849. * @param[out] p_output_len Length of compressed packet.
  850. *
  851. * @return NRF_SUCCESS on success, otherwise an error code.
  852. */
  853. static uint32_t iphc_encode(const iot_interface_t * p_interface,
  854. uint8_t ** p_output,
  855. uint16_t * p_output_len,
  856. const uint8_t * p_input,
  857. uint16_t input_len)
  858. {
  859. // Create a buffer with maximum of IPHC value.
  860. uint32_t err_code;
  861. uint8_t iphc_buff[IPV6_IP_HEADER_SIZE + UDP_HEADER_SIZE];
  862. uint8_t traffic_class;
  863. uint8_t * p_iphc = &iphc_buff[2];
  864. uint16_t iphc_len = 0;
  865. uint16_t nhc_length = 0;
  866. iot_context_t * p_ctx = NULL;
  867. uint8_t sci = p_interface->tx_contexts.src_cntxt_id;
  868. uint8_t dci = p_interface->tx_contexts.dest_cntxt_id;
  869. bool sci_cover = false;
  870. bool dci_cover = false;
  871. // IPv6 header.
  872. ipv6_header_t * p_iphdr = (ipv6_header_t *)p_input;
  873. *p_iphc = 0;
  874. // Set IPHC dispatch.
  875. iphc_buff[0] = IPHC_START_DISPATCH << IPHC_START_DISPATCH_POS;
  876. // Check if address can be compressed using context identifier.
  877. if (sci == IPV6_CONTEXT_IDENTIFIER_NONE)
  878. {
  879. BLE_6LOWPAN_MUTEX_UNLOCK();
  880. err_code = iot_context_manager_get_by_addr(p_interface, &p_iphdr->srcaddr, &p_ctx);
  881. BLE_6LOWPAN_MUTEX_LOCK();
  882. if (err_code == NRF_SUCCESS)
  883. {
  884. sci_cover = is_context_cover_iid(&p_iphdr->srcaddr, p_ctx, &p_interface->local_addr);
  885. sci = p_ctx->context_id;
  886. }
  887. }
  888. if (dci == IPV6_CONTEXT_IDENTIFIER_NONE)
  889. {
  890. BLE_6LOWPAN_MUTEX_UNLOCK();
  891. err_code = iot_context_manager_get_by_addr(p_interface, &p_iphdr->destaddr, &p_ctx);
  892. BLE_6LOWPAN_MUTEX_LOCK();
  893. if (err_code == NRF_SUCCESS)
  894. {
  895. dci_cover = is_context_cover_iid(&p_iphdr->destaddr, p_ctx, &p_interface->peer_addr);
  896. dci = p_ctx->context_id;
  897. }
  898. }
  899. if (((sci != IPV6_CONTEXT_IDENTIFIER_NONE) || dci != IPV6_CONTEXT_IDENTIFIER_NONE))
  900. {
  901. iphc_buff[1] = (IPHC_CID_1 << IPHC_CID_POS);
  902. // Add Source Context if exists.
  903. if (sci != IPV6_CONTEXT_IDENTIFIER_NONE)
  904. {
  905. *p_iphc |= (sci << 4);
  906. }
  907. // Add Destination Context if exists.
  908. if (dci != IPV6_CONTEXT_IDENTIFIER_NONE)
  909. {
  910. *p_iphc |= dci;
  911. }
  912. p_iphc += 1;
  913. }
  914. else
  915. {
  916. // Unset Context Identifier bit.
  917. iphc_buff[1] = (IPHC_CID_0 << IPHC_CID_POS);
  918. }
  919. // Change ECN with DSCP in Traffic Class.
  920. traffic_class = (p_iphdr->version_traffic_class & 0x0f) << 4;
  921. traffic_class |= ((p_iphdr->traffic_class_flowlabel & 0xf0) >> 4);
  922. traffic_class = (((traffic_class & 0x03) << 6) | (traffic_class >> 2));
  923. if ((p_iphdr->flowlabel == 0) && ((p_iphdr->traffic_class_flowlabel & 0x0f) == 0))
  924. {
  925. if (traffic_class == 0)
  926. {
  927. // Elide Flow Label and Traffic Class.
  928. iphc_buff[0] |= (IPHC_TF_11 << IPHC_TF_POS);
  929. }
  930. else
  931. {
  932. // Elide Flow Label and carry Traffic Class in-line.
  933. iphc_buff[0] |= (IPHC_TF_10 << IPHC_TF_POS);
  934. *p_iphc++ = traffic_class;
  935. }
  936. }
  937. else
  938. {
  939. if (traffic_class & IPHC_TF_DSCP_MASK)
  940. {
  941. // Carry Flow Label and Traffic Class in-line.
  942. iphc_buff[0] |= (IPHC_TF_00 << IPHC_TF_POS);
  943. *p_iphc++ = traffic_class;
  944. *p_iphc++ = (p_iphdr->traffic_class_flowlabel & 0x0f);
  945. memcpy(p_iphc, &p_iphdr->flowlabel, 2);
  946. p_iphc += 2;
  947. }
  948. else
  949. {
  950. // Carry Flow Label and ECN only with 2-bit padding.
  951. iphc_buff[0] |= (IPHC_TF_01 << IPHC_TF_POS);
  952. *p_iphc++ =
  953. ((traffic_class & IPHC_TF_ECN_MASK) | (p_iphdr->traffic_class_flowlabel & 0x0f));
  954. memcpy(p_iphc, &p_iphdr->flowlabel, 2);
  955. p_iphc += 2;
  956. }
  957. }
  958. // Checks if next header is compressed.
  959. if (iphc_nhc_compressable(p_iphdr->next_header))
  960. {
  961. iphc_buff[0] |= (IPHC_NH_1 << IPHC_NH_POS);
  962. }
  963. else
  964. {
  965. iphc_buff[0] |= (IPHC_NH_0 << IPHC_NH_POS);
  966. *p_iphc++ = p_iphdr->next_header;
  967. }
  968. // Hop limit compression.
  969. switch (p_iphdr->hoplimit)
  970. {
  971. case 1:
  972. iphc_buff[0] |= (IPHC_HLIM_01 << IPHC_HLIM_POS);
  973. break;
  974. case 64:
  975. iphc_buff[0] |= (IPHC_HLIM_10 << IPHC_HLIM_POS);
  976. break;
  977. case 255:
  978. iphc_buff[0] |= (IPHC_HLIM_11 << IPHC_HLIM_POS);
  979. break;
  980. default:
  981. // Carry Hop Limit in-line.
  982. iphc_buff[0] |= (IPHC_HLIM_00 << IPHC_HLIM_POS);
  983. *p_iphc++ = p_iphdr->hoplimit;
  984. break;
  985. }
  986. // Source address compression.
  987. if (IPV6_ADDRESS_IS_UNSPECIFIED(&p_iphdr->srcaddr))
  988. {
  989. iphc_buff[1] |= (IPHC_SAC_1 << IPHC_SAC_POS);
  990. iphc_buff[1] |= (IPHC_SAM_00 << IPHC_SAM_POS);
  991. }
  992. else if (sci != IPV6_CONTEXT_IDENTIFIER_NONE || IPV6_ADDRESS_IS_LINK_LOCAL(&p_iphdr->srcaddr))
  993. {
  994. if (sci != IPV6_CONTEXT_IDENTIFIER_NONE)
  995. {
  996. // Set stateful source address compression.
  997. iphc_buff[1] |= (IPHC_SAC_1 << IPHC_SAC_POS);
  998. }
  999. if (IPV6_ADDRESS_IS_FULLY_ELIDABLE(p_interface->local_addr.identifier,
  1000. &p_iphdr->srcaddr)
  1001. ||
  1002. sci_cover == true)
  1003. {
  1004. iphc_buff[1] |= (IPHC_SAM_11 << IPHC_SAM_POS);
  1005. }
  1006. else if (IPV6_ADDRESS_IS_16_BIT_COMPRESSABLE(&p_iphdr->srcaddr))
  1007. {
  1008. iphc_buff[1] |= (IPHC_SAM_10 << IPHC_SAM_POS);
  1009. memcpy(p_iphc, &p_iphdr->srcaddr.u8[14], 2);
  1010. p_iphc += 2;
  1011. }
  1012. else
  1013. {
  1014. iphc_buff[1] |= (IPHC_SAM_01 << IPHC_SAM_POS);
  1015. memcpy(p_iphc, &p_iphdr->srcaddr.u8[8], 8);
  1016. p_iphc += 8;
  1017. }
  1018. }
  1019. else
  1020. {
  1021. // Carry full source address in-line.
  1022. iphc_buff[1] |= (IPHC_SAC_0 << IPHC_SAC_POS);
  1023. iphc_buff[1] |= (IPHC_SAM_00 << IPHC_SAM_POS);
  1024. memcpy(p_iphc, p_iphdr->srcaddr.u8, IPV6_ADDR_SIZE);
  1025. p_iphc += IPV6_ADDR_SIZE;
  1026. }
  1027. // Destination compression.
  1028. if (IPV6_ADDRESS_IS_MULTICAST(&p_iphdr->destaddr))
  1029. {
  1030. iphc_buff[1] |= (IPHC_M_1 << IPHC_M_POS);
  1031. if (dci != IPV6_CONTEXT_IDENTIFIER_NONE)
  1032. {
  1033. iphc_buff[1] |= (IPHC_DAC_1 << IPHC_DAC_POS);
  1034. iphc_buff[1] |= (IPHC_DAM_00 << IPHC_DAM_POS);
  1035. p_iphdr->destaddr.u8[0] = 0xff;
  1036. memcpy(p_iphc, &p_iphdr->destaddr.u8[1], 2);
  1037. memcpy(p_iphc + 2, &p_iphdr->destaddr.u8[12], 4);
  1038. p_iphc += 6;
  1039. }
  1040. else if (IPV6_ADDRESS_IS_8_BIT_MCAST_COMPRESSABLE(&p_iphdr->destaddr))
  1041. {
  1042. iphc_buff[1] |= (IPHC_DAC_0 << IPHC_DAC_POS);
  1043. iphc_buff[1] |= (IPHC_DAM_11 << IPHC_DAM_POS);
  1044. *p_iphc++ = p_iphdr->destaddr.u8[15];
  1045. }
  1046. else if (IPV6_ADDRESS_IS_32_BIT_MCAST_COMPRESSABLE(&p_iphdr->destaddr))
  1047. {
  1048. iphc_buff[1] |= (IPHC_DAC_0 << IPHC_DAC_POS);
  1049. iphc_buff[1] |= (IPHC_DAM_10 << IPHC_DAM_POS);
  1050. *p_iphc = p_iphdr->destaddr.u8[1];
  1051. memcpy(p_iphc + 1, &p_iphdr->destaddr.u8[13], 3);
  1052. p_iphc += 4;
  1053. }
  1054. else if (IPV6_ADDRESS_IS_48_BIT_MCAST_COMPRESSABLE(&p_iphdr->destaddr))
  1055. {
  1056. iphc_buff[1] |= (IPHC_DAC_0 << IPHC_DAC_POS);
  1057. iphc_buff[1] |= (IPHC_DAM_01 << IPHC_DAM_POS);
  1058. *p_iphc = p_iphdr->destaddr.u8[1];
  1059. memcpy(p_iphc + 1, &p_iphdr->destaddr.u8[11], 5);
  1060. p_iphc += 6;
  1061. }
  1062. else
  1063. {
  1064. // Carry full destination multi-cast address in-line.
  1065. iphc_buff[1] |= (IPHC_DAC_0 << IPHC_DAC_POS);
  1066. iphc_buff[1] |= (IPHC_DAM_00 << IPHC_DAM_POS);
  1067. memcpy(p_iphc, p_iphdr->destaddr.u8, IPV6_ADDR_SIZE);
  1068. p_iphc += IPV6_ADDR_SIZE;
  1069. }
  1070. }
  1071. else
  1072. {
  1073. iphc_buff[1] |= (IPHC_M_0 << IPHC_M_POS);
  1074. if (dci != IPV6_CONTEXT_IDENTIFIER_NONE || IPV6_ADDRESS_IS_LINK_LOCAL(&p_iphdr->destaddr))
  1075. {
  1076. if (dci != IPV6_CONTEXT_IDENTIFIER_NONE)
  1077. {
  1078. iphc_buff[1] |= (IPHC_DAC_1 << IPHC_DAC_POS);
  1079. }
  1080. if (IPV6_ADDRESS_IS_FULLY_ELIDABLE(p_interface->peer_addr.identifier,
  1081. &p_iphdr->destaddr)
  1082. ||
  1083. dci_cover == true)
  1084. {
  1085. iphc_buff[1] |= (IPHC_DAM_11 << IPHC_DAM_POS);
  1086. }
  1087. else if (IPV6_ADDRESS_IS_16_BIT_COMPRESSABLE(&p_iphdr->destaddr))
  1088. {
  1089. iphc_buff[1] |= (IPHC_DAM_10 << IPHC_DAM_POS);
  1090. memcpy(p_iphc, &p_iphdr->destaddr.u8[14], 2);
  1091. p_iphc += 2;
  1092. }
  1093. else
  1094. {
  1095. iphc_buff[1] |= (IPHC_DAM_01 << IPHC_DAM_POS);
  1096. memcpy(p_iphc, &p_iphdr->destaddr.u8[8], 8);
  1097. p_iphc += 8;
  1098. }
  1099. }
  1100. else
  1101. {
  1102. // Carry full destination address in-line.
  1103. iphc_buff[1] |= (IPHC_DAC_0 << IPHC_DAC_POS);
  1104. iphc_buff[1] |= (IPHC_DAM_00 << IPHC_DAM_POS);
  1105. memcpy(p_iphc, p_iphdr->destaddr.u8, IPV6_ADDR_SIZE);
  1106. p_iphc += IPV6_ADDR_SIZE;
  1107. }
  1108. }
  1109. if ( iphc_buff[0] & IPHC_NH_MASK)
  1110. {
  1111. p_iphc += iphc_nhc_encode(p_iphc, p_input, &nhc_length);
  1112. }
  1113. // Calculate IPHC size.
  1114. iphc_len = (p_iphc - iphc_buff);
  1115. // Calculate IPv6 packet size.
  1116. *p_output_len = input_len - (IPV6_IP_HEADER_SIZE - iphc_len + nhc_length);
  1117. // Use p_data as final buffer (since IPHC+NHC <= IPv6 Header + NHC (UDP)).
  1118. memcpy((uint8_t *)&p_input[IPV6_IP_HEADER_SIZE + nhc_length - iphc_len], iphc_buff, iphc_len);
  1119. // Set correct address of output data.
  1120. *p_output = (uint8_t *) &p_input[IPV6_IP_HEADER_SIZE + nhc_length - iphc_len];
  1121. return NRF_SUCCESS;
  1122. }
  1123. /******************************************************************************
  1124. * @name 6LoWPAN transport functions
  1125. *****************************************************************************/
  1126. /**@brief Function for notifying application of an error in an ongoing procedure.
  1127. *
  1128. * @param[in] p_interface Pointer to 6LoWPAN interface.
  1129. * @param[in] err_code Internally error code.
  1130. *
  1131. * @return None.
  1132. */
  1133. static void app_notify_error(iot_interface_t * p_interface,
  1134. uint32_t err_code)
  1135. {
  1136. ble_6lowpan_event_t event;
  1137. event.event_id = BLE_6LO_EVT_ERROR;
  1138. event.event_result = err_code;
  1139. BLE_6LOWPAN_MUTEX_UNLOCK();
  1140. m_event_handler(p_interface, &event);
  1141. BLE_6LOWPAN_MUTEX_LOCK();
  1142. }
  1143. /**@brief Function for notifying application of the new packet received.
  1144. *
  1145. * @param[in] p_interface Pointer to iot interface.
  1146. * @param[in] p_packet Pointer to decompressed IPv6 packet.
  1147. * @param[in] packet_len Length of IPv6 packet.
  1148. * @param[in] result Processing result of packet. Could be NRF_SUCCESS, or
  1149. * NRF_ERROR_NOT_FOUND if context identifier is unknown.
  1150. * @param[in] p_rx_contexts Received contexts if any.
  1151. *
  1152. * @return None.
  1153. */
  1154. static void app_notify_rx_data(iot_interface_t * p_interface,
  1155. uint8_t * p_packet,
  1156. uint16_t packet_len,
  1157. uint32_t result,
  1158. iot_context_id_t * p_rx_contexts)
  1159. {
  1160. ble_6lowpan_event_t event;
  1161. event.event_id = BLE_6LO_EVT_INTERFACE_DATA_RX;
  1162. event.event_result = result;
  1163. event.event_param.rx_event_param.p_packet = p_packet;
  1164. event.event_param.rx_event_param.packet_len = packet_len;
  1165. event.event_param.rx_event_param.rx_contexts = *(p_rx_contexts);
  1166. BLE_6LOWPAN_MUTEX_UNLOCK();
  1167. m_event_handler(p_interface, &event);
  1168. BLE_6LOWPAN_MUTEX_LOCK();
  1169. }
  1170. /**@brief Function for notifying application of the new interface established.
  1171. *
  1172. * @param[in] p_interface Pointer to new iot interface.
  1173. *
  1174. * @return None.
  1175. */
  1176. static void app_notify_interface_add(iot_interface_t * p_interface)
  1177. {
  1178. ble_6lowpan_event_t event;
  1179. event.event_id = BLE_6LO_EVT_INTERFACE_ADD;
  1180. event.event_result = NRF_SUCCESS;
  1181. BLE_6LOWPAN_MUTEX_UNLOCK();
  1182. m_event_handler(p_interface, &event);
  1183. BLE_6LOWPAN_MUTEX_LOCK();
  1184. }
  1185. /**@brief Function for notifying application of the interface disconnection.
  1186. *
  1187. * @param[in] p_interface Pointer to removed iot interface.
  1188. *
  1189. * @return None.
  1190. */
  1191. static void app_notify_interface_delete(iot_interface_t * p_interface)
  1192. {
  1193. ble_6lowpan_event_t event;
  1194. event.event_id = BLE_6LO_EVT_INTERFACE_DELETE;
  1195. event.event_result = NRF_SUCCESS;
  1196. BLE_6LOWPAN_MUTEX_UNLOCK();
  1197. m_event_handler(p_interface, &event);
  1198. BLE_6LOWPAN_MUTEX_LOCK();
  1199. }
  1200. /**@brief Function for initialize transmit FIFO.
  1201. *
  1202. * @param[in] p_fifo Pointer to transmit FIFO instance.
  1203. *
  1204. * @return None.
  1205. */
  1206. static void tx_fifo_init(tx_fifo_t * p_fifo)
  1207. {
  1208. memset(p_fifo->packets, 0, BLE_6LOWPAN_TX_FIFO_SIZE * sizeof (tx_packet_t));
  1209. p_fifo->read_pos = 0;
  1210. p_fifo->write_pos = 0;
  1211. }
  1212. /**@brief Function for putting new packet to transmit FIFO.
  1213. *
  1214. * @param[in] p_fifo Pointer to transmit FIFO instance.
  1215. * @param[in] p_packet Pointer to new packet.
  1216. *
  1217. * @return NRF_SUCCESS on success, otherwise NRF_ERROR_NO_MEM error.
  1218. */
  1219. static uint32_t tx_fifo_put(tx_fifo_t * p_fifo, tx_packet_t * p_packet)
  1220. {
  1221. uint32_t err_code = BLE_6LOWPAN_TX_FIFO_FULL;
  1222. // To prevent "The order of volatile accesses is undefined in this statement"
  1223. // in subsequent conditional statement.
  1224. uint32_t write_pos = p_fifo->write_pos;
  1225. uint32_t read_pos = p_fifo->read_pos;
  1226. if ((write_pos - read_pos) <= TX_FIFO_MASK)
  1227. {
  1228. p_fifo->packets[p_fifo->write_pos & TX_FIFO_MASK].p_data = p_packet->p_data;
  1229. p_fifo->packets[p_fifo->write_pos & TX_FIFO_MASK].data_len = p_packet->data_len;
  1230. p_fifo->packets[p_fifo->write_pos & TX_FIFO_MASK].p_mem_block = p_packet->p_mem_block;
  1231. p_fifo->write_pos++;
  1232. err_code = NRF_SUCCESS;
  1233. }
  1234. return err_code;
  1235. }
  1236. /**@brief Function for popping currently processed packet in transmit FIFO.
  1237. * It releases element on FIFO only when processing of the element is done.
  1238. *
  1239. * @param[in] p_fifo Pointer to transmit FIFO instance.
  1240. *
  1241. * @return None.
  1242. */
  1243. static void tx_fifo_release(tx_fifo_t * p_fifo)
  1244. {
  1245. p_fifo->read_pos++;
  1246. }
  1247. /**@brief Function for reading front element of transmit FIFO.
  1248. * After finish processing element in queue, it must be
  1249. * released using tx_fifo_release function.
  1250. *
  1251. * @param[in] p_fifo Pointer to transmit FIFO instance.
  1252. * @param[in] pp_packet Pointer to front packet.
  1253. *
  1254. * @return NRF_SUCCESS on success, otherwise NRF_ERROR_NO_MEM error.
  1255. */
  1256. static uint32_t tx_fifo_get(tx_fifo_t * p_fifo, tx_packet_t * * pp_packet)
  1257. {
  1258. uint32_t err_code = NRF_ERROR_NOT_FOUND;
  1259. // To prevent "The order of volatile accesses is undefined in this statement"
  1260. // in subsequent conditional statement.
  1261. uint32_t write_pos = p_fifo->write_pos;
  1262. uint32_t read_pos = p_fifo->read_pos;
  1263. if ((write_pos - read_pos) != 0)
  1264. {
  1265. *pp_packet = &p_fifo->packets[p_fifo->read_pos & TX_FIFO_MASK];
  1266. err_code = NRF_SUCCESS;
  1267. }
  1268. return err_code;
  1269. }
  1270. /**@brief Function for searching transport interface by given IPSP handle.
  1271. *
  1272. * @param[in] p_ipsp_handle Pointer to IPSP handle.
  1273. *
  1274. * @return Transport interface related with IPSP handle, or NULL if not found.
  1275. */
  1276. static transport_instance_t * interface_get_by_handle(const ble_ipsp_handle_t * p_ipsp_handle)
  1277. {
  1278. uint32_t index;
  1279. for (index = 0; index < BLE_6LOWPAN_MAX_INTERFACE; index++)
  1280. {
  1281. if (m_instances[index].handle.cid == p_ipsp_handle->cid &&
  1282. m_instances[index].handle.conn_handle == p_ipsp_handle->conn_handle)
  1283. {
  1284. return &m_instances[index];
  1285. }
  1286. }
  1287. return NULL;
  1288. }
  1289. /**@brief Function for initializing transport instance.
  1290. *
  1291. * @param[in] index Index of instance.
  1292. *
  1293. * @return None.
  1294. */
  1295. static void instance_init(uint32_t index)
  1296. {
  1297. memset(&m_instances[index], 0, sizeof (transport_instance_t));
  1298. m_instances[index].handle.cid = BLE_L2CAP_CID_INVALID;
  1299. m_instances[index].p_tx_cur_packet = NULL;
  1300. m_instances[index].interface.p_transport = (void *) index;
  1301. }
  1302. /**@brief Function for resetting specific 6lowpan interface.
  1303. *
  1304. * @param[in] p_interface Pointer to transport interface.
  1305. *
  1306. * @return None.
  1307. */
  1308. static void interface_reset(transport_instance_t * p_instance)
  1309. {
  1310. uint32_t index;
  1311. uint32_t instance_id = (uint32_t) p_instance->interface.p_transport;
  1312. // Free all queued packets.
  1313. for (index = 0; index < BLE_6LOWPAN_TX_FIFO_SIZE; index++)
  1314. {
  1315. if (m_instances[instance_id].tx_fifo.packets[index].p_mem_block != NULL)
  1316. {
  1317. nrf_free(m_instances[instance_id].tx_fifo.packets[index].p_mem_block);
  1318. }
  1319. }
  1320. instance_init (instance_id);
  1321. }
  1322. /**@brief Function for adding new transport instance.
  1323. *
  1324. * @param[in] p_peer_addr Pointer EUI-64 of peer address.
  1325. * @param[in] p_ipsp_handle Pointer IPSP handle, related with L2CAP CoC channel.
  1326. * @param[out] pp_instance Pointer to added transport instances, if operation succeeded.
  1327. *
  1328. * @return NRF_SUCCESS on success, otherwise NRF_ERROR_NO_MEM error.
  1329. */
  1330. static uint32_t interface_add(const eui64_t * p_peer_addr,
  1331. const ble_ipsp_handle_t * p_ipsp_handle,
  1332. transport_instance_t ** pp_instance)
  1333. {
  1334. uint32_t index;
  1335. for (index = 0; index < BLE_6LOWPAN_MAX_INTERFACE; index++)
  1336. {
  1337. if (m_instances[index].handle.cid == BLE_L2CAP_CID_INVALID)
  1338. {
  1339. m_instances[index].handle.cid = p_ipsp_handle->cid;
  1340. m_instances[index].handle.conn_handle = p_ipsp_handle->conn_handle;
  1341. m_instances[index].interface.tx_contexts.src_cntxt_id = IPV6_CONTEXT_IDENTIFIER_NONE;
  1342. m_instances[index].interface.tx_contexts.dest_cntxt_id = IPV6_CONTEXT_IDENTIFIER_NONE;
  1343. memcpy(&m_instances[index].interface.peer_addr, p_peer_addr, sizeof (eui64_t));
  1344. memcpy(&m_instances[index].interface.local_addr, &m_ll_addr, sizeof (eui64_t));
  1345. // Initialize TX FIFO.
  1346. tx_fifo_init(&m_instances[index].tx_fifo);
  1347. *pp_instance = &m_instances[index];
  1348. return NRF_SUCCESS;
  1349. }
  1350. }
  1351. return NRF_ERROR_NO_MEM;
  1352. }
  1353. /**@brief Function for checking if any transmission is currently processing on specific interface.
  1354. * Current version of L2CAP CoC in SoftDevice has limitation to send one packet at same
  1355. * time.
  1356. *
  1357. * @param[in] p_instance Pointer to transport instance.
  1358. *
  1359. * @return TRUE if interface not sending any packets, FALSE if busy.
  1360. */
  1361. static bool tx_is_free(transport_instance_t * p_instance)
  1362. {
  1363. return (NULL == p_instance->p_tx_cur_packet);
  1364. }
  1365. /**@brief Function uses for indicating transmission complete on specific interface.
  1366. *
  1367. * @param[in] p_instance Pointer to transport instance.
  1368. *
  1369. * @return None.
  1370. */
  1371. static void tx_complete(transport_instance_t * p_instance)
  1372. {
  1373. BLE_6LOWPAN_TRC("[CID 0x%04X]: Transmission complete.",
  1374. p_instance->handle.cid);
  1375. // Free the transmit buffer.
  1376. nrf_free(p_instance->p_tx_cur_packet->p_mem_block);
  1377. p_instance->p_tx_cur_packet = NULL;
  1378. // Release last processed packet.
  1379. tx_fifo_release(&p_instance->tx_fifo);
  1380. }
  1381. /**@brief Function for sending front packet from transmit FIFO on specific interface.
  1382. *
  1383. * @param[in] p_instance Pointer to transport instance.
  1384. *
  1385. * @return None.
  1386. */
  1387. static void tx_send(transport_instance_t * p_instance)
  1388. {
  1389. uint32_t err_code = NRF_SUCCESS;
  1390. if (NRF_SUCCESS == tx_fifo_get(&p_instance->tx_fifo,
  1391. &p_instance->p_tx_cur_packet))
  1392. {
  1393. err_code = ble_ipsp_send(&p_instance->handle,
  1394. p_instance->p_tx_cur_packet->p_data,
  1395. p_instance->p_tx_cur_packet->data_len);
  1396. if (NRF_SUCCESS != err_code)
  1397. {
  1398. BLE_6LOWPAN_TRC("Cannot send the packet, error = 0x%08lX", err_code);
  1399. app_notify_error(&p_instance->interface, err_code);
  1400. tx_complete(p_instance);
  1401. // Try to send another packet.
  1402. tx_send(p_instance);
  1403. }
  1404. }
  1405. }
  1406. /**@brief Callback registered with IPSP to receive asynchronous events from the module.
  1407. *
  1408. * @param[in] p_handle Pointer to IPSP handle.
  1409. * @param[in] p_evt Pointer to specific event, generated by IPSP module.
  1410. *
  1411. * @return NRF_SUCCESS on success, otherwise NRF_ERROR_NO_MEM error.
  1412. */
  1413. static uint32_t ipsp_evt_handler(ble_ipsp_handle_t const * p_handle, ble_ipsp_evt_t const * p_evt)
  1414. {
  1415. BLE_6LOWPAN_ENTRY();
  1416. VERIFY_MODULE_IS_INITIALIZED();
  1417. uint32_t mem_size;
  1418. uint16_t buff_len;
  1419. eui64_t peer_addr;
  1420. iot_context_id_t rx_contexts;
  1421. uint32_t retval = NRF_SUCCESS;
  1422. transport_instance_t * p_instance = NULL;
  1423. uint8_t * p_buff = NULL;
  1424. BLE_6LOWPAN_MUTEX_LOCK();
  1425. p_instance = interface_get_by_handle(p_handle);
  1426. switch (p_evt->evt_id)
  1427. {
  1428. case BLE_IPSP_EVT_CHANNEL_CONNECTED:
  1429. {
  1430. BLE_6LOWPAN_TRC("[CID 0x%04X]: >> BLE_IPSP_EVT_CHANNEL_CONNECTED",
  1431. p_handle->cid);
  1432. BLE_6LOWPAN_TRC("New channel established.");
  1433. if (p_instance == NULL)
  1434. {
  1435. IPV6_EUI64_CREATE_FROM_EUI48(peer_addr.identifier,
  1436. p_evt->p_evt_param->p_peer->addr,
  1437. p_evt->p_evt_param->p_peer->addr_type);
  1438. // Add interface to internal table.
  1439. retval = interface_add(&peer_addr, p_handle, &p_instance);
  1440. if (NRF_SUCCESS == retval)
  1441. {
  1442. BLE_6LOWPAN_TRC("Added new IPSP interface.");
  1443. // Notify application.
  1444. app_notify_interface_add(&p_instance->interface);
  1445. }
  1446. else
  1447. {
  1448. BLE_6LOWPAN_ERR("Cannot add new interface. Table is full.");
  1449. UNUSED_VARIABLE(ble_ipsp_disconnect(p_handle));
  1450. }
  1451. }
  1452. else
  1453. {
  1454. // Got a connection event, when already connected.
  1455. UNUSED_VARIABLE(ble_ipsp_disconnect(p_handle));
  1456. }
  1457. break;
  1458. }
  1459. case BLE_IPSP_EVT_CHANNEL_DISCONNECTED:
  1460. {
  1461. BLE_6LOWPAN_TRC("[CID 0x%04X]: >> BLE_IPSP_EVT_CHANNEL_DISCONNECTED",
  1462. p_handle->cid);
  1463. BLE_6LOWPAN_TRC("Channel disconnection.");
  1464. if (NULL != p_instance)
  1465. {
  1466. BLE_6LOWPAN_TRC("Removed IPSP interface.");
  1467. // Notify application.
  1468. app_notify_interface_delete(&p_instance->interface);
  1469. // Remove interface from internal table.
  1470. interface_reset(p_instance);
  1471. }
  1472. break;
  1473. }
  1474. case BLE_IPSP_EVT_CHANNEL_DATA_RX:
  1475. {
  1476. if (NULL != p_instance)
  1477. {
  1478. const uint16_t packet_len = MIN(p_evt->p_evt_param->p_l2cap_evt->params.rx.sdu_buf.len,
  1479. p_evt->p_evt_param->p_l2cap_evt->params.rx.sdu_len);
  1480. BLE_6LOWPAN_TRC("[CID 0x%04X]: >> BLE_IPSP_EVT_CHANNEL_DATA_RX",
  1481. p_handle->cid);
  1482. BLE_6LOWPAN_DUMP(p_evt->p_evt_param->p_l2cap_evt->params.rx.sdu_buf.p_data,
  1483. packet_len);
  1484. BLE_6LOWPAN_TRC("Processing received data.");
  1485. mem_size = packet_len + IPHC_MAX_COMPRESSED_DIFF;
  1486. // Try to allocate memory for incoming data.
  1487. retval = nrf_mem_reserve(&p_buff, &mem_size);
  1488. if (retval == NRF_SUCCESS)
  1489. {
  1490. // Decompress IPHC data into IPv6 packet.
  1491. retval = iphc_decode(&p_instance->interface,
  1492. p_buff,
  1493. &buff_len,
  1494. p_evt->p_evt_param->p_l2cap_evt->params.rx.sdu_buf.p_data,
  1495. packet_len,
  1496. &rx_contexts);
  1497. // Do not discard if packet decompressed successfully,
  1498. // otherwise error in Context based decompression.
  1499. if (retval == NRF_SUCCESS || retval == BLE_6LOWPAN_CID_NOT_FOUND)
  1500. {
  1501. if ((p_evt->evt_result == NRF_ERROR_BLE_IPSP_RX_PKT_TRUNCATED) &&
  1502. (retval == NRF_SUCCESS))
  1503. {
  1504. // Inform the application that the packet is truncated.
  1505. retval = NRF_ERROR_BLE_IPSP_RX_PKT_TRUNCATED;
  1506. }
  1507. BLE_6LOWPAN_TRC("Decompressed packet:");
  1508. BLE_6LOWPAN_DUMP(p_buff, buff_len);
  1509. // Notify application.
  1510. app_notify_rx_data(&p_instance->interface,
  1511. p_buff,
  1512. buff_len,
  1513. retval,
  1514. &rx_contexts);
  1515. }
  1516. else
  1517. {
  1518. BLE_6LOWPAN_ERR("Decompression failed!");
  1519. nrf_free(p_buff);
  1520. }
  1521. }
  1522. else
  1523. {
  1524. BLE_6LOWPAN_ERR(
  1525. "No buffer in memory pool available, packet dropped!");
  1526. }
  1527. }
  1528. else
  1529. {
  1530. BLE_6LOWPAN_ERR("Got data to unknown interface!");
  1531. }
  1532. break;
  1533. }
  1534. case BLE_IPSP_EVT_CHANNEL_DATA_TX_COMPLETE:
  1535. {
  1536. BLE_6LOWPAN_TRC("[CID 0x%04X]: >> BLE_IPSP_EVT_CHANNEL_DATA_TX_COMPLETE",
  1537. p_handle->cid);
  1538. // Free TX buffer.
  1539. tx_complete(p_instance);
  1540. // Try to send another packet.
  1541. tx_send(p_instance);
  1542. break;
  1543. }
  1544. default:
  1545. break;
  1546. }
  1547. BLE_6LOWPAN_MUTEX_UNLOCK();
  1548. BLE_6LOWPAN_EXIT();
  1549. return retval;
  1550. }
  1551. /******************************************************************************
  1552. * @name 6LoWPAN API functions
  1553. *****************************************************************************/
  1554. uint32_t ble_6lowpan_init(const ble_6lowpan_init_t * p_init)
  1555. {
  1556. BLE_6LOWPAN_ENTRY();
  1557. uint32_t index;
  1558. uint32_t retval = NRF_SUCCESS;
  1559. ble_ipsp_init_t ipsp_init_params;
  1560. NULL_PARAM_CHECK(p_init);
  1561. NULL_PARAM_CHECK(p_init->p_eui64);
  1562. NULL_PARAM_CHECK(p_init->event_handler);
  1563. // Check if the transmit FIFO size is a power of two.
  1564. if (!IS_POWER_OF_TWO(BLE_6LOWPAN_TX_FIFO_SIZE))
  1565. {
  1566. return NRF_ERROR_INVALID_LENGTH | BLE_6LOWPAN_ERR_BASE;
  1567. }
  1568. SDK_MUTEX_INIT(m_6lowpan_mutex);
  1569. BLE_6LOWPAN_MUTEX_LOCK();
  1570. // Store EUI64 in internal variable.
  1571. memcpy(m_ll_addr.identifier, p_init->p_eui64->identifier, EUI_64_ADDR_SIZE);
  1572. // Set application event handler.
  1573. m_event_handler = p_init->event_handler;
  1574. // Clear transport interfaces table.
  1575. for (index = 0; index < BLE_6LOWPAN_MAX_INTERFACE; index++)
  1576. {
  1577. instance_init(index);
  1578. }
  1579. // IPSP module initialization.
  1580. ipsp_init_params.evt_handler = ipsp_evt_handler;
  1581. // Initialize the IPSP module.
  1582. retval = ble_ipsp_init(&ipsp_init_params);
  1583. BLE_6LOWPAN_MUTEX_UNLOCK();
  1584. BLE_6LOWPAN_EXIT();
  1585. return retval;
  1586. }
  1587. uint32_t ble_6lowpan_interface_disconnect(const iot_interface_t * p_interface)
  1588. {
  1589. BLE_6LOWPAN_ENTRY();
  1590. VERIFY_MODULE_IS_INITIALIZED();
  1591. NULL_PARAM_CHECK(p_interface);
  1592. uint32_t retval;
  1593. transport_instance_t * p_instance = &m_instances[(uint32_t)p_interface->p_transport];
  1594. BLE_6LOWPAN_MUTEX_LOCK();
  1595. retval = ble_ipsp_disconnect(&p_instance->handle);
  1596. BLE_6LOWPAN_MUTEX_UNLOCK();
  1597. BLE_6LOWPAN_EXIT();
  1598. return retval;
  1599. }
  1600. uint32_t ble_6lowpan_interface_send(const iot_interface_t * p_interface,
  1601. const uint8_t * p_packet,
  1602. uint16_t packet_len)
  1603. {
  1604. BLE_6LOWPAN_ENTRY();
  1605. VERIFY_MODULE_IS_INITIALIZED();
  1606. NULL_PARAM_CHECK(p_packet);
  1607. NULL_PARAM_CHECK(p_interface);
  1608. PACKET_LENGTH_CHECK(packet_len);
  1609. uint32_t retval = NRF_SUCCESS;
  1610. uint8_t * p_output_buff = NULL;
  1611. uint16_t output_len;
  1612. tx_packet_t tx_packet;
  1613. transport_instance_t * p_instance = &m_instances[(uint32_t)p_interface->p_transport];
  1614. BLE_6LOWPAN_MUTEX_LOCK();
  1615. BLE_6LOWPAN_TRC("Uncompressed packet:");
  1616. BLE_6LOWPAN_DUMP((uint8_t *)p_packet, packet_len);
  1617. // Encode IP packet into IPHC.
  1618. retval = iphc_encode(p_interface,
  1619. &p_output_buff,
  1620. &output_len,
  1621. p_packet,
  1622. packet_len);
  1623. if (NRF_SUCCESS == retval)
  1624. {
  1625. BLE_6LOWPAN_TRC("Successfully compressed packet.");
  1626. tx_packet.p_data = p_output_buff;
  1627. tx_packet.data_len = output_len;
  1628. tx_packet.p_mem_block = (uint8_t *)p_packet;
  1629. retval = tx_fifo_put(&p_instance->tx_fifo, &tx_packet);
  1630. if (NRF_SUCCESS == retval)
  1631. {
  1632. BLE_6LOWPAN_TRC("Compressed packet:");
  1633. BLE_6LOWPAN_DUMP(p_output_buff, output_len);
  1634. // Send packet immediately if transport interface is not busy.
  1635. if (tx_is_free(p_instance))
  1636. {
  1637. tx_send(p_instance);
  1638. }
  1639. }
  1640. else
  1641. {
  1642. BLE_6LOWPAN_ERR("No place in TX queue!");
  1643. }
  1644. }
  1645. else
  1646. {
  1647. BLE_6LOWPAN_ERR("Error while compression!");
  1648. }
  1649. BLE_6LOWPAN_MUTEX_UNLOCK();
  1650. BLE_6LOWPAN_EXIT();
  1651. return retval;
  1652. }