Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c
  2. index bbf68d1..bf65c5e 100644
  3. --- a/subsys/net/lib/lwm2m/lwm2m_engine.c
  4. +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c
  5. @@ -110,7 +110,9 @@ static sys_slist_t engine_observer_list;
  6. /* TODO: figure out what's correct value */
  7. #define TIMEOUT_BLOCKWISE_TRANSFER K_SECONDS(30)
  8.  
  9. -#define GET_BLOCK_NUM(v) ((v) >> 4)
  10. +#define GET_BLOCK_NUM(v) ((v) >> 4)
  11. +#define GET_BLOCK_SIZE(v) (((v) & 0x7))
  12. +#define GET_MORE(v) (!!((v) & 0x08))
  13.  
  14. struct block_context {
  15. struct zoap_block_context ctx;
  16. @@ -548,6 +550,20 @@ int lwm2m_delete_obj_inst(u16_t obj_id, u16_t obj_inst_id)
  17.  
  18. /* utility functions */
  19.  
  20. +static int get_option_int(const struct zoap_packet *zpkt, u8_t opt)
  21. +{
  22. + struct zoap_option option = {};
  23. + u16_t count = 1;
  24. + int r;
  25. +
  26. + r = zoap_find_options(zpkt, opt, &option, count);
  27. + if (r <= 0) {
  28. + return -ENOENT;
  29. + }
  30. +
  31. + return zoap_option_value_to_int(&option);
  32. +}
  33. +
  34. static void engine_clear_context(struct lwm2m_engine_context *context)
  35. {
  36. if (context->in) {
  37. @@ -1625,11 +1641,12 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
  38. void *data_ptr = NULL;
  39. size_t data_len = 0;
  40. size_t len = 0;
  41. + size_t total_size = 0;
  42. int ret = 0;
  43. - struct block_context *block_ctx = NULL;
  44. u8_t tkl = 0;
  45. const u8_t *token;
  46. bool last_block = true;
  47. + struct block_context *block_ctx = NULL;
  48.  
  49. if (!obj_inst || !res || !obj_field || !context) {
  50. return -EINVAL;
  51. @@ -1639,6 +1656,13 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
  52. data_ptr = res->data_ptr;
  53. data_len = res->data_len;
  54.  
  55. + /* setup data_ptr/data_len for OPAQUE when it has none setup */
  56. + if (obj_field->data_type == LWM2M_RES_TYPE_OPAQUE &&
  57. + data_ptr == NULL && data_len == 0) {
  58. + data_ptr = in->inbuf;
  59. + data_len = in->insize;
  60. + }
  61. +
  62. /* allow user to override data elements via callback */
  63. if (res->pre_write_cb) {
  64. data_ptr = res->pre_write_cb(obj_inst->obj_inst_id, &data_len);
  65. @@ -1731,14 +1755,22 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
  66. }
  67.  
  68. if (res->post_write_cb) {
  69. - token = zoap_header_get_token(in->in_zpkt, &tkl);
  70. - if (token != NULL && !get_block_ctx(token, tkl, &block_ctx)) {
  71. - last_block = block_ctx->ctx.current == 0;
  72. + /* Get block1 option for checking MORE block flag */
  73. + ret = get_option_int(in->in_zpkt, ZOAP_OPTION_BLOCK1);
  74. + if (ret >= 0) {
  75. + last_block = !GET_MORE(ret);
  76. +
  77. + /* Get block_ctx for total_size (might be zero) */
  78. + token = zoap_header_get_token(in->in_zpkt, &tkl);
  79. + if (token != NULL &&
  80. + !get_block_ctx(token, tkl, &block_ctx)) {
  81. + total_size = block_ctx->ctx.total_size;
  82. + }
  83. }
  84.  
  85. /* ignore return value here */
  86. ret = res->post_write_cb(obj_inst->obj_inst_id, data_ptr, len,
  87. - last_block, 0);
  88. + last_block, total_size);
  89. if (ret >= 0) {
  90. ret = 0;
  91. }
  92. @@ -2054,20 +2086,6 @@ static int do_write_op(struct lwm2m_engine_obj *obj,
  93. }
  94. }
  95.  
  96. -static int get_option_int(const struct zoap_packet *zpkt, u8_t opt)
  97. -{
  98. - struct zoap_option option = {};
  99. - u16_t count = 1;
  100. - int r;
  101. -
  102. - r = zoap_find_options(zpkt, opt, &option, count);
  103. - if (r <= 0) {
  104. - return -ENOENT;
  105. - }
  106. -
  107. - return zoap_option_value_to_int(&option);
  108. -}
  109. -
  110. static int handle_request(struct zoap_packet *request,
  111. struct zoap_packet *response,
  112. struct sockaddr *from_addr)
  113. @@ -2086,6 +2104,8 @@ static int handle_request(struct zoap_packet *request,
  114. int observe = -1; /* default to -1, 0 = ENABLE, 1 = DISABLE */
  115. bool discover = false;
  116. struct block_context *block_ctx = NULL;
  117. + size_t block_offset = 0;
  118. + enum zoap_block_size block_size;
  119.  
  120. /* setup engine context */
  121. memset(&context, 0, sizeof(struct lwm2m_engine_context));
  122. @@ -2201,6 +2221,15 @@ static int handle_request(struct zoap_packet *request,
  123. /* Check for block transfer */
  124. r = get_option_int(in.in_zpkt, ZOAP_OPTION_BLOCK1);
  125. if (r > 0) {
  126. + /* RFC7252: 4.6. Message Size */
  127. + block_size = GET_BLOCK_SIZE(r);
  128. + if (GET_MORE(r) &&
  129. + zoap_block_size_to_bytes(block_size) > in.insize) {
  130. + SYS_LOG_DBG("Trailing payload is discarded!");
  131. + r = -EFBIG;
  132. + goto error;
  133. + }
  134. +
  135. if (GET_BLOCK_NUM(r) == 0) {
  136. r = init_block_ctx(token, tkl, &block_ctx);
  137. } else {
  138. @@ -2211,7 +2240,8 @@ static int handle_request(struct zoap_packet *request,
  139. goto error;
  140. }
  141.  
  142. - zoap_next_block(in.in_zpkt, &block_ctx->ctx);
  143. + /* 0 will be returned if it's the last block */
  144. + block_offset = zoap_next_block(in.in_zpkt, &block_ctx->ctx);
  145. }
  146.  
  147. switch (context.operation) {
  148. @@ -2289,7 +2319,7 @@ static int handle_request(struct zoap_packet *request,
  149.  
  150. /* Handle blockwise 1 */
  151. if (block_ctx) {
  152. - if (block_ctx->ctx.current > 0) {
  153. + if (block_offset > 0) {
  154. /* More to come, ack with correspond block # */
  155. r = zoap_add_block1_option(
  156. out.out_zpkt, &block_ctx->ctx);
  157. @@ -2327,6 +2357,9 @@ error:
  158. } else if (r == -EEXIST) {
  159. zoap_header_set_code(out.out_zpkt,
  160. ZOAP_RESPONSE_CODE_BAD_REQUEST);
  161. + } else if (r == -EFBIG) {
  162. + zoap_header_set_code(out.out_zpkt,
  163. + ZOAP_RESPONSE_CODE_REQUEST_TOO_LARGE);
  164. } else {
  165. /* Failed to handle the request */
  166. zoap_header_set_code(out.out_zpkt,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement