00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <config/net_list_content.h>
00025
00026 #include <netcomm/utils/dynamic_buffer.h>
00027 #include <netcomm/fawkes/component_ids.h>
00028 #include <core/exceptions/software.h>
00029 #include <cstdlib>
00030 #include <cstring>
00031
00032 namespace fawkes {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 ConfigListContent::ConfigListContent()
00045 {
00046 config_list = new DynamicBuffer(&(msg.config_list));
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 ConfigListContent::ConfigListContent(unsigned int component_id,
00058 unsigned int msg_id,
00059 void *payload, size_t payload_size)
00060 {
00061 if ( component_id != FAWKES_CID_CONFIGMANAGER ) {
00062 throw TypeMismatchException("ConfigListContent: invalid component ID");
00063 }
00064 config_list_msg_t *tmsg = (config_list_msg_t *)payload;
00065 void *config_list_payload = (void *)((size_t)payload + sizeof(msg));
00066 config_list = new DynamicBuffer(&(tmsg->config_list), config_list_payload,
00067 payload_size - sizeof(msg));
00068 }
00069
00070
00071
00072 ConfigListContent::~ConfigListContent()
00073 {
00074 delete config_list;
00075 if (_payload != NULL) {
00076 free(_payload);
00077 _payload = NULL;
00078 _payload_size = 0;
00079 }
00080 }
00081
00082
00083
00084
00085
00086
00087 void
00088 ConfigListContent::append(Configuration::ValueIterator *i)
00089 {
00090 if ( i->is_float() ) {
00091 append_float(i->path(), i->get_float(), i->is_default());
00092 } else if ( i->is_int() ) {
00093 append_int(i->path(), i->get_int(), i->is_default());
00094 } else if ( i->is_uint() ) {
00095 append_uint(i->path(), i->get_uint(), i->is_default());
00096 } else if ( i->is_bool() ) {
00097 append_bool(i->path(), i->get_bool(), i->is_default());
00098 } else if ( i->is_string() ) {
00099 append_string(i->path(), i->get_string().c_str(), i->is_default());
00100 } else {
00101 throw TypeMismatchException("Invalid type of config iterator value");
00102 }
00103
00104 std::string comment = i->get_comment();
00105 if (comment != "") {
00106 append_comment(i->path(), comment.c_str(), i->is_default());
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115 void
00116 ConfigListContent::append_float(const char *path, float f, bool def_val)
00117 {
00118 config_list_float_entity_t cle;
00119 memset(&cle, 0, sizeof(cle));
00120 strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00121 cle.header.type = MSG_CONFIG_FLOAT_VALUE;
00122 cle.header.cp.is_default = (def_val ? 1 : 0);
00123 cle.f = f;
00124 config_list->append(&cle, sizeof(cle));
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 void
00134 ConfigListContent::append_int(const char *path, int i, bool def_val)
00135 {
00136 config_list_int_entity_t cle;
00137 memset(&cle, 0, sizeof(cle));
00138 strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00139 cle.header.type = MSG_CONFIG_INT_VALUE;
00140 cle.header.cp.is_default = (def_val ? 1 : 0);
00141 cle.i = i;
00142 config_list->append(&cle, sizeof(cle));
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 void
00152 ConfigListContent::append_uint(const char *path, unsigned int u, bool def_val)
00153 {
00154 config_list_uint_entity_t cle;
00155 memset(&cle, 0, sizeof(cle));
00156 strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00157 cle.header.type = MSG_CONFIG_UINT_VALUE;
00158 cle.header.cp.is_default = (def_val ? 1 : 0);
00159 cle.u = u;
00160 config_list->append(&cle, sizeof(cle));
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 void
00170 ConfigListContent::append_bool(const char *path, bool b, bool def_val)
00171 {
00172 config_list_bool_entity_t cle;
00173 memset(&cle, 0, sizeof(cle));
00174 strncpy(cle.header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00175 cle.header.type = MSG_CONFIG_BOOL_VALUE;
00176 cle.header.cp.is_default = (def_val ? 1 : 0);
00177 cle.b = b;
00178 config_list->append(&cle, sizeof(cle));
00179 }
00180
00181
00182
00183
00184
00185
00186
00187 void
00188 ConfigListContent::append_string(const char *path, const char *s, bool def_val)
00189 {
00190 size_t s_length = strlen(s);
00191 size_t sl = sizeof(config_list_string_entity_t) + s_length;
00192 config_list_string_entity_t *cle = (config_list_string_entity_t *)calloc(1, sl);
00193 strncpy(cle->header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00194 cle->header.type = MSG_CONFIG_STRING_VALUE;
00195 cle->header.cp.is_default = (def_val ? 1 : 0);
00196 cle->s_length = s_length;
00197 strcpy(cle->s, s);
00198 config_list->append(cle, sl);
00199 free(cle);
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 void
00209 ConfigListContent::append_comment(const char *path, const char *s, bool def_val)
00210 {
00211 size_t s_length = strlen(s);
00212 size_t sl = sizeof(config_list_string_entity_t) + s_length;
00213 config_list_comment_entity_t *cle = (config_list_comment_entity_t *)calloc(1, sl);
00214 strncpy(cle->header.cp.path, path, CONFIG_MSG_PATH_LENGTH);
00215 cle->header.type = MSG_CONFIG_COMMENT_VALUE;
00216 cle->header.cp.is_default = (def_val ? 1 : 0);
00217 cle->s_length = s_length;
00218 strcpy(cle->s, s);
00219 config_list->append(cle, sl);
00220 free(cle);
00221 }
00222
00223
00224 void
00225 ConfigListContent::serialize()
00226 {
00227 _payload_size = sizeof(msg) + config_list->buffer_size();
00228 _payload = malloc(_payload_size);
00229 copy_payload(0, &msg, sizeof(msg));
00230 copy_payload(sizeof(msg), config_list->buffer(), config_list->buffer_size());
00231 }
00232
00233
00234
00235
00236
00237 void
00238 ConfigListContent::reset_iterator()
00239 {
00240 config_list->reset_iterator();
00241 }
00242
00243
00244
00245
00246
00247
00248 bool
00249 ConfigListContent::has_next()
00250 {
00251 return config_list->has_next();
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261 config_list_entity_header_t *
00262 ConfigListContent::next(size_t *size)
00263 {
00264 void *tmp = config_list->next(size);
00265 return (config_list_entity_header_t *)tmp;
00266 }
00267
00268 }