#include <stdio.h>
typedef enum {
ITE_STATE_EVERYTHING,
ITE_STATE_DEV_MODEL
}EVEN_E;
typedef struct {
int eventid;
void *callback;
} impl_event_map_t;
static impl_event_map_t g_impl_event_map[] = {
{ITE_STATE_EVERYTHING, NULL},
{ITE_STATE_DEV_MODEL, NULL}
};
#define IOT_RegisterCallback(evt, cb) iotx_register_for_##evt(cb)
#define DECLARE_EVENT_CALLBACK(evt, cb) int iotx_register_for_##evt(cb);
#define DEFINE_EVENT_CALLBACK(evt, cb) int iotx_register_for_##evt(cb) { \
if (evt < 0 || evt >= sizeof(g_impl_event_map)/sizeof(impl_event_map_t)) {return -1;} \
g_impl_event_map[evt].callback = (void *)callback;return 0;}
/* 状态 */
typedef void (*sighandler_t)(int);
typedef int (*state_handler_t)(const int state_code, const char *state_message);
DECLARE_EVENT_CALLBACK(ITE_STATE_EVERYTHING, state_handler_t cb);
DECLARE_EVENT_CALLBACK(ITE_STATE_DEV_MODEL, state_handler_t cb);
DEFINE_EVENT_CALLBACK(ITE_STATE_EVERYTHING, int (*callback)(const int, const char *))
DEFINE_EVENT_CALLBACK(ITE_STATE_DEV_MODEL, int (*callback)(const int, const char *))
void *iotx_event_callback(int evt)
{
if (evt < 0 || evt >= sizeof(g_impl_event_map) / sizeof(impl_event_map_t)) {
return NULL;
}
return g_impl_event_map[evt].callback;
}
int everything_state_handle(const int state_code, const char *state_message);
int everything_state_handle2(const int state_code, const char * state_message);
int main(void)
{
IOT_RegisterCallback(ITE_STATE_EVERYTHING, everything_state_handle);
IOT_RegisterCallback(ITE_STATE_DEV_MODEL, everything_state_handle2);
void *callback = NULL;
int intstate_code = 123;
const char * state_message = "44444";
callback = iotx_event_callback(ITE_STATE_EVERYTHING);
if (callback)
{
((int(*) (const int state_code, const char *state_message)) callback)(intstate_code, state_message);
}
intstate_code = 999;
state_message = "6666666";
callback = iotx_event_callback(ITE_STATE_DEV_MODEL);
if (callback)
{
((int(*) (const int state_code, const char *state_message)) callback)(intstate_code, state_message);
}
while(1);
return 0;
}
int everything_state_handle(const int state_code, const char * state_message)
{
printf("code: %d, message: %s\r\n", state_code, state_message);
return 0;
}
int everything_state_handle2(const int state_code, const char * state_message)
{
printf("code: %d, message: %s\r\n", state_code, state_message);
return 0;
}
阿里sdk 回调机制例子
最新推荐文章于 2023-05-06 13:51:16 发布