Christian Burri / Mbed 2 deprecated autonomous Robot Android

Dependencies:   mbed

Fork of autonomous Robot Android by Christian Burri

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adb.h Source File

Adb.h

00001 /*
00002     Copyright 2011 Niels Brouwers
00003 
00004     Licensed under the Apache License, Version 2.0 (the "License");
00005     you may not use this file except in compliance with the License.
00006     You may obtain a copy of the License at
00007 
00008        http://www.apache.org/licenses/LICENSE-2.0
00009 
00010     Unless required by applicable law or agreed to in writing, software
00011     distributed under the License is distributed on an "AS IS" BASIS,
00012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013     See the License for the specific language governing permissions and
00014     limitations under the License.#include <string.h>
00015 */
00016 
00017 /* Changed by Junichi Katsu */
00018 
00019 #ifndef __adb_h__
00020 #define __adb_h__
00021 
00022 #include "mbed.h"
00023 #include "USBHost.h"
00024 #include "PacketBuffer.h"
00025 
00026 typedef bool boolean;
00027 typedef unsigned char uint8_t;
00028 typedef unsigned short uint16_t;
00029 typedef unsigned int uint32_t;
00030 
00031 
00032 #define MAX_PAYLOAD 4096;
00033 
00034 #define A_SYNC 0x434e5953
00035 #define A_CNXN 0x4e584e43
00036 #define A_OPEN 0x4e45504f
00037 #define A_OKAY 0x59414b4f
00038 #define A_CLSE 0x45534c43
00039 #define A_WRTE 0x45545257
00040 
00041 #define ADB_CLASS 0xff
00042 #define ADB_SUBCLASS 0x42
00043 #define ADB_PROTOCOL 0x1
00044 
00045 #define ADB_USB_PACKETSIZE 0x40
00046 #define ADB_CONNECTION_RETRY_TIME 1000
00047 
00048 typedef struct
00049 {
00050     uint8_t address;
00051     uint8_t configuration;
00052     uint8_t interface;
00053     uint8_t inputEndPointAddress;
00054     uint8_t outputEndPointAddress;
00055 } adb_usbConfiguration;
00056 
00057 typedef struct
00058 {
00059     // Command identifier constant
00060     uint32_t command;
00061 
00062     // First argument
00063     uint32_t arg0;
00064 
00065     // Second argument
00066     uint32_t arg1;
00067 
00068     // Payload length (0 is allowed)
00069     uint32_t data_length;
00070 
00071     // Checksum of data payload
00072     uint32_t data_check;
00073 
00074     // Command ^ 0xffffffff
00075     uint32_t magic;
00076 
00077 } adb_message;
00078 
00079 typedef enum
00080 {
00081     ADB_UNUSED = 0,
00082     ADB_CLOSED,
00083     ADB_OPEN,
00084     ADB_OPENING,
00085     ADB_RECEIVING,
00086     ADB_WRITING
00087 } ConnectionStatus;
00088 
00089 typedef enum
00090 {
00091     ADB_CONNECT = 0,
00092     ADB_DISCONNECT,
00093     ADB_CONNECTION_OPEN,
00094     ADB_CONNECTION_CLOSE,
00095     ADB_CONNECTION_FAILED,
00096     ADB_CONNECTION_RECEIVE
00097 } adb_eventType;
00098 
00099 class Connection;
00100 
00101 // Event handler
00102 typedef void(adb_eventHandler)(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data);
00103 
00104 class Connection
00105 {
00106 private:
00107 public:
00108     char * connectionString;
00109     uint32_t localID, remoteID;
00110     uint32_t lastConnectionAttempt;
00111     uint16_t dataSize, dataRead;
00112     ConnectionStatus status;
00113     boolean reconnect;
00114     adb_eventHandler * eventHandler;
00115     Connection * next;
00116 
00117     int write(uint16_t length, uint8_t * data);
00118     int writeString(char * str);
00119     bool isOpen();
00120 };
00121 
00122 class ADB
00123 {
00124 
00125 private:
00126     static void fireEvent(Connection * connection, adb_eventType type, uint16_t length, uint8_t * data);
00127     static int writeEmptyMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1);
00128     static int writeMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1, uint32_t length, uint8_t * data);
00129     static int writeStringMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1, char * str);
00130     static boolean pollMessage(adb_message * message, boolean poll);
00131     static void openClosedConnections();
00132     static void handleOkay(Connection * connection, adb_message * message);
00133     static void handleClose(Connection * connection);
00134     static void handleWrite(Connection * connection, adb_message * message);
00135     static void handleConnect(adb_message * message);
00136     static void AdbreadCallback(int device, int endpoint, int status, uint8_t* buf, int len, void* userData);
00137     
00138 public:
00139     static void init();
00140     static void poll();
00141 
00142     static void setEventHandler(adb_eventHandler * handler);
00143     virtual void setupDevice()=0;
00144     static Connection * addConnection(const char * connectionString, boolean reconnect, adb_eventHandler * eventHandler);
00145     static int write(Connection * connection, uint16_t length, uint8_t * data);
00146     static int writeString(Connection * connection, char * str);
00147     
00148     static boolean isAdbDevice(int device, int configuration, int interfaceNumber);
00149     static void closeAll();
00150 };
00151 
00152 #endif