Beispiel #1
0
static int
VitaMTP_PTPIP_Connect(PTPParams *params, struct sockaddr_in *saddr, int port)
{
    uint16_t    ret;

    VitaMTP_Log(VitaMTP_DEBUG, "ptpip/connect: connecting to port %d.\n", port);
    saddr->sin_port     = htons(port);
    params->cmdfd = socket(PF_INET, SOCK_STREAM, 0);

    if (params->cmdfd == -1)
    {
        perror("socket cmd");
        return -1;
    }

    params->evtfd = socket(PF_INET, SOCK_STREAM, 0);

    if (params->evtfd == -1)
    {
        perror("socket evt");
        close(params->cmdfd);
        return -1;
    }

    if (-1 == connect(params->cmdfd, (struct sockaddr *)saddr, sizeof(struct sockaddr_in)))
    {
        perror("connect cmd");
        close(params->cmdfd);
        close(params->evtfd);
        return -1;
    }

    // on Vita both must be connected before anything can be recieved
    if (-1 == connect(params->evtfd, (struct sockaddr *)saddr, sizeof(struct sockaddr_in)))
    {
        perror("connect evt");
        close(params->cmdfd);
        close(params->evtfd);
        return -1;
    }

    ret = ptp_ptpip_init_command_request(params);

    if (ret != PTP_RC_OK)
    {
        close(params->cmdfd);
        close(params->evtfd);
        return -1;
    }

    ret = ptp_ptpip_init_command_ack(params);

    if (ret != PTP_RC_OK)
    {
        close(params->cmdfd);
        close(params->evtfd);
        return -1;
    }

    ret = ptp_ptpip_init_event_request(params);

    if (ret != PTP_RC_OK)
    {
        close(params->cmdfd);
        close(params->evtfd);
        return -1;
    }

    ret = ptp_ptpip_init_event_ack(params);

    if (ret != PTP_RC_OK)
    {
        close(params->cmdfd);
        close(params->evtfd);
        return -1;
    }

    VitaMTP_Log(VitaMTP_DEBUG, "ptpip/connect: ptpip connected!\n");
    return 0;
}
Beispiel #2
0
int
ptp_ptpip_connect (PTPParams* params, const char *address) {
	char 		*addr, *s, *p;
	int		port;
	struct sockaddr_in	saddr;
	uint16_t	ret;

	gp_log (GP_LOG_DEBUG,"ptpip/connect", "connecting to %s.", address);
	if (NULL == strchr (address,':'))
		return GP_ERROR_BAD_PARAMETERS;
	addr = strdup (address);
	if (!addr)
		return GP_ERROR_NO_MEMORY;
	s = strchr (addr,':');
	if (!s) {
		gp_log (GP_LOG_ERROR,"ptpip/connect", "addr %s should contain a :", address);
		return GP_ERROR_BAD_PARAMETERS;
	}
	*s = '\0';
	p = strchr (s+1,':');
	port = 15740;
	if (p) {
		*p = '\0';
		if (!sscanf (p+1,"%d",&port)) {
			fprintf(stderr,"failed to scan for port in %s\n", p+1);
			free (addr);
			return GP_ERROR_BAD_PARAMETERS;
		}
	}
	if (!inet_aton (s+1,  &saddr.sin_addr)) {
		fprintf(stderr,"failed to scan for addr in %s\n", s+1);
		free (addr);
		return GP_ERROR_BAD_PARAMETERS;
	}
	saddr.sin_port		= htons(port);
	saddr.sin_family	= AF_INET;
	free (addr);
	params->cmdfd = socket (PF_INET, SOCK_STREAM, 0);
	if (params->cmdfd == -1) {
		perror ("socket cmd");
		return GP_ERROR_BAD_PARAMETERS;
	}
	params->evtfd = socket (PF_INET, SOCK_STREAM, 0);
	if (params->evtfd == -1) {
		perror ("socket evt");
		close (params->cmdfd);
		return GP_ERROR_BAD_PARAMETERS;
	}
	if (-1 == connect (params->cmdfd, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in))) {
		perror ("connect cmd");
		close (params->cmdfd);
		close (params->evtfd);
		return GP_ERROR_IO;
	}
	if (-1 == connect (params->evtfd, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in))) {
		perror ("connect evt");
		close (params->cmdfd);
		close (params->evtfd);
		return GP_ERROR_IO;
	}
	ret = ptp_ptpip_init_command_request (params);
	if (ret != PTP_RC_OK)
		return translate_ptp_result (ret);
	ret = ptp_ptpip_init_command_ack (params);
	if (ret != PTP_RC_OK)
		return translate_ptp_result (ret);
	ret = ptp_ptpip_init_event_request (params);
	if (ret != PTP_RC_OK)
		return translate_ptp_result (ret);
	ret = ptp_ptpip_init_event_ack (params);
	if (ret != PTP_RC_OK)
		return translate_ptp_result (ret);
	gp_log (GP_LOG_DEBUG, "ptpip/connect", "ptpip connected!");
	return GP_OK;
}