OpenOCD
device.c
Go to the documentation of this file.
1 /*
2  * This file is part of the libjaylink project.
3  *
4  * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #ifdef _WIN32
29 #include <winsock2.h>
30 #else
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #endif
34 #ifdef HAVE_LIBUSB
35 #include <libusb.h>
36 #endif
37 
38 #include "libjaylink.h"
39 #include "libjaylink-internal.h"
40 
48 #define CMD_GET_VERSION 0x01
49 #define CMD_GET_HW_STATUS 0x07
50 #define CMD_REGISTER 0x09
51 #define CMD_GET_HW_INFO 0xc1
52 #define CMD_GET_COUNTERS 0xc2
53 #define CMD_GET_FREE_MEMORY 0xd4
54 #define CMD_GET_CAPS 0xe8
55 #define CMD_GET_EXT_CAPS 0xed
56 #define CMD_GET_HW_VERSION 0xf0
57 #define CMD_READ_CONFIG 0xf2
58 #define CMD_WRITE_CONFIG 0xf3
59 
60 #define REG_CMD_REGISTER 0x64
61 #define REG_CMD_UNREGISTER 0x65
62 
64 #define REG_HEADER_SIZE 8
65 
66 #define REG_MIN_SIZE 0x4c
67 
68 #define REG_MAX_SIZE 0x200
69 
70 #define REG_CONN_INFO_SIZE 16
71 
72 /* The maximum path depth according to the USB 3.0 specification. */
73 #define MAX_USB_PATH_DEPTH 7
74 
78  struct jaylink_context *ctx)
79 {
80  struct jaylink_device *dev;
81  struct list *list;
82 
83  dev = malloc(sizeof(struct jaylink_device));
84 
85  if (!dev)
86  return NULL;
87 
88  list = list_prepend(ctx->devs, dev);
89 
90  if (!list) {
91  free(dev);
92  return NULL;
93  }
94 
95  ctx->devs = list;
96 
97  dev->ctx = ctx;
98  dev->ref_count = 1;
99 
100  return dev;
101 }
102 
104 {
105  struct jaylink_device **list;
106 
107  list = malloc(sizeof(struct jaylink_device *) * (length + 1));
108 
109  if (!list)
110  return NULL;
111 
112  list[length] = NULL;
113 
114  return list;
115 }
116 
138  struct jaylink_device ***devs, size_t *count)
139 {
140  size_t num;
141  struct list *item;
142  struct jaylink_device **tmp;
143  struct jaylink_device *dev;
144 
145  if (!ctx || !devs)
146  return JAYLINK_ERR_ARG;
147 
148  num = list_length(ctx->discovered_devs);
149  tmp = allocate_device_list(num);
150 
151  if (!tmp) {
152  log_err(ctx, "Failed to allocate device list.");
153  return JAYLINK_ERR_MALLOC;
154  }
155 
156  item = ctx->discovered_devs;
157 
158  for (size_t i = 0; i < num; i++) {
159  dev = (struct jaylink_device *)item->data;
160  tmp[i] = jaylink_ref_device(dev);
161  item = item->next;
162  }
163 
164  if (count)
165  *count = num;
166 
167  *devs = tmp;
168 
169  return JAYLINK_OK;
170 }
171 
184 {
185  if (!devs)
186  return;
187 
188  if (unref) {
189  for (size_t i = 0; devs[i]; i++)
190  jaylink_unref_device(devs[i]);
191  }
192 
193  free(devs);
194 }
195 
209  const struct jaylink_device *dev,
211 {
212  if (!dev || !iface)
213  return JAYLINK_ERR_ARG;
214 
215  *iface = dev->iface;
216 
217  return JAYLINK_OK;
218 }
219 
237  const struct jaylink_device *dev, uint32_t *serial_number)
238 {
239  if (!dev || !serial_number)
240  return JAYLINK_ERR_ARG;
241 
242  if (!dev->has_serial_number)
244 
245  *serial_number = dev->serial_number;
246 
247  return JAYLINK_OK;
248 }
249 
270  const struct jaylink_device *dev,
271  enum jaylink_usb_address *address)
272 {
273  if (!dev || !address)
274  return JAYLINK_ERR_ARG;
275 
276  if (dev->iface != JAYLINK_HIF_USB)
278 
279 #ifdef HAVE_LIBUSB
280  *address = dev->usb_address;
281 
282  return JAYLINK_OK;
283 #else
285 #endif
286 }
287 
309  const struct jaylink_device *dev, uint8_t *bus,
310  uint8_t **ports, size_t *length)
311 {
312  if (!dev || !bus || !ports || !length)
313  return JAYLINK_ERR_ARG;
314 
315  if (dev->iface != JAYLINK_HIF_USB)
317 
318 #ifdef HAVE_LIBUSB
319  struct jaylink_context *ctx = dev->ctx;
320  int ret;
321 
322  *ports = malloc(MAX_USB_PATH_DEPTH * sizeof(uint8_t));
323 
324  if (!*ports) {
325  return JAYLINK_ERR_MALLOC;
326  }
327 
328  ret = libusb_get_port_numbers(dev->usb_dev, *ports,
329  MAX_USB_PATH_DEPTH);
330 
331  if (ret == LIBUSB_ERROR_OVERFLOW) {
332  log_err(ctx, "Failed to get port numbers: %s.",
333  libusb_error_name(ret));
334  return JAYLINK_ERR_ARG;
335  }
336 
337  *length = ret;
338  *bus = libusb_get_bus_number(dev->usb_dev);
339 
340  return JAYLINK_OK;
341 #else
343 #endif
344 }
345 
361  const struct jaylink_device *dev, char *address)
362 {
363  if (!dev || !address)
364  return JAYLINK_ERR_ARG;
365 
366  if (dev->iface != JAYLINK_HIF_TCP)
368 
369  memcpy(address, dev->ipv4_address, sizeof(dev->ipv4_address));
370 
371  return JAYLINK_OK;
372 }
373 
391  const struct jaylink_device *dev, uint8_t *address)
392 {
393  if (!dev || !address)
394  return JAYLINK_ERR_ARG;
395 
396  if (dev->iface != JAYLINK_HIF_TCP)
398 
399  if (!dev->has_mac_address)
401 
402  memcpy(address, dev->mac_address, sizeof(dev->mac_address));
403 
404  return JAYLINK_OK;
405 }
406 
426  const struct jaylink_device *dev,
427  struct jaylink_hardware_version *version)
428 {
429  if (!dev || !version)
430  return JAYLINK_ERR_ARG;
431 
432  if (dev->iface != JAYLINK_HIF_TCP)
434 
435  if (!dev->has_hw_version)
437 
438  *version = dev->hw_version;
439 
440  return JAYLINK_OK;
441 }
442 
460  const struct jaylink_device *dev, char *name)
461 {
462  if (!dev || !name)
463  return JAYLINK_ERR_ARG;
464 
465  if (dev->iface != JAYLINK_HIF_TCP)
467 
468  if (!dev->has_product_name)
470 
471  memcpy(name, dev->product_name, sizeof(dev->product_name));
472 
473  return JAYLINK_OK;
474 }
475 
493  char *nickname)
494 {
495  if (!dev || !nickname)
496  return JAYLINK_ERR_ARG;
497 
498  if (dev->iface != JAYLINK_HIF_TCP)
500 
501  if (!dev->has_nickname)
503 
504  memcpy(nickname, dev->nickname, sizeof(dev->nickname));
505 
506  return JAYLINK_OK;
507 }
508 
519  struct jaylink_device *dev)
520 {
521  if (!dev)
522  return NULL;
523 
524  dev->ref_count++;
525 
526  return dev;
527 }
528 
537 {
538  struct jaylink_context *ctx;
539 
540  if (!dev)
541  return;
542 
543  dev->ref_count--;
544 
545  if (!dev->ref_count) {
546  ctx = dev->ctx;
547  ctx->devs = list_remove(dev->ctx->devs, dev);
548 
549  if (dev->iface == JAYLINK_HIF_USB) {
550 #ifdef HAVE_LIBUSB
551  log_dbg(ctx, "Device destroyed (bus:address = "
552  "%03u:%03u).",
553  libusb_get_bus_number(dev->usb_dev),
554  libusb_get_device_address(dev->usb_dev));
555 
556  libusb_unref_device(dev->usb_dev);
557 #endif
558  } else if (dev->iface == JAYLINK_HIF_TCP) {
559  log_dbg(ctx, "Device destroyed (IPv4 address = %s).",
560  dev->ipv4_address);
561  } else {
562  log_err(ctx, "BUG: Invalid host interface: %u.",
563  dev->iface);
564  }
565 
566  free(dev);
567  }
568 }
569 
571  struct jaylink_device *dev)
572 {
573  struct jaylink_device_handle *devh;
574 
575  devh = malloc(sizeof(struct jaylink_device_handle));
576 
577  if (!devh)
578  return NULL;
579 
580  devh->dev = jaylink_ref_device(dev);
581 
582  return devh;
583 }
584 
586 {
587  jaylink_unref_device(devh->dev);
588  free(devh);
589 }
590 
608  struct jaylink_device_handle **devh)
609 {
610  int ret;
611  struct jaylink_device_handle *handle;
612 
613  if (!dev || !devh)
614  return JAYLINK_ERR_ARG;
615 
616  handle = allocate_device_handle(dev);
617 
618  if (!handle) {
619  log_err(dev->ctx, "Device handle malloc failed.");
620  return JAYLINK_ERR_MALLOC;
621  }
622 
623  ret = transport_open(handle);
624 
625  if (ret != JAYLINK_OK) {
626  free_device_handle(handle);
627  return ret;
628  }
629 
630  *devh = handle;
631 
632  return JAYLINK_OK;
633 }
634 
647 {
648  int ret;
649 
650  if (!devh)
651  return JAYLINK_ERR_ARG;
652 
653  ret = transport_close(devh);
654  free_device_handle(devh);
655 
656  return ret;
657 }
658 
671  struct jaylink_device_handle *devh)
672 {
673  if (!devh)
674  return NULL;
675 
676  return devh->dev;
677 }
678 
701  struct jaylink_device_handle *devh, char **version,
702  size_t *length)
703 {
704  int ret;
705  struct jaylink_context *ctx;
706  uint8_t buf[2];
707  uint16_t dummy;
708  char *tmp;
709 
710  if (!devh || !version || !length)
711  return JAYLINK_ERR_ARG;
712 
713  ctx = devh->dev->ctx;
714  ret = transport_start_write_read(devh, 1, 2, true);
715 
716  if (ret != JAYLINK_OK) {
717  log_err(ctx, "transport_start_write_read() failed: %s.",
718  jaylink_strerror(ret));
719  return ret;
720  }
721 
722  buf[0] = CMD_GET_VERSION;
723 
724  ret = transport_write(devh, buf, 1);
725 
726  if (ret != JAYLINK_OK) {
727  log_err(ctx, "transport_write() failed: %s.",
728  jaylink_strerror(ret));
729  return ret;
730  }
731 
732  ret = transport_read(devh, buf, 2);
733 
734  if (ret != JAYLINK_OK) {
735  log_err(ctx, "transport_read() failed: %s.",
736  jaylink_strerror(ret));
737  return ret;
738  }
739 
740  dummy = buffer_get_u16(buf, 0);
741  *length = dummy;
742 
743  if (!dummy)
744  return JAYLINK_OK;
745 
746  ret = transport_start_read(devh, dummy);
747 
748  if (ret != JAYLINK_OK) {
749  log_err(ctx, "transport_start_read() failed: %s.",
750  jaylink_strerror(ret));
751  return ret;
752  }
753 
754  tmp = malloc(dummy);
755 
756  if (!tmp) {
757  log_err(ctx, "Firmware version string malloc failed.");
758  return JAYLINK_ERR_MALLOC;
759  }
760 
761  ret = transport_read(devh, (uint8_t *)tmp, dummy);
762 
763  if (ret != JAYLINK_OK) {
764  log_err(ctx, "transport_read() failed: %s.",
765  jaylink_strerror(ret));
766  free(tmp);
767  return ret;
768  }
769 
770  /* Last byte is reserved for null-terminator. */
771  tmp[dummy - 1] = 0;
772  *version = tmp;
773 
774  return JAYLINK_OK;
775 }
776 
802  uint32_t mask, uint32_t *info)
803 {
804  int ret;
805  struct jaylink_context *ctx;
806  uint8_t buf[5];
807  unsigned int num;
808  unsigned int length;
809 
810  if (!devh || !mask || !info)
811  return JAYLINK_ERR_ARG;
812 
813  ctx = devh->dev->ctx;
814  num = 0;
815 
816  for (unsigned int i = 0; i < 32; i++) {
817  if (mask & (1 << i))
818  num++;
819  }
820 
821  length = num * sizeof(uint32_t);
822 
823  ret = transport_start_write_read(devh, 5, length, true);
824 
825  if (ret != JAYLINK_OK) {
826  log_err(ctx, "transport_start_write_read() failed: %s.",
827  jaylink_strerror(ret));
828  return ret;
829  }
830 
831  buf[0] = CMD_GET_HW_INFO;
832  buffer_set_u32(buf, mask, 1);
833 
834  ret = transport_write(devh, buf, 5);
835 
836  if (ret != JAYLINK_OK) {
837  log_err(ctx, "transport_write() failed: %s.",
838  jaylink_strerror(ret));
839  return ret;
840  }
841 
842  ret = transport_read(devh, (uint8_t *)info, length);
843 
844  if (ret != JAYLINK_OK) {
845  log_err(ctx, "transport_read() failed: %s.",
846  jaylink_strerror(ret));
847  return ret;
848  }
849 
850  for (unsigned int i = 0; i < num; i++)
851  info[i] = buffer_get_u32((uint8_t *)info,
852  i * sizeof(uint32_t));
853 
854  return JAYLINK_OK;
855 }
856 
881  uint32_t mask, uint32_t *values)
882 {
883  int ret;
884  struct jaylink_context *ctx;
885  uint8_t buf[5];
886  unsigned int num;
887  unsigned int length;
888 
889  if (!devh || !mask || !values)
890  return JAYLINK_ERR_ARG;
891 
892  ctx = devh->dev->ctx;
893  num = 0;
894 
895  for (unsigned int i = 0; i < 32; i++) {
896  if (mask & (1 << i))
897  num++;
898  }
899 
900  length = num * sizeof(uint32_t);
901  ret = transport_start_write_read(devh, 5, length, true);
902 
903  if (ret != JAYLINK_OK) {
904  log_err(ctx, "transport_start_write_read() failed: %s.",
905  jaylink_strerror(ret));
906  return ret;
907  }
908 
909  buf[0] = CMD_GET_COUNTERS;
910  buffer_set_u32(buf, mask, 1);
911 
912  ret = transport_write(devh, buf, 5);
913 
914  if (ret != JAYLINK_OK) {
915  log_err(ctx, "transport_write() failed: %s.",
916  jaylink_strerror(ret));
917  return ret;
918  }
919 
920  ret = transport_read(devh, (uint8_t *)values, length);
921 
922  if (ret != JAYLINK_OK) {
923  log_err(ctx, "transport_read() failed: %s.",
924  jaylink_strerror(ret));
925  return ret;
926  }
927 
928  for (unsigned int i = 0; i < num; i++)
929  values[i] = buffer_get_u32((uint8_t *)values,
930  i * sizeof(uint32_t));
931 
932  return JAYLINK_OK;
933 }
934 
957  struct jaylink_device_handle *devh,
958  struct jaylink_hardware_version *version)
959 {
960  int ret;
961  struct jaylink_context *ctx;
962  uint8_t buf[4];
963  uint32_t tmp;
964 
965  if (!devh || !version)
966  return JAYLINK_ERR_ARG;
967 
968  ctx = devh->dev->ctx;
969  ret = transport_start_write_read(devh, 1, 4, true);
970 
971  if (ret != JAYLINK_OK) {
972  log_err(ctx, "transport_start_write_read() failed: %s.",
973  jaylink_strerror(ret));
974  return ret;
975  }
976 
977  buf[0] = CMD_GET_HW_VERSION;
978 
979  ret = transport_write(devh, buf, 1);
980 
981  if (ret != JAYLINK_OK) {
982  log_err(ctx, "transport_write() failed: %s.",
983  jaylink_strerror(ret));
984  return ret;
985  }
986 
987  ret = transport_read(devh, buf, 4);
988 
989  if (ret != JAYLINK_OK) {
990  log_err(ctx, "transport_read() failed: %s.",
991  jaylink_strerror(ret));
992  return ret;
993  }
994 
995  tmp = buffer_get_u32(buf, 0);
996 
997  version->type = (tmp / 1000000) % 100;
998  version->major = (tmp / 10000) % 100;
999  version->minor = (tmp / 100) % 100;
1000  version->revision = tmp % 100;
1001 
1002  return JAYLINK_OK;
1003 }
1004 
1020  struct jaylink_hardware_status *status)
1021 {
1022  int ret;
1023  struct jaylink_context *ctx;
1024  uint8_t buf[8];
1025 
1026  if (!devh || !status)
1027  return JAYLINK_ERR_ARG;
1028 
1029  ctx = devh->dev->ctx;
1030  ret = transport_start_write_read(devh, 1, 8, true);
1031 
1032  if (ret != JAYLINK_OK) {
1033  log_err(ctx, "transport_start_write_read() failed: %s.",
1034  jaylink_strerror(ret));
1035  return ret;
1036  }
1037 
1038  buf[0] = CMD_GET_HW_STATUS;
1039 
1040  ret = transport_write(devh, buf, 1);
1041 
1042  if (ret != JAYLINK_OK) {
1043  log_err(ctx, "transport_write() failed: %s.",
1044  jaylink_strerror(ret));
1045  return ret;
1046  }
1047 
1048  ret = transport_read(devh, buf, 8);
1049 
1050  if (ret != JAYLINK_OK) {
1051  log_err(ctx, "transport_read() failed: %s.",
1052  jaylink_strerror(ret));
1053  return ret;
1054  }
1055 
1056  status->target_voltage = buffer_get_u16(buf, 0);
1057  status->tck = buf[2];
1058  status->tdi = buf[3];
1059  status->tdo = buf[4];
1060  status->tms = buf[5];
1061  status->tres = buf[6];
1062  status->trst = buf[7];
1063 
1064  return JAYLINK_OK;
1065 }
1066 
1095  uint8_t *caps)
1096 {
1097  int ret;
1098  struct jaylink_context *ctx;
1099  uint8_t buf[1];
1100 
1101  if (!devh || !caps)
1102  return JAYLINK_ERR_ARG;
1103 
1104  ctx = devh->dev->ctx;
1105  ret = transport_start_write_read(devh, 1, JAYLINK_DEV_CAPS_SIZE, true);
1106 
1107  if (ret != JAYLINK_OK) {
1108  log_err(ctx, "transport_start_write_read() failed: %s.",
1109  jaylink_strerror(ret));
1110  return ret;
1111  }
1112 
1113  buf[0] = CMD_GET_CAPS;
1114 
1115  ret = transport_write(devh, buf, 1);
1116 
1117  if (ret != JAYLINK_OK) {
1118  log_err(ctx, "transport_write() failed: %s.",
1119  jaylink_strerror(ret));
1120  return ret;
1121  }
1122 
1123  ret = transport_read(devh, caps, JAYLINK_DEV_CAPS_SIZE);
1124 
1125  if (ret != JAYLINK_OK) {
1126  log_err(ctx, "transport_read() failed: %s.",
1127  jaylink_strerror(ret));
1128  return ret;
1129  }
1130 
1131  return JAYLINK_OK;
1132 }
1133 
1162  uint8_t *caps)
1163 {
1164  int ret;
1165  struct jaylink_context *ctx;
1166  uint8_t buf[1];
1167 
1168  if (!devh || !caps)
1169  return JAYLINK_ERR_ARG;
1170 
1171  ctx = devh->dev->ctx;
1173  true);
1174 
1175  if (ret != JAYLINK_OK) {
1176  log_err(ctx, "transport_start_write_read() failed: %s.",
1177  jaylink_strerror(ret));
1178  return ret;
1179  }
1180 
1181  buf[0] = CMD_GET_EXT_CAPS;
1182 
1183  ret = transport_write(devh, buf, 1);
1184 
1185  if (ret != JAYLINK_OK) {
1186  log_err(ctx, "transport_write() failed: %s.",
1187  jaylink_strerror(ret));
1188  return ret;
1189  }
1190 
1191  ret = transport_read(devh, caps, JAYLINK_DEV_EXT_CAPS_SIZE);
1192 
1193  if (ret != JAYLINK_OK) {
1194  log_err(ctx, "transport_read() failed: %s.",
1195  jaylink_strerror(ret));
1196  return ret;
1197  }
1198 
1199  return JAYLINK_OK;
1200 }
1201 
1221  uint32_t *size)
1222 {
1223  int ret;
1224  struct jaylink_context *ctx;
1225  uint8_t buf[4];
1226 
1227  if (!devh || !size)
1228  return JAYLINK_ERR_ARG;
1229 
1230  ctx = devh->dev->ctx;
1231  ret = transport_start_write_read(devh, 1, 4, true);
1232 
1233  if (ret != JAYLINK_OK) {
1234  log_err(ctx, "transport_start_write_read() failed: %s.",
1235  jaylink_strerror(ret));
1236  return ret;
1237  }
1238 
1239  buf[0] = CMD_GET_FREE_MEMORY;
1240 
1241  ret = transport_write(devh, buf, 1);
1242 
1243  if (ret != JAYLINK_OK) {
1244  log_err(ctx, "transport_write() failed: %s.",
1245  jaylink_strerror(ret));
1246  return ret;
1247  }
1248 
1249  ret = transport_read(devh, buf, 4);
1250 
1251  if (ret != JAYLINK_OK) {
1252  log_err(ctx, "transport_read() failed: %s.",
1253  jaylink_strerror(ret));
1254  return ret;
1255  }
1256 
1257  *size = buffer_get_u32(buf, 0);
1258 
1259  return JAYLINK_OK;
1260 }
1261 
1283  uint8_t *config)
1284 {
1285  int ret;
1286  struct jaylink_context *ctx;
1287  uint8_t buf[1];
1288 
1289  if (!devh || !config)
1290  return JAYLINK_ERR_ARG;
1291 
1292  ctx = devh->dev->ctx;
1294  true);
1295 
1296  if (ret != JAYLINK_OK) {
1297  log_err(ctx, "transport_start_write_read() failed: %s.",
1298  jaylink_strerror(ret));
1299  return ret;
1300  }
1301 
1302  buf[0] = CMD_READ_CONFIG;
1303 
1304  ret = transport_write(devh, buf, 1);
1305 
1306  if (ret != JAYLINK_OK) {
1307  log_err(ctx, "transport_write() failed: %s.",
1308  jaylink_strerror(ret));
1309  return ret;
1310  }
1311 
1312  ret = transport_read(devh, config, JAYLINK_DEV_CONFIG_SIZE);
1313 
1314  if (ret != JAYLINK_OK) {
1315  log_err(ctx, "transport_read() failed: %s.",
1316  jaylink_strerror(ret));
1317  return ret;
1318  }
1319 
1320  return JAYLINK_OK;
1321 }
1322 
1343  const uint8_t *config)
1344 {
1345  int ret;
1346  struct jaylink_context *ctx;
1347  uint8_t buf[1];
1348 
1349  if (!devh || !config)
1350  return JAYLINK_ERR_ARG;
1351 
1352  ctx = devh->dev->ctx;
1353  ret = transport_start_write(devh, 1 + JAYLINK_DEV_CONFIG_SIZE, true);
1354 
1355  if (ret != JAYLINK_OK) {
1356  log_err(ctx, "transport_start_write() failed: %s.",
1357  jaylink_strerror(ret));
1358  return ret;
1359  }
1360 
1361  buf[0] = CMD_WRITE_CONFIG;
1362 
1363  ret = transport_write(devh, buf, 1);
1364 
1365  if (ret != JAYLINK_OK) {
1366  log_err(ctx, "transport_write() failed: %s.",
1367  jaylink_strerror(ret));
1368  return ret;
1369  }
1370 
1371  ret = transport_write(devh, config, JAYLINK_DEV_CONFIG_SIZE);
1372 
1373  if (ret != JAYLINK_OK) {
1374  log_err(ctx, "transport_write() failed: %s.",
1375  jaylink_strerror(ret));
1376  return ret;
1377  }
1378 
1379  return JAYLINK_OK;
1380 }
1381 
1382 static void parse_conn_table(struct jaylink_connection *conns,
1383  const uint8_t *buffer, uint16_t num, uint16_t entry_size)
1384 {
1385  size_t offset;
1386  struct in_addr in;
1387 
1388  offset = 0;
1389 
1390  for (unsigned int i = 0; i < num; i++) {
1391  conns[i].pid = buffer_get_u32(buffer, offset);
1392 
1393  in.s_addr = buffer_get_u32(buffer, offset + 4);
1394  /*
1395  * Use inet_ntoa() instead of inet_ntop() because the latter
1396  * requires at least Windows Vista.
1397  */
1398  strcpy(conns[i].hid, inet_ntoa(in));
1399 
1400  conns[i].iid = buffer[offset + 8];
1401  conns[i].cid = buffer[offset + 9];
1402  conns[i].handle = buffer_get_u16(buffer, offset + 10);
1403  conns[i].timestamp = buffer_get_u32(buffer, offset + 12);
1404  offset = offset + entry_size;
1405  }
1406 }
1407 
1408 static bool _inet_pton(const char *str, struct in_addr *in)
1409 {
1410 #ifdef _WIN32
1411  int ret;
1412  struct sockaddr_in sock_in;
1413  int length;
1414 
1415  length = sizeof(sock_in);
1416 
1417  /*
1418  * Use WSAStringToAddress() instead of inet_pton() because the latter
1419  * requires at least Windows Vista.
1420  */
1421  ret = WSAStringToAddress((LPTSTR)str, AF_INET, NULL,
1422  (LPSOCKADDR)&sock_in, &length);
1423 
1424  if (ret != 0)
1425  return false;
1426 
1427  *in = sock_in.sin_addr;
1428 #else
1429  if (inet_pton(AF_INET, str, in) != 1)
1430  return false;
1431 #endif
1432 
1433  return true;
1434 }
1435 
1517  struct jaylink_connection *connections, size_t *count)
1518 {
1519  int ret;
1520  struct jaylink_context *ctx;
1521  uint8_t buf[REG_MAX_SIZE];
1522  uint16_t handle;
1523  uint16_t num;
1524  uint16_t entry_size;
1525  uint32_t size;
1526  uint32_t table_size;
1527  uint16_t info_size;
1528  struct in_addr in;
1529 
1530  if (!devh || !connection || !connections || !count)
1531  return JAYLINK_ERR_ARG;
1532 
1533  ctx = devh->dev->ctx;
1534 
1535  buf[0] = CMD_REGISTER;
1536  buf[1] = REG_CMD_REGISTER;
1537  buffer_set_u32(buf, connection->pid, 2);
1538 
1539  if (!_inet_pton(connection->hid, &in))
1540  return JAYLINK_ERR_ARG;
1541 
1542  buffer_set_u32(buf, in.s_addr, 6);
1543 
1544  buf[10] = connection->iid;
1545  buf[11] = connection->cid;
1546  buffer_set_u16(buf, connection->handle, 12);
1547 
1548  ret = transport_start_write_read(devh, 14, REG_MIN_SIZE, true);
1549 
1550  if (ret != JAYLINK_OK) {
1551  log_err(ctx, "transport_start_write_read() failed: %s.",
1552  jaylink_strerror(ret));
1553  return ret;
1554  }
1555 
1556  ret = transport_write(devh, buf, 14);
1557 
1558  if (ret != JAYLINK_OK) {
1559  log_err(ctx, "transport_write() failed: %s.",
1560  jaylink_strerror(ret));
1561  return ret;
1562  }
1563 
1564  ret = transport_read(devh, buf, REG_MIN_SIZE);
1565 
1566  if (ret != JAYLINK_OK) {
1567  log_err(ctx, "transport_read() failed: %s.",
1568  jaylink_strerror(ret));
1569  return ret;
1570  }
1571 
1572  handle = buffer_get_u16(buf, 0);
1573  num = buffer_get_u16(buf, 2);
1574  entry_size = buffer_get_u16(buf, 4);
1575  info_size = buffer_get_u16(buf, 6);
1576 
1577  if (num > JAYLINK_MAX_CONNECTIONS) {
1578  log_err(ctx, "Maximum number of device connections exceeded: "
1579  "%u.", num);
1580  return JAYLINK_ERR_PROTO;
1581  }
1582 
1583  if (entry_size != REG_CONN_INFO_SIZE) {
1584  log_err(ctx, "Invalid connection entry size: %u bytes.",
1585  entry_size);
1586  return JAYLINK_ERR_PROTO;
1587  }
1588 
1589  table_size = num * entry_size;
1590  size = REG_HEADER_SIZE + table_size + info_size;
1591 
1592  if (size > REG_MAX_SIZE) {
1593  log_err(ctx, "Maximum registration information size exceeded: "
1594  "%u bytes.", size);
1595  return JAYLINK_ERR_PROTO;
1596  }
1597 
1598  if (size > REG_MIN_SIZE) {
1599  ret = transport_start_read(devh, size - REG_MIN_SIZE);
1600 
1601  if (ret != JAYLINK_OK) {
1602  log_err(ctx, "transport_start_read() failed: %s.",
1603  jaylink_strerror(ret));
1604  return JAYLINK_ERR;
1605  }
1606 
1607  ret = transport_read(devh, buf + REG_MIN_SIZE,
1608  size - REG_MIN_SIZE);
1609 
1610  if (ret != JAYLINK_OK) {
1611  log_err(ctx, "transport_read() failed: %s.",
1612  jaylink_strerror(ret));
1613  return JAYLINK_ERR;
1614  }
1615  }
1616 
1617  if (!handle) {
1618  log_err(ctx, "Obtained invalid connection handle.");
1619  return JAYLINK_ERR_PROTO;
1620  }
1621 
1622  connection->handle = handle;
1623  parse_conn_table(connections, buf + REG_HEADER_SIZE, num, entry_size);
1624 
1625  *count = num;
1626 
1627  return JAYLINK_OK;
1628 }
1629 
1657  const struct jaylink_connection *connection,
1658  struct jaylink_connection *connections, size_t *count)
1659 {
1660  int ret;
1661  struct jaylink_context *ctx;
1662  uint8_t buf[REG_MAX_SIZE];
1663  uint16_t num;
1664  uint16_t entry_size;
1665  uint32_t size;
1666  uint32_t table_size;
1667  uint16_t info_size;
1668  struct in_addr in;
1669 
1670  if (!devh || !connection || !connections || !count)
1671  return JAYLINK_ERR_ARG;
1672 
1673  ctx = devh->dev->ctx;
1674 
1675  buf[0] = CMD_REGISTER;
1676  buf[1] = REG_CMD_UNREGISTER;
1677  buffer_set_u32(buf, connection->pid, 2);
1678 
1679  if (!_inet_pton(connection->hid, &in))
1680  return JAYLINK_ERR_ARG;
1681 
1682  buffer_set_u32(buf, in.s_addr, 6);
1683 
1684  buf[10] = connection->iid;
1685  buf[11] = connection->cid;
1686  buffer_set_u16(buf, connection->handle, 12);
1687 
1688  ret = transport_start_write_read(devh, 14, REG_MIN_SIZE, true);
1689 
1690  if (ret != JAYLINK_OK) {
1691  log_err(ctx, "transport_start_write_read() failed: %s.",
1692  jaylink_strerror(ret));
1693  return ret;
1694  }
1695 
1696  ret = transport_write(devh, buf, 14);
1697 
1698  if (ret != JAYLINK_OK) {
1699  log_err(ctx, "transport_write() failed: %s.",
1700  jaylink_strerror(ret));
1701  return ret;
1702  }
1703 
1704  ret = transport_read(devh, buf, REG_MIN_SIZE);
1705 
1706  if (ret != JAYLINK_OK) {
1707  log_err(ctx, "transport_read() failed: %s.",
1708  jaylink_strerror(ret));
1709  return ret;
1710  }
1711 
1712  num = buffer_get_u16(buf, 2);
1713  entry_size = buffer_get_u16(buf, 4);
1714  info_size = buffer_get_u16(buf, 6);
1715 
1716  if (num > JAYLINK_MAX_CONNECTIONS) {
1717  log_err(ctx, "Maximum number of device connections exceeded: "
1718  "%u.", num);
1719  return JAYLINK_ERR_PROTO;
1720  }
1721 
1722  if (entry_size != REG_CONN_INFO_SIZE) {
1723  log_err(ctx, "Invalid connection entry size: %u bytes.",
1724  entry_size);
1725  return JAYLINK_ERR_PROTO;
1726  }
1727 
1728  table_size = num * entry_size;
1729  size = REG_HEADER_SIZE + table_size + info_size;
1730 
1731  if (size > REG_MAX_SIZE) {
1732  log_err(ctx, "Maximum registration information size exceeded: "
1733  "%u bytes.", size);
1734  return JAYLINK_ERR_PROTO;
1735  }
1736 
1737  if (size > REG_MIN_SIZE) {
1738  ret = transport_start_read(devh, size - REG_MIN_SIZE);
1739 
1740  if (ret != JAYLINK_OK) {
1741  log_err(ctx, "transport_start_read() failed: %s.",
1742  jaylink_strerror(ret));
1743  return JAYLINK_ERR;
1744  }
1745 
1746  ret = transport_read(devh, buf + REG_MIN_SIZE,
1747  size - REG_MIN_SIZE);
1748 
1749  if (ret != JAYLINK_OK) {
1750  log_err(ctx, "transport_read() failed: %s.",
1751  jaylink_strerror(ret));
1752  return JAYLINK_ERR;
1753  }
1754  }
1755 
1756  parse_conn_table(connections, buf + REG_HEADER_SIZE, num, entry_size);
1757 
1758  *count = num;
1759 
1760  return JAYLINK_OK;
1761 }
#define CMD_GET_VERSION
Definition: arm-jtag-ew.c:40
int mask
Definition: esirisc.c:1709
JAYLINK_API int jaylink_get_free_memory(struct jaylink_device_handle *devh, uint32_t *size)
Retrieve the size of free memory of a device.
Definition: device.c:1220
const char * name
Definition: armv4_5.c:87
JAYLINK_API int jaylink_register(struct jaylink_device_handle *devh, struct jaylink_connection *connection, struct jaylink_connection *connections, size_t *count)
Register a connection on a device.
Definition: device.c:1515
JAYLINK_API int jaylink_device_get_usb_address(const struct jaylink_device *dev, enum jaylink_usb_address *address)
Get the USB address of a device.
Definition: device.c:269
static libusb_device ** devs
The usb device list.
Definition: libusb_helper.c:34
JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset)
Read a 32-bit unsigned integer value from a buffer.
Definition: buffer.c:122
uint32_t offset
Definition: arm_cti.c:179
JAYLINK_API int jaylink_get_caps(struct jaylink_device_handle *devh, uint8_t *caps)
Retrieve the capabilities of a device.
Definition: device.c:1094
JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value, size_t offset)
Write a 16-bit unsigned integer value to a buffer.
Definition: buffer.c:43
JAYLINK_API int jaylink_get_hardware_version(struct jaylink_device_handle *devh, struct jaylink_hardware_version *version)
Retrieve the hardware version of a device.
Definition: device.c:956
JAYLINK_API int jaylink_device_get_mac_address(const struct jaylink_device *dev, uint8_t *address)
Get the MAC address of a device.
Definition: device.c:390
static struct ublast_lowlevel_priv info
JAYLINK_API int jaylink_device_get_hardware_version(const struct jaylink_device *dev, struct jaylink_hardware_version *version)
Get the hardware version of a device.
Definition: device.c:425
JAYLINK_API struct jaylink_device * jaylink_ref_device(struct jaylink_device *dev)
Increment the reference count of a device.
Definition: device.c:518
JAYLINK_API int jaylink_device_get_usb_bus_ports(const struct jaylink_device *dev, uint8_t *bus, uint8_t **ports, size_t *length)
Get the USB bus and port numbers of a device.
Definition: device.c:308
JAYLINK_API int jaylink_get_firmware_version(struct jaylink_device_handle *devh, char **version, size_t *length)
Retrieve the firmware version of a device.
Definition: device.c:700
JAYLINK_API int jaylink_write_raw_config(struct jaylink_device_handle *devh, const uint8_t *config)
Write the raw configuration data of a device.
Definition: device.c:1342
JAYLINK_API int jaylink_get_hardware_status(struct jaylink_device_handle *devh, struct jaylink_hardware_status *status)
Retrieve the hardware status of a device.
Definition: device.c:1019
static struct jaylink_device ** allocate_device_list(size_t length)
Definition: device.c:103
JAYLINK_API int jaylink_device_get_ipv4_address(const struct jaylink_device *dev, char *address)
Get the IPv4 address string of a device.
Definition: device.c:360
static void parse_conn_table(struct jaylink_connection *conns, const uint8_t *buffer, uint16_t num, uint16_t entry_size)
Definition: device.c:1382
JAYLINK_API int jaylink_device_get_product_name(const struct jaylink_device *dev, char *name)
Get the product name of a device.
Definition: device.c:459
JAYLINK_API int jaylink_unregister(struct jaylink_device_handle *devh, const struct jaylink_connection *connection, struct jaylink_connection *connections, size_t *count)
Unregister a connection from a device.
Definition: device.c:1656
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:37
JAYLINK_API int jaylink_device_get_nickname(const struct jaylink_device *dev, char *nickname)
Get the nickname of a device.
Definition: device.c:492
static void free_device_handle(struct jaylink_device_handle *devh)
Definition: device.c:585
JAYLINK_API int jaylink_device_get_host_interface(const struct jaylink_device *dev, enum jaylink_host_interface *iface)
Get the host interface of a device.
Definition: device.c:208
JAYLINK_API const char * jaylink_strerror(int error_code)
Return a human-readable description of a libjaylink error code.
Definition: error.c:40
static struct jaylink_device_handle * allocate_device_handle(struct jaylink_device *dev)
Definition: device.c:570
JAYLINK_API int jaylink_close(struct jaylink_device_handle *devh)
Close a device.
Definition: device.c:646
JAYLINK_API void jaylink_unref_device(struct jaylink_device *dev)
Decrement the reference count of a device.
Definition: device.c:536
static int count
Definition: helper/log.c:62
JAYLINK_API struct jaylink_device * jaylink_get_device(struct jaylink_device_handle *devh)
Get the device instance from a device handle.
Definition: device.c:670
JAYLINK_API int jaylink_get_hardware_info(struct jaylink_device_handle *devh, uint32_t mask, uint32_t *info)
Retrieve the hardware information of a device.
Definition: device.c:801
JAYLINK_API int jaylink_get_counters(struct jaylink_device_handle *devh, uint32_t mask, uint32_t *values)
Retrieve the counter values of a device.
Definition: device.c:880
JAYLINK_API int jaylink_device_get_serial_number(const struct jaylink_device *dev, uint32_t *serial_number)
Get the serial number of a device.
Definition: device.c:236
#define NULL
Definition: usb.h:27
static bool _inet_pton(const char *str, struct in_addr *in)
Definition: device.c:1408
JAYLINK_API int jaylink_get_extended_caps(struct jaylink_device_handle *devh, uint8_t *caps)
Retrieve the extended capabilities of a device.
Definition: device.c:1161
struct list * next
JAYLINK_API void jaylink_free_devices(struct jaylink_device **devs, bool unref)
Free devices.
Definition: device.c:183
JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset)
Read a 16-bit unsigned integer value from a buffer.
Definition: buffer.c:68
JAYLINK_API int jaylink_read_raw_config(struct jaylink_device_handle *devh, uint8_t *config)
Read the raw configuration data of a device.
Definition: device.c:1282
JAYLINK_API int jaylink_open(struct jaylink_device *dev, struct jaylink_device_handle **devh)
Open a device.
Definition: device.c:607
JAYLINK_API int jaylink_get_devices(struct jaylink_context *ctx, struct jaylink_device ***devs, size_t *count)
Get available devices.
Definition: device.c:137
JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value, size_t offset)
Write a 32-bit unsigned integer value to a buffer.
Definition: buffer.c:95