qmi-message.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * uqmi -- tiny QMI support implementation
  3. *
  4. * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301 USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include "qmi-message.h"
  25. static uint8_t buf[QMI_BUFFER_LEN];
  26. static unsigned int buf_ofs;
  27. uint8_t *__qmi_get_buf(unsigned int *ofs)
  28. {
  29. *ofs = buf_ofs;
  30. return buf;
  31. }
  32. void __qmi_alloc_reset(void)
  33. {
  34. buf_ofs = 0;
  35. }
  36. void *__qmi_alloc_static(unsigned int len)
  37. {
  38. void *ret;
  39. if (buf_ofs + len > sizeof(buf)) {
  40. fprintf(stderr, "ERROR: static buffer for message data too small\n");
  41. abort();
  42. }
  43. ret = &buf[buf_ofs];
  44. buf_ofs += len;
  45. memset(ret, 0, len);
  46. return ret;
  47. }
  48. char *__qmi_copy_string(void *data, unsigned int len)
  49. {
  50. char *res;
  51. res = (char *) &buf[buf_ofs];
  52. buf_ofs += len + 1;
  53. memcpy(res, data, len);
  54. res[len] = 0;
  55. return res;
  56. }
  57. struct tlv *tlv_get_next(void **buf, unsigned int *buflen)
  58. {
  59. struct tlv *tlv = NULL;
  60. unsigned int tlv_len;
  61. if (*buflen < sizeof(*tlv))
  62. return NULL;
  63. tlv = *buf;
  64. tlv_len = le16_to_cpu(tlv->len) + sizeof(*tlv);
  65. if (tlv_len > *buflen)
  66. return NULL;
  67. *buflen -= tlv_len;
  68. *buf += tlv_len;
  69. return tlv;
  70. }
  71. static struct tlv *qmi_msg_next_tlv(struct qmi_msg *qm, int add)
  72. {
  73. int tlv_len;
  74. void *tlv;
  75. if (qm->qmux.service == QMI_SERVICE_CTL) {
  76. tlv = qm->ctl.tlv;
  77. tlv_len = le16_to_cpu(qm->ctl.tlv_len);
  78. qm->ctl.tlv_len = cpu_to_le16(tlv_len + add);
  79. } else {
  80. tlv = qm->svc.tlv;
  81. tlv_len = le16_to_cpu(qm->svc.tlv_len);
  82. qm->svc.tlv_len = cpu_to_le16(tlv_len + add);
  83. }
  84. tlv += tlv_len;
  85. return tlv;
  86. }
  87. void tlv_new(struct qmi_msg *qm, uint8_t type, uint16_t len, void *data)
  88. {
  89. struct tlv *tlv;
  90. tlv = qmi_msg_next_tlv(qm, sizeof(*tlv) + len);
  91. tlv->type = type;
  92. tlv->len = cpu_to_le16(len);
  93. memcpy(tlv->data, data, len);
  94. }
  95. void qmi_init_request_message(struct qmi_msg *qm, QmiService service)
  96. {
  97. memset(qm, 0, sizeof(*qm));
  98. qm->marker = 1;
  99. qm->qmux.service = service;
  100. }
  101. int qmi_complete_request_message(struct qmi_msg *qm)
  102. {
  103. void *tlv_end = qmi_msg_next_tlv(qm, 0);
  104. void *msg_start = &qm->qmux;
  105. qm->qmux.len = cpu_to_le16(tlv_end - msg_start);
  106. return tlv_end - msg_start + 1;
  107. }
  108. int qmi_check_message_status(void *tlv_buf, unsigned int len)
  109. {
  110. struct tlv *tlv;
  111. struct {
  112. uint16_t status;
  113. uint16_t code;
  114. } __packed *status;
  115. while ((tlv = tlv_get_next(&tlv_buf, &len)) != NULL) {
  116. if (tlv->type != 2)
  117. continue;
  118. if (tlv_data_len(tlv) != sizeof(*status))
  119. return QMI_ERROR_INVALID_DATA;
  120. status = (void *) tlv->data;
  121. if (!status->status)
  122. return 0;
  123. return le16_to_cpu(status->code);
  124. }
  125. return QMI_ERROR_NO_DATA;
  126. }
  127. void *qmi_msg_get_tlv_buf(struct qmi_msg *qm, int *tlv_len)
  128. {
  129. void *ptr;
  130. int len;
  131. if (qm->qmux.service == QMI_SERVICE_CTL) {
  132. ptr = qm->ctl.tlv;
  133. len = qm->ctl.tlv_len;
  134. } else {
  135. ptr = qm->svc.tlv;
  136. len = qm->svc.tlv_len;
  137. }
  138. if (tlv_len)
  139. *tlv_len = len;
  140. return ptr;
  141. }