Subversion Repositories Projects

Rev

Rev 127 | Blame | Last modification | View Log | RSS feed

#include "fc.h"


unsigned char TxBuffer[150];
unsigned char _TxBuffer[150];

char RxBuffer[150];
char PrintableRxBuffer[150];



int mk_socket;

int status;

void AddCRC(unsigned int wieviele)
{
  unsigned int tmpCRC = 0,i;
  for(i = 0; i < wieviele;i++)
    {
      tmpCRC += TxBuffer[i];
    }
  tmpCRC %= 4096;
  TxBuffer[i++] = '=' + tmpCRC / 64;
  TxBuffer[i++] = '=' + tmpCRC % 64;
  TxBuffer[i++] = '\r';
}


void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
{
  int status =0;
  unsigned int pt = 0;
  unsigned char a,b,c;
  unsigned char ptr = 0;

  TxBuffer[pt++] = '#';               // Startzeichen
  TxBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
  TxBuffer[pt++] = cmd;                 // Commando

  while(len)
    {
      if(len) { a = snd[ptr++]; len--;} else a = 0;
      if(len) { b = snd[ptr++]; len--;} else b = 0;
      if(len) { c = snd[ptr++]; len--;} else c = 0;
      TxBuffer[pt++] = '=' + (a >> 2);
      TxBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
      TxBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
      TxBuffer[pt++] = '=' + ( c & 0x3f);
    }
 
 

  AddCRC(pt);
  printf("Sending to MK %d \n" , pt);

  status = send(mk_socket,"\r" , 1, 0);
 
 
  //  for (c=0;c<pt+2;c++)
  // {
  status = write(mk_socket,&TxBuffer , pt+3);
 

  //   printf("Send to MK %d \n" , TxBuffer[c] );
  // }
  /* int i=0;
  while(TxBuffer[i] !='\r' && i<150)
    {
    //     TxBuffer[i]='#';
    status = send(mk_socket,TxBuffer[i] , 1, 0);
    printf(" +%d%c ",i,TxBuffer[i]);
    i++;
    }

  //  status = send(s,"\r" , 1, 0);
  */

   status = send(mk_socket,"\r" , 1, 0);
   status = send(mk_socket,"\n" , 1, 0);
  printf("\n");
}


int rx_last_length;

int read_from_mk()
{      
  char in_char='#';
  int count=0;
  int r=0;
 
  printf("starting read\n");
  while(in_char!='\n')
    {
      //      printf("b read\n");
      count=read(mk_socket,&in_char,1);
      //printf("a read %d\n",count);
      if (count!=-1)
        {
          //  printf("%c\n",in_char);
          RxBuffer[r]=in_char;
     
          if (in_char!=0)
            PrintableRxBuffer[r++]=in_char;
          else
            PrintableRxBuffer[r++]='0';
        }

    }
  rx_last_length=r;
  PrintableRxBuffer[r++]='\0'; // terminate
  printf("done --->%s\n",PrintableRxBuffer);

  if (RxBuffer[2]=='D')
    debug_sets++;

  return 1;
}


int connect_mk_bluetooth(char dest[18])
{
 
  struct sockaddr_rc addr ;

  // allocate a socket
  mk_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
 
  // set the connection parameters (who to connect to)
  addr.rc_family = AF_BLUETOOTH;
  addr.rc_channel = 1;
  str2ba( dest, &addr.rc_bdaddr );

  // connect to server
  status = connect(mk_socket, (struct sockaddr *)&addr, sizeof(addr));

  printf("connection status %d\n",status);
  return status;
 
}




int connect_mk_localhost_socket(int port)
{

  int status;
  struct sockaddr_in sa;
 
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl(0x0);
  sa.sin_port = htons(port);
 
  // allocate a socket
  //  s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  mk_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  //);

  // set the connection parameters (who to connect to)

  // connect to server
  //  status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
  status = connect(mk_socket,(struct sockaddr*) &sa, sizeof(struct sockaddr_in));

  printf("connection status %d\n",status);
  return status;

}