Skip to content

Commit

Permalink
refactor(api): hardware information (#460)
Browse files Browse the repository at this point in the history
AB#4550
  • Loading branch information
madhavilosetty-intel authored Dec 13, 2021
1 parent 52044e7 commit fb55dfc
Show file tree
Hide file tree
Showing 11 changed files with 695 additions and 141 deletions.
42 changes: 23 additions & 19 deletions src/amt/CIRAHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class CIRAHandler {
httpHandler: HttpHandler
username: string
password: string
channel: CIRAChannel
constructor (httpHandler: HttpHandler, username: string, password: string) {
this.username = username
this.password = password
Expand All @@ -52,8 +53,11 @@ export class CIRAHandler {

// Setup CIRA Channel
SetupCiraChannel (socket: CIRASocket, targetPort: number): CIRAChannel {
if (this.channel?.state === 2) {
return this.channel
}
const sourcePort = (socket.tag.nextsourceport++ % 30000) + 1024
const channel: CIRAChannel = {
this.channel = {
targetport: targetPort,
channelid: socket.tag.nextchannelid++,
socket: socket,
Expand All @@ -63,34 +67,33 @@ export class CIRAHandler {
amtCiraWindow: 0,
ciraWindow: 32768
}
APFProcessor.SendChannelOpen(channel.socket, false, channel.channelid, channel.ciraWindow, channel.socket.tag.host, channel.targetport, '1.2.3.4', sourcePort)
APFProcessor.SendChannelOpen(this.channel.socket, false, this.channel.channelid, this.channel.ciraWindow, this.channel.socket.tag.host, this.channel.targetport, '1.2.3.4', sourcePort)

// This function closes this CIRA channel
channel.close = (): void => {
if (channel.state === 0 || channel.closing === 1) return
if (channel.state === 1) {
channel.closing = 1
channel.state = 0
if (channel.onStateChange) {
channel.onStateChange(channel, channel.state)
this.channel.close = (): void => {
if (this.channel.state === 0 || this.channel.closing === 1) return
if (this.channel.state === 1) {
this.channel.closing = 1
this.channel.state = 0
if (this.channel.onStateChange) {
this.channel.onStateChange(this.channel, this.channel.state)
}
return
}
channel.state = 0
channel.closing = 1
APFProcessor.SendChannelClose(channel.socket, channel.amtchannelid)
if (channel.onStateChange) {
channel.onStateChange(channel, channel.state)
this.channel.state = 0
this.channel.closing = 1
APFProcessor.SendChannelClose(this.channel.socket, this.channel.amtchannelid)
if (this.channel.onStateChange) {
this.channel.onStateChange(this.channel, this.channel.state)
}
}

channel.sendchannelclose = (): void => {
console.log('Channel closed called')
APFProcessor.SendChannelClose(channel.socket, channel.amtchannelid)
this.channel.sendchannelclose = (): void => {
APFProcessor.SendChannelClose(this.channel.socket, this.channel.amtchannelid)
}

socket.tag.channels[channel.channelid] = channel
return channel
socket.tag.channels[this.channel.channelid] = this.channel
return this.channel
}

async Enumerate (socket: CIRASocket, rawXml: string): Promise<Response<Enumerate>> {
Expand All @@ -111,6 +114,7 @@ export class CIRAHandler {
result = await this.Go(this.SetupCiraChannel(socket, amtPort), rawXml)
} catch (error) {
if (error.message === 'Unauthorized') {
this.channel.state = 0
result = await this.Go(this.SetupCiraChannel(socket, amtPort), rawXml)
} else {
throw error
Expand Down
106 changes: 104 additions & 2 deletions src/amt/ConnectedDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ import { CIRAChannel, CIRAHandler } from './CIRAHandler'
import { logger } from '../utils/logger'
import { Methods as AMT_Methods, AMT } from './amt/index'
import { Methods as IPS_Methods, IPS } from './ips/index'
import { CIM_AssociatedPowerManagementService, CIM_SoftwareIdentity } from './models/cim_models'
import {
CIM_AssociatedPowerManagementService,
CIM_BIOSElement,
CIM_Card,
CIM_Chassis,
CIM_Chip,
CIM_ComputerSystemPackage,
CIM_MediaAccessDevice,
CIM_PhysicalMemory,
CIM_PhysicalPackage,
CIM_Processor,
CIM_SoftwareIdentity,
CIM_SystemPackaging
} from './models/cim_models'
import { AMT_GeneralSettings, AMT_BootCapabilities, AMT_SetupAndConfigurationService } from './models/amt_models'
import { Pull, Response } from './models/common'
import { CancelOptIn_OUTPUT, SendOptInCode_OUTPUT, StartOptIn_OUTPUT } from './models/ips_models'
Expand Down Expand Up @@ -114,13 +127,102 @@ export class ConnectedDevice {
return getResponse
}

async getComputerSystemPackage (): Promise<Response<CIM_ComputerSystemPackage>> {
const xmlRequestBody = this.cim.ComputerSystemPackage(CIM_Methods.GET, (this.messageId++).toString())
const getResponse = await this.ciraHandler.Get<CIM_ComputerSystemPackage>(this.ciraSocket, xmlRequestBody)
return getResponse
}

async getChassis (): Promise<Response<CIM_Chassis>> {
const xmlRequestBody = this.cim.Chassis(CIM_Methods.GET, (this.messageId++).toString())
const getResponse = await this.ciraHandler.Get<CIM_Chassis>(this.ciraSocket, xmlRequestBody)
return getResponse
}

async getCard (): Promise<Response<CIM_Card>> {
const xmlRequestBody = this.cim.Card(CIM_Methods.GET, (this.messageId++).toString())
const getResponse = await this.ciraHandler.Get<CIM_Card>(this.ciraSocket, xmlRequestBody)
return getResponse
}

async getBIOSElement (): Promise<Response<CIM_BIOSElement>> {
const xmlRequestBody = this.cim.BIOSElement(CIM_Methods.GET, (this.messageId++).toString())
const getResponse = await this.ciraHandler.Get<CIM_BIOSElement>(this.ciraSocket, xmlRequestBody)
return getResponse
}

async getProcessor (): Promise<Response<Pull<CIM_Processor>>> {
let xmlRequestBody = this.cim.Processor(CIM_Methods.ENUMERATE, (this.messageId++).toString())
const enumResponse = await this.ciraHandler.Enumerate(this.ciraSocket, xmlRequestBody)
if (enumResponse == null) {
logger.error('failed to get CIM Processor')
}
xmlRequestBody = this.cim.Processor(CIM_Methods.PULL, (this.messageId++).toString(), enumResponse.Envelope.Body.EnumerateResponse.EnumerationContext)
const pullResponse = await this.ciraHandler.Pull<CIM_Processor>(this.ciraSocket, xmlRequestBody)
return pullResponse
}

async getPhysicalMemory (): Promise<Response<Pull<CIM_PhysicalMemory>>> {
let xmlRequestBody = this.cim.PhysicalMemory(CIM_Methods.ENUMERATE, (this.messageId++).toString())
const enumResponse = await this.ciraHandler.Enumerate(this.ciraSocket, xmlRequestBody)
if (enumResponse == null) {
logger.error('failed to get CIM PhysicalMemory')
}
xmlRequestBody = this.cim.PhysicalMemory(CIM_Methods.PULL, (this.messageId++).toString(), enumResponse.Envelope.Body.EnumerateResponse.EnumerationContext)
const pullResponse = await this.ciraHandler.Pull<CIM_PhysicalMemory>(this.ciraSocket, xmlRequestBody)
return pullResponse
}

async getMediaAccessDevice (): Promise<Response<Pull<CIM_MediaAccessDevice>>> {
let xmlRequestBody = this.cim.MediaAccessDevice(CIM_Methods.ENUMERATE, (this.messageId++).toString())
const enumResponse = await this.ciraHandler.Enumerate(this.ciraSocket, xmlRequestBody)
if (enumResponse == null) {
logger.error('failed to get CIM Media Access Device')
}
xmlRequestBody = this.cim.MediaAccessDevice(CIM_Methods.PULL, (this.messageId++).toString(), enumResponse.Envelope.Body.EnumerateResponse.EnumerationContext)
const pullResponse = await this.ciraHandler.Pull<CIM_MediaAccessDevice>(this.ciraSocket, xmlRequestBody)
return pullResponse
}

async getPhysicalPackage (): Promise<Response<Pull<CIM_PhysicalPackage>>> {
let xmlRequestBody = this.cim.PhysicalPackage(CIM_Methods.ENUMERATE, (this.messageId++).toString())
const enumResponse = await this.ciraHandler.Enumerate(this.ciraSocket, xmlRequestBody)
if (enumResponse == null) {
logger.error('failed to get CIM Physical Package')
}
xmlRequestBody = this.cim.PhysicalPackage(CIM_Methods.PULL, (this.messageId++).toString(), enumResponse.Envelope.Body.EnumerateResponse.EnumerationContext)
const pullResponse = await this.ciraHandler.Pull<CIM_PhysicalPackage>(this.ciraSocket, xmlRequestBody)
return pullResponse
}

async getSystemPackaging (): Promise<Response<Pull<CIM_SystemPackaging>>> {
let xmlRequestBody = this.cim.SystemPackaging(CIM_Methods.ENUMERATE, (this.messageId++).toString())
const enumResponse = await this.ciraHandler.Enumerate(this.ciraSocket, xmlRequestBody)
if (enumResponse == null) {
logger.error('failed to get CIM System Packaging')
}
xmlRequestBody = this.cim.SystemPackaging(CIM_Methods.PULL, (this.messageId++).toString(), enumResponse.Envelope.Body.EnumerateResponse.EnumerationContext)
const pullResponse = await this.ciraHandler.Pull<CIM_SystemPackaging>(this.ciraSocket, xmlRequestBody)
return pullResponse
}

async getChip (): Promise<Response<Pull<CIM_Chip>>> {
let xmlRequestBody = this.cim.Chip(CIM_Methods.ENUMERATE, (this.messageId++).toString())
const enumResponse = await this.ciraHandler.Enumerate(this.ciraSocket, xmlRequestBody)
if (enumResponse == null) {
logger.error('failed to get CIM Chip')
}
xmlRequestBody = this.cim.Chip(CIM_Methods.PULL, (this.messageId++).toString(), enumResponse.Envelope.Body.EnumerateResponse.EnumerationContext)
const pullResponse = await this.ciraHandler.Pull<CIM_Chip>(this.ciraSocket, xmlRequestBody)
return pullResponse
}

async getAuditLog (startIndex: number): Promise<any> {
const xmlRequestBody = this.amt.AuditLog(AMT_Methods.READ_RECORDS, (this.messageId++).toString(), startIndex)
const pullResponse = await this.ciraHandler.Pull<CIM_SoftwareIdentity>(this.ciraSocket, xmlRequestBody)
if (pullResponse == null) {
logger.error('failed to pull CIM_SoftwareIdentity in getAuditLog')
return null
}
return pullResponse
}
}
7 changes: 1 addition & 6 deletions src/amt/connectedDevice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('general settings', () => {
})
})

describe('request user conset code', () => {
describe('user consent code', () => {
it('should request for user consent code ', async () => {
const enumerateSpy = jest.spyOn(device.ciraHandler, 'Get')
enumerateSpy.mockResolvedValueOnce(startOptInResponse)
Expand All @@ -102,9 +102,7 @@ describe('request user conset code', () => {
const result = await device.requestUserConsetCode()
expect(result).toBe(null)
})
})

describe('cancel user conset code', () => {
it('should cancel for user consent code ', async () => {
const enumerateSpy = jest.spyOn(device.ciraHandler, 'Get')
enumerateSpy.mockResolvedValueOnce(cancelOptInResponse)
Expand All @@ -117,9 +115,6 @@ describe('cancel user conset code', () => {
const result = await device.cancelUserConsetCode()
expect(result).toBe(null)
})
})

describe('send user conset code', () => {
it('should send for user consent code ', async () => {
const enumerateSpy = jest.spyOn(device.ciraHandler, 'Get')
enumerateSpy.mockResolvedValueOnce(sendOptInCodeResponse)
Expand Down
12 changes: 6 additions & 6 deletions src/amt/models/amt_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface amtAuthenticateObject {
certificates?: number[]
}

export interface AMT_GeneralSettings extends CIM_SettingData<AMT_GeneralSettings> {
export interface AMT_GeneralSettings extends CIM_SettingData {
NetworkInterfaceEnabled?: boolean
DigestRealm?: string
IdleWakeTimeout?: number
Expand All @@ -41,7 +41,7 @@ export interface AMT_GeneralSettings extends CIM_SettingData<AMT_GeneralSettings
AMTAuthenticate?: (mcNonce: number) => amtAuthenticateObject
}

export interface AMT_EthernetPortSettings extends CIM_SettingData<AMT_EthernetPortSettings> {
export interface AMT_EthernetPortSettings extends CIM_SettingData {
VLANTag?: number
SharedMAC?: boolean
MACAddress?: string
Expand Down Expand Up @@ -83,15 +83,15 @@ export interface RemoteAccessPolicyRule {
ExtendedData?: string
}

export interface AMT_EnvironmentDetectionSettingData extends CIM_SettingData<AMT_EnvironmentDetectionSettingData> {
export interface AMT_EnvironmentDetectionSettingData extends CIM_SettingData {
DetectionAlgorithm?: number
DetectionStrings?: string[]
DetectionIPv6LocalPrefixes?: string[]
SetSystemDefensePolicy?: (policy: AMT_SystemDefensePolicy) => number
EnableVpnRouting?: (enable: boolean) => number
}

export interface AMT_SystemDefensePolicy extends CIM_ManagedElement<AMT_SystemDefensePolicy> {
export interface AMT_SystemDefensePolicy extends CIM_ManagedElement {
PolicyName?: string
PolicyPrecedence?: number
AntiSpoofingSupport?: number
Expand All @@ -106,7 +106,7 @@ export interface AMT_SystemDefensePolicy extends CIM_ManagedElement<AMT_SystemDe
SetTimeout?: (number) => number
UpdateStatistics?: (networkInterface: CIM_EthernetPort, resetOnRead: boolean) => number
}
export interface AMT_BootCapabilities extends CIM_ManagedElement<AMT_BootCapabilities>{
export interface AMT_BootCapabilities extends CIM_ManagedElement{
AMT_BootCapabilities: {
// The user friendly name for this instance of Capabilities . . .
ElementName: string
Expand Down Expand Up @@ -170,7 +170,7 @@ export interface AMT_BootCapabilities extends CIM_ManagedElement<AMT_BootCapabil
PlatformErase: number
}
}
export interface AMT_BootSettingData extends CIM_BootSettingData<AMT_BootSettingData> {
export interface AMT_BootSettingData extends CIM_BootSettingData {
UseSOL?: boolean
UseSafeMode?: boolean
ReflashBIOS?: boolean
Expand Down
Loading

0 comments on commit fb55dfc

Please sign in to comment.