event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #define _POSIX_C_SOURCE 199309L
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <strings.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <stdarg.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/mman.h>
  12. #include <arpa/inet.h>
  13. #include <errno.h>
  14. #include <assert.h>
  15. #include <time.h>
  16. #include "pci.h"
  17. #include "tools.h"
  18. #include "error.h"
  19. #ifndef __timespec_defined
  20. struct timespec {
  21. time_t tv_sec;
  22. long tv_nsec;
  23. };
  24. #endif /* __timespec_defined */
  25. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event) {
  26. int i;
  27. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  28. pcilib_event_description_t *events = model_info->events;
  29. for (i = 0; events[i].name; i++) {
  30. if (!strcasecmp(events[i].name, event)) return events[i].evid;
  31. }
  32. return (pcilib_event_t)-1;
  33. }
  34. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type) {
  35. int i;
  36. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  37. pcilib_event_data_type_description_t *data_types = model_info->data_types;
  38. for (i = 0; data_types[i].name; i++) {
  39. if ((data_types[i].evid&event)&&(!strcasecmp(data_types[i].name, data_type))) return data_types[i].data_type;
  40. }
  41. return (pcilib_event_data_type_t)-1;
  42. }
  43. int pcilib_init_event_engine(pcilib_t *ctx) {
  44. pcilib_event_api_description_t *api;
  45. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  46. api = model_info->event_api;
  47. // api = pcilib_model[model].event_api;
  48. if ((api)&&(api->init)) {
  49. ctx->event_ctx = api->init(ctx);
  50. if (ctx->event_ctx) {
  51. ctx->event_ctx->pcilib = ctx;
  52. }
  53. }
  54. return 0;
  55. }
  56. int pcilib_reset(pcilib_t *ctx) {
  57. pcilib_event_api_description_t *api;
  58. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  59. api = model_info->event_api;
  60. if (!api) {
  61. pcilib_error("Event API is not supported by the selected model");
  62. return PCILIB_ERROR_NOTSUPPORTED;
  63. }
  64. if (api->reset)
  65. return api->reset(ctx->event_ctx);
  66. return 0;
  67. }
  68. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user) {
  69. pcilib_event_api_description_t *api;
  70. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  71. api = model_info->event_api;
  72. if (!api) {
  73. pcilib_error("Event API is not supported by the selected model");
  74. return PCILIB_ERROR_NOTSUPPORTED;
  75. }
  76. ctx->event_ctx->params.rawdata.callback = callback;
  77. ctx->event_ctx->params.rawdata.user = user;
  78. return 0;
  79. }
  80. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration) {
  81. pcilib_event_api_description_t *api;
  82. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  83. api = model_info->event_api;
  84. if (!api) {
  85. pcilib_error("Event API is not supported by the selected model");
  86. return PCILIB_ERROR_NOTSUPPORTED;
  87. }
  88. ctx->event_ctx->params.autostop.max_events = max_events;
  89. ctx->event_ctx->params.autostop.duration = duration;
  90. return 0;
  91. }
  92. int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
  93. /* To support hardware without autotriggering, we need to provide in event.c a code
  94. to generate multiple triggers in a thread (available in cli). The function should
  95. be re-enabled afterwards: just set parameters and let implementation decide if it
  96. can make triggering in hardware or will use our emulation */
  97. return PCILIB_ERROR_NOTSUPPORTED;
  98. }
  99. int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads) {
  100. pcilib_event_api_description_t *api;
  101. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  102. api = model_info->event_api;
  103. if (!api) {
  104. pcilib_error("Event API is not supported by the selected model");
  105. return PCILIB_ERROR_NOTSUPPORTED;
  106. }
  107. ctx->event_ctx->params.parallel.max_threads = max_threads;
  108. return 0;
  109. }
  110. int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
  111. pcilib_event_api_description_t *api;
  112. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  113. api = model_info->event_api;
  114. if (!api) {
  115. pcilib_error("Event API is not supported by the selected model");
  116. return PCILIB_ERROR_NOTSUPPORTED;
  117. }
  118. if (api->start)
  119. return api->start(ctx->event_ctx, event_mask, flags);
  120. return 0;
  121. }
  122. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags) {
  123. pcilib_event_api_description_t *api;
  124. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  125. api = model_info->event_api;
  126. if (!api) {
  127. pcilib_error("Event API is not supported by the selected model");
  128. return PCILIB_ERROR_NOTSUPPORTED;
  129. }
  130. if (api->stop)
  131. return api->stop(ctx->event_ctx, flags);
  132. return 0;
  133. }
  134. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user) {
  135. pcilib_event_api_description_t *api;
  136. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  137. api = model_info->event_api;
  138. if (!api) {
  139. pcilib_error("Event API is not supported by the selected model");
  140. return PCILIB_ERROR_NOTSUPPORTED;
  141. }
  142. if (api->stream)
  143. return api->stream(ctx->event_ctx, callback, user);
  144. if (api->next_event) {
  145. pcilib_error("Streaming using next_event API is not implemented yet");
  146. }
  147. pcilib_error("Event enumeration is not suppored by API");
  148. return PCILIB_ERROR_NOTSUPPORTED;
  149. }
  150. /*
  151. typedef struct {
  152. pcilib_event_id_t event_id;
  153. pcilib_event_info_t *info;
  154. } pcilib_return_event_callback_context_t;
  155. static int pcilib_return_event_callback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
  156. pcilib_return_event_callback_context_t *ctx = (pcilib_return_event_callback_context_t*)user;
  157. ctx->event_id = event_id;
  158. ctx->info = info;
  159. }
  160. */
  161. 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) {
  162. pcilib_event_api_description_t *api;
  163. // pcilib_return_event_callback_context_t user;
  164. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  165. api = model_info->event_api;
  166. if (!api) {
  167. pcilib_error("Event API is not supported by the selected model");
  168. return PCILIB_ERROR_NOTSUPPORTED;
  169. }
  170. if (api->next_event)
  171. return api->next_event(ctx->event_ctx, timeout, evid, info_size, info);
  172. /*
  173. if (api->stream) {
  174. err = api->stream(ctx->event_ctx, 1, timeout, pcilib_return_event_callback, &user);
  175. if (err) return err;
  176. if (evid) *evid = user->event_id;
  177. if (info) *info = user->info;
  178. return 0;
  179. }
  180. */
  181. pcilib_error("Event enumeration is not suppored by API");
  182. return PCILIB_ERROR_NOTSUPPORTED;
  183. }
  184. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
  185. pcilib_event_api_description_t *api;
  186. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  187. api = model_info->event_api;
  188. if (!api) {
  189. pcilib_error("Event API is not supported by the selected model");
  190. return PCILIB_ERROR_NOTSUPPORTED;
  191. }
  192. if (api->trigger)
  193. return api->trigger(ctx->event_ctx, event, trigger_size, trigger_data);
  194. pcilib_error("Self triggering is not supported by the selected model");
  195. return PCILIB_ERROR_NOTSUPPORTED;
  196. }
  197. 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) {
  198. int err;
  199. void *res = NULL;
  200. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  201. pcilib_event_api_description_t *api = model_info->event_api;
  202. if (!api) {
  203. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  204. pcilib_error("Event API is not supported by the selected model");
  205. return NULL;
  206. }
  207. if (api->get_data) {
  208. err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, size, &res);
  209. if (err) {
  210. if (size) *size = (size_t)err;
  211. return NULL;
  212. }
  213. return res;
  214. }
  215. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  216. return NULL;
  217. }
  218. 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) {
  219. int err;
  220. void *res = buf;
  221. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  222. pcilib_event_api_description_t *api = model_info->event_api;
  223. if (!api) {
  224. pcilib_error("Event API is not supported by the selected model");
  225. return PCILIB_ERROR_NOTSUPPORTED;
  226. }
  227. if (api->get_data) {
  228. err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, &size, &res);
  229. if (err) return err;
  230. if (buf != res) memcpy(buf, res, size);
  231. if (retsize) *retsize = size;
  232. return 0;
  233. }
  234. return PCILIB_ERROR_NOTSUPPORTED;
  235. }
  236. void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size) {
  237. int err;
  238. void *res = NULL;
  239. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  240. pcilib_event_api_description_t *api = model_info->event_api;
  241. if (!api) {
  242. pcilib_error("Event API is not supported by the selected model");
  243. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  244. return NULL;
  245. }
  246. if (api->get_data) {
  247. err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, size, &res);
  248. if (err) {
  249. if (size) *size = (size_t)err;
  250. return NULL;
  251. }
  252. return res;
  253. }
  254. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  255. return NULL;
  256. }
  257. 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) {
  258. int err;
  259. void *res = buf;
  260. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  261. pcilib_event_api_description_t *api = model_info->event_api;
  262. if (!api) {
  263. pcilib_error("Event API is not supported by the selected model");
  264. return PCILIB_ERROR_NOTSUPPORTED;
  265. }
  266. if (api->get_data) {
  267. err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, &size, &res);
  268. if (err) return err;
  269. if (buf != res) memcpy(buf, res, size);
  270. if (ret_size) *ret_size = size;
  271. return 0;
  272. }
  273. return PCILIB_ERROR_NOTSUPPORTED;
  274. }
  275. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data) {
  276. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  277. pcilib_event_api_description_t *api = model_info->event_api;
  278. if (!api) {
  279. pcilib_error("Event API is not supported by the selected model");
  280. return PCILIB_ERROR_NOTSUPPORTED;
  281. }
  282. if (api->return_data)
  283. return api->return_data(ctx->event_ctx, event_id, data_type, data);
  284. return 0;
  285. }
  286. typedef struct {
  287. pcilib_t *ctx;
  288. size_t *size;
  289. void **data;
  290. } pcilib_grab_callback_user_data_t;
  291. static int pcilib_grab_callback(pcilib_event_t event, pcilib_event_id_t event_id, void *vuser) {
  292. int err;
  293. void *data;
  294. size_t size;
  295. int allocated = 0;
  296. pcilib_grab_callback_user_data_t *user = (pcilib_grab_callback_user_data_t*)vuser;
  297. data = pcilib_get_data(user->ctx, event_id, PCILIB_EVENT_DATA, &size);
  298. if (!data) {
  299. pcilib_error("Error getting event data");
  300. return -(int)size;
  301. }
  302. if (*(user->data)) {
  303. if ((user->size)&&(*(user->size) < size)) {
  304. 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);
  305. return -PCILIB_ERROR_MEMORY;
  306. }
  307. *(user->size) = size;
  308. } else {
  309. *(user->data) = malloc(size);
  310. if (!*(user->data)) {
  311. pcilib_error("Memory allocation (%i bytes) for event data is failed");
  312. return -PCILIB_ERROR_MEMORY;
  313. }
  314. if (*(user->size)) *(user->size) = size;
  315. allocated = 1;
  316. }
  317. memcpy(*(user->data), data, size);
  318. err = pcilib_return_data(user->ctx, event_id, PCILIB_EVENT_DATA, data);
  319. if (err) {
  320. if (allocated) {
  321. free(*(user->data));
  322. *(user->data) = NULL;
  323. }
  324. pcilib_error("The event data had been overwritten before it was returned, data corruption may occur");
  325. return -err;
  326. }
  327. return PCILIB_STREAMING_CONTINUE;
  328. }
  329. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout) {
  330. int err;
  331. pcilib_event_id_t eid;
  332. pcilib_grab_callback_user_data_t user = {ctx, size, data};
  333. err = pcilib_start(ctx, event_mask, PCILIB_EVENT_FLAGS_DEFAULT);
  334. if (!err) err = pcilib_trigger(ctx, event_mask, 0, NULL);
  335. if (!err) {
  336. err = pcilib_get_next_event(ctx, timeout, &eid, 0, NULL);
  337. if (!err) pcilib_grab_callback(event_mask, eid, &user);
  338. }
  339. pcilib_stop(ctx, PCILIB_EVENT_FLAGS_DEFAULT);
  340. return err;
  341. }