ADKに対応していないAndroid端末にマイコンを繋いでADBインターフェースで通信できるMicroBridgeのmbed移植版。 mbedに繋いだサーボをAndroid端末から動かすサンプルプログラムです。 とりあえず暫定バージョンを公開します。 Dev Phone 1でのみ動作確認をしています。 USBのインターフェース番号を固定しているので他で繋がるかわかりません。

Dependencies:   TextLCD mbed

Committer:
jksoft
Date:
Sun Aug 21 14:36:35 2011 +0000
Revision:
0:dde3f92088d5

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:dde3f92088d5 1 /*
jksoft 0:dde3f92088d5 2 Copyright 2011 Niels Brouwers
jksoft 0:dde3f92088d5 3
jksoft 0:dde3f92088d5 4 Licensed under the Apache License, Version 2.0 (the "License");
jksoft 0:dde3f92088d5 5 you may not use this file except in compliance with the License.
jksoft 0:dde3f92088d5 6 You may obtain a copy of the License at
jksoft 0:dde3f92088d5 7
jksoft 0:dde3f92088d5 8 http://www.apache.org/licenses/LICENSE-2.0
jksoft 0:dde3f92088d5 9
jksoft 0:dde3f92088d5 10 Unless required by applicable law or agreed to in writing, software
jksoft 0:dde3f92088d5 11 distributed under the License is distributed on an "AS IS" BASIS,
jksoft 0:dde3f92088d5 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 0:dde3f92088d5 13 See the License for the specific language governing permissions and
jksoft 0:dde3f92088d5 14 limitations under the License.#include <string.h>
jksoft 0:dde3f92088d5 15 */
jksoft 0:dde3f92088d5 16
jksoft 0:dde3f92088d5 17 /* Changed by Junichi Katsu */
jksoft 0:dde3f92088d5 18
jksoft 0:dde3f92088d5 19 #ifndef __adb_h__
jksoft 0:dde3f92088d5 20 #define __adb_h__
jksoft 0:dde3f92088d5 21
jksoft 0:dde3f92088d5 22 #include "mbed.h"
jksoft 0:dde3f92088d5 23 #include "USBHost.h"
jksoft 0:dde3f92088d5 24 #include "PacketBuffer.h"
jksoft 0:dde3f92088d5 25
jksoft 0:dde3f92088d5 26 typedef bool boolean;
jksoft 0:dde3f92088d5 27 typedef unsigned char uint8_t;
jksoft 0:dde3f92088d5 28 typedef unsigned short uint16_t;
jksoft 0:dde3f92088d5 29 typedef unsigned int uint32_t;
jksoft 0:dde3f92088d5 30
jksoft 0:dde3f92088d5 31
jksoft 0:dde3f92088d5 32 #define MAX_PAYLOAD 4096;
jksoft 0:dde3f92088d5 33
jksoft 0:dde3f92088d5 34 #define A_SYNC 0x434e5953
jksoft 0:dde3f92088d5 35 #define A_CNXN 0x4e584e43
jksoft 0:dde3f92088d5 36 #define A_OPEN 0x4e45504f
jksoft 0:dde3f92088d5 37 #define A_OKAY 0x59414b4f
jksoft 0:dde3f92088d5 38 #define A_CLSE 0x45534c43
jksoft 0:dde3f92088d5 39 #define A_WRTE 0x45545257
jksoft 0:dde3f92088d5 40
jksoft 0:dde3f92088d5 41 #define ADB_CLASS 0xff
jksoft 0:dde3f92088d5 42 #define ADB_SUBCLASS 0x42
jksoft 0:dde3f92088d5 43 #define ADB_PROTOCOL 0x1
jksoft 0:dde3f92088d5 44
jksoft 0:dde3f92088d5 45 #define ADB_USB_PACKETSIZE 0x40
jksoft 0:dde3f92088d5 46 #define ADB_CONNECTION_RETRY_TIME 1000
jksoft 0:dde3f92088d5 47
jksoft 0:dde3f92088d5 48 typedef struct
jksoft 0:dde3f92088d5 49 {
jksoft 0:dde3f92088d5 50 uint8_t address;
jksoft 0:dde3f92088d5 51 uint8_t configuration;
jksoft 0:dde3f92088d5 52 uint8_t interface;
jksoft 0:dde3f92088d5 53 uint8_t inputEndPointAddress;
jksoft 0:dde3f92088d5 54 uint8_t outputEndPointAddress;
jksoft 0:dde3f92088d5 55 } adb_usbConfiguration;
jksoft 0:dde3f92088d5 56
jksoft 0:dde3f92088d5 57 typedef struct
jksoft 0:dde3f92088d5 58 {
jksoft 0:dde3f92088d5 59 // Command identifier constant
jksoft 0:dde3f92088d5 60 uint32_t command;
jksoft 0:dde3f92088d5 61
jksoft 0:dde3f92088d5 62 // First argument
jksoft 0:dde3f92088d5 63 uint32_t arg0;
jksoft 0:dde3f92088d5 64
jksoft 0:dde3f92088d5 65 // Second argument
jksoft 0:dde3f92088d5 66 uint32_t arg1;
jksoft 0:dde3f92088d5 67
jksoft 0:dde3f92088d5 68 // Payload length (0 is allowed)
jksoft 0:dde3f92088d5 69 uint32_t data_length;
jksoft 0:dde3f92088d5 70
jksoft 0:dde3f92088d5 71 // Checksum of data payload
jksoft 0:dde3f92088d5 72 uint32_t data_check;
jksoft 0:dde3f92088d5 73
jksoft 0:dde3f92088d5 74 // Command ^ 0xffffffff
jksoft 0:dde3f92088d5 75 uint32_t magic;
jksoft 0:dde3f92088d5 76
jksoft 0:dde3f92088d5 77 } adb_message;
jksoft 0:dde3f92088d5 78
jksoft 0:dde3f92088d5 79 typedef enum
jksoft 0:dde3f92088d5 80 {
jksoft 0:dde3f92088d5 81 ADB_UNUSED = 0,
jksoft 0:dde3f92088d5 82 ADB_CLOSED,
jksoft 0:dde3f92088d5 83 ADB_OPEN,
jksoft 0:dde3f92088d5 84 ADB_OPENING,
jksoft 0:dde3f92088d5 85 ADB_RECEIVING,
jksoft 0:dde3f92088d5 86 ADB_WRITING
jksoft 0:dde3f92088d5 87 } ConnectionStatus;
jksoft 0:dde3f92088d5 88
jksoft 0:dde3f92088d5 89 typedef enum
jksoft 0:dde3f92088d5 90 {
jksoft 0:dde3f92088d5 91 ADB_CONNECT = 0,
jksoft 0:dde3f92088d5 92 ADB_DISCONNECT,
jksoft 0:dde3f92088d5 93 ADB_CONNECTION_OPEN,
jksoft 0:dde3f92088d5 94 ADB_CONNECTION_CLOSE,
jksoft 0:dde3f92088d5 95 ADB_CONNECTION_FAILED,
jksoft 0:dde3f92088d5 96 ADB_CONNECTION_RECEIVE
jksoft 0:dde3f92088d5 97 } adb_eventType;
jksoft 0:dde3f92088d5 98
jksoft 0:dde3f92088d5 99 class Connection;
jksoft 0:dde3f92088d5 100
jksoft 0:dde3f92088d5 101 // Event handler
jksoft 0:dde3f92088d5 102 typedef void(adb_eventHandler)(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data);
jksoft 0:dde3f92088d5 103
jksoft 0:dde3f92088d5 104 class Connection
jksoft 0:dde3f92088d5 105 {
jksoft 0:dde3f92088d5 106 private:
jksoft 0:dde3f92088d5 107 public:
jksoft 0:dde3f92088d5 108 char * connectionString;
jksoft 0:dde3f92088d5 109 uint32_t localID, remoteID;
jksoft 0:dde3f92088d5 110 uint32_t lastConnectionAttempt;
jksoft 0:dde3f92088d5 111 uint16_t dataSize, dataRead;
jksoft 0:dde3f92088d5 112 ConnectionStatus status;
jksoft 0:dde3f92088d5 113 boolean reconnect;
jksoft 0:dde3f92088d5 114 adb_eventHandler * eventHandler;
jksoft 0:dde3f92088d5 115 Connection * next;
jksoft 0:dde3f92088d5 116
jksoft 0:dde3f92088d5 117 int write(uint16_t length, uint8_t * data);
jksoft 0:dde3f92088d5 118 int writeString(char * str);
jksoft 0:dde3f92088d5 119 bool isOpen();
jksoft 0:dde3f92088d5 120 };
jksoft 0:dde3f92088d5 121
jksoft 0:dde3f92088d5 122 class ADB
jksoft 0:dde3f92088d5 123 {
jksoft 0:dde3f92088d5 124
jksoft 0:dde3f92088d5 125 private:
jksoft 0:dde3f92088d5 126 static void fireEvent(Connection * connection, adb_eventType type, uint16_t length, uint8_t * data);
jksoft 0:dde3f92088d5 127 static int writeEmptyMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1);
jksoft 0:dde3f92088d5 128 static int writeMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1, uint32_t length, uint8_t * data);
jksoft 0:dde3f92088d5 129 static int writeStringMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1, char * str);
jksoft 0:dde3f92088d5 130 static boolean pollMessage(adb_message * message, boolean poll);
jksoft 0:dde3f92088d5 131 static void openClosedConnections();
jksoft 0:dde3f92088d5 132 static void handleOkay(Connection * connection, adb_message * message);
jksoft 0:dde3f92088d5 133 static void handleClose(Connection * connection);
jksoft 0:dde3f92088d5 134 static void handleWrite(Connection * connection, adb_message * message);
jksoft 0:dde3f92088d5 135 static void handleConnect(adb_message * message);
jksoft 0:dde3f92088d5 136 static void AdbreadCallback(int device, int endpoint, int status, uint8_t* buf, int len, void* userData);
jksoft 0:dde3f92088d5 137
jksoft 0:dde3f92088d5 138 public:
jksoft 0:dde3f92088d5 139 static void init();
jksoft 0:dde3f92088d5 140 static void poll();
jksoft 0:dde3f92088d5 141
jksoft 0:dde3f92088d5 142 static void setEventHandler(adb_eventHandler * handler);
jksoft 0:dde3f92088d5 143 virtual void setupDevice()=0;
jksoft 0:dde3f92088d5 144 static Connection * addConnection(const char * connectionString, boolean reconnect, adb_eventHandler * eventHandler);
jksoft 0:dde3f92088d5 145 static int write(Connection * connection, uint16_t length, uint8_t * data);
jksoft 0:dde3f92088d5 146 static int writeString(Connection * connection, char * str);
jksoft 0:dde3f92088d5 147
jksoft 0:dde3f92088d5 148 static boolean isAdbDevice(int device, int configuration, int interfaceNumber);
jksoft 0:dde3f92088d5 149 static void closeAll();
jksoft 0:dde3f92088d5 150 };
jksoft 0:dde3f92088d5 151
jksoft 0:dde3f92088d5 152 #endif