]> rtime.felk.cvut.cz Git - mirosot.git/blobdiff - bluetooth/bth_main.c
An unsucesfull attempt to clean bluetooth library and make it working.
[mirosot.git] / bluetooth / bth_main.c
index 69e95b1067657d99ed76ae198bc1db7151d45f35..4a049ca02c04360e7ea80181d7b3461b36defd08 100644 (file)
@@ -6,15 +6,15 @@
   Copyright (C) 2006 by Petr Kovacik petr_kovacik@gmail.com
 
  *******************************************************************/
-#ifdef BTH_LX
+#include <bth_config.h>
 #include <types.h>
+#ifdef BTH_LX
 #include <cpu_def.h>
 #include <h8s2638h.h>
 #include <periph/sci_rs232.h>
 #include <system_def.h>
 #include <string.h>
 #else
-#include "types.h"
 #include <stdio.h>
 #endif
 
@@ -32,7 +32,9 @@
 #include "bth_inface.h"
 #include "bth_fce_out.h"
 #include "bth_h8s2638.h"
-#include <periph/sci_rs232.h>
+
+
+#define BTH_BUF_LEN 300
 
 /*--------BD address whitch are accept for connection-------------*/
 bdaddr_t bth_accept_bd_addr[]={};
@@ -44,20 +46,16 @@ bths_dev_info  bth_local_info;//={0,{0,0,0,0,0,0,0,0},{0,0,0},0,{0,0,0},0};
 bths_connect_bluet* bth_connected[8];
 
 /*-------------------------------- recieve data buffer -----------------------------*/
-uint8_t  bth_pole_char_in[SCI_BTH_BUF_LEN];
 bth_que_t bth_rs232_que_in;
-uint8_t bth_rs232_buf_in[SCI_BTH_BUF_LEN];
-//-- --- ----- ----- ---- ------ ----- -- ---- --
-//-- --- ----- ----- ---- ------ ----- -- ---- --
-bth_que_t bth_inface_que_in;
-bth_que_t bth_inface_que_out;
+//bth_que_t bth_inface_que_in;
+//bth_que_t bth_inface_que_out;
 
 
 
-void  *bth_pole_adrr_comm_packet[LENCOMMAND]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
+void  *bth_array_adrr_comm_packet[LENCOMMAND]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
 bths_command_buf_info bth_com_buf_info={LENCOMMAND,0,0};
 /***********************************************************************************/
-void  *bth_pole_adrr_check_packet[LENCOMMAND]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
+void  *bth_array_adrr_check_packet[LENCOMMAND]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
 bths_command_buf_info bth_check_buf_info={LENCOMMAND,0,0};
 /***********************************************************************************/
 
@@ -84,58 +82,58 @@ int  bth_send_queue(void)
   int i;
   int hp_size;
   if(bth_local_info.busy!=0)
-    {return 1;}; //device is busy
+  {return 1;}; //device is busy
   if(bth_com_buf_info.aktual==bth_com_buf_info.sent) //PRAVDEPODOBNE neni co odesilat
   {
     if(bth_com_buf_info.sent<(LENCOMMAND-2))
     {
-      if(bth_pole_adrr_comm_packet[bth_com_buf_info.sent+1]==NULL)
+      if(bth_array_adrr_comm_packet[bth_com_buf_info.sent+1]==NULL)
       {return 1;}; //There is certainly nothing to send
     }
     else
     {
-      if(bth_pole_adrr_comm_packet[0]==NULL)
+      if(bth_array_adrr_comm_packet[0]==NULL)
       {return 1;}; //There is certainly nothing to send
     };
   };
     
   /*bth_send_packet(AdrFrom,howmanyBytes;)*/
-    switch(*(uint8_t*)bth_pole_adrr_comm_packet[bth_com_buf_info.sent])
-    {
-      case HCI_ACLDATA_PKT:
-        __bthtomc16((uint8_t*)&(hp_size),((uint8_t*)bth_pole_adrr_comm_packet[bth_com_buf_info.sent])+HCI_PKT_SIZE+HCI_ACL_HDR____dlen);
-        hp_size=hp_size+HCI_ACL_HDR_SIZE+HCI_PKT_SIZE;
-        if(!(bth_send_packet(bth_pole_adrr_comm_packet[bth_com_buf_info.sent],hp_size)))
+  switch(*(uint8_t*)bth_array_adrr_comm_packet[bth_com_buf_info.sent])
+  {
+    case HCI_ACLDATA_PKT:
+      __bthtomc16((uint8_t*)&(hp_size),((uint8_t*)bth_array_adrr_comm_packet[bth_com_buf_info.sent])+HCI_PKT_SIZE+HCI_ACL_HDR____dlen);
+      hp_size=hp_size+HCI_ACL_HDR_SIZE+HCI_PKT_SIZE;
+      if(!(bth_send_packet(bth_array_adrr_comm_packet[bth_com_buf_info.sent],hp_size)))
+      {
+        free(bth_array_adrr_comm_packet[bth_com_buf_info.sent]);
+        bth_array_adrr_comm_packet[bth_com_buf_info.sent]=NULL;
+        if(bth_com_buf_info.sent<(LENCOMMAND-2)){bth_com_buf_info.sent++;}
+        else{ bth_com_buf_info.sent=0;};
+        return(0);
+      };
+      free(bth_array_adrr_comm_packet[bth_com_buf_info.sent]);
+      bth_array_adrr_comm_packet[bth_com_buf_info.sent]=NULL;
+      return (-6);
+    case HCI_COMMAND_PKT:
+      hp_size=((uint16_t)*(((uint8_t*)bth_array_adrr_comm_packet[bth_com_buf_info.sent])+HCI_PKT_SIZE+2)+HCI_COMMAND_HDR_SIZE+HCI_PKT_SIZE);
+      if(!(bth_send_packet(bth_array_adrr_comm_packet[bth_com_buf_info.sent],hp_size)))
+      {/*sent - add the command to allow confirmation*/
+        for(i=0; i<LENCOMMAND; i++)
         {
-          free(bth_pole_adrr_comm_packet[bth_com_buf_info.sent]);
-          bth_pole_adrr_comm_packet[bth_com_buf_info.sent]=NULL;
-          if(bth_com_buf_info.sent<(LENCOMMAND-2)){bth_com_buf_info.sent++;}
-          else{ bth_com_buf_info.sent=0;};
-          return(0);
-        };
-        free(bth_pole_adrr_comm_packet[bth_com_buf_info.sent]);
-        bth_pole_adrr_comm_packet[bth_com_buf_info.sent]=NULL;
-        return (-6);
-      case HCI_COMMAND_PKT:
-        hp_size=((uint16_t)*(((uint8_t*)bth_pole_adrr_comm_packet[bth_com_buf_info.sent])+HCI_PKT_SIZE+2)+HCI_COMMAND_HDR_SIZE+HCI_PKT_SIZE);
-        if(!(bth_send_packet(bth_pole_adrr_comm_packet[bth_com_buf_info.sent],hp_size)))
-        {/*sent - add the command to allow confirmation*/
-          for(i=0; i<LENCOMMAND; i++)
+          if(bth_array_adrr_check_packet[i]==NULL)
           {
-            if(bth_pole_adrr_check_packet[i]==NULL)
-            {
-              bth_pole_adrr_check_packet[i]=bth_pole_adrr_comm_packet[bth_com_buf_info.sent];
-              bth_pole_adrr_comm_packet[bth_com_buf_info.sent]=NULL;
-              /*set the address for the next command - principle of ring buffer*/
-              if(bth_com_buf_info.sent<(LENCOMMAND-2)){bth_com_buf_info.sent++;}
-              else{ bth_com_buf_info.sent=0;};
-              return 0;
-            }
-          };
-          return(-7);
+            bth_array_adrr_check_packet[i]=bth_array_adrr_comm_packet[bth_com_buf_info.sent];
+            bth_array_adrr_comm_packet[bth_com_buf_info.sent]=NULL;
+            /*set the address for the next command - principle of ring buffer*/
+            if(bth_com_buf_info.sent<(LENCOMMAND-2)){bth_com_buf_info.sent++;}
+            else{ bth_com_buf_info.sent=0;};
+            return 0;
+          }
         };
-        return(-6);
-    };
+        return(-7);
+      };
+      return(-6);
+  };
   return (1);
 };
 
@@ -150,10 +148,11 @@ int  bth_send_queue(void)
 // OUTPUT :none
 //*************************************************************************** 
 void bth_conv_char_text(uint8_t zn)
-{ uint8_t map_leters[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+{
   uint8_t val;
   val=(zn&0xf0)>>4;
 #ifdef BTH_LX
+  uint8_t map_leters[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
   sci_rs232_sendch(map_leters[val],sci_rs232_chan_default);
   val=(zn&0x0f);
   sci_rs232_sendch(map_leters[val],sci_rs232_chan_default);
@@ -163,41 +162,11 @@ void bth_conv_char_text(uint8_t zn)
 
 
 
-/**
-  //FIXME
- *Sends data of the size hp_size from address hp to the bluetooth device (UART)
- */
-int bth_send_packet(uint8_t *hp, uint16_t hp_size)
-{  
-  int zn,i;
-  /*is bluetooth device free, or is it busy by the previous command?*/
-  if(bth_local_info.busy==0)
-  {
-#ifndef BTH_LX
-      VypisHexa(hp,hp_size);
-#else
-
-    for(i=0; i<hp_size;i++)
-    {
-      zn=*(hp+i);
-      sci_rs232_sendch(zn,2); //bth
-//      VypisHexa((hp+i),1);
-    };
-#endif
-    bth_local_info.busy=1;
-    return(0);    //everything sent ==== OK =====
-  }
-  else
-  {
-    return 2;    //busy
-  };
-};
-
 /**
  *Inserts a character to the queue
  */
 
-inline int bth_que_put(bth_que_t *q, int c)
+static inline int bth_que_put(bth_que_t *q, int c)
 {
   uint8_t *p;
   p=q->ip;                       //FIXME set us to the actual position in the file
@@ -397,8 +366,8 @@ int bth_recieve_packet(int c)
   {
   case HCI_EVENT_PKT:
     /*fill in structure "hci_event_hdr" from the queue (bye by byte)*/
-    store16(bth_event_packet.evt,  *(((uint8_t*)bth_rs232_que_in.buf_beg)+HCI_PKT_SIZE));
-    store16(bth_event_packet.plen, *(((uint8_t*)bth_rs232_que_in.buf_beg)+HCI_PKT_SIZE+1));
+    store8(bth_event_packet.evt,  *(((uint8_t*)bth_rs232_que_in.buf_beg)+HCI_PKT_SIZE));
+    store8(bth_event_packet.plen, *(((uint8_t*)bth_rs232_que_in.buf_beg)+HCI_PKT_SIZE+1));
 
       if(bth_rs232_que_in.ip-bth_rs232_que_in.buf_beg>3)
     {
@@ -451,7 +420,7 @@ int bth_recieve_packet(int c)
           bth_acl_packet.handle=bth_acl_packet.handle & 0x0fff;
           
           /*variable 'char_acl_pkt' stores the properties of the packet, if it is point to point or broadcast, or char. L2CAP*/
-          store16(char_acl_pkt, (0xf0&*(((uint8_t*)bth_rs232_que_in.buf_beg)+HCI_PKT_SIZE+HCI_ACL_HDR____handle+1)));
+          store8(char_acl_pkt, (0xf0&*(((uint8_t*)bth_rs232_que_in.buf_beg)+HCI_PKT_SIZE+HCI_ACL_HDR____handle+1)));
           
           /*decide which packet is this (PtP,Piconet,broat...) and the type of communication (L2CAP, HCI)*/
           data_ad=(uint8_t*)bth_rs232_que_in.buf_beg+HCI_PKT_SIZE+HCI_ACL_HDR_SIZE;
@@ -513,11 +482,12 @@ void bth_start(void)
 */
 void bth_init(void)
 {
+  static uint8_t  bth_array_char_in[70];
 //  bth_check_command_for_send=NULL;
 
-  bth_rs232_que_in.buf_end=(uint8_t*)bth_pole_char_in+SCI_RS232_BUF_LEN;
-  bth_rs232_que_in.buf_beg=bth_pole_char_in;
-  bth_rs232_que_in.ip=bth_pole_char_in;
+  bth_rs232_que_in.buf_end=(uint8_t*)bth_array_char_in+BTH_BUF_LEN;
+  bth_rs232_que_in.buf_beg=bth_array_char_in;
+  bth_rs232_que_in.ip=bth_array_char_in;
 };
 
 /****************************************************************************/