#pragma once #ifndef ANSCLOUD_DEVICE_AGENT_C_H #define ANSCLOUD_DEVICE_AGENT_C_H #ifdef ANSCLOUD_DEVICE_EXPORTS #define ANSCLOUD_DEVICE_CAPI __declspec(dllexport) #else #define ANSCLOUD_DEVICE_CAPI __declspec(dllimport) #endif #ifdef __cplusplus extern "C" { #endif //============================================================================= // Opaque handle //============================================================================= typedef struct AnsDeviceAgent* AnsDeviceAgentHandle; //============================================================================= // C-compatible config structs //============================================================================= typedef struct { const char* host; int port; const char* username; const char* password; const char* vhost; int use_tls; } AnsDeviceBrokerConfig; typedef struct { const char* device_id; const char* device_secret; const char* tenant_id; } AnsDeviceCredentials; typedef struct { AnsDeviceBrokerConfig broker; AnsDeviceCredentials credentials; int heartbeat_interval_sec; int metrics_interval_sec; const char* firmware_version; const char* ansvis_version; const char* ip_address; } AnsDeviceConfig; //============================================================================= // Callback types //============================================================================= // command_json: JSON string of Command. Return: JSON string of CommandResponse. // Caller must free returned string with ansdevice_free_string(). typedef const char* (*AnsDeviceCommandHandlerFn)(const char* command_json, void* user_data); // metrics_json: caller must free returned JSON string typedef const char* (*AnsDeviceMetricsProviderFn)(void* user_data); typedef void (*AnsDeviceConnectionCallbackFn)(int connected, void* user_data); typedef void (*AnsDeviceErrorCallbackFn)(const char* error, void* user_data); typedef void (*AnsDeviceBroadcastCallbackFn)(const char* message, void* user_data); //============================================================================= // Lifecycle //============================================================================= // broker_factory_fn: function pointer that creates your IMessageBroker. // For simplicity in C API, pass NULL to use a default (if you register one). ANSCLOUD_DEVICE_CAPI AnsDeviceAgentHandle ansdevice_create(void); ANSCLOUD_DEVICE_CAPI void ansdevice_destroy(AnsDeviceAgentHandle handle); ANSCLOUD_DEVICE_CAPI int ansdevice_configure(AnsDeviceAgentHandle handle, const AnsDeviceConfig* config); ANSCLOUD_DEVICE_CAPI int ansdevice_start(AnsDeviceAgentHandle handle); ANSCLOUD_DEVICE_CAPI void ansdevice_stop(AnsDeviceAgentHandle handle); ANSCLOUD_DEVICE_CAPI int ansdevice_is_running(AnsDeviceAgentHandle handle); ANSCLOUD_DEVICE_CAPI int ansdevice_is_connected(AnsDeviceAgentHandle handle); //============================================================================= // Handlers & providers //============================================================================= ANSCLOUD_DEVICE_CAPI void ansdevice_set_command_handler(AnsDeviceAgentHandle handle, AnsDeviceCommandHandlerFn fn, void* user_data); ANSCLOUD_DEVICE_CAPI void ansdevice_set_metrics_provider(AnsDeviceAgentHandle handle, AnsDeviceMetricsProviderFn fn, void* user_data); //============================================================================= // Publishing //============================================================================= ANSCLOUD_DEVICE_CAPI int ansdevice_publish_event(AnsDeviceAgentHandle handle, const char* event_json); ANSCLOUD_DEVICE_CAPI void ansdevice_send_heartbeat(AnsDeviceAgentHandle handle); ANSCLOUD_DEVICE_CAPI void ansdevice_send_metrics(AnsDeviceAgentHandle handle); //============================================================================= // Callbacks //============================================================================= ANSCLOUD_DEVICE_CAPI void ansdevice_on_connection_changed(AnsDeviceAgentHandle handle, AnsDeviceConnectionCallbackFn fn, void* user_data); ANSCLOUD_DEVICE_CAPI void ansdevice_on_error(AnsDeviceAgentHandle handle, AnsDeviceErrorCallbackFn fn, void* user_data); ANSCLOUD_DEVICE_CAPI void ansdevice_on_broadcast(AnsDeviceAgentHandle handle, AnsDeviceBroadcastCallbackFn fn, void* user_data); //============================================================================= // Status & memory //============================================================================= // Returns JSON string. Caller must free with ansdevice_free_string(). ANSCLOUD_DEVICE_CAPI const char* ansdevice_get_status(AnsDeviceAgentHandle handle); ANSCLOUD_DEVICE_CAPI void ansdevice_free_string(const char* str); ANSCLOUD_DEVICE_CAPI const char* ansdevice_version(void); #ifdef __cplusplus } #endif #endif // ANSCLOUD_DEVICE_AGENT_C_H