From 71116c8a65a5ef2bf6417e72d40ff3dde0bfcc21 Mon Sep 17 00:00:00 2001
From: Grzegorz Daniluk <grzegorz.daniluk@gmail.com>
Date: Wed, 18 Jul 2012 11:57:32 +0200
Subject: [PATCH] shell: sfp match command for getting SFP parameters from the
 SFP DB

---
 dev/eeprom.c     | 29 +++++++++++++++++++++++++++++
 include/eeprom.h |  8 +++++++-
 shell/cmd_sfp.c  | 22 +++++++++++++++++++++-
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/dev/eeprom.c b/dev/eeprom.c
index 71568e666..8924155e8 100644
--- a/dev/eeprom.c
+++ b/dev/eeprom.c
@@ -145,6 +145,35 @@ int32_t eeprom_get_sfp(uint8_t i2cif, uint8_t i2c_addr, struct s_sfpinfo* sfp, u
   return sfpcount;
 }
 
+int8_t eeprom_match_sfp(uint8_t i2cif, uint8_t i2c_addr, struct s_sfpinfo* sfp)
+{
+  uint8_t sfpcount = 1;
+  int8_t i, temp;
+  struct s_sfpinfo dbsfp;
+
+  for(i=0; i<sfpcount; ++i)
+  {   
+    temp = eeprom_get_sfp(WRPC_FMC_I2C, FMC_EEPROM_ADR, &dbsfp, 0, i); 
+    if(!i) 
+    {   
+      sfpcount=temp; //only in first round valid sfpcount is returned from eeprom_get_sfp
+      if(sfpcount == 0 || sfpcount == 0xFF)
+        return 0;
+      else if(sfpcount<0) 
+        return sfpcount;
+    }   
+    if( !strncmp(dbsfp.pn, sfp->pn, 16) )
+    {
+      sfp->dTx = dbsfp.dTx;
+      sfp->dRx = dbsfp.dRx;
+      sfp->alpha = dbsfp.alpha;
+      return 1;
+    }
+  } 
+
+  return 0;
+}
+
 int8_t eeprom_get_sfpinfo(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, struct s_sfpinfo *sfpinfo, uint16_t section_sz)
 {
   uint8_t *buf;
diff --git a/include/eeprom.h b/include/eeprom.h
index c1c3fb5c0..2602c06f6 100644
--- a/include/eeprom.h
+++ b/include/eeprom.h
@@ -3,6 +3,7 @@
 
 #define SFP_SECTION_PATTERN 0xdeadbeef
 #define SFPS_MAX 4
+#define SFP_PN_LEN 16
 #define EE_BASE_SFP 4*1024
 #define EE_BASE_INIT 4*1024+SFPS_MAX*29
 
@@ -11,9 +12,13 @@
 #define EE_RET_CHKSUM -3
 #define EE_RET_POSERR -4
 
+extern int32_t sfp_alpha;
+extern int32_t sfp_deltaTx;
+extern int32_t sfp_deltaRx;
+
 struct s_sfpinfo
 {
-  char pn[16];
+  char pn[SFP_PN_LEN];
   int32_t alpha;
   int32_t dTx;
   int32_t dRx;
@@ -26,6 +31,7 @@ int eeprom_write(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, uint8_t *buf,
 
 int32_t eeprom_sfpdb_erase(uint8_t i2cif, uint8_t i2c_addr);
 int32_t eeprom_sfp_section(uint8_t i2cif, uint8_t i2c_addr, size_t size, uint16_t *section_sz);
+int8_t eeprom_match_sfp(uint8_t i2cif, uint8_t i2c_addr, struct s_sfpinfo* sfp);
 int8_t eeprom_get_sfpinfo(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, struct s_sfpinfo *sfpinfo, uint16_t section_sz);
 int8_t access_eeprom(char *sfp_pn, int32_t *alpha, int32_t *deltaTx, int32_t *deltaRx);
 
diff --git a/shell/cmd_sfp.c b/shell/cmd_sfp.c
index f4ffe554f..cbc199cef 100644
--- a/shell/cmd_sfp.c
+++ b/shell/cmd_sfp.c
@@ -6,6 +6,7 @@
 		Subcommands:
 			add vendor_type delta_tx delta_rx alpha - adds an SFP to the database, with given alpha/delta_rx/delta_rx values
 			show - shows the SFP database
+      match - tries to get calibration parameters from DB for a detected SFP
       erase - cleans the SFP database
 			detect - detects the transceiver type		
 */
@@ -20,10 +21,10 @@ int cmd_sfp(const char *args[])
 {
   int8_t sfpcount=1, i, temp;
   struct s_sfpinfo sfp;
+  static char pn[SFP_PN_LEN+1] = "\0";
 
 	if(args[0] && !strcasecmp(args[0], "detect"))
 	{
-		char pn[17];
 		if(!sfp_present())
 			mprintf("No SFP.\n");
 		else
@@ -85,6 +86,25 @@ int cmd_sfp(const char *args[])
       mprintf(" dTx: %d, dRx: %d, alpha: %d\n", sfp.dTx, sfp.dRx, sfp.alpha);
     }
   }
+  else if (args[0] && !strcasecmp(args[0], "match"))
+  {
+    if(pn[0]=='\0')
+    {
+      mprintf("Run sfp detect first\n");
+      return 0;
+    }
+    strncpy(sfp.pn, pn, SFP_PN_LEN);
+    if(eeprom_match_sfp(WRPC_FMC_I2C, FMC_EEPROM_ADR, &sfp) > 0)
+    {
+      mprintf("SFP matched, dTx=%d, dRx=%d, alpha=%d\n", sfp.dTx, sfp.dRx, sfp.alpha);
+      sfp_deltaTx = sfp.dTx;
+      sfp_deltaRx = sfp.dRx;
+      sfp_alpha = sfp.alpha;
+    }
+    else
+      mprintf("Could not match to DB\n");
+    return 0;
+  }
 	
 	return 0;
 }
-- 
GitLab