thingsboard-client-sdk icon indicating copy to clipboard operation
thingsboard-client-sdk copied to clipboard

RPC_Subscribe invoking the singular function and not the vector

Open altoriot opened this issue 2 years ago • 4 comments

Hi. Ive pulled the latest thingsboard.h into my project and after looking at the example .INO's, implemented the necessary code changes to line it up. but I am not getting the project to compile.

const std::vector<RPC_Callback> callbacks = { {"RebootRequest" , RPC_RebootRequest }, {"RPCButtonPress" , RPC_ButtonPress }, {"getRelayState1" , RPC_GetRelay1 }, {"setRelayState1" , RPC_SetRelay1 }, {"getRelayState2" , RPC_GetRelay2 }, {"setRelayState2" , RPC_SetRelay2 } } this is my RPC list now as expected.

main code, ommiting usual if checks n balances ` // reason for pointer is client can be wifi, wifi SSL, eth or eth SSL via SSLClinet lib setup(): Client* tbClient; ThingsBoard *tb; tb = new ThingsBoard(*tbClient); tb->connect(etc, etc,etc);

loop():

if(tb->connected()){ if(!tb->RPC_Subscribe(callbacks)){ log_i("failed RPC sub"); } ` problem im having is the lib has the RPC_Subscribe as a overloaded function, where the INO uses this very method to subscribe for the RPC, which has 2 rpc calls in it. error: src/main.cpp:165:55: error: no matching function for call to 'ThingsBoardSized<>::RPC_Subscribe(const std::vector<RPC_Callback>&)'

ctrl-click always referes to the singular function call, where the multiple wants a RPC_Subscribe(const InputIterator& first_itr, const InputIterator& last_itr)

so whats going on here? shouldnt the vector have an itterator already built in that can be itterated over to load the m_rpcCallbacks which is itself already a vector of type RPC_callback? why the first, last iter when you are passing it a vector?

anyone with same experience? Thanks in advance

altoriot avatar Aug 30 '22 09:08 altoriot

the error is caused by the call to RPC_Subscribe:

use RPC_Subscribe(callbacks.begin(),callbacks.end())

JfloresIoTF avatar Sep 05 '22 15:09 JfloresIoTF

Yeah sorry my bad, the method can't be passed the vector directly you have to pass the cbegin()pointer and the cend() pointer to the method, this is done because theoretically if you know the size beforehand you could use a const std::array with a fixed size instead.

OekoSolveMG avatar Sep 09 '22 05:09 OekoSolveMG

I adjusted the example to reflect the current change of the library sorry for the inconvience.

OekoSolveMG avatar Sep 09 '22 07:09 OekoSolveMG

No worries. I need to take the lib and test it against my currently heavily modified lib from march, see how the new features work in my current project. thanks for the fix. will check it out

altoriot avatar Sep 12 '22 06:09 altoriot

@imbeacon I think this issue can be closed, the example has been update and compiles now.

MathewHDYT avatar Mar 13 '23 12:03 MathewHDYT

Hi , I am facing the same issue when compiling the code error is "no matching function for call to 'ThingsBoardSized<>::RPC_Subscribe(const RPC_Callback [1], const char*)". anyone can suggest the solution ? Thanks in advance

sushant5252 avatar Jul 25 '23 15:07 sushant5252

@sushant5252 It seems that how you call the RPC_Subscribe method is not correct, because this method expects 2 iterators to the start and end of an std::array or std::vector, when using ESP32 or ESP8266

See the RPC example, on how to call the method with an array of RPC_Callbacks that should be subscribed, when using ESP32 or ESP8266.


If you are using the Arduino Uno it expects the array as well as its size as the second argument.

See the RPC example, on how to call the method, when using the Arduino Uno.


If you want to subscribe only one RPC_Callback and not multiple at once, you can simply call it like this for both the ESP32 / ESP8266 and the Arduino Uno.

RPC_Callback callback(METHOD_NAME, callbackFunction);
tb.RPC_Subscribe(callback);

MathewHDYT avatar Jul 25 '23 15:07 MathewHDYT

@MathewHDYT Thanks for your reply, Actually I am using STM32Core in arduino IDE and selection Hardware STM32G070, when i am compiling then gettin this Error"no matching function for call to 'ThingsBoardSized<>::RPC_Subscribe(const RPC_Callback [1], const size_t&)" when checking calling function definition by ctrl+click always refer to RPC_Subscribe(const InputIterator& first_itr, const InputIterator& last_itr), But when i am compiling same code for Arduino Mega Its working fine.

sushant5252 avatar Jul 25 '23 17:07 sushant5252

@sushant5252 It seems you are currently using the RPC_Subscribe(const InputIterator& first_itr, const InputIterator& last_itr) method, which is only enabled if the C++ STL is confirmed to work.

This is done over the internal flag THINGSBOARD_ENABLE_STL, which consists of these checks.

__has_include(<string>) && __has_include(<functional>) && __has_include(<vector>) && __has_include(<iterator>)

If these conditions are not all fulfilled, which does not seem to be the case for your STM32Core, then the method you are using does not exist but is instead replaced with another method that does not rely on C++ STL functionality.

Which would be the method in the error message ThingsBoardSized<>::RPC_Subscribe(const RPC_Callback [1], const size_t&), which simply expects an array and the amount of elements in that array.


Either integrate the THINGSBOARD_ENABLE_STL in your code as well to call the method differently depending on for which hardware you are compiling or to test you could simply set #define THINGSBOARD_ENABLE_STL 1 and see if the program compiles. If it doesn't that clearly indicates that this board does not include the C++ STL base functionality and you therefore have to use the other method that I mentioned above.

MathewHDYT avatar Jul 25 '23 17:07 MathewHDYT