public void setAuthGroup(IntPtr sdkContext, uint deviceID, bool isMasterDevice) { Console.WriteLine("How many auth groups do you want to set? [1(default)-128]"); Console.Write(">>>> "); char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; int amount = Util.GetInput(1); List <BS2AuthGroup> authGroupList = new List <BS2AuthGroup>(); for (int idx = 0; idx < amount; ++idx) { BS2AuthGroup authGroup = Util.AllocateStructure <BS2AuthGroup>(); Console.WriteLine("Enter a value for auth group[{0}]", idx); Console.WriteLine(" Enter the ID for the auth group which you want to set"); Console.Write(" >>>> "); authGroup.id = (UInt32)Util.GetInput(); Console.WriteLine(" Enter the name for the auth group which you want to set"); Console.Write(" >>>> "); string authGroupName = Console.ReadLine(); if (authGroupName.Length == 0) { Console.WriteLine(" [Warning] auth group name will be displayed as empty."); } else if (authGroupName.Length > BS2Envirionment.BS2_MAX_AUTH_GROUP_NAME_LEN) { Console.WriteLine(" Name of auth group should less than {0} words.", BS2Envirionment.BS2_MAX_AUTH_GROUP_NAME_LEN); return; } else { byte[] authGroupArray = Encoding.UTF8.GetBytes(authGroupName); Array.Clear(authGroup.name, 0, BS2Envirionment.BS2_MAX_AUTH_GROUP_NAME_LEN); Array.Copy(authGroupArray, authGroup.name, authGroupArray.Length); } authGroupList.Add(authGroup); } int structSize = Marshal.SizeOf(typeof(BS2AuthGroup)); IntPtr authGroupListObj = Marshal.AllocHGlobal(structSize * authGroupList.Count); IntPtr curAuthGroupListObj = authGroupListObj; foreach (BS2AuthGroup item in authGroupList) { Marshal.StructureToPtr(item, curAuthGroupListObj, false); curAuthGroupListObj = (IntPtr)((long)curAuthGroupListObj + structSize); } Console.WriteLine("Trying to set auth groups to device."); BS2ErrorCode result = (BS2ErrorCode)API.BS2_SetAuthGroup(sdkContext, deviceID, authGroupListObj, (UInt32)authGroupList.Count); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); } Marshal.FreeHGlobal(authGroupListObj); }
public void getAuthGroup(IntPtr sdkContext, uint deviceID, bool isMasterDevice) { IntPtr authGroupObj = IntPtr.Zero; UInt32 numAuthGroup = 0; BS2ErrorCode result = BS2ErrorCode.BS_SDK_SUCCESS; Console.WriteLine("Do you want to get all auth groups? [Y/n]"); Console.Write(">>>> "); if (Util.IsYes()) { Console.WriteLine("Trying to get all auth gruops from device."); result = (BS2ErrorCode)API.BS2_GetAllAuthGroup(sdkContext, deviceID, out authGroupObj, out numAuthGroup); } else { Console.WriteLine("Enter the ID of the access group which you want to get: [ID_1,ID_2 ...]"); Console.Write(">>>> "); char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; string[] authGroupIDs = Console.ReadLine().Split(delimiterChars); List <UInt32> authGroupIDList = new List <UInt32>(); foreach (string authGroupID in authGroupIDs) { if (authGroupID.Length > 0) { UInt32 item; if (UInt32.TryParse(authGroupID, out item)) { authGroupIDList.Add(item); } } } if (authGroupIDList.Count > 0) { IntPtr authGroupIDObj = Marshal.AllocHGlobal(4 * authGroupIDList.Count); IntPtr curAuthGroupIDObj = authGroupIDObj; foreach (UInt32 item in authGroupIDList) { Marshal.WriteInt32(curAuthGroupIDObj, (Int32)item); curAuthGroupIDObj = (IntPtr)((long)curAuthGroupIDObj + 4); } Console.WriteLine("Trying to get auth gruops from device."); result = (BS2ErrorCode)API.BS2_GetAuthGroup(sdkContext, deviceID, authGroupIDObj, (UInt32)authGroupIDList.Count, out authGroupObj, out numAuthGroup); Marshal.FreeHGlobal(authGroupIDObj); } else { Console.WriteLine("Invalid parameter"); } } if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); } else if (numAuthGroup > 0) { IntPtr curAuthGroupObj = authGroupObj; int structSize = Marshal.SizeOf(typeof(BS2AuthGroup)); for (int idx = 0; idx < numAuthGroup; ++idx) { BS2AuthGroup item = (BS2AuthGroup)Marshal.PtrToStructure(curAuthGroupObj, typeof(BS2AuthGroup)); print(sdkContext, item); curAuthGroupObj = (IntPtr)((long)curAuthGroupObj + structSize); } API.BS2_ReleaseObject(authGroupObj); } else { Console.WriteLine(">>> There is no auth group in the device."); } }
void print(IntPtr sdkContext, BS2AuthGroup authGroup) { Console.WriteLine(">>>> AuthGroup id[{0}] name[{1}]", authGroup.id, Encoding.UTF8.GetString(authGroup.name).TrimEnd('\0')); }