i have a OBEXConnect and OBEXRequest custom functions, i am not using library for it
OBEXConnect function is as following
private bool OBEXConnect()
{
//send client request
byte[] ConnectPacket = new byte[7];
ConnectPacket[0] = 0x80; // Connect
ConnectPacket[1] = 0x00; // Packetlength Hi Byte
ConnectPacket[2] = 0x07; // Packetlength Lo Byte
ConnectPacket[3] = 0x10; // Obex v1
ConnectPacket[4] = 0x00; // no flags
ConnectPacket[5] = 0x20; // 8k max packet size Hi Byte
ConnectPacket[6] = 0x00; // 8k max packet size Lo Byte
stream.Write(ConnectPacket,0,ConnectPacket.Length);
//listen for server response
byte[] ReceiveBufferA = new byte[3];
stream.Read(ReceiveBufferA,0,3);
if (ReceiveBufferA[0] == 160) // 0xa0
{
//success, decode rest of packet
int plength = (0xff * ReceiveBufferA[1]) + ReceiveBufferA[2]; //length of packet is...
//listen for rest of packet
byte[] ReceiveBufferB = new byte[plength-3];
stream.Read(ReceiveBufferB,0,plength-3);
int obver = ReceiveBufferB[0]; //server obex version (16 = v1.0)
int cflags = ReceiveBufferB[1]; //connect flags
int maxpack = (0xff * ReceiveBufferB[2]) + ReceiveBufferB[3]; //max packet size
return true;
}
else
{
return false;
}
}
and here is OBEXRequest function
private int OBEXRequest(string tReqType, string tName, string tType, string tFileContent)
{
//send client request
int i;
int offset;
int packetsize;
byte reqtype = 0x82;
int tTypeLen = 0x03;
int typeheadsize;
int typesizeHi = 0x00;
int typesizeLo = 0x03;
//tName = "contact.vcf";
//tType = "text/x-vCard";
//tFileContent = "BEGIN:VCARD\r\nVERSION:2.1\r\nN:;aardvark\r\nFN:aardvark\r\nEND:VCARD\r\n";
if (tReqType == "GET")
{
reqtype = 0x83; // 131 GET-Final
}
if (tReqType == "PUT")
{
reqtype = 0x82; // 130 PUT-Final
}
packetsize = 3;
//Name Header
int tNameLength = tName.Length;
int nameheadsize = (3 + (tNameLength*2) + 2);
int namesizeHi = (nameheadsize & 0xff00)/0xff;
int namesizeLo = nameheadsize & 0x00ff;
packetsize = packetsize + nameheadsize;
if (tType != "")
{
//Type Header
tTypeLen = tType.Length;
typeheadsize = 3 + tTypeLen + 1;
typesizeHi = (typeheadsize & 0xff00)/0xff;
typesizeLo = typeheadsize & 0x00ff;
packetsize = packetsize + typeheadsize;
}
//Body
int fileLen = tFileContent.Length;
int fileheadsize = 3 + fileLen ;
int filesizeHi = (fileheadsize & 0xff00)/0xff;;
int filesizeLo = fileheadsize & 0x00ff;;
packetsize = packetsize + fileheadsize;
int packetsizeHi = (packetsize & 0xff00)/0xff;
int packetsizeLo = packetsize & 0x00ff;
byte[] tSendByte = new byte[packetsize];
//PUT-final Header
tSendByte[0] = reqtype; // Request type e.g. PUT-final 130
tSendByte[1] = Convert.ToByte(packetsizeHi); // Packetlength Hi
tSendByte[2] = Convert.ToByte(packetsizeLo); // Packetlength Lo
offset = 2;
//Name Header
tSendByte[offset+1] = 0x01; // HI for Name header
tSendByte[offset+2] = Convert.ToByte(namesizeHi); // Length of Name header (2 bytes per char)
tSendByte[offset+3] = Convert.ToByte(namesizeLo); // Length of Name header (2 bytes per char)
// Name+\n\n in unicode
byte[] tNameU = System.Text.Encoding.BigEndianUnicode.GetBytes(tName);
tNameU.CopyTo(tSendByte,offset+4);
offset = offset + 3 + (tNameLength*2);
tSendByte[offset+1] = 0x00; // null term
tSendByte[offset+2] = 0x00; // null term
offset = offset + 2;
if (tType != "")
{
//Type Header
tSendByte[offset+1] = 0x42; // HI for Type Header 66
tSendByte[offset+2] = Convert.ToByte(typesizeHi); // Length of Type Header
tSendByte[offset+3] = Convert.ToByte(typesizeLo); // Length of Type Header
for (i=0;i<=(tTypeLen-1);i++)
{
tSendByte[offset+4+i] = Convert.ToByte(Convert.ToChar(tType.Substring(i,1)));
}
tSendByte[offset+3+tTypeLen+1] = 0x00; // null terminator
offset = offset+3+tTypeLen+1;
}
//Body
tSendByte[offset+1] = 0x49; //HI End of Body 73
tSendByte[offset+2] = Convert.ToByte(filesizeHi); //
tSendByte[offset+3] = Convert.ToByte(filesizeLo); //1k payload + 3 for HI header
for (i=0;i<=(fileLen-1);i++)
{
tSendByte[offset+4+i] = Convert.ToByte(Convert.ToChar(tFileContent.Substring(i,1)));
}
//tSendByte[offset+4+fileLen] = 0x00; // null terminator
offset = offset+3+fileLen;
stream.Write(tSendByte,0,tSendByte.Length );
//listen for server response
//TODO: can hang here forever waiting response...
bool x = stream.DataAvailable; // changed bluetoothclient - public NetworkStream GetStream()
byte[] tArray4 = new byte[3];
stream.Read(tArray4,0,3);
x = stream.DataAvailable;
if (tArray4[0] == 160) // 0xa0
{
int plength = (tArray4[1] * 256) + tArray4[2] - 3;
byte[] tArray5 = new byte[plength];
if (plength >0)
{
stream.Read(tArray5,0,plength);
//TODO: data in returned packet to deal with
}
return 160;
}
if (tArray4[0] == 197) // 0xc5 Method not allowed
{
return 197;
}
if (tArray4[0] == 192) // 0xc0 Bad Request
{
return 192;
}
return 0;
}
i want to read the phone book of mobile but i not succeeded yet
i try on one samsung mobile it gives error bad request i follow the solutions above mentioned but when name = null then obexrequest function gives error
when i try it on huawei mobile it gives unknown error
i try this code as well it throw error that this method is not implemented
Read contacts using obex in c#
i call these functions like this
string tName = "";
string tType = "text/x-vCard";
string tFileContent = "";
int result = OBEXRequest("GET",tName,tType,tFileContent);
switch (result)
{
case 160: // 0xa0
addtolog("OK");
break;
case 197: // 0xc5
addtolog("Method not allowed");
break;
case 192: // 0xc0
addtolog("Bad Request");
break;
default:
addtolog("Other Error");
break;
}
can any body point out my mistake
Related
I am very new to firmware updates and the whole zephyr rtos. I am trying to perform a firmware update using the mcumgr. I ran into some problems. I read the documentation, but it's not very well written and even the autor phrased that it isn't even complete. So the problem is that i split up my bin file into packets and add the header (i think this is working), but when the transfer completes, the firmware isn't uploaded to any slot on the device. I am learning and implementing this code from boogie https://github.com/boogie/mcumgr-web
This is my code. The DfuSend method runs on a button click.
public async void DfuSend()
{
if (SelectedFirmwareFile == null)
{
await Application.Current.MainPage.DisplayAlert("Error", "No file selected", "OK");
return;
}
var service = await Globals.ConnectedDevice.GetServiceAsync(GattIdentifiers.UartGattSMPServiceId);
if (service != null)
{
try
{
sendCharacteristic = await service.GetCharacteristicAsync(GattIdentifiers.UartGattSMPCharacteristicsId);
receiveCharacteristic = await service.GetCharacteristicAsync(GattIdentifiers.UartGattSMPCharacteristicsId);
uploadIsInProgress = true;
sendCharacteristic.WriteType = CharacteristicWriteType.WithoutResponse;
FirmwareFileBytes = File.ReadAllBytes(SelectedFirmwareFile.FullName);
while (uploadIsInProgress == true)
{
await _uploadNext();
HandleUploadProgress(_uploadOffset);
}
sendCharacteristic.WriteType = CharacteristicWriteType.WithResponse;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
await Application.Current.MainPage.DisplayAlert("Error", "No service found", "OK");
}
The header bytes definition:
byte MGMT_OP_READ = 0;
byte MGMT_OP_READ_RSP = 1;
byte MGMT_OP_WRITE = 2;
byte MGMT_OP_WRITE_RSP = 3;
// Groups
byte MGMT_GROUP_ID_OS = 0;
byte MGMT_GROUP_ID_IMAGE = 1;
byte MGMT_GROUP_ID_STAT = 2;
byte MGMT_GROUP_ID_CONFIG = 3;
byte MGMT_GROUP_ID_LOG = 4;
byte MGMT_GROUP_ID_CRASH = 5;
byte MGMT_GROUP_ID_SPLIT = 6;
byte MGMT_GROUP_ID_RUN = 7;
byte MGMT_GROUP_ID_FS = 8;
byte MGMT_GROUP_ID_SHELL = 9;
// OS group
byte OS_MGMT_ID_ECHO = 0;
byte OS_MGMT_ID_CONS_ECHO_CTRL = 1;
byte OS_MGMT_ID_TASKSTAT = 2;
byte OS_MGMT_ID_MPSTAT = 3;
byte OS_MGMT_ID_DATETIME_STR = 4;
byte OS_MGMT_ID_RESET = 5;
// Image group
byte IMG_MGMT_ID_STATE = 0;
byte IMG_MGMT_ID_UPLOAD = 1;
byte IMG_MGMT_ID_FILE = 2;
byte IMG_MGMT_ID_CORELIST = 3;
byte IMG_MGMT_ID_CORELOAD = 4;
byte IMG_MGMT_ID_ERASE = 5;
private int _seq = 0;
and the uploadnext method:
private async Task _uploadNext()
{
if (_uploadOffset >= SelectedFirmwareFile.Length)
{
uploadIsInProgress = false;
HandleUploadFinished();
return;
}
if (uploadIsInProgress == false)
return;
const int nmpOverhead = 8;
var message = new ImageData();
if (_uploadOffset == 0)
{
message.len = Convert.ToInt32(SelectedFirmwareFile.Length);
using (var sha = SHA256.Create())
{
message.sha = sha.ComputeHash(await File.ReadAllBytesAsync(SelectedFirmwareFile.FullName));
}
}
//the encode function that i am trying to implement
var length = _mtu - Encode(message).Length - nmpOverhead;
message.data = new byte[SelectedFirmwareFile.Length - _uploadOffset > length ? length : SelectedFirmwareFile.Length - _uploadOffset];
Array.Copy(await File.ReadAllBytesAsync(SelectedFirmwareFile.FullName), _uploadOffset, message.data, 0, message.data.Length);
_uploadOffset += message.data.Length;
await SendMessage(MGMT_OP_WRITE, MGMT_GROUP_ID_IMAGE, IMG_MGMT_ID_UPLOAD, message);
}
And then i need to send the packet and encode it using CBOR. There is my code for the SendMessage method (i am really sure that it doesn't work)
async Task SendMessage(byte op, byte group, byte id, ImageData data)
{
var cborMessage = CBORObject.FromObject(new { op, group, id, seq = _seq++, data });
await sendCharacteristic.WriteAsync(cborMessage.EncodeToBytes());
}
BTW the characteristics use the SMP id.
I'll appreciate every comment or an idea. Thank you and have a nice day
I am trying to implement a C# listener for a Concox GPS tracker (model HVT001) using the code examples that are available online but for some reason I am not able to get the Terminal ID (IMEI) correctly and I can't get any location messages after the login
Below is a screen capture of the input from my console window:
As you can see the Terminal ID appears as gibberish and no location messages appear (I'm also getting protocol numbers that I don't recognize)
Any help would be appreciated.
The full code is available here: https://docs.google.com/document/d/1UsF7ocb5CsCI1rxTcJHLP2eejR6vvboD_M8UeadkVmI/edit?usp=sharing
And here is a snippet of the code:
public void ProcessMessages()
{
UInt16 sendCRC = 0;
DateTime date;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
KeyValuePair<List<byte>, StateObject> byteState;
KeyValuePair<UNPACK_STATUS, byte[]> status;
byte[] receiveMessage = null;
StateObject state = null;
byte[] serialNumber = null;
byte[] serverFlagBit = null;
byte[] stringArray = null;
string stringMessage = "";
byte lengthOfCommand = 0;
PROTOCOL_NUMBER protocolNumber = PROTOCOL_NUMBER.NONE;
try
{
Boolean firstMessage = true;
acceptDone.Set();
//loop forever
while (true)
{
allDone.WaitOne();
//read fifo until empty
while (true)
{
//read one connection until buffer doesn't contain any more packets
byteState = ReadWrite(PROCESS_STATE.PROCESS, null, null, -1);
if (byteState.Value.fifoCount == -1) break;
state = byteState.Value;
while (true)
{
status = Unpack(byteState);
if (status.Key == UNPACK_STATUS.NOT_ENOUGH_BYTES)
break;
if (status.Key == UNPACK_STATUS.ERROR)
{
Console.WriteLine("Error : Bad Receive Message, Data");
break;
}
//message is 2 start bytes + 1 byte (message length) + 1 byte message length + 2 end bytes
receiveMessage = status.Value;
int messageLength = receiveMessage[2];
Console.WriteLine("Status : '{0}', Receive Message : '{1}'", status.Key == UNPACK_STATUS.GOOD_MESSAGE ? "Good" : "Bad", BytesToString(receiveMessage.Take(messageLength + 5).ToArray()));
if (status.Key != UNPACK_STATUS.GOOD_MESSAGE)
{
break;
}
else
{
if (firstMessage)
{
if (receiveMessage[3] != 0x01)
{
Console.WriteLine("Error : Expected Login Message : '{0}'", BytesToString(receiveMessage));
break;
}
firstMessage = false;
}
//skip start bytes, message length. then go back 4 bytes (CRC and serial number)
serialNumber = receiveMessage.Skip(2 + 1 + messageLength - 4).Take(2).ToArray();
protocolNumber = (PROTOCOL_NUMBER)receiveMessage[3];
Console.WriteLine("Protocol Number : '{0}'", protocolNumber.ToString());
switch (protocolNumber)
{
case PROTOCOL_NUMBER.LOGIN_MESSAGE:
serialNumber.CopyTo(loginResponse, 4);
sendCRC = crc_bytes(loginResponse.Skip(2).Take(loginResponse.Length - 6).ToArray());
loginResponse[loginResponse.Length - 4] = (byte)((sendCRC >> 8) & 0xFF);
loginResponse[loginResponse.Length - 3] = (byte)((sendCRC) & 0xFF);
string IMEI = Encoding.ASCII.GetString(receiveMessage.Skip(4).Take(messageLength - 5).ToArray());
Console.WriteLine("Received good login message from Serial Number : '{0}', Terminal ID = '{1}'", "0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"), IMEI);
//byteState.Value.IMEI = IMEI;
Console.WriteLine("Send Message : '{0}'", BytesToString(loginResponse));
Send(state.workSocket, loginResponse);
WriteDBMessageLogin loginMessage = new WriteDBMessageLogin() { message = DATABASE_MESSAGE_TYPE.LOGIN, IMEI = IMEI, date = DateTime.Now };
WriteDBAsync.ReadWriteFifo(WriteDBAsync.Mode.WRITE, loginMessage);
Console.WriteLine("Wrote to database");
break;
case PROTOCOL_NUMBER.LOCATION_DATA:
year = receiveMessage[4];
month = receiveMessage[5];
day = receiveMessage[6];
hour = receiveMessage[7];
minute = receiveMessage[8];
second = receiveMessage[9];
date = new DateTime(2000 + year, month, day, hour, minute, second);
WriteDBMessageLocation locationMessage = new WriteDBMessageLocation();
locationMessage.message = DATABASE_MESSAGE_TYPE.LOCATION;
locationMessage.trackTime = date;
locationMessage.currTime = DateTime.Now;
locationMessage.lattitude = new byte[4];
Array.Copy(receiveMessage, 11, locationMessage.lattitude, 0, 4);
locationMessage.longitude = new byte[4];
Array.Copy(receiveMessage, 15, locationMessage.longitude, 0, 4);
locationMessage.speed = receiveMessage[19];
locationMessage.courseStatus = new byte[2];
Array.Copy(receiveMessage, 20, locationMessage.courseStatus, 0, 2);
locationMessage.IMEI = byteState.Value.IMEI;
WriteDBAsync.ReadWriteFifo(WriteDBAsync.Mode.WRITE, locationMessage);
Console.WriteLine("Received good location message from Serial Number '{0}', Time = '{1}'", "0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"), date.ToLongDateString());
break;
case PROTOCOL_NUMBER.ALARM_DATA:
//first response
int alarmPacketLen = alarmResponse.Length - 5;
alarmResponse[2] = (byte)(alarmPacketLen & 0xFF);
serialNumber.CopyTo(alarmResponse, alarmPacketLen - 1);
sendCRC = crc_bytes(alarmResponse.Skip(2).Take(alarmPacketLen - 1).ToArray());
alarmResponse[alarmPacketLen + 1] = (byte)((sendCRC >> 8) & 0xFF);
alarmResponse[alarmPacketLen + 2] = (byte)((sendCRC) & 0xFF);
Console.WriteLine("Send Alarm Response Message : '{0}'", BytesToString(alarmResponse));
Send(state.workSocket, alarmResponse);
//second response
year = receiveMessage[4];
month = receiveMessage[5];
day = receiveMessage[6];
hour = receiveMessage[7];
minute = receiveMessage[8];
second = receiveMessage[9];
date = new DateTime(2000 + year, month, day, hour, minute, second);
Console.WriteLine("Received good alarm message from Serial Number '{0}', Time = '{1}'", "0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"), date.ToLongDateString());
int alarmDataAddressPacketLen = alarmDataAddressResponse.Length - 5;
alarmDataAddressResponse[2] = (byte)(alarmDataAddressPacketLen & 0xFF);
serialNumber.CopyTo(alarmDataAddressResponse, alarmDataAddressPacketLen - 1);
sendCRC = crc_bytes(alarmDataAddressResponse.Skip(2).Take(alarmDataAddressPacketLen - 1).ToArray());
alarmDataAddressResponse[alarmDataAddressPacketLen + 1] = (byte)((sendCRC >> 8) & 0xFF);
alarmDataAddressResponse[alarmDataAddressPacketLen + 2] = (byte)((sendCRC) & 0xFF);
Console.WriteLine("Send Alarm Data Address Message : '{0}'", BytesToString(alarmDataAddressResponse));
Send(state.workSocket, alarmDataAddressResponse);
break;
case PROTOCOL_NUMBER.STATUS_INFO:
serialNumber.CopyTo(heartbeatResponse, 4);
byte info = receiveMessage[4];
byte voltage = receiveMessage[5];
byte GSMsignalStrength = receiveMessage[6];
UInt16 alarmLanguage = (UInt16)((receiveMessage[7] << 8) | receiveMessage[8]);
ALARM alarm = (ALARM)((info >> 3) & 0x07);
sendCRC = crc_bytes(heartbeatResponse.Skip(2).Take(heartbeatResponse.Length - 6).ToArray());
heartbeatResponse[heartbeatResponse.Length - 4] = (byte)((sendCRC >> 8) & 0xFF);
heartbeatResponse[heartbeatResponse.Length - 3] = (byte)((sendCRC) & 0xFF);
Console.WriteLine("Received good status message from Serial Number : '{0}', INFO : '0x{1}{2}{3}{4}'",
"0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"),
info.ToString("X2"), voltage.ToString("X2"), GSMsignalStrength.ToString("X2"),
alarmLanguage.ToString("X4"));
Console.WriteLine("Send Message : '{0}'", BytesToString(heartbeatResponse));
Send(state.workSocket, heartbeatResponse);
switch (alarm)
{
//reset cut off alarm
case ALARM.POWER_CUT_ALARM:
int connectOilAndElectricityPacketLen = connectOilAndEletricity.Length - 5;
serialNumber.CopyTo(connectOilAndEletricity, connectOilAndElectricityPacketLen - 1);
sendCRC = crc_bytes(connectOilAndEletricity.Skip(2).Take(connectOilAndEletricity.Length - 6).ToArray());
connectOilAndEletricity[connectOilAndEletricity.Length - 4] = (byte)((sendCRC >> 8) & 0xFF);
connectOilAndEletricity[connectOilAndEletricity.Length - 3] = (byte)((sendCRC) & 0xFF);
serverFlagBit = new byte[4];
Array.Copy(connectOilAndEletricity, 5, serverFlagBit, 0, 4);
lengthOfCommand = connectOilAndEletricity[4];
stringArray = new byte[lengthOfCommand - 4]; //do not include server flag bit
Array.Copy(connectOilAndEletricity, 9, stringArray, 0, lengthOfCommand - 4);
stringMessage = Encoding.ASCII.GetString(stringArray);
Console.WriteLine("Reset Oil and Electricity, Server Flag Bit : '{0}{1}{2}{3}', Message : '{4}'",
serverFlagBit[0].ToString("X2"),
serverFlagBit[1].ToString("X2"),
serverFlagBit[2].ToString("X2"),
serverFlagBit[3].ToString("X2"),
stringMessage);
Send(state.workSocket, connectOilAndEletricity);
break;
}
break;
case PROTOCOL_NUMBER.STRING_INFO:
lengthOfCommand = receiveMessage[4];
serverFlagBit = new byte[4];
Array.Copy(receiveMessage, 5, serverFlagBit, 0, 4);
stringArray = new byte[lengthOfCommand - 4]; //do not include server flag bit
Array.Copy(receiveMessage, 9, stringArray, 0, lengthOfCommand - 4);
stringMessage = Encoding.ASCII.GetString(stringArray);
Console.WriteLine("String Message, Server Flag Bit : '{0}{1}{2}{3}', Message : '{4}'",
serverFlagBit[0].ToString("X2"),
serverFlagBit[1].ToString("X2"),
serverFlagBit[2].ToString("X2"),
serverFlagBit[3].ToString("X2"),
stringMessage);
break;
} //end switch
}// End if
} //end while
}//end while fifo > 0
allDone.Reset();
}//end while true
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
First of all, a google docs is no way to share code bear that in mind for next questions. I cant really test your code, but from what i read in the documentacion of this GPS. I can tell you that your IMEI problem is that you are not desearializing it well.
According to the documentacion the Login Message Packet has 18 bytes, and its divided this way in order
2 as start bit
1 for the length of the package
1 for the protocol number (in this case protocol 1 or 0x01)
8 for the IMEI as a example this IMEI 123456789123456 gets sent like this: 0x01
0x23 0x45 0x67 0x89 0x120x34 0x56
2 for serial information on the number
2 for error check
and 2 more for stop
Here is your line where you deserialize it:
string IMEI = Encoding.ASCII.GetString(receiveMessage.Skip(4).Take(messageLength - 5).ToArray());
To begin with there is no need to make a calculation on how many you should take, they will always be 8 so receiveMessage.Skip(4).Take(8).ToArray()
Then there is the problem with the encoding, you dont want to convert those numbers into their ascii char, you want only to take the numbers being writen as a hex string, removing the dashes that generates the method, and removing the leading zero if any exists
BitConverter.ToString(ByteArr).Replace("-","").TrimStart('0')
This is how your IMEI variable should look like:
string IMEI = BitConverter.ToString(receiveMessage.Skip(4).Take(8).ToArray()).Replace("-","").TrimStart('0');
I am looking for a little help.
I have a program to communicate with a controller through Modbus TCP.
The only problem is I cannot extend the Nop from 125 to 400 because I got Illegal Data Address error message.
Could you please help me with this?
try
{
byte slaveid = 1;
byte function = 4;
ushort id = function;
ushort startAddress = 0;
uint NoP = 125;
byte[] frame = ReadInputRegistersMsg(id, slaveid, startAddress, function, NoP);
this.Write(frame); //data send to controller
Thread.Sleep(100);
byte[] buffReceiver = this.Read(); //data recieving from controller
int SizeByte = buffReceiver[8]; // Data what I got from the controller
UInt16[] temp = null;
if (function != buffReceiver[7])
{
byte[] byteMsg = new byte[9];
Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
byte[] data = new byte[SizeByte];
textBox2.Text = Display(byteMsg);
byte[] errorbytes = new byte[3];
Array.Copy(buffReceiver, 6, errorbytes, 0, errorbytes.Length);
this.CheckValidate(errorbytes); // check the answer -> error message
}
else
{
byte[] byteMsg = new byte[9 + SizeByte];
Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
byte[] data = new byte[SizeByte];
textBox2.Text = Display(byteMsg); // Show received messages in windows form app
Array.Copy(buffReceiver, 9, data, 0, data.Length);
temp = Word.ConvertByteArrayToWordArray(data); // Convert Byte[]-> Word[]
}
// Result
if (temp == null) return;
string result = string.Empty;
//foreach (var item in temp) // show all the data
for(int i=0;i<100;i++) // show the first 100 data
{
//result += string.Format("{0} ", item);
result += temp[i];
}
textBox3.Text = result; // insert the result into the textbox (windows form app)
}
catch
{
}
The ReadInputRegister Message is the following:
private byte[] ReadInputRegistersMsg(ushort id, byte slaveAddress, ushort startAddress, byte function, uint NoP)
{
byte[] frame = new byte[12];
frame[0] = (byte)(id >> 8); // Transaction Identifier High
frame[1] = (byte)id; // Transaction Identifier Low
frame[2] = 0; // Protocol Identifier High
frame[3] = 0; // Protocol Identifier Low
frame[4] = 0; // Message Length High
frame[5] = 6; // Message Length Low(6 bytes to follow)
frame[6] = slaveAddress; // Slave address(Unit Identifier)
frame[7] = function; // Function
frame[8] = (byte)(startAddress >> 8); // Starting Address High
frame[9] = (byte)startAddress; // Starting Address Low
frame[10] = (byte)(NoP >> 8); // Quantity of Registers High
frame[11] = (byte)NoP; // Quantity of Registers Low
return frame;
}
The hard limit for modbus is 125 NoP
I have a server side app written in C
struct recv_packet
{
int magic;
int code;
int length;
char *body;
};
char send_buff[1024+1] = "";
ZeroMemory(&send_buff, 1024);
memset(send_buff, 'A', 1024);
//send_buff[1024] = '\0';
recv_packet rcv_pkt = { 0 };
rcv_pkt.magic = MAGIC;
rcv_pkt.code = 0;
rcv_pkt.length = strlen(send_buff);
rcv_pkt.body = send_buff;
int size = sizeof(rcv_pkt.magic) + sizeof(rcv_pkt.code) + sizeof(rcv_pkt.length) + 1024+1;
if (send(ClientSocket, (char *)&rcv_pkt, size, 0) == SOCKET_ERROR)
{
printf("Error %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
On the other side i grab this packet like this:
public struct recv_packet
{
public int magic;
public int code;
public int length;
public byte[] body;
};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int port = 4000;
TcpClient client = new TcpClient("127.0.0.1", 4000);
NetworkStream nws = client.GetStream();
BinaryWriter bw = new BinaryWriter(nws);
BinaryReader br = new BinaryReader(nws);
byte[] buff = new byte[512];
send_packet pkt = new send_packet();
pkt.magic = magic;
pkt.cmd = (int)command.MOVE_MOUSE;
while (true)
{
bw.Write(pkt.magic);
bw.Write(pkt.cmd);
//br.Read(buff, 0, 512);
recv_packet rcv_pkt = new recv_packet();
rcv_pkt.magic = br.ReadInt32();
rcv_pkt.code = br.ReadInt32();
rcv_pkt.length = br.ReadInt32();
rcv_pkt.body = br.ReadBytes(rcv_pkt.length);
//string str = rcv_pkt.length.ToString();
string str = System.Text.Encoding.Default.GetString(rcv_pkt.body);
MessageBox.Show(str);
}
}
So it suppose that body will have only '65', but instead I've got some trash in it.
Why could this happen? Thank you for your time.
As I understood there are few ways of resolving this problem - one of them is to redecrlare struct a little bit and then creare a buffer, where all structure elements will be fitted one-by-one. So the solution looks like this:
char send_buff[1024+1] = "";
ZeroMemory(&send_buff, 1025);
memset(send_buff, 'A', 1024);
recv_packet *rcv_pkt = (recv_packet *)malloc(sizeof(recv_packet)+1024+1);
//recv_packet rcv_pkt = { 0 };
rcv_pkt->magic = MAGIC;
rcv_pkt->code = 0;
rcv_pkt->length = strlen(send_buff);
memcpy(rcv_pkt->body, send_buff, 1025);
int size = sizeof(rcv_pkt->magic) + sizeof(rcv_pkt->code) + sizeof(rcv_pkt->length) + 1024 + 1;
//printf("%d", size);
//getchar();
//return 0;
//if (send(ClientSocket, rcv_pkt.body, rcv_pkt.length, 0) == SOCKET_ERROR)
if (send(ClientSocket, (char *)rcv_pkt, size, 0) == SOCKET_ERROR)
I am trying to make a demonstration for web socket communication for one of our new projects at work, and I've been trying to revise an old web socket server program I wrote quite a while ago.
It does a proper handshake and can send data to the client properly (which is all it REALLY needs to do), but the client data that comes back is in the special web socket communication protocol, and I am not that good with working with binary or hex or encryption algorithms.
I know from research that the text I'm receiving back contains a frame around it, and that it is sha1 encrypted, but my problem is that I have no idea how to read or remove this frame or unencrypt it.
Here is the full code of the web server so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Threading;
using System.Security.Cryptography;
namespace WebSocks
{
public class WebSockServer
{
/// <summary>
/// Port number to listen on
/// </summary>
private const int PortNumber = 8181;
/// <summary>
/// Socket which awaits connections
/// </summary>
private Socket ListenerSocket;
/// <summary>
/// Thread in which we await for incomming connections.
/// </summary>
private System.Threading.Thread _serverThread;
public delegate void ClientConnectedHandler (Socket Sock);
public delegate void ReceivedDataHandler(Socket Sock, string Message);
public event ClientConnectedHandler ClientConnected;
public event ReceivedDataHandler ReceivedData;
static WebSockServer() { }
/// <summary>
/// Starts thread with listening socket.
/// </summary>
public void Start()
{
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(Listen);
_serverThread = new System.Threading.Thread(ts);
_serverThread.Start();
}
/// <summary>
/// Stops listening for connections.
/// </summary>
public void End()
{
_serverThread.Abort();
ListenerSocket.Dispose();
}
public void Listen()
{
//Start listening
ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint ep = new IPEndPoint(IPAddress.Parse("0.0.0.0"), PortNumber);
ListenerSocket.Bind(ep);
ListenerSocket.Listen(5);
while (true)
{
//New client
Socket client = ListenerSocket.Accept();
//Receiving clientHandshake
string clientHandshake = String.Empty;
byte[] buffer = null;
int readBytes = 0;
do
{
buffer = new byte[client.Available];
readBytes = client.Receive(buffer);
clientHandshake += Encoding.UTF8.GetString(buffer);
}
while (client.Available > 0);
//Last eight bytes are body of requets (we should include it in response)
byte[] secKey3 = buffer.Skip(readBytes - 8).Take(8).ToArray();
//Variables we can extract from clientHandshake
string clientOrigin = String.Empty;
string secKey1 = String.Empty;
string secKey2 = String.Empty;
string WebSocketVersion = String.Empty;
int WSV = 0;
string WebSocketKey = String.Empty;
//Extracting values from headers (key:value)
string[] clientHandshakeLines = Regex.Split(clientHandshake, Environment.NewLine);
foreach (string hline in clientHandshakeLines)
{
int valueStartIndex = hline.IndexOf(':') + 2;
if (valueStartIndex > 0)
{
if (hline.StartsWith("Origin"))
{
clientOrigin = hline.Substring(valueStartIndex, hline.Length - valueStartIndex);
}
else if (hline.StartsWith("Sec-WebSocket-Key2"))
{
secKey2 = hline.Substring(valueStartIndex, hline.Length - valueStartIndex);
}
else if (hline.StartsWith("Sec-WebSocket-Key1"))
{
secKey1 = hline.Substring(valueStartIndex, hline.Length - valueStartIndex);
}
if (hline.StartsWith("Sec-WebSocket-Version"))
{
WebSocketVersion = hline.Replace("Sec-WebSocket-Version: ", "");
WSV = Convert.ToInt32(WebSocketVersion);
}
if (hline.StartsWith("Sec-WebSocket-Key"))
{
WebSocketKey = hline.Replace("Sec-WebSocket-Key: ", "");
}
}
}
if (!String.IsNullOrEmpty(WebSocketVersion)) //WebSocketVersion 8 and up handshake check
{
//New WebSocketVersion number, included after Version 8
StringBuilder mResponse = new StringBuilder();
mResponse.AppendLine("HTTP/1.1 101 Switching Protocols");
mResponse.AppendLine("Upgrade: WebSocket");
mResponse.AppendLine("Connection: Upgrade");
mResponse.AppendLine(String.Format("Sec-WebSocket-Accept: {0}", ComputeWebSocketHandshakeSecurityHash09(WebSocketKey)) + Environment.NewLine);
byte[] HSText = Encoding.UTF8.GetBytes(mResponse.ToString());
client.Send(HSText, 0, HSText.Length, 0);
}
else
{
//This part is common for all websockets editions (v. 75 & v.76)
client.Send(Encoding.UTF8.GetBytes("HTTP/1.1 101 Web Socket Protocol Handshake" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("Upgrade: WebSocket" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("Connection: Upgrade" + Environment.NewLine));
if (String.IsNullOrEmpty(secKey1) && String.IsNullOrEmpty(secKey2)) //75 or less handshake check
{
client.Send(Encoding.UTF8.GetBytes(String.Format("WebSocket-Origin: {0}", clientOrigin) + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("WebSocket-Location: ws://localhost:8181/websock" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes(Environment.NewLine));
}
else //76 handshake check
{
//Keys present, this means 76 version is used. Writing Sec-* headers
client.Send(Encoding.UTF8.GetBytes(String.Format("Sec-WebSocket-Origin: {0}", clientOrigin) + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes("Sec-WebSocket-Location: ws://localhost:8181/websock" + Environment.NewLine));
client.Send(Encoding.UTF8.GetBytes(Environment.NewLine));
//Calculating response body
byte[] secret = CalculateSecurityBody(secKey1, secKey2, secKey3);
client.Send(secret);
}
}
if (ClientConnected != null)
{
ClientConnected(client);
}
Thread t = new Thread(new ParameterizedThreadStart(WaitForMessages));
t.Start(client);
}
}
private static void SendMessage(string Msg, Socket client, int WebSockVersion)
{
if (WebSockVersion >= 8)
{
bool IsFinal = true;
int OpCode = 1;
int? Mask = null;
byte[] payload = Encoding.UTF8.GetBytes(Msg);
int PayloadLength = payload.Length;
byte[] buffer = new byte[64]; // for working out the header
int offset = 0;
buffer[offset++] = (byte)((IsFinal ? 128 : 0) | ((int)OpCode & 15));
if (PayloadLength > ushort.MaxValue)
{ // write as a 64-bit length
buffer[offset++] = (byte)((Mask.HasValue ? 128 : 0) | 127);
buffer[offset++] = 0;
buffer[offset++] = 0;
buffer[offset++] = 0;
buffer[offset++] = 0;
buffer[offset++] = (byte)(PayloadLength >> 24);
buffer[offset++] = (byte)(PayloadLength >> 16);
buffer[offset++] = (byte)(PayloadLength >> 8);
buffer[offset++] = (byte)(PayloadLength);
}
else if (PayloadLength > 125)
{ // write as a 16-bit length
buffer[offset++] = (byte)((Mask.HasValue ? 128 : 0) | 126);
buffer[offset++] = (byte)(PayloadLength >> 8);
buffer[offset++] = (byte)(PayloadLength);
}
else
{ // write in the header
buffer[offset++] = (byte)((Mask.HasValue ? 128 : 0) | PayloadLength);
}
if (Mask.HasValue)
{
int mask = Mask.Value;
buffer[offset++] = (byte)(mask >> 24);
buffer[offset++] = (byte)(mask >> 16);
buffer[offset++] = (byte)(mask >> 8);
buffer[offset++] = (byte)(mask);
}
// you might want to manually combine these into 1 packet
client.Send(buffer, 0, offset, SocketFlags.None);
client.Send(payload, 0, payload.Length, SocketFlags.None);
}
else
{
client.Send(new byte[] { 0x00 });
client.Send(Encoding.UTF8.GetBytes(Msg));
client.Send(new byte[] { 0xFF });
}
}
private void WaitForMessages(object client)
{
Socket sock = (Socket)client;
byte[] buffer = new byte[1024];
while (true)
{
sock.Receive(buffer);
ReceivedData(sock, Encoding.UTF8.GetString(buffer));
}
}
public byte[] CalculateSecurityBody(string secKey1, string secKey2, byte[] secKey3)
{
//Remove all symbols that are not numbers
string k1 = Regex.Replace(secKey1, "[^0-9]", String.Empty);
string k2 = Regex.Replace(secKey2, "[^0-9]", String.Empty);
//Convert received string to 64 bit integer.
Int64 intK1 = Int64.Parse(k1);
Int64 intK2 = Int64.Parse(k2);
//Dividing on number of spaces
int k1Spaces = secKey1.Count(c => c == ' ');
int k2Spaces = secKey2.Count(c => c == ' ');
int k1FinalNum = (int)(intK1 / k1Spaces);
int k2FinalNum = (int)(intK2 / k2Spaces);
//Getting byte parts
byte[] b1 = BitConverter.GetBytes(k1FinalNum).Reverse().ToArray();
byte[] b2 = BitConverter.GetBytes(k2FinalNum).Reverse().ToArray();
//byte[] b3 = Encoding.UTF8.GetBytes(secKey3);
byte[] b3 = secKey3;
//Concatenating everything into 1 byte array for hashing.
List<byte> bChallenge = new List<byte>();
bChallenge.AddRange(b1);
bChallenge.AddRange(b2);
bChallenge.AddRange(b3);
//Hash and return
byte[] hash = MD5.Create().ComputeHash(bChallenge.ToArray());
return hash;
}
public String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
{
const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
String secWebSocketAccept = String.Empty;
// 1. Combine the request Sec-WebSocket-Key with magic key.
String ret = secWebSocketKey + MagicKEY;
// 2. Compute the SHA1 hash
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
// 3. Base64 encode the hash
secWebSocketAccept = Convert.ToBase64String(sha1Hash);
return secWebSocketAccept;
}
}
}
I know its very crude, it doesn't clean up after itself, and it doesn't separate the different connections properly and hold them in a list or anything, this is purely for a demonstration project.
So the main section I need help with is the WaitForMessages function:
private void WaitForMessages(object client)
{
Socket sock = (Socket)client;
byte[] buffer = new byte[1024];
while (true)
{
sock.Receive(buffer);
//Remove Frame and decrypt here
ReceivedData(sock, Encoding.UTF8.GetString(buffer));
}
}
I really just want to drop some code in here that will work, if you point me to a "demonstration that is kind of like your code and you should be able to figure it out from this" I'm really not going to understand it, I can almost guarantee you. I'm an app developer, not an API developer, I just don't want to wait until Microsoft finally gets around to writing an API into .NET 6.0 or something for this feature.