PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Functions.ino 11KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. void pollthread() {
  2. elapsedMicros pollTimer;
  3. Serial.println("Poll Thread Active!");
  4. while(1){
  5. if(pollTimer >= 1000){
  6. pollTimer -= 1000;
  7. checkLink();
  8. if(fnet_netif_is_initialized(current_netif)){
  9. fnet_poll();
  10. }
  11. #ifdef STATS
  12. LoopedPoll++;
  13. printStats();
  14. #endif
  15. }
  16. else{
  17. threads.yield();
  18. }
  19. }
  20. }
  21. void dhcp_cln_callback_updated(fnet_dhcp_cln_desc_t _dhcp_desc, fnet_netif_desc_t netif, void *p ) { //Called when DHCP updates
  22. struct fnet_dhcp_cln_options current_options;
  23. fnet_dhcp_cln_get_options(dhcp_desc, &current_options);
  24. uint8_t *ip = (uint8_t*)&current_options.ip_address.s_addr;
  25. Serial.print("IPAddress: ");
  26. Serial.print((uint8_t)*ip++);
  27. Serial.print(".");
  28. Serial.print((uint8_t)*ip++);
  29. Serial.print(".");
  30. Serial.print((uint8_t)*ip++);
  31. Serial.print(".");
  32. Serial.println((uint8_t)*ip);
  33. ip = (uint8_t*)&current_options.netmask.s_addr;
  34. Serial.print("SubnetMask: ");
  35. Serial.print((uint8_t)*ip++);
  36. Serial.print(".");
  37. Serial.print((uint8_t)*ip++);
  38. Serial.print(".");
  39. Serial.print((uint8_t)*ip++);
  40. Serial.print(".");
  41. Serial.println((uint8_t)*ip);
  42. ip = (uint8_t*)&current_options.gateway.s_addr;
  43. Serial.print("Gateway: ");
  44. Serial.print((uint8_t)*ip++);
  45. Serial.print(".");
  46. Serial.print((uint8_t)*ip++);
  47. Serial.print(".");
  48. Serial.print((uint8_t)*ip++);
  49. Serial.print(".");
  50. Serial.println((uint8_t)*ip);
  51. ip = (uint8_t*)&current_options.dhcp_server.s_addr;
  52. Serial.print("DHCPServer: ");
  53. Serial.print((uint8_t)*ip++);
  54. Serial.print(".");
  55. Serial.print((uint8_t)*ip++);
  56. Serial.print(".");
  57. Serial.print((uint8_t)*ip++);
  58. Serial.print(".");
  59. Serial.println((uint8_t)*ip);
  60. Serial.print("State: ");
  61. Serial.println(fnet_dhcp_cln_get_state(_dhcp_desc));
  62. Serial.println();
  63. Serial.println();
  64. }
  65. void checkLink(){
  66. // Serial.print("Link: ");
  67. // Serial.println(linkStatus());
  68. if(linkStatus() && fnet_dhcp_cln_is_enabled(dhcp_desc)){
  69. // Serial.println("DHCP already running!");
  70. }
  71. else if(linkStatus()){
  72. Serial.println("Initializing DHCP");
  73. fnet_memset_zero(&dhcp_desc, sizeof(dhcp_desc));
  74. fnet_memset_zero(&dhcp_params, sizeof(dhcp_params));
  75. dhcp_params.netif = current_netif;
  76. // Enable DHCP client.
  77. if((dhcp_desc = fnet_dhcp_cln_init(&dhcp_params))){
  78. /*Register DHCP event handler callbacks.*/
  79. fnet_dhcp_cln_set_callback_updated(dhcp_desc, dhcp_cln_callback_updated, (void*)dhcp_desc);
  80. fnet_dhcp_cln_set_callback_discover(dhcp_desc, dhcp_cln_callback_updated, (void*)dhcp_desc);
  81. Serial.println("DHCP initialization done!");
  82. bench_srv_init();
  83. }
  84. else{
  85. Serial.println("ERROR: DHCP initialization failed!");
  86. }
  87. }
  88. else if(!linkStatus() && fnet_dhcp_cln_is_enabled(dhcp_desc)){
  89. Serial.println("DHCP Released");
  90. fnet_dhcp_cln_release(dhcp_desc);
  91. fnet_memset_zero(dhcp_desc, sizeof(dhcp_desc));
  92. bench_srv_release();
  93. // fnet_release();
  94. }
  95. else if(!linkStatus()){
  96. // Serial.println("DHCP already released!");
  97. }
  98. }
  99. uint8_t linkStatus(){
  100. uint16_t reg_data;
  101. fnet_eth_phy_read(fnet_netif_get_default(), 0x01, &reg_data);
  102. return (reg_data & (1 << 2)) ? true : false;
  103. }
  104. fnet_bool_t handleIsConnected() {
  105. return linkStatus() ? FNET_TRUE : FNET_FALSE;
  106. }
  107. fnet_bench_srv_params_t bench_srv_params;
  108. fnet_bench_srv_desc_t bench_srv_desc;
  109. void bench_srv_init(){
  110. /* Set Benchmark server parameters.*/
  111. fnet_memset_zero(&bench_srv_params, sizeof(bench_srv_params));
  112. bench_srv_params.type = SOCK_STREAM; /* TCP by default */
  113. if(current_netif){ /* Only on one interface */
  114. bench_srv_params.address.sa_scope_id = fnet_netif_get_scope_id(current_netif);
  115. }
  116. /* Start Benchmark server. */
  117. bench_srv_desc = fnet_bench_srv_init(&bench_srv_params);
  118. if(bench_srv_desc){
  119. /* Instal callbacks */
  120. // fnet_bench_srv_set_callback_session_begin (fapp_bench_srv_desc, fapp_bench_srv_callback_session_begin, shell_desc);
  121. fnet_bench_srv_set_callback_session_end (bench_srv_desc, bench_srv_callback_session_end, bench_srv_desc);
  122. }
  123. }
  124. void bench_srv_release(void){
  125. fnet_bench_srv_release(bench_srv_desc);
  126. bench_srv_desc = 0;
  127. }
  128. static void bench_srv_callback_session_end(fnet_bench_srv_desc_t desc, const struct fnet_bench_srv_result *bench_srv_result, void *cookie) {
  129. if(bench_srv_result){
  130. uint64_t totalBytes = (bench_srv_result->megabytes * 1000000) + bench_srv_result->bytes;
  131. // Serial.println("Benchmark results:");
  132. Serial.print("Megabytes: ");
  133. Serial.print(totalBytes / 1000000.0, 6);
  134. Serial.print(" Seconds: ");
  135. Serial.print(bench_srv_result->time_ms/1000.0, 4);
  136. // Serial.print("Bytes/Sec: ");
  137. // Serial.println(totalBytes/(bench_srv_result->time_ms/1000.0), 4);
  138. // Serial.print("KBytes/Sec: ");
  139. // Serial.println((totalBytes/(bench_srv_result->time_ms/1000.0))/1000.0, 4);
  140. Serial.print(" KBits/Sec: ");
  141. Serial.println((((totalBytes/(bench_srv_result->time_ms/1000.0))/1000.0)*8), 4);
  142. // Serial.println();
  143. }
  144. }
  145. fnet_bench_cln_desc_t teensy_bench_cln_desc;
  146. void bench_cln_cmd(uint8_t argc, char* argv){
  147. fnet_bench_cln_params_t bench_cln_params;
  148. fnet_netif_desc_t netif = current_netif;
  149. fnet_bench_cln_desc_t bench_cln_desc;
  150. fnet_char_t ip_str[FNET_IP_ADDR_STR_SIZE_MAX];
  151. uint8_t i;
  152. /* Set Benchmark client parameters.*/
  153. fnet_memset_zero(&bench_cln_params, sizeof(bench_cln_params));
  154. /* Default values */
  155. bench_cln_params.type = SOCK_STREAM; /* TCP by default */
  156. bench_cln_params.message_size = 1272; /* Default message size */
  157. bench_cln_params.message_number = 10000; /* Default number of messages */
  158. /* -a <remote ip> [tcp|udp] [-m <message size>] [-mn <number of messages>] */
  159. for(i = 0; i < (argc); i++){
  160. if (!fnet_strcmp(&argv[i], "-a")){ /*[-a <remote ip>] */
  161. i += 3;
  162. if(i < argc){
  163. if(fnet_inet_ptos(&argv[i], &bench_cln_params.address) == FNET_ERR){
  164. goto ERROR_PARAMETER;
  165. }
  166. while(argv[i] != '\0'){
  167. i++;
  168. }
  169. }
  170. else{
  171. goto ERROR_PARAMETER;
  172. }
  173. }
  174. else if (!fnet_strcmp(&argv[i], "tcp")){ /* TCP */
  175. bench_cln_params.type = SOCK_STREAM;
  176. while(argv[i] != '\0'){
  177. i++;
  178. }
  179. }
  180. else if (!fnet_strcmp(&argv[i], "udp")){ /* udp */
  181. bench_cln_params.type = SOCK_DGRAM;
  182. while(argv[i] != '\0'){
  183. i++;
  184. }
  185. }
  186. else if (!fnet_strcmp(&argv[i], "-m")){ /* [-m <message size>] */
  187. fnet_char_t *p = 0;
  188. i += 3;
  189. if(i < argc){
  190. bench_cln_params.message_size = fnet_strtoul(&argv[i], &p, 0);
  191. if (bench_cln_params.message_size == 0){
  192. goto ERROR_PARAMETER;
  193. }
  194. }
  195. else{
  196. goto ERROR_PARAMETER;
  197. }
  198. while(argv[i] != '\0'){
  199. i++;
  200. }
  201. }
  202. else if (!fnet_strcmp(&argv[i], "-mn")){ /* [-mn <number of messages>] */
  203. fnet_char_t *p = 0;
  204. i += 4;
  205. if(i < argc){
  206. bench_cln_params.message_number = fnet_strtoul(&argv[i], &p, 0);
  207. if (bench_cln_params.message_number == 0){
  208. goto ERROR_PARAMETER;
  209. }
  210. }
  211. else{
  212. goto ERROR_PARAMETER;
  213. }
  214. while(argv[i] != '\0'){
  215. i++;
  216. }
  217. }
  218. else if (argv[i] == '\0'){
  219. }
  220. else{/* Wrong parameter.*/
  221. goto ERROR_PARAMETER;
  222. }
  223. }
  224. if(fnet_socket_addr_is_unspecified(&bench_cln_params.address) == FNET_TRUE){ /* Address is not set. */
  225. Serial.println("Error: <remote ip> is not set");
  226. }
  227. else{
  228. if(netif){ /* Only on one interface */
  229. bench_cln_params.address.sa_scope_id = fnet_netif_get_scope_id(netif);
  230. }
  231. bench_cln_params.callback = bench_cln_callback_session_end; /* Callback function.*/
  232. /* Run Benchmark client. */
  233. bench_cln_desc = fnet_bench_cln_init(&bench_cln_params);
  234. if(bench_cln_desc){
  235. teensy_bench_cln_desc = bench_cln_desc;
  236. //
  237. Serial.println("Benchmark client started.");
  238. Serial.print("Protocol: ");
  239. Serial.println((bench_cln_params.type == SOCK_STREAM) ? "TCP" : "UDP");
  240. Serial.print("Remote IP Addr: ");
  241. Serial.println(fnet_inet_ntop(bench_cln_params.address.sa_family, bench_cln_params.address.sa_data, ip_str, sizeof(ip_str)));
  242. Serial.print("Remote Port: ");
  243. Serial.println(FNET_NTOHS(FNET_CFG_BENCH_CLN_PORT));
  244. Serial.print("Message Size: ");
  245. Serial.println(bench_cln_params.message_size);
  246. Serial.print("Num. of messages: ");
  247. Serial.println(bench_cln_params.message_number);
  248. Serial.println();
  249. }
  250. else{
  251. Serial.println("Benchmark client failed to start.");
  252. }
  253. }
  254. return;
  255. ERROR_PARAMETER:
  256. Serial.print("Bad parameter: ");
  257. Serial.println(&argv[i]);
  258. return;
  259. }
  260. static void bench_cln_callback_session_end(fnet_bench_cln_desc_t bench_cln_desc, const fnet_bench_cln_result_t *bench_cln_result, void *cookie){
  261. if(bench_cln_result){
  262. // fapp_bench_print_results (shell_desc, bench_cln_result->megabytes, bench_cln_result->bytes, bench_cln_result->time_ms);
  263. uint64_t totalBytes = (bench_cln_result->megabytes * 1000000) + bench_cln_result->bytes;
  264. // Serial.println("Benchmark results:");
  265. Serial.print("Megabytes: ");
  266. Serial.print(totalBytes / 1000000.0, 6);
  267. Serial.print(" Seconds: ");
  268. Serial.print(bench_cln_result->time_ms/1000.0, 4);
  269. Serial.print(" KBits/Sec: ");
  270. Serial.println((((totalBytes/(bench_cln_result->time_ms/1000.0))/1000.0)*8), 4);
  271. }
  272. }
  273. void serialParser(uint8_t argc, char* argv){
  274. for(uint8_t i = 0; i < (argc) /*avoid the last parameter.*/; i++){
  275. if (!fnet_strcmp(&argv[i], "benchtx")){ /*benchtx */
  276. i++;
  277. if(i < argc){
  278. argv += 8; //Advance length of command + 1
  279. argc -= 8; //Decrease length same amount
  280. Serial.println("Starting benchtx...");
  281. bench_cln_cmd(argc, argv);
  282. return;
  283. }
  284. else{
  285. goto ERROR_PARAMETER;
  286. }
  287. }
  288. else if (!fnet_strcmp(&argv[i], "benchrx")){ /*benchrx */
  289. i++;
  290. if(i < argc){
  291. argv += 8; //Advance length of command + 1
  292. argc -= 8; //Decrease length same amount
  293. // bench_cln_cmd(argc, argv);
  294. Serial.println("Starting benchrx...");
  295. return;
  296. }
  297. else{
  298. goto ERROR_PARAMETER;
  299. }
  300. }
  301. else{/* Wrong parameter.*/
  302. goto ERROR_PARAMETER;
  303. }
  304. }
  305. return;
  306. ERROR_PARAMETER:
  307. Serial.println("Invalid command");
  308. return;
  309. }