main/ims_udp.c
/*
 * Retrieve udp socket data from flash and load into local data structure
 * set up socket connection for each
 */
Retrieve udp socket data from flash and load into local data structure
set up socket connection for each
Retrieve udp socket data from flash and load into local data structure set up socket connection for each
bool init_UDP() {
	uint32_t temp, localip;
	char tempstr[20];
	int err;
	fdmax = -1;
	FD_ZERO(&master);    
	FD_ZERO(&read_fds);
	if(!get_flash_uint32( &localip, "localip" )){
		return false;
	}
	for(int ii = 0; ii < NUMREMOTES; ++ii){
		if ( udpParams.udpConnection[ii].socket != -1 ){
			if ((err = close( udpParams.udpConnection[ii].socket )) < 0 ){
				ESP_LOGI(TAG, "init_udp: could not close socket, %d", err);
				continue;
			} else{
				ESP_LOGI(TAG, "UDP socket (%d:%d) closed", udpParams.udpConnection[ii].socket, ntohs(udpParams.udpConnection[ii].udpLocal.sin_port));
				udpParams.udpConnection[ii].socket = -1;
			}
		}
		sprintf(tempstr,"remoteip%d",ii);
		if(get_flash_uint32( &temp, tempstr ) && (temp > 0)){
		    udpParams.udpConnection[ii].udpRemote.sin_addr.s_addr 	= temp;
		    udpParams.udpConnection[ii].udpRemote.sin_family 		= AF_INET;
		}
		else
			continue;
		sprintf(tempstr,"remoteport%d",ii);
		if(get_flash_uint32( &temp, tempstr ) && (temp > 0)){
			udpParams.udpConnection[ii].udpRemote.sin_port 		= htons(temp);
		}
		else
			continue;
		sprintf(tempstr,"localport%d",ii);
		if(get_flash_uint32( &temp, tempstr ) && (temp > 0)){
		    udpParams.udpConnection[ii].udpLocal.sin_family 		= AF_INET;
			udpParams.udpConnection[ii].udpLocal.sin_port 			= htons(temp);
		    udpParams.udpConnection[ii].udpLocal.sin_addr.s_addr 	= localip;
		}
		else
			continue;
		memset( udpParams.udpConnection[ii].udpLocal.sin_zero, '\0', sizeof udpParams.udpConnection[ii].udpLocal.sin_zero);
		memset( udpParams.udpConnection[ii].udpRemote.sin_zero, '\0', sizeof udpParams.udpConnection[ii].udpRemote.sin_zero);
		if((err = ( udpParams.udpConnection[ii].socket = socket(AF_INET, SOCK_DGRAM, 0))) < 0){
			ESP_LOGE(TAG, "Init_udp: could not create socket, %d", err);
			continue;
		}
		if (bind( udpParams.udpConnection[ii].socket, (struct sockaddr *) &udpParams.udpConnection[ii].udpLocal, sizeof(udpParams.udpConnection[ii].udpLocal)) < 0) {
	        ESP_LOGE(TAG, "Init_udp: could not bind to local udp port %d", udpParams.udpConnection[ii].udpLocal.sin_port);
	        continue;
	    }
		else {
			ESP_LOGI(TAG, "UDP socket (%d:%d) open",udpParams.udpConnection[ii].socket, ntohs(udpParams.udpConnection[ii].udpLocal.sin_port));
		}
		FD_SET( udpParams.udpConnection[ii].socket, &master);
		if(udpParams.udpConnection[ii].socket > fdmax){
			fdmax = udpParams.udpConnection[ii].socket;
		}
	}
	xEventGroupSetBits( globalPtrs->wifi_event_group, UDP_ENABLED );
	return true;
}
bool init_UDP ( ) { uint32_t temp , localip ; char tempstr [ 20 ] ; int err ; fdmax = -1 ; FD_ZERO ( & master ) ; FD_ZERO ( & read_fds ) ; if ( ! get_flash_uint32 ( & localip , " " ) ) { return false ; } for ( int ii = 0 ; ii < NUMREMOTES ; ++ ii ) { if ( udpParams . udpConnection [ ii ] . socket != -1 ) { if ( ( err = close ( udpParams . udpConnection [ ii ] . socket ) ) < 0 ) { ESP_LOGI ( TAG , " " , err ) ; continue ; } else { ESP_LOGI ( TAG , " " , udpParams . udpConnection [ ii ] . socket , ntohs ( udpParams . udpConnection [ ii ] . udpLocal . sin_port ) ) ; udpParams . udpConnection [ ii ] . socket = -1 ; } } sprintf ( tempstr , " " , ii ) ; if ( get_flash_uint32 ( & temp , tempstr ) && ( temp > 0 ) ) { udpParams . udpConnection [ ii ] . udpRemote . sin_addr . s_addr = temp ; udpParams . udpConnection [ ii ] . udpRemote . sin_family = AF_INET ; } else continue ; sprintf ( tempstr , " " , ii ) ; if ( get_flash_uint32 ( & temp , tempstr ) && ( temp > 0 ) ) { udpParams . udpConnection [ ii ] . udpRemote . sin_port = htons ( temp ) ; } else continue ; sprintf ( tempstr , " " , ii ) ; if ( get_flash_uint32 ( & temp , tempstr ) && ( temp > 0 ) ) { udpParams . udpConnection [ ii ] . udpLocal . sin_family = AF_INET ; udpParams . udpConnection [ ii ] . udpLocal . sin_port = htons ( temp ) ; udpParams . udpConnection [ ii ] . udpLocal . sin_addr . s_addr = localip ; } else continue ; memset ( udpParams . udpConnection [ ii ] . udpLocal . sin_zero , ' \0 ' , sizeof udpParams . udpConnection [ ii ] . udpLocal . sin_zero ) ; memset ( udpParams . udpConnection [ ii ] . udpRemote . sin_zero , ' \0 ' , sizeof udpParams . udpConnection [ ii ] . udpRemote . sin_zero ) ; if ( ( err = ( udpParams . udpConnection [ ii ] . socket = socket ( AF_INET , SOCK_DGRAM , 0 ) ) ) < 0 ) { ESP_LOGE ( TAG , " " , err ) ; continue ; } if ( bind ( udpParams . udpConnection [ ii ] . socket , ( struct sockaddr * ) & udpParams . udpConnection [ ii ] . udpLocal , sizeof ( udpParams . udpConnection [ ii ] . udpLocal ) ) < 0 ) { ESP_LOGE ( TAG , " " , udpParams . udpConnection [ ii ] . udpLocal . sin_port ) ; continue ; } else { ESP_LOGI ( TAG , " " , udpParams . udpConnection [ ii ] . socket , ntohs ( udpParams . udpConnection [ ii ] . udpLocal . sin_port ) ) ; } FD_SET ( udpParams . udpConnection [ ii ] . socket , & master ) ; if ( udpParams . udpConnection [ ii ] . socket > fdmax ) { fdmax = udpParams . udpConnection [ ii ] . socket ; } } xEventGroupSetBits ( globalPtrs -> wifi_event_group , UDP_ENABLED ) ; return true ; }
Retrieve udp socket data from flash and load into local data structure
 set up socket connection for each
Retrieve udp socket data from flash and load into local data structure set up socket connection for each
// clear the master and temp sets //localip will be the same for all. //if it doesnt exist, something's wrong, so exit. //set up sockets for multiple remotes //close the socket if it's already open //socket is -1 on startup //get remote ip //get remote port //get local port /*if we get here, we can init the socket and bind it to the local port and ip*/ //connect to socket //bind the socket to the local port // add the listener to the master set