11#include " core/handlers.hpp"
22
3+ #include < cstdint>
4+ #include < map>
35#include < string>
46#include < vector>
57
@@ -19,6 +21,10 @@ using anolis::deviceprovider::v1::ReadSignalsRequest;
1921using anolis::deviceprovider::v1::Status;
2022using anolis::deviceprovider::v1::WaitReadyRequest;
2123
24+ #ifndef ANOLIS_PROVIDER_SIM_VERSION
25+ #define ANOLIS_PROVIDER_SIM_VERSION " 0.0.0"
26+ #endif
27+
2228static inline void set_status_ok (anolis::deviceprovider::v1::Response &resp) {
2329 resp.mutable_status ()->set_code (Status::CODE_OK);
2430 resp.mutable_status ()->set_message (" ok" );
@@ -41,7 +47,7 @@ void handle_hello(const HelloRequest &req,
4147 auto *hello = resp.mutable_hello ();
4248 hello->set_protocol_version (" v1" );
4349 hello->set_provider_name (" anolis-provider-sim" );
44- hello->set_provider_version (" 0.0.3 " );
50+ hello->set_provider_version (ANOLIS_PROVIDER_SIM_VERSION );
4551
4652 (*hello->mutable_metadata ())[" transport" ] = " stdio+uint32_le" ;
4753 (*hello->mutable_metadata ())[" max_frame_bytes" ] =
@@ -60,8 +66,13 @@ void handle_list_devices(const ListDevicesRequest &req,
6066 *out->add_devices () = d;
6167 }
6268
63- // v1 sim: we ignore include_health for now (device_health omitted
64- // intentionally).
69+ if (req.include_health ()) {
70+ const auto device_health = sim_health::make_list_devices_health (devices);
71+ for (const auto &health : device_health) {
72+ *out->add_device_health () = health;
73+ }
74+ }
75+
6576 set_status_ok (resp);
6677}
6778
@@ -149,11 +160,26 @@ void handle_call(const CallRequest &req,
149160 return ;
150161 }
151162
152- // v1: only function_id supported (matches our DescribeDevice contract).
163+ uint32_t resolved_function_id = req.function_id ();
164+
153165 if (req.function_id () == 0 ) {
154- set_status (resp, Status::CODE_UNIMPLEMENTED,
155- " function_name lookup not implemented in sim provider v1" );
156- return ;
166+ const auto function_id =
167+ sim_devices::resolve_function_id (req.device_id (), req.function_name ());
168+ if (!function_id.has_value ()) {
169+ set_status (resp, Status::CODE_NOT_FOUND,
170+ " unknown function_name '" + req.function_name () +
171+ " ' for device_id '" + req.device_id () + " '" );
172+ return ;
173+ }
174+ resolved_function_id = *function_id;
175+ } else if (!req.function_name ().empty ()) {
176+ const auto by_name =
177+ sim_devices::resolve_function_id (req.device_id (), req.function_name ());
178+ if (by_name.has_value () && *by_name != req.function_id ()) {
179+ set_status (resp, Status::CODE_INVALID_ARGUMENT,
180+ " function_id/function_name mismatch" );
181+ return ;
182+ }
157183 }
158184
159185 std::map<std::string, anolis::deviceprovider::v1::Value> args;
@@ -162,7 +188,7 @@ void handle_call(const CallRequest &req,
162188 }
163189
164190 const auto result =
165- sim_devices::call_function (req.device_id (), req. function_id () , args);
191+ sim_devices::call_function (req.device_id (), resolved_function_id , args);
166192 if (result.code != Status::CODE_OK) {
167193 set_status (resp, result.code , result.message );
168194 return ;
@@ -176,19 +202,14 @@ void handle_call(const CallRequest &req,
176202
177203void handle_get_health (const GetHealthRequest & /* req*/ ,
178204 anolis::deviceprovider::v1::Response &resp) {
205+ const auto init_report =
206+ anolis_provider_sim::DeviceFactory::get_initialization_report ();
179207 auto *out = resp.mutable_get_health ();
180- *out->mutable_provider () = sim_health::make_provider_health_ok ( );
208+ *out->mutable_provider () = sim_health::make_provider_health (init_report );
181209
182- // v1 sim: device health is optional; include basic OK states for known
183- // devices
184- const auto devices = sim_devices::list_devices (false );
185- for (const auto &d : devices) {
186- auto *dh = out->add_devices ();
187- dh->set_device_id (d.device_id ());
188- dh->set_state (anolis::deviceprovider::v1::DeviceHealth::STATE_OK);
189- dh->set_message (" ok" );
190- // last_seen omitted in v1 sim
191- (*dh->mutable_metrics ())[" impl" ] = " sim" ;
210+ const auto device_health = sim_health::make_get_health_devices (init_report);
211+ for (const auto &health : device_health) {
212+ *out->add_devices () = health;
192213 }
193214
194215 set_status_ok (resp);
@@ -205,10 +226,24 @@ void handle_wait_ready(const WaitReadyRequest & /*req*/,
205226
206227 std::cerr << " [WaitReady] Processing wait_ready() request\n " ;
207228
229+ const auto init_report =
230+ anolis_provider_sim::DeviceFactory::get_initialization_report ();
208231 auto *out = resp.mutable_wait_ready ();
209232 (*out->mutable_diagnostics ())[" init_time_ms" ] = " 0" ;
210233 (*out->mutable_diagnostics ())[" device_count" ] =
211234 std::to_string (sim_devices::list_devices (false ).size ());
235+ (*out->mutable_diagnostics ())[" startup_policy" ] =
236+ sim_health::startup_policy_name (init_report.startup_policy );
237+ (*out->mutable_diagnostics ())[" startup_configured_devices" ] =
238+ std::to_string (init_report.configured_device_count );
239+ (*out->mutable_diagnostics ())[" startup_initialized_devices" ] =
240+ std::to_string (init_report.successful_device_ids .size ());
241+ (*out->mutable_diagnostics ())[" startup_failed_devices" ] =
242+ std::to_string (init_report.failed_devices .size ());
243+ (*out->mutable_diagnostics ())[" startup_degraded" ] =
244+ init_report.failed_devices .empty () ? " false" : " true" ;
245+ (*out->mutable_diagnostics ())[" provider_version" ] =
246+ ANOLIS_PROVIDER_SIM_VERSION;
212247 (*out->mutable_diagnostics ())[" provider_impl" ] = " sim" ;
213248
214249 set_status_ok (resp);
0 commit comments