1+ /* Fluent Bit
2+ * ==========
3+ * Copyright (C) 2015-2024 The Fluent Bit Authors
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ #include <stdint.h>
18+ #include <stdio.h>
19+ #include <stdlib.h>
20+ #include <string.h>
21+ #include <unistd.h>
22+
23+ #include "someip_api.h"
24+
25+
26+ static const char * NAME = "Test Service" ;
27+ static const uint16_t SERVICE_ID = 4 ;
28+ static const uint16_t INSTANCE_ID = 1 ;
29+ static const uint16_t METHOD_ID = 1 ;
30+ static const uint16_t EVENT_ID = 0x8000U ;
31+ static const uint16_t EVENT_GROUP_ID = 1 ;
32+
33+ static uint16_t client_id = 0 ;
34+
35+ /*
36+ * Function to handle callback when a request is received.
37+ * @param request_ptr Pointer to the structure that has the request details.
38+ */
39+ void HandleRequest (void * , struct some_ip_request * request_ptr ) {
40+ static const char * response = "This is the response to the request" ;
41+ int ret = 0 ;
42+ if (request_ptr == NULL ) {
43+ return ;
44+ }
45+ printf ("Received request (method = %d)\n" , request_ptr -> method_id );
46+ printf ("Payload length = %ld\n" , request_ptr -> payload_len );
47+
48+ /* Normal service would Parse the request and perform/initiate some actions on it*/
49+ /* For this example just send back a canned response */
50+
51+ ret = someip_send_response (client_id , request_ptr -> request_id .client_request_id ,
52+ (void * )response , strlen (response ));
53+ if (ret != SOMEIP_RET_SUCCESS ) {
54+ printf ("Failed to send response: %d\n" , ret );
55+ }
56+ }
57+
58+ /*
59+ * Function to initialize the test service with the SOME/IP library.
60+ * @return 0 on success, -1 on failure.
61+ */
62+ int Initialize () {
63+ int ret = someip_initialize (NAME , & client_id );
64+ if (ret != SOMEIP_RET_SUCCESS ) {
65+ printf ("Failed to initialize SOME/IP: %d\n" , ret );
66+ return -1 ;
67+ }
68+
69+ /* Register Request Handler */
70+ ret = someip_register_request_handler (client_id , SERVICE_ID , INSTANCE_ID ,
71+ METHOD_ID , NULL , HandleRequest );
72+
73+ if (ret != SOMEIP_RET_SUCCESS ) {
74+ printf ("Failed to register request handler: %d\n" , ret );
75+ someip_shutdown (client_id );
76+ return -1 ;
77+ }
78+
79+ /* Offer Event */
80+ ret = someip_offer_event (client_id , SERVICE_ID , INSTANCE_ID , EVENT_ID , (uint16_t * )& EVENT_GROUP_ID , 1 );
81+ if (ret != SOMEIP_RET_SUCCESS ) {
82+ printf ("Failed to Offer Event: %d\n" , ret );
83+ someip_shutdown (client_id );
84+ return -1 ;
85+ }
86+
87+ /* Offer Service */
88+ ret = someip_offer_service (client_id , SERVICE_ID , INSTANCE_ID );
89+ if (ret != SOMEIP_RET_SUCCESS ) {
90+ printf ("Failed to Offer Service: %d\n" , ret );
91+ someip_shutdown (client_id );
92+ return -1 ;
93+ }
94+
95+ return 0 ;
96+ }
97+
98+ void Teardown () {
99+ someip_shutdown (client_id );
100+ }
101+
102+ void SendEvent (const int num ) {
103+ const char * base_msg = "Event Number " ;
104+ char buffer [128 ];
105+ int ret = 0 ;
106+ strcpy (buffer , base_msg );
107+ sprintf (buffer + strlen (base_msg ), "%d" , num );
108+
109+ printf ("Sending event with message %s\n" , buffer );
110+
111+ ret = someip_send_event (client_id , SERVICE_ID , INSTANCE_ID , EVENT_ID ,
112+ buffer , strlen (buffer ));
113+ if (ret != SOMEIP_RET_SUCCESS ) {
114+ printf ("Failed to send event, %d\n" , ret );
115+ }
116+ }
117+
118+
119+ int main () {
120+ int num_events = 10 ;
121+ if (Initialize () != 0 ) {
122+ return EXIT_FAILURE ;
123+ }
124+
125+
126+ for (int i = 0 ; i <= num_events ; ++ i ) {
127+ SendEvent (i );
128+ sleep (2 );
129+ }
130+
131+ Teardown ();
132+ return EXIT_SUCCESS ;
133+ }
0 commit comments