/home/tai/chorist/xian-1.2/src/xmr.c

Go to the documentation of this file.
00001 /*
00011  *  Copyright (C) 2006 THALES Communications
00012  *
00013  *  This file is part of XIAN software.
00014  *
00015  *  XIAN is free software; you can redistribute it and/or modify it
00016  *  under the terms of the GNU General Public License as published by
00017  *  the Free Software Foundation.
00018  *
00019  *  XIAN software is distributed in the hope that it will be useful,
00020  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  *  GNU General Public License for more details.
00023  *
00024  *  You should have received a copy of the GNU General Public License
00025  *  along with XIAN software; if not, write to the Free Software
00026  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  *
00028  *  */
00029 
00030 /************************************************************************************************/
00031  
00032 
00033 #include <linux/init.h>
00034 #include <linux/module.h>
00035 #include "include/xian_proto.h"
00036 
00037 MODULE_DESCRIPTION("XMR - XIAN Metric Repository Module");
00038 MODULE_LICENSE("GPL");
00039 
00040 struct xian_stats *stats;
00041 
00042 /**************************************************************************************************************************************/
00048 struct xian_float xian_float_add(struct xian_float a, struct xian_float b){
00049   struct xian_float value;
00050   value.numerator=0;
00051   value.denominator=0;
00052   if(a.denominator==0){
00053     return b;
00054   }else{
00055     if(b.denominator==0){
00056     return a;
00057     }else{
00058       value.denominator=a.denominator*b.denominator;
00059       value.numerator=(a.numerator*b.denominator)+(b.numerator*a.denominator);
00060       return value;
00061     }
00062   }
00063 }
00064 XIAN_EXPORT_SYMBOL(xian_float_add);
00065 
00066 /**************************************************************************************************************************************/
00072 struct xian_float xian_float_mult(struct xian_float a, struct xian_float b){
00073   struct xian_float value;
00074   value.numerator=a.numerator*b.numerator;
00075   value.denominator=a.denominator*b.denominator;
00076   return value;
00077 }
00078 XIAN_EXPORT_SYMBOL(xian_float_mult);
00079 
00080 /**************************************************************************************************************************************/
00086 struct xian_float xian_float_div(struct xian_float a, struct xian_float b){
00087   struct xian_float value;
00088   struct xian_float inv;
00089   inv.numerator=b.denominator;
00090   inv.denominator=b.numerator;
00091   if(b.numerator!=0){
00092     value=xian_float_mult(a, inv);
00093   }
00094   return value;
00095 }
00096 XIAN_EXPORT_SYMBOL(xian_float_div);
00097 
00098 /**************************************************************************************************************************************/
00104 int xian_float_cmp(struct xian_float a, struct xian_float b){
00105   int c,d;
00106   c=a.numerator*b.denominator;
00107   d=b.numerator*a.denominator;
00108   if(c==d){//a==b
00109     return 0;
00110   }else{
00111     if(c<d){//a<b
00112       return -1;
00113     }else{//a>b
00114       return 1;
00115     }
00116   }
00117 }
00118 XIAN_EXPORT_SYMBOL(xian_float_cmp);
00119 
00120 /**************************************************************************************************************************************/
00125 struct xian_float int2xian_float(unsigned int x){
00126   struct xian_float value;
00127   value.numerator=x;
00128   value.denominator=1;
00129   return value;
00130 }
00131 XIAN_EXPORT_SYMBOL(int2xian_float);
00132 
00133 /**************************************************************************************************************************************/
00136 void print_stats(void){
00137   struct xian_stats *cur_stats;
00138   struct xian_stat *cur_stat;
00139   if(stats!=NULL){
00140     cur_stats=stats;
00141     while(cur_stats!=NULL){
00142       printk(KERN_ALERT "[XMR] id_conf=%d", cur_stats->id_conf);
00143       cur_stat=cur_stats->stat;
00144       while(cur_stat!=NULL){
00145         printk("\tmetric send by %02x:%02x:%02x:%02x:%02x:%02x", cur_stat->saddr[0], cur_stat->saddr[1], cur_stat->saddr[2], cur_stat->saddr[3], cur_stat->saddr[4], cur_stat->saddr[5]);
00146         printk(" val=%llu/%llu", cur_stat->value.numerator, cur_stat->value.denominator);
00147         cur_stat=cur_stat->next_stat;
00148       }
00149       printk("\n");
00150       cur_stats=cur_stats->next_stats;
00151     }
00152   }else{
00153     printk(KERN_ALERT "[XMR] vide\n");
00154   }
00155 }
00156 
00157 /**************************************************************************************************************************************/
00165 struct xian_float
00166 get_xian_stat(unsigned char macadd[IEEE80211_MAC_ADDR_LEN+1], 
00167               unsigned char dev_name[IFNAMESIZ+1],
00168               unsigned int id_conf,
00169               unsigned int *code_err){
00170   struct xian_float value;
00171   unsigned int code;
00172   unsigned char mac_addr_converted[ETH_ALEN];
00173 
00174   struct xian_stats *cur_stats;
00175   struct xian_stat *cur_stat;
00176   value.numerator = 0;
00177   value.denominator = 0;
00178   cur_stats=stats;
00179   while(cur_stats!=NULL){
00180     if(cur_stats->id_conf==id_conf){
00181       cur_stat=cur_stats->stat;
00182       convert_mac_addr(macadd, mac_addr_converted, &code);
00183       //printk("MAC to find=%02x:%02x:%02x:%02x:%02x:%02x\n", mac_addr_converted[0], mac_addr_converted[1], mac_addr_converted[2], mac_addr_converted[3], mac_addr_converted[4], mac_addr_converted[5]);
00184       while(cur_stat!=NULL){
00185         if(strcmp_mac(cur_stat->saddr,mac_addr_converted)==0){//metric founded
00186           value = cur_stat->value;
00187           return value;
00188         }
00189         cur_stat = cur_stat->next_stat;
00190       }
00191     }
00192     cur_stats = cur_stats->next_stats;
00193   }
00194   code=UNKNOW_METRIC;
00195   return value;
00196 }
00197 XIAN_EXPORT_SYMBOL(get_xian_stat);
00198 
00199 /**************************************************************************************************************************************/
00207 void update_xian_stat(unsigned char saddr[IEEE80211_MAC_ADDR_LEN+1], unsigned char dev_name[IFNAMESIZ+1], unsigned int id_conf, struct xian_float value){  
00208   unsigned char saddr_converted[ETH_ALEN];
00209   unsigned int code;
00210   unsigned int ok;
00211   int i;
00212   struct xian_stats *cur_stats, *new_stats;
00213   struct xian_stat *cur_stat, *new_stat;
00214 
00215   //printk(KERN_ALERT "\n");
00216   //printk(KERN_ALERT "[XMR] update BEGINNING %d %s\n", id_conf, saddr);
00217   //print_stats();
00218   code=NO_ERRORS;//=0
00219   ok=1;
00220   convert_mac_addr(saddr, saddr_converted, &code);
00221   if(code==INVALID_MAC_ADDR){
00222     printk(KERN_ALERT "[XMR] INVALID_MAC_ADDR\n");
00223     return;
00224   }
00225   cur_stats=stats;
00226   while(cur_stats!=NULL){
00227     if(cur_stats->id_conf==id_conf){
00228       cur_stat=cur_stats->stat;
00229       while(cur_stat!=NULL){
00230         if(strcmp_mac(cur_stat->saddr,saddr_converted)==0){//metric founded
00231           cur_stat->value=value;
00232           //printk(KERN_ALERT "[XMR] stat updated\n");
00233           ok=0;
00234         }  
00235         cur_stat=cur_stat->next_stat;
00236       }
00237       if(ok!=0){//insert at beginning of xian_stats
00238         struct xian_stat *new_stat;
00239         new_stat=kmalloc(sizeof(struct xian_stat),GFP_KERNEL);
00240         for(i=0; i<ETH_ALEN; i++){
00241           new_stat->saddr[i]=saddr_converted[i];
00242         }
00243         new_stat->value=value;
00244         new_stat->next_stat=cur_stats->stat;
00245         cur_stats->stat=new_stat;
00246         printk(KERN_ALERT "[XMR] stat inserted\n");
00247         ok=0;
00248       }
00249     }
00250     cur_stats=cur_stats->next_stats;
00251   }
00252   if(ok!=0){//insert at beginning of stats
00253     new_stats=kmalloc(sizeof(struct xian_stats),GFP_KERNEL);
00254     new_stats->next_stats=stats;
00255     new_stats->id_conf=id_conf;
00256     //new metric
00257     new_stat=kmalloc(sizeof(struct xian_stat),GFP_KERNEL);
00258     new_stat->next_stat=NULL;
00259     //memcpy(new_stat->saddr,saddr_converted,ETH_ALEN);
00260     for(i=0; i<ETH_ALEN; i++){
00261       new_stat->saddr[i]=saddr_converted[i];
00262     }
00263     new_stat->value=value;
00264     new_stats->stat=new_stat;
00265     stats=new_stats;
00266     printk(KERN_ALERT "[XMR] new stats inserted\n");
00267   }
00268   //print_stats();
00269   //printk(KERN_ALERT "[XMR] update END\n");
00270 }
00271 XIAN_EXPORT_SYMBOL(update_xian_stat);
00272 
00273 /**************************************************************************************************************************************/
00279 void del_xian_stat(unsigned char dev_name[IFNAMESIZ+1], unsigned int id_conf){//to do : macadd
00280   struct xian_stats *cur_stats;
00281   struct xian_stats *previous;
00282   struct xian_stat *cur_stat;
00283   struct xian_stat *prev;
00284   if(stats!=NULL){
00285     if((stats->next_stats==NULL)&&(stats->id_conf==id_conf)){
00286       stats=NULL;
00287     }else{
00288       cur_stats=stats;
00289       previous=stats;
00290       while(cur_stats!=NULL){
00291         if(cur_stats->id_conf==id_conf){
00292           cur_stat=cur_stats->stat;
00293           while(cur_stat!=NULL){
00294             prev=cur_stat;
00295             cur_stat=cur_stat->next_stat;
00296             kfree(prev);
00297           }
00298           previous->next_stats=cur_stats->next_stats;
00299           kfree(cur_stats);
00300         }
00301         previous=cur_stats;
00302         cur_stats=cur_stats->next_stats;
00303       }
00304     }
00305   }
00306   //print_stats();
00307 }
00308 XIAN_EXPORT_SYMBOL(del_xian_stat);
00309 
00310 
00311 int xian_stats_init(void){
00312   printk(KERN_ALERT "Init XIAN XMR (XIAN Metric Repository module)\n");
00313   stats=NULL;
00314   return 0;
00315 }
00316 
00317 void xian_stats_exit(void){
00318   printk(KERN_ALERT "Unload XIAN XMR (XIAN Metric Repository module)\n");
00319   kfree(stats);
00320 }
00321 
00322 module_init(xian_stats_init);
00323 module_exit(xian_stats_exit);

Generated on Mon Jan 21 12:31:46 2008 for XIAN by  doxygen 1.5.3