PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/FNET/fnet_demos/common/fnet_application/fapp_ping.c

https://gitlab.com/fuggles/ucos
C | 249 lines | 155 code | 28 blank | 66 comment | 49 complexity | 4c44cb345aaa95d5a5fb91be7c3c85e8 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /**************************************************************************
  2. *
  3. * Copyright 2011-2015 by Andrey Butok. FNET Community.
  4. *
  5. ***************************************************************************
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License Version 3
  8. * or later (the "LGPL").
  9. *
  10. * As a special exception, the copyright holders of the FNET project give you
  11. * permission to link the FNET sources with independent modules to produce an
  12. * executable, regardless of the license terms of these independent modules,
  13. * and to copy and distribute the resulting executable under terms of your
  14. * choice, provided that you also meet, for each linked independent module,
  15. * the terms and conditions of the license of that module.
  16. * An independent module is a module which is not derived from or based
  17. * on this library.
  18. * If you modify the FNET sources, you may extend this exception
  19. * to your version of the FNET sources, but you are not obligated
  20. * to do so. If you do not wish to do so, delete this
  21. * exception statement from your version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * and the GNU Lesser General Public License along with this program.
  29. * If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. **********************************************************************/ /*!
  32. *
  33. * @file fapp_ping.c
  34. *
  35. * @author Andrey Butok
  36. *
  37. * @brief FNET Shell Demo implementation (Ping).
  38. *
  39. ***************************************************************************/
  40. #include "fapp.h"
  41. #include "fapp_prv.h"
  42. #if FNET_CFG_PING && (FAPP_CFG_PING_CMD || FAPP_CFG_PING6_CMD)
  43. #include "fapp_ping.h"
  44. /************************************************************************
  45. * Definitions.
  46. *************************************************************************/
  47. #define FAPP_PING_BUFFER_SIZE (100)
  48. #define FAPP_PING_IDENTIFIER (1)
  49. #define FAPP_PING_STR_REPLY "Reply from %s"
  50. #define FAPP_PING_STR_TIMEOUT "Request timed out."
  51. /************************************************************************
  52. * Function Prototypes
  53. *************************************************************************/
  54. static void fapp_ping_handler(int result, unsigned long packet_count, struct sockaddr *target_addr, long cookie);
  55. static void fapp_ping_on_ctrlc(fnet_shell_desc_t desc);
  56. /************************************************************************
  57. * NAME: fapp_ping_handler
  58. *
  59. * DESCRIPTION:
  60. ************************************************************************/
  61. static void fapp_ping_handler (int result, unsigned long packet_count, struct sockaddr *target_addr, long cookie)
  62. {
  63. char ip_str[FNET_IP_ADDR_STR_SIZE];
  64. fnet_shell_desc_t desc = (fnet_shell_desc_t)cookie;
  65. if(result == FNET_OK)
  66. {
  67. fnet_shell_println(desc, FAPP_PING_STR_REPLY, fnet_inet_ntop(target_addr->sa_family, target_addr->sa_data, ip_str, sizeof(ip_str)));
  68. }
  69. else if(result == (int)FNET_ERR_TIMEDOUT)
  70. {
  71. fnet_shell_println(desc, FAPP_PING_STR_TIMEOUT);
  72. }
  73. else
  74. {
  75. fnet_shell_println(desc, "Error = %d", result);
  76. }
  77. if(packet_count == 0U)
  78. {
  79. fnet_shell_unblock(desc); /* Unblock the shell. */
  80. }
  81. }
  82. /************************************************************************
  83. * NAME: fapp_ping_on_ctrlc
  84. *
  85. * DESCRIPTION:
  86. ************************************************************************/
  87. static void fapp_ping_on_ctrlc(fnet_shell_desc_t desc)
  88. {
  89. /* Release PING. */
  90. fnet_ping_release();
  91. fnet_shell_println( desc, FAPP_CANCELLED_STR);
  92. }
  93. /************************************************************************
  94. * NAME: fapp_ping_cmd
  95. *
  96. * DESCRIPTION: Ping command.
  97. ************************************************************************/
  98. void fapp_ping_cmd( fnet_shell_desc_t desc, int argc, char ** argv )
  99. {
  100. struct fnet_ping_params ping_params;
  101. int i;
  102. char *p;
  103. unsigned long value;
  104. char ip_str[FNET_IP_ADDR_STR_SIZE];
  105. fnet_memset_zero(&ping_params, sizeof(ping_params));
  106. ping_params.cookie = desc;
  107. ping_params.handler = fapp_ping_handler;
  108. ping_params.packet_size = FAPP_PING_DEFAULT_SIZE;
  109. ping_params.timeout = FAPP_PING_DEFAULT_TIMEOUT;
  110. ping_params.pattern = FAPP_PING_DEFAULT_PATTERN;
  111. ping_params.ttl = FAPP_PING_DEFAULT_HOP_LIMIT;
  112. ping_params.packet_count = FAPP_PING_DEFAULT_NUMBER;
  113. /* Last parameter must be ip address.*/
  114. i = argc-1;
  115. if(fnet_inet_ptos(argv[i], &ping_params.target_addr) == FNET_ERR)
  116. {
  117. goto ERROR_PARAMETER;
  118. }
  119. else
  120. {
  121. /* TBD Optimise parameters parsing.*/
  122. if(argc > 2) /* There are additional parameters */
  123. {
  124. /* [-c <count>][-i <seconds>]\n\t[-p <pattern>][-s <size>][-h <hoplimit/ttl>] */
  125. for(i=1; i<argc-1; i++)
  126. {
  127. if (!fnet_strcmp(argv[i], "-c"))
  128. {
  129. i++;
  130. value = fnet_strtoul(argv[i], &p, 10);
  131. if ((value == 0U) && (p == argv[i]))
  132. {
  133. goto ERROR_PARAMETER;
  134. }
  135. else
  136. ping_params.packet_count = value;
  137. }
  138. else if (!fnet_strcmp(argv[i], "-i"))
  139. {
  140. i++;
  141. value = fnet_strtoul(argv[i], &p, 10);
  142. if ((value == 0U) && (p == argv[i]))
  143. {
  144. goto ERROR_PARAMETER;
  145. }
  146. else
  147. ping_params.timeout = value*1000U;
  148. }
  149. else if (!fnet_strcmp(argv[i], "-p"))
  150. {
  151. i++;
  152. value = fnet_strtoul(argv[i], &p, 10);
  153. if ((value == 0U) && (p == argv[i]))
  154. {
  155. goto ERROR_PARAMETER;
  156. }
  157. else
  158. ping_params.pattern = (unsigned char)value;
  159. }
  160. else if (!fnet_strcmp(argv[i], "-s"))
  161. {
  162. i++;
  163. value = fnet_strtoul(argv[i], &p, 10);
  164. if ((value == 0U) && (p == argv[i]))
  165. {
  166. goto ERROR_PARAMETER;
  167. }
  168. else
  169. ping_params.packet_size = (unsigned short)value;
  170. }
  171. else if (!fnet_strcmp(argv[i], "-h"))
  172. {
  173. i++;
  174. value = fnet_strtoul(argv[i], &p, 10);
  175. if ((value == 0U) && (p == argv[i]))
  176. {
  177. goto ERROR_PARAMETER;
  178. }
  179. else
  180. ping_params.ttl = (unsigned char)value;
  181. }
  182. else if (!fnet_strcmp(argv[i], "-n"))
  183. {
  184. /* Just ignore the -n parameter.*/
  185. }
  186. else if (!fnet_strcmp(argv[i], "-I"))
  187. {
  188. i++;
  189. /* Just ignore the -I parameter and its value.*/
  190. }
  191. else /* Wrong parameter.*/
  192. {
  193. goto ERROR_PARAMETER;
  194. }
  195. }
  196. }
  197. if(fnet_ping_request(&ping_params) == FNET_OK)
  198. {
  199. fnet_shell_println(desc, FAPP_DELIMITER_STR);
  200. fnet_shell_println(desc, " PING" );
  201. fnet_shell_println(desc, FAPP_DELIMITER_STR);
  202. fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Remote IP addr", fnet_inet_ntop(ping_params.target_addr.sa_family, ping_params.target_addr.sa_data, ip_str, sizeof(ip_str)));
  203. fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Message Size", ping_params.packet_size>FNET_CFG_PING_PACKET_MAX?FNET_CFG_PING_PACKET_MAX:ping_params.packet_size);
  204. fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Num. of messages", ping_params.packet_count);
  205. fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Pattern", ping_params.pattern);
  206. fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Hoplimit (TTL)", ping_params.ttl);
  207. fnet_shell_println(desc, FAPP_TOCANCEL_STR);
  208. fnet_shell_println(desc, FAPP_DELIMITER_STR);
  209. fnet_shell_block(desc, fapp_ping_on_ctrlc); /* Block shell. */
  210. }
  211. else
  212. {
  213. fnet_shell_println(desc, FAPP_INIT_ERR, "PING");
  214. }
  215. }
  216. return;
  217. ERROR_PARAMETER:
  218. fnet_shell_println(desc, FAPP_PARAM_ERR, argv[i]);
  219. return;
  220. }
  221. #endif /* FAPP_CFG_PING_CMD || FAPP_CFG_PING6_CMD */