# HG changeset patch # User Markus Bröker # Date 1335984674 -7200 # Node ID f551b78c3eee2de8f0426ca6ca02ac5a523afe65 # Parent e1f4bba1097ad3d8033cc9d4b6927b0808908a5f a bluetooth and a c++ demo committer: Markus Bröker diff --git a/bluetooth/Makefile b/bluetooth/Makefile 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 diff --git a/bluetooth/bluetooth-client.c b/bluetooth/bluetooth-client.c new file mode 100644 --- /dev/null +++ b/bluetooth/bluetooth-client.c @@ -0,0 +1,42 @@ +/** + * bluetooth.c + * Copyright (C) 2010 Markus Bröker + */ + +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/bluetooth/bluetooth.c b/bluetooth/bluetooth.c new file mode 100644 --- /dev/null +++ b/bluetooth/bluetooth.c @@ -0,0 +1,51 @@ +/** + * bluetooth.c + * Copyright (C) 2010 Markus Bröker + */ + +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/c++/dcast.cpp b/c++/dcast.cpp new file mode 100644 --- /dev/null +++ b/c++/dcast.cpp @@ -0,0 +1,38 @@ +#include +#include + +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; +} diff --git a/c++/max.cpp b/c++/max.cpp new file mode 100644 --- /dev/null +++ b/c++/max.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +template +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; +} diff --git a/c++/over.cpp b/c++/over.cpp new file mode 100644 --- /dev/null +++ b/c++/over.cpp @@ -0,0 +1,36 @@ +#include +#include + +template +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; +} diff --git a/c++/test.cpp b/c++/test.cpp new file mode 100644 --- /dev/null +++ b/c++/test.cpp @@ -0,0 +1,49 @@ +#include +#include + +#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; +}