Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | #include "metadata-defs.h" |
| 35 | |
| 36 | int parse_metadata(char *metadata, char **metadata_saveptr, |
Steve Kondik | 1921d30 | 2014-12-08 01:37:30 -0800 | [diff] [blame] | 37 | char *attribute, int attribute_size, char *value, |
| 38 | unsigned int value_size) |
Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 39 | { |
| 40 | char *attribute_string; |
| 41 | char *attribute_value_delim; |
| 42 | unsigned int bytes_to_copy; |
| 43 | |
| 44 | attribute_string = strtok_r(metadata, ATTRIBUTE_STRING_DELIM, |
| 45 | metadata_saveptr); |
| 46 | |
| 47 | if (attribute_string == NULL) |
| 48 | return METADATA_PARSING_DONE; |
| 49 | |
| 50 | attribute[0] = value[0] = '\0'; |
| 51 | |
| 52 | if ((attribute_value_delim = strchr(attribute_string, |
| 53 | ATTRIBUTE_VALUE_DELIM)) != NULL) { |
| 54 | bytes_to_copy = MIN((attribute_value_delim - attribute_string), |
| 55 | attribute_size - 1); |
| 56 | strncpy(attribute, attribute_string, |
| 57 | bytes_to_copy); |
| 58 | attribute[bytes_to_copy] = '\0'; |
| 59 | |
| 60 | bytes_to_copy = MIN(strlen(attribute_string) - strlen(attribute) - 1, |
| 61 | value_size - 1); |
| 62 | strncpy(value, attribute_value_delim + 1, |
| 63 | bytes_to_copy); |
| 64 | value[bytes_to_copy] = '\0'; |
| 65 | } |
| 66 | |
| 67 | return METADATA_PARSING_CONTINUE; |
| 68 | } |
| 69 | |
Dilip Gudlur | 032f125 | 2015-03-02 16:30:54 -0800 | [diff] [blame] | 70 | int parse_cam_preview_metadata(char *metadata, |
| 71 | struct cam_preview_metadata_t *cam_preview_metadata) |
| 72 | { |
| 73 | char attribute[1024], value[1024], *saveptr; |
| 74 | char *temp_metadata = metadata; |
| 75 | int parsing_status; |
| 76 | |
| 77 | while ((parsing_status = parse_metadata(temp_metadata, &saveptr, |
| 78 | attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) { |
| 79 | if (strlen(attribute) == strlen("hint_id") && |
| 80 | (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) { |
| 81 | if (strlen(value) > 0) { |
| 82 | cam_preview_metadata->hint_id = atoi(value); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (strlen(attribute) == strlen("state") && |
| 87 | (strncmp(attribute, "state", strlen("state")) == 0)) { |
| 88 | if (strlen(value) > 0) { |
| 89 | cam_preview_metadata->state = atoi(value); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | temp_metadata = NULL; |
| 94 | } |
| 95 | |
| 96 | if (parsing_status == METADATA_PARSING_ERR) |
| 97 | return -1; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 102 | int parse_video_encode_metadata(char *metadata, |
| 103 | struct video_encode_metadata_t *video_encode_metadata) |
| 104 | { |
| 105 | char attribute[1024], value[1024], *saveptr; |
| 106 | char *temp_metadata = metadata; |
| 107 | int parsing_status; |
| 108 | |
| 109 | while ((parsing_status = parse_metadata(temp_metadata, &saveptr, |
| 110 | attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) { |
| 111 | if (strlen(attribute) == strlen("hint_id") && |
| 112 | (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) { |
| 113 | if (strlen(value) > 0) { |
| 114 | video_encode_metadata->hint_id = atoi(value); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (strlen(attribute) == strlen("state") && |
| 119 | (strncmp(attribute, "state", strlen("state")) == 0)) { |
| 120 | if (strlen(value) > 0) { |
| 121 | video_encode_metadata->state = atoi(value); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | temp_metadata = NULL; |
| 126 | } |
| 127 | |
| 128 | if (parsing_status == METADATA_PARSING_ERR) |
| 129 | return -1; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int parse_video_decode_metadata(char *metadata, |
| 135 | struct video_decode_metadata_t *video_decode_metadata) |
| 136 | { |
| 137 | char attribute[1024], value[1024], *saveptr; |
| 138 | char *temp_metadata = metadata; |
| 139 | int parsing_status; |
| 140 | |
| 141 | while ((parsing_status = parse_metadata(temp_metadata, &saveptr, |
| 142 | attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) { |
| 143 | if (strlen(attribute) == strlen("hint_id") && |
| 144 | (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) { |
| 145 | if (strlen(value) > 0) { |
| 146 | video_decode_metadata->hint_id = atoi(value); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (strlen(attribute) == strlen("state") && |
| 151 | (strncmp(attribute, "state", strlen("state")) == 0)) { |
| 152 | if (strlen(value) > 0) { |
| 153 | video_decode_metadata->state = atoi(value); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | temp_metadata = NULL; |
| 158 | } |
| 159 | |
| 160 | if (parsing_status == METADATA_PARSING_ERR) |
| 161 | return -1; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int parse_audio_metadata(char *metadata, |
| 167 | struct audio_metadata_t *audio_metadata) |
| 168 | { |
| 169 | char attribute[1024], value[1024], *saveptr; |
| 170 | char *temp_metadata = metadata; |
| 171 | int parsing_status; |
| 172 | |
| 173 | while ((parsing_status = parse_metadata(temp_metadata, &saveptr, |
| 174 | attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) { |
| 175 | if (strlen(attribute) == strlen("hint_id") && |
| 176 | (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) { |
| 177 | if (strlen(value) > 0) { |
| 178 | audio_metadata->hint_id = atoi(value); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (strlen(attribute) == strlen("state") && |
| 183 | (strncmp(attribute, "state", strlen("state")) == 0)) { |
| 184 | if (strlen(value) > 0) { |
| 185 | audio_metadata->state = atoi(value); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | temp_metadata = NULL; |
| 190 | } |
| 191 | |
| 192 | if (parsing_status == METADATA_PARSING_ERR) |
| 193 | return -1; |
| 194 | |
| 195 | return 0; |
| 196 | } |