Skip to content

Commit 135792a

Browse files
committed
add attributes required by add printer wizard
1 parent 4d259bb commit 135792a

File tree

2 files changed

+86
-29
lines changed

2 files changed

+86
-29
lines changed

SharpIppNextServer/Models/PrinterOptions.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,47 @@ namespace SharpIppNextServer.Models;
44

55
public class PrinterOptions
66
{
7-
public string Name { get; set; } = "SharpIpp";
7+
public string Name { get; set; } = "SharpIppNext";
8+
public string DnsSdName { get; set; } = "SharpIppNext [231076]";
9+
public Guid UUID { get; set; } = new Guid("d178b387-3a93-4d17-a561-007f876c4901");
10+
public string FirmwareName { get; set; } = "SIN22183498";
811
public Sides[] Sides { get; set; } = [SharpIpp.Protocol.Models.Sides.OneSided];
912
public PrintScaling[] PrintScaling { get; set; } = [SharpIpp.Protocol.Models.PrintScaling.Auto];
10-
public string[] Media { get; set; } = ["iso_a4_210x297mm"];
13+
public string[] Media { get; set; } = [
14+
"iso_a4_210x297mm",
15+
"na_executive_7.25x10.5in",
16+
"na_letter_8.5x11in",
17+
"na_legal_8.5x14in",
18+
"na_govt-letter_8x10in",
19+
"na_invoice_5.5x8.5in",
20+
"iso_a5_148x210mm",
21+
"jis_b5_182x257mm",
22+
"jpn_hagaki_100x148mm",
23+
"iso_a6_105x148mm",
24+
"na_index-4x6_4x6in",
25+
"na_index-5x8_5x8in",
26+
"na_index-3x5_3x5in",
27+
"na_monarch_3.875x7.5in",
28+
"na_number-10_4.125x9.5in",
29+
"iso_dl_110x220mm",
30+
"iso_c5_162x229mm",
31+
"iso_c6_114x162mm",
32+
"na_a2_4.375x5.75in",
33+
"jpn_chou3_120x235mm",
34+
"jpn_chou4_90x205mm",
35+
"oe_photo-l_3.5x5in",
36+
"jpn_photo-2l_127x177.8mm",
37+
"na_5x7_5x7in",
38+
"oe_photo_4x5in",
39+
"na_personal_3.625x6.5in",
40+
"iso_b5_176x250mm",
41+
"om_small-photo_100x150mm",
42+
"na_foolscap_8.5x13in",
43+
"custom_min_3x5in",
44+
"custom_max_8.5x14in",
45+
"stationery",
46+
"photographic-glossy"
47+
];
1148
public Resolution[] Resolution { get; set; } = [new(600, 600, ResolutionUnit.DotsPerInch)];
1249
public Finishings[] Finishings { get; set; } = [SharpIpp.Protocol.Models.Finishings.None];
1350
public PrintQuality[] PrintQuality { get; set; } = [SharpIpp.Protocol.Models.PrintQuality.High];

SharpIppNextServer/Services/PrinterService.cs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Options;
77
using SharpIpp.Exceptions;
88
using SharpIppNextServer.Models;
9+
using System.Text;
910

1011
namespace SharpIppNextServer.Services;
1112

@@ -35,7 +36,7 @@ public async Task ProcessRequestAsync(Stream inputStream, Stream outputStream)
3536
IIppResponseMessage response = await GetResponseAsync(request);
3637
IIppResponseMessage rawResponse = await sharpIppServer.CreateRawResponseAsync(response);
3738
ImproveRawResponse(request, rawResponse);
38-
await sharpIppServer.SendRawResponseAsync(response, outputStream);
39+
await sharpIppServer.SendRawResponseAsync(rawResponse, outputStream);
3940
}
4041
catch (IppRequestException ex)
4142
{
@@ -97,22 +98,24 @@ private void ImproveRawResponse(IIppRequest request, IIppResponseMessage rawResp
9798

9899
private void ImproveGetPrinterAttributesRawResponse(GetPrinterAttributesRequest request, IIppResponseMessage rawResponse)
99100
{
100-
if (request.OperationAttributes is null
101-
|| request.OperationAttributes.RequestedAttributes is null || request.OperationAttributes.RequestedAttributes.Length == 0
102-
|| request.OperationAttributes.RequestedAttributes.All(x => x == string.Empty)
103-
|| request.OperationAttributes.RequestedAttributes.Any(x => x == "all"))
104-
return;
105101
var section = rawResponse.Sections.FirstOrDefault(x => x.Tag == SectionTag.PrinterAttributesTag);
106102
if(section is null)
107103
return;
108-
foreach (var attributeName in request.OperationAttributes.RequestedAttributes.Where(x => !string.IsNullOrEmpty(x)))
109-
{
110-
var attribute = section.Attributes.FirstOrDefault(x => x.Name == attributeName);
111-
if (attribute is not null)
112-
continue;
113-
section.Attributes.Add(new IppAttribute(Tag.NoValue, attributeName, NoValue.Instance));
114-
logger.LogDebug("{name} attribute has been added with no value.", attributeName);
115-
}
104+
bool IsRequired(string attributeName) => !section.Attributes.Any(x => x.Name.Equals(attributeName))
105+
&& IsAttributeRequired(request, attributeName);
106+
var options = printerOptions.Value;
107+
if(IsRequired("printer-device-id"))
108+
section.Attributes.Add(new IppAttribute(Tag.TextWithoutLanguage, "printer-device-id", GetPrinterDeviceId()));
109+
if(IsRequired("printer-uuid"))
110+
section.Attributes.Add(new IppAttribute(Tag.Uri, "printer-uuid", $"urn:uuid:{options.UUID}"));
111+
if(IsRequired("printer-dns-sd-name"))
112+
section.Attributes.Add(new IppAttribute(Tag.NameWithoutLanguage, "printer-dns-sd-name", options.DnsSdName));
113+
if(IsRequired("printer-make-and-model"))
114+
section.Attributes.Add(new IppAttribute(Tag.TextWithoutLanguage, "printer-make-and-model", options.Name));
115+
if(IsRequired("printer-firmware-name"))
116+
section.Attributes.Add(new IppAttribute(Tag.NameWithoutLanguage, "printer-firmware-name", options.FirmwareName));
117+
if(IsRequired("printer-firmware-string-version"))
118+
section.Attributes.Add(new IppAttribute(Tag.TextWithoutLanguage, "printer-firmware-string-version", options.FirmwareName));
116119
}
117120

118121
private ValidateJobResponse GetValidateJobResponse(ValidateJobRequest request)
@@ -126,6 +129,22 @@ private ValidateJobResponse GetValidateJobResponse(ValidateJobRequest request)
126129
};
127130
}
128131

132+
private static string GetPrinterDeviceId()
133+
{
134+
return new StringBuilder()
135+
.Append("MFG:danielklecha;") //Manufacturer
136+
.Append("MDL:SharpIppNext1;") //Model
137+
.Append("CMD:Automatic,JPEG;") //Command Set
138+
.Append("CLS:PRINTER") //Class
139+
.Append("DES:SIN1;") //Designator or Description
140+
.Append("CID:SharpIppNext_1;") //Compatible ID
141+
.Append("LEDMDIS:USB#FF#CC#00,USB#07#01#02,USB#FF#04#01;") //Legacy Device ID String
142+
.Append("SN:SIN279BJ23J07PX;") //Serial Number
143+
.Append("S:038000C480a00001002c240005ac1400032;") //Status
144+
.Append("Z:05000008000009,12000,17000000000000,181;") //Vendor-Specific
145+
.ToString();
146+
}
147+
129148
private async Task<SendUriResponse> GetSendUriResponseAsync(SendUriRequest request)
130149
{
131150
var response = new SendUriResponse
@@ -318,20 +337,21 @@ private async Task<HoldJobResponse> GetHoldJobResponseAsync(HoldJobRequest reque
318337
return response;
319338
}
320339

340+
private static bool IsAttributeRequired(GetPrinterAttributesRequest request, string attributeName)
341+
{
342+
return request.OperationAttributes is null
343+
|| request.OperationAttributes.RequestedAttributes is null
344+
|| request.OperationAttributes.RequestedAttributes.Length == 0
345+
|| request.OperationAttributes.RequestedAttributes.All(x => x == string.Empty)
346+
|| request.OperationAttributes.RequestedAttributes.Any(x => x.Equals("all", StringComparison.InvariantCultureIgnoreCase))
347+
|| request.OperationAttributes.RequestedAttributes.Contains(attributeName);
348+
}
349+
321350
private GetPrinterAttributesResponse GetGetPrinterAttributesResponse(GetPrinterAttributesRequest request)
322351
{
323352
var options = printerOptions.Value;
324353
var allAttributes = PrinterAttribute.GetAttributes(request.Version).ToList();
325-
bool IsRequired(string attributeName)
326-
{
327-
if (request.OperationAttributes is null)
328-
return true;
329-
if (request.OperationAttributes.RequestedAttributes is null || request.OperationAttributes.RequestedAttributes.Length == 0)
330-
return true;
331-
if (request.OperationAttributes.RequestedAttributes.All(x => x == string.Empty))
332-
return true;
333-
return request.OperationAttributes.RequestedAttributes.Contains(attributeName);
334-
}
354+
bool IsRequired(string attributeName) => IsAttributeRequired(request, attributeName);
335355
logger.LogInformation("System returned printer attributes");
336356
return new GetPrinterAttributesResponse
337357
{
@@ -379,7 +399,7 @@ bool IsRequired(string attributeName)
379399
PrinterLocation = !IsRequired(PrinterAttribute.PrinterLocation) ? null : "Internet",
380400
PrintScalingDefault = !IsRequired(PrinterAttribute.PrintScalingDefault) ? null : options.PrintScaling.FirstOrDefault(),
381401
PrintScalingSupported = !IsRequired(PrinterAttribute.PrintScalingSupported) ? null : options.PrintScaling,
382-
PrinterUriSupported = !IsRequired(PrinterAttribute.PrinterUriSupported) ? null : [GetPrinterUrl()],
402+
PrinterUriSupported = !IsRequired(PrinterAttribute.PrinterUriSupported) ? null : [GetPrinterUrl("/ipp/print")],
383403
UriAuthenticationSupported = !IsRequired(PrinterAttribute.UriAuthenticationSupported) ? null : [UriAuthentication.None],
384404
UriSecuritySupported = !IsRequired(PrinterAttribute.UriSecuritySupported) ? null : [GetUriSecuritySupported()],
385405
PrinterUpTime = !IsRequired(PrinterAttribute.PrinterUpTime) ? null : (int)(dateTimeOffsetProvider.UtcNow - _startTime).TotalSeconds,
@@ -661,10 +681,10 @@ private async Task<PrintJobResponse> GetPrintJobResponseAsync(PrintJobRequest re
661681
return response;
662682
}
663683

664-
private string GetPrinterUrl()
684+
private string GetPrinterUrl(string? path = null)
665685
{
666686
var request = httpContextAccessor.HttpContext?.Request ?? throw new Exception("Unable to access HttpContext");
667-
return $"ipp://{request.Host}{request.PathBase}{request.Path}";
687+
return $"ipp://{request.Host}{request.PathBase}{(path is null ? request.Path : path)}";
668688
}
669689

670690
private string GetPrinterMoreInfo()

0 commit comments

Comments
 (0)