I'm trying to understand the "if (strcmp(URL, "/") == 0)" line in the Arduino sketch below (see the sendMyPage function about halfway down):
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
#define AREF_VOLTAGE 5
const int tmpPin = A0;
int tmpReading = 0;
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,31,199}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"MERCURY_7F3F70"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"11163127"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
float testTmp (void){
tmpReading = analogRead (tmpPin);
float voltage = tmpReading * AREF_VOLTAGE;
voltage /= 1023;
float tmpC = (voltage - 0.5) * 100;
return tmpC;
}
// This is our page serving function that generates web pages
**boolean sendMyPage(char* URL)** {
// Check if the requested URL matches "/"
**if (strcmp(URL, "/") == 0) {**
// Use WiServer's print and println functions to write out the page content
float tmpC = testTmp ();
WiServer.print("<html>");
// WiServer.print("Hello World!");
WiServer.print(tmpC);
WiServer.print("</html>");
// URL was recognized
return true;
}
// URL not found
return false;
}
void setup() {
// Initialize WiServer and have it use the sendMyPage function to serve pages
WiServer.init(sendMyPage);
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
}
void loop(){
// Run WiServer
WiServer.server_task();
delay(10);
}
It seems that the function accepts a URL as an argument, and sends data to the page using WiSever.print if the URL is valid. But how does 'if (strcmp(URL, "/") == 0)' determine if the URL is good?
I've seen this kind of check before, but don't understand how it works.
Thanks!
The line:
if (strcmp(URL, "/") == 0) {
Is testing to see if the string URL exactly matches the string literal "/". If so, it returns 0.
Note, if you wanted to test to see if the string URL contained "/", then use:
if (strstr(URL, "/") != NULL) {
Regarding But how does 'if (strcmp(URL, "/") == 0)' determine if the URL is good?
It does not. More would be needed than a single character to determine if URL was good, it appears that the comment, as written, is not really expressing what that code block actually does.
Related
I'm trying the implement the following openssl command line in C:
openssl enc -aes-256-cbc -d -in /tmp/out_enc -K \
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
-iv 0 -nopad -p
Output of the command line:
key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
iv =00000000000000000000000000000000
<CONNECTION_REQUEST ATS_ID="2" ATP_SEQ_ID="1" REGISTRATION_ID="Y2G5R52S8PP6YX47" SERIAL_NUMBER="724574802" SPC_PRODUCT_TITLE="SPC5300" SPC_FW_VERSION="3.8.5 - R.31629" ATS_NAME="Système (ATS) 2" ATP1_ID="2" ATP1_UID="34" ATP1_NAME="Principal ATP 1" ATP1_COMMS_INTERFACE="1" ATP1_DEST="1, 192.168.1.62:52000" ATP1_CATEGORY="50"/>�z
This is my equivalent in C:
long
_ast_crypt_decrypt_generic(unsigned char* ciphertext, long cipherlen, unsigned char* plaintext, const EVP_CIPHER *cipher) {
long result = 0;
/* A 256 bit key */
unsigned char *key = (unsigned char *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0000000000000000";
EVP_CIPHER_CTX *ctx = NULL;
int len;
long plaintext_len;
if (cipher) {
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) {
g_warning("AstCrypt : EVP_CIPHER_CTX_new failed");
goto end;
}
if (1 != EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv)) {
g_warning("AstCrypt : EVP_DecryptInit_ex");
}
EVP_CIPHER_CTX_set_padding(ctx, 0);
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, cipherlen)) {
g_warning("AstCrypt : EVP_DecryptUpdate");
goto end;
}
plaintext_len = len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
g_warning("AstCrypt : EVP_DecryptFinal_ex failed");
goto end;
}
plaintext_len += len;
result= plaintext_len;
}
else {
g_warning("AstCrypt : Failed to get the openssl elements.");
}
end:
if (ctx)
EVP_CIPHER_CTX_free(ctx);
return result;
}
long plainSize = _ast_crypt_decrypt_generic(headerData, headerLength, bPlain, EVP_aes_256_cbc());
//fwrite(bPlain, plainSize, 1, stdout);
The previous code sample doesn't decrypt the data correctly (in C).
Do you have any ideas what I am missing?
It's probably a missing understanding of the key/iv format in the openssl lib.
The answer is that the key/iv must be represented in hex :
The following works as expected :
unsigned char key[] =
{ 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,0 };
//unsigned char key[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
/* A 128 bit IV */
unsigned char iv[] =
{ 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,0 };
The problem is that you are assuming the key is direct copy of the "password". What you are doing with that command line is specifying the key as a hex string. So what you need to do is convert your "password" from a hex string into a the key buffer. In your example you should end up with every byte set to the hex value "0xAA".
Because you are passing in the key directly, there is no need for the MD5 parameter (to your function or into openssl).
If you wanted to using a password instead of a "hex string", then you need to hash it somehow into the key buffer. This is where the MD5 parameter comes in. You can use something like PBKDF2 (although I would use the openssl default SHA256 and not MD5 as the hashing function).
e.g. openssl enc -aes-256-cbc -pbkdf2 -k password
I'm using an online CRC-32 calculator to check that my output is correct however it seems that Wireshark has a different expected FCS for the ethernet packet.
message2 is the ethernet frame minus the FCS as seen in Wireshark
#include <stdio.h>
#include <stdint.h>
unsigned int crc32b(unsigned char *message) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
//printf("%i %x \n\n", i, message[i]);
byte = message[i];
crc = crc ^ byte;
for (j = 7; j >= 0; j--) {
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
int main(void)
{
unsigned char * message = "hello test";
unsigned char * message2 = "aabbccddeeff5cb9017c5a53080000000000000000000000000000";
unsigned int res = crc32b(message2);
printf("%x\n", res);
return 0;
}
I've tried using different Polynomials as defined in [1 - subsection CRC-32 IEEE 802.3], however the result does not match Wireshark.
Output using 0xED Polynomial: 0xd81e4af3
Wireshark FCS expected: 0xa8cd3084
I'd really like to code in the FCS for my ethhdr packet, I guess when creating a software packet, the FCS isn't entered by the NIC...
Sources:
[1] - http://crppit.epfl.ch/documentation/Hash_Function/WiKi/Cyclic_redundancy_check.htm
Your implementation is definitively correct (for NUL terminated C strings). It's a maybe a wrong configuration of the network interface. In default mode Wireshark doesn't get a FCS from the network driver. If you use Linux and the driver supports this, then you must enable this with ethtool to get the FCS.
Unfortunately, on my system this only works for receiving frames:
$ ethtool -K eth0 rx-fcs on
See this for details.
I use a slightly different algorithm in my embedded (for AVR microcontrollers) projects and it works perfectly for me:
#define CRC_POLY 0xEDB88320
uint32_t crc32_calc(uint8_t *data, int len)
{
int i, j;
uint32_t crc;
if (!data)
return 0;
if (len < 1)
return 0;
crc = 0xFFFFFFFF;
for (j = 0; j < len; j++) {
crc ^= data[j];
for (i = 0; i < 8; i++) {
crc = (crc & 1) ? ((crc >> 1) ^ CRC_POLY) : (crc >> 1);
}
}
return (crc ^ 0xFFFFFFFF);
}
A real world example:
The Ethernet frame in Wireshark (with ethtool rx-fcs on):
The test with my used implementation:
uint8_t frame[] = { 0x20, 0xcf, 0x30, 0x1a, 0xce, 0xa1, 0x62, 0x38,
0xe0, 0xc2, 0xbd, 0x30, 0x08, 0x06, 0x00, 0x01,
0x08, 0x00 ,0x06 ,0x04 ,0x00 ,0x01 ,0x62 ,0x38,
0xe0 ,0xc2 ,0xbd ,0x30 ,0x0a, 0x2a, 0x2a, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x2a,
0x2a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
printf("0x%x\n", crc32_calc(frame, sizeof(frame)));
The output:
$ ./fcs-test
0x6026b722
$
You can see, Wireshark reports 0x22bf2660 as correct FCS. Here is only a different output because of the byte-order. But the CRC calculation algorithm is correct.
EDIT:
I have modified your code:
uint32_t crc32b(uint8_t *message, int len) {
int i, j;
uint32_t crc, mask;
uint8_t byte;
crc = 0xFFFFFFFF;
for (j = 0; j < len; j++) {
byte = message[j];
crc = crc ^ byte;
for (i = 7; i >= 0; i--) {
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
}
return ~crc;
}
I added a length argument, because your implementation only works correct when message is a NUL terminated C string. If your input is a byte array, then you get a incorrect CRC value.
See the differences (Array and C string):
uint8_t msg_arr[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x5c, 0xb9, 0x01, 0x7c, 0x5a, 0x53, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
char *msg_str = "aabbccddeeff5cb9017c5a53080000000000000000000000000000";
printf("0x%x\n", crc32b(msg_arr, sizeof(msg_arr)));
printf("0x%x\n", crc32b(msg_str, strlen(msg_str)));
Output:
$
0x3422dd71
0xd81e4af3
$
There are a number of problems with your code. There are also plenty of existing implementations you could compare (eg, this one linked from the real Wikipedia page on CRC).
unsigned char * message2 = "aabbccddeeff5cb9017c5a53080000000000000000000000000000";
Are you hoping that this will be the octet sequece 0xAA 0xBB 0xCC ... as you see them in Wireshark? Because that isn't at all what you have.
This string actually contains 0x61 0x61 0x62 0x62 ... (assuming your platform uses ASCII encoding) because it is a character string and not an octet string.
specifically, here: byte = message[i]; you assume the first 8 bits of your message are an octet, and again I assume since you didn't say, that you expected this to be 0xAA. It will actually be 0x61.
If you want this to work correctly, translate each pair of characters into an integer value using strtoul(pair, NULL, 16) or similar.
You have a loop for (j = 7; j >= 0; j--) but never use j inside it. You do use the integer constant 1 in an odd-looking way: is there supposed to be a (1 << j) or something?
I suggest fixing the obvious errors, and then writing some self-contained tests before you try comparing whole frames with Wireshark. The code you posted has some basic errors that should be tested, identified and fixed before you get to this point.
Im not sure about your question, but if you want calculate a checksum of a network packet, you have to deploy the data in it's proper structure.
Please make sure your problem is not related with endianness.
The network byte-order is big-endian, here is the point that the things getting a little bit harder.
Little-Endian mostly used in PCs but may vary by hardware and manufacturer.
2byte integer (16 bit integer) with value 255.
Little Endian: FF00
Big Endian: 00FF
Im not sure what kind of checksum you are trying to match with, but checksum not only for data field, mostly it contains all flags and options, issued at last step thats why the implementation require the corresponding data structure.
About checksums, there are a lot of case when you get an invalid checksum with wireshark, it could cause kernel, virtual adapter, accelerated network, dedicated CPU in your NIC, etc...
Example for TCP header:
/* include/linux/tcp.h */
struct tcphdr {
__u16 source;
__u16 dest;
__u32 seq;
__u32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u16 res1:4,
doff:4,
fin:1,
syn:1,
rst:1,
psh:1,
ack:1,
urg:1,
ece:1,
cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
__u16 doff:4,
res1:4,
cwr:1,
ece:1,
urg:1,
ack:1,
psh:1,
rst:1,
syn:1,
fin:1;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
__u16 window;
__u16 check;
__u16 urg_ptr;
};
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
CHAR fileBuffer[1000];
uint8_t tmpArray[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t tmpArray2] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
for (i = 0; i < sizeof(fileBuffer); i++)
{
printf("%02X ", (uint8_t)tmpArray[i]);
}
take string from the file and convert to a byte array as in tmpArray. The string in the file would be like,
5FE1908F5FA463A9F94B8B1EA460B70A7D946B144E6A5093965A882E7855931A
I did try to read this into two byte arrays like this
memmove(tmpArray, fileBuffer, 32 * sizeof(uint8_t));
memmove(tmpArray2,fileBuffer[32], 32 *sizeof(uint8_t));
it copied in ok to tmpArray first 16bytes but the next 16bytes got messed up in tmpArray2.
If you could help with either way it would be great
string from file, string as above and convert to uint8_t arrays. the two separate arrays should result after reading/converting
uint8_t tmpArray[] = { 0x5F, 0xE1, 0x90, 0x8F, 0x5F, 0xA4, 0x63, 0xA9, 0xF9, 0x4B, 0x8B, 0x1E, 0xA4, 0x60, 0xB7, 0x0A };
uint8_t tmpArray2[] = { 0x7D, 0x94, 0x6B, 0x14, 0x4E, 0x6A, 0x50, 0x93, 0x96, 0x5A, 0x88, 0x2E, 0x78, 0x55, 0x93, 0x1A };
Is that what you wanted? (to test give binary 01 combination as an first argument)
#include <stdio.h>
#include <stdint.h>
uint8_t charToBin(char c)
{
switch(c)
{
case '0': return 0;
case '1': return 1;
}
return 0;
}
uint8_t CstringToU8(const char * ptr)
{
uint8_t value = 0;
for(int i = 0; (ptr[i] != '\0') && (i<8); ++i)
{
value = (value<<1) | charToBin(ptr[i]);
}
return value;
}
int main(int argc,const char *argv[])
{
printf("%d\n",CstringToU8(argv[1]));
return 0;
}
You can use CstringToU8() to convert 8 characters to one u8 number. Read whole data to char array (e.g. char * text) and then convert 8 characters to u8 number, store it and move you pointer 8 bytes further until array won't end.
Because after edit question was changed so here is my new solution. Code reading hex numbers from file and storing it into array.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main()
{
FILE * fp;
uint8_t number = 0;
size_t fileSize = 0;
uint8_t * array = NULL;
size_t readedBytes = 0;
size_t iterator = 0;
fp = fopen ("hexNumbers.txt", "r");
// check file size
fseek(fp, 0L, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
/// allocate max possible array size = fileSize/2
array = malloc(fileSize/2 * sizeof(uint8_t));
/// read data into array
while(!feof(fp))
{
if (fscanf(fp,"%2hhx",&number) == 1)
{
array[readedBytes++] = number;
}
}
fclose(fp);
/// print array output
for (iterator=0; iterator<readedBytes; ++iterator)
{
printf("%02x ", array[iterator]);
}
free(array);
return 0;
}
I am working on a sketch to help set up a remote door opener, based on the result of a web call. I've got a BlackWidow Arduino running WiServer, and the wifi works fine and I can get a result from my URL. I'm simply returning a 0 or 1 as the content.
The problem is in my loop the relayControlState is always HIGH, and I can't seem to get the loop to make the relay turn off/on.
When I just use a simple "blinker" sketch I can get the relay to work fine, it's only when it's intertwined with my server fetch code that it doesn't work. What am I missing? Code is below. Why does relayControlState not update inside the WiServer.getStatus callback? Is the relay not getting enough juice to switch?
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,10}; // IP address of WiShield 192.168.1.10
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"monitored"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters
// setup the wireless mode
// infrastructure - connect to AP
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// IP Address for macpro.local
uint8 ip[] = {192,168,1,12};
// End of wireless configuration parameters ----------------------------------------
// A request that gets the aggregate status of the build system
GETrequest getStatus(ip, 80, "macpro.local", "/open-says-me/index.html");
const int relayPin = 12;
int relayControlState = HIGH;
// Function that sets pin/light states
// BEWARE: THIS FUNCTION IS CALLED MULTIPLE (2) TIMES PER HTTP REQ
// Hidden call before/after call that returns payload 0, 1, 2, or null
void setRelayControlState(char* data, int len) {
// Serial.print("=========================\n\nLEN:\n");
// Serial.print(len);
if(len > 0) {
Serial.print("\nDATA:");
Serial.print(data[len - 1]);
Serial.print("\n");
// Serial.print("\n\nsetRelayControlState\n\n");
if(data[len - 1] == '0') {
relayControlState = LOW;
Serial.print("SET LOW");
}
if(data[len-1] == '1') {
relayControlState = HIGH;
Serial.print("SET HIGH");
}
} else {
relayControlState = LOW;
}
}
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(57600);
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
// Enable Serial output and ask WiServer to generate log messages (optional)
WiServer.enableVerboseMode(true);
// Have the processData function called when data is returned by the server
getStatus.setReturnFunc(setRelayControlState);
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void loop(){
// Check if it's time to get an update
if (millis() >= updateTime) {
// Get another update 15s from now
updateTime += 1000 * 5;
getStatus.submit();
}
// Run WiServer
WiServer.server_task();
// turn on light pins based on stored vals
Serial.print("\nrelayControlState: ");
Serial.print(relayControlState);
Serial.print("\n");
digitalWrite(relayPin, relayControlState);
delay(10);
}
This is the code that worked in the end, but it also could have just been inconsistent behavior of the code loading onto the BlackWidow. I started to switch pins - each time I switched to try a new pin, it would work once, but only once, until I started power cycling the arduino. Seems it likes a power down instead of only a reset or new code upload. Still a bit finicky, but a working example of polling a URL for a specific last character. If 1 set pin high for 5.5 seconds. If 0 do nothing.
#include <WiServer.h>
// ---------------------------------------------------------------------------------
// Wireless configuration parameters
// ---------------------------------------------------------------------------------
unsigned char local_ip[] = {192,168,1,10}; // IP address of WiShield 192.168.1.10
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"monitored"}; // max 32 bytes
// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
unsigned char security_type = 3;
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// ---------------------------------------------------------------------------------
// GET REQUEST
// ---------------------------------------------------------------------------------
// IP Address for macpro.local
uint8 ip[] = {192,168,1,12};
// The request URL
GETrequest getStatus(ip, 80, "macpro.local", "/open-says-me/index.html");
const int relayPin = 3;
int relayControlState = LOW;
// ---------------------------------------------------------------------------------
// Callback for WiServer's getStatus
// ---------------------------------------------------------------------------------
void setRelayControlState(char* data, int len) {
Serial.print("[setRelayControlState] last digit of data: ");
Serial.println(data[len-1]);
Serial.print("[setRelayControlState] len: ");
Serial.println(len);
if(len > 0
&& data[len-1] == '1') {
relayControlState = HIGH;
Serial.print("\nSET HIGH FOR 5.5s\n");
digitalWrite(relayPin, HIGH);
delay(5500);
digitalWrite(relayPin, LOW);
}
}
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(57600);
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
// Enable Serial output and ask WiServer to generate log messages (optional)
WiServer.enableVerboseMode(true);
// Have the processData function called when data is returned by the server
getStatus.setReturnFunc(setRelayControlState);
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void loop(){
// Check if it's time to get an update
if (millis() >= updateTime) {
// Get another update 15s from now
updateTime += 1000 * 15;
getStatus.submit();
Serial.print("end update # ms ");
Serial.println(millis());
}
WiServer.server_task();
delay(100);
}
psApdu->prgbCData = (byte_t*)malloc(APDU_BUFFER_LENGTH);
memset((void*)psApdu->prgbCData, 0, APDU_BUFFER_LENGTH);
byte_t prgData[] = { 0x01, 0x38, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02 };
memcpy((byte*)psApdu->prgbCData, prgData, sizeof(prgData));
free(psApdu->prgbCData);
'free' statement failing here. What is the reason?
Firstly, you shouldn't cast to and from void *. Also sizeof is an operator.
You need to check that sizeof prgData <= APDU_BUFFER_LENGTH. You could try this with a static assertion:
psApdu->prgbCData = malloc(APDU_BUFFER_LENGTH);
memset(psApdu->prgbCData, 0, APDU_BUFFER_LENGTH);
byte_t prgData[] = { 0x01, 0x38, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02 };
_Static_assert(sizeof prgData <= APDU_BUFFER_LENGTH, "too big");
memcpy(psApdu->prgbCData, prgData, sizeof prgData);
free(psApdu->prgbCData);
Suggested changes:
byte_t prgData[] = { 0x01, 0x38, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02 };
int ilen = sizeof (prgData);
if (ilen > APDU_BUFFER_LENGTH) {
printf ("ERROR: expected %d bytes, got %d\n", APDU_BUFFER_LENGTH, ilen);
return;
}
psApdu->prgbCData = (byte_t*)malloc(APDU_BUFFER_LENGTH);
if (!psApdu->prgbCData) {
printf ("ERROR: Unable to malloc(%d) bytes...\n", APDU_BUFFER_LENGTH);
return;
}
memset((void*)psApdu->prgbCData, 0, APDU_BUFFER_LENGTH);
memcpy((byte*)psApdu->prgbCData, prgData, sizeof(prgData));
free(psApdu->prgbCData);
ALSO:
If you successfully allocate the data, make sure nothing "steps" on it.
Q: Are "memset()/memcpy()" the only two functions that update psApdu->prgbCData? Is there anything else that writes to this buffer? Like filling in colors to your color table or pixels to your image array, for example?