From b88cb51d9eabda739ab930cb3389dfc594537175 Mon Sep 17 00:00:00 2001 From: Anthony Payne Date: Mon, 18 Nov 2024 13:02:34 -0700 Subject: [PATCH] lib: someip-c: Re-wrote test service in C Re-wrote the test SOME/IP service code in C, rather than in C++. Tested against fluent-bit binary with the in-someip plugin enabled. Signed-off-by: Anthony Payne --- lib/libsomeip-c/example/CMakeLists.txt | 2 +- lib/libsomeip-c/example/test_service.c | 133 +++++++++++++++++++++++ lib/libsomeip-c/example/test_service.cc | 138 ------------------------ 3 files changed, 134 insertions(+), 139 deletions(-) create mode 100644 lib/libsomeip-c/example/test_service.c delete mode 100644 lib/libsomeip-c/example/test_service.cc diff --git a/lib/libsomeip-c/example/CMakeLists.txt b/lib/libsomeip-c/example/CMakeLists.txt index 1c8975e1d06..a53713fca39 100644 --- a/lib/libsomeip-c/example/CMakeLists.txt +++ b/lib/libsomeip-c/example/CMakeLists.txt @@ -1,3 +1,3 @@ -add_executable(someip_test_service test_service.cc) +add_executable(someip_test_service test_service.c) target_link_libraries(someip_test_service PRIVATE someip-c) \ No newline at end of file diff --git a/lib/libsomeip-c/example/test_service.c b/lib/libsomeip-c/example/test_service.c new file mode 100644 index 00000000000..016e915a722 --- /dev/null +++ b/lib/libsomeip-c/example/test_service.c @@ -0,0 +1,133 @@ +/* Fluent Bit + * ========== + * Copyright (C) 2015-2024 The Fluent Bit Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include + +#include "someip_api.h" + + +static const char* NAME = "Test Service"; +static const uint16_t SERVICE_ID = 4; +static const uint16_t INSTANCE_ID = 1; +static const uint16_t METHOD_ID = 1; +static const uint16_t EVENT_ID = 0x8000U; +static const uint16_t EVENT_GROUP_ID = 1; + +static uint16_t client_id = 0; + +/* + * Function to handle callback when a request is received. + * @param request_ptr Pointer to the structure that has the request details. + */ +void HandleRequest(void*, struct some_ip_request *request_ptr) { + static const char* response = "This is the response to the request"; + int ret = 0; + if (request_ptr == NULL) { + return; + } + printf("Received request (method = %d)\n", request_ptr->method_id); + printf("Payload length = %ld\n", request_ptr->payload_len); + + /* Normal service would Parse the request and perform/initiate some actions on it*/ + /* For this example just send back a canned response */ + + ret = someip_send_response(client_id, request_ptr->request_id.client_request_id, + (void*)response, strlen(response)); + if (ret != SOMEIP_RET_SUCCESS) { + printf("Failed to send response: %d\n", ret); + } +} + +/* + * Function to initialize the test service with the SOME/IP library. + * @return 0 on success, -1 on failure. + */ +int Initialize() { + int ret = someip_initialize(NAME, &client_id); + if (ret != SOMEIP_RET_SUCCESS) { + printf("Failed to initialize SOME/IP: %d\n", ret); + return -1; + } + + /* Register Request Handler */ + ret = someip_register_request_handler(client_id, SERVICE_ID, INSTANCE_ID, + METHOD_ID, NULL, HandleRequest); + + if (ret != SOMEIP_RET_SUCCESS) { + printf("Failed to register request handler: %d\n", ret); + someip_shutdown(client_id); + return -1; + } + + /* Offer Event */ + ret = someip_offer_event(client_id, SERVICE_ID, INSTANCE_ID, EVENT_ID, (uint16_t*)&EVENT_GROUP_ID, 1); + if (ret != SOMEIP_RET_SUCCESS) { + printf("Failed to Offer Event: %d\n", ret); + someip_shutdown(client_id); + return -1; + } + + /* Offer Service */ + ret = someip_offer_service(client_id, SERVICE_ID, INSTANCE_ID); + if (ret != SOMEIP_RET_SUCCESS) { + printf("Failed to Offer Service: %d\n", ret); + someip_shutdown(client_id); + return -1; + } + + return 0; +} + +void Teardown() { + someip_shutdown(client_id); +} + +void SendEvent(const int num) { + const char* base_msg = "Event Number "; + char buffer[128]; + int ret = 0; + strcpy(buffer, base_msg); + sprintf(buffer + strlen(base_msg), "%d", num); + + printf("Sending event with message %s\n", buffer); + + ret = someip_send_event(client_id, SERVICE_ID, INSTANCE_ID, EVENT_ID, + buffer, strlen(buffer)); + if (ret != SOMEIP_RET_SUCCESS) { + printf ("Failed to send event, %d\n", ret); + } +} + + +int main() { + int num_events = 10; + if (Initialize() != 0) { + return EXIT_FAILURE; + } + + + for (int i = 0; i <= num_events; ++i) { + SendEvent(i); + sleep(2); + } + + Teardown(); + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/lib/libsomeip-c/example/test_service.cc b/lib/libsomeip-c/example/test_service.cc deleted file mode 100644 index 2ccc9c16256..00000000000 --- a/lib/libsomeip-c/example/test_service.cc +++ /dev/null @@ -1,138 +0,0 @@ -/* Fluent Bit - * ========== - * Copyright (C) 2015-2024 The Fluent Bit Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "someip_api.h" -#include -#include -#include -#include -#include -#include - -/* Class declaration */ -class TestService { -public: - bool Initialize(); - void Teardown(); - void HandleRequest(const struct some_ip_request* request_ptr); - void SendEvent(const int num); -private: - uint16_t client_id_{0}; -}; - -namespace { - constexpr auto NAME = "Test Service"; - constexpr uint16_t SERVICE_ID = 4; - constexpr uint16_t INSTANCE_ID = 1; - constexpr uint16_t METHOD_ID = 1; - constexpr uint16_t EVENT_ID = 0x8000U; - constexpr uint16_t EVENT_GROUP_ID = 1; - - void RequestCallback(void* cookie, struct some_ip_request* request_ptr) { - if (cookie == nullptr) { - return; - } - auto service_pointer{static_cast(cookie)}; - service_pointer->HandleRequest(request_ptr); - } -} - -bool TestService::Initialize() { - auto ret = someip_initialize(NAME, &client_id_); - if (ret != SOMEIP_RET_SUCCESS) { - std::cout << "Failed to initialize SOME/IP: " << ret << std::endl; - return false; - } - - /* Register Request Handler */ - auto request_handler{[this](struct some_ip_request* request_ptr) { - HandleRequest(request_ptr); - }}; - ret = someip_register_request_handler(client_id_, SERVICE_ID, INSTANCE_ID, - METHOD_ID, this, RequestCallback); - - if (ret != SOMEIP_RET_SUCCESS) { - std::cout << "Failed to register request handler: " << ret << std::endl; - someip_shutdown(client_id_); - return false; - } - - /* Offer Event */ - ret = someip_offer_event(client_id_, SERVICE_ID, INSTANCE_ID, EVENT_ID, const_cast(&EVENT_GROUP_ID), 1); - if (ret != SOMEIP_RET_SUCCESS) { - std::cout << "Failed to Offer Event: " << ret << std::endl; - someip_shutdown(client_id_); - return false; - } - - /* Offer Service */ - ret = someip_offer_service(client_id_, SERVICE_ID, INSTANCE_ID); - if (ret != SOMEIP_RET_SUCCESS) { - std::cout << "Failed to Offer Service: " << ret << std::endl; - someip_shutdown(client_id_); - return false; - } - - return true; -} - -void TestService::Teardown() { - someip_shutdown(client_id_); -} - -void TestService::HandleRequest(const struct some_ip_request* request_ptr) { - if (request_ptr == nullptr) { - return; - } - std::cout << "Received request (method = " << request_ptr->method_id << ")" << std::endl; - std::cout << "Payload length = " << request_ptr->payload_len << std::endl; - - /* Normal service would Parse the request and perform/initiate some actions on it*/ - /* For this example just send back a canned response */ - auto response{"This is the response to the request"}; - const auto ret = someip_send_response(client_id_, request_ptr->request_id.client_request_id, - const_cast(response), strlen(response)); - if (ret != SOMEIP_RET_SUCCESS) { - std::cout << "Failed to send response: %d" << ret << std::endl; - } -} - -void TestService::SendEvent(const int num) { - std::stringstream ss; - ss << "Event Number " << num; - const auto message = ss.str(); - - auto ret = someip_send_event(client_id_, SERVICE_ID, INSTANCE_ID, EVENT_ID, - message.data(), message.size()); -} - - -int main() { - TestService service; - if (!service.Initialize()) { - return EXIT_FAILURE; - } - - auto num_events{10}; - - for (auto i = 0; i < num_events; ++i) { - service.SendEvent(i); - std::this_thread::sleep_for(std::chrono::seconds(2)); - } - - service.Teardown(); - return EXIT_SUCCESS; -} \ No newline at end of file