AVR Z-LINKŪ


atParser.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00027 /* === Includes ============================================================ */
00028 #include<stdbool.h>
00029 #include<stdint.h>
00030 
00031 #include"compiler.h"
00032 
00033 #include"ieee_const.h"
00034 #include"ieee_types.h"
00035 #include"wpan_mac.h"
00036 
00037 #include"chat.h"
00038 #include"atParser.h"
00039 #include"serialPortHAL.h"
00040 #include"utilities.h"
00041 
00042 /* ==== Macros ============================================================= */
00043 /* === Typedefs ============================================================ */
00044 /* === Variables =========================================================== */
00045 unsigned char __flash ATC[ ] = "AT+C";  
00046 unsigned char __flash ATD[ ] = "AT+D";  
00047 unsigned char __flash ATR[ ] = "AT+R";  
00048 unsigned char __flash ATT[ ] = "AT+T";  
00049 
00050 uint8_t chatChannel = DEFAULT_CHANNEL;
00051 uint16_t chatPANID  = DEFAULT_PANID;
00052 chatState_t state   = CHAT_IDLE;
00053 chatRole_t role     = UNDEFINED;
00054 
00055 /* === Prototypes ========================================================== */
00056 static void startNetworkOperation( uint8_t channelToScan );
00057 static void stopNetworkOperation( void );
00058 static void sendMsg( uint8_t msgLength, uint8_t *msg );
00059 /* === Implementation ====================================================== */
00060 
00061 
00077 void handleNewCommand( void ){
00078 
00079         uint8_t *ATCommand;
00080         uint8_t *msg;
00081         uint8_t msgLength;
00082         uint8_t deviceType;
00083         
00084         //Read new AT-Command from the receive buffer.
00085         ATCommand = getATCommand( );
00086         
00087         //Check if the received command is known.
00088 
00089         //Start network operation.
00090         if( stringCompare( ATCommand, ATC ) ){
00091                 
00092                 //Ensure that this function is only called after reset.
00093                 if( state != CHAT_IDLE ){
00094                         
00095                         sendERROR( INVALID_REQUEST );
00096                         return;
00097                 }
00098 
00099                 //Extract and check channel to chat on.
00100                 if( !htoi8( getParameterNumber( PARAMETER_1 ), &chatChannel  ) ){
00101                 
00102                         //Error occured during conversion.      Send error message and end.
00103                         sendERROR( DATA_CONVERSION_FAILED );
00104                         return;
00105                 }
00106                 
00107                 //Extract and check PANID.
00108                 if( !htoi16( getParameterNumber( PARAMETER_2 ), &chatPANID ) ){
00109                 
00110                         //Error occured during conversion.      Send error message and end.
00111                         sendERROR( DATA_CONVERSION_FAILED );
00112                         return;
00113                 }               
00114                 
00115                 //Read device type. Will be checked below.
00116                 deviceType = *( getParameterNumber( PARAMETER_3 ) );
00117                 
00118                 
00119                 //Check if desired device type equals coordinator.
00120                 if( deviceType == DEVICE_TYPE_COORDINATOR ){
00121 
00122                         role = COORDINATOR;
00123                 }
00124                 
00125                 //Check if desired device type equals end-device.
00126                 else if( deviceType == DEVICE_TYPE_END_DEVICE ){
00127                         
00128                         role = END_DEVICE;
00129                 }
00130                 
00131                 //This is not a valid device type.
00132                 else{
00133                 
00134                         sendERROR( UNKNOWN_DEVICE_TYPE );
00135                         return;
00136                 }
00137                 
00138                 //It is now safe to start opertaion on current channel and PANID.
00139                 startNetworkOperation( chatChannel );
00140         }
00141         
00142         //Stop network...disassociation procedure.              
00143         else if( stringCompare( ATCommand, ATD ) ){
00144 
00145                 stopNetworkOperation( );                                
00146         }
00147         
00148         //Reset.                
00149         else if( stringCompare( ATCommand, ATR ) ){
00150         
00151                 //wpan_init( );
00152                 wpan_mlme_reset_request( true );
00153                 state = CHAT_RESET_PENDING;
00154         }
00155         
00156         //Send data.            
00157         else if( stringCompare( ATCommand, ATT ) ){
00158                 
00159                 //Important that messages are not sent without being connected.
00160                 if( state  != CHAT_CONNECTED ){
00161                 
00162                         sendERROR( INCONSISTENT_STATE );
00163                         return;
00164                 }
00165 
00166                 //Extract and check length of data.
00167                 if( !htoi8( getParameterNumber( PARAMETER_1 ), &msgLength ) ){
00168                 
00169                         //Error occured during conversion.      Send error message and end.
00170                         sendERROR( DATA_CONVERSION_FAILED );
00171                         return;
00172                 }
00173 
00174                 //Extract the data it self. No checks will be done.
00175                 msg = getParameterNumber( PARAMETER_2 );
00176                 sendMsg( msgLength, msg );
00177         }
00178         
00179         //Received an AT command that is not supported.         
00180         else{
00181 
00182                 sendERROR( UNKNOWN_AT_COMMAND );
00183         }
00184 }
00185 
00186 
00197 static void startNetworkOperation( uint8_t channelToScan ){
00198 
00199         uint32_t channelMask = 1UL;
00200         
00201         //Generate channel mask as defined in MLME_SCAN_request.
00202         channelMask = channelMask << chatChannel;
00203 
00204         //Issue an active scan on the given channel for a define period.
00205         wpan_mlme_scan_request( MLME_SCAN_TYPE_ACTIVE, channelMask,
00206                                                         SCANDURATION );
00207 
00208         state = CHAT_ACTIVE_SCAN_PENDING;
00209 }
00210 
00211 
00216 static void stopNetworkOperation( void ){
00217 
00218         
00219 }
00220 
00230 static void sendMsg( uint8_t msgLength, uint8_t *msg ){
00231         
00232         //Setup address and PAN information.
00233         wpan_mcpsdata_addr_t addr_info;
00234     addr_info.SrcAddrMode = WPAN_ADDRMODE_SHORT;
00235     addr_info.DstAddrMode = WPAN_ADDRMODE_SHORT;
00236         addr_info.SrcPANId = chatPANID;
00237         addr_info.DstPANId = chatPANID;
00238         
00239         //Check length of message.
00240         if( !( msgLength <= MAX_MSG_LENGTH ) ){
00241                 
00242                 sendERROR( INVALID_REQUEST );
00243                 return;
00244         }
00245         
00246 
00247         //Coordinator.
00248         if( role  == COORDINATOR ){
00249         
00250                 addr_info.SrcAddr = COORD_SHORT_ADDRESS;
00251                 addr_info.DstAddr = END_DEVICE_SHORT_ADDRESS;   
00252         }
00253         
00254         //End-device.
00255         else{
00256         
00257                 addr_info.SrcAddr = END_DEVICE_SHORT_ADDRESS;
00258                 addr_info.DstAddr = COORD_SHORT_ADDRESS;        
00259         }
00260 
00261 
00262         if( wpan_mcps_data_request( &addr_info, 0x01, WPAN_TXOPT_ACK, msg, msgLength ) ){
00263                 
00264                 ;                       
00265         }
00266           
00267         else{
00268                 
00269                 sendERROR( EVENT_BUFFER_FULL );  
00270         }
00271 }
@DOC_TITLE@
Generated on Sat Dec 2 16:05:51 2006 for AVR414 User's Guide - ATAVRRZ502 - Accessory Kit by doxygen 1.4.7