a bluetooth and a c++ demo
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Wed, 02 May 2012 20:51:14 +0200
changeset 165 f551b78c3eee
parent 164 e1f4bba1097a
child 166 ecd6492274ad
a bluetooth and a c++ demo committer: Markus Bröker <mbroeker@largo.homelinux.org>
bluetooth/Makefile
bluetooth/bluetooth-client.c
bluetooth/bluetooth.c
c++/dcast.cpp
c++/max.cpp
c++/over.cpp
c++/test.cpp
new file mode 100644
--- /dev/null
+++ b/bluetooth/Makefile
@@ -0,0 +1,55 @@
+     CC = gcc -g -ggdb $(PROF)
+    CPP = g++ -g -ggdb $(PROF)
+ CFLAGS = -Wall -O2 -Iinclude $(EXTRA)
+  EXTRA = -I/usr/local/include
+LDFLAGS = -L/usr/lib -L/usr/local/lib
+     RM = rm -f
+   FIND = find
+   MAKE = make
+ INDENT = indent
+ ERASER = eraser
+
+ifeq ("$(PROFILER)", "linux")
+   PROF = -fprofile-arcs -ftest-coverage -pg
+endif
+
+TARGET  = bluetooth
+TARGET += bluetooth-client
+
+.SUFFIXES: .c .cc .asm
+
+.c.o:
+	@echo Compiling $< ...
+	@$(CC) -c $(CFLAGS) -o $@ $<
+
+all: $(TARGET)
+
+bluetooth: bluetooth.o
+	@echo Linking $< ...
+	@$(CC) $(LDFLAGS) -lbluetooth -o $@ $<
+
+bluetooth-client: bluetooth-client.o
+	@echo Linking $< ...
+	@$(CC) $(LDFLAGS) -lbluetooth -o $@ $<
+
+.PHONY: beauty clean uninstall
+
+clean:
+ifdef FIND
+	$(FIND) . -name '*~' -exec $(RM) {} \;
+	$(FIND) . -name '*.[oa]' -exec $(RM) {} \;
+	$(FIND) . -name '*.gcov' -exec $(RM) {} \;
+	$(FIND) . -name '*.gcda' -exec $(RM) {} \;
+	$(FIND) . -name '*.gcno' -exec $(RM) {} \;
+	$(FIND) . -name 'gmon.out' -exec $(RM) {} \;
+endif
+	$(RM) $(TARGET)
+
+beauty:
+ifdef FIND
+	$(FIND) . -name '*.[ch]' -exec $(INDENT) {} \;
+	$(FIND) . -name '*.[ch]' -exec $(ERASER) {} \;
+	$(FIND) . -name '*.java' -exec $(ERASER) {} \;
+	$(FIND) . -name 'Makefile*' -exec $(ERASER) {} \;
+endif
+	@$(MAKE) clean
new file mode 100644
--- /dev/null
+++ b/bluetooth/bluetooth-client.c
@@ -0,0 +1,42 @@
+/**
+ * bluetooth.c
+ * Copyright (C) 2010 Markus Bröker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/rfcomm.h>
+
+int main (int argc, char **argv)
+{
+    struct sockaddr_rc addr = { 0 };
+    int s, status;
+    // Hard-coded for simplicity
+    char dest[18] = "58:17:0C:FF:36:D7";    // E10i
+
+    // allocate a socket
+    s = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+
+    // set the connection parameters (who to connect to)
+    addr.rc_family = AF_BLUETOOTH;
+    addr.rc_channel = (uint8_t) 1;
+    str2ba (dest, &addr.rc_bdaddr);
+
+    // connect to server
+    status = connect (s, (struct sockaddr *)&addr, sizeof (addr));
+
+    // send a message
+    if (status == 0) {
+        status = write (s, "hello!", 6);
+    }
+
+    if (status < 0)
+        perror ("uh oh");
+
+    close (s);
+
+    return EXIT_SUCCESS;
+}
new file mode 100644
--- /dev/null
+++ b/bluetooth/bluetooth.c
@@ -0,0 +1,51 @@
+/**
+ * bluetooth.c
+ * Copyright (C) 2010 Markus Bröker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/rfcomm.h>
+
+int main (int argc, char **argv)
+{
+    struct sockaddr_rc loc_addr = { 0 }, rem_addr = {
+    0};
+    char buf[1024] = { 0 };
+    int s, client, bytes_read;
+    socklen_t opt = sizeof (rem_addr);
+
+    // allocate socket
+    s = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+
+    // bind socket to port 1 of the first available
+    // local bluetooth adapter
+    loc_addr.rc_family = AF_BLUETOOTH;
+    loc_addr.rc_bdaddr = *BDADDR_ANY;
+    loc_addr.rc_channel = (uint8_t) 1;
+    bind (s, (struct sockaddr *)&loc_addr, sizeof (loc_addr));
+
+    // put socket into listening mode
+    listen (s, 1);
+
+    // accept one connection
+    client = accept (s, (struct sockaddr *)&rem_addr, &opt);
+
+    ba2str (&rem_addr.rc_bdaddr, buf);
+    fprintf (stderr, "accepted connection from %s\n", buf);
+    memset (buf, 0, sizeof (buf));
+
+    // read data from the client
+    bytes_read = read (client, buf, sizeof (buf));
+    if (bytes_read > 0) {
+        printf ("received [%s]\n", buf);
+    }
+    // close connection
+    close (client);
+    close (s);
+
+    return EXIT_SUCCESS;
+}
new file mode 100644
--- /dev/null
+++ b/c++/dcast.cpp
@@ -0,0 +1,38 @@
+#include <typeinfo>
+#include <iostream>
+
+using namespace std;
+
+class A {
+  public:
+    void f () {
+        cout << "Ich bin f() in der Klasse A\n";
+    }
+};
+
+class B:public A {
+  public:
+    void f () {
+        cout << "Ich bin f() in der Klasse B\n";
+    }
+};
+
+class C {
+  public:
+    void f () {
+        cout << "Ich bin auch ein f()\n";
+    }
+};
+
+int main ()
+{
+    A *ap = new A ();
+    B *bp = new B ();
+
+    // instanceof
+    C *cp = dynamic_cast < A * >(bp);
+    if (cp != NULL)
+        cp->f ();
+
+    return 0;
+}
new file mode 100644
--- /dev/null
+++ b/c++/max.cpp
@@ -0,0 +1,31 @@
+#include <iostream>
+#include <exception>
+
+using namespace std;
+
+template<class T>
+void maxx(T a, T b) {
+    if (a > b)
+        cout << b << endl;
+    else
+        cout << a << endl;
+}
+
+void maxx(int a, int b) {
+    cout << "handle ints special..." << endl;
+}
+
+int main(int argc, char **argv)
+{
+    double d1=3.2, d2=5.2;
+
+    try {
+        maxx(3.5, 5.1);
+        maxx(5, 1);
+        maxx(d1, d2);
+    } catch (exception &e) {
+        cout << e.what();
+    }
+
+    return 0;
+}
new file mode 100644
--- /dev/null
+++ b/c++/over.cpp
@@ -0,0 +1,36 @@
+#include <iostream>
+#include <cstdlib>
+
+template<class T>
+T add(T a, T b)
+{
+	return a+b;
+}
+
+int add (int a, int b)
+{
+	std::cout << "overloaded..." << std::endl;
+	return b+a;
+}
+
+int main(int argc, char **argv)
+{
+	float a, b;
+	int c, d;
+    std::string s1, s2;
+
+    a = 1.0;
+    b = 2.5;
+
+    c = 1;
+	d = 2;
+
+	s1 = "Hello ";
+	s2 = "World!";
+
+	std::cout << add (a, b) << std::endl;
+	std::cout << add (c, d) << std::endl;
+	std::cout << add (s1, s2) << std::endl;
+
+	return EXIT_SUCCESS;
+}
new file mode 100644
--- /dev/null
+++ b/c++/test.cpp
@@ -0,0 +1,49 @@
+#include <cstdio>
+#include <cstdlib>
+
+#define CASEMAX 5
+
+void inittab (int arr[][CASEMAX], int cols)
+{
+    int i, j;
+    for (i = 0; i < cols; i++) {
+        for (j = 0; j < cols; j++) {
+            arr[i][j] = 0;
+        }
+    }
+}
+
+void display (int arr[][CASEMAX], int cols)
+{
+
+    int i, j;
+
+    for (i = 0; i < cols; i++) {
+        printf ("+--");
+        for (j = 0; j < cols; j++)
+            printf ("-----");
+        printf ("--+\n");
+
+        for (j = 0; j < cols; j++) {
+            printf ("|%-4d", arr[i][j]);
+        }
+
+        printf ("|\n");
+    }
+
+    printf ("+--");
+    for (j = 0; j < cols; j++)
+        printf ("-----");
+    printf ("--+\n");
+}
+
+int main (int argc, char **argv)
+{
+    int cols = CASEMAX;
+    int arr[CASEMAX][CASEMAX];
+
+    inittab (arr, cols);
+    display (arr, cols);
+
+    return EXIT_SUCCESS;
+}