bexus_udp_client.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "bexus_udp_client.h"
  2. extern struct netif gnetif;
  3. extern ip4_addr_t ipaddr; //own
  4. struct udp_pcb *udp;
  5. ip_addr_t ip; //remote
  6. //struct pbuf *p;
  7. uint32_t structLen;
  8. u16_t groundPort = 1234;
  9. u16_t rawPort = 1111;
  10. u16_t debugPort = 1337;
  11. char * reset_key = "RESET!!";
  12. #define UDP_RAW_LEN (11 * 4)
  13. uint8_t udpMsg[IN_LEN * 3];// max size 1472
  14. uint8_t udpMsgRaw[UDP_RAW_LEN * 2];
  15. void udp_echo_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
  16. if(strcmp(p->payload, reset_key) == 0) {
  17. NVIC_SystemReset();
  18. }
  19. pbuf_free(p);
  20. }
  21. void Udp_Init() {
  22. __HAL_RCC_ETHMAC_CLK_ENABLE();
  23. __HAL_RCC_ETHMACTX_CLK_ENABLE();
  24. __HAL_RCC_ETHMACRX_CLK_ENABLE();
  25. IP_ADDR4(&ip, 172, 16, 18, 255);
  26. udp = udp_new();
  27. structLen = sizeof(Dataset_Master[0]);
  28. memset(udpMsg, 'E', sizeof(udpMsg));
  29. //p = pbuf_alloc(PBUF_TRANSPORT, sizeof(udpMsg), PBUF_REF);
  30. //p->payload = udpMsg; //pointer
  31. udp_recv(udp, udp_echo_recv, NULL);
  32. udp_bind(udp, &ipaddr, 65100);
  33. }
  34. err_t errorstate = HAL_OK;
  35. err_t UDP_send(uint8_t * udp_buff, uint16_t size, uint16_t port) {
  36. struct pbuf *pb;
  37. pb = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_POOL);
  38. pbuf_take(pb, udp_buff, size);
  39. errorstate = udp_sendto(udp, pb, &ip, port); //dest port
  40. pbuf_free(pb);
  41. return errorstate;
  42. }
  43. err_t er;
  44. void Udp_Send_Ground() {
  45. memcpy(&udpMsg[0], &Dataset_Master[buff_sensor_data_index], structLen);
  46. memcpy(&udpMsg[structLen], &Dataset_Slave[buff_sensor_data_index], structLen);
  47. memcpy(&udpMsg[structLen*2], &Dataset_Fusion[buff_sensor_data_index], structLen);
  48. //udp_sendto(udp, p, &ip, groundPort);
  49. er = UDP_send(udpMsg, sizeof(udpMsg), groundPort);
  50. }
  51. void Udp_Send_Raw() {
  52. memcpy(&udpMsgRaw[0], &Dataset_Master[buff_sensor_data_index].accel_raw, 9*4);
  53. memcpy(&udpMsgRaw[9*4], &Dataset_Master[buff_sensor_data_index].time, 4);
  54. memcpy(&udpMsgRaw[10*4], &Dataset_Master[buff_sensor_data_index].count, 4);
  55. memcpy(&udpMsgRaw[UDP_RAW_LEN], &Dataset_Slave[buff_sensor_data_index].accel_raw, 9*4);
  56. memcpy(&udpMsgRaw[UDP_RAW_LEN + 9*4], &Dataset_Slave[buff_sensor_data_index].time, 4);
  57. memcpy(&udpMsgRaw[UDP_RAW_LEN + 10*4], &Dataset_Slave[buff_sensor_data_index].count, 4);
  58. //udp_sendto(udp, p, &ip, rawPort);
  59. UDP_send(udpMsgRaw, sizeof(udpMsgRaw), rawPort);
  60. }
  61. void Udp_Send_Debug(uint8_t *msg) {
  62. UDP_send(msg, sizeof(msg), debugPort);
  63. }
  64. u8_t isNetworkUp() {
  65. return netif_is_link_up(&gnetif);
  66. }