Jump to content

Help on bluetooth and threading

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts
Hello
i have a problem so i thought maybe you can help me :)
I'm working on a project which is in c, its a bluetooth server that sends and receives a string type message. its using BlueZ library ( compile against -lbluetooth). so there's two functions, bt_send() and bt_recv(), there's also another function called logger(), that creates a log file for messages that are being sent and received.
i already have coded this bluetooth server and its functioning.
i wanted to add a GUI for this , so i got a free web server from this web site wwwdotpaulgriffithsdotnet; The Internet Home of Paul Griffiths) and modified so i could use my own bluetooth server with it.
the modification includes, that from the HTML page that is using a <FORM> element and GET method to send a string to the web server:
GET /send.htm?Text=START HTTP/1.1
the "START" part can differ, it could be START or STOP. but the buffer always looks like this. when the web server receives this buffer, i managed to modified so it can now analyse the buffer and parse the "START" part from the buffer and send a bluetooth message using bt_send() accordingly.
everything on this point is working flawlessly. i compile the code like this:
gcc -http.c server.c -o sim -lbluetooth
http.c so the webserver and server.c for bluetooth sever functionality.
o run it in the terminal as: ./sim
and the terminal goes to the process of hosting the web pages; i simply open my browser and write this in the address bar:
localhost:8080/index.html
when i click on the "send" button the http server prints the command (START or STOP) and the message is sent to the other device ( using the defined MAC address) via my bluetooth server.
now my problem and what i want to achieve is to have the bluetooth receive part at same time of web server hosting pages, i think i need to use threads(pthread) , I've never worked with threads.
that means two process are running at the same time;
one for web server (http.c) for hosting web pages,
and one for bluetooth server receive function to receive incoming messages at any time (bt_recv() in server.c).
the bt_recv() function in server.c is an endless while loop that is always creating a bluetooth socket and makes it listen on a port for any incoming message.
the output that i have in my head is like this:
i run the output file like:
./btserver
i go to the url and i can send perfectly and the http server prints the message and logs it,
and if any device like another computer sends a message from a bluetooth function (like my bt_send() ) , the server would run the bt_recv() function as a thread, receives the message, print the message and logs it.

i would really appriciate your help, and thanking you in advance,
i could send you my source code but how would i?
thanks
Ali.B

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Attach your source code. Click "Go Advanced" at the bottom, then click "Manage Attachments" somewhere around there... For reasons beyond my understanding you can't attach .h files. Just change the extension to .txt or something. So I take it you need help with the pthread library? Here's a simple example:

Linux Tutorial: POSIX Threads

What operating system is this for, anyway?

POSIX pthread Reference

Edited by dargueta, 10 August 2009 - 04:24 PM.
Added link

sudo rm -rf /

#3
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts
im using linux - ubuntu
compiler : gcc

i will attach my source code. .c files are as they are... .h files are as .txt files

i've read those tutorials but no use, it kinda too late to start from begining. i need to submit these project very soon, and I've already done more than minimum requirements, i just wanna do this for learning and getting a better grade, so something like an example or sample code would be really nice,
thanks

Attached Files



#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
So where exactly do you need the pthreads? I see you use fork() just fine.
sudo rm -rf /

#5
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts

dargueta said:

So where exactly do you need the pthreads? I see you use fork() just fine.

fork() , but where would you use it?

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
It's in your code already. :)
sudo rm -rf /

#7
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts

dargueta said:

It's in your code already. :)

could u paste a piece of code and show me how to call the bt_recv() function in a fork?
the fork is used in the http server, but how would the bluetooth server be running on a fork?

Edited by alibahaloo, 11 August 2009 - 04:43 AM.


#8
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts
anyone?

#9
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts
..........?

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Dude, be patient. I'll get on it today after work.
sudo rm -rf /

#11
alibahaloo

alibahaloo

    Newbie

  • Members
  • Pip
  • 7 posts
thanks :)

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Um...it looks like you're using _beginthread() in the right place already. I'm pretty sure that's a non-standard Microsoft extension, but you've got the right idea. However, you've got a problem with your current code: you need to keep track of what threads/processes (depends on whether you use fork() or pthreads) are still running and which ones aren't; otherwise you could be leaving zombies if you exit before they're done. I've written an HTTP server before, but I'm not entirely sure in this case whether you'd want to use fork() or a pthread, seeing as you're not really doing much complicated processing. There are advantages and disadvantages to each:

FORKS
Pros: If one client goes haywire, you won't take out the entire server, and other clients can still connect and get their data. You also don't have to deal with concurrency, as all data is copied.
Cons: Lots of overhead in forking. All data is duplicated, as are all open file handles, pipes, etc. You need to close the ones you don't use, or you could eat up system resources really quickly. Communication between forked children is possible, but a chore.

THREADS
Pros: Faster, gets the same job done as forking. Communication between threads is relatively easy, as they share all the same data.
Cons: You need to be careful with concurrency. Data and file handles are not duplicated, so you need to make sure that your threads play nice and share properly, or else you'll get corrupted data and very unhappy clients.

Either way, you need to keep track of your child processes/threads. Your current implementation isn't really geared for that.

Edited by dargueta, 12 August 2009 - 06:11 PM.
Reformatted for easier reading

sudo rm -rf /