diff --git a/gdigi.c b/gdigi.c index 772aad0..a125a4f 100644 --- a/gdigi.c +++ b/gdigi.c @@ -403,6 +403,92 @@ void append_value(GString *msg, guint value) } } +/** + * \param str pointer to value to unpack + * \param len return location for how many bytes value is encoded on (length is added to current value) + * + * Unpacks value using scheme used on all newer DigiTech products. + * + * \return unpacked value + **/ +guint unpack_value(gchar *str, int *len) +{ + guint value; + gint tmp; + + value = (unsigned char)str[0]; + *len += 1; + + if (value > 0x80) { + tmp = value & 0x7F; + value = 0; + gint i; + for (i = 0; iid = -1; + param->position = -1; + param->value = -1; + + return param; +} + +/** + * \param str pointer to setting param in message + * \param len return location for how many bytes value is encoded on (length is added to current value) + * + * Creates SettingParam basing on data. + * This function expects str to point on: + * -Parameter ID - 2 bytes + * -Parameter position - 1 byte + * -Parameter value - var + * + * \return newly created SettingParam which must be freed using setting_param_free. + **/ +SettingParam *setting_param_new_from_data(gchar *str, gint *len) +{ + gint id; + gint position; + guint value; + + id = ((unsigned char)str[0] << 8) | (unsigned char)str[1]; + position = (unsigned char)str[2]; + *len += 3; + + value = unpack_value(&str[3], len); + + SettingParam *param = g_slice_new(SettingParam); + param->id = id; + param->position = position; + param->value = value; + + return param; +} + +/** + * \param param SettingParam to be freed + * + * Frees all memory used by SettingParam. + **/ +void setting_param_free(SettingParam *param) +{ + g_slice_free(SettingParam, param); +} + /** * \param id Parameter ID * \param position Parameter position diff --git a/gdigi.h b/gdigi.h index 2a3ee5c..2cfde1c 100644 --- a/gdigi.h +++ b/gdigi.h @@ -682,9 +682,18 @@ typedef enum { NACK = 0x7F } MessageID; +typedef struct { + int id; + int position; + int value; +} SettingParam; + void send_message(gint procedure, gchar *data, gint len); void append_value(GString *msg, guint value); GString *get_message_by_id(MessageID id); +SettingParam *setting_param_new(); +SettingParam *setting_param_new_from_data(gchar *str, gint *len); +void setting_param_free(SettingParam *param); void set_option(guint id, guint position, guint value); void switch_preset(guint bank, guint x); void store_preset_name(int x, const gchar *name); diff --git a/preset.c b/preset.c index c3099c8..e4928c6 100644 --- a/preset.c +++ b/preset.c @@ -18,6 +18,7 @@ #include #include #include "preset.h" +#include "gdigi.h" #ifndef DOXYGEN_SHOULD_SKIP_THIS @@ -53,10 +54,7 @@ static void XMLCALL start(void *data, const char *el, const char **attr) { if (ad->preset->params != NULL) g_message("Params aleady exists!"); } else if (g_strcmp0(el, "Param") == 0) { - SettingParam *param = g_slice_new(SettingParam); - param->id = -1; - param->position = -1; - param->value = -1; + SettingParam *param = setting_param_new(); ad->preset->params = g_list_prepend(ad->preset->params, param); } else if (g_strcmp0(el, "ID") == 0) { ad->id = PARSER_TYPE_PARAM_ID; @@ -185,11 +183,7 @@ Preset *create_preset_from_data(GString *data) { gint total; gint n; - gint id; - gint position; - guint value; gint x; - gint tmp; g_return_val_if_fail(data != NULL, NULL); @@ -203,27 +197,10 @@ Preset *create_preset_from_data(GString *data) preset->params = NULL; do { - id = ((unsigned char)data->str[x] << 8) | (unsigned char)data->str[x+1]; - position = (unsigned char)data->str[x+2]; - x+=3; - value = data->str[x]; - x++; - if (value > 0x80) { - tmp = value & 0x7F; - value = 0; - gint i; - for (i=0; istr[x+i] << (8*(tmp-i-1))); - } - x+=tmp; - } + SettingParam *param = setting_param_new_from_data(&data->str[x], &x); n++; - SettingParam *param = g_slice_new(SettingParam); - param->id = id; - param->position = position; - param->value = value; preset->params = g_list_prepend(preset->params, param); - g_message("%d ID %d Position %d Value %d", n, id, position, value); + g_message("%d ID %d Position %d Value %d", n, param->id, param->position, param->value); } while ((x < data->len) && nparams = g_list_reverse(preset->params); @@ -243,7 +220,7 @@ void preset_free(Preset *preset) if (preset->params != NULL) { GList *iter; for (iter = preset->params; iter; iter = iter->next) { - g_slice_free(SettingParam, iter->data); + setting_param_free((SettingParam*)iter->data); } g_list_free(preset->params); } diff --git a/preset.h b/preset.h index d8b110f..0141d4f 100644 --- a/preset.h +++ b/preset.h @@ -19,12 +19,6 @@ #include -typedef struct { - int id; - int position; - int value; -} SettingParam; - typedef struct { gchar *name; GList *params;