1 /* 2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited 3 */ 4 5 #ifndef __ZCONFIG_PROTOCOL_H__ 6 #define __ZCONFIG_PROTOCOL_H__ 7 8 #include <stdint.h> 9 #include "zconfig_utils.h" 10 #include "zconfig_ieee80211.h" 11 #include "zconfig_lib.h" 12 13 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ 14 extern "C" { 15 #endif 16 17 enum state_machine { 18 STATE_CHN_SCANNING, 19 STATE_CHN_LOCKED_BY_P2P, /* wps/action used for enrollee */ 20 STATE_CHN_LOCKED_BY_MCAST, /* broadcast used for smartconfig */ 21 STATE_CHN_LOCKED_BY_BR, /* broadcast used for smartconfig */ 22 STATE_GOT_BEACON, 23 STATE_RCV_IN_PROGRESS, 24 STATE_RCV_COMPLETE, 25 STATE_RCV_DONE 26 }; 27 28 enum _GOT_RESULT_ { 29 GOT_NOTHING = 0, 30 GOT_CHN_LOCK = 1, 31 GOT_SSID_PASSWD = 2, 32 }; 33 34 #define PASSWD_ENCRYPT_BIT_OFFSET (1) 35 #define PASSWD_ENCRYPT_MASK (0x06) 36 #define SSID_EXIST_BIT (0) 37 #define SSID_EXIST_MASK (1 << SSID_EXIST_BIT) 38 #define SSID_ENCODE_BIT (5) 39 #define SSID_ENCODE_MASK (1 << SSID_ENCODE_BIT) 40 41 enum passwd_encpyt_type { 42 PASSWD_ENCRYPT_OPEN = 0, 43 PASSWD_ENCRYPT_CIPHER, 44 PASSWD_ENCRYPT_AESCFB, 45 PASSWD_ENCRYPT_AESOFB, 46 }; 47 48 #define flag_tods(tods) ((tods) ? 'T' : 'F') 49 50 #define ZC_MAX_CHANNEL (14) 51 #define ZC_MIN_CHANNEL (1) 52 int zconfig_is_valid_channel(int channel); 53 54 #define P2P_ENCODE_TYPE_OFFSET (0x05) 55 #define P2P_SSID_LEN_MASK ((1 << P2P_ENCODE_TYPE_OFFSET) - 1) 56 #define P2P_ENCRYPT_BIT_MASK ((uint8_t)(~P2P_SSID_LEN_MASK)) 57 enum p2p_encode_type { 58 P2P_ENCODE_TYPE_DICT = 0x00, 59 P2P_ENCODE_TYPE_ENCRYPT, 60 }; 61 62 /* global data */ 63 /* max: 48(ssid gbk encode) + 64 (passwd) + 6 (1(tlen) + 1(flag) + 1(ssid_len) + 64 * 1(passwd_len) + 2(crc)) */ 65 #define MAX_PKG_NUMS (128) 66 67 /* zconfig protocol */ 68 #define START_FRAME (0x4E0) /* 0x4E0 is group 0 */ 69 #define GROUP_FRAME (0x3E0) /* exclusive, 0x401 is group 1, 0x400 is not used \ 70 */ 71 #define GROUP_FRAME_END \ 72 (GROUP_FRAME + MAX_PKG_NUMS / GROUP_NUMBER) /* exclusive */ 73 #define GROUP_NUMBER (8) 74 #define ZC_GRP_PKT_IDX_START (2) 75 #define ZC_GRP_PKT_IDX_END (ZC_GRP_PKT_IDX_START + GROUP_NUMBER - 1) 76 77 struct package { 78 uint16_t len; 79 char score; 80 }; 81 82 struct zconfig_data { 83 struct { 84 uint8_t state_machine; /* state for tods/fromds */ 85 uint8_t frame_offset; /* frame fixed offset */ 86 uint8_t group_pos; /* latest group pkg pos */ 87 uint8_t cur_pos; /* data abs. position */ 88 uint8_t max_pos; /* data max len */ 89 uint8_t last_index; 90 uint8_t replace; /* whether pkg has been replaced recently */ 91 uint8_t score_uplimit; 92 #define score_max (100) 93 #define score_high (98) 94 #define score_mid (50) 95 #define score_low (1) 96 #define score_min (0) 97 98 uint8_t pos_unsync; 99 uint16_t group_sn; /* latest group pkg sn */ 100 uint16_t prev_sn; /* last sn */ 101 uint16_t last_len; /* len pkg len */ 102 uint32_t timestamp; /* last timestamp */ 103 #define time_interval (300) /* ms */ 104 } data[2]; 105 106 /* package store */ 107 struct package pkg[2][MAX_PKG_NUMS]; 108 struct package tmp_pkg[2][GROUP_NUMBER + 1]; 109 uint8_t src_mac[ETH_ALEN]; 110 uint8_t channel; /* from 1 -- 13 */ 111 112 /* result, final result */ 113 uint8_t ssid[ZC_MAX_SSID_LEN]; 114 uint8_t passwd[ZC_MAX_PASSWD_LEN]; 115 uint8_t bssid[ETH_ALEN]; 116 uint8_t token[ZC_MAX_TOKEN_LEN]; 117 uint8_t ssid_is_gbk; 118 uint8_t ssid_auto_complete_disable; 119 /* used by v2 android p2p protocol, for gbk ssid correctness */ 120 uint8_t android_pre_ssid[ZC_MAX_SSID_LEN]; 121 uint8_t android_ssid[ZC_MAX_SSID_LEN]; 122 uint8_t android_bssid[ETH_ALEN]; 123 uint8_t android_src[ETH_ALEN]; 124 void *mutex; 125 }; 126 127 #define zc_state (zconfig_data->data[tods].state_machine) 128 #define zc_frame_offset (zconfig_data->data[tods].frame_offset) 129 #define zc_group_pos (zconfig_data->data[tods].group_pos) 130 #define zc_group_sn (zconfig_data->data[tods].group_sn) 131 #define zc_prev_sn (zconfig_data->data[tods].prev_sn) 132 #define zc_cur_pos (zconfig_data->data[tods].cur_pos) 133 #define zc_max_pos (zconfig_data->data[tods].max_pos) 134 #define zc_last_index (zconfig_data->data[tods].last_index) 135 #define zc_last_len (zconfig_data->data[tods].last_len) 136 #define zc_replace (zconfig_data->data[tods].replace) 137 #define zc_score_uplimit (zconfig_data->data[tods].score_uplimit) 138 #define zc_timestamp (zconfig_data->data[tods].timestamp) 139 #define zc_pos_unsync (zconfig_data->data[tods].pos_unsync) 140 141 #define zc_src_mac (&zconfig_data->src_mac[0]) 142 143 #define zc_channel (zconfig_data->channel) 144 145 #define zc_ssid (&zconfig_data->ssid[0]) 146 #define zc_passwd (&zconfig_data->passwd[0]) 147 #define zc_bssid (&zconfig_data->bssid[0]) 148 #define zc_token (&zconfig_data->token[0]) 149 #define zc_ssid_is_gbk (zconfig_data->ssid_is_gbk) 150 #define zc_ssid_auto_complete_disable (zconfig_data->ssid_auto_complete_disable) 151 152 #define pkg_score(n) (zconfig_data->pkg[tods][n].score) 153 #define pkg_len(n) (zconfig_data->pkg[tods][n].len) 154 #define pkg(n) (&zconfig_data->pkg[tods][n]) 155 156 #define tmp_score(n) (zconfig_data->tmp_pkg[tods][n].score) 157 #define tmp_len(n) (zconfig_data->tmp_pkg[tods][n].len) 158 #define tmp(n) (&zconfig_data->tmp_pkg[tods][n]) 159 160 #define zc_pre_ssid (&zconfig_data->android_pre_ssid[0]) 161 #define zc_android_ssid (&zconfig_data->android_ssid[0]) 162 #define zc_android_bssid (&zconfig_data->android_bssid[0]) 163 #define zc_android_src (&zconfig_data->android_src[0]) 164 #define zc_mutex (zconfig_data->mutex) 165 166 void zconfig_force_destroy(void); 167 void encode_chinese(uint8_t *in, uint8_t in_len, uint8_t *out, uint8_t *out_len, 168 uint8_t bits); 169 void decode_chinese(uint8_t *in, uint8_t in_len, uint8_t *out, uint8_t *out_len, 170 uint8_t bits); 171 void zconfig_set_state(uint8_t state, uint8_t tods, uint8_t channel); 172 int is_ascii_string(uint8_t *str); 173 174 /* 175 * [IN] ssid or bssid 176 * [OUT] auth, encry, channel 177 */ 178 uint8_t zconfig_get_auth_info(uint8_t *ssid, uint8_t *bssid, uint8_t *auth, 179 uint8_t *encry, uint8_t *channel); 180 uint8_t zconfig_callback_over(uint8_t *ssid, uint8_t *passwd, uint8_t *bssid, 181 uint8_t *token); 182 183 #define MAC_FORMAT "%02x%02x%02x%02x%02x%02x" 184 #define MAC_VALUE(mac) mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] 185 186 extern const char *zc_default_ssid; 187 extern const char *zc_default_passwd; 188 extern struct zconfig_data *zconfig_data; 189 extern uint8_t zconfig_finished; 190 /* broadcast mac address */ 191 extern uint8_t br_mac[ETH_ALEN]; 192 /* all zero mac address */ 193 extern uint8_t zero_mac[ETH_ALEN]; 194 195 #define MCAST_MAX_LEN (127) 196 struct mcast_smartconfig_data_type { 197 /* result, final result */ 198 uint8_t tlen; 199 uint8_t flag; 200 uint8_t passwd_len; 201 uint8_t *passwd; 202 uint8_t ssid_len; 203 uint8_t *ssid; 204 uint8_t token_len; 205 uint8_t *token; 206 uint8_t bssid_type_len; 207 uint8_t *bssid; 208 uint8_t ssid_is_gbk; 209 uint8_t ssid_auto_complete_disable; 210 uint8_t data[MCAST_MAX_LEN]; 211 uint8_t checksum; 212 }; 213 214 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ 215 } 216 #endif 217 218 #endif /* __IEEE80211_123_H */ 219