1 // Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) 2 // This Source Code Form is subject to the terms of the Mozilla Public 3 // License, v. 2.0. If a copy of the MPL was not distributed with this 4 // file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 #include <vsomeip/vsomeip.hpp> 6 7 #if defined ANDROID || defined __ANDROID__ 8 #include "android/log.h" 9 #define LOG_TAG "hello_world_client" 10 #define LOG_INF(...) fprintf(stdout, __VA_ARGS__), fprintf(stdout, "\n"), (void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, ##__VA_ARGS__) 11 #define LOG_ERR(...) fprintf(stderr, __VA_ARGS__), fprintf(stderr, "\n"), (void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ##__VA_ARGS__) 12 #else 13 #include <cstdio> 14 #define LOG_INF(...) fprintf(stdout, __VA_ARGS__), fprintf(stdout, "\n") 15 #define LOG_ERR(...) fprintf(stderr, __VA_ARGS__), fprintf(stderr, "\n") 16 #endif 17 18 static vsomeip::service_t service_id = 0x1111; 19 static vsomeip::instance_t service_instance_id = 0x2222; 20 static vsomeip::method_t service_method_id = 0x3333; 21 22 class hello_world_client { 23 public: 24 // Get the vSomeIP runtime and 25 // create a application via the runtime, we could pass the application name 26 // here otherwise the name supplied via the VSOMEIP_APPLICATION_NAME 27 // environment variable is used hello_world_client()28 hello_world_client() : 29 rtm_(vsomeip::runtime::get()), 30 app_(rtm_->create_application()) 31 { 32 } 33 init()34 bool init(){ 35 // init the application 36 if (!app_->init()) { 37 LOG_ERR ("Couldn't initialize application"); 38 return false; 39 } 40 41 // register a state handler to get called back after registration at the 42 // runtime was successful 43 app_->register_state_handler( 44 std::bind(&hello_world_client::on_state_cbk, this, 45 std::placeholders::_1)); 46 47 // register a callback for responses from the service 48 app_->register_message_handler(vsomeip::ANY_SERVICE, 49 service_instance_id, vsomeip::ANY_METHOD, 50 std::bind(&hello_world_client::on_message_cbk, this, 51 std::placeholders::_1)); 52 53 // register a callback which is called as soon as the service is available 54 app_->register_availability_handler(service_id, service_instance_id, 55 std::bind(&hello_world_client::on_availability_cbk, this, 56 std::placeholders::_1, std::placeholders::_2, 57 std::placeholders::_3)); 58 return true; 59 } 60 start()61 void start() 62 { 63 // start the application and wait for the on_event callback to be called 64 // this method only returns when app_->stop() is called 65 app_->start(); 66 } 67 on_state_cbk(vsomeip::state_type_e _state)68 void on_state_cbk(vsomeip::state_type_e _state) 69 { 70 if(_state == vsomeip::state_type_e::ST_REGISTERED) 71 { 72 // we are registered at the runtime now we can request the service 73 // and wait for the on_availability callback to be called 74 app_->request_service(service_id, service_instance_id); 75 } 76 } 77 on_availability_cbk(vsomeip::service_t _service,vsomeip::instance_t _instance,bool _is_available)78 void on_availability_cbk(vsomeip::service_t _service, 79 vsomeip::instance_t _instance, bool _is_available) 80 { 81 // Check if the available service is the the hello world service 82 if(service_id == _service && service_instance_id == _instance 83 && _is_available) 84 { 85 // The service is available then we send the request 86 // Create a new request 87 std::shared_ptr<vsomeip::message> rq = rtm_->create_request(); 88 // Set the hello world service as target of the request 89 rq->set_service(service_id); 90 rq->set_instance(service_instance_id); 91 rq->set_method(service_method_id); 92 93 // Create a payload which will be sent to the service 94 std::shared_ptr<vsomeip::payload> pl = rtm_->create_payload(); 95 std::string str("World"); 96 std::vector<vsomeip::byte_t> pl_data(std::begin(str), std::end(str)); 97 98 pl->set_data(pl_data); 99 rq->set_payload(pl); 100 // Send the request to the service. Response will be delivered to the 101 // registered message handler 102 LOG_INF("Sending: %s", str.c_str()); 103 app_->send(rq); 104 } 105 } 106 on_message_cbk(const std::shared_ptr<vsomeip::message> & _response)107 void on_message_cbk(const std::shared_ptr<vsomeip::message> &_response) 108 { 109 if(service_id == _response->get_service() 110 && service_instance_id == _response->get_instance() 111 && vsomeip::message_type_e::MT_RESPONSE 112 == _response->get_message_type() 113 && vsomeip::return_code_e::E_OK == _response->get_return_code()) 114 { 115 // Get the payload and print it 116 std::shared_ptr<vsomeip::payload> pl = _response->get_payload(); 117 std::string resp = std::string( 118 reinterpret_cast<const char*>(pl->get_data()), 0, 119 pl->get_length()); 120 LOG_INF("Received: %s", resp.c_str()); 121 stop(); 122 } 123 } 124 stop()125 void stop() 126 { 127 // unregister the state handler 128 app_->unregister_state_handler(); 129 // unregister the message handler 130 app_->unregister_message_handler(vsomeip::ANY_SERVICE, 131 service_instance_id, vsomeip::ANY_METHOD); 132 // alternatively unregister all registered handlers at once 133 app_->clear_all_handler(); 134 // release the service 135 app_->release_service(service_id, service_instance_id); 136 // shutdown the application 137 app_->stop(); 138 } 139 140 private: 141 std::shared_ptr<vsomeip::runtime> rtm_; 142 std::shared_ptr<vsomeip::application> app_; 143 }; 144