1
    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.

user2398101
  • 339
  • 3
  • 9
  • 21

1 Answers1

2

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

Community
  • 1
  • 1
xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • line 153 : dll_states[link].data.wifi = dll_wifi_new_state(link, up_from_dll, true /* is_ds */); – user2398101 May 19 '13 at 03:43
  • struct dll_wifi_state *dll_wifi_new_state(int link, up_from_dll_fn_ty callback, bool is_ds) { if (link > nodeinfo.nlinks || linkinfo[link].linktype != LT_WLAN) return NULL; struct dll_wifi_state *state = calloc(1, sizeof(struct dll_wifi_state)); 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; } – user2398101 May 19 '13 at 03:45
  • dll_wifi_state calls the up_from_dll() to pass my packet from data link layer to next layer up. 'seq' number is to check whether the recieved msg is the expected msg or not before sending it up to the next layer. – user2398101 May 19 '13 at 03:59
  • Pretty sure I've answered the question. Been a while since I've dealt with function pointers so it took a while to figure this one out :) – xaxxon May 19 '13 at 04:02
  • No, there is no typedef for (*up_from_dll_fn_ty) anywhere in the whole project. To my understanding, up_from_dll_fn_ty nl_callback refers to up_from_dll() method. – user2398101 May 19 '13 at 09:02
  • `up_from_dll_fn_ty` isn't a built-in type, so it must be defined somewhere. You have to find where it's defined (if you're using a third-party library, then you likely can't change it, but it's there), and change it if possible, or you cannot add a parameter. – xaxxon May 19 '13 at 09:07
  • It was defined in one of the header files, which i wasn't expecting at all. Solved the issue. Thanks a lot :) – user2398101 May 19 '13 at 10:08
  • Glad we finally got to the bottom of all that :) If that solved your problem, can I have the green checkmark of satisfaction? :) – xaxxon May 19 '13 at 10:09
  • Definitely, u deserve it :) – user2398101 May 19 '13 at 10:18