BYTE control(int req, int cs, int index, BYTE* buf, int size){ BYTE bRequest = req;//GET_CUR,GET_MINなど WORD wValue = cs << 8; WORD wIndex = index;//Entity ID and Interface または End Point WORD wLength = size;//パラメータの長さ if(req == SET_CUR){ BYTE bmRequestType = USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE; return USBHostIssueDeviceRequest(deviceAddress,bmRequestType,bRequest,wValue,wIndex,wLength,buf,USB_DEVICE_REQUEST_SET,0); }else{ BYTE bmRequestType = USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE; return USBHostIssueDeviceRequest(deviceAddress,bmRequestType,bRequest,wValue,wIndex,wLength,buf,USB_DEVICE_REQUEST_GET,0); } }
BYTE USBHostCtrlWrite( BYTE deviceAddress, void *buffer, DWORD length ) { BYTE RetVal; DWORD i; // Validate the call if ((g_usbbt.initialized != 1) || (g_usbbt.deviceAddress != deviceAddress)) return USB_INVALID_STATE; if (g_usbbt.endpointCtrl_Busy) return USB_BUSY; UART2PrintString( "USBHostCtrlWrite\r\n" ); for(i = 0; i < length; i++) { UART2PutHex(*((char*)buffer+i)); } UART2PrintString( "\r\n" ); g_usbbt.endpointCtrl_Busy = 1; RetVal = USBHostIssueDeviceRequest( deviceAddress, USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS, 0, 0, 0, length, buffer, USB_DEVICE_REQUEST_SET, g_usbbt.clientDriverID); if (RetVal != USB_SUCCESS) { g_usbbt.endpointCtrl_Busy = 0; } return RetVal; }
static BYTE USBHostBluetoothWrite(BLUETOOTH_ENDPOINT *ep, const void *buffer, DWORD length) { BYTE RetVal; // Validate the call assert(gc_BluetoothDevData.initialized); if (ep->busy) return USB_BUSY; // log_printf("Sending message with %u bytes to endpoint 0x%x: ", // (unsigned) length, // ep->address); // log_print_buf(buffer, length); // Set the busy flag and start a new OUT transfer. ep->busy = 1; if (ep->address == 0) { RetVal = USBHostIssueDeviceRequest(gc_BluetoothDevData.ID.deviceAddress, 0x20, 0, 0, 0,//gc_BluetoothDevData.interface, length, (BYTE *) buffer, USB_DEVICE_REQUEST_SET, gc_BluetoothDevData.driverID); } else { RetVal = USBHostWrite(gc_BluetoothDevData.ID.deviceAddress, ep->address, (BYTE *)buffer, length); } if (RetVal != USB_SUCCESS) { log_printf("Write failed"); ep->busy = 0; // Clear flag to allow re-try } return RetVal; }
/**************************************************************************** Function: BYTE AndroidCommandGetProtocol(ANDROID_DEVICE_DATA* device, WORD *protocol) Summary: Requests the protocol version from the specified Android device. Description: Requests the protocol version from the specified Android device. Precondition: None Parameters: ANDROID_DEVICE_DATA* device - pointer to the Android device to query WORD *protocol - pointer to where to store the resulting protocol version Return Values: USB_SUCCESS - Request processing started USB_UNKNOWN_DEVICE - Device not found USB_INVALID_STATE - The host must be in a normal running state to do this request USB_ENDPOINT_BUSY - A read or write is already in progress USB_ILLEGAL_REQUEST - SET CONFIGURATION cannot be performed with this function. Remarks: Internal API only. Should not be called by a user. ***************************************************************************/ BYTE AndroidCommandGetProtocol(ANDROID_DEVICE_DATA* device, WORD *protocol) { return USBHostIssueDeviceRequest ( device->address, //BYTE deviceAddress, USB_SETUP_DEVICE_TO_HOST //BYTE bmRequestType, | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE, ANDROID_ACCESSORY_GET_PROTOCOL, //BYTE bRequest, 0, //WORD wValue, 0, //WORD wIndex, 2, //WORD wLength, (BYTE*)protocol, //BYTE *data, USB_DEVICE_REQUEST_GET, //BYTE dataDirection, device->clientDriverID //BYTE clientDriverID ); }
/**************************************************************************** Function: static BYTE AndroidCommandStart_Pv1(void *handle) Summary: Sends a the start command that makes the Android device go into accessory mode Description: Sends a the start command that makes the Android device go into accessory mode Precondition: None Parameters: void* handle - the device entering accessory mode Return Values: USB_SUCCESS - Request processing started USB_UNKNOWN_DEVICE - Device not found USB_INVALID_STATE - The host must be in a normal running state to do this request USB_ENDPOINT_BUSY - A read or write is already in progress USB_ILLEGAL_REQUEST - SET CONFIGURATION cannot be performed with this function. Remarks: This is a internal API only. ***************************************************************************/ static BYTE AndroidCommandStart_Pv1(void *handle) { ANDROID_PROTOCOL_V1_DEVICE_DATA* device = (ANDROID_PROTOCOL_V1_DEVICE_DATA*)handle; return USBHostIssueDeviceRequest ( device->address, //BYTE deviceAddress, USB_SETUP_HOST_TO_DEVICE //BYTE bmRequestType, | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE, ANDROID_ACCESSORY_START, //BYTE bRequest, 0, //WORD wValue, 0, //WORD wIndex, 0, //WORD wLength, NULL, //BYTE *data, USB_DEVICE_REQUEST_SET, //BYTE dataDirection, device->clientDriverID //BYTE clientDriverID ); }
/**************************************************************************** Function: static BYTE AndroidCommandSendString_Pv1(void *handle, ANDROID_ACCESSORY_STRINGS stringType, const char *string, WORD stringLength) Summary: Sends a command String to the Android device using the EP0 command Description: Sends a command String to the Android device using the EP0 command Precondition: None Parameters: void* handle - the device to send the message to ANDROID_ACCESSORY_STRINGS stringType - the type of string message being sent const char* string - the string data being sent WORD stringLength - the length of the string Return Values: USB_SUCCESS - Request processing started USB_UNKNOWN_DEVICE - Device not found USB_INVALID_STATE - The host must be in a normal running state to do this request USB_ENDPOINT_BUSY - A read or write is already in progress USB_ILLEGAL_REQUEST - SET CONFIGURATION cannot be performed with this function. Remarks: This is a internal API only. ***************************************************************************/ static BYTE AndroidCommandSendString_Pv1(void *handle, ANDROID_ACCESSORY_STRINGS stringType, const char *string, WORD stringLength) { ANDROID_PROTOCOL_V1_DEVICE_DATA* device = (ANDROID_PROTOCOL_V1_DEVICE_DATA*)handle; return USBHostIssueDeviceRequest ( device->address, //BYTE deviceAddress, USB_SETUP_HOST_TO_DEVICE //BYTE bmRequestType, | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE, ANDROID_ACCESSORY_SEND_STRING, //BYTE bRequest, 0, //WORD wValue, (WORD)stringType, //WORD wIndex, stringLength, //WORD wLength, (BYTE*)string, //BYTE *data, USB_DEVICE_REQUEST_SET, //BYTE dataDirection, device->clientDriverID //BYTE clientDriverID ); }
BYTE USBHostBluetoothWrite_EP0( BYTE deviceAddress, void *buffer, DWORD length ) { BYTE RetVal; // Validate the call if (!API_VALID( deviceAddress)) return USB_INVALID_STATE; if (gc_DevData.flags.txCtlBusy) return USB_BUSY; // Set the busy flag and start a new OUT transfer. gc_DevData.flags.txCtlBusy = 1; RetVal = USBHostIssueDeviceRequest( deviceAddress, USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE, 0, 0, 0, length, (BYTE *)buffer, USB_DEVICE_REQUEST_SET,0x00); if (RetVal != USB_SUCCESS) { gc_DevData.flags.txCtlBusy = 0; // Clear flag to allow re-try DelayMs(1); } return RetVal; } // USBHostBluetoothWrite
void ManageDemoState ( void ) { int j; DWORD byteCount; BYTE errorCode; BYTE RetVal; //接続されていなかったら初期化 if (USBHostGenericDeviceDetached(deviceAddress) && deviceAddress != 0) { long i; for(i = 0;i < sizeof(jpeg);i++){ jpeg[i] = 0; } UART2PrintString( "Generic demo device detached - polled\r\n" ); DemoState = DEMO_INITIALIZE; deviceAddress = 0; } switch (DemoState) { case DEMO_INITIALIZE: DemoState = DEMO_STATE_IDLE; break; case DEMO_STATE_IDLE: //接続されたら、読み出し処理に移行 if (CheckForNewAttach()) { DemoState = DEMO_STATE_GET_INFO; UART2PrintString( "USB_Ver=" ); UART2PutDec(((USB_DEVICE_DESCRIPTOR *)pDeviceDescriptor)->bcdUSB >> 8); UART2PrintString( "-" ); UART2PutDec(((USB_DEVICE_DESCRIPTOR *)pDeviceDescriptor)->bcdUSB); UART2PrintString( "\r\n" ); } break; case DEMO_STATE_GET_INFO: if(control(GET_INFO, VS_PROBE_CONTROL, 1, param, 1) == USB_SUCCESS){ UART2PrintString( "GET_INFO, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_INFO; } break; case DEMO_STATE_WAIT_GET_INFO: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_INFO, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(param,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_DEF; } break; case DEMO_STATE_GET_DEF: if(control(GET_DEF, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_DEF, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_DEF; } break; case DEMO_STATE_WAIT_GET_DEF: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_DEF, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_MIN; } break; case DEMO_STATE_GET_MIN: if(control(GET_MIN, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_MIN, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_MIN; } break; case DEMO_STATE_WAIT_GET_MIN: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_MIN, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_MAX; } break; case DEMO_STATE_GET_MAX: if(control(GET_MAX, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_MAX, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_MAX; } break; case DEMO_STATE_WAIT_GET_MAX: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_MAX, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_CUR; } break; case DEMO_STATE_GET_CUR: if(control(GET_CUR, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_CUR, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_CUR; } break; case DEMO_STATE_WAIT_GET_CUR: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_CUR, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_SET_CUR ; } break; case DEMO_STATE_SET_CUR: for(j = 0;j < param_len;j++){ temp[j] = 0; } temp[2] = 2;//フォーマットインデックス temp[3] = 1;//フレームインデックス /*temp[4] = 0x80; temp[5] = 0x84; temp[6] = 0x1E; temp[7] = 0x00;*/ temp[4] = 0x15;//30Hz temp[5] = 0x16; temp[6] = 0x05; temp[7] = 0x00; if(control(SET_CUR, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "SET_CUR, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_SET_CUR; } break; case DEMO_STATE_WAIT_SET_CUR: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=SET_CUR, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_GET_CUR2 ; } break; case DEMO_STATE_GET_CUR2: if(control(GET_CUR, VS_PROBE_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_CUR2, VS_PROBE_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_CUR2; } break; case DEMO_STATE_WAIT_GET_CUR2: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_CUR2, VS_PROBE_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_GET_INFO2 ; } break; case DEMO_STATE_GET_INFO2: if(control(GET_INFO, VS_COMMIT_CONTROL, 1, param, 1) == USB_SUCCESS){ UART2PrintString( "GET_INFO, VS_COMMIT_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_INFO2; } break; case DEMO_STATE_WAIT_GET_INFO2: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_INFO, VS_COMMIT_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(param,byteCount); UART2PrintString( "\r\n" ); DemoState = DEMO_STATE_GET_CUR3; } break; case DEMO_STATE_GET_CUR3: if(control(GET_CUR, VS_COMMIT_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "GET_CUR2, VS_COMMIT_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_GET_CUR3; } break; case DEMO_STATE_WAIT_GET_CUR3: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=GET_CUR2, VS_COMMIT_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_SET_CUR2 ; } break; case DEMO_STATE_SET_CUR2: for(j = 0;j < param_len;j++){ temp[j] = 0; } temp[2] = 2; temp[3] = 0x01;//640x480 30Hz //temp[3] = 0x02;//160x120 30Hz //temp[3] = 0x0C;//800x600 temp[4] = 0x80;//5Hz temp[5] = 0x84; temp[6] = 0x1E; temp[7] = 0x00; /*temp[4] = 0x15;//30Hz temp[5] = 0x16; temp[6] = 0x05; temp[7] = 0x00;*/ if(control(SET_CUR, VS_COMMIT_CONTROL, 1, temp, param_len) == USB_SUCCESS){ UART2PrintString( "SET_CUR, VS_COMMIT_CONTROL\r\n" ); DemoState = DEMO_STATE_WAIT_SET_CUR2; } break; case DEMO_STATE_WAIT_SET_CUR2: if (USBHostTransferIsComplete( deviceAddress, 0, &errorCode, &byteCount )){ UART2PrintString( "OK=SET_CUR, VS_COMMIT_CONTROL\r\n" ); UART2PrintString( "Byte Count=" ); UART2PutDec(byteCount); UART2PrintString( "\r\n" ); packet_dump(temp,byteCount); UART2PrintString( "\r\n" ); DemoState =DEMO_STATE_SET_ISOCHRONOUS ; } break; case DEMO_STATE_SET_ISOCHRONOUS: if(USBHostIssueDeviceRequest( deviceAddress, 0x01, USB_REQUEST_SET_INTERFACE, 0x06/*Alternate Setting:0x1*/, 0x01/*interface:0x03*/, 0, NULL, USB_DEVICE_REQUEST_SET, 0x00 )== USB_SUCCESS){ UART2PrintString( "USB_REQUEST_SET_INTERFACE=OK!\r\n" ); DemoState =DEMO_STATE_WAIT_SET_ISOCHRONOUS ; } break; case DEMO_STATE_WAIT_SET_ISOCHRONOUS: if(USBHostReadIsochronous(deviceAddress,0x81,&isocData) == USB_SUCCESS){ //UART2PrintString( "USBHostReadIsochronous=OK!\r\n" ); DemoState = DEMO_STATE_ERROR; } break; case DEMO_STATE_ERROR: break; default: DemoState = DEMO_INITIALIZE; break; } //DelayMs(1); // 1ms delay } // ManageDemoState