netio.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <aos/list.h>
  7. #include <aos/kernel.h>
  8. #include <yoc/netio.h>
  9. #include <ulog/ulog.h>
  10. #define TAG "fota"
  11. typedef struct fota_netio_list {
  12. slist_t next;
  13. const netio_cls_t *cls;
  14. } netio_node_t;
  15. static AOS_SLIST_HEAD(netio_cls_list);
  16. int netio_register(const netio_cls_t *cls)
  17. {
  18. netio_node_t *node = aos_malloc(sizeof(netio_node_t));
  19. if (node) {
  20. node->cls = cls;
  21. slist_add_tail(&node->next, &netio_cls_list);
  22. return 0;
  23. }
  24. return -1;
  25. }
  26. netio_t *netio_open(const char *path)
  27. {
  28. netio_t *io = NULL;
  29. char *delim = strstr(path, "://");
  30. LOGD(TAG, "path:%s delim:%s\n", path, delim);
  31. if (delim) {
  32. int len = delim - path;
  33. netio_node_t *node;
  34. if (strstr(path, "https")) {
  35. len -= 1;
  36. }
  37. slist_for_each_entry(&netio_cls_list, node, netio_node_t, next) {
  38. if (strncmp(node->cls->name, path, len) == 0) {
  39. if (node->cls->open) {
  40. io = aos_zalloc(sizeof(netio_t));
  41. io->cls = node->cls;
  42. if (io->cls->open(io, path) < 0) {
  43. aos_free(io);
  44. LOGD(TAG, "open fail\n");
  45. return NULL;
  46. }
  47. }
  48. LOGD(TAG, "open break,%s\n", node->cls->name);
  49. break;
  50. }
  51. }
  52. }
  53. return io;
  54. }
  55. int netio_close(netio_t *io)
  56. {
  57. int ret = -1;
  58. if (io->cls->close) {
  59. ret = io->cls->close(io);
  60. aos_free(io);
  61. }
  62. return ret;
  63. }
  64. int netio_read(netio_t *io, uint8_t *buffer, size_t lenght, int timeoutms)
  65. {
  66. if (io->cls->read)
  67. return io->cls->read(io, buffer, lenght, timeoutms);
  68. return -1;
  69. }
  70. int netio_write(netio_t *io, uint8_t *buffer, size_t lenght, int timeoutms)
  71. {
  72. if (io->cls->write)
  73. return io->cls->write(io, buffer, lenght, timeoutms);
  74. return -1;
  75. }
  76. int netio_seek(netio_t *io, size_t offset, int whence)
  77. {
  78. if (io->cls->seek)
  79. return io->cls->seek(io, offset, whence);
  80. return -1;
  81. }