예제 #1
0
    /**
     * Called from the client to disconnect this server.
     * @param socket
     * @return
     */
    wiced_result_t disconnect(wiced_tcp_socket_t* socket) {
        os_mutex_recursive_lock(accept_lock);
        wiced_tcp_disconnect(socket);
        // remove from client array as this socket is getting closed and
        // subsequently destroyed
	    int idx = index(socket);
	    if (idx >= 0) {
		  clients[idx] = NULL;
	    }
        wiced_result_t result = wiced_tcp_server_disconnect_socket(this, socket);
        os_mutex_recursive_unlock(accept_lock);
        return result;
    }
static wiced_result_t received_data_callback( wiced_tcp_socket_t* socket, void* arg )
{

	uint16_t        request_length;
	uint16_t        available_data_length;
	wiced_packet_t* temp_packet = NULL;

	if(wiced_tcp_receive( socket, &temp_packet, 500 ) == WICED_SUCCESS) // get the data from the client
	{
		char *rbuffer;
		dbEntry_t receive;
		char commandId;
		int err=1;
		char returnMessage[15];

		// get the pointer to the packet sent by the client and the data
		wiced_packet_get_data( temp_packet, 0, (uint8_t**) &rbuffer, &request_length, &available_data_length );
		sscanf(rbuffer,"%c%4x%2x%4x",&commandId,(unsigned int *)&receive.deviceId,(unsigned int *)&receive.regId,(unsigned int *)&receive.value);
		wiced_packet_delete( temp_packet ); // free the packet

		if(request_length >= 11 && request_length <= 13  ) //11 if no end 12 if CR 13 if CRLF
		{
			dbEntry_t *newDbEntry;
			switch(commandId)
			{
			case 'R': // they sent a Read command
				newDbEntry = dbFind(&receive); // look through the database to find a previous write of the deviceId/regId
				if(newDbEntry)
				{
					err=0;
					sprintf(returnMessage,"A%04X%02X%04X",(unsigned int)newDbEntry->deviceId,(unsigned int)newDbEntry->regId,(unsigned int)newDbEntry->value);
				}
				else
					err = 1;
				break;
			case 'W': // they sent a Write command
				sprintf(returnMessage,"A%04X%02X%04X",(unsigned int)receive.deviceId,(unsigned int)receive.regId,(unsigned int)receive.value);
				dbEntry_t *newDB;
				newDB = malloc(sizeof(dbEntry_t)); // make a new entry to put in the database
				memcpy(newDB,&receive,sizeof(dbEntry_t)); // copy the received data into the new entry
				dbSetValue(newDB); // save it.
				err = 0;
				break;
			default: // if they don't send a legal command then it is an error
				err = 1;
				break;
			}
		}

		// Print IP address of the client (peer) that sent the data and print to terminal
		uint32_t 		peerAddressV4;
		peerAddressV4 = (*socket).socket.nx_tcp_socket_connect_ip.nxd_ip_address.v4;
		WPRINT_APP_INFO(("%u.%u.%u.%u\t",
				(uint8_t)(peerAddressV4 >> 24),
				(uint8_t)(peerAddressV4 >> 16),
				(uint8_t)(peerAddressV4 >> 8),
				(uint8_t)(peerAddressV4 >> 0)));

		// Print the port that the peer connected from
		uint16_t	    peerPort;
		peerPort = (*socket).socket.nx_tcp_socket_connect_port;
		WPRINT_APP_INFO(("%d\t",peerPort));

		// Print the data that was sent by the client (peer)
		if(err)
		{
			strcpy(returnMessage,"X");
			WPRINT_APP_INFO(("X length=%d\n",available_data_length));
		}
		else
		{
			uint32_t count;
			linked_list_get_count(&db,&count);
			WPRINT_APP_INFO(("%c\t%4X\t%2X\t%4X\t%d\n",commandId,(unsigned int)receive.deviceId,(unsigned int)receive.regId,(unsigned int)receive.value,(int)count));
		}

		// send response packet
		wiced_packet_t* tx_packet;
		uint8_t *tx_data;
		wiced_packet_create_tcp(socket, strlen(returnMessage), &tx_packet, (uint8_t**)&tx_data, &available_data_length);
		memcpy(tx_data, returnMessage, strlen(returnMessage));
		wiced_packet_set_data_end(tx_packet, (uint8_t*)&tx_data[strlen(returnMessage)]);
		wiced_tcp_send_packet(socket, tx_packet);
		wiced_packet_delete(tx_packet);

		wiced_tcp_server_disconnect_socket(&tcp_server,socket);

	}