123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #define _POSIX_C_SOURCE 199309L
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdarg.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <assert.h>
- #include <time.h>
- #include "pci.h"
- #include "tools.h"
- #include "error.h"
- #ifndef __timespec_defined
- struct timespec {
- time_t tv_sec;
- long tv_nsec;
- };
- #endif /* __timespec_defined */
- pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event) {
- int i;
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- pcilib_event_description_t *events = model_info->events;
- for (i = 0; events[i].name; i++) {
- if (!strcasecmp(events[i].name, event)) return events[i].evid;
- }
- return (pcilib_event_t)-1;
- }
- pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type) {
- int i;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- pcilib_event_data_type_description_t *data_types = model_info->data_types;
-
- for (i = 0; data_types[i].name; i++) {
- if ((data_types[i].evid&event)&&(!strcasecmp(data_types[i].name, data_type))) return data_types[i].data_type;
- }
- return (pcilib_event_data_type_t)-1;
- }
- int pcilib_init_event_engine(pcilib_t *ctx) {
- pcilib_event_api_description_t *api;
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- // api = pcilib_model[model].event_api;
- if ((api)&&(api->init)) {
- ctx->event_ctx = api->init(ctx);
- if (ctx->event_ctx) {
- ctx->event_ctx->pcilib = ctx;
- }
- }
-
- return 0;
- }
- int pcilib_reset(pcilib_t *ctx) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
-
- if (api->reset)
- return api->reset(ctx->event_ctx);
-
- return 0;
- }
- int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- ctx->event_ctx->params.rawdata.callback = callback;
- ctx->event_ctx->params.rawdata.user = user;
-
- return 0;
- }
- int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- ctx->event_ctx->params.autostop.max_events = max_events;
- ctx->event_ctx->params.autostop.duration = duration;
-
- return 0;
- }
- int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
- /* To support hardware without autotriggering, we need to provide in event.c a code
- to generate multiple triggers in a thread (available in cli). The function should
- be re-enabled afterwards: just set parameters and let implementation decide if it
- can make triggering in hardware or will use our emulation */
-
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- ctx->event_ctx->params.parallel.max_threads = max_threads;
- return 0;
- }
- int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->start)
- return api->start(ctx->event_ctx, event_mask, flags);
- return 0;
- }
- int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->stop)
- return api->stop(ctx->event_ctx, flags);
- return 0;
- }
- int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->stream)
- return api->stream(ctx->event_ctx, callback, user);
- if (api->next_event) {
- pcilib_error("Streaming using next_event API is not implemented yet");
- }
- pcilib_error("Event enumeration is not suppored by API");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- /*
- typedef struct {
- pcilib_event_id_t event_id;
- pcilib_event_info_t *info;
- } pcilib_return_event_callback_context_t;
- static int pcilib_return_event_callback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
- pcilib_return_event_callback_context_t *ctx = (pcilib_return_event_callback_context_t*)user;
- ctx->event_id = event_id;
- ctx->info = info;
- }
- */
- int pcilib_get_next_event(pcilib_t *ctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
- pcilib_event_api_description_t *api;
- // pcilib_return_event_callback_context_t user;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->next_event)
- return api->next_event(ctx->event_ctx, timeout, evid, info_size, info);
- /*
- if (api->stream) {
- err = api->stream(ctx->event_ctx, 1, timeout, pcilib_return_event_callback, &user);
- if (err) return err;
-
- if (evid) *evid = user->event_id;
- if (info) *info = user->info;
- return 0;
- }
- */
- pcilib_error("Event enumeration is not suppored by API");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
- pcilib_event_api_description_t *api;
-
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->trigger)
- return api->trigger(ctx->event_ctx, event, trigger_size, trigger_data);
- pcilib_error("Self triggering is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- void *pcilib_get_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t *size) {
- int err;
- void *res = NULL;
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- pcilib_event_api_description_t *api = model_info->event_api;
- if (!api) {
- if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
- pcilib_error("Event API is not supported by the selected model");
- return NULL;
- }
- if (api->get_data) {
- err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, size, &res);
- if (err) {
- if (size) *size = (size_t)err;
- return NULL;
- }
- return res;
- }
-
- if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
- return NULL;
- }
- int pcilib_copy_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t size, void *buf, size_t *retsize) {
- int err;
- void *res = buf;
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- pcilib_event_api_description_t *api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->get_data) {
- err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, &size, &res);
- if (err) return err;
-
- if (buf != res) memcpy(buf, res, size);
-
- if (retsize) *retsize = size;
- return 0;
- }
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size) {
- int err;
- void *res = NULL;
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- pcilib_event_api_description_t *api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
- return NULL;
- }
- if (api->get_data) {
- err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, size, &res);
- if (err) {
- if (size) *size = (size_t)err;
- return NULL;
- }
- return res;
- }
- if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
- return NULL;
- }
- int pcilib_copy_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t size, void *buf, size_t *ret_size) {
- int err;
- void *res = buf;
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- pcilib_event_api_description_t *api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->get_data) {
- err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, &size, &res);
- if (err) return err;
-
- if (buf != res) memcpy(buf, res, size);
- if (ret_size) *ret_size = size;
- return 0;
- }
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data) {
- pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
-
- pcilib_event_api_description_t *api = model_info->event_api;
- if (!api) {
- pcilib_error("Event API is not supported by the selected model");
- return PCILIB_ERROR_NOTSUPPORTED;
- }
- if (api->return_data)
- return api->return_data(ctx->event_ctx, event_id, data_type, data);
- return 0;
- }
- typedef struct {
- pcilib_t *ctx;
-
- size_t *size;
- void **data;
- } pcilib_grab_callback_user_data_t;
- static int pcilib_grab_callback(pcilib_event_t event, pcilib_event_id_t event_id, void *vuser) {
- int err;
- void *data;
- size_t size;
- int allocated = 0;
- pcilib_grab_callback_user_data_t *user = (pcilib_grab_callback_user_data_t*)vuser;
- data = pcilib_get_data(user->ctx, event_id, PCILIB_EVENT_DATA, &size);
- if (!data) {
- pcilib_error("Error getting event data");
- return -(int)size;
- }
-
- if (*(user->data)) {
- if ((user->size)&&(*(user->size) < size)) {
- pcilib_error("The supplied buffer does not have enough space to hold the event data. Buffer size is %z, but %z is required", user->size, size);
- return -PCILIB_ERROR_MEMORY;
- }
- *(user->size) = size;
- } else {
- *(user->data) = malloc(size);
- if (!*(user->data)) {
- pcilib_error("Memory allocation (%i bytes) for event data is failed");
- return -PCILIB_ERROR_MEMORY;
- }
- if (*(user->size)) *(user->size) = size;
- allocated = 1;
- }
-
- memcpy(*(user->data), data, size);
-
- err = pcilib_return_data(user->ctx, event_id, PCILIB_EVENT_DATA, data);
- if (err) {
- if (allocated) {
- free(*(user->data));
- *(user->data) = NULL;
- }
- pcilib_error("The event data had been overwritten before it was returned, data corruption may occur");
- return -err;
- }
-
- return PCILIB_STREAMING_CONTINUE;
- }
- int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout) {
- int err;
- pcilib_event_id_t eid;
-
- pcilib_grab_callback_user_data_t user = {ctx, size, data};
-
- err = pcilib_start(ctx, event_mask, PCILIB_EVENT_FLAGS_DEFAULT);
- if (!err) err = pcilib_trigger(ctx, event_mask, 0, NULL);
- if (!err) {
- err = pcilib_get_next_event(ctx, timeout, &eid, 0, NULL);
- if (!err) pcilib_grab_callback(event_mask, eid, &user);
- }
- pcilib_stop(ctx, PCILIB_EVENT_FLAGS_DEFAULT);
- return err;
- }
|