static struct dll_wifi_state **dll_states;
enum dll_type {
DLL_UNSUPPORTED,
DLL_ETHERNET,
DLL_WIFI
};
struct dll_state {
enum dll_type type;
union {
struct dll_eth_state *ethernet;
struct dll_wifi_state *wifi;
} data;
};
static struct dll_state *dll_states = NULL;
struct dll_wifi_state {
int link;
// A pointer to the function that is called to pass data up to the next layer.
up_from_dll_fn_ty nl_callback;
bool is_ds;
};
This is the method whose pointer is being passed in the dll_wifi_state struct.
static void up_from_dll(int link, const char *data, size_t length)
{
//some code here
}
In other file, I am calling this method
void reboot_accesspoint()
{
// We require each node to have a different stream of random numbers.
CNET_srand(nodeinfo.time_of_day.sec + nodeinfo.nodenumber);
// Provide the required event handlers.
CHECK(CNET_set_handler(EV_PHYSICALREADY, physical_ready, 0));
// Prepare to talk via our wireless connection.
CHECK(CNET_set_wlan_model(my_WLAN_model));
// Setup our data link layer instances.
dll_states = calloc(nodeinfo.nlinks + 1, sizeof(struct dll_state));
for (int link = 0; link <= nodeinfo.nlinks; ++link) {
switch (linkinfo[link].linktype) {
case LT_LOOPBACK:
dll_states[link].type = DLL_UNSUPPORTED;
break;
case LT_WAN:
dll_states[link].type = DLL_UNSUPPORTED;
break;
case LT_LAN:
dll_states[link].type = DLL_ETHERNET;
dll_states[link].data.ethernet = dll_eth_new_state(link, up_from_dll);
break;
case LT_WLAN:
dll_states[link].type = DLL_WIFI;
dll_states[link].data.wifi = dll_wifi_new_state(link,
up_from_dll,
true /* is_ds */);
break;
}
}
// printf("reboot_accesspoint() complete.\n");
}
It works fine like this, but I want to add another argument i.e. up_from_dll((int link, const char *data, size_t length, int seq). And as soon as I add this argument, following error starts coming up
ap.c:153: warning: passing argument 2 of ‘dll_wifi_new_state’ from incompatible pointer type
Is there a way of adding another argument to that method without getting error ??? I am really bad with pointers :(
Any help would be much appreciated.
Line 153 :
dll_states[link].data.wifi = dll_wifi_new_state(link,
up_from_dll,
true /* is_ds */);
And method
struct dll_wifi_state *dll_wifi_new_state(int link,
up_from_dll_fn_ty callback,
bool is_ds)
{
// Ensure that the given link exists and is a WLAN link.
if (link > nodeinfo.nlinks || linkinfo[link].linktype != LT_WLAN)
return NULL;
// Allocate memory for the state.
struct dll_wifi_state *state = calloc(1, sizeof(struct dll_wifi_state));
// Check whether or not the allocation was successful.
if (state == NULL)
return NULL;
// Initialize the members of the structure.
state->link = link;
state->nl_callback = callback;
state->is_ds = is_ds;
return state;
}
I haven't changed anything else apart from adding the new parameter to up_from_dll.
The second parameter to dll_wifi_new_state is up_from_dll_fn_ty callback.
It's not in your code listing right now, but up_from_dll_fn_ty is a typedef saying that the up_from_dll_fn_ty is a function pointer with specific parameters (which don't include int seq)
When you updated up_from_dll with different parameters, it no longer matches the type specified by up_from_dll_fn_ty and expected as the second parameter for dll_wifi_new_state. You'll need to add the parameter to up_from_dll_fn_ty and you should be good.
If you post the definition of up_from_dll_fn_ty, it would make the question have all the information and allow me to help you more if you still need it.
You're looking for something like:
typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length);
and change it to
typedef void (*up_from_dll_fn_ty)(int link, const char *data, size_t length, int seq);
Here's a link to a question that has good information about creating typedefs for function pointers:
Understanding typedefs for function pointers in C
Related
I use the Aeron client implementation in C. And I need to make closure. But there are no closures in C...
For example:
void poll_handler(void *clientd, const uint8_t *buffer, size_t length, aeron_header_t *header)
{
aeron_subscription_t* subscription = (aeron_subscription_t*)clientd;
aeron_subscription_constants_t subscription_constants;
aeron_header_values_t header_values;
aeron_subscription_constants(subscription, &subscription_constants)
aeron_header_values(header, &header_values);
// How to get some_data here?
}
void some_func(int some_data)
{
aeron_fragment_assembler_create(fragment_assembler, poll_handler, subscription)
}
I tried using Clang blocks extension, but there is no function implementation that accepts block.
How can I get some local data in handler?
The aeron_fragment_assembler_create function takes a pointer to any structure as the third parameter. It doesn't need to be the aeron_subscription_t*.
You can create your own structure, initialize it with the necessary data and pass a pointer to it. And for convenience, you can save a pointer to aeron_subscription_t in the structure.
More or less like this:
typedef struct some_data_t {
aeron_subscription_t* subscription;
// ...
} some_data_t ;
void poll_handler(void *clientd, const uint8_t *buffer, size_t length, aeron_header_t *header)
{
some_data_t * some_data = (some_data_t *) clientd;
aeron_subscription_t* subscription = some_data->subscription;
// ...
}
void some_func(some_data_t* some_data)
{
aeron_fragment_assembler_create(fragment_assembler, poll_handler, some_data)
}
Actually I am registering Callback to lower layer of code from upper layer.
So In a c "config file" file I have this :
typedef enum eActivationType
{
TYPE_A = 0,
TYPE_B,
TYPE_C,
}ActivationType_t;
typedef struct Callbacks_s
{
void (*OnJoin)(ActivationType_t Mode);
}NemeusCallbacks_t;
static Callbacks_t Callbacks = {
.OnJoin = OnJoin,
.//I have other callbacks here but I the first one works the other will work
};
static Params_t Params = {
.DefaultActivationType = TYPE_A,
//other params
}
Callbacks_t* GetCallbacks(void)
{
return &Callbacks;
}
Params_t* GetParams(void)
{
return &Params;
}
From the main file I call
typedef void (*OnJoin)(ActivationType_t Mode);
static OnJoinRequest JoinCB;
void Nemeus_Init_ReceiveCallback(OnJoin joinCB)
{
JoinCB = joinCB;
}
And then when I want to use my register callback I can't
...
if( 0 == memcmp(&addr, temp_uint8_array, sizeof(addr)) && NULL != JoinCB)
{
JoinCB(&(*(GetParams())->DefaultActivationType));
}
...
The error I have is : invalid type argument of '->' (have 'int').
I think it deals with the adress of GetParams that I want to dereference as adress are store in int.
It is quite complicated for me to understand whats going on but as I am a begginer I want to learn.
I thought that by writing this *(GetParams()) , I can acceed the structure and then by writing this &(*(GetParams())->DefaultActivationType) I can pass the adress of the structur field to the callback
I am new to C and can't yet freely navigate trough my program memory. Anyways, I am creating a static memory data type (gc_menu) that should hold a pointer to created at execution time structure (mcl_items).
For simplicity mcl_items structure have one virtual method (push) that is going to be run inside of gc_menu_add_item and also assigned to the gc_menu static space. push saves an menu item name (letter) and method to mcl_item virtual object.
mcl_items.h code:
[...]
typedef struct Items_t {
int8_t size;
char names[64];
void (*methods[64])();
// Interface
void (*push)(struct Items_t *self, char c, void (*method)());
}mcl_items;
mcl_items *new_mcl_items();
void mcl_items_push(mcl_items *self, char c, void (*method)());
mcl_items.c code:
[...]
#include "mcl_items.h"
mcl_items *new_mcl_items() {
fprintf(stderr, "MCL_Items: Generating a new set of mcl_items..");
// Build a virtual object
mcl_items *items = calloc(1, sizeof(struct Items_t));
items->push = mcl_items_push;
// Set data
items->size = 0;
return items;
}
void mcl_items_push(mcl_items *self, char c, void (*method)()) {
fprintf(stderr, "MCL_Items: pushing a new item..");
self->names[self->size] = c;
self->methods[self->size] = method;
self->size ++;
}
gc_menu.h code:
#include "items.h"
typedef struct {
// Interface
void (*add_item)(char c, void (*method)());
// Data
mcl_items *items;
}__gc_menu;
extern __gc_menu const gc_menu;
gc_menu.c code:
static void gc_menu_add_item(char c, void (*method)) {
fprintf(stderr, "GC_Menu: Passing an new item..");
fprintf(stderr, "length = %i\n", gc_menu.items->size);
gc_menu.items->push(gc_menu.items, c, method);
}
__gc_menu const gc_menu = {gc_menu_add_item, // Virtual methods
new_mcl_items}; // Data
After callng gc_menu.add_item the segmentation fault occurs and gc_menu.items->size is equal to 72, not 0 as is defined in the definition of new_mcl_items.
main.c code:
gc_menu.add_item('q', xw->end(xw));
GC_Menu: Passing an new item..length = 72
[1] 66021 segmentation fault (core dumped) ./3D_scean
So what am I doing wrong? Why is there such a weird data written to instances of my gc_menu.items?
You've initialized gc_menu.items to new_mcl_items, i.e. a pointer to the function new_mcl_items (which should give you a warning since it is of type mcl_items *(*)(void) and not mcl_items *).
It looks like what you want is to actually call the function new_mcl_items() and set gc_menu.items to the value that new_mcl_items() returns. You can't do this with an initializer; initializers of global or static objects must be known at compile or link time. Standard C doesn't have "constructors".
So you'll have to remove the const from the declaration and definition of gc_menu, and add code to main (or some function called by main, etc) to initialize gc_menu.items at run time.
gc_menu.h:
extern __gc_menu gc_menu;
gc_menu.c:
__gc_menu gc_menu = {
gc_menu_add_item,
NULL // or whatever else you like
};
main.c or whatever you have called it:
int main(void) {
// ...
gc_menu.items = new_mcl_items();
// ...
}
I am passing queues like these between source files a.c and b.c
File : a.c
sq[a]=new_queue();
pthread_create(&st[a],NULL,sendPacket,sq[a]);
File : b.c
void *sendPacket(void *queue){
/* here i need to know which queue has come ,determine
the index of queue how can I do it? */
}
Create a more high-level representation of your queue. It seems the queue can be a void * (you're not showing its actual type, i.e. what does the new_queue() call return?), so embed that in a structure while adding the additional parameters:
struct queue_state {
void *queue;
int index;
};
Then instantiate a structure, and pass a pointer to it to the thread function:
struct queue_state qsa = malloc(sizeof *qsa);
if(qsa != NULL)
{
qsa->queue = new_queue();
qsa->index = 4711; /* or whatever */
pthread_create(&st[a], NULL, sendPacket, qsa);
}
Then the thread function can use the struct declaration to access all the fields. Of course, the declaration needs to be in a shared header (say queue.h) which is included from both C files.
Your question description is very rough. But at least from what I understand, you actually need to pass 2 parameters to your function: the (pointer to) queue (which seems an array for me), and the index within this queue.
You may not pack both your parameters in a single variable of type void*. What you may do is declare a struct with all the needed parameters, fill it, and pass a pointer to it to your thread.
Like this (error handling omitted):
struct Params
{
queue* m_Queue;
size_t m_Idx;
};
// ...
Params* pParams = new Params;
pParams->m_Queue = sq;
pParams->m_Idx = a;
pthread_create(&st[a],NULL,sendPacket, pParams);
void *sendPacket(void *pPtr)
{
Params* pParams = (Params*) pPtr;
// ...
delete pParams;
}
Probably it is easier if you just pass the index to the function:
void *sendPacket(int queue_idx) {
queue_t *queue = &sq[queue_idx];
}
If in b.c you have access to sq, you can just pass the index to the queue. Otherwise you can pass a struct containing the actual queue and the index
I have following query:
THis is my structure in some .h file
typedef struct
{
recUEInfo_t *recUEInfoPtr_t;
Int32 frameID;
Int32 slotIndx;
Int32 symNumber;
} recControlList;
If I do recControlList recControlListPtr; I can pass address to caller function
and collect it as a pointer in the definition
Fun(recControlListPtr);/* caller*/
and void Fun(*recControlListPtr);/* actual func*/
But if i do recControlList *recControlListPtr; then what should I do to
get the correct pointer?
Please help
I misunderstood who was the declarer and caller of the function initially, sorry about that, so if the function definition is:
Fun(recControlListPtr *precControlListPtr)
{
// Do stuff
}
You could call this way:
recControlListPtr rec1;
recContrlListPtr* prec2;
Fun(&rec1);
Fun(prec2);
Additional edit - My best guess at what I think you are trying to accomplish
typedef struct
{
recUEInfo_t *recUEInfoPtr_t;
int frameID;
int slotIndx;
int symNumber;
} recControlList;
void Fun(recControlList* pRecList)
{
ASSERT(pRecList != NULL);
int nFrameID = pRecList->frameID; // This line shows accessing the struct
// Do other stuff
}
recControlList rec1;
recControlList* pRec2 = &rec1;
Fun(&rec1);
Fun(pRec2);