STM32 USB buffer not retrieved correctly - qt

im pretty new on the STM32 and i encountered a problem.
With a Qt App i'm sending stuff over the USB with the following code:
if (m_hidDevice->isOpen())
{
QByteArray buffer(m_hidDevice->readOutputBufferSize(), 0);
buffer[0] = 16;
buffer[1] = 18;
uint16_t number = 4096;
uint16_t randomValue = qrand() % number;
buffer[2] = (char)((randomValue >> 8) & 0x00ff);
buffer[3] = (char)(randomValue & 0x00ff);
buffer[4] = (char)((2556 >> 8) & 0x00ff);
buffer[5] = (char)(2556 & 0x00ff);
qDebug() << "------------" << randomValue;
qDebug() << "//" << (uint8_t)buffer[2] << "//" << (uint8_t)buffer[3];
qDebug() << "//" << (uint8_t)buffer[4] << "//" << (uint8_t)buffer[5];
m_hidDevice->write(buffer);
and on the STM32F4 im using
switch (buffer[1])
{
case 18:
x = ((uint16_t)buffer[2] << 8) + buffer[3];
y = ((uint16_t)buffer[4] << 8) + buffer[5];
sr.m_value1[0] = x;
sr.m_value1[1] = y;
do(M);
m_value has size 4 and it is uint16_t;
The output on the Qt App is
------------ 2083
// 8 // 35
// 9 // 252
while on the STM32F4 x and y have values
x = 2083 (as expected)
y = 0
Now the size of the buffer should be 64 bytes while my data is 8*5 = 40 bytes.
My question then is why i can't retrieve correctly the values in the buffer?
Regards,

0x09, 0x01, // USAGE (Vendor Usage x) --> x = 1,2,3
0x75, 0x08, // REPORT_SIZE (8) --> 2^8 = 255
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x85, 0x02, // REPORT_ID (n) --> n must be the report id
0x95, 0x3f, // REPORT_COUNT (63) --> size
0x91, 0x02 // OUTPUT (Data,Var,Abs)

Related

Openssl encrypt large file

I'm trying to encrypt a large file using OpenSSL AES_set_encrypt_key & AES_cbc_encrypt functions. I've written a C++ program to encrypt a file using AES_set_encrypt_key & AES_cbc_encrypt. The contents is got from a large file. I'm allocating memory in heap and calling AES_cbc_encrypt. But I observed only 8 bytes are getting encrypted. But when I allocate memory on stack, the function is working properly. Any help is highly appreciated.
unsigned char enc_out[encsize]; //working
unsigned char *enc_out = new unsigned char[encsize];//not working
`
QByteArray WebMessages::encryption(QString e_text) {
QByteArray ba = e_text.toLatin1();
char *encinput = strdup(ba.constData());
const int UserDataSize = strlen(encinput);
unsigned char *test2 = new unsigned char[UserDataSize];
for (int i = 0; i < UserDataSize; i++) {
test2[i] = encinput[i];
// qDebug() << test2[i];
}
int keylength = 128;
unsigned char aes_key[] = "";
unsigned char iv_enc[] = "";
unsigned char iv_dec[] = "";
const int encsize =
((UserDataSize + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
qDebug() << "$$$$$$$$$$$" << UserDataSize << AES_BLOCK_SIZE << encsize
<< sizeof(unsigned char *); //1481 16 1488 8
unsigned char enc_out[encsize];
// unsigned char *enc_out = new unsigned char[encsize];
AES_KEY enc_key;
AES_set_encrypt_key(aes_key, keylength, &enc_key);
AES_cbc_encrypt(test2, enc_out, UserDataSize, &enc_key, iv_enc,
AES_ENCRYPT);
qDebug() << "enc_out" << enc_out << sizeof(enc_out);
}
`

Read resgister LSM6DSO32 Arduino

I would like to read some register from a LSM6DSO32 captor. But I've a problem for me tha values I got are strange. I user Atmel SAMD21 Cortex M0 and i2C for communicate with the captor. First off all I set my captor to 8g and 500° for gyroscope like that
#define DSO_ADDRESS 0x6A
void setup() {
SerialUSB.begin(115200);
while(!SerialUSB);
Wire.begin();
//gyro 500°
Wire.beginTransmission(DSO_ADDRESS);
Wire.write(0x11);
Wire.write(0x44);
Wire.endTransmission();
//accel 8g
Wire.beginTransmission(DSO_ADDRESS);
Wire.write(0x10);
Wire.write(0x48);
Wire.endTransmission();
}
For me this is works beceause if I read 0x10 after I get 0x48 value, and if I try to read who i am register I get the good value 0x6C.
After that I try to read registers value, my readSensor function
//gloabl variables to store values
int temperature;
int gyro_raw[3] = {0, 0, 0};
int acc_raw[3] = {0 , 0 , 0};
void readSensor(){
Wire.beginTransmission(DSO_ADDRESS);
Wire.write(0x20);
Wire.endTransmission();
Wire.requestFrom(DSO_ADDRESS, 14);
uint8_t buff[14];
Wire.readBytes(buff, 14);
temperature = buff[1] << 8 | buff[0];
gyro_raw[X] = buff[3] << 8 | buff[2];
gyro_raw[Y] = buff[5] << 8 | buff[4];
gyro_raw[Z] = buff[7] << 8 | buff[6];
acc_raw[X] = buff[9] << 8 | buff[8];
acc_raw[Y] = buff[11] << 8 | buff[10];
acc_raw[Z] = buff[13] << 8 | buff[12];
}
So if I called this function 2000 times for exemple to get an offset I got very strange values
long gyro_offset[3] = {0, 0, 0};
int max_samples = 500;
for (int i = 0; i < max_samples; i++) {
readSensor();
gyro_offset[X] += gyro_raw[X];
gyro_offset[Y] += gyro_raw[Y];
gyro_offset[Z] += gyro_raw[Z];
delay(50);
}
gyro_offset[X] /= max_samples;
gyro_offset[Y] /= max_samples;
gyro_offset[Z] /= max_samples;
X Y and Z are defined by value 0,1 and 2. So if I print gyro_offset[X] I get value 23 for me this is normal, but for gyro_offset[Y] and gyro_offset[Z] I got a big value around 65514 and for me these aren't good no ?

Qbytearray byte to int and storing it as string value

I want to convert byte data that stored in QBytearray into string value. that string value am using it for displaying in ui window..
QByteArray array;
array.append( 0x02 );
array.append( 0xC1);
qDebug()<<( uint )array[0]<<" "<<( uint )array[1];
uint i = 0x00000000;
i |= array[1];
qDebug()<<i;
uint j = 0x00000000 | ( array[0] << 8 );
qDebug()<<j;
i |= j;
bool b = false;
QString str = QString::number( i );
qDebug()<<str;
but the str prints "4294967233"...this code works for some of the bytes like 0x1, 0x45 and for some of other..but this code not working perfectly for all bytes of data into string..please help me with this and write code for this and post it here..thanks
All values equal or bigger than 0x80 interprets in your sample as negative values, so it need cast to unsigned type before bitwise operations.
QByteArray array;
array.append( 0x02 );
array.append( 0xC1);
unsigned int value = 0;
for (int i = 0; i < array.size(); i++)
value = (value << 8) | static_cast<unsigned char>(array[i]);
QString str = QString::number(value);
qDebug() << value << str;

QByteArray initialization

I can initialize a QByteArray like:
QByteArray m_data;
m_data[0] = 0x0c;
m_data[1] = 0x06;
m_data[2] = 0x04;
m_data[3] = 0x04;
m_data[4] = 0x02;
m_data[5] = 0x00;
But I would like something more compact, like:
QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
Unfortunately, this form isn't allowed:
error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
^
Are there any alternatives (closer to my dream)?
(Does C++11/14/17 helps on this issue?)
You could let a template figure out how many elements are in the list:
using myarr = char[];
template <size_t N>
QByteArray make_QByteArray(const char(&a)[N]) {
return {a,N};
}
Then create QByteArray with:
auto m_data{make_QByteArray(myarr{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00})};
I was looking for such answer, but including declared variables into the expression; for instance:
char v1 = 0x06, v2 = 0x04;
QByteArray m_data;
m_data[0] = 0x0c; m_data[1] = v1 ; m_data[2] = v2 ;
m_data[3] = v2 ; m_data[4] = 0x02; m_data[5] = 0x00;
//And a character higher than 0x7f
m_data[6] = 0x8b;
Here is a solution I've found:
char v1 = 0x06, v2 = 0x04;
QByteArray ba(std::begin<char>({0x0c, v1, v2, v2, 0x02, 0x00, '\x8b'}), 7);
Using std::begin or even std::initializer_list C++11 features this can be achieved.
I am using:
QByteArray data(QByteArray::fromRawData("\x8B\x55\x0C\x83\xFA\x02\x75", 7));

Get latitude and longitude from cellid, lac, MCC, MNC in ASP.NET webservice

I have an .asmx webservice that gets the cellid, MCC, MNC and LAC and I want to use opencellid to get the latitude and longitude from these information, I didn't find resources in the internet, so I don't kow where to start. if you can help me to find out how to do that, or give me an alternative solution to find latitude and longitude from these informations it would be nice. Thanks
i face the same situation. read this.. answer is here. you can merge this code to your projecct..
http://android-coding.blogspot.com/2011/06/convert-celllocation-to-real-location.html
How to get latitude and longitude from these parameters in a nice and elegant way using GoogleMaps Api.
I wrote A blog post about it here: Blog Post
Add this class to your project:
public class GoogleMapsApi
{
static byte[] PostData(int MCC, int MNC, int LAC, int CID)
{
/* The shortCID parameter follows heuristic experiences:
* Sometimes UMTS CIDs are build up from the original GSM CID (lower 4 hex digits)
* and the RNC-ID left shifted into the upper 4 digits.
*/
byte[] pd = new byte[] {
0x00, 0x0e,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x1b,
0x00, 0x00, 0x00, 0x00, // Offset 0x11
0x00, 0x00, 0x00, 0x00, // Offset 0x15
0x00, 0x00, 0x00, 0x00, // Offset 0x19
0x00, 0x00,
0x00, 0x00, 0x00, 0x00, // Offset 0x1f
0x00, 0x00, 0x00, 0x00, // Offset 0x23
0x00, 0x00, 0x00, 0x00, // Offset 0x27
0x00, 0x00, 0x00, 0x00, // Offset 0x2b
0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00
};
bool isUMTSCell = ((Int64)CID > 65535);
/* if (shortCID)
CID &= 0xFFFF; /* Attempt to resolve the cell using the
GSM CID part */
if ((Int64)CID > 65536) /* GSM: 4 hex digits, UTMS: 6 hex
digits */
pd[0x1c] = 5;
else
pd[0x1c] = 3;
pd[0x11] = (byte)((MNC >> 24) & 0xFF);
pd[0x12] = (byte)((MNC >> 16) & 0xFF);
pd[0x13] = (byte)((MNC >> 8) & 0xFF);
pd[0x14] = (byte)((MNC >> 0) & 0xFF);
pd[0x15] = (byte)((MCC >> 24) & 0xFF);
pd[0x16] = (byte)((MCC >> 16) & 0xFF);
pd[0x17] = (byte)((MCC >> 8) & 0xFF);
pd[0x18] = (byte)((MCC >> 0) & 0xFF);
pd[0x27] = (byte)((MNC >> 24) & 0xFF);
pd[0x28] = (byte)((MNC >> 16) & 0xFF);
pd[0x29] = (byte)((MNC >> 8) & 0xFF);
pd[0x2a] = (byte)((MNC >> 0) & 0xFF);
pd[0x2b] = (byte)((MCC >> 24) & 0xFF);
pd[0x2c] = (byte)((MCC >> 16) & 0xFF);
pd[0x2d] = (byte)((MCC >> 8) & 0xFF);
pd[0x2e] = (byte)((MCC >> 0) & 0xFF);
pd[0x1f] = (byte)((CID >> 24) & 0xFF);
pd[0x20] = (byte)((CID >> 16) & 0xFF);
pd[0x21] = (byte)((CID >> 8) & 0xFF);
pd[0x22] = (byte)((CID >> 0) & 0xFF);
pd[0x23] = (byte)((LAC >> 24) & 0xFF);
pd[0x24] = (byte)((LAC >> 16) & 0xFF);
pd[0x25] = (byte)((LAC >> 8) & 0xFF);
pd[0x26] = (byte)((LAC >> 0) & 0xFF);
return pd;
}
static public string[] GetLatLng(int MCC, int MNC, int LAC, int CID)
{
string[] result = new string[2];
try
{
String url = "http://www.google.com/glm/mmap";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
new Uri(url));
req.Method = "POST";
byte[] pd = PostData(MCC, MNC, LAC, CID);
req.ContentLength = pd.Length;
req.ContentType = "application/binary";
Stream outputStream = req.GetRequestStream();
outputStream.Write(pd, 0, pd.Length);
outputStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
byte[] ps = new byte[res.ContentLength];
int totalBytesRead = 0;
while (totalBytesRead < ps.Length)
{
totalBytesRead += res.GetResponseStream().Read(
ps, totalBytesRead, ps.Length - totalBytesRead);
}
if (res.StatusCode == HttpStatusCode.OK)
{
short opcode1 = (short)(ps[0] << 8 | ps[1]);
byte opcode2 = ps[2];
int ret_code = (int)((ps[3] << 24) | (ps[4] << 16) |
(ps[5] << 8) | (ps[6]));
if (ret_code == 0)
{
double lat = ((double)((ps[7] << 24) | (ps[8] << 16)
| (ps[9] << 8) | (ps[10]))) / 1000000;
double lon = ((double)((ps[11] << 24) | (ps[12] <<
16) | (ps[13] << 8) | (ps[14]))) /
1000000;
result[0] = lat.ToString();
result[1] = lon.ToString();
return result;
}
else
{
return result;
}
}
else
return result;
}
catch (Exception ex)
{
result[0] = ex.Message;
return result;
}
}
}
Call the class to get the latitude and logitude wherever you want:
string[] s = GoogleMapsApi.GetLatLng(MCC, MNC, lac, cellid);
double lat = 0;
Boolean b1 = double.TryParse(s[0], out lat);
double lon = 0;
Boolean b2 = double.TryParse(s[1], out lon);

Resources