|
| 1 | +/* *************************************************************************** |
| 2 | + * |
| 3 | + * Copyright 2019-2021 Samsung Electronics All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 14 | + * either express or implied. See the License for the specific |
| 15 | + * language governing permissions and limitations under the License. |
| 16 | + * |
| 17 | + ****************************************************************************/ |
| 18 | + |
| 19 | +#include <string.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | + |
| 23 | +#include "st_dev.h" |
| 24 | +#include "caps_mode.h" |
| 25 | + |
| 26 | +static const char **caps_mode_get_supportedModes_value(caps_mode_data_t *caps_data) |
| 27 | +{ |
| 28 | + if (!caps_data) { |
| 29 | + printf("caps_data is NULL\n"); |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + return (const char **)caps_data->supportedModes_value; |
| 33 | +} |
| 34 | + |
| 35 | +static void caps_mode_set_supportedModes_value(caps_mode_data_t *caps_data, const char **value, int arraySize) |
| 36 | +{ |
| 37 | + int i; |
| 38 | + if (!caps_data) { |
| 39 | + printf("caps_data is NULL\n"); |
| 40 | + return; |
| 41 | + } |
| 42 | + if (caps_data->supportedModes_value) { |
| 43 | + for (i = 0; i < caps_data->supportedModes_arraySize; i++) { |
| 44 | + free(caps_data->supportedModes_value[i]); |
| 45 | + } |
| 46 | + free(caps_data->supportedModes_value); |
| 47 | + } |
| 48 | + caps_data->supportedModes_value = malloc(sizeof(char *) * arraySize); |
| 49 | + if (!caps_data->supportedModes_value) { |
| 50 | + printf("fail to malloc for supportedModes_value\n"); |
| 51 | + caps_data->supportedModes_arraySize = 0; |
| 52 | + return; |
| 53 | + } |
| 54 | + for (i = 0; i < arraySize; i++) { |
| 55 | + caps_data->supportedModes_value[i] = strdup(value[i]); |
| 56 | + } |
| 57 | + |
| 58 | + caps_data->supportedModes_arraySize = arraySize; |
| 59 | +} |
| 60 | + |
| 61 | +static void caps_mode_attr_supportedModes_send(caps_mode_data_t *caps_data) |
| 62 | +{ |
| 63 | + int sequence_no = -1; |
| 64 | + |
| 65 | + if (!caps_data || !caps_data->handle) { |
| 66 | + printf("fail to get handle\n"); |
| 67 | + return; |
| 68 | + } |
| 69 | + if (!caps_data->supportedModes_value) { |
| 70 | + printf("value is NULL\n"); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + ST_CAP_SEND_ATTR_STRINGS_ARRAY(caps_data->handle, |
| 75 | + (char *)caps_helper_mode.attr_supportedModes.name, |
| 76 | + caps_data->supportedModes_value, |
| 77 | + caps_data->supportedModes_arraySize, |
| 78 | + NULL, |
| 79 | + NULL, |
| 80 | + sequence_no); |
| 81 | + |
| 82 | + if (sequence_no < 0) |
| 83 | + printf("fail to send supportedModes value\n"); |
| 84 | + else |
| 85 | + printf("Sequence number return : %d\n", sequence_no); |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +static const char *caps_mode_get_mode_value(caps_mode_data_t *caps_data) |
| 90 | +{ |
| 91 | + if (!caps_data) { |
| 92 | + printf("caps_data is NULL\n"); |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + return caps_data->mode_value; |
| 96 | +} |
| 97 | + |
| 98 | +static void caps_mode_set_mode_value(caps_mode_data_t *caps_data, const char *value) |
| 99 | +{ |
| 100 | + if (!caps_data) { |
| 101 | + printf("caps_data is NULL\n"); |
| 102 | + return; |
| 103 | + } |
| 104 | + if (caps_data->mode_value) { |
| 105 | + free(caps_data->mode_value); |
| 106 | + } |
| 107 | + caps_data->mode_value = strdup(value); |
| 108 | +} |
| 109 | + |
| 110 | +static void caps_mode_attr_mode_send(caps_mode_data_t *caps_data) |
| 111 | +{ |
| 112 | + int sequence_no = -1; |
| 113 | + |
| 114 | + if (!caps_data || !caps_data->handle) { |
| 115 | + printf("fail to get handle\n"); |
| 116 | + return; |
| 117 | + } |
| 118 | + if (!caps_data->mode_value) { |
| 119 | + printf("value is NULL\n"); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + ST_CAP_SEND_ATTR_STRING(caps_data->handle, |
| 124 | + (char *)caps_helper_mode.attr_mode.name, |
| 125 | + caps_data->mode_value, |
| 126 | + NULL, |
| 127 | + NULL, |
| 128 | + sequence_no); |
| 129 | + |
| 130 | + if (sequence_no < 0) |
| 131 | + printf("fail to send mode value\n"); |
| 132 | + else |
| 133 | + printf("Sequence number return : %d\n", sequence_no); |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +static void caps_mode_cmd_setMode_cb(IOT_CAP_HANDLE *handle, iot_cap_cmd_data_t *cmd_data, void *usr_data) |
| 138 | +{ |
| 139 | + caps_mode_data_t *caps_data = (caps_mode_data_t *)usr_data; |
| 140 | + char *value; |
| 141 | + |
| 142 | + printf("called [%s] func with num_args:%u\n", __func__, cmd_data->num_args); |
| 143 | + |
| 144 | + value = cmd_data->cmd_data[0].string; |
| 145 | + |
| 146 | + caps_mode_set_mode_value(caps_data, value); |
| 147 | + if (caps_data && caps_data->cmd_setMode_usr_cb) |
| 148 | + caps_data->cmd_setMode_usr_cb(caps_data); |
| 149 | + caps_mode_attr_mode_send(caps_data); |
| 150 | +} |
| 151 | + |
| 152 | +static void caps_mode_init_cb(IOT_CAP_HANDLE *handle, void *usr_data) |
| 153 | +{ |
| 154 | + caps_mode_data_t *caps_data = usr_data; |
| 155 | + if (caps_data && caps_data->init_usr_cb) |
| 156 | + caps_data->init_usr_cb(caps_data); |
| 157 | + caps_mode_attr_supportedModes_send(caps_data); |
| 158 | + caps_mode_attr_mode_send(caps_data); |
| 159 | +} |
| 160 | + |
| 161 | +caps_mode_data_t *caps_mode_initialize(IOT_CTX *ctx, const char *component, void *init_usr_cb, void *usr_data) |
| 162 | +{ |
| 163 | + caps_mode_data_t *caps_data = NULL; |
| 164 | + int err; |
| 165 | + |
| 166 | + caps_data = malloc(sizeof(caps_mode_data_t)); |
| 167 | + if (!caps_data) { |
| 168 | + printf("fail to malloc for caps_mode_data\n"); |
| 169 | + return NULL; |
| 170 | + } |
| 171 | + |
| 172 | + memset(caps_data, 0, sizeof(caps_mode_data_t)); |
| 173 | + |
| 174 | + caps_data->init_usr_cb = init_usr_cb; |
| 175 | + caps_data->usr_data = usr_data; |
| 176 | + |
| 177 | + caps_data->get_supportedModes_value = caps_mode_get_supportedModes_value; |
| 178 | + caps_data->set_supportedModes_value = caps_mode_set_supportedModes_value; |
| 179 | + caps_data->attr_supportedModes_send = caps_mode_attr_supportedModes_send; |
| 180 | + caps_data->get_mode_value = caps_mode_get_mode_value; |
| 181 | + caps_data->set_mode_value = caps_mode_set_mode_value; |
| 182 | + caps_data->attr_mode_send = caps_mode_attr_mode_send; |
| 183 | + if (ctx) { |
| 184 | + caps_data->handle = st_cap_handle_init(ctx, component, caps_helper_mode.id, caps_mode_init_cb, caps_data); |
| 185 | + } |
| 186 | + if (caps_data->handle) { |
| 187 | + err = st_cap_cmd_set_cb(caps_data->handle, caps_helper_mode.cmd_setMode.name, caps_mode_cmd_setMode_cb, caps_data); |
| 188 | + if (err) { |
| 189 | + printf("fail to set cmd_cb for setMode of mode\n"); |
| 190 | + } |
| 191 | + } else { |
| 192 | + printf("fail to init mode handle\n"); |
| 193 | + } |
| 194 | + |
| 195 | + return caps_data; |
| 196 | +} |
0 commit comments