Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
fix: use storage in role manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 7, 2022
1 parent fc92c77 commit b2448a9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
14 changes: 0 additions & 14 deletions Jacdac.Dev.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jacdac", "Jacdac\Jacdac.csproj", "{F6DB89ED-1C71-481F-AAE2-1A3D4563743C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jacdac.Explorer", "Jacdac.Explorer\Jacdac.Explorer.csproj", "{D736AFDA-90FA-472E-AD8E-40E81EBD06B1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BF5CADE3-4BFB-4BEC-A09A-6D40110ADADE}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build.yml = .github\workflows\build.yml
Expand All @@ -18,8 +16,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jacdac.Tests", "Jacdac.Test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jacdac.TinyCLR", "Jacdac.TinyCLR\Jacdac.TinyCLR.csproj", "{FEAE5AA6-08FC-449E-8CE1-6F3697211323}"
EndProject
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Jacdac.Nano", "Jacdac.Nano\Jacdac.Nano.nfproj", "{071C8FF7-00A1-4C77-9192-146C24327E8B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jacdac.NET", "Jacdac.NET\Jacdac.NET.csproj", "{CD87610F-D4CE-455A-AA11-5950747B53FE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jacdac.NET.Playground", "Jacdac.NET.Playground\Jacdac.NET.Playground.csproj", "{C19AE6F7-7D55-4BB3-9621-20FC1AA86253}"
Expand Down Expand Up @@ -52,10 +48,6 @@ Global
{F6DB89ED-1C71-481F-AAE2-1A3D4563743C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6DB89ED-1C71-481F-AAE2-1A3D4563743C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6DB89ED-1C71-481F-AAE2-1A3D4563743C}.Release|Any CPU.Build.0 = Release|Any CPU
{D736AFDA-90FA-472E-AD8E-40E81EBD06B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D736AFDA-90FA-472E-AD8E-40E81EBD06B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D736AFDA-90FA-472E-AD8E-40E81EBD06B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D736AFDA-90FA-472E-AD8E-40E81EBD06B1}.Release|Any CPU.Build.0 = Release|Any CPU
{5540EEE8-5DA1-4FCF-9721-81E65E6244C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5540EEE8-5DA1-4FCF-9721-81E65E6244C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5540EEE8-5DA1-4FCF-9721-81E65E6244C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -64,12 +56,6 @@ Global
{FEAE5AA6-08FC-449E-8CE1-6F3697211323}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEAE5AA6-08FC-449E-8CE1-6F3697211323}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEAE5AA6-08FC-449E-8CE1-6F3697211323}.Release|Any CPU.Build.0 = Release|Any CPU
{071C8FF7-00A1-4C77-9192-146C24327E8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{071C8FF7-00A1-4C77-9192-146C24327E8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{071C8FF7-00A1-4C77-9192-146C24327E8B}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{071C8FF7-00A1-4C77-9192-146C24327E8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{071C8FF7-00A1-4C77-9192-146C24327E8B}.Release|Any CPU.Build.0 = Release|Any CPU
{071C8FF7-00A1-4C77-9192-146C24327E8B}.Release|Any CPU.Deploy.0 = Release|Any CPU
{CD87610F-D4CE-455A-AA11-5950747B53FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD87610F-D4CE-455A-AA11-5950747B53FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD87610F-D4CE-455A-AA11-5950747B53FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
4 changes: 3 additions & 1 deletion Jacdac.TinyCLR.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Start()
var sdStorage = new StorageManager(StorageController.FromName(SC20100.StorageController.SdCard));
var ssidStorage = sdStorage.MountSettingsStorage("wifi.json");
var specStorage = sdStorage.MountSpecificationStorage("services");
var rolesStorage = sdStorage.MountSettingsStorage("roles.json");

// start wifi
Display.WriteLine("Start wifi....");
Expand All @@ -57,7 +58,8 @@ public void Start()
Description = "TinyCLR Demo",
FirmwareVersion = "0.0.0",
Services = new JDServiceServer[] { rtc, protoTest, wifiServer, settingsServer },
SpecificationCatalog = new ServiceSpecificationCatalog(specStorage)
SpecificationCatalog = new ServiceSpecificationCatalog(specStorage),
RoleStorage = rolesStorage
});
bus.DeviceConnected += Bus_DeviceConnected;
bus.DeviceDisconnected += Bus_DeviceDisconnected;
Expand Down
30 changes: 19 additions & 11 deletions Jacdac.TinyCLR.Storage/StorageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ public void Clear()
try
{
var fn = this.FullFileName();
this.Parent.drive.Delete(fn);
var info = new FileInfo(fn);
if (info.Exists)
this.Parent.drive.Delete(fn);
}
finally
{
Expand All @@ -186,16 +188,22 @@ private StorageEntry[] deserialize(string fn)
StorageEntry[] entries = null;
try
{
var bytes = System.IO.File.ReadAllBytes(fn);
var text = UTF8Encoding.UTF8.GetString(bytes);
entries = (StorageEntry[])JsonConverter.DeserializeObject(text, typeof(StorageEntry[]),
(string instancePath, JToken token, Type baseType, string fieldName, int length) =>
{
if (instancePath == "/")
return new StorageEntry();

return null;
});
var info = new FileInfo(fn);
if (!info.Exists)
entries = new StorageEntry[0];
else
{
var bytes = System.IO.File.ReadAllBytes(fn);
var text = UTF8Encoding.UTF8.GetString(bytes);
entries = (StorageEntry[])JsonConverter.DeserializeObject(text, typeof(StorageEntry[]),
(string instancePath, JToken token, Type baseType, string fieldName, int length) =>
{
if (instancePath == "/")
return new StorageEntry();

return null;
});
}
}
catch (Exception ex)
{
Expand Down
6 changes: 4 additions & 2 deletions Jacdac/Bus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Jacdac.Servers;
using System;
using System.Diagnostics;
using System.Threading;

Expand All @@ -12,7 +13,8 @@ public sealed class JDBusOptions
public uint ProductIdentifier;
public bool IsClient = true;
public bool IsPassive = false;
public bool RoleManager = true;
public bool DisableRoleManager = false;
public ISettingsStorage RoleStorage;
public JDServiceServer[] Services;
public ControlAnnounceFlags StatusLight = Platform.StatusLight;
public SetStatusLightHandler SetStatusLight = Platform.SetStatusLight;
Expand Down
6 changes: 3 additions & 3 deletions Jacdac/DeviceServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public JDDeviceServer(JDBus bus, string deviceId, JDBusOptions options)
this.statusLight = options != null ? options.StatusLight : ControlAnnounceFlags.StatusLightNone;
this.DeviceId = deviceId;
this.IsClient = options.IsClient;
this.services = new JDServiceServer[1 + (options.RoleManager ? 1 : 0) + (options.Services != null ? options.Services.Length : 0)];
this.services = new JDServiceServer[1 + (options.DisableRoleManager ? 0 : 1) + (options.Services != null ? options.Services.Length : 0)];
var k = 0;
this.services[k++] = this.Control = new ControlServer(options);
if (options.RoleManager)
this.services[k++] = this.RoleManager = new RoleManagerServer();
if (!options.DisableRoleManager)
this.services[k++] = this.RoleManager = new RoleManagerServer(options.RoleStorage);
if (options.Services != null)
options.Services.CopyTo(this.services, k);
for (byte i = 0; i < services.Length; i++)
Expand Down
14 changes: 7 additions & 7 deletions Jacdac/PacketEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,19 +357,19 @@ static int jdpackCore(byte[] trg, string fmt, object[] data, int off)
else
{
byte[] buf;
if (v is string)
if (v != null && v is byte[])
{
// assume buffer
if (c0 == ch_b) buf = (byte[])v;
else throw new ArgumentException("unexpected buffer");
}
else if (v != null && v is string)
{
var s = (string)v;
if (c0 == ch_z) buf = System.Text.UTF8Encoding.UTF8.GetBytes(s + "\u0000");
else if (c0 == ch_s) buf = System.Text.UTF8Encoding.UTF8.GetBytes(s);
else throw new ArgumentException("unexpected string");
}
else if (v != null && v is byte[])
{
// assume buffer
if (c0 == ch_b) buf = (byte[])v;
else throw new ArgumentException("unexpected buffer");
}
else
{
throw new ArgumentException("expecting string or buffer");
Expand Down
5 changes: 3 additions & 2 deletions Jacdac/Servers/RoleManagerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void handleChanged(JDNode sender, EventArgs e)
private void SaveBindings()
{
if (this.Storage == null) return;

var roles = this.roles;
foreach (var role in roles)
{
Expand All @@ -76,8 +76,9 @@ private void SaveBindings()
if (device != null)
{
var current = this.Storage.Read(role.Name);
var did = HexEncoding.ToBuffer(device.DeviceId);
var payload = PacketEncoding.Pack(PACK_FORMAT, new object[] {
HexEncoding.ToBuffer(device.DeviceId),
did,
role.ServiceClass,
service.ServiceIndex
});
Expand Down
3 changes: 2 additions & 1 deletion Jacdac/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public static bool IsNumber(object value)
|| value is long
|| value is ulong
|| value is float
|| value is double;
|| value is double
|| value is byte;
}

public static byte[] Slice(byte[] source, int start, int end = 0)
Expand Down

0 comments on commit b2448a9

Please sign in to comment.