Bowls Eye  1.0
main.c
1 /*
2  ============================================================================
3  Name : main.c
4  Author :
5  Version :
6  Copyright : Your copyright notice
7  Description : Hello World in C, Ansi-style
8  ============================================================================
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "bluetoothController.h"
14 
15 int main(int argc, char **argv)
16 {
17  struct sockaddr_rc locAddr = { 0 }, remAddr = { 0 };
18  char buf[1024] = { 0 };
19  int sRec, sSend, client, bytesRead, status;
20  socklen_t opt = sizeof(remAddr);
21  double receivedData[1024] = {0.0};
22 
23  struct sockaddr_rc destAddr = {0};
24  char dest[18] = "50:9E:A7:77:0C:2A"; //address of pi
25 
26  // allocate sockets
27  sRec = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
28  sSend = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
29 
30 
31  // bind socket to port 1 of the first available
32  // local bluetooth adapter
33  locAddr.rc_family = AF_BLUETOOTH;
34  locAddr.rc_bdaddr = *BDADDR_ANY;
35  locAddr.rc_channel = (uint8_t)1;
36  bind(sRec, (struct sockaddr *)&locAddr, sizeof(locAddr));
37 
38  // put socket into listening mode
39  listen(sRec, 1);
40 
41  // accept one connection
42  client = accept(sRec, (struct sockaddr *)&remAddr, &opt);
43 
44  ba2str(&remAddr.rc_bdaddr, buf);
45  fprintf(stderr, "accepted connection from %s\n", buf);
46  memset(buf, 0, sizeof(buf));
47 
48 
49 
50  destAddr.rc_family = AF_BLUETOOTH;
51  destAddr.rc_channel = (uint8_t)1;
52  str2ba(dest, &destAddr.rc_bdaddr);
53 
54  status = connect(sSend, (struct sockaddr *)&destAddr, sizeof(destAddr));
55 
56  double send[1] = {3.1456789654123554};
57 
58  for(;;){
59 
60  bytesRead = read(client, receivedData, sizeof(receivedData));
61  if( bytesRead > 0 ) {
62  for(uint8_t i=0; i<bytesRead/8; i++){
63  printf("received [%f]\t", receivedData[i]);
64  }
65  printf("\n");
66  }
67  scanf("%lf", send);
68 
69  write(sSend, send, sizeof(send));
70  }
71 
72  // close connection
73  close(client);
74  close(sRec);
75  close(sSend);
76  return 0;
77 }