📄 command.vb
字号:
'Copyright (c) 2005,
' HeSicong of UESTC, Dreamworld Site(http://www.hesicong.com), All rights reserved.
'Redistribution and use in source and binary forms, with or without modification,
'are permitted provided that the following conditions are met:
'1.Redistributions of source code must retain the above copyright notice, this list
' of conditions and the following disclaimer.
'2.Redistributions in binary form must reproduce the above copyright notice, this
' list of conditions and the following disclaimer in the documentation and/or other
' materials provided with the distribution.
'3.Neither the name of the Dreamworld nor the names of its contributors may be
' used to endorse or promote products derived from this software without specific prior
' written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
'CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
'DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
'DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
'IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
'OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Imports System.IO.Ports
Imports System.Text.Encoding
Imports system.Text
Imports Dreamworld.Protocol.OBEXClient
Imports Dreamworld.Protocol.OBEXClient.Header
Namespace Dreamworld.Protocol.OBEXClient
Public MustInherit Class Command
Protected mPort As SerialPort
Protected mFolderListingServiceUUID As Byte() = {&HF9, &HEC, &H7B, &HC4, &H95, &H3C, &H11, &HD2, &H98, &H4E, &H52, &H54, &H0, &HDC, &H9E, &H9}
Protected mIrMCServiceUUID As Byte() = ASCII.GetBytes("IRMC-SYNC")
Protected mConnectionID As Integer = -1
Protected mMaxClientPacketSize As Integer = 400
Protected mMaxServerPacketSize As Integer
Protected mOBEXTimeOut As Integer = 10000 '10s
Protected mAbort As Boolean 'About current process
Protected mLastError As PacketDecoder.PacketStructure.EnumResponseCode
Public TotalByteSend As Integer
Public TotalByteReceived As Integer
''' <summary>
''' A serial port to perform OBEX commands.
''' </summary>
''' <value>A serial port</value>
''' <returns>A serial port</returns>
''' <remarks></remarks>
Public Property SerialPort() As SerialPort
Get
Return mPort
End Get
Set(ByVal value As SerialPort)
mPort = value
End Set
End Property
''' <summary>
''' Set or Get UUID of Folder Listing Service
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property FolderListingServiceUUID() As Byte()
Get
Return mFolderListingServiceUUID
End Get
Set(ByVal value As Byte())
mFolderListingServiceUUID = value
End Set
End Property
''' <summary>
''' Set or Get UUID of IrMC Service
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property IrMCServiceUUID() As Byte()
Get
Return mIrMCServiceUUID
End Get
Set(ByVal value As Byte())
mIrMCServiceUUID = value
End Set
End Property
''' <summary>
''' Set or Get max packet size a client can handle.Default 400
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property MaxClientPacketSize() As Integer
Get
Return mMaxClientPacketSize
End Get
Set(ByVal value As Integer)
mMaxClientPacketSize = value
End Set
End Property
''' <summary>
''' Get last error.
''' </summary>
''' <value></value>
''' <returns>Header Value of response code</returns>
''' <remarks></remarks>
Public Property LastError() As PacketDecoder.PacketStructure.EnumResponseCode
Get
Return mLastError
End Get
Set(ByVal value As PacketDecoder.PacketStructure.EnumResponseCode)
mLastError = value
End Set
End Property
''' <summary>
''' Get max packet size a client can handle.
''' </summary>
''' <value></value>
''' <returns>Size returned by the server.</returns>
''' <remarks></remarks>
Public ReadOnly Property MaxServerPacketSize() As Integer
Get
Return mMaxServerPacketSize
End Get
End Property
''' <summary>
''' Set or Get timeout of sending OBEX packets. Default 10s
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property TimeOut() As Integer
Get
Return mOBEXTimeOut
End Get
Set(ByVal value As Integer)
mOBEXTimeOut = value
End Set
End Property
Sub New(ByVal serialPort As SerialPort)
mPort = serialPort
mPort.NewLine = vbCrLf
End Sub
''' <summary>
''' Connect to OBEX.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public MustOverride Function EnterOBEX() As Boolean
''' <summary>
''' Exit OBEX mode
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public MustOverride Function ExitOBEX() As Boolean
''' <summary>
''' Connect To Folder Listing Service. Default UUID is F9EC7BC4-953C-11d2-984E-525400DC9E09
''' </summary>
''' <returns>True if success;False if failed.</returns>
''' <remarks></remarks>
Public Overridable Function ConnectToFolderListingService() As Boolean
Dim conn As New PacketEncoder.ConnectRequestPacket()
conn.MaxOBEXPacketLength = mMaxClientPacketSize
conn.AddHeader(New TargetHeader(mFolderListingServiceUUID))
Dim data As Byte() = Send(conn.ToByte)
Dim rsp As New PacketDecoder.ConnectionResponseData
rsp = PacketDecoder.DecodeConnectionResponse(data)
mMaxServerPacketSize = rsp.MaxOBEXPacketLength
Dim detail As New PacketDecoder.PacketDetail
detail = PacketDecoder.GetDetail(rsp.PacketBase)
mConnectionID = detail.ConnectionID
If rsp.PacketBase.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then
Return True
Else
mLastError = rsp.PacketBase.ResponseCode
Return False
End If
End Function
''' <summary>
''' Connect to IrMC Serivce. Default target UUID is the ASCII code fo string "IRMC-SYNC"
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function ConnectToIrMCSerivce() As Boolean
Dim conn As New PacketEncoder.ConnectRequestPacket()
conn.MaxOBEXPacketLength = mMaxClientPacketSize
conn.AddHeader(New TargetHeader(mIrMCServiceUUID))
Dim data As Byte() = Send(conn.ToByte)
Dim rsp As New PacketDecoder.ConnectionResponseData
rsp = PacketDecoder.DecodeConnectionResponse(data)
mMaxServerPacketSize = rsp.MaxOBEXPacketLength
Dim detail As New PacketDecoder.PacketDetail
detail = PacketDecoder.GetDetail(rsp.PacketBase)
mConnectionID = detail.ConnectionID
If rsp.PacketBase.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then
Return True
Else
mLastError = rsp.PacketBase.ResponseCode
Return False
End If
End Function
''' <summary>
''' Disconnect from current service
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function Disconnect() As Boolean
Dim packet As New PacketEncoder
packet.OperateCode = PacketEncoder.OpCode.DISCONNECT
packet.FinalBitSet = True
If mConnectionID <> -1 Then
packet.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
Dim data As Byte() = Send(packet.ToByteData)
Dim rsp As New PacketDecoder.PacketStructure
rsp = PacketDecoder.Decode(data)
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then
Return True
Else
mLastError = rsp.ResponseCode
Return False
End If
End Function
''' <summary>
''' Retrive current Folder.
''' </summary>
''' <returns>Data from server. Normally a XML File (UTF8 encoding)</returns>
''' <remarks>
''' To retrieve the current folder:
''' Send a GET Request with an empty Name header and a Typeheader that specifies the folder object type.
''' </remarks>
Public Overridable Function GetCurrentFolderList() As String
Dim encoder As New PacketEncoder
encoder.OperateCode = PacketEncoder.OpCode.GET
If mConnectionID >= 0 Then
encoder.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
encoder.AddHeader(New TypeHeader(ASCII.GetBytes("x-obex/folder-listing" & Chr(0))))
'encoder.AddHeader(New TypeHeader(ASCII.GetBytes("x-obex/folder-listing")))
Dim DirListXML As New StringBuilder
Do
Dim Response As Byte()
'Send command
Response = Send(encoder.ToByteData)
'Decode OBEX Packet
Dim Decoder As New PacketDecoder
Dim Detail As PacketDecoder.PacketDetail
Detail = PacketDecoder.GetDetail(PacketDecoder.Decode(Response))
'Add to Response String
Select Case Detail.ResponseCode
Case PacketDecoder.PacketStructure.EnumResponseCode.Continue
encoder = New PacketEncoder
encoder.OperateCode = PacketEncoder.OpCode.GET
DirListXML.Append(Encoding.UTF8.GetString(Detail.BodyHeader))
Case PacketDecoder.PacketStructure.EnumResponseCode.Success
DirListXML.Append(Encoding.UTF8.GetString(Detail.BodyHeader))
Exit Do
Case Else
mLastError = Detail.ResponseCode
Throw New Exception("Can't get dir list")
End Select
Loop
Return DirListXML.ToString
End Function
''' <summary>
''' Get files and directories under the path. Current folder will be set to path.
''' </summary>
''' <param name="path"></param>
''' <returns>Data from server. Normally a XML File (UTF8 encoding)</returns>
''' <remarks>
''' First change folder and the get current folder list.
''' </remarks>
Public Overridable Function GetFolderList(ByVal path As String) As String
If SetPath(path) = False Then
Throw New Exception("Can't set path")
End If
Return GetCurrentFolderList()
End Function
''' <summary>
''' Back to root path
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function BackToRoot() As Boolean
Dim packet As New PacketEncoder.SetPathPacket()
packet.CreateFolderIfNotExist = False
packet.BackupALevelBeforeApplyingName = False
If mConnectionID >= 0 Then
packet.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
packet.AddHeader(New NameHeader(String.Empty))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -