summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/p1905_managerd/src/p1905_ap_autoconfig.c
blob: 884131df5f04821369ebb355dd2b9f8f001590e5 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * cleopatre/application/p1905_managerd/src/p1905_ap_autoconfig.c
 *
 * (C) Copyright 2013 MSsar Semiconductor, Inc.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include "p1905_managerd.h"
#include "p1905_ap_autoconfig.h"
#include "cmdu_message.h"
#include "libspid.h"

//#define P1905_AP_AUTOCONFIG_DEBUG
#ifdef P1905_AP_AUTOCONFIG_DEBUG
#define debug(...)          printf(__VA_ARGS__)
#define debug_syslog(...)        printf(__VA_ARGS__)
#else
#define debug(...)
#define debug_syslog(format, ...)   syslog(LOG_WARNING, format, ##__VA_ARGS__)
#endif

#define AUTO_CONFIG_TIMEOUT_CNT 3//15 seconds(5 seconds per count)

extern const unsigned char p1905_multicast_address[6];

void ap_autoconfig_init(struct p1905_managerd_ctx *ctx)
{
#if defined(SUPPORT_AP_ENROLLE)
    unsigned char conf_status;
    debug_syslog("AP is an enrollee\n");

    ctx->enrolle_state = no_ap_autoconfig;
    ctx->ap_config_data = NULL;
    ctx->last_tx_data = NULL;
    ctx->current_rx_data = NULL;
    ctx->is_authenticator_exist_in_M2 = 0;
    ctx->is_in_encrypt_settings = 0;
    ctx->get_config_attr_kind = 0;

    /* check wsc configured status.
     * if this enrollee is not configured, send ap auto config search
     */
    ctx->need_ap_config = 0;
    if(wifi_utils_success == get_wsc_conf_status(&conf_status))
    {
        if(conf_status == AP_NOT_CONFIGURED)
            ctx->need_ap_config = 1;
    }
#elif defined(SUPPORT_AP_REGISTRAR)
    /* if it is ap registrar, check the WIFI_CFG_CHANGE in /etc/web_reset.info
     * if get value=yes, need to send ap auto config renew
     */
    unsigned char status[5] = {0};
    libspid_error_t ret;
    debug_syslog("AP is an registrar\n");

    if(LIBSPID_SUCCESS == libspid_config_read_item(LIBSPID_WEB_RESET_INFO_PATH,
        LIBSPID_WEB_RESET_INFO_LABEL_WIFI_CFG_CHANGE, status, 5))
    {
        if(!strcmp(status, LIBSPID_WEB_RESET_INFO_RESET_BY_WEB_YES))
        {
            ctx->need_ap_config = 1;
            ret = libspid_config_write_item (LIBSPID_WEB_RESET_INFO_PATH,
                                LIBSPID_WEB_RESET_INFO_LABEL_WIFI_CFG_CHANGE,
                                LIBSPID_WEB_RESET_INFO_RESET_BY_WEB_NO);
            if (LIBSPID_SUCCESS == ret)
                libspid_system_save_file (LIBSPID_WEB_RESET_INFO_PATH);
        }
    }

    ctx->last_rx_data = NULL;
#endif
    ctx->ap_config = NULL;
    get_uuid(ctx->uuid);
}

void ap_autoconfig_free_memory(struct p1905_managerd_ctx *ctx)
{


}

#ifdef SUPPORT_AP_ENROLLE
void ap_autoconfig_enrolle_sm(struct p1905_managerd_ctx *ctx)
{
    WSC_CONFIG config_data;

    switch(ctx->enrolle_state)
    {
        case wait_4_send_ap_autoconfig_search:
            /* if PLC is not authenticated, break!
             * this function will be called periodically by timer handler
             * so system will check PLC status periodically
             */
            if(!is_connection_status_authenticated())
            {
                debug("cannot send auto config search message\n");    
                break;
            }
            //debug_syslog("enrollee send ap config search\n");
            ctx->mid++;
            ctx->autoconfig_search_mid = ctx->mid;
            insert_cmdu_txq(p1905_multicast_address,ctx->plc0_mac_addr,\
                              e_ap_autoconfiguration_search, ctx->mid);
            insert_cmdu_txq(p1905_multicast_address,ctx->eth0_mac_addr,\
                              e_ap_autoconfiguration_search, ctx->mid);
            ctx->enrolle_state = wait_4_recv_ap_autoconfig_resp;
            ctx->ap_config_timeout_cnt = 0;
            break;

        case wait_4_recv_ap_autoconfig_resp:
            /*check timeout*/
            if(ctx->ap_config_timeout_cnt > AUTO_CONFIG_TIMEOUT_CNT)
            {
                /* ap auto config timeout. Set state to no_ap_autoconfig
                   re-trigger ap autoconfig search process.
                 */
                ctx->enrolle_state = no_ap_autoconfig;
                debug_syslog("enrollee wait_4_recv_ap_autoconfig_resp timeout\n");
            }
            break;

        case wait_4_recv_m2:
            /*allocate memory for wifi config use*/
            if(!ctx->ap_config_data)
                ctx->ap_config_data = (WSC_CONFIG *)malloc(sizeof(WSC_CONFIG));
            /*check timeout*/
            /*if timeout happen, need to free ctx->ap_config_data*/
            if(ctx->ap_config_timeout_cnt > AUTO_CONFIG_TIMEOUT_CNT)
            {
                 debug_syslog("enrollee wait_4_recv_m2 timeout\n");

                /* ap auto config timeout. Set state to no_ap_autoconfig
                   re-trigger ap autoconfig search process.
                 */
                ctx->enrolle_state = no_ap_autoconfig;
                if(ctx->ap_config_data != NULL)
                {
                    free(ctx->ap_config_data);
                    ctx->ap_config_data = NULL;
                }
                if(ctx->ap_config != NULL)
                {
                    free(ctx->ap_config);
                    ctx->ap_config = NULL;
                }
                if(ctx->last_tx_data != NULL)
                {
                    free(ctx->last_tx_data);
                    ctx->last_tx_data = NULL;
                }
            }
            break;

        case wait_4_set_config:
            if(ctx->ap_config_data != NULL)
            {
                if(wifi_utils_success != set_wsc_config(ctx->ap_config_data))
                {
                    debug_syslog("set wsc config error\n");
                }

                /*test setting*/
                get_wsc_config(&config_data);
                debug_syslog("after key = %s\n",config_data.WPAKey);
                debug_syslog("after SSID = %s\n",config_data.Ssid);
                debug_syslog("after Auth = 0x%.4x\n",config_data.AuthMode);
                debug_syslog("after Encry = 0x%.4x\n",config_data.EncrypType);

                if(wifi_utils_success != update_wifi_config_file(ctx->ap_config_data))
                {
                    debug_syslog("ap auto-config write config file error\n");
                }
                /*no need to do ap autoconfig anymore*/
                ctx->need_ap_config = 0;
                /*set the configured statue of local wifi device*/
                set_wsc_conf_status(AP_ALREADY_CONFIGURED);

                free(ctx->ap_config_data);
                ctx->ap_config_data = NULL;
            }
            ctx->enrolle_state = no_ap_autoconfig;
            break;
    }
}
#endif

#ifdef SUPPORT_AP_REGISTRAR
void ap_auto_config_renew_start(struct p1905_managerd_ctx *ctx)
{
    ctx->mid++;
    insert_cmdu_txq(p1905_multicast_address,ctx->plc0_mac_addr,\
                      e_ap_autoconfiguration_renew, ctx->mid);
    insert_cmdu_txq(p1905_multicast_address,ctx->eth0_mac_addr,\
                      e_ap_autoconfiguration_renew, ctx->mid);
}
#endif