Test version of BlueUSB stack. Includes SDP and RFCOMM. As Client it allows to connect to my fischertechnik TX Controller. As Server it echo\\\\\\\'s characters to Putty. PIN=1234

Dependencies:   mbed myUSBHost AvailableMemory

Dependents:   mbed_TANK_Kinect myBlueUSB_ros ftusbClass

Revision:
1:0dde58e0cccf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestShell.cpp	Mon Apr 04 16:47:10 2011 +0000
@@ -0,0 +1,223 @@
+
+/*
+Copyright (c) 2010 Peter Barrett
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+#include "mbed.h"
+#include <vector>
+#include "Utils.h"
+#include "USBHost.h"
+#include "hci.h"
+#include "HCITransportUSB.h"
+#include "RFCOMM.h"
+#include "ftclasslibusbdevbt.h"
+#include "sdp_data.h"
+#include "sdp.h"
+
+/************************************************
+TODO:
+mtu and credits are completely unhandled - in progress
+multiple rfcomm sessions should be possible - done
+SDP would be nice - beta
+multiple rfcomm channels are untested
+decoupling of rfcomm and application - much better
+packets are not reassembled - some are (HCI and ft application level)
+disconnect and timeouts
+************************************************/
+#define DEBUG   1
+int state = 0;
+
+void printf(const BD_ADDR* addr) {
+    const u8* a = addr->addr;
+    printf("%02X:%02X:%02X:%02X:%02X:%02X",a[5],a[4],a[3],a[2],a[1],a[0]);
+}
+
+//int bulk = 0;
+
+char FtDevClass[3] = {0x00, 0x1F, 0x82 };
+
+//  Instance
+RFCOMMManager rfcomm_manager;
+
+class application : public HCI {
+        BTDevice* devs[8];
+        int count, i, pending;
+public:
+    //  We have connected to a device
+    void ConnectionComplete(connection_info* info) {
+        printf("ConnectionComplete ");
+        BD_ADDR* a = &info->bdaddr;
+        printf(a);
+        printf("\n");
+        RemoteNameRequest(a);
+        for (i++; i < count; i++) {//find the next device to open
+            printfBytes("DEVICE CLASS",devs[i]->_info.dev_class,3);
+            if (devs[i]->_handle == 0 && memcmp(devs[i]->_info.dev_class, FtDevClass, 3)==0) {//or some other way to connect to RFCOMM devices
+                BD_ADDR* bd = &devs[i]->_info.bdaddr;
+                printf("Connecting to ");
+                printf(bd);
+                printf("\n");
+        pending++;
+                CreateConnection(bd); //some low level connect, just let it happen for now (sets pin, mtu etc.)
+                printf("connection cmd was sent\n");
+                return;
+            }
+        }
+        //state = 1; //start the real application
+    }
+
+    void ConnectDevices() {
+        count = GetDevices(devs,8);//get pointers to all bluetooth devices
+        pending = 0;
+        for (i = 0; i < count; i++) {
+            printfBytes("DEVICE CLASS",devs[i]->_info.dev_class,3);
+            if (devs[i]->_handle == 0 && memcmp(devs[i]->_info.dev_class, FtDevClass, 3)==0) {//or some other way to connect to RFCOMM devices
+                BD_ADDR* bd = &devs[i]->_info.bdaddr;
+                printf("Connecting to ");
+                printf(bd);
+                printf("\n");
+        pending++;
+                CreateConnection(bd); //some low level connect, just let it happen for now (sets pin, mtu etc.)
+                printf("connection cmd was sent\n");
+                return;
+            }
+        }
+        if (pending == 0) state = 1;//for the case when there are no ft devices
+    }
+    virtual void Callback(HCI_CALLBACK_EVENT c, const u8* data, int len);
+} App; //application instance
+
+
+void application::Callback(HCI_CALLBACK_EVENT evt, const u8* data, int len) {//these events are forwarded (in)directly from HCIRecv
+    unsigned char pin[] = "1234";
+    printf("\x1b[%dm", 33);
+    switch (evt) {
+        case CALLBACK_READY:
+            printf("CALLBACK_READY\n");
+            Inquiry();//start the second phase of the discovery
+            break;
+
+        case CALLBACK_INQUIRY_RESULT: //optionally build the list of FT devices here
+            printf("CALLBACK_INQUIRY_RESULT ");
+            printf((BD_ADDR*)data);
+            printf("\n");//data points to inquiry_info struct
+//          RemoteNameRequest((BD_ADDR*)data);
+            break;
+
+        case CALLBACK_INQUIRY_DONE:
+            printf("CALLBACK_INQUIRY_DONE\n");
+            ConnectDevices();
+            break;
+
+        case CALLBACK_REMOTE_NAME: {
+            BD_ADDR* addr = (BD_ADDR*)data;
+            const char* name = (const char*)(data + 6);
+            printf(addr);
+            printf(" = % s\n",name);
+            pending--;
+            if (pending == 0) state = 1;
+        }
+        break;
+
+        case CALLBACK_CONNECTION_COMPLETE:
+            ConnectionComplete((connection_info*)data);
+            {
+                printf("Going to open sdp socket\n");
+                L2CAPAddr addr;
+                connection_info *ci = (connection_info*)data;
+                memcpy(&addr.bdaddr, &ci->bdaddr, 6);
+                addr.psm = 1;//should not matter
+                int s = Socket_Open(SOCKET_SDP, &addr.hdr, &SDP.OnSockCallback, &SDP);
+            }
+            break;
+        case CALLBACK_PIN_REQ:
+            printf("Enter PIN for ");
+            printf((BD_ADDR*)data);
+            printf(" :  submitting %s\n", pin);
+            PinCodeReply(data, pin);
+            break;
+    };
+    printf("\x1b[%dm", 0);
+}
+
+//  these should be placed in the DMA SRAM
+typedef struct {
+    u8 _hciBuffer[MAX_HCL_SIZE];
+    u8 _aclBuffer[MAX_ACL_SIZE];
+} SRAMPlacement;
+
+HCITransportUSB _HCITransportUSB;  //use USB as the transport to the radio
+
+int OnBluetoothInsert(int device) {//install the HCI and start discovery, user callbacks are made to HciCalback
+    printf("Bluetooth inserted of %d\n",device);
+    u32 sramLen;
+    u8* sram =  USBGetBuffer(&sramLen);
+    sram = (u8*)(((u32)sram + 1023) & ~1023);
+    SRAMPlacement* s = (SRAMPlacement*)sram;
+    _HCITransportUSB.Open(device,s->_hciBuffer,s->_aclBuffer);//setup buffers for USB host, incoming data goes first to HCIRecv and ACLRecv
+    RegisterSocketHandler(SOCKET_L2CAP,&App); //register the application::hci as handler for L2CAP events
+    RegisterSocketHandler(SOCKET_RFCOM, &rfcomm_manager);//set the RFCOMMManager as the RFCOM socket handler
+    if (RegisterSocketHandler(SOCKET_SDP, &SDP))
+        printf("Could not register SDP socket type\n");
+    App.Open(&_HCITransportUSB);
+    App.Inquiry();//start discovery of BT devices phase 0
+    return 0;
+}
+
+DigitalOut led(LED1), loop(LED2);
+
+void TestShell() {
+    int n=0;
+    USBInit();
+    for (;;) {
+        switch (state) {
+            case 0: //inquiry and low-level connection
+                break;
+            case 1: {//initialisation
+                printf("Ready to open ports\n");
+                InitFtBtDeviceList();
+                int n = GetNrOfFtBtDevices();
+                printf("%d ft BT devices have been found\n", n);
+                if (n > 0) {
+                    ftbtdev *d = GetFtUsbDeviceHandle(0);
+                    if (d==0) printf("could not get device handle\n");
+                    int sock = OpenFtBtDevice(d);
+                }
+                state = 2;
+            }
+            break;
+            case 2://main loop
+                break;
+            default:
+                break;
+        }
+        loop=1;
+        USBLoop();
+        loop=0;
+        n++;
+        if (n>=500000) {
+            n=0;
+            led = !led;
+        }
+    }
+    //printf("Dropped out of main loop!\n");
+}
+
+//********************************************************************************************************************************