-
Notifications
You must be signed in to change notification settings - Fork 15
/
tcp_handler.c
117 lines (109 loc) · 2.81 KB
/
tcp_handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "tcp_handler.h"
static const char *TAG = "tcp_handler";
/**
* @brief Manages TCP connection to the server
*
* @param nm tcp_network_data struct which contains necessary data for a TCP connection
* @return void
**/
bool tcp_network_manager(struct tcp_network_data* nm)
{
nm->dest_addr.sin_addr.s_addr = inet_addr(TCP_HOST_IP_ADDR);
nm->dest_addr.sin_family = AF_INET;
nm->dest_addr.sin_port = htons(TCP_PORT);
nm->addr_family = AF_INET;
nm->ip_protocol = IPPROTO_IP;
inet_ntoa_r(nm->dest_addr.sin_addr, nm->addr_str, sizeof(nm->addr_str) - 1);
nm->sock = socket(nm->addr_family, SOCK_STREAM, nm->ip_protocol);
if (nm->sock < 0)
{
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
tcp_close_network_manager(nm);
return false;
}
else
{
ESP_LOGI(TAG, "Socket created");
}
int err = connect(nm->sock, (struct sockaddr *)&nm->dest_addr, sizeof(nm->dest_addr));
if (err != 0) {
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
tcp_close_network_manager(nm);
return false;
}
else
{
ESP_LOGI(TAG, "Successfully connected to %s:%d with status %d", TCP_HOST_IP_ADDR, TCP_PORT, errno);
}
return true;
}
/**
* @brief Sends data to the server through a TCP socket
*
* @param nm A pointer to tcp_network_data struct
* @param payload char array which contains data to be sent
* @return int - returns -1 if sending failed, number of bytes sent if successfully sent the data
**/
int tcp_send_data(struct tcp_network_data* nm, char* payload)
{
if(nm->sock < 0)
{
ESP_LOGE(TAG, "%s", "Socket does not exist");
return -1;
}
else
{
int err = send(nm->sock, payload, strlen(payload), 0);
if (err < 0)
{
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
}
else
{
ESP_LOGD(TAG, "%s", "Message sent");
}
return err;
}
}
/**
* @brief Receives data from TCP server
*
* @param nm tcp_network_data struct which contains connection info
* @return char array which contains data received
**/
char* tcp_receive_data(struct tcp_network_data* nm)
{
if (nm->sock < 0)
{
ESP_LOGE(TAG, "%s", "Socket doesnot exist");
return NULL;
}
else
{
int len = recv(nm->sock, nm->rx_buffer, sizeof(nm->rx_buffer) - 1, 0);
if (len < 0)
{
ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
return NULL;
}
else
{
nm->rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
ESP_LOGD(TAG, "Received %d bytes from %s:", len, nm->addr_str);
ESP_LOGD(TAG, "%s", nm->rx_buffer);
return nm->rx_buffer;
}
}
}
/**
* @brief Shutdown active connection, deallocate memory
*
* @param nm tcp_network_data struct which contains connection info
* @return void
**/
void tcp_close_network_manager(struct tcp_network_data* nm)
{
ESP_LOGI(TAG, "%s", "Shutting down socket and restarting...");
shutdown(nm->sock, 0);
close(nm->sock);
}