event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
  93. pcilib_event_api_description_t *api;
  94. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  95. api = model_info->event_api;
  96. if (!api) {
  97. pcilib_error("Event API is not supported by the selected model");
  98. return PCILIB_ERROR_NOTSUPPORTED;
  99. }
  100. if (api->start)
  101. return api->start(ctx->event_ctx, event_mask, flags);
  102. return 0;
  103. }
  104. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags) {
  105. pcilib_event_api_description_t *api;
  106. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  107. api = model_info->event_api;
  108. if (!api) {
  109. pcilib_error("Event API is not supported by the selected model");
  110. return PCILIB_ERROR_NOTSUPPORTED;
  111. }
  112. if (api->stop)
  113. return api->stop(ctx->event_ctx, flags);
  114. return 0;
  115. }
  116. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user) {
  117. pcilib_event_api_description_t *api;
  118. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  119. api = model_info->event_api;
  120. if (!api) {
  121. pcilib_error("Event API is not supported by the selected model");
  122. return PCILIB_ERROR_NOTSUPPORTED;
  123. }
  124. if (api->stream)
  125. return api->stream(ctx->event_ctx, callback, user);
  126. if (api->next_event) {
  127. pcilib_error("Streaming using next_event API is not implemented yet");
  128. }
  129. pcilib_error("Event enumeration is not suppored by API");
  130. return PCILIB_ERROR_NOTSUPPORTED;
  131. }
  132. /*
  133. typedef struct {
  134. pcilib_event_id_t event_id;
  135. pcilib_event_info_t *info;
  136. } pcilib_return_event_callback_context_t;
  137. static int pcilib_return_event_callback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
  138. pcilib_return_event_callback_context_t *ctx = (pcilib_return_event_callback_context_t*)user;
  139. ctx->event_id = event_id;
  140. ctx->info = info;
  141. }
  142. */
  143. 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) {
  144. pcilib_event_api_description_t *api;
  145. // pcilib_return_event_callback_context_t user;
  146. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  147. api = model_info->event_api;
  148. if (!api) {
  149. pcilib_error("Event API is not supported by the selected model");
  150. return PCILIB_ERROR_NOTSUPPORTED;
  151. }
  152. if (api->next_event)
  153. return api->next_event(ctx->event_ctx, timeout, evid, info_size, info);
  154. /*
  155. if (api->stream) {
  156. err = api->stream(ctx->event_ctx, 1, timeout, pcilib_return_event_callback, &user);
  157. if (err) return err;
  158. if (evid) *evid = user->event_id;
  159. if (info) *info = user->info;
  160. return 0;
  161. }
  162. */
  163. pcilib_error("Event enumeration is not suppored by API");
  164. return PCILIB_ERROR_NOTSUPPORTED;
  165. }
  166. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
  167. pcilib_event_api_description_t *api;
  168. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  169. api = model_info->event_api;
  170. if (!api) {
  171. pcilib_error("Event API is not supported by the selected model");
  172. return PCILIB_ERROR_NOTSUPPORTED;
  173. }
  174. if (api->trigger)
  175. return api->trigger(ctx->event_ctx, event, trigger_size, trigger_data);
  176. pcilib_error("Self triggering is not supported by the selected model");
  177. return PCILIB_ERROR_NOTSUPPORTED;
  178. }
  179. 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) {
  180. int err;
  181. void *res = NULL;
  182. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  183. pcilib_event_api_description_t *api = model_info->event_api;
  184. if (!api) {
  185. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  186. pcilib_error("Event API is not supported by the selected model");
  187. return NULL;
  188. }
  189. if (api->get_data) {
  190. err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, size, &res);
  191. if (err) {
  192. if (size) *size = (size_t)err;
  193. return NULL;
  194. }
  195. return res;
  196. }
  197. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  198. return NULL;
  199. }
  200. 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) {
  201. int err;
  202. void *res = buf;
  203. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  204. pcilib_event_api_description_t *api = model_info->event_api;
  205. if (!api) {
  206. pcilib_error("Event API is not supported by the selected model");
  207. return PCILIB_ERROR_NOTSUPPORTED;
  208. }
  209. if (api->get_data) {
  210. err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, &size, &res);
  211. if (err) return err;
  212. if (buf != res) memcpy(buf, res, size);
  213. if (retsize) *retsize = size;
  214. return 0;
  215. }
  216. return PCILIB_ERROR_NOTSUPPORTED;
  217. }
  218. void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size) {
  219. int err;
  220. void *res = NULL;
  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. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  226. return NULL;
  227. }
  228. if (api->get_data) {
  229. err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, size, &res);
  230. if (err) {
  231. if (size) *size = (size_t)err;
  232. return NULL;
  233. }
  234. return res;
  235. }
  236. if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
  237. return NULL;
  238. }
  239. 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) {
  240. int err;
  241. void *res = buf;
  242. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  243. pcilib_event_api_description_t *api = model_info->event_api;
  244. if (!api) {
  245. pcilib_error("Event API is not supported by the selected model");
  246. return PCILIB_ERROR_NOTSUPPORTED;
  247. }
  248. if (api->get_data) {
  249. err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, &size, &res);
  250. if (err) return err;
  251. if (buf != res) memcpy(buf, res, size);
  252. if (ret_size) *ret_size = size;
  253. return 0;
  254. }
  255. return PCILIB_ERROR_NOTSUPPORTED;
  256. }
  257. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data) {
  258. pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
  259. pcilib_event_api_description_t *api = model_info->event_api;
  260. if (!api) {
  261. pcilib_error("Event API is not supported by the selected model");
  262. return PCILIB_ERROR_NOTSUPPORTED;
  263. }
  264. if (api->return_data)
  265. return api->return_data(ctx->event_ctx, event_id, data_type, data);
  266. return 0;
  267. }
  268. typedef struct {
  269. pcilib_t *ctx;
  270. size_t *size;
  271. void **data;
  272. } pcilib_grab_callback_user_data_t;
  273. static int pcilib_grab_callback(pcilib_event_t event, pcilib_event_id_t event_id, void *vuser) {
  274. int err;
  275. void *data;
  276. size_t size;
  277. int allocated = 0;
  278. pcilib_grab_callback_user_data_t *user = (pcilib_grab_callback_user_data_t*)vuser;
  279. data = pcilib_get_data(user->ctx, event_id, PCILIB_EVENT_DATA, &size);
  280. if (!data) {
  281. pcilib_error("Error getting event data");
  282. return -(int)size;
  283. }
  284. if (*(user->data)) {
  285. if ((user->size)&&(*(user->size) < size)) {
  286. 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);
  287. return -PCILIB_ERROR_MEMORY;
  288. }
  289. *(user->size) = size;
  290. } else {
  291. *(user->data) = malloc(size);
  292. if (!*(user->data)) {
  293. pcilib_error("Memory allocation (%i bytes) for event data is failed");
  294. return -PCILIB_ERROR_MEMORY;
  295. }
  296. if (*(user->size)) *(user->size) = size;
  297. allocated = 1;
  298. }
  299. memcpy(*(user->data), data, size);
  300. err = pcilib_return_data(user->ctx, event_id, PCILIB_EVENT_DATA, data);
  301. if (err) {
  302. if (allocated) {
  303. free(*(user->data));
  304. *(user->data) = NULL;
  305. }
  306. pcilib_error("The event data had been overwritten before it was returned, data corruption may occur");
  307. return -err;
  308. }
  309. return PCILIB_STREAMING_CONTINUE;
  310. }
  311. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout) {
  312. int err;
  313. pcilib_event_id_t eid;
  314. pcilib_grab_callback_user_data_t user = {ctx, size, data};
  315. err = pcilib_start(ctx, event_mask, PCILIB_EVENT_FLAGS_DEFAULT);
  316. if (!err) err = pcilib_trigger(ctx, event_mask, 0, NULL);
  317. if (!err) {
  318. err = pcilib_get_next_event(ctx, timeout, &eid, 0, NULL);
  319. if (!err) pcilib_grab_callback(event_mask, eid, &user);
  320. }
  321. pcilib_stop(ctx, PCILIB_EVENT_FLAGS_DEFAULT);
  322. return err;
  323. }