summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcd/src/plcd_files.c
blob: d877bc47c14bb1a499def5244afde89f91533efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Cleopatre project {{{
 *
 * Copyright (C) 2013 MStar Semiconductor
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    devkit/plcd/src/plcd_files.c
 * \ingroup plcd
 *
 * Manage read and write operation for :
 *  - HPAV.CONF
 *  - HPAV.INFO
 */
#include "plcd_files.h"
#include "plcd_ctx.h"

#include "libspid.h"

int
plcd_files_read_hpavconf (plcd_ctx_t *ctx)
{
    char is_cco_preferred_str[LIBSPID_BOOLEAN_STR_MAX_LEN];
    char was_cco_str[LIBSPID_BOOLEAN_STR_MAX_LEN];
    char force_role_str[LIBSPID_HPAV_CONF_FORCE_ROLE_MAX_LEN];
    libspid_error_t libspid_ret;

    PLCD_ASSERT (ctx);

    memset (is_cco_preferred_str, 0, sizeof (is_cco_preferred_str));
    memset (was_cco_str, 0, sizeof (was_cco_str));
    memset (force_role_str, 0, sizeof (force_role_str));

    /* Only NMK item is mandatory, other are optionals.
     * There is no default value possible. */
    if (LIBSPID_SUCCESS
        != libspid_config_read_item (
            ctx->hpav_conf_path,
            LIBSPID_HPAV_CONF_LABEL_NMK,
            ctx->hpav_conf.nmk_str,
            LIBSPID_HPAV_CONF_NMK_STR_LEN))
    {
        syslog (LOG_ERR, "libspid NMK read failed");

        return -1;
    }

    /* Optional items.
     * Be careful, amount of labels, values and lens must be sync. */
#define PLCD_HPAV_CONF_NB_ITEM 7
    const char * const hpav_conf_labels[PLCD_HPAV_CONF_NB_ITEM] = {
        LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED,
        LIBSPID_HPAV_CONF_LABEL_WAS_CCO,
        LIBSPID_HPAV_CONF_LABEL_NID,
        LIBSPID_HPAV_CONF_LABEL_SL,
        LIBSPID_HPAV_CONF_LABEL_USER_HFID,
        LIBSPID_HPAV_CONF_LABEL_AVLN_HFID,
        LIBSPID_HPAV_CONF_LABEL_FORCE_ROLE
    };
    char *hpav_conf_values[PLCD_HPAV_CONF_NB_ITEM] = {
        is_cco_preferred_str,
        was_cco_str,
        ctx->hpav_conf.nid_str,
        ctx->hpav_conf.sl_str,
        ctx->hpav_conf.user_hfid,
        ctx->hpav_conf.avln_hfid,
        force_role_str
    };
    const int hpav_conf_len[PLCD_HPAV_CONF_NB_ITEM] = {
        LIBSPID_BOOLEAN_STR_MAX_LEN,
        LIBSPID_BOOLEAN_STR_MAX_LEN,
        LIBSPID_HPAV_CONF_NID_STR_LEN,
        LIBSPID_HPAV_CONF_SL_STR_MAX_LEN,
        LIBSPID_HPAV_CONF_HFID_MAX_LEN,
        LIBSPID_HPAV_CONF_HFID_MAX_LEN,
        LIBSPID_HPAV_CONF_FORCE_ROLE_MAX_LEN
    };

    libspid_ret = libspid_config_read_items (
        ctx->hpav_conf_path, hpav_conf_labels, hpav_conf_values,
        hpav_conf_len, PLCD_HPAV_CONF_NB_ITEM);

    if (LIBSPID_ERROR_NOT_FOUND != libspid_ret
        && LIBSPID_SUCCESS != libspid_ret)
    {
        syslog (LOG_ERR, "libspid read optional items failed");
        return -1;
    }

    /* Interpret strings returned (or not) by libspid. */
    ctx->hpav_conf.is_cco_preferred = LIBSPID_GET_BOOLEAN (is_cco_preferred_str);
    ctx->hpav_conf.was_cco = LIBSPID_GET_BOOLEAN (was_cco_str);

    if (strlen (ctx->hpav_conf.nid_str) == 0)
        strncpy (ctx->hpav_conf.nid_str,
                 LIBSPID_VALUE_NONE,
                 LIBSPID_HPAV_CONF_NID_STR_LEN - 1);

    /* Set to default value (Simple Connect),
     * if any available value found. */
    if (strcmp (ctx->hpav_conf.sl_str, LIBSPID_HPAV_CONF_VALUE_SL_SC)
        && strcmp (ctx->hpav_conf.sl_str, LIBSPID_HPAV_CONF_VALUE_SL_HS))
        strncpy (ctx->hpav_conf.sl_str,
                 LIBSPID_HPAV_CONF_VALUE_SL_SC,
                 LIBSPID_HPAV_CONF_SL_STR_MAX_LEN - 1);

    /* No default value for user_hfid and avln_hfid. */

    /* Set force_role. Convert libspid string result to plcd enum value. */
    if (!strcmp (LIBSPID_HPAV_CONF_VALUE_FORCE_ROLE_CCO,
                 force_role_str))
        ctx->hpav_conf.force_role = PLCD_FORCE_ROLE_CCO;
    else if (!strcmp (LIBSPID_HPAV_CONF_VALUE_FORCE_ROLE_STA,
                      force_role_str))
        ctx->hpav_conf.force_role = PLCD_FORCE_ROLE_STA;
    else
        ctx->hpav_conf.force_role = PLCD_FORCE_ROLE_NONE;

    return 0;
}

int
plcd_files_write_default_hpavinfo (plcd_ctx_t *ctx)
{
    PLCD_ASSERT (ctx);

    /* Set these default values into PLCD context */
    strcpy (ctx->hpav_info.status, LIBSPID_HPAV_INFO_VALUE_STATUS_UNASSOCIATED);
    strcpy (ctx->hpav_info.cco, LIBSPID_HPAV_INFO_VALUE_CCO_STATION);
    ctx->hpav_info.is_backup_cco = LIBSPID_FALSE;
    ctx->hpav_info.is_sc = LIBSPID_FALSE;
    ctx->hpav_info.is_sc_button = LIBSPID_FALSE;

    /* Be careful, amount of labels and values must be sync. */
#define PLCD_HPAV_INFO_NB_ITEM 5

    /* Create a default hpav.info file with libspid. */
    const char * const hpav_info_labels[PLCD_HPAV_INFO_NB_ITEM] = {
        LIBSPID_HPAV_INFO_LABEL_STATUS,
        LIBSPID_HPAV_INFO_LABEL_CCO,
        LIBSPID_HPAV_INFO_LABEL_BACKUP_CCO,
        LIBSPID_HPAV_INFO_LABEL_SC,
        LIBSPID_HPAV_INFO_LABEL_SC_BUTTON
    };
    const char * const hpav_info_values[PLCD_HPAV_INFO_NB_ITEM] = {
        ctx->hpav_info.status,
        ctx->hpav_info.cco,
        LIBSPID_VALUE_BOOLEAN_FALSE,
        LIBSPID_VALUE_BOOLEAN_FALSE,
        LIBSPID_VALUE_BOOLEAN_FALSE
    };

    if (LIBSPID_SUCCESS != libspid_config_write_items (
            ctx->hpav_info_path,
            hpav_info_labels, hpav_info_values,
            PLCD_HPAV_INFO_NB_ITEM))
    {
        syslog (LOG_ERR,
                "libspid write items failed (file: %s)",
                ctx->hpav_info_path);
        return -1;
    }

    return 0;
}