Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
127 ligi 1
#include "fc.h"
2
 
3
 
4
unsigned char TxBuffer[150];
5
unsigned char _TxBuffer[150];
6
 
130 ligi 7
char RxBuffer[150];
8
char PrintableRxBuffer[150];
127 ligi 9
 
130 ligi 10
 
11
 
127 ligi 12
int mk_socket;
13
 
130 ligi 14
int status;
127 ligi 15
 
16
void AddCRC(unsigned int wieviele)
17
{
18
  unsigned int tmpCRC = 0,i;
19
  for(i = 0; i < wieviele;i++)
20
    {
21
      tmpCRC += TxBuffer[i];
22
    }
23
  tmpCRC %= 4096;
24
  TxBuffer[i++] = '=' + tmpCRC / 64;
25
  TxBuffer[i++] = '=' + tmpCRC % 64;
26
  TxBuffer[i++] = '\r';
27
}
28
 
29
 
30
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
31
{
32
  int status =0;
33
  unsigned int pt = 0;
34
  unsigned char a,b,c;
35
  unsigned char ptr = 0;
36
 
37
  TxBuffer[pt++] = '#';               // Startzeichen
38
  TxBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
39
  TxBuffer[pt++] = cmd;                 // Commando
40
 
41
  while(len)
42
    {
43
      if(len) { a = snd[ptr++]; len--;} else a = 0;
44
      if(len) { b = snd[ptr++]; len--;} else b = 0;
45
      if(len) { c = snd[ptr++]; len--;} else c = 0;
46
      TxBuffer[pt++] = '=' + (a >> 2);
47
      TxBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
48
      TxBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
49
      TxBuffer[pt++] = '=' + ( c & 0x3f);
50
    }
51
 
52
 
53
 
54
  AddCRC(pt);
55
  printf("Sending to MK %d \n" , pt);
56
 
57
  status = send(mk_socket,"\r" , 1, 0);
58
 
59
 
60
  //  for (c=0;c<pt+2;c++)
61
  // {
62
  status = write(mk_socket,&TxBuffer , pt+3);
130 ligi 63
 
64
 
127 ligi 65
  //   printf("Send to MK %d \n" , TxBuffer[c] );
66
  // }
130 ligi 67
  /* int i=0;
68
  while(TxBuffer[i] !='\r' && i<150)
127 ligi 69
    {
70
    //     TxBuffer[i]='#';
130 ligi 71
    status = send(mk_socket,TxBuffer[i] , 1, 0);
127 ligi 72
    printf(" +%d%c ",i,TxBuffer[i]);
73
    i++;
74
    }
75
 
130 ligi 76
  //  status = send(s,"\r" , 1, 0);
127 ligi 77
  */
130 ligi 78
   status = send(mk_socket,"\r" , 1, 0);
79
   status = send(mk_socket,"\n" , 1, 0);
127 ligi 80
  printf("\n");
81
}
82
 
83
 
130 ligi 84
int rx_last_length;
85
 
86
int read_from_mk()
87
{      
88
  char in_char='#';
89
  int count=0;
90
  int r=0;
91
 
92
  printf("starting read\n");
93
  while(in_char!='\n')
94
    {
95
      //      printf("b read\n");
96
      count=read(mk_socket,&in_char,1);
97
      //printf("a read %d\n",count);
98
      if (count!=-1)
99
        {
100
          //  printf("%c\n",in_char);
101
          RxBuffer[r]=in_char;
102
 
103
          if (in_char!=0)
104
            PrintableRxBuffer[r++]=in_char;
105
          else
106
            PrintableRxBuffer[r++]='0';
107
        }
108
 
109
    }
110
  rx_last_length=r;
111
  PrintableRxBuffer[r++]='\0'; // terminate 
112
  printf("done --->%s\n",PrintableRxBuffer);
113
 
114
  if (RxBuffer[2]=='D')
115
    debug_sets++;
116
 
117
  return 1;
118
}
119
 
120
 
127 ligi 121
int connect_mk_bluetooth(char dest[18])
122
{
123
 
124
  struct sockaddr_rc addr ;
125
 
126
  // allocate a socket
127
  mk_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
128
 
129
  // set the connection parameters (who to connect to)
130
  addr.rc_family = AF_BLUETOOTH;
131
  addr.rc_channel = 1;
132
  str2ba( dest, &addr.rc_bdaddr );
133
 
134
  // connect to server
135
  status = connect(mk_socket, (struct sockaddr *)&addr, sizeof(addr));
136
 
130 ligi 137
  printf("connection status %d\n",status);
127 ligi 138
  return status;
139
 
140
}
141
 
142
 
143
 
130 ligi 144
 
127 ligi 145
int connect_mk_localhost_socket(int port)
146
{
147
 
148
  int status;
149
  struct sockaddr_in sa;
150
 
151
  sa.sin_family = AF_INET;
152
  sa.sin_addr.s_addr = htonl(0x0);
153
  sa.sin_port = htons(port);
154
 
155
  // allocate a socket
156
  //  s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
157
  mk_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
158
  //);
159
 
160
  // set the connection parameters (who to connect to)
161
 
162
  // connect to server
163
  //  status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
164
  status = connect(mk_socket,(struct sockaddr*) &sa, sizeof(struct sockaddr_in));
165
 
130 ligi 166
  printf("connection status %d\n",status);
127 ligi 167
  return status;
168
 
169
}
170