Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_socket_help.c Source File

arm_uc_socket_help.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "update-client-common/arm_uc_config.h"
00020 
00021 #if defined(ARM_UC_FEATURE_FW_SOURCE_HTTP) && (ARM_UC_FEATURE_FW_SOURCE_HTTP == 1)
00022 
00023 #include "arm_uc_socket_help.h"
00024 
00025 #include <string.h>
00026 
00027 /**
00028  * @brief Helper function for picking out values from an HTTP header.
00029  * @details The buffer is searched for the provided tag and if found the
00030  *          remainder of that line is MOVED to the front of the buffer and the
00031  *          size is shrunk to only include the detected value.
00032  *
00033  * @param buffer Pointer to an arm_uc_buffer_t.
00034  * @param tag_buffer Pointer to tag (a string).
00035  * @param tag_size Length of the tag.
00036  * @return True if tag was found. False otherwise.
00037  */
00038 bool arm_uc_http_socket_trim_value(arm_uc_buffer_t *buffer,
00039                                    const char *tag_buffer,
00040                                    uint32_t tag_size)
00041 {
00042     /* default return value */
00043     bool result = false;
00044 
00045     /* check for NULL pointers */
00046     if (buffer && buffer->ptr && tag_buffer) {
00047         /* search for the tag */
00048         uint32_t start = arm_uc_strnstrn(buffer->ptr,
00049                                          buffer->size,
00050                                          (const uint8_t *) tag_buffer,
00051                                          tag_size);
00052 
00053         /* check if index is within bounds */
00054         if (start < buffer->size) {
00055             /* remove tag */
00056             start += tag_size;
00057 
00058             /* remove ": " between tag and value */
00059             start += 2;
00060 
00061             /* search for the end of the line */
00062             uint32_t length = arm_uc_strnstrn(&(buffer->ptr[start]),
00063                                               buffer->size - start,
00064                                               (const uint8_t *) "\r",
00065                                               1);
00066 
00067             /* move value to front of buffer if all indices are within bounds */
00068             if ((start + length) < buffer->size) {
00069                 /* memmove handles overlapping regions */
00070                 memmove(buffer->ptr, &(buffer->ptr[start]), length);
00071 
00072                 /* resize buffer */
00073                 buffer->size = length;
00074 
00075                 /* set return value to success */
00076                 result = true;
00077             }
00078         }
00079     }
00080 
00081     return result;
00082 }
00083 
00084 #endif // ARM_UC_FEATURE_FW_SOURCE_HTTP
00085