appl_proxy.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * \file appl_proxy.c
  3. *
  4. *
  5. */
  6. /*
  7. * Copyright (C) 2017. Mindtree Limited.
  8. * All rights reserved.
  9. */
  10. /* --------------------------------------------- Header File Inclusion */
  11. #include "appl_main.h"
  12. #include "blebrr.h"
  13. #include "MS_net_api.h"
  14. #ifdef MS_PROXY_SUPPORT
  15. /* --------------------------------------------- External Global Variables */
  16. /* --------------------------------------------- Exported Global Variables */
  17. /* --------------------------------------------- Static Global Variables */
  18. DECL_STATIC const char main_proxy_options[] = "\n\
  19. ========= Network Menu =============== \n\
  20. 0. Exit. \n\
  21. 1. Refresh \n\
  22. \n\
  23. 10. Register with Proxy Layer \n\
  24. \n\
  25. 20. Start Proxy ADV with Network ID \n\
  26. 21. Start Proxy ADV with Node Identity \n\
  27. 22. Stop Proxy ADV \n\
  28. \n\
  29. 30. Set WhiteList Filter Type \n\
  30. 31. Set BlackList Filter Type \n\
  31. 32. Add Address to Filter \n\
  32. 33. Remove Address from Filter \n\
  33. \n\
  34. 200. Enable Proxy \n\
  35. 201. Disable Proxy \n\
  36. Your Option ? \0";
  37. /* Global Network Interface Handle : Initialize to MAX Ifaces[Invalid Value] */
  38. NETIF_HANDLE g_appl_netif_hndl;
  39. /* --------------------------------------------- Functions */
  40. void appl_proxy_callback
  41. (
  42. NETIF_HANDLE * handle,
  43. UCHAR p_evt,
  44. UCHAR * data_param,
  45. UINT16 data_len
  46. );
  47. void appl_add_or_del_filter_addr(UCHAR opcode);
  48. void appl_proxy_start_net_id_adv(MS_SUBNET_HANDLE subnet_handle);
  49. void appl_proxy_start_node_identity_adv(MS_SUBNET_HANDLE subnet_handle);
  50. void main_proxy_operations (void);
  51. void main_proxy_operations (void)
  52. {
  53. int choice;
  54. static UINT8 first_time = 0;
  55. if (0 == first_time)
  56. {
  57. g_appl_netif_hndl = (NETIF_HANDLE)MS_CONFIG_LIMITS(MS_NUM_NETWORK_INTERFACES);
  58. first_time = 1;
  59. }
  60. MS_LOOP_FOREVER()
  61. {
  62. printf("%s", main_proxy_options);
  63. scanf("%d", &choice);
  64. if (choice < 0)
  65. {
  66. printf("*** Invalid Choice. Try Again.\n");
  67. continue;
  68. }
  69. switch (choice)
  70. {
  71. case 0:
  72. return;
  73. case 1:
  74. break;
  75. case 10:
  76. /* Register with the Proxy Layer */
  77. MS_proxy_register(appl_proxy_callback);
  78. break;
  79. case 20:
  80. #ifdef MS_PROXY_SERVER
  81. /* Start Proxy ADV with Network ID */
  82. printf("\nEnter Subnet Handle :\n");
  83. scanf("%d", &choice);
  84. appl_proxy_start_net_id_adv((MS_SUBNET_HANDLE)choice);
  85. #else /* MS_PROXY_SERVER */
  86. CONSOLE_OUT(
  87. "Proxy Server Feature Currently Disabled!!!\n");
  88. #endif /* MS_PROXY_SERVER */
  89. break;
  90. case 21:
  91. #ifdef MS_PROXY_SERVER
  92. /* Start Proxy ADV with Node Identity */
  93. printf("\nEnter Subnet Handle :\n");
  94. scanf("%d", &choice);
  95. appl_proxy_start_node_identity_adv((MS_SUBNET_HANDLE)choice);
  96. #else /* MS_PROXY_SERVER */
  97. CONSOLE_OUT(
  98. "Proxy Server Feature Currently Disabled!!!\n");
  99. #endif /* MS_PROXY_SERVER */
  100. break;
  101. case 22:
  102. #ifdef MS_PROXY_SERVER
  103. /* Stop Proxy ADV */
  104. MS_proxy_server_adv_stop();
  105. #else /* MS_PROXY_SERVER */
  106. CONSOLE_OUT(
  107. "Proxy Server Feature Currently Disabled!!!\n");
  108. #endif /* MS_PROXY_SERVER */
  109. break;
  110. case 30:
  111. #ifdef MS_PROXY_CLIENT
  112. /* Set Filter Type as WhiteList */
  113. MS_proxy_set_whitelist_filter(&g_appl_netif_hndl, 0x0000);
  114. #else /* MS_PROXY_CLIENT */
  115. CONSOLE_OUT(
  116. "Proxy Client Feature Currently Disabled!!!\n");
  117. #endif /* MS_PROXY_CLIENT */
  118. break;
  119. case 31:
  120. #ifdef MS_PROXY_CLIENT
  121. /* Set Filter Type as BlackList */
  122. MS_proxy_set_blacklist_filter(&g_appl_netif_hndl, 0x0000);
  123. #else /* MS_PROXY_CLIENT */
  124. CONSOLE_OUT(
  125. "Proxy Client Feature Currently Disabled!!!\n");
  126. #endif /* MS_PROXY_CLIENT */
  127. break;
  128. case 32:
  129. /* Add Address to Filters */
  130. appl_add_or_del_filter_addr(MS_PROXY_ADD_TO_FILTER_OPCODE);
  131. break;
  132. case 33:
  133. /* Remove Address from Filters */
  134. appl_add_or_del_filter_addr(MS_PROXY_REM_FROM_FILTER_OPCODE);
  135. break;
  136. case 200:
  137. /* Enable Proxy */
  138. MS_ENABLE_PROXY_FEATURE();
  139. break;
  140. case 201:
  141. /* Disable Proxy */
  142. MS_DISABLE_PROXY_FEATURE();
  143. break;
  144. default:
  145. printf ("Invalid option %d\n", choice);
  146. }
  147. }
  148. }
  149. void appl_proxy_callback
  150. (
  151. NETIF_HANDLE * handle,
  152. UCHAR p_evt,
  153. UCHAR * data_param,
  154. UINT16 data_len
  155. )
  156. {
  157. PROXY_FILTER_TYPE filter_type;
  158. UINT16 count;
  159. UCHAR role;
  160. MS_IGNORE_UNUSED_PARAM(data_len);
  161. switch(p_evt)
  162. {
  163. case MS_PROXY_UP_EVENT:
  164. CONSOLE_OUT(
  165. "\n\n[PROXY APPL]: MS_PROXY_UP_EVENT Received for NETIF Handle 0x%02X\n\n", *handle);
  166. g_appl_netif_hndl = *handle;
  167. CONSOLE_OUT(
  168. "\n[PROXY APPL]: Enabling Proxy Feature!!\n");
  169. if (NULL != data_param)
  170. {
  171. /* Catch the current role into a local */
  172. role = data_param[0];
  173. if (BRR_SERVER_ROLE == role)
  174. {
  175. /* Enable Proxy */
  176. MS_ENABLE_PROXY_FEATURE();
  177. /* Send Secure Network Beacons */
  178. MS_net_broadcast_secure_beacon(0x0000);
  179. }
  180. }
  181. break;
  182. case MS_PROXY_DOWN_EVENT:
  183. CONSOLE_OUT(
  184. "\n\n[PROXY APPL]: MS_PROXY_DOWN_EVENT Received for NETIF Handle 0x%02X\n\n", *handle);
  185. g_appl_netif_hndl = (NETIF_HANDLE)MS_CONFIG_LIMITS(MS_NUM_NETWORK_INTERFACES);
  186. CONSOLE_OUT(
  187. "\n[PROXY APPL]: Disabling Proxy Feature!!\n");
  188. /* Disable Proxy */
  189. MS_DISABLE_PROXY_FEATURE();
  190. break;
  191. case MS_PROXY_STATUS_EVENT:
  192. /* TODO: Length Check */
  193. /* Extract the Status contents */
  194. filter_type = data_param[0];
  195. MS_UNPACK_BE_2_BYTE(&count, &data_param[1]);
  196. CONSOLE_OUT(
  197. "\n\n[PROXY APPL]: MS_PROXY_STATUS_EVENT Received for NETIF Handle 0x%02X\n"
  198. "Filter Type :- %s\r\n Addr Count :- %04d\n\n",
  199. *handle,
  200. (MS_PROXY_WHITELIST_FILTER == filter_type)? "WhiteList":
  201. "BlackList",
  202. count);
  203. break;
  204. default:
  205. CONSOLE_OUT(
  206. "\n\n[PROXY APPL ERR]: Unknown Event Received for NETIF Handle 0x%02X!!\n\n", *handle);
  207. break;
  208. }
  209. }
  210. void appl_add_or_del_filter_addr(UCHAR opcode)
  211. {
  212. #ifdef MS_PROXY_CLIENT
  213. PROXY_ADDR appl_proxy_addr_list[5];
  214. UINT16 addr_count;
  215. UINT32 index;
  216. int read_in;
  217. /* Read and fill Addresses */
  218. CONSOLE_OUT("Enter Count of Address to be Added (in Hex)\n");
  219. CONSOLE_IN("%x", &read_in);
  220. addr_count = (UINT16)read_in;
  221. for (index = 0; index < addr_count; index++)
  222. {
  223. CONSOLE_OUT("Enter %d'th Address[in HEX]: \n", (index + 1));
  224. CONSOLE_IN("%x", &read_in);
  225. appl_proxy_addr_list[index] = (UINT8)read_in;
  226. }
  227. if (MS_PROXY_ADD_TO_FILTER_OPCODE == opcode)
  228. {
  229. MS_proxy_add_to_list
  230. (
  231. &g_appl_netif_hndl,
  232. 0x0000,
  233. appl_proxy_addr_list,
  234. addr_count
  235. );
  236. }
  237. else
  238. {
  239. MS_proxy_del_from_list
  240. (
  241. &g_appl_netif_hndl,
  242. 0x0000,
  243. appl_proxy_addr_list,
  244. addr_count
  245. );
  246. }
  247. #else /* MS_PROXY_CLIENT */
  248. MS_IGNORE_UNUSED_PARAM(opcode);
  249. CONSOLE_OUT(
  250. "Proxy Client Feature Currently Disabled!!!\n");
  251. #endif /* MS_PROXY_CLIENT */
  252. }
  253. void appl_proxy_register(void)
  254. {
  255. MS_proxy_register(appl_proxy_callback);
  256. }
  257. void appl_proxy_start_net_id_adv(MS_SUBNET_HANDLE subnet_handle)
  258. {
  259. #ifdef MS_PROXY_SERVER
  260. /* Set the role to Proxy with bearer */
  261. blebrr_gatt_mode_set(BLEBRR_GATT_PROXY_MODE);
  262. MS_proxy_server_adv_start
  263. (
  264. subnet_handle,
  265. MS_PROXY_NET_ID_ADV_MODE
  266. );
  267. #else /* MS_PROXY_SERVER */
  268. CONSOLE_OUT(
  269. "Proxy Server Feature Currently Disabled!!!\n");
  270. return API_FAILURE;
  271. #endif /* MS_PROXY_SERVER */
  272. }
  273. void appl_proxy_start_node_identity_adv(MS_SUBNET_HANDLE subnet_handle)
  274. {
  275. #ifdef MS_PROXY_SERVER
  276. /* Set the role to Proxy with bearer */
  277. blebrr_gatt_mode_set(BLEBRR_GATT_PROXY_MODE);
  278. MS_proxy_server_adv_start
  279. (
  280. subnet_handle,
  281. MS_PROXY_NODE_ID_ADV_MODE
  282. );
  283. #else /* MS_PROXY_SERVER */
  284. CONSOLE_OUT(
  285. "Proxy Server Feature Currently Disabled!!!\n");
  286. return API_FAILURE;
  287. #endif /* MS_PROXY_SERVER */
  288. }
  289. void appl_proxy_adv
  290. (
  291. UCHAR identification_type,
  292. MS_SUBNET_HANDLE subnet_handle
  293. )
  294. {
  295. #ifdef MS_PROXY_SERVER
  296. (MS_PROXY_NET_ID_ADV_MODE == identification_type) ?
  297. appl_proxy_start_net_id_adv(subnet_handle) : appl_proxy_start_node_identity_adv(subnet_handle);
  298. #else /* MS_PROXY_SERVER */
  299. CONSOLE_OUT(
  300. "Proxy Server Feature Currently Disabled!!!\n");
  301. return API_FAILURE;
  302. #endif /* MS_PROXY_SERVER */
  303. }
  304. #endif /* MS_PROXY_SUPPORT */