Event
Register PnSServiceOn* a runtime callback.
Each event is logged again when it is triggered OnLogged(with the severity level matching the source event), regardless of whether a corresponding event has been subscribed to. Subscribe OnLogged to receive messages in Chinese.
The first snapshot is not triggered. cb Send NULL, ctx Send, Unsubscribe NULL . Returns an empty session PNS_ERR_INVALID_PARAM.
Subscription is single-slot
Each event has only one callback slot in a session:
- Calling
PnSServiceOn*again for the same event overwrites the old callback (the later registration wins); it is not appended. When you need multiple consumers, dispatch again inside your own callback. - Setting
cbtoNULLunsubscribes from that event;OnLoggedis also a single-slot method. - Callbacks run synchronously on the thread that triggered the event (snapshot calls such as
PnSServiceGetStatus/PnSServiceGetIo), not on a new thread. The snapshot call does not return until the callback returns; do not block for long inside a callback, and do not re-enter the registration or polling of the same session. - For each source event,
OnLoggedis called first, then that event's callback. - No exceptions in C: the SDK does not wrap callback exceptions or crashes; the callback must guarantee it does not crash.
Feature Overview
| Category | Register Function | Note | Automatic Logging |
|---|---|---|---|
| Connect | PnSServiceOnConnectedChanged | Alternate between on and off in sync with the IO controller's cycle | ✓ |
| Operating State | PnSServiceOnStateChanged | Changes in Operating State | ✓ |
| Runtime Error | PnSServiceOnErrorOccurred | Runtime Error | ✓ |
| IO Quality | PnSServiceOnIoStatusChanged | Changes in the "non-GOOD" byte count | ✓ |
| Link | PnSServiceOnLinkChanged | Network Card Link Up / Down | ✓ |
| Log | PnSServiceOnLogged | Each event listed above will trigger this one again. | — |
Connect
PnSServiceOnConnectedChanged
pns_result_t PnSServiceOnConnectedChanged(
pns_service_handle_t session,
pns_connected_changed_fn cb,
void * ctx);
Triggered when the periodic data exchange connection is lost (connected == false where ever_connected == true = disconnection; ever_connected == false = never connected).
Callback parameters:
typedef void (*pns_connected_changed_fn)(
void * ctx,
bool connected, /* Is the data currently being updated for a new cycle? */
bool ever_connected, /* Have you ever connected to it? */
pns_event_level_t level); /* Connected = Info; Disconnected / Never Connected = Warn */
Example:
static void on_connected(void * ctx, bool connected, bool ever_connected,
pns_event_level_t level)
{
(void)ctx;
(void)level;
if (!connected && ever_connected)
printf("掉线\n");
}
PnSServiceOnConnectedChanged(session, on_connected, NULL);
Operating State
PnSServiceOnStateChanged
pns_result_t PnSServiceOnStateChanged(
pns_service_handle_t session,
pns_state_changed_fn cb,
void * ctx);
Triggered when the operating state changes (SDK observation only; does not actively switch states).
Callback parameters:
typedef void (*pns_state_changed_fn)(
void * ctx,
pns_device_state_t old_state, /* Before the change */
pns_device_state_t new_state, /* After the change */
pns_event_level_t level); /* Info */
Example:
static void on_state(void * ctx,
pns_device_state_t old_state, pns_device_state_t new_state,
pns_event_level_t level)
{
(void)ctx;
(void)level;
printf("%d → %d\n", (int)old_state, (int)new_state);
}
PnSServiceOnStateChanged(session, on_state, NULL);
PnSServiceOnErrorOccurred
pns_result_t PnSServiceOnErrorOccurred(
pns_service_handle_t session,
pns_error_occurred_fn cb,
void * ctx);
Triggered when a runtime error occurs; error_code it is an int primitive value. For Chinese, please use PnSServiceGetErrorMessage.
Callback parameters:
typedef void (*pns_error_occurred_fn)(
void * ctx,
int error_code, /* Original value of a runtime error (not an enumeration) */
const char * message, /* News in Chinese */
pns_event_level_t level); /* Warn */
Example:
static void on_error(void * ctx, int error_code, const char * message,
pns_event_level_t level)
{
(void)ctx;
(void)level;
printf("%d: %s\n", error_code, message);
}
PnSServiceOnErrorOccurred(session, on_error, NULL);
IO Quality
PnSServiceOnIoStatusChanged
pns_result_t PnSServiceOnIoStatusChanged(
pns_service_handle_t session,
pns_io_status_changed_fn cb,
void * ctx);
The first time IOPS is read, only the baseline is recorded and no callback fires. If both inputIopsHex and outputIopsHex are missing, nothing is invented and no callback fires. A callback fires only when the non-GOOD byte count changes. All six languages follow the same rule; it is not a callback on every read. The criterion shares a source with PnSServiceGetIoStatus's iops_bad_count.
Callback parameters:
typedef void (*pns_io_status_changed_fn)(
void * ctx,
bool iops_bad, /* true = bytes IOPS with "yes" or "no GOOD " */
pns_event_level_t level); /* BAD = Warn, Restore GOOD = Info */
Link
PnSServiceOnLinkChanged
pns_result_t PnSServiceOnLinkChanged(
pns_service_handle_t session,
pns_link_changed_fn cb,
void * ctx);
Triggered when the network card link is Up or Down.
Callback parameters:
typedef void (*pns_link_changed_fn)(
void * ctx,
bool link_up, /* true = Link Up */
pns_event_level_t level); /* Up = Info, Down = Warn */
Log
PnSServiceOnLogged
pns_result_t PnSServiceOnLogged(
pns_service_handle_t session,
pns_logged_fn cb,
void * ctx);
This message is sent again each time an event is triggered, and its severity level follows that of the source event.
Callback parameters:
level(pns_event_level_t) — Log Levelmessage(const char *) — Log Body
pns_event_level_t
Events and log levels. Connection = Info; Disconnection / Runtime Error = Warn.
typedef enum {
PNS_EVENT_LEVEL_INFO = 0, /* Information */
PNS_EVENT_LEVEL_WARN = 1, /* Warning */
PNS_EVENT_LEVEL_ERROR = 2 /* Error */
} pns_event_level_t;
Example:
static void on_log(void * ctx, pns_event_level_t level, const char * message)
{
(void)ctx;
printf("[%d] %s\n", (int)level, message);
}
PnSServiceOnLogged(session, on_log, NULL);
Complete Example
PnSServiceOnConnectedChanged(session, on_connected, NULL);
PnSServiceOnStateChanged(session, on_state, NULL);
PnSServiceOnErrorOccurred(session, on_error, NULL);
PnSServiceOnIoStatusChanged(session, on_io_status, NULL);
PnSServiceOnLinkChanged(session, on_link, NULL);
PnSServiceOnLogged(session, on_log, NULL);