语言

Menu
Sites
Language
Wait bt_socket_connection_state_changed_cb complete.

I want to use client_socket_fd.

 

way1

main thread function{ //caller call this function

res = bt_socket_set_connection_state_changed_cb(socket_connection_state_changed, NULL);

res = bt_socket_connect_rfcomm(pServerID, service_uuid);

res = bt_socket_send_data(client_socket_fd, hash, len); //I'll get client_socket_fd in socket_connection_state_changed. NOT work.

... some process

return process reuslt;

}

In this routine, bt_socket_send_data use bad client_socket_fd because socket_connection_state_changed is not called yet.

 

way2

main thread function{  //caller call this function

res = bt_socket_set_connection_state_changed_cb(socket_connection_state_changed, NULL);

res = bt_socket_connect_rfcomm(pServerID, service_uuid);

}

other thread function{

while(client_socket_fd==0){}//busy wait for checking client_socket_fd

res = bt_socket_send_data(client_socket_fd, hash, len);  //called after socket_connection_state_changed is called.

... some process

return process result;  //impossible returning to caller

}

This work well but can't return value to caller.

 

way3

I want to wait client_socket_fd in main thread like below.

main thread function{ //caller call this function

res = bt_socket_set_connection_state_changed_cb(socket_connection_state_changed, NULL);

res = bt_socket_connect_rfcomm(pServerID, service_uuid);

while(client_socket_fd==0){} <- block

res = bt_socket_send_data(client_socket_fd, hash, len);

... some process

return process result;

}

Actually I have to return value in main thread function after getting socket like way3.  But while() is blocking and socket_connection_state_changed is not called continuously.

 It's seems for socket_connection_state_changed to be called in main thread too.   In way2, I can't return client_socket_fd and result of some process to function caller.

How can I wait client_socket_fd(socket_connection_state_changed call complete) in main thread function?  Or can I call callback(socket_connection_state_changed) in other thread?

编辑者为: An 07 11月, 2016

响应

7 回复
Eugene Kurzberg

You should not block the function in which you called bt_socket_connect_rfcomm. Instead you should return from this function and send your data in your  socket_connection_state_changed callback. This might seem like a not very nice solution but this API is using and asynchronous non-blocking I/O model which is less convenient but more efficient than synchronous model with several threads.

An

I made new thread for bt_socket_connect_rfcomm.  In the new thread, bt_socket_connect_rfcomm is called and return. And  main thread is waiting for callback is called like above.  But callback is not called.  I returned in the function that call bt_socket_connect_rfcomm.   Can you explain why this happen?

 

 

Eugene Kurzberg

You should call bt_socket_connect_rfcomm in the main thread. All callbacks in Tizen are delivered via main loop started by ui_app_main (or similar, depending on applicaiton type). It is possible that callback is always called in the same thread in which you called  bt_socket_connect_rfcomm. And since you don't have an event loop in your new thread the callback is never called.

An

Thank you for your answer.  I tried one more. 

 

In main thread, I make a thread(thread A) and call pthread_join()[wait thread A end].

In thread A, I make thread B and waiting callback..

In thread B, bt_socket_connect_rfcomm is called and return.  And it work as my intend. 

 

Thread A wait some time, pass and can use assigned socket well and after process, return.  Main thread waiting with pthread_join() is passed and return.

 

However, I have question.

According to your answer, first, Main thread is blocked with pthread_join(). Second, Thread B call bt_socket_connect_rfcomm is dead.  But callback is called and Thread A can use socket.

 

Why it is possible?

Eugene Kurzberg

How are you certain that callback is called in Thread A? Anyway I don't think that this API was intended to be used like this. And since bt_socket_connect_rfcomm function is non-blocking I don't see why do you want to call it in the separate thread.

In any case I don't know the details of implementation of this API, however I am familiar with a project that was using it in a single-threaded environment and it worked fine. If your are interested in implementation details you can visit this repository and maybe even try to contact some of its developers.

An

I didn't certain that callback is called in Thread A in any my sentence.

Anyway bt_socket_connect_rfcomm is non-blocking right.  But to use assigned socket,  after call bt_socket_connect_rfcomm, I have to wait until socket_connection_state_changed callback is called.  But waiting is blocking main thread so socket_connection_state_changed callback can't be called.  I made a thread for 'You should not block the function in which you called bt_socket_connect_rfcomm'.   Because I have to support api for and in api, bt connection occured and have to return value like post.  Thank you.

 

GEUNSOO KIM

I wonder why you should wait for callback? callback means the process is asynchronous and not to mean you wait there.

If you just want to serialize follow-actions of the callback, use event. you can add custom event and event handler.

(check ecore_event_handler_add() on API document)

Then every moment which meets the condition you want to take care of, post an event then handle it on the event handler.