I studied this code in one of the youtube tutorials, I cannot send info to LCD, the program shows error, the error which I keep getting is
Initializing argument 1 of 'void Send_A_String(char*)' [-fpermissive]
initializing argument 3 of 'void Send_An_IntegerToMrLCD(uint8_t, uint8_t, int, char)' [-fpermissive]
invalid conversion from 'char' to 'char*' [-fpermissive]
invalid conversion from 'char*' to 'int' [-fpermissive]
Below is the header file for LCD - MrLCD.h
#ifndef MrLCD
#define MrLCD
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define MrLCDsCrib PORTB
#define DataDir_MrLCDsCrib DDRB
#define MrLCDsControl PORTD
#define DataDir_MrLCDsControl DDRD
#define LightSwitch 5
#define ReadWrite 7
#define BiPolarMood 2
char firstColumnPositionsForMrLCD[4] = {0, 64, 20, 84};
void Check_IF_MrLCD_isBusy(void);
void Peek_A_Boo(void);
void Send_A_Command(unsigned char command);
void Send_A_Character(unsigned char character);
void Send_A_String(char *StringOfCharacters);
void initializeMrLCD(void);
void GotoMrLCDsLocation(uint8_t x, uint8_t y);
void Send_A_StringToMrLCDWithLocation(uint8_t x, uint8_t y, char *StringOfCharacters);
void Send_An_IntegerToMrLCD(uint8_t x, uint8_t y, int IntegerToDisplay, char NumberOfDigits);
void Check_IF_MrLCD_isBusy()
{
DataDir_MrLCDsCrib = 0;
MrLCDsControl |= 1<<ReadWrite;
MrLCDsControl &= ~1<<BiPolarMood;
while (MrLCDsCrib >= 0x80)
{
Peek_A_Boo();
}
DataDir_MrLCDsCrib = 0xFF;
}
void Peek_A_Boo()
{
MrLCDsControl |= 1<<LightSwitch;
asm volatile ("nop");
asm volatile ("nop");
MrLCDsControl &= ~1<<LightSwitch;
}
void Send_A_Command(unsigned char command)
{
Check_IF_MrLCD_isBusy();
MrLCDsCrib = command;
MrLCDsControl &= ~ ((1<<ReadWrite)|(1<<BiPolarMood));
Peek_A_Boo();
MrLCDsCrib = 0;
}
void Send_A_Character(unsigned char character)
{
Check_IF_MrLCD_isBusy();
MrLCDsCrib = character;
MrLCDsControl &= ~ (1<<ReadWrite);
MrLCDsControl |= 1<<BiPolarMood;
Peek_A_Boo();
MrLCDsCrib = 0;
}
void Send_A_String(char *StringOfCharacters)
{
while(*StringOfCharacters > 0)
{
Send_A_Character(*StringOfCharacters++);
}
}
void initializeMrLCD()
{
DataDir_MrLCDsControl |= 1<<LightSwitch | 1<<ReadWrite | 1<<BiPolarMood;
_delay_ms(15);
Send_A_Command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(2);
Send_A_Command(0x38);
_delay_us(50);
Send_A_Command(0b00001110);
_delay_us(50);
}
void GotoMrLCDsLocation(uint8_t x, uint8_t y)
{
Send_A_Command(0x80 + firstColumnPositionsForMrLCD[y-1] + (x-1));
}
void Send_A_StringToMrLCDWithLocation(uint8_t x, uint8_t y, char *StringOfCharacters)
{
GotoMrLCDsLocation(x, y);
Send_A_String(*StringOfCharacters);
}
void Send_An_IntegerToMrLCD(uint8_t x, uint8_t y, int IntegerToDisplay, char NumberOfDigits)
{
char StringToDisplay[NumberOfDigits];
itoa(IntegerToDisplay, StringToDisplay, 10);
Send_A_StringToMrLCDWithLocation(x, y, StringToDisplay);
Send_A_String(" ");
}
#endif
And in the simplest of programs in the main file, if I try to call these functions in the header file like Send_A_StringToMrLCDWithLocation or Send_An_IntegerToMrLCD... The program shows error...
You need to move all this code into a C file, i.e. MrLCD.c because that is where the code is instantiated.
You have not included the .c file where you call these functions, but the error appears to say that you are calling the functions with the wrong type of variables.
You are calling void Send_A_String(char*) with a char rather than a pointer to a string.
You are calling void Send_An_IntegerToMrLCD(uint8_t, uint8_t, int, char) with a char pointer (string) rather than an int in the third parameter.
Related
I am trying to use the write protection feature of Linux's userfaultfd, but it does not appear to be enabled in my kernel even though I am using version 5.13 (write protection should be fully supported in 5.10+).
When I run
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
static int has_bit(uint64_t val, uint64_t bit) {
return (val & bit) == bit;
}
int main() {
long uffd; /* userfaultfd file descriptor */
struct uffdio_api uffdio_api;
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
printf("UFFDIO_API: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_API));
printf("UFFDIO_REGISTER: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_REGISTER));
printf("UFFDIO_UNREGISTER: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_UNREGISTER));
printf("UFFDIO_WRITEPROTECT: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_WRITEPROTECT));
printf("UFFD_FEATURE_PAGEFAULT_FLAG_WP: %d\n", has_bit(uffdio_api.features, UFFD_FEATURE_PAGEFAULT_FLAG_WP));
}
The output is
UFFDIO_API: 1
UFFDIO_REGISTER: 1
UFFDIO_UNREGISTER: 1
UFFDIO_WRITEPROTECT: 0
UFFD_FEATURE_PAGEFAULT_FLAG_WP: 1
The UFFD_FEATURE_PAGEFAULT_FLAG_WP feature is enabled, but the UFFDIO_WRITEPROTECT ioctl is marked as not supported, which is necessary to enable write protection.
What might lead to this feature being disabled, and how can I enable it?
I am using Ubuntu MATE 21.10 with Linux kernel version 5.13.0-30-generic.
EDIT:
It seems like despite the man page section on the UFFD_API ioctl (https://man7.org/linux/man-pages/man2/ioctl_userfaultfd.2.html), this might be the intended behavior for a system where write protection is enabled. However, when I run a full program that spawns a poller thread and writes to the protected memory, the poller thread does not receive any notification.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
static int page_size;
static void* fault_handler_thread(void* arg) {
long uffd; /* userfaultfd file descriptor */
uffd = (long) arg;
/* Loop, handling incoming events on the userfaultfd
file descriptor. */
for (;;) {
/* See what poll() tells us about the userfaultfd. */
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
if (nready == -1)
errExit("poll");
printf("\nfault_handler_thread():\n");
printf(
" poll() returns: nready = %d; "
"POLLIN = %d; POLLERR = %d\n",
nready, (pollfd.revents & POLLIN) != 0,
(pollfd.revents & POLLERR) != 0);
// received fault, exit the program
exit(EXIT_FAILURE);
}
}
int main() {
long uffd; /* userfaultfd file descriptor */
char* addr; /* Start of region handled by userfaultfd */
uint64_t len; /* Length of region handled by userfaultfd */
pthread_t thr; /* ID of thread that handles page faults */
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;
struct uffdio_writeprotect uffdio_wp;
int s;
page_size = sysconf(_SC_PAGE_SIZE);
len = page_size;
/* Create and enable userfaultfd object. */
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED)
errExit("mmap");
printf("Address returned by mmap() = %p\n", addr);
/* Register the memory range of the mapping we just created for
handling by the userfaultfd object. */
uffdio_register.range.start = (unsigned long) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl-UFFDIO_REGISTER");
printf("uffdio_register.ioctls = 0x%llx\n", uffdio_register.ioctls);
printf("Have _UFFDIO_WRITEPROTECT? %s\n", (uffdio_register.ioctls & _UFFDIO_WRITEPROTECT) ? "YES" : "NO");
uffdio_wp.range.start = (unsigned long) addr;
uffdio_wp.range.len = len;
uffdio_wp.mode = UFFDIO_WRITEPROTECT_MODE_WP;
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &uffdio_wp) == -1)
errExit("ioctl-UFFDIO_WRITEPROTECT");
/* Create a thread that will process the userfaultfd events. */
s = pthread_create(&thr, NULL, fault_handler_thread, (void*) uffd);
if (s != 0) {
errno = s;
errExit("pthread_create");
}
/* Main thread now touches memory in the mapping, touching
locations 1024 bytes apart. This will trigger userfaultfd
events for all pages in the region. */
usleep(100000);
size_t l;
l = 0xf; /* Ensure that faulting address is not on a page
boundary, in order to test that we correctly
handle that case in fault_handling_thread(). */
char i = 0;
while (l < len) {
printf("Write address %p in main(): ", addr + l);
addr[l] = i++;
printf("%d\n", addr[l]);
l += 1024;
usleep(100000); /* Slow things down a little */
}
exit(EXIT_SUCCESS);
}
The UFFD_API ioctl does not seem to ever report _UFFD_WRITEPROTECT as can be seen here in the kernel source code (1, 2). I assume that this is because whether this operation is supported or not depends on the kind of underlying mapping.
The feature is in fact reporeted on a per-registered-range basis. You will have to set the API with ioctl(uffd, UFFDIO_API, ...) first, then register a range with ioctl(uffd, UFFDIO_REGISTER, ...) and then check the uffdio_register.ioctls field.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
int main(void) {
long uffd;
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
struct uffdio_api uffdio_api = { .api = UFFD_API };
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl(UFFDIO_API)");
const size_t region_sz = 0x4000;
void *region = mmap(NULL, region_sz, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
if (region == MAP_FAILED)
errExit("mmap");
if (posix_memalign((void **)region, sysconf(_SC_PAGESIZE), region_sz))
errExit("posix_memalign");
printf("Region mapped at %p - %p\n", region, region + region_sz);
struct uffdio_register uffdio_register = {
.range = { .start = (unsigned long)region, .len = region_sz },
.mode = UFFDIO_REGISTER_MODE_WP
};
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl(UFFDIO_REGISTER)");
printf("uffdio_register.ioctls = 0x%llx\n", uffdio_register.ioctls);
printf("Have _UFFDIO_WRITEPROTECT? %s\n", (uffdio_register.ioctls & _UFFDIO_WRITEPROTECT) ? "YES" : "NO");
if ((uffdio_register.ioctls & UFFD_API_RANGE_IOCTLS) != UFFD_API_RANGE_IOCTLS)
errExit("bad ioctl set");
struct uffdio_writeprotect wp = {
.range = { .start = (unsigned long)region, .len = region_sz },
.mode = UFFDIO_WRITEPROTECT_MODE_WP
};
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &wp) == -1)
errExit("ioctl(UFFDIO_WRITEPROTECT)");
puts("ioctl(UFFDIO_WRITEPROTECT) successful.");
return EXIT_SUCCESS;
}
Output:
Region mapped at 0x7f45c48fe000 - 0x7f45c4902000
uffdio_register.ioctls = 0x5c
Have _UFFDIO_WRITEPROTECT? YES
ioctl(UFFDIO_WRITEPROTECT) successful.
I found the solution. The write-protected pages must be touched after registering but before marking them as write-protected. This is an undocumented requirement, from what I can tell.
In other words, add
for (size_t i = 0; i < len; i += page_size) {
addr[i] = 0;
}
between registering and write-protecting.
It works if I change the full example to
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
static int page_size;
static void* fault_handler_thread(void* arg) {
long uffd; /* userfaultfd file descriptor */
uffd = (long) arg;
/* Loop, handling incoming events on the userfaultfd
file descriptor. */
for (;;) {
/* See what poll() tells us about the userfaultfd. */
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
if (nready == -1)
errExit("poll");
printf("\nfault_handler_thread():\n");
printf(
" poll() returns: nready = %d; "
"POLLIN = %d; POLLERR = %d\n",
nready, (pollfd.revents & POLLIN) != 0,
(pollfd.revents & POLLERR) != 0);
// received fault, exit the program
exit(EXIT_FAILURE);
}
}
int main() {
long uffd; /* userfaultfd file descriptor */
char* addr; /* Start of region handled by userfaultfd */
uint64_t len; /* Length of region handled by userfaultfd */
pthread_t thr; /* ID of thread that handles page faults */
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;
struct uffdio_writeprotect uffdio_wp;
int s;
page_size = sysconf(_SC_PAGE_SIZE);
len = page_size;
/* Create and enable userfaultfd object. */
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED)
errExit("mmap");
printf("Address returned by mmap() = %p\n", addr);
/* Register the memory range of the mapping we just created for
handling by the userfaultfd object. */
uffdio_register.range.start = (unsigned long) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl-UFFDIO_REGISTER");
printf("uffdio_register.ioctls = 0x%llx\n", uffdio_register.ioctls);
printf("Have _UFFDIO_WRITEPROTECT? %s\n", (uffdio_register.ioctls & _UFFDIO_WRITEPROTECT) ? "YES" : "NO");
for (size_t i = 0; i < len; i += page_size) {
addr[i] = 0;
}
uffdio_wp.range.start = (unsigned long) addr;
uffdio_wp.range.len = len;
uffdio_wp.mode = UFFDIO_WRITEPROTECT_MODE_WP;
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &uffdio_wp) == -1)
errExit("ioctl-UFFDIO_WRITEPROTECT");
/* Create a thread that will process the userfaultfd events. */
s = pthread_create(&thr, NULL, fault_handler_thread, (void*) uffd);
if (s != 0) {
errno = s;
errExit("pthread_create");
}
/* Main thread now touches memory in the mapping, touching
locations 1024 bytes apart. This will trigger userfaultfd
events for all pages in the region. */
usleep(100000);
size_t l;
l = 0xf; /* Ensure that faulting address is not on a page
boundary, in order to test that we correctly
handle that case in fault_handling_thread(). */
char i = 0;
while (l < len) {
printf("Write address %p in main(): ", addr + l);
addr[l] = i++;
printf("%d\n", addr[l]);
l += 1024;
usleep(100000); /* Slow things down a little */
}
exit(EXIT_SUCCESS);
}
**I have a C++ API and corresponding library(api-140.lib) file, using this I am developing a GUI project in a Microsoft Visual Studio-2015 with Qt. First of all I am checking the code in console application.
Before Build the project, I done the below things,
*Entered the IP Address in Command Line argument,
Added the library file path to Properties->Linker->Additional Library Dirctories &
Added the library file in Properties->Linker->Input->Additional Dependencies*
During Build the project I struct with Linker errors. I am newbie to Qt & MSVS. Kindly guide me to rectify these error.[enter image description here][1]
My System details are below:
Windows 10 64 bit
VS-2015, Qt-5.6.0(qt-opensource-windows-x86-msvc2015_64-5.6.0)**
My C++ code:
main.cpp
#include <string>
#include <stdint.h>
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <QtCore/QCoreApplication>
#include "qtcpserver.h"
#pragma comment (lib, "Ws2_32.lib")
#ifdef _UNICODE
std::string convertToString(_TCHAR* textW)
{
std::string result;
int lenA = WideCharToMultiByte(CP_ACP, 0, textW, -1, 0, 0, NULL, NULL);
printf("lenA = %d\n", lenA);
if (lenA > 0)
{
char* textA = new char[lenA + 1]; // allocate a final null terminator as well
printf("textA = %p\n", textA);
WideCharToMultiByte(CP_ACP, 0, textW, -1, textA, lenA, NULL, NULL);
printf("%d\n", __LINE__);
textA[lenA] = 0; // Set the null terminator yourself
result = textA;
delete[] textA;
}
return result;
}
#endif
#define GE_COMMON_VERSION "2.4.1"
#define GE_OK 0
#define GE_ERROR 1
#define GE_OVERFLOW 2
#define GE_INVALID_PARAMETER 3
#define GE_RESOURCE_UNAVAILABLE 4
#define GE_FILE_IN_USE 5
#define GE_ADDRESS_EXISTS 6
#define GE_INVALID_ADDRESS 7
#define GE_NOT_EXISTING 8
#define GE_UNDERRUN 9
#define GE_ABORT 10
#define GE_COMMFAIL 11
#define GE_BUSY 12
#define GE_NO_ACCESS 13
#define GE_NOT_PERMITTED 14
#define GEC_SFPDP_RECORDER_VERSION "3.5.0"
#define GEC_SFPDP_RECORDER_SERVER_UNIX_SOCKET_FILE "/tmp/ge-sfpdp-server"
#define GEC_SFPDP_RECORDER_SERVER_TCP_PORT 8001
#define GEC_SFPDP_NO_WAIT_FOR_SYNC 0
#define GEC_SFPDP_WAIT_FOR_ANY_SYNC 1
#define GEC_SFPDP_PLAYBACK_WITHOUT_SIGNALS 0
#define GEC_SFPDP_PLAYBACK_WITH_UNTIMED_SIGNALS 1
#define GEC_SFPDP_PLAYBACK_WITH_TIMED_SIGNALS 2
#define GEC_SFPDP_512_WORDS_CYCLE 0
#define GEC_SFPDP_16K_WORDS_CYCLE 1
#define GEC_SFPDP_512K_WORDS_CYCLE 2
#define GEC_SFPDP_16M_WORDS_CYCLE 3
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_0 (1 << 0)
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_1 (1 << 1)
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_2 (1 << 2)
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_3 (1 << 3)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT0 (1 << 4)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT1 (1 << 5)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT2 (1 << 6)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT3 (1 << 7)
#define GEC_SFPDP_AUTOMATIC_CONTROL 0
#define GEC_SFPDP_MANUAL_CONTROL 1
#define GEC_SFPDP_START_CHANNEL_SYNC 0
#define GEC_SFPDP_PAUSE_CHANNEL_SYNC 1
#define GEC_SFPDP_STOP_CHANNEL_SYNC 2
enum {
GEC_SFPDP_1_0625_GBPS,
GEC_SFPDP_2_125_GBPS,
GEC_SFPDP_2_5_GBPS,
GEC_SFPDP_3_125_GBPS,
GEC_SFPDP_4_25_GBPS
};
class GEC_ISfpdpRecorder
{
public:
virtual int setIntChannelSyncControl(int mode) = 0;
virtual int setIntChannelSync(uint32_t iDevice, int masterMask, int action) = 0;
virtual int setChannelSyncOutput(uint32_t iDevice, int signal, int source) = 0;
virtual int reset() = 0;
virtual int create(std::string path, std::string name, uint32_t iDevice, uint32_t iPort, uint32_t& id) = 0;
virtual int open(std::string path, std::string name, uint32_t iDevice, uint32_t iPort, int mode, uint32_t& id) = 0;
virtual int destroy(uint32_t id) = 0;
virtual int setCRC(uint32_t id, bool enableCRC) = 0;
virtual int setCopyMode(uint32_t id, bool enable) = 0;
virtual int setWaitForSync(uint32_t id, int mode) = 0;
virtual int setMaxSize(uint32_t id, uint64_t sizeInBytes) = 0;
virtual int setLinkSpeed(uint32_t id, int speed) = 0;
virtual int setFlowControl(uint32_t id, bool enable) = 0;
virtual int setRateControl(uint32_t id, int cycleSize, uint32_t cyclePartsForTx) = 0;
virtual int setSimplexLinkMode(uint32_t id, bool enable) = 0;
virtual int setChannelSyncSource(uint32_t id, int source) = 0;
virtual int setSwapping(uint32_t id, bool enable8in16, bool enable16in32) = 0;
virtual int getStatus(uint32_t id, uint64_t& numBytes, std::string& state) = 0;
virtual int checkForOverflow(uint32_t id, bool& detected, bool clear) = 0;
virtual int startAll() = 0;
virtual int stopAll() = 0;
};
class GEC_SfpdpRecorderFactory
{
public:
static GEC_ISfpdpRecorder* createTcpBasedInstance(std::string address);
static void destroyTcpBasedInstance(GEC_ISfpdpRecorder* instance);
};
int _tmain(int argc, _TCHAR* argv[])
{
QCoreApplication a(argc, argv);
if (argc < 2) {
printf("ERROR: Missing argument with the IP address of the SFPDP Record Manager Server\n");
return -1;
}
// Initialize Winsock
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
printf("WSAStartup failed with error: %d\n", result);
return -1;
}
int status = 0;
std::string ipAddress;
#ifdef _UNICODE
ipAddress = convertToString(argv[1]);
#else
ipAddress = argv[1];
#endif
GEC_ISfpdpRecorder* instance = GEC_SfpdpRecorderFactory::createTcpBasedInstance("ipAddress");
if (instance) {
int status = instance->reset();
printf("reset() returned %d as return code\n", status);
uint32_t id = 0;
status = instance->create("/data", "test", 0, 0, id);
printf("create() returned %d as return code\n", status);
printf(" and 0x%x as id\n", id);
status = instance->startAll();
printf("startAll() returned %d as return code\n", status);
uint64_t numBytesRecorded = 0;
std::string state;
status = instance->getStatus(id, numBytesRecorded, state);
printf("getStatus() returned %d as return code,\n", status);
printf(" %lld as number of recorded bytes,\n", numBytesRecorded);
printf(" and %s as state\n", state.c_str());
for (int i = 0; i < 12; ++i) {
Sleep(5000);
status = instance->getStatus(id, numBytesRecorded, state);
printf("getStatus() returned %d as return code,\n", status);
printf(" %lld as number of recorded bytes,\n", numBytesRecorded);
printf(" and %s as state\n", state.c_str());
}
status = instance->stopAll();
printf("stopAll() returned %d as return code\n", status);
status = instance->destroy(id);
printf("destroy() returned %d as return code\n", status);
GEC_SfpdpRecorderFactory::destroyTcpBasedInstance(instance);
}
WSACleanup();
return a.exec(),status;
}
Build Process:
1>------ Build started: Project: sfpdpgui1, Configuration: Debug x64 ------
1> Moc'ing sfpdpgui1.h...
1> moc_sfpdpgui1.cpp
1> main.cpp
1> sfpdpgui1.cpp
1> Generating Code...
1>main.obj : error LNK2019: unresolved external symbol "public: static class GEC_ISfpdpRecorder * __cdecl GEC_SfpdpRecorderFactory::createTcpBasedInstance(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?createTcpBasedInstance#GEC_SfpdpRecorderFactory##SAPEAVGEC_ISfpdpRecorder##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function main
1>main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl GEC_SfpdpRecorderFactory::destroyTcpBasedInstance(class GEC_ISfpdpRecorder *)" (?destroyTcpBasedInstance#GEC_SfpdpRecorderFactory##SAXPEAVGEC_ISfpdpRecorder###Z) referenced in function main
1>C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\x64\Debug\\sfpdpgui1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Build Errors:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: static class GEC_ISfpdpRecorder * __cdecl GEC_SfpdpRecorderFactory::createTcpBasedInstance(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?createTcpBasedInstance#GEC_SfpdpRecorderFactory##SAPEAVGEC_ISfpdpRecorder##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function main sfpdpgui1 C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\sfpdpgui1\main.obj 1
Error LNK2019 unresolved external symbol "public: static void __cdecl GEC_SfpdpRecorderFactory::destroyTcpBasedInstance(class GEC_ISfpdpRecorder *)" (?destroyTcpBasedInstance#GEC_SfpdpRecorderFactory##SAXPEAVGEC_ISfpdpRecorder###Z) referenced in function main sfpdpgui1 C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\sfpdpgui1\main.obj 1
Error LNK1120 2 unresolved externals sfpdpgui1 C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\x64\Debug\\sfpdpgui1.exe 1
**Please find the link for Error image:**
[1]: https://i.stack.imgur.com/p2MY8.png
#include <cstdint>
#include <cstring>
template<typename T>
T oph_(const char *s){
constexpr std::size_t MAX = sizeof(T);
const std::size_t size = strnlen(s, MAX);
T r = 0;
for(auto it = s; it - s < size; ++it)
r = r << 8 | *it;
return r;
}
inline uint64_t oph(const char *s){
return oph_<uint64_t>(s);
}
int main(){
uint64_t const a = oph("New York City");
uint64_t const b = oph("Boston International");
return a > b;
}
I want to convert first 8 characters from const char * to uint64_t so I can easily compare if two strings are greater / lesser.
I am aware that equals will semi-work.
However I am not sure if this is most efficient implementation.
I want the implementation to work on both little and big endian machines.
This is a C implementation, that should be faster that your implementation, but I still need to use strncpy which should be the bottleneck
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <byteswap.h>
union small_str {
uint64_t v;
char buf[8];
};
static uint64_t fill_small_str(const char *str)
{
union small_str ss = { 0 };
strncpy(ss.buf, str, 8);
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return ss.v;
#else
return bswap_64(ss.v);
#endif
}
int main(void)
{
uint64_t const a = fill_small_str("Aew York City");
uint64_t const b = fill_small_str("Boston International");
printf("%lu ; %lu ; %d\n", a, b, (a < b));
return 0;
}
I am getting the following error even though it seems there are no syntax errors.
usb_libusb10.h(72): error C2146: syntax error : missing ';' before identifier 'cb'
The line where the error is generating is given below-
fnusb_iso_cb cb;
I also tried to highlighted this line by using "**" in the code.
Following is the code I am using.
#pragma once
#include <Windows.h>
#include <afxwin.h>
#include "libfreenect.h"
#include <libusb.h>
// There are a few rules: PKTS_PER_XFER * NUM_XFERS <= 1000, PKTS_PER_XFER % 8 == 0.
#if defined(__APPLE__)
#define DEPTH_PKTBUF 2048
#define VIDEO_PKTBUF 2048
#define PKTS_PER_XFER 128
#define NUM_XFERS 4
#else
#define DEPTH_PKTBUF 1920
#define VIDEO_PKTBUF 1920
#if defined(_WIN32)
#define PKTS_PER_XFER 32
#define NUM_XFERS 8
#else
#define PKTS_PER_XFER 16
#define NUM_XFERS 16
#endif
#endif
typedef struct {
libusb_context *ctx;
int should_free_ctx;
} fnusb_ctx;
typedef struct {
freenect_device *parent; //so we can go up from the libusb userdata
libusb_device_handle *dev;
int device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged)
int VID;
int PID;
} fnusb_dev;
typedef struct {
fnusb_dev *parent; //so we can go up from the libusb userdata
struct libusb_transfer **xfers;
uint8_t *buffer;
**fnusb_iso_cb cb;**
int num_xfers;
int pkts;
int len;
int dead;
int dead_xfers;
} fnusb_isoc_stream;
int fnusb_num_devices(fnusb_ctx *ctx);
int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes** attribute_list);
int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx);
int fnusb_shutdown(fnusb_ctx *ctx);
int fnusb_process_events(fnusb_ctx *ctx);
int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout);
int fnusb_open_subdevices(freenect_device *dev, int index);
int fnusb_close_subdevices(freenect_device *dev);
int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, unsigned char endpoint, int xfers, int pkts, int len);
int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm);
int fnusb_get_max_iso_packet_size(fnusb_dev *dev, unsigned char endpoint, int default_size);
int fnusb_control(fnusb_dev *dev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength);
int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred);
int fnusb_num_interfaces(fnusb_dev *dev);
good afternoon.
I got the code below on a book. I'm trying to execute it, but I don't know what is the "first" and "last" parameters on the MakeCodeWritable function, or where I can find them. Someone can help? This code is about C obfuscation method. I'm using Xcode program and LLVM GCC 4.2 compiler.
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
typedef unsigned int uint32;
typedef char* caddr_t;
typedef uint32* waddr_t;
#define Tam_celula 64
#define ALIGN __attribute__((aligned(Tam_celula)))
void makeCodeWritable(char* first, char* last) {
char* firstpage = first - ((int)first % getpagesize());
char* lastpage = last - ((int)last % getpagesize());
int pages = (lastpage-firstpage)/getpagesize()+1;
if (mprotect(firstpage,pages*getpagesize(), PROT_READ|PROT_EXEC|PROT_WRITE)==-1) perror("mprotect");
}
void xor(caddr_t from, caddr_t to, int len){
int i;
for(i=0;i<len;i++){
*to ^= *from; from++; to++;
} }
void swap(caddr_t from, caddr_t to, int len){
int i;
for(i=0;i<len;i++){
char t = *from; *from = *to; *to = t; from++; to++;
} }
#define CELLSIZE 64
#define ALIGN asm volatile (".align 64\n");
void P() {
static int firsttime=1; if (firsttime) {
xor(&&cell5,&&cell2,CELLSIZE);
xor(&&cell0,&&cell3,CELLSIZE);
swap(&&cell1,&&cell4,CELLSIZE);
firsttime = 0; }
char* a[] = {&&align0,&&align1,&&align2,&&align3,&&align4,&&align5};
char*next[] ={&&cell0,&&cell1,&&cell2,&&cell3, &&cell4,&&cell5};
goto *next[0];
align0: ALIGN
cell0: printf("SPGM0\n");
xor(&&cell0,&&cell3,3*CELLSIZE);
goto *next[3];
align1: ALIGN
cell1: printf("SPGM2\n"); xor(&&cell0,&&cell3,3*CELLSIZE);
goto *next[4];
align2: ALIGN
cell2: printf("SPGM4\n"); xor(&&cell0,&&cell3,3*CELLSIZE);
goto *next[5];
align3: ALIGN
cell3: printf("SPGM1\n"); xor(&&cell3,&&cell0,3*CELLSIZE);
goto *next[1];
align4: ALIGN
cell4: printf("SPGM3\n"); xor(&&cell3,&&cell0,3*CELLSIZE);
goto *next[2];
align5: ALIGN
cell5: printf("SPGM5\n");
xor(&&cell3,&&cell0,3*CELLSIZE);
}
int main (int argc, char *argv[]) {
makeCodeWritable(...);
P(); P();
}
The first argument should be (char *)P, because it looks like you want to modify code inside function P. The second argument is the ending address of function P. You can first compile the code, and using objdump -d to see the address of beginning and end of P, then calculate the size of the function, SIZE, then manually specify in the makeCodeWritable( (char *)P, ((char *)P) + SIZE.
The second way is utilizing the as to get the size of function P, but it depends on the assembler language on your platform. This is code snipe I modified from your code, it should be able to compile and run in x86, x86_64 in GCC 4.x on Linux platform.
align5: ALIGN
cell5: printf("SPGM5\n");
xor(&&cell3,&&cell0,3*CELLSIZE);
// adding an label to the end of function P to assembly code
asm ("END_P: \n");
;
}
extern char __sizeof__myfunc[];
int main (int argc, char *argv[]) {
// calculate the code size, ending - starting address of P
asm (" __sizeof__myfunc = END_P-P \n");
// you can see the code size of P
printf("code size is %d\n", (unsigned)__sizeof__myfunc);
makeCodeWritable( (char*)P, ((char *)P) + (unsigned)__sizeof__myfunc);
P(); P();
}
With some modification to support LLVM GCC and as in Mac OS X
int main (int argc, char *argv[]) {
size_t sizeof__myfunc = 0;
asm volatile ("movq $(_END_P - _P),%0;"
: "=r" (sizeof__myfunc)
: );
printf("%d\n", sizeof__myfunc);