public byte GetNumberOfPages(PageDetails pageDetails) { int numberOfbytes = 0; int pages = 0; for (int i = 0; i < pageDetails.details.Count; i++) { if (pageDetails.details[i].NumberOfBytes == 0) { pages += pageDetails.details[i].StuctNumberOfPages; } else { numberOfbytes += pageDetails.details[i].NumberOfBytes; } } if (numberOfbytes != 0) { float pagesInfloat = ((float)numberOfbytes / (float)pageSize); if (pagesInfloat % 1 != 0) { pagesInfloat += 0.5f; } pages = (int)Math.Round(pagesInfloat, MidpointRounding.AwayFromZero); } return(Convert.ToByte(pages)); }
public void CreateEEPROMPropsFromByteArr(PageDetails details, Dictionary <object, object> ByteArrDic, PropertyInfo[] pinf, object obj) { EEPROM_Props val = null; for (int i = 0; i < details.NumberOfStructures; i++) { StructureDetails structDetails = details.details[i]; if (structDetails.NumberOfBytes == 0) { object result = GetSettingsInstanceFromStructureType(structDetails.TypeOfStructure); StrutureTypes st = (StrutureTypes)structDetails.TypeOfStructure; byte[] arr = byteArrList[structDetails.StructStartPage]; PropertyInfo[] propInf = result.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); val = EEPROM_Props.CreateIVLProperties(EEPROM_Type_Info.CreateEEPROM_TypeInfo(DataTypes.Tree, structDetails.TypeOfStructure), result); val.name = st.ToString(); val.text = resourceManager.GetString(val.name); val.PageDetails = GetPageDetailsFromByteArr(arr); Dictionary <object, object> dic = new Dictionary <object, object>(); offset = 0; CreateEEPROMPropsFromByteArr(val.PageDetails, dic, propInf, result); ByteArrDic.Add(val, dic); } else { val = pinf[i].GetValue(obj) as EEPROM_Props; val.name = pinf[i].Name; val.text = resourceManager.GetString(val.name); byte[] arr = new Byte[details.details[i].NumberOfBytes]; if (details.details[i].StuctNumberOfPages > 1) { byte[] byteArr1 = byteArrList[details.details[i].StructStartPage]; Array.Copy(byteArr1, offset, arr, 0, pageSize - offset); byte[] byteArr2 = byteArrList[details.details[i].StructStartPage + 1]; Array.Copy(byteArr2, 0, arr, pageSize - offset, arr.Length - (pageSize - offset)); } else { byteArr = byteArrList[details.details[i].StructStartPage]; Array.Copy(byteArr, offset, arr, 0, arr.Length); } val.GetValFromBytes(arr); ByteArrDic.Add("EEPROM." + val.name, val); offset += details.details[i].NumberOfBytes; if (offset > pageSize) { offset = (byte)((int)offset - (int)pageSize); } else if (offset == pageSize) { offset = 0; } } } }
public void GetFields(List <byte []> eepromArr) { finf = detailsPage.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); fieldDic = new Dictionary <object, object>(); resultDic = new Dictionary <object, object>(); detailsPage.pageDetails = new PageDetails(); string versionNum = "10"; detailsPage.pageDetails.versionNumber = Encoding.UTF8.GetBytes(versionNum); byteArr = new byte[pageSize]; byteArrList = new List <byte[]>(); byteArrList.Add(byteArr); byteArr.Initialize(); Array.Copy(detailsPage.pageDetails.versionNumber, 0, byteArr, 0, detailsPage.pageDetails.versionNumber.Length); byteArrList.Add(byteArr); int index = byteArrList.Count - 1; TraverseProperties(finf, detailsPage, fieldDic, detailsPage.pageDetails, index);// populate field dic byte[] tempArr = GetBytesFromPageDetails(detailsPage.pageDetails); Array.Copy(tempArr, detailsPage.pageDetails.versionNumber.Length, byteArrList[index], detailsPage.pageDetails.versionNumber.Length, tempArr.Length - detailsPage.pageDetails.versionNumber.Length); if (eepromArr == null || eepromArr.Count == 0) { byteArr = byteArrList[1].Clone() as byte[]; } else { eepromArr[0].CopyTo(byteArr, 0); } byte[] versionArr = new byte[3]; Array.Copy(byteArr, 0, versionArr, 0, versionArr.Length); string byteArrVersion = Encoding.UTF8.GetString(versionArr); int LastStructIndex = ((int)byteArr[2] * (int)4); byte numberOfPages2Read = (byte)((int)byteArr[LastStructIndex] + (int)byteArr[LastStructIndex + 1]); pageDetails = GetPageDetailsFromByteArr(byteArr); offset = 0; CreateEEPROMPropsFromByteArr(pageDetails, resultDic, null, null); versionNum = "200"; if (versionNum == byteArrVersion) { fieldDic = resultDic; } else { byteArrList = eepromArr; SetFromByteArrDicToCurrentVersionDic(resultDic, fieldDic); } }
public byte[] GetBytesFromPageDetails(PageDetails pageDetails) { byte NumOfPages = 0; byte[] pageDetailsByteArr = new byte[(pageDetails.details.Count * 4) + 4]; pageDetailsByteArr[3] = (byte)pageDetails.details.Count; byte indx = 4; for (int i = 0; i < pageDetails.details.Count; i++) { pageDetailsByteArr[indx] = pageDetails.details[i].StructStartPage; indx += (byte)(1); pageDetailsByteArr[indx] = pageDetails.details[i].StuctNumberOfPages; indx += (byte)(1); pageDetailsByteArr[indx] = pageDetails.details[i].NumberOfBytes; indx += (byte)(1); pageDetailsByteArr[indx] = pageDetails.details[i].TypeOfStructure; indx += (byte)(1); } return(pageDetailsByteArr); }
public PageDetails GetPageDetailsFromByteArr(byte[] Arr) { PageDetails TempPageDetails = new PageDetails(); TempPageDetails.NumberOfStructures = Arr[3]; int indx = 4; for (int i = 0; i < TempPageDetails.NumberOfStructures; i++) { StructureDetails structDetails = new StructureDetails(); structDetails.StructStartPage = Arr[indx]; indx += (byte)(1); structDetails.StuctNumberOfPages = Arr[indx]; indx += (byte)(1); structDetails.NumberOfBytes = Arr[indx]; indx += (byte)(1); structDetails.TypeOfStructure = Arr[indx]; indx += (byte)(1); TempPageDetails.details.Add(structDetails); } return(TempPageDetails); }
public void TraverseProperties(PropertyInfo[] finf, object obj, Dictionary <object, object> dic, PageDetails pageDetails, int byteArrIndx) { EEPROM_Props val = null; pageDetails.NumberOfStructures = (byte)finf.Length; numberOfbytes = 0; for (int i = 0; i < finf.Length; i++) { Type t = finf[i].PropertyType; if (t == typeof(EEPROM_Props)) { val = finf[i].GetValue(obj) as EEPROM_Props; val.name = finf[i].Name; val.text = resourceManager.GetString(val.name); if (val.eepromDataType.dataType == DataTypes.Tree) { offset = 0; val.PageDetails = new PageDetails(); StructureDetails structureDetails = new StructureDetails(); object objProperty = GetSettingsInstanceFromStructureType(val.eepromDataType.length); PropertyInfo[] pinf = objProperty.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); val.PageDetails.NumberOfStructures = Convert.ToByte(pinf.Length); Dictionary <object, Object> temp = new Dictionary <object, object>(); byteArr = new byte[pageSize]; byteArrList.Add(byteArr); int index = byteArrList.Count - 1; TraverseProperties(pinf, objProperty, temp, val.PageDetails, index); byte[] tempArr = GetBytesFromPageDetails(val.PageDetails); Array.Copy(tempArr, byteArrList[index], tempArr.Length); structureDetails.StructStartPage = Convert.ToByte(index); int pages = (int)GetNumberOfPages(val.PageDetails); structureDetails.StuctNumberOfPages = (byte)(pages + 1); // the + 1 is for pageDetails page. structureDetails.TypeOfStructure = val.eepromDataType.length; pageDetails.details.Add(structureDetails); dic.Add(val, temp); } else { dic.Add("EEPROM." + val.name, val); byte[] arr = val.GetBytesFromVal(); byte[] resArr = new byte[pageSize]; if (offset == 0) { byteArrList.Add(resArr); } StructureDetails structureDetails = new StructureDetails(); structureDetails.StructStartPage = (byte)(byteArrList.Count - 1); //structureDetails.OffsetPageIndx = offset; structureDetails.NumberOfBytes = Convert.ToByte(arr.Length);//+ (int) numberOfbytes); numberOfbytes += structureDetails.NumberOfBytes; float pagesInfloat = ((float)numberOfbytes / (float)pageSize); if (pagesInfloat % 1 != 0) { pagesInfloat += 0.5f; } int pages = (int)Math.Round(pagesInfloat, MidpointRounding.AwayFromZero); if (numberOfbytes > pageSize) { structureDetails.StuctNumberOfPages = Convert.ToByte(pages); int pageOverFlow = (((int)structureDetails.StuctNumberOfPages - 1) * (int)pageSize); pageOverFlow -= (int)offset; numberOfbytes = 0; byte[] arr1 = new byte[pageSize]; byte[] arr2 = new byte[pageSize]; int index1 = pageSize - offset; Array.Copy(arr, 0, byteArrList[byteArrList.Count - 1], offset, index1); Array.Copy(arr, index1, arr2, 0, arr.Length - index1); byteArrList.Add(arr2); offset = Convert.ToByte((int)structureDetails.NumberOfBytes - pageOverFlow); } else { structureDetails.StuctNumberOfPages = 1; byte[] arr1 = byteArrList[byteArrList.Count - 1]; Array.Copy(arr, 0, byteArrList[byteArrList.Count - 1], offset, arr.Length); offset += structureDetails.NumberOfBytes; if (numberOfbytes == pageSize) { numberOfbytes = 0; offset = 0; } } pageDetails.details.Add(structureDetails); } } } }