I send broadcasts to the network to find other devices. They are sent periodically by setting a timeout time for
int len = recvfrom (sock, udpcBuffer, sizeof (udpcBuffer) - 1, 0, (struct sockaddr *) & source_addr, & socklen);
The first time I send a request, everything goes well, I get a complete list of all responses.
On the second run to send the request, the application crashes.
Code: Select all
// Database of serial numbers and correspondences of IP addresses found on the network
char * devsList;
/* Create json with serial number of current device and IP address */
cJSON * root = cJSON_CreateObject();
cJSON_AddStringToObject(root, getDeviceSN(commonBuffer), ipAddr);
devsList = cJSON_PrintUnformatted(root);
cJSON_Delete(root);
Code: Select all
// Checking the received package and adding new devices to the list
static void checkDevSN(const char * udpcBuffer, const char * addressString) {
cJSON * rxData = cJSON_Parse(udpcBuffer);
if (rxData != NULL) {
const char * currentDev = cJSON_GetObjectItem(rxData, "DevSN")->valuestring;
if (validDevSN(currentDev) == true) {
cJSON * devsList_t = cJSON_Parse(devsList);
cJSON * item = NULL;
int isExist = false;
cJSON_ArrayForEach(item, devsList_t) {
if (strcmp(item->string, currentDev) == 0) {
isExist = true;
break;
}
}
cJSON_Delete(item);
if (isExist == false) {
cJSON_AddStringToObject(devsList_t, currentDev, addressString);
char * newDevsList = cJSON_PrintUnformatted(devsList_t);
cJSON_Delete(devsList_t);
strcpy(devsList, newDevsList); // If disabled, it does not crash
cJSON_free(newDevsList);
} else {
cJSON_Delete(devsList_t);
}
}
}
cJSON_Delete(rxData);
}
Code: Select all
// Sending requests to find devices (Task UDC Client)
static void taskUDPClient(void * pvParameters) {
char udpcBuffer[128];
char addressString[128];
int addressFamily = 0;
int ipProtocol = 0;
/* Timeout for recvfrom */
struct timeval read_timeout;
read_timeout.tv_sec = 3;
while (1) {
struct sockaddr_in dest_addr;
dest_addr.sin_addr.s_addr = inet_addr(UDPC_HOST_IP_ADDR);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(UDP_PORT);
addressFamily = AF_INET;
ipProtocol = IPPROTO_IP;
int sock = socket(addressFamily, SOCK_DGRAM, ipProtocol);
if (sock < 0) {
sendUART("ER!", "UDP Client Task", "Unable to create socket");
break;
}
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof read_timeout);
int requestCounter = 3;
while (1) {
int err = sendto(sock, UDP_SEARCH_PHRASE, strlen(UDP_SEARCH_PHRASE), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0) {
sendUART("ER!" , "UDP Client Task", "Error Sending");
break;
}
/* Waiting for responses */
while (1) {
struct sockaddr_storage source_addr;
socklen_t socklen = sizeof(source_addr);
int len = recvfrom(sock, udpcBuffer, sizeof(udpcBuffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
if (len < 0) break;
/* Get the sender's IP address */
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addressString, sizeof(addressString) - 1);
/* Data received */
if (len > 0) {
udpcBuffer[len] = 0;
checkDevSN(udpcBuffer, addressString);
}
}
if (--requestCounter == 0) {
/* Displaying an updated database of serial numbers */
sendUART("UDPc", "devsList", devsList);
vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);
requestCounter = 3;
}
}
if (sock != -1) {
sendUART("ER!" , "UDP Client Task", "Restarting");
shutdown(sock, 0);
close(sock);
}
}
udpClientDeInit();
}
On the second occurrence (after the delay vTaskDelay (10 * 1000 / portTICK_PERIOD_MS);) the application crashes with an error:
Code: Select all
CORRUPT HEAP: multi_heap.c:540 detected at 0x3ffb93b0
abort() was called at PC 0x4008bb99 on core 0
Backtrace:0x40087d77:0x3ffcb7b0 0x40088415:0x3ffcb7d0 0x4008ef7a:0x3ffcb7f0 0x4008bb99:0x3ffcb860 0x4008141a:0x3ffcb880 0x4008efa5:0x3ffcb8a0 0x400d772e:0x3ffcb8c0 0x400d771d:0x3ffcb8e0 0x400d5f5f:0x3ffcb900 0x400d6082:0x3ffcb930 0x4008841e:0x3ffcba90