Example #1
0
int network_listen(struct network *net, const char *addr, uint16_t port, uint32_t flags)
{
	if (net == NULL || addr == 0 || port == 0) {
		return kr_error(EINVAL);
	}

	/* Parse address. */
	int ret = 0;
	struct sockaddr_storage sa;
	if (strchr(addr, ':') != NULL) {
		ret = uv_ip6_addr(addr, port, (struct sockaddr_in6 *)&sa);
	} else {
		ret = uv_ip4_addr(addr, port, (struct sockaddr_in *)&sa);
	}
	if (ret != 0) {
		return ret;
	}

	/* Bind interfaces */
	struct endpoint *ep = malloc(sizeof(*ep));
	memset(ep, 0, sizeof(*ep));
	ep->flags = NET_DOWN;
	ep->port = port;
	ret = open_endpoint(net, ep, (struct sockaddr *)&sa, flags);
	if (ret == 0) {
		ret = insert_endpoint(net, addr, ep);
	}
	if (ret != 0) {
		close_endpoint(ep);
	}

	return ret;
}
Example #2
0
static int audio_open_output_stream(struct audio_hw_device *dev,
					audio_io_handle_t handle,
					audio_devices_t devices,
					audio_output_flags_t flags,
					struct audio_config *config,
					struct audio_stream_out **stream_out)

{
	struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
	struct a2dp_stream_out *out;

	out = calloc(1, sizeof(struct a2dp_stream_out));
	if (!out)
		return -ENOMEM;

	DBG("");

	out->stream.common.get_sample_rate = out_get_sample_rate;
	out->stream.common.set_sample_rate = out_set_sample_rate;
	out->stream.common.get_buffer_size = out_get_buffer_size;
	out->stream.common.get_channels = out_get_channels;
	out->stream.common.get_format = out_get_format;
	out->stream.common.set_format = out_set_format;
	out->stream.common.standby = out_standby;
	out->stream.common.dump = out_dump;
	out->stream.common.set_parameters = out_set_parameters;
	out->stream.common.get_parameters = out_get_parameters;
	out->stream.common.add_audio_effect = out_add_audio_effect;
	out->stream.common.remove_audio_effect = out_remove_audio_effect;
	out->stream.get_latency = out_get_latency;
	out->stream.set_volume = out_set_volume;
	out->stream.write = out_write;
	out->stream.get_render_position = out_get_render_position;

	/* We want to autoselect opened endpoint */
	out->ep = NULL;

	if (!open_endpoint(&out->ep, &out->cfg))
		goto fail;

	DBG("rate=%d channels=%d format=%d", out->cfg.rate,
					out->cfg.channels, out->cfg.format);

	if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
		out->downmix_buf = malloc(FIXED_BUFFER_SIZE / 2);
		if (!out->downmix_buf)
			goto fail;
	}

	*stream_out = &out->stream;
	a2dp_dev->out = out;

	out->audio_state = AUDIO_A2DP_STATE_STANDBY;

	return 0;

fail:
	error("audio: cannot open output stream");
	free(out);
	*stream_out = NULL;
	return -EIO;
}
void HostProxy_GadgetFS::setConfig(Configuration* fs_cfg,Configuration* hs_cfg,bool hs) {
	int ifc_idx, aifc_idx;
	__u8 ifc_count=fs_cfg->get_descriptor()->bNumInterfaces;
	for (ifc_idx=0;ifc_idx<ifc_count;ifc_idx++) {
		// modified 20141010 [email protected]
		// for considering alternate interface
		// begin
		int aifc_cnt = fs_cfg->get_interface_alternate_count( ifc_idx);
		for ( aifc_idx=0; aifc_idx < aifc_cnt; aifc_idx++) {
			Interface* fs_aifc=fs_cfg->get_interface_alternate(ifc_idx, aifc_idx);
			Interface* hs_aifc=hs_cfg?hs_cfg->get_interface_alternate(aifc_idx, aifc_idx):fs_aifc;
			hs_aifc=hs_aifc?hs_aifc:fs_aifc;
			__u8 ep_count=fs_aifc->get_endpoint_count();
			int ep_idx;
			for (ep_idx=0;ep_idx<ep_count;ep_idx++) {
				const usb_endpoint_descriptor* fs_ep=fs_aifc->get_endpoint_by_idx(ep_idx)->get_descriptor();
				const usb_endpoint_descriptor* hs_ep=(hs_aifc->get_endpoint_by_idx(ep_idx))?hs_aifc->get_endpoint_by_idx(ep_idx)->get_descriptor():fs_ep;
				__u8 bufSize=4+fs_ep->bLength+hs_ep->bLength;
				__u8* buf=(__u8*)calloc(1,bufSize);
				buf[0]=1;

				memcpy(buf+4,fs_ep,fs_ep->bLength);
				memcpy(buf+4+fs_ep->bLength,hs_ep,hs_ep->bLength);

				__u8 epAddress=fs_ep->bEndpointAddress;

				int fd=open_endpoint(epAddress, device_filename);
				if (fd<0) {
					fprintf(stderr,"Fail on open EP%02x %d %s\n",epAddress,errno,strerror(errno));
					free(buf);
					return;
				}
				int rc = write(fd, buf, bufSize);
				free(buf);
				if (rc != bufSize)
					std::cerr << "Error writing to EP 0x" << std::hex << epAddress << std::dec << '\n';
				aiocb* aio=new aiocb;
				std::memset(aio, 0, sizeof(struct aiocb));
				aio->aio_fildes = fd;
				aio->aio_sigevent.sigev_notify = SIGEV_NONE;
				if (epAddress & 0x80) {
					p_epin_async[epAddress&0x0f]=aio;
				} else {
					if (hs) {
						aio->aio_nbytes=(hs_ep->bmAttributes&0x02)?hs_ep->wMaxPacketSize:hs_ep->wMaxPacketSize;
					} else {
						aio->aio_nbytes=(fs_ep->bmAttributes&0x02)?fs_ep->wMaxPacketSize:fs_ep->wMaxPacketSize;
					}
					if (debugLevel > 2)
						std::cerr << "gadgetfs: max. packet size is " << aio->aio_nbytes << " bytes for EP" << std::hex << (unsigned)epAddress << std::dec << '\n';
					aio->aio_buf=malloc(aio->aio_nbytes);
					rc=aio_read(aio);
					if (rc) {
						delete(aio);fprintf(stderr,"Error submitting aio for EP%02x %d %s\n",epAddress,errno,strerror(errno));
					} else {
						p_epout_async[epAddress&0x0f]=aio;
					}
				}
				fprintf(stderr,"Opened EP%02x\n",epAddress);
			}
		}
		// end
	}
}