0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[開発日記]c++のlibdbusが難しい

Posted at

前回の記事の通りにC++からBLE接続をするプログラム実装をしようとしているが、libdbus周りがなかなか難しく思うように進まない。
完成するまで待っていたらいつまで経っても記事の更新ができないので、スタックしている内容を書くことにした。

現在はDBusから非同期でBLE接続リクエストを送るところでつまづいている

  • libdbusnにはdbus_connection_send_with_replyという関数があり、非同期で送信したいmessageをキューに入れることができる。
  • messageのreplyはDBusPendingCallと呼ばれるオブジェクトを介して取得できる。
  • dbus_connection_send_with_replyを実行するとDBusPendingCallが戻ってくるので、これをdbus_pending_call_set_notifyという関数を使ってコールバック関数に紐づけることができる
  • その後、dbus_connection_read_write_dispatch関数を呼ばれるとキューに入っているMessageを送信する

今messageにBLE機器への接続リクエストを入れて非同期で送ろうとしたのだが、コールバックが呼ばれる気配が一向にない。

// Created connection msg before
DBusPendingCall* pending = nullptr;
if (!dbus_connection_send_with_reply(mConn.get(), msg, &pending, -1))
{
  std::cerr << "Failed to send connection message" << std::endl;
  dbus_message_unref(msg);return;
}
if (!pending)
{
  std::cerr << "Pending call is null" << std::endl;dbus_message_unref(msg);return;
}
else
{
  std::cout << "Connection request sent for " << handler.first->getMacAddr() << std::endl;
  handler.first->setState(BleDeviceState::CONNECTING);
}

dbus_pending_call_set_notify(pending, &BleDbusConnectionManager::connectionCb, new std::shared_ptr<BleDeviceHandler>(handler.first),
 [](void* data) { delete static_cast<std::shared_ptr<BleDeviceHandler>*>(data); });
dbus_message_unref(msg);

dbus_connection_read_write_dispatch(mConn.get(), -1);

同期リクエストだとBLE接続できたのでこのコードのどこかが悪いのは間違いない。
このコードスニペットは定期的に実行される関数内で呼ばれているのでdbus_connection_read_write_dispatchも同じ場所で呼べばいいやと思っていたが、もしかしたらちゃんと別スレッドで実行させないといけなかったりするのだろうか?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?