Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

C C

The document is a C program that simulates sending commands to a device and retrieving various information such as firmware version, serial number, platform name, MAC address, and device name. It defines several data structures for handling command responses and utilizes functions to extract and display the requested information. The main function tests these functionalities by printing the retrieved data to the console.

Uploaded by

uhydqiz783
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

C C

The document is a C program that simulates sending commands to a device and retrieving various information such as firmware version, serial number, platform name, MAC address, and device name. It defines several data structures for handling command responses and utilizes functions to extract and display the requested information. The main function tests these functionalities by printing the retrieved data to the console.

Uploaded by

uhydqiz783
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

// Define a constant for the maximum response size


#define RESPONSE_SIZE 1024

// Simulate sending a command and getting a response


typedef struct {
int status; // Simulates the command status
uint8_t data[RESPONSE_SIZE]; // Raw byte array (TCP payload)
} CommandResponse;

typedef struct {
uint16_t magic1;
uint16_t magic2;
uint32_t length;
} TopHeader;

typedef struct {
uint16_t command;
uint16_t checksum;
uint16_t session_id;
uint16_t reply_id;
} Header;

typedef union {
TopHeader top;
Header head;
} u_tcp_head;

typedef struct {
TopHeader top;
Header head;
} s_tcp_head;

typedef struct {
Header head;
uint8_t *cmd;
} s_tcp_data;

typedef struct {
int faces;
int __unk0;
int faces_cap;
int __unk1;
int users;
int __unk2;
int fingers;
int __unk3;
int records;
int __unk4;
int dummy;
int __unk5;
int cards;
int __unk6;
int fingers_cap;
int users_cap;
int rec_cap;
int fingers_av;
int users_av;
int rec_av;
} ZKData;

// Function to simulate sending a command


CommandResponse send_command(int command, const char *command_string, int
response_size) {
CommandResponse response;
response.status = 1; // Simulating a successful response

uint8_t data[] =
{0x0b,0x00,0x80,0xe3,0xf7,0x41,0x04,0x00,0x7e,0x44,0x65,0x76,0x69,0x63,0x65,0x4e,0x
61,0x6d,0x65};
// memcpy(response.data, data, sizeof(data));

// Simulated response: Add the command_string and a mock payload


// snprintf((char *)response.data, response_size, "%s=SimulatedResponse,0x00",
command_string);
snprintf((char *)response.data, response_size, "%s=SimulatedResponse,0x00",
(char*)data);
return response;
}

// Helper function to extract a string from a uint8_t byte array


char *extract_string(const uint8_t *data, size_t length, const char *delimiter) {
char *buffer = (char *)malloc(length + 1);
if (!buffer) {
fprintf(stderr, "Memory allocation error\n");
exit(EXIT_FAILURE);
}

memcpy(buffer, data, length); // Copy raw data into a buffer


buffer[length] = '\0'; // Null-terminate the buffer for string functions

char *start = strstr(buffer, delimiter);


if (start) {
start += strlen(delimiter); // Skip the delimiter
char *end = strchr(start, ',0x00'); // Find the null terminator
if (end) *end = '\0';
} else {
start = ""; // If the delimiter is not found, return an empty string
}

char *result = strdup(start); // Copy the extracted string


free(buffer);
return result;
}

// Get firmware version


const char* get_firmware_version() {
CommandResponse response = send_command(0x01, "", RESPONSE_SIZE); // Replace
0x01 with CMD_GET_VERSION
if (response.status) {
return extract_string(response.data, RESPONSE_SIZE, "");
} else {
fprintf(stderr, "Error: Can't read firmware version\n");
exit(EXIT_FAILURE);
}
}

// Get serial number


const char* get_serialnumber() {
CommandResponse response = send_command(0x02, "~SerialNumber,0x00",
RESPONSE_SIZE); // Replace 0x02 with CMD_OPTIONS_RRQ
if (response.status) {
return extract_string(response.data, RESPONSE_SIZE, "~SerialNumber=");
} else {
fprintf(stderr, "Error: Can't read serial number\n");
exit(EXIT_FAILURE);
}
}

// Get platform name


const char* get_platform() {
CommandResponse response = send_command(0x02, "~Platform,0x00", RESPONSE_SIZE);
// Replace 0x02 with CMD_OPTIONS_RRQ
if (response.status) {
return extract_string(response.data, RESPONSE_SIZE, "~Platform=");
} else {
fprintf(stderr, "Error: Can't read platform name\n");
exit(EXIT_FAILURE);
}
}

// Get MAC address


const char* get_mac() {
CommandResponse response = send_command(0x02, "MAC,0x00", RESPONSE_SIZE); //
Replace 0x02 with CMD_OPTIONS_RRQ
if (response.status) {
return extract_string(response.data, RESPONSE_SIZE, "MAC=");
} else {
fprintf(stderr, "Error: Can't read MAC address\n");
exit(EXIT_FAILURE);
}
}

// Get device name


const char* get_device_name() {
CommandResponse response = send_command(0x02, "~DeviceName,0x00",
RESPONSE_SIZE); // Replace 0x02 with CMD_OPTIONS_RRQ
if (response.status) {
return extract_string(response.data, RESPONSE_SIZE, "~DeviceName=");
} else {
return strdup("");
}
}

// Main function for testing


int main() {
printf("Firmware Version: %s\n", get_firmware_version());
printf("Serial Number: %s\n", get_serialnumber());
printf("Platform Name: %s\n", get_platform());
printf("MAC Address: %s\n", get_mac());
printf("Device Name: %s\n", get_device_name());

printf("uint8_t: %d top: %d head: %d u_head: %d s_head: %d s_data: %d


zk_data: %d\n", sizeof(uint8_t), sizeof(TopHeader), sizeof(Header),
sizeof(u_tcp_head), sizeof(s_tcp_head), sizeof(s_tcp_data), sizeof(ZKData));

return 0;
}

You might also like