1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18
19#define LOG_TAG "SupplicantEventFactory"
20#include <cutils/log.h>
21
22#include "SupplicantEvent.h"
23#include "SupplicantEventFactory.h"
24#include "SupplicantAssociatingEvent.h"
25#include "SupplicantAssociatedEvent.h"
26#include "SupplicantConnectedEvent.h"
27#include "SupplicantStateChangeEvent.h"
28#include "SupplicantScanResultsEvent.h"
29#include "SupplicantConnectionTimeoutEvent.h"
30#include "SupplicantDisconnectedEvent.h"
31#if 0
32#include "SupplicantTerminatingEvent.h"
33#include "SupplicantPasswordChangedEvent.h"
34#include "SupplicantEapNotificationEvent.h"
35#include "SupplicantEapStartedEvent.h"
36#include "SupplicantEapMethodEvent.h"
37#include "SupplicantEapSuccessEvent.h"
38#include "SupplicantEapFailureEvent.h"
39#include "SupplicantLinkSpeedEvent.h"
40#include "SupplicantDriverStateEvent.h"
41#endif
42
43#include "libwpa_client/wpa_ctrl.h"
44
45SupplicantEventFactory::SupplicantEventFactory() {
46}
47
48SupplicantEvent *SupplicantEventFactory::createEvent(char *event, size_t len) {
49    int level = 0;
50
51    if (event[0] == '<') {
52        char *match = strchr(event, '>');
53        if (match) {
54            char tmp[16];
55
56            strncpy(tmp, &event[1], (match - event));
57            level = atoi(tmp);
58            event += (match - event) + 1;
59        } else
60            LOGW("Unclosed level brace in event");
61    } else
62        LOGW("No level specified in event");
63
64    /*
65     * <N>CTRL-EVENT-XXX
66     *    ^
67     *    +---- event
68     */
69
70    if (!strncmp(event, "Authentication with ", 20)) {
71        if (!strcmp(event + strlen(event) - strlen(" timed out."),
72                    " timed out.")) {
73            return new SupplicantConnectionTimeoutEvent(level,
74                                                        event + 20,
75                                                        len);
76        } else
77            return NULL;
78
79    } else if (!strncmp(event, "Associated with ", 16))
80        return new SupplicantAssociatedEvent(level, event + 16, len);
81    else if (!strncmp(event, "Trying to associate with ", 25))
82        return new SupplicantAssociatingEvent(level, event + 25, len);
83    else if (!strncmp(event, WPA_EVENT_CONNECTED, strlen(WPA_EVENT_CONNECTED))) {
84        return new SupplicantConnectedEvent(level,
85                                            event + strlen(WPA_EVENT_CONNECTED),
86                                            len);
87    } else if (!strncmp(event, WPA_EVENT_SCAN_RESULTS, strlen(WPA_EVENT_SCAN_RESULTS))) {
88        return new SupplicantScanResultsEvent(level,
89                                              event + strlen(WPA_EVENT_SCAN_RESULTS),
90                                              len);
91    } else if (!strncmp(event, WPA_EVENT_STATE_CHANGE, strlen(WPA_EVENT_STATE_CHANGE))) {
92        return new SupplicantStateChangeEvent(level,
93                                              event + strlen(WPA_EVENT_STATE_CHANGE),
94                                              len);
95    }
96    else if (!strncmp(event, WPA_EVENT_DISCONNECTED, strlen(WPA_EVENT_DISCONNECTED)))
97        return new SupplicantDisconnectedEvent(level, event, len);
98#if 0
99    else if (!strncmp(event, WPA_EVENT_TERMINATING, strlen(WPA_EVENT_TERMINATING)))
100        return new SupplicantTerminatingEvent(event, len);
101    else if (!strncmp(event, WPA_EVENT_PASSWORD_CHANGED, strlen(WPA_EVENT_PASSWORD_CHANGED)))
102        return new SupplicantPasswordChangedEvent(event, len);
103    else if (!strncmp(event, WPA_EVENT_EAP_NOTIFICATION, strlen(WPA_EVENT_EAP_NOTIFICATION)))
104        return new SupplicantEapNotificationEvent(event, len);
105    else if (!strncmp(event, WPA_EVENT_EAP_STARTED, strlen(WPA_EVENT_EAP_STARTED)))
106        return new SupplicantEapStartedEvent(event, len);
107    else if (!strncmp(event, WPA_EVENT_EAP_METHOD, strlen(WPA_EVENT_EAP_METHOD)))
108        return new SupplicantEapMethodEvent(event, len);
109    else if (!strncmp(event, WPA_EVENT_EAP_SUCCESS, strlen(WPA_EVENT_EAP_SUCCESS)))
110        return new SupplicantEapSuccessEvent(event, len);
111    else if (!strncmp(event, WPA_EVENT_EAP_FAILURE, strlen(WPA_EVENT_EAP_FAILURE)))
112        return new SupplicantEapFailureEvent(event, len);
113    else if (!strncmp(event, WPA_EVENT_LINK_SPEED, strlen(WPA_EVENT_LINK_SPEED)))
114        return new SupplicantLinkSpeedEvent(event, len);
115    else if (!strncmp(event, WPA_EVENT_DRIVER_STATE, strlen(WPA_EVENT_DRIVER_STATE)))
116         return new SupplicantDriverStateEvent(event, len);
117#endif
118    return NULL;
119}
120