/// Loads configuration from a serialized and signed Protobuf /// Configuration message. /// Unauthenticated. void PluginAPI::configure(const std::string &config_str_hex) { try { const std::string config_str = utils::hex_decode(config_str_hex); const uint8_t *config_buf = (const uint8_t *)config_str.c_str(); const size_t config_len = config_str.size(); bool verified = false; if (config_len >= utils::SIGNATURE_LENGTH) verified = utils::signature_verify(config_buf, config_buf + utils::SIGNATURE_LENGTH, config_len - utils::SIGNATURE_LENGTH); if (!verified) { FBLOG_ERROR("configure()", "Signature verification failed"); throw ConfigurationError("Signature verification failed"); } Configuration config; config.ParseFromArray(config_buf + utils::SIGNATURE_LENGTH, config_len - utils::SIGNATURE_LENGTH); acquire_plugin()->configure(config); } catch (const std::exception &e) { FBLOG_ERROR("configure()", "Exception caught"); FBLOG_ERROR("configure()", e.what()); throw FB::script_error(e.what()); } }
/// Closes the device. void PluginAPI::close(const FB::JSAPIPtr &device, const FB::JSObjectPtr &callbacks) { try { FBLOG_INFO("close()", "Closing device"); acquire_plugin() ->communicator(boost::static_pointer_cast<DeviceAPI>(device)->device) ->close(callbacks); } catch (const std::exception &e) { FBLOG_ERROR("close()", "Exception caught"); FBLOG_ERROR("close()", e.what()); callbacks->InvokeAsync("error", FB::variant_list_of(e.what())); } }
/// Calls the device. void PluginAPI::call(const FB::JSAPIPtr &device, bool use_timeout, const std::string &type_name, const FB::VariantMap &message_map, const FB::JSObjectPtr &callbacks) { try { FBLOG_INFO("call()", "Calling device"); acquire_plugin() ->communicator(boost::static_pointer_cast<DeviceAPI>(device)->device) ->call(use_timeout, type_name, message_map, callbacks); } catch (const std::exception &e) { FBLOG_ERROR("call()", "Exception caught"); FBLOG_ERROR("call()", e.what()); callbacks->InvokeAsync("error", FB::variant_list_of(e.what())); } }
/// Lists allowed device objects. /// Authentication required. /// Returns array of DeviceAPI. std::vector<FB::JSAPIPtr> PluginAPI::devices() { if (!acquire_plugin()->authenticate()) { FBLOG_ERROR("get_devices()", "URL not allowed"); throw ConfigurationError("URL not allowed"); } try { std::vector<DeviceDescriptor> devices = acquire_plugin()->enumerate(); std::vector<FB::JSAPIPtr> result; for (size_t i = 0; i < devices.size(); i++) result.push_back(boost::make_shared<DeviceAPI>(devices[i])); return result; } catch (const std::exception &e) { FBLOG_ERROR("get_devices()", "Exception caught"); FBLOG_ERROR("get_devices()", e.what()); throw FB::script_error(e.what()); } }
/// BIP32 PubKey derivation FB::VariantMap PluginAPI::derive_child_node(const FB::VariantMap &node_map, unsigned int index) { try { std::auto_ptr<PB::Message> node_msg; HDNode node; node_msg = create_message("HDNodeType"); message_from_map(*node_msg, node_map); message_to_hdnode(*node_msg, node); hdnode_public_ckd(&node, index); message_from_hdnode(*node_msg, node); return message_to_map(*node_msg); } catch (const std::exception &e) { FBLOG_ERROR("hdnode_public_ckd()", "Exception caught"); FBLOG_ERROR("hdnode_public_ckd()", e.what()); throw FB::script_error(e.what()); } }
int WebEmberLinker::link(const char* prefix) { assert(!mModuleHandle); std::string libdir(prefix); #ifdef _WIN32 if(!libdir.empty()) { #if defined(_DEBUG) && defined(SEPARATE_BUILDS) //In msvc I prefer to separate release/debug builds to prevent mixing the runtime. //Set SEPARATE_BUILDS to enable this feature. libdir += "\\bin_d"; #else libdir += "\\bin"; #endif SetDllDirectoryA(libdir.c_str()); SetCurrentDirectoryA(libdir.c_str()); libdir += "\\"; } //normally the browsers are disabling verbose popup messages from the OS on errors, //but to get more info on what is the missing DLL when linking recursively, we need to enable this. UINT browserErrorMode = GetErrorMode(); SetErrorMode(0); #else if (!libdir.empty()) { libdir += "/lib/"; } #endif std::string libfile(libdir + sLibName); FBLOG_INFO("WebEmberLinker::runEmber", "Loading library: " << libfile); //Loads the specified module into the address space of the calling process. mModuleHandle = LoadLib(libfile.c_str()); if (mModuleHandle == NULL) { #ifndef _WIN32 FBLOG_ERROR("WebEmberLinker::runEmber", "dlerror: " << dlerror()); #endif FBLOG_ERROR("WebEmberLinker::runEmber", "Unable to load " << libfile); return 1; } //Retrieves the address of an exported function or variable from the specified DLL. pStartWebEmber = (funcTypeStart)GetFunction(mModuleHandle, sFuncNameStart); if (pStartWebEmber == NULL) { #ifndef _WIN32 FBLOG_ERROR("WebEmberLinker::runEmber", "dlerror: " << dlerror()); #endif FBLOG_ERROR("WebEmberLinker::runEmber", "Unable to load function " << sFuncNameStart << " in " << libfile); UnloadLib(mModuleHandle); return 2; } //same as above pQuitWebEmber = (funcTypeQuit)GetFunction(mModuleHandle, sFuncNameQuit); if (pQuitWebEmber == NULL) { #ifndef _WIN32 FBLOG_ERROR("WebEmberLinker::runEmber", "dlerror: " << dlerror()); #endif FBLOG_ERROR("WebEmberLinker::runEmber", "Unable to load function " << sFuncNameQuit << " in " << libfile); UnloadLib(mModuleHandle); return 3; } #ifdef _WIN32 //SetErrorMode of the process back to the original. SetErrorMode(browserErrorMode); #endif return 0; }