Bowls Eye  1.0
bluetoothController.c
Go to the documentation of this file.
1 
8 #include "bluetoothController.h"
9 
10 char PC_ADDRESS_[18] = "34:E6:AD:06:21:46";
11 
17 
18  server->opt = sizeof(server->remAddr);
19  server->sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
20 
21  server->locAddr.rc_family = AF_BLUETOOTH;
22  server->locAddr.rc_bdaddr = *BDADDR_ANY;
23  server->locAddr.rc_channel = (uint8_t)1;
24 
25  bind(server->sock, (struct sockaddr *)&(server->locAddr), sizeof(server->locAddr));
26 
27  for(int i=0; i<1024; i++){
28 
29  server->recData[i] = 0;
30  }
31 
32  BluetoothServer_enable(server);
33 }
34 
40 
41  listen(server->sock, 1);
42  server->client = accept(server->sock, (struct sockaddr *)&(server->remAddr), &server->opt);
43 
44  ba2str(&server->remAddr.rc_bdaddr, server->buf);
45  fprintf(stderr, "accepted connection from %s\n", server->buf);
46  memset(server->buf, 0, sizeof(server->buf));
47 }
48 
54 
55  close(server->client);
56  close(server->sock);
57 }
58 
65 
66  int bytesRead = read(server->client, server->recData, sizeof(server->recData));
67  if(bytesRead > 0){
68 
69  for(uint16_t i=0; i<bytesRead; i++){
70 
71  printf("received [%i]\t", (int)(server->recData[i]));
72  }
73 
74  printf("\n");
75  *dst = (int)(server->recData[0]);
76  }
77 }
78 
85 
86  int bytesRead = read(server->client, server->recData, sizeof(server->recData));
87  if(bytesRead > 0){
88 
89  printf("received [%s]", (server->recData));
90 
91  printf("\n");
92  *dst = (char)(server->recData[0]);
93  }
94 }
95 
101 
102  client->dst = PC_ADDRESS_;
103  client->sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
104 
105  client->addr.rc_family = AF_BLUETOOTH;
106  client->addr.rc_channel = (uint8_t)1;
107  str2ba(client->dst, &(client->addr.rc_bdaddr));
108 
109  BluetoothClient_enable(client);
110 }
111 
117 
118  client->status = connect(client->sock, (struct sockaddr *)&(client->addr), sizeof(client->addr));
120 }
121 
127 
128  close(client->sock);
129 }
130 
137 void BluetoothClient_sendString(BluetoothClient_t *client, char *data, int size){
138 
139  client->status = write(client->sock, data, size);
141 }
142 
148 
149  if(client->status < 0){
150  perror(":(\t");
151  }
152 }
153 
154 
155 
156 
157 
158 
159 
160 
void BluetoothClient_enable(BluetoothClient_t *client)
struct sockaddr_rc remAddr
void BluetoothServer_disable(BluetoothServer_t *server)
void BluetoothClient_checkError(BluetoothClient_t *client)
void BluetoothClient_sendString(BluetoothClient_t *client, char *data, int size)
void BluetoothServer_readInt(BluetoothServer_t *server, int *dst)
void BluetoothServer_construct(BluetoothServer_t *server)
Structure for creating a Bluetooth server.
struct sockaddr_rc addr
Structure for creating a Bluetooth client.
void BluetoothServer_readChar(BluetoothServer_t *server, char *dst)
struct sockaddr_rc locAddr
void BluetoothServer_enable(BluetoothServer_t *server)
void BluetoothClient_construct(BluetoothClient_t *client)
void BluetoothClient_disable(BluetoothClient_t *client)