bluetooth/bluetooth.c
author Markus Bröker<broeker.markus@googlemail.com>
Sat, 21 Oct 2017 13:45:05 +0200
changeset 171 c6e0af68825a
parent 165 f551b78c3eee
permissions -rw-r--r--
Entrypoint and RET fixed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
165
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * bluetooth.c
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2010 Markus Bröker
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <unistd.h>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <sys/socket.h>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <bluetooth/bluetooth.h>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <bluetooth/rfcomm.h>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
int main (int argc, char **argv)
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
{
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    0};
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    char buf[1024] = { 0 };
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    int s, client, bytes_read;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    socklen_t opt = sizeof (rem_addr);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    // allocate socket
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    s = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    // bind socket to port 1 of the first available
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    // local bluetooth adapter
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    loc_addr.rc_family = AF_BLUETOOTH;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    loc_addr.rc_bdaddr = *BDADDR_ANY;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    loc_addr.rc_channel = (uint8_t) 1;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    bind (s, (struct sockaddr *)&loc_addr, sizeof (loc_addr));
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    // put socket into listening mode
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    listen (s, 1);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    // accept one connection
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    client = accept (s, (struct sockaddr *)&rem_addr, &opt);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    ba2str (&rem_addr.rc_bdaddr, buf);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    fprintf (stderr, "accepted connection from %s\n", buf);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    memset (buf, 0, sizeof (buf));
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    // read data from the client
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    bytes_read = read (client, buf, sizeof (buf));
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    if (bytes_read > 0) {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
        printf ("received [%s]\n", buf);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    }
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    // close connection
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    close (client);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    close (s);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    return EXIT_SUCCESS;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
}