00001 // DSWifi Project - Arm9 Library Header File (dswifi9.h) 00002 // Copyright (C) 2005-2006 Stephen Stair - sgstair@akkit.org - http://www.akkit.org 00003 /****************************************************************************** 00004 DSWifi Lib and test materials are licenced under the MIT open source licence: 00005 Copyright (c) 2005-2006 Stephen Stair 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a copy of 00008 this software and associated documentation files (the "Software"), to deal in 00009 the Software without restriction, including without limitation the rights to 00010 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 00011 of the Software, and to permit persons to whom the Software is furnished to do 00012 so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in all 00015 copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00023 SOFTWARE. 00024 ******************************************************************************/ 00025 00026 #ifndef DSWIFI9_H 00027 #define DSWIFI9_H 00028 00029 #include "dswifi_version.h" 00030 00031 // well, some flags and stuff are just stuffed in here and not documented very well yet... Most of the important stuff is documented though. 00032 // Next version should clean up some of this a lot more :) 00033 00034 #define WIFIINIT_OPTION_USELED 0x0002 00035 00036 // default option is to use 128k heap 00037 #define WIFIINIT_OPTION_USEHEAP_128 0x0000 00038 #define WIFIINIT_OPTION_USEHEAP_64 0x1000 00039 #define WIFIINIT_OPTION_USEHEAP_256 0x2000 00040 #define WIFIINIT_OPTION_USEHEAP_512 0x3000 00041 #define WIFIINIT_OPTION_USECUSTOMALLOC 0x4000 00042 #define WIFIINIT_OPTION_HEAPMASK 0xF000 00043 00044 #define WFLAG_PACKET_DATA 0x0001 00045 #define WFLAG_PACKET_MGT 0x0002 00046 #define WFLAG_PACKET_BEACON 0x0004 00047 #define WFLAG_PACKET_CTRL 0x0008 00048 00049 #define WFLAG_PACKET_ALL 0xFFFF 00050 00051 00052 #define WFLAG_APDATA_ADHOC 0x0001 00053 #define WFLAG_APDATA_WEP 0x0002 00054 #define WFLAG_APDATA_WPA 0x0004 00055 #define WFLAG_APDATA_COMPATIBLE 0x0008 00056 #define WFLAG_APDATA_EXTCOMPATIBLE 0x0010 00057 #define WFLAG_APDATA_SHORTPREAMBLE 0x0020 00058 #define WFLAG_APDATA_ACTIVE 0x8000 00059 00060 enum WIFI_RETURN { 00061 WIFI_RETURN_OK = 0, // Everything went ok 00062 WIFI_RETURN_LOCKFAILED = 1, // the spinlock attempt failed (it wasn't retried cause that could lock both cpus- retry again after a delay. 00063 WIFI_RETURN_ERROR = 2, // There was an error in attempting to complete the requested task. 00064 WIFI_RETURN_PARAMERROR = 3, // There was an error in the parameters passed to the function. 00065 }; 00066 00067 enum WIFI_STATS { 00068 // software stats 00069 WSTAT_RXQUEUEDPACKETS, // number of packets queued into the rx fifo 00070 WSTAT_TXQUEUEDPACKETS, // number of packets queued into the tx fifo 00071 WSTAT_RXQUEUEDBYTES, // number of bytes queued into the rx fifo 00072 WSTAT_TXQUEUEDBYTES, // number of bytes queued into the tx fifo 00073 WSTAT_RXQUEUEDLOST, // number of packets lost due to space limitations in queuing 00074 WSTAT_TXQUEUEDREJECTED, // number of packets rejected due to space limitations in queuing 00075 WSTAT_RXPACKETS, 00076 WSTAT_RXBYTES, 00077 WSTAT_RXDATABYTES, 00078 WSTAT_TXPACKETS, 00079 WSTAT_TXBYTES, 00080 WSTAT_TXDATABYTES, 00081 // harware stats (function mostly unknown.) 00082 WSTAT_HW_1B0,WSTAT_HW_1B1,WSTAT_HW_1B2,WSTAT_HW_1B3,WSTAT_HW_1B4,WSTAT_HW_1B5,WSTAT_HW_1B6,WSTAT_HW_1B7, 00083 WSTAT_HW_1B8,WSTAT_HW_1B9,WSTAT_HW_1BA,WSTAT_HW_1BB,WSTAT_HW_1BC,WSTAT_HW_1BD,WSTAT_HW_1BE,WSTAT_HW_1BF, 00084 WSTAT_HW_1C0,WSTAT_HW_1C1,WSTAT_HW_1C4,WSTAT_HW_1C5, 00085 WSTAT_HW_1D0,WSTAT_HW_1D1,WSTAT_HW_1D2,WSTAT_HW_1D3,WSTAT_HW_1D4,WSTAT_HW_1D5,WSTAT_HW_1D6,WSTAT_HW_1D7, 00086 WSTAT_HW_1D8,WSTAT_HW_1D9,WSTAT_HW_1DA,WSTAT_HW_1DB,WSTAT_HW_1DC,WSTAT_HW_1DD,WSTAT_HW_1DE,WSTAT_HW_1DF, 00087 00088 NUM_WIFI_STATS 00089 }; 00090 00091 // user code should NEVER have to use the WIFI_MODE or WIFI_AUTHLEVEL enums... is here in case I want to have some debug code... 00092 enum WIFI_MODE { 00093 WIFIMODE_DISABLED, 00094 WIFIMODE_NORMAL, 00095 WIFIMODE_SCAN, 00096 WIFIMODE_ASSOCIATE, 00097 WIFIMODE_ASSOCIATED, 00098 WIFIMODE_DISASSOCIATE, 00099 WIFIMODE_CANNOTASSOCIATE, 00100 }; 00101 enum WIFI_AUTHLEVEL { 00102 WIFI_AUTHLEVEL_DISCONNECTED, 00103 WIFI_AUTHLEVEL_AUTHENTICATED, 00104 WIFI_AUTHLEVEL_ASSOCIATED, 00105 WIFI_AUTHLEVEL_DEASSOCIATED, 00106 }; 00107 00108 // user code uses members of the WIFIGETDATA structure in calling Wifi_GetData to retreive miscellaneous odd information 00109 enum WIFIGETDATA { 00110 WIFIGETDATA_MACADDRESS, // MACADDRESS: returns data in the buffer, requires at least 6 bytes 00111 WIFIGETDATA_NUMWFCAPS, // NUM WFC APS: returns number between 0 and 3, doesn't use buffer. 00112 00113 MAX_WIFIGETDATA 00114 }; 00115 00116 00117 enum WEPMODES { 00118 WEPMODE_NONE = 0, 00119 WEPMODE_40BIT = 1, 00120 WEPMODE_128BIT = 2 00121 }; 00122 // WIFI_ASSOCSTATUS - returned by Wifi_AssocStatus() after calling Wifi_ConnectAPk 00123 enum WIFI_ASSOCSTATUS { 00124 ASSOCSTATUS_DISCONNECTED, // not *trying* to connect 00125 ASSOCSTATUS_SEARCHING, // data given does not completely specify an AP, looking for AP that matches the data. 00126 ASSOCSTATUS_AUTHENTICATING, // connecting... 00127 ASSOCSTATUS_ASSOCIATING, // connecting... 00128 ASSOCSTATUS_ACQUIRINGDHCP, // connected to AP, but getting IP data from DHCP 00129 ASSOCSTATUS_ASSOCIATED, // Connected! (COMPLETE if Wifi_ConnectAP was called to start) 00130 ASSOCSTATUS_CANNOTCONNECT, // error in connecting... (COMPLETE if Wifi_ConnectAP was called to start) 00131 }; 00132 00133 extern const char * ASSOCSTATUS_STRINGS[]; 00134 00135 // most user code will never need to know about the WIFI_TXHEADER or WIFI_RXHEADER 00136 typedef struct WIFI_TXHEADER { 00137 u16 enable_flags; 00138 u16 unknown; 00139 u16 countup; 00140 u16 beaconfreq; 00141 u16 tx_rate; 00142 u16 tx_length; 00143 } Wifi_TxHeader; 00144 00145 typedef struct WIFI_RXHEADER { 00146 u16 a; 00147 u16 b; 00148 u16 c; 00149 u16 d; 00150 u16 byteLength; 00151 u16 rssi_; 00152 } Wifi_RxHeader; 00153 00154 // WIFI_ACCESSPOINT is an important structure in that it defines how to connect to an access point. 00155 // listed inline are information about the members and their function 00156 // if a field is not necessary for Wifi_ConnectAP it will be marked as such 00157 // *only* 4 fields are absolutely required to be filled in correctly for the connection to work, they are: 00158 // ssid, ssid_len, bssid, and channel - all others can be ignored (though flags should be set to 0) 00159 typedef struct WIFI_ACCESSPOINT { 00160 char ssid[33]; // the AP's SSID - zero terminated is not necessary.. if ssid[0] is zero, the ssid will be ignored in trying to find an AP to connect to. [REQUIRED] 00161 char ssid_len; // number of valid bytes in the ssid field (0-32) [REQUIRED] 00162 u8 bssid[6]; // BSSID is the AP's SSID - setting it to all 00's indicates this is not known and it will be ignored [REQUIRED] 00163 u8 macaddr[6]; // mac address of the "AP" is only necessary in ad-hoc mode. [generally not required to connect] 00164 u16 maxrate; // max rate is measured in steps of 1/2Mbit - 5.5Mbit will be represented as 11, or 0x0B [not required to connect] 00165 u32 timectr; // internal information about how recently a beacon has been received [not required to connect] 00166 u16 rssi; // running average of the recent RSSI values for this AP, will be set to 0 after not receiving beacons for a while. [not required to connect] 00167 u16 flags; // flags indicating various parameters for the AP [not required, but the WFLAG_APDATA_ADHOC flag will be used] 00168 u32 spinlock; // internal data word used to lock the record to guarantee data coherence [not required to connect] 00169 u8 channel; // valid channels are 1-13, setting the channel to 0 will indicate the system should search. [REQUIRED] 00170 u8 rssi_past[8]; // rssi_past indicates the RSSI values for the last 8 beacons received ([7] is the most recent) [not required to connect] 00171 u8 base_rates[16]; // list of the base rates "required" by the AP (same format as maxrate) - zero-terminated list [not required to connect] 00172 } Wifi_AccessPoint; 00173 00174 // Wifi Packet Handler function: (int packetID, int packetlength) - packetID is only valid while the called function is executing. 00175 // call Wifi_RxRawReadPacket while in the packet handler function, to retreive the data to a local buffer. 00176 typedef void (*WifiPacketHandler)(int, int); 00177 00178 // Wifi Sync Handler function: Callback function that is called when the arm7 needs to be told to synchronize with new fifo data. 00179 // If this callback is used (see Wifi_SetSyncHandler()), it should send a message via the fifo to the arm7, which will call Wifi_Sync() on arm7. 00180 typedef void (*WifiSyncHandler)(); 00181 00182 00183 #ifdef __cplusplus 00184 extern "C" { 00185 #endif 00186 00188 // Init/update/state management functions 00189 00190 // Wifi_Init: Initializes the wifi library (arm9 side) and the sgIP library. 00191 // int initflags: set up some optional things, like controlling the LED blinking. 00192 // Returns: a 32bit value that *must* be passed to arm7. 00193 extern unsigned long Wifi_Init(int initflags); 00194 00195 // Wifi_CheckInit: Verifies when the ARM7 has been successfully initialized 00196 // Returns: 1 if the arm7 is ready for wifi, 0 otherwise 00197 extern int Wifi_CheckInit(); 00198 00199 // Wifi_DisableWifi: Instructs the ARM7 to disengage wireless and stop receiving or 00200 // transmitting. 00201 extern void Wifi_DisableWifi(); 00202 00203 // Wifi_EnableWifi: Instructs the ARM7 to go into a basic "active" mode, not actually 00204 // associated to an AP, but actively receiving and potentially transmitting 00205 extern void Wifi_EnableWifi(); 00206 00207 // Wifi_SetPromiscuousMode: Allows the DS to enter or leave a "promsicuous" mode, in which 00208 // all data that can be received is forwarded to the arm9 for user processing. 00209 // Best used with Wifi_RawSetPacketHandler, to allow user code to use the data 00210 // (well, the lib won't use 'em, so they're just wasting CPU otherwise.) 00211 // int enable: 0 to disable promiscuous mode, nonzero to engage 00212 extern void Wifi_SetPromiscuousMode(int enable); 00213 00214 // Wifi_ScanMode: Instructs the ARM7 to periodically rotate through the channels to 00215 // pick up and record information from beacons given off by APs 00216 extern void Wifi_ScanMode(); 00217 00218 // Wifi_SetChannel: If the wifi system is not connected or connecting to an access point, instruct 00219 // the chipset to change channel 00220 // int channel: the channel to change to, in the range of 1-13 00221 extern void Wifi_SetChannel(int channel); 00222 00223 // Wifi_GetNumAP: 00224 // Returns: the current number of APs that are known about and tracked internally 00225 extern int Wifi_GetNumAP(); 00226 00227 // Wifi_GetAPData: Grabs data from internal structures for user code (always succeeds) 00228 // int apnum: the 0-based index of the access point record to fetch 00229 // Wifi_AccessPoint * apdata: Pointer to the location where the retrieved data should be stored 00230 extern int Wifi_GetAPData(int apnum, Wifi_AccessPoint * apdata); 00231 00232 // Wifi_FindMatchingAP: determines whether various APs exist in the local area. You provide a 00233 // list of APs, and it will return the index of the first one in the list that can be found 00234 // in the internal list of APs that are being tracked 00235 // int numaps: number of records in the list 00236 // Wifi_AccessPoint * apdata: pointer to an array of structures with information about the APs to find 00237 // Wifi_AccessPoint * match_dest: OPTIONAL pointer to a record to receive the matching AP record. 00238 // Returns: -1 for none found, or a positive/zero integer index into the array 00239 extern int Wifi_FindMatchingAP(int numaps, Wifi_AccessPoint * apdata, Wifi_AccessPoint * match_dest); 00240 00241 // Wifi_ConnectAP: Connect to an access point 00242 // Wifi_AccessPoint * apdata: basic data on the AP 00243 // int wepmode: indicates whether wep is used, and what kind 00244 // int wepkeyid: indicates which wep key ID to use for transmitting 00245 // unsigned char * wepkey: the wep key, to be used in all 4 key slots (should make this more flexible in the future) 00246 // Returns: 0 for ok, -1 for error with input data 00247 extern int Wifi_ConnectAP(Wifi_AccessPoint * apdata, int wepmode, int wepkeyid, unsigned char * wepkey); 00248 00249 // Wifi_AutoConnect: Connect to an access point specified by the WFC data in the firmware 00250 extern void Wifi_AutoConnect(); 00251 00252 // Wifi_AssocStatus: Returns information about the status of connection to an AP 00253 // Returns: a value from the WIFI_ASSOCSTATUS enum, continue polling until you 00254 // receive ASSOCSTATUS_CONNECTED or ASSOCSTATUS_CANNOTCONNECT 00255 extern int Wifi_AssocStatus(); 00256 00257 // Wifi_DisconnectAP: Disassociate from the Access Point 00258 extern int Wifi_DisconnectAP(); 00259 00260 // Wifi_Timer: This function should be called in a periodic interrupt. It serves as the basis 00261 // for all updating in the sgIP library, all retransmits, timeouts, and etc are based on this 00262 // function being called. It's not timing critical but it is rather essential. 00263 // int num_ms: The number of milliseconds since the last time this function was called. 00264 extern void Wifi_Timer(int num_ms); 00265 00266 // Wifi_GetIP: 00267 // Returns: The current IP address of the DS (may not be valid before connecting to an AP, or setting the IP manually.) 00268 extern unsigned long Wifi_GetIP(); // get local ip 00269 00270 // Wifi_GetIPInfo: (values may not be valid before connecting to an AP, or setting the IP manually.) 00271 // struct in_addr * pGateway: pointer to receive the currently configured gateway IP 00272 // struct in_addr * pSnmask: pointer to receive the currently configured subnet mask 00273 // struct in_addr * pDns1: pointer to receive the currently configured primary DNS server IP 00274 // struct in_addr * pDns2: pointer to receive the currently configured secondary DNS server IP 00275 // Returns: The current IP address of the DS 00276 extern struct in_addr Wifi_GetIPInfo(struct in_addr * pGateway,struct in_addr * pSnmask,struct in_addr * pDns1,struct in_addr * pDns2); 00277 00278 // Wifi_SetIP: Set the DS's IP address and other IP configuration information. 00279 // unsigned long IPaddr: The new IP address (NOTE! if this value is zero, the IP, the gateway, and the subnet mask will be allocated via DHCP) 00280 // unsigned long gateway: The new gateway (example: 192.168.1.1 is 0xC0A80101) 00281 // unsigned long subnetmask: The new subnet mask (example: 255.255.255.0 is 0xFFFFFF00) 00282 // unsigned long dns1: The new primary dns server (NOTE! if this value is zero AND the IPaddr value is zero, dns1 and dns2 will be allocated via DHCP) 00283 // unsigned long dns2: The new secondary dns server 00284 extern void Wifi_SetIP(unsigned long IPaddr, unsigned long gateway, unsigned long subnetmask, unsigned long dns1, unsigned long dns2); 00285 00286 // Wifi_GetData: Retrieve an arbitrary or misc. piece of data from the wifi hardware. see WIFIGETDATA enum. 00287 // int datatype: element from the WIFIGETDATA enum specifing what kind of data to get 00288 // int bufferlen: length of the buffer to copy data to (not always used) 00289 // unsigned char * buffer: buffer to copy element data to (not always used) 00290 // Returns: -1 for failure, the number of bytes written to the buffer, or the value requested if the buffer isn't used. 00291 extern int Wifi_GetData(int datatype, int bufferlen, unsigned char * buffer); 00292 00293 // Wifi_GetStats: Retreive an element of the wifi statistics gathered 00294 // int statnum: Element from the WIFI_STATS enum, indicating what statistic to return 00295 // Returns: the requested stat, or 0 for failure 00296 extern u32 Wifi_GetStats(int statnum); 00298 // Raw Send/Receive functions 00299 00300 // Wifi_RawTxFrame: Send a raw 802.11 frame at a specified rate 00301 // unsigned short datalen: The length in bytes of the frame to send 00302 // unsigned short rate: The rate to transmit at (Specified as mbits/10, 1mbit=0x000A, 2mbit=0x0014) 00303 // unsigned short * data: Pointer to the data to send (should be halfword-aligned) 00304 // Returns: Nothing of interest. 00305 extern int Wifi_RawTxFrame(unsigned short datalen, unsigned short rate, unsigned short * data); 00306 00307 // Wifi_RawSetPacketHandler: Set a handler to process all raw incoming packets 00308 // WifiPacketHandler wphfunc: Pointer to packet handler (see WifiPacketHandler definition for more info) 00309 extern void Wifi_RawSetPacketHandler(WifiPacketHandler wphfunc); 00310 00311 // Wifi_RxRawReadPacket: Allows user code to read a packet from within the WifiPacketHandler function 00312 // long packetID: a non-unique identifier which locates the packet specified in the internal buffer 00313 // long readlength: number of bytes to read (actually reads (number+1)&~1 bytes) 00314 // unsigned short * data: location for the data to be read into 00315 extern int Wifi_RxRawReadPacket(long packetID, long readlength, unsigned short * data); 00316 00318 // Fast transfer support - update functions 00319 00320 // Wifi_Update: Checks for new data from the arm7 and initiates routing if data 00321 // is available. 00322 extern void Wifi_Update(); 00323 00324 // Wifi_Sync: Call this function when requested to sync by the arm7 side of the 00325 // wifi lib 00326 extern void Wifi_Sync(); 00327 00328 // Wifi_SetSyncHandler: Call this function to request notification of when the 00329 // ARM7-side Wifi_Sync function should be called. 00330 // WifiSyncHandler sh: Pointer to the function to be called for notification. 00331 extern void Wifi_SetSyncHandler(WifiSyncHandler sh); 00332 00333 #define WFC_CONNECT true 00334 #define INIT_ONLY false 00335 00336 00337 extern bool Wifi_InitDefault(bool useFirmwareSettings); 00338 00339 #ifdef __cplusplus 00340 }; 00341 #endif 00342 00343 00344 #endif