Main Page | Data Structures | File List | Data Fields | Globals

libmac_receiver.c File Reference

Code sample that illustrates the use of the recv. function in this library. More...

#include <libmac/libmac.h>

Functions

int main (int argc, char *argv[])


Detailed Description


Function Documentation

int main int  argc,
char *  argv[]
 

Example usage, of the recv-side function alongwith the parameter information functions and the interface information functions, follows. This piece of code:

  • obtains the list of interfaces using the wl_get_ifinfo function,
  • for the interface, passed as a command-line argument,
    • get parameter information and store it in the mac_params struct.
    • try to set parameters to specified values.
    • receive 2 frames,
  • It return errno and prints the error information on the screen in case of failure of any of the calls.
00021 { 00022 // Initializations. 00023 int i, j, l, err; 00024 struct mac_ifinfo *ifinfo_ptr = NULL; 00025 struct mac_ifinfo_list *temp; 00026 struct mac_params *params_ptr = NULL; 00027 struct mac_params_flags *flags_ptr = NULL; 00028 struct ether_header *eth_hdr = NULL; 00029 char *payload = NULL; 00030 unsigned short len = 0; 00031 unsigned char *send_parameter_info = NULL, *recv_parameter_info = NULL; 00032 00033 if (argc < 2) { 00034 printf("Usage: ./receiver name_of_interface.\n"); 00035 return(-1); 00036 } 00037 00038 // START: ALLOCATE MEMORY FOR THE LIBRARY STRUCTS. ON RECV SIDE. 00039 00040 // Get interface information. 00041 if ( (err = mac_get_ifinfo(&ifinfo_ptr)) ) 00042 { 00043 #ifdef DEBUG_ERR 00044 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00045 #endif 00046 return err; 00047 } 00048 00049 // Initialize params. struct. and params_flags struct. 00050 if ( (err = mac_init_params(&params_ptr, &flags_ptr)) ) 00051 { 00052 #ifdef DEBUG_ERR 00053 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00054 #endif 00055 return err; 00056 } 00057 // END: ALLOCATE MEMORY FOR THE LIBRARY STRUCTS. 00058 00059 #ifdef DEBUG_BASIC 00060 // Go through the list of interfaces. 00061 temp = ifinfo_ptr->mac_list; 00062 while (temp != NULL) 00063 { 00064 // Call the resp. library functions for the interface eth2/wlan0. 00065 if ( !(strcmp(temp->if_name, argv[1])) ) 00066 { 00067 printf("----------- FOR INTERFACE %s ---------\n", temp->if_name); 00068 /* Get parameter information from this interface and store 00069 it in the mac_params struct. 00070 */ 00071 if ( (err = mac_get_params(params_ptr, temp, 4, mac_rssi, 00072 mac_txpower, mac_noise, 00073 mac_currentXmitRate)) ) 00074 { 00075 #ifdef DEBUG_ERR 00076 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00077 #endif 00078 return err; 00079 } 00080 00081 // Print out parameter information retrieved from interface. 00082 printf("----------- VALUES AFTER CALL TO GET ------------\n"); 00083 for (i = START_PARAMS_KEY; i < 4; i++) 00084 printf("Key: %d, Value: %d\n", params_ptr->key[i], 00085 params_ptr->value[i]); 00086 // Try to set parameters to the values specified. 00087 if ( (err = mac_set_params(params_ptr, temp, 3, 00088 mac_noise, 10, mac_rssi, 88, 00089 mac_currentXmitRate, 2000000)) ) 00090 { 00091 #ifdef DEBUG_ERR 00092 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00093 #endif 00094 return err; 00095 } 00096 00097 /* Print out the values to which the parameters have been set. 00098 If the value printed here corresponds to the largest integer 00099 value, it means that the parameter is read-only. If it prints 00100 out the largest negative integer value, it means that the 00101 parameter is unsupported. 00102 */ 00103 printf("------------ VALUES AFTER CALL TO SET -----------------\n"); 00104 for (i = START_PARAMS_KEY; i < 4; i++) 00105 { 00106 printf("Key: %d, Value: %d\n", params_ptr->key[i], 00107 params_ptr->value[i]); 00108 } 00109 00110 /* Call function to append parameter information on all incoming 00111 frames. The parameters to be appended in this case are mac_noise 00112 and mac_rssi. 00113 */ 00114 if ( (err = mac_append_params(temp, INCOMING, 2, 00115 mac_noise, mac_rssi)) ) 00116 { 00117 #ifdef DEBUG_ERR 00118 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00119 #endif 00120 return err; 00121 } 00122 00123 00124 for (i=0;i<2;i++) 00125 { 00126 eth_hdr = NULL; 00127 payload = NULL; 00128 len = 0; 00129 send_parameter_info = NULL; 00130 recv_parameter_info = NULL; 00131 // Receive frames, of all types, on the specified interface. 00132 if ( (err = mac_recv(&eth_hdr, &payload, &len, 00133 &send_parameter_info, 00134 &recv_parameter_info, temp, "")) ) 00135 { 00136 #ifdef DEBUG_ERR 00137 printf("Error, error code: %d, %s\n\n", err, strerror(err)); 00138 #endif 00139 return err; 00140 } 00141 00142 printf("Src MAC addr: %s\n", 00143 pr_ether((unsigned char *)((eth_hdr)->ether_shost))); 00144 printf("Dst MAC addr: %s\n", 00145 pr_ether((unsigned char *)((eth_hdr)->ether_dhost))); 00146 printf("TYPE: %x\n", htons( (eth_hdr)->ether_type ) ); 00147 00148 // Print out the Length of the received payload in bytes. 00149 printf("Length of just payload: %d\n", len); 00150 00151 // Check for send-side parameter information. 00152 if ( (send_parameter_info) != NULL ) 00153 { 00154 // Calculate the number of appended parameters. 00155 params_ptr->number_of_params = *send_parameter_info; 00156 /* Calculate the number of bytes occupied by the 00157 appended parameter field. 00158 */ 00159 l = SIZEOF_PARAMS_LIST_LEN + 00160 ( (params_ptr->number_of_params) * 00161 (SIZEOF_PARAMS_LIST_KEY + SIZEOF_PARAMS_LIST_VALUE) ); 00162 for(j=SIZEOF_PARAMS_LIST_LEN;j < l; 00163 j=j+SIZEOF_PARAMS_LIST_VALUE) 00164 { 00165 printf("Param key: %d\n", send_parameter_info[j]); 00166 j = j+SIZEOF_PARAMS_LIST_KEY; 00167 printf("Param value: %hd", 00168 *((unsigned short *)(send_parameter_info + j))); 00169 printf("\n"); 00170 } 00171 00172 // Check for recv-side parameter information. 00173 if ( (recv_parameter_info) != NULL ) 00174 { 00175 // Calculate the number of appended parameters. 00176 params_ptr->number_of_params = *recv_parameter_info; 00177 /* Calculate the number of bytes occupied by the 00178 appended parameter field. 00179 */ 00180 l = SIZEOF_PARAMS_LIST_LEN + 00181 ((params_ptr->number_of_params) * 00182 (SIZEOF_PARAMS_LIST_KEY + SIZEOF_PARAMS_LIST_VALUE)); 00183 for(j=SIZEOF_PARAMS_LIST_LEN;j < l; 00184 j=j+SIZEOF_PARAMS_LIST_VALUE) 00185 { 00186 printf("Param key: %d\n", recv_parameter_info[j]); 00187 if (recv_parameter_info[j] == mac_noise) { 00188 j = j+SIZEOF_PARAMS_LIST_KEY; 00189 printf("Param value: %d", 00190 *((unsigned short *)(recv_parameter_info + j)) - 256); 00191 printf("\n"); 00192 } else { 00193 j = j+SIZEOF_PARAMS_LIST_KEY; 00194 printf("Param value: %hd", 00195 *((unsigned short *)(recv_parameter_info + j))); 00196 printf("\n"); 00197 } 00198 } 00199 } 00200 } 00201 else 00202 { // Check for recv-side parameter information. 00203 if ( (recv_parameter_info) != NULL ) 00204 { 00205 // Calculate the number of appended parameters. 00206 params_ptr->number_of_params = *recv_parameter_info; 00207 /* Calculate the number of bytes occupied by the 00208 appended parameter field. 00209 */ 00210 l = SIZEOF_PARAMS_LIST_LEN + 00211 ((params_ptr->number_of_params) * 00212 (SIZEOF_PARAMS_LIST_KEY + SIZEOF_PARAMS_LIST_VALUE)); 00213 for(j=SIZEOF_PARAMS_LIST_LEN;j < l; 00214 j=j+SIZEOF_PARAMS_LIST_VALUE) 00215 { 00216 printf("Param key: %d\n", recv_parameter_info[j]); 00217 if (recv_parameter_info[j] == mac_noise) { 00218 j = j+SIZEOF_PARAMS_LIST_KEY; 00219 printf("Param value: %d", 00220 *((unsigned short *)(recv_parameter_info + j)) - 256); 00221 printf("\n"); 00222 } else { 00223 j = j+SIZEOF_PARAMS_LIST_KEY; 00224 printf("Param value: %hd", 00225 *((unsigned short *)(recv_parameter_info + j))); 00226 printf("\n"); 00227 } 00228 } 00229 } 00230 } 00231 } 00232 } 00233 temp = temp->next; 00234 } 00235 #endif 00236 00237 // START: FREE MEMORY ALLOCATED FOR THE LIBRARY STRUCTS. 00238 00239 // Free parameter data structures. 00240 mac_free_params(&params_ptr, &flags_ptr); 00241 // Free interface information collected and return. 00242 mac_free_ifinfo(&ifinfo_ptr); 00243 00244 // END: FREE MEMORY ALLOCATED FOR THE LIBRARY STRUCTS. 00245 00246 return 0; 00247 }


Generated on Tue Aug 24 17:01:41 2004 for libmac by doxygen 1.3.8