Dependents:   mbed_TANK_Kinect ftusbClass

Revision:
3:a90b1a8fd184
Parent:
2:06b267903269
Child:
4:1c90839a1e70
--- a/RFCOMM.cpp	Sat Jun 11 17:37:35 2011 +0000
+++ b/RFCOMM.cpp	Sun Jun 19 12:36:31 2011 +0000
@@ -35,13 +35,20 @@
 
 #define NR_CREDITS  1
 #define INITIAL_CREDITS 1 //0...7
-#define MAX_FRAME_SIZE  350  //ACL buffer - some headroom
+
+#define DCD (1<<7) //DV data carrier detect
+#define RI  (1<<6) //IC ring indicator
+#define RTS (1<<3) //RTR request to send
+#define DSR (1<<2) //RTC data set ready
+#define FC  (1<<1) //Flow Control
+#define EA  (1<<0) //extended address (always 1)
+#define SIGNALS (DCD | RTS | DSR | EA)
 
 #define DEBUG   1
 #define TAKE_INITIATIVE
 
 //uint8_t rfcomm_out_buffer[1000];//seems a bit big as default max framesize is 127
-unsigned rfcomm::maxframesize = MAX_FRAME_SIZE; //only initial value
+//unsigned rfcomm::maxframesize = MAX_FRAME_SIZE; //only initial value
 
 //these functions are obtained from rfcomm.c on google code
 void _bt_rfcomm_send_sabm(unsigned short source_cid, unsigned char initiator, unsigned char channel);
@@ -153,13 +160,36 @@
     printf("%d channels are still open\n", n);
     if (n == 0) {//all rfcomm channels are closed
         printf("....L2CAP must be closed as well\n");
-        rfcomm_send_packet(_l2cap, (1 << 0) | (initiator << 1) /*|  (!initiator << 2)*/, BT_RFCOMM_DISC, 0, 0, 0); //close dlci0
+        rfcomm_send_packet(_l2cap, (1 << 0) | (initiator << 1), BT_RFCOMM_DISC, 0, 0, 0); //close dlci0
         Socket_Close(_l2cap);
         _l2cap = 0; //return rfcomm to the pool
     }
     return 0;
 }
 
+int rfcomm::SetOpt(SocketInternal *sock, int so, int* data, int len) {
+  switch (so) {
+    case SO_RECBUF:
+    case SO_SNDBUF:
+       maxframesize = *data; //pointless because setting takes effect only before socket is opened (before PN)
+       break;
+    default: return NOPROTOOPT;
+  }
+  return 0;
+}
+
+int rfcomm::GetOpt(SocketInternal *sock, int so, int* data, int len) {
+  switch (so) {
+    case SO_RECBUF:
+    case SO_SNDBUF:
+         if (len >= sizeof(int))
+           *data = maxframesize;
+         break;
+    default: return NOPROTOOPT;
+  }
+  return 0;
+}
+
 int rfcomm::Disconnect(RFCOMMSocket *s) {
     unsigned char address = (1 << 0) | (initiator << 1) | (s->dlci << 2);
     return rfcomm_send_packet(_l2cap, address, BT_RFCOMM_DISC, 0, 0, 0);
@@ -204,21 +234,6 @@
     return 0;
 }
 
-/*
-int rfcomm::Open(BD_ADDR* bdAddr, inquiry_info* info) {//obsolete???
-    _addr = *bdAddr;
-    L2CAPAddr sockAddr;
-    sockAddr.bdaddr = _addr;
-    //open an L2CAP socket for RFCOMM
-    sockAddr.psm = L2CAP_PSM_RFCOMM;
-    _l2cap = Socket_Open(SOCKET_L2CAP,&sockAddr.hdr,OnRfCommControl,this);//this is the socket between the RFCOMM and the L2CAP layer
-
-    printfBytes("OPEN DEVICE CLASS",info->dev_class,3);
-    _devClass = (info->dev_class[0] << 16) | (info->dev_class[1] << 8) | info->dev_class[2];
-    return _l2cap;
-}
-*/
-
 //socket is an L2CAP socket and state is the state of the L2CAP socket, not the RFCOMM socket
 void rfcomm::OnRfCommControl(int socket, SocketState state, const u8* data, int len, void* userData) {
     int packet_processed = 0;
@@ -269,7 +284,6 @@
                 printf("Received RFCOMM unnumbered acknowledgement for channel 0 - multiplexer working\n");
                 printf("Sending UIH Parameter Negotiation Command from OnRfCommControl\n");
                 self->initChannels(socket);
-                //_bt_rfcomm_send_uih_pn_command(socket, initiator, rfcomm_ch, MAX_FRAME_SIZE);
                 break;
             case BT_RFCOMM_UIH://  received UIH Parameter Negotiation Response
                 switch (payload[0]) {
@@ -281,14 +295,14 @@
                         RFCOMMSocket *r = self->find_socket(payload[2]);
                         if (r==0) break;
                         r->my_credits = payload[9]; //initial amount of credits
-                        maxframesize = min(maxframesize, payload[6] + (payload[7]<<8));
-                        printf("Max Frame Size = %d, got %d initial credits\n", maxframesize, payload[9]);
+                        self->maxframesize = min(self->maxframesize, payload[6] + (payload[7]<<8));
+                        printf("Max Frame Size = %d, got %d initial credits\n", self->maxframesize, payload[9]);
                     }
                     break;
-                    case BT_RFCOMM_PN_CMD: { //remote side send PN command, mtu and initial credits
+                    case BT_RFCOMM_PN_CMD: { //remote side sent PN command, mtu and initial credits
                         packet_processed++;
                         printf("UIH Parameter Negotiation Indication\n");
-                        maxframesize = min(maxframesize, payload[6] + (payload[7]<<8));
+                        self->maxframesize = min(self->maxframesize, payload[6] + (payload[7]<<8));
                         unsigned char cred = payload[9] & 7;
                         unsigned char _dlci = payload[2];
 
@@ -305,10 +319,10 @@
                         memcpy(reply, payload, sizeof(reply));
                         reply[0] = BT_RFCOMM_PN_RSP;//[1]=len, [2]=dlci, [4]=priority, [5]=timer(must be 0), [8] retransmissions (must be 0)
                         reply[3] = payload[3]==0xF0 ? 0xE0 : 0; //support credit based flow control
-                        reply[6] = maxframesize;
-                        reply[7] = maxframesize>>8;
+                        reply[6] = self->maxframesize;
+                        reply[7] = self->maxframesize>>8;
                         reply[9] = payload[3]==0xF0 ? r->peer_credits : 0;
-                        printf("Max Frame Size = %d, give %d initial credits\n", maxframesize, reply[9]);
+                        printf("Max Frame Size = %d, give %d initial credits\n", self->maxframesize, reply[9]);
                         rfcomm_send_packet(socket, addr^2, BT_RFCOMM_UIH, 0, reply, sizeof(reply));
                     }
                     break;
@@ -316,7 +330,7 @@
                         packet_processed++;
                         {
                             printf("Received BT_RFCOMM_MSC_IND\n");
-                            // fine with this
+                            // fine with this, return the same status and ignore the value, there is no room in the socket to store the value, rfcomm could generate an event
                             RFCOMMSocket *r = self->find_socket(payload[2]>>2);
                             if (r==0) break;
                             unsigned char reply[5];
@@ -328,7 +342,7 @@
                                 case SocketState_L2CAP_Config_wait:
                                     r->State = SocketState_L2CAP_Config_wait_send;
                                     printf("Sending MSC_CMD\n");
-                                    _bt_rfcomm_send_uih_msc_cmd(socket, initiator, payload[2]>>2, 0x8d);  // ea=1,fc=0,rtc(DSR/DTR)=1,rtr(RTS/CTs)=1,ic(RI)=0,dv(DCD)=1
+                                    _bt_rfcomm_send_uih_msc_cmd(socket, initiator, payload[2]>>2, SIGNALS);  // ea=1,fc=0,rtc(DSR/DTR)=1,rtr(RTS/CTs)=1,ic(RI)=0,dv(DCD)=1
                                     r->State = SocketState_L2CAP_Config_wait_rsp;
                                     break;
                                 case SocketState_L2CAP_Config_wait_reqrsp:
@@ -492,6 +506,8 @@
     printf("\x1B[%dm", 0);//reset terminal colour
 }
 
+//should make the functions below member functions
+
 /**
  * @param credits - only used for RFCOMM flow control in UIH wiht P/F = 1
  */