MS_common.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /**
  2. * \file MS_common.h
  3. *
  4. * This Header file describes common declarations for the
  5. * EtherMind Mesh modules.
  6. */
  7. /*
  8. * Copyright (C) 2017. Mindtree Ltd.
  9. * All rights reserved.
  10. */
  11. #ifndef _H_MS_COMMON_
  12. #define _H_MS_COMMON_
  13. /**
  14. * \defgroup ms_common_module MESH Common
  15. * \{
  16. */
  17. /* -------------------------------------------- Header File Inclusion */
  18. /* The EtherMind OS Abstraction */
  19. #include "EM_os.h"
  20. /* The EtherMind Configuration Parameters */
  21. #include "MS_features.h"
  22. /* The EtherMind Tunable Constant */
  23. #include "MS_limits.h"
  24. /* The Bluetooth Assigned Numbers */
  25. #include "MS_assigned_numbers.h"
  26. /* The EtherMind Error Codes */
  27. #include "MS_error.h"
  28. /* The EtherMind Debug Library */
  29. #include "EM_debug.h"
  30. /* For EM_assert() macro */
  31. #include "EM_assert.h"
  32. /* For Status Flag APIs */
  33. /* #include "MS_status.h" */
  34. /* The EtherMind Timer Library */
  35. #include "EM_timer.h"
  36. /* EtherMind Platform Specific Initialization & Shutdown Handlers */
  37. #include "MS_common_pl.h"
  38. /* For Memory (leak, corruption) Testing */
  39. #ifdef MEMWATCH
  40. #include "memwatch.h"
  41. #endif /* MEMWATCH */
  42. /* TODO: See what should be the order of inclusion */
  43. #include "MS_model_states.h"
  44. /* -------------------------------------------- Global Definitions */
  45. /**
  46. * \cond ignore_this
  47. * \{
  48. */
  49. /* MS_COMMON Debug Macros */
  50. #ifndef MS_COMMON_NO_DEBUG
  51. #define MS_COMMON_ERR(...) EM_debug_error(MS_MODULE_ID_COMMON, __VA_ARGS__)
  52. #else /* COMMON_NO_DEBUG */
  53. #define MS_COMMON_ERR EM_debug_null
  54. #endif /* MS_COMMON_NO_DEBUG */
  55. #ifdef MS_COMMON_DEBUG
  56. #define MS_COMMON_TRC(...) EM_debug_trace(MS_MODULE_ID_COMMON, __VA_ARGS__)
  57. #define MS_COMMON_INF(...) EM_debug_info(MS_MODULE_ID_COMMON, __VA_ARGS__)
  58. #else /* MS_COMMON_DEBUG */
  59. #define MS_COMMON_TRC EM_debug_null
  60. #define MS_COMMON_INF EM_debug_null
  61. #endif /* MS_COMMON_DEBUG */
  62. /** \endcond */
  63. /**
  64. * \defgroup ms_common_defines Defines
  65. * \{
  66. */
  67. /**
  68. * \defgroup ms_common_constants Constants
  69. * \{
  70. */
  71. #define MS_STACK_INIT_UNDEFINED 0x00
  72. #define MS_STACK_INIT_ETHERMIND_INIT 0x01
  73. /* Definition for True/False */
  74. #ifndef MS_FALSE
  75. #define MS_FALSE 0
  76. #endif /* MS_FALSE */
  77. #ifndef MS_TRUE
  78. #define MS_TRUE 1
  79. #endif /* MS_TRUE */
  80. /** \} */
  81. /** \} */
  82. /* -------------------------------------------- Macros */
  83. /**
  84. * \defgroup ms_common_utility_macros Utility Macros
  85. * \{
  86. */
  87. /**
  88. * Packing Macros.
  89. *
  90. * Syntax: MS_PACK_<Endian-ness LE/BE>_<no_of_bytes>_BYTE
  91. *
  92. * Usage: Based on the endian-ness defined for each protocol/profile layer,
  93. * appropriate packing macros to be used by each layer.
  94. *
  95. * Example: HCI is defined as little endian protocol,
  96. * so if HCI defines HCI_PACK_2_BYTE for packing a parameter of size 2 byte,
  97. * that shall be mapped to MS_PACK_LE_2_BYTE
  98. *
  99. * By default both the packing and unpaking macros uses pointer to
  100. * a single or multi-octet variable which to be packed to or unpacked from
  101. * a buffer (unsinged character array).
  102. *
  103. * For the packing macro, another variation is available,
  104. * where the single or multi-octet variable itself is used (not its pointer).
  105. *
  106. * Syntax: MS_PACK_<Endian-ness LE/BE>_<no_of_bytes>_BYTE_VAL
  107. */
  108. /* Little Endian Packing Macros */
  109. #define MS_PACK_LE_1_BYTE(dst, src) \
  110. { \
  111. UCHAR val; \
  112. val = (UCHAR)(*(src)); \
  113. MS_PACK_LE_1_BYTE_VAL((dst), val); \
  114. }
  115. #define MS_PACK_LE_1_BYTE_VAL(dst, src) \
  116. *((UCHAR *)(dst) + 0) = src;
  117. #define MS_PACK_LE_2_BYTE(dst, src) \
  118. { \
  119. UINT16 val; \
  120. val = (UINT16)(*(src)); \
  121. MS_PACK_LE_2_BYTE_VAL((dst), val); \
  122. }
  123. #define MS_PACK_LE_2_BYTE_VAL(dst, src) \
  124. *((UCHAR *)(dst) + 0) = (UCHAR)(src); \
  125. *((UCHAR *)(dst) + 1) = (UCHAR)(src >> 8);
  126. #define MS_PACK_LE_3_BYTE(dst, src) \
  127. { \
  128. UINT32 val; \
  129. val = (UINT32)(*(src)); \
  130. MS_PACK_LE_3_BYTE_VAL((dst), val); \
  131. }
  132. #define MS_PACK_LE_3_BYTE_VAL(dst, src) \
  133. *((UCHAR *)(dst) + 0) = (UCHAR)(src);\
  134. *((UCHAR *)(dst) + 1) = (UCHAR)(src >> 8);\
  135. *((UCHAR *)(dst) + 2) = (UCHAR)(src >> 16);
  136. #define MS_PACK_LE_4_BYTE(dst, src) \
  137. { \
  138. UINT32 val; \
  139. val = (UINT32)(*(src)); \
  140. MS_PACK_LE_4_BYTE_VAL((dst), val); \
  141. }
  142. #define MS_PACK_LE_4_BYTE_VAL(dst, src) \
  143. *((UCHAR *)(dst) + 0) = (UCHAR)(src);\
  144. *((UCHAR *)(dst) + 1) = (UCHAR)(src >> 8);\
  145. *((UCHAR *)(dst) + 2) = (UCHAR)(src >> 16);\
  146. *((UCHAR *)(dst) + 3) = (UCHAR)(src >> 24);
  147. /* TBD: Update based on 64 Bit, 128 Bit Data Types */
  148. #define MS_PACK_LE_8_BYTE(dst,val)\
  149. EM_mem_copy ((dst), (val), 8)
  150. #define MS_PACK_LE_16_BYTE(dst,val)\
  151. EM_mem_copy ((dst), (val), 16)
  152. #define MS_PACK_LE_N_BYTE(dst,val,n)\
  153. EM_mem_copy ((dst), (val), (n))
  154. /* Big Endian Packing Macros */
  155. #define MS_PACK_BE_1_BYTE(dst, src) \
  156. { \
  157. UCHAR val; \
  158. val = (UCHAR)(*((UCHAR *)(src))); \
  159. MS_PACK_BE_1_BYTE_VAL((dst), val); \
  160. }
  161. #define MS_PACK_BE_1_BYTE_VAL(dst, src) \
  162. *((UCHAR *)(dst) + 0) = src;
  163. #define MS_PACK_BE_2_BYTE(dst, src) \
  164. { \
  165. UINT16 val; \
  166. val = (UINT16)(*((UINT16 *)(src))); \
  167. MS_PACK_BE_2_BYTE_VAL((dst), val); \
  168. }
  169. #define MS_PACK_BE_2_BYTE_VAL(dst, src) \
  170. *((UCHAR *)(dst) + 1) = (UCHAR)(src); \
  171. *((UCHAR *)(dst) + 0) = (UCHAR)(src >> 8);
  172. #define MS_PACK_BE_3_BYTE(dst, src) \
  173. { \
  174. UINT32 val; \
  175. val = (UINT32)(*((UINT32 *)(src))); \
  176. MS_PACK_BE_3_BYTE_VAL((dst), val); \
  177. }
  178. #define MS_PACK_BE_3_BYTE_VAL(dst, src) \
  179. *((UCHAR *)(dst) + 2) = (UCHAR)(src);\
  180. *((UCHAR *)(dst) + 1) = (UCHAR)(src >> 8);\
  181. *((UCHAR *)(dst) + 0) = (UCHAR)(src >> 16);
  182. #define MS_PACK_BE_4_BYTE(dst, src) \
  183. { \
  184. UINT32 val; \
  185. val = (UINT32)(*((UINT32 *)(src))); \
  186. MS_PACK_BE_4_BYTE_VAL((dst), val); \
  187. }
  188. #define MS_PACK_BE_4_BYTE_VAL(dst, src) \
  189. *((UCHAR *)(dst) + 3) = (UCHAR)(src);\
  190. *((UCHAR *)(dst) + 2) = (UCHAR)(src >> 8);\
  191. *((UCHAR *)(dst) + 1) = (UCHAR)(src >> 16);\
  192. *((UCHAR *)(dst) + 0) = (UCHAR)(src >> 24);
  193. /* TBD: Update based on 64 Bit, 128 Bit Data Types */
  194. #define MS_PACK_BE_8_BYTE(dst,val)\
  195. EM_mem_copy ((dst), (val), 8)
  196. #define MS_PACK_BE_16_BYTE(dst,val)\
  197. EM_mem_copy ((dst), (val), 16)
  198. #define MS_PACK_BE_N_BYTE(dst,val,n)\
  199. EM_mem_copy ((dst), (val), (n))
  200. /**
  201. * Unpacking Macros.
  202. *
  203. * Syntax: MS_UNPACK_<Endian-ness LE/BE>_<no_of_bytes>_BYTE
  204. *
  205. * Usage: Based on the endian-ness defined for each protocol/profile layer,
  206. * appropriate unpacking macros to be used by each layer.
  207. *
  208. * Example: HCI is defined as little endian protocol,
  209. * so if HCI defines HCI_UNPACK_4_BYTE for unpacking a parameter of size 4 byte,
  210. * that shall be mapped to MS_UNPACK_LE_4_BYTE
  211. */
  212. /* Little Endian Unpacking Macros */
  213. #define MS_UNPACK_LE_1_BYTE(dst,src)\
  214. *((UCHAR *)(dst)) = (UCHAR)(*((UCHAR *)(src)));
  215. #define MS_UNPACK_LE_2_BYTE(dst,src)\
  216. *((UINT16 *)(dst)) = *((src) + 1); \
  217. *((UINT16 *)(dst)) = *((UINT16 *)(dst)) << 8; \
  218. *((UINT16 *)(dst)) |= *((src) + 0);
  219. #define MS_UNPACK_LE_3_BYTE(dst,src)\
  220. *((UINT32 *)(dst)) = *((src) + 2);\
  221. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  222. *((UINT32 *)(dst)) |= *((src) + 1);\
  223. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  224. *((UINT32 *)(dst)) |= *((src) + 0);
  225. #define MS_UNPACK_LE_4_BYTE(dst,src)\
  226. *((UINT32 *)(dst)) = *((src) + 3);\
  227. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  228. *((UINT32 *)(dst)) |= *((src) + 2);\
  229. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  230. *((UINT32 *)(dst)) |= *((src) + 1);\
  231. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  232. *((UINT32 *)(dst)) |= *((src) + 0);
  233. /* TBD: Update based on 64 Bit, 128 Bit Data Types */
  234. #define MS_UNPACK_LE_8_BYTE(dst,src)\
  235. EM_mem_copy ((dst), (src), 8)
  236. #define MS_UNPACK_LE_16_BYTE(dst,src)\
  237. EM_mem_copy ((dst), (src), 16)
  238. #define MS_UNPACK_LE_N_BYTE(dst,src,n)\
  239. EM_mem_copy ((dst), (src), (n))
  240. /* Big Endian Unpacking Macros */
  241. #define MS_UNPACK_BE_1_BYTE(dst,src)\
  242. *((UCHAR *)(dst)) = (UCHAR)(*((UCHAR *)(src)));
  243. #define MS_UNPACK_BE_2_BYTE(dst,src)\
  244. *((UINT16 *)(dst)) = *((src) + 0); \
  245. *((UINT16 *)(dst)) = *((UINT16 *)(dst)) << 8; \
  246. *((UINT16 *)(dst)) |= *((src) + 1);
  247. #define MS_UNPACK_BE_3_BYTE(dst,src)\
  248. *((UINT32 *)(dst)) = *((src) + 0);\
  249. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  250. *((UINT32 *)(dst)) |= *((src) + 1);\
  251. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  252. *((UINT32 *)(dst)) |= *((src) + 2);
  253. #define MS_UNPACK_BE_4_BYTE(dst,src)\
  254. *((UINT32 *)(dst)) = *((src) + 0);\
  255. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  256. *((UINT32 *)(dst)) |= *((src) + 1);\
  257. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  258. *((UINT32 *)(dst)) |= *((src) + 2);\
  259. *((UINT32 *)(dst)) = (*((UINT32 *)(dst))) << 8;\
  260. *((UINT32 *)(dst)) |= *((src) + 3);
  261. /* TBD: Update based on 64 Bit, 128 Bit Data Types */
  262. #define MS_UNPACK_BE_8_BYTE(dst,src)\
  263. EM_mem_copy ((dst), (src), 8)
  264. #define MS_UNPACK_BE_16_BYTE(dst,src)\
  265. EM_mem_copy ((dst), (src), 16)
  266. #define MS_UNPACK_BE_N_BYTE(dst,src,n)\
  267. EM_mem_copy ((dst), (src), (n))
  268. #ifndef MS_DISABLE_MUTEX
  269. /* Macro to define a Mutex Variable */
  270. #define MS_DEFINE_MUTEX(mutex) EM_thread_mutex_type mutex;
  271. /* Macro to define a Mutex Variable with a type qualifier */
  272. #define MS_DEFINE_MUTEX_TYPE(type, mutex) type EM_thread_mutex_type mutex;
  273. /* Macro to define a Conditional Variable */
  274. #define MS_DEFINE_COND(cond) EM_thread_cond_type cond;
  275. /* Macro to define a Conditional Variable with a type qualifier */
  276. #define MS_DEFINE_COND_TYPE(type, cond) type EM_thread_cond_type cond;
  277. /*
  278. * Macro to Initialize Mutex.
  279. * To be used in void function as it returns no error.
  280. */
  281. #define MS_MUTEX_INIT_VOID(mutex, MODULE) \
  282. if (EM_thread_mutex_init(&(mutex), NULL) < 0) \
  283. { \
  284. EM_debug_error( \
  285. MS_MODULE_ID_##MODULE, \
  286. "FAILED to Initialize Mutex in " #MODULE ".\n"); \
  287. return; \
  288. }
  289. /*
  290. * Macro to Initialize Mutex.
  291. * It returns an error if mutex initialization fails.
  292. */
  293. #define MS_MUTEX_INIT(mutex, MODULE) \
  294. if (EM_thread_mutex_init(&(mutex), NULL) < 0) \
  295. { \
  296. EM_debug_error( \
  297. MS_MODULE_ID_##MODULE, \
  298. "FAILED to Initialize Mutex in " #MODULE ".\n"); \
  299. return MODULE##_MUTEX_INIT_FAILED; \
  300. }
  301. /*
  302. * Macro to Initialize Conditional Variable.
  303. * To be used in void function as it returns no error.
  304. */
  305. #define MS_COND_INIT_VOID(cond, MODULE) \
  306. if (EM_thread_cond_init(&(cond), NULL) < 0) \
  307. { \
  308. EM_debug_error( \
  309. MS_MODULE_ID_##MODULE, \
  310. "FAILED to Initialize Conditional Variable in " #MODULE ".\n"); \
  311. return; \
  312. }
  313. /*
  314. * Macro to Initialize Conditional Variable.
  315. * It returns an error if conditional variable initialization fails.
  316. */
  317. #define MS_COND_INIT(cond, MODULE) \
  318. if (EM_thread_cond_init(&(cond), NULL) < 0) \
  319. { \
  320. EM_debug_error( \
  321. MS_MODULE_ID_##MODULE, \
  322. "FAILED to Initialize Conditional Variable in " #MODULE ".\n"); \
  323. return MODULE##_COND_INIT_FAILED; \
  324. }
  325. /*
  326. * Locks the Module Specific Mutex which prevents any global variable being
  327. * overwritten by any function. It returns an error if mutex lock fails.
  328. */
  329. #define MS_MUTEX_LOCK(mutex, MODULE) \
  330. if (EM_thread_mutex_lock(&(mutex)) < 0) \
  331. { \
  332. EM_debug_error( \
  333. MS_MODULE_ID_##MODULE, \
  334. "FAILED to Lock Mutex in " #MODULE ".\n"); \
  335. return MODULE##_MUTEX_LOCK_FAILED; \
  336. }
  337. /*
  338. * Locks the Module Specific Mutex which prevents any global variable being
  339. * overwritten by any function. To be used in void function as it
  340. * returns no error.
  341. */
  342. #define MS_MUTEX_LOCK_VOID(mutex, MODULE) \
  343. if (EM_thread_mutex_lock(&(mutex)) < 0) \
  344. { \
  345. EM_debug_error( \
  346. MS_MODULE_ID_##MODULE, \
  347. "FAILED to Lock Mutex in " #MODULE ".\n"); \
  348. return; \
  349. }
  350. /*
  351. * Locks the Module Specific Mutex which prevents any global variable being
  352. * overwritten by any function.
  353. * It returns the error 'value' if mutex lock failes.
  354. */
  355. #define MS_MUTEX_LOCK_RETURN_ON_FAILURE(mutex, MODULE, value) \
  356. if (EM_thread_mutex_lock(&(mutex)) < 0) \
  357. { \
  358. EM_debug_error( \
  359. MS_MODULE_ID_##MODULE, \
  360. "FAILED to Lock Mutex in " #MODULE ".\n"); \
  361. return (value); \
  362. }
  363. /*
  364. * Locks the Module Specific Mutex which prevents any global variable being
  365. * overwritten by any function. On failure, only an Error is logged.
  366. * It can be used from both void and non-void functions.
  367. */
  368. #define MS_MUTEX_LOCK_DONOT_RETURN_ON_FAILURE(mutex, MODULE) \
  369. if (EM_thread_mutex_lock(&(mutex)) < 0) \
  370. { \
  371. EM_debug_error( \
  372. MS_MODULE_ID_##MODULE, \
  373. "FAILED to Lock Mutex in " #MODULE ".\n"); \
  374. }
  375. /*
  376. * Unlocks the Module Specific Mutex which realeses the global variables
  377. * to be written into. It returns an error if mutex unlock fails.
  378. */
  379. #define MS_MUTEX_UNLOCK(mutex, MODULE) \
  380. if (EM_thread_mutex_unlock(&(mutex)) < 0) \
  381. { \
  382. EM_debug_error( \
  383. MS_MODULE_ID_##MODULE, \
  384. "FAILED to Unlock Mutex in " #MODULE ".\n"); \
  385. return MODULE##_MUTEX_UNLOCK_FAILED; \
  386. }
  387. /*
  388. * Unlocks the Module Specific Mutex which realeses the global variables
  389. * to be written into. To be used in void functions as it returns
  390. * no error.
  391. */
  392. #define MS_MUTEX_UNLOCK_VOID(mutex, MODULE) \
  393. if (EM_thread_mutex_unlock(&(mutex)) < 0) \
  394. { \
  395. EM_debug_error( \
  396. MS_MODULE_ID_##MODULE, \
  397. "FAILED to Unlock Mutex in " #MODULE ".\n"); \
  398. return; \
  399. }
  400. /*
  401. * Unlocks the Module Specific Mutex which realeses the global variables
  402. * to be written into.
  403. * It returns the error 'value' if mutex unlock failes.
  404. */
  405. #define MS_MUTEX_UNLOCK_RETURN_ON_FAILURE(mutex, MODULE, value) \
  406. if (EM_thread_mutex_unlock(&(mutex)) < 0) \
  407. { \
  408. EM_debug_error( \
  409. MS_MODULE_ID_##MODULE, \
  410. "FAILED to Unlock Mutex in " #MODULE ".\n"); \
  411. return (value); \
  412. }
  413. /*
  414. * Unlocks the Module Specific Mutex which realeses the global variables
  415. * to be written into. On failure, only Error is logged.
  416. * It can be used from both void and non-void functions.
  417. */
  418. #define MS_MUTEX_UNLOCK_DONOT_RETURN_ON_FAILURE(mutex, MODULE) \
  419. if (EM_thread_mutex_unlock(&(mutex)) < 0) \
  420. { \
  421. EM_debug_error( \
  422. MS_MODULE_ID_##MODULE, \
  423. "FAILED to Unlock Mutex in " #MODULE ".\n"); \
  424. }
  425. #else /* MS_DISABLE_MUTEX */
  426. /* Macro to define a Mutex Variable */
  427. #define MS_DEFINE_MUTEX(mutex)
  428. /* Macro to define a Mutex Variable with a type qualifier */
  429. #define MS_DEFINE_MUTEX_TYPE(type, mutex)
  430. /* Macro to define a Conditional Variable */
  431. #define MS_DEFINE_COND(cond)
  432. /* Macro to define a Conditional Variable with a type qualifier */
  433. #define MS_DEFINE_COND_TYPE(type, cond)
  434. /*
  435. * Macro to Initialize Mutex.
  436. * To be used in void function as it returns no error.
  437. */
  438. #define MS_MUTEX_INIT_VOID(mutex, MODULE)
  439. /*
  440. * Macro to Initialize Mutex.
  441. * It returns an error if mutex initialization fails.
  442. */
  443. #define MS_MUTEX_INIT(mutex, MODULE)
  444. /*
  445. * Macro to Initialize Conditional Variable.
  446. * To be used in void function as it returns no error.
  447. */
  448. #define MS_COND_INIT_VOID(cond, MODULE)
  449. /*
  450. * Macro to Initialize Conditional Variable.
  451. * It returns an error if conditional variable initialization fails.
  452. */
  453. #define MS_COND_INIT(cond, MODULE)
  454. /*
  455. * Locks the Module Specific Mutex which prevents any global variable being
  456. * overwritten by any function. It returns an error if mutex lock fails.
  457. */
  458. #define MS_MUTEX_LOCK(mutex, MODULE)
  459. /*
  460. * Locks the Module Specific Mutex which prevents any global variable being
  461. * overwritten by any function. To be used in void function as it
  462. * returns no error.
  463. */
  464. #define MS_MUTEX_LOCK_VOID(mutex, MODULE)
  465. /*
  466. * Locks the Module Specific Mutex which prevents any global variable being
  467. * overwritten by any function.
  468. * It returns the error 'value' if mutex lock failes.
  469. */
  470. #define MS_MUTEX_LOCK_RETURN_ON_FAILURE(mutex, MODULE, value)
  471. /*
  472. * Locks the Module Specific Mutex which prevents any global variable being
  473. * overwritten by any function. On failure, only an Error is logged.
  474. * It can be used from both void and non-void functions.
  475. */
  476. #define MS_MUTEX_LOCK_DONOT_RETURN_ON_FAILURE(mutex, MODULE)
  477. /*
  478. * Unlocks the Module Specific Mutex which realeses the global variables
  479. * to be written into. It returns an error if mutex unlock fails.
  480. */
  481. #define MS_MUTEX_UNLOCK(mutex, MODULE)
  482. /*
  483. * Unlocks the Module Specific Mutex which realeses the global variables
  484. * to be written into. To be used in void functions as it returns
  485. * no error.
  486. */
  487. #define MS_MUTEX_UNLOCK_VOID(mutex, MODULE)
  488. /*
  489. * Unlocks the Module Specific Mutex which realeses the global variables
  490. * to be written into.
  491. * It returns the error 'value' if mutex unlock failes.
  492. */
  493. #define MS_MUTEX_UNLOCK_RETURN_ON_FAILURE(mutex, MODULE, value)
  494. /*
  495. * Unlocks the Module Specific Mutex which realeses the global variables
  496. * to be written into. On failure, only Error is logged.
  497. * It can be used from both void and non-void functions.
  498. */
  499. #define MS_MUTEX_UNLOCK_DONOT_RETURN_ON_FAILURE(mutex, MODULE)
  500. #endif /* MS_DISABLE_MUTEX */
  501. /* Abstractions for bit-wise operation */
  502. #define MS_EXTRACT_BITNUM(val, bitnum) (((val) >> (bitnum)) & 1)
  503. #define MS_SET_BITNUM(val, bitnum) ((val) |= (1 << (bitnum)))
  504. #define MS_CLR_BITNUM(val, bitnum) ((val) &= (~(1 << (bitnum))))
  505. /* Macro to find Minimum and Maximum value */
  506. #define MS_GET_MIN(a, b) \
  507. (((a) > (b)) ? (b) : (a))
  508. #define MS_GET_MAX(a, b) \
  509. (((a) > (b)) ? (a) : (b))
  510. /* Unreferenced variable macro to avoid compilation warnings */
  511. #define MS_IGNORE_UNUSED_PARAM(v) (void)(v)
  512. /* Loop for ever */
  513. #define MS_LOOP_FOREVER() for(;;)
  514. #ifdef MS_HAVE_DYNAMIC_CONFIG
  515. #define MS_INIT_CONFIG(config) \
  516. (config).config_MS_REPLAY_CACHE_SIZE = MS_REPLAY_CACHE_SIZE; \
  517. (config).config_MS_DEFAULT_COMPANY_ID = MS_DEFAULT_COMPANY_ID; \
  518. (config).config_MS_DEFAULT_PID = MS_DEFAULT_PID; \
  519. (config).config_MS_DEFAULT_VID = MS_DEFAULT_VID
  520. #else
  521. #define MS_INIT_CONFIG(config)
  522. #endif /* MS_HAVE_DYNAMIC_CONFIG */
  523. #define MS_CONFIG_LIMITS(x) (x)
  524. #define MS_DEFINE_GLOBAL_ARRAY(type, var, s) \
  525. type var[(s)]
  526. #define MS_DECLARE_GLOBAL_ARRAY(type, var, s) \
  527. extern type var[(s)]
  528. #define MS_INIT_GLOBAL_ARRAY(type, var, s, i) \
  529. EM_mem_set(var, (i), ((s) * sizeof(type)))
  530. #ifdef MS_HAVE_MODEL_OPCODE_EMPTY_HANDLERS
  531. /* Macro to define an empty model Opcode Handler */
  532. #define MODEL_OPCODE_HANDLER_EMPTY_DEF(x) \
  533. static API_RESULT (x) \
  534. ( \
  535. MS_ACCESS_MODEL_HANDLE * handle, \
  536. MS_NET_ADDR saddr, \
  537. MS_NET_ADDR daddr, \
  538. MS_SUBNET_HANDLE subnet_handle, \
  539. MS_APPKEY_HANDLE appkey_handle, \
  540. UINT32 opcode, \
  541. UCHAR * data_param, \
  542. UINT16 data_len \
  543. ) \
  544. { \
  545. API_RESULT retval; \
  546. \
  547. MS_IGNORE_UNUSED_PARAM(handle); \
  548. MS_IGNORE_UNUSED_PARAM(saddr); \
  549. MS_IGNORE_UNUSED_PARAM(daddr); \
  550. MS_IGNORE_UNUSED_PARAM(subnet_handle); \
  551. MS_IGNORE_UNUSED_PARAM(appkey_handle); \
  552. MS_IGNORE_UNUSED_PARAM(opcode); \
  553. MS_IGNORE_UNUSED_PARAM(data_param); \
  554. MS_IGNORE_UNUSED_PARAM(data_len); \
  555. \
  556. retval = API_SUCCESS; \
  557. \
  558. return retval; \
  559. }
  560. /* Callback Handler */
  561. #define MODEL_OPCODE_HANDLER_CALL(handler) \
  562. (handler) (handle, saddr, daddr, subnet_handle, appkey_handle, opcode, data_param, data_len)
  563. #else
  564. /* Macro to define an empty model Opcode Handler */
  565. #define MODEL_OPCODE_HANDLER_EMPTY_DEF(x)
  566. /* Callback Handler */
  567. #define MODEL_OPCODE_HANDLER_CALL(handler)
  568. #endif /* MS_HAVE_MODEL_OPCODE_EMPTY_HANDLERS */
  569. #define MS_STREAM_REV_ENDIANNESS(s, d, n) \
  570. { \
  571. UCHAR i; \
  572. for (i = 0; i < (n); i++) \
  573. { \
  574. (d)[(n - 1) - i] = (s)[i]; \
  575. } \
  576. }
  577. /** \} */
  578. /**
  579. * \addtogroup ms_common_constants Constants
  580. * \{
  581. */
  582. /*
  583. * Module Identifier definitions.
  584. * Currently used for runtime debug enable/disable scenario.
  585. * In future, this can be used for other purposes as well,
  586. * hence these defines are placed under common header file.
  587. */
  588. /* Page 4 - Bluetooth Protocol Modules */
  589. #define MS_MODULE_PAGE_4 0x40000000
  590. /* Module - Bit Mask */
  591. #define MS_MODULE_BIT_MASK_COMMON 0x00000001
  592. #define MS_MODULE_BIT_MASK_BRR 0x00000002
  593. #define MS_MODULE_BIT_MASK_NET 0x00000004
  594. #define MS_MODULE_BIT_MASK_LTRN 0x00000008
  595. #define MS_MODULE_BIT_MASK_TRN 0x00000010
  596. #define MS_MODULE_BIT_MASK_ACCESS 0x00000020
  597. #define MS_MODULE_BIT_MASK_APP 0x00000040
  598. #define MS_MODULE_BIT_MASK_STBX 0x00000080
  599. #define MS_MODULE_BIT_MASK_CONFIG 0x00000100
  600. #define MS_MODULE_BIT_MASK_FSM 0x00000200
  601. #define MS_MODULE_BIT_MASK_PROV 0x00000400
  602. #define MS_MODULE_BIT_MASK_MESH_MODEL 0x00000800
  603. /* Module ID */
  604. #define MS_MODULE_ID_COMMON (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_COMMON)
  605. #define MS_MODULE_ID_BRR (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_BRR)
  606. #define MS_MODULE_ID_NET (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_NET)
  607. #define MS_MODULE_ID_LTRN (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_LTRN)
  608. #define MS_MODULE_ID_TRN (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_TRN)
  609. #define MS_MODULE_ID_ACCESS (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_ACCESS)
  610. #define MS_MODULE_ID_APP (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_APP)
  611. #define MS_MODULE_ID_STBX (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_STBX)
  612. #define MS_MODULE_ID_CONFIG (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_CONFIG)
  613. #define MS_MODULE_ID_FSM (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_FSM)
  614. #define MS_MODULE_ID_PROV (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_PROV)
  615. #define MS_MODULE_ID_MESH_MODEL (MS_MODULE_PAGE_4 | MS_MODULE_BIT_MASK_MESH_MODEL)
  616. /** Device UUID Size */
  617. #define MS_DEVICE_UUID_SIZE 16
  618. /** Beacon Type - Size */
  619. #define MS_BCON_TYPE_SIZE 1
  620. /** Beacon OOB Indicator Size */
  621. #define MS_BCON_OOB_IND_SIZE 2
  622. /** Beacon URI Hash Size */
  623. #define MS_BCON_URI_HASH_SIZE 4
  624. /** Beacon Types */
  625. /** Unprovisioned Device Beacon Type */
  626. #define MS_BCON_TYPE_UNPRVSNG_DEV 0x00
  627. /* Secure Network Beacon Type */
  628. #define MS_BCON_TYPE_SECURE 0x01
  629. /** Friend Role */
  630. /* Invalid */
  631. #define MS_FRND_ROLE_INVALID 0x00
  632. /* Friend */
  633. #define MS_FRND_ROLE_FRIEND 0x01
  634. /* LPN */
  635. #define MS_FRND_ROLE_LPN 0x02
  636. /** Relay Feature */
  637. #define MS_FEATURE_RELAY 0x00
  638. /** Proxy Feature */
  639. #define MS_FEATURE_PROXY 0x01
  640. /** Friend Feature */
  641. #define MS_FEATURE_FRIEND 0x02
  642. /** Low Power Feature */
  643. #define MS_FEATURE_LPN 0x03
  644. /** Secure Nework Beacon */
  645. #define MS_FEATURE_SEC_NET_BEACON 0x04
  646. /** Operation: Enable */
  647. #define MS_ENABLE 0x01
  648. /** Operation: Disable */
  649. #define MS_DISABLE 0x00
  650. /**
  651. * Feature not supported.
  652. * Used as stutus for Get/Set Friend/Proxy etc.,
  653. * when the feature is not supported.
  654. */
  655. #define MS_NOT_SUPPORTED 0x02
  656. /** Network Tx State */
  657. #define MS_NETWORK_TX_STATE 0x00
  658. /** Relay Tx State */
  659. #define MS_RELAY_TX_STATE 0x01
  660. /** Label UUID Length. Associated with Virtual Address */
  661. #define MS_LABEL_UUID_LENGTH 16
  662. /** \} */
  663. /* -------------------------------------------- Structures/Data Types */
  664. /**
  665. * \addtogroup ms_common_defines Defines
  666. * \{
  667. */
  668. /**
  669. * \defgroup ms_common_structures Structures
  670. * \{
  671. */
  672. /** Payload type */
  673. typedef struct _MS_BUFFER
  674. {
  675. /* Payload Pointer */
  676. UCHAR * payload;
  677. /* Payload Length */
  678. UINT16 length;
  679. } MS_BUFFER;
  680. /**
  681. * Dynamic configuration of Mesh Datastructure.
  682. * Used only if 'MS_HAVE_DYNAMIC_GLOBAL_ARRAY' is defined.
  683. */
  684. typedef struct _MS_CONFIG
  685. {
  686. /** The size of the Replay Protection cache. */
  687. UINT16 config_MS_REPLAY_CACHE_SIZE;
  688. /** Company ID */
  689. UINT16 config_MS_DEFAULT_COMPANY_ID;
  690. /** Product ID */
  691. UINT16 config_MS_DEFAULT_PID;
  692. /** Vendor ID */
  693. UINT16 config_MS_DEFAULT_VID;
  694. } MS_CONFIG;
  695. /** \} */
  696. /** \} */
  697. /* -------------------------------------------- Function/API Declarations */
  698. #ifdef __cplusplus
  699. extern "C"{
  700. #endif
  701. #ifdef MS_HAVE_DYNAMIC_CONFIG
  702. /* Global Configuration for Mesh Stack */
  703. extern MS_CONFIG ms_global_config;
  704. #endif /* MS_HAVE_DYNAMIC_CONFIG */
  705. /**
  706. * \defgroup ms_common_api API Definitions
  707. * \{
  708. */
  709. /**
  710. * API to initialize Mesh Stack. This is the first API that the
  711. * application should call before any other API. This function
  712. * initializes all the internal stack modules and creates necessary tasks.
  713. *
  714. * \note
  715. */
  716. /**
  717. * \brief To initialize Mesh Stack.
  718. *
  719. * \par Description
  720. * API to initialize Mesh Stack. This is the first API that the
  721. * application should call before any other API. This function
  722. * initializes all the internal stack modules and data structures.
  723. *
  724. * \param [in] blob
  725. * If 'MS_HAVE_DYNAMIC_CONFIG' defined,
  726. * application shall provide the desired dynamic configuration
  727. * using a pointer to MS_CONFIG data structure instance.
  728. * else,
  729. * this parameter shall be NULL and ignored by the API.
  730. *
  731. * \return API_SUCCESS or an error code indicating reason for failure
  732. */
  733. void MS_init
  734. (
  735. /* IN */ void * blob
  736. );
  737. /**
  738. * API to turn off Bluetooth Hardware. This API should be called after
  739. * \ref MS_init.
  740. *
  741. * \return
  742. * \ref API_RESULT on successful Bluetooth OFF
  743. */
  744. API_RESULT MS_shutdown
  745. (
  746. void
  747. );
  748. /**
  749. * \brief To start transition timer.
  750. *
  751. * \par Description
  752. * API to start a transition timer.
  753. *
  754. * \param [in] transition
  755. * State Transition data structure, which includes the timeout,
  756. * transition start and complete callback etc.
  757. *
  758. * \param [out] transition_time_handle
  759. * Transition Time Handle, which can be used to stop the transition
  760. * timer if required.
  761. *
  762. * \return API_SUCCESS or an error code indicating reason for failure
  763. */
  764. API_RESULT MS_common_start_transition_timer
  765. (
  766. /* IN */ MS_ACCESS_STATE_TRANSITION_TYPE * transition,
  767. /* OUT */ UINT16 * transition_time_handle
  768. );
  769. /**
  770. * \brief To stop transition timer.
  771. *
  772. * \par Description
  773. * API to stop a transition timer.
  774. *
  775. * \param [in] transition_time_handle
  776. * Transition Time Handle, returned by the Start Transition Timer
  777. * interface.
  778. *
  779. * \return API_SUCCESS or an error code indicating reason for failure
  780. */
  781. API_RESULT MS_common_stop_transition_timer
  782. (
  783. /* IN */ UINT16 transition_time_handle
  784. );
  785. /**
  786. * \brief To get remaining Transition Time.
  787. *
  788. * \par Description
  789. * API to get remaining Transition Time.
  790. *
  791. * \param [in] transition_time_handle
  792. * Transition Time Handle, returned by the Start Transition Timer
  793. * interface.
  794. *
  795. * \param [out] remaining_transition_time
  796. * Remaining Transition Time.
  797. *
  798. * \return API_SUCCESS or an error code indicating reason for failure
  799. */
  800. API_RESULT MS_common_get_remaining_transition_time
  801. (
  802. /* IN */ UINT16 transition_time_handle,
  803. /* OUT */ UINT8 * remaining_transition_time
  804. );
  805. /**
  806. * \brief To get remaining Transition Time, with offset.
  807. *
  808. * \par Description
  809. * API to get remaining Transition Time with offset in ms.
  810. *
  811. * \param [in] transition_time_handle
  812. * Transition Time Handle, returned by the Start Transition Timer
  813. * interface.
  814. *
  815. * \param [in] offset_in_ms
  816. * Offset in ms.
  817. *
  818. * \param [out] remaining_transition_time
  819. * Remaining Transition Time.
  820. *
  821. * \return API_SUCCESS or an error code indicating reason for failure
  822. */
  823. API_RESULT MS_common_get_remaining_transition_time_with_offset
  824. (
  825. /* IN */ UINT16 transition_time_handle,
  826. /* IN */ UINT32 offset_in_ms,
  827. /* OUT */ UINT8 * remaining_transition_time
  828. );
  829. /**
  830. * \brief To convert transition time from milisecond.
  831. *
  832. * \par Description
  833. * API to convert transition timer in milisecond to Generic Default
  834. * Transition Time state format.
  835. *
  836. * \param [in] transition_time_in_ms
  837. * Transition Time in milisecond.
  838. *
  839. * \param [out] transition_time
  840. * Converted value in Generic Default Transition Time state format.
  841. *
  842. * \return API_SUCCESS or an error code indicating reason for failure
  843. */
  844. API_RESULT MS_common_get_transition_time_from_ms
  845. (
  846. /* IN */ UINT32 transition_time_in_ms,
  847. /* OUT */ UINT8 * transition_time
  848. );
  849. /**
  850. * \cond ignore_this Ignore this function while generating doxygen document
  851. */
  852. /* Internal Function */
  853. API_RESULT ms_common_init_transition_timer(void);
  854. /* Internal Function */
  855. API_RESULT ms_internal_verificaiton_check(void);
  856. /**
  857. * \endcond
  858. */
  859. #ifdef __cplusplus
  860. };
  861. #endif
  862. /** \} */
  863. /** \} */
  864. #endif /* _H_MS_COMMON_ */